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 to delete blank and comment lines.
21 * 11/04/02 (seiwald) - const-ing for string literals
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];
36 char *p
, *e
, quoteCh
, *outp
;
37 int doDotC
= 0, wasScreen
, dontStrip
= 0;
40 fprintf(stderr
, "usage: %s jambase.c Jambase ...\n", argv
[0]);
44 if (!(fout
= fopen(argv
[1], "w"))) {
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 */
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"))) {
65 if (doDotC
) fprintf(fout
, "/* %s */\n", *argv
); else fprintf(fout
, "### %s ###\n", *argv
);
67 while (fgets(buf
, sizeof(buf
), fin
)) {
69 if (!strncmp(buf
, "#DONT_TOUCH", 11)) {
70 dontStrip
= !dontStrip
;
74 /* strip leading whitespace */
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;
83 for (; *p
&& *p
!= '\n' && *p
!= '\r'; p
++) {
85 if (!quoteCh
&& !wasScreen
&& *p
== '#') break; /* comment follows; drop it */
89 EMIT('\\'); EMIT('\\');
90 wasScreen
= !wasScreen
;
93 EMIT('\\'); EMIT('"');
94 if (!wasScreen
) quoteCh
= (quoteCh
==*p
)?0:*p
;
98 if (!wasScreen
) quoteCh
= (quoteCh
==*p
)?0:*p
;
106 /* terminate output */
109 /* strip ending whitespace */
111 while (e
>= outbuf
&& *((unsigned char *)e
) <= ' ') e
--;
113 /* drop empty line */
114 if (!outbuf
[0]) continue;
116 fprintf(fout
, "%s\\n\",\n", outbuf
);
117 } else fprintf(fout
, "%s", buf
);
121 if (doDotC
) fprintf(fout
, "0};\n");