removed unused rule 'Strip' (if was not even defined)
[k8jam.git] / src / mkjambase.c
blob29596d3c895bd982c6668dede1562601b263bbf9
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
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 //#define MKJAMBASE_COMPACT
31 static void fatal (const char *msg) {
32 fprintf(stdout, "FATAL: %s\n", msg);
33 exit(1);
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];
42 FILE *fin;
43 FILE *fout;
44 char *p, *e, quoteCh, *outp;
45 int doDotC = 0, wasScreen, dontStrip = 0, dropSpaces;
47 if (argc < 3) {
48 fprintf(stderr, "usage: %s jambase.c Jambase ...\n", argv[0]);
49 return 1;
52 if (!(fout = fopen(argv[1], "w"))) {
53 perror(argv[1]);
54 return 1;
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 */
61 argc -= 2, argv += 2;
63 if (doDotC) {
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"))) {
70 fclose(fout);
71 perror(*argv);
72 return 1;
74 if (doDotC) fprintf(fout, "/* %s */\n", *argv); else fprintf(fout, "### %s ###\n", *argv);
76 while (fgets(buf, sizeof(buf), fin)) {
77 if (doDotC) {
78 #ifdef MKJAMBASE_COMPACT
79 if (!strncmp(buf, "#DONT_TOUCH", 11)) {
80 dontStrip = !dontStrip;
81 continue;
83 #else
84 dontStrip = 1;
85 #endif
86 char *p = buf;
87 /* strip leading whitespace */
88 if (!dontStrip) {
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;
95 EMIT('"');
96 dropSpaces = 0;
97 for (; *p && *p != '\n' && *p != '\r'; p++) {
98 if (!dontStrip) {
99 if (!quoteCh && !wasScreen && *p == '#') break; /* comment follows; drop it */
101 switch (*p) {
102 case '\\':
103 EMIT('\\'); EMIT('\\');
104 wasScreen = !wasScreen;
105 dropSpaces = 0;
106 break;
107 case '"':
108 EMIT('\\'); EMIT('"');
109 if (!wasScreen) quoteCh = (quoteCh==*p)?0:*p;
110 dropSpaces = 0;
111 break;
112 case '\x27': /* ' */
113 EMIT('\x27');
114 if (!wasScreen) quoteCh = (quoteCh==*p)?0:*p;
115 dropSpaces = 0;
116 break;
117 default:
118 if (!dontStrip && *((unsigned char *)p) <= ' ') {
119 if (wasScreen || !dropSpaces) EMIT(*p);
120 dropSpaces = !wasScreen;
121 } else {
122 EMIT(*p);
123 dropSpaces = 0;
125 wasScreen = 0;
126 break;
129 /* terminate output */
130 *outp = '\0';
131 if (!dontStrip) {
132 /* strip ending whitespace */
133 e = outp-1;
134 while (e >= outbuf && *((unsigned char *)e) <= ' ') --e;
135 *(++e) = '\0';
136 /* drop empty line */
137 if (!outbuf[0]) continue;
139 fprintf(fout, "%s\\n\",\n", outbuf);
140 } else {
141 fprintf(fout, "%s", buf);
144 fclose(fin);
146 if (doDotC) fprintf(fout, "0};\n");
147 fclose(fout);
149 return 0;