dispatch.c revision 29222
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 * $Id: dispatch.c,v 1.21 1997/08/11 13:08:18 jkh Exp $
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#include <ctype.h>
39
40static int _shutdown(dialogMenuItem *unused);
41static int _systemExecute(dialogMenuItem *unused);
42static int _loadConfig(dialogMenuItem *unused);
43
44static struct _word {
45    char *name;
46    int (*handler)(dialogMenuItem *self);
47} resWords[] = {
48    { "configAnonFTP",		configAnonFTP		},
49    { "configRouter",		configRouter		},
50    { "configNFSServer",	configNFSServer		},
51    { "configNTP",		configNTP		},
52    { "configPCNFSD",		configPCNFSD		},
53    { "configPackages",		configPackages		},
54    { "configRegister",		configRegister		},
55    { "configUsers",		configUsers		},
56    { "configXEnvironment",	configXEnvironment	},
57    { "diskPartitionEditor",	diskPartitionEditor	},
58    { "diskPartitionWrite",	diskPartitionWrite	},
59    { "diskLabelEditor",	diskLabelEditor		},
60    { "diskLabelCommit",	diskLabelCommit		},
61    { "distReset",		distReset		},
62    { "distSetCustom",		distSetCustom		},
63    { "distSetDeveloper",	distSetDeveloper	},
64    { "distSetXDeveloper",	distSetXDeveloper	},
65    { "distSetKernDeveloper",	distSetKernDeveloper	},
66    { "distSetUser",		distSetUser		},
67    { "distSetXUser",		distSetXUser		},
68    { "distSetMinimum",		distSetMinimum		},
69    { "distSetEverything",	distSetEverything	},
70    { "distSetDES",		distSetDES		},
71    { "distSetSrc",		distSetSrc		},
72    { "distSetXF86",		distSetXF86		},
73    { "distExtractAll",		distExtractAll		},
74    { "docBrowser",		docBrowser		},
75    { "docShowDocument",	docShowDocument		},
76    { "installCommit",		installCommit		},
77    { "installExpress",		installExpress		},
78    { "installNovice",		installNovice		},
79    { "installUpgrade",		installUpgrade		},
80    { "installFixup",		installFixup		},
81    { "installFixitHoloShell",	installFixitHoloShell	},
82    { "installFixitCDROM",	installFixitCDROM	},
83    { "installFixitFloppy",	installFixitFloppy	},
84    { "installFilesystems",	installFilesystems	},
85    { "installVarDefaults",	installVarDefaults	},
86    { "loadConfig",		_loadConfig		},
87    { "mediaSetCDROM",		mediaSetCDROM		},
88    { "mediaSetFloppy",		mediaSetFloppy		},
89    { "mediaSetDOS",		mediaSetDOS		},
90    { "mediaSetTape",		mediaSetTape		},
91    { "mediaSetFTP",		mediaSetFTP		},
92    { "mediaSetFTPActive",	mediaSetFTPActive	},
93    { "mediaSetFTPPassive",	mediaSetFTPPassive	},
94    { "mediaSetUFS",		mediaSetUFS		},
95    { "mediaSetNFS",		mediaSetNFS		},
96    { "mediaSetFTPUserPass",	mediaSetFTPUserPass	},
97    { "mediaSetCPIOVerbosity",	mediaSetCPIOVerbosity	},
98    { "mediaGetType",		mediaGetType		},
99    { "optionsEditor",		optionsEditor		},
100    { "register",		configRegister		},	/* Alias */
101    { "packageAdd",		packageAdd		},
102    { "addGroup",		userAddGroup		},
103    { "addUser",		userAddUser		},
104    { "shutdown",		_shutdown 		},
105    { "system",			_systemExecute 		},
106    { NULL, NULL },
107};
108
109static int
110call_possible_resword(char *name, dialogMenuItem *value, int *status)
111{
112    int i, rval;
113
114    rval = 0;
115    for (i = 0; resWords[i].name; i++) {
116	if (!strcmp(name, resWords[i].name)) {
117	    *status = resWords[i].handler(value);
118	    rval = 1;
119	    break;
120	}
121    }
122    return rval;
123}
124
125/* Just convenience */
126static int
127_shutdown(dialogMenuItem *unused)
128{
129    systemShutdown(0);
130    return DITEM_FAILURE;
131}
132
133static int
134_systemExecute(dialogMenuItem *unused)
135{
136    char *cmd = variable_get("command");
137
138    if (cmd)
139	return systemExecute(cmd) ? DITEM_FAILURE : DITEM_SUCCESS;
140    else
141	msgDebug("_systemExecute: No command passed in `command' variable.\n");
142    return DITEM_FAILURE;
143}
144
145static int
146_loadConfig(dialogMenuItem *unused)
147{
148    FILE *fp;
149    char *cp, buf[BUFSIZ];
150    int i = DITEM_SUCCESS;
151
152    cp = variable_get("file");
153    if ((!cp || (fp = fopen(cp, "r")) == NULL) &&
154	(fp = fopen("install.cfg", "r")) == NULL &&
155	(fp = fopen("/stand/install.cfg", "r")) == NULL &&
156	(fp = fopen("/tmp/install.cfg", "r")) == NULL) {
157	msgConfirm("Unable to locate an install.cfg file in $CWD, /stand or /tmp.");
158	i =  DITEM_FAILURE;
159    }
160    else {
161    	variable_set2(VAR_NONINTERACTIVE, "YES");
162    	while (fgets(buf, sizeof buf, fp)) {
163	    if ((i = DITEM_STATUS(dispatchCommand(buf))) != DITEM_SUCCESS) {
164		msgDebug("Command `%s' failed - rest of script aborted.\n", buf);
165		break;
166	    }
167	}
168    }
169    fclose(fp);
170    variable_unset(VAR_NONINTERACTIVE);
171    return i;
172}
173
174/* For a given string, call it or spit out an undefined command diagnostic */
175int
176dispatchCommand(char *str)
177{
178    int i;
179    char *cp;
180
181    if (!str || !*str) {
182	msgConfirm("Null or zero-length string passed to dispatchCommand");
183	return DITEM_FAILURE;
184    }
185    /* If it's got a newline, trim it */
186    if ((cp = index(str, '\n')) != NULL)
187	*cp = '\0';
188
189    /* If it's got a `=' sign in there, assume it's a variable setting */
190    if (index(str, '=')) {
191	if (isDebug())
192	    msgDebug("dispatch: setting variable `%s'\n", str);
193	variable_set(str);
194	i = DITEM_SUCCESS;
195    }
196    else {
197	/* A command might be a pathname if it's encoded in argv[0], which we also support */
198	if ((cp = rindex(str, '/')) != NULL)
199	    str = cp + 1;
200	if (isDebug())
201	    msgDebug("dispatch: calling resword `%s'\n", str);
202	if (!call_possible_resword(str, NULL, &i)) {
203	    msgNotify("Warning: No such command ``%s''", str);
204	    i = DITEM_FAILURE;
205	}
206    }
207    return i;
208}
209