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.
7 * $FreeBSD: src/release/sysinstall/command.c,v 1.17.2.2 2002/07/02 21:25:56 jhb Exp $
8 * $DragonFly: src/release/sysinstall/Attic/command.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
11 * Jordan Hubbard. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer,
18 * verbatim and that no modifications are made prior to this
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "sysinstall.h"
40 #define MAX_NUM_COMMANDS 10
43 char key[FILENAME_MAX];
45 enum { CMD_SHELL, CMD_FUNCTION } type;
47 } cmds[MAX_NUM_COMMANDS];
52 static Command *commandStack[MAX_CMDS];
55 /* Nuke the command stack */
61 for (i = 0; i < numCommands; i++)
62 for (j = 0; j < commandStack[i]->ncmds; j++)
63 if (commandStack[i]->cmds[j].type == CMD_SHELL)
64 free(commandStack[i]->cmds[j].ptr);
65 free(commandStack[i]);
70 addit(char *key, int type, void *cmd, void *data)
74 /* First, look for the key already present and add a command to it if found */
75 for (i = 0; i < numCommands; i++) {
76 if (!strcmp(commandStack[i]->key, key)) {
77 if (commandStack[i]->ncmds == MAX_NUM_COMMANDS)
78 msgFatal("More than %d commands stacked up behind %s??", MAX_NUM_COMMANDS, key);
79 commandStack[i]->cmds[commandStack[i]->ncmds].type = type;
80 commandStack[i]->cmds[commandStack[i]->ncmds].ptr = cmd;
81 commandStack[i]->cmds[commandStack[i]->ncmds].data = data;
82 ++(commandStack[i]->ncmds);
86 if (numCommands == MAX_CMDS)
87 msgFatal("More than %d commands accumulated??", MAX_CMDS);
89 /* If we fell to here, it's a new key */
90 commandStack[numCommands] = safe_malloc(sizeof(Command));
91 strcpy(commandStack[numCommands]->key, key);
92 commandStack[numCommands]->ncmds = 1;
93 commandStack[numCommands]->cmds[0].type = type;
94 commandStack[numCommands]->cmds[0].ptr = cmd;
95 commandStack[numCommands]->cmds[0].data = data;
99 /* Add a shell command under a given key */
101 command_shell_add(char *key, char *fmt, ...)
106 cmd = (char *)safe_malloc(256);
108 vsnprintf(cmd, 256, fmt, args);
111 addit(key, CMD_SHELL, cmd, NULL);
114 /* Add a shell command under a given key */
116 command_func_add(char *key, commandFunc func, void *data)
118 addit(key, CMD_FUNCTION, func, data);
122 sort_compare(Command *p1, Command *p2)
126 else if (!p1 && p2) /* NULL has a "greater" value for commands */
131 return strcmp(p1->key, p2->key);
139 commandStack[numCommands] = NULL;
140 /* Just do a crude bubble sort since the list is small */
141 for (i = 0; i < numCommands; i++) {
142 for (j = 0; j < numCommands; j++) {
143 if (sort_compare(commandStack[j], commandStack[j + 1]) > 0) {
144 Command *tmp = commandStack[j];
146 commandStack[j] = commandStack[j + 1];
147 commandStack[j + 1] = tmp;
153 /* Run all accumulated commands in sorted order */
155 command_execute(void)
160 for (i = 0; i < numCommands; i++) {
161 for (j = 0; j < commandStack[i]->ncmds; j++) {
162 /* If it's a shell command, run system on it */
163 if (commandStack[i]->cmds[j].type == CMD_SHELL) {
164 msgNotify("Doing %s", (char *)commandStack[i]->cmds[j].ptr);
165 ret = vsystem("%s", (char *)commandStack[i]->cmds[j].ptr);
167 msgDebug("Command `%s' returns status %d\n",
168 (char *)commandStack[i]->cmds[j].ptr, ret);
171 /* It's a function pointer - call it with the key and
173 func = (commandFunc)commandStack[i]->cmds[j].ptr;
175 msgDebug("%p: Execute(%s, %s)",
176 func, commandStack[i]->key,
177 (char *)commandStack[i]->cmds[j].data);
178 ret = (*func)(commandStack[i]->key, commandStack[i]->cmds[j].data);
180 msgDebug("Function @ %p returns status %d\n",
181 commandStack[i]->cmds[j].ptr, ret);