]>
diplodocus.org Git - nmh/blob - sbr/arglist.c
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.
12 * Split up a command into an appropriate array to pass to execvp()
13 * or similar function. Returns an allocated argv[] array.
17 * command - String to split up
18 * file - the first argument to "command", suitable for the first argument
19 * to execvp(). Returns allocated memory that must be free()d.
20 * argp - Index to last element (NULL) of returned argv[] array.
22 * Our basic algorithm is this:
24 * - If there are no spaces or shell metacharacters in "command", then
26 * - If there are spaces in command, space-split command string.
27 * - If we have shell metacharacters, run the command using
28 * /bin/sh -c 'command "$@"'. In this case, any additional arguments
29 * appended to the arglist will be expanded by "$@".
31 * In all cases additional arguments can be added to the argv[] array.
34 /* Shell metacharacters we use to trigger a call to the shell */
36 #define METACHARS "$&*(){}[]'\";\\|?<>~`\n"
39 argsplit(char *command
, char **file
, int *argp
)
42 int space
= 0, metachar
= 0, i
;
44 for (p
= command
; *p
; p
++) {
45 if (*p
== ' ' || *p
== '\t') {
47 } else if (strchr(METACHARS
, *p
)) {
53 argvarray
= (char **)mh_xmalloc(sizeof *argvarray
* (MAXARGS
+ 5));
56 * The simple case - no spaces or shell metacharacters
59 if (!space
&& !metachar
) {
60 argvarray
[0] = getcpy(r1bindex(command
, '/'));
62 *file
= mh_xstrdup(command
);
69 * Spaces, but no shell metacharacters; space-split into separate
73 if (space
&& !metachar
) {
75 p
= mh_xstrdup(command
);
76 split
= brkstring(p
, " \t", NULL
);
77 if (split
[0] == NULL
) {
78 adios(NULL
, "Invalid blank command found");
80 argvarray
[0] = mh_xstrdup(r1bindex(split
[0], '/'));
81 for (i
= 1; split
[i
] != NULL
; i
++) {
83 adios(NULL
, "Command exceeded argument limit");
85 argvarray
[i
] = mh_xstrdup(split
[i
]);
88 *file
= mh_xstrdup(split
[0]);
96 * Remaining option - pass to the shell.
100 * - The command passed to "sh -c" is actually:
103 * If there are additional arguments they will be expanded by the
104 * shell, otherwise "$@" expands to nothing.
106 * - Every argument after the -c string gets put into positional
107 * parameters starting at $0, but $@ starts expanding with $1.
108 * So we put in a dummy argument (we just use /bin/sh)
111 *file
= mh_xstrdup("/bin/sh");
112 argvarray
[0] = mh_xstrdup("sh");
113 argvarray
[1] = mh_xstrdup("-c");
114 argvarray
[2] = mh_xstrdup(command
);
115 argvarray
[2] = add(" \"$@\"", argvarray
[2]);
116 argvarray
[3] = mh_xstrdup("/bin/sh");
126 * Free our argument array
130 arglist_free(char *command
, char **argvarray
)
136 if (argvarray
!= NULL
) {
137 for (i
= 0; argvarray
[i
] != NULL
; i
++)
144 * Similar in functionality to argsplit, but is designed to deal with
149 argsplit_msgarg(struct msgs_array
*msgs
, char *command
, char **program
)
154 vec
= argsplit(command
, program
, &argp
);
157 * As usual, there is lousy memory management in nmh. Nothing ever
158 * free's the msgs_array, and a lot of the arguments are allocated
159 * from static memory. I could have app_msgarg() allocate new
160 * memory for each pointer, but for now I decided to stick with
161 * the existing interface; maybe that will be revisited later.
162 * So we'll just copy over our pointers and free the pointer list
163 * (not the actual pointers themselves). Note that we don't
164 * include a trailing NULL, since we are expecting the application
165 * to take care of that.
168 for (i
= 0; i
< argp
; i
++) {
169 app_msgarg(msgs
, vec
[i
]);
176 * Insert a arglist vector into the beginning of an struct msgs array
178 * Uses by some programs (e.g., show) who want to decide which proc
179 * to use after the argument vector has been constructed
187 argsplit_insert(struct msgs_array
*msgs
, char *command
, char **program
)
192 vec
= argsplit(command
, program
, &argp
);
195 * Okay, we want to shuffle all of our arguments down so we have room
196 * for argp number of arguments. This means we need to know about
197 * msgs_array internals. If that changes, we need to change this
201 if (msgs
->size
+ argp
>= msgs
->max
) {
202 msgs
->max
+= max(MAXMSGS
, argp
);
203 msgs
->msgs
= mh_xrealloc(msgs
->msgs
, msgs
->max
* sizeof(*msgs
->msgs
));
206 for (i
= msgs
->size
- 1; i
>= 0; i
--)
207 msgs
->msgs
[i
+ argp
] = msgs
->msgs
[i
];
212 * Now fill in the arguments at the beginning of the vector.
215 for (i
= 0; i
< argp
; i
++)
216 msgs
->msgs
[i
] = vec
[i
];