5 * Jordan Hubbard. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * verbatim and that no modifications are made prior to this
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #define MAX_NUM_COMMANDS 10
37 char key[FILENAME_MAX];
39 enum { CMD_SHELL, CMD_FUNCTION } type;
41 } cmds[MAX_NUM_COMMANDS];
46 static Command *commandStack[MAX_CMDS];
49 /* Nuke the command stack */
55 for (i = 0; i < numCommands; i++)
56 for (j = 0; j < commandStack[i]->ncmds; j++)
57 if (commandStack[i]->cmds[j].type == CMD_SHELL)
58 free(commandStack[i]->cmds[j].ptr);
59 free(commandStack[i]);
64 addit(char *key, int type, void *cmd, void *data)
68 /* First, look for the key already present and add a command to it if found */
69 for (i = 0; i < numCommands; i++) {
70 if (!strcmp(commandStack[i]->key, key)) {
71 if (commandStack[i]->ncmds == MAX_NUM_COMMANDS)
72 msgFatal("More than %d commands stacked up behind %s??", MAX_NUM_COMMANDS, key);
73 commandStack[i]->cmds[commandStack[i]->ncmds].type = type;
74 commandStack[i]->cmds[commandStack[i]->ncmds].ptr = cmd;
75 commandStack[i]->cmds[commandStack[i]->ncmds].data = data;
76 ++(commandStack[i]->ncmds);
80 if (numCommands == MAX_CMDS)
81 msgFatal("More than %d commands accumulated??", MAX_CMDS);
83 /* If we fell to here, it's a new key */
84 commandStack[numCommands] = safe_malloc(sizeof(Command));
85 strcpy(commandStack[numCommands]->key, key);
86 commandStack[numCommands]->ncmds = 1;
87 commandStack[numCommands]->cmds[0].type = type;
88 commandStack[numCommands]->cmds[0].ptr = cmd;
89 commandStack[numCommands]->cmds[0].data = data;
93 /* Add a shell command under a given key */
95 command_shell_add(char *key, const char *fmt, ...)
100 cmd = (char *)safe_malloc(256);
102 vsnprintf(cmd, 256, fmt, args);
105 addit(key, CMD_SHELL, cmd, NULL);
108 /* Add a shell command under a given key */
110 command_func_add(char *key, commandFunc func, void *data)
112 addit(key, CMD_FUNCTION, func, data);
116 sort_compare(Command *p1, Command *p2)
120 else if (!p1 && p2) /* NULL has a "greater" value for commands */
125 return strcmp(p1->key, p2->key);
133 commandStack[numCommands] = NULL;
134 /* Just do a crude bubble sort since the list is small */
135 for (i = 0; i < numCommands; i++) {
136 for (j = 0; j < numCommands; j++) {
137 if (sort_compare(commandStack[j], commandStack[j + 1]) > 0) {
138 Command *tmp = commandStack[j];
140 commandStack[j] = commandStack[j + 1];
141 commandStack[j + 1] = tmp;
147 /* Run all accumulated commands in sorted order */
149 command_execute(void)
154 for (i = 0; i < numCommands; i++) {
155 for (j = 0; j < commandStack[i]->ncmds; j++) {
156 /* If it's a shell command, run system on it */
157 if (commandStack[i]->cmds[j].type == CMD_SHELL) {
158 msgNotify("Doing %s", (char *)commandStack[i]->cmds[j].ptr);
159 ret = vsystem("%s", (char *)commandStack[i]->cmds[j].ptr);
161 msgDebug("Command `%s' returns status %d\n",
162 (char *)commandStack[i]->cmds[j].ptr, ret);
165 /* It's a function pointer - call it with the key and
167 func = (commandFunc)commandStack[i]->cmds[j].ptr;
169 msgDebug("%p: Execute(%s, %s)\n",
170 func, commandStack[i]->key,
171 (char *)commandStack[i]->cmds[j].data);
172 ret = (*func)(commandStack[i]->key, commandStack[i]->cmds[j].data);
174 msgDebug("Function @ %p returns status %d\n",
175 commandStack[i]->cmds[j].ptr, ret);