- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
133
initializer.c
133
initializer.c
@@ -13,9 +13,9 @@ Markus Zolliker, March 2005
|
||||
|
||||
typedef struct Item {
|
||||
struct Item *next;
|
||||
char *type; /* "Object" for all commands created by makeobject, else something more general */
|
||||
char *name; /* the name for identifying an initializer */
|
||||
char *desc; /* a description of the initializer. not the same as pObjectDescriptor->name */
|
||||
char *type; /* "Object" for all commands created by makeobject, else something more general */
|
||||
char *name; /* the name for identifying an initializer */
|
||||
char *desc; /* a description of the initializer. not the same as pObjectDescriptor->name */
|
||||
Initializer maker;
|
||||
int startupOnly;
|
||||
} Item;
|
||||
@@ -24,9 +24,10 @@ static Item *list = NULL;
|
||||
static int startup = 1;
|
||||
|
||||
void MakeInitializer(const char *type, const char *name, Initializer maker,
|
||||
int startupOnly, const char *desc) {
|
||||
int startupOnly, const char *desc)
|
||||
{
|
||||
Item *item;
|
||||
|
||||
|
||||
item = calloc(1, sizeof *item);
|
||||
assert(item);
|
||||
item->maker = maker;
|
||||
@@ -38,9 +39,10 @@ void MakeInitializer(const char *type, const char *name, Initializer maker,
|
||||
list = item;
|
||||
}
|
||||
|
||||
Initializer GetInitializer(const char *type, const char *name) {
|
||||
Initializer GetInitializer(const char *type, const char *name)
|
||||
{
|
||||
Item *p, **last;
|
||||
|
||||
|
||||
if (startup && !ServerIsStarting(pServ)) {
|
||||
/* pServ->pReader exists: startup finished */
|
||||
startup = 0;
|
||||
@@ -59,36 +61,39 @@ Initializer GetInitializer(const char *type, const char *name) {
|
||||
}
|
||||
}
|
||||
for (p = list; p != NULL; p = p->next) {
|
||||
if (strcasecmp(p->name, name) == 0 && strcasecmp(p->type, type) == 0) {
|
||||
if (strcasecmp(p->name, name) == 0 && strcasecmp(p->type, type) == 0) {
|
||||
return p->maker;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int MakeObject(SConnection *con, SicsInterp *sics,
|
||||
void *data, int argc, char *argv[]) {
|
||||
static int MakeObject(SConnection * con, SicsInterp * sics,
|
||||
void *data, int argc, char *argv[])
|
||||
{
|
||||
CmdInitializer cmdin;
|
||||
|
||||
|
||||
if (argc < 3) {
|
||||
SCPrintf(con, eError, "ERROR: should be: %s <object> <type> ...", argv[0]);
|
||||
SCPrintf(con, eError, "ERROR: should be: %s <object> <type> ...",
|
||||
argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cmdin = (CmdInitializer)GetInitializer("Object", argv[2]);
|
||||
|
||||
cmdin = (CmdInitializer) GetInitializer("Object", argv[2]);
|
||||
if (cmdin) {
|
||||
return cmdin(con, argc, argv, strcasecmp(argv[0],"makeobject") == 0);
|
||||
return cmdin(con, argc, argv, strcasecmp(argv[0], "makeobject") == 0);
|
||||
} else {
|
||||
SCPrintf(con, eError, "do not know how to make a %s object", argv[2]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int DriverList(SConnection *con, SicsInterp *sics,
|
||||
void *data, int argc, char *argv[]) {
|
||||
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) {
|
||||
@@ -106,7 +111,9 @@ static int DriverList(SConnection *con, SicsInterp *sics,
|
||||
type = argv[1];
|
||||
}
|
||||
p = list;
|
||||
while (p != NULL && (strcasecmp(p->type, type) != 0 || strcasecmp(p->name, name) != 0)) {
|
||||
while (p != NULL
|
||||
&& (strcasecmp(p->type, type) != 0
|
||||
|| strcasecmp(p->name, name) != 0)) {
|
||||
p = p->next;
|
||||
}
|
||||
if (p) {
|
||||
@@ -118,8 +125,9 @@ static int DriverList(SConnection *con, SicsInterp *sics,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int RemoveObject(SConnection *con, SicsInterp *sics,
|
||||
void *data, int argc, char *argv[]) {
|
||||
static int RemoveObject(SConnection * con, SicsInterp * sics,
|
||||
void *data, int argc, char *argv[])
|
||||
{
|
||||
CmdInitializer cmdin;
|
||||
ObjectDescriptor *desc;
|
||||
char *className;
|
||||
@@ -127,18 +135,18 @@ static int RemoveObject(SConnection *con, SicsInterp *sics,
|
||||
char *p;
|
||||
int removeAllowed;
|
||||
char *creationCommand;
|
||||
|
||||
|
||||
if (argc != 2) {
|
||||
SCPrintf(con, eError, "ERROR: should be: %s <object>", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
desc = FindCommandDescriptor(sics, argv[1]);
|
||||
if (!desc) {
|
||||
SCPrintf(con, eError, "ERROR: %s not found", argv[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
creationCommand = GetDescriptorKey(desc, "creationCommand");
|
||||
if (creationCommand != NULL) {
|
||||
/* if there is a creationCommand, we are allowed to remove */
|
||||
@@ -146,20 +154,22 @@ static int RemoveObject(SConnection *con, SicsInterp *sics,
|
||||
} else {
|
||||
/* if we have an initializer: we are also allowed to remove */
|
||||
className = desc->name;
|
||||
cmdin = (CmdInitializer)GetInitializer("Object", className);
|
||||
cmdin = (CmdInitializer) GetInitializer("Object", className);
|
||||
if (cmdin == 0) {
|
||||
/* allow also a longer descriptor starting with the initializer name and a blank */
|
||||
p = strchr(className, ' ');
|
||||
if (p) {
|
||||
snprintf(shortClassName, sizeof shortClassName, "%.*s", p - className, className);
|
||||
cmdin = (CmdInitializer)GetInitializer("Object", shortClassName);
|
||||
snprintf(shortClassName, sizeof shortClassName, "%.*s",
|
||||
p - className, className);
|
||||
cmdin = (CmdInitializer) GetInitializer("Object", shortClassName);
|
||||
}
|
||||
}
|
||||
removeAllowed = (cmdin != NULL);
|
||||
}
|
||||
if (removeAllowed) {
|
||||
if (pServ->pExecutor && isInRunMode(pServ->pExecutor)) {
|
||||
SCPrintf(con, eError, "ERROR: cannot remove %s while running", argv[1]);
|
||||
SCPrintf(con, eError, "ERROR: cannot remove %s while running",
|
||||
argv[1]);
|
||||
return 0;
|
||||
}
|
||||
SCPrintf(con, eValue, "remove %s", argv[1]);
|
||||
@@ -176,11 +186,13 @@ typedef struct {
|
||||
FILE *fil;
|
||||
} SaveData;
|
||||
|
||||
static int SaveCreationCommand(char *name, pDummy object, void *userData) {
|
||||
static int SaveCreationCommand(char *name, pDummy object, void *userData)
|
||||
{
|
||||
SaveData *saveData = userData;
|
||||
char *creationCommand;
|
||||
|
||||
creationCommand = GetDescriptorKey(object->pDescriptor, "creationCommand");
|
||||
|
||||
creationCommand =
|
||||
GetDescriptorKey(object->pDescriptor, "creationCommand");
|
||||
if (creationCommand && strcmp(creationCommand, "0") != 0) {
|
||||
if (saveData->printHeader == 0) {
|
||||
saveData->printHeader = 1;
|
||||
@@ -191,9 +203,10 @@ static int SaveCreationCommand(char *name, pDummy object, void *userData) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int SaveCreationCommands(void *object, char *name, FILE *fil) {
|
||||
static int SaveCreationCommands(void *object, char *name, FILE * fil)
|
||||
{
|
||||
SaveData saveData;
|
||||
|
||||
|
||||
saveData.fil = fil;
|
||||
saveData.printHeader = 0;
|
||||
ForEachCommand(SaveCreationCommand, &saveData);
|
||||
@@ -203,8 +216,9 @@ static int SaveCreationCommands(void *object, char *name, FILE *fil) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int CreationCommand(SConnection *con, SicsInterp *sics,
|
||||
void *data, int argc, char *argv[]) {
|
||||
static int CreationCommand(SConnection * con, SicsInterp * sics,
|
||||
void *data, int argc, char *argv[])
|
||||
{
|
||||
CmdInitializer cmdin;
|
||||
char *className;
|
||||
char shortClassName[32];
|
||||
@@ -213,12 +227,14 @@ static int CreationCommand(SConnection *con, SicsInterp *sics,
|
||||
ObjectDescriptor *desc;
|
||||
char *creationCommand;
|
||||
char buf[256];
|
||||
|
||||
|
||||
if (argc < 2) {
|
||||
SCPrintf(con, eError, "ERROR: should be: %s <object> [<creation command>]", argv[0]);
|
||||
SCPrintf(con, eError,
|
||||
"ERROR: should be: %s <object> [<creation command>]",
|
||||
argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
desc = FindCommandDescriptor(sics, argv[1]);
|
||||
if (!desc) {
|
||||
SCPrintf(con, eError, "ERROR: %s not found", argv[1]);
|
||||
@@ -239,7 +255,8 @@ static int CreationCommand(SConnection *con, SicsInterp *sics,
|
||||
creationCommand = Arg2Tcl(argc - 2, argv + 2, buf, sizeof buf);
|
||||
if (creationCommand) {
|
||||
SetDescriptorKey(desc, "creationCommand", creationCommand);
|
||||
if (creationCommand != buf) free(creationCommand);
|
||||
if (creationCommand != buf)
|
||||
free(creationCommand);
|
||||
} else {
|
||||
SetDescriptorKey(desc, "creationCommand", "0");
|
||||
}
|
||||
@@ -247,34 +264,46 @@ static int CreationCommand(SConnection *con, SicsInterp *sics,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void KillInitializers(void *data) {
|
||||
static void KillInitializers(void *data)
|
||||
{
|
||||
KillDummy(data);
|
||||
Item *item, *next;
|
||||
|
||||
|
||||
item = list;
|
||||
while (item) {
|
||||
next = item->next;
|
||||
if (item->name) free(item->name);
|
||||
if (item->type) free(item->type);
|
||||
if (item->desc) free(item->desc);
|
||||
if (item->name)
|
||||
free(item->name);
|
||||
if (item->type)
|
||||
free(item->type);
|
||||
if (item->desc)
|
||||
free(item->desc);
|
||||
free(item);
|
||||
item = next;
|
||||
}
|
||||
list = NULL;
|
||||
}
|
||||
|
||||
void MakeDriver(const char *driver, CmdInitializer maker, int startupOnly, const char *desc) {
|
||||
MakeInitializer("Object", driver, (Initializer)maker, startupOnly, desc);
|
||||
void MakeDriver(const char *driver, CmdInitializer maker, int startupOnly,
|
||||
const char *desc)
|
||||
{
|
||||
MakeInitializer("Object", driver, (Initializer) maker, startupOnly,
|
||||
desc);
|
||||
}
|
||||
|
||||
void InitializerInit(void) {
|
||||
void InitializerInit(void)
|
||||
{
|
||||
pDummy cc = NULL;
|
||||
AddCommandWithFlag(pServ->pSics, "MakeObject", MakeObject, KillInitializers, NULL, 0);
|
||||
AddCommandWithFlag(pServ->pSics, "MakeStaticObject", MakeObject, NULL, NULL, 0);
|
||||
AddCommandWithFlag(pServ->pSics, "RemoveObject", RemoveObject, NULL, NULL, 0);
|
||||
AddCommandWithFlag(pServ->pSics, "DriverList", DriverList, NULL, NULL, 0);
|
||||
AddCommandWithFlag(pServ->pSics, "MakeObject", MakeObject,
|
||||
KillInitializers, NULL, 0);
|
||||
AddCommandWithFlag(pServ->pSics, "MakeStaticObject", MakeObject, NULL,
|
||||
NULL, 0);
|
||||
AddCommandWithFlag(pServ->pSics, "RemoveObject", RemoveObject, NULL,
|
||||
NULL, 0);
|
||||
AddCommandWithFlag(pServ->pSics, "DriverList", DriverList, NULL, NULL,
|
||||
0);
|
||||
cc = CreateDummy("creation commands");
|
||||
cc->pDescriptor->SaveStatus = SaveCreationCommands;
|
||||
AddCommandWithFlag(pServ->pSics, "CreationCommand", CreationCommand,
|
||||
KillDummy, cc, 0);
|
||||
KillDummy, cc, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user