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