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