home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / misc~1 / 198 / viscalc / sc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-10  |  18.8 KB  |  855 lines

  1. /*    SC    A Spreadsheet Calculator
  2.  
  3.  *        Main driver
  4.  
  5.  *
  6.  
  7.  *        original by James Gosling, September 1982
  8.  
  9.  *        modifications by Mark Weiser and Bruce Israel,
  10.  
  11.  *            University of Maryland
  12.  
  13.  *
  14.  
  15.  *              More mods Robert Bond, 12/86
  16.  
  17.  *        Major mods to run on VMS and AMIGA, 1/17/87
  18.  
  19.  *
  20.  
  21.  */
  22.  
  23.  
  24.  
  25. #include "sc.h"
  26.  
  27.  
  28.  
  29. extern char *malloc();
  30.  
  31.  
  32.  
  33. /* default column width */
  34.  
  35.  
  36.  
  37. #define DEFWIDTH 10
  38.  
  39. #define DEFPREC   2
  40.  
  41.  
  42.  
  43. #define RESCOL 4  /* columns reserved for row numbers */
  44.  
  45. #define RESROW 3  /* rows reserved for prompt, error, and column numbers */
  46.  
  47.  
  48.  
  49. char curfile[1024];
  50.  
  51.  
  52.  
  53. int showme = 1;  /* 1 to display the current cell in the top line */
  54.  
  55. int batch = 0;
  56.  
  57.  
  58.  
  59. error (fmt,a,b,c,d,e) {
  60.  
  61.     if (batch) fprintf(stderr,fmt,a,b,c,d,e);
  62.  
  63.     else {
  64.  
  65.     move (1,0);
  66.  
  67.     clrtoeol ();
  68.  
  69.     printw (fmt,a,b,c,d,e);
  70.  
  71.     }
  72.  
  73. }
  74.  
  75.  
  76.  
  77. int seenerr;
  78.  
  79.  
  80.  
  81. yyerror (err)
  82.  
  83. char *err; {
  84.  
  85.     if (seenerr) return;
  86.  
  87.     seenerr++;
  88.  
  89.     move (1,0);
  90.  
  91.     clrtoeol ();
  92.  
  93. #ifdef TOS
  94.  
  95.     {
  96.  
  97.         char format[20];
  98.  
  99.         sprintf(format,"%%s: %%.%ds <= %%s",linelim);
  100.  
  101.         printw(format,err,line,line+linelim);
  102.  
  103.     }
  104.  
  105. #else
  106.  
  107.     printw ("%s: %.*s <= %s",err,linelim,line,line+linelim);
  108.  
  109. #endif
  110.  
  111. }
  112.  
  113.  
  114.  
  115. struct ent *
  116.  
  117. lookat(row,col){
  118.  
  119.     register struct ent **p;
  120.  
  121.     if (row < 0)
  122.  
  123.     row = 0;
  124.  
  125.     else if (row > MAXROWS-1) 
  126.  
  127.     row = MAXROWS-1;
  128.  
  129.     if (col < 0) 
  130.  
  131.     col = 0;
  132.  
  133.     else if (col > MAXCOLS-1)
  134.  
  135.     col = MAXCOLS-1;
  136.  
  137.     p = &tbl[row][col];
  138.  
  139.     if (*p==0) {
  140.  
  141.     *p = (struct ent *) malloc (sizeof (struct ent));
  142.  
  143.     if (row>maxrow) maxrow = row;
  144.  
  145.     if (col>maxcol) maxcol = col;
  146.  
  147.     (*p)->label = 0;
  148.  
  149.     (*p)->flags = 0;
  150.  
  151.     (*p)->row = row;
  152.  
  153.     (*p)->col = col;
  154.  
  155.     (*p)->expr = 0;
  156.  
  157.     (*p)->v = (double) 0.0;
  158.  
  159.     } else if ((*p)->flags & is_deleted) 
  160.  
  161.     debug("But %s%d has been deleted!", coltoa(col), row);
  162.  
  163.     return *p;
  164.  
  165. }
  166.  
  167.  
  168.  
  169. /*
  170.  
  171.  * This structure is used to keep ent structs around before they
  172.  
  173.  * are deleted to allow the sync_refs routine a chance to fix the
  174.  
  175.  * variable references.
  176.  
  177.  * We also use it as a last-deleted buffer for the 'p' command.
  178.  
  179.  */
  180.  
  181.  
  182.  
  183. free_ent(p)
  184.  
  185. register struct ent *p;
  186.  
  187. {
  188.  
  189.     p->next = to_fix;
  190.  
  191.     to_fix = p;
  192.  
  193.     p->flags |= is_deleted;
  194.  
  195. }
  196.  
  197.  
  198.  
  199. flush_saved()
  200.  
  201. {
  202.  
  203.     register struct ent *p;
  204.  
  205.     register struct ent *q;
  206.  
  207.  
  208.  
  209.     if (!(p = to_fix))
  210.  
  211.     return;
  212.  
  213.     while (p) {
  214.  
  215.     clearent(p);
  216.  
  217.     q = p->next;
  218.  
  219.     free(p);
  220.  
  221.     p = q;
  222.  
  223.     }
  224.  
  225.     to_fix = 0;
  226.  
  227. }
  228.  
  229.  
  230.  
  231. update () {
  232.  
  233.     register    row,
  234.  
  235.                 col;
  236.  
  237.     register struct ent **p;
  238.  
  239.     static  lastmx,
  240.  
  241.             lastmy;
  242.  
  243.     static  char *under_cursor = " ";
  244.  
  245.     int     maxcol;
  246.  
  247.     int     maxrow;
  248.  
  249.     int     rows;
  250.  
  251.     int     cols;
  252.  
  253.     register r;
  254.  
  255.  
  256.  
  257.     while (hidden_row[currow])   /* You can't hide the last row or col */
  258.  
  259.     currow++;
  260.  
  261.     while (hidden_col[curcol])
  262.  
  263.     curcol++;
  264.  
  265.     if (curcol < stcol)
  266.  
  267.     stcol = curcol, FullUpdate++;
  268.  
  269.     if (currow < strow)
  270.  
  271.     strow = currow, FullUpdate++;
  272.  
  273.     while (1) {
  274.  
  275.     register    i;
  276.  
  277.     for (i = stcol, cols = 0, col = RESCOL;
  278.  
  279.          (col + fwidth[i]) < COLS-1 && i < MAXCOLS; i++) {
  280.  
  281.         cols++;
  282.  
  283.         if (hidden_col[i])
  284.  
  285.         continue;
  286.  
  287.         col += fwidth[i];
  288.  
  289.     }
  290.  
  291.     if (curcol >= stcol + cols)
  292.  
  293.         stcol++, FullUpdate++;
  294.  
  295.     else
  296.  
  297.         break;
  298.  
  299.     }
  300.  
  301.     while (1) {
  302.  
  303.     register    i;
  304.  
  305.     for (i = strow, rows = 0, row = RESROW;
  306.  
  307.          row < ROWS && i < MAXROWS; i++) {
  308.  
  309.         rows++;
  310.  
  311.         if (hidden_row[i])
  312.  
  313.         continue;
  314.  
  315.         row++;
  316.  
  317.     }
  318.  
  319.     if (currow >= strow + rows)
  320.  
  321.         strow++, FullUpdate++;
  322.  
  323.     else
  324.  
  325.         break;
  326.  
  327.     }
  328.  
  329.     maxcol = stcol + cols - 1;
  330.  
  331.     maxrow = strow + rows - 1;
  332.  
  333.     if (FullUpdate) {
  334.  
  335.     register int i;
  336.  
  337.     move (2, 0);
  338.  
  339.     clrtobot ();
  340.  
  341.     standout();
  342.  
  343.     for (row=RESROW, i=strow; i <= maxrow; i++) {
  344.  
  345.         if (hidden_row[i]) 
  346.  
  347.         continue;
  348.  
  349.         move(row,0);
  350.  
  351. #ifdef TOS
  352.  
  353.         {
  354.  
  355.             char format[10];
  356.  
  357.             sprintf(format,"%%-%dd",RESCOL);
  358.  
  359.             printw(format,i);
  360.  
  361.         }
  362.  
  363. #else
  364.  
  365.         printw("%-*d", RESCOL, i);
  366.  
  367. #endif
  368.  
  369.         row++;
  370.  
  371.     }
  372.  
  373.     move (2,0);
  374.  
  375. #ifdef TOS
  376.  
  377.     {
  378.  
  379.         char format[10];
  380.  
  381.         sprintf(format,"%%%ds",RESCOL);
  382.  
  383.         printw(format," ");
  384.  
  385.     }
  386.  
  387. #else
  388.  
  389.     printw("%*s", RESCOL, " ");
  390.  
  391. #endif
  392.  
  393.     for (col=RESCOL, i = stcol; i <= maxcol; i++) {
  394.  
  395.         if (hidden_col[i])
  396.  
  397.         continue;
  398.  
  399.         move(2, col);
  400.  
  401. #ifdef TOS
  402.  
  403.     {
  404.  
  405.         char format[10];
  406.  
  407.         sprintf(format,"%%%ds",fwidth[i]);
  408.  
  409.         printw(format,coltoa(i));
  410.  
  411.     }
  412.  
  413. #else
  414.  
  415.         printw("%*s", fwidth[i], coltoa(i));
  416.  
  417. #endif
  418.  
  419.         col += fwidth[i];
  420.  
  421.     }
  422.  
  423.     standend();
  424.  
  425.     }
  426.  
  427.     for (row = strow, r = RESROW; row <= maxrow; row++) {
  428.  
  429.     register    c = RESCOL;
  430.  
  431.     if (hidden_row[row])
  432.  
  433.         continue;
  434.  
  435.     for (p = &tbl[row][col = stcol]; col <= maxcol; col++, p++) {
  436.  
  437.         if (hidden_col[col])
  438.  
  439.         continue;
  440.  
  441.         if (*p && ((*p) -> flags & is_changed || FullUpdate)) {
  442.  
  443.         char   *s;
  444.  
  445.         move (r, c);
  446.  
  447.         (*p) -> flags &= ~is_changed;
  448.  
  449.         if ((*p) -> flags & is_valid)
  450.  
  451. #ifdef TOS
  452.  
  453.             {
  454.  
  455.                 char format[16];
  456.  
  457.                 sprintf(format,"%%%d.%df",fwidth[col],precision[col]);
  458.  
  459.                 printw(format,(*p)->v);
  460.  
  461.             }
  462.  
  463. #else
  464.  
  465.             printw ("%*.*f", fwidth[col], precision[col], (*p) -> v);
  466.  
  467. #endif
  468.  
  469.         if (s = (*p) -> label) {
  470.  
  471.             char field[1024];
  472.  
  473.  
  474.  
  475.             strncpy(field,s,fwidth[col]);
  476.  
  477.             field[fwidth[col]] = 0;
  478.  
  479.             move (r,(*p) -> flags & is_leftflush
  480.  
  481.                 ? c : c - strlen (field) + fwidth[col]);
  482.  
  483.             addstr(field);
  484.  
  485.         }
  486.  
  487.         }
  488.  
  489.         c += fwidth[col];
  490.  
  491.     }
  492.  
  493.     r++;
  494.  
  495.     }
  496.  
  497.     
  498.  
  499.     move(lastmy, lastmx);
  500.  
  501.     if (inch() == '<')
  502.  
  503.         addstr (under_cursor);
  504.  
  505.     lastmy =  RESROW;
  506.  
  507.     for (row = strow; row < currow; row++)
  508.  
  509.     if (!hidden_row[row])
  510.  
  511.         lastmy += 1;
  512.  
  513.     lastmx = RESCOL;
  514.  
  515.     for (col = stcol; col <= curcol; col++)
  516.  
  517.     if (!hidden_col[col])
  518.  
  519.         lastmx += fwidth[col];
  520.  
  521.     move(lastmy, lastmx);
  522.  
  523.     *under_cursor = inch();
  524.  
  525.     addstr ("<");
  526.  
  527.     move (0, 0);
  528.  
  529.     clrtoeol ();
  530.  
  531.     if (linelim >= 0) {
  532.  
  533.     addstr (">> ");
  534.  
  535.     addstr (line);
  536.  
  537.     } else {
  538.  
  539.     if (showme) {
  540.  
  541.         register struct ent *p;
  542.  
  543.         p = tbl[currow][curcol];
  544.  
  545.         if (p && ((p->flags & is_valid) || p->label)) {
  546.  
  547.         if (p->expr || !p->label) {
  548.  
  549.             linelim = 0;
  550.  
  551.             editexp(currow, curcol);
  552.  
  553.         } else {
  554.  
  555.             sprintf(line, "%s", p->label);
  556.  
  557.         }
  558.  
  559.         addstr("[");
  560.  
  561.         addstr (line);
  562.  
  563.         addstr("]");
  564.  
  565.         linelim = -1;
  566.  
  567.         } else {
  568.  
  569.         addstr("[]");
  570.  
  571.         }
  572.  
  573.     }
  574.  
  575.     move (lastmy, lastmx);
  576.  
  577.     }
  578.  
  579.     FullUpdate = 0;
  580.  
  581. }
  582.  
  583.  
  584.  
  585. void
  586.  
  587. quit()
  588.  
  589. {
  590.  
  591.     endwin ();
  592.  
  593.     exit();
  594.  
  595. }
  596.  
  597.  
  598.  
  599. main (argc, argv)
  600.  
  601. char  **argv; {
  602.  
  603.     int     inloop = 1;
  604.  
  605.     register int   c;
  606.  
  607.     int     edistate = -1;
  608.  
  609.     int     arg = 1;
  610.  
  611.     int     narg;
  612.  
  613.     int     nedistate = -1;
  614.  
  615.     int        running = 1;
  616.  
  617.     {
  618.  
  619.     register    i;
  620.  
  621.     for (i = 0; i < MAXCOLS; i++) {
  622.  
  623.         fwidth[i] = DEFWIDTH;
  624.  
  625.         precision[i] = DEFPREC;
  626.  
  627.     }
  628.  
  629.     }
  630.  
  631.     linelim = -1;
  632.  
  633.     curfile[0]=0;
  634.  
  635.     running = 1;
  636.  
  637.  
  638.  
  639.     if (argc > 1 && (! strcmp(argv[1],"-b"))) {
  640.  
  641.     argc--, argv++;
  642.  
  643.     batch = 1;
  644.  
  645.     }
  646.  
  647.  
  648.  
  649.     if (! batch) {
  650.  
  651.     initscr ();
  652.  
  653.     }
  654.  
  655.     initkbd();
  656.  
  657.     if (argc > 1) {
  658.  
  659.     strcpy(curfile,argv[1]);
  660.  
  661.     readfile (argv[1],0);
  662.  
  663.     }
  664.  
  665.     modflg = 0;
  666.  
  667.     if (batch) exit(0);
  668.  
  669.     error ("VC 2.1  Type '?' for help.");
  670.  
  671.     FullUpdate++;
  672.  
  673.     while (inloop) { running = 1;
  674.  
  675.     while (running) {
  676.  
  677.     nedistate = -1;
  678.  
  679.     narg = 1;
  680.  
  681.     if (edistate < 0 && linelim < 0 && (changed || FullUpdate))
  682.  
  683.         EvalAll (), changed = 0;
  684.  
  685.     update ();
  686.  
  687.     refresh ();
  688.  
  689.     move (1, 0);
  690.  
  691.     clrtoeol ();
  692.  
  693.     fflush (stdout);
  694.  
  695.     seenerr = 0;
  696.  
  697.     if (((c = nmgetch ()) < ' ') || ( c == 0177 ))
  698.  
  699.         switch (c) {
  700.  
  701.         case ctl (z):
  702.  
  703.             quit();
  704.  
  705.             break;
  706.  
  707.         case ctl (r):
  708.  
  709.             FullUpdate++;
  710.  
  711.             touchwin();
  712.  
  713.             clear();
  714.  
  715.             break;
  716.  
  717.         default:
  718.  
  719.             error ("No such command  (^%c)", c + 0100);
  720.  
  721.             break;
  722.  
  723.         case ctl (b):
  724.  
  725.             while (--arg>=0) {
  726.  
  727.             if (curcol)
  728.  
  729.                 curcol--;
  730.  
  731.             else
  732.  
  733.                 error ("At column A");
  734.  
  735.             while(hidden_col[curcol] && curcol)
  736.  
  737.                 curcol--;
  738.  
  739.             }
  740.  
  741.             break;
  742.  
  743.         case ctl (c):
  744.  
  745.             running = 0;
  746.  
  747.             break;
  748.  
  749.         case ctl (f):
  750.  
  751.             while (--arg>=0) {
  752.  
  753.             if (curcol < MAXCOLS - 1)
  754.  
  755.                 curcol++;
  756.  
  757.             else
  758.  
  759.                 error ("The table can't be any wider");
  760.  
  761.             while(hidden_col[curcol]&&(curcol<MAXCOLS-1))
  762.  
  763.                 curcol++;
  764.  
  765.             }
  766.  
  767.             break;
  768.  
  769.         case ctl (g):
  770.  
  771.         case ctl ([):
  772.  
  773.             linelim = -1;
  774.  
  775.             move (1, 0);
  776.  
  777.             clrtoeol ();
  778.  
  779.             break;
  780.  
  781.         case 0177:
  782.  
  783.         case ctl (h):
  784.  
  785.             while (--arg>=0) if (linelim > 0)
  786.  
  787.             line[--linelim] = 0;
  788.  
  789.             break;
  790.  
  791.         case ctl (l):
  792.  
  793.             FullUpdate++;
  794.  
  795.             break;
  796.  
  797.         case ctl (m):
  798.  
  799.             if (linelim < 0)
  800.  
  801.             line[linelim = 0] = 0;
  802.  
  803.             else {
  804.  
  805.             linelim = 0;
  806.  
  807.             yyparse ();
  808.  
  809.             linelim = -1;
  810.  
  811.             }
  812.  
  813.             break;
  814.  
  815.         case ctl (n):
  816.  
  817.             while (--arg>=0) {
  818.  
  819.             if (currow < MAXROWS - 1)
  820.  
  821.                 currow++;
  822.  
  823.             else
  824.  
  825.                 error ("The table can't be any longer");
  826.  
  827.             while (hidden_row[currow] && (currow < MAXROWS - 1))
  828.  
  829.                 currow++;
  830.  
  831.             }
  832.  
  833.             break;
  834.  
  835.         case ctl (p):
  836.  
  837.             while (--arg>=0) {
  838.  
  839.             if (currow)
  840.  
  841.                 currow--;
  842.  
  843.             else
  844.  
  845.                 error ("At row zero");
  846.  
  847.             while (hidden_row[currow] && currow)
  848.  
  849.                 currow--;
  850.  
  851.             }
  852.  
  853.             break;
  854.  
  855.         case ctl (q):
  856.  
  857.             break;    /* ignore flow control */
  858.  
  859.         case ctl (s):
  860.  
  861.             break;    /* ignore flow control */
  862.  
  863.         case ctl (t):
  864.  
  865.             showme ^= 1;
  866.  
  867.             break;
  868.  
  869.         case ctl (u):
  870.  
  871.             narg = arg * 4;
  872.  
  873.             nedistate = 1;
  874.  
  875.             break;
  876.  
  877.         case ctl (v):    /* insert variable name */
  878.  
  879.             if (linelim > 0) {
  880.  
  881.             sprintf (line+linelim,"%s%d", coltoa(curcol), currow);
  882.  
  883.             linelim = strlen (line);
  884.  
  885.             }
  886.  
  887.             break;
  888.  
  889.         case ctl (e):    /* insert variable expression */
  890.  
  891.             if (linelim > 0) editexp(currow,curcol);
  892.  
  893.             break;
  894.  
  895.         case ctl (a):    /* insert variable value */
  896.  
  897.             if (linelim > 0) {
  898.  
  899.             struct ent *p = tbl[currow][curcol];
  900.  
  901.  
  902.  
  903.             if (p && p -> flags & is_valid) {
  904.  
  905.                 sprintf (line + linelim, "%.*f",
  906.  
  907.                     precision[curcol],p -> v);
  908.  
  909.                 linelim = strlen (line);
  910.  
  911.             }
  912.  
  913.             }
  914.  
  915.             break;
  916.  
  917.         }
  918.  
  919.     else
  920.  
  921.         if ('0' <= c && c <= '9' && (linelim < 0 || edistate >= 0)) {
  922.  
  923.         if (edistate != 0) {
  924.  
  925.             if (c == '0')      /* just a '0' goes to left col */
  926.  
  927.             curcol = 0;
  928.  
  929.             else {
  930.  
  931.                 nedistate = 0;
  932.  
  933.                 narg = c - '0';
  934.  
  935.             }
  936.  
  937.         } else {
  938.  
  939.             nedistate = 0;
  940.  
  941.             narg = arg * 10 + (c - '0');
  942.  
  943.         }
  944.  
  945.         }
  946.  
  947.         else
  948.  
  949.         if (linelim >= 0) {
  950.  
  951.             line[linelim++] = c;
  952.  
  953.             line[linelim] = 0;
  954.  
  955.         }
  956.  
  957.         else
  958.  
  959.             switch (c) {
  960.  
  961.             case '.':
  962.  
  963.                 nedistate = 1;
  964.  
  965.                 break;
  966.  
  967.             case ':':
  968.  
  969.                 break;    /* Be nice to vi users */
  970.  
  971.             case '=':
  972.  
  973.                 sprintf(line,"let %s%d = ",coltoa(curcol),currow);
  974.  
  975.                 linelim = strlen (line);
  976.  
  977.                 break;
  978.  
  979.             case '/':
  980.  
  981.                 sprintf(line,"copy [to] %s%d [from] ", 
  982.  
  983.                       coltoa(curcol), currow);
  984.  
  985.                 linelim = strlen (line);
  986.  
  987.                 break;
  988.  
  989.             case '$':
  990.  
  991.                 curcol = MAXCOLS - 1;
  992.  
  993.                 while (!tbl[currow][curcol] && curcol > 0)
  994.  
  995.                 curcol--;
  996.  
  997.                 break;
  998.  
  999.             case '?':
  1000.  
  1001.                 help ();
  1002.  
  1003.                 break;
  1004.  
  1005.             case '"':
  1006.  
  1007.                 sprintf (line, "label %s%d = \"",
  1008.  
  1009.                         coltoa(curcol), currow);
  1010.  
  1011.                 linelim = strlen (line);
  1012.  
  1013.                 break;
  1014.  
  1015.             case '<':
  1016.  
  1017.                 sprintf (line, "leftstring %s%d = \"",
  1018.  
  1019.                     coltoa(curcol), currow);
  1020.  
  1021.                 linelim = strlen (line);
  1022.  
  1023.                 break;
  1024.  
  1025.             case '>':
  1026.  
  1027.                 sprintf (line, "rightstring %s%d = \"",
  1028.  
  1029.                     coltoa(curcol), currow);
  1030.  
  1031.                 linelim = strlen (line);
  1032.  
  1033.                 break;
  1034.  
  1035.             case 'e':
  1036.  
  1037.                 editv (currow, curcol);
  1038.  
  1039.                 break;
  1040.  
  1041.             case 'E':
  1042.  
  1043.                 edits (currow, curcol);
  1044.  
  1045.                 break;
  1046.  
  1047.             case 'f':
  1048.  
  1049.                 sprintf (line, "format [for column] %s [is] ",
  1050.  
  1051.                     coltoa(curcol));
  1052.  
  1053.                 error("Current format is %d %d",
  1054.  
  1055.                     fwidth[curcol],precision[curcol]);
  1056.  
  1057.                 linelim = strlen (line);
  1058.  
  1059.                 break;
  1060.  
  1061.             case 'P':
  1062.  
  1063.                 sprintf (line, "put [database into] \"");
  1064.  
  1065.                 if (*curfile)
  1066.  
  1067.                 error("default file is '%s'",curfile);
  1068.  
  1069.                 linelim = strlen (line);
  1070.  
  1071.                 break;
  1072.  
  1073.             case 'M':
  1074.  
  1075.                 sprintf (line, "merge [database from] \"");
  1076.  
  1077.                 linelim = strlen (line);
  1078.  
  1079.                 break;
  1080.  
  1081.             case 'G':
  1082.  
  1083.                 sprintf (line, "get [database from] \"");
  1084.  
  1085.                 if (*curfile)
  1086.  
  1087.                 error("default file is '%s'",curfile);
  1088.  
  1089.                 linelim = strlen (line);
  1090.  
  1091.                 break;
  1092.  
  1093.             case 'W':
  1094.  
  1095.                 sprintf (line, "write [listing to] \"");
  1096.  
  1097.                 linelim = strlen (line);
  1098.  
  1099.                 break;
  1100.  
  1101.             case 'T':    /* tbl output */
  1102.  
  1103.                 sprintf (line, "tbl [listing to] \"");
  1104.  
  1105.                 linelim = strlen (line);
  1106.  
  1107.                 break;
  1108.  
  1109.             case 'i':
  1110.  
  1111.                 switch (get_qual()) {
  1112.  
  1113.                 case 'r':
  1114.  
  1115.                 insertrow(arg);
  1116.  
  1117.                 break;
  1118.  
  1119.                 case 'c':
  1120.  
  1121.                 insertcol(arg);
  1122.  
  1123.                 break;
  1124.  
  1125.                 default:
  1126.  
  1127.                 break;
  1128.  
  1129.                 }
  1130.  
  1131.                 break;
  1132.  
  1133.             case 'd':
  1134.  
  1135.                 switch (get_qual()) {
  1136.  
  1137.                 case 'r':
  1138.  
  1139.                 deleterow(arg);
  1140.  
  1141.                 break;
  1142.  
  1143.                 case 'c':
  1144.  
  1145.                 deletecol(arg);
  1146.  
  1147.                 break;
  1148.  
  1149.                 default:
  1150.  
  1151.                 break;
  1152.  
  1153.                 }
  1154.  
  1155.                 break;
  1156.  
  1157.             case 'v':
  1158.  
  1159.                 switch (get_qual()) {
  1160.  
  1161.                 case 'r':
  1162.  
  1163.                 valueizerow(arg);
  1164.  
  1165.                 break;
  1166.  
  1167.                 case 'c':
  1168.  
  1169.                 valueizecol(arg);
  1170.  
  1171.                 break;
  1172.  
  1173.                 default:
  1174.  
  1175.                 break;
  1176.  
  1177.                 }
  1178.  
  1179.                 break;
  1180.  
  1181.             case 'p':
  1182.  
  1183.                 {
  1184.  
  1185.                 register qual;
  1186.  
  1187.                 qual = get_qual();
  1188.  
  1189.                 while (arg--)
  1190.  
  1191.                     pullcells(qual);
  1192.  
  1193.                 break;
  1194.  
  1195.                 }
  1196.  
  1197.             case 'x':
  1198.  
  1199.                 {
  1200.  
  1201.                 register struct ent **p;
  1202.  
  1203.                 register int c;
  1204.  
  1205.                 flush_saved();
  1206.  
  1207.                 for (c = curcol; arg-- && c < MAXCOLS; c++) {
  1208.  
  1209.                 p = &tbl[currow][c];
  1210.  
  1211.                 if (*p) {
  1212.  
  1213.                         free_ent(*p);
  1214.  
  1215.                         *p = 0;
  1216.  
  1217.                 }
  1218.  
  1219.                 }
  1220.  
  1221.                 sync_refs();
  1222.  
  1223.                 FullUpdate++;
  1224.  
  1225.                 }
  1226.  
  1227.                 break;
  1228.  
  1229.             case 'Q':
  1230.  
  1231.             case 'q':
  1232.  
  1233.                 running = 0;
  1234.  
  1235.                 break;
  1236.  
  1237.             case 'h':
  1238.  
  1239.                 while (--arg>=0) {
  1240.  
  1241.                 if (curcol)
  1242.  
  1243.                     curcol--;
  1244.  
  1245.                 else
  1246.  
  1247.                     error ("At column A");
  1248.  
  1249.                 while(hidden_col[curcol] && curcol)
  1250.  
  1251.                     curcol--;
  1252.  
  1253.                 }
  1254.  
  1255.                 break;
  1256.  
  1257.             case 'j':
  1258.  
  1259.                 while (--arg>=0) {
  1260.  
  1261.                 if (currow < MAXROWS - 1)
  1262.  
  1263.                     currow++;
  1264.  
  1265.                 else
  1266.  
  1267.                     error ("The table can't be any longer");
  1268.  
  1269.                 while (hidden_row[currow]&&(currow<MAXROWS-1))
  1270.  
  1271.                     currow++;
  1272.  
  1273.                 }
  1274.  
  1275.                 break;
  1276.  
  1277.             case 'k':
  1278.  
  1279.                 while (--arg>=0) {
  1280.  
  1281.                 if (currow)
  1282.  
  1283.                     currow--;
  1284.  
  1285.                 else
  1286.  
  1287.                     error ("At row zero");
  1288.  
  1289.                 while (hidden_row[currow] && currow)
  1290.  
  1291.                     currow--;
  1292.  
  1293.                 }
  1294.  
  1295.                 break;
  1296.  
  1297.             case 'l':
  1298.  
  1299.                 while (--arg>=0) {
  1300.  
  1301.                 if (curcol < MAXCOLS - 1)
  1302.  
  1303.                     curcol++;
  1304.  
  1305.                 else
  1306.  
  1307.                     error ("The table can't be any wider");
  1308.  
  1309.                 while(hidden_col[curcol]&&(curcol<MAXCOLS-1))
  1310.  
  1311.                     curcol++;
  1312.  
  1313.                 }
  1314.  
  1315.                 break;
  1316.  
  1317.             case 'm':
  1318.  
  1319.                 savedrow = currow;
  1320.  
  1321.                 savedcol = curcol;
  1322.  
  1323.                 break;
  1324.  
  1325.             case 'c': {
  1326.  
  1327.                 register struct ent *p = tbl[savedrow][savedcol];
  1328.  
  1329.                 register c;
  1330.  
  1331.                 register struct ent *n;
  1332.  
  1333.                 if (!p)
  1334.  
  1335.                 break;
  1336.  
  1337.                 FullUpdate++;
  1338.  
  1339.                 modflg++;
  1340.  
  1341.                 for (c = curcol; arg-- && c < MAXCOLS; c++) {
  1342.  
  1343.                 n = lookat (currow, c);
  1344.  
  1345.                 clearent(n);
  1346.  
  1347.                 n -> flags = p -> flags;
  1348.  
  1349.                 n -> v = p -> v;
  1350.  
  1351.                 n -> expr = copye(p->expr,
  1352.  
  1353.                         currow - savedrow,
  1354.  
  1355.                         c - savedcol);
  1356.  
  1357.                 n -> label = 0;
  1358.  
  1359.                 if (p -> label) {
  1360.  
  1361.                     n -> label = (char *)
  1362.  
  1363.                          malloc(strlen(p->label)+1);
  1364.  
  1365.                 strcpy (n -> label, p -> label);
  1366.  
  1367.                 }
  1368.  
  1369.                 }
  1370.  
  1371.                 break;
  1372.  
  1373.             }
  1374.  
  1375.             case 'z':
  1376.  
  1377.                 switch (get_qual()) {
  1378.  
  1379.                 case 'r':
  1380.  
  1381.                 hiderow(arg);
  1382.  
  1383.                 break;
  1384.  
  1385.                 case 'c':
  1386.  
  1387.                 hidecol(arg);
  1388.  
  1389.                 break;
  1390.  
  1391.                 default:
  1392.  
  1393.                 break;
  1394.  
  1395.                 }
  1396.  
  1397.                 break;
  1398.  
  1399.             case 's':
  1400.  
  1401.                 switch (get_qual()) {
  1402.  
  1403.                 case 'r':
  1404.  
  1405.                 showrow_op();
  1406.  
  1407.                 break;
  1408.  
  1409.                 case 'c':
  1410.  
  1411.                 showcol_op();
  1412.  
  1413.                 break;
  1414.  
  1415.                 default:
  1416.  
  1417.                 break;
  1418.  
  1419.                 }
  1420.  
  1421.                 break;
  1422.  
  1423.             case 'a':
  1424.  
  1425.                 switch (get_qual()) {
  1426.  
  1427.                 case 'r':
  1428.  
  1429.                 while (arg--)
  1430.  
  1431.                     duprow();
  1432.  
  1433.                 break;
  1434.  
  1435.                 case 'c':
  1436.  
  1437.                 while (arg--)
  1438.  
  1439.                     dupcol();
  1440.  
  1441.                 break;
  1442.  
  1443.                 default:
  1444.  
  1445.                 break;
  1446.  
  1447.                 }
  1448.  
  1449.                 break;
  1450.  
  1451.             default:
  1452.  
  1453.                 if ((c & 0177) != c)
  1454.  
  1455.                 error("Weird character, decimal '%d'.\n",
  1456.  
  1457.                     (int) c);
  1458.  
  1459.                 else error ("No such command  (%c)", c);
  1460.  
  1461.                 break;
  1462.  
  1463.             }
  1464.  
  1465.     edistate = nedistate;
  1466.  
  1467.     arg = narg;
  1468.  
  1469.     }                /* while (running) */
  1470.  
  1471.     inloop = modcheck(" before exiting");
  1472.  
  1473.     }                /*  while (inloop) */
  1474.  
  1475.     endwin ();
  1476.  
  1477. }
  1478.  
  1479.  
  1480.  
  1481. modcheck(endstr) char *endstr; {
  1482.  
  1483.     if (modflg && curfile[0]) {
  1484.  
  1485.     char ch, lin[100];
  1486.  
  1487.  
  1488.  
  1489.     move (0, 0);
  1490.  
  1491.     clrtoeol ();
  1492.  
  1493.     sprintf (lin,"File '%s' is modified, save%s? ",curfile,endstr);
  1494.  
  1495.     addstr (lin);
  1496.  
  1497.     refresh();
  1498.  
  1499.     ch = nmgetch();
  1500.  
  1501.     if (ch == 'y' || ch == 'Y') writefile(curfile);
  1502.  
  1503.     else if (ch == ctl (g)) return(1);
  1504.  
  1505.     }
  1506.  
  1507.     return(0);
  1508.  
  1509. }
  1510.  
  1511.     
  1512.  
  1513. writefile (fname)
  1514.  
  1515. char *fname; {
  1516.  
  1517.     register FILE *f;
  1518.  
  1519.     register struct ent **p;
  1520.  
  1521.     register r, c;
  1522.  
  1523.     char save[1024];
  1524.  
  1525.  
  1526.  
  1527.     if (*fname == 0) fname = &curfile[0];
  1528.  
  1529.  
  1530.  
  1531.     strcpy(save,fname);
  1532.  
  1533.  
  1534.  
  1535.     f = fopen (fname, "w");
  1536.  
  1537.     if (f==0) {
  1538.  
  1539.     error ("Can't create %s", fname);
  1540.  
  1541.     return;
  1542.  
  1543.     }
  1544.  
  1545.  
  1546.  
  1547.     fprintf (f, "# This data file was generated by the Spreadsheet ");
  1548.  
  1549.     fprintf (f, "Calculator.\n");
  1550.  
  1551.     fprintf (f, "# You almost certainly shouldn't edit it.\n\n");
  1552.  
  1553.     for (c=0; c<MAXCOLS; c++)
  1554.  
  1555.     if (fwidth[c] != DEFWIDTH || precision[c] != DEFPREC)
  1556.  
  1557.         fprintf (f, "format %s %d %d\n",coltoa(c),fwidth[c],precision[c]);
  1558.  
  1559.     for (r=0; r<=maxrow; r++) {
  1560.  
  1561.     p = &tbl[r][0];
  1562.  
  1563.     for (c=0; c<=maxcol; c++, p++)
  1564.  
  1565.         if (*p) {
  1566.  
  1567.         if ((*p)->label)
  1568.  
  1569.             fprintf (f, "%sstring %s%d = \"%s\"\n",
  1570.  
  1571.                 (*p)->flags&is_leftflush ? "left" : "right",
  1572.  
  1573.                 coltoa(c),r,(*p)->label);
  1574.  
  1575.         if ((*p)->flags&is_valid) {
  1576.  
  1577.             editv (r, c);
  1578.  
  1579.             fprintf (f, "%s\n",line);
  1580.  
  1581.         }
  1582.  
  1583.         }
  1584.  
  1585.     }
  1586.  
  1587.     fclose (f);
  1588.  
  1589.     strcpy(curfile,save);
  1590.  
  1591.  
  1592.  
  1593.     modflg = 0;
  1594.  
  1595.     error("File '%s' written.",curfile);
  1596.  
  1597. }
  1598.  
  1599.  
  1600.  
  1601. readfile (fname,eraseflg)
  1602.  
  1603. char *fname; int eraseflg; {
  1604.  
  1605.     register FILE *f;
  1606.  
  1607.     char save[1024];
  1608.  
  1609.  
  1610.  
  1611.     if (*fname == 0) fname = &curfile[0];
  1612.  
  1613.     strcpy(save,fname);
  1614.  
  1615.  
  1616.  
  1617.     if (eraseflg && strcmp(fname,curfile) && modcheck(" first")) return;
  1618.  
  1619.  
  1620.  
  1621.     f = fopen (save, "r");
  1622.  
  1623.     if (f==0) {
  1624.  
  1625.     error ("Can't read %s", save);
  1626.  
  1627.     return;
  1628.  
  1629.     }
  1630.  
  1631.  
  1632.  
  1633.     if (eraseflg) erasedb ();
  1634.  
  1635.  
  1636.  
  1637.     while (fgets(line,sizeof line,f)) {
  1638.  
  1639.     linelim = 0;
  1640.  
  1641.     if (line[0] != '#') yyparse ();
  1642.  
  1643.     }
  1644.  
  1645.     fclose (f);
  1646.  
  1647.     linelim = -1;
  1648.  
  1649.     modflg++;
  1650.  
  1651.     if (eraseflg) {
  1652.  
  1653.     strcpy(curfile,save);
  1654.  
  1655.     modflg = 0;
  1656.  
  1657.     }
  1658.  
  1659.     EvalAll();
  1660.  
  1661. }
  1662.  
  1663.  
  1664.  
  1665. erasedb () {
  1666.  
  1667.     register r, c;
  1668.  
  1669.     for (c = 0; c<=maxcol; c++) {
  1670.  
  1671.     fwidth[c] = DEFWIDTH;
  1672.  
  1673.     precision[c] = DEFPREC;
  1674.  
  1675.     }
  1676.  
  1677.  
  1678.  
  1679.     for (r = 0; r<=maxrow; r++) {
  1680.  
  1681.     register struct ent **p = &tbl[r][0];
  1682.  
  1683.     for (c=0; c++<=maxcol; p++)
  1684.  
  1685.         if (*p) {
  1686.  
  1687.         if ((*p)->expr) efree ((*p) -> expr);
  1688.  
  1689.         if ((*p)->label) free ((*p) -> label);
  1690.  
  1691.         free (*p);
  1692.  
  1693.         *p = 0;
  1694.  
  1695.         }
  1696.  
  1697.     }
  1698.  
  1699.     maxrow = 0;
  1700.  
  1701.     maxcol = 0;
  1702.  
  1703.     FullUpdate++;
  1704.  
  1705. }
  1706.  
  1707.  
  1708.  
  1709.