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