arg.h (1781B)
1 /* 2 * Copy me if you can. 3 * by 20h 4 */ 5 6 #ifndef ARG_H__ 7 #define ARG_H__ 8 9 extern char *argv0; 10 11 /* use main(int argc, char *argv[]) */ 12 #define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ 13 argv[0] && argv[0][0] && argv[0][1];\ 14 argc--, argv++) {\ 15 char argc_;\ 16 char **argv_;\ 17 int brk_;\ 18 if (argv[0][0] == '-') {\ 19 if (argv[0][1] == '-' && argv[0][2] == '\0') {\ 20 argv++;\ 21 argc--;\ 22 break;\ 23 }\ 24 for (brk_ = 0, argv[0]++, argv_ = argv;\ 25 argv[0][0] && !brk_;\ 26 argv[0]++) {\ 27 if (argv_ != argv)\ 28 break;\ 29 argc_ = argv[0][0];\ 30 switch (argc_) 31 32 /* Handles obsolete -NUM syntax */ 33 #define ARGNUM case '0':\ 34 case '1':\ 35 case '2':\ 36 case '3':\ 37 case '4':\ 38 case '5':\ 39 case '6':\ 40 case '7':\ 41 case '8':\ 42 case '9' 43 44 #define ARGALT(SYMBOL) }\ 45 } else if (argv[0][0] == SYMBOL) {\ 46 for (brk_ = 0, argv[0]++, argv_ = argv;\ 47 argv[0][0] && !brk_;\ 48 argv[0]++) {\ 49 if (argv_ != argv)\ 50 break;\ 51 argc_ = argv[0][0];\ 52 switch (argc_) 53 54 #define ARGEND }\ 55 } else {\ 56 break;\ 57 }\ 58 } 59 60 #define ARGC() argc_ 61 62 #define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX)) 63 64 #define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ 65 ((x), abort(), (char *)0) :\ 66 (brk_ = 1, (argv[0][1] != '\0')?\ 67 (&argv[0][1]) :\ 68 (argc--, argv++, argv[0]))) 69 70 #define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ 71 (char *)0 :\ 72 (brk_ = 1, (argv[0][1] != '\0')?\ 73 (&argv[0][1]) :\ 74 (argc--, argv++, argv[0]))) 75 76 #define UARGF() EARGF(usage()) 77 78 #define LNGARG() &argv[0][0] 79 80 #define UNOFLAGS(...) ARGBEGIN {\ 81 default:\ 82 usage();\ 83 } ARGEND;\ 84 if (__VA_ARGS__)\ 85 usage() 86 87 88 #endif