home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / IRITS.ZIP / IRIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  12.5 KB  |  391 lines

  1. /*****************************************************************************
  2. *   Main module of "Irit" - the 3d polygonal solid modeller.             *
  3. ******************************************************************************
  4. * Usage:                                     *
  5. *   Irit [-z]                                     *
  6. *                                         *
  7. * Written by:  Gershon Elber                Ver 2.1, Apr. 1990   *
  8. *****************************************************************************/
  9.  
  10. #ifdef __MSDOS__
  11. #include <stdlib.h>
  12. #include <graphics.h>
  13. #include <conio.h>
  14. #include <dir.h>
  15. #include <dos.h>
  16. #endif /* __MSDOS__ */
  17.  
  18. #include <stdio.h>
  19. #include <signal.h>
  20. #include <string.h>
  21. #include "program.h"
  22. #include "dataprsg.h"
  23. #include "inptprsg.h"
  24. #include "ctrl-brk.h"
  25. #include "config.h"
  26. #include "matherr.h"
  27. #include "windowsg.h"
  28. #include "objects.h"
  29. #include "dosintrg.h"
  30.  
  31. #ifdef __MSDOS__
  32. #include "mousedrv.h"
  33. #else
  34. #include "xgraphic.h"
  35. #endif /* __MSDOS__ */
  36.  
  37. #include "graphgng.h"
  38.  
  39. #ifdef __MSDOS__
  40. static char *VersionStr = "Irit        IBMPC " VERSION
  41.         "    Gershon Elber,    " __DATE__ ",   " __TIME__ "\n"
  42.         "(C) Copyright 1989 Gershon Elber, Non commercial use only.";
  43. #else
  44. static char *VersionStr = "Irit        Unix Vertion 2.1, Gershon Elber,\n\
  45.         (C) Copyright 1989 Gershon Elber, Non commercial use only.";
  46. #endif /* __MSDOS__ */
  47.  
  48. static char *CtrlStr = "Irit [-z] [file.irt]";
  49.  
  50. char *HelpFileName = "irit.hlp";
  51.  
  52. char *PrgmHeader = "Irit - the polygonal solid modeller";
  53. #ifdef __MSDOS__
  54. char *CopyRight  = "(C) Copyright 1989 Gershon Elber,  IBMPC " VERSION ",   "
  55.         __DATE__;
  56. #else
  57. char *CopyRight  = "(C) Copyright 1989 Gershon Elber,  Unix  Version 2.1";
  58. #endif /* __MSDOS__ */
  59. char *AuthorName = "Written by Gershon Elber";
  60.  
  61. struct ObjectStruct *GlblObjList = NULL;   /* All objects defined on system. */
  62.  
  63. char CurrentWorkingDir[LINE_LEN];      /* Save start CWD to recover on exit. */
  64.  
  65. int  GlblFatalError = FALSE;      /* True if disaster in system - must quit! */
  66. int  PrintLogFile = FALSE;         /* If TRUE everything goes to log file. */
  67.  
  68. FILE *LogFile = NULL;
  69.  
  70. jmp_buf LongJumpBuffer;                      /* Used in error recovery. */
  71.  
  72. /* The following are setable variables (via configuration file poly3d-h.cfg).*/
  73. #ifdef __MSDOS__
  74. int  MouseExists = FALSE,
  75.      GraphDriver = DETECT;
  76. #else
  77. int  MouseExists = TRUE;
  78. #endif /* __MSDOS__ */
  79.  
  80. int  DelayConstant = DELAY_CONST_DEFAULT,
  81.      LoadColor = DEFAULT_LOAD_COLOR,
  82.      BoolColor = DEFAULT_BOOL_COLOR,
  83.      ICrvColor = DEFAULT_ICRV_COLOR,
  84.      PrimColor = DEFAULT_PRIM_COLOR;
  85. char *StartFileName = "",
  86.      *LogFileName = "",
  87.      *EditPrgm = "";
  88.  
  89. static ConfigStruct SetUp[] =
  90. {
  91. #ifdef __MSDOS__
  92.   { "Mouse",        (VoidPtr) &MouseExists,        SU_BOOLEAN_TYPE },
  93.   { "GraphDriver",    (VoidPtr) &GraphDriver,        SU_INTEGER_TYPE },
  94. #endif /* __MSDOS__ */
  95.   { "DelayConst",    (VoidPtr) &DelayConstant,    SU_INTEGER_TYPE },
  96.   { "LoadColor",    (VoidPtr) &LoadColor,        SU_INTEGER_TYPE },
  97.   { "BoolColor",    (VoidPtr) &BoolColor,        SU_INTEGER_TYPE },
  98.   { "ICrvColor",    (VoidPtr) &ICrvColor,        SU_INTEGER_TYPE },
  99.   { "PrimColor",    (VoidPtr) &PrimColor,        SU_INTEGER_TYPE },
  100.   { "StartFile",    (VoidPtr) &StartFileName,    SU_STRING_TYPE },
  101.   { "LogFile",        (VoidPtr) &LogFileName,        SU_STRING_TYPE },
  102.   { "EditPrgm",        (VoidPtr) &EditPrgm,        SU_STRING_TYPE },
  103. };
  104. #define NUM_SET_UP    (sizeof(SetUp) / sizeof(ConfigStruct))
  105.  
  106. #ifdef __MSDOS__
  107. extern unsigned int _stklen = 16384;         /* Increase default stack size. */
  108. #endif /* __MSDOS__ */
  109.  
  110. static void Interact(void);
  111. static void PrintInptPrsrError(void);
  112.  
  113. /*****************************************************************************
  114. * Main routine - Read Parameter    line and do what you need...             *
  115. *****************************************************************************/
  116. void main(int argc, char **argv)
  117. {
  118.     char *ErrorMsg, *FullPathStartFileName;
  119.  
  120.     getcwd(CurrentWorkingDir, LINE_LEN-1);
  121.  
  122.     SetUpCtrlBrk();         /* Set up control break trap routine (int 1bh). */
  123.     SetUpHardErr();        /* Set up hardware error trap routine (int 24h). */
  124.     signal(SIGFPE, DefaultFPEHandler);     /* Will trap floating point errors. */
  125.  
  126. #ifdef __MSDOS__
  127.     MouseExists = MouseDetect();       /* Auto test for mouse existance. */
  128. #endif /* __MSDOS__ */
  129.  
  130.     Config("irit", SetUp, NUM_SET_UP);       /* Read config. file if exists. */
  131.  
  132.     if (argc == 2 &&
  133.     (strncmp(argv[1], "-z", 2) == 0 || strncmp(argv[1], "-Z", 2) == 0)) {
  134.     argv++;
  135.     argc--;
  136.     fprintf(stderr, "\n%s\n\nUsage: %s\n", VersionStr, CtrlStr);
  137.     ConfigPrint(SetUp, NUM_SET_UP);
  138.     MyExit(0);
  139.     }
  140.  
  141.     /* Execute the file specified in the command line if was one: */
  142.     if (argc == 2) FileInclude(argv[1]);
  143.  
  144. #ifdef __MSDOS__
  145.     GGInitGraph();                  /* Initiate the graphic driver. */
  146. #else
  147.     GGInitGraph(argc, argv);
  148. #endif /* __MSDOS__ */
  149.  
  150.     WndwSetUpAllWndws();                  /* Set up the windows. */
  151.     SetUpPredefObjects();          /* Prepare the predefined objects. */
  152.  
  153.     /* Execute the start up file first by inserting it to the include stack. */
  154.     if (strlen(StartFileName) > 0 &&
  155.     (FullPathStartFileName = searchpath(StartFileName)) != NULL)
  156.     FileInclude(FullPathStartFileName);
  157.  
  158. #ifdef __MSDOS__
  159.     if (MouseExists)
  160.     /* Must be called before any usage! */
  161.     if ((ErrorMsg = MouseInit()) != 0) {
  162.         WndwInputWindowPutStr(ErrorMsg, RED);
  163.         MouseExists = FALSE;
  164.         getch();
  165.     }
  166.  
  167.     DosGetTime(1.0);                    /* Reset the time count. */
  168. #endif /* __MSDOS__ */
  169.  
  170.     AliasReset();
  171.  
  172.     Interact();                      /* Go and do some real work... */
  173.  
  174.     MyExit(0);
  175. }
  176.  
  177. /*****************************************************************************
  178. * Interact - the main read/eval/print routine. This routine reads data from  *
  179. * standart input and execute it for ever (using Input Parser).             *
  180. * Note exit from this program is controled by input parser itself.         *
  181. *****************************************************************************/
  182. static void Interact(void)
  183. {
  184.     /* setjmp return 0 on first install time. Will return 1 if error is      */
  185.     /* recoverable, 2 if cannt continue - must quit the program now.         */
  186.     switch (setjmp(LongJumpBuffer)) {              /* Used in error recovery. */
  187.     case 0:
  188.     case 1:
  189.         while (TRUE) {
  190.         if (!InputParser())         /* Print the error message. */
  191.             PrintInptPrsrError();
  192.         }
  193.         break;
  194.     case 2:
  195.         WndwInputWindowPutStr("Press return to die...", RED);
  196. #ifdef __MSDOS__
  197.         while (kbhit()) getch();        /* Flush any old keystrokes. */
  198.         getch();
  199. #else
  200.         getchar();
  201. #endif /* __MSDOS__ */
  202.         break;
  203.     }
  204. }
  205.  
  206. /*****************************************************************************
  207. * Routine to query (and print) the errors found in InputParser:             *
  208. *****************************************************************************/
  209. static void PrintInptPrsrError(void)
  210. {
  211.     int ErrorNum;
  212.     char *ErrorMsg;
  213.     char Line[LINE_LEN_LONG];
  214.  
  215.     if ((ErrorNum = InptPrsrParseError(&ErrorMsg)) != 0) {/* Error in parse. */
  216.     sprintf(Line, "Parsing Error: ");
  217.     switch(ErrorNum) {
  218.         case IP_ERR_WrongSyntax:
  219.         sprintf(&Line[strlen(Line)], "Wrong syntax\n");
  220.         break;
  221.         case IP_ERR_ParamExpect:
  222.         sprintf(&Line[strlen(Line)], "Parameter Expected\n");
  223.         break;
  224.         case IP_ERR_OneOperand:
  225.         case IP_ERR_TwoOperand:
  226.         sprintf(&Line[strlen(Line)],
  227.                 "Wrong # of operands - %s\n", ErrorMsg);
  228.         break;
  229.         case IP_ERR_StackOV:
  230.         sprintf(&Line[strlen(Line)],
  231.                 "Internal Stack OverFlow at - %s\n", ErrorMsg);
  232.         break;
  233.         case IP_ERR_ParaMatch:
  234.         sprintf(&Line[strlen(Line)],
  235.                 "Parenthesis mismatch - %s\n", ErrorMsg);
  236.         break;
  237.         case IP_ERR_UndefToken:
  238.         sprintf(&Line[strlen(Line)],
  239.                 "Undefined token - %s\n", ErrorMsg);
  240.         break;
  241.         case IP_ERR_UndefFunc:
  242.         sprintf(&Line[strlen(Line)],
  243.                 "Undefined function - %s\n", ErrorMsg);
  244.         break;
  245.         case IP_ERR_NameTooLong:
  246.         sprintf(&Line[strlen(Line)],
  247.                 "Object name too long - %s\n", ErrorMsg);
  248.         break;
  249.         case IP_ERR_ParamFunc:
  250.         sprintf(&Line[strlen(Line)],
  251.                 "Parameters expected in func %s\n", ErrorMsg);
  252.         break;
  253.         case IP_ERR_NoParamFunc:
  254.         sprintf(&Line[strlen(Line)],
  255.             "No Parameters expected in func %s\n", ErrorMsg);
  256.         break;
  257.     }
  258.         WndwInputWindowPutStr(Line, RED);
  259.     return;
  260.     }
  261.  
  262.     if ((ErrorNum = InptPrsrEvalError(&ErrorMsg)) != 0) {  /* Error in eval. */
  263.     sprintf(Line, "Eval Error: ");
  264.     switch(ErrorNum) {
  265.         case IE_ERR_FatalError:
  266.         sprintf(&Line[strlen(Line)], "Fatal Error - %s\n", ErrorMsg);
  267.         break;
  268.         case IE_ERR_DivByZero:
  269.         sprintf(&Line[strlen(Line)],
  270.                     "Division by zero - %s\n", ErrorMsg);
  271.         break;
  272.         case IE_ERR_NoObjMethod:
  273.         sprintf(&Line[strlen(Line)],
  274.                 "No such method for object - %s\n", ErrorMsg);
  275.         break;
  276.         case IE_ERR_TypeMismatch:
  277.         sprintf(&Line[strlen(Line)],
  278.                 "Parameter type mismatch - %s\n", ErrorMsg);
  279.         break;
  280.         case IE_ERR_AssignLeftOp:
  281.         sprintf(&Line[strlen(Line)],
  282.                 "Lval is not a parameter - %s\n", ErrorMsg);
  283.         break;
  284.         case IE_ERR_MixedObj:
  285.         sprintf(&Line[strlen(Line)],
  286.                 "Mixed types in expression - %s\n", ErrorMsg);
  287.         break;
  288.         case IE_ERR_UndefObject:
  289.         sprintf(&Line[strlen(Line)],
  290.                 "No such object defined - %s\n", ErrorMsg);
  291.         break;
  292.         case IE_ERR_NoAssignment:
  293.         sprintf(&Line[strlen(Line)],
  294.                 "Assignment was expected\n");
  295.         break;
  296.         case IE_ERR_FPError:
  297.         sprintf(&Line[strlen(Line)],
  298.                 "Floating Point Error - %s\n", ErrorMsg);
  299.         break;
  300.         case IE_ERR_NumPrmMismatch:
  301.         sprintf(&Line[strlen(Line)],
  302.             "Number of func. param. mismatch - %s\n", ErrorMsg);
  303.         break;
  304.         case IE_ERR_MatPower:
  305.         sprintf(&Line[strlen(Line)],
  306.              "Wrong range or not exists, operator - %s\n", ErrorMsg);
  307.         break;
  308.         case IE_ERR_FreeSimple:
  309.         sprintf(&Line[strlen(Line)],
  310.             "Free only Geometric Objects - %s\n", ErrorMsg);
  311.         break;
  312.         case IE_ERR_ModifIterVar:
  313.         sprintf(&Line[strlen(Line)],
  314.             "Iteration var. type modified or freed - %s\n", ErrorMsg);
  315.         break;
  316.         case IE_ERR_BooleanErr:
  317.         sprintf(&Line[strlen(Line)],
  318.             "Geometric Boolean operation error - %s\n", ErrorMsg);
  319.         break;
  320.         case IE_ERR_ListTooLong:
  321.         sprintf(&Line[strlen(Line)],
  322.             "List length too big - up to %d only.\n", MAX_OBJ_LIST);
  323.         break;
  324.         case IE_ERR_NonParamInList:
  325.         sprintf(&Line[strlen(Line)],
  326.             "List element not an object (expression!?)\n");
  327.         break;
  328.         case IE_ERR_FreeNoRefObj:
  329.         sprintf(&Line[strlen(Line)],
  330.             "Cannt free referenced (by others!) object %s\n", ErrorMsg);
  331.         break;
  332.         case IE_ERR_DataPrsrError:
  333.         sprintf(&Line[strlen(Line)], "%s", ErrorMsg);
  334.         break;
  335.     }
  336.         WndwInputWindowPutStr(Line, RED);
  337.     return;
  338.     }
  339. }
  340.  
  341. /*****************************************************************************
  342. * Exit routine - called when this program should terminate.             *
  343. *****************************************************************************/
  344. void MyExit(int ExitCode)
  345. {
  346.     GGCloseGraph();                /* Close the graphic driver. */
  347. #ifdef __MSDOS__
  348.     MouseClose();                /* Disable mouse interrupts. */
  349. #endif /* __MSDOS__ */
  350.     RestoreCtrlBrk();                  /* Restore ctrl-brk interrupt. */
  351.  
  352.     chdir(CurrentWorkingDir);      /* Recover original directory before exit. */
  353. #ifdef __MSDOS__
  354.     setdisk(CurrentWorkingDir[0] - 'A');        /* Move to the old disk. */
  355. #endif /* __MSDOS__ */
  356.  
  357.     if (PrintLogFile) fclose(LogFile);    /* Close log file if one was open. */
  358.  
  359.     exit(ExitCode);
  360. }
  361.  
  362. /*****************************************************************************
  363. * FatalEror routine. print a fatal error message and go back to cursor mode  *
  364. *****************************************************************************/
  365. void FatalError(char *ErrorMsg)
  366. {
  367.     if (ErrorMsg != NULL) {
  368. #ifdef __MSDOS__
  369.     WndwInputWindowPutStr("Fatal error occured, please report it:", RED);
  370.     WndwInputWindowPutStr(ErrorMsg, RED);
  371. #else
  372.     fprintf(stderr, "%s\n%s\n",
  373.         "Fatal error occured, please report it:", ErrorMsg);
  374. #endif /* __MSDOS__ */
  375.     }
  376.  
  377.     longjmp(LongJumpBuffer, 1);            /* Go back to menu directly. */
  378. }
  379.  
  380. /*****************************************************************************
  381. *   Routine that is called from the floating point package in case of fatal  *
  382. * floating point error. Print error message, long jump to main loop. Default *
  383. * FPE handler - must be reset after redirected to other module.             *
  384. *****************************************************************************/
  385. void DefaultFPEHandler(int Sig, int Type, int *RegList)
  386. {
  387.     PrintFPError(Type);             /* Print the floating point error type. */
  388.  
  389.     longjmp(LongJumpBuffer, 1);
  390. }
  391.