]> diplodocus.org Git - nmh/blob - sbr/arglist.c
Make sure these stdarg lists are properly terminated with pointer-context
[nmh] / sbr / arglist.c
1
2 /*
3 * arglist.c -- Routines for handling argument lists for execvp() and friends
4 *
5 * This code is Copyright (c) 2013, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 /*
14 * Split up a command into an appropriate array to pass to execvp()
15 * or similar function. Returns an allocated argv[] array.
16 *
17 * Function arguments:
18 *
19 * command - String to split up
20 * file - the first argument to "command", suitable for the first argument
21 * to execvp(). Returns allocated memory that must be free()d.
22 * argp - Index to last element (NULL) of returned argv[] array.
23 *
24 * Our basic algorithm is this:
25 *
26 * - If there are no spaces or shell metacharacters in "command", then
27 * take it as-is.
28 * - If there are spaces in command, space-split command string.
29 * - If we have shell metacharacters, run the command using
30 * /bin/sh -c 'command "$@"'. In this case, any additional arguments
31 * appended to the arglist will be expanded by "$@".
32 *
33 * In all cases additional arguments can be added to the argv[] array.
34 */
35
36 /* Shell metacharacters we use to trigger a call to the shell */
37
38 #define METACHARS "$&*(){}[]'\";\\|?<>~`\n"
39
40 char **
41 argsplit(char *command, char **file, int *argp)
42 {
43 char **argvarray, *p;
44 int space = 0, metachar = 0, i;
45
46 for (p = command; *p; p++) {
47 if (*p == ' ' || *p == '\t') {
48 space = 1;
49 } else if (strchr(METACHARS, *p)) {
50 metachar = 1;
51 break;
52 }
53 }
54
55 argvarray = (char **) mh_xmalloc((sizeof(char **) * MAXARGS));
56
57 /*
58 * The simple case - no spaces or shell metacharacters
59 */
60
61 if (!space && !metachar) {
62 argvarray[0] = getcpy(r1bindex(command, '/'));
63 argvarray[1] = NULL;
64 *file = getcpy(command);
65 if (argp)
66 *argp = 1;
67 return argvarray;
68 }
69
70 /*
71 * Spaces, but no shell metacharacters; space-split into seperate
72 * arguments
73 */
74
75 if (space && !metachar) {
76 char **split;
77 p = getcpy(command);
78 split = brkstring(p, " \t", NULL);
79 if (split[0] == NULL) {
80 adios(NULL, "Invalid blank command found");
81 }
82 argvarray[0] = getcpy(r1bindex(split[0], '/'));
83 for (i = 1; split[i] != NULL; i++) {
84 if (i > MAXARGS) {
85 adios(NULL, "Command exceeded argument limit");
86 }
87 argvarray[i] = getcpy(split[i]);
88 }
89 argvarray[i] = NULL;
90 *file = getcpy(split[0]);
91 if (argp)
92 *argp = i;
93 free(p);
94 return argvarray;
95 }
96
97 /*
98 * Remaining option - pass to the shell.
99 *
100 * Some notes here:
101 *
102 * - The command passed to "sh -c" is actually:
103 * command "$@"
104 *
105 * If there are additional arguments they will be expanded by the
106 * shell, otherwise "$@" expands to nothing.
107 *
108 * - Every argument after the -c string gets put into positional
109 * parameters starting at $0, but $@ starts expanding with $1.
110 * So we put in a dummy argument (we just use /bin/sh)
111 */
112
113 *file = getcpy("/bin/sh");
114 argvarray[0] = getcpy("sh");
115 argvarray[1] = getcpy("-c");
116 argvarray[2] = getcpy(command);
117 argvarray[2] = add(" \"$@\"", argvarray[2]);
118 argvarray[3] = getcpy("/bin/sh");
119 argvarray[4] = NULL;
120
121 if (argp)
122 *argp = 4;
123
124 return argvarray;
125 }
126
127 /*
128 * Free our argument array
129 */
130
131 void
132 arglist_free(char *command, char **argvarray)
133 {
134 int i;
135
136 if (command != NULL)
137 free(command);
138
139 if (argvarray != NULL) {
140 for (i = 0; argvarray[i] != NULL; i++)
141 free(argvarray[i]);
142 free(argvarray);
143 }
144 }