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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
/* -*- c -*- */
/*
* filler.c
*
* chpp
*
* Copyright (C) 1997-1998 Heinz Deinhart
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*****************************************************************************
* Command parse ,,black box'' (named by Schani)
*
* Created by a fluctuation of void *
*/
#define _FILLER_C_
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "memory.h"
#include "dynstring.h"
#include "chash.h"
#include "error.h"
#include "depends.h"
#include "filler.h"
#include "commands.h"
#include "recorder.h"
/* #undef MERD */
/* define MERD(x) ; */
/* include paths */
dynstring *defDirs = 0;
int nrOfDefDirs = 0;
/* used by filler & commands */
int bufPosAtCommandBegin;
char filler_cmdChar;
int flowsExtern = 0;
dynstring externFlow;
FlowStack *flowStack;
int flowDepth = 1;
int incFiles;
int zeilenNummer = 0;
dynstring currentFileName;
dynstring currentFilePath;
InputFileStack *ifStack = 0;
/* ------------------------- */
static FILE* inFile;
static int activeNL, bufOverflow;
static dynstring tmpStr, overflowStr;
/* static int commandWantsFlush = 0; */
static dynstring nextCmdCommand;
static dynstring nextCmdArgument;
static void *nextCmdFun = 0;
static hash cmdHash;
int warningAlert = 0;
void
cutExtern( int i ) {
dsShrinkRear( &externFlow, i );
}
void
goExtern( char ch ) {
dsAppendChar( &externFlow, ch );
}
void
goExternDS( dynstring *ds ) {
dsAppendString( &externFlow, ds->data, ds->length );
}
void
flowDepthCeck() {
if( flowDepth == 1 ) {
fprintf( stderr,
"chmop: flowDepth should be greater than 1, but is 1.\n" );
exit( 1 );
}
}
dynstring getPathFromFileName( const char *name ) {
char *tmp = memXAllocAtomic( strlen( name ) + 1 );
char *p;
dynstring ds;
strcpy( tmp, name );
p = strrchr( tmp, '/' );
if( !p ) return dsNewFrom( "" );
*(p+1)=0;
ds = dsNewFrom( tmp );
memFree( tmp );
return ds;
}
dynstring getLastPathComponent( const char *path ) {
char *p;
int i = strlen( path );
if( !i || path[i-1] == '/' ) return dsNewFrom( "" );
p = strrchr( path, '/' );
return dsNewFrom( p ? (p+1) : path );
}
/* returns 0 if file not found */
int
includeFile( const char *name ) {
InputFileStack *tmpIFS;
FILE *ifi = 0;
dynstring tmpDy, path;
int strlen_name = strlen( name );
path.data = 0;
/* filenames starting with "./" are always local */
if( nrOfDefDirs && name[0] != '.' && name[1] != '/' ) {
int i;
for( i = (nrOfDefDirs-1); i>=0; i-- ) {
tmpDy = dsCopy( &defDirs[i] );
dsAppendString( &tmpDy, name, strlen_name );
if( (ifi = fopen( tmpDy.data, "r" )) != 0 ) {
i = 0;
path = getPathFromFileName( tmpDy.data );
}
}
}
if( !ifi ) { /* try to open file relativ to old file */
tmpDy = dsCopy( ¤tFilePath );
dsAppendString( &tmpDy, name, strlen_name );
if( !(ifi = fopen( tmpDy.data, "r" ))) { /* try working dir */
if( !(ifi = fopen( name, "r" ))) {
return 0;
} else {
path = dsNewFrom( "./" );
// return 1; *** warum steht das hier ?
}
} else {
path = getPathFromFileName( tmpDy.data );
}
}
if( !ifi ) { /* file not found */
// issueError(ERRCMD_NO_INCLUDE, name, 0, 0);
return 0;
}
tmpIFS = ifStack;
ifStack = (InputFileStack *)memXAlloc(sizeof(InputFileStack));
ifStack->next = tmpIFS;
ifStack->f = inFile;
ifStack->zeilenNummer = zeilenNummer;
ifStack->name = currentFileName;
ifStack->path = currentFilePath;
currentFileName = getLastPathComponent( name );
currentFilePath = path;
zeilenNummer = 0;
inFile = ifi;
activeNL = 1;
recordNewFile( bufPosAtCommandBegin, name );
if( currentFilePath.length >= 2 && !strncmp( currentFilePath.data, "./",2 ))
dsShrinkFront( ¤tFilePath, 2 );
incFiles++;
if( generateDependencies ) {
dynstring tmpDynni = dsNewFrom( currentFilePath.data );
dsAppendString( &tmpDynni, currentFileName.data,
strlen( currentFileName.data ));
dsAppendChar( &tmpDynni, ' ' );
addDependency( 0, &tmpDynni );
}
return 1;
}
void
finishFile() {
InputFileStack *tmpIFS ;
if( ifStack ) { /* not with last file */
fclose( inFile );
tmpIFS = ifStack;
inFile = ifStack->f;
zeilenNummer = ifStack->zeilenNummer;
/*
fprintf( stderr, "i wipe di aus du sau (%p).\n",
currentFilePath.data );
*/
currentFileName = ifStack->name;
currentFilePath = ifStack->path;
ifStack = tmpIFS->next;
recordNewFile( bufPosAtCommandBegin, currentFileName.data );
}
incFiles--;
}
static void
generateCommands() {
cmdHash = hash_new( 100 );
hash_insert( cmdHash, "include", cmd_include );
hash_insert( cmdHash, "define", cmd_define );
hash_insert( cmdHash, "def", cmd_define );
hash_insert( cmdHash, "ende", cmd_ende );
hash_insert( cmdHash, "edefine", cmd_edefine );
hash_insert( cmdHash, "edef", cmd_edefine );
hash_insert( cmdHash, "discard", cmd_discard );
hash_insert( cmdHash, "disc", cmd_discard );
hash_insert( cmdHash, "endd", cmd_endd );
hash_insert( cmdHash, "ifdefined", cmd_ifdef );
hash_insert( cmdHash, "ifdef", cmd_ifdef );
hash_insert( cmdHash, "ifnotdefined", cmd_ifndef );
hash_insert( cmdHash, "ifndef", cmd_ifndef );
hash_insert( cmdHash, "if", cmd_if );
hash_insert( cmdHash, "else", cmd_else );
hash_insert( cmdHash, "endif", cmd_endif );
hash_insert( cmdHash, "elseif", cmd_elseif );
hash_insert( cmdHash, "elif", cmd_elseif );
hash_insert( cmdHash, "end", cmd_end );
hash_insert( cmdHash, "!", cmd_rem );
hash_insert( cmdHash, "rem", cmd_rem );
hash_insert( cmdHash, "error", cmd_error );
}
void
initCommands( FILE *inputFile, char commandChar, const char *filename, const char *filepath) {
inFile = inputFile;
incFiles = 1;
filler_cmdChar = commandChar;
activeNL = 1;
tmpStr = dsNew();
ifStack = 0;
bufOverflow = 0;
generateCommands();
/* init flowStack */
flowStack = (FlowStack *)memXAlloc( sizeof( FlowStack ));
flowStack->next = 0;
flowStack->flowLevel = 1;
flowStack->openCmd = 0;
currentFileName = dsNewFrom(filename);
currentFilePath = dsNewFrom(filepath);
}
/* processActiveNL
* Starts to copy a line to tmp buffer till
* 1) it knows that line is no command -> tmp buf is copied to output buf
* and processText is called
* 2) it knows that line is a command .. command is execd
*/
int
processActiveNL( char *buf, int max, int akt ) {
int bufp;
char ch;
zeilenNummer++;
recordNewLine( akt, zeilenNummer );
if( !tmpStr.data ) tmpStr = dsNew();
/* MERD( "processActiveNL" ); */
bufp = akt;
ch = (char)fgetc( inFile );
/*fprintf( stderr, "-%c-", ch ); */
/* fprintf( stderr, "<<processActiveNL - %i - %i - ch = '%c'>>\n", max, akt, ch ); */
/*fprintf( stderr, "-%c-", ch ); */
while( (ch == ' ' || ch == '\t') && !feof( inFile )) {
/* fprintf( stderr, "kurding '%c'\n", ch ); */
dsAppendChar( &tmpStr, ch );
ch = (char)fgetc( inFile );
/* fprintf( stderr, "-%c-", ch ); */
}
if( feof( inFile )) {
ch = 0;
}
if( ch ) {
if( flowStack->flowLevel ) { /* HACK, but i weiss net warum der char */
/* durchrutscht wenn i des net mach .... */
dsAppendChar( &tmpStr, ch );
}/* dont miss the ch who killed command */
/* fprintf( stderr, "kurding II '%c'\n", ch ); */
}
if( ch == filler_cmdChar ) { /* oh yes, it is a command */
nextCmdCommand = dsNew();
nextCmdArgument = dsNew();
do { /* read rest of line */
ch = (char)fgetc( inFile );
dsAppendChar( &nextCmdCommand, ch );
} while( ch != '\n' && !feof( inFile));
dsShrinkRear( &nextCmdCommand, 1 );
dsRemoveOuterWS( &nextCmdCommand );
nextCmdArgument = dsSplitWS( &nextCmdCommand );
dsRemoveOuterWS( &nextCmdArgument );
/* fprintf( stderr, "[COMMAND FOUND: '%s' '%s']\n", */
/* nextCmdCommand.data, nextCmdArgument.data ); */
{
nextCmdFun = hash_lookup( cmdHash, nextCmdCommand.data );
if( !nextCmdFun ) {
issueWarning(WARNCMD_UNKNOWN_COMMAND, nextCmdCommand.data, 0, 0);
} else {
/* ((void(*)(const char*))fun)( nextCmdArgument.data ); */
return bufp; /* return now, next time go to processCommand */
}
}
tmpStr = dsNew();
/* aktiveNL stays 1 cause command may follow in next line */
} else { /* no command, pass tmpStr to schani */
/*MERD( "reingfalln, is doch kein command" ); */
if( flowsExtern ) { /* in externen buffa eine */
goExternDS( &tmpStr );
/* fprintf( stderr, "activeNL: '%s'\n", tmpStr.data ); */
if( ch != '\n' ) {
activeNL = 0;
} else {
tmpStr = dsNew();
}
} else {
if( !flowStack->flowLevel || tmpStr.length <= (max-akt-1)) {
/* super, buffer gross genug, oder eh kein output */
if( flowStack->flowLevel ) {
memcpy( &buf[bufp], tmpStr.data, tmpStr.length );
bufp += tmpStr.length;
}
if( ch != '\n' ) {
activeNL = 0;
} else {
tmpStr = dsNew();
}
} else { /* mega scheisse, buffer to short */
/* fprintf( stderr, "*#*#*#*#*#*#*#*#*quake\n" ); */
memcpy( &buf[bufp], tmpStr.data, max - akt - 1 ); /* fully fill buffer */
bufp += max - akt - 1;
/* store rest of tmpStr in overflowStr */
overflowStr = dsCopy( &tmpStr );
dsShrinkFront( &overflowStr, max - akt - 1 );
bufOverflow = 1;
}
}
}
if( feof( inFile )) { /* Change files */
/* go down 1 include level */
bufPosAtCommandBegin = bufp; /* hack: end of file is no command */
finishFile();
activeNL = 1;
return bufp;
}
return bufp;
}
/* BUG: may loose a character if buffer is full */
/* processText
* reads chars, and wipes them into buffer
* if newline is encounterd, processActiveNL is called
*/
int
processText( char *buf, int max, int akt ) {
int bufp;
char ch;
/* fprintf( stderr, "<<processText - %i - %i ->>\n", max, akt ); */
bufp = akt;
ch = (char)fgetc( inFile );
/* fprintf( stderr, "-%c-", ch ); */
while( ch != '\n' && bufp < (max - 1) && !feof( inFile )) {
/* add chars */
if( flowStack->flowLevel ) { /* if flowLevel write to buf */
if( flowsExtern ) {
goExtern( ch );
/* fprintf( stderr, "processText: '%c'\n", ch ); */
} else {
buf[bufp++] = ch;
}
}
ch = (char)fgetc( inFile );
/* fprintf( stderr, "-%c-", ch ); */
}
if( feof( inFile )) {
finishFile();
return bufp;
} else {
if( ch == '\n' && bufp < max) { /* add newline if possible */
if( flowStack->flowLevel ) {
if( flowsExtern ) {
goExtern( ch );
/* fprintf( stderr, "processText: '%c'\n", ch ); */
} else {
buf[bufp++] = ch;
}
}
}
if( bufp == (max - 1)) {
overflowStr = dsNew();
if( ch != '\n' ) dsAppendChar( &overflowStr, ch );
/* fprintf( stderr, "+> %i\n", unRecordLineNumber( bufp )); */
bufOverflow = 1;
}
}
if( ch == '\n' ) {
activeNL = 1;
tmpStr = dsNew();
return bufp;
}
return bufp;
}
/* processOverflow
* 1) handles the last char that did not fit into the output buffer
* 2) splits the buffer created by processActiveNL into output buffer
*/
int
processOverflow( char *buf, int max, int akt) {
int bufp = akt;
char ch;
/* fprintf( stderr, "PROCESS OVERFLOW\n" );*/
if( overflowStr.length <= (max-akt-1)) { /* super, buffer gross genug */
memcpy( &buf[bufp], overflowStr.data, overflowStr.length );
bufp += overflowStr.length;
ch = buf[bufp-1]; /* needed to check if last char was newline */
if( ch != '\n' ) {
activeNL = 0;
} else {
activeNL = 1;
overflowStr = dsNew();
}
bufOverflow = 0;
} else { /* mega scheisse, buffer to short */
memcpy( &buf[bufp], overflowStr.data, max - akt - 1 ); /* fully fill buffer */
/* store rest of tmpStr in overflowStr */
dsShrinkFront( &overflowStr, max - akt - 1 );
/* lets do it again */
}
return bufp;
}
int
processCommand( char *buf, int max, int akt ) {
/* Hack to access the bufPos inside of commands (used in include) */
bufPosAtCommandBegin = akt;
if( nextCmdArgument.data[0] ) {
((void(*)(const char*))nextCmdFun)( nextCmdArgument.data );
} else {
((void(*)(const char*))nextCmdFun)( 0 );
}
tmpStr = dsNew();
nextCmdFun = 0;
/* fprintf( stderr, "processCommand: tmpStr = '%s'\n", tmpStr.data ); */
activeNL = 1;
return akt;
}
int /* returnd anzahl or -1 if all done */
fillBuffer( char *buf, int max ) { /* max is anzahl */
/* scan 4 cmd */
int bufp = 0;
recordChangeTape();
recordNewFile( 0, currentFileName.data );
if( !incFiles ) return -1;
if( nextCmdFun ) bufp = processCommand( buf, max, bufp );
do {
if( bufOverflow ) bufp = processOverflow( buf, max, bufp );
if( activeNL ) { /* tmpStr muss allociert sein */
bufp = processActiveNL( buf, max, bufp );
if( nextCmdFun ) return bufp; /* aha, command wants flush */
} else {
bufp = processText( buf, max, bufp );
}
} while( bufp < (max - 1) && incFiles );
return bufp;
}
/*
int
fillBuffer( char *buf, int max ) {
int xxx;
xxx= fillBuffer2( buf, max );
fprintf( stderr, "-> %i (bufoverflow=%i activeNL=%i)\n",
unRecordLineNumber( xxx ), bufOverflow, activeNL );
return xxx;
}
*/
|