1772
|
1 /* getargs.c
|
|
2
|
|
3 This replaces the original parse.c. It is little more compact and with
|
|
4 a minor change it will parse quoted or unquoted arguments (double quotes,
|
|
5 ("). If there is nothing between two sets of quotes (""), the field is
|
|
6 replaced with the '\0' character and parsing continues. This routine was
|
|
7 originally posted to comp.lang.c by Steve Summit <scs@eskimo.c>. Thanks
|
|
8 to Steve for his okey dokey to use it in UUCPbb.
|
|
9
|
|
10 Quote and OS-9 mods by Bob Billson <bob@kc2wz.bubble.org> (REB)
|
|
11 */
|
|
12
|
|
13 /*
|
|
14 * takes a string (line) and builds an array of pointers to each word in it.
|
|
15 * words are separated by spaces or any control characters. At most maxargs
|
|
16 * pointers are calculated. \0's are inserted in line, so that each word
|
|
17 * becomes a string in its own right. The number of pointers (argc) is
|
|
18 * returned.
|
|
19 */
|
|
20
|
|
21 #ifndef ORIGINAL
|
|
22 #include "uucp.h"
|
|
23 #else
|
|
24 #include <stdio.h>
|
|
25 #endif
|
|
26
|
|
27 #ifndef ORIGINAL
|
|
28 #define iswhite(c) ((c)==' ' || (c)=='\t' || (c)=='\x0D' || (c)=='\x0A')
|
|
29 #else
|
|
30 #define iswhite(c) ((c)==' ' || (c)=='\t' || (c)=='\n') /* original code */
|
|
31 #endif
|
|
32
|
|
33 int getargs(argv, line, maxargs)
|
|
34 #ifndef ORIGINAL
|
|
35 char *argv[];
|
|
36 register char *line;
|
|
37 #else
|
|
38 register char *argv[];
|
|
39 register char *line;
|
|
40 #endif
|
|
41 int maxargs;
|
|
42 {
|
|
43 register int nargs = 0;
|
|
44 int firstquote = FALSE; /* added REB */
|
|
45
|
|
46 for(;;)
|
|
47 {
|
|
48 while(iswhite(*line))
|
|
49 line++;
|
|
50
|
|
51 if(*line == '\0')
|
|
52 {
|
|
53 if(nargs < maxargs) *argv = NULL;
|
|
54 return(nargs);
|
|
55 }
|
|
56 #ifndef ORIGINAL
|
|
57 if (*line == '"' && !firstquote) /* added REB */
|
|
58 {
|
|
59 firstquote = TRUE;
|
|
60 line++;
|
|
61 }
|
|
62 #endif
|
|
63 *argv++ = line;
|
|
64 nargs++;
|
|
65
|
|
66 #ifndef ORIGINAL
|
|
67 while (((!firstquote && !iswhite(*line)) /* added REB */
|
|
68 || (firstquote && *line != '"')) && *line != '\0')
|
|
69 #else
|
|
70 while(!iswhite(*line) && *line != '\0')
|
|
71 #endif
|
|
72 line++;
|
|
73
|
|
74 if(*line == '\0')
|
|
75 {
|
|
76 if(nargs < maxargs) *argv = NULL;
|
|
77 return(nargs);
|
|
78 }
|
|
79
|
|
80 #ifndef ORIGINAL
|
|
81 if(*line == '"') /* added REB */
|
|
82 firstquote = FALSE;
|
|
83 #endif
|
|
84
|
|
85 *line++ = '\0';
|
|
86 if(nargs == maxargs) return(nargs);
|
|
87 }
|
|
88 }
|
|
89
|
|
90
|
|
91
|
|
92 /************************************************************
|
|
93 ************************************************************
|
|
94 *** The following code is not part of getargs.c. This ***
|
|
95 *** code IS covered by the GNU General Public license. ***
|
|
96 ************************************************************
|
|
97 ************************************************************/
|
|
98
|
|
99 /* parse.c Routines to deal with parsing passed strings.
|
|
100 Copyright (C) 1990, 1993 Rick Adams and Bob Billson
|
|
101
|
|
102 This file is part of the OS-9 UUCP package, UUCPbb.
|
|
103
|
|
104 This program is free software; you can redistribute it and/or modify
|
|
105 it under the terms of the GNU General Public License as published by
|
|
106 the Free Software Foundation; either version 2 of the License, or
|
|
107 (at your option) any later version.
|
|
108
|
|
109 This program is distributed in the hope that it will be useful,
|
|
110 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
111 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
112 GNU General Public License for more details.
|
|
113
|
|
114 You should have received a copy of the GNU General Public License
|
|
115 along with this program; if not, write to the Free Software
|
|
116 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
117
|
|
118 The author of UUCPbb, Bob Billson, can be contacted at:
|
|
119 bob@kc2wz.bubble.org or uunet!kc2wz!bob or by snail mail:
|
|
120 21 Bates Way, Westfield, NJ 07090
|
|
121 */
|
|
122
|
|
123 /* Returns pointer to first non-whitespace character in string pointed to by
|
|
124 string. Added --REB */
|
|
125
|
|
126 char *skipspace (string)
|
|
127 char *string;
|
|
128 {
|
|
129 register char *p;
|
|
130
|
|
131 p = string;
|
|
132
|
|
133 while (iswhite (*p))
|
|
134 p++;
|
|
135
|
|
136 return (p);
|
|
137 }
|
|
138
|
|
139
|
|
140
|
|
141 /* Parse command line parameters. Broken out of docmd.c. Originally, this
|
|
142 was part of docmd.c. It was split into a separate file because it is
|
|
143 needed in osk.c as well. All the modules which link osk.c do not
|
|
144 necessarily link docmd.c as well.
|
|
145
|
|
146 Modified for OSK -- BGP
|
|
147 */
|
|
148
|
|
149 #ifdef _OSK
|
|
150 /* Parse the command passed us under OSK */
|
|
151
|
|
152 int parse_cmd (argvect, string)
|
|
153 char **argvect;
|
|
154 char *string;
|
|
155 {
|
|
156 char *p, delimit;
|
|
157 int count = -1;
|
|
158
|
|
159 p = string;
|
|
160 do
|
|
161 {
|
|
162 p = skipspace (p); /* skip leading spaces */
|
|
163
|
|
164 if (*p == '\"')
|
|
165 delimit = *(p++); /* quote is delimiter */
|
|
166 else
|
|
167 delimit = ' '; /* space is delimiter */
|
|
168
|
|
169 argvect[++count] = p;
|
|
170
|
|
171 /* include all between */
|
|
172 while (*(++p) != delimit && *p != '\0');
|
|
173 if (*p == '\0')
|
|
174 --p;
|
|
175 else
|
|
176 *p = '\0';
|
|
177 }
|
|
178 while (*(++p) != '\0');
|
|
179
|
|
180 argvect[++count] = NULL; /* NULL terminate last */
|
|
181 }
|
|
182
|
|
183 #else
|
|
184 /* Parse the command passed us under OS-9/6809 */
|
|
185
|
|
186 char *parse_cmd (cmd)
|
|
187 char *cmd;
|
|
188 {
|
|
189 register char *p;
|
|
190
|
|
191 /* Make sure command line end with a \n */
|
|
192 if ((p = strchr (cmd, '\n')) == NULL)
|
|
193 strcat (cmd, "\n");
|
|
194
|
|
195 /* Return a pointer to the command line parameters or the \n if there are
|
|
196 no parameters. */
|
|
197
|
|
198 if ((p = strchr (cmd, ' ')) != NULL)
|
|
199 *p++ = '\0';
|
|
200 else
|
|
201 p = cmd + strlen (cmd) - 1;
|
|
202
|
|
203 return (p);
|
|
204 }
|
|
205 #endif
|