2 * Copyright 1987, 1988 by MIT Student Information Processing Board
4 * For copyright info, see copyright.h.
8 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
9 * Use is subject to license terms.
12 #include "ss_internal.h"
13 #include "copyright.h"
16 enum parse_mode
{ WHITESPACE
, TOKEN
, QUOTED_STRING
};
21 * ss_parse has been modified slightly from the original in two ways.
22 * 1) A new parameter "quiet" has been added which is used to silence
23 * error or warning messages.
24 * 2) ss_parse now returns an error status instead of argv - this is to
25 * allow an error to be distinguished from no tokens when parsing an empty
27 * Both of these changes allow ss_parse to be used during tab-completion.
31 * parse(line_ptr, argc_ptr)
34 * Parses line, dividing at whitespace, into tokens, returns
35 * the "argc" and "argv" values.
38 * Pointer to text string to be parsed.
40 * Where to put the "argc" (number of tokens) value.
42 * Where to put the series of pointers to parsed tokens.
44 * error (0 - success, non-zero on failure)
47 #define NEW_ARGV(old,n) (char **)realloc((char *)old,\
48 (unsigned)(n+2)*sizeof(char*))
50 int ss_parse (sci_idx
, line_ptr
, argc_ptr
, argv_ptr
, quiet
)
52 register char *line_ptr
;
57 register char **argv
, *cp
;
59 register enum parse_mode parse_mode
;
61 argv
= (char **) malloc (sizeof(char *));
62 if (argv
== (char **)NULL
) {
64 ss_error(sci_idx
, errno
, "Can't allocate storage");
73 parse_mode
= WHITESPACE
; /* flushing whitespace */
74 cp
= line_ptr
; /* cp is for output */
78 printf ("character `%c', mode %d\n", *line_ptr
, parse_mode
);
81 while (parse_mode
== WHITESPACE
) {
82 if (*line_ptr
== '\0')
84 if (*line_ptr
== ' ' || *line_ptr
== '\t') {
88 if (*line_ptr
== '"') {
89 /* go to quoted-string mode */
90 parse_mode
= QUOTED_STRING
;
92 argv
= NEW_ARGV (argv
, argc
);
97 /* random-token mode */
100 argv
= NEW_ARGV (argv
, argc
);
101 argv
[argc
++] = line_ptr
;
105 while (parse_mode
== TOKEN
) {
106 if (*line_ptr
== '\0') {
110 else if (*line_ptr
== ' ' || *line_ptr
== '\t') {
113 parse_mode
= WHITESPACE
;
115 else if (*line_ptr
== '"') {
117 parse_mode
= QUOTED_STRING
;
123 while (parse_mode
== QUOTED_STRING
) {
124 if (*line_ptr
== '\0') {
126 ss_error (sci_idx
, 0,
127 "Unbalanced quotes in command line");
133 else if (*line_ptr
== '"') {
134 if (*++line_ptr
== '"') {
152 printf ("argc = %d\n", argc
);
153 for (i
= 0; i
<= argc
; i
++)
154 printf ("\targv[%2d] = `%s'\n", i
,
155 argv
[i
] ? argv
[i
] : "<NULL>");