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