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 #define _PRNTUTIL_SOURCE_
00032
00033 #include <stdio.h>
00034 #define _STDIO_INCLUDED_
00035 #include <string.h>
00036
00037 #include "setup.h"
00038
00039 #include "constant.h"
00040 #include "envrnmnt.h"
00041 #include "symbol.h"
00042 #include "utility.h"
00043 #include "evaluatn.h"
00044 #include "argacces.h"
00045 #include "router.h"
00046 #include "multifun.h"
00047 #include "factmngr.h"
00048 #include "inscom.h"
00049 #include "insmngr.h"
00050 #include "memalloc.h"
00051 #include "sysdep.h"
00052
00053 #include "prntutil.h"
00054
00055
00056
00057
00058
00059 globle void InitializePrintUtilityData(
00060 void *theEnv)
00061 {
00062 AllocateEnvironmentData(theEnv,PRINT_UTILITY_DATA,sizeof(struct printUtilityData),NULL);
00063 }
00064
00065
00066
00067
00068
00069
00070 globle void PrintInChunks(
00071 void *theEnv,
00072 char *logicalName,
00073 char *bigString)
00074 {
00075 char tc, *subString;
00076
00077 subString = bigString;
00078
00079 if (subString == NULL) return;
00080
00081 while (((int) strlen(subString)) > 500)
00082 {
00083 if (EvaluationData(theEnv)->HaltExecution) return;
00084 tc = subString[500];
00085 subString[500] = EOS;
00086 EnvPrintRouter(theEnv,logicalName,subString);
00087 subString[500] = tc;
00088 subString += 500;
00089 }
00090
00091 EnvPrintRouter(theEnv,logicalName,subString);
00092 }
00093
00094
00095
00096
00097 globle void PrintFloat(
00098 void *theEnv,
00099 char *fileid,
00100 double number)
00101 {
00102 char *theString;
00103
00104 theString = FloatToString(theEnv,number);
00105 EnvPrintRouter(theEnv,fileid,theString);
00106 }
00107
00108
00109
00110
00111 globle void PrintLongInteger(
00112 void *theEnv,
00113 char *logicalName,
00114 long long number)
00115 {
00116 char printBuffer[32];
00117
00118 gensprintf(printBuffer,"%lld",number);
00119 EnvPrintRouter(theEnv,logicalName,printBuffer);
00120 }
00121
00122
00123
00124
00125 globle void PrintAtom(
00126 void *theEnv,
00127 char *logicalName,
00128 int type,
00129 void *value)
00130 {
00131 struct externalAddressHashNode *theAddress;
00132 char buffer[20];
00133
00134 switch (type)
00135 {
00136 case FLOAT:
00137 PrintFloat(theEnv,logicalName,ValueToDouble(value));
00138 break;
00139 case INTEGER:
00140 PrintLongInteger(theEnv,logicalName,ValueToLong(value));
00141 break;
00142 case SYMBOL:
00143 EnvPrintRouter(theEnv,logicalName,ValueToString(value));
00144 break;
00145 case STRING:
00146 if (PrintUtilityData(theEnv)->PreserveEscapedCharacters)
00147 { EnvPrintRouter(theEnv,logicalName,StringPrintForm(theEnv,ValueToString(value))); }
00148 else
00149 {
00150 EnvPrintRouter(theEnv,logicalName,"\"");
00151 EnvPrintRouter(theEnv,logicalName,ValueToString(value));
00152 EnvPrintRouter(theEnv,logicalName,"\"");
00153 }
00154 break;
00155
00156 case EXTERNAL_ADDRESS:
00157 theAddress = (struct externalAddressHashNode *) value;
00158
00159 if (PrintUtilityData(theEnv)->AddressesToStrings) EnvPrintRouter(theEnv,logicalName,"\"");
00160
00161 if ((EvaluationData(theEnv)->ExternalAddressTypes[theAddress->type] != NULL) &&
00162 (EvaluationData(theEnv)->ExternalAddressTypes[theAddress->type]->longPrintFunction != NULL))
00163 { (*EvaluationData(theEnv)->ExternalAddressTypes[theAddress->type]->longPrintFunction)(theEnv,logicalName,value); }
00164 else
00165 {
00166 EnvPrintRouter(theEnv,logicalName,"<Pointer-");
00167
00168 gensprintf(buffer,"%d-",theAddress->type);
00169 EnvPrintRouter(theEnv,logicalName,buffer);
00170
00171 gensprintf(buffer,"%p",ValueToExternalAddress(value));
00172 EnvPrintRouter(theEnv,logicalName,buffer);
00173 EnvPrintRouter(theEnv,logicalName,">");
00174 }
00175
00176 if (PrintUtilityData(theEnv)->AddressesToStrings) EnvPrintRouter(theEnv,logicalName,"\"");
00177 break;
00178
00179 #if OBJECT_SYSTEM
00180 case INSTANCE_NAME:
00181 EnvPrintRouter(theEnv,logicalName,"[");
00182 EnvPrintRouter(theEnv,logicalName,ValueToString(value));
00183 EnvPrintRouter(theEnv,logicalName,"]");
00184 break;
00185 #endif
00186
00187 case RVOID:
00188 break;
00189
00190 default:
00191 if (EvaluationData(theEnv)->PrimitivesArray[type] == NULL) break;
00192 if (EvaluationData(theEnv)->PrimitivesArray[type]->longPrintFunction == NULL)
00193 {
00194 EnvPrintRouter(theEnv,logicalName,"<unknown atom type>");
00195 break;
00196 }
00197 (*EvaluationData(theEnv)->PrimitivesArray[type]->longPrintFunction)(theEnv,logicalName,value);
00198 break;
00199 }
00200 }
00201
00202
00203
00204
00205
00206
00207 globle void PrintTally(
00208 void *theEnv,
00209 char *logicalName,
00210 long long count,
00211 char *singular,
00212 char *plural)
00213 {
00214 if (count == 0) return;
00215
00216 EnvPrintRouter(theEnv,logicalName,"For a total of ");
00217 PrintLongInteger(theEnv,logicalName,count);
00218 EnvPrintRouter(theEnv,logicalName," ");
00219
00220 if (count == 1) EnvPrintRouter(theEnv,logicalName,singular);
00221 else EnvPrintRouter(theEnv,logicalName,plural);
00222
00223 EnvPrintRouter(theEnv,logicalName,".\n");
00224 }
00225
00226
00227
00228
00229
00230 globle void PrintErrorID(
00231 void *theEnv,
00232 char *module,
00233 int errorID,
00234 int printCR)
00235 {
00236 if (printCR) EnvPrintRouter(theEnv,WERROR,"\n");
00237 EnvPrintRouter(theEnv,WERROR,"[");
00238 EnvPrintRouter(theEnv,WERROR,module);
00239 PrintLongInteger(theEnv,WERROR,(long int) errorID);
00240 EnvPrintRouter(theEnv,WERROR,"] ");
00241 }
00242
00243
00244
00245
00246
00247 globle void PrintWarningID(
00248 void *theEnv,
00249 char *module,
00250 int warningID,
00251 int printCR)
00252 {
00253 if (printCR) EnvPrintRouter(theEnv,WWARNING,"\n");
00254 EnvPrintRouter(theEnv,WWARNING,"[");
00255 EnvPrintRouter(theEnv,WWARNING,module);
00256 PrintLongInteger(theEnv,WWARNING,(long int) warningID);
00257 EnvPrintRouter(theEnv,WWARNING,"] WARNING: ");
00258 }
00259
00260
00261
00262
00263
00264 globle void CantFindItemErrorMessage(
00265 void *theEnv,
00266 char *itemType,
00267 char *itemName)
00268 {
00269 PrintErrorID(theEnv,"PRNTUTIL",1,FALSE);
00270 EnvPrintRouter(theEnv,WERROR,"Unable to find ");
00271 EnvPrintRouter(theEnv,WERROR,itemType);
00272 EnvPrintRouter(theEnv,WERROR," ");
00273 EnvPrintRouter(theEnv,WERROR,itemName);
00274 EnvPrintRouter(theEnv,WERROR,".\n");
00275 }
00276
00277
00278
00279
00280
00281 globle void CantFindItemInFunctionErrorMessage(
00282 void *theEnv,
00283 char *itemType,
00284 char *itemName,
00285 char *func)
00286 {
00287 PrintErrorID(theEnv,"PRNTUTIL",1,FALSE);
00288 EnvPrintRouter(theEnv,WERROR,"Unable to find ");
00289 EnvPrintRouter(theEnv,WERROR,itemType);
00290 EnvPrintRouter(theEnv,WERROR," ");
00291 EnvPrintRouter(theEnv,WERROR,itemName);
00292 EnvPrintRouter(theEnv,WERROR," in function ");
00293 EnvPrintRouter(theEnv,WERROR,func);
00294 EnvPrintRouter(theEnv,WERROR,".\n");
00295 }
00296
00297
00298
00299
00300
00301 globle void CantDeleteItemErrorMessage(
00302 void *theEnv,
00303 char *itemType,
00304 char *itemName)
00305 {
00306 PrintErrorID(theEnv,"PRNTUTIL",4,FALSE);
00307 EnvPrintRouter(theEnv,WERROR,"Unable to delete ");
00308 EnvPrintRouter(theEnv,WERROR,itemType);
00309 EnvPrintRouter(theEnv,WERROR," ");
00310 EnvPrintRouter(theEnv,WERROR,itemName);
00311 EnvPrintRouter(theEnv,WERROR,".\n");
00312 }
00313
00314
00315
00316
00317
00318 globle void AlreadyParsedErrorMessage(
00319 void *theEnv,
00320 char *itemType,
00321 char *itemName)
00322 {
00323 PrintErrorID(theEnv,"PRNTUTIL",5,TRUE);
00324 EnvPrintRouter(theEnv,WERROR,"The ");
00325 if (itemType != NULL) EnvPrintRouter(theEnv,WERROR,itemType);
00326 if (itemName != NULL) EnvPrintRouter(theEnv,WERROR,itemName);
00327 EnvPrintRouter(theEnv,WERROR," has already been parsed.\n");
00328 }
00329
00330
00331
00332
00333 globle void SyntaxErrorMessage(
00334 void *theEnv,
00335 char *location)
00336 {
00337 PrintErrorID(theEnv,"PRNTUTIL",2,TRUE);
00338 EnvPrintRouter(theEnv,WERROR,"Syntax Error");
00339 if (location != NULL)
00340 {
00341 EnvPrintRouter(theEnv,WERROR,": Check appropriate syntax for ");
00342 EnvPrintRouter(theEnv,WERROR,location);
00343 }
00344
00345 EnvPrintRouter(theEnv,WERROR,".\n");
00346 SetEvaluationError(theEnv,TRUE);
00347 }
00348
00349
00350
00351
00352
00353
00354 globle void LocalVariableErrorMessage(
00355 void *theEnv,
00356 char *byWhat)
00357 {
00358 PrintErrorID(theEnv,"PRNTUTIL",6,TRUE);
00359 EnvPrintRouter(theEnv,WERROR,"Local variables can not be accessed by ");
00360 EnvPrintRouter(theEnv,WERROR,byWhat);
00361 EnvPrintRouter(theEnv,WERROR,".\n");
00362 }
00363
00364
00365
00366
00367
00368 globle void SystemError(
00369 void *theEnv,
00370 char *module,
00371 int errorID)
00372 {
00373 PrintErrorID(theEnv,"PRNTUTIL",3,TRUE);
00374
00375 EnvPrintRouter(theEnv,WERROR,"\n*** ");
00376 EnvPrintRouter(theEnv,WERROR,APPLICATION_NAME);
00377 EnvPrintRouter(theEnv,WERROR," SYSTEM ERROR ***\n");
00378
00379 EnvPrintRouter(theEnv,WERROR,"ID = ");
00380 EnvPrintRouter(theEnv,WERROR,module);
00381 PrintLongInteger(theEnv,WERROR,(long int) errorID);
00382 EnvPrintRouter(theEnv,WERROR,"\n");
00383
00384 EnvPrintRouter(theEnv,WERROR,APPLICATION_NAME);
00385 EnvPrintRouter(theEnv,WERROR," data structures are in an inconsistent or corrupted state.\n");
00386 EnvPrintRouter(theEnv,WERROR,"This error may have occurred from errors in user defined code.\n");
00387 EnvPrintRouter(theEnv,WERROR,"**************************\n");
00388 }
00389
00390
00391
00392
00393
00394 globle void DivideByZeroErrorMessage(
00395 void *theEnv,
00396 char *functionName)
00397 {
00398 PrintErrorID(theEnv,"PRNTUTIL",7,FALSE);
00399 EnvPrintRouter(theEnv,WERROR,"Attempt to divide by zero in ");
00400 EnvPrintRouter(theEnv,WERROR,functionName);
00401 EnvPrintRouter(theEnv,WERROR," function.\n");
00402 }
00403
00404
00405
00406
00407 globle char *FloatToString(
00408 void *theEnv,
00409 double number)
00410 {
00411 char floatString[40];
00412 int i;
00413 char x;
00414 void *thePtr;
00415
00416 gensprintf(floatString,"%.15g",number);
00417
00418 for (i = 0; (x = floatString[i]) != '\0'; i++)
00419 {
00420 if ((x == '.') || (x == 'e'))
00421 {
00422 thePtr = EnvAddSymbol(theEnv,floatString);
00423 return(ValueToString(thePtr));
00424 }
00425 }
00426
00427 genstrcat(floatString,".0");
00428
00429 thePtr = EnvAddSymbol(theEnv,floatString);
00430 return(ValueToString(thePtr));
00431 }
00432
00433
00434
00435
00436 globle char *LongIntegerToString(
00437 void *theEnv,
00438 long long number)
00439 {
00440 char buffer[50];
00441 void *thePtr;
00442
00443 gensprintf(buffer,"%lld",number);
00444
00445 thePtr = EnvAddSymbol(theEnv,buffer);
00446 return(ValueToString(thePtr));
00447 }
00448
00449
00450
00451
00452 globle char *DataObjectToString(
00453 void *theEnv,
00454 DATA_OBJECT *theDO)
00455 {
00456 void *thePtr;
00457 char *theString, *newString;
00458 char *prefix, *postfix;
00459 size_t length;
00460 struct externalAddressHashNode *theAddress;
00461 char buffer[30];
00462
00463 switch (GetpType(theDO))
00464 {
00465 case MULTIFIELD:
00466 prefix = "(";
00467 theString = ValueToString(ImplodeMultifield(theEnv,theDO));
00468 postfix = ")";
00469 break;
00470
00471 case STRING:
00472 prefix = "\"";
00473 theString = DOPToString(theDO);
00474 postfix = "\"";
00475 break;
00476
00477 case INSTANCE_NAME:
00478 prefix = "[";
00479 theString = DOPToString(theDO);
00480 postfix = "]";
00481 break;
00482
00483 case SYMBOL:
00484 return(DOPToString(theDO));
00485
00486 case FLOAT:
00487 return(FloatToString(theEnv,DOPToDouble(theDO)));
00488
00489 case INTEGER:
00490 return(LongIntegerToString(theEnv,DOPToLong(theDO)));
00491
00492 case RVOID:
00493 return("");
00494
00495 #if OBJECT_SYSTEM
00496 case INSTANCE_ADDRESS:
00497 thePtr = DOPToPointer(theDO);
00498
00499 if (thePtr == (void *) &InstanceData(theEnv)->DummyInstance)
00500 { return("<Dummy Instance>"); }
00501
00502 if (((struct instance *) thePtr)->garbage)
00503 {
00504 prefix = "<Stale Instance-";
00505 theString = ValueToString(((struct instance *) thePtr)->name);
00506 postfix = ">";
00507 }
00508 else
00509 {
00510 prefix = "<Instance-";
00511 theString = ValueToString(GetFullInstanceName(theEnv,(INSTANCE_TYPE *) thePtr));
00512 postfix = ">";
00513 }
00514
00515 break;
00516 #endif
00517
00518 case EXTERNAL_ADDRESS:
00519 theAddress = (struct externalAddressHashNode *) DOPToPointer(theDO);
00520
00521 gensprintf(buffer,"<Pointer-%d-%p>",(int) theAddress->type,DOPToExternalAddress(theDO));
00522 thePtr = EnvAddSymbol(theEnv,buffer);
00523 return(ValueToString(thePtr));
00524
00525 #if DEFTEMPLATE_CONSTRUCT
00526 case FACT_ADDRESS:
00527 if (DOPToPointer(theDO) == (void *) &FactData(theEnv)->DummyFact)
00528 { return("<Dummy Fact>"); }
00529
00530 thePtr = DOPToPointer(theDO);
00531 gensprintf(buffer,"<Fact-%lld>",((struct fact *) thePtr)->factIndex);
00532 thePtr = EnvAddSymbol(theEnv,buffer);
00533 return(ValueToString(thePtr));
00534 #endif
00535
00536 default:
00537 return("UNK");
00538 }
00539
00540 length = strlen(prefix) + strlen(theString) + strlen(postfix) + 1;
00541 newString = (char *) genalloc(theEnv,length);
00542 newString[0] = '\0';
00543 genstrcat(newString,prefix);
00544 genstrcat(newString,theString);
00545 genstrcat(newString,postfix);
00546 thePtr = EnvAddSymbol(theEnv,newString);
00547 genfree(theEnv,newString,length);
00548 return(ValueToString(thePtr));
00549 }
00550
00551
00552
00553
00554
00555 globle void SalienceInformationError(
00556 void *theEnv,
00557 char *constructType,
00558 char *constructName)
00559 {
00560 PrintErrorID(theEnv,"PRNTUTIL",8,TRUE);
00561 EnvPrintRouter(theEnv,WERROR,"This error occurred while evaluating the salience");
00562 if (constructName != NULL)
00563 {
00564 EnvPrintRouter(theEnv,WERROR," for ");
00565 EnvPrintRouter(theEnv,WERROR,constructType);
00566 EnvPrintRouter(theEnv,WERROR," ");
00567 EnvPrintRouter(theEnv,WERROR,constructName);
00568 }
00569 EnvPrintRouter(theEnv,WERROR,".\n");
00570 }
00571
00572
00573
00574
00575
00576
00577 globle void SalienceRangeError(
00578 void *theEnv,
00579 int min,
00580 int max)
00581 {
00582 PrintErrorID(theEnv,"PRNTUTIL",9,TRUE);
00583 EnvPrintRouter(theEnv,WERROR,"Salience value out of range ");
00584 PrintLongInteger(theEnv,WERROR,(long int) min);
00585 EnvPrintRouter(theEnv,WERROR," to ");
00586 PrintLongInteger(theEnv,WERROR,(long int) max);
00587 EnvPrintRouter(theEnv,WERROR,".\n");
00588 }
00589
00590
00591
00592
00593
00594 globle void SalienceNonIntegerError(
00595 void *theEnv)
00596 {
00597 PrintErrorID(theEnv,"PRNTUTIL",10,TRUE);
00598 EnvPrintRouter(theEnv,WERROR,"Salience value must be an integer value.\n");
00599 }
00600
00601
00602
00603
00604
00605
00606
00607 globle void SlotExistError(
00608 void *theEnv,
00609 char *sname,
00610 char *func)
00611 {
00612 PrintErrorID(theEnv,"INSFUN",3,FALSE);
00613 EnvPrintRouter(theEnv,WERROR,"No such slot ");
00614 EnvPrintRouter(theEnv,WERROR,sname);
00615 EnvPrintRouter(theEnv,WERROR," in function ");
00616 EnvPrintRouter(theEnv,WERROR,func);
00617 EnvPrintRouter(theEnv,WERROR,".\n");
00618 SetEvaluationError(theEnv,TRUE);
00619 }