1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/**************************************************************
* A simple cpp preprocessor processing simple tokens.
* It support the binary syntax @if token, @else, @endif
* as well as @include (can be recursive) and @comment. Space
* between if and token are facultative.
* Conditions can be nested up to 32 levels.
* Will compile to support 32 tokens. Easily expandable.
*
* Usage:
*
* simplecpp [-v] -Dtoken1 [-Dtoken2 ...] [input [output]]
*
* Simple is beautiful! Conserve true line number. Discard false.
*
* Martin-D. Lacasse 1994
*
**************************************************************/
#include <stdio.h>
#include <string.h>
char *gettok(char *);
#define MAXTOKEN 32
#define MAXIFS 32
#define MAXLVL 2 /* Keep it simple! */
#define KEYCHAR '@'
#define USAGE fprintf(stderr, "Usage: %s [-v] -Ddef [-Ddef2...] [infile [outfile]]\n", argv[0]); exit(1)
#define SWALLOW_SPACE(c) while (*c && (*c == ' ' || *c == '\t') ) c++;
int main(int argc, char **argv)
{
FILE *fpin[MAXLVL], *fpout;
char bufline[1024], token[MAXTOKEN][256];
int lineno[MAXLVL];
int truestate[MAXIFS];
int falsestate;
int verbose;
int iflvl, fplvl, total, toptoken;
int i;
char *cp, *infile, *cptok;
fpin[0] = stdin; infile = "stdin";
fpout = stdout;
fplvl = total = toptoken = 0;
falsestate = iflvl = -1;
verbose = 0;
for (i=1;i<argc;i++) { /* PARSE ARGUMENTS */
if (strncmp(argv[i], "-v", 2) == 0) {
verbose++;
continue;
}
if (strncmp(argv[i], "-D", 2) == 0) {
if (toptoken >= MAXTOKEN) {
fprintf(stderr,
"%s: Too many tokens for present capacity.\n", argv[0]);
exit(1);
}
strncpy(token[toptoken], argv[i]+2, 255);
if (strlen(token[toptoken]))
toptoken++;
}
else if (fpin[0] == stdin) {
if ((fpin[fplvl] = fopen(argv[i], "r")) == NULL) {
fprintf(stderr, "%s: %s: File not found\n", argv[0], argv[i]);
exit(1);
}
infile = argv[i];
}
else if (fpout == stdout) {
if ((fpout = fopen(argv[i], "w")) == NULL) {
fprintf(stderr,
"%s: %s: Permission denied.\n", argv[0], argv[i]);
exit(1);
}
}
else {
USAGE;
}
} /* END OF PARSING ARGUMENTS */
lineno[fplvl] = 0;
while (fplvl >= 0) { /* HERE WE START */
if (fgets(bufline, 1023, fpin[fplvl]) == NULL) {
fclose(fpin[fplvl]);
if (verbose) {
if (fplvl) {
fprintf(stderr, "%d lines.\n", lineno[fplvl]);
total += lineno[fplvl];
}
else {
fprintf(stderr, "%s: %d lines.\n", infile, lineno[0]);
total += lineno[fplvl];
fprintf(stderr, "Total: %d lines.\n", total);
}
}
fplvl--;
continue;
}
lineno[fplvl]++;
if (bufline[0] == KEYCHAR) {
cp = bufline+1;
SWALLOW_SPACE(cp);
if ((cptok = gettok(cp)) == NULL) {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl], bufline);
fprintf(stderr, "String too long for compiled capacity.\n");
exit(1);
}
if (strncmp(cptok, "if", 2) == 0) {
if (++iflvl > MAXIFS) {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl], bufline);
fprintf(stderr, "If too nested for compiled capacity.\n");
exit(1);
}
cp += 2;
SWALLOW_SPACE(cp);
if ((cptok = gettok(cp)) == NULL) {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl], bufline);
fprintf(stderr, "String too long for compiled capacity.\n");
exit(1);
}
for (i=0;i<toptoken;i++) {
if (strcmp(cptok, token[i]) == 0) {
truestate[iflvl] = 1;
break;
}
}
if (falsestate < 0 && !truestate[iflvl])
falsestate = iflvl;
}
else if (strncmp(cptok, "else", 4) == 0) {
if (iflvl < 0) {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl], bufline);
fprintf(stderr, "Found else without if!\n");
exit(1);
}
truestate[iflvl] = !truestate[iflvl];
if (falsestate == iflvl)
falsestate = -1;
else if (falsestate < 0 && !truestate[iflvl])
falsestate = iflvl;
}
else if (strncmp(cptok, "endif", 5) == 0) {
if (iflvl < 0) {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl], bufline);
fprintf(stderr, "Found endif without if.\n");
exit(1);
}
truestate[iflvl] = 0; /* RESET TO FALSE */
if (falsestate == iflvl)
falsestate = -1;
iflvl--;
}
else if (strncmp(cptok, "include", 7) == 0) {
if (falsestate < 0) {
if (++fplvl > MAXLVL-1) {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl-1], bufline);
fprintf(stderr, "Too many @include's!\n");
exit(1);
}
lineno[fplvl] = 0;
cp += 7;
SWALLOW_SPACE(cp);
if ((cptok = gettok(cp)) == NULL) {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl], bufline);
fprintf(stderr,
"String too long for compiled capacity.\n");
exit(1);
}
if ((fpin[fplvl] = fopen(cptok, "r")) == NULL) {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl], bufline);
fprintf(stderr,
"%s: File not found\n", cptok);
exit(1);
}
if (verbose)
fprintf(stderr, "%s: ", cptok);
}
continue;
}
else if (strncmp(cptok, "comment", 7) == 0) {
continue;
}
else {
fprintf(stderr, "%s: Error: Line %d: %s",
argv[0], lineno[fplvl], bufline);
fprintf(stderr, "Unknown command.\n");
exit(1);
}
continue;
}
if (falsestate < 0)
fputs(bufline, fpout);
}
if (iflvl >= 0) {
fprintf(stderr, "%s: Error: Exiting with an open if statement.\n",
argv[0]);
exit(1);
}
exit(0);
}
char *gettok(char *cpin)
{
/* It is safe to use static but be aware of recursive limits ;-( */
static char string[256];
int i = 0;
SWALLOW_SPACE(cpin);
while (*cpin && i < 256) {
if (*cpin == ' ' || *cpin == '\t' || *cpin == '\n')
break;
string[i++] = *cpin++;
}
string[i] = '\0';
if (i == 256)
return (0);
return(string);
}
|