-
Notifications
You must be signed in to change notification settings - Fork 0
/
program_args.c
executable file
·499 lines (438 loc) · 16.3 KB
/
program_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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
** program_args.c
** Functions for parsing the program arguments.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include "char_set.h"
#include "data.h"
#include "debug.h"
#include "memory.h"
#include "program_args.h"
#include "parse_regex.h"
extern gg g;
void program_args_parse(int argc, char ** argv)
{
char got_regex = 0;
int useful_int, bufsize = 256, end = 0;
char ** end_ptr;
char * buf = NULL, * regex_file = NULL;
char_set temp_regex;
/* Get the program name */
g->progname = (char *) check_malloc (sizeof(char) * (strlen(argv[0]) + 1));
strcpy(g->progname, argv[0]);
argv++;
if (argc < 2)
program_args_print_usage(g->progname);
if (argc > 16)
program_args_print_usage(g->progname);
end_ptr = (char **) check_malloc (sizeof(char *));
*end_ptr = (char *) check_malloc (sizeof(char));
/* Get the arguments */
while (argv[0] != NULL) {
if (strcmp(argv[0], "-us") == 0) {
argv++;
if (argv[0] == NULL) {
debug_print(D_Error, "missing required number (for -us)");
program_args_print_usage(g->progname);
}
/* Setting the character universe */
useful_int = strtol(argv[0], end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "error in number string (-us NNN)");
debug_print(D_Program_Args, "debug: the first invalid character was %d", *end_ptr[0]);
program_args_print_usage(g->progname);
}
char_set_g_free(g->universe);
char_set_g_add_universe(g->universe, useful_int);
if (g->debug_code & D_Program_Args) {
debug_print(D_Program_Args, "Set universe_set to:");
char_set_g_display(g->universe);
}
}
else if (strncmp(argv[0], "--universe-set=", 15) == 0) {
/* Setting the universe set */
if (argv[0][11] == '\0') {
debug_print(D_Error, "missing required number (for --universe-set=NNN)");
program_args_print_usage(g->progname);
}
/* Setting the character universe */
useful_int = strtol(argv[0]+(15*sizeof(char)), end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "error in number string (for --universe-set=NNN)");
debug_print(D_Program_Args, "debug: the first invalid character was %d", *end_ptr[0]);
program_args_print_usage(g->progname);
}
char_set_g_free(g->universe);
char_set_g_add_universe(g->universe, useful_int);
if (g->debug_code & D_Program_Args) {
debug_print(D_Program_Args, "Set universe_set to:");
char_set_g_display(g->universe);
}
}
else if (strcmp(argv[0], "-uc") == 0) {
/* Setting the universe check code */
argv++;
if (argv[0] == NULL) {
debug_print(D_Error, "missing required number (for -uc)");
program_args_print_usage(g->progname);
}
useful_int = strtol(argv[0], end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "%s: error in number string (-uc NNN)", g->progname);
program_args_print_usage(g->progname);
}
if (useful_int > 3) {
debug_print(D_Error, "-uc code set too high (max is 3)");
program_args_print_usage(g->progname);
}
g->universe_check_code = useful_int;
debug_print(D_Program_Args, "Set universe_check_code to %d", useful_int);
}
else if (strncmp(argv[0], "--universe-checking=", 20) == 0) {
if (argv[0][20] == '\0') {
debug_print(D_Error, "missing required number (for --universe_checking=NNN)");
program_args_print_usage(g->progname);
}
/* Setting the max word length */
useful_int = strtol(argv[0]+(20*sizeof(char)), end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "error in number string (for --universe-checking=NNN)");
program_args_print_usage(g->progname);
}
if (useful_int > 3) {
debug_print(D_Error, "--universe-checking code set too high (max is 3)");
program_args_print_usage(g->progname);
}
g->universe_check_code = useful_int;
debug_print(D_Program_Args, "Set universe_check_code to %d", useful_int);
}
else if (strcmp(argv[0], "-m") == 0) {
/* Setting the max word length */
argv++;
if (argv[0] == NULL) {
debug_print(D_Error, "missing required number (for -u)");
program_args_print_usage(g->progname);
}
useful_int = strtol(argv[0], end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "%s: error in number string (-m NNN)", g->progname);
program_args_print_usage(g->progname);
}
g->max_word_length = useful_int;
debug_print(D_Program_Args, "Set max_word_length to %d", useful_int);
}
else if (strncmp(argv[0], "--max-length=", 13) == 0) {
if (argv[0][13] == '\0') {
debug_print(D_Error, "missing required number (for --max-length=NNN)");
program_args_print_usage(g->progname);
}
/* Setting the max word length */
useful_int = strtol(argv[0]+(13*sizeof(char)), end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "error in number string (for --max-length=NNN)");
program_args_print_usage(g->progname);
}
g->max_word_length = useful_int;
debug_print(D_Program_Args, "Set max_word_length to %d", useful_int);
}
else if (strcmp(argv[0], "-d") == 0) {
/* Setting the debug code */
argv++;
if (argv[0] == NULL) {
debug_print(D_Error, "missing required number (for -d)");
program_args_print_usage(g->progname);
}
useful_int = strtol(argv[0], end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "error in number string (for -d NNN");
program_args_print_usage(g->progname);
}
g->debug_code = useful_int;
debug_print(D_Program_Args, "Set debug_code to %d", useful_int);
}
else if (strncmp(argv[0], "--debug-code=", 13) == 0) {
if (argv[0][13] == '\0') {
debug_print(D_Error, "missing required number (for --debug-code=NNN)");
program_args_print_usage(g->progname);
}
/* Setting the debug code */
useful_int = strtol(argv[0]+(13*sizeof(char)), end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "error in number string (for --debug-code=NNN)");
program_args_print_usage(g->progname);
}
g->debug_code = useful_int;
debug_print(D_Program_Args, "Set debug_code to %d", useful_int);
}
else if (strcmp(argv[0], "-p") == 0) {
/* Parse the regex only */
g->stop_code = 'p';
debug_print(D_Program_Args, "Stopping after parsing the regex.");
}
else if (strncmp(argv[0], "--parse-only", 12) == 0) {
/* Parse the regex only */
g->stop_code = 'p';
debug_print(D_Program_Args, "Stopping after parsing the regex.");
}
else if (strcmp(argv[0], "-r") == 0) {
/* Enable readable output */
g->readable_output = 1;
}
else if (strncmp(argv[0], "--readable-output", 17) == 0) {
/* Enable readable output */
g->readable_output = 1;
}
else if (strcmp(argv[0], "-u") == 0) {
/* Explicitly setting the character universe */
argv++;
if (argv[0] == NULL) {
debug_print(D_Error, "missing required number (for -u)");
program_args_print_usage(g->progname);
}
temp_regex = char_set_g_constructor();
char_set_g_deep_copy(temp_regex, g->regex);
/* Read in char class, parse it, and set universe to its results */
char_set_g_free(g->regex);
g->regex = char_set_g_constructor();
char_set_g_init(g->regex, argv[0]);
/* Make sure it starts with [ */
if (char_set_g_char_n(g->regex, 0) != '[') {
debug_print(D_Error, "When explicitly setting a character universe, you must start with [ and end with ]!");
program_args_print_usage(g->progname);
}
/* Force universe checking off temporarily */
useful_int = g->universe_check_code;
g->universe_check_code &= 2;
g->last_chartype_parsed = CT_CHAR_CLASS_START;
char_set_g_adv_pos(g->regex, 1);
/* NOTE: Negated character classes will be relative to the current universe,
** which would be either the default, or the one specified on the command line _iff_
** it was specified before this -u argument! */
/* Should we be verbose in its parse? */
parse_regex_pass_char_class(g->regex, g->debug_code & D_Program_Args & D_Parse_Regex_Eachstep);
/* Revert to previous universe checking state */
g->universe_check_code = useful_int;
char_set_g_free(g->universe);
g->universe = char_set_g_constructor();
char_set_g_deep_copy(g->universe, g->last_class); /* Got it here! */
/* Clean up */
char_set_g_free(g->regex);
char_set_g_deep_copy(g->regex, temp_regex);
char_set_g_free(temp_regex);
char_set_g_free(g->last_class);
g->last_class = char_set_g_constructor();
g->last_chartype_parsed = CT_UNDEFINED;
g->last_value_parsed = 0;
g->last_value_parsed_extra = 0;
g->current_atom_start_pos = 0;
debug_print(D_Program_Args, "Set character universe to:");
if (g->debug_code & D_Program_Args) {
char_set_g_display(g->universe);
}
}
else if (strncmp(argv[0], "--universe=", 11) == 0) {
/* Explicitly setting the character universe */
if (argv[0][11] == '\0') {
debug_print(D_Error, "missing required UNIVERSE (for --universe=[UNIVERSE])");
program_args_print_usage(g->progname);
}
/* Read in char class, parse it, and set universe to its results */
temp_regex = char_set_g_constructor();
char_set_g_deep_copy(temp_regex, g->regex);
char_set_g_free(g->regex);
g->regex = char_set_g_constructor();
char_set_g_init(g->regex, argv[0] + 11);
/* Make sure it starts with [ */
if (char_set_g_char_n(g->regex, 0) != '[') {
debug_print(D_Error, "When explicitly setting a character universe, you must start with [ and end with ]!");
program_args_print_usage(g->progname);
}
/* Force universe checking off temporarily */
useful_int = g->universe_check_code;
g->universe_check_code &= 2;
g->last_chartype_parsed = CT_CHAR_CLASS_START;
char_set_g_adv_pos(g->regex, 1);
/* NOTE: Negated character classes will be relative to the current universe,
** which would be either the default, or the one specified on the command line _iff_
** it was specified before this -u argument! */
/* Should we be verbose in its parse? */
parse_regex_pass_char_class(g->regex, g->debug_code & D_Program_Args & D_Parse_Regex_Eachstep);
/* Revert to previous universe checking state */
g->universe_check_code = useful_int;
char_set_g_free(g->universe);
g->universe = char_set_g_constructor();
char_set_g_deep_copy(g->universe, g->last_class); /* Got it here! */
/* Clean up */
char_set_g_free(g->regex);
char_set_g_deep_copy(g->regex, temp_regex);
char_set_g_free(temp_regex);
char_set_g_free(g->last_class);
g->last_class = char_set_g_constructor();
g->last_chartype_parsed = CT_UNDEFINED;
g->last_value_parsed = 0;
g->last_value_parsed_extra = 0;
g->current_atom_start_pos = 0;
debug_print(D_Program_Args, "Set character universe to:");
if (g->debug_code & D_Program_Args) {
char_set_g_display(g->universe);
}
}
else if (strcmp(argv[0], "-n") == 0) {
/* Setting the number of words to output */
argv++;
if (argv[0] == NULL) {
debug_print(D_Error, "missing required number (for -n)");
program_args_print_usage(g->progname);
}
useful_int = strtol(argv[0], end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "error in number string (for -n NNN");
program_args_print_usage(g->progname);
}
g->num_words_output = useful_int;
debug_print(D_Program_Args, "Set num_words_output to %d", useful_int);
}
else if (strncmp(argv[0], "--num-words-output=", 19) == 0) {
/* Setting the number of words to output */
if (argv[0][19] == '\0') {
debug_print(D_Error, "missing required number (for --num-words-output=NNN)");
program_args_print_usage(g->progname);
}
/* Setting the debug code */
useful_int = strtol(argv[0]+(19*sizeof(char)), end_ptr, 10);
if (*end_ptr[0] != '\0') {
/* error in number string */
debug_print(D_Error, "error in number string (for --num-words-output=NNN)");
program_args_print_usage(g->progname);
}
g->num_words_output = useful_int;
debug_print(D_Program_Args, "Set num_words_output to %d", useful_int);
}
else if (strcmp(argv[0], "-f") == 0) {
/* Getting the regex from a file */
if (got_regex == 1) {
debug_print(D_Error, "read two regular expressions:");
debug_print(D_Error, " first: %s", char_set_g_get_set(g->regex));
debug_print(D_Error, " second: %s", argv[0]);
program_args_print_usage(g->progname);
}
argv++;
if (argv[0] == NULL) {
debug_print(D_Error, "missing required number (for -f)");
program_args_print_usage(g->progname);
}
regex_file = argv[0];
got_regex = 1;
debug_print(D_Program_Args, "Will get the regex from file %s", argv[0]);
}
else if (strncmp(argv[0], "--file=", 7) == 0) {
/* Getting the regex from a file */
if (argv[0][7] == '\0') {
debug_print(D_Error, "missing required number (for --file=filename)");
program_args_print_usage(g->progname);
}
if (got_regex == 1) {
debug_print(D_Error, "read two regular expressions:");
debug_print(D_Error, " first: %s", char_set_g_get_set(g->regex));
debug_print(D_Error, " second: %s", argv[0]);
program_args_print_usage(g->progname);
}
regex_file = argv[0] + (7*sizeof(char));
got_regex = 1;
debug_print(D_Program_Args, "Will get the regex from file %s", argv[0] + (7*sizeof(char)));
}
else {
if (got_regex == 1) {
debug_print(D_Error, "read two regular expressions:");
debug_print(D_Error, " first: %s", char_set_g_get_set(g->regex));
debug_print(D_Error, " second: %s", argv[0]);
program_args_print_usage(g->progname);
}
got_regex = 1;
char_set_g_init(g->regex, argv[0]);
}
argv++;
}
if (!got_regex) {
debug_print(D_Error, "No regex specified!");
program_args_print_usage(g->progname);
}
if (regex_file != NULL) {
if (strncmp(regex_file, "-\0", 2) == 0) {
useful_int = 0;
} else {
useful_int = open(regex_file, O_RDONLY, 444);
if (useful_int < 0) {
debug_print(D_Error, "Couldn't open regex file %s for reading!", regex_file);
exit(-52);
}
}
buf = (char *) malloc ((bufsize + 1)*sizeof(char));
while (read(useful_int, buf, bufsize)) {
buf[bufsize] = '\0';
if (buf[strlen(buf)-1] == 10) {
buf[strlen(buf)-1] = '\0';
end = 1;
}
char_set_g_insert_string(g->regex, buf);
if (end) {
break;
}
}
free(buf);
close(useful_int);
debug_print(D_Program_Args, "Read in the regular expression as %s", g->regex);
}
if (g->stop_code == 'p') {
g->debug_code |= D_Parse_Regex_Eachstep;
}
debug_print(D_Program_Args, "using regex: %s", char_set_g_get_set(g->regex));
}
void program_args_print_usage(char * progname)
{
fprintf(stderr, "usage: %s [options] regex\n", progname);
fprintf(stderr, "options:\n");
fprintf(stderr, "\t-d NNN\n");
fprintf(stderr, "\t--debug-code=NNN\n");
fprintf(stderr, "\t\tSet the debugging code to NNN.\n");
fprintf(stderr, "\t-us NNN\n");
fprintf(stderr, "\t--universe-set=NNN\n");
fprintf(stderr, "\t\tSet the character universe to set NNN.\n");
fprintf(stderr, "\t-uc NNN\n");
fprintf(stderr, "\t--universe-checking=NNN\n");
fprintf(stderr, "\t\tSet the universe checking code to NNN.\n");
fprintf(stderr, "\t-m NNN\n");
fprintf(stderr, "\t--max-length=NNN\n");
fprintf(stderr, "\t\tSet the maximum output length to NNN.\n");
fprintf(stderr, "\t-p\n");
fprintf(stderr, "\t--parse-only\n");
fprintf(stderr, "\t\tOnly parse the regex, and display the parsing.\n");
fprintf(stderr, "\t-r\n");
fprintf(stderr, "\t--readable-output\n");
fprintf(stderr, "\t\tIn output, display ASCII codes for non-printable characters.\n");
fprintf(stderr, "\t-u [UNIVERSE]\n");
fprintf(stderr, "\t--universe=[UNIVERSE]\n");
fprintf(stderr, "\t\tExplicitly set the character universe as a character class.\n");
fprintf(stderr, "\t-n NNN\n");
fprintf(stderr, "\t--num-words-output=NNN\n");
fprintf(stderr, "\t\tSet the number of \"words\" to be output to NNN (default is no limit).\n");
fprintf(stderr, "\t-f filename\n");
fprintf(stderr, "\t--file=filename\n");
fprintf(stderr, "\t\tRead the regex from a file (- is stdin).\n");
exit(-1);
}