mktags.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Process TAGGED_ARRAY() macros to emit TAG_STRING index macros.
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. int main(int argc, char *argv[])
  7. {
  8. char *tag = 0;
  9. int idx = 0;
  10. for (;;) {
  11. char *line = 0, *s;
  12. ssize_t len;
  13. len = getline(&line, (void *)&len, stdin);
  14. if (len<0) break;
  15. while (len && isspace(line[len-1])) line[--len]=0;
  16. // Very simple parser: If we haven't got a TAG then first line is TAG.
  17. // Then look for { followed by "str" (must be on same line, may have
  18. // more than one per line), for each one emit #define. Current TAG ended
  19. // by ) at start of line.
  20. if (!tag) {
  21. if (!isalpha(*line)) {
  22. fprintf(stderr, "bad tag %s\n", line);
  23. exit(1);
  24. }
  25. tag = strdup(line);
  26. idx = 0;
  27. continue;
  28. }
  29. for (s = line; isspace(*s); s++);
  30. if (*s == ')') tag = 0;
  31. else for (;;) {
  32. char *start;
  33. while (*s && *s != '{') s++;
  34. while (*s && *s != '"') s++;
  35. if (!*s) break;
  36. start = ++s;
  37. while (*s && *s != '"') {
  38. if (!isalpha(*s) && !isdigit(*s)) *s = '_';
  39. s++;
  40. }
  41. printf("#define %s_%*.*s %d\n", tag, -40, (int)(s-start), start, idx);
  42. printf("#define _%s_%*.*s (1%s<<%d)\n", tag, -39, (int)(s-start), start,
  43. idx>31 ? "LL": "", idx);
  44. idx++;
  45. }
  46. free(line);
  47. }
  48. }