home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / pac / conv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  13.4 KB  |  523 lines

  1. /* conv.c */
  2. /**********************************************************************
  3. *    File Name     : conv.c
  4. *    Function      : user conversion routines of pac
  5. *                  : Recursion is too big a task to fit the framework
  6. *                  :  (i.e. defining 'to foo' as 'to {to foo}' brings
  7. *                  :  up all sorts of control problems).  To simplify,
  8. *                  :  only numerical expressions and binary operands
  9. *                  :  are permitted, in a single statement (no ';').
  10. *                  : 
  11. *    Author        : Istvan Mohos, 1987
  12. ***********************************************************************/
  13.  
  14.  
  15. #include "defs.h"
  16. #define CONVMAP
  17. #include "maps.h"
  18. #undef CONVMAP
  19.  
  20. show_uconv()
  21. {
  22.     register ri;
  23.     static char *fid = "show_uconv";
  24.  
  25.     _TR
  26.     if (Convsel < CENTER)
  27.         Topconv = 0;
  28.     else if (Convsel >= Convcount - CENTER)
  29.         Topconv = Convcount - (2 * CENTER);
  30.     else
  31.         Topconv = Convsel - CENTER + 1;
  32.  
  33.     for (ri = 0; ri < FITCONV; ri++) {
  34.         mvaddstr(ri + TOP+1, CONVLEFT, Convlist[ri + Topconv][0]);
  35.         addch(' ');
  36.         mvaddstr(ri + TOP+1, KEYLEFT, Convlist[ri + Topconv][1]);
  37.     }
  38.  
  39.     standout();
  40.     for (ri = 0; ri < FITCONV; ri++)
  41.         mvaddch(UTOP + ri, RBOUND, ' ');
  42.  
  43.     mvaddstr(TOP+1 + Convsel - Topconv, CONVLEFT, Convlist[Convsel][0]);
  44.     standend();
  45.     TR_
  46. }
  47.  
  48. setup_uconv()
  49. {
  50.     register char *rc;
  51.     register int ri;
  52.     static char *fid = "setup_uconv";
  53.  
  54.     _TR
  55.     Convhsiz = Convcount * (FROMTOSIZ + KEYSIZ);
  56.     for (ri = 0; ri < Convcount; ri++)
  57.         Convhsiz += strlen(Convlist[ri][2]) + 1; /* conv string + \n */
  58.     if ((Convhom = calloc(Convhsiz, 1)) == ZERO)
  59.         fatal("calloc error in setup_uconv");
  60.  
  61.     /* move static defs to dynamically alterable Convhom buffer */
  62.     for (rc = Convhom, ri = 0; ri < Convcount; ri++) {
  63.         strcpy(rc, Convlist[ri][0]);
  64.         rc += FROMTOSIZ;
  65.         strcpy(rc, Convlist[ri][1]);
  66.         rc += KEYSIZ;
  67.         strcpy(rc, Convlist[ri][2]);
  68.         while (*rc++ != '\0');
  69.     }
  70.     realign_conv();
  71.     TR_
  72. }
  73.  
  74. conv_id(c_ptr)
  75. char *c_ptr;
  76. {
  77.     int inlist_val, under;
  78.     static char *fid = "conv_id";
  79.  
  80.     _TR
  81.     if ((inlist_val = spacefill(c_ptr, 3)) != -1)
  82.         inlist_val = keysearch(Tokbuf, Convcount, &under);
  83.     TR_
  84.     return(inlist_val);
  85. }
  86.  
  87. keysearch (key, max, under)
  88. register char *key;
  89. int max, *under;
  90. {
  91.     register int guess, mid, lo = 0, hi;
  92.     static char *fid = "keysearch";
  93.  
  94.     _TR
  95.     hi = max - 1;
  96.     while (lo <= hi) {
  97.         mid = (lo + hi) >> 1;
  98.         if ((guess = strcmp(key, Convlist[mid][1])) < 0)
  99.             hi = mid - 1;
  100.         else if (guess > 0)
  101.             lo = mid + 1;
  102.         else {
  103.             TR_
  104.             return(mid);
  105.         }
  106.     }
  107.     *under = lo;
  108.     TR_
  109.     return (-1);
  110. }
  111.  
  112. newconv()
  113. {
  114.     int c;
  115.     int lbound;
  116.     char calbuf[LINEMAX];
  117.     int pyp, pxp;
  118.     int ri;
  119.     register int rj;
  120.     register char *rc;
  121.     static char *fid = "newconv";
  122.  
  123.     _TR
  124.     CYX;
  125.     rc = calbuf;
  126.     for (ri = UTOP; ri <= UBOT; ri++)
  127.         for (rj = ULEFT; rj <= URIGHT; rj++)
  128.             *rc++ = stdscr->_y[ri][rj];
  129.  
  130.     mvaddstr(UTOP, ULEFT, Sp44);
  131.     mvaddstr(UTOP + 1, ULEFT, Sp44);
  132.     mvaddstr(UTOP + 2, ULEFT, Sp44);
  133.  
  134.     if (Convcount == 255) {
  135.         mvaddstr(UTOP, ULEFT,
  136.             "SELECT:   ...full...    Remove     Change  _");
  137.         lbound = ULEFT + strlen(
  138.             "SELECT:   ...full...    Remove     Change  ");
  139.  
  140.         while((c = ledit(ZERO,c_sel_map,UTOP,lbound,lbound,0,0,0)
  141.             | 32) != 'r' && c != 'c');
  142.     }
  143.     else if (Convcount == 22) {
  144.         mvaddstr(UTOP, ULEFT,
  145.             "SELECT:     Install   ...low...    Change  _");
  146.         lbound = ULEFT + strlen(
  147.             "SELECT:     Install   ...low...    Change  ");
  148.  
  149.         while((c = ledit(ZERO,c_sel_map,UTOP,lbound,lbound,0,0,0)
  150.             | 32) != 'i' && c != 'c');
  151.     }
  152.     else {
  153.         mvaddstr(UTOP, ULEFT,
  154.             "SELECT:     Install     Remove     Change  _");
  155.         lbound = ULEFT + strlen(
  156.             "SELECT:     Install     Remove     Change  ");
  157.  
  158.         while((c = ledit(ZERO,c_sel_map,UTOP,lbound,lbound,0,0,0)
  159.             | 32) != 'i' && c != 'r' && c != 'c');
  160.     }
  161.  
  162.     mvaddstr(UTOP, ULEFT, Sp44);
  163.     mvaddstr(UTOP + 1, ULEFT, Sp44);
  164.     mvaddstr(UTOP + 2, ULEFT, Sp44);
  165.  
  166.     switch(c) {
  167.         case 'i':
  168.             addnew(specify(pyp, pxp));
  169.             break;
  170.  
  171.         case 'r':
  172.             remove(celect());
  173.             break;
  174.  
  175.         case 'c':
  176.             remove(celect());
  177.             addnew(specify(pyp, pxp));
  178.             break;
  179.     }
  180.  
  181.     rc = calbuf;
  182.     for (ri = UTOP; ri <= UBOT; ri++)
  183.         for (rj = ULEFT; rj <= URIGHT; rj++)
  184.             mvaddch(ri, rj, *rc++);
  185.  
  186.     PYX;
  187.     pfresh();
  188.     TR_
  189. }
  190.  
  191. celect()
  192. {
  193.     int lbound, rbound;
  194.     char *c_key;
  195.     int keylen, old_id, dummy;
  196.     static char *fid = "celect";
  197.  
  198.     _TR
  199.     clear_wline(UTOP, ULEFT, URIGHT, 0, 3);
  200.     c_key = "   ";
  201.     mvaddstr(UTOP, ULEFT,
  202.         "ENTER OLD 'TO' SEARCH KEY:   [");
  203.     lbound = ULEFT + strlen(
  204.         "ENTER OLD 'TO' SEARCH KEY:   [");
  205.     rbound = lbound + 2;
  206.     mvaddstr(UTOP, rbound + 1, "]");
  207.  
  208.     old_id = -1;
  209.     while (old_id == -1) {
  210.         mvaddstr(UTOP, lbound, c_key);
  211.     
  212.  
  213.         ledit(&K_buf[0], c_ed_map, UTOP, lbound, rbound, 0, 1, 1);
  214.         /* stripping surrounding spaces */
  215.  
  216.         if(!*K_buf)
  217.             continue;
  218.         keylen = strlen(K_buf);
  219.         if (keylen < 3)
  220.             K_buf[2] = ' ';
  221.         else if (*(K_buf+1) == ' ')
  222.             *(K_buf+1) = '_'; /* turn embedded space into _ */
  223.         if (keylen < 2)
  224.             K_buf[1] = ' ';
  225.         old_id = keysearch(K_buf, Convcount, &dummy);
  226.     }
  227.     TR_
  228.     return(old_id);
  229. }
  230.  
  231. specify(pyp, pxp)
  232. int pyp, pxp;
  233. {
  234.     int lbound, rbound;
  235.     char *c_label, *c_key;
  236.     int neednewkey = 1, keylen, insert;
  237.     static char *fid = "specify";
  238.  
  239.     _TR
  240.     clear_wline(UTOP, ULEFT, URIGHT, 0, 3);
  241.     while (neednewkey) {
  242.         c_key = "   ";
  243.         mvaddstr(UTOP + 1, ULEFT,
  244.             "ENTER NEW 'TO' SEARCH KEY:   [");
  245.         lbound = ULEFT + strlen(
  246.             "ENTER NEW 'TO' SEARCH KEY:   [");
  247.         mvaddstr(UTOP + 1, lbound, c_key);
  248.         rbound = lbound + 2;
  249.         mvaddstr(UTOP + 1, rbound + 1, "]");
  250.     
  251.  
  252.         ledit(&K_buf[0], c_ed_map, UTOP+1, lbound, rbound, 0, 1, 1);
  253.         /* stripping surrounding spaces */
  254.  
  255.         if (keylen = strlen(K_buf)) {
  256.             if (keylen < 3)
  257.                 K_buf[2] = ' ';
  258.             else if (*(K_buf+1) == ' ')
  259.                 *(K_buf+1) = '_'; /* turn emmbedded space into _ */
  260.             if (keylen < 2)
  261.                 K_buf[1] = ' ';
  262.         }
  263.         else
  264.             continue;
  265.         if (keysearch(K_buf, Convcount, &insert) == -1)
  266.             neednewkey = FALSE;
  267.         /* if it did find it, the proposed new key is a duplicate,
  268.            and is disallowed */
  269.     }
  270.  
  271.     c_label = "             ";
  272.     mvaddstr(UTOP + 2, ULEFT, "ENTER 'CONVERSIONS' LABEL:   [");
  273.     lbound = ULEFT + strlen( "ENTER 'CONVERSIONS' LABEL:   [");
  274.     mvaddstr(UTOP + 2, lbound, c_label);
  275.     rbound = lbound + 12;
  276.     mvaddstr(UTOP + 2, rbound + 1, "]");
  277.  
  278.  
  279.     ledit(&L_buf[0], c_ed_map, UTOP+2, lbound, rbound, 0, 0, 1);
  280.  
  281.     mvaddstr(UTOP, ULEFT, Sp44);
  282.     mvaddstr(UTOP + 1, ULEFT, Sp44);
  283.     mvaddstr(UTOP + 2, ULEFT, Sp44);
  284.  
  285.     mvaddstr(UTOP, ULEFT,
  286.         "COMPLETE THE EQUATION USING THE BACKSLASH \\");
  287.     mvaddstr(UTOP + 1, ULEFT,
  288.         "CHARACTER TO REPRESENT THE 'FROM' QUANTITY.");
  289.     mvaddstr(UTOP + 2, ULEFT,
  290.         "CONSTANTS ARE INTERPRETED IN DECIMAL RADIX.");
  291.  
  292.     Basq[EDITREQ] = Bb[EDITREQ];
  293.     update();  /* this returns to the original coordinates,
  294.                   but does'nt pfresh */
  295.  
  296. enter_conv:
  297.  
  298.     ledit(&Eq_buf[0], eq_ed_map, BOT, FBOUND, RBOUND, 1, 1, 1);
  299.  
  300.     if (verify_uconv())
  301.         goto enter_conv;
  302.  
  303.     Basq[EDITREQ] = ZERO;
  304.     PYX;
  305.     update();
  306.     pfresh();
  307.     TR_
  308.     return(insert);
  309. }
  310.  
  311. realign_conv()
  312. {
  313.     register int ri;
  314.     register char *rc;
  315.     static char *fid = "realign_conv";
  316.  
  317.     _TR
  318.     for (rc = Convhom, ri = 0; ri < Convcount; ri++) {
  319.         Convlist[ri][0] = rc; /* ptr to first word of conversion */
  320.         rc += FROMTOSIZ;         /* buffer pointer to second word */
  321.         Convlist[ri][1] = rc; /* ptr to 2nd word of conversion */
  322.         rc += KEYSIZ;            /* buffer pointer to conv string */
  323.         Convlist[ri][2] = rc; /* ptr to conversion string */
  324.         while (*rc++ != '\0');   /* find next conversion in buffer */
  325.     }
  326.     TR_
  327. }
  328.  
  329. remove(old_id)
  330. int old_id;
  331. {
  332. /*
  333.    malloc Convhsiz size temp buffer;
  334.    copy Convhom into temp until old_id conversion;
  335.    skip (do not copy) old_id conversion;
  336.    copy rest of Convhom into temp;
  337.    free Convhom;
  338.    assign Convhom pointer to temp;
  339.    decrement Convcount;
  340.    adjust Convhsiz;
  341.    realign Convlist pointers along new buffer;
  342. */
  343.  
  344.     char *Tmp;
  345.     register char *tp;
  346.     unsigned rmsiz;
  347.     register int ri;
  348.     static char *fid = "remove";
  349.  
  350.     _TR
  351.     if ((Tmp = malloc(Convhsiz)) == ZERO)
  352.         fatal("malloc error in remove");
  353.     tp = Tmp;
  354.  
  355.     for (ri = 0; ri < old_id; ri++) {
  356.         strcpy(tp, Convlist[ri][0]);
  357.         tp += FROMTOSIZ;
  358.         strcpy(tp, Convlist[ri][1]);
  359.         tp += KEYSIZ;
  360.         strcpy(tp, Convlist[ri][2]);
  361.         while (*tp++ != '\0');
  362.     }
  363.     rmsiz = FROMTOSIZ + KEYSIZ + strlen(Convlist[ri][2]) + 1; 
  364.     for (ri = old_id + 1; ri < Convcount; ri++) {
  365.         strcpy(tp, Convlist[ri][0]);
  366.         tp += FROMTOSIZ;
  367.         strcpy(tp, Convlist[ri][1]);
  368.         tp += KEYSIZ;
  369.         strcpy(tp, Convlist[ri][2]);
  370.         while (*tp++ != '\0');
  371.     }
  372.     free(Convhom);
  373.     Convhom = Tmp;
  374.     --Convcount;
  375.     Convhsiz -= rmsiz;
  376.     realign_conv();
  377.     if (Convsel > old_id)
  378.         --Convsel;
  379.     else if (Convsel == old_id)
  380.         Convsel = CONVSEL;
  381.     show_uconv();
  382.     TR_
  383. }
  384.  
  385. addnew(insert)
  386. int insert;
  387. {
  388. /*
  389.    Malloc new buffer, to hold Convhom + size of new conversion.
  390.    Copy Convhom into new buffer, until the alphabetic ordering
  391.        of KEYs reaches the new KEY.
  392.    Insert new conversion in new buffer.
  393.    Finish copying old conversions from Convhom to new buffer.
  394.    Free Convhom.
  395.    Assign Convhom (pointer) to new buffer.
  396.    Increment Convcount.
  397.    adjust Convhsiz;
  398.    Realign Convlist pointers along new buffer.
  399. */
  400.  
  401.     char *Tmp;
  402.     register char *tp;
  403.     unsigned addsiz;
  404.     register int ri;
  405.     static char *fid = "addnew";
  406.  
  407.     _TR
  408.     addsiz = FROMTOSIZ + KEYSIZ + strlen(Eq_buf) + 1; 
  409.     if ((Tmp = malloc(Convhsiz + addsiz)) == ZERO)
  410.         fatal("malloc error in addnew");
  411.     tp = Tmp;
  412.  
  413.     for (ri = 0; ri < insert; ri++) {
  414.         strcpy(tp, Convlist[ri][0]);
  415.         tp += FROMTOSIZ;
  416.         strcpy(tp, Convlist[ri][1]);
  417.         tp += KEYSIZ;
  418.         strcpy(tp, Convlist[ri][2]);
  419.         while (*tp++ != '\0');
  420.     }
  421.     strcpy(tp, L_buf);
  422.     tp += FROMTOSIZ;
  423.     strcpy(tp, K_buf);
  424.     tp += KEYSIZ;
  425.     strcpy(tp, Eq_buf);
  426.     while (*tp++ != '\0');
  427.     for (ri = insert; ri < Convcount; ri++) {
  428.         strcpy(tp, Convlist[ri][0]);
  429.         tp += FROMTOSIZ;
  430.         strcpy(tp, Convlist[ri][1]);
  431.         tp += KEYSIZ;
  432.         strcpy(tp, Convlist[ri][2]);
  433.         while (*tp++ != '\0');
  434.     }
  435.     free(Convhom);
  436.     Convhom = Tmp;
  437.     ++Convcount;
  438.     Convhsiz += addsiz;
  439.     realign_conv();
  440.     Convsel = insert;
  441.     show_uconv();
  442.     TR_
  443. }
  444.  
  445. verify_uconv()
  446. {
  447.     static char *fid = "verify_uconv";
  448.  
  449.     _TR
  450.     if (strlen(Eq_buf) == 0) {
  451.         strcpy(Eq_buf, "\\");
  452.         TR_
  453.         return(0);
  454.     }
  455.  
  456.     TR_
  457.     return(0);
  458. }
  459.  
  460. /* the routine that interprets the user keyword TO */
  461. conv_usr()
  462. {
  463.     char *ctab, *cb, cbuf[LINEMAX];
  464.     char hardbuf[LINEMAX];
  465.     int ri;
  466.     int sav_ibase;
  467.     static char *fid = "conv_usr";
  468.  
  469.     _TR
  470.     e_syntax();
  471.     e_divby0();
  472.     e_exponent();
  473.     e_bcexec();
  474.  
  475.     if (!Bc_error) {
  476.         sav_ibase = Ibase;
  477.         Ibase = 10;
  478.         conv_bc(Mainbuf, ZERO, 0, 0);
  479.         *(Convbuf + DIGMAX + 2) = '\0';
  480.         ri = strlen(Convbuf);
  481.         Ibase = sav_ibase;
  482.     
  483.         cb = cbuf;
  484.         strcpy(cb, "ibase=A;");
  485.         cb += strlen(cb);
  486.     
  487.         ctab = Convlist[Convsel][2];
  488.         sprintf(Bb[CONVREQ] + BUFSTOP, ctab);
  489.         rev_clear(Bb[CONVREQ] + TITSIZ);
  490.         Basq[CONVREQ] = Bb[CONVREQ];
  491.         update();
  492.     
  493.         if ( Hc != -1 && Hf != FXTER) {
  494.             if (Hf == FVER)
  495.                 sprintf(hardbuf, "TO %s: %s\n", Convlist[Convsel][1], ctab);
  496.             else
  497.                 sprintf(hardbuf, "TO %s\n", Convlist[Convsel][1]);
  498.             if ((write(Hc, hardbuf, strlen(hardbuf))) !=
  499.                 strlen(hardbuf))
  500.                 fatal("hardcopy write");
  501.         }
  502.     
  503.         while (*ctab != '\0') {
  504.             if (*ctab == '\\') {
  505.                 strcpy(cb, Convbuf);
  506.                 cb += ri;
  507.                 ctab++;
  508.             }
  509.             else
  510.                 *cb++ = *ctab++;
  511.         }
  512.         sprintf(cb, ";ibase=%d\n", Ibase);
  513.         if (write(A_write[1], cbuf, strlen(cbuf)) == -1)
  514.             fatal("main pipe write");
  515.         clear_accwin();
  516.         wait_main_pipe();
  517.     }
  518.     if (Autoconv == DISA)
  519.         Do_conv = FALSE;
  520.     TR_
  521. }
  522.  
  523.