home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p024 / 12.img / ADS1.LIB / CAL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-10  |  7.0 KB  |  252 lines

  1. /*****************************************************************************
  2.       CAL.C
  3.       (C) ¬⌐┼v 1988-1992  Autodesk ñ╜Ñq
  4.  
  5.       Ñ╗╡{ªíñwÑ╤ Autodesk ñ╜Ñq╡∙ÑU¬⌐┼v, ╢╚⌐≤ñU¡z▒í¬pñUÑi▒┬╗P▒zíu│\ÑiívíC
  6.       ╗╒ñUñú▒oÑHÑ⌠ª≤º╬ªí╡oªµ⌐╬ÑX¬⌐ª╣╡{ªí¬║íu¡∞⌐l╜Xív; ª²ñ╣│\▒zªb»S⌐w¡lÑ═
  7.       ¬║ñuº@ñW╡▓ªXª╣╡{ªí¬║íuÑ╪¬║╜Xív¿╧Ñ╬íCª│├÷│o├■¡lÑ═ñuº@¬║▒°Ñ≤ªpñU:
  8.  
  9.       ( i)  │]¡pñW╗Pñuº@ñW¼╥»┬║Θ░w╣∩ Autodesk ñ╜Ñq¬║▓ú½~íC
  10.       (ii)  ╕ⁿª│íu¬⌐┼v  (C) 1988-1992  Autodesk ñ╜Ñqív¬║¬⌐┼v│qºiíC
  11.  
  12.  
  13.  
  14.       AUTODESKñ╜Ñq┤ú¿╤ª╣╡{ªí╢╚¿╤º@íu├■ªⁿív¬║░╤ª╥, ª╙ÑBñú▒╞░úª│Ñ⌠ª≤┐∙╗~¬║
  15.       Ñi»αíCAUTODESKñ╜Ñq»Sª╣º_╗{Ñ⌠ª≤»S⌐wÑ╬│~ñº╛A║┘⌐╩, ÑHñ╬░╙╖~╛P░Γ⌐╥┴⌠ºt
  16.       ÑX¿π¬║½O├╥íCAUTODESKñ╜ÑqªP«╔ÑτñúÑX¿πª╣╡{ªí░⌡ªµ«╔ñ@⌐wñú╖|íuññ┬_ív⌐╬
  17.       íuº╣Ñ■╡L╗~ív¬║½O├╥íC
  18.  
  19.  
  20.   Description: The main file of the "Geometry Calculator" ADS application.
  21.  
  22.   Notes:
  23.  
  24.   The Geometry Calculator application consists of the following files:
  25.  
  26.   cal.c      ... The main file establishing link with ADS
  27.   callex.c   ... Lexical analyser
  28.   calexpr.c  ... Syntax analyser
  29.   calmngf.c  ... Management of calculator functions
  30.   calstdf.c  ... Standard calculator functions
  31.   calusrf.c  ... User-defined calculator functions
  32.   calerr.c   ... Error handling
  33.   util.c     ... Some utility functions
  34.  
  35.   callex.h   ... Hearer file for callex.c
  36.   calexpr.h  ... Header file for calexpr.c
  37.   calmngf.h  ... Header file for calmngf.c
  38.   calerr.h   ... Header file for calerr.c
  39.   util.h     ... Header file for util.c
  40.   cal.h      ... Includes all the above include files plus some basic
  41.                  C include files.
  42.  
  43. *****************************************************************************/
  44.  
  45.  
  46. /****************************************************************************/
  47. /*  INCLUDES                                                                */
  48. /****************************************************************************/
  49.  
  50. #include  "cal.h"
  51.  
  52.  
  53. /****************************************************************************/
  54. /*  IMPORTED FUNCTIONS                                                      */
  55. /****************************************************************************/
  56.  
  57. void cal_register_standard_functions();
  58. void cal_register_user_functions();
  59.  
  60.  
  61. /****************************************************************************/
  62. /*  STATIC VARIABLES ANF FUNCTIONS                                          */
  63. /****************************************************************************/
  64.  
  65. static void calculator_function _((struct resbuf *buff));
  66.  
  67. static struct {char *name; void (*func)();} ADS_funcs_table[] =
  68. {
  69.     "CAL",    calculator_function,
  70.     "C:CAL",  calculator_function,
  71. };
  72.  
  73.  
  74. /****************************************************************************/
  75. /*.doc load_ADS_functions(internal)*/
  76. /*+
  77.     Standard code for loading the ADS functions from 'ADS_funcs_table[]'.
  78. -*/
  79. /****************************************************************************/
  80.  
  81.  
  82. static int
  83. /*FCN*/load_ADS_functions()
  84. {
  85.     int i;
  86.  
  87.     for (i = 0; i < ELEMENTS(ADS_funcs_table); i++)
  88.         if (ads_defun(ADS_funcs_table[i].name, i) != RTNORM) {
  89.             return(RSERR);
  90.         }
  91.     return(RSRSLT);
  92. } /*load_ADS_functions*/
  93.  
  94.  
  95. /****************************************************************************/
  96. /*.doc execute_ADS_function(internal)*/
  97. /*+
  98.     Standard code for executing an ADS function from 'ADS_funcs_table[]'.
  99. -*/
  100. /****************************************************************************/
  101.  
  102.  
  103. static int
  104. /*FCN*/execute_ADS_function()
  105. {
  106.     struct resbuf *buff;
  107.     int           i;
  108.  
  109.     if (((i = ads_getfuncode()) < 0) ||
  110.         (i >= ELEMENTS(ADS_funcs_table))) {
  111.         return(RSERR);
  112.     }
  113.  
  114.     ads_retnil();
  115.  
  116.     buff = ads_getargs();
  117.     (*ADS_funcs_table[i].func)(buff);
  118.  
  119.     return(RSRSLT);
  120. } /*execute_ADS_function*/
  121.  
  122.  
  123. /****************************************************************************/
  124. /*.doc main(external)*/
  125. /*+
  126.     The main function.
  127. -*/
  128. /****************************************************************************/
  129.  
  130.  
  131. void
  132. /*FCN*/main(argc, argv)
  133.  
  134.   int  argc;
  135.   char *argv[];
  136. {
  137.     int   request;
  138.     short success         = RSRSLT;
  139.     int   load_first_time = TRUE;
  140.  
  141.     ads_init(argc,argv);
  142.     cal_err = FALSE;
  143.  
  144.     for (;;) {
  145.  
  146.         if ((request = ads_link(success)) < 0) {
  147.             fflush(stdout);
  148.             exit(1);
  149.         }
  150.         success = RSRSLT;
  151.  
  152.         switch (request) {
  153.  
  154.         case RQXLOAD:
  155.             if (load_first_time) {
  156.                 cal_register_standard_functions();
  157.                 cal_register_user_functions();
  158.                 if (cal_err)
  159.                     exit(1);
  160.                 load_first_time = FALSE;
  161.             }
  162.             success = load_ADS_functions();
  163.             break;
  164.  
  165.         case RQSUBR:
  166.             success = execute_ADS_function();
  167.             break;
  168.  
  169.         case RQXUNLD:
  170.         case RQSAVE:
  171.         case RQQUIT:
  172.         case RQEND:
  173.             break;
  174.  
  175.         default:
  176.             break;
  177.         } /*switch*/
  178.     } /*forever*/
  179.  
  180. } /*main*/
  181.  
  182.  
  183. /****************************************************************************/
  184. /*.doc calculator_function(internal)*/
  185. /*+
  186.   Function evaluates arithmetic expression string which it receives
  187.   in 'buff'. If no result buffer is provided, the function prompts
  188.   for a string.
  189. -*/
  190. /****************************************************************************/
  191.  
  192.  
  193. static void
  194. /*FCN*/calculator_function(buff)
  195.  
  196.   struct resbuf *buff;
  197. {
  198.     int             success;
  199.     vector_real_int value;
  200.     char            line[MAX_LINE_LENGTH+1];
  201.  
  202.     do {
  203.         cal_err = 0;                  /* Reset the global error flag */
  204.  
  205.         /* Get the input entier from the string resbuf, or,
  206.            if not present, prompt the user to enter a string */
  207.  
  208.         if (buff != NULL) {
  209.             if ((buff->restype != RTSTR) || (buff->rbnext != NULL)) {
  210.                 error(27, NULL);
  211.                 return;
  212.             } else {
  213.                 strcpy(line, buff->resval.rstring);
  214.             }
  215.         } else {
  216.             do {
  217.                 ads_initget(1, NULL);
  218.                 success = ads_getstring(TRUE, ">> ¡zÑy: ", line);
  219.  
  220.                 if (success == RTCAN) {
  221.                     return;
  222.                 } else if (success != RTNORM) {
  223.                     error(27, NULL);
  224.                     return;
  225.                 }
  226.             } while (line[0] == EOS);
  227.         }
  228.  
  229.         success = cal_evaluate_expression(line, &value);
  230.  
  231.         if (success) {
  232.             switch (value.type) {
  233.             case vector_type:
  234.                 ads_retpoint(value.v);
  235.                 break;
  236.             case real_type:
  237.                 ads_retreal(value.r);
  238.                 break;
  239.             case int_type:
  240.                 ads_retint((int)value.r);
  241.                 break;
  242.             default:
  243.                 break;
  244.             } /*switch*/
  245.         }
  246.     } while (!success && (buff == NULL));
  247.  
  248. } /*calculator_function*/
  249.  
  250.  
  251.  
  252.