xref: /titanic_41/usr/src/lib/krb5/ss/parse.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate  * Copyright 1987, 1988 by MIT Student Information Processing Board
5*7c478bd9Sstevel@tonic-gate  *
6*7c478bd9Sstevel@tonic-gate  * For copyright info, see copyright.h.
7*7c478bd9Sstevel@tonic-gate  */
8*7c478bd9Sstevel@tonic-gate 
9*7c478bd9Sstevel@tonic-gate #include "ss_internal.h"
10*7c478bd9Sstevel@tonic-gate #include "copyright.h"
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate /* global indicating if we should be printing debug messages */
13*7c478bd9Sstevel@tonic-gate extern int g_displayDebugSS;
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate enum parse_mode { WHITESPACE, TOKEN, QUOTED_STRING };
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate /*
19*7c478bd9Sstevel@tonic-gate  * parse(line_ptr, argc_ptr)
20*7c478bd9Sstevel@tonic-gate  *
21*7c478bd9Sstevel@tonic-gate  * Function:
22*7c478bd9Sstevel@tonic-gate  *      Parses line, dividing at whitespace, into tokens, returns
23*7c478bd9Sstevel@tonic-gate  *      the "argc" and "argv" values.
24*7c478bd9Sstevel@tonic-gate  * Arguments:
25*7c478bd9Sstevel@tonic-gate  *      line_ptr (char *)
26*7c478bd9Sstevel@tonic-gate  *              Pointer to text string to be parsed.
27*7c478bd9Sstevel@tonic-gate  *      argc_ptr (int *)
28*7c478bd9Sstevel@tonic-gate  *              Where to put the "argc" (number of tokens) value.
29*7c478bd9Sstevel@tonic-gate  * Returns:
30*7c478bd9Sstevel@tonic-gate  *      argv (char **)
31*7c478bd9Sstevel@tonic-gate  *              Series of pointers to parsed tokens.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #define NEW_ARGV(old,n) (char **)realloc((char *)old,\
35*7c478bd9Sstevel@tonic-gate 					 (unsigned)(n+2)*sizeof(char*))
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate char **ss_parse (sci_idx, line_ptr, argc_ptr)
38*7c478bd9Sstevel@tonic-gate     int sci_idx;
39*7c478bd9Sstevel@tonic-gate     register char *line_ptr;
40*7c478bd9Sstevel@tonic-gate     int *argc_ptr;
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate     register char **argv, *cp;
43*7c478bd9Sstevel@tonic-gate     register int argc;
44*7c478bd9Sstevel@tonic-gate     register enum parse_mode parse_mode;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate     argv = (char **) malloc (sizeof(char *));
47*7c478bd9Sstevel@tonic-gate     if (argv == (char **)NULL) {
48*7c478bd9Sstevel@tonic-gate 	ss_error(sci_idx, errno, "Can't allocate storage");
49*7c478bd9Sstevel@tonic-gate 	*argc_ptr = 0;
50*7c478bd9Sstevel@tonic-gate 	return(argv);
51*7c478bd9Sstevel@tonic-gate     }
52*7c478bd9Sstevel@tonic-gate     *argv = (char *)NULL;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate     argc = 0;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate     parse_mode = WHITESPACE;	/* flushing whitespace */
57*7c478bd9Sstevel@tonic-gate     cp = line_ptr;		/* cp is for output */
58*7c478bd9Sstevel@tonic-gate     while (1) {
59*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
60*7c478bd9Sstevel@tonic-gate 	{
61*7c478bd9Sstevel@tonic-gate 		if (g_displayDebugSS)
62*7c478bd9Sstevel@tonic-gate 			printf ("character `%c', mode %d\n",
63*7c478bd9Sstevel@tonic-gate 				*line_ptr, parse_mode);
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate #endif
66*7c478bd9Sstevel@tonic-gate 	while (parse_mode == WHITESPACE) {
67*7c478bd9Sstevel@tonic-gate 	    if (*line_ptr == '\0')
68*7c478bd9Sstevel@tonic-gate 		goto end_of_line;
69*7c478bd9Sstevel@tonic-gate 	    if (*line_ptr == ' ' || *line_ptr == '\t') {
70*7c478bd9Sstevel@tonic-gate 		line_ptr++;
71*7c478bd9Sstevel@tonic-gate 		continue;
72*7c478bd9Sstevel@tonic-gate 	    }
73*7c478bd9Sstevel@tonic-gate 	    if (*line_ptr == '"') {
74*7c478bd9Sstevel@tonic-gate 		/* go to quoted-string mode */
75*7c478bd9Sstevel@tonic-gate 		parse_mode = QUOTED_STRING;
76*7c478bd9Sstevel@tonic-gate 		cp = line_ptr++;
77*7c478bd9Sstevel@tonic-gate 		argv = NEW_ARGV (argv, argc);
78*7c478bd9Sstevel@tonic-gate 		argv[argc++] = cp;
79*7c478bd9Sstevel@tonic-gate 		argv[argc] = NULL;
80*7c478bd9Sstevel@tonic-gate 	    }
81*7c478bd9Sstevel@tonic-gate 	    else {
82*7c478bd9Sstevel@tonic-gate 		/* random-token mode */
83*7c478bd9Sstevel@tonic-gate 		parse_mode = TOKEN;
84*7c478bd9Sstevel@tonic-gate 		cp = line_ptr;
85*7c478bd9Sstevel@tonic-gate 		argv = NEW_ARGV (argv, argc);
86*7c478bd9Sstevel@tonic-gate 		argv[argc++] = line_ptr;
87*7c478bd9Sstevel@tonic-gate 		argv[argc] = NULL;
88*7c478bd9Sstevel@tonic-gate 	    }
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 	while (parse_mode == TOKEN) {
91*7c478bd9Sstevel@tonic-gate 	    if (*line_ptr == '\0') {
92*7c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
93*7c478bd9Sstevel@tonic-gate 		goto end_of_line;
94*7c478bd9Sstevel@tonic-gate 	    }
95*7c478bd9Sstevel@tonic-gate 	    else if (*line_ptr == ' ' || *line_ptr == '\t') {
96*7c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
97*7c478bd9Sstevel@tonic-gate 		line_ptr++;
98*7c478bd9Sstevel@tonic-gate 		parse_mode = WHITESPACE;
99*7c478bd9Sstevel@tonic-gate 	    }
100*7c478bd9Sstevel@tonic-gate 	    else if (*line_ptr == '"') {
101*7c478bd9Sstevel@tonic-gate 		line_ptr++;
102*7c478bd9Sstevel@tonic-gate 		parse_mode = QUOTED_STRING;
103*7c478bd9Sstevel@tonic-gate 	    }
104*7c478bd9Sstevel@tonic-gate 	    else {
105*7c478bd9Sstevel@tonic-gate 		*cp++ = *line_ptr++;
106*7c478bd9Sstevel@tonic-gate 	    }
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	while (parse_mode == QUOTED_STRING) {
109*7c478bd9Sstevel@tonic-gate 	    if (*line_ptr == '\0') {
110*7c478bd9Sstevel@tonic-gate 		ss_error (sci_idx, 0,
111*7c478bd9Sstevel@tonic-gate 			  "Unbalanced quotes in command line");
112*7c478bd9Sstevel@tonic-gate 		free (argv);
113*7c478bd9Sstevel@tonic-gate 		*argc_ptr = 0;
114*7c478bd9Sstevel@tonic-gate 		return NULL;
115*7c478bd9Sstevel@tonic-gate 	    }
116*7c478bd9Sstevel@tonic-gate 	    else if (*line_ptr == '"') {
117*7c478bd9Sstevel@tonic-gate 		if (*++line_ptr == '"') {
118*7c478bd9Sstevel@tonic-gate 		    *cp++ = '"';
119*7c478bd9Sstevel@tonic-gate 		    line_ptr++;
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 		else {
122*7c478bd9Sstevel@tonic-gate 		    parse_mode = TOKEN;
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 	    }
125*7c478bd9Sstevel@tonic-gate 	    else {
126*7c478bd9Sstevel@tonic-gate 		*cp++ = *line_ptr++;
127*7c478bd9Sstevel@tonic-gate 	    }
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate     }
130*7c478bd9Sstevel@tonic-gate end_of_line:
131*7c478bd9Sstevel@tonic-gate     *argc_ptr = argc;
132*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if (g_displayDebugSS)
135*7c478bd9Sstevel@tonic-gate     	{
136*7c478bd9Sstevel@tonic-gate 		int i;
137*7c478bd9Sstevel@tonic-gate 		printf ("argc = %d\n", argc);
138*7c478bd9Sstevel@tonic-gate 		for (i = 0; i <= argc; i++)
139*7c478bd9Sstevel@tonic-gate 	    		printf ("\targv[%2d] = `%s'\n", i,
140*7c478bd9Sstevel@tonic-gate 		    		argv[i] ? argv[i] : "<NULL>");
141*7c478bd9Sstevel@tonic-gate     	}
142*7c478bd9Sstevel@tonic-gate #endif
143*7c478bd9Sstevel@tonic-gate     return(argv);
144*7c478bd9Sstevel@tonic-gate }
145