2 * The new sysinstall program.
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
10 * Jordan Hubbard. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer,
17 * verbatim and that no modifications are made prior to this
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "sysinstall.h"
39 #define MAX_NUM_COMMANDS 10
42 char key[FILENAME_MAX];
44 enum { CMD_SHELL, CMD_FUNCTION } type;
46 } cmds[MAX_NUM_COMMANDS];
51 static Command *commandStack[MAX_CMDS];
54 /* Nuke the command stack */
60 for (i = 0; i < numCommands; i++)
61 for (j = 0; j < commandStack[i]->ncmds; j++)
62 if (commandStack[i]->cmds[j].type == CMD_SHELL)
63 free(commandStack[i]->cmds[j].ptr);
64 free(commandStack[i]);
69 addit(char *key, int type, void *cmd, void *data)
73 /* First, look for the key already present and add a command to it if found */
74 for (i = 0; i < numCommands; i++) {
75 if (!strcmp(commandStack[i]->key, key)) {
76 if (commandStack[i]->ncmds == MAX_NUM_COMMANDS)
77 msgFatal("More than %d commands stacked up behind %s??", MAX_NUM_COMMANDS, key);
78 commandStack[i]->cmds[commandStack[i]->ncmds].type = type;
79 commandStack[i]->cmds[commandStack[i]->ncmds].ptr = cmd;
80 commandStack[i]->cmds[commandStack[i]->ncmds].data = data;
81 ++(commandStack[i]->ncmds);
85 if (numCommands == MAX_CMDS)
86 msgFatal("More than %d commands accumulated??", MAX_CMDS);
88 /* If we fell to here, it's a new key */
89 commandStack[numCommands] = safe_malloc(sizeof(Command));
90 strcpy(commandStack[numCommands]->key, key);
91 commandStack[numCommands]->ncmds = 1;
92 commandStack[numCommands]->cmds[0].type = type;
93 commandStack[numCommands]->cmds[0].ptr = cmd;
94 commandStack[numCommands]->cmds[0].data = data;
98 /* Add a shell command under a given key */
100 command_shell_add(char *key, char *fmt, ...)
105 cmd = (char *)safe_malloc(256);
107 vsnprintf(cmd, 256, fmt, args);
110 addit(key, CMD_SHELL, cmd, NULL);
113 /* Add a shell command under a given key */
115 command_func_add(char *key, commandFunc func, void *data)
117 addit(key, CMD_FUNCTION, func, data);
121 sort_compare(Command *p1, Command *p2)
125 else if (!p1 && p2) /* NULL has a "greater" value for commands */
130 return strcmp(p1->key, p2->key);
138 commandStack[numCommands] = NULL;
139 /* Just do a crude bubble sort since the list is small */
140 for (i = 0; i < numCommands; i++) {
141 for (j = 0; j < numCommands; j++) {
142 if (sort_compare(commandStack[j], commandStack[j + 1]) > 0) {
143 Command *tmp = commandStack[j];
145 commandStack[j] = commandStack[j + 1];
146 commandStack[j + 1] = tmp;
152 /* Run all accumulated commands in sorted order */
154 command_execute(void)
159 for (i = 0; i < numCommands; i++) {
160 for (j = 0; j < commandStack[i]->ncmds; j++) {
161 /* If it's a shell command, run system on it */
162 if (commandStack[i]->cmds[j].type == CMD_SHELL) {
163 msgNotify("Doing %s", (char *)commandStack[i]->cmds[j].ptr);
164 ret = vsystem("%s", (char *)commandStack[i]->cmds[j].ptr);
166 msgDebug("Command `%s' returns status %d\n",
167 (char *)commandStack[i]->cmds[j].ptr, ret);
170 /* It's a function pointer - call it with the key and
172 func = (commandFunc)commandStack[i]->cmds[j].ptr;
174 msgDebug("%p: Execute(%s, %s)\n",
175 func, commandStack[i]->key,
176 (char *)commandStack[i]->cmds[j].data);
177 ret = (*func)(commandStack[i]->key, commandStack[i]->cmds[j].data);
179 msgDebug("Function @ %p returns status %d\n",
180 commandStack[i]->cmds[j].ptr, ret);