2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * mkjambase.c - turn Jambase into a big C structure
10 * Usage: mkjambase jambase.c Jambase ...
12 * Results look like this:
19 * Handles \'s and "'s specially; knows how to delete blank and comment lines.
21 * 11/04/02 (seiwald) - const-ing for string literals
28 //#define MKJAMBASE_COMPACT
31 static void fatal (const char *msg
) {
32 fprintf(stdout
, "FATAL: %s\n", msg
);
37 #define EMIT(ch) {if (outp-outbuf > 2046) fatal("output line too big\n"); *(outp++) = (ch); }
40 int main (int argc
, char **argv
) {
41 char buf
[1024], outbuf
[2048];
44 char *p
, *e
, quoteCh
, *outp
;
45 int doDotC
= 0, wasScreen
, dontStrip
= 0, dropSpaces
;
48 fprintf(stderr
, "usage: %s jambase.c Jambase ...\n", argv
[0]);
52 if (!(fout
= fopen(argv
[1], "w"))) {
57 /* if the file ends in .c generate a C source file */
58 if ((p
= strrchr(argv
[1], '.')) && !strcmp(p
, ".c")) ++doDotC
;
60 /* now process the files */
64 fprintf(fout
, "/* Generated by mkjambase from Jambase */\n");
65 fprintf(fout
, "const char *jambase[] = {\n");
68 for (; argc
--; ++argv
) {
69 if (!(fin
= fopen(*argv
, "r"))) {
74 if (doDotC
) fprintf(fout
, "/* %s */\n", *argv
); else fprintf(fout
, "### %s ###\n", *argv
);
76 while (fgets(buf
, sizeof(buf
), fin
)) {
78 #ifdef MKJAMBASE_COMPACT
79 if (!strncmp(buf
, "#DONT_TOUCH", 11)) {
80 dontStrip
= !dontStrip
;
87 /* strip leading whitespace */
89 while (*p
&& *((unsigned char *)p
) <= ' ') ++p
;
90 /* drop comments and empty lines */
91 if (*p
== '#' || !*p
) continue;
93 /* copy; drop comments if # is not in quotes */
94 outp
= outbuf
; quoteCh
= 0; wasScreen
= 0;
97 for (; *p
&& *p
!= '\n' && *p
!= '\r'; p
++) {
99 if (!quoteCh
&& !wasScreen
&& *p
== '#') break; /* comment follows; drop it */
103 EMIT('\\'); EMIT('\\');
104 wasScreen
= !wasScreen
;
108 EMIT('\\'); EMIT('"');
109 if (!wasScreen
) quoteCh
= (quoteCh
==*p
)?0:*p
;
114 if (!wasScreen
) quoteCh
= (quoteCh
==*p
)?0:*p
;
118 if (!dontStrip
&& *((unsigned char *)p
) <= ' ') {
119 if (wasScreen
|| !dropSpaces
) EMIT(*p
);
120 dropSpaces
= !wasScreen
;
129 /* terminate output */
132 /* strip ending whitespace */
134 while (e
>= outbuf
&& *((unsigned char *)e
) <= ' ') --e
;
136 /* drop empty line */
137 if (!outbuf
[0]) continue;
139 fprintf(fout
, "%s\\n\",\n", outbuf
);
141 fprintf(fout
, "%s", buf
);
146 if (doDotC
) fprintf(fout
, "0};\n");