-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargs.c
417 lines (375 loc) · 7.07 KB
/
args.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
*
* Sccsid @(#)args.c 1.5 (gritter) 6/16/05
*/
/* from OpenSolaris "args.c 1.10 05/06/08 SMI" SVr4.0 1.10.4.1 */
/*
* UNIX shell
*/
#include "defs.h"
static void freedolh(void);
static struct dolnod *copyargs(unsigned char *[], int);
static struct dolnod *dolh;
/* Used to save outermost positional parameters */
static struct dolnod *globdolh;
static unsigned char **globdolv;
static int globdolc;
unsigned char flagadr[16];
unsigned char flagchar[] =
{
'x',
'n',
'v',
't',
STDFLG,
'i',
'e',
'r',
'k',
'u',
'h',
'f',
'a',
'm',
'p',
0
};
long flagval[] =
{
execpr,
noexec,
readpr,
oneflg,
stdflg,
intflg,
errflg,
rshflg,
keyflg,
setflg,
hashflg,
nofngflg,
exportflg,
monitorflg,
privflg,
0
};
/* ======== option handling ======== */
int
options(int argc, unsigned char **argv)
{
register unsigned char *cp;
register unsigned char **argp = argv;
register unsigned char *flagc;
unsigned char *flagp;
int len;
wchar_t wc;
if (argc > 1 && *argp[1] == '-')
{
/*
* if first argument is "--" then options are not
* to be changed. Fix for problems getting
* $1 starting with a "-"
*/
cp = argp[1];
if (cp[1] == '-')
{
argp[1] = argp[0];
argc--;
return(argc);
}
if (cp[1] == '\0')
flags &= ~(execpr|readpr);
/*
* Step along 'flagchar[]' looking for matches.
* 'sicrp' are not legal with 'set' command.
*/
cp++;
while (*cp) {
if ((len = nextc(&wc, (char *)cp)) <= 0) {
len = 1;
wc = (unsigned char)*cp;
failed(argv[1],badopt);
}
cp += len;
flagc = flagchar;
while (*flagc && wc != *flagc)
flagc++;
if (wc == *flagc)
{
if (eq(argv[0], "set") && any(wc, "sicrp"))
failed(argv[1], badopt);
else
{
flags |= flagval[flagc-flagchar];
if (flags & errflg)
eflag = errflg;
}
}
else if (wc == 'c' && argc > 2 && comdiv == 0)
{
comdiv = argp[2];
argp[1] = argp[0];
argp++;
argc--;
}
else
failed(argv[1],badopt);
}
argp[1] = argp[0];
argc--;
}
else if (argc > 1 && *argp[1] == '+') /* unset flags x, k, t, n, v, e, u */
{
cp = argp[1];
cp++;
while (*cp)
{
if ((len = nextc(&wc, (char *)cp)) <= 0) {
cp++;
continue;
}
flagc = flagchar;
while (*flagc && wc != *flagc)
flagc++;
/*
* step through flags
*/
if (!any(wc, "sicrp") && wc == *flagc) {
flags &= ~(flagval[flagc-flagchar]);
if (wc == 'e')
eflag = 0;
}
cp += len;
}
argp[1] = argp[0];
argc--;
}
/*
* set up $-
*/
flagp = flagadr;
if (flags)
{
flagc = flagchar;
while (*flagc)
{
if (flags & flagval[flagc-flagchar])
*flagp++ = *flagc;
flagc++;
}
}
*flagp = 0;
return(argc);
}
/*
* sets up positional parameters
*/
void
setargs(unsigned char *argi[])
{
register unsigned char **argp = argi; /* count args */
register int argn = 0;
while (*argp++ != (unsigned char *)ENDARGS)
argn++;
/*
* free old ones unless on for loop chain
*/
freedolh();
dolh = copyargs(argi, argn);
dolc = argn - 1;
}
static void
freedolh(void)
{
register unsigned char **argp;
register struct dolnod *argblk;
if (argblk = dolh)
{
if ((--argblk->doluse) == 0)
{
for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
free(*argp);
free(argblk->dolarg);
free(argblk);
}
}
}
struct dolnod *
freeargs(struct dolnod *blk)
{
register unsigned char **argp;
register struct dolnod *argr = 0;
register struct dolnod *argblk;
int cnt;
if (argblk = blk)
{
argr = argblk->dolnxt;
cnt = --argblk->doluse;
if (argblk == dolh)
{
if (cnt == 1)
return(argr);
else
return(argblk);
}
else
{
if (cnt == 0)
{
for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
free(*argp);
free(argblk->dolarg);
free(argblk);
}
}
}
return(argr);
}
static struct dolnod *
copyargs(unsigned char *from[], int n)
{
register struct dolnod *np = (struct dolnod *)alloc(sizeof(struct dolnod));
register unsigned char **fp = from;
register unsigned char **pp;
np -> dolnxt = 0;
np->doluse = 1; /* use count */
pp = np->dolarg = (unsigned char **)alloc((n+1)*sizeof(char *));
dolv = pp;
while (n--)
*pp++ = make(*fp++);
*pp++ = ENDARGS;
return(np);
}
struct dolnod *
clean_args(struct dolnod *blk)
{
register unsigned char **argp;
register struct dolnod *argr = 0;
register struct dolnod *argblk;
if (argblk = blk)
{
argr = argblk->dolnxt;
if (argblk == dolh)
argblk->doluse = 1;
else
{
for (argp = argblk->dolarg; *argp != (unsigned char *)ENDARGS; argp++)
free(*argp);
free(argblk->dolarg);
free(argblk);
}
}
return(argr);
}
void
clearup(void)
{
/*
* force `for' $* lists to go away
*/
if(globdolv)
dolv = globdolv;
if(globdolc)
dolc = globdolc;
if(globdolh)
dolh = globdolh;
globdolv = 0;
globdolc = 0;
globdolh = 0;
while (argfor = clean_args(argfor))
;
/*
* clean up io files
*/
while (pop())
;
/*
* Clean up pipe file descriptor
* from command substitution
*/
if(savpipe != -1) {
close(savpipe);
savpipe = -1;
}
/*
* clean up tmp files
*/
while (poptemp())
;
}
/*
* Save positiional parameters before outermost function invocation
* in case we are interrupted.
* Increment use count for current positional parameters so that they aren't thrown
* away.
*/
struct dolnod *
savargs(int funcnt)
{
if (!funcnt) {
globdolh = dolh;
globdolv = dolv;
globdolc = dolc;
}
useargs();
return(dolh);
}
/* After function invocation, free positional parameters,
* restore old positional parameters, and restore
* use count.
*/
void
restorargs(struct dolnod *olddolh, int funcnt)
{
if(argfor != olddolh)
while ((argfor = clean_args(argfor)) != olddolh && argfor);
if(!argfor)
return;
freedolh();
dolh = olddolh;
if(dolh)
dolh -> doluse++; /* increment use count so arguments aren't freed */
argfor = freeargs(dolh);
if(funcnt == 1) {
globdolh = 0;
globdolv = 0;
globdolc = 0;
}
}
struct dolnod *
useargs(void)
{
if (dolh)
{
if (dolh->doluse++ == 1)
{
dolh->dolnxt = argfor;
argfor = dolh;
}
}
return(dolh);
}