00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 #include <stdio.h>
00075 #include <stdlib.h>
00076 #include <string.h>
00077 #include <ctype.h>
00078
00079 #include "filesys.h"
00080 #include "report.h"
00081 #include "errcodes.h"
00082 #include "conftree.h"
00083 #include "reader.h"
00084 #include "dynlolib.h"
00085 #include "scriba.h"
00086 #include "ipreproc.h"
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 #if (!defined(_WIN32) && !defined(__MACOS__))
00104 int strnicmp(char *a, char *b, int n){
00105 char ca,cb;
00106
00107 while( n-- ){
00108 ca = *a++;
00109 cb = *b++;
00110 ca = isupper(ca) ? tolower(ca) : ca;
00111 cb = isupper(cb) ? tolower(cb) : cb;
00112 if( ca == (char)0 && cb == (char)0 )return 0;
00113 if( ca != cb )return ca-cb;
00114 }
00115 return 0;
00116 }
00117 int stricmp(char *a, char *b){
00118 char ca,cb;
00119
00120 while( 1 ){
00121 ca = *a++;
00122 cb = *b++;
00123 ca = isupper(ca) ? tolower(ca) : ca;
00124 cb = isupper(cb) ? tolower(cb) : cb;
00125 if( ca == (char)0 && cb == (char)0 )return 0;
00126 if( ca != cb )return ca-cb;
00127 }
00128 }
00129 #endif
00130
00131 #define REPORT(x1,x2,x3,x4) if( pRo->report )pRo->report(pRo->reportptr,x1,x2,x3,REPORT_ERROR,&(pRo->iErrorCounter),x4,&(pRo->fErrorFlags));
00132
00133 static int reader_AllocateInitialBuffer(pReadObject pRo){
00134 pRo->dwBuffer = BUFFER_INITIAL_SIZE;
00135 pRo->Buffer = (char *)pRo->memory_allocating_function(pRo->dwBuffer,pRo->pMemorySegment);
00136 if( pRo->Buffer )return READER_ERROR_SUCCESS; else return READER_ERROR_MEMORY_LOW;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 int reader_IncreaseBuffer(pReadObject pRo
00155 ){
00156
00157
00158 char *s,*r,*q;
00159
00160 if( ! pRo->Buffer )return reader_AllocateInitialBuffer(pRo);
00161 r = s = pRo->Buffer;
00162 pRo->dwBuffer += BUFFER_INCREMENT;
00163 pRo->Buffer = (char *)pRo->memory_allocating_function(pRo->dwBuffer,pRo->pMemorySegment);
00164 if( ! pRo->Buffer ){
00165 pRo->Buffer = s;
00166 return READER_ERROR_MEMORY_LOW;
00167 }
00168 for( q = pRo->Buffer ; *q=*s ; s++ , q++ );
00169 pRo->memory_releasing_function(r,pRo->pMemorySegment);
00170 return READER_ERROR_SUCCESS;
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 int reader_gets(pReadObject pRo,
00188 void *fp
00189 ){
00190
00191
00192 int i,ch;
00193
00194 ch = pRo->fpGetCharacter(fp,pRo->pFileHandleClass);
00195 if( ch == EOF )return EOF;
00196 for( i=0 ; ch != '\n' && ch != EOF ; i++ ){
00197 if( i >= pRo->dwBuffer )reader_IncreaseBuffer(pRo);
00198 pRo->Buffer[i] = ch;
00199 ch = pRo->fpGetCharacter(fp,pRo->pFileHandleClass);
00200 }
00201 if( i >= pRo->dwBuffer )reader_IncreaseBuffer(pRo);
00202 if( pRo->fForceFinalNL || ch == '\n' )
00203 pRo->Buffer[i++] = '\n';
00204 if( i >= pRo->dwBuffer )reader_IncreaseBuffer(pRo);
00205 pRo->Buffer[i++] = (char)0;
00206 pRo->cBuffer = i;
00207 return !EOF;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217 int reader_ReadLines(pReadObject pRo,
00218 char *szFileName
00219 ){
00220
00221
00222
00223 int iResult;
00224 pSourceLine p;
00225
00226 iResult = 0;
00227 if( pRo->pPREP )iResult = ipreproc_Process(pRo->pPREP,PreprocessorReadStart,pRo);
00228 if( iResult )return iResult;
00229
00230 pRo->FirstUNIXline = NULL;
00231 iResult = reader_ReadLines_r(pRo,szFileName,&(pRo->Result));
00232 if( iResult )return iResult;
00233 if( pRo->Result == NULL )return READER_ERROR_EMPTY_INPUT;
00234
00235 if( pRo->pPREP )iResult = ipreproc_Process(pRo->pPREP,PreprocessorReadDone0,pRo);
00236 if( iResult )return iResult;
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 if( strncmp(pRo->Result->line,"#!",2) == 0 ||
00277 strncmp(pRo->Result->line,"@goto",5) == 0 ){
00278 pRo->Result = (p=pRo->Result)->next;
00279 pRo->FirstUNIXline = p->line;
00280 pRo->memory_releasing_function(p,pRo->pMemorySegment);
00281 }else{
00282 pRo->FirstUNIXline = NULL;
00283 }
00284
00285 if( pRo->pPREP )iResult = ipreproc_Process(pRo->pPREP,PreprocessorReadDone1,pRo);
00286 if( iResult )return iResult;
00287
00288
00289 reader_ProcessIncludeFiles(pRo,&(pRo->Result));
00290
00291 iResult = ipreproc_Process(pRo->pPREP,PreprocessorReadDone2,pRo);
00292 if( iResult )return iResult;
00293
00294
00295 reader_LoadPreprocessors(pRo,&(pRo->Result));
00296
00297 if( pRo->pPREP )iResult = ipreproc_Process(pRo->pPREP,PreprocessorReadDone3,pRo);
00298 return iResult;
00299 }
00300
00301
00302
00303
00304
00305
00306 int reader_ReadLines_r(pReadObject pRo,
00307 char *szFileName,
00308 pSourceLine *pLine
00309 ){
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 void *fp;
00327 pSourceLine pL;
00328 long nLine;
00329
00330 if( szFileName == NULL ){
00331 pRo->iErrorCounter++;
00332 return READER_ERROR_FILE_OPEN;
00333 }
00334 fp = pRo->fpOpenFile(szFileName,pRo->pFileHandleClass);
00335 if( fp == NULL ){
00336 REPORT(szFileName ,0 ,READER_ERROR_FILE_OPEN,NULL);
00337 return READER_ERROR_FILE_OPEN;
00338 }
00339 nLine = 1;
00340 while( reader_gets(pRo,fp) != EOF ){
00341 pL = (pSourceLine)pRo->memory_allocating_function(sizeof(SourceLine),pRo->pMemorySegment);
00342 if( pL == NULL )return READER_ERROR_MEMORY_LOW;
00343 pL->line = (char *)pRo->memory_allocating_function(pRo->cBuffer,pRo->pMemorySegment);
00344 if( pL->line == NULL ){
00345 pRo->memory_releasing_function(pL,pRo->pMemorySegment);
00346 return READER_ERROR_MEMORY_LOW;
00347 }
00348 pL->szFileName = szFileName;
00349 pL->lLineNumber = nLine++;
00350 pL->LineLength = pRo->cBuffer;
00351
00352 strcpy(pL->line,pRo->Buffer);
00353
00354 pL->next = (*pLine);
00355 *pLine = pL;
00356 pLine = &(pL->next);
00357 }
00358 pRo->fpCloseFile(fp,pRo->pFileHandleClass);
00359 return READER_ERROR_SUCCESS;
00360 }
00361
00362 extern int GlobalDebugDisplayFlag;
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 void reader_ProcessIncludeFiles(pReadObject pRo,
00395 pSourceLine *pLine
00396 ){
00397
00398
00399
00400
00401
00402 #define FNLEN 1024
00403 pSourceLine p;
00404 char *s,*file_name;
00405 CFT_NODE Node;
00406 char szBuffer[FNLEN];
00407 void *fp;
00408 int isImport;
00409 pImportedFileList pIFL;
00410 long IncludeCounter;
00411
00412 IncludeCounter = 1000;
00413 cft_GetEx(pRo->pConfig,"maxinclude",&Node,&s,&IncludeCounter,NULL,NULL);
00414 p = *pLine;
00415 while( p ){
00416 s = p->line;
00417 while( isspace(*s) )s++;
00418 if( (((!strnicmp(s,"include",7)) && (s+=7) && !(isImport=0) ) ||
00419 ((!strnicmp(s,"import" ,6)) && (s+=6) && (isImport=1) )) &&
00420
00421 isspace(*s) ){
00422 if( --IncludeCounter == 0 ){
00423 REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_TOOMANY_INCLUDE,NULL);
00424 return;
00425 }
00426 while( isspace(*s) )s++;
00427 if( *s == '"' ){
00428
00429 s++;
00430 file_name = s;
00431 while( *s && *s != '"' )s++;
00432 if( *s != '"' ){
00433 REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_INCLUDE_SYNTAX,NULL);
00434 p = p->next;
00435 continue;
00436 }
00437 *s = (char)0;
00438 s++;
00439 while( isspace(*s) )s++;
00440 if( *s && *s != '\n' ){
00441 REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_INCLUDE_SYNTAX,NULL);
00442 p = p->next;
00443 continue;
00444 }
00445 file_name = reader_RelateFile(pRo,p->szFileName,file_name);
00446
00447
00448 }else{
00449
00450 file_name = s;
00451 while( *s && ! isspace(*s) )s++;
00452 if( *s ){
00453 *s = (char)0;
00454 s++;
00455 }else *s = (char)0;
00456 while( isspace(*s) )s++;
00457 if( *s && *s != '\n' ){
00458 REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_INCLUDE_SYNTAX,NULL);
00459 p = p->next;
00460 continue;
00461 }
00462
00463 if( GlobalDebugDisplayFlag ){
00464 fprintf(stderr,"Searching installed module header file '%s' ...\n",file_name);
00465 }
00466
00467
00468
00469 fp = NULL;
00470 for( cft_GetEx(pRo->pConfig,"include",&Node,&s,NULL,NULL,NULL);
00471 ! cft_GetEx(pRo->pConfig,NULL,&Node,&s,NULL,NULL,NULL) ;
00472 Node = cft_EnumNext(pRo->pConfig,Node) ){
00473 if( ! strcmp(cft_GetKey(pRo->pConfig,Node),"include") ){
00474 if( s && strlen(s) > FNLEN )REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_INCLUDE_SYNTAX,NULL);
00475 if( s )strcpy(szBuffer,s); else *szBuffer = (char)0;
00476 strcat(szBuffer,file_name);
00477 fp = pRo->fpOpenFile(szBuffer,pRo->pFileHandleClass);
00478 if( GlobalDebugDisplayFlag ){
00479 fprintf(stderr,"Checking installed module header file location '%s' Result=%s\n",szBuffer, fp ? "OK" : "FAILED" );
00480 }
00481 if( fp != NULL )break;
00482 }
00483 }
00484
00485 if( fp == NULL ){
00486 REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_INCLUDE_FILE,NULL);
00487 goto NotInclude;
00488 }
00489 pRo->fpCloseFile(fp,pRo->pFileHandleClass);
00490 file_name = pRo->memory_allocating_function(strlen(szBuffer)+1,pRo->pMemorySegment);
00491 if( file_name == NULL )REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_MEMORY_LOW,NULL);
00492 strcpy(file_name,szBuffer);
00493 }
00494
00495 if( isImport ){
00496
00497 pIFL = pRo->pImportList;
00498 while( pIFL ){
00499 if( ! strcmp(file_name,pIFL->pszFileName) ){
00500 *pLine = (*pLine)->next;
00501 p = *pLine;
00502 goto NextP;
00503 }
00504 pIFL = pIFL->next;
00505 }
00506 }
00507
00508
00509
00510 pIFL = pRo->memory_allocating_function(sizeof(ImportedFileList),pRo->pMemorySegment);
00511 if( pIFL == NULL )REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_MEMORY_LOW,NULL);
00512 pIFL->next = pRo->pImportList;
00513 pIFL->pszFileName = file_name;
00514 pRo->pImportList = pIFL;
00515 *pLine = (*pLine)->next;
00516 if( GlobalDebugDisplayFlag ){
00517 fprintf(stderr,"Including file '%s'\n",file_name);
00518 }
00519 reader_ReadLines_r(pRo,file_name,pLine);
00520
00521 pRo->memory_releasing_function(p->line,pRo->pMemorySegment);
00522 pRo->memory_releasing_function(p,pRo->pMemorySegment);
00523
00524
00525
00526 p = *pLine;
00527 }else
00528 NotInclude:
00529 if( p ){
00530 pLine = &(p->next);
00531 p = *pLine;
00532 }
00533 NextP:;
00534 }
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 void reader_LoadPreprocessors(pReadObject pRo,
00553 pSourceLine *pLine
00554 ){
00555
00556
00557
00558 #define FNLEN 1024
00559 pSourceLine p,*prev;
00560 char *s;
00561 int iError;
00562 char szBuffer[FNLEN];
00563
00564 if( pRo->pPREP == NULL ){
00565
00566
00567
00568 p = *pLine;
00569 prev = pLine;
00570 while( p ){
00571 s = p->line;
00572 while( isspace(*s) )s++;
00573 if( !strnicmp(s,"use",3) )
00574 (*prev) = p->next;
00575 prev = &(p->next);
00576 p = p->next;
00577 }
00578 return;
00579 }
00580
00581 p = *pLine;
00582 prev = pLine;
00583 while( p ){
00584 s = p->line;
00585 while( isspace(*s) )s++;
00586 if( !strnicmp(s,"use",3) ){
00587 s += 3;
00588 if( ! isspace(*s) ){
00589 prev = &(p->next);
00590 p = p->next;
00591 continue;
00592 }
00593 while( isspace(*s) )s++;
00594 if( strlen(s) > FNLEN ){
00595 REPORT(p->szFileName ,p->lLineNumber ,READER_ERROR_PREPROC_LONG ,s);
00596 continue;
00597 }
00598 strcpy(szBuffer,s);
00599 s = szBuffer;
00600 while( *s && ! isspace(*s) )s++;
00601 *s = (char)0;
00602 if( pRo->pPREP && (iError = ipreproc_LoadInternalPreprocessor(pRo->pPREP,szBuffer)) ){
00603 REPORT(p->szFileName ,p->lLineNumber ,iError ,szBuffer);
00604 }
00605 (*prev) = p->next;
00606 }else
00607 prev = &(p->next);
00608 p = p->next;
00609 }
00610 }
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620 void reader_StartIteration(pReadObject pRo
00621 ){
00622
00623
00624 pRo->CurrentLine = pRo->Result;
00625 pRo->NextCharacterPosition = 0;
00626 }
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 char *reader_NextLine(pReadObject pRo
00641 ){
00642
00643
00644 long ThisCharacter;
00645
00646 if( ! pRo->CurrentLine )return NULL;
00647 if( pRo->CurrentLine->line[pRo->NextCharacterPosition] == (char)0 ){
00648 pRo->CurrentLine = pRo->CurrentLine->next;
00649 pRo->NextCharacterPosition = 0;
00650 }
00651 if( ! pRo->CurrentLine )return NULL;
00652 ThisCharacter = pRo->NextCharacterPosition;
00653 pRo->NextCharacterPosition = pRo->CurrentLine->LineLength;
00654 return pRo->CurrentLine->line+ThisCharacter;
00655 }
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668 int reader_NextCharacter(void *p
00669 ){
00670
00671
00672 pReadObject pRo = (pReadObject)p;
00673 if( ! pRo->CurrentLine )return EOF;
00674 if( pRo->CurrentLine->line[pRo->NextCharacterPosition] == (char)0 ){
00675 pRo->CurrentLine = pRo->CurrentLine->next;
00676 pRo->NextCharacterPosition = 0;
00677 }
00678 if( ! pRo->CurrentLine )return EOF;
00679 return (int)pRo->CurrentLine->line[pRo->NextCharacterPosition++];
00680 }
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690 char *reader_FileName(void *p
00691 ){
00692
00693
00694 pReadObject pRo = (pReadObject)p;
00695 if( ! pRo || ! pRo->CurrentLine )return "No-File";
00696 return pRo->CurrentLine->szFileName;
00697 }
00698
00699
00700
00701
00702
00703
00704
00705
00706 long reader_LineNumber(void *p
00707 ){
00708
00709
00710 pReadObject pRo = (pReadObject)p;
00711 if( !pRo || ! pRo->CurrentLine )return 0;
00712 return pRo->CurrentLine->lLineNumber;
00713 }
00714
00715
00716 static void *reader_malloc(size_t n, void *pMemorySegment){
00717 return malloc(n);
00718 }
00719 static void reader_free(void *p, void *pMemorySegment){
00720 free(p);
00721 }
00722
00723
00724 static void *_MyOpenFile(char *FileName, void *p){
00725 return (void *)file_fopen(FileName,"r");
00726 }
00727 static int _MyGetCharacter(void *fp, void *p){
00728 return file_fgetc( (FILE *)fp);
00729 }
00730 static void _MyCloseFile(void *fp, void *p){
00731 file_fclose((FILE *)fp);
00732 }
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742 void reader_InitStructure(pReadObject pRo
00743 ){
00744
00745
00746
00747 pRo->fpOpenFile = _MyOpenFile;
00748 pRo->fpGetCharacter = _MyGetCharacter;
00749 pRo->fpCloseFile = _MyCloseFile;
00750 pRo->pFileHandleClass = NULL;
00751
00752 pRo->memory_allocating_function = reader_malloc;
00753 pRo->memory_releasing_function = reader_free;
00754 pRo->pMemorySegment = NULL;
00755
00756 pRo->pImportList = NULL;
00757
00758
00759 pRo->Buffer = NULL;
00760 pRo->dwBuffer = 0;
00761
00762 pRo->Result = NULL;
00763 pRo->fForceFinalNL = 1;
00764
00765 pRo->pPREP = NULL;
00766
00767 }
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783 char *reader_RelateFile(pReadObject pRo,
00784 char *pszBaseFile,
00785 char *pszRelativeFile
00786 ){
00787
00788
00789 long lLen;
00790 char *pszBuffer,*s,*r,*q;
00791
00792 #ifdef __MACOS__
00793 #define ischardsep(s) ((s) == ':')
00794
00795 if( !ischardsep(*pszRelativeFile)
00796 #else
00797
00798
00799
00800
00801 #define ischardsep(s) ((s) == '/' || (s) == '\\')
00802
00803 if( ischardsep(*pszRelativeFile)
00804 #ifdef WIN32
00805
00806
00807 || pszRelativeFile[1] == ':'
00808 #endif
00809 #endif
00810 ){
00811
00812 lLen = strlen(pszRelativeFile)+1;
00813 pszBuffer = pRo->memory_allocating_function(lLen,pRo->pMemorySegment);
00814 if( pszBuffer == NULL ){
00815 REPORT("" ,0 ,READER_ERROR_MEMORY_LOW,NULL);
00816 return NULL;
00817 }
00818 strcpy(pszBuffer,pszRelativeFile);
00819 #ifndef __MACOS__
00820
00821
00822 s = pszBuffer;
00823 while( *s ){
00824 if( *s == '\\' )*s = '/';
00825 s++;
00826 }
00827 #endif
00828 return pszBuffer;
00829 }
00830
00831 lLen = strlen(pszBaseFile) + strlen(pszRelativeFile) +1;
00832 pszBuffer = pRo->memory_allocating_function(lLen,pRo->pMemorySegment);
00833 if( pszBuffer == NULL ){
00834 REPORT("" ,0 ,READER_ERROR_MEMORY_LOW,NULL);
00835 return NULL;
00836 }
00837 strcpy(pszBuffer,pszBaseFile);
00838 r = s = pszBuffer;
00839
00840 while( *s ){
00841 if( ischardsep(*s) )r = s;
00842 s++;
00843 }
00844 if( ischardsep(*r) )r++;
00845
00846 strcpy(r,pszRelativeFile);
00847 s = pszBuffer;
00848 while( *s ){
00849 r = s+1;
00850 while( *r && ! ischardsep(*r) )r++;
00851 if( (ischardsep(*r) && r[1] == '.' && r[2] == '.' && ischardsep(r[3])) &&
00852
00853 !( s[0] == '.' && s[1] == '.' && ischardsep(s[2]) ) ){
00854
00855 q = s;
00856 r += 4;
00857 while( *q++ = *r++ );
00858 }else
00859 s = r+1;
00860 }
00861 #ifndef __MACOS__
00862
00863
00864 s = pszBuffer;
00865 while( *s ){
00866 if( *s == '\\' )*s = '/';
00867 s++;
00868 }
00869 #endif
00870 return pszBuffer;
00871 }
00872
00873
00874
00875
00876
00877
00878 void reader_DumpLines(pReadObject pRo,
00879 FILE *fp
00880 ){
00881
00882
00883
00884 pSourceLine p;
00885 p = pRo->Result;
00886 while( p ){
00887 fprintf(fp,"%s",p->line);
00888 p = p->next;
00889 }
00890 }