-
Notifications
You must be signed in to change notification settings - Fork 2
/
args.c
274 lines (248 loc) · 5.7 KB
/
args.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* $Id: args.c 536 2007-06-02 06:09:00Z elliotth $
*
* Copyright (c) 1999-2002, Darren Hiebert
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* This module contains functions for reading command line arguments.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "args.h"
#include "debug.h"
#include "routines.h"
/*
* FUNCTION DEFINITIONS
*/
static char *nextStringArg (const char** const next)
{
char* result = NULL;
const char* start;
Assert (*next != NULL);
for (start = *next ; isspace ((int) *start) ; ++start)
;
if (*start == '\0')
*next = start;
else
{
size_t length;
const char* end;
for (end = start ; *end != '\0' && ! isspace ((int) *end) ; ++end)
;
length = end - start;
Assert (length > 0);
result = xMalloc (length + 1, char);
strncpy (result, start, length);
result [length] = '\0';
*next = end;
}
return result;
}
static char* nextStringLine (const char** const next)
{
char* result = NULL;
size_t length;
const char* end;
Assert (*next != NULL);
for (end = *next ; *end != '\n' && *end != '\0' ; ++end)
;
length = end - *next;
if (length > 0)
{
result = xMalloc (length + 1, char);
strncpy (result, *next, length);
result [length] = '\0';
}
if (*end == '\n')
++end;
else if (*end == '\r')
{
++end;
if (*end == '\n')
++end;
}
*next = end;
return result;
}
static char* nextString (const Arguments* const current, const char** const next)
{
char* result;
if (current->lineMode)
result = nextStringLine (next);
else
result = nextStringArg (next);
return result;
}
static char* nextFileArg (FILE* const fp)
{
char* result = NULL;
Assert (fp != NULL);
if (! feof (fp))
{
vString* vs = vStringNew ();
int c;
do
c = fgetc (fp);
while (isspace (c));
if (c != EOF)
{
do
{
vStringPut (vs, c);
c = fgetc (fp);
} while (c != EOF && ! isspace (c));
vStringTerminate (vs);
Assert (vStringLength (vs) > 0);
result = xMalloc (vStringLength (vs) + 1, char);
strcpy (result, vStringValue (vs));
}
vStringDelete (vs);
}
return result;
}
static char* nextFileLine (FILE* const fp)
{
char* result = NULL;
if (! feof (fp))
{
vString* vs = vStringNew ();
int c;
Assert (fp != NULL);
c = fgetc (fp);
while (c != EOF)
{
if (c != '\n' && c != '\r')
vStringPut (vs, c);
else if (vStringLength (vs) > 0)
break;
c = fgetc (fp);
}
if (c != EOF || vStringLength (vs) > 0)
{
if (c == '\r')
{
c = fgetc (fp);
if (c != '\n')
c = ungetc (c, fp);
}
vStringTerminate (vs);
vStringStripTrailing (vs);
result = xMalloc (vStringLength (vs) + 1, char);
strcpy (result, vStringValue (vs));
}
vStringDelete (vs);
}
return result;
}
static char* nextFileString (const Arguments* const current, FILE* const fp)
{
char* result;
if (current->lineMode)
result = nextFileLine (fp);
else
result = nextFileArg (fp);
return result;
}
extern Arguments* argNewFromString (const char* const string)
{
Arguments* result = xMalloc (1, Arguments);
memset (result, 0, sizeof (Arguments));
result->type = ARG_STRING;
result->u.stringArgs.string = string;
result->u.stringArgs.item = string;
result->u.stringArgs.next = string;
result->item = nextString (result, &result->u.stringArgs.next);
return result;
}
extern Arguments* argNewFromArgv (char* const* const argv)
{
Arguments* result = xMalloc (1, Arguments);
memset (result, 0, sizeof (Arguments));
result->type = ARG_ARGV;
result->u.argvArgs.argv = argv;
result->u.argvArgs.item = result->u.argvArgs.argv;
result->item = *result->u.argvArgs.item;
return result;
}
extern Arguments* argNewFromFile (FILE* const fp)
{
Arguments* result = xMalloc (1, Arguments);
memset (result, 0, sizeof (Arguments));
result->type = ARG_FILE;
result->u.fileArgs.fp = fp;
result->item = nextFileString (result, result->u.fileArgs.fp);
return result;
}
extern Arguments* argNewFromLineFile (FILE* const fp)
{
Arguments* result = xMalloc (1, Arguments);
memset (result, 0, sizeof (Arguments));
result->type = ARG_FILE;
result->lineMode = TRUE;
result->u.fileArgs.fp = fp;
result->item = nextFileString (result, result->u.fileArgs.fp);
return result;
}
extern char *argItem (const Arguments* const current)
{
Assert (current != NULL);
Assert (! argOff (current));
return current->item;
}
extern boolean argOff (const Arguments* const current)
{
Assert (current != NULL);
return (boolean) (current->item == NULL);
}
extern void argSetWordMode (Arguments* const current)
{
Assert (current != NULL);
current->lineMode = FALSE;
}
extern void argSetLineMode (Arguments* const current)
{
Assert (current != NULL);
current->lineMode = TRUE;
}
extern void argForth (Arguments* const current)
{
Assert (current != NULL);
Assert (! argOff (current));
switch (current->type)
{
case ARG_STRING:
if (current->item != NULL)
eFree (current->item);
current->u.stringArgs.item = current->u.stringArgs.next;
current->item = nextString (current, ¤t->u.stringArgs.next);
break;
case ARG_ARGV:
++current->u.argvArgs.item;
current->item = *current->u.argvArgs.item;
break;
case ARG_FILE:
if (current->item != NULL)
eFree (current->item);
current->item = nextFileString (current, current->u.fileArgs.fp);
break;
default:
Assert ("Invalid argument type" == NULL);
break;
}
}
extern void argDelete (Arguments* const current)
{
Assert (current != NULL);
if (current->type == ARG_STRING && current->item != NULL)
eFree (current->item);
memset (current, 0, sizeof (Arguments));
eFree (current);
}
/* vi:set tabstop=4 shiftwidth=4: */