The command stack and how to load it

Previous: xml_read: Using expat to parse XML files into memory ] [ Top: wftk core index ] [ Next: Our oh-so-complex output stream ]

The term "command stack" is probably a misnomer. What the structure really is, is a list of things to do to the process. Generally these things to do are task transitions (tasks have been activated or completed) -- so I had thought to call these transitions. But then another command is to start a project, and then yet another is to set a value in a datasheet (which should be done through the workflow engine to allow thread-safe usage, later.)

So I finally decided that commands were what they are. Sorry for the cheesy limit of 5 arguments to a command, but I really don't want to get into a double-malloc situation for a totally throwaway data structure.
 
typedef struct _command COMMAND;
struct _command {
   char * name;
   int    argc;
   char * argv[5];
   COMMAND * next;
};
The next member, of course, is to allow commands to be strung together into a linked list. Note that this "command language" is really simple, because the "commands" are really notifications from the task manager that something has happened which the engine needs to act upon. Maybe "messages" would be a better terminology -- but nope, they're commands now. Live with it.

Well, so the other thing we want to define here is a handy little function to tack a command onto a list of commands.
 
COMMAND * command_add (COMMAND * list, char * name, int argc, ...)
{
   COMMAND * cmd;
   int i;
   va_list argv;

   va_start(argv, argc);
   cmd = (COMMAND *) malloc (sizeof (struct _command));
   cmd->name = name;
   cmd->argc = argc;
   for (i=0; i < argc; i++) {
      cmd->argv[i] = va_arg(argv, char *);
   }
   va_end(argv);
   cmd->next = NULL;

   if (!list) return (cmd);

   while (list->next != NULL) list = list->next;
   list->next = cmd;

   return cmd;
}
Oh, and we'll want a function to free up lists, too:
 
void command_freelist (COMMAND * list)
{
   COMMAND * cmd;

   while (list) {
      cmd = list;
      list = list->next;
      free ((void *) cmd);
   }
}
And I guess one more function, this one to take a file stream and load up a command list from it. The version I have here is rather crude but we'll presumably improve on it later.
 
void command_load (COMMAND * list, FILE * file)
{
   
}
Oh. Well, that's even cruder than I had intended, heh, but I'll get to it soon enough.
Previous: xml_read: Using expat to parse XML files into memory ] [ Top: wftk core index ] [ Next: Our oh-so-complex output stream ]


This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2000, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license.