added driver list function

This commit is contained in:
zolliker
2006-08-17 15:37:32 +00:00
parent 9fb7be2ecb
commit 7c5595c073
2 changed files with 53 additions and 10 deletions

View File

@ -12,8 +12,9 @@ Markus Zolliker, March 2005
typedef struct Item { typedef struct Item {
struct Item *next; struct Item *next;
const char *type; char *type;
const char *name; char *name;
char *desc;
Initializer maker; Initializer maker;
int startupOnly; int startupOnly;
} Item; } Item;
@ -21,15 +22,17 @@ typedef struct Item {
static Item *list = NULL; static Item *list = NULL;
static int startup = 1; static int startup = 1;
void MakeInitializer(const char *type, const char *name, Initializer maker, int startupOnly) { void MakeInitializer(const char *type, const char *name, Initializer maker,
int startupOnly, const char *desc) {
Item *item; Item *item;
item = calloc(1, sizeof *item); item = calloc(1, sizeof *item);
assert(item); assert(item);
item->maker = maker; item->maker = maker;
item->next = list; item->next = list;
item->type = type; item->type = strdup(type);
item->name = name; item->name = strdup(name);
item->desc = strdup(desc);
item->startupOnly = startupOnly; item->startupOnly = startupOnly;
list = item; list = item;
} }
@ -84,6 +87,40 @@ static int MakeObject(SConnection *con, SicsInterp *sics,
} }
} }
static int DriverList(SConnection *con, SicsInterp *sics,
void *data, int argc, char *argv[]) {
Item *p;
char *name, *type;
if (argc < 2 || strcasecmp(argv[1], "list") == 0) {
for (p = list; p != NULL; p = p->next) {
if (argc < 3) {
SCPrintf(con, eStatus, "%s %s %s", p->type, p->name, p->desc);
} else if (strcasecmp(argv[2], p->type) == 0) {
SCPrintf(con, eStatus, "%s %s", p->name, p->desc);
}
}
} else {
if (argc == 2) {
name = argv[1];
type = "Object";
} else {
name = argv[2];
type = argv[1];
}
p = list;
while (p != NULL && (strcasecmp(p->type, type) != 0 || strcasecmp(p->name, name) != 0)) {
p = p->next;
}
if (p) {
SCPrintf(con, eValue, "%s", p->desc);
} else {
SCPrintf(con, eValue, "notfound");
}
}
return 1;
}
static int RemoveObject(SConnection *con, SicsInterp *sics, static int RemoveObject(SConnection *con, SicsInterp *sics,
void *data, int argc, char *argv[]) { void *data, int argc, char *argv[]) {
CmdInitializer cmdin; CmdInitializer cmdin;
@ -91,7 +128,7 @@ static int RemoveObject(SConnection *con, SicsInterp *sics,
char *className; char *className;
if (argc != 2) { if (argc != 2) {
SCPrintf(con, eError, "%s has 1 argument", argv[0]); SCPrintf(con, eError, "%s needs 1 argument", argv[0]);
return 0; return 0;
} }
@ -124,16 +161,20 @@ static void KillInitializers(void *data) {
item = list; item = list;
while (item) { while (item) {
next = item->next; next = item->next;
if (item->name) free(item->name);
if (item->type) free(item->type);
if (item->desc) free(item->desc);
free(item); free(item);
item = next; item = next;
} }
list = NULL; list = NULL;
} }
void MakeDriver(const char *driver, CmdInitializer maker, int startupOnly) { void MakeDriver(const char *driver, CmdInitializer maker, int startupOnly, const char *desc) {
if (! FindCommand(pServ->pSics, "MakeObject")) { if (! FindCommand(pServ->pSics, "MakeObject")) {
AddCommandWithFlag(pServ->pSics, "MakeObject", MakeObject, KillInitializers, NULL, 0); AddCommandWithFlag(pServ->pSics, "MakeObject", MakeObject, KillInitializers, NULL, 0);
AddCommandWithFlag(pServ->pSics, "RemoveObject", RemoveObject, NULL, NULL, 0); AddCommandWithFlag(pServ->pSics, "RemoveObject", RemoveObject, NULL, NULL, 0);
AddCommandWithFlag(pServ->pSics, "DriverList", DriverList, NULL, NULL, 0);
} }
MakeInitializer("Object", driver, (Initializer)maker, startupOnly); MakeInitializer("Object", driver, (Initializer)maker, startupOnly, desc);
} }

View File

@ -12,7 +12,8 @@ Markus Zolliker, March 2005
typedef void (*Initializer)(void); typedef void (*Initializer)(void);
void MakeInitializer(const char *type, const char *name, Initializer maker, int startupOnly); void MakeInitializer(const char *type, const char *name, Initializer maker, int startupOnly,
const char *desc);
/* /*
install an initializer install an initializer
@ -54,7 +55,8 @@ typedef int (*CmdInitializer) (SConnection *pCon, int argc, char *argv[], int dy
- dynamic: the initializer was called _after_ startup - dynamic: the initializer was called _after_ startup
*/ */
void MakeDriver(const char *driver, CmdInitializer maker, int startupOnly); void MakeDriver(const char *driver, CmdInitializer maker, int startupOnly,
const char *desc);
/* /*
Install a driver of type "Object" with the initializer function maker. Install a driver of type "Object" with the initializer function maker.
- startupOnly: the driver creation should only be possible at startup - startupOnly: the driver creation should only be possible at startup