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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/*
** ____ _
** ___| _ \ ___ _ __| |
** / _ \ |_) / _ \ '__| |
** | __/ __/ __/ | | |
** \___|_| \___|_| |_|
**
** ePerl -- Embedded Perl 5 Language
**
** ePerl interprets an ASCII file bristled with Perl 5 program statements
** by evaluating the Perl 5 code while passing through the plain ASCII
** data. It can operate both as a standard Unix filter for general file
** generation tasks and as a powerful Webserver scripting language for
** dynamic HTML page programming.
**
** ======================================================================
**
** Copyright (c) 1996,1997,1998,1999 Ralf S. Engelschall <rse@engelschall.com>
**
** This program is free software; it may be redistributed and/or modified
** only under the terms of either the Artistic License or the GNU General
** Public License, which may be found in the ePerl source distribution.
** Look at the files ARTISTIC and COPYING or run ``eperl -l'' to receive
** a built-in copy of both license files.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
** Artistic License or the GNU General Public License for more details.
**
** ======================================================================
**
** eperl_pp.c -- ePerl preprocessor
*/
#include "eperl_config.h"
#include "eperl_global.h"
#include "eperl_proto.h"
static char ePerl_PP_ErrorString[1024] = "";
/*
** set PP error string
*/
void ePerl_PP_SetError(char *str, ...)
{
va_list ap;
va_start(ap, str);
vsnprintf(ePerl_PP_ErrorString, sizeof(ePerl_PP_ErrorString), str, ap);
ePerl_PP_ErrorString[sizeof(ePerl_PP_ErrorString)-1] = NUL;
va_end(ap);
return;
}
/*
** get PP error string
*/
char *ePerl_PP_GetError(void)
{
return ePerl_PP_ErrorString;
}
/*
** expand #include directives in buffer
*/
char *ePerl_PP_Process(char *cpInput, char **cppINC, int mode)
{
char *cpOutBuf = NULL;
char *cpEND;
char *cps;
char *cp;
char *cp2;
char *cp3;
char *cp4;
char *cpT = NULL;
char *cpInBuf = NULL;
char *cpBuf;
char caName[1024];
char caArg[1024];
char caStr[1024];
int n;
int l1;
int l2;
int nBuf;
int nOut;
int nOutBuf;
int i;
FILE *fp;
if (strlen(cpInput) == 0) {
/* make sure we return a buffer which the caller can free() */
cpOutBuf = (char *)malloc(sizeof(char) * 1);
*cpOutBuf = NUL;
return cpOutBuf;
}
if (mode == 1) {
/* treat input as buffer */
cpBuf = cpInput;
nBuf = strlen(cpBuf);
}
else {
/* treat input as filename */
if (strncmp(cpInput, "http://", 7) == 0) {
fp = HTTP_openURLasFP(cpInput);
}
else if (*cpInput == '/') {
fp = fopen(cpInput, "r");
}
else {
fp = fopen(cpInput, "r");
if (fp == NULL) {
/* we have to try in all include directories! */
for (i = 0; cppINC[i] != NULL; i++) {
snprintf(caName, sizeof(caName), "%s/%s", cppINC[i], cpInput);
caName[sizeof(caName)-1] = NUL;
if ((fp = fopen(caName, "r")) != NULL)
break;
}
}
}
if (fp == NULL) {
ePerl_PP_SetError("Cannot open source file `%s' for reading", cpInput);
return NULL;
}
nBuf = 16384;
if ((cpInBuf = (char *)malloc(sizeof(char) * nBuf)) == NULL) {
ePerl_PP_SetError("Cannot allocate %d bytes of memory", nBuf);
return NULL;
}
i = 0;
while ((n = fread(cpInBuf+i, 1, 16384, fp)) > 0) {
i += n;
if (n < 16384 && feof(fp))
break;
else {
nBuf += 16384;
if ((cpInBuf = (char *)realloc(cpInBuf, nBuf)) == NULL) {
ePerl_PP_SetError("Cannot reallocate %d bytes of memory", nBuf);
return NULL;
}
continue;
}
}
cpInBuf[i] = '\0';
cpBuf = cpInBuf;
nBuf = i;
fclose(fp);
}
cpEND = cpBuf+nBuf;
cps = cpBuf;
nOutBuf = 64;
cpOutBuf = (char *)malloc(64);
nOut = 0;
while (cps < cpEND) {
/*
* search for any more directives
*/
cp = NULL;
if (cps == cpBuf || ((cps > cpBuf) && (*(cps-1) == '\n'))) {
if ((strncmp(cps, "#include", 8) == 0) && (cp == NULL)) cp = cps;
if ((strncmp(cps, "#sinclude", 9) == 0) && (cp == NULL)) cp = cps;
if ((strncmp(cps, "#if", 3) == 0) && (cp == NULL)) cp = cps;
if ((strncmp(cps, "#elsif", 6) == 0) && (cp == NULL)) cp = cps;
if ((strncmp(cps, "#else", 5) == 0) && (cp == NULL)) cp = cps;
if ((strncmp(cps, "#endif", 6) == 0) && (cp == NULL)) cp = cps;
if ((strncmp(cps, "#c", 2) == 0) && (cp == NULL)) cp = cps;
}
if (((cpT = ep_strnstr(cps, "\n#include", cpEND-cps)) != NULL) && ((cpT < cp) || (cp == NULL))) cp = cpT+1;
if (((cpT = ep_strnstr(cps, "\n#sinclude", cpEND-cps)) != NULL) && ((cpT < cp) || (cp == NULL))) cp = cpT+1;
if (((cpT = ep_strnstr(cps, "\n#if", cpEND-cps)) != NULL) && ((cpT < cp) || (cp == NULL))) cp = cpT+1;
if (((cpT = ep_strnstr(cps, "\n#elsif", cpEND-cps)) != NULL) && ((cpT < cp) || (cp == NULL))) cp = cpT+1;
if (((cpT = ep_strnstr(cps, "\n#else", cpEND-cps)) != NULL) && ((cpT < cp) || (cp == NULL))) cp = cpT+1;
if (((cpT = ep_strnstr(cps, "\n#endif", cpEND-cps)) != NULL) && ((cpT < cp) || (cp == NULL))) cp = cpT+1;
if (((cpT = ep_strnstr(cps, "\n#c", cpEND-cps)) != NULL) && ((cpT < cp) || (cp == NULL))) cp = cpT+1;
if (cp != NULL && (cp == cpBuf || (cp > cpBuf && *(cp-1) == '\n'))) {
/*
* Ok, one more directive found...
*/
/* allocate space and add data up to directive */
i = cp-cps;
nOutBuf += i;
if ((cp2 = (char *)realloc(cpOutBuf, nOutBuf)) == NULL) {
ePerl_PP_SetError("Failed on realloc(buf, %d)", nOutBuf);
free(cpOutBuf);
return NULL;
}
cpOutBuf = cp2;
strncpy(cpOutBuf+nOut, cps, cp-cps);
*(cpOutBuf+nOutBuf-1) = NUL;
nOut += i;
/*
* now process the specific directives...
*/
if (strncmp(cp, "#include", 8) == 0) {
/*
* found a #include directive
*/
cps = cp+8;
/* skip whitespaces */
for ( ; cps < cpEND && (*cps == ' ' || *cps == '\t'); cps++)
;
/* skip possible quotation mark or opening angle bracket */
if (*cps == '"' || *cps == '<')
cps++;
/* check for EOL */
if (*cps == '\n') {
ePerl_PP_SetError("Missing filename or URL for #include directive");
free(cpOutBuf);
return NULL;
}
/* copy the filename and skip to end of line */
for (i = 0; cps < cpEND &&
(*cps != ' ' && *cps != '\t' &&
*cps != '>' && *cps != '"' &&
*cps != '\n' ); )
caName[i++] = *cps++;
caName[i++] = NUL;
for ( ; cps < cpEND && *cps != '\n'; cps++)
;
if (*cps == '\n')
cps++;
/* recursive usage */
if ((cp = ePerl_PP_Process(caName, cppINC, 0 /*mode=file*/)) == NULL)
return NULL;
}
else if (strncmp(cp, "#sinclude", 9) == 0) {
/*
* found a #sinclude directive
*/
cps = cp+9;
/* skip whitespaces */
for ( ; cps < cpEND && (*cps == ' ' || *cps == '\t'); cps++)
;
/* skip possible quotation mark or opening angle bracket */
if (*cps == '"' || *cps == '<')
cps++;
/* check for EOL */
if (*cps == '\n') {
ePerl_PP_SetError("Missing filename or URL for #sinclude directive");
free(cpOutBuf);
return NULL;
}
/* copy the filename and skip to end of line */
for (i = 0; i < sizeof(caName) && cps < cpEND &&
(*cps != ' ' && *cps != '\t' &&
*cps != '>' && *cps != '"' &&
*cps != '\n' ); )
caName[i++] = *cps++;
caName[i++] = NUL;
for ( ; cps < cpEND && *cps != '\n'; cps++)
;
if (*cps == '\n')
cps++;
/* recursive usage */
if ((cp = ePerl_PP_Process(caName, cppINC, 0 /*mode=file*/)) == NULL)
return NULL;
/* make it secure by removing all begin/end delimiters!! */
if ((cp2 = (char *)malloc(strlen(cp)*9/8)) == NULL)
return NULL;
l1 = strlen(ePerl_begin_delimiter);
l2 = strlen(ePerl_end_delimiter);
for (cp3 = cp, cp4 = cp2; *cp3 != NUL; ) {
if (strncasecmp(cp3, ePerl_begin_delimiter, l1) == 0)
cp3 += l1;
else if (strncasecmp(cp3, ePerl_end_delimiter, l2) == 0)
cp3 += l2;
else if (strncmp(cp3, "#include", 8) == 0) {
/* Replace all occurences of #include by #sinclude */
strcpy(cp4, "#sinclude");
cp4 += 9;
}
else
*cp4++ = *cp3++;
}
*cp4 = NUL;
free(cp);
cp = cp2;
}
else if (strncmp(cp, "#if", 3) == 0) {
/*
* found a #if directive
*/
cps = cp+3;
/* skip whitespaces */
for ( ; cps < cpEND && (*cps == ' ' || *cps == '\t'); cps++)
;
if (*cps == '\n') {
ePerl_PP_SetError("Missing expression for #if directive");
free(cpOutBuf);
return NULL;
}
/* copy the argument and create replacement string */
for (i = 0; i < sizeof(caArg) && cps < cpEND && *cps != '\n'; )
caArg[i++] = *cps++;
caArg[i++] = NUL;
if (*cps == '\n')
cps++;
snprintf(caStr, sizeof(caStr), "%s if (%s) { _%s//\n",
ePerl_begin_delimiter, caArg, ePerl_end_delimiter);
caStr[sizeof(caStr)-1] = NUL;
cp = caStr;
}
else if (strncmp(cp, "#elsif", 6) == 0) {
/*
* found a #elsif directive
*/
cps = cp+6;
/* skip whitespaces */
for ( ; cps < cpEND && (*cps == ' ' || *cps == '\t'); cps++)
;
if (*cps == '\n') {
ePerl_PP_SetError("Missing expression for #elsif directive");
free(cpOutBuf);
return NULL;
}
/* copy the argument and create replacement string */
for (i = 0; i < sizeof(caArg) && cps < cpEND && *cps != '\n'; )
caArg[i++] = *cps++;
caArg[i++] = NUL;
if (*cps == '\n')
cps++;
snprintf(caStr, sizeof(caStr), "%s } elsif (%s) { _%s//\n",
ePerl_begin_delimiter, caArg, ePerl_end_delimiter);
caStr[sizeof(caStr)-1] = NUL;
cp = caStr;
}
else if (strncmp(cp, "#else", 5) == 0) {
/*
* found a #else directive
*/
cps = cp+5;
/* skip to end of line */
for (i = 0; cps < cpEND && *cps != '\n'; cps++)
;
if (*cps == '\n')
cps++;
/* create replacement string */
snprintf(caStr, sizeof(caStr), "%s } else { _%s//\n",
ePerl_begin_delimiter, ePerl_end_delimiter);
caStr[sizeof(caStr)-1] = NUL;
cp = caStr;
}
else if (strncmp(cp, "#endif", 6) == 0) {
/*
* found a #endif directive
*/
cps = cp+6;
/* skip to end of line */
for (i = 0; cps < cpEND && *cps != '\n'; cps++)
;
if (*cps == '\n')
cps++;
/* create replacement string */
snprintf(caStr, sizeof(caStr), "%s } _%s//\n",
ePerl_begin_delimiter, ePerl_end_delimiter);
caStr[sizeof(caStr)-1] = NUL;
cp = caStr;
}
else if (strncmp(cp, "#c", 2) == 0) {
/*
* found a #c directive
*/
cps = cp+2;
/* skip to end of line */
for (i = 0; cps < cpEND && *cps != '\n'; cps++)
;
if (*cps == '\n')
cps++;
/* create replacement string: just a newline
* to preserve line numbers */
sprintf(caStr, "\n");
cp = caStr;
}
/* allocate space and add replacement data */
i = strlen(cp);
nOutBuf += i;
if ((cp2 = (char *)realloc(cpOutBuf, nOutBuf)) == NULL) {
ePerl_PP_SetError("Failed on realloc(buf, %d)", nOutBuf);
free(cpOutBuf);
return NULL;
}
cpOutBuf = cp2;
strncpy(cpOutBuf+nOut, cp, nOutBuf - nOut);
*(cpOutBuf+nOutBuf-1) = NUL;
nOut += i;
continue;
}
else {
/* no more found */
/* allocate space and add data */
nOutBuf += (cpEND-cps);
if ((cp2 = (char *)realloc(cpOutBuf, nOutBuf)) == NULL) {
ePerl_PP_SetError("Failed on realloc(buf, %d)", nOutBuf);
free(cpOutBuf);
/* XXX free(cp); */
return NULL;
}
cpOutBuf = cp2;
strncpy(cpOutBuf+nOut, cps, nOutBuf - nOut);
*(cpOutBuf+nOutBuf-1) = NUL;
break;
}
}
if (cpInBuf)
free(cpInBuf);
return cpOutBuf;
}
char *ePerl_PP(char *cpBuf, char **cppINC)
{
return ePerl_PP_Process(cpBuf, cppINC, 1 /*mode=buffer*/);
}
/*EOF*/
|