mkflags.c 6.6 KB

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