]>
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.
10 #include "brkstring.h"
16 * Split up a command into an appropriate array to pass to execvp()
17 * or similar function. Returns an allocated argv[] array.
21 * command - String to split up
22 * file - the first argument to "command", suitable for the first argument
23 * to execvp(). Returns allocated memory that must be free()d.
24 * argp - Index to last element (NULL) of returned argv[] array.
26 * Our basic algorithm is this:
28 * - If there are no spaces or shell metacharacters in "command", then
30 * - If there are spaces in command, space-split command string.
31 * - If we have shell metacharacters, run the command using
32 * /bin/sh -c 'command "$@"'. In this case, any additional arguments
33 * appended to the arglist will be expanded by "$@".
35 * In all cases additional arguments can be added to the argv[] array.
38 /* Shell metacharacters we use to trigger a call to the shell */
40 #define METACHARS "$&*(){}[]'\";\\|?<>~`\n"
43 argsplit(char *command
, char **file
, int *argp
)
49 bool metachar
= false;
50 for (p
= command
; *p
; p
++) {
51 if (*p
== ' ' || *p
== '\t') {
53 } else if (strchr(METACHARS
, *p
)) {
59 argvarray
= mh_xmalloc(sizeof *argvarray
* (MAXARGS
+ 5));
62 * The simple case - no spaces or shell metacharacters
65 if (!space
&& !metachar
) {
66 argvarray
[0] = mh_xstrdup(r1bindex(command
, '/'));
68 *file
= mh_xstrdup(command
);
75 * Spaces, but no shell metacharacters; space-split into separate
79 if (space
&& !metachar
) {
81 p
= mh_xstrdup(command
);
82 split
= brkstring(p
, " \t", NULL
);
83 if (split
[0] == NULL
) {
84 die("Invalid blank command found");
86 argvarray
[0] = mh_xstrdup(r1bindex(split
[0], '/'));
87 for (i
= 1; split
[i
] != NULL
; i
++) {
89 die("Command exceeded argument limit");
91 argvarray
[i
] = mh_xstrdup(split
[i
]);
94 *file
= mh_xstrdup(split
[0]);
102 * Remaining option - pass to the shell.
106 * - The command passed to "sh -c" is actually:
109 * If there are additional arguments they will be expanded by the
110 * shell, otherwise "$@" expands to nothing.
112 * - Every argument after the -c string gets put into positional
113 * parameters starting at $0, but $@ starts expanding with $1.
114 * So we put in a dummy argument (we just use /bin/sh)
117 *file
= mh_xstrdup("/bin/sh");
118 argvarray
[0] = mh_xstrdup("sh");
119 argvarray
[1] = mh_xstrdup("-c");
120 argvarray
[2] = mh_xstrdup(command
);
121 argvarray
[2] = add(" \"$@\"", argvarray
[2]);
122 argvarray
[3] = mh_xstrdup("/bin/sh");
132 * Free our argument array
136 arglist_free(char *command
, char **argvarray
)
142 if (argvarray
!= NULL
) {
143 for (i
= 0; argvarray
[i
] != NULL
; i
++)
150 * Similar in functionality to argsplit, but is designed to deal with
155 argsplit_msgarg(struct msgs_array
*msgs
, char *command
, char **program
)
160 vec
= argsplit(command
, program
, &argp
);
163 * As usual, there is lousy memory management in nmh. Nothing ever
164 * free's the msgs_array, and a lot of the arguments are allocated
165 * from static memory. I could have app_msgarg() allocate new
166 * memory for each pointer, but for now I decided to stick with
167 * the existing interface; maybe that will be revisited later.
168 * So we'll just copy over our pointers and free the pointer list
169 * (not the actual pointers themselves). Note that we don't
170 * include a trailing NULL, since we are expecting the application
171 * to take care of that.
174 for (i
= 0; i
< argp
; i
++) {
175 app_msgarg(msgs
, vec
[i
]);
182 * Insert a arglist vector into the beginning of an struct msgs array
184 * Uses by some programs (e.g., show) who want to decide which proc
185 * to use after the argument vector has been constructed
193 argsplit_insert(struct msgs_array
*msgs
, char *command
, char **program
)
198 vec
= argsplit(command
, program
, &argp
);
201 * Okay, we want to shuffle all of our arguments down so we have room
202 * for argp number of arguments. This means we need to know about
203 * msgs_array internals. If that changes, we need to change this
207 if (msgs
->size
+ argp
>= msgs
->max
) {
208 msgs
->max
+= max(MAXMSGS
, argp
);
209 msgs
->msgs
= mh_xrealloc(msgs
->msgs
, msgs
->max
* sizeof(*msgs
->msgs
));
212 for (i
= msgs
->size
- 1; i
>= 0; i
--)
213 msgs
->msgs
[i
+ argp
] = msgs
->msgs
[i
];
218 * Now fill in the arguments at the beginning of the vector.
221 for (i
= 0; i
< argp
; i
++)
222 msgs
->msgs
[i
] = vec
[i
];