Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / release / sysinstall / command.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last program in the `sysinstall' line - the next
5  * generation being essentially a complete rewrite.
6  *
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 $
9  *
10  * Copyright (c) 1995
11  *      Jordan Hubbard.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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
19  *    point in the file.
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.
23  *
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
34  * SUCH DAMAGE.
35  *
36  */
37
38 #include "sysinstall.h"
39
40 #define MAX_NUM_COMMANDS        10
41
42 typedef struct {
43     char key[FILENAME_MAX];
44     struct {
45         enum { CMD_SHELL, CMD_FUNCTION } type;
46         void *ptr, *data;
47     } cmds[MAX_NUM_COMMANDS];
48     int ncmds;
49 } Command;
50
51 #define MAX_CMDS        200
52 static Command *commandStack[MAX_CMDS];
53 int numCommands;
54
55 /* Nuke the command stack */
56 void
57 command_clear(void)
58 {
59     int i, j;
60
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]);
66     numCommands = 0;
67 }
68
69 static void
70 addit(char *key, int type, void *cmd, void *data)
71 {
72     int i;
73
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);
83             return;
84         }
85     }
86     if (numCommands == MAX_CMDS)
87         msgFatal("More than %d commands accumulated??", MAX_CMDS);
88
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;
96     ++numCommands;
97 }
98
99 /* Add a shell command under a given key */
100 void
101 command_shell_add(char *key, char *fmt, ...)
102 {
103     va_list args;
104     char *cmd;
105
106     cmd = (char *)safe_malloc(256);
107     va_start(args, fmt);
108     vsnprintf(cmd, 256, fmt, args);
109     va_end(args);
110
111     addit(key, CMD_SHELL, cmd, NULL);
112 }
113
114 /* Add a shell command under a given key */
115 void
116 command_func_add(char *key, commandFunc func, void *data)
117 {
118     addit(key, CMD_FUNCTION, func, data);
119 }
120
121 static int
122 sort_compare(Command *p1, Command *p2)
123 {
124     if (!p1 && !p2)
125         return 0;
126     else if (!p1 && p2) /* NULL has a "greater" value for commands */
127         return 1;
128     else if (p1 && !p2)
129         return -1;
130     else
131         return strcmp(p1->key, p2->key);
132 }
133
134 void
135 command_sort(void)
136 {
137     int i, j;
138
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];
145
146                 commandStack[j] = commandStack[j + 1];
147                 commandStack[j + 1] = tmp;
148             }
149         }
150     }
151 }
152
153 /* Run all accumulated commands in sorted order */
154 void
155 command_execute(void)
156 {
157     int i, j, ret;
158     commandFunc func;
159
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);
166                 if (isDebug())
167                     msgDebug("Command `%s' returns status %d\n", 
168                         (char *)commandStack[i]->cmds[j].ptr, ret);
169             }
170             else {
171                 /* It's a function pointer - call it with the key and
172                    the data */
173                 func = (commandFunc)commandStack[i]->cmds[j].ptr;
174                 if (isDebug())
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);
179                 if (isDebug())
180                     msgDebug("Function @ %p returns status %d\n", 
181                         commandStack[i]->cmds[j].ptr, ret);
182             }
183         }
184     }
185 }