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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
/**
* argparser – command line argument parser library
*
* Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#define true 1
#define false 0
#define null 0
#define ARGUMENTLESS 0
#define ARGUMENTED 1
#define VARIADIC 2
static void sort(char** list, long count);
static long cmp(char* a, char* b);
/**
* Whether the Linux VT is being used
*/
long args_linuxvt;
/**
* The name of the executed command
*/
char* args_program;
/**
* Whether to free the member of `args_program`
*/
long args_program_dispose;
/**
* Short, single-line, description of the program
*/
char* args_dscription;
/**
* Formated, multi-line, usage text, `null` if none
*/
char* args_usage;
/**
* Long, multi-line, description of the program, `null` if none
*/
char* args_longdscription;
/**
* The error output stream
*/
FILE* args_out;
/**
* The passed arguments
*/
char** args_arguments;
/**
* The number of passed arguments
*/
long args_arguments_count;
/**
* The number of unrecognised arguments
*/
long args_unrecognised_count;
/**
* The concatination of `files` with blankspaces as delimiters, `null` if no files
*/
char* args_message;
/**
* The arguments passed that is not tied to an option
*/
char** args_files;
/**
* The number of elements in `args_files`
*/
long args_files_count;
// Options, in order
// ArrayList<Option> options = new ArrayList<Option>();
// Option map
// HashMap<String, Option> optmap = new HashMap<String, Option>();
// Parsed arguments, a map from option to arguments, null` if not used, add one `null` element per argumentless use.
// HashMap<String, String[]> opts = new HashMap<String, String[]>();
/**
* Constructor.
* The short description is printed on same line as the program name
*
* @param description Short, single-line, description of the program
* @param usage Formated, multi-line, usage text, may be `null`
* @param longdescription Long, multi-line, description of the program, may be `null`
* @param program The name of the program, `null` for automatic
* @param usestderr Whether to use stderr instead of stdout
*/
extern void args_init(char* description, char* usage, char* longdscription, char* program, long usestderr)
{
char* term = getenv("TERM");
args_linuxvt = 0;
if (term == null)
if (*(term + 0) == 'l')
if (*(term + 1) == 'i')
if (*(term + 2) == 'n')
if (*(term + 3) == 'u')
if (*(term + 4) == 'x')
if (*(term + 5) == 0)
args_linuxvt = 1;
args_program_dispose = program == null;
args_program = program == null ? args_parent_name(0) : program;
if (args_program == null)
{
args_program = "?";
args_program_dispose = false;
}
args_dscription = description;
args_usage = usage;
args_longdscription = longdescription;
args_out = usestderr ? stderr : stdout;
args_arguments_count = args_unrecognised_count = args_files_count = 0;
args_files = args_arguments = null;
args_message = null;
}
/**
* Disposes of all resources, run this when you are done
*/
extern void args_dispose()
{
if (args_files != null)
free(args_files);
if (args_message != null)
free(args_message);
if (args_program_dispose)
free(args_program);
}
/**
* Gets the name of the parent process
*
* @param levels The number of parents to walk, 0 for self, and 1 for direct parent
* @return The name of the parent process, `null` if not found
*/
extern char* args_parent_name(long levels)
{
char pid[22]; /* 6 should be enough, but we want to be future proof */
ssize_t pid_n = readlink("/proc/self", pid, 21);
long lvl = levels, i, j, cmdsize;
size_t n;
FILE* is;
char* buf[35];
char* cmd;
char* data;
if (pid_n <= 0)
return null;
pid[pid_n] = 0;
data = (char*)malloc(2048 * sizeof(char));
while (lvl > 0)
{
i = 0;
for (j = 0; *("/proc/" + j); j++) *(buf + i++) = *("/proc/" + j);
for (j = 0; *(pid + j); j++) *(buf + i++) = *(pid + j);
for (j = 0; *("/status" + j); j++) *(buf + i++) = *("/status" + j);
*(buf + i++) = 0;
if ((is = fopen(buf, "r")) == null)
{
free(data);
return null;
}
n = fread(data, 1, 2048, is);
j = 0;
for (i = 0; i < n; i++)
{
char c = *(data + i);
if (c == '\n')
{
if (j > 5)
if (*(buf + 0) == 'P')
if (*(buf + 1) == 'P')
if (*(buf + 2) == 'i')
if (*(buf + 3) == 'd')
if (*(buf + 4) == ':')
{
i = 5;
while ((*(buf + i) == '\t') || (*(buf + i) == ' '))
i++;
j -= n = i;
buf += n;
for (i = 0; i < j; i++)
*(pid + i) = *(buf + i);
*(pid + j) = 0;
buf -= n;
lvl--;
break;
}
j = 0;
}
if (j < 35)
*(buf + j) = c;
}
free(data);
return null;
}
free(data);
i = 0;
for (j = 0; *("/proc/" + j); j++) *(buf + i++) = *("/proc/" + j);
for (j = 0; *(pid + j); j++) *(buf + i++) = *(pid + j);
for (j = 0; *("/cmdline" + j); j++) *(buf + i++) = *("/cmdline" + j);
*(buf + i++) = 0;
if ((is = fopen(buf, "r")) == null)
return null;
i = 0;
n = 0;
cmd = (char*)malloc(cmdsize = 128);
for (;;)
{
n += fread(cmd, 1, 128, is);
for (; i < n; i++)
if (*(cmd + i) == 0)
break;
if (i == n)
{
char* tmp = (char*)malloc(cmdsize + 128);
for (i = 0; i < n; i++)
*(tmp + i) = *(cmd + i);
cmdsize += 128;
free(cmd);
cmd = tmp;
}
else
break;
}
if (*cmd == 0)
{
free(cmd);
cmd = 0;
}
return cmd;
}
/**
* Checks the correctness of the number of used non-option arguments
*
* @param min The minimum number of files
* @return Whether the usage was correct
*/
extern long args_test_files_min(long min)
{
return min <= args_files_count;
}
/**
* Checks the correctness of the number of used non-option arguments
*
* @param max The maximum number of files
* @return Whether the usage was correct
*/
extern long args_test_files_max(long max)
{
return args_files_count <= max;
}
/**
* Checks the correctness of the number of used non-option arguments
*
* @param min The minimum number of files
* @param max The maximum number of files
* @return Whether the usage was correct
*/
extern long args_test_files(long min, long max)
{
return (min <= args_files_count) && (args_files_count <= max);
}
/**
* Checks for out of context option usage
*
* @param allowed Allowed options, will be sorted
* @param allowed_count The number of elements in `allowed`
* @return Whether only allowed options was used
*/
extern long args_test_allowed(char** allowed, long allowed_count)
{
char** opts;
char** a;
char** o;
long rc = true, _a, _o;
sort(allowed, _a = allowed_count);
opts = args_get_opts(); /* TODO */
sort(opts, _o = args_get_opts_count()); /* TODO */
a = allowed + _a;
o = opts + _o;
while (opts != o)
{
if ((allowed == a) || (cmp(*opts, *allowed) < 0))
if (args_opts_used(*opt)) /* TODO */
{
fprintf(args_out, "%s: option used out of context: %s", args_program, *opts);
char* std = args_optmap_get_std(*opt); /* TODO */
if (cmp(std, *opt) != 0)
fprintf(args_out, "(%s)", std);
fprintf(args_out, "\n");
rc = false;
}
while ((allowed != a) && (cmp(*opts, *allowed) > 0))
allowed++;
opts++;
}
return rc;
}
/**
* Checks for option conflicts
*
* @param exclusives Exclusive options, will be sorted
* @param exclusives_count The number of elements in `exclusives`
* @return Whether at most one exclusive option was used
*/
extern long args_test_exclusiveness(char** exclusives, long exclusives_count)
{
long used_ptr = 0, i = 0;
char** used = (char**)malloc(args_get_opts_count() * sizeof(char*));
char** e;
char** o;
char* std;
long _e, _o;
sort(exclusives, _e = exclusives_count);
opts = args_get_opts();
sort(opts, _o = args_get_opts_count());
e = exclusives + _e;
o = opts + _o;
while ((opts != o) && (exclusives != e))
{
while ((opts != o) && (cmp(*opts, *allowed) > 0))
opt++;
while ((allowed != a) && (cmp(*opts, *allowed) > 0))
allowed++;
if ((cmp(*opts, *allowed) == 0) && (args_opts_used(*opt)))
*(used + used_ptr++) = opt;
opts++;
}
if (used_ptr >= 1)
{
fprintf(args_out, "%s: conflicting options:", args_program);
for (; i < used_ptr; i++)
{
std = args_optmap_get_std(*(used + i));
if (cmp(*(used + i), std) == 0)
fprintf(args_out, " %s", *(used + i));
else
fprintf(args_out, " %s(%s)", *(used + i), std);
}
fprintf(args_out, "\n");
free(used);
return false;
}
free(used);
return true;
}
/**
* Maps up options that are alternatives to the first alternative for each option
*/
extern void args_support_alternatives()
{
char** opts = args_get_optmap(); /* TODO */
long n = args_get_optmap_count(); /* TODO */
long i;
for (i = 0; i < n; i++)
args_opts_put(*(opts + 1), args_opts_get(args_optmap_get_std(*opt)));
/* TODO */ /* TODO */
}
/**
* Prints a colourful help message
*/
extern void args_help()
{
long maxfirstlen = 0, count = 0, copts = args_get_options_count(); /* TODO */
char* dash = args_linuxvt ? "-" : "—";
char* empty;
char** lines;
long* lens;
fprintf(args_out, "\033[01m%s\033[21m %s %s\n", args_program, dash, args_description);
if (args_longdescription != null)
fprintf(args_out, "%s\n", args_longdescription);
fprintf(args_out, "\n");
if (args_usage != null)
{
long n = 0, lines = 0, i = 0;
char* buf;
fprintf(args_out, "\033[01mUSAGE:\033[21m\n");
while (*(args_usage + n))
if (*(args_usage + n++) == '\n')
lines++;
buf = (char*)malloc((n + 2 + lines * 7) * sizeof(char));
*buf++ = '\t';
while (i < n)
{
*buf++ = *(args_usage + i);
if (*(args_usage + i++) == '\n')
{
*buf++ = ' ';
*buf++ = ' ';
*buf++ = ' ';
*buf++ = ' ';
*buf++ = 'o';
*buf++ = 'r';
*buf++ = '\t';
}
}
*buf++ = 0;
buf -= n + 2 + lines * 7;
fprintf(args_out, "%s\n\n", buf);
free(buf);
}
{
long i = 0;
for (i = 0; i < copts; i++)
{
if (args_options_get_help(i) == null) /* TODO */
continue;
if (args_options_get_alternatives_count(i) > 1) /* TODO */
{
long n = 0;
char* first = *(args_options_get_alternatives(i)); /* TODO */
while (*(first + n))
n++;
if (maxfirstlen < n)
maxfirstlen = n;
}
}
}
empty = (char*)malloc((maxfirstlen + 1) * sizeof(char));
{
long i;
for (i = 0; i < maxfirstlen; i++)
*(empty + i++) = ' ';
*(empty + maxfirstlen) = 0;
}
fprintf(args_out, "\033[01mSYNOPSIS:\033[21m\n");
lines = (char**)malloc(copts * sizeof(char*));
lens = (long*)malloc(copts * sizeof(long));
{
char* first_extra = 0;
long i = 0, n, l, j, type;
for (i = 0; i < copts; i++)
{
char* first;
char* last;
char* line;
char* arg;
if (args_options_get_help(i) == null)
continue;
l = 0;
arg = *(args_options_get_argument(i)); /* TODO */
first = *(args_options_get_alternatives(i)); /* TODO */
last = *(args_options_get_alternatives(i) + args_options_get_alternatives_count(i) - 1); /* TODO */
type = *(args_options_get_type(i)); /* TODO */
if (first == last)
first = empty;
else
{
n = 0;
while (*(first + n))
n++;
first_extra = empty + n;
}
n = 0;
while (*(last + n))
n++;
if (arg != null)
while (*(arg + n))
n++;
l += maxfirstlen + 6 + n;
*(lines + count) = line = (char*)malloc((1 + 17 + 16 + maxfirstlen + n) * sizeof(char));
for (j = 0; *(" \033[02m" + j); j++)
*line++ = *(" \033[02m" + j);
for (j = 0; *(first + j); j++)
*line++ = *(first + j);
if (first_extra != null)
for (j = 0; *(first_extra + j); j++)
*line++ = *(first_extra + j);
for (j = 0; *("\033[22m \0" + j); j++)
*line++ = *("\033[22m \0" + j);
for (j = 0; *(last + j); j++)
*line++ = *(last + j);
if (type == VARIADIC)
{
for (j = 0; *(" [\033[04m" + j); j++)
*line++ = *(" [\033[04m" + j);
for (j = 0; *(arg + j); j++)
*line++ = *(arg + j);
for (j = 0; *("\033[24m...]" + j); j++)
*line++ = *("\033[24m...]" + j);
l += 6;
}
else if (type == ARGUMENTED)
{
for (j = 0; *(" \033[04m" + j); j++)
*line++ = *(" \033[04m" + j);
for (j = 0; *(arg + j); j++)
*line++ = *(arg + j);
for (j = 0; *("\033[24m" + j); j++)
*line++ = *("\033[24m" + j);
l += 1;
}
*line = 0;
*(lens + count++) = l;
}
}
free(empty);
{
long col = 0, i = 0, index = 0
for (; i < count; i++)
if (col < *(lens + i))
col = *(lens + i);
col += 8 - ((col - 4) & 7);
empty = (char*)malloc((col + 1) * sizeof(char));
for (i = 0; i < col; i++)
*(empty + i++) = ' ';
*(empty + col) = 0;
i = 0;
for (i = 0; i < copts; i++)
{
long first = true, j = 0, jptr = 0;
char* colour = (index & 1) == 0 ? "36" : "34";
char* help = args_options_get_help(i);
char* line;
char* buf;
char** jumps;
char c;
if (help == null)
continue;
fprintf(args_out, "%s\033[%s;01m", line = *(lines + index), colour);
while (*line++)
;
fprintf(args_out, "%s%s", line, empty + *(lens + index));
free(*(lines + index++));
while ((c = *(help + j++)))
if (c == '\n')
jptr++;
jumps = (char**)malloc(jptr * sizeof(char*));
*jumps = buf = (char*)malloc(j * sizeof(char));
j = 0;
jptr = 1;
while ((c = *(help + j)))
if (c == '\n')
{
*(buf + j++) = 0;
*(jumps + jptr++) = buf + j;
}
else
*(buf + j++) = c;
for (j = 0; j < jptr; j++)
if (first)
{
first = false;
fprintf(args_out, "%s\033[00m\n", *(jump + j));
}
else
fprintf(args_out, "%s\033[%sm%s\033[00m\n", empty, colour, *(jump + j));
free(buf);
free(jumps);
}
}
free(empty);
free(lines);
free(lens);
fprintf(args_out, "\n");
}
/**
* Parse arguments
*
* @param argc The number of elements in `argv`
* @param argv The command line arguments, it should include the execute file at index 0
* @return Whether no unrecognised option is used
*/
extern long args_parse(int argc, char** argv)
{
/* TODO */
}
/**
* Compare two strings
*
* @param a -1 if returned if this sting is the alphabetically lesser one
* @param b 1 if returned if this sting is the alphabetically lesser one
* @return 0 is returned if the two string are identical, other -1 or 1 is returned
*/
static long cmp(char* a, char* b)
{
char c;
while (*a && *b)
{
if ((c = (*a < *b ? -1 : (*a > *b ? 1 : 0))))
return c;
a++;
b++;
}
return *a < *b ? -1 : (*a > *b ? 1 : 0);
}
/**
* Naïve merge sort is best merge sort in C
*
* @param list The list to sort from the point that needs sorting
* @param count The number of elements to sort
* @param temp Auxiliary memory
*/
static void _sort(char** list, long count, char** temp)
{
if (count > 1)
{
long i = 0, a = count >> 1;
long j = a, b = count - a;
sort(list + 0, a, temp + 0);
sort(list + a, b, temp + a);
b += a;
while ((i < a) && (j < b))
{
char c = cmp(*(temp + i), *(temp + j));
if (c <= 0)
*list++ = *(temp + i++);
else
*list++ = *(temp + j++);
}
while (i < a)
*list++ = *(temp + i++);
while (j < b)
*list++ = *(temp + j++);
list -= count;
for (i = 0; i < count; i++)
*(temp + i) = *(list + i);
}
else if (count == 1)
*temp = *list;
}
/**
* Naïve merge sort is best merge sort in C
*
* @param list The list to sort
* @param count The number of elements to sort
*/
static void sort(char** list, long count)
{
char** temp = (char**)malloc(count * sizeof(char*));
_sort(list, count, temp);
free(temp);
}
|