home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume1 / pcurses / part08 < prev    next >
Text File  |  1986-11-30  |  60KB  |  2,534 lines

  1. Subject:  Terminfo/Curses Part 8 of 11
  2.  
  3. : Run this shell script with "sh" not "csh"
  4. PATH=:/bin:/usr/bin:/usr/ucb
  5. export PATH
  6. if test ! -d =src
  7. then
  8.     echo 'Making directory "=src"'
  9.     mkdir =src
  10. fi
  11. echo 'x - =src/lib_doupdate.c'
  12. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_doupdate.c
  13. X/*********************************************************************
  14. *                         COPYRIGHT NOTICE                           *
  15. **********************************************************************
  16. *        This software is copyright (C) 1982 by Pavel Curtis         *
  17. *                                                                    *
  18. *        Permission is granted to reproduce and distribute           *
  19. *        this file by any means so long as no fee is charged         *
  20. *        above a nominal handling fee and so long as this            *
  21. *        notice is always included in the copies.                    *
  22. *                                                                    *
  23. *        Other rights are reserved except as explicitly granted      *
  24. *        by written permission of the author.                        *
  25. *                Pavel Curtis                                        *
  26. *                Computer Science Dept.                              *
  27. *                405 Upson Hall                                      *
  28. *                Cornell University                                  *
  29. *                Ithaca, NY 14853                                    *
  30. *                                                                    *
  31. *                Ph- (607) 256-4934                                  *
  32. *                                                                    *
  33. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  34. *                decvax!cornell!pavel       (UUCPnet)                *
  35. *********************************************************************/
  36.  
  37. X/*
  38.  *    lib_doupdate.c
  39.  *
  40.  *    The routine doupdate() and its dependents
  41.  *
  42.  *  $Log:    lib_doupdate.c,v $
  43.  * Revision 3.1  84/12/13  11:20:28  john
  44.  * Revisions by Mark Horton
  45.  * 
  46.  * Revision 2.1  82/10/25  14:47:05  pavel
  47.  * Added Copyright Notice
  48.  * 
  49.  * Revision 2.0  82/10/25  13:44:33  pavel
  50.  * Beta-one Test Release
  51.  * 
  52.  *
  53.  */
  54.  
  55. static char RCSid[] =
  56.     "$Header: lib_doupdate.c,v 3.1 84/12/13 11:20:28 john Exp $";
  57.  
  58. #include <signal.h>
  59. #include "curses.h"
  60. #include "curses.priv.h"
  61. #include "term.h"
  62.  
  63. #define GoTo(row,col)        mvcur(SP->_cursrow, SP->_curscol, row, col);  \
  64.                 SP->_cursrow = row;                           \
  65.                 SP->_curscol = col;
  66.  
  67. #define PutAttrChar(ch)        if (curscr->_attrs != (ch & A_ATTRIBUTES))    \
  68.                 {                                             \
  69.                     curscr->_attrs = ch & A_ATTRIBUTES;       \
  70.                     vidputs(curscr->_attrs, outc);            \
  71.                 }                                             \
  72.                 putc(ch & A_CHARTEXT, SP->_ofp);
  73.  
  74. #define PutChar(ch)        if (!auto_right_margin  ||                    \
  75.                         SP->_cursrow != lines - 1  || \
  76.                         SP->_curscol != columns - 1)  \
  77.                 {                                             \
  78.                     PutAttrChar(ch);                          \
  79.                     SP->_curscol++;                           \
  80.                     if (SP->_curscol >= columns)              \
  81.                     if (auto_right_margin)                \
  82.                     {                                     \
  83.                         SP->_curscol = 0;                 \
  84.                         SP->_cursrow++;                   \
  85.                     }                                     \
  86.                     else                                  \
  87.                         SP->_curscol--;                   \
  88.                 }
  89.  
  90.  
  91.  
  92. outc(ch)
  93. char    ch;
  94. {
  95.         putc(ch, SP->_ofp);
  96. }
  97.  
  98.  
  99.  
  100. doupdate()
  101. {
  102.     int    i;
  103.     int    (*oldsig)();
  104.  
  105. #ifdef TRACE
  106.     if (_tracing)
  107.         _tracef("doupdate() called");
  108. #endif
  109.  
  110.     oldsig = signal(SIGTSTP, SIG_IGN);
  111.  
  112.     if (curscr->_clear)
  113.     {
  114.         ClrUpdate(curscr);
  115.         curscr->_clear = FALSE;
  116.  
  117.         GoTo(curscr->_cury, curscr->_curx);
  118.     }
  119.     else
  120.     {
  121.         if (newscr->_clear)
  122.         {
  123.         ClrUpdate(newscr);
  124.         newscr->_clear = FALSE;
  125.         }
  126.         else if (curscr->_idlok)
  127.         IdlUpdate();
  128.         else
  129.         NoIdlUpdate();
  130.  
  131.         for (i = 0; i < lines; i++)
  132.         {
  133.         newscr->_firstchar[i] = _NOCHANGE;
  134.         newscr->_lastchar[i] = _NOCHANGE;
  135.         newscr->_numchngd[i] = 0;
  136.         }
  137.  
  138.         curscr->_curx = newscr->_curx;
  139.         curscr->_cury = newscr->_cury;
  140.  
  141.         GoTo(curscr->_cury, curscr->_curx);
  142.     }
  143.  
  144.  
  145.     fflush(SP->_ofp);
  146.  
  147.     signal(SIGTSTP, oldsig);
  148. }
  149.  
  150.  
  151.  
  152. X/*
  153. **    ClrUpdate(scr)
  154. **
  155. **    Update by clearing and redrawing the entire screen.
  156. **
  157. */
  158.  
  159. static
  160. ClrUpdate(scr)
  161. WINDOW    *scr;
  162. {
  163.         int    i, j;
  164.     int    lastNonBlank;
  165.  
  166.     ClearScreen();
  167.  
  168.     for (i=0; i < lines; i++)
  169.     {
  170.         lastNonBlank = columns - 1;
  171.         while (scr->_line[i][lastNonBlank] == ' ')
  172.         lastNonBlank--;
  173.  
  174.         if (i == lines  &&  lastNonBlank == columns - 1)
  175.         lastNonBlank--;
  176.  
  177.         for (j=0; j <= lastNonBlank; j++)
  178.         {
  179.         PutAttrChar(scr->_line[i][j]);
  180.         }
  181.  
  182.         if (! auto_right_margin  ||  lastNonBlank < columns - 1)
  183.         {
  184.         SP->_cursrow = i;
  185.         SP->_curscol = lastNonBlank < 0  ?  0  :  lastNonBlank;
  186.  
  187.         GoTo(i + 1, 0);
  188.         }
  189.     }
  190.  
  191.     if (scr != curscr)
  192.     {
  193.         for (i=0; i < LINES; i++)
  194.         for (j=0; j < COLS; j++)
  195.             curscr->_line[i][j] = scr->_line[i][j];
  196.     }
  197. }
  198.  
  199.  
  200.  
  201. X/*
  202. **    NoIdlUpdate()
  203. **
  204. **    Update screen without using Insert/Delete Line capabilities
  205. **
  206. */
  207.  
  208. static
  209. NoIdlUpdate()
  210. {
  211.     int    i;
  212.  
  213. #ifdef TRACE
  214.     if (_tracing)
  215.         _tracef("NoIdlUpdate() called");
  216. #endif
  217.  
  218.     for (i=0; i < lines; i++)
  219.         if (newscr->_numchngd[i])
  220.         TransformLine(i);
  221. }
  222.  
  223.  
  224.  
  225. X/*
  226. **    IdlUpdate()
  227. **
  228. **    Update screen using Insert/Delete Line capabilities
  229. **
  230. */
  231.  
  232. #define UNCHANGED(n)    (newscr->_numchngd[n] <= columns/10        \
  233.                 ||  newscr->_lastchar[n]        \
  234.                       - newscr->_firstchar[n] <= columns/10)
  235.  
  236. static
  237. IdlUpdate()
  238. {
  239.     int    firstLine, lastLine, thisLine;
  240.  
  241. #ifdef TRACE
  242.     if (_tracing)
  243.         _tracef("IdlUpdate() called");
  244. #endif
  245.  
  246.     firstLine = -1;
  247.  
  248.     for (thisLine = 0; thisLine < lines; thisLine++)
  249.     {
  250.         if (UNCHANGED(thisLine))
  251.         {
  252.         if (firstLine != -1)
  253.         {
  254.             lastLine = thisLine - 1;
  255.             Gosling(firstLine, lastLine);
  256.             firstLine = -1;
  257.         }
  258.         
  259.         if (newscr->_firstchar[thisLine] != _NOCHANGE)
  260.             TransformLine(thisLine);
  261.         }
  262.         else if (firstLine == -1)
  263.         firstLine = thisLine;
  264.     }
  265.  
  266.     if (firstLine != -1)
  267.         Gosling(firstLine, lines - 1);
  268. }
  269.  
  270.  
  271.  
  272. X/*
  273. **    TransformLine(lineno)
  274. **
  275. **    Call either IDcTransformLine or NoIDcTransformLine to do the
  276. **    update, depending upon availability of insert/delete character.
  277. */
  278.  
  279. static
  280. TransformLine(lineno)
  281. int    lineno;
  282. {
  283.         if ( (insert_character  ||  (enter_insert_mode  &&  exit_insert_mode))
  284.          &&  delete_character)
  285.         IDcTransformLine(lineno);
  286.     else
  287.         NoIDcTransformLine(lineno);
  288. }
  289.  
  290.  
  291.  
  292. X/*
  293. **    NoIDcTransformLine(lineno)
  294. **
  295. **    Transform the given line in curscr to the one in newscr, without
  296. **    using Insert/Delete Character.
  297. **
  298. **        firstChar = position of first different character in line
  299. **        lastChar = position of last different character in line
  300. **
  301. **        overwrite all characters between firstChar and lastChar.
  302. **
  303. */
  304.  
  305. static
  306. NoIDcTransformLine(lineno)
  307. int    lineno;
  308. {
  309.     register int    firstChar, lastChar;
  310.     register chtype    *newLine = newscr->_line[lineno];
  311.     register chtype    *oldLine = curscr->_line[lineno];
  312.         register int     k;
  313.  
  314. #ifdef TRACE
  315.     if (_tracing)
  316.         _tracef("NoIDcTransformLine(%d) called", lineno);
  317. #endif
  318.  
  319.     firstChar = 0;
  320.     while (firstChar < columns
  321.              &&  newLine[firstChar] == oldLine[firstChar])
  322.         firstChar++;
  323.  
  324.     if (firstChar >= columns)
  325.         return;
  326.  
  327.     lastChar = columns - 1;
  328.     while (lastChar > firstChar  &&  newLine[lastChar] == oldLine[lastChar])
  329.         lastChar--;
  330.         
  331.     GoTo(lineno, firstChar);
  332.  
  333.     for (k=firstChar; k <= lastChar; k++)
  334.     {
  335.         PutChar(newLine[k]);
  336.         oldLine[k] = newLine[k];
  337.     }
  338. }
  339.  
  340.  
  341.  
  342. X/*
  343. **    IDcTransformLine(lineno)
  344. **
  345. **    Transform the given line in curscr to the one in newscr, using
  346. **    Insert/Delete Character.
  347. **
  348. **        firstChar = position of first different character in line
  349. **        oLastChar = position of last different character in old line
  350. **        nLastChar = position of last different character in new line
  351. **
  352. **        move to firstChar
  353. **        overwrite chars up to min(oLastChar, nLastChar)
  354. **        if oLastChar < nLastChar
  355. **            insert newLine[oLastChar+1..nLastChar]
  356. **        else
  357. **            delete oLastChar - nLastChar spaces
  358. */
  359.  
  360. static
  361. IDcTransformLine(lineno)
  362. int    lineno;
  363. {
  364.     int    firstChar, oLastChar, nLastChar;
  365.     chtype    *newLine = newscr->_line[lineno];
  366.     chtype    *oldLine = curscr->_line[lineno];
  367.         int     k, n;
  368.  
  369. #ifdef TRACE
  370.     if (_tracing)
  371.         _tracef("IDcTransformLine(%d) called", lineno);
  372. #endif
  373.  
  374.     firstChar = 0;
  375.     while (firstChar < columns  &&  newLine[firstChar] == oldLine[firstChar])
  376.         firstChar++;
  377.  
  378.     if (firstChar >= columns)
  379.         return;
  380.  
  381.     oLastChar = columns - 1;
  382.     while (oLastChar > firstChar  &&  oldLine[oLastChar] == ' ')
  383.         oLastChar--;
  384.  
  385.     nLastChar = columns - 1;
  386.     while (nLastChar > firstChar  &&  newLine[nLastChar] == ' ')
  387.         nLastChar--;
  388.  
  389.     while (newLine[nLastChar] == oldLine[oLastChar])
  390.     {
  391.         nLastChar--;
  392.         oLastChar--;
  393.     }
  394.  
  395.     n = min(oLastChar, nLastChar);
  396.     GoTo(lineno, firstChar);
  397.  
  398.     for (k=firstChar; k <= n; k++)
  399.         PutChar(newLine[k]);
  400.  
  401.     if (oLastChar < nLastChar)
  402.         InsStr(&newLine[k], nLastChar - oLastChar);
  403.     else if (oLastChar > nLastChar)
  404.         DelChar(oLastChar - nLastChar);
  405.  
  406.     for (k=firstChar; k < columns; k++)
  407.         oldLine[k] = newLine[k];
  408. }
  409.  
  410.  
  411.  
  412. X/*
  413. **    Gosling(firstLine, lastLine)
  414. **
  415. **    Change the given range of lines on curscr into the same lines
  416. **    on newscr, using Gosling's Algorithm.
  417. */
  418.  
  419. static short lineCost[MAXLINES][MAXLINES];
  420. static short lineOps[MAXLINES][MAXLINES];
  421. static short lineDels[MAXLINES];
  422. static short lineIRs[MAXLINES];
  423.  
  424. #define INSERT    1
  425. #define DELETE    2
  426. #define REPLACE 3
  427.  
  428. static
  429. Gosling(firstLine, lastLine)
  430. int    firstLine, lastLine;
  431. {
  432.     int    i, count;
  433.  
  434. #ifdef TRACE
  435.     if (_tracing)
  436.         _tracef("Gosling(%d,%d) called", firstLine, lastLine);
  437. #endif
  438.  
  439.     Goscost(firstLine, lastLine - firstLine + 1);
  440.  
  441.     for (i=0; i <= lastLine - firstLine + 1; i++)
  442.         lineDels[i] = lineIRs[i] = 0;
  443.  
  444.     Gosdraw(lastLine - firstLine + 1, lastLine - firstLine + 1);
  445.  
  446.     count = 0;
  447.     for (i = lastLine - firstLine + 1; i > 0; i--)
  448.     {
  449.         if (lineDels[i] == DELETE)
  450.         count++;
  451.         else if (count)
  452.         {    
  453.         DelLine(count, firstLine + i, lastLine);
  454.         count = 0;
  455.         }
  456.     }
  457.  
  458.     if (count)
  459.         DelLine(count, firstLine, lastLine);
  460.  
  461.  
  462.     for (i = 1; i <= lastLine - firstLine + 1; i++)
  463.     {
  464.         switch (lineIRs[i])
  465.         {
  466.         case REPLACE:
  467.             TransformLine(firstLine + i - 1);
  468.             break;
  469.  
  470.         case INSERT:
  471.             InsLine(firstLine + i - 1, lastLine);
  472.             break;
  473.  
  474.         default:
  475.             /* do nothing */
  476.             break;
  477.         }
  478.     }
  479. }
  480.  
  481.  
  482.  
  483. #define RPLCOST(old,new)    (oHash[old] == nHash[new]  ?  0  :  columns)
  484.  
  485. static
  486. Goscost(lineno, length)
  487. int    lineno, length;
  488. {    
  489.     int    i, j, cost;
  490.     int    ILcost, DLcost;
  491.     long    nHash[MAXLINES], oHash[MAXLINES];
  492.         long    HashFn();
  493.  
  494. #ifdef TRACE
  495.     if (_tracing)
  496.         _tracef("Goscost(lineno=%d,length=%d) called", lineno, length);
  497. #endif
  498.  
  499.     ILcost = (insert_line ? strlen(insert_line) : 9999) + columns;
  500.     DLcost = (delete_line ? strlen(delete_line) : 9999);
  501.  
  502.     for (i=1; i <= length; i++)
  503.     {
  504.         nHash[i] = HashFn(newscr->_line[lineno + i - 1]);
  505.         oHash[i] = HashFn(curscr->_line[lineno + i - 1]);
  506.     }
  507.  
  508.     lineCost[0][0] = 0;
  509.  
  510.     for (i=1; i <= length; i++)
  511.     {
  512.         lineCost[i][0] = lineCost[i-1][0] + DLcost;
  513.         lineOps[i][0] = DELETE;
  514.  
  515.         lineCost[0][i] = lineCost[0][i-1] + ILcost;
  516.         lineOps[0][i] = INSERT;
  517.     }
  518.  
  519.     for (i=1; i <= length; i++)
  520.     {
  521.         for (j=1; j <= length; j++)
  522.         {
  523.         lineCost[i][j] = lineCost[i-1][j-1] + RPLCOST(i, j);
  524.         lineOps[i][j] = REPLACE;
  525.  
  526.         cost = lineCost[i][j-1] + ILcost;
  527.         if (cost < lineCost[i][j])
  528.         {
  529.             lineCost[i][j] = cost;
  530.             lineOps[i][j] = INSERT;
  531.         }
  532.  
  533.         cost = lineCost[i-1][j] + DLcost;
  534.         if (cost < lineCost[i][j])
  535.         {
  536.             lineCost[i][j] = cost;
  537.             lineOps[i][j] = DELETE;
  538.         }
  539.         }
  540.     }
  541.  
  542.     return(lineCost[columns][columns]);
  543. }
  544.  
  545.  
  546.  
  547. X/*
  548. **    _PrintCosts(length)
  549. **
  550. **    Print out the cost matrix.  Called only from sdb.
  551. **
  552. **
  553. **    _DumpNewscr(first, last)
  554. **
  555. **    Print the specified range of lines from newscr.  Called only from sdb.
  556. **
  557. **
  558. **    _DumpCurscr(first, last)
  559. **
  560. **    Print the specified range of lines from curscr.  Called only from sdb.
  561. **
  562. */
  563.  
  564.  
  565. _PrintCosts(length)
  566. int    length;
  567. {
  568.         int    i, j;
  569.  
  570.     for (i=0; i <= length; i++)
  571.     {
  572.         for (j=0; j <= length; j++)
  573.         {
  574.         printf("%5d/%d", lineCost[i][j], lineOps[i][j]);
  575.         }
  576.  
  577.         putchar('\n');
  578.     }
  579.  
  580.     fflush(stdout);
  581. }
  582.  
  583.  
  584. _DumpNewscr(first, last)
  585. int    first, last;
  586. {
  587.         int    i, j;
  588.  
  589.     for (i=first; i <= last; i++)
  590.     {
  591.         for (j=0; j < columns; j++)
  592.         putchar(newscr->_line[i][j]);
  593.         
  594.         putchar('\n');
  595.     }
  596. }
  597.  
  598.  
  599. _DumpCurscr(first, last)
  600. int    first, last;
  601. {
  602.         int    i, j;
  603.  
  604.     for (i=first; i <= last; i++)
  605.     {
  606.         for (j=0; j < columns; j++)
  607.         putchar(curscr->_line[i][j]);
  608.         
  609.         putchar('\n');
  610.     }
  611. }
  612.  
  613.  
  614.  
  615. long
  616. HashFn(line)
  617. chtype    *line;
  618. {
  619.         int    i = 0;
  620.     long    hash = 0;
  621.  
  622.     while(i < columns  &&  (line[i] | A_CHARTEXT) == ' ')
  623.         i++;
  624.  
  625.     for (; i+1 < columns; i += 2)
  626.         hash += (line[i] << 8) + line[i+1];
  627.  
  628.     return (hash);
  629. }
  630.  
  631.  
  632.  
  633.  
  634.  
  635. static
  636. Gosdraw(i, j)
  637. int    i, j;
  638. {
  639.         if (i == 0  &&  j == 0)
  640.         return;
  641.     
  642.     switch (lineOps[i][j])
  643.     {
  644.         case INSERT:
  645.         Gosdraw(i, j-1);
  646.         lineIRs[j] = INSERT;
  647.         break;
  648.  
  649.         case DELETE:
  650.         lineDels[i] = DELETE;
  651.         Gosdraw(i-1, j);
  652.         break;
  653.  
  654.         case REPLACE:
  655.         Gosdraw(i-1, j-1);
  656.         lineIRs[j] = REPLACE;
  657.         break;
  658.     }
  659. }
  660.  
  661.  
  662.  
  663. X/*
  664. **    ClearScreen()
  665. **
  666. **    Clear the physical screen and put cursor at home
  667. **
  668. */
  669.  
  670. static
  671. ClearScreen()
  672. {
  673.     if (clear_screen)
  674.     {
  675.         tputs(clear_screen, 1, outc);
  676.         SP->_cursrow = SP->_curscol = 0;
  677.     }
  678.     else if (clr_eos)
  679.     {
  680.         SP->_cursrow = SP->_curscol = -1;
  681.         GoTo(0,0);
  682.  
  683.         tputs(clr_eos, 1, outc);
  684.     }
  685.     else if (clr_eol)
  686.     {
  687.         SP->_cursrow = SP->_curscol = -1;
  688.  
  689.         while (SP->_cursrow < lines)
  690.         {
  691.         GoTo(SP->_cursrow, 0);
  692.         tputs(clr_eol, 1, outc);
  693.         }
  694.  
  695.         GoTo(0,0);
  696.     }
  697. }
  698.  
  699.  
  700.  
  701. X/*
  702. **    InsStr(line, count)
  703. **
  704. **    Insert the count characters pointed to by line.
  705. **
  706. */
  707.  
  708. InsStr(line, count)
  709. chtype    *line;
  710. int    count;
  711. {
  712. #ifdef TRACE
  713.     if (_tracing)
  714.         _tracef("InsStr(%o,%d) called", line, count);
  715. #endif
  716.  
  717.         if (enter_insert_mode  &&  exit_insert_mode)
  718.     {
  719.         tputs(enter_insert_mode, 1, outc);
  720.         while (count)
  721.         {
  722.         PutChar(*line);
  723.         line++;
  724.         count--;
  725.         }
  726.         tputs(exit_insert_mode, 1, outc);
  727.     }
  728.     else if (parm_ich)
  729.     {
  730.         tputs(tparm(parm_ich, count), 1, outc);
  731.         while (count)
  732.         {
  733.         PutChar(*line);
  734.         line++;
  735.         count--;
  736.         }
  737.     }
  738.     else
  739.     {
  740.         while (count)
  741.         {
  742.         tputs(insert_character, 1, outc);
  743.         PutChar(*line);
  744.         line++;
  745.         count--;
  746.         }
  747.     }
  748. }
  749.  
  750.  
  751.  
  752. X/*
  753. **    DelChar(count)
  754. **
  755. **    Delete count characters at current position
  756. **
  757. */
  758.  
  759. DelChar(count)
  760. int    count;
  761. {
  762. #ifdef TRACE
  763.     if (_tracing)
  764.         _tracef("DelChar(%d) called", count);
  765. #endif
  766.  
  767.         if (parm_dch)
  768.     {
  769.         tputs(tparm(parm_dch, count), 1, outc);
  770.     }
  771.     else
  772.     {
  773.         while (count--)
  774.         tputs(delete_character, 1, outc);
  775.     }
  776. }
  777.  
  778.  
  779.  
  780. X/*
  781. **    InsLine(lineno, lastLine)
  782. **
  783. **    Insert line number lineno, affecting up to lastLine
  784. **
  785. */
  786.  
  787. InsLine(lineno, lastLine)
  788. int    lineno, lastLine;
  789. {
  790.     int    i, j, k;
  791.     chtype    *temp;
  792.     chtype    *line = newscr->_line[lineno];
  793.  
  794. #ifdef TRACE
  795.     if (_tracing)
  796.         _tracef("InsLine(%d,%d) called", lineno, lastLine);
  797. #endif
  798.     
  799.     GoTo(lineno, 0);
  800.  
  801.         tputs(insert_line, 1, outc);
  802.  
  803.     for (i=0; i < columns; i++)
  804.         PutChar(line[i]);
  805.  
  806.     temp = curscr->_line[lastLine];
  807.     for (k = lastLine; k > lineno; k--)
  808.         curscr->_line[k] = curscr->_line[k - 1];
  809.  
  810.     curscr->_line[k] = temp;
  811.  
  812.     for (j=0; j < columns; j++)
  813.         curscr->_line[k][j] = newscr->_line[k][j];
  814. }
  815.  
  816.  
  817.  
  818. X/*
  819. **    DelLine(count, lineno, lastLine)
  820. **
  821. **    Delete count lines at lineno, affecting up to lastLine
  822. **
  823. */
  824.  
  825. DelLine(count, lineno, lastLine)
  826. int    count, lineno, lastLine;
  827. {
  828.     int    j, k;
  829.     chtype    *temp;
  830.  
  831. #ifdef TRACE
  832.     if (_tracing)
  833.         _tracef("DelLine(%d,%d,%d) called", count, lineno, lastLine);
  834. #endif
  835.  
  836.     GoTo(lineno, 0);
  837.  
  838.         if (parm_delete_line)
  839.     {
  840.         tputs(tparm(parm_delete_line, count), 1, outc);
  841.     }
  842.     else
  843.     {
  844.         while (count--)
  845.         tputs(delete_line, 1, outc);
  846.     }
  847.  
  848.     for (k = lineno; k + count <= lastLine; k++)
  849.     {
  850.         temp = curscr->_line[k];
  851.         curscr->_line[k] = curscr->_line[k + count];
  852.         curscr->_line[k + count] = temp;
  853.     }
  854.  
  855.     for (; k <= lastLine; k++)
  856.         for (j=0; j < columns; j++)
  857.         curscr->_line[k][j] = ' ';
  858. }
  859. //go.sysin dd *
  860. echo 'x - =src/lib_overlay.c'
  861. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_overlay.c
  862. X/*********************************************************************
  863. *                         COPYRIGHT NOTICE                           *
  864. **********************************************************************
  865. *        This software is copyright (C) 1982 by Pavel Curtis         *
  866. *                                                                    *
  867. *        Permission is granted to reproduce and distribute           *
  868. *        this file by any means so long as no fee is charged         *
  869. *        above a nominal handling fee and so long as this            *
  870. *        notice is always included in the copies.                    *
  871. *                                                                    *
  872. *        Other rights are reserved except as explicitly granted      *
  873. *        by written permission of the author.                        *
  874. *                Pavel Curtis                                        *
  875. *                Computer Science Dept.                              *
  876. *                405 Upson Hall                                      *
  877. *                Cornell University                                  *
  878. *                Ithaca, NY 14853                                    *
  879. *                                                                    *
  880. *                Ph- (607) 256-4934                                  *
  881. *                                                                    *
  882. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  883. *                decvax!cornell!pavel       (UUCPnet)                *
  884. *********************************************************************/
  885.  
  886. X/*
  887. **    lib_overlay.c
  888. **
  889. **    The routines overlay() and overwrite().
  890. **
  891. ** $Log:    RCS/lib_overlay.v $
  892.  * Revision 2.2  82/10/28  18:10:11  pavel
  893.  * Fixed confusion about which direction the copy was supposed to go and
  894.  * also added updates to win2->_{first,last}char.
  895.  * 
  896.  * Revision 2.1  82/10/25  14:48:35  pavel
  897.  * Added Copyright Notice
  898.  * 
  899.  * Revision 2.0  82/10/25  13:48:09  pavel
  900.  * Beta-one Test Release
  901.  * 
  902. **
  903. */
  904.  
  905. static char RCSid[] =
  906.     "$Header:   RCS/lib_overlay.v  Revision 2.2  82/10/28  18:10:11  pavel  Exp$";
  907.  
  908. #include "curses.h"
  909. #include "curses.priv.h"
  910.  
  911.  
  912. X/*
  913. **
  914. **    overlay(win1, win2)
  915. **
  916. **
  917. **    overlay() writes win1 on win2 non-destructively.
  918. **
  919. **/
  920.  
  921. overlay(win1, win2)
  922. WINDOW    *win1, *win2;
  923. {
  924.     int    col, line, last_line, last_col;
  925.     short   *firstchar, *lastchar;
  926.     chtype    *w1ptr, *w2ptr, attrs;
  927.  
  928. #ifdef TRACE
  929.     if (_tracing)
  930.         _tracef("overlay(%o, %o) called", win1, win2);
  931. #endif
  932.     
  933.     last_col = min(win1->_maxx, win2->_maxx);
  934.     last_line = min(win1->_maxy, win2->_maxy);
  935.     attrs = win2->_attrs;
  936.     firstchar = win2->_firstchar;
  937.     lastchar = win2->_lastchar;
  938.  
  939.     for(line = 0;  line <= last_line;  line++)
  940.     {
  941.         short   fc, lc;
  942.         
  943.         w1ptr = win1->_line[line];
  944.         w2ptr = win2->_line[line];
  945.         fc = _NOCHANGE;
  946.  
  947.         for(col = 0;  col <= last_col;  col++)
  948.         {
  949.         if ((*w1ptr & A_CHARTEXT) != ' ')
  950.         {
  951.             *w2ptr = (*w1ptr & A_CHARTEXT) | attrs;
  952.             if (fc == _NOCHANGE)
  953.                 fc = col;
  954.             lc = col;
  955.         }
  956.  
  957.         w1ptr++;
  958.         w2ptr++;
  959.         }
  960.         
  961.         if (*firstchar == _NOCHANGE)
  962.         {
  963.             *firstchar = fc;
  964.         *lastchar = lc;
  965.         }
  966.         else if (fc != _NOCHANGE)
  967.         {
  968.             if (fc < *firstchar)
  969.             *firstchar = fc;
  970.  
  971.             if (lc > *lastchar)
  972.             *lastchar = lc;
  973.             }
  974.  
  975.         firstchar++;
  976.         lastchar++;
  977.     }
  978. }
  979.  
  980.  
  981. X/*
  982. **
  983. **    overwrite(win1, win2)
  984. **
  985. **
  986. **    overwrite() writes win1 on win2 destructively.
  987. **
  988. **/
  989.  
  990. overwrite(win1, win2)
  991. WINDOW    *win1, *win2;
  992. {
  993.     int    col, line, last_line, last_col;
  994.     short   *firstchar, *lastchar;
  995.     chtype    *w1ptr, *w2ptr, attrs;
  996.  
  997. #ifdef TRACE
  998.     if (_tracing)
  999.         _tracef("overwrite(%o, %o) called", win1, win2);
  1000. #endif
  1001.     
  1002.     last_col = min(win1->_maxx, win2->_maxx);
  1003.     last_line = min(win1->_maxy, win2->_maxy);
  1004.     attrs = win2->_attrs;
  1005.     firstchar = win2->_firstchar;
  1006.     lastchar = win2->_lastchar;
  1007.  
  1008.     for(line = 0;  line <= last_line;  line++)
  1009.     {
  1010.         short   fc, lc;
  1011.         
  1012.         w1ptr = win1->_line[line];
  1013.         w2ptr = win2->_line[line];
  1014.         fc = _NOCHANGE;
  1015.  
  1016.         for(col = 0;  col <= last_col;  col++)
  1017.         {
  1018.         if ((*w1ptr & A_CHARTEXT) != (*w2ptr & A_CHARTEXT))
  1019.         {
  1020.             *w2ptr = (*w1ptr & A_CHARTEXT) | attrs;
  1021.             if (fc == _NOCHANGE)
  1022.                 fc = col;
  1023.             lc = col;
  1024.         }
  1025.  
  1026.         w1ptr++;
  1027.         w2ptr++;
  1028.         }
  1029.  
  1030.         if (*firstchar == _NOCHANGE)
  1031.         {
  1032.             *firstchar = fc;
  1033.         *lastchar = lc;
  1034.         }
  1035.         else if (fc != _NOCHANGE)
  1036.         {
  1037.             if (fc < *firstchar)
  1038.             *firstchar = fc;
  1039.  
  1040.             if (lc > *lastchar)
  1041.             *lastchar = lc;
  1042.             }
  1043.         
  1044.         firstchar++;
  1045.         lastchar++;
  1046.     }
  1047. }
  1048. //go.sysin dd *
  1049. echo 'x - =src/lib_printw.c'
  1050. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_printw.c
  1051. X/*********************************************************************
  1052. *                         COPYRIGHT NOTICE                           *
  1053. **********************************************************************
  1054. *        This software is copyright (C) 1982 by Pavel Curtis         *
  1055. *                                                                    *
  1056. *        Permission is granted to reproduce and distribute           *
  1057. *        this file by any means so long as no fee is charged         *
  1058. *        above a nominal handling fee and so long as this            *
  1059. *        notice is always included in the copies.                    *
  1060. *                                                                    *
  1061. *        Other rights are reserved except as explicitly granted      *
  1062. *        by written permission of the author.                        *
  1063. *                Pavel Curtis                                        *
  1064. *                Computer Science Dept.                              *
  1065. *                405 Upson Hall                                      *
  1066. *                Cornell University                                  *
  1067. *                Ithaca, NY 14853                                    *
  1068. *                                                                    *
  1069. *                Ph- (607) 256-4934                                  *
  1070. *                                                                    *
  1071. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  1072. *                decvax!cornell!pavel       (UUCPnet)                *
  1073. *********************************************************************/
  1074.  
  1075. X/*
  1076. **    lib_printw.c
  1077. **
  1078. **    The routines printw(), wprintw() and friend.
  1079. **
  1080. ** $Log:    RCS/lib_printw.v $
  1081.  * Revision 2.1  82/10/25  14:48:38  pavel
  1082.  * Added Copyright Notice
  1083.  * 
  1084.  * Revision 2.0  82/10/25  13:48:22  pavel
  1085.  * Beta-one Test Release
  1086.  * 
  1087. **
  1088. */
  1089.  
  1090. static char RCSid[] =
  1091.     "$Header:   RCS/lib_printw.v  Revision 2.1  82/10/25  14:48:38  pavel  Exp$";
  1092.  
  1093. #include "curses.h"
  1094. #include "curses.priv.h"
  1095.  
  1096.  
  1097. printw(fmt, args)
  1098. char    *fmt;
  1099. int    args;
  1100. {
  1101. #ifdef TRACE
  1102.     if (_tracing)
  1103.         _tracef("printw(%s,...) called", fmt);
  1104. #endif
  1105.  
  1106.     return(sprintw(stdscr, fmt, &args));
  1107. }
  1108.  
  1109.  
  1110.  
  1111. wprintw(win, fmt, args)
  1112. WINDOW    *win;
  1113. char    *fmt;
  1114. int    args;
  1115. {
  1116. #ifdef TRACE
  1117.     if (_tracing)
  1118.         _tracef("wprintw(%o,%s,...) called", win, fmt);
  1119. #endif
  1120.  
  1121.     return(sprintw(win, fmt, &args));
  1122. }
  1123.  
  1124.  
  1125.  
  1126. mvprintw(y, x, fmt, args)
  1127. int        y, x;
  1128. char        *fmt;
  1129. int        args;
  1130. {
  1131.     return(move(y, x) == OK ? sprintw(stdscr, fmt, &args) : ERR);
  1132. }
  1133.  
  1134.  
  1135.  
  1136. mvwprintw(win, y, x, fmt, args)
  1137. WINDOW    *win;
  1138. int    y, x;
  1139. char    *fmt;
  1140. int    args;
  1141. {
  1142.     return(wmove(win, y, x) == OK ? sprintw(win, fmt, &args) : ERR);
  1143. }
  1144.  
  1145.  
  1146.  
  1147. X/*
  1148. **    This routine actually executes the printf and adds it to the window
  1149. **
  1150. **    This is really a modified version of "sprintf".  As such,
  1151. ** it assumes that sprintf interfaces with the other printf functions
  1152. ** in a certain way.  If this is not how your system works, you
  1153. ** will have to modify this routine to use the interface that your
  1154. ** "sprintf" uses.
  1155. */
  1156.  
  1157. static
  1158. sprintw(win, fmt, args)
  1159. WINDOW    *win;
  1160. char    *fmt;
  1161. int    *args;
  1162. {
  1163.     FILE    junk;
  1164.     char    buf[512];
  1165.  
  1166.     junk._flag = _IOWRT + _IOSTRG;
  1167.     junk._ptr = buf;
  1168.     junk._cnt = 32767;
  1169.  
  1170.     _doprnt(fmt, args, &junk);
  1171.     putc('\0', &junk);
  1172.  
  1173.     return(waddstr(win, buf));
  1174. }
  1175. //go.sysin dd *
  1176. echo 'x - =src/lib_raw.c'
  1177. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_raw.c
  1178. X/*********************************************************************
  1179. *                         COPYRIGHT NOTICE                           *
  1180. **********************************************************************
  1181. *        This software is copyright (C) 1982 by Pavel Curtis         *
  1182. *                                                                    *
  1183. *        Permission is granted to reproduce and distribute           *
  1184. *        this file by any means so long as no fee is charged         *
  1185. *        above a nominal handling fee and so long as this            *
  1186. *        notice is always included in the copies.                    *
  1187. *                                                                    *
  1188. *        Other rights are reserved except as explicitly granted      *
  1189. *        by written permission of the author.                        *
  1190. *                Pavel Curtis                                        *
  1191. *                Computer Science Dept.                              *
  1192. *                405 Upson Hall                                      *
  1193. *                Cornell University                                  *
  1194. *                Ithaca, NY 14853                                    *
  1195. *                                                                    *
  1196. *                Ph- (607) 256-4934                                  *
  1197. *                                                                    *
  1198. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  1199. *                decvax!cornell!pavel       (UUCPnet)                *
  1200. *********************************************************************/
  1201.  
  1202. X/*
  1203.  *    raw.c
  1204.  *
  1205.  *    Routines:
  1206.  *        raw()
  1207.  *        echo()
  1208.  *        nl()
  1209.  *        cbreak()
  1210.  *        crmode()
  1211.  *        noraw()
  1212.  *        noecho()
  1213.  *        nonl()
  1214.  *        nocbreak()
  1215.  *        nocrmode()
  1216.  *
  1217.  *    cbreak() == crmode()
  1218.  *    nocbreak() == crmode()
  1219.  *
  1220.  *  $Log:    RCS/lib_raw.v $
  1221.  * Revision 2.1  82/10/25  14:48:42  pavel
  1222.  * Added Copyright Notice
  1223.  * 
  1224.  * Revision 2.0  82/10/24  15:17:40  pavel
  1225.  * Beta-one Test Release
  1226.  * 
  1227.  * Revision 1.3  82/08/23  22:30:32  pavel
  1228.  * The REAL Alpha-one Release Version
  1229.  * 
  1230.  * Revision 1.2  82/08/19  19:11:25  pavel
  1231.  * Alpha Test Release One
  1232.  * 
  1233.  * Revision 1.1  82/08/12  18:43:18  pavel
  1234.  * Initial revision
  1235.  * 
  1236.  *
  1237.  */
  1238.  
  1239. static char RCSid[] =
  1240.     "$Header:   RCS/lib_raw.v  Revision 2.1  82/10/25  14:48:42  pavel  Exp$";
  1241.  
  1242. #include "curses.h"
  1243. #include "curses.priv.h"
  1244. #include "term.h"
  1245.  
  1246.  
  1247.  
  1248.  
  1249. raw()
  1250. {
  1251. #ifdef UNDEFINED
  1252.     if (_tracing)
  1253.         _tracef("raw() called");
  1254. #endif
  1255.  
  1256.     cur_term->Nttyb.sg_flags |= RAW;
  1257.     stty(cur_term->Filedes, &cur_term->Nttyb);
  1258.  
  1259.     SP->_raw = TRUE;
  1260.     SP->_nlmapping = TRUE;
  1261. }
  1262.  
  1263.  
  1264. cbreak()
  1265. {
  1266. #ifdef UNDEFINED
  1267.     if (_tracing)
  1268.         _tracef("cbreak() called");
  1269. #endif
  1270.  
  1271.     cur_term->Nttyb.sg_flags |= CBREAK;
  1272.     stty(cur_term->Filedes, &cur_term->Nttyb);
  1273.  
  1274.     SP->_cbreak = TRUE;
  1275. }
  1276.  
  1277. crmode()
  1278. {
  1279. #ifdef UNDEFINED
  1280.     if (_tracing)
  1281.         _tracef("crmode() called");
  1282. #endif
  1283.  
  1284.     cbreak();
  1285. }
  1286.  
  1287.  
  1288. echo()
  1289. {
  1290. #ifdef UNDEFINED
  1291.     if (_tracing)
  1292.         _tracef("echo() called");
  1293. #endif
  1294.  
  1295.     cur_term->Nttyb.sg_flags |= ECHO;
  1296.     stty(cur_term->Filedes, &cur_term->Nttyb);
  1297.  
  1298.     SP->_echo = TRUE;
  1299. }
  1300.  
  1301.  
  1302. nl()
  1303. {
  1304. #ifdef UNDEFINED
  1305.     if (_tracing)
  1306.         _tracef("nl() called");
  1307. #endif
  1308.  
  1309.     cur_term->Nttyb.sg_flags |= CRMOD;
  1310.     stty(cur_term->Filedes, &cur_term->Nttyb);
  1311.  
  1312.     SP->_nl = TRUE;
  1313.     SP->_nlmapping = ! SP->_raw;
  1314. }
  1315.  
  1316.  
  1317. noraw()
  1318. {
  1319. #ifdef UNDEFINED
  1320.     if (_tracing)
  1321.         _tracef("noraw() called");
  1322. #endif
  1323.  
  1324.     cur_term->Nttyb.sg_flags &= ~RAW;
  1325.     stty(cur_term->Filedes, &cur_term->Nttyb);
  1326.  
  1327.     SP->_raw = FALSE;
  1328.     SP->_nlmapping = SP->_nl;
  1329. }
  1330.  
  1331.  
  1332. nocbreak()
  1333. {
  1334. #ifdef UNDEFINED
  1335.     if (_tracing)
  1336.         _tracef("nocbreak() called");
  1337. #endif
  1338.  
  1339.     cur_term->Nttyb.sg_flags &= ~CBREAK;
  1340.     stty(cur_term->Filedes, &cur_term->Nttyb);
  1341.  
  1342.     SP->_cbreak = FALSE;
  1343. }
  1344.  
  1345. nocrmode()
  1346. {
  1347. #ifdef UNDEFINED
  1348.     if (_tracing)
  1349.         _tracef("nocrmode() called");
  1350. #endif
  1351.  
  1352.     nocbreak();
  1353. }
  1354.  
  1355.  
  1356. noecho()
  1357. {
  1358. #ifdef UNDEFINED
  1359.     if (_tracing)
  1360.         _tracef("noecho() called");
  1361. #endif
  1362.  
  1363.     cur_term->Nttyb.sg_flags &= ~ECHO;
  1364.     stty(cur_term->Filedes, &cur_term->Nttyb);
  1365.  
  1366.     SP->_echo = FALSE;
  1367. }
  1368.  
  1369.  
  1370. nonl()
  1371. {
  1372. #ifdef UNDEFINED
  1373.     if (_tracing)
  1374.         _tracef("nonl() called");
  1375. #endif
  1376.  
  1377.     cur_term->Nttyb.sg_flags &= ~CRMOD;
  1378.     stty(cur_term->Filedes, &cur_term->Nttyb);
  1379.  
  1380.     SP->_nl = FALSE;
  1381.     SP->_nlmapping = FALSE;
  1382. }
  1383. //go.sysin dd *
  1384. echo 'x - =src/lib_refresh.c'
  1385. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_refresh.c
  1386. X/*********************************************************************
  1387. *                         COPYRIGHT NOTICE                           *
  1388. **********************************************************************
  1389. *        This software is copyright (C) 1982 by Pavel Curtis         *
  1390. *                                                                    *
  1391. *        Permission is granted to reproduce and distribute           *
  1392. *        this file by any means so long as no fee is charged         *
  1393. *        above a nominal handling fee and so long as this            *
  1394. *        notice is always included in the copies.                    *
  1395. *                                                                    *
  1396. *        Other rights are reserved except as explicitly granted      *
  1397. *        by written permission of the author.                        *
  1398. *                Pavel Curtis                                        *
  1399. *                Computer Science Dept.                              *
  1400. *                405 Upson Hall                                      *
  1401. *                Cornell University                                  *
  1402. *                Ithaca, NY 14853                                    *
  1403. *                                                                    *
  1404. *                Ph- (607) 256-4934                                  *
  1405. *                                                                    *
  1406. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  1407. *                decvax!cornell!pavel       (UUCPnet)                *
  1408. *********************************************************************/
  1409.  
  1410. X/*
  1411.  *    lib_refresh.c
  1412.  *
  1413.  *    The routines wrefresh() and wnoutrefresh().
  1414.  *
  1415.  *  $Log:    lib_refresh.c,v $
  1416.  * Revision 3.1  84/12/13  11:20:51  john
  1417.  * Revisions by Mark Horton
  1418.  * 
  1419.  * Revision 2.1  82/10/25  14:48:45  pavel
  1420.  * Added Copyright Notice
  1421.  * 
  1422.  * Revision 2.0  82/10/25  13:48:47  pavel
  1423.  * Beta-one Test Release
  1424.  * 
  1425.  *
  1426.  */
  1427.  
  1428. static char RCSid[] =
  1429.     "$Header: lib_refresh.c,v 3.1 84/12/13 11:20:51 john Exp $";
  1430.  
  1431.  
  1432. #include "curses.h"
  1433. #include "curses.priv.h"
  1434.  
  1435.  
  1436. wrefresh(win)
  1437. WINDOW    *win;
  1438. {
  1439. #ifdef TRACE
  1440.     if (_tracing)
  1441.         _tracef("wrefresh(%o) called", win);
  1442. #endif
  1443.  
  1444.     if (win == curscr)
  1445.     {
  1446.         curscr->_clear = TRUE;
  1447.         doupdate();
  1448.     }
  1449.     else
  1450.     {
  1451.         wnoutrefresh(win);
  1452.         doupdate();
  1453.     }
  1454. }
  1455.  
  1456.  
  1457.  
  1458. wnoutrefresh(win)
  1459. WINDOW    *win;
  1460. {
  1461.     int    i, j;
  1462.     int    begx = win->_begx;
  1463.     int    begy = win->_begy;
  1464.     int    m, n;
  1465.  
  1466. #ifdef TRACE
  1467.     if (_tracing)
  1468.         _tracef("wnoutrefresh(%o) called", win);
  1469. #endif
  1470.  
  1471.     for (i=0, m=begy; i <= win->_maxy; i++, m++)
  1472.     {
  1473. #ifdef TRACE
  1474.         if (_tracing)
  1475.         {
  1476.         _tracef("win->_firstchar[%d]= %d\t_lastchar= %d\t_numchngd= %d",
  1477.                 i, win->_firstchar[i],
  1478.                    win->_lastchar[i],
  1479.                    win->_numchngd[i]);
  1480.         }
  1481. #endif
  1482.  
  1483.         if (win->_numchngd[i])
  1484.         {
  1485.         j = win->_firstchar[i];
  1486.         n = j + begx;
  1487.         for (; j <= win->_lastchar[i]; j++, n++)
  1488.         {
  1489.             if (win->_line[i][j] != newscr->_line[m][n])
  1490.             {
  1491.             newscr->_line[m][n] = win->_line[i][j];
  1492.             newscr->_numchngd[m] += 1;
  1493.  
  1494.             if (newscr->_firstchar[m] == _NOCHANGE)
  1495.                 newscr->_firstchar[m] = newscr->_lastchar[m] = n;
  1496.             else if (n < newscr->_firstchar[m])
  1497.                 newscr->_firstchar[m] = n;
  1498.             else if (n > newscr->_lastchar[m])
  1499.                 newscr->_lastchar[m] = n;
  1500.             }
  1501.         }
  1502.         }
  1503.  
  1504.         win->_numchngd[i] = 0;
  1505.         win->_firstchar[i] = win->_lastchar[i] = _NOCHANGE;
  1506.     }
  1507.  
  1508.     if (win->_clear)
  1509.     {
  1510.         win->_clear = FALSE;
  1511.         newscr->_clear = TRUE;
  1512.     }
  1513.  
  1514.     if (! win->_leave)
  1515.     {
  1516.         newscr->_cury = win->_cury + win->_begy;
  1517.         newscr->_curx = win->_curx + win->_begx;
  1518.     }
  1519. }
  1520. //go.sysin dd *
  1521. echo 'x - =src/lib_scanw.c'
  1522. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_scanw.c
  1523. X/*********************************************************************
  1524. *                         COPYRIGHT NOTICE                           *
  1525. **********************************************************************
  1526. *        This software is copyright (C) 1982 by Pavel Curtis         *
  1527. *                                                                    *
  1528. *        Permission is granted to reproduce and distribute           *
  1529. *        this file by any means so long as no fee is charged         *
  1530. *        above a nominal handling fee and so long as this            *
  1531. *        notice is always included in the copies.                    *
  1532. *                                                                    *
  1533. *        Other rights are reserved except as explicitly granted      *
  1534. *        by written permission of the author.                        *
  1535. *                Pavel Curtis                                        *
  1536. *                Computer Science Dept.                              *
  1537. *                405 Upson Hall                                      *
  1538. *                Cornell University                                  *
  1539. *                Ithaca, NY 14853                                    *
  1540. *                                                                    *
  1541. *                Ph- (607) 256-4934                                  *
  1542. *                                                                    *
  1543. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  1544. *                decvax!cornell!pavel       (UUCPnet)                *
  1545. *********************************************************************/
  1546.  
  1547. X/*
  1548. **    lib_scanw.c
  1549. **
  1550. **    The routines scanw(), wscanw() and friend.
  1551. **
  1552. ** $Log:    RCS/lib_scanw.v $
  1553.  * Revision 2.1  82/10/25  14:48:51  pavel
  1554.  * Added Copyright Notice
  1555.  * 
  1556.  * Revision 2.0  82/10/25  13:49:07  pavel
  1557.  * Beta-one Test Release
  1558.  * 
  1559. **
  1560. */
  1561.  
  1562. static char RCSid[] =
  1563.     "$Header:   RCS/lib_scanw.v  Revision 2.1  82/10/25  14:48:51  pavel  Exp$";
  1564.  
  1565. #include "curses.h"
  1566. #include "curses.priv.h"
  1567.  
  1568.  
  1569. scanw(fmt, args)
  1570. char    *fmt;
  1571. int    args;
  1572. {
  1573. #ifdef TRACE
  1574.     if (_tracing)
  1575.         _tracef("scanw(%s,...) called", fmt);
  1576. #endif
  1577.  
  1578.     return(sscans(stdscr, fmt, &args));
  1579. }
  1580.  
  1581.  
  1582.  
  1583. wscanw(win, fmt, args)
  1584. WINDOW    *win;
  1585. char    *fmt;
  1586. int    args;
  1587. {
  1588. #ifdef TRACE
  1589.     if (_tracing)
  1590.         _tracef("wscanw(%o,%s,...) called", win, fmt);
  1591. #endif
  1592.  
  1593.     return(sscans(win, fmt, &args));
  1594. }
  1595.  
  1596.  
  1597.  
  1598. mvscanw(y, x, fmt, args)
  1599. int    y, x;
  1600. char    *fmt;
  1601. int    args;
  1602. {
  1603.     return(move(y, x) == OK ? sscanw(stdscr, fmt, &args) : ERR);
  1604. }
  1605.  
  1606.  
  1607.  
  1608. mvwscanw(win, y, x, fmt, args)
  1609. WINDOW    *win;
  1610. int    y, x;
  1611. char    *fmt;
  1612. int    args;
  1613. {
  1614.     return(wmove(win, y, x) == OK ? sscanw(win, fmt, &args) : ERR);
  1615. }
  1616.  
  1617.  
  1618.  
  1619. X/*
  1620. **    This routine actually executes the scanf from the window.
  1621. **
  1622. **    This is really a modified version of "sscanf".  As such,
  1623. ** it assumes that sscanf interfaces with the other scanf functions
  1624. ** in a certain way.  If this is not how your system works, you
  1625. ** will have to modify this routine to use the interface that your
  1626. ** "sscanf" uses.
  1627. */
  1628.  
  1629. static
  1630. sscans(win, fmt, args)
  1631. WINDOW    *win;
  1632. char    *fmt;
  1633. int    *args;
  1634. {
  1635.     char    buf[100];
  1636.     FILE    junk;
  1637.  
  1638.     junk._flag = _IOREAD|_IOSTRG;
  1639.     junk._base = junk._ptr = buf;
  1640.  
  1641.     if (wgetstr(win, buf) == ERR)
  1642.         return(ERR);
  1643.  
  1644.     junk._cnt = strlen(buf);
  1645.  
  1646.     return(_doscan(&junk, fmt, args));
  1647. }
  1648. //go.sysin dd *
  1649. echo 'x - =src/lib_scroll.c'
  1650. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_scroll.c
  1651. X/*********************************************************************
  1652. *                         COPYRIGHT NOTICE                           *
  1653. **********************************************************************
  1654. *        This software is copyright (C) 1982 by Pavel Curtis         *
  1655. *                                                                    *
  1656. *        Permission is granted to reproduce and distribute           *
  1657. *        this file by any means so long as no fee is charged         *
  1658. *        above a nominal handling fee and so long as this            *
  1659. *        notice is always included in the copies.                    *
  1660. *                                                                    *
  1661. *        Other rights are reserved except as explicitly granted      *
  1662. *        by written permission of the author.                        *
  1663. *                Pavel Curtis                                        *
  1664. *                Computer Science Dept.                              *
  1665. *                405 Upson Hall                                      *
  1666. *                Cornell University                                  *
  1667. *                Ithaca, NY 14853                                    *
  1668. *                                                                    *
  1669. *                Ph- (607) 256-4934                                  *
  1670. *                                                                    *
  1671. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  1672. *                decvax!cornell!pavel       (UUCPnet)                *
  1673. *********************************************************************/
  1674.  
  1675. X/*
  1676. **    lib_scroll.c
  1677. **
  1678. **    The routine scroll().
  1679. **
  1680. ** $Log:    RCS/lib_scroll.v $
  1681.  * Revision 2.1  82/10/25  14:48:54  pavel
  1682.  * Added Copyright Notice
  1683.  * 
  1684.  * Revision 2.0  82/10/25  13:49:22  pavel
  1685.  * Beta-one Test Release
  1686.  * 
  1687. **
  1688. */
  1689.  
  1690. static char RCSid[] =
  1691.     "$Header:   RCS/lib_scroll.v  Revision 2.1  82/10/25  14:48:54  pavel  Exp$";
  1692.  
  1693. #include "curses.h"
  1694. #include "curses.priv.h"
  1695.  
  1696.  
  1697. scroll(win)
  1698. WINDOW    *win;
  1699. {
  1700.     int    i;
  1701.     chtype    *ptr, *temp;
  1702.     chtype    blank = ' ' | win->_attrs;
  1703.  
  1704. #ifdef TRACE
  1705.     if (_tracing)
  1706.         _tracef("scroll(%o) called", win);
  1707. #endif
  1708.  
  1709.     if (! win->_scroll)
  1710.         return;
  1711.  
  1712.     temp = win->_line[0];
  1713.     for (i = 0; i < win->_regbottom; i++)
  1714.         win->_line[i] = win->_line[i+1];
  1715.  
  1716.     for (ptr = temp; ptr - temp <= win->_maxx; ptr++)
  1717.         *ptr = blank;
  1718.  
  1719.     win->_line[win->_regbottom] = temp;
  1720.  
  1721.     win->_cury--;
  1722. }
  1723. //go.sysin dd *
  1724. echo 'x - =src/lib_scrreg.c'
  1725. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_scrreg.c
  1726. X/*********************************************************************
  1727. *                         COPYRIGHT NOTICE                           *
  1728. **********************************************************************
  1729. *        This software is copyright (C) 1982 by Pavel Curtis         *
  1730. *                                                                    *
  1731. *        Permission is granted to reproduce and distribute           *
  1732. *        this file by any means so long as no fee is charged         *
  1733. *        above a nominal handling fee and so long as this            *
  1734. *        notice is always included in the copies.                    *
  1735. *                                                                    *
  1736. *        Other rights are reserved except as explicitly granted      *
  1737. *        by written permission of the author.                        *
  1738. *                Pavel Curtis                                        *
  1739. *                Computer Science Dept.                              *
  1740. *                405 Upson Hall                                      *
  1741. *                Cornell University                                  *
  1742. *                Ithaca, NY 14853                                    *
  1743. *                                                                    *
  1744. *                Ph- (607) 256-4934                                  *
  1745. *                                                                    *
  1746. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  1747. *                decvax!cornell!pavel       (UUCPnet)                *
  1748. *********************************************************************/
  1749.  
  1750. X/*
  1751. **    lib_scrreg.c
  1752. **
  1753. **    The routine wsetscrreg().
  1754. **
  1755. ** $Log:    RCS/lib_scrreg.v $
  1756.  * Revision 2.1  82/10/25  14:49:00  pavel
  1757.  * Added Copyright Notice
  1758.  * 
  1759.  * Revision 2.0  82/10/25  13:49:29  pavel
  1760.  * Beta-one Test Release
  1761.  * 
  1762. **
  1763. */
  1764.  
  1765. static char RCSid[] =
  1766.     "$Header:   RCS/lib_scrreg.v  Revision 2.1  82/10/25  14:49:00  pavel  Exp$";
  1767.  
  1768. #include "curses.h"
  1769. #include "curses.priv.h"
  1770.  
  1771.  
  1772. wsetscrreg(win, top, bottom)
  1773. WINDOW    *win;
  1774. int    top, bottom;
  1775. {
  1776. #ifdef TRACE
  1777.     if (_tracing)
  1778.         _tracef("wsetscrreg(%o,%d,%d) called", win, top, bottom);
  1779. #endif
  1780.  
  1781.         if (0 <= top  &&  top <= win->_cury  &&
  1782.         win->_cury <= bottom  &&  bottom <= win->_maxy)
  1783.     {
  1784.         win->_regtop = top;
  1785.         win->_regbottom = bottom;
  1786.  
  1787.         return(OK);
  1788.     }
  1789.     else
  1790.         return(ERR);
  1791. }
  1792. //go.sysin dd *
  1793. echo 'x - =src/lib_set_term.c'
  1794. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_set_term.c
  1795. X/*********************************************************************
  1796. *                         COPYRIGHT NOTICE                           *
  1797. **********************************************************************
  1798. *        This software is copyright (C) 1982 by Pavel Curtis         *
  1799. *                                                                    *
  1800. *        Permission is granted to reproduce and distribute           *
  1801. *        this file by any means so long as no fee is charged         *
  1802. *        above a nominal handling fee and so long as this            *
  1803. *        notice is always included in the copies.                    *
  1804. *                                                                    *
  1805. *        Other rights are reserved except as explicitly granted      *
  1806. *        by written permission of the author.                        *
  1807. *                Pavel Curtis                                        *
  1808. *                Computer Science Dept.                              *
  1809. *                405 Upson Hall                                      *
  1810. *                Cornell University                                  *
  1811. *                Ithaca, NY 14853                                    *
  1812. *                                                                    *
  1813. *                Ph- (607) 256-4934                                  *
  1814. *                                                                    *
  1815. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  1816. *                decvax!cornell!pavel       (UUCPnet)                *
  1817. *********************************************************************/
  1818.  
  1819. X/*
  1820. **    lib_set_term.c
  1821. **
  1822. **    The routine set_term().
  1823. **
  1824. ** $Log:    RCS/lib_set_term.v $
  1825.  * Revision 2.1  82/10/25  14:49:06  pavel
  1826.  * Added Copyright Notice
  1827.  * 
  1828.  * Revision 2.0  82/10/25  13:49:42  pavel
  1829.  * Beta-one Test Release
  1830.  * 
  1831. **
  1832. */
  1833.  
  1834. static char RCSid[] =
  1835.     "$Header:   RCS/lib_set_term.v  Revision 2.1  82/10/25  14:49:06  pavel  Exp$";
  1836.  
  1837. #include "curses.h"
  1838. #include "curses.priv.h"
  1839. #include "term.h"
  1840.  
  1841.  
  1842. struct screen *
  1843. set_term(screen)
  1844. struct screen *screen;
  1845. {
  1846.         struct screen    *oldSP;
  1847.  
  1848. #ifdef TRACE
  1849.     if (_tracing)
  1850.         _tracef("set_term(%o) called", screen);
  1851. #endif
  1852.  
  1853.     oldSP = SP;
  1854.     SP = screen;
  1855.  
  1856.     cur_term = SP->_term;
  1857.     curscr   = SP->_curscr;
  1858.     newscr   = SP->_newscr;
  1859.  
  1860.     return(oldSP);
  1861. }
  1862. //go.sysin dd *
  1863. echo 'x - =src/lib_setup.c'
  1864. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_setup.c
  1865. X/*********************************************************************
  1866. *                         COPYRIGHT NOTICE                           *
  1867. **********************************************************************
  1868. *        This software is copyright (C) 1982 by Pavel Curtis         *
  1869. *                                                                    *
  1870. *        Permission is granted to reproduce and distribute           *
  1871. *        this file by any means so long as no fee is charged         *
  1872. *        above a nominal handling fee and so long as this            *
  1873. *        notice is always included in the copies.                    *
  1874. *                                                                    *
  1875. *        Other rights are reserved except as explicitly granted      *
  1876. *        by written permission of the author.                        *
  1877. *                Pavel Curtis                                        *
  1878. *                Computer Science Dept.                              *
  1879. *                405 Upson Hall                                      *
  1880. *                Cornell University                                  *
  1881. *                Ithaca, NY 14853                                    *
  1882. *                                                                    *
  1883. *                Ph- (607) 256-4934                                  *
  1884. *                                                                    *
  1885. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  1886. *                decvax!cornell!pavel       (UUCPnet)                *
  1887. *********************************************************************/
  1888.  
  1889. X/*
  1890.  *    setupterm(termname, filedes, errret)
  1891.  *
  1892.  *    Find and read the appropriate object file for the terminal
  1893.  *    Make cur_term point to the structure.
  1894.  *    Turn off the XTABS bit in the tty structure if it was on
  1895.  *    If XTABS was on, remove the tab and backtab capabilities.
  1896.  *
  1897.  *  $Log:    RCS/lib_setup.v $
  1898.  * Revision 2.1  82/10/25  14:49:09  pavel
  1899.  * Added Copyright Notice
  1900.  * 
  1901.  * Revision 2.0  82/10/24  15:17:46  pavel
  1902.  * Beta-one Test Release
  1903.  * 
  1904.  * Revision 1.5  82/08/23  22:30:35  pavel
  1905.  * The REAL Alpha-one Release Version
  1906.  * 
  1907.  * Revision 1.4  82/08/20  15:12:24  pavel
  1908.  * Fixed use of un-initialised cur_term.
  1909.  * 
  1910.  * Revision 1.3  82/08/19  19:22:09  pavel
  1911.  * Alpha Test Release One
  1912.  * 
  1913.  * Revision 1.2  82/08/19  19:11:28  pavel
  1914.  * Alpha Test Release One
  1915.  * 
  1916.  * Revision 1.1  82/08/12  18:45:07  pavel
  1917.  * Initial revision
  1918.  * 
  1919.  *
  1920.  */
  1921.  
  1922. static char RCSid[] =
  1923.     "$Header:   RCS/lib_setup.v  Revision 2.1  82/10/25  14:49:09  pavel  Exp$";
  1924.  
  1925. #include <stdio.h>
  1926. #include "curses.h"
  1927. #include "curses.priv.h"
  1928. #include "term.h"
  1929.  
  1930. #define ret_error(code, fmt, arg)    if (errret)\
  1931.                     {\
  1932.                         *errret = code;\
  1933.                         return(code);\
  1934.                     }\
  1935.                     else\
  1936.                     {\
  1937.                         fprintf(stderr, fmt, arg);\
  1938.                         exit(1);\
  1939.                     }
  1940.  
  1941.  
  1942. setupterm(termname, filedes, errret)
  1943. char    *termname;
  1944. int    filedes;
  1945. int    *errret;
  1946. {
  1947.     char        filename1[1024];
  1948.     char        filename2[1024];
  1949.     char        *directory = SRCDIR;
  1950.     char        *malloc(), *getenv();
  1951.     char        *terminfo;
  1952.     struct term    *term_ptr;
  1953.  
  1954. #ifdef TRACE
  1955.     _init_trace();
  1956.     if (_tracing)
  1957.         _tracef("setupterm(%s,%d,%o) called", termname, filedes, errret);
  1958. #endif
  1959.  
  1960.     if (termname == NULL)
  1961.     {
  1962.         termname = getenv("TERM");
  1963.         if (termname == NULL)
  1964.         ret_error(-1, "TERM environment variable not set.\n", "");
  1965.     }
  1966.  
  1967.     if (cur_term == 0)
  1968.         term_ptr = &_first_term;
  1969.     else
  1970.     {
  1971.         term_ptr = (struct term *) malloc(sizeof(struct term));
  1972.  
  1973.         if (term_ptr == NULL)
  1974.         ret_error(-1, "Not enough memory to create terminal structure.\n", "");
  1975.     }
  1976.  
  1977.     if ((terminfo = getenv("TERMINFO")) != NULL)
  1978.         directory = terminfo;
  1979.  
  1980.     sprintf(filename1, "%s/%c/%s", directory, termname[0], termname);
  1981.     sprintf(filename2, "%s/%c/%s", SRCDIR, termname[0], termname);
  1982.  
  1983.     if (read_entry(filename1, term_ptr) < 0
  1984.                     &&  read_entry(filename2, term_ptr) < 0)
  1985.         ret_error(0, "'%s': Unknown terminal type.\n", termname);
  1986.  
  1987.     if (command_character  &&  getenv("CC"))
  1988.         do_prototype();
  1989.  
  1990.     cur_term = term_ptr;
  1991.     strncpy(ttytype, cur_term->term_names, NAMESIZE - 1);
  1992.     ttytype[NAMESIZE - 1] = '\0';
  1993.     cur_term->Filedes = filedes;
  1994.     gtty(filedes, &cur_term->Ottyb);
  1995.     if (cur_term->Ottyb.sg_flags & XTABS)
  1996.         tab = back_tab = NULL;
  1997.  
  1998.     cur_term->Nttyb = cur_term->Ottyb;
  1999.     cur_term->Nttyb.sg_flags &= ~XTABS;
  2000.  
  2001.     fixterm();
  2002.  
  2003.     if (errret)
  2004.         *errret = 1;
  2005.     
  2006.     return(1);
  2007. }
  2008.  
  2009.  
  2010.  
  2011. X/*
  2012. **    do_prototype()
  2013. **
  2014. **    Take the real command character out of the CC environment variable
  2015. **    and substitute it in for the prototype given in 'command_character'.
  2016. **
  2017. */
  2018.  
  2019. static
  2020. do_prototype()
  2021. {
  2022.         int    i, j;
  2023.     char    CC;
  2024.     char    proto;
  2025.  
  2026.     CC = *getenv("CC");
  2027.     proto = *command_character;
  2028.  
  2029.     for (i=0; i < STRCOUNT; i++)
  2030.     {
  2031.         j = 0;
  2032.         while (cur_term->Strings[i][j])
  2033.         {
  2034.         if (cur_term->Strings[i][j] == proto)
  2035.             cur_term->Strings[i][j] = CC;
  2036.         j++;
  2037.         }
  2038.     }
  2039. }
  2040. //go.sysin dd *
  2041. echo 'x - =src/lib_touchwin.c'
  2042. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_touchwin.c
  2043. X/*********************************************************************
  2044. *                         COPYRIGHT NOTICE                           *
  2045. **********************************************************************
  2046. *        This software is copyright (C) 1982 by Pavel Curtis         *
  2047. *                                                                    *
  2048. *        Permission is granted to reproduce and distribute           *
  2049. *        this file by any means so long as no fee is charged         *
  2050. *        above a nominal handling fee and so long as this            *
  2051. *        notice is always included in the copies.                    *
  2052. *                                                                    *
  2053. *        Other rights are reserved except as explicitly granted      *
  2054. *        by written permission of the author.                        *
  2055. *                Pavel Curtis                                        *
  2056. *                Computer Science Dept.                              *
  2057. *                405 Upson Hall                                      *
  2058. *                Cornell University                                  *
  2059. *                Ithaca, NY 14853                                    *
  2060. *                                                                    *
  2061. *                Ph- (607) 256-4934                                  *
  2062. *                                                                    *
  2063. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  2064. *                decvax!cornell!pavel       (UUCPnet)                *
  2065. *********************************************************************/
  2066.  
  2067. X/*
  2068. **    lib_touchwin.c
  2069. **
  2070. **    The routine touchwin().
  2071. **
  2072. ** $Log:    RCS/lib_touchwin.v $
  2073.  * Revision 2.1  82/10/25  14:49:13  pavel
  2074.  * Added Copyright Notice
  2075.  * 
  2076.  * Revision 2.0  82/10/25  13:49:52  pavel
  2077.  * Beta-one Test Release
  2078.  * 
  2079. **
  2080. */
  2081.  
  2082. static char RCSid[] =
  2083.     "$Header:   RCS/lib_touchwin.v  Revision 2.1  82/10/25  14:49:13  pavel  Exp$";
  2084.  
  2085. #include "curses.h"
  2086. #include "curses.priv.h"
  2087.  
  2088.  
  2089. touchwin(win)
  2090. WINDOW    *win;
  2091. {
  2092.     int    y, maxy, maxx;
  2093.  
  2094. #ifdef TRACE
  2095.     if (_tracing)
  2096.         _tracef("touchwin(%o) called", win);
  2097. #endif
  2098.  
  2099.     maxy = win->_maxy;
  2100.     maxx = win->_maxx;
  2101.  
  2102.     for (y = 0; y <= maxy; y++)
  2103.     {
  2104.         win->_firstchar[y] = 0;
  2105.         win->_lastchar[y] = maxx;
  2106.         win->_numchngd[y] = maxx;
  2107.     }
  2108. }
  2109. //go.sysin dd *
  2110. echo 'x - =src/lib_tparm.c'
  2111. sed 's/^X//' <<'//go.sysin dd *' >=src/lib_tparm.c
  2112. X/*********************************************************************
  2113. *                         COPYRIGHT NOTICE                           *
  2114. **********************************************************************
  2115. *        This software is copyright (C) 1982 by Pavel Curtis         *
  2116. *                                                                    *
  2117. *        Permission is granted to reproduce and distribute           *
  2118. *        this file by any means so long as no fee is charged         *
  2119. *        above a nominal handling fee and so long as this            *
  2120. *        notice is always included in the copies.                    *
  2121. *                                                                    *
  2122. *        Other rights are reserved except as explicitly granted      *
  2123. *        by written permission of the author.                        *
  2124. *                Pavel Curtis                                        *
  2125. *                Computer Science Dept.                              *
  2126. *                405 Upson Hall                                      *
  2127. *                Cornell University                                  *
  2128. *                Ithaca, NY 14853                                    *
  2129. *                                                                    *
  2130. *                Ph- (607) 256-4934                                  *
  2131. *                                                                    *
  2132. *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
  2133. *                decvax!cornell!pavel       (UUCPnet)                *
  2134. *********************************************************************/
  2135.  
  2136. X/*
  2137.  *    tparm.c
  2138.  *
  2139.  *  $Log:    RCS/lib_tparm.v $
  2140.  * Revision 2.1  82/10/25  14:49:19  pavel
  2141.  * Added Copyright Notice
  2142.  * 
  2143.  * Revision 2.0  82/10/24  15:17:53  pavel
  2144.  * Beta-one Test Release
  2145.  * 
  2146.  * Revision 1.3  82/08/23  22:30:38  pavel
  2147.  * The REAL Alpha-one Release Version
  2148.  * 
  2149.  * Revision 1.2  82/08/19  19:11:33  pavel
  2150.  * Alpha Test Release One
  2151.  * 
  2152.  * Revision 1.1  82/08/12  18:45:33  pavel
  2153.  * Initial revision
  2154.  * 
  2155.  *
  2156.  */
  2157.  
  2158. static char RCSid[] =
  2159.     "$Header:   RCS/lib_tparm.v  Revision 2.1  82/10/25  14:49:19  pavel  Exp$";
  2160.  
  2161. #include "curses.h"
  2162. #include "curses.priv.h"
  2163. #include "term.h"
  2164.  
  2165.  
  2166.  
  2167. X/*
  2168.  *    char *
  2169.  *    tparm(string, parms)
  2170.  *
  2171.  *    Substitute the given parameters into the given string by the following
  2172.  *    rules (taken from terminfo(5)):
  2173.  *
  2174.  *         Cursor addressing and other strings  requiring  parame-
  2175.  *    ters in the terminal are described by a parameterized string
  2176.  *    capability, with like escapes %x in  it.   For  example,  to
  2177.  *    address  the  cursor, the cup capability is given, using two
  2178.  *    parameters: the row and column to  address  to.   (Rows  and
  2179.  *    columns  are  numbered  from  zero and refer to the physical
  2180.  *    screen visible to the user, not to any  unseen  memory.)  If
  2181.  *    the terminal has memory relative cursor addressing, that can
  2182.  *    be indicated by
  2183.  *    
  2184.  *         The parameter mechanism uses  a  stack  and  special  %
  2185.  *    codes  to manipulate it.  Typically a sequence will push one
  2186.  *    of the parameters onto the stack and then print it  in  some
  2187.  *    format.  Often more complex operations are necessary.
  2188.  *    
  2189.  *         The % encodings have the following meanings:
  2190.  *    
  2191.  *         %%        outputs `%'
  2192.  *         %d        print pop() like %d in printf()
  2193.  *         %2d       print pop() like %2d in printf()
  2194.  *         %02d      print pop() like %02d in printf()
  2195.  *         %3d       print pop() like %3d in printf()
  2196.  *         %03d      print pop() like %03d in printf()
  2197.  *         %c        print pop() like %c in printf()
  2198.  *         %s        print pop() like %s in printf()
  2199.  *    
  2200.  *         %p[1-9]   push ith parm
  2201.  *         %P[a-z]   set variable [a-z] to pop()
  2202.  *         %g[a-z]   get variable [a-z] and push it
  2203.  *         %'c'      push char constant c
  2204.  *         %{nn}     push integer constant nn
  2205.  *    
  2206.  *         %+ %- %* %/ %m
  2207.  *                   arithmetic (%m is mod): push(pop() op pop())
  2208.  *         %& %| %^  bit operations: push(pop() op pop())
  2209.  *         %= %> %<  logical operations: push(pop() op pop())
  2210.  *         %! %~     unary operations push(op pop())
  2211.  *         %i        add 1 to first two parms (for ANSI terminals)
  2212.  *    
  2213.  *         %? expr %t thenpart %e elsepart %;
  2214.  *                   if-then-else, %e elsepart is optional.
  2215.  *                   else-if's are possible ala Algol 68:
  2216.  *                   %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %;
  2217.  *    
  2218.  *    For those of the above operators which are binary and not commutative,
  2219.  *    the stack works in the usual way, with
  2220.  *            %gx %gy %m
  2221.  *    resulting in x mod y, not the reverse.
  2222.  */
  2223.  
  2224. #define STACKSIZE    20
  2225.  
  2226. #define npush(x)    if (stack_ptr < STACKSIZE) {stack[stack_ptr].num = x;\
  2227.                                                 stack_ptr++;\
  2228.                                                }
  2229. #define npop()       (stack_ptr > 0  ?  stack[--stack_ptr].num  :  0)
  2230. #define spop()       (stack_ptr > 0  ?  stack[--stack_ptr].str  :  (char *) 0)
  2231.  
  2232. typedef union
  2233. {
  2234.     unsigned int    num;
  2235.     char        *str;
  2236. }        stack_frame;
  2237.  
  2238. stack_frame    stack[STACKSIZE];
  2239. static    int    stack_ptr;
  2240. static    char    buffer[256];
  2241. static    int    *param;
  2242. static    char    *bufptr;
  2243. static    int    variable[26];
  2244.  
  2245. static    char    *do_tparm();
  2246.  
  2247. char *
  2248. tparm(string, parms)
  2249. char    *string;
  2250. int    parms;
  2251. {
  2252.     char    len;
  2253.     int    number;
  2254.     int    level;
  2255.     int    x, y;
  2256.  
  2257.     param = &parms;
  2258.  
  2259. #ifdef UNDEFINED
  2260.     if (_tracing)
  2261.         _tracef("tparm(%s,%d,%d,%d,%d,%d,%d,%d,%d,%d) called",
  2262.             string, param[0], param[1], param[2], param[3],
  2263.             param[4], param[5], param[6], param[7], param[8]);
  2264. #endif
  2265.  
  2266.     stack_ptr = 0;
  2267.     bufptr = buffer;
  2268.  
  2269.     while (*string)
  2270.     {
  2271.         if (*string != '%')
  2272.         *(bufptr++) = *string;
  2273.         else
  2274.         {
  2275.         string++;
  2276.         switch (*string)
  2277.         {
  2278.             default:
  2279.             break;
  2280.  
  2281.             case '%':
  2282.             *(bufptr++) = '%';
  2283.             break;
  2284.  
  2285.             case 'd':
  2286.             sprintf(bufptr, "%d", npop());
  2287.             bufptr += strlen(bufptr);
  2288.             break;
  2289.  
  2290.             case '0':
  2291.             string++;
  2292.             len = *string;
  2293.             if ((len == '2'  ||  len == '3')  &&  *++string == 'd')
  2294.             {
  2295.                 if (len == '2')
  2296.                 sprintf(bufptr, "%02d", npop());
  2297.                 else
  2298.                 sprintf(bufptr, "%03d", npop());
  2299.                 
  2300.                 bufptr += strlen(bufptr);
  2301.             }
  2302.             break;
  2303.  
  2304.             case '2':
  2305.             string++;
  2306.             if (*string == 'd')
  2307.             {
  2308.                 sprintf(bufptr, "%2d", npop());
  2309.                 bufptr += strlen(bufptr);
  2310.             }
  2311.             break;
  2312.  
  2313.             case '3':
  2314.             string++;
  2315.             if (*string == 'd')
  2316.             {
  2317.                 sprintf(bufptr, "%3d", npop());
  2318.                 bufptr += strlen(bufptr);
  2319.             }
  2320.             break;
  2321.  
  2322.             case 'c':
  2323.             *(bufptr++) = (char) npop();
  2324.             break;
  2325.  
  2326.             case 's':
  2327.             strcpy(bufptr, spop());
  2328.             bufptr += strlen(bufptr);
  2329.             break;
  2330.  
  2331.             case 'p':
  2332.             string++;
  2333.             if (*string >= '1'  &&  *string <= '9')
  2334.                 npush(param[*string - '1']);
  2335.             break;
  2336.  
  2337.             case 'P':
  2338.             string++;
  2339.             if (*string >= 'a'  &&  *string <= 'z')
  2340.                 variable[*string - 'a'] = npop();
  2341.             break;
  2342.  
  2343.             case 'g':
  2344.             string++;
  2345.             if (*string >= 'a'  &&  *string <= 'z')
  2346.                 npush(variable[*string - 'a']);
  2347.             break;
  2348.  
  2349.             case '\'':
  2350.             string++;
  2351.             npush(*string);
  2352.             string++;
  2353.             break;
  2354.  
  2355.             case '{':
  2356.             number = 0;
  2357.             string++;
  2358.             while (*string >= '0'  &&  *string <= '9')
  2359.             {
  2360.                 number = number * 10 + *string - '0';
  2361.                 string++;
  2362.             }
  2363.             npush(number);
  2364.             break;
  2365.  
  2366.             case '+':
  2367.             npush(npop() + npop());
  2368.             break;
  2369.  
  2370.             case '-':
  2371.             y = npop();
  2372.             x = npop();
  2373.             npush(x - y);
  2374.             break;
  2375.  
  2376.             case '*':
  2377.             npush(npop() * npop());
  2378.             break;
  2379.  
  2380.             case '/':
  2381.             y = npop();
  2382.             x = npop();
  2383.             npush(x / y);
  2384.             break;
  2385.  
  2386.             case 'm':
  2387.             y = npop();
  2388.             x = npop();
  2389.             npush(x % y);
  2390.             break;
  2391.  
  2392.             case '&':
  2393.             npush(npop() & npop());
  2394.             break;
  2395.  
  2396.             case '|':
  2397.             npush(npop() | npop());
  2398.             break;
  2399.  
  2400.             case '^':
  2401.             npush(npop() ^ npop());
  2402.             break;
  2403.  
  2404.             case '=':
  2405.             y = npop();
  2406.             x = npop();
  2407.             npush(x == y);
  2408.             break;
  2409.  
  2410.             case '<':
  2411.             y = npop();
  2412.             x = npop();
  2413.             npush(x < y);
  2414.             break;
  2415.  
  2416.             case '>':
  2417.             y = npop();
  2418.             x = npop();
  2419.             npush(x > y);
  2420.             break;
  2421.  
  2422.             case '!':
  2423.             npush(! npop());
  2424.             break;
  2425.  
  2426.             case '~':
  2427.             npush(~ npop());
  2428.             break;
  2429.  
  2430.             case 'i':
  2431.             param[0]++;
  2432.             param[1]++;
  2433.             break;
  2434.  
  2435.             case '?':
  2436.             break;
  2437.  
  2438.             case 't':
  2439.             x = npop();
  2440.             if (x)
  2441.             {
  2442.                 /* do nothing; keep executing */
  2443.             }
  2444.             else
  2445.             {
  2446.                 /* scan forward for %e or %; at level zero */
  2447.                 string++;
  2448.                 level = 0;
  2449.                 while (*string)
  2450.                 {
  2451.                     if (*string == '%')
  2452.                     {
  2453.                     string++;
  2454.                     if (*string == '?')
  2455.                         level++;
  2456.                     else if (*string == ';')
  2457.                     {
  2458.                         if (level > 0)
  2459.                         level--;
  2460.                         else
  2461.                         break;
  2462.                     }
  2463.                     else if (*string == 'e'  && level == 0)
  2464.                         break;
  2465.                     }
  2466.  
  2467.                     if (*string)
  2468.                     string++;
  2469.                 }
  2470.             }
  2471.             break;
  2472.  
  2473.             case 'e':
  2474.             /* scan forward for a %; at level zero */
  2475.                 string++;
  2476.                 level = 0;
  2477.                 while (*string)
  2478.                 {
  2479.                 if (*string == '%')
  2480.                 {
  2481.                     string++;
  2482.                     if (*string == '?')
  2483.                     level++;
  2484.                     else if (*string == ';')
  2485.                     {
  2486.                     if (level > 0)
  2487.                         level--;
  2488.                     else
  2489.                         break;
  2490.                     }
  2491.                 }
  2492.  
  2493.                 if (*string)
  2494.                     string++;
  2495.                 }
  2496.             break;
  2497.  
  2498.             case ';':
  2499.             break;
  2500.  
  2501.         } /* endswitch (*string) */
  2502.         } /* endelse (*string == '%') */
  2503.  
  2504.         if (*string == '\0')
  2505.         break;
  2506.         
  2507.         string++;
  2508.     } /* endwhile (*string) */
  2509.  
  2510.     *bufptr = '\0';
  2511.     return(buffer);
  2512. }
  2513.  
  2514.  
  2515.  
  2516. X/*
  2517.  *    char *
  2518.  *    tgoto(string, x, y)
  2519.  *
  2520.  *    Retained solely for upward compatibility.  Note the intentional
  2521.  *    reversing of the last two arguments.
  2522.  *
  2523.  */
  2524.  
  2525. char *
  2526. tgoto(string, x, y)
  2527. char    *string;
  2528. int    x, y;
  2529. {
  2530.     return(tparm(string, y, x));
  2531. }
  2532. //go.sysin dd *
  2533. exit
  2534.