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
, char **envp
) {
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"))) {
73 if (doDotC
) fprintf(fout
, "/* %s */\n", *argv
); else fprintf(fout
, "### %s ###\n", *argv
);
75 while (fgets(buf
, sizeof(buf
), fin
)) {
77 #ifdef MKJAMBASE_COMPACT
78 if (!strncmp(buf
, "#DONT_TOUCH", 11)) {
79 dontStrip
= !dontStrip
;
86 /* strip leading whitespace */
88 while (*p
&& *((unsigned char *)p
) <= ' ') ++p
;
89 /* drop comments and empty lines */
90 if (*p
== '#' || !*p
) continue;
92 /* copy; drop comments if # is not in quotes */
93 outp
= outbuf
; quoteCh
= 0; wasScreen
= 0;
96 for (; *p
&& *p
!= '\n' && *p
!= '\r'; p
++) {
98 if (!quoteCh
&& !wasScreen
&& *p
== '#') break; /* comment follows; drop it */
102 EMIT('\\'); EMIT('\\');
103 wasScreen
= !wasScreen
;
107 EMIT('\\'); EMIT('"');
108 if (!wasScreen
) quoteCh
= (quoteCh
==*p
)?0:*p
;
113 if (!wasScreen
) quoteCh
= (quoteCh
==*p
)?0:*p
;
117 if (!dontStrip
&& *((unsigned char *)p
) <= ' ') {
118 if (wasScreen
|| !dropSpaces
) EMIT(*p
);
119 dropSpaces
= !wasScreen
;
128 /* terminate output */
131 /* strip ending whitespace */
133 while (e
>= outbuf
&& *((unsigned char *)e
) <= ' ') --e
;
135 /* drop empty line */
136 if (!outbuf
[0]) continue;
138 fprintf(fout
, "%s\\n\",\n", outbuf
);
140 fprintf(fout
, "%s", buf
);
145 if (doDotC
) fprintf(fout
, "0};\n");