]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/ss/parse.c
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 (sci_idx
, line_ptr
, argc_ptr
)
48 register char *line_ptr
;
51 register char **argv
, *cp
;
53 register enum parse_mode parse_mode
;
55 argv
= (char **) malloc (sizeof(char *));
56 if (argv
== (char **)NULL
) {
57 ss_error(sci_idx
, errno
, "Can't allocate storage");
65 parse_mode
= WHITESPACE
; /* flushing whitespace */
66 cp
= line_ptr
; /* cp is for output */
70 printf ("character `%c', mode %d\n", *line_ptr
, parse_mode
);
73 while (parse_mode
== WHITESPACE
) {
74 if (*line_ptr
== '\0')
76 if (*line_ptr
== ' ' || *line_ptr
== '\t') {
80 if (*line_ptr
== '"') {
81 /* go to quoted-string mode */
82 parse_mode
= QUOTED_STRING
;
84 argv
= NEW_ARGV (argv
, argc
);
89 /* random-token mode */
92 argv
= NEW_ARGV (argv
, argc
);
93 argv
[argc
++] = line_ptr
;
97 while (parse_mode
== TOKEN
) {
98 if (*line_ptr
== '\0') {
102 else if (*line_ptr
== ' ' || *line_ptr
== '\t') {
105 parse_mode
= WHITESPACE
;
107 else if (*line_ptr
== '"') {
109 parse_mode
= QUOTED_STRING
;
115 while (parse_mode
== QUOTED_STRING
) {
116 if (*line_ptr
== '\0') {
117 ss_error (sci_idx
, 0,
118 "Unbalanced quotes in command line");
123 else if (*line_ptr
== '"') {
124 if (*++line_ptr
== '"') {
142 printf ("argc = %d\n", argc
);
143 for (i
= 0; i
<= argc
; i
++)
144 printf ("\targv[%2d] = `%s'\n", i
,
145 argv
[i
] ? argv
[i
] : "<NULL>");