|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <ctype.h> ! 3: ! 4: #define NFILES 500 ! 5: #define NRULES 50 ! 6: #define NTAGS 50 ! 7: #define MAXFLDS 20 ! 8: #define MAXRULES 10 ! 9: ! 10: #define ARB 200 ! 11: ! 12: typedef struct file { ! 13: char *f_obj; ! 14: char *f_src; ! 15: char *f_type; ! 16: } File; ! 17: ! 18: File files[NFILES]; ! 19: ! 20: typedef struct rule { ! 21: char *r_type; ! 22: char *r_dep; ! 23: char *r_rules[MAXRULES]; ! 24: } Rule; ! 25: ! 26: Rule rules[NRULES]; ! 27: ! 28: char *wanted[NTAGS]; ! 29: ! 30: main(argc, argv) ! 31: int argc; ! 32: char **argv; ! 33: { ! 34: char *templ, *flist, *out; ! 35: register char **p; ! 36: ! 37: /* hack */ ! 38: templ = "templ"; ! 39: flist = "files"; ! 40: out = NULL; ! 41: p = wanted; ! 42: while (--argc > 0) { ! 43: if (p >= &wanted[NTAGS-1]) { ! 44: fprintf(stderr, "mkconf: too many tags\n"); ! 45: exit(1); ! 46: } ! 47: *p++ = *++argv; ! 48: } ! 49: *p = NULL; ! 50: readlist(flist); ! 51: putmake(templ, out); ! 52: exit(0); ! 53: } ! 54: ! 55: /* ! 56: * read the list of files and stuff ! 57: * colon-separated fields: ! 58: * object: sources: type: tags ! 59: */ ! 60: ! 61: readlist(list) ! 62: char *list; ! 63: { ! 64: FILE *fp; ! 65: register File *f; ! 66: char *fld[MAXFLDS]; ! 67: char buf[ARB], xbuf[ARB]; ! 68: char *save(); ! 69: ! 70: if ((fp = fopen(list, "r")) == NULL) { ! 71: fprintf(stderr, "mkconf: %s: cannot open\n", list); ! 72: exit(1); ! 73: } ! 74: f = files; ! 75: while (getline(buf, ARB, fp)) { ! 76: if (buf[0] == 0 || buf[0] == '#') ! 77: continue; ! 78: strcpy(xbuf, buf); ! 79: if (crack(xbuf, ':', fld) != 4) { ! 80: fprintf(stderr, "bad files line: %s\n", buf); ! 81: continue; ! 82: } ! 83: if (wanttag(fld[3]) == 0) ! 84: continue; ! 85: if (f >= &files[NFILES-1]) { ! 86: fprintf(stderr, "mkconf: too many files\n"); ! 87: exit(1); ! 88: } ! 89: f->f_obj = save(fld[0]); ! 90: f->f_src = save(fld[1]); ! 91: f->f_type = save(fld[2]); ! 92: f++; ! 93: } ! 94: f->f_obj = NULL; ! 95: fclose(fp); ! 96: } ! 97: ! 98: wanttag(tags) ! 99: char *tags; ! 100: { ! 101: char *t[MAXFLDS]; ! 102: register int i, j; ! 103: register int n; ! 104: ! 105: detab(tags); ! 106: n = crack(tags, ' ', t); ! 107: for (i = 0; wanted[i]; i++) ! 108: for (j = 0; j < n; j++) ! 109: if (strcmp(wanted[i], t[j]) == 0) ! 110: return (1); ! 111: return (0); ! 112: } ! 113: ! 114: /* ! 115: * print the makefile ! 116: * read the template, store the rules ! 117: * spit out the literal stuff ! 118: * spit out the rules ! 119: */ ! 120: ! 121: putmake(templ, out) ! 122: char *templ, *out; ! 123: { ! 124: FILE *tf, *of; ! 125: char buf[ARB]; ! 126: register File *f; ! 127: ! 128: if ((tf = fopen(templ, "r")) == NULL) { ! 129: fprintf(stderr, "mkconf: %s: cannot open\n", templ); ! 130: exit(1); ! 131: } ! 132: if (out == NULL) ! 133: of = stdout; ! 134: else if ((of = fopen(out, "w")) == NULL) { ! 135: fprintf(stderr, "mkconf: %s: cannot create\n", out); ! 136: exit(1); ! 137: } ! 138: getrules(tf); ! 139: fprintf(of, "FILES="); ! 140: for (f = files; f->f_obj; f++) ! 141: fprintf(of, " %s", f->f_obj); ! 142: putc('\n', of); ! 143: while (getline(buf, ARB, tf)) ! 144: fprintf(of, "%s\n", buf); ! 145: fclose(tf); ! 146: for (f = files; f->f_obj; f++) { ! 147: fprintf(of, "%s: %s", f->f_obj, f->f_src); ! 148: putrule(f, of); ! 149: } ! 150: fclose(of); ! 151: } ! 152: ! 153: getrules(tf) ! 154: FILE *tf; ! 155: { ! 156: char buf[ARB]; ! 157: register Rule *r; ! 158: register int i; ! 159: register char *s; ! 160: char *strchr(); ! 161: char *save(); ! 162: ! 163: r = NULL; ! 164: while (getline(buf, ARB, tf)) { ! 165: if (buf[0] == '%' && buf[1] == '%' && buf[2] == 0) ! 166: break; ! 167: if (buf[0] == 0 || buf[0] == '#') ! 168: continue; ! 169: if (buf[0] != '\t') { ! 170: if (r == NULL) ! 171: r = rules; ! 172: else if (++r >= &rules[NRULES-1]) { ! 173: fprintf(stderr, "mkconf: too many object types: %s\n", buf); ! 174: exit(1); ! 175: } ! 176: if ((s = strchr(buf, ':')) == NULL) { ! 177: fprintf(stderr, "mkconf: bad line in template: %s\n", buf); ! 178: continue; ! 179: } ! 180: if (*s) ! 181: *s++ = 0; ! 182: while (isspace(*s)) ! 183: s++; ! 184: r->r_type = save(buf); ! 185: r->r_dep = save(s); ! 186: i = 0; ! 187: continue; ! 188: } ! 189: if (r == NULL) { ! 190: fprintf(stderr, "mkconf: bad line in template: %s\n", buf); ! 191: continue; ! 192: } ! 193: if (i >= MAXRULES - 1) { ! 194: fprintf(stderr, "mkconf: too many lines in template: %s\n", buf); ! 195: exit(1); ! 196: } ! 197: r->r_rules[i] = save(buf); ! 198: } ! 199: } ! 200: ! 201: putrule(f, of) ! 202: File *f; ! 203: FILE *of; ! 204: { ! 205: register Rule *r; ! 206: register int i; ! 207: char buf[ARB]; ! 208: char *deps[MAXFLDS]; ! 209: int n; ! 210: ! 211: for (r = rules; r->r_type; r++) ! 212: if (strcmp(r->r_type, f->f_type) == 0) ! 213: break; ! 214: if (r->r_type == NULL) { ! 215: putc('\n', of); ! 216: return; ! 217: } ! 218: strcpy(buf, f->f_src); ! 219: strcat(buf, " "); ! 220: strcat(buf, r->r_dep); ! 221: detab(buf); ! 222: n = crack(buf, ' ', deps); ! 223: fprintf(of, " %s\n", r->r_dep); ! 224: for (i = 0; r->r_rules[i]; i++) ! 225: expout(of, r->r_rules[i], deps, n); ! 226: } ! 227: ! 228: /* ! 229: * print a line of a rule ! 230: * $n expands to the nth arg (source file) ! 231: */ ! 232: ! 233: expout(fp, s, args, n) ! 234: FILE *fp; ! 235: register char *s; ! 236: register char **args; ! 237: int n; ! 238: { ! 239: register int i; ! 240: ! 241: for (; *s; s++) { ! 242: if (*s == '$' && isdigit(s[1])) { ! 243: for (s++, i = 0; isdigit(*s); s++) ! 244: i = i * 10 + *s - '0'; ! 245: s--; ! 246: i--; ! 247: if (i >= 0 && i < n) ! 248: fprintf(fp, "%s", args[i]); ! 249: continue; ! 250: } ! 251: putc(*s, fp); ! 252: } ! 253: putc('\n', fp); ! 254: } ! 255: ! 256: getline(buf, n, fp) ! 257: char *buf; ! 258: register int n; ! 259: FILE *fp; ! 260: { ! 261: register int c; ! 262: register char *p; ! 263: ! 264: p = buf; ! 265: while (--n >= 0) { ! 266: if ((c = getc(fp)) == EOF || c == '\n') ! 267: break; ! 268: *p++ = c; ! 269: } ! 270: *p = 0; ! 271: if (c == EOF && p == buf) ! 272: return (0); ! 273: return (1); ! 274: } ! 275: ! 276: char * ! 277: save(s) ! 278: char *s; ! 279: { ! 280: char *ss; ! 281: char *malloc(); ! 282: ! 283: if ((ss = malloc(strlen(s) + 1)) == NULL) { ! 284: fprintf(stderr, "mkconf: out of mem\n"); ! 285: exit(1); ! 286: } ! 287: strcpy(ss, s); ! 288: return (ss); ! 289: } ! 290: ! 291: detab(s) ! 292: register char *s; ! 293: { ! 294: ! 295: for (; *s; s++) ! 296: if (*s == '\t') ! 297: *s = ' '; ! 298: } ! 299: ! 300: ! 301: /* ! 302: * split a line into fields ! 303: * s is the line; it is destroyed ! 304: * c is the character that marks the end of a field; ! 305: * it may be followed by blanks and tabs ! 306: * a is an array of pointers to the fields, NULL terminated ! 307: * the number of fields is returned ! 308: * there are at most MAXFLDS fields ! 309: */ ! 310: ! 311: int ! 312: crack(s, c, a) ! 313: register char *s; ! 314: register char c; ! 315: register char **a; ! 316: { ! 317: register int n; ! 318: ! 319: n = 0; ! 320: for (;;) { ! 321: while (*s && isspace(*s)) ! 322: s++; ! 323: if (*s == 0) ! 324: break; ! 325: if (n >= MAXFLDS - 1) ! 326: break; /* and lose the remainder. oh well. */ ! 327: *a++ = s; ! 328: n++; ! 329: while (*s && *s != c) ! 330: s++; ! 331: if (*s) ! 332: *s++ = 0; ! 333: } ! 334: *a = NULL; ! 335: return (n); ! 336: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.