File: MakeArgv.c

package info (click to toggle)
ncftp2 1%3A2.4.3-12
  • links: PTS
  • area: main
  • in suites: woody
  • size: 948 kB
  • ctags: 1,232
  • sloc: ansic: 13,880; makefile: 166; sh: 7
file content (574 lines) | stat: -rw-r--r-- 16,910 bytes parent folder | download | duplicates (6)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* MakeArgv.c
 *
 * Purpose: Translate a command line string into an array of arguments
 *   for use with the user commands;  Pre-process a command line string
 *   and expand $variables for the macros.
 */

#include "Sys.h"
#include <ctype.h>
#include "Util.h"
#include "MakeArgv.h"

static char *gMAVErrStrings[] = {
	NULL,
	"Unbalanced quotes",
	"Too many sets of quotes",
	"Cannot redirect output to more than one file",
	"No filename supplied to redirect output to",
	"No command supplied to pipe output to",
	"Redirection of input not implemented",
	"Cannot redirect and pipe out at the same time"
};

int MakeArgVector(char *str, CmdLineInfoPtr clp)
{
	register char *src;		/* The command line to parse. */
	register char *dst;		/* Where the argument(s) are written. */
	int argc;				/* The number of arguments. */
	char **argv;			/* An array of pointers to the arguments. */
	char quotestack[kMaxQuotePairs + 1];
							/* Keeps track of each successive group of
							 * (different) quotes.
							 */
	char *qp;				/* The most recent set of quotes we're tracking. */
	int err;				/* Which syntax error occurred. */
	int addQuoteToArg;		/* Needed to tell whether a quote character
							 * should be added to the current argument.
							 */
	int prevArgType;		/* Used to track if a previous parameter was
							 * one that should not be included in the
							 * argument buffer, such as a redirection
							 * symbol (>, >>) and its filename.
							 */
	char *curArg;			/* Points to the beginning of the token we
							 * are writing to.
							 */
	unsigned int c;			/* Char to copy to dst. */
	char numBuf[4];			/* An atoi buffer for a 3-digit octal number. */
	unsigned int octalNum;	/* The resulting integer. */

	err = kMavNoErr;
	src = str;
	dst = clp->argBuf;
	argc = 0;
	argv = clp->argVector;
	
	/* Initialize the stack of quote characters.  Each time we get a
	 * quote character, we add it to the stack.  When we encounter
	 * another quote character of the same type, we remove it from the
	 * stack to denote that the set has been balanced.
	 *
	 * The stack pointer, qp, is init'ed to the first element of the
	 * stack, to denote that there are no quote sets active.
	 */
	qp = quotestack;
	*qp = 0;

	/* Init this to a regular type, meaning we don't have to do any
	 * cleanup after this argument like removing the redirected file
	 * from the list of arguments.
	 */
	prevArgType = kRegularArg;
	
	/* Initially, no outfile has been specified. */
	*clp->outFileName = 0;
	clp->isAppend = 0;
	
	/* Initially, no pipe sub-command either. */
	*clp->pipeCmdLine = 0;

	for (;argc < kMaxArgs; argc++) {
		/* First, decide where to write the next argument.
		 * Normally we would write it to the argument buffer,
		 * but if in a previous iteration we detected a redirection
		 * symbol, we would want to write the next argument to the
		 * outfile string instead of adding the name of the out file
		 * to the argument list.
		 */
		if (prevArgType == kRegularArg)
			argv[argc] = dst;
		else {
			/* If the out file string already had something in it when
			 * we get here, it means that we are ready to start collecting
			 * arguments again.
			 */
			if (*clp->outFileName) {
				/* Fix the argument counter to what it should be. */
				argc -= 2;
				
				/* Re-set dst to point to the argument directly after
				 * the argument before the redirection symbol and the
				 * redirected filename.
				 */
				dst = argv[argc];
				
				/* Don't forget to keep track of whether we got an
				 * overwrite redirection symbol (>) or an append
				 * symbol (>>).
				 */
				clp->isAppend = (prevArgType == kReDirectOutAppendArg);
				
				/* Tell the parser that we're writing arguments again. */
				prevArgType = kRegularArg;
			} else {
				/* The out file's name has not been set yet, so write the
				 * next token as the name of the out file.
				 */
				dst = clp->outFileName;
			}
		}
		
		/* Save the location of the current token for later. */
		curArg = dst;
		*curArg = 0;

		/* Skip any whitespace between arguments. */
		while (isspace(*src))
			src++;
		if (!*src) {
			/* We have reached the end of the command line. */
			
			/* If dst was left pointing to the name of the outfile,
			 * outfile has an empty name, meaning the user did something
			 * like 'aaa bbb >' and forgot the outfile name.
			 */
			if (dst == clp->outFileName) {
				err = kMavErrNoReDirectedFileName;
				argc--;
			}
				
			/* If the quote stack pointer does not point to the
			 * first element of the stack, it means that there
			 * is atleast one set of quotes that wasn't properly
			 * balanced.
			 */
			if (qp != quotestack)
				err = kMavErrUnbalancedQuotes;
			goto done;
		}

		for (;;) {
			if (*src == '\\') {
				/* As usual, go ahead and add the character directly after
				 * backslash without worrying about that character's
				 * special meaning, if any.
				 */
				src++;	/* Skip the backslash, which we don't want. */
				if (isdigit(src[0]) && isdigit(src[1]) && isdigit(src[2])) {
					/* They can also specify a non-printing character,
					 * by saying \xxx.
					 */
					memcpy(numBuf, src, (size_t)3);
					numBuf[3] = '\0';
					sscanf(numBuf, "%o", &octalNum);
					c = octalNum;
					src += 3;
					goto copyCharToArg2;
				}
				goto copyCharToArg;
			} else if (!*src) {
				/* If there are no more characters in the command line,
				 * end the current argument in progress.
				 */
				*dst++ = '\0';
				break;
			} else if (*src == '^') {
				/* Let them fake a control character by using the two
				 * character sequence ^X.
				 */
				++src;
				if (*src == '?')
					c = 0x7f;	/* DEL */
				else
					c = ((*src) & 31);
				++src;
				goto copyCharToArg2;
			} else if (ISQUOTE(*src)) {
				/* First, check to see if this quote is the second
				 * quote to complete the pair.
				 */
				if (*src == *qp) {
					/* If it is, remove this set from the stack. */
					*qp-- = 0;
					
					/* Add the quote to the argument if it is not
					 * the outermost set.
					 */
					addQuoteToArg = (quotestack < qp);
				} else {
					/* Add the quote to the argument if it is not
					 * the outermost set.
					 */
					addQuoteToArg = (quotestack < qp);
					
					/* It's extremely unlikely, but prevent over-writing
					 * the quotestack if the user has an extremely
					 * complicated command line.
					 */
					if (qp < (quotestack + kMaxQuotePairs))
						*++qp = *src;
					else {
						err = kMavErrTooManyQuotePairs;
						goto done;
					}
				}
				if (addQuoteToArg)
					goto copyCharToArg;
				else
					++src;
			} else if (qp == quotestack) {
				/* Take action on some special characters, as long
				 * as we aren't in the middle of a quoted argument.
				 */
				if (isspace(*src)) {
					/* End an argument in progress if we encounter
					 * delimiting whitespace, unless this whitespace
					 * is being enclosed within quotes.
					 */
					*dst++ = 0;
					break;
				} else if (*src == '>') {
					/* The user wants to redirect stdout out to a file. */
					if (*curArg != 0) {
						/* End an argument in progress.  We'll be back
						 * at the statement above in the next loop
						 * iteration, but we'll take the 'else'
						 * branch next time since it will look like we
						 * had an empy argument.
						 */
						*dst++ = 0;	
					} else {
						/* Make sure the user doesn't try to redirect
						 * to 2 different files.
						 */
						if ((*clp->outFileName == 0) && (prevArgType == kRegularArg)) {
							++src;	/* Skip the redirection symbol (>). */
							prevArgType = kReDirectOutArg;
							if (*src == '>') {
								/* If there we had >>, we want to append
								 * to a file, not overwrite it.
								 */
								++src;	/* Skip this one also. */
								prevArgType = kReDirectOutAppendArg;
							}
						} else {
							err = kMavErrTooManyReDirections;
							goto done;
						}
					}
					break;
					/* End '>' */
				} else if (*src == '<') {
					err = kMavErrNoReDirectedInput;
					goto done;
					/* End '<' */
				} else if (*src == '|') {
					/* The rest of this line is an another whole
					 * sub-command line, so we copy everything beyond
					 * the | into the string pipeCmd.
					 */

					/* But first check if we had started an argument,
					 * and if we did, go ahead and
					 * finish it.
					 */
					if (dst > curArg)
						*dst++ = 0;
					else {
						/* We have an empty argument, so get rid of it.
						 * To do that, we prevent the arg counter from
						 * advancing to the next item by decrementing
						 * it now, and letting the loop counter
						 * re-increment it, thus having the effect of
						 * not adding anything at all.
						 */
						--argc;
					}

					/* It would be an error to try and >redirect and
					 * |pipe out both, so check for this first by
					 * making sure the outfile is empty.
					 */
					if (*clp->outFileName != 0) {
						err = kMavErrBothPipeAndReDirOut;
						goto done;
					}

					/* Now start writing to the pipe string. */
					dst = clp->pipeCmdLine;
					src++;		/* Don't copy the | character... */
					while (isspace(*src))	/* ...or whitespace. */
						src++;
					while ((*src != '\0') && (*src != '\n'))
						*dst++ = *src++;
					*dst = 0;
					
					/* Ensure the user gave us something to pipe into. */
					if (dst == clp->pipeCmdLine)
						err = kMavErrNoPipeCommand;
					break;
					/* End '|' */
				} else if (*src == '#') {
					/* Ignore everything after the #comment character. */
					*src = 0;
					
					/* If we had started an argument, go ahead and
					 * finish it.
					 */
					if (dst > curArg)
						*dst++ = 0;
					else {
						/* We have an empty argument, so get rid of it.
						 * To do that, we prevent the arg counter from
						 * advancing to the next item by decrementing
						 * it now, and letting the loop counter
						 * re-increment it, thus having the effect of
						 * not adding anything at all.
						 */
						--argc;
					}
					break;
					/* End '#' */
				} else if (*src == '!') {
					/* We need a special case when the user want to
					 * spawn a shell command.  We need argument 0 to
					 * be just an !.
					 */
					if (dst == argv[0]) {
						/* Form argument one, then break. */
						*dst++ = *src++;
						*dst++ = 0;
						break;
					} else {
						/* Otherwise pretend that ! is a normal character. */
						goto copyCharToArg;
					}
					/* End '!' */
				} else {
					/* We have a regular character, not within a
					 * quoted string, to copy to an argument.
					 */
					goto copyCharToArg;
				}
				/* End if not within a quoted string. */
			} else {
				/* If we get to this point (naturally), we are within
				 * set of quotes, so just copy everything in between
				 * them to the argument.
				 */
				 
copyCharToArg:	/* Add this character to the current token. */
				c = *src++;
copyCharToArg2:
				*dst++ = c;
			}
			/* End collecting an argument. */
		}
		/* End collecting all arguments. */
	}

done:

	/* Probably should not act on lines with syntax errors. */
	if (err)
		argc = 0;
	clp->argCount = argc;
	argv[argc] = NULL;
	
	/* Keep a copy of the original command line, for commands that
	 * need to peek at it.
	 */
	argv[argc + 1] = str;
	argv[argc + 2] = (char *) clp;
	argv[argc + 3] = NULL;	/* This will stay NULL. */

	clp->err = err;
	clp->errStr = gMAVErrStrings[err];
	return err;
}	/* MakeArgVector */




/* This routine scans a command line and expands dollar-variables, like
 * $2 (to denote the second argument), given a previously formed argument
 * vector.  Why isn't this a part of the MakeArgVector() routine, above?
 * Because MakeArgVector() quits after it finds a |;  Because we only
 * need to do this for macros, so don't waste time doing it if it isn't
 * necessary;  and because MakeArgVector() is already quite large.
 */
void ExpandDollarVariables(char *cmdLine, size_t sz, int argc, char **argv)
{
	char *dst, *cp, *cp2, *start;
	char s[32];
	int argNum, argNum2;
	size_t dstLen, amtToMove;
	long amtToShift;

	/* We need to create a buffer large enough to hold a possibly long
	 * string consisting of several arguments cat'ted together.
	 */
	dst = (char *) malloc(sz);
	if ((dst != NULL) && (argc > 0)) {
		/* Scan the line looking for trouble (i.e. $vars). */
		for (cp = cmdLine; ; cp++) {
			/* Allow the user to insert a $ if she needs to, without
			 * having our routine trying to expand it into something.
			 */
			if (*cp == '\\') {
				++cp;	/* skip backslash. */
				if (!*cp)
					break;	/* no \, then 0 please. */
				++cp;	/* skip thing being escaped. */
			}
			if (!*cp)
				break;	/* Done. */
			if (*cp == '$') {
				start = cp;
				*dst = 0;
				/* Now determine whether it is the simple form,
				 * like $3, or a complex form, which requires parentheses,
				 * such as:
				 *   $(1,2,4)  :  Insert arguments one, two, and four.
				 *   $(1-4)    :  Arguments one through four inclusive.
				 *   $(3+)     :  Arg 3, and all args after it.
				 * or a special form, such as:
				 *   $*        :  All arguments (except 0).
				 *   $@        :  All arguments (except 0), each "quoted."
				 */
				++cp;	/* skip $. */
				if (*cp == '(') {
					/* Complex form. */
					++cp;	/* Skip left paren. */
					
					while (1) {
						if (!*cp) break;
						if (*cp == ')') {
							++cp;
							break;
						}
						/* Collect the first (and maybe only) number. */
						for (cp2=s; isdigit(*cp); )
							*cp2++ = *cp++;
						*cp2 = 0;
						argNum = atoi(s);
						if (argNum >= 0 && argNum < argc) {
							if (!*dst)
								strncpy(dst, argv[argNum], sz);
							else {
								strncat(dst, " ", sz);							
								strncat(dst, argv[argNum], sz);							
							}
							if (*cp == '-') {
								/* We have a range. */
								++cp;	/* Skip dash. */
								/* Collect the second number. */
								for (cp2=s; isdigit(*cp); )
									*cp2++ = *cp++;
								*cp2 = 0;
								argNum2 = atoi(s);
								
								/* Make sure we don't copy too many. */
								if (argNum2 >= argc)
									argNum2 = argc - 1;
								if (argNum2 > argNum)
									goto copyRest;
								/* End range. */
							} else if (*cp == '+') {
								++cp;
								argNum2 = argc - 1;
							copyRest:
								for (++argNum; argNum <= argNum2; ++argNum) {
									strncat(dst, " ", sz);
									strncat(dst, argv[argNum], sz);
								}
							}
							/* End if the argument number was valid. */
						}
						/* Now see if we have a comma.  If we have a comma,
						 * we can do some more.  Otherwise it is either a
						 * bad char or a right paren, so we'll break out.
						 */
						if (*cp == ',')
							++cp;
						/* End loop between parens. */
					}
				} else if (isdigit(*cp)) {
					/* Simple form. */
					for (cp2=s; isdigit(*cp); )
						*cp2++ = *cp++;
					*cp2 = 0;
					argNum = atoi(s);
					if (argNum >= 0 && argNum < argc)
						strncpy(dst, argv[argNum], sz);
				} else if (*cp == '*') {
					/* Insert all arguments, except 0. */
					++cp;	/* Skip *. */
					argNum = 1;
					argNum2 = argc - 1;
					if (argNum <= argNum2)
						strncpy(dst, argv[argNum], sz);
					for (++argNum; argNum <= argNum2; ++argNum) {
						strncat(dst, " ", sz);
						strncat(dst, argv[argNum], sz);
					}
				} else if (*cp == '@') {
					/* Insert all arguments, except 0, each quoted. */
					++cp;	/* Skip @. */
					argNum = 1;
					argNum2 = argc - 1;
					if (argNum <= argNum2) {
						strncpy(dst, "\"", sz);
						strncat(dst, argv[argNum], sz);
						strncat(dst, "\"", sz);
					}
					for (++argNum; argNum <= argNum2; ++argNum) {
						strncat(dst, " \"", sz);
						strncat(dst, argv[argNum], sz);
						strncat(dst, "\"", sz);
					}
				}
				dstLen = strlen(dst);
				
				/* This is the number of bytes we need to move the
				 * rest of the line down, so we can insert the
				 * variable's value, which would be the dst pointer.
				 */
				amtToShift = dstLen - (cp - start);
				
				/* Don't bother moving if we don't have to. */
				if (amtToShift != 0) {
					amtToMove = (size_t) (
						(long)sz - (long)(cp - cmdLine) - amtToShift - 1);
					/* Move the rest of the line down, so we can do
					 * the insertion without overwriting any of the
					 * original string.
					 */
					MEMMOVE(cp + amtToShift, cp, amtToMove);
				}
				
				/* Copy the value of the variable into the space we
				 * created for it.
				 */
				memcpy(start, dst, dstLen);
				
				/* Adjust the scan pointer so it points after the stuff
				 * we just wrote.
				 */
				cp = start + amtToShift;
			}
			
		}
		/* It's possible (but extremely unlikely) that the null terminator
		 * got overwritten when we did an insertion.  This could happen
		 * if the command line was very close to "full," or if the stuff
		 * we inserted was long.
		 */
		cmdLine[sz - 1] = 0;

		free(dst);
	}
}	/* ExpandDollarVariables */

/* eof makeargv.c */