xref: /original-bsd/usr.bin/uucp/libuu/getargs.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)getargs.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "uucp.h"
13 
14 /*LINTLIBRARY*/
15 
16 /*
17  *	getargs  -  this routine will generate a vector of
18  *	pointers (arps) to the substrings in string "s".
19  *	Each substring is separated by blanks and/or tabs.
20  *
21  *	If FANCYARGS is defined, you get the following:
22  *	Strings containing blanks may be specified by quoting,
23  *	in a manner similar to using the shell.
24  *	Control characters are entered by ^X where X is any
25  *	character; ^? gets you a rubout and ^^ is a real ^.
26  *	Warning (rti!trt): I doubt FANCYARGS is wise, since getargs
27  *	is used all over the place.  Its features may be useful
28  *	but a separate fancy_getargs() should be called instead.
29  *
30  *	return - the number of subfields, or -1 if >= maxargs.
31  */
32 
33 getargs(s, arps, maxargs)
34 register char *s;
35 char *arps[];
36 int maxargs;
37 {
38 	register int i;
39 #ifdef	FANCYARGS
40 	register char *sp;
41 	register char qchar;
42 #endif
43 
44 	i = 0;
45 #ifndef	FANCYARGS
46 	while (i < maxargs) {
47 		while (*s == ' ' || *s == '\t')
48 			*s++ = '\0';
49 		if (*s == '\n')
50 			*s = '\0';
51 		if (*s == '\0')
52 			break;
53 		arps[i++] = s++;
54 		while (*s != '\0' && *s != ' '
55 			&& *s != '\t' && *s != '\n')
56 				s++;
57 	}
58 #else
59 	while (i < maxargs) {
60 		while (*s == ' ' || *s == '\t')
61 			++s;
62 		if (*s == '\n' || *s == '\0')
63 			break;
64 		arps[i++] = sp = s;
65 		qchar = 0;
66 		while(*s != '\0'  &&  *s != '\n') {
67 			if (qchar == 0 && (*s == ' ' || *s == '\t')) {
68 				++s;
69 				break;
70 			}
71 			switch(*s) {
72 			default:
73 				*sp++ = *s++;
74 				break;
75 			case '^':
76 				if(*++s == '^')
77 					*sp++ = '^';
78 				else if(*s == '?')
79 					*sp++ = 0177;
80 				else
81 					*sp++ = *s & 037;
82 				s++;
83 				break;
84 			case '"':
85 			case '\'':
86 				if(qchar == *s) {
87 					qchar = 0;
88 					++s;
89 					break;
90 				}
91 				if(qchar)
92 					*sp++ = *s++;
93 				else
94 					qchar = *s++;
95 				break;
96 			}
97 		}
98 		*sp++ = 0;
99 	}
100 #endif
101 	if (i >= maxargs)
102 		return FAIL;
103 	arps[i] = NULL;
104 	return i;
105 }
106