xref: /onnv-gate/usr/src/lib/krb5/ss/parse.c (revision 2881)
10Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
20Sstevel@tonic-gate 
30Sstevel@tonic-gate /*
40Sstevel@tonic-gate  * Copyright 1987, 1988 by MIT Student Information Processing Board
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  * For copyright info, see copyright.h.
70Sstevel@tonic-gate  */
80Sstevel@tonic-gate 
90Sstevel@tonic-gate #include "ss_internal.h"
100Sstevel@tonic-gate #include "copyright.h"
11*2881Smp153739 #include <errno.h>
120Sstevel@tonic-gate 
130Sstevel@tonic-gate enum parse_mode { WHITESPACE, TOKEN, QUOTED_STRING };
140Sstevel@tonic-gate 
150Sstevel@tonic-gate /*
160Sstevel@tonic-gate  * parse(line_ptr, argc_ptr)
170Sstevel@tonic-gate  *
180Sstevel@tonic-gate  * Function:
190Sstevel@tonic-gate  *      Parses line, dividing at whitespace, into tokens, returns
200Sstevel@tonic-gate  *      the "argc" and "argv" values.
210Sstevel@tonic-gate  * Arguments:
220Sstevel@tonic-gate  *      line_ptr (char *)
230Sstevel@tonic-gate  *              Pointer to text string to be parsed.
240Sstevel@tonic-gate  *      argc_ptr (int *)
250Sstevel@tonic-gate  *              Where to put the "argc" (number of tokens) value.
260Sstevel@tonic-gate  * Returns:
270Sstevel@tonic-gate  *      argv (char **)
280Sstevel@tonic-gate  *              Series of pointers to parsed tokens.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #define NEW_ARGV(old,n) (char **)realloc((char *)old,\
320Sstevel@tonic-gate 					 (unsigned)(n+2)*sizeof(char*))
330Sstevel@tonic-gate 
340Sstevel@tonic-gate char **ss_parse (sci_idx, line_ptr, argc_ptr)
350Sstevel@tonic-gate     int sci_idx;
360Sstevel@tonic-gate     register char *line_ptr;
370Sstevel@tonic-gate     int *argc_ptr;
380Sstevel@tonic-gate {
390Sstevel@tonic-gate     register char **argv, *cp;
400Sstevel@tonic-gate     register int argc;
410Sstevel@tonic-gate     register enum parse_mode parse_mode;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate     argv = (char **) malloc (sizeof(char *));
440Sstevel@tonic-gate     if (argv == (char **)NULL) {
450Sstevel@tonic-gate 	ss_error(sci_idx, errno, "Can't allocate storage");
460Sstevel@tonic-gate 	*argc_ptr = 0;
470Sstevel@tonic-gate 	return(argv);
480Sstevel@tonic-gate     }
490Sstevel@tonic-gate     *argv = (char *)NULL;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate     argc = 0;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate     parse_mode = WHITESPACE;	/* flushing whitespace */
540Sstevel@tonic-gate     cp = line_ptr;		/* cp is for output */
550Sstevel@tonic-gate     while (1) {
560Sstevel@tonic-gate #ifdef DEBUG
570Sstevel@tonic-gate 	{
58*2881Smp153739 	    printf ("character `%c', mode %d\n", *line_ptr, parse_mode);
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate 	while (parse_mode == WHITESPACE) {
620Sstevel@tonic-gate 	    if (*line_ptr == '\0')
630Sstevel@tonic-gate 		goto end_of_line;
640Sstevel@tonic-gate 	    if (*line_ptr == ' ' || *line_ptr == '\t') {
650Sstevel@tonic-gate 		line_ptr++;
660Sstevel@tonic-gate 		continue;
670Sstevel@tonic-gate 	    }
680Sstevel@tonic-gate 	    if (*line_ptr == '"') {
690Sstevel@tonic-gate 		/* go to quoted-string mode */
700Sstevel@tonic-gate 		parse_mode = QUOTED_STRING;
710Sstevel@tonic-gate 		cp = line_ptr++;
720Sstevel@tonic-gate 		argv = NEW_ARGV (argv, argc);
730Sstevel@tonic-gate 		argv[argc++] = cp;
740Sstevel@tonic-gate 		argv[argc] = NULL;
750Sstevel@tonic-gate 	    }
760Sstevel@tonic-gate 	    else {
770Sstevel@tonic-gate 		/* random-token mode */
780Sstevel@tonic-gate 		parse_mode = TOKEN;
790Sstevel@tonic-gate 		cp = line_ptr;
800Sstevel@tonic-gate 		argv = NEW_ARGV (argv, argc);
810Sstevel@tonic-gate 		argv[argc++] = line_ptr;
820Sstevel@tonic-gate 		argv[argc] = NULL;
830Sstevel@tonic-gate 	    }
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	while (parse_mode == TOKEN) {
860Sstevel@tonic-gate 	    if (*line_ptr == '\0') {
870Sstevel@tonic-gate 		*cp++ = '\0';
880Sstevel@tonic-gate 		goto end_of_line;
890Sstevel@tonic-gate 	    }
900Sstevel@tonic-gate 	    else if (*line_ptr == ' ' || *line_ptr == '\t') {
910Sstevel@tonic-gate 		*cp++ = '\0';
920Sstevel@tonic-gate 		line_ptr++;
930Sstevel@tonic-gate 		parse_mode = WHITESPACE;
940Sstevel@tonic-gate 	    }
950Sstevel@tonic-gate 	    else if (*line_ptr == '"') {
960Sstevel@tonic-gate 		line_ptr++;
970Sstevel@tonic-gate 		parse_mode = QUOTED_STRING;
980Sstevel@tonic-gate 	    }
990Sstevel@tonic-gate 	    else {
1000Sstevel@tonic-gate 		*cp++ = *line_ptr++;
1010Sstevel@tonic-gate 	    }
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	while (parse_mode == QUOTED_STRING) {
1040Sstevel@tonic-gate 	    if (*line_ptr == '\0') {
1050Sstevel@tonic-gate 		ss_error (sci_idx, 0,
1060Sstevel@tonic-gate 			  "Unbalanced quotes in command line");
1070Sstevel@tonic-gate 		free (argv);
1080Sstevel@tonic-gate 		*argc_ptr = 0;
1090Sstevel@tonic-gate 		return NULL;
1100Sstevel@tonic-gate 	    }
1110Sstevel@tonic-gate 	    else if (*line_ptr == '"') {
1120Sstevel@tonic-gate 		if (*++line_ptr == '"') {
1130Sstevel@tonic-gate 		    *cp++ = '"';
1140Sstevel@tonic-gate 		    line_ptr++;
1150Sstevel@tonic-gate 		}
1160Sstevel@tonic-gate 		else {
1170Sstevel@tonic-gate 		    parse_mode = TOKEN;
1180Sstevel@tonic-gate 		}
1190Sstevel@tonic-gate 	    }
1200Sstevel@tonic-gate 	    else {
1210Sstevel@tonic-gate 		*cp++ = *line_ptr++;
1220Sstevel@tonic-gate 	    }
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate     }
1250Sstevel@tonic-gate end_of_line:
1260Sstevel@tonic-gate     *argc_ptr = argc;
1270Sstevel@tonic-gate #ifdef DEBUG
128*2881Smp153739     {
129*2881Smp153739 	int i;
130*2881Smp153739 	printf ("argc = %d\n", argc);
131*2881Smp153739 	for (i = 0; i <= argc; i++)
132*2881Smp153739 	    printf ("\targv[%2d] = `%s'\n", i,
133*2881Smp153739 		    argv[i] ? argv[i] : "<NULL>");
134*2881Smp153739     }
1350Sstevel@tonic-gate #endif
1360Sstevel@tonic-gate     return(argv);
1370Sstevel@tonic-gate }
138