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.
22 #include "ss_internal.h"
24 enum parse_mode { WHITESPACE, TOKEN, QUOTED_STRING };
27 * parse(line_ptr, argc_ptr)
30 * Parses line, dividing at whitespace, into tokens, returns
31 * the "argc" and "argv" values.
34 * Pointer to text string to be parsed.
36 * Where to put the "argc" (number of tokens) value.
39 * Series of pointers to parsed tokens.
42 #define NEW_ARGV(old,n) (char **)realloc((char *)old,\
43 (unsigned)(n+2)*sizeof(char*))
45 char **ss_parse (sci_idx, line_ptr, argc_ptr)
47 register char *line_ptr;
50 register char **argv, *cp;
52 register enum parse_mode parse_mode;
54 argv = (char **) malloc (sizeof(char *));
55 if (argv == (char **)NULL) {
56 ss_error(sci_idx, errno, "Can't allocate storage");
64 parse_mode = WHITESPACE; /* flushing whitespace */
65 cp = line_ptr; /* cp is for output */
69 printf ("character `%c', mode %d\n", *line_ptr, parse_mode);
72 while (parse_mode == WHITESPACE) {
73 if (*line_ptr == '\0')
75 if (*line_ptr == ' ' || *line_ptr == '\t') {
79 if (*line_ptr == '"') {
80 /* go to quoted-string mode */
81 parse_mode = QUOTED_STRING;
83 argv = NEW_ARGV (argv, argc);
88 /* random-token mode */
91 argv = NEW_ARGV (argv, argc);
92 argv[argc++] = line_ptr;
96 while (parse_mode == TOKEN) {
97 if (*line_ptr == '\0') {
101 else if (*line_ptr == ' ' || *line_ptr == '\t') {
104 parse_mode = WHITESPACE;
106 else if (*line_ptr == '"') {
108 parse_mode = QUOTED_STRING;
114 while (parse_mode == QUOTED_STRING) {
115 if (*line_ptr == '\0') {
116 ss_error (sci_idx, 0,
117 "Unbalanced quotes in command line");
122 else if (*line_ptr == '"') {
123 if (*++line_ptr == '"') {
141 printf ("argc = %d\n", argc);
142 for (i = 0; i <= argc; i++)
143 printf ("\targv[%2d] = `%s'\n", i,
144 argv[i] ? argv[i] : "<NULL>");