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 #define _ROUTER_SOURCE_
00033
00034 #include <stdio.h>
00035 #define _STDIO_INCLUDED_
00036 #include <stdlib.h>
00037 #include <string.h>
00038
00039 #include "setup.h"
00040
00041 #include "argacces.h"
00042 #include "constant.h"
00043 #include "envrnmnt.h"
00044 #include "extnfunc.h"
00045 #include "filertr.h"
00046 #include "memalloc.h"
00047 #include "strngrtr.h"
00048 #include "sysdep.h"
00049
00050 #include "router.h"
00051
00052
00053
00054
00055
00056 static int QueryRouter(void *,char *,struct router *);
00057 static void DeallocateRouterData(void *);
00058
00059
00060
00061
00062 globle void InitializeDefaultRouters(
00063 void *theEnv)
00064 {
00065 AllocateEnvironmentData(theEnv,ROUTER_DATA,sizeof(struct routerData),DeallocateRouterData);
00066
00067 RouterData(theEnv)->CommandBufferInputCount = 0;
00068 RouterData(theEnv)->AwaitingInput = TRUE;
00069
00070 #if (! RUN_TIME)
00071 EnvDefineFunction2(theEnv,"exit", 'v', PTIEF ExitCommand, "ExitCommand", "*1i");
00072 #endif
00073 InitializeFileRouter(theEnv);
00074 InitializeStringRouter(theEnv);
00075 }
00076
00077
00078
00079
00080
00081 static void DeallocateRouterData(
00082 void *theEnv)
00083 {
00084 struct router *tmpPtr, *nextPtr;
00085
00086 tmpPtr = RouterData(theEnv)->ListOfRouters;
00087 while (tmpPtr != NULL)
00088 {
00089 nextPtr = tmpPtr->next;
00090 genfree(theEnv,tmpPtr->name,strlen(tmpPtr->name) + 1);
00091 rtn_struct(theEnv,router,tmpPtr);
00092 tmpPtr = nextPtr;
00093 }
00094 }
00095
00096
00097
00098
00099 globle int EnvPrintRouter(
00100 void *theEnv,
00101 char *logicalName,
00102 char *str)
00103 {
00104 struct router *currentPtr;
00105
00106
00107
00108
00109
00110
00111
00112
00113 if (((char *) RouterData(theEnv)->FastSaveFilePtr) == logicalName)
00114 {
00115 fprintf(RouterData(theEnv)->FastSaveFilePtr,"%s",str);
00116 return(2);
00117 }
00118
00119
00120
00121
00122
00123
00124 currentPtr = RouterData(theEnv)->ListOfRouters;
00125 while (currentPtr != NULL)
00126 {
00127 if ((currentPtr->printer != NULL) ? QueryRouter(theEnv,logicalName,currentPtr) : FALSE)
00128 {
00129 SetEnvironmentRouterContext(theEnv,currentPtr->context);
00130 if (currentPtr->environmentAware)
00131 { (*currentPtr->printer)(theEnv,logicalName,str); }
00132 else
00133 { ((int (*)(char *,char *)) (*currentPtr->printer))(logicalName,str); }
00134
00135 return(1);
00136 }
00137 currentPtr = currentPtr->next;
00138 }
00139
00140
00141
00142
00143
00144 if (strcmp(WERROR,logicalName) != 0) UnrecognizedRouterMessage(theEnv,logicalName);
00145 return(0);
00146 }
00147
00148
00149
00150
00151 globle int EnvGetcRouter(
00152 void *theEnv,
00153 char *logicalName)
00154 {
00155 struct router *currentPtr;
00156 int inchar;
00157
00158
00159
00160
00161
00162
00163
00164
00165 if (((char *) RouterData(theEnv)->FastLoadFilePtr) == logicalName)
00166 {
00167 inchar = getc(RouterData(theEnv)->FastLoadFilePtr);
00168
00169 if ((inchar == '\r') || (inchar == '\n'))
00170 {
00171 if (((char *) RouterData(theEnv)->FastLoadFilePtr) == RouterData(theEnv)->LineCountRouter)
00172 { IncrementLineCount(theEnv); }
00173 }
00174
00175
00176
00177 return(inchar);
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187 if (RouterData(theEnv)->FastCharGetRouter == logicalName)
00188 {
00189 inchar = (unsigned char) RouterData(theEnv)->FastCharGetString[RouterData(theEnv)->FastCharGetIndex];
00190
00191 RouterData(theEnv)->FastCharGetIndex++;
00192
00193 if (inchar == '\0') return(EOF);
00194
00195 if ((inchar == '\r') || (inchar == '\n'))
00196 {
00197 if (RouterData(theEnv)->FastCharGetRouter == RouterData(theEnv)->LineCountRouter)
00198 { IncrementLineCount(theEnv); }
00199 }
00200
00201 return(inchar);
00202 }
00203
00204
00205
00206
00207
00208
00209 currentPtr = RouterData(theEnv)->ListOfRouters;
00210 while (currentPtr != NULL)
00211 {
00212 if ((currentPtr->charget != NULL) ? QueryRouter(theEnv,logicalName,currentPtr) : FALSE)
00213 {
00214 SetEnvironmentRouterContext(theEnv,currentPtr->context);
00215 if (currentPtr->environmentAware)
00216 { inchar = (*currentPtr->charget)(theEnv,logicalName); }
00217 else
00218 { inchar = ((int (*)(char *)) (*currentPtr->charget))(logicalName); }
00219
00220 if ((inchar == '\r') || (inchar == '\n'))
00221 {
00222 if ((RouterData(theEnv)->LineCountRouter != NULL) &&
00223 (strcmp(logicalName,RouterData(theEnv)->LineCountRouter) == 0))
00224 { IncrementLineCount(theEnv); }
00225 }
00226
00227 return(inchar);
00228 }
00229 currentPtr = currentPtr->next;
00230 }
00231
00232
00233
00234
00235
00236 UnrecognizedRouterMessage(theEnv,logicalName);
00237 return(-1);
00238 }
00239
00240
00241
00242
00243 globle int EnvUngetcRouter(
00244 void *theEnv,
00245 int ch,
00246 char *logicalName)
00247 {
00248 struct router *currentPtr;
00249
00250
00251
00252
00253
00254
00255
00256
00257 if (((char *) RouterData(theEnv)->FastLoadFilePtr) == logicalName)
00258 {
00259 if ((ch == '\r') || (ch == '\n'))
00260 {
00261 if (((char *) RouterData(theEnv)->FastLoadFilePtr) == RouterData(theEnv)->LineCountRouter)
00262 { DecrementLineCount(theEnv); }
00263 }
00264
00265 return(ungetc(ch,RouterData(theEnv)->FastLoadFilePtr));
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275 if (RouterData(theEnv)->FastCharGetRouter == logicalName)
00276 {
00277 if ((ch == '\r') || (ch == '\n'))
00278 {
00279 if (RouterData(theEnv)->FastCharGetRouter == RouterData(theEnv)->LineCountRouter)
00280 { DecrementLineCount(theEnv); }
00281 }
00282
00283 if (RouterData(theEnv)->FastCharGetIndex > 0) RouterData(theEnv)->FastCharGetIndex--;
00284 return(ch);
00285 }
00286
00287
00288
00289
00290
00291
00292 currentPtr = RouterData(theEnv)->ListOfRouters;
00293 while (currentPtr != NULL)
00294 {
00295 if ((currentPtr->charunget != NULL) ? QueryRouter(theEnv,logicalName,currentPtr) : FALSE)
00296 {
00297 if ((ch == '\r') || (ch == '\n'))
00298 {
00299 if ((RouterData(theEnv)->LineCountRouter != NULL) &&
00300 (strcmp(logicalName,RouterData(theEnv)->LineCountRouter) == 0))
00301 { DecrementLineCount(theEnv); }
00302 }
00303
00304 SetEnvironmentRouterContext(theEnv,currentPtr->context);
00305 if (currentPtr->environmentAware)
00306 { return((*currentPtr->charunget)(theEnv,ch,logicalName)); }
00307 else
00308 { return(((int (*)(int,char *)) (*currentPtr->charunget))(ch,logicalName)); }
00309 }
00310
00311 currentPtr = currentPtr->next;
00312 }
00313
00314
00315
00316
00317
00318 UnrecognizedRouterMessage(theEnv,logicalName);
00319 return(-1);
00320 }
00321
00322
00323
00324
00325 globle void ExitCommand(
00326 void *theEnv)
00327 {
00328 int argCnt;
00329 int status;
00330
00331 if ((argCnt = EnvArgCountCheck(theEnv,"exit",NO_MORE_THAN,1)) == -1) return;
00332 if (argCnt == 0)
00333 { EnvExitRouter(theEnv,EXIT_SUCCESS); }
00334 else
00335 {
00336 status = (int) EnvRtnLong(theEnv,1);
00337 if (GetEvaluationError(theEnv)) return;
00338 EnvExitRouter(theEnv,status);
00339 }
00340
00341 return;
00342 }
00343
00344
00345
00346
00347
00348 globle void EnvExitRouter(
00349 void *theEnv,
00350 int num)
00351 {
00352 struct router *currentPtr, *nextPtr;
00353
00354 RouterData(theEnv)->Abort = FALSE;
00355 currentPtr = RouterData(theEnv)->ListOfRouters;
00356 while (currentPtr != NULL)
00357 {
00358 nextPtr = currentPtr->next;
00359 if (currentPtr->active == TRUE)
00360 {
00361 if (currentPtr->exiter != NULL)
00362 {
00363 SetEnvironmentRouterContext(theEnv,currentPtr->context);
00364 if (currentPtr->environmentAware)
00365 { (*currentPtr->exiter)(theEnv,num); }
00366 else
00367 { ((int (*)(int))(*currentPtr->exiter))(num); }
00368 }
00369 }
00370 currentPtr = nextPtr;
00371 }
00372
00373 if (RouterData(theEnv)->Abort) return;
00374 genexit(theEnv,num);
00375 }
00376
00377
00378
00379
00380
00381 globle void AbortExit(
00382 void *theEnv)
00383 {
00384 RouterData(theEnv)->Abort = TRUE;
00385 }
00386
00387 #if ALLOW_ENVIRONMENT_GLOBALS
00388
00389
00390
00391 globle intBool AddRouter(
00392 char *routerName,
00393 int priority,
00394 int (*queryFunction)(char *),
00395 int (*printFunction)(char *,char *),
00396 int (*getcFunction)(char *),
00397 int (*ungetcFunction)(int,char *),
00398 int (*exitFunction)(int))
00399 {
00400 struct router *newPtr, *lastPtr, *currentPtr;
00401 void *theEnv;
00402 char *nameCopy;
00403
00404 theEnv = GetCurrentEnvironment();
00405
00406 newPtr = get_struct(theEnv,router);
00407
00408 nameCopy = (char *) genalloc(theEnv,strlen(routerName) + 1);
00409 genstrcpy(nameCopy,routerName);
00410 newPtr->name = nameCopy;
00411
00412 newPtr->active = TRUE;
00413 newPtr->environmentAware = FALSE;
00414 newPtr->priority = priority;
00415 newPtr->context = NULL;
00416 newPtr->query = (int (*)(void *,char *)) queryFunction;
00417 newPtr->printer = (int (*)(void *,char *,char *)) printFunction;
00418 newPtr->exiter = (int (*)(void *,int)) exitFunction;
00419 newPtr->charget = (int (*)(void *,char *)) getcFunction;
00420 newPtr->charunget = (int (*)(void *,int,char *)) ungetcFunction;
00421 newPtr->next = NULL;
00422
00423 if (RouterData(theEnv)->ListOfRouters == NULL)
00424 {
00425 RouterData(theEnv)->ListOfRouters = newPtr;
00426 return(1);
00427 }
00428
00429 lastPtr = NULL;
00430 currentPtr = RouterData(theEnv)->ListOfRouters;
00431 while ((currentPtr != NULL) ? (priority < currentPtr->priority) : FALSE)
00432 {
00433 lastPtr = currentPtr;
00434 currentPtr = currentPtr->next;
00435 }
00436
00437 if (lastPtr == NULL)
00438 {
00439 newPtr->next = RouterData(theEnv)->ListOfRouters;
00440 RouterData(theEnv)->ListOfRouters = newPtr;
00441 }
00442 else
00443 {
00444 newPtr->next = currentPtr;
00445 lastPtr->next = newPtr;
00446 }
00447
00448 return(1);
00449 }
00450 #endif
00451
00452
00453
00454
00455 globle intBool EnvAddRouter(
00456 void *theEnv,
00457 char *routerName,
00458 int priority,
00459 int (*queryFunction)(void *,char *),
00460 int (*printFunction)(void *,char *,char *),
00461 int (*getcFunction)(void *,char *),
00462 int (*ungetcFunction)(void *,int,char *),
00463 int (*exitFunction)(void *,int))
00464 {
00465 return EnvAddRouterWithContext(theEnv,routerName,priority,
00466 queryFunction,printFunction,getcFunction,
00467 ungetcFunction,exitFunction,NULL);
00468 }
00469
00470
00471
00472
00473 globle intBool EnvAddRouterWithContext(
00474 void *theEnv,
00475 char *routerName,
00476 int priority,
00477 int (*queryFunction)(void *,char *),
00478 int (*printFunction)(void *,char *,char *),
00479 int (*getcFunction)(void *,char *),
00480 int (*ungetcFunction)(void *,int,char *),
00481 int (*exitFunction)(void *,int),
00482 void *context)
00483 {
00484 struct router *newPtr, *lastPtr, *currentPtr;
00485 char *nameCopy;
00486
00487 newPtr = get_struct(theEnv,router);
00488
00489 nameCopy = (char *) genalloc(theEnv,strlen(routerName) + 1);
00490 genstrcpy(nameCopy,routerName);
00491 newPtr->name = nameCopy;
00492
00493 newPtr->active = TRUE;
00494 newPtr->environmentAware = TRUE;
00495 newPtr->context = context;
00496 newPtr->priority = priority;
00497 newPtr->query = queryFunction;
00498 newPtr->printer = printFunction;
00499 newPtr->exiter = exitFunction;
00500 newPtr->charget = getcFunction;
00501 newPtr->charunget = ungetcFunction;
00502 newPtr->next = NULL;
00503
00504 if (RouterData(theEnv)->ListOfRouters == NULL)
00505 {
00506 RouterData(theEnv)->ListOfRouters = newPtr;
00507 return(1);
00508 }
00509
00510 lastPtr = NULL;
00511 currentPtr = RouterData(theEnv)->ListOfRouters;
00512 while ((currentPtr != NULL) ? (priority < currentPtr->priority) : FALSE)
00513 {
00514 lastPtr = currentPtr;
00515 currentPtr = currentPtr->next;
00516 }
00517
00518 if (lastPtr == NULL)
00519 {
00520 newPtr->next = RouterData(theEnv)->ListOfRouters;
00521 RouterData(theEnv)->ListOfRouters = newPtr;
00522 }
00523 else
00524 {
00525 newPtr->next = currentPtr;
00526 lastPtr->next = newPtr;
00527 }
00528
00529 return(1);
00530 }
00531
00532
00533
00534
00535 globle int EnvDeleteRouter(
00536 void *theEnv,
00537 char *routerName)
00538 {
00539 struct router *currentPtr, *lastPtr;
00540
00541 currentPtr = RouterData(theEnv)->ListOfRouters;
00542 lastPtr = NULL;
00543
00544 while (currentPtr != NULL)
00545 {
00546 if (strcmp(currentPtr->name,routerName) == 0)
00547 {
00548 genfree(theEnv,currentPtr->name,strlen(currentPtr->name) + 1);
00549 if (lastPtr == NULL)
00550 {
00551 RouterData(theEnv)->ListOfRouters = currentPtr->next;
00552 rm(theEnv,currentPtr,(int) sizeof(struct router));
00553 return(1);
00554 }
00555 lastPtr->next = currentPtr->next;
00556 rm(theEnv,currentPtr,(int) sizeof(struct router));
00557 return(1);
00558 }
00559 lastPtr = currentPtr;
00560 currentPtr = currentPtr->next;
00561 }
00562
00563 return(0);
00564 }
00565
00566
00567
00568
00569 globle int QueryRouters(
00570 void *theEnv,
00571 char *logicalName)
00572 {
00573 struct router *currentPtr;
00574
00575 currentPtr = RouterData(theEnv)->ListOfRouters;
00576 while (currentPtr != NULL)
00577 {
00578 if (QueryRouter(theEnv,logicalName,currentPtr) == TRUE) return(TRUE);
00579 currentPtr = currentPtr->next;
00580 }
00581
00582 return(FALSE);
00583 }
00584
00585
00586
00587
00588
00589 static int QueryRouter(
00590 void *theEnv,
00591 char *logicalName,
00592 struct router *currentPtr)
00593 {
00594
00595
00596
00597
00598 if (currentPtr->active == FALSE)
00599 { return(FALSE); }
00600
00601
00602
00603
00604
00605 if (currentPtr->query == NULL) return(FALSE);
00606
00607
00608
00609
00610
00611
00612 SetEnvironmentRouterContext(theEnv,currentPtr->context);
00613 if (currentPtr->environmentAware)
00614 {
00615 if ((*currentPtr->query)(theEnv,logicalName) == TRUE)
00616 { return(TRUE); }
00617 }
00618 else
00619 {
00620 if (((int (*)(char *)) (*currentPtr->query))(logicalName) == TRUE)
00621 { return(TRUE); }
00622 }
00623
00624 return(FALSE);
00625 }
00626
00627
00628
00629
00630 globle int EnvDeactivateRouter(
00631 void *theEnv,
00632 char *routerName)
00633 {
00634 struct router *currentPtr;
00635
00636 currentPtr = RouterData(theEnv)->ListOfRouters;
00637
00638 while (currentPtr != NULL)
00639 {
00640 if (strcmp(currentPtr->name,routerName) == 0)
00641 {
00642 currentPtr->active = FALSE;
00643 return(TRUE);
00644 }
00645 currentPtr = currentPtr->next;
00646 }
00647
00648 return(FALSE);
00649 }
00650
00651
00652
00653
00654 globle int EnvActivateRouter(
00655 void *theEnv,
00656 char *routerName)
00657 {
00658 struct router *currentPtr;
00659
00660 currentPtr = RouterData(theEnv)->ListOfRouters;
00661
00662 while (currentPtr != NULL)
00663 {
00664 if (strcmp(currentPtr->name,routerName) == 0)
00665 {
00666 currentPtr->active = TRUE;
00667 return(TRUE);
00668 }
00669 currentPtr = currentPtr->next;
00670 }
00671
00672 return(FALSE);
00673 }
00674
00675
00676
00677
00678 globle void SetFastLoad(
00679 void *theEnv,
00680 FILE *filePtr)
00681 {
00682 RouterData(theEnv)->FastLoadFilePtr = filePtr;
00683 }
00684
00685
00686
00687
00688 globle void SetFastSave(
00689 void *theEnv,
00690 FILE *filePtr)
00691 {
00692 RouterData(theEnv)->FastSaveFilePtr = filePtr;
00693 }
00694
00695
00696
00697
00698 globle FILE *GetFastLoad(
00699 void *theEnv)
00700 {
00701 return(RouterData(theEnv)->FastLoadFilePtr);
00702 }
00703
00704
00705
00706
00707 globle FILE *GetFastSave(
00708 void *theEnv)
00709 {
00710 return(RouterData(theEnv)->FastSaveFilePtr);
00711 }
00712
00713
00714
00715
00716
00717 globle void UnrecognizedRouterMessage(
00718 void *theEnv,
00719 char *logicalName)
00720 {
00721 PrintErrorID(theEnv,"ROUTER",1,FALSE);
00722 EnvPrintRouter(theEnv,WERROR,"Logical name ");
00723 EnvPrintRouter(theEnv,WERROR,logicalName);
00724 EnvPrintRouter(theEnv,WERROR," was not recognized by any routers\n");
00725 }
00726
00727
00728
00729
00730 globle int PrintNRouter(
00731 void *theEnv,
00732 char *logicalName,
00733 char *str,
00734 unsigned long length)
00735 {
00736 char *tempStr;
00737 int rv;
00738
00739 tempStr = (char *) genalloc(theEnv,length+1);
00740 genstrncpy(tempStr,str,length);
00741 tempStr[length] = 0;
00742 rv = EnvPrintRouter(theEnv,logicalName,tempStr);
00743 genfree(theEnv,tempStr,length+1);
00744 return(rv);
00745 }