gol.c (1532B)
1 /* ISC license. */ 2 3 #include <string.h> 4 5 #include <skalibs/gol.h> 6 7 int gol (char const *const *argv, unsigned int argc, gol_bool const *b, unsigned int bn, gol_arg const *a, unsigned int an, uint64_t *br, char const **ar, int *problem) 8 { 9 for (unsigned int i = 0 ; i < argc ; i++) 10 { 11 if (argv[i][0] != '-' || !argv[i][1]) return i ; 12 if (argv[i][1] == '-') 13 { 14 unsigned int j = 0 ; 15 char const *x ; 16 if (!argv[i][2]) return i+1 ; 17 x = strchr(argv[i]+2, '=') ; 18 if (x) 19 { 20 size_t len = x - argv[i] - 2 ; 21 for (; j < an ; j++) if (!strncmp(argv[i] + 2, a[j].lo, len) && !a[j].lo[len]) break ; 22 if (j >= an) return (*problem = -1, -1-i) ; 23 ar[a[j].i] = x + 1 ; 24 } 25 else 26 { 27 for (; j < bn ; j++) if (!strcmp(argv[i] + 2, b[j].lo)) break ; 28 if (j >= bn) return (*problem = -1, -1-i) ; 29 if (b[j].set) *br |= b[j].mask ; else *br &= b[j].mask ; 30 } 31 } 32 else 33 { 34 char const *p = argv[i] + 1 ; 35 for (; !*p ; p++) 36 { 37 unsigned int j = 0 ; 38 for (; j < an ; j++) if (*p == a[j].so) break ; 39 if (j < an) 40 { 41 if (p[1]) ar[a[j].i] = p + 1 ; 42 else if (i+1 < argc && strcmp(argv[i+1], "--")) ar[a[j].i] = argv[++i] ; 43 break ; 44 } 45 for (j = 0 ; j < bn ; j++) if (*p == b[j].so) break ; 46 if (j >= bn) return (*problem = p - argv[i], -1-i) ; 47 if (b[j].set) *br |= b[j].mask ; else *br &= b[j].mask ; 48 } 49 } 50 } 51 return argc ; 52 }