This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
arg.h
65 lines (57 loc) · 2.02 KB
/
arg.h
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
/* Copy me if you can. */
/*
* usage:
* #include "arg.h"
* char *argv0;
* int main(int argc, char **argv) {
* char *f;
* ARGBEGIN {
* case 'b': printf("option -b\n"); break;
* case 'f': printf("option -f %s\n", (f=ARGF())? f : "no arg"); break;
* default: printf("bad option %c\n", ARGC()); break;
* } ARGEND
* return 0;
* }
*
* ARGC() returns the current option character.
* ARGF() returns the current option argument:
* the rest of the option string if not empty, or the next argument
* in argv if any, or NULL. ARGF() must be called only once for each
* option argument.
* EARGF(code) same as ARGF() but instead of returning NULL runs `code' and,
* if that returns, calls abort(2). A typical value for code is usage(),
* as in EARGF(usage()).
*
* After ARGBEGIN, argv0 is a copy of argv[0]
*
* After ARGEND, argv points at a NULL terminated list of the remaining
* argc arguments
*
*/
#ifndef ARG_H
#define ARG_H
#define ARGBEGIN {char *a, **argp, **args; \
for (argv0 = *argv++, argc--, args = argp = argv; \
(a = *argp) != NULL; /* while != NULL */ \
*argp ? argp++ : 0) /* inc only if _argp != NULL */ \
if (a[0] == '-' && a[1] == '-' && a[2] == '\0') \
for (argc--, argp++; /* skip the '--' arg */ \
(a = *argp) != NULL; \
*argp ? argp++ : 0) \
*(args++) = a; /* copy all arguments */ \
else if (a[0] == '-' && a[1] != '-' && a[1] != '\0') \
for (argc--, a++; *a; *a ? a++ : 0) \
switch (a[0])
#define ARGLONG else if (a[0] == '-' && a[1] == '-' && a[2] != '\0')
#define ARGEND else *(args++) = a; /* else copy the argument */ }
#define ARGC() (a[0])
#define ARGF() (a[1]? a + 1 :\
argp[1]? (argc--, argp++, argp[0]) :\
NULL)
#define EARGF(x) (a[1]? a + 1 :\
argp[1]? (argc--, argp++, argp[0]) :\
((x), abort(), NULL))
#define ARGLC() (&a[2])
#define ARGLF() (argp ? (argp++, *argp) : NULL)
#define EARGLF(x) (argp ? (argp++, *argp) : ((x), abort(), NULL))
#endif /* ARG_H */