home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd7.lzh / SRC / lex.c < prev    next >
Text File  |  1990-05-06  |  15KB  |  670 lines

  1. /*      SC      A Spreadsheet Calculator
  2.  *              Lexical analyser
  3.  *
  4.  *              original by James Gosling, September 1982
  5.  *              modifications by Mark Weiser and Bruce Israel,
  6.  *                      University of Maryland
  7.  *
  8.  *              More mods Robert Bond, 12/86
  9.  *              More mods by Alan Silverstein, 3/88, see list of changes.
  10.  *              $Revision: 6.1 $
  11.  *
  12.  */
  13.  
  14.  
  15.  
  16. #if defined(BSD42) || defined(BSD43)
  17. #include <sys/ioctl.h>
  18. #endif
  19.  
  20. #include <curses.h>
  21. #include <signal.h>
  22. #include <setjmp.h>
  23. #include "sc.h"
  24. #include <ctype.h>
  25.  
  26. #if defined(BSD42) || defined(OSK)
  27. #include <strings.h>
  28. #else
  29. #ifndef SYSIII
  30. #include <string.h>
  31. #endif
  32. #endif
  33.  
  34. #ifdef VMS
  35. #include "gram_tab.h"
  36. #endif
  37. #if defined (VMS)
  38. typedef union {
  39.     int ival;
  40.     double fval;
  41.     struct ent *ent;
  42.     struct enode *enode;
  43.     char *sval;
  44.     struct range_s rval;
  45. } YYSTYPE;
  46. extern YYSTYPE yylval;
  47. #ifdef VMS
  48. extern int VMS_read_raw;   /*sigh*/
  49. #endif /* VMS */
  50. #endif   /* VMS | OSK */
  51. #ifndef VMS
  52. #include "y.tab.h"
  53. #endif /* VMS */
  54.  
  55. char *strtof();
  56.  
  57. jmp_buf wakeup;
  58. jmp_buf fpe_buf;
  59.  
  60. struct key {
  61.     char *key;
  62.     int val;
  63. };
  64.  
  65. struct key experres[] = {
  66. #include "experres.h"
  67.     0, 0};
  68.  
  69. struct key statres[] = {
  70. #include "statres.h"
  71.     0, 0};
  72.  
  73. #define ctl(x) ('x'&037)
  74.  
  75. yylex ()
  76. {
  77.     register char *p = line+linelim;
  78.     int ret;
  79.     while (isspace(*p)) p++;
  80.     if (*p==0) ret = -1;
  81.     else if (isalpha(*p)) {
  82.         char *tokenst = p;
  83.         register tokenl;
  84.         register struct key *tblp;
  85.         tokenl = 0;
  86.         /*
  87.          * This picks up either 1 or 2 alpha characters (a column) or
  88.          * tokens with at least three leading alphas and '_' or digits
  89.          * (a function or token or command or a range name)
  90.         */
  91.         while (isalpha(*p) || ((*p == '_') || isdigit(*p)) && (tokenl > 2)) {
  92.             p++;
  93.             tokenl++;
  94.         }
  95.         if (tokenl <= 2) { /* a COL is 1 or 2 char alpha (and not pi or ln!) */
  96.             if (tokenl == 2 && tokenst[0] == 'p' && tokenst[1] == 'i') {
  97.                 ret = K_PI;
  98.             } else if (tokenl == 2 && tokenst[0] == 'l' && tokenst[1] == 'n') {
  99.                 ret = K_LN;
  100.  
  101.             } else if (tokenl == 2 && tokenst[0] == 'f' && tokenst[1] == 'v') {
  102.                 ret = K_FV;
  103.             } else if (tokenl == 2 && tokenst[0] == 'p' && tokenst[1] == 'v') {
  104.                 ret = K_PV;
  105.  
  106.             } else {
  107.                 ret = COL;
  108.                 yylval.ival = atocol (tokenst, tokenl);
  109.             }
  110.         } else {
  111.             ret = _WORD;
  112.             for (tblp = linelim ? experres : statres; tblp->key; tblp++)
  113.                     if (((tblp->key[0]^tokenst[0])&0137)==0
  114.                      && tblp->key[tokenl]==0) {
  115.                         register i = 1;
  116.                         while (i<tokenl && ((tokenst[i]^tblp->key[i])&0137)==0)
  117.                             i++;
  118.                         if (i>=tokenl) {
  119.                             ret = tblp->val;
  120.                             break;
  121.                         }
  122.                     }
  123.             if (ret==_WORD) {
  124.                 struct range *r;
  125.                 if (r = find_range(tokenst, tokenl,
  126.                                    (struct ent *)0, (struct ent *)0)) {
  127.                     yylval.rval.left = r->r_left;
  128.                     yylval.rval.right = r->r_right;
  129.                     if (r->r_is_range)
  130.                         ret = RANGE;
  131.                     else
  132.                         ret = VAR;
  133.                 } else {
  134.                     linelim = p-line;
  135.                     yyerror ("Unintelligible word");
  136.                 }
  137.             }
  138.         }
  139.     } else if ((*p == '.') || isdigit(*p)) {
  140.         double v = 0;
  141.         int temp;
  142.         char *nstart = p;
  143.         if (*p != '.') {
  144.             do v = v*10 + (double)(*p-'0');
  145.             while (isdigit(*++p));
  146.         }
  147.         if (*p=='.' || *p == 'e' || *p == 'E') {
  148.             ret = FNUMBER;
  149.             p = strtof(nstart, &yylval.fval);
  150.         } else {
  151.             /* A NUMBER must hold at least MAXROW and MAXCOL */
  152.             /* This is consistent with a short row and col in struct ent */
  153.             if (v > (double)32767 || v < (double)-32768) {
  154.                 ret = FNUMBER;
  155.                 yylval.fval = v;
  156.             } else {
  157.                 temp = (int)v;
  158.                 if((double)temp != v) {
  159.                     ret = FNUMBER;
  160.                     yylval.fval = v;
  161.                 } else {
  162.                     ret = NUMBER;
  163.                     yylval.ival = temp;
  164.                 }
  165.             }
  166.         }
  167.     } else if (*p=='"') {
  168.         char *ptr;
  169.         ptr = p+1;
  170.         while(*ptr && *ptr++ != '"');
  171.         ptr = xmalloc((unsigned)(ptr-p));
  172.         yylval.sval = ptr;
  173.         p += 1;
  174.         while (*p && *p!='"') *ptr++ = *p++;
  175.         *ptr = 0;
  176.         if (*p) p += 1;
  177.         ret = STRING;
  178.     } else if (*p=='[') {
  179.         while (*p && *p!=']') p++;
  180.         if (*p) p++;
  181.         linelim = p-line;
  182.         return yylex();
  183.     } else ret = *p++;
  184.     linelim = p-line;
  185.     return ret;
  186. }
  187.  
  188.  
  189. /*
  190.  * Given a token string starting with a symbolic column name and its valid
  191.  * length, convert column name ("A"-"Z" or "AA"-"ZZ") to a column number (0-N).
  192.  * Never mind if the column number is illegal (too high).  The procedure's name
  193.  * and function are the inverse of coltoa().
  194.  *
  195.  * Case-insensitivity is done crudely, by ignoring the 040 bit.
  196.  */
  197.  
  198. int
  199. atocol (string, len)
  200.         char    *string;
  201.         int     len;
  202. {
  203.         register int col;
  204.  
  205.         col = (string [0] & 0137) - 'A';
  206.  
  207.         if (len == 2)           /* has second char */
  208.             col = ((col + 1) * 26) + ((string [1] & 0137) - 'A');
  209.  
  210.         return (col);
  211. }
  212.  
  213.  
  214. #ifdef SIMPLE
  215.  
  216. initkbd()
  217. {}
  218.  
  219. kbd_again()
  220. {}
  221.  
  222. resetkbd()
  223. {}
  224.  
  225. #ifndef VMS
  226.  
  227. nmgetch()
  228. {
  229. #ifndef INTERNATIONAL
  230.     return (getchar() & 0x7f);
  231. #else
  232.     return (getchar() & 0xff);
  233. #endif /* INTERNATIONAL */
  234. }
  235.  
  236. #else /* VMS */
  237.  
  238. nmgetch()
  239. /*
  240.    This is not perfect, it doesn't move the cursor when goraw changes
  241.    over to deraw, but it works well enough since the whole sc package
  242.    is incredibly stable (loop constantly positions cursor).
  243.  
  244.    Question, why didn't the VMS people just implement cbreak?
  245.  
  246.    NOTE: During testing it was discovered that the DEBUGGER and curses
  247.    and this method of reading would collide (the screen was not updated
  248.    when continuing from screen mode in the debugger).
  249. */
  250. {
  251.     short c;
  252.     static int key_id=0;
  253.     int status;
  254. #define VMScheck(a) {if (~(status = (a)) & 1) VMS_MSG (status);}
  255.  
  256.     if (VMS_read_raw) {
  257.       VMScheck(smg$read_keystroke (&stdkb->_id, &c, 0, 0, 0));
  258.     }
  259.     else
  260.        c = getchar();
  261.  
  262.     switch (c) {
  263.     case SMG$K_TRM_LEFT:  c = ctl(b); break;
  264.     case SMG$K_TRM_RIGHT: c = ctl(f); break;
  265.     case SMG$K_TRM_UP:    c = ctl(p); break;
  266.     case SMG$K_TRM_DOWN:  c = ctl(n); break;
  267.     default:   c = c & 0x7f;
  268.     }
  269.     return (c);
  270. }
  271.  
  272.  
  273. VMS_MSG (status)
  274. int status;
  275. /*
  276.    Routine to put out the VMS operating system error (if one occurs).
  277. */
  278. {
  279. #include <descrip.h>
  280.    char errstr[81], buf[120];
  281.    $DESCRIPTOR(errdesc, errstr);
  282.    short int length;
  283. #define err_out(msg) fprintf (stderr,msg)
  284.  
  285. /* Check for no error or standard error */
  286.  
  287.    if (~status & 1) {
  288.       status = status & 0x8000 ? status & 0xFFFFFFF : status & 0xFFFF;
  289.       if (SYS$GETMSG(status, &length, &errdesc, 1, 0) == SS$_NORMAL) {
  290.          errstr[length] = '\0';
  291.          sprintf (buf, "<0x%x> %s", status, errdesc.dsc$a_pointer);
  292.          err_out (buf);
  293.       }
  294.       else
  295.          err_out ("System error");
  296.    }
  297. }
  298. #endif /* VMS */
  299.  
  300. #else /*SIMPLE*/
  301.  
  302. #if defined(BSD42) || defined (SYSIII) || defined(BSD43) || defined(OSK)
  303.  
  304. #define N_KEY 4
  305.  
  306. struct key_map {
  307.     char *k_str;
  308.     char k_val;
  309.     char k_index;
  310. };
  311.  
  312. struct key_map km[N_KEY];
  313.  
  314. char keyarea[N_KEY*10];
  315.  
  316. char *tgetstr();
  317. char *getenv();
  318. char *ks;
  319. char ks_buf[20];
  320. char *ke;
  321. char ke_buf[20];
  322.  
  323. #ifdef TIOCSLTC
  324. struct ltchars old_chars, new_chars;
  325. #endif
  326.  
  327. char dont_use[] = {
  328.     ctl(z), ctl(r), ctl(l), ctl(b), ctl(c), ctl(f), ctl(g), ctl([),
  329.     ctl(h), ctl(m), ctl(j), ctl(n), ctl(p), ctl(q), ctl(s), ctl(t),
  330.     ctl(u), ctl(v), ctl(e), ctl(a), ctl(i), ctl(w), 0,
  331. };
  332.  
  333. charout(c)
  334. int c;
  335. {
  336.         (void)putchar(c);
  337. }
  338.  
  339. void initkbd()
  340. {
  341.     register struct key_map *kp;
  342.     register i,j;
  343.     char *p = keyarea;
  344.     char *ktmp;
  345.     static char buf[1024]; /* Why do I have to do this again? */
  346.  
  347.     if (tgetent(buf, getenv("TERM")) <= 0)
  348.         return;
  349.  
  350.     km[0].k_str = tgetstr("kl", &p); km[0].k_val = ctl(b);
  351.     km[1].k_str = tgetstr("kr", &p); km[1].k_val = ctl(f);
  352.     km[2].k_str = tgetstr("ku", &p); km[2].k_val = ctl(p);
  353.     km[3].k_str = tgetstr("kd", &p); km[3].k_val = ctl(n);
  354.     ktmp = tgetstr("ks",&p);
  355.     if (ktmp)  {
  356.         (void) strcpy(ks_buf, ktmp);
  357.         ks = ks_buf;
  358.         tputs(ks, 1, charout);
  359.     }
  360.     ktmp = tgetstr("ke",&p);
  361.     if (ktmp)  {
  362.         (void) strcpy(ke_buf, ktmp);
  363.         ke = ke_buf;
  364.     }
  365.  
  366.     /* Unmap arrow keys which conflict with our ctl keys   */
  367.     /* Ignore unset, longer than length 1, and 1-1 mapped keys */
  368.  
  369.     for (i = 0; i < N_KEY; i++) {
  370.         kp = &km[i];
  371.         if (kp->k_str && (kp->k_str[1] == 0) && (kp->k_str[0] != kp->k_val))
  372.             for (j = 0; dont_use[j] != 0; j++)
  373.                 if (kp->k_str[0] == dont_use[j]) {
  374.                      kp->k_str = (char *)0;
  375.                      break;
  376.                 }
  377.     }
  378.  
  379.  
  380. #ifdef TIOCSLTC
  381.     (void)ioctl(fileno(stdin), TIOCGLTC, (char *)&old_chars);
  382.     new_chars = old_chars;
  383.     if (old_chars.t_lnextc == ctl(v))
  384.         new_chars.t_lnextc = -1;
  385.     if (old_chars.t_rprntc == ctl(r))
  386.         new_chars.t_rprntc = -1;
  387.     (void)ioctl(fileno(stdin), TIOCSLTC, (char *)&new_chars);
  388. #endif
  389. }
  390.  
  391. kbd_again()
  392. {
  393.     if (ks)
  394.         tputs(ks, 1, charout);
  395.  
  396. #ifdef TIOCSLTC
  397.     (void)ioctl(fileno(stdin), TIOCSLTC, (char *)&new_chars);
  398. #endif
  399. }
  400.  
  401. resetkbd()
  402. {
  403.     if (ke)
  404.         tputs(ke, 1, charout);
  405.  
  406. #ifdef TIOCSLTC
  407.     (void)ioctl(fileno(stdin), TIOCSLTC, (char *)&old_chars);
  408. #endif
  409. }
  410.  
  411. nmgetch()
  412. {
  413.     register int c;
  414.     register struct key_map *kp;
  415.     register struct key_map *biggest;
  416.     register int i;
  417.     int almost;
  418.     int maybe;
  419. #ifdef OSK
  420.     char ch;
  421. #endif
  422.  
  423.     static char dumpbuf[10];
  424.     static char *dumpindex;
  425.  
  426. #ifdef SIGVOID
  427.     void time_out();
  428. #else
  429.     int time_out();
  430. #endif
  431.  
  432.     if (dumpindex && *dumpindex)
  433.             return (*dumpindex++);
  434.  
  435. #ifndef OSK
  436. #ifndef INTERNATIONAL
  437.     c = getchar() & 0x7f;
  438. #else
  439.     c = getchar() & 0xff;
  440. #endif /* INTERNATIONAL */
  441. #else
  442.     read(0,&ch,1);
  443.     c = ch;
  444. #endif
  445.     biggest = 0;
  446.     almost = 0;
  447.  
  448.     for (kp = &km[0]; kp < &km[N_KEY]; kp++) {
  449.         if (!kp->k_str)
  450.             continue;
  451.         if ((char) c == kp->k_str[kp->k_index]) {
  452.             almost = 1;
  453.             kp->k_index++;
  454.             if (kp->k_str[kp->k_index] == 0) {
  455.                 c = kp->k_val;
  456.                 for (kp = &km[0]; kp < &km[N_KEY]; kp++)
  457.                     kp->k_index = 0;
  458.                 return((unsigned char) c);
  459.             }
  460.         }
  461.         if (!biggest && kp->k_index)
  462.             biggest = kp;
  463.         else if (kp->k_index && biggest->k_index < kp->k_index)
  464.             biggest = kp;
  465.     }
  466.  
  467.     if (almost) {
  468.  
  469.         (void) signal(SIGALRM, time_out);
  470.         (void) alarm(1);
  471.         if (setjmp(wakeup) == 0) {
  472.             maybe = nmgetch();
  473.             (void) alarm(0);
  474.             return(maybe);
  475.         }
  476.     }
  477.  
  478.     if (biggest) {
  479.         for (i = 0; i<biggest->k_index; i++)
  480.             dumpbuf[i] = biggest->k_str[i];
  481.         dumpbuf[i++] = c;
  482.         dumpbuf[i] = 0;
  483.         dumpindex = &dumpbuf[1];
  484.         for (kp = &km[0]; kp < &km[N_KEY]; kp++)
  485.             kp->k_index = 0;
  486.         return (dumpbuf[0]);
  487.     }
  488.  
  489.     return(c);
  490. }
  491.  
  492. #endif
  493.  
  494. #if defined(SYSV2) || defined(SYSV3)
  495.  
  496. initkbd()
  497. {
  498.     keypad(stdscr, TRUE);
  499. }
  500.  
  501. kbd_again()
  502. {
  503.     keypad(stdscr, TRUE);
  504. }
  505.  
  506. resetkbd()
  507. {
  508.     keypad(stdscr, FALSE);
  509. }
  510.  
  511. nmgetch()
  512. {
  513.     register int c;
  514.  
  515.     c = getch();
  516.     switch (c) {
  517.     case KEY_LEFT:  c = ctl(b); break;
  518.     case KEY_RIGHT: c = ctl(f); break;
  519.     case KEY_UP:    c = ctl(p); break;
  520.     case KEY_DOWN:  c = ctl(n); break;
  521. #ifdef KEY_C1
  522. /* This stuff works for a wyse wy75 in ANSI mode under 5.3.  Good luck. */
  523. /* It is supposed to map the curses keypad back to the numeric equiv. */
  524.     case KEY_C1:    c = '0'; break;
  525.     case KEY_A1:    c = '1'; break;
  526.     case KEY_B2:    c = '2'; break;
  527.     case KEY_A3:    c = '3'; break;
  528.     case KEY_F(5):  c = '4'; break;
  529.     case KEY_F(6):  c = '5'; break;
  530.     case KEY_F(7):  c = '6'; break;
  531.     case KEY_F(9):  c = '7'; break;
  532.     case KEY_F(10): c = '8'; break;
  533.     case KEY_F0:    c = '9'; break;
  534.     case KEY_C3:    c = '.'; break;
  535.     case KEY_ENTER: c = ctl(m); break;
  536. #endif
  537. #ifndef INTERNATIONAL
  538.     default:   c = c & 0x7f;
  539. #else
  540.     default:   c = c & 0xff;
  541. #endif /* INTERNATIONAL */
  542.     break;
  543.     }
  544.     return (c);
  545. }
  546.  
  547. #endif /* SYSV2 || SYSV3 */
  548.  
  549. #endif /* SIMPLE */
  550.  
  551. #ifdef SIGVOID
  552. void
  553. #endif
  554. time_out()
  555. {
  556.     longjmp(wakeup, -1);
  557. }
  558.  
  559. #ifdef SIGVOID
  560. void
  561. #endif
  562. fpe_trap()
  563. {
  564.     longjmp(fpe_buf, 1);
  565. }
  566.  
  567. /*
  568.  * This converts a floating point number of the form
  569.  * [s]ddd[.d*][esd*]  where s can be a + or - and e is E or e.
  570.  * to floating point.
  571.  * p is advanced.
  572.  */
  573.  
  574. char *
  575. strtof(p, res)
  576. register char *p;
  577. double *res;
  578. {
  579.     double acc;
  580.     int sign;
  581.     double fpos;
  582.     int exp;
  583.     int exps;
  584. #ifdef SIGVOID
  585.     void (*sig_save)();
  586. #else
  587.     int (*sig_save)();
  588. #endif
  589.  
  590. #ifndef OSK
  591.     sig_save = signal(SIGFPE, fpe_trap);
  592. #endif
  593.     if (setjmp(fpe_buf)) {
  594.         error("Floating point exception\n");
  595.         *res = 0.0;
  596. #ifndef OSK
  597.         (void) signal(SIGFPE, sig_save);
  598. #endif
  599.         return(p);
  600.     }
  601.     acc = 0.0;
  602.     sign = 1;
  603.     exp = 0;
  604.     exps = 1;
  605.     if (*p == '+')
  606.         p++;
  607.     else if (*p == '-') {
  608.         p++;
  609.         sign = -1;
  610.     }
  611.     while (isdigit(*p)) {
  612.         acc = acc * 10.0 + (double)(*p - '0');
  613.         p++;
  614.     }
  615.     if (*p == 'e' || *p == 'E') {
  616.             p++;
  617.         if (*p == '+')
  618.             p++;
  619.         else if (*p == '-') {
  620.             p++;
  621.             exps = -1;
  622.         }
  623.         while(isdigit(*p)) {
  624.             exp = exp * 10 + (*p - '0');
  625.             p++;
  626.         }
  627.     }
  628.     if (*p == '.') {
  629.         fpos = 1.0/10.0;
  630.         p++;
  631.         while(isdigit(*p)) {
  632.             acc += (*p - '0') * fpos;
  633.             fpos *= 1.0/10.0;
  634.             p++;
  635.         }
  636.     }
  637.     if (*p == 'e' || *p == 'E') {
  638.         exp = 0;
  639.         exps = 1;
  640.         p++;
  641.         if (*p == '+')
  642.             p++;
  643.         else if (*p == '-') {
  644.             p++;
  645.             exps = -1;
  646.         }
  647.         while(isdigit(*p)) {
  648.             exp = exp * 10 + (*p - '0');
  649.             p++;
  650.         }
  651.     }
  652.     if (exp) {
  653.         if (exps > 0)
  654.             while (exp--)
  655.                 acc *= 10.0;
  656.         else
  657.             while (exp--)
  658.                 acc *= 1.0/10.0;
  659.     }
  660.     if (sign > 0)
  661.         *res = acc;
  662.     else
  663.         *res = -acc;
  664.  
  665. #ifndef OSK
  666.     (void) signal(SIGFPE, sig_save);
  667. #endif
  668.     return(p);
  669. }
  670.