File: arglist.c

package info (click to toggle)
nmh 1.6-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,204 kB
  • ctags: 3,851
  • sloc: ansic: 48,922; sh: 16,422; makefile: 559; perl: 509; lex: 402; awk: 74
file content (222 lines) | stat: -rw-r--r-- 5,624 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

/*
 * arglist.c -- Routines for handling argument lists for execvp() and friends
 *
 * This code is Copyright (c) 2013, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/utils.h>

/*
 * Split up a command into an appropriate array to pass to execvp()
 * or similar function.  Returns an allocated argv[] array.
 *
 * Function arguments:
 *
 * command - String to split up
 * file    - the first argument to "command", suitable for the first argument
 *           to execvp().  Returns allocated memory that must be free()d.
 * argp    - Index to last element (NULL) of returned argv[] array.
 *
 * Our basic algorithm is this:
 *
 * - If there are no spaces or shell metacharacters in "command", then
 *   take it as-is.
 * - If there are spaces in command, space-split command string.
 * - If we have shell metacharacters, run the command using
 *   /bin/sh -c 'command "$@"'.  In this case, any additional arguments
 *   appended to the arglist will be expanded by "$@".
 *
 * In all cases additional arguments can be added to the argv[] array.
 */

/* Shell metacharacters we use to trigger a call to the shell */

#define METACHARS	"$&*(){}[]'\";\\|?<>~`\n"

char **
argsplit(char *command, char **file, int *argp)
{
    char **argvarray, *p;
    int space = 0, metachar = 0, i;

    for (p = command; *p; p++) {
    	if (*p == ' ' || *p == '\t') {
		space = 1;
	} else if (strchr(METACHARS, *p)) {
		metachar = 1;
		break;
	}
    }

    argvarray = (char **) mh_xmalloc((sizeof(char **) * (MAXARGS + 5)));

    /*
     * The simple case - no spaces or shell metacharacters
     */

    if (!space && !metachar) {
    	argvarray[0] = getcpy(r1bindex(command, '/'));
	argvarray[1] = NULL;
	*file = getcpy(command);
	if (argp)
	    *argp = 1;
	return argvarray;
    }

    /*
     * Spaces, but no shell metacharacters; space-split into seperate
     * arguments
     */

    if (space && !metachar) {
    	char **split;
	p = getcpy(command);
	split = brkstring(p, " \t", NULL);
	if (split[0] == NULL) {
	    adios(NULL, "Invalid blank command found");
	}
	argvarray[0] = getcpy(r1bindex(split[0], '/'));
	for (i = 1; split[i] != NULL; i++) {
	    if (i > MAXARGS) {
		adios(NULL, "Command exceeded argument limit");
	    }
	    argvarray[i] = getcpy(split[i]);
	}
	argvarray[i] = NULL;
	*file = getcpy(split[0]);
	if (argp)
	    *argp = i;
	free(p);
	return argvarray;
    }

    /*
     * Remaining option - pass to the shell.
     *
     * Some notes here:
     *
     * - The command passed to "sh -c" is actually:
     *      command "$@"
     *
     *   If there are additional arguments they will be expanded by the
     *   shell, otherwise "$@" expands to nothing.
     *
     * - Every argument after the -c string gets put into positional
     *   parameters starting at $0, but $@ starts expanding with $1.
     *   So we put in a dummy argument (we just use /bin/sh)
     */

    *file = getcpy("/bin/sh");
    argvarray[0] = getcpy("sh");
    argvarray[1] = getcpy("-c");
    argvarray[2] = getcpy(command);
    argvarray[2] = add(" \"$@\"", argvarray[2]);
    argvarray[3] = getcpy("/bin/sh");
    argvarray[4] = NULL;

    if (argp)
    	*argp = 4;

    return argvarray;
}

/*
 * Free our argument array
 */

void
arglist_free(char *command, char **argvarray)
{
    int i;

    if (command != NULL)
    	free(command);

    if (argvarray != NULL) {
    	for (i = 0; argvarray[i] != NULL; i++)
	    free(argvarray[i]);
	free(argvarray);
    }
}

/*
 * Similar in functionality to argsplit, but is designed to deal with
 * a msgs_array.
 */

void
argsplit_msgarg(struct msgs_array *msgs, char *command, char **program)
{
    int argp, i;
    char **vec;

    vec = argsplit(command, program, &argp);

    /*
     * As usual, there is lousy memory management in nmh.  Nothing ever
     * free's the msgs_array, and a lot of the arguments are allocated
     * from static memory.  I could have app_msgarg() allocate new
     * memory for each pointer, but for now I decided to stick with
     * the existing interface; maybe that will be revisited later.
     * So we'll just copy over our pointers and free the pointer list
     * (not the actual pointers themselves).  Note that we don't
     * include a trailing NULL, since we are expecting the application
     * to take care of that.
     */

    for (i = 0; i < argp; i++) {
    	app_msgarg(msgs, vec[i]);
    }

    free(vec);
}

/*
 * Insert a arglist vector into the beginning of an struct msgs array
 *
 * Uses by some programs (e.g., show) who want to decide which proc
 * to use after the argument vector has been constructed
 */

#ifndef MAXMSGS
#define MAXMSGS 256
#endif

void
argsplit_insert(struct msgs_array *msgs, char *command, char **program)
{
    int argp, i;
    char **vec;

    vec = argsplit(command, program, &argp);

    /*
     * Okay, we want to shuffle all of our arguments down so we have room
     * for argp number of arguments.  This means we need to know about
     * msgs_array internals.  If that changes, we need to change this
     * code here.
     */

    if (msgs->size + argp >= msgs->max) {
    	msgs->max += MAXMSGS > argp ? MAXMSGS : argp;
	msgs->msgs = mh_xrealloc(msgs->msgs, msgs->max * sizeof(*msgs->msgs));
    }

    for (i = msgs->size - 1; i >= 0; i--)
    	msgs->msgs[i + argp] = msgs->msgs[i];

    msgs->size += argp;

    /*
     * Now fill in the arguments at the beginning of the vector.
     */

    for (i = 0; i < argp; i++)
    	msgs->msgs[i] = vec[i];

    free(vec);
}