2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * $DragonFly: src/usr.bin/make/shell.c,v 1.3 2005/05/19 16:56:37 okumoto Exp $
47 #include "pathnames.h"
52 * Descriptions for various shells.
54 static const struct CShell shells[] = {
56 * CSH description. The csh can do echo control by playing
57 * with the setting of the 'echo' shell variable. Sadly,
58 * however, it is unable to do error control nicely.
62 TRUE, "unset verbose", "set verbose", "unset verbose",
63 FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"",
67 * SH description. Echo control is also possible and, under
68 * sun UNIX anyway, one can even control error checking.
72 TRUE, "set -", "set -v", "set -",
73 TRUE, "set -e", "set +e",
75 FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n",
80 * KSH description. The Korn shell has a superset of
81 * the Bourne shell's functionality.
85 TRUE, "set -", "set -v", "set -",
86 TRUE, "set -e", "set +e",
91 static char *shellName = NULL; /* last component of shell */
92 char *shellPath = NULL; /* full pathname of executable image */
93 struct Shell *commandShell = NULL;
96 * Find a matching shell in 'shells' given its final component.
99 * A pointer to a freshly allocated Shell structure with a copy
100 * of the static structure or NULL if no shell with the given name
103 static struct Shell *
104 JobMatchShell(const char name[])
106 const struct CShell *sh; /* Pointer into shells table */
109 for (sh = shells; sh < shells + __arysize(shells); sh++)
110 if (strcmp(sh->name, name) == 0)
113 if (sh == shells + __arysize(shells))
117 nsh = emalloc(sizeof(*nsh));
119 nsh->name = estrdup(sh->name);
120 nsh->echoOff = estrdup(sh->echoOff);
121 nsh->echoOn = estrdup(sh->echoOn);
122 nsh->hasEchoCtl = sh->hasEchoCtl;
123 nsh->noPrint = estrdup(sh->noPrint);
124 nsh->hasErrCtl = sh->hasErrCtl;
125 nsh->errCheck = estrdup(sh->errCheck);
126 nsh->ignErr = estrdup(sh->ignErr);
127 nsh->echo = estrdup(sh->echo);
128 nsh->exit = estrdup(sh->exit);
134 * Make a new copy of the shell structure including a copy of the strings
135 * in it. This also defaults some fields in case they are NULL.
138 * The function returns a pointer to the new shell structure.
140 static struct Shell *
141 JobCopyShell(const struct Shell *osh)
145 nsh = emalloc(sizeof(*nsh));
146 nsh->name = estrdup(osh->name);
148 if (osh->echoOff != NULL)
149 nsh->echoOff = estrdup(osh->echoOff);
152 if (osh->echoOn != NULL)
153 nsh->echoOn = estrdup(osh->echoOn);
156 nsh->hasEchoCtl = osh->hasEchoCtl;
158 if (osh->noPrint != NULL)
159 nsh->noPrint = estrdup(osh->noPrint);
163 nsh->hasErrCtl = osh->hasErrCtl;
164 if (osh->errCheck == NULL)
165 nsh->errCheck = estrdup("");
167 nsh->errCheck = estrdup(osh->errCheck);
168 if (osh->ignErr == NULL)
169 nsh->ignErr = estrdup("%s");
171 nsh->ignErr = estrdup(osh->ignErr);
173 if (osh->echo == NULL)
174 nsh->echo = estrdup("");
176 nsh->echo = estrdup(osh->echo);
178 if (osh->exit == NULL)
179 nsh->exit = estrdup("");
181 nsh->exit = estrdup(osh->exit);
187 * Free a shell structure and all associated strings.
190 JobFreeShell(struct Shell *sh)
207 * Given the line following a .SHELL target, parse the
208 * line as a shell specification. Returns FALSE if the
209 * spec was incorrect.
211 * Parse a shell specification and set up commandShell, shellPath appropriately.
214 * TRUE if the specification was correct. FALSE otherwise.
217 * commandShell points to a Shell structure (either predefined or
218 * created from the shell spec), shellPath is the full path of the
219 * shell described by commandShell, while shellName is just the
220 * final component of shellPath.
223 * A shell specification consists of a .SHELL target, with dependency
224 * operator, followed by a series of blank-separated words. Double
225 * quotes can be used to use blanks in words. A backslash escapes
226 * anything (most notably a double-quote and a space) and
227 * provides the functionality it does in C. Each word consists of
228 * keyword and value separated by an equal sign. There should be no
229 * unnecessary spaces in the word. The keywords are as follows:
230 * name Name of shell.
231 * path Location of shell. Overrides "name" if given
232 * quiet Command to turn off echoing.
233 * echo Command to turn echoing on
234 * filter Result of turning off echoing that shouldn't be
236 * echoFlag Flag to turn echoing on at the start
237 * errFlag Flag to turn error checking on at the start
238 * hasErrCtl True if shell has error checking control
239 * check Command to turn on error checking if hasErrCtl
240 * is TRUE or template of command to echo a command
241 * for which error checking is off if hasErrCtl is
243 * ignore Command to turn off error checking if hasErrCtl
244 * is TRUE or template of command to execute a
245 * command so as to ignore any errors it returns if
246 * hasErrCtl is FALSE.
249 Job_ParseShell(const char line[])
255 Boolean fullSpec = FALSE;
256 struct Shell newShell;
259 memset(&newShell, 0, sizeof(newShell));
263 * Parse the specification by keyword but skip the first word
265 brk_string(&aa, line, TRUE);
267 for (argc = aa.argc - 1, argv = aa.argv + 1; argc != 0; argc--, argv++)
272 * Split keyword and value
274 if ((eq = strchr(*argv, '=')) == NULL) {
275 Parse_Error(PARSE_FATAL, "missing '=' in shell "
276 "specification keyword '%s'", *argv);
282 if (strcmp(*argv, "path") == 0) {
284 } else if (strcmp(*argv, "name") == 0) {
286 } else if (strcmp(*argv, "quiet") == 0) {
287 newShell.echoOff = eq;
289 } else if (strcmp(*argv, "echo") == 0) {
290 newShell.echoOn = eq;
292 } else if (strcmp(*argv, "filter") == 0) {
293 newShell.noPrint = eq;
295 } else if (strcmp(*argv, "echoFlag") == 0) {
298 } else if (strcmp(*argv, "errFlag") == 0) {
301 } else if (strcmp(*argv, "hasErrCtl") == 0) {
302 newShell.hasErrCtl = (*eq == 'Y' || *eq == 'y' ||
303 *eq == 'T' || *eq == 't');
305 } else if (strcmp(*argv, "check") == 0) {
306 newShell.errCheck = eq;
308 } else if (strcmp(*argv, "ignore") == 0) {
309 newShell.ignErr = eq;
312 Parse_Error(PARSE_FATAL, "unknown keyword in shell "
313 "specification '%s'", *argv);
320 * Some checks (could be more)
323 if ((newShell.echoOn != NULL) ^ (newShell.echoOff != NULL))
324 Parse_Error(PARSE_FATAL, "Shell must have either both "
325 "echoOff and echoOn or none of them");
327 if (newShell.echoOn != NULL && newShell.echoOff)
328 newShell.hasEchoCtl = TRUE;
333 * If no path was given, the user wants one of the pre-defined
334 * shells, yes? So we find the one s/he wants with the help of
335 * JobMatchShell and set things up the right way. shellPath
336 * will be set up by Job_Init.
338 if (newShell.name == NULL) {
339 Parse_Error(PARSE_FATAL,
340 "Neither path nor name specified");
344 if ((sh = JobMatchShell(newShell.name)) == NULL) {
345 Parse_Error(PARSE_FATAL, "%s: no matching shell",
353 * The user provided a path. If s/he gave nothing else
354 * (fullSpec is FALSE), try and find a matching shell in the
355 * ones we know of. Else we just take the specification at its
356 * word and copy it to a new location. In either case, we need
357 * to record the path the user gave for the shell.
359 path = estrdup(path);
360 if (newShell.name == NULL) {
361 /* get the base name as the name */
362 if ((newShell.name = strrchr(path, '/')) == NULL) {
363 newShell.name = path;
370 if ((sh = JobMatchShell(newShell.name)) == NULL) {
371 Parse_Error(PARSE_FATAL,
372 "%s: no matching shell", newShell.name);
378 sh = JobCopyShell(&newShell);
384 /* set the new shell */
385 JobFreeShell(commandShell);
388 shellName = commandShell->name;
397 commandShell = JobMatchShell(DEFSHELLNAME);
400 * Both the absolute path and the last component
401 * must be set. The last component is taken from the 'name'
402 * field of the default shell description pointed-to by
403 * commandShell. All default shells are located in
406 shellName = commandShell->name;
407 shellPath = str_concat(