mkflags.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Take three word input lines on stdin and produce flag #defines to stdout.
  2. // The three words on each input line are command name, option string with
  3. // current config, option string from allyesconfig. The three are space
  4. // separated and the last two are in double quotes.
  5. // This is intentionally crappy code because we control the inputs. It leaks
  6. // memory like a sieve and segfaults if malloc returns null, but does the job.
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <ctype.h>
  13. struct flag {
  14. struct flag *next, *lopt;
  15. char *command;
  16. };
  17. int chrtype(char c)
  18. {
  19. // Does this populate a GLOBALS() variable?
  20. if (strchr("^-:#|@*; %.", c)) return 1;
  21. // Is this followed by a numeric argument in optstr?
  22. if (strchr("=<>", c)) return 2;
  23. if (strchr("?&0", c)) return 3;
  24. return 0;
  25. }
  26. // replace chopped out USE_BLAH() sections with low-ascii characters
  27. // showing how many flags got skipped so FLAG_ macros stay constant
  28. char *mark_gaps(char *flags, char *all)
  29. {
  30. char *n, *new, c;
  31. int bare = 1;
  32. // Shell feeds in " " for blank args, leading space not meaningful.
  33. while (isspace(*flags)) flags++;
  34. while (isspace(*all)) all++;
  35. n = new = strdup(all);
  36. while (*all) {
  37. // --longopt parentheticals dealt with as a unit
  38. if (*all == '(') {
  39. int len = 0;
  40. while (all[len]) if (all[len++] == ')') break;
  41. if (strncmp(flags, all, len)) {
  42. // bare longopts need their own skip placeholders
  43. if (bare) *(new++) = 1;
  44. } else {
  45. memcpy(new, all, len);
  46. new += len;
  47. flags += len;
  48. }
  49. all += len;
  50. continue;
  51. }
  52. c = *(all++);
  53. if (bare && !chrtype(c)) bare = 0;
  54. if (*flags == c) {
  55. *(new++) = c;
  56. flags++;
  57. continue;
  58. }
  59. c = chrtype(c);
  60. if (!c || (!bare && c==3)) *(new++) = 1;
  61. else if (c==2) while (isdigit(*all)) all++;
  62. }
  63. *new = 0;
  64. return n;
  65. }
  66. // Break down a command string into linked list of "struct flag".
  67. struct flag *digest(char *string)
  68. {
  69. struct flag *list = 0;
  70. char *err = string, c;
  71. while (*string) {
  72. // Groups must be at end.
  73. if (*string == '[') break;
  74. // Longopts
  75. if (*string == '(') {
  76. struct flag *new = calloc(sizeof(struct flag), 1);
  77. if (string[1]==')') {
  78. fprintf(stderr, "empty () longopt in '%s'", err);
  79. exit(1);
  80. }
  81. new->command = ++string;
  82. // Attach longopt to previous short opt, if any.
  83. if (list && list->command) {
  84. new->next = list->lopt;
  85. list->lopt = new;
  86. } else {
  87. struct flag *blank = calloc(sizeof(struct flag), 1);
  88. blank->next = list;
  89. blank->lopt = new;
  90. list = blank;
  91. }
  92. // An empty longopt () would break this.
  93. while (*++string != ')') if (*string == '-') *string = '_';
  94. *(string++) = 0;
  95. continue;
  96. }
  97. c = chrtype(*string);
  98. if (c == 1 || (c == 3 && !list)) string++;
  99. else if (c == 2) {
  100. if (string[1]=='-') string++;
  101. if (!isdigit(string[1])) {
  102. fprintf(stderr, "%c without number in '%s'", *string, err);
  103. exit(1);
  104. }
  105. while (isdigit(*++string)) {
  106. if (!list) {
  107. string++;
  108. break;
  109. }
  110. }
  111. } else {
  112. struct flag *new = calloc(sizeof(struct flag), 1);
  113. if (string[0]=='~' && string[1]!='(') {
  114. fprintf(stderr, "~ without (longopt) in '%s'", err);
  115. exit(1);
  116. }
  117. new->command = string++;
  118. new->next = list;
  119. list = new;
  120. }
  121. }
  122. return list;
  123. }
  124. // Parse C-style octal escape
  125. void octane(char *from)
  126. {
  127. unsigned char *to = (void *)from;
  128. while (*from) {
  129. if (*from == '\\') {
  130. *to = 0;
  131. while (isdigit(*++from)) *to = (8**to)+*from-'0';
  132. to++;
  133. } else *to++ = *from++;
  134. }
  135. *to = 0;
  136. }
  137. int main(int argc, char *argv[])
  138. {
  139. char command[256], flags[1024], allflags[1024];
  140. char *out, *outbuf = malloc(1024*1024);
  141. // Yes, the output buffer is 1 megabyte with no bounds checking.
  142. // See "intentionally crappy", above.
  143. if (!(out = outbuf)) return 1;
  144. printf("#undef FORCED_FLAG\n#undef FORCED_FLAGLL\n"
  145. "#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n#define FORCED_FLAGLL 1ULL\n"
  146. "#else\n#define FORCED_FLAG 0\n#define FORCED_FLAGLL 0LL\n#endif\n\n");
  147. for (;;) {
  148. struct flag *flist, *aflist, *offlist;
  149. char *mgaps = 0;
  150. unsigned bit;
  151. *command = *flags = *allflags = 0;
  152. bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
  153. command, flags, allflags);
  154. octane(flags);
  155. octane(allflags);
  156. if (getenv("DEBUG"))
  157. fprintf(stderr, "command=%s, flags=%s, allflags=%s\n",
  158. command, flags, allflags);
  159. if (!*command) break;
  160. if (bit != 3) {
  161. fprintf(stderr, "\nError in %s (see generated/flags.raw)\n", command);
  162. exit(1);
  163. }
  164. bit = 0;
  165. printf("// %s %s %s\n", command, flags, allflags);
  166. if (*flags != ' ') mgaps = mark_gaps(flags, allflags);
  167. else if (*allflags != ' ') mgaps = allflags;
  168. // If command disabled, use allflags for OLDTOY()
  169. printf("#undef OPTSTR_%s\n#define OPTSTR_%s ", command, command);
  170. if (mgaps) printf("\"%s\"\n", mgaps);
  171. else printf("0\n");
  172. if (mgaps != allflags) free(mgaps);
  173. flist = digest(flags);
  174. offlist = aflist = digest(allflags);
  175. printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
  176. command, command, command);
  177. while (offlist) {
  178. char *s = (char []){0, 0, 0, 0};
  179. if (!offlist->command || *offlist->command=='~')
  180. s = offlist->lopt->command;
  181. else {
  182. *s = *offlist->command;
  183. if (127 < (unsigned char)*s) sprintf(s, "X%02X", 127&*s);
  184. }
  185. printf("#undef FLAG_%s\n", s);
  186. offlist = offlist->next;
  187. }
  188. printf("#endif\n\n");
  189. sprintf(out, "#ifdef FOR_%s\n#define CLEANUP_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
  190. command, command, command);
  191. out += strlen(out);
  192. while (aflist) {
  193. char *llstr = bit>30 ? "LL" : "", *s = (char []){0, 0, 0, 0};
  194. int enabled = 0;
  195. // Output flag macro for bare longopts
  196. if (!aflist->command || *aflist->command=='~') {
  197. s = aflist->lopt->command;
  198. if (flist && flist->lopt &&
  199. !strcmp(flist->lopt->command, aflist->lopt->command)) enabled++;
  200. // Output normal flag macro
  201. } else {
  202. *s = *aflist->command;
  203. if (127 < (unsigned char)*s) sprintf(s, "X%02X", 127&*s);
  204. if (flist && flist->command && *aflist->command == *flist->command)
  205. enabled++;
  206. }
  207. out += sprintf(out, "#define FLAG_%s (%s%s<<%d)\n",
  208. s, enabled ? "1" : "FORCED_FLAG", llstr, bit++);
  209. aflist = aflist->next;
  210. if (enabled) flist = flist->next;
  211. }
  212. out = stpcpy(out, "#endif\n\n");
  213. }
  214. if (fflush(0) && ferror(stdout)) return 1;
  215. out = outbuf;
  216. while (*out) {
  217. int i = write(1, outbuf, strlen(outbuf));
  218. if (i<0) return 1;
  219. out += i;
  220. }
  221. return 0;
  222. }