aboutsummaryrefslogtreecommitdiff
path: root/scripts/mktags.c
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/mktags.c')
-rw-r--r--scripts/mktags.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/scripts/mktags.c b/scripts/mktags.c
new file mode 100644
index 00000000..e6fceeab
--- /dev/null
+++ b/scripts/mktags.c
@@ -0,0 +1,55 @@
+// Process TAGGED_ARRAY() macros to emit TAG_STRING index macros.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char *argv[])
+{
+ char *tag = 0;
+ int idx = 0;
+
+ for (;;) {
+ char *line = 0, *s;
+ ssize_t len;
+
+ len = getline(&line, &len, stdin);
+ if (len<0) break;
+ while (len && isspace(line[len-1])) line[--len]=0;
+
+ // Very simple parser: If we haven't got a TAG then first line is TAG.
+ // Then look for { followed by "str" (must be on same line, may have
+ // more than one per line), for each one emit #define. Current TAG ended
+ // by ) at start of line.
+
+ if (!tag) {
+ if (!isalpha(*line)) {
+ fprintf(stderr, "bad tag %s\n", line);
+ exit(1);
+ }
+ tag = strdup(line);
+ idx = 0;
+
+ continue;
+ }
+
+ for (s = line; isspace(*s); s++);
+ if (*s == ')') tag = 0;
+ else for (;;) {
+ char *start;
+
+ while (*s && *s != '{') s++;
+ while (*s && *s != '"') s++;
+ if (!*s) break;
+
+ start = ++s;
+ while (*s && *s != '"') {
+ if (!isalpha(*s) && !isdigit(*s)) *s = '_';
+ s++;
+ }
+ printf("% *d\n",
+ 30-printf("#define %s_%.*s", tag, (int)(s-start), start), ++idx);
+ }
+ free(line);
+ }
+}