aboutsummaryrefslogtreecommitdiff
path: root/scripts/config2help.c
blob: cf65641c14d8ab9ba5eee07a22cdc73aedce7223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "toys.h"

// Humor toys.h
struct toy_context toys;
char libbuf[4096], toybuf[4096];
void show_help(void) {;}
void toy_exec(char *argv[]) {;}

// Parse config files into data structures.

struct symbol {
  struct symbol *next;
  int enabled;
  char *name, *depends;
  struct double_list *help;
} *sym;

char *keyword(char *name, char *line)
{
  int len = strlen(name);

  while (isspace(*line)) line++;
  if (strncmp(name, line, len)) return 0;
  line += len;
  if (*line && !isspace(*line)) return 0;
  while (isspace(*line)) line++;

  return line;
}

void parse(char *filename)
{
  FILE *fp = xfopen(filename, "r");
  struct symbol *new = 0;
  int help = 0;

  for (;;) {
    char *s, *line = NULL;
    size_t len;

    // Read line, trim whitespace at right edge.
    if (getline(&line, &len, fp) < 1) break;
    s = line+strlen(line);
    while (--s >= line) {
      if (!isspace(*s)) break;
      *s = 0;
    }

    // source or config keyword at left edge?
    if (*line && !isspace(*line)) {
      help = 0;
      if ((s = keyword("config", line))) {
        new = xzalloc(sizeof(struct symbol));
        new->next = sym;
        new->name = s;
        sym = new;
      } else if ((s = keyword("source", line))) parse(s);

      continue;
    }
    if (!new) continue;

    if (help) dlist_add(&(new->help), line);
    else if ((s = keyword("depends", line)) && (s = keyword("on", s)))
      new->depends = s;
    else if (keyword("help", line)) help++;
  }

  fclose(fp);
}

int main(int argc, char *argv[])
{
  FILE *fp;
  char *file;

  if (argc != 3) {
    fprintf(stderr, "usage: config2help Config.in .config\n");
    exit(1);
  }

  // Read Config.in
  parse(argv[1]);

  // read .config
  fp = xfopen(argv[2], "r");
  for (;;) {
    char *line = NULL;
    size_t len;

    if (getline(&line, &len, fp) < 1) break;
    if (!strncmp("CONFIG_", line, 7)) {
      struct symbol *try;
      char *s = line+7;

      for (try=sym; try; try=try->next) {
        len = strlen(try->name);
        if (!strncmp(try->name, s, len) && s[len]=='=' && s[len+1]=='y') {
          try->enabled++;
          break;
        } 
      }
    }
  }

  // Collate help according to usage, depends, and .config

  // Loop through each entry, finding duplicate enabled "usage:" names

  for (;;) {
    struct symbol *throw = 0, *catch;
    char *this, *that, *name;
    int len;

    // find a usage: name and collate all enabled entries with that name
    for (catch = sym; catch; catch = catch->next) {
      if (catch->enabled != 1) continue;
      if (catch->help && (this = keyword("usage:", catch->help->data))) {
        struct double_list *bang;

        if (!throw) {
          throw = catch;
          catch->enabled++;
          name = this;
          while (!isspace(*this) && *this) this++;
          len = (that = this)-name;
          while (isspace(*that)) that++;

          continue;
        }

        if (strncmp(name, this, len) || !isspace(this[len])) continue;
        catch->enabled++;

        // Suck help text out of throw into catch.
        throw->enabled = 0;

        // splice together circularly linked lists
        bang = throw->help->prev;
        throw->help->prev->next = catch->help;
        throw->help->prev = catch->help->prev;
        catch->help->prev->next = throw->help;
        catch->help->prev = bang;
        throw->help = 0;
        throw = catch;
      }
    }

    // Did we find one?

    if (!throw) break;

      // Collate first [-abc] option block?

//      if (*s == '[' && s[1] == '-' && s[2] != '-') {
//      }
  }

  // Print out help #defines
  while (sym) {
    struct double_list *dd;

    if (sym->help) {
      int i, padlen = 0;
      char *s = xstrdup(sym->name);

      for (i = 0; s[i]; i++) s[i] = tolower(s[i]);
      printf("#define help_%s \"", s);
      free(s);

      // Measure leading whitespace of first line
      dd = sym->help;
      while (isspace(dd->data[padlen])) padlen++;

      for (;;) {
        i = padlen;

        // Trim leading whitespace
        s = dd->data;
        while (isspace(*s) && i) {
          s++;
          i--;
        }
        for (i=0; s[i]; i++) {
          if (s[i] == '"' || s[i] == '\\') putchar('\\');
          putchar(s[i]);
        }
        putchar('\\');
        putchar('n');
        dd = dd->next;
        if (dd == sym->help) break;
      }
      printf("\"\n");
    }
    sym = sym->next;
  }
}