]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/sysinstall/command.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / 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$
8  *
9  * Copyright (c) 1995
10  *      Jordan Hubbard.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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
18  *    point in the file.
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  */
36
37 #include "sysinstall.h"
38
39 #define MAX_NUM_COMMANDS        10
40
41 typedef struct {
42     char key[FILENAME_MAX];
43     struct {
44         enum { CMD_SHELL, CMD_FUNCTION } type;
45         void *ptr, *data;
46     } cmds[MAX_NUM_COMMANDS];
47     int ncmds;
48 } Command;
49
50 #define MAX_CMDS        200
51 static Command *commandStack[MAX_CMDS];
52 int numCommands;
53
54 /* Nuke the command stack */
55 void
56 command_clear(void)
57 {
58     int i, j;
59
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]);
65     numCommands = 0;
66 }
67
68 static void
69 addit(char *key, int type, void *cmd, void *data)
70 {
71     int i;
72
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);
82             return;
83         }
84     }
85     if (numCommands == MAX_CMDS)
86         msgFatal("More than %d commands accumulated??", MAX_CMDS);
87
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;
95     ++numCommands;
96 }
97
98 /* Add a shell command under a given key */
99 void
100 command_shell_add(char *key, char *fmt, ...)
101 {
102     va_list args;
103     char *cmd;
104
105     cmd = (char *)safe_malloc(256);
106     va_start(args, fmt);
107     vsnprintf(cmd, 256, fmt, args);
108     va_end(args);
109
110     addit(key, CMD_SHELL, cmd, NULL);
111 }
112
113 /* Add a shell command under a given key */
114 void
115 command_func_add(char *key, commandFunc func, void *data)
116 {
117     addit(key, CMD_FUNCTION, func, data);
118 }
119
120 static int
121 sort_compare(Command *p1, Command *p2)
122 {
123     if (!p1 && !p2)
124         return 0;
125     else if (!p1 && p2) /* NULL has a "greater" value for commands */
126         return 1;
127     else if (p1 && !p2)
128         return -1;
129     else
130         return strcmp(p1->key, p2->key);
131 }
132
133 void
134 command_sort(void)
135 {
136     int i, j;
137
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];
144
145                 commandStack[j] = commandStack[j + 1];
146                 commandStack[j + 1] = tmp;
147             }
148         }
149     }
150 }
151
152 /* Run all accumulated commands in sorted order */
153 void
154 command_execute(void)
155 {
156     int i, j, ret;
157     commandFunc func;
158
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);
165                 if (isDebug())
166                     msgDebug("Command `%s' returns status %d\n", 
167                         (char *)commandStack[i]->cmds[j].ptr, ret);
168             }
169             else {
170                 /* It's a function pointer - call it with the key and
171                    the data */
172                 func = (commandFunc)commandStack[i]->cmds[j].ptr;
173                 if (isDebug())
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);
178                 if (isDebug())
179                     msgDebug("Function @ %p returns status %d\n", 
180                         commandStack[i]->cmds[j].ptr, ret);
181             }
182         }
183     }
184 }