]>
diplodocus.org Git - nmh/blob - sbr/arglist.c
ed8dfbc6886cd9478c30c39eb9c611c59cb89bb1
1 /* arglist.c -- Routines for handling argument lists for execvp() and friends
3 * This code is Copyright (c) 2013, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
13 * Split up a command into an appropriate array to pass to execvp()
14 * or similar function. Returns an allocated argv[] array.
18 * command - String to split up
19 * file - the first argument to "command", suitable for the first argument
20 * to execvp(). Returns allocated memory that must be free()d.
21 * argp - Index to last element (NULL) of returned argv[] array.
23 * Our basic algorithm is this:
25 * - If there are no spaces or shell metacharacters in "command", then
27 * - If there are spaces in command, space-split command string.
28 * - If we have shell metacharacters, run the command using
29 * /bin/sh -c 'command "$@"'. In this case, any additional arguments
30 * appended to the arglist will be expanded by "$@".
32 * In all cases additional arguments can be added to the argv[] array.
35 /* Shell metacharacters we use to trigger a call to the shell */
37 #define METACHARS "$&*(){}[]'\";\\|?<>~`\n"
40 argsplit(char *command
, char **file
, int *argp
)
46 bool metachar
= false;
47 for (p
= command
; *p
; p
++) {
48 if (*p
== ' ' || *p
== '\t') {
50 } else if (strchr(METACHARS
, *p
)) {
56 argvarray
= mh_xmalloc(sizeof *argvarray
* (MAXARGS
+ 5));
59 * The simple case - no spaces or shell metacharacters
62 if (!space
&& !metachar
) {
63 argvarray
[0] = mh_xstrdup(r1bindex(command
, '/'));
65 *file
= mh_xstrdup(command
);
72 * Spaces, but no shell metacharacters; space-split into separate
76 if (space
&& !metachar
) {
78 p
= mh_xstrdup(command
);
79 split
= brkstring(p
, " \t", NULL
);
80 if (split
[0] == NULL
) {
81 die("Invalid blank command found");
83 argvarray
[0] = mh_xstrdup(r1bindex(split
[0], '/'));
84 for (i
= 1; split
[i
] != NULL
; i
++) {
86 die("Command exceeded argument limit");
88 argvarray
[i
] = mh_xstrdup(split
[i
]);
91 *file
= mh_xstrdup(split
[0]);
99 * Remaining option - pass to the shell.
103 * - The command passed to "sh -c" is actually:
106 * If there are additional arguments they will be expanded by the
107 * shell, otherwise "$@" expands to nothing.
109 * - Every argument after the -c string gets put into positional
110 * parameters starting at $0, but $@ starts expanding with $1.
111 * So we put in a dummy argument (we just use /bin/sh)
114 *file
= mh_xstrdup("/bin/sh");
115 argvarray
[0] = mh_xstrdup("sh");
116 argvarray
[1] = mh_xstrdup("-c");
117 argvarray
[2] = mh_xstrdup(command
);
118 argvarray
[2] = add(" \"$@\"", argvarray
[2]);
119 argvarray
[3] = mh_xstrdup("/bin/sh");
129 * Free our argument array
133 arglist_free(char *command
, char **argvarray
)
139 if (argvarray
!= NULL
) {
140 for (i
= 0; argvarray
[i
] != NULL
; i
++)
147 * Similar in functionality to argsplit, but is designed to deal with
152 argsplit_msgarg(struct msgs_array
*msgs
, char *command
, char **program
)
157 vec
= argsplit(command
, program
, &argp
);
160 * As usual, there is lousy memory management in nmh. Nothing ever
161 * free's the msgs_array, and a lot of the arguments are allocated
162 * from static memory. I could have app_msgarg() allocate new
163 * memory for each pointer, but for now I decided to stick with
164 * the existing interface; maybe that will be revisited later.
165 * So we'll just copy over our pointers and free the pointer list
166 * (not the actual pointers themselves). Note that we don't
167 * include a trailing NULL, since we are expecting the application
168 * to take care of that.
171 for (i
= 0; i
< argp
; i
++) {
172 app_msgarg(msgs
, vec
[i
]);
179 * Insert a arglist vector into the beginning of an struct msgs array
181 * Uses by some programs (e.g., show) who want to decide which proc
182 * to use after the argument vector has been constructed
190 argsplit_insert(struct msgs_array
*msgs
, char *command
, char **program
)
195 vec
= argsplit(command
, program
, &argp
);
198 * Okay, we want to shuffle all of our arguments down so we have room
199 * for argp number of arguments. This means we need to know about
200 * msgs_array internals. If that changes, we need to change this
204 if (msgs
->size
+ argp
>= msgs
->max
) {
205 msgs
->max
+= max(MAXMSGS
, argp
);
206 msgs
->msgs
= mh_xrealloc(msgs
->msgs
, msgs
->max
* sizeof(*msgs
->msgs
));
209 for (i
= msgs
->size
- 1; i
>= 0; i
--)
210 msgs
->msgs
[i
+ argp
] = msgs
->msgs
[i
];
215 * Now fill in the arguments at the beginning of the vector.
218 for (i
= 0; i
< argp
; i
++)
219 msgs
->msgs
[i
] = vec
[i
];