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
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 #include <stdio.h>
00108 #include <stdlib.h>
00109 #include <string.h>
00110 #include <stdarg.h>
00111 #include <stddef.h>
00112
00113 #include "scriba.h"
00114 #include "basext.h"
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 pSbProgram scriba_new(void * (*maf)(size_t),
00128 void (*mrf)(void *)
00129 ){
00130
00131
00132 pSbProgram pProgram;
00133 void *p;
00134
00135 p = alloc_InitSegment(maf,mrf);
00136 if( p == NULL )return NULL;
00137
00138 pProgram = (pSbProgram)alloc_Alloc(sizeof(SbProgram),p);
00139 if( pProgram == NULL ){
00140 alloc_FinishSegment(p);
00141 return NULL;
00142 }
00143
00144 pProgram->maf = maf;
00145 pProgram->mrf = mrf;
00146 pProgram->pMEM = p;
00147 pProgram->fErrorFlags = 0;
00148 pProgram->pszFileName = NULL;
00149 pProgram->pszCacheFileName = NULL;
00150 pProgram->FirstUNIXline = NULL;
00151 pProgram->fpStdouFunction = NULL;
00152 pProgram->fpStdinFunction = NULL;
00153 pProgram->fpEnvirFunction = NULL;
00154 pProgram->pEmbedder = NULL;
00155 pProgram->fpReportFunction = report_report;
00156 pProgram->pReportPointer = (void *)stderr;
00157 pProgram->pSTI = NULL;
00158 pProgram->pEPo = NULL;
00159
00160 pProgram->pCONF = NULL;
00161 pProgram->pREAD = NULL;
00162 pProgram->pLEX = NULL;
00163 pProgram->pEX = NULL;
00164 pProgram->pBUILD = NULL;
00165 pProgram->pEXE = NULL;
00166 pProgram->pPREP = NULL;
00167
00168 return pProgram;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 void scriba_destroy(pSbProgram pProgram
00180 ){
00181
00182
00183
00184
00185 scriba_PurgeReaderMemory(pProgram);
00186 scriba_PurgeLexerMemory(pProgram);
00187 scriba_PurgeSyntaxerMemory(pProgram);
00188 scriba_PurgeBuilderMemory(pProgram);
00189 scriba_PurgeExecuteMemory(pProgram);
00190 scriba_PurgePreprocessorMemory(pProgram);
00191
00192
00193
00194
00195 alloc_FinishSegment(pProgram->pMEM);
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 pSbData scriba_NewSbData(pSbProgram pProgram
00215 ){
00216
00217
00218 pSbData p;
00219 p = alloc_Alloc(sizeof(SbData),pProgram->pMEM);
00220 if( p )scriba_InitSbData(pProgram,p);
00221 return p;
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 void scriba_InitSbData(pSbProgram pProgram,
00236 pSbData p
00237 ){
00238
00239
00240 p->v.s = NULL;
00241 p->type = SBT_UNDEF;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 void scriba_UndefSbData(pSbProgram pProgram,
00262 pSbData p
00263 ){
00264
00265
00266 if( p->type == SBT_STRING && p->v.s )alloc_Free(p->v.s,pProgram->pMEM);
00267 p->v.s = NULL;
00268 p->type = SBT_UNDEF;
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 pSbData scriba_NewSbLong(pSbProgram pProgram,
00283 long lInitValue
00284 ){
00285
00286
00287
00288
00289 pSbData p;
00290
00291 p = scriba_NewSbData(pProgram);
00292 if( p == NULL )return NULL;
00293 p->type = SBT_LONG;
00294 p->v.l = lInitValue;
00295 return p;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 pSbData scriba_NewSbDouble(pSbProgram pProgram,
00309 double dInitValue
00310 ){
00311
00312
00313
00314
00315 pSbData p;
00316
00317 p = scriba_NewSbData(pProgram);
00318 if( p == NULL )return NULL;
00319 p->type = SBT_DOUBLE;
00320 p->v.d = dInitValue;
00321 return p;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 pSbData scriba_NewSbUndef(pSbProgram pProgram
00334 ){
00335
00336
00337
00338
00339 pSbData p;
00340
00341 p = scriba_NewSbData(pProgram);
00342 if( p == NULL )return NULL;
00343 p->type = SBT_UNDEF;
00344 return p;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 pSbData scriba_NewSbString(pSbProgram pProgram,
00359 char *pszInitValue
00360 ){
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 pSbData p;
00382
00383 if( pszInitValue == NULL )return scriba_NewSbUndef(pProgram);
00384
00385 p = scriba_NewSbData(pProgram);
00386 if( p == NULL )return NULL;
00387 p->type = SBT_STRING;
00388 p->size = strlen(pszInitValue);
00389 if( p->size ){
00390 p->v.s = alloc_Alloc(p->size+1,pProgram->pMEM);
00391 if( p->v.s == NULL ){
00392 alloc_Free(p,pProgram->pMEM);
00393 return NULL;
00394 }
00395 memcpy(p->v.s,pszInitValue,p->size+1);
00396 }else{
00397 p->v.s = NULL;
00398 }
00399 return p;
00400 }
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412 pSbData scriba_NewSbBytes(pSbProgram pProgram,
00413 unsigned long len,
00414 unsigned char *pszInitValue
00415 ){
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 pSbData p;
00429
00430 if( pszInitValue == NULL )return scriba_NewSbUndef(pProgram);
00431
00432 p = scriba_NewSbData(pProgram);
00433 if( p == NULL )return NULL;
00434 p->type = SBT_STRING;
00435 p->size = len;
00436 if( p->size ){
00437 p->v.s = alloc_Alloc(p->size+1,pProgram->pMEM);
00438 if( p->v.s == NULL ){
00439 alloc_Free(p,pProgram->pMEM);
00440 return NULL;
00441 }
00442 memcpy(p->v.s,pszInitValue,p->size);
00443
00444 p->v.s[p->size] = (char)0;
00445 }else{
00446 p->v.s = NULL;
00447 }
00448 return p;
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 void scriba_DestroySbData(pSbProgram pProgram,
00461 pSbData p
00462 ){
00463
00464
00465
00466
00467 if( p->type == SBT_STRING )
00468 alloc_Free(p->v.s,pProgram->pMEM);
00469 alloc_Free(p,pProgram->pMEM);
00470 }
00471
00472
00473
00474
00475
00476
00477
00478
00479 void scriba_PurgeReaderMemory(pSbProgram pProgram
00480 ){
00481
00482
00483 if( pProgram->pREAD ){
00484 alloc_FinishSegment(pProgram->pREAD->pMemorySegment);
00485 alloc_Free(pProgram->pREAD,pProgram->pMEM);
00486 }
00487 pProgram->pREAD = NULL;
00488 }
00489
00490
00491
00492
00493
00494 void scriba_PurgeLexerMemory(pSbProgram pProgram
00495 ){
00496
00497
00498 if( pProgram->pLEX )
00499 alloc_FinishSegment(pProgram->pLEX->pMemorySegment);
00500 alloc_Free(pProgram->pLEX,pProgram->pMEM);
00501 pProgram->pLEX = NULL;
00502 }
00503
00504
00505
00506
00507
00508 void scriba_PurgeSyntaxerMemory(pSbProgram pProgram
00509 ){
00510
00511
00512 if( pProgram->pEX )
00513 ex_free( pProgram->pEX );
00514 alloc_Free(pProgram->pEX,pProgram->pMEM);
00515 pProgram->pEX = NULL;
00516 }
00517
00518
00519
00520
00521
00522 void scriba_PurgeBuilderMemory(pSbProgram pProgram
00523 ){
00524
00525
00526 if( pProgram->pBUILD && pProgram->pBUILD->pMemorySegment)
00527 alloc_FinishSegment(pProgram->pBUILD->pMemorySegment);
00528 alloc_Free(pProgram->pBUILD,pProgram->pMEM);
00529 pProgram->pBUILD = NULL;
00530 }
00531
00532
00533
00534
00535
00536
00537
00538
00539 void scriba_PurgePreprocessorMemory(pSbProgram pProgram
00540 ){
00541
00542
00543
00544 if( pProgram->pPREP ){
00545 ipreproc_PurgePreprocessorMemory(pProgram->pPREP);
00546 alloc_Free(pProgram->pPREP,pProgram->pMEM);
00547 pProgram->pPREP = NULL;
00548 }
00549 }
00550
00551
00552
00553
00554
00555
00556
00557
00558 void scriba_PurgeExecuteMemory(pSbProgram pProgram
00559 ){
00560
00561
00562 int iErrorCode;
00563
00564 if( pProgram->pEXE ){
00565 execute_FinishExecute(pProgram->pEXE,&iErrorCode);
00566 if( pProgram->pEXE->pMo &&
00567 pProgram->pEXE->pMo->pMemorySegment )alloc_FinishSegment(pProgram->pEXE->pMo->pMemorySegment);
00568 alloc_FinishSegment(pProgram->pEXE->pMemorySegment);
00569 }
00570 alloc_Free(pProgram->pEXE,pProgram->pMEM);
00571 pProgram->pEXE = NULL;
00572 }
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583 int scriba_SetFileName(pSbProgram pProgram,
00584 char *pszFileName
00585 ){
00586
00587
00588
00589
00590 if( pProgram->pszFileName )alloc_Free(pProgram->pszFileName,pProgram->pMEM);
00591 pProgram->pszFileName = NULL;
00592 if( pszFileName ){
00593 pProgram->pszFileName = alloc_Alloc(strlen(pszFileName)+1,pProgram->pMEM);
00594 if( pProgram->pszFileName == NULL )SCRIBA_ERROR_MEMORY_LOW;
00595 strcpy(pProgram->pszFileName,pszFileName);
00596 }
00597 return SCRIBA_ERROR_SUCCESS;
00598 }
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 int scriba_LoadConfiguration(pSbProgram pProgram,
00646 char *pszForcedConfigurationFileName
00647 ){
00648
00649
00650 int iError;
00651 pProgram->pCONF = alloc_Alloc(sizeof(tConfigTree),pProgram->pMEM);
00652 if( pProgram->pCONF == NULL )return SCRIBA_ERROR_MEMORY_LOW;
00653
00654 iError = cft_start(pProgram->pCONF,alloc_Alloc,alloc_Free,pProgram->pMEM,CONFIG_ENVIR,CONFIG_FILE,pszForcedConfigurationFileName);
00655 return iError;
00656 }
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668 int scriba_GetConfigFileName(pSbProgram pProgram,
00669 char **ppszFileName
00670 ){
00671
00672
00673
00674 int iError;
00675
00676 iError = cft_GetConfigFileName(pProgram->pCONF,ppszFileName,CONFIG_ENVIR,CONFIG_FILE);
00677 return iError;
00678 }
00679
00680
00681
00682
00683
00684
00685
00686
00687 int scriba_InheritConfiguration(pSbProgram pProgram,
00688 pSbProgram pFrom
00689 ){
00690
00691
00692 if( pFrom == NULL )return 1;
00693 pProgram->pCONF = pFrom->pCONF;
00694 if( pProgram->pCONF == NULL )return 1;
00695 return 0;
00696 }
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 int scriba_InitModuleInterface(pSbProgram pProgram
00755 ){
00756
00757
00758
00759 if( pProgram->pEXE == NULL ){
00760 pProgram->pEXE = alloc_Alloc( sizeof(ExecuteObject) , pProgram->pMEM );
00761 if( pProgram->pEXE == NULL )return SCRIBA_ERROR_MEMORY_LOW;
00762
00763 pProgram->pEXE->pST = NULL;
00764
00765 pProgram->pEXE->pEPo = pProgram->pEXE;
00766 thread_InitMutex( &(pProgram->pEXE->mxModules) );
00767 pProgram->pEXE->memory_allocating_function = pProgram->maf;
00768 pProgram->pEXE->memory_releasing_function = pProgram->mrf;
00769 pProgram->pEXE->pMemorySegment = alloc_InitSegment(pProgram->pEXE->memory_allocating_function,
00770 pProgram->pEXE->memory_releasing_function);
00771 if( pProgram->pEXE->pMemorySegment == NULL )return SCRIBA_ERROR_MEMORY_LOW;
00772
00773
00774 pProgram->pEXE->pConfig = pProgram->pCONF;
00775 }
00776 modu_Init(pProgram->pEXE,1);
00777 return SCRIBA_ERROR_SUCCESS;
00778 }
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792 int scriba_InheritModuleInterface(pSbProgram pProgram,
00793 pSbProgram pFrom
00794 ){
00795
00796
00797
00798 pProgram->pSTI = pFrom->pEXE->pST;
00799 return SCRIBA_ERROR_SUCCESS;
00800 }
00801
00802
00803
00804
00805
00806 int scriba_InheritExecuteObject(pSbProgram pProgram,
00807 pSbProgram pFrom
00808 ){
00809
00810
00811 pProgram->pEPo = pFrom->pEXE;
00812 return SCRIBA_ERROR_SUCCESS;
00813 }
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832 int scriba_SetProcessSbObject(pSbProgram pProgram,
00833 pSbProgram pProcessObject
00834 ){
00835
00836
00837 int iError;
00838
00839 if( iError = scriba_InheritConfiguration(pProgram,pProcessObject) )return iError;
00840 if( iError = scriba_InheritModuleInterface(pProgram,pProcessObject) )return iError;
00841 if( iError = scriba_InheritExecuteObject(pProgram,pProcessObject) )return iError;
00842 return SCRIBA_ERROR_SUCCESS;
00843 }
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856 int scriba_ShutdownMtModules(pSbProgram pProgram
00857 ){
00858
00859
00860 pModule pThisModule;
00861
00862 thread_LockMutex( &(pProgram->pEXE->mxModules) );
00863 pThisModule = pProgram->pEXE->modules;
00864 while( pThisModule ){
00865 modu_ShutdownModule(pProgram->pEXE,pThisModule);
00866 pThisModule = pThisModule->next;
00867 }
00868 thread_UnlockMutex( &(pProgram->pEXE->mxModules) );
00869 return SCRIBA_ERROR_SUCCESS;
00870 }
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882 void scriba_SetCgiFlag(pSbProgram pProgram
00883 ){
00884
00885
00886 pProgram->fErrorFlags |= REPORT_F_CGI;
00887 }
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900 void scriba_SetReportFunction(pSbProgram pProgram,
00901 void *fpReportFunction
00902 ){
00903
00904
00905 pProgram->fpReportFunction = fpReportFunction;
00906 if( pProgram->pEXE )pProgram->pEXE->report = fpReportFunction;
00907 }
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920 void scriba_SetReportPointer(pSbProgram pProgram,
00921 void *pReportPointer
00922 ){
00923
00924
00925 pProgram->pReportPointer = pReportPointer;
00926 if( pProgram->pEXE )pProgram->pEXE->reportptr = pReportPointer;
00927 }
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942 void scriba_SetStdin(pSbProgram pProgram,
00943 void *fpStdinFunction
00944 ){
00945
00946
00947 pProgram->fpStdinFunction = fpStdinFunction;
00948 if( pProgram->pEXE )pProgram->pEXE->fpStdinFunction = fpStdinFunction;
00949 }
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964 void scriba_SetStdout(pSbProgram pProgram,
00965 void *fpStdoutFunction
00966 ){
00967
00968
00969 pProgram->fpStdouFunction = fpStdoutFunction;
00970 if( pProgram->pEXE )pProgram->pEXE->fpStdouFunction = fpStdoutFunction;
00971 }
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991 void scriba_SetEmbedPointer(pSbProgram pProgram,
00992 void *pEmbedder
00993 ){
00994
00995
00996 pProgram->pEmbedder = pEmbedder;
00997 if( pProgram->pEXE )pProgram->pEXE->pEmbedder = pEmbedder;
00998 }
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024 void scriba_SetEnvironment(pSbProgram pProgram,
01025 void *fpEnvirFunction
01026 ){
01027
01028
01029
01030
01031
01032 pProgram->fpEnvirFunction = fpEnvirFunction;
01033 if( pProgram->pEXE )pProgram->pEXE->fpEnvirFunction = fpEnvirFunction;
01034 }
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046 int scriba_LoadBinaryProgramWithOffset(pSbProgram pProgram,
01047 long lOffset,
01048 long lEOFfset
01049 ){
01050
01051
01052
01053
01054 pProgram->pBUILD = alloc_Alloc( sizeof(BuildObject) , pProgram->pMEM);
01055 if( pProgram->pBUILD == NULL )return 1;
01056
01057 pProgram->pBUILD->memory_allocating_function = pProgram->maf;
01058 pProgram->pBUILD->memory_releasing_function = pProgram->mrf;
01059 pProgram->pBUILD->iErrorCounter = 0;
01060 pProgram->pBUILD->reportptr = pProgram->pReportPointer;
01061 pProgram->pBUILD->report = pProgram->fpReportFunction;
01062 pProgram->pBUILD->fErrorFlags = pProgram->fErrorFlags;
01063 build_LoadCodeWithOffset(pProgram->pBUILD,pProgram->pszFileName,lOffset,lEOFfset);
01064 return pProgram->pBUILD->iErrorCounter;
01065 }
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076 int scriba_LoadBinaryProgram(pSbProgram pProgram
01077 ){
01078
01079
01080
01081
01082 return scriba_LoadBinaryProgramWithOffset(pProgram,0L,0L);
01083 }
01084
01085
01086
01087
01088
01089
01090
01091 int scriba_InheritBinaryProgram(pSbProgram pProgram,
01092 pSbProgram pFrom
01093 ){
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106 pProgram->pBUILD = alloc_Alloc( sizeof(BuildObject) , pProgram->pMEM);
01107 if( pProgram->pBUILD == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01108
01109 memcpy(pProgram->pBUILD,pFrom->pBUILD,sizeof(BuildObject));
01110 pProgram->pBUILD->memory_allocating_function = pProgram->maf;
01111 pProgram->pBUILD->memory_releasing_function = pProgram->mrf;
01112 pProgram->pBUILD->iErrorCounter = 0;
01113 pProgram->pBUILD->reportptr = pProgram->pReportPointer;
01114 pProgram->pBUILD->report = pProgram->fpReportFunction;
01115 pProgram->pBUILD->fErrorFlags = pProgram->fErrorFlags;
01116 return SCRIBA_ERROR_SUCCESS;
01117 }
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140 int scriba_LoadInternalPreprocessor(pSbProgram pProgram,
01141 char *ppszPreprocessorName[]
01142 ){
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155 int iError,i;
01156
01157
01158 if( pProgram->pPREP == NULL ){
01159 pProgram->pPREP = alloc_Alloc( sizeof(PreprocObject) , pProgram->pMEM );
01160 if( pProgram->pPREP == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01161 ipreproc_InitStructure(pProgram->pPREP);
01162 pProgram->pPREP->pMemorySegment = alloc_InitSegment(pProgram->maf,
01163 pProgram->mrf);
01164 if( pProgram->pPREP->pMemorySegment == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01165 pProgram->pPREP->pSB = pProgram;
01166 }
01167
01168 for( i = 0 ; ppszPreprocessorName[i] ; i++ )
01169 if( iError = ipreproc_LoadInternalPreprocessor(
01170 pProgram->pPREP,
01171 ppszPreprocessorName[i]) )return iError;
01172 return COMMAND_ERROR_SUCCESS;
01173 }
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183 int scriba_ReadSource(pSbProgram pProgram
01184 ){
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197 pProgram->pREAD = alloc_Alloc( sizeof(ReadObject) , pProgram->pMEM );
01198 if( pProgram->pREAD == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01199
01200 reader_InitStructure(pProgram->pREAD);
01201 pProgram->pREAD->memory_allocating_function = alloc_Alloc;
01202 pProgram->pREAD->memory_releasing_function = alloc_Free;
01203 pProgram->pREAD->pMemorySegment = alloc_InitSegment(
01204 pProgram->maf,
01205 pProgram->mrf);
01206 if( pProgram->pREAD->pMemorySegment == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01207 pProgram->pREAD->report = pProgram->fpReportFunction;
01208 pProgram->pREAD->reportptr = pProgram->pReportPointer;
01209 pProgram->pREAD->iErrorCounter = 0;
01210 pProgram->pREAD->fErrorFlags = pProgram->fErrorFlags;
01211 pProgram->pREAD->pConfig = pProgram->pCONF;
01212
01213
01214
01215 if( pProgram->pPREP == NULL ){
01216 pProgram->pPREP = alloc_Alloc( sizeof(PreprocObject) , pProgram->pMEM );
01217 if( pProgram->pPREP == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01218 ipreproc_InitStructure(pProgram->pPREP);
01219 pProgram->pPREP->pMemorySegment = alloc_InitSegment(
01220 pProgram->maf,
01221 pProgram->mrf);
01222 if( pProgram->pPREP->pMemorySegment == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01223 pProgram->pPREP->pSB = pProgram;
01224 }
01225 pProgram->pREAD->pPREP = pProgram->pPREP;
01226
01227 if( ! reader_ReadLines(pProgram->pREAD,pProgram->pszFileName) ){
01228 if( pProgram->pREAD->FirstUNIXline ){
01229 pProgram->FirstUNIXline = alloc_Alloc(strlen(pProgram->pREAD->FirstUNIXline)+1,pProgram->pMEM);
01230 if( pProgram->FirstUNIXline == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01231 strcpy(pProgram->FirstUNIXline,pProgram->pREAD->FirstUNIXline);
01232 }
01233 }
01234 return pProgram->pREAD->iErrorCounter;
01235 }
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245 int scriba_DoLexicalAnalysis(pSbProgram pProgram
01246 ){
01247
01248
01249
01250
01251 pProgram->pLEX = alloc_Alloc( sizeof(LexObject) , pProgram->pMEM );
01252 if( pProgram->pLEX == NULL )return 1;
01253
01254 reader_StartIteration(pProgram->pREAD);
01255
01256 pProgram->pLEX->memory_allocating_function = alloc_Alloc;
01257 pProgram->pLEX->memory_releasing_function = alloc_Free;
01258 pProgram->pLEX->pMemorySegment = alloc_InitSegment(
01259 pProgram->maf,
01260 pProgram->mrf);
01261 if( pProgram->pLEX->pMemorySegment == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01262
01263 pProgram->pLEX->pPREP = pProgram->pPREP;
01264 lex_InitStructure( pProgram->pLEX);
01265
01266 pProgram->pLEX->pfGetCharacter = reader_NextCharacter;
01267 pProgram->pLEX->pfFileName = reader_FileName;
01268 pProgram->pLEX->pfLineNumber = reader_LineNumber;
01269
01270 if( pProgram->pLEX->pNASymbols == NULL )pProgram->pLEX->pNASymbols = NASYMBOLS;
01271 if( pProgram->pLEX->pASymbols == NULL )pProgram->pLEX->pASymbols = ASYMBOLS;
01272 if( pProgram->pLEX->pCSymbols == NULL )pProgram->pLEX->pCSymbols = CSYMBOLS;
01273 pProgram->pLEX->report = pProgram->fpReportFunction;
01274 pProgram->pLEX->reportptr = pProgram->pReportPointer;
01275 pProgram->pLEX->fErrorFlags = pProgram->fErrorFlags;
01276 pProgram->pLEX->iErrorCounter = 0;
01277 pProgram->pLEX->pLexResult = (void *)stderr;
01278
01279
01280 pProgram->pLEX->pvInput = (void *) pProgram->pREAD;
01281 lex_ReadInput(pProgram->pLEX);
01282
01283 if( pProgram->pLEX->iErrorCounter )return pProgram->pLEX->iErrorCounter;
01284 lex_RemoveComments(pProgram->pLEX);
01285 lex_RemoveSkipSymbols(pProgram->pLEX);
01286 lex_HandleContinuationLines(pProgram->pLEX);
01287 return pProgram->pLEX->iErrorCounter;
01288 }
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298 int scriba_DoSyntaxAnalysis(pSbProgram pProgram
01299 ){
01300
01301
01302
01303
01304 peNODE_l CommandList;
01305 int iError;
01306
01307 pProgram->pEX = alloc_Alloc( sizeof(eXobject) , pProgram->pMEM);
01308 if( pProgram->pEX == NULL )return 1;
01309
01310 pProgram->pEX->pPREP = pProgram->pPREP;
01311 pProgram->pEX->memory_allocating_function = pProgram->maf;
01312 pProgram->pEX->memory_releasing_function = pProgram->mrf;
01313 pProgram->pEX->cbBuffer = 1024;
01314 pProgram->pEX->cbCurrentNameSpace = 1024;
01315 pProgram->pEX->pLex = pProgram->pLEX;
01316
01317 pProgram->pEX->Unaries = UNARIES;
01318 pProgram->pEX->Binaries = BINARIES;
01319 pProgram->pEX->BuiltInFunctions = INTERNALFUNCTIONS;
01320 pProgram->pEX->MAXPREC = MAX_BINARY_OPERATOR_PRECEDENCE;
01321 pProgram->pEX->PredeclaredLongConstants = PREDLCONSTS;
01322 pProgram->pEX->reportptr = pProgram->pReportPointer;
01323 pProgram->pEX->report = pProgram->fpReportFunction;
01324 pProgram->pEX->fErrorFlags = pProgram->fErrorFlags;
01325 pProgram->pEX->iErrorCounter = 0;
01326 iError = 0;
01327
01328 pProgram->pEX->Command = COMMANDS;
01329
01330 ex_init(pProgram->pEX);
01331
01332 ex_Command_l(pProgram->pEX,&CommandList);
01333
01334 pProgram->pEX->pCommandList = CommandList;
01335 if( pProgram->pPREP && pProgram->pPREP->n )
01336 iError = ipreproc_Process(pProgram->pPREP,PreprocessorExFinish,pProgram->pEX);
01337 if( iError )pProgram->pEX->iErrorCounter++;
01338
01339 return pProgram->pEX->iErrorCounter;
01340 }
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351 int scriba_BuildCode(pSbProgram pProgram
01352 ){
01353
01354
01355
01356
01357
01358 pProgram->pBUILD = alloc_Alloc( sizeof(BuildObject) , pProgram->pMEM );
01359 if( pProgram->pBUILD == NULL )return 1;
01360
01361 pProgram->pBUILD->pPREP = pProgram->pPREP;
01362 pProgram->pBUILD->memory_allocating_function = pProgram->maf;
01363 pProgram->pBUILD->memory_releasing_function = pProgram->mrf;
01364 pProgram->pBUILD->pEx = pProgram->pEX;
01365 pProgram->pBUILD->iErrorCounter = 0;
01366 pProgram->pBUILD->fErrorFlags = pProgram->pEX->fErrorFlags;
01367 pProgram->pBUILD->FirstUNIXline = pProgram->FirstUNIXline;
01368
01369 build_Build(pProgram->pBUILD);
01370
01371 if( pProgram->pBUILD->iErrorCounter )return pProgram->pBUILD->iErrorCounter;
01372 return 0;
01373 }
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386 int scriba_IsFileBinaryFormat(pSbProgram pProgram
01387 ){
01388
01389
01390
01391 return build_IsFileBinaryFormat(pProgram->pszFileName);
01392 }
01393
01394
01395
01396
01397
01398
01399
01400
01401 int scriba_GetCacheFileName(pSbProgram pProgram
01402 ){
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413 #define FULL_PATH_BUFFER_LENGTH 256
01414 char *pszCache;
01415 char *s,*q;
01416 char CachedFileName[FULL_PATH_BUFFER_LENGTH];
01417
01418 if( pProgram->pszFileName == NULL )return SCRIBA_ERROR_FAIL;
01419 pszCache = cft_GetString(pProgram->pCONF,"cache");
01420 if( pszCache == NULL)return SCRIBA_ERROR_FAIL;
01421 if( strlen(pszCache) >= FULL_PATH_BUFFER_LENGTH )return SCRIBA_ERROR_BUFFER_SHORT;
01422 strcpy(CachedFileName,pszCache);
01423 s = CachedFileName + strlen(CachedFileName);
01424
01425 #ifdef WIN32
01426
01427 if( GetFullPathName(pProgram->pszFileName,
01428 FULL_PATH_BUFFER_LENGTH-strlen(CachedFileName),s,&q)==0 )
01429 return SCRIBA_ERROR_FAIL;
01430 #else
01431
01432 if( strlen(pProgram->pszFileName) > FULL_PATH_BUFFER_LENGTH - strlen(CachedFileName) )
01433 return SCRIBA_ERROR_BUFFER_SHORT;
01434 strcpy(s,pProgram->pszFileName);
01435 #endif
01436
01437 uniqfnam(s,s);
01438 if( pProgram->pszCacheFileName )alloc_Free(pProgram->pszCacheFileName,pProgram->pMEM);
01439 pProgram->pszCacheFileName = alloc_Alloc(strlen(CachedFileName)+1,pProgram->pMEM);
01440 if( pProgram->pszCacheFileName == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01441 strcpy(pProgram->pszCacheFileName,CachedFileName);
01442 return SCRIBA_ERROR_SUCCESS;
01443 }
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462 int scriba_UseCacheFile(pSbProgram pProgram
01463 ){
01464
01465
01466
01467
01468
01469 unsigned long FileTime,CacheTime;
01470 int iError;
01471
01472 if( iError = scriba_GetCacheFileName(pProgram) )return iError;
01473
01474 FileTime = file_time_modified(pProgram->pszFileName);
01475 CacheTime = file_time_modified(pProgram->pszCacheFileName);
01476 if( FileTime && CacheTime && CacheTime > FileTime &&
01477 build_IsFileBinaryFormat(pProgram->pszCacheFileName) ){
01478 alloc_Free(pProgram->pszFileName,pProgram->pMEM);
01479 pProgram->pszFileName = alloc_Alloc(strlen(pProgram->pszCacheFileName)+1,
01480 pProgram->pMEM);
01481 if( pProgram->pszFileName == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01482 strcpy(pProgram->pszFileName,pProgram->pszCacheFileName);
01483 return SCRIBA_ERROR_SUCCESS;
01484 }
01485 return SCRIBA_ERROR_FAIL;
01486 }
01487
01488
01489
01490
01491
01492
01493
01494
01495 int scriba_SaveCacheFile(pSbProgram pProgram
01496 ){
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506 if( ! pProgram->pszCacheFileName )
01507 scriba_GetCacheFileName(pProgram);
01508 if( pProgram->pszCacheFileName )
01509 return scriba_SaveCode(pProgram,pProgram->pszCacheFileName);
01510 return SCRIBA_ERROR_SUCCESS;
01511 }
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522 int scriba_RunExternalPreprocessor(pSbProgram pProgram,
01523 char **ppszArgPreprocessor
01524 ){
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541 int iError;
01542 char *pszPreprocessedFileName=NULL;
01543
01544 iError = epreproc(pProgram->pCONF,
01545 pProgram->pszFileName,
01546 &pszPreprocessedFileName,
01547 ppszArgPreprocessor,
01548 pProgram->maf,
01549 pProgram->mrf);
01550
01551
01552 if( iError )return iError;
01553
01554
01555 if( pszPreprocessedFileName == NULL )return SCRIBA_ERROR_SUCCESS;
01556
01557 if( pProgram->pszFileName ){
01558 alloc_Free(pProgram->pszFileName,pProgram->pMEM);
01559 pProgram->pszFileName = NULL;
01560 }
01561
01562
01563
01564 pProgram->pszFileName = alloc_Alloc(strlen(pszPreprocessedFileName)+1,pProgram->pMEM);
01565 if( pProgram->pszFileName == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01566 strcpy(pProgram->pszFileName,pszPreprocessedFileName);
01567 pProgram->mrf(pszPreprocessedFileName);
01568 return SCRIBA_ERROR_SUCCESS;
01569 }
01570
01571
01572
01573
01574
01575
01576
01577 int scriba_SaveCode(pSbProgram pProgram,
01578 char *pszCodeFileName
01579 ){
01580
01581
01582
01583
01584
01585 return build_SaveCode(pProgram->pBUILD,pszCodeFileName);
01586 }
01587
01588
01589
01590
01591
01592 void scriba_SaveCCode(pSbProgram pProgram,
01593 char *pszCodeFileName
01594 ){
01595
01596
01597 build_SaveCCode(pProgram->pBUILD,pszCodeFileName);
01598 }
01599
01600
01601
01602
01603
01604 void scriba_SaveECode(pSbProgram pProgram,
01605 char *pszInterpreter,
01606 char *pszCodeFileName
01607 ){
01608
01609
01610 build_SaveECode(pProgram->pBUILD,pszInterpreter,pszCodeFileName);
01611 }
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626 int scriba_LoadSourceProgram(pSbProgram pProgram
01627 ){
01628
01629
01630
01631
01632
01633
01634
01635 int iError;
01636
01637 if( iError = scriba_ReadSource(pProgram) )return iError;
01638 if( iError = scriba_DoLexicalAnalysis(pProgram) )return iError;
01639 if( iError = scriba_DoSyntaxAnalysis(pProgram) )return iError;
01640 if( iError = scriba_BuildCode(pProgram) )return iError;
01641
01642
01643
01644
01645 scriba_PurgeReaderMemory(pProgram);
01646 scriba_PurgeLexerMemory(pProgram);
01647 scriba_PurgeSyntaxerMemory(pProgram);
01648 return SCRIBA_ERROR_SUCCESS;
01649 }
01650
01651
01652
01653
01654
01655 typedef struct _StringInputState {
01656 char *pszFileName;
01657 char *pszBuffer;
01658 unsigned long cbBuffer;
01659 unsigned long lBufferPosition;
01660
01661 void * (*fpOpenFile)(char *, void *);
01662 int (*fpGetCharacter)(void *, void *);
01663 void (*fpCloseFile)(void *, void *);
01664 int iActive;
01665 } StringInputState, *pStringInputState;
01666
01667 static void *StringOpen(char *psz, pStringInputState p){
01668 p->iActive = 0;
01669 if( psz != p->pszFileName )return p->fpOpenFile(psz,NULL);
01670 p->iActive = 1;
01671 p->lBufferPosition = 0;
01672 return (void *)p;
01673 }
01674
01675 static void StringClose(void *p, pStringInputState q){
01676 if( ! q->iActive ){
01677 q->fpCloseFile(p,NULL);
01678 return;
01679 }
01680 return;
01681 }
01682
01683 static int StringGetCharacter(void *pv, pStringInputState p){
01684 if( ! p->iActive )return p->fpGetCharacter(pv,NULL);
01685
01686 if( p->lBufferPosition < p->cbBuffer )return p->pszBuffer[p->lBufferPosition++];
01687 return -1;
01688 }
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698 int scriba_LoadProgramString(pSbProgram pProgram,
01699 char *pszSourceCode,
01700 unsigned long cbSourceCode
01701 ){
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723 int iError;
01724 StringInputState SIS;
01725
01726 if( pProgram->pszFileName == NULL )scriba_SetFileName(pProgram,"");
01727
01728 pProgram->pREAD = alloc_Alloc( sizeof(ReadObject) , pProgram->pMEM );
01729 if( pProgram->pREAD == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01730
01731 reader_InitStructure(pProgram->pREAD);
01732
01733
01734
01735 SIS.fpOpenFile = pProgram->pREAD->fpOpenFile;
01736 pProgram->pREAD->fpOpenFile = (void *)StringOpen;
01737 SIS.fpGetCharacter = pProgram->pREAD->fpGetCharacter;
01738 pProgram->pREAD->fpGetCharacter = (void *)StringGetCharacter;
01739 SIS.fpCloseFile = pProgram->pREAD->fpCloseFile;
01740 pProgram->pREAD->fpCloseFile = (void *)StringClose;
01741
01742 pProgram->pREAD->memory_allocating_function = alloc_Alloc;
01743 pProgram->pREAD->memory_releasing_function = alloc_Free;
01744 pProgram->pREAD->pMemorySegment = alloc_InitSegment(
01745 pProgram->maf,
01746 pProgram->mrf);
01747 if( pProgram->pREAD->pMemorySegment == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01748 pProgram->pREAD->report = pProgram->fpReportFunction;
01749 pProgram->pREAD->reportptr = pProgram->pReportPointer;
01750 pProgram->pREAD->iErrorCounter = 0;
01751 pProgram->pREAD->fErrorFlags = pProgram->fErrorFlags;
01752 pProgram->pREAD->pConfig = pProgram->pCONF;
01753 pProgram->pREAD->pFileHandleClass = &SIS;
01754 SIS.pszBuffer = pszSourceCode;
01755 SIS.cbBuffer = cbSourceCode;
01756 SIS.pszFileName = pProgram->pszFileName;
01757
01758
01759
01760 if( pProgram->pPREP == NULL ){
01761 pProgram->pPREP = alloc_Alloc( sizeof(PreprocObject) , pProgram->pMEM );
01762 if( pProgram->pPREP == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01763 ipreproc_InitStructure(pProgram->pPREP);
01764 pProgram->pPREP->pMemorySegment = alloc_InitSegment(
01765 pProgram->maf,
01766 pProgram->mrf);
01767 if( pProgram->pPREP->pMemorySegment == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01768 pProgram->pPREP->pSB = pProgram;
01769 }
01770 pProgram->pREAD->pPREP = pProgram->pPREP;
01771
01772 if( ! (iError = reader_ReadLines(pProgram->pREAD,pProgram->pszFileName)) ){
01773 if( pProgram->pREAD->FirstUNIXline ){
01774 pProgram->FirstUNIXline = alloc_Alloc(strlen(pProgram->pREAD->FirstUNIXline)+1,pProgram->pMEM);
01775 if( pProgram->FirstUNIXline == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01776 strcpy(pProgram->FirstUNIXline,pProgram->pREAD->FirstUNIXline);
01777 }
01778 }else{
01779 return iError;
01780 }
01781 if( pProgram->pREAD->iErrorCounter )return pProgram->pREAD->iErrorCounter;
01782 if( iError = scriba_DoLexicalAnalysis(pProgram) )return iError;
01783 if( iError = scriba_DoSyntaxAnalysis(pProgram) )return iError;
01784 if( iError = scriba_BuildCode(pProgram) )return iError;
01785
01786
01787
01788
01789 scriba_PurgeReaderMemory(pProgram);
01790 scriba_PurgeLexerMemory(pProgram);
01791 scriba_PurgeSyntaxerMemory(pProgram);
01792 return SCRIBA_ERROR_SUCCESS;
01793 }
01794
01795 static int scriba_PreRun(pSbProgram pProgram){
01796 int iError;
01797
01798 if( pProgram->pEXE == NULL ){
01799 pProgram->pEXE = alloc_Alloc( sizeof(ExecuteObject) , pProgram->pMEM );
01800 if( pProgram->pEXE == NULL )return SCRIBA_ERROR_MEMORY_LOW;
01801
01802 pProgram->pEXE->memory_allocating_function = pProgram->maf;
01803 pProgram->pEXE->memory_releasing_function = pProgram->mrf;
01804 pProgram->pEXE->reportptr = pProgram->pReportPointer;
01805 pProgram->pEXE->report = pProgram->fpReportFunction;
01806 pProgram->pEXE->fErrorFlags = pProgram->fErrorFlags;
01807
01808 pProgram->pEXE->pConfig = pProgram->pCONF;
01809 build_MagicCode(&(pProgram->pEXE->Ver));
01810 if( iError=execute_InitStructure( pProgram->pEXE,pProgram->pBUILD) )
01811 return iError;
01812 pProgram->pEXE->fpStdouFunction = pProgram->fpStdouFunction;
01813 pProgram->pEXE->fpStdinFunction = pProgram->fpStdinFunction;
01814 pProgram->pEXE->fpEnvirFunction = pProgram->fpEnvirFunction;
01815 pProgram->pEXE->pEmbedder = pProgram->pEmbedder;
01816 pProgram->pEXE->pSTI = pProgram->pSTI;
01817 pProgram->pEXE->pEPo = pProgram->pEPo;
01818 }else{
01819 if( iError=execute_ReInitStructure( pProgram->pEXE,pProgram->pBUILD) )
01820 return iError;
01821 }
01822
01823 pProgram->pEXE->CmdLineArgument = NULL;
01824
01825 return SCRIBA_ERROR_SUCCESS;
01826 }
01827
01828
01829
01830
01831
01832
01833
01834
01835
01836
01837
01838
01839
01840
01841
01842 int scriba_Run(pSbProgram pProgram,
01843 char *pszCommandLineArgument
01844 ){
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858 int iError;
01859
01860 if( iError = scriba_PreRun(pProgram) ){
01861 return iError;
01862 }
01863
01864 pProgram->pEXE->CmdLineArgument = pszCommandLineArgument;
01865 execute_InitExecute(pProgram->pEXE,&iError);
01866
01867 iError = 0;
01868 if( pProgram->pPREP && pProgram->pPREP->n )
01869 iError = ipreproc_Process(pProgram->pPREP,PreprocessorExeStart,pProgram->pEXE);
01870 if( iError )return iError;
01871
01872 execute_Execute_r(pProgram->pEXE,&iError);
01873 if( iError )return iError;
01874 if( pProgram->pPREP && pProgram->pPREP->n )
01875 iError = ipreproc_Process(pProgram->pPREP,PreprocessorExeFinish,pProgram->pEXE);
01876 return iError;
01877 }
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887
01888
01889
01890
01891 int scriba_NoRun(pSbProgram pProgram
01892 ){
01893
01894
01895
01896 int iError;
01897
01898 scriba_PreRun(pProgram);
01899 execute_InitExecute(pProgram->pEXE,&iError);
01900 if( iError )return iError;
01901 if( pProgram->pPREP && pProgram->pPREP->n )
01902 iError = ipreproc_Process(pProgram->pPREP,PreprocessorExeNoRun,pProgram->pEXE);
01903 return iError;
01904 }
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917 void scriba_ResetVariables(pSbProgram pProgram
01918 ){
01919
01920
01921
01922
01923 memory_ReleaseVariable(pProgram->pEXE->pMo,pProgram->pEXE->GlobalVariables);
01924 pProgram->pEXE->GlobalVariables = NULL;
01925 }
01926
01927
01928
01929
01930
01931
01932
01933
01934 int scriba_Call(pSbProgram pProgram,
01935 unsigned long lEntryNode
01936 ){
01937
01938
01939
01940
01941
01942
01943
01944
01945
01946
01947
01948 int iError;
01949
01950 execute_ExecuteFunction(pProgram->pEXE,lEntryNode,0,NULL,NULL,&iError);
01951
01952 return iError;
01953 }
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964 int scriba_CallArg(pSbProgram pProgram,
01965 unsigned long lEntryNode,
01966 char *pszFormat, ...
01967 ){
01968
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005 int iError;
02006 VARIABLE vArgs;
02007 va_list marker;
02008 unsigned long cArgs,i,slen;
02009 char *s;
02010 char *arg;
02011
02012 cArgs = 0;
02013 if( pszFormat ){
02014 s = pszFormat;
02015 while( *s ){
02016 switch( *s++ ){
02017 case 'U':
02018 case 'u':
02019
02020 case 'B':
02021 case 'b':
02022
02023 case 'S':
02024 case 's':
02025 case 'I':
02026 case 'i':
02027 case 'R':
02028 case 'r':
02029 cArgs ++;
02030 break;
02031 default:;
02032 }
02033 }
02034 }
02035
02036 if( cArgs )
02037 vArgs = memory_NewArray(pProgram->pEXE->pMo,0,cArgs-1);
02038 else
02039 vArgs = NULL;
02040
02041 if( vArgs ){
02042 i = 0;
02043 va_start(marker,pszFormat);
02044 s = pszFormat;
02045 while( *s ){
02046 switch( *s++ ){
02047 case 'U':
02048 case 'u':
02049 vArgs->Value.aValue[i] = NULL;
02050 i++;
02051 break;
02052 case 'B':
02053 case 'b':
02054 slen = va_arg(marker, long);
02055 arg = va_arg(marker, char *);
02056 if( arg == NULL )arg = "";
02057 vArgs->Value.aValue[i] = memory_NewString(pProgram->pEXE->pMo,slen);
02058 memcpy(STRINGVALUE(vArgs->Value.aValue[i]),arg,slen);
02059 i++;
02060 break;
02061 case 'S':
02062 case 's':
02063 arg = va_arg(marker, char *);
02064 if( arg == NULL )arg = "";
02065 slen = strlen(arg);
02066 vArgs->Value.aValue[i] = memory_NewString(pProgram->pEXE->pMo,slen);
02067 memcpy(STRINGVALUE(vArgs->Value.aValue[i]),arg,slen);
02068 i++;
02069 break;
02070 case 'I':
02071 case 'i':
02072 vArgs->Value.aValue[i] = memory_NewLong(pProgram->pEXE->pMo);
02073 LONGVALUE(vArgs->Value.aValue[i]) = va_arg(marker, long);
02074 i++;
02075 break;
02076 case 'R':
02077 case 'r':
02078 vArgs->Value.aValue[i] = memory_NewDouble(pProgram->pEXE->pMo);
02079 DOUBLEVALUE(vArgs->Value.aValue[i]) = va_arg(marker, double);
02080 i++;
02081 break;
02082 }
02083 }
02084 }
02085
02086 execute_ExecuteFunction(pProgram->pEXE,lEntryNode,cArgs,vArgs ? vArgs->Value.aValue : NULL ,NULL,&iError);
02087 memory_ReleaseVariable(pProgram->pEXE->pMo,vArgs);
02088 return iError;
02089 }
02090
02091
02092
02093
02094
02095
02096
02097
02098 void scriba_DestroySbArgs(pSbProgram pProgram,
02099 pSbData Args,
02100 unsigned long cArgs
02101 ){
02102
02103
02104
02105
02106
02107
02108
02109
02110
02111
02112 unsigned long i;
02113
02114 for( i=0 ; i<cArgs ; i++ )
02115 if( Args[i].type == SBT_STRING )
02116 alloc_Free(Args[i].v.s,pProgram->pMEM);
02117 alloc_Free(Args,pProgram->pMEM);
02118 }
02119
02120
02121
02122
02123
02124
02125
02126
02127
02128
02129 pSbData scriba_NewSbArgs(pSbProgram pProgram,
02130 char *pszFormat, ...
02131 ){
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143
02144
02145
02146
02147
02148
02149
02150
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167
02168
02169
02170
02171
02172 va_list marker;
02173 unsigned long cArgs,i;
02174 char *s;
02175 char *arg;
02176 pSbData p;
02177
02178 if( pszFormat == NULL )return NULL;
02179
02180 cArgs = 0;
02181 s = pszFormat;
02182 while( *s ){
02183 switch( *s++ ){
02184 case 'U':
02185 case 'u':
02186
02187 case 'B':
02188 case 'b':
02189
02190 case 'S':
02191 case 's':
02192 case 'I':
02193 case 'i':
02194 case 'R':
02195 case 'r':
02196 cArgs ++;
02197 break;
02198 default:;
02199 }
02200 }
02201 p = alloc_Alloc(sizeof(SbData)*cArgs,pProgram->pMEM);
02202 if( p == NULL )return NULL;
02203
02204 i = 0;
02205 va_start(marker,pszFormat);
02206 s = pszFormat;
02207 while( *s ){
02208 switch( *s++ ){
02209 case 'U':
02210 case 'u':
02211 p[i].type = SBT_UNDEF;
02212 i++;
02213 break;
02214 case 'B':
02215 case 'b':
02216 p[i].type = SBT_STRING;
02217 p[i].size = va_arg(marker, long);
02218 arg = va_arg(marker, char *);
02219 if( arg == NULL && p[i].size != 0 ){
02220 p[i++].type = SBT_UNDEF;
02221 break;
02222 }
02223 p[i].size = strlen(arg);
02224 if( p[i].size ){
02225 p[i].v.s = alloc_Alloc(p[i].size,pProgram->pMEM);
02226 if( p[i].v.s == NULL ){
02227 while( i ){
02228 if( p[i].type == SBT_STRING && p[i].v.s )alloc_Free(p[i].v.s,pProgram->pMEM);
02229 i--;
02230 }
02231 alloc_Free(p,pProgram->pMEM);
02232 return NULL;
02233 }
02234 memcpy(p[i].v.s,arg,p[i].size);
02235 }else{
02236 p[i].v.s = NULL;
02237 }
02238 i++;
02239 break;
02240 case 'S':
02241 case 's':
02242 p[i].type = SBT_STRING;
02243 arg = va_arg(marker, char *);
02244 if( arg == NULL )arg = "";
02245 p[i].size = strlen(arg);
02246 if( p[i].size ){
02247 p[i].v.s = alloc_Alloc(p[i].size,pProgram->pMEM);
02248 if( p[i].v.s == NULL ){
02249 while( i ){
02250 if( p[i].type == SBT_STRING && p[i].v.s )alloc_Free(p[i].v.s,pProgram->pMEM);
02251 i--;
02252 }
02253 alloc_Free(p,pProgram->pMEM);
02254 return NULL;
02255 }
02256 memcpy(p[i].v.s,arg,p[i].size);
02257 }else{
02258 p[i].v.s = NULL;
02259 }
02260 i++;
02261 break;
02262 case 'I':
02263 case 'i':
02264 p[i].type = SBT_LONG;
02265 p[i].v.l = va_arg(marker, long);
02266 i++;
02267 break;
02268 case 'R':
02269 case 'r':
02270 p[i].type = SBT_DOUBLE;
02271 p[i].v.d = va_arg(marker, double);
02272 i++;
02273 break;
02274 }
02275 }
02276
02277 return p;
02278 }
02279
02280
02281
02282
02283
02284
02285
02286
02287
02288 int scriba_CallArgEx(pSbProgram pProgram,
02289 unsigned long lEntryNode,
02290 pSbData ReturnValue,
02291 unsigned long cArgs,
02292 pSbData Args
02293 ){
02294
02295
02296
02297
02298
02299
02300
02301
02302
02303
02304
02305 int iError;
02306 VARIABLE vArgs;
02307 VARIABLE vReturn;
02308 unsigned long i;
02309
02310 if( cArgs )
02311 vArgs = memory_NewArray(pProgram->pEXE->pMo,0,cArgs-1);
02312 else
02313 vArgs = NULL;
02314
02315 if( vArgs ){
02316 for( i = 0 ; i < cArgs ; i ++ ){
02317 switch( Args[i].type ){
02318 case SBT_UNDEF:
02319 vArgs->Value.aValue[i] = NULL;
02320 break;
02321 case SBT_STRING:
02322 vArgs->Value.aValue[i] = memory_NewString(pProgram->pEXE->pMo,Args[i].size);
02323 memcpy(STRINGVALUE(vArgs->Value.aValue[i]),Args[i].v.s,Args[i].size);
02324 alloc_Free(Args[i].v.s,pProgram->pMEM);
02325 break;
02326 case SBT_LONG:
02327 vArgs->Value.aValue[i] = memory_NewLong(pProgram->pEXE->pMo);
02328 LONGVALUE(vArgs->Value.aValue[i]) = Args[i].v.l;
02329 break;
02330 case SBT_DOUBLE:
02331 vArgs->Value.aValue[i] = memory_NewDouble(pProgram->pEXE->pMo);
02332 DOUBLEVALUE(vArgs->Value.aValue[i]) = Args[i].v.d;
02333 break;
02334 }
02335 }
02336 }
02337
02338 execute_ExecuteFunction(pProgram->pEXE,lEntryNode,cArgs,vArgs ? vArgs->Value.aValue : NULL ,&vReturn,&iError);
02339 scriba_UndefSbData(pProgram,ReturnValue);
02340
02341 if( ! iError && vReturn ){
02342 switch( vReturn->vType ){
02343 case VTYPE_LONG:
02344 ReturnValue->type = SBT_LONG;
02345 ReturnValue->v.l = LONGVALUE(vReturn);
02346 break;
02347 case VTYPE_DOUBLE:
02348 ReturnValue->type = SBT_DOUBLE;
02349 ReturnValue->v.d = DOUBLEVALUE(vReturn);
02350 break;
02351 case VTYPE_STRING:
02352 ReturnValue->type = SBT_STRING;
02353
02354 ReturnValue->size=STRLEN(vReturn);
02355 ReturnValue->v.s = alloc_Alloc(ReturnValue->size+1,pProgram->pMEM);
02356 if( ReturnValue->v.s ){
02357 memcpy(ReturnValue->v.s,STRINGVALUE(vReturn),ReturnValue->size);
02358 ReturnValue->v.s[ReturnValue->size] = (char)0;
02359 }
02360 break;
02361 default:
02362 ReturnValue->type = SBT_UNDEF;
02363 break;
02364 }
02365 }
02366
02367 if( vArgs && ! iError ){
02368 for( i = 0 ; i < cArgs ; i ++ ){
02369 if( vArgs->Value.aValue[i] == NULL ){
02370 Args[i].type = SBT_UNDEF;
02371 continue;
02372 }
02373 switch( vArgs->Value.aValue[i]->vType ){
02374 case VTYPE_LONG:
02375 Args[i].type = SBT_LONG;
02376 Args[i].v.l = LONGVALUE(vArgs->Value.aValue[i]);
02377 break;
02378 case VTYPE_DOUBLE:
02379 Args[i].type = SBT_DOUBLE;
02380 Args[i].v.d = DOUBLEVALUE(vArgs->Value.aValue[i]);
02381 break;
02382 case VTYPE_STRING:
02383
02384 Args[i].type = SBT_STRING;
02385 Args[i].size=STRLEN(vArgs->Value.aValue[i]);
02386 Args[i].v.s = alloc_Alloc(Args[i].size+1,pProgram->pMEM);
02387 if( Args[i].v.s ){
02388 memcpy(Args[i].v.s,STRINGVALUE(vArgs->Value.aValue[i]),Args[i].size);
02389 Args[i].v.s[Args[i].size] = (char)0;
02390 }
02391 break;
02392 default:
02393 Args[i].type = SBT_UNDEF;
02394 break;
02395 }
02396 }
02397 }
02398
02399 memory_ReleaseVariable(pProgram->pEXE->pMo,vArgs);
02400 memory_ReleaseVariable(pProgram->pEXE->pMo,vReturn);
02401 return iError;
02402 }
02403
02404
02405
02406
02407
02408
02409
02410
02411
02412
02413 long scriba_LookupFunctionByName(pSbProgram pProgram,
02414 char *pszFunctionName
02415 ){
02416
02417
02418
02419
02420 return build_LookupFunctionByName(pProgram->pBUILD,pszFunctionName);
02421 }
02422
02423
02424
02425
02426
02427
02428
02429
02430
02431
02432
02433 long scriba_LookupVariableByName(pSbProgram pProgram,
02434 char *pszVariableName
02435 ){
02436
02437
02438
02439
02440
02441
02442
02443
02444
02445 if( pProgram->pBUILD == NULL )return 0;
02446 return build_LookupVariableByName(pProgram->pBUILD,pszVariableName);
02447 }
02448
02449
02450
02451
02452
02453
02454
02455
02456
02457
02458
02459
02460
02461
02462
02463 long scriba_GetVariableType(pSbProgram pProgram,
02464 long lSerial
02465 ){
02466
02467
02468
02469
02470
02471
02472
02473 if( lSerial <= 0 || lSerial > pProgram->pEXE->cGlobalVariables )return SBT_UNDEF;
02474
02475 if( pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] == NULL )return SBT_UNDEF;
02476
02477 switch( pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->vType ){
02478 case VTYPE_LONG: return SBT_LONG;
02479 case VTYPE_DOUBLE: return SBT_DOUBLE;
02480 case VTYPE_STRING: return SBT_STRING;
02481 default: return SBT_UNDEF;
02482 }
02483 }
02484
02485
02486
02487
02488
02489
02490
02491
02492
02493
02494
02495 int scriba_GetVariable(pSbProgram pProgram,
02496 long lSerial,
02497 pSbData *pVariable
02498 ){
02499
02500
02501
02502
02503
02504
02505
02506
02507
02508
02509
02510 if( lSerial <= 0 || lSerial > pProgram->pEXE->cGlobalVariables )return SCRIBA_ERROR_FAIL;
02511
02512 if( pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] == NULL ){
02513 *pVariable = scriba_NewSbUndef(pProgram);
02514 if( *pVariable )return SCRIBA_ERROR_SUCCESS;
02515 return SCRIBA_ERROR_FAIL;
02516 }
02517
02518 switch( pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->vType ){
02519 case VTYPE_LONG:
02520 *pVariable = scriba_NewSbLong(pProgram,
02521 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->Value.lValue);
02522 if( *pVariable )return SCRIBA_ERROR_SUCCESS;
02523 return SCRIBA_ERROR_MEMORY_LOW;
02524 case VTYPE_DOUBLE:
02525 *pVariable = scriba_NewSbDouble(pProgram,
02526 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->Value.dValue);
02527 if( *pVariable )return SCRIBA_ERROR_SUCCESS;
02528 return SCRIBA_ERROR_MEMORY_LOW;
02529 case VTYPE_STRING:
02530 *pVariable = scriba_NewSbBytes(pProgram,
02531 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->Size,
02532 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->Value.pValue);
02533 if( *pVariable )return SCRIBA_ERROR_SUCCESS;
02534 return SCRIBA_ERROR_MEMORY_LOW;
02535 default:
02536 *pVariable = scriba_NewSbUndef(pProgram);
02537 if( *pVariable )return SCRIBA_ERROR_SUCCESS;
02538 return SCRIBA_ERROR_FAIL;
02539 }
02540 }
02541
02542
02543
02544
02545
02546
02547
02548
02549
02550 int scriba_SetVariable(pSbProgram pProgram,
02551 long lSerial,
02552 int type,
02553 long lSetValue,
02554 double dSetValue,
02555 char *pszSetValue,
02556 unsigned long size
02557 ){
02558
02559
02560
02561
02562
02563
02564
02565
02566
02567
02568
02569
02570
02571
02572
02573
02574
02575
02576
02577
02578
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588
02589
02590
02591
02592
02593
02594
02595
02596
02597
02598
02599
02600 if( lSerial <= 0 || lSerial > pProgram->pEXE->cGlobalVariables )return SCRIBA_ERROR_FAIL;
02601
02602 if( pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] ){
02603 memory_ReleaseVariable(pProgram->pEXE->pMo,pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]);
02604 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] = NULL;
02605 }
02606 if( type == SBT_UNDEF )return SCRIBA_ERROR_SUCCESS;
02607 switch( type ){
02608 case SBT_DOUBLE:
02609 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] =
02610 memory_NewDouble(pProgram->pEXE->pMo);
02611 if( pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] == NULL )return SCRIBA_ERROR_MEMORY_LOW;
02612 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->Value.dValue = dSetValue;
02613 return SCRIBA_ERROR_SUCCESS;
02614 case SBT_LONG:
02615 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] =
02616 memory_NewLong(pProgram->pEXE->pMo);
02617 if( pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] == NULL )return SCRIBA_ERROR_MEMORY_LOW;
02618 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->Value.lValue = lSetValue;
02619 return SCRIBA_ERROR_SUCCESS;
02620 case SBT_ZCHAR:
02621 size = strlen(pszSetValue);
02622 type = SBT_STRING;
02623
02624 case SBT_STRING:
02625 pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] =
02626 memory_NewString(pProgram->pEXE->pMo,size);
02627 if( pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1] == NULL )return SCRIBA_ERROR_MEMORY_LOW;
02628 memcpy(pProgram->pEXE->GlobalVariables->Value.aValue[lSerial-1]->Value.pValue,pszSetValue,size);
02629 return SCRIBA_ERROR_SUCCESS;
02630
02631 default: return SCRIBA_ERROR_FAIL;
02632 }
02633 }
02634
02635 extern MODLIST StaticallyLinkedModules[];
02636
02637
02638
02639
02640
02641
02642
02643
02644
02645
02646
02647
02648
02649
02650
02651
02652 void scriba_InitStaticModules(void
02653 ){
02654
02655
02656 MODLIST *SLM = StaticallyLinkedModules;
02657 PSLFST slf;
02658 void (*fn)(void);
02659
02660 while( SLM->name ){
02661 for( slf = (PSLFST)SLM->table ; slf->name ; slf++ ){
02662 if( ! strcmp(slf->name,"_init") && (fn = (void *)slf->function) )fn();
02663 }
02664 SLM++;
02665 }
02666
02667 }
02668
02669
02670
02671
02672
02673
02674
02675
02676
02677
02678
02679
02680 void scriba_FinishStaticModules(void
02681 ){
02682
02683
02684 MODLIST *SLM = StaticallyLinkedModules;
02685 PSLFST slf;
02686 void (*fn)(void);
02687
02688 while( SLM->name ){
02689 for( slf = (PSLFST)SLM->table ; slf->name ; slf++ ){
02690 if( ! strcmp(slf->name,"_fini") && (fn = (void *)slf->function) )fn();
02691 }
02692 SLM++;
02693 }
02694
02695 }