xref: /csrg-svn/usr.bin/uucp/libuu/getargs.c (revision 23602)
113646Ssam #ifndef lint
2*23602Sbloom static char sccsid[] = "@(#)getargs.c	5.3 (Berkeley) 06/19/85";
313646Ssam #endif
413646Ssam 
517835Sralph #include "uucp.h"
613646Ssam 
7*23602Sbloom /*LINTLIBRARY*/
8*23602Sbloom 
917835Sralph /*
1013646Ssam  *	getargs  -  this routine will generate a vector of
1113646Ssam  *	pointers (arps) to the substrings in string "s".
1213646Ssam  *	Each substring is separated by blanks and/or tabs.
1313646Ssam  *
1413646Ssam  *	If FANCYARGS is defined, you get the following:
1513646Ssam  *	Strings containing blanks may be specified by quoting,
1613646Ssam  *	in a manner similar to using the shell.
1713646Ssam  *	Control characters are entered by ^X where X is any
1813646Ssam  *	character; ^? gets you a rubout and ^^ is a real ^.
1913646Ssam  *	Warning (rti!trt): I doubt FANCYARGS is wise, since getargs
2013646Ssam  *	is used all over the place.  Its features may be useful
2113646Ssam  *	but a separate fancy_getargs() should be called instead.
2213646Ssam  *
2317835Sralph  *	return - the number of subfields, or -1 if >= maxargs.
2413646Ssam  */
2513646Ssam 
2617835Sralph getargs(s, arps, maxargs)
2713646Ssam register char *s;
2813646Ssam char *arps[];
2917835Sralph int maxargs;
3013646Ssam {
3113646Ssam 	register int i;
3213646Ssam #ifdef	FANCYARGS
3313646Ssam 	register char *sp;
3413646Ssam 	register char qchar;
3513646Ssam #endif
3613646Ssam 
3713646Ssam 	i = 0;
3813646Ssam #ifndef	FANCYARGS
3917835Sralph 	while (i < maxargs) {
4013646Ssam 		while (*s == ' ' || *s == '\t')
4113646Ssam 			*s++ = '\0';
4213646Ssam 		if (*s == '\n')
4313646Ssam 			*s = '\0';
4413646Ssam 		if (*s == '\0')
4513646Ssam 			break;
4613646Ssam 		arps[i++] = s++;
4713646Ssam 		while (*s != '\0' && *s != ' '
4813646Ssam 			&& *s != '\t' && *s != '\n')
4913646Ssam 				s++;
5013646Ssam 	}
5113646Ssam #else
5217835Sralph 	while (i < maxargs) {
5313646Ssam 		while (*s == ' ' || *s == '\t')
5413646Ssam 			++s;
5513646Ssam 		if (*s == '\n' || *s == '\0')
5613646Ssam 			break;
5713646Ssam 		arps[i++] = sp = s;
5813646Ssam 		qchar = 0;
5913646Ssam 		while(*s != '\0'  &&  *s != '\n') {
6013646Ssam 			if (qchar == 0 && (*s == ' ' || *s == '\t')) {
6113646Ssam 				++s;
6213646Ssam 				break;
6313646Ssam 			}
6413646Ssam 			switch(*s) {
6513646Ssam 			default:
6613646Ssam 				*sp++ = *s++;
6713646Ssam 				break;
6813646Ssam 			case '^':
6913646Ssam 				if(*++s == '^')
7013646Ssam 					*sp++ = '^';
7113646Ssam 				else if(*s == '?')
7213646Ssam 					*sp++ = 0177;
7313646Ssam 				else
7413646Ssam 					*sp++ = *s & 037;
7513646Ssam 				s++;
7613646Ssam 				break;
7713646Ssam 			case '"':
7813646Ssam 			case '\'':
7913646Ssam 				if(qchar == *s) {
8013646Ssam 					qchar = 0;
8113646Ssam 					++s;
8213646Ssam 					break;
8313646Ssam 				}
8413646Ssam 				if(qchar)
8513646Ssam 					*sp++ = *s++;
8613646Ssam 				else
8713646Ssam 					qchar = *s++;
8813646Ssam 				break;
8913646Ssam 			}
9013646Ssam 		}
9113646Ssam 		*sp++ = 0;
9213646Ssam 	}
9317835Sralph #endif
9417835Sralph 	if (i >= maxargs)
9517835Sralph 		return FAIL;
9613646Ssam 	arps[i] = NULL;
9717835Sralph 	return i;
9813646Ssam }
99