2 * Copyright 1987, 1988 by MIT Student Information Processing Board
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose is hereby granted, provided that
6 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
7 * advertising or publicity pertaining to distribution of the software
8 * without specific, written prior permission. M.I.T. and the
9 * M.I.T. S.I.P.B. make no representations about the suitability of
10 * this software for any purpose. It is provided "as is" without
11 * express or implied warranty.
23 #include "ss_internal.h"
25 enum parse_mode { WHITESPACE, TOKEN, QUOTED_STRING };
28 * parse(line_ptr, argc_ptr)
31 * Parses line, dividing at whitespace, into tokens, returns
32 * the "argc" and "argv" values.
35 * Pointer to text string to be parsed.
37 * Where to put the "argc" (number of tokens) value.
40 * Series of pointers to parsed tokens.
43 #define NEW_ARGV(old,n) (char **)realloc((char *)old,\
44 (unsigned)(n+2)*sizeof(char*))
46 char **ss_parse(int sci_idx, register char *line_ptr, int *argc_ptr)
48 register char **argv, *cp;
50 register enum parse_mode parse_mode;
52 argv = (char **) malloc (sizeof(char *));
53 if (argv == (char **)NULL) {
54 ss_error(sci_idx, errno, "Can't allocate storage");
62 parse_mode = WHITESPACE; /* flushing whitespace */
63 cp = line_ptr; /* cp is for output */
67 printf ("character `%c', mode %d\n", *line_ptr, parse_mode);
70 while (parse_mode == WHITESPACE) {
71 if (*line_ptr == '\0')
73 if (*line_ptr == ' ' || *line_ptr == '\t') {
77 if (*line_ptr == '"') {
78 /* go to quoted-string mode */
79 parse_mode = QUOTED_STRING;
81 argv = NEW_ARGV (argv, argc);
86 /* random-token mode */
89 argv = NEW_ARGV (argv, argc);
94 argv[argc++] = line_ptr;
98 while (parse_mode == TOKEN) {
99 if (*line_ptr == '\0') {
103 else if (*line_ptr == ' ' || *line_ptr == '\t') {
106 parse_mode = WHITESPACE;
108 else if (*line_ptr == '"') {
110 parse_mode = QUOTED_STRING;
116 while (parse_mode == QUOTED_STRING) {
117 if (*line_ptr == '\0') {
118 ss_error (sci_idx, 0,
119 "Unbalanced quotes in command line");
124 else if (*line_ptr == '"') {
125 if (*++line_ptr == '"') {
143 printf ("argc = %d\n", argc);
144 for (i = 0; i <= argc; i++)
145 printf ("\targv[%2d] = `%s'\n", i,
146 argv[i] ? argv[i] : "<NULL>");