some of new builtins documented
[k8jam.git] / mkjambase.c
blob9012b7731c39a79fa634f777cda334c3e796a5d5
1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * mkjambase.c - turn Jambase into a big C structure
10 * Usage: mkjambase jambase.c Jambase ...
12 * Results look like this:
14 * char *jambase[] = {
15 * "...\n",
16 * ...
17 * 0 };
19 * Handles \'s and "'s specially; knows how to delete blank and comment lines.
21 * 11/04/02 (seiwald) - const-ing for string literals
24 #include <stdio.h>
25 #include <string.h>
26 #include <error.h>
29 #define EMIT(ch) {if (outp-outbuf>2046) error(1, 0, "output line too big\n"); *(outp++) = (ch); }
32 int main (int argc, char **argv, char **envp) {
33 char buf[1024], outbuf[2048];
34 FILE *fin;
35 FILE *fout;
36 char *p, *e, quoteCh, *outp;
37 int doDotC = 0, wasScreen, dontStrip = 0, dropSpaces;
39 if (argc < 3) {
40 fprintf(stderr, "usage: %s jambase.c Jambase ...\n", argv[0]);
41 return -1;
44 if (!(fout = fopen(argv[1], "w"))) {
45 perror(argv[1]);
46 return -1;
49 /* if the file ends in .c generate a C source file */
50 if ((p = strrchr(argv[1], '.')) && !strcmp(p, ".c")) ++doDotC;
52 /* now process the files */
53 argc -= 2, argv += 2;
55 if (doDotC) {
56 fprintf(fout, "/* Generated by mkjambase from Jambase */\n");
57 fprintf(fout, "const char *jambase[] = {\n");
60 for (; argc--; ++argv) {
61 if (!(fin = fopen(*argv, "r"))) {
62 perror(*argv);
63 return -1;
65 if (doDotC) fprintf(fout, "/* %s */\n", *argv); else fprintf(fout, "### %s ###\n", *argv);
67 while (fgets(buf, sizeof(buf), fin)) {
68 if (doDotC) {
69 if (!strncmp(buf, "#DONT_TOUCH", 11)) {
70 dontStrip = !dontStrip;
71 continue;
73 char *p = buf;
74 /* strip leading whitespace */
75 if (!dontStrip) {
76 while (*p && *((unsigned char *)p) <= ' ') ++p;
77 /* drop comments and empty lines */
78 if (*p == '#' || !*p) continue;
80 /* copy; drop comments if # is not in quotes */
81 outp = outbuf; quoteCh = 0; wasScreen = 0;
82 EMIT('"');
83 dropSpaces = 0;
84 for (; *p && *p != '\n' && *p != '\r'; p++) {
85 if (!dontStrip) {
86 if (!quoteCh && !wasScreen && *p == '#') break; /* comment follows; drop it */
88 switch (*p) {
89 case '\\':
90 EMIT('\\'); EMIT('\\');
91 wasScreen = !wasScreen;
92 dropSpaces = 0;
93 break;
94 case '"':
95 EMIT('\\'); EMIT('"');
96 if (!wasScreen) quoteCh = (quoteCh==*p)?0:*p;
97 dropSpaces = 0;
98 break;
99 case '\x27': /* ' */
100 EMIT('\x27');
101 if (!wasScreen) quoteCh = (quoteCh==*p)?0:*p;
102 dropSpaces = 0;
103 break;
104 default:
105 if (!dontStrip && *((unsigned char *)p) <= ' ') {
106 if (wasScreen || !dropSpaces) EMIT(*p);
107 dropSpaces = !wasScreen;
108 } else {
109 EMIT(*p);
110 dropSpaces = 0;
112 wasScreen = 0;
113 break;
116 /* terminate output */
117 *outp = '\0';
118 if (!dontStrip) {
119 /* strip ending whitespace */
120 e = outp-1;
121 while (e >= outbuf && *((unsigned char *)e) <= ' ') --e;
122 *(++e) = '\0';
123 /* drop empty line */
124 if (!outbuf[0]) continue;
126 fprintf(fout, "%s\\n\",\n", outbuf);
127 } else {
128 fprintf(fout, "%s", buf);
131 fclose(fin);
133 if (doDotC) fprintf(fout, "0};\n");
134 fclose(fout);
136 return 0;