home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume1 / sol / sol.c < prev    next >
C/C++ Source or Header  |  1987-07-20  |  17KB  |  879 lines

  1. #include    <stdio.h>
  2. #include    <signal.h>
  3. #include    <sys/time.h>
  4. #include    <sys/types.h>
  5. #include    <sys/stat.h>
  6. #include    <sys/ioctl.h>
  7.  
  8. #define     TRUE    1
  9. #define        FALSE    0
  10.  
  11. int    amode;
  12. int    cnt;
  13. int    cheat;
  14. int    deck[53];
  15. int    over[53];
  16. int    ace[4][14];
  17. int    run[7][14];
  18. int    hidden[7][8];
  19. char    cmd[40], *cp;
  20. char    *resgb;
  21.  
  22. char    PC;
  23. char    BC[2];
  24. char    UP[2];
  25.  
  26. char    *sc_bs;
  27. char    *sc_move;
  28. char    *sc_clear;
  29. char    *sc_cleol;
  30. char    *sc_init;
  31. char    *sc_deinit;
  32. char    *sc_s_in;
  33. char    *sc_s_out;
  34.  
  35. short    ospeed;
  36.  
  37. main(n, a)
  38. char **a;
  39.  {
  40.     int i, p, c, r, s;
  41.     struct timeval tp;
  42.     struct timezone tzp;
  43.     int from;
  44.     int dest;
  45.     int brkflg;
  46.  
  47.     amode = 0;
  48.     gettimeofday(&tp, &tzp);
  49.     srand(getpid() + tp.tv_sec);
  50.     rawmode(TRUE);
  51.     setsig(TRUE);
  52.     getterm();
  53.     init();
  54.     for (i = 0; i < 52; i++)
  55.       deck[i] = i;
  56.     deck[52] = -1;
  57.     shuffle(deck);
  58.     for (i = 0; i < 4; i++)
  59.       ace[i][0] = -1;
  60.     for (i = 0; i < 7; i++)
  61.       hidden[i][0] = run[i][0] = -1;
  62.     over[0] = -1;
  63.     for (p = 0; p < 7; p++)
  64.       for (c = p; c < 7; c++)
  65.     put(hidden[c],get(deck));
  66.     for (r = 0; r < 7; r++)
  67.       put(run[r],get(hidden[r]));
  68.     redraw(TRUE);
  69.     for (r = 0; r < 7; r++)
  70.      {
  71.     while (getvalue(run[r][0]) == 0)
  72.      {
  73.         s = getsuit(run[r][0]);
  74.         put(ace[s],get(run[r]));
  75.         put(run[r],get(hidden[r]));
  76.         redraw(FALSE);
  77.      }
  78.      }
  79.     cnt = cheat = 0;
  80.     if (n > 1 && !strcmp(a[1], "auto"))
  81.       autopilot();
  82.     brkflg = 1;
  83.     while (brkflg)
  84.      {
  85.     cnt++;
  86.     do
  87.      {
  88.         moveto(17, 0);
  89.         printf("cmd:%d> ", cnt);
  90.         cleol();
  91.         fflush(stdout);
  92.      } while (!getst(cp = cmd));
  93.     while (*cp == ' ' || *cp == '\t')
  94.       cp++;
  95.     switch (*cp++)
  96.      {
  97.         case 'a':
  98.           autopilot();
  99.         break;
  100.         case 'm':
  101.          {
  102.         while (*cp == ' ' || *cp == '\t')
  103.           cp++;
  104.         if (!(from = *cp++) || ((from < '1' || from > '7') &&
  105.                                 from != 'd'))
  106.          {
  107.             whoops();
  108.             continue;
  109.          }
  110.         while (*cp == ' ' || *cp == '\t')
  111.           cp++;
  112.         if (!(dest = *cp++) || ((dest < '1' || dest > '7') &&
  113.                 dest != 'a') || !movecard(from, dest, FALSE))
  114.          {
  115.             whoops();
  116.             continue;
  117.          }
  118.         if (cardsleft() == 0)
  119.           finish();
  120.          }
  121.         break;
  122.         case 't':
  123.         case 0:
  124.           thumb();
  125.         break;
  126.         case 'h':
  127.         case '?':
  128.           disphelp();
  129.         break;
  130.         case 'r':
  131.           disprules();
  132.         break;
  133.         case 'q':
  134.           brkflg = 0;
  135.         break;
  136.         case 's':
  137.          {
  138.         while (peek(over) != -1)
  139.           put(deck, get(over));
  140.         shuffle(deck);
  141.         redraw(FALSE);
  142.         cheat++;
  143.          }
  144.         break;
  145.         case 'p':
  146.          {
  147.         while (*cp == ' ' || *cp == '\t')
  148.           cp++;
  149.         if (!(from = *cp++) || from < '1' || from > '7')
  150.          {
  151.             whoops();
  152.             continue;
  153.          }
  154.             if (hidden[from -= '1'][0] != -1)
  155.          {
  156.             while (hidden[from][0] != -1)
  157.               put(deck, get(hidden[from]));
  158.             redraw(FALSE);
  159.             cheat++;
  160.          }
  161.         else
  162.           whoops();
  163.          }
  164.         break;
  165.         case 'd':
  166.          {
  167.         moveto(19, 0);
  168.         printf("%d cards in deck:\n", lstlen(deck) + lstlen(over));
  169.         i = lstlen(deck);
  170.         while (--i != -1)
  171.          {
  172.             pcard(deck[i]);
  173.             putchar(' ');
  174.          }
  175.         putchar('\n');
  176.         for (i = 0; over[i] != -1; i++)
  177.          {
  178.             pcard(over[i]);
  179.             putchar(' ');
  180.          }
  181.         printf("\nHit return -");
  182.         fflush(stdout);
  183.         getcx();
  184.         moveto(19, 0);
  185.         for (i = 0; i < 4; i++)
  186.          {
  187.             cleol();
  188.             putchar('\n');
  189.          }
  190.         cheat++;
  191.          }
  192.         break;
  193.         case 'w':
  194.          {
  195.         while (*cp == ' ' || *cp == '\t')
  196.           cp++;
  197.         if (!(from = *cp++) || from < '1' || from > '7')
  198.          {
  199.             whoops();
  200.             continue;
  201.          }
  202.         moveto(19, 0);
  203.         from -= '1';
  204.         printf("%d cards hidden under run %c:\n", lstlen(hidden[from]),
  205.                                 from + '1');
  206.         for (i = 0; hidden[from][i] != -1; i++)
  207.          {
  208.             pcard(hidden[from][i]);
  209.             putchar(' ');
  210.          }
  211.         printf("\nHit return -");
  212.         fflush(stdout);
  213.         getcx();
  214.         moveto(19,0);
  215.         for (i = 0; i < 4; i++)
  216.          {
  217.             cleol();
  218.             putchar('\n');
  219.          }
  220.         cheat++;
  221.          }
  222.         break;
  223.         default:
  224.           whoops();
  225.         break;
  226.      }
  227.      }
  228.     moveto(19, 0);
  229.     cleol();
  230.     printf("I see you gave up\n");
  231.     if (cheat > 0)
  232.       printf("    ... even after you cheated %d times!\n", cheat);
  233.     else
  234.       printf("    ... but at least you didn't cheat...congratulations!\n");
  235.     unwind();
  236.  }
  237.  
  238. unwind()
  239.  {
  240.     moveto(23, 0);
  241.     deinit();
  242.     rawmode(FALSE);
  243.     exit(0);
  244.  }
  245.  
  246. movecard(from,dest,limitmove)
  247.  {
  248.     if (from == dest)
  249.       return(FALSE);
  250.     if (from == 'd')
  251.       return(dest == 'a' ? deck2ace() : deck2run(dest));
  252.     else
  253.       return(dest == 'a' ? run2ace(from) : run2run(from,dest,limitmove));
  254.  }
  255.  
  256. deck2run(dest)
  257.  {
  258.     int fcard, dcard, s, ok;
  259.  
  260.     if ((fcard = peek(over)) == -1)
  261.       return(FALSE);
  262.     dcard = peek(run[dest -= '1']);
  263.     if (ok = chk2run(fcard,dcard))
  264.      {
  265.     put(run[dest],get(over));
  266.     redraw(FALSE);
  267.     while (getvalue(peek(over)) == 0)
  268.      {
  269.         s = getsuit(peek(over));
  270.         put(ace[s],get(over));
  271.         redraw(FALSE);
  272.      }
  273.      }
  274.     return(ok);
  275.  }
  276.  
  277. deck2ace()
  278.  {
  279.     int fcard, s, ok;
  280.  
  281.     if (ok = (fcard = peek(over)) != -1 &&
  282.         getvalue(peek(ace[s = getsuit(fcard)])) == getvalue(fcard) - 1)
  283.      {
  284.     put(ace[s], get(over));
  285.     redraw(FALSE);
  286.     while (getvalue(peek(over)) == 0)
  287.      {
  288.         s = getsuit(peek(over));
  289.         put(ace[s],get(over));
  290.         redraw(FALSE);
  291.      }
  292.      }
  293.     return(ok);
  294.  }
  295.  
  296. run2ace(from)
  297.  {
  298.     int fcard, s, ok;
  299.  
  300.     fcard = peek(run[from -= '1']);
  301.     if (ok = getvalue(peek(ace[s = getsuit(fcard)])) == getvalue(fcard) - 1)
  302.      {
  303.     put(ace[s], get(run[from]));
  304.     redraw(FALSE);
  305.     if (run[from][0] == -1 && hidden[from][0] >= 0)
  306.      {
  307.         while (getvalue(peek(hidden[from])) == 0)
  308.          {
  309.         s = getsuit(peek(hidden[from]));
  310.         put(ace[s],get(hidden[from]));
  311.         redraw(FALSE);
  312.          }
  313.         put(run[from],get(hidden[from]));
  314.         redraw(FALSE);
  315.      }
  316.      }
  317.     return(ok);
  318.  }
  319.  
  320. run2run(from,dest,limitmove)
  321.  {
  322.     int fcard, dcard, i, ok, s;
  323.  
  324.     if ((fcard = run[from -= '1'][0]) == -1)
  325.       return(FALSE);
  326.     dcard = peek(run[dest -= '1']);
  327.     if (amode > 0 && dcard == -1 && getvalue(fcard) == 12 &&
  328.                         lstlen(hidden[from]) == 0)
  329.       return(FALSE);
  330.     if (amode > 0 && !limitmove && lstlen(hidden[from]) == 0)
  331.       return(FALSE);
  332.     if (ok = chk2run(fcard,dcard))
  333.      {
  334.     for (i = 0; run[from][i] != -1; i++)
  335.       put(run[dest], run[from][i]);
  336.     run[from][0] = -1;
  337.     redraw(FALSE);
  338.     if (lstlen(hidden[from]) > 0)
  339.      {
  340.         while (getvalue(peek(hidden[from])) == 0)
  341.          {
  342.         s = getsuit(peek(hidden[from]));
  343.         put(ace[s],get(hidden[from]));
  344.         redraw(FALSE);
  345.          }
  346.         put(run[from],get(hidden[from]));
  347.         redraw(FALSE);
  348.      }
  349.      }
  350.     return(ok);
  351.  }
  352.  
  353. chk2run(fcard,dcard)
  354.  {
  355.     if (dcard == -1 && getvalue(fcard) == 12)
  356.       return(TRUE);
  357.     return(getvalue(dcard) - 1 == getvalue(fcard) &&
  358.                 getcolour(dcard) != getcolour(fcard));
  359.  }
  360.  
  361. finish()
  362.  {
  363.     int i, k;
  364.  
  365.     moveto(19,0);
  366.     printf("\007I'll finish for you now\007");
  367.     fflush(stdout);
  368.     do
  369.      {
  370.     k = FALSE;
  371.     for (i = 0; i < 7; i++)
  372.       k = movecard(i + '1', 'a', FALSE) || k;
  373.      } while (k);
  374.     moveto(20,0);
  375.     cleol();
  376.     printf("\007You WIN\007\n");
  377.     if (cheat > 0)
  378.       printf("    ... but you cheated %d times!", cheat);
  379.     else
  380.       printf("    ... and without cheating ... congratulations!");
  381.     unwind();
  382.  }
  383.  
  384. autopilot()
  385.  {
  386.     int i, j, k;
  387.     int iter;
  388.  
  389.     moveto(19,0);
  390.     printf("Going into automatic mode...");
  391.     fflush(stdout);
  392.     amode = TRUE;
  393.     while (cardsleft() > 0)
  394.      {
  395.     k = FALSE;
  396.     for (i = 0; i < 7; i++)
  397.       for (j = 0; j < 7; j++)
  398.         k = movecard(i + '1', j + '1', TRUE) || k;
  399.     for (i = 0; i < 7; i++)
  400.      {
  401.         k = movecard(i + '1', 'a', FALSE) || k;
  402.         k = movecard('d', i + '1', FALSE) || k;
  403.      }
  404.     k = movecard('d', 'a', FALSE) || k;
  405.     if (!k)
  406.      {
  407.         if (iter--)
  408.           thumb();
  409.         else
  410.           break;
  411.      }
  412.     else
  413.       iter = lstlen(over) + lstlen(deck);
  414.      }
  415.     do
  416.      {
  417.     k = FALSE;
  418.     for (i = 0; i < 7; i++)
  419.       k = movecard(i + '1', 'a', FALSE) || k;
  420.      } while (k);
  421.     moveto(19, 28);
  422.     if (cardsleft() == 0)
  423.      {
  424.     printf("\007YEA...\007");
  425.     fflush(stdout);
  426.     for (i = 0; i < 7; i++)
  427.       if (movecard(i + '1', 'a', FALSE));
  428.     moveto(19, 34);
  429.     printf("I won!!!!!");
  430.      }
  431.     else
  432.      {
  433.     printf("I couldn't win this time");
  434.     moveto(20, 0);
  435.     printf("%d cards in deck", lstlen(deck) + lstlen(over));
  436.      }
  437.     unwind();
  438.  }
  439.  
  440. redraw(display)
  441.  {
  442.     static long_run[7];
  443.     static long_hide[7];
  444.     static long_ace[4];
  445.     static base_run[7];
  446.  
  447.     int i, j, k;
  448.  
  449.     if (display)
  450.      {
  451.     clear();
  452.     for (i = 0; i < 7; i++)
  453.      {
  454.         long_run[i] = long_hide[i] = base_run[i] = -1;
  455.         moveto(0, 8 + i * 5);
  456.         putchar(i + '1');
  457.      }
  458.     for (i = 0; i < 4; i++)
  459.       long_ace[i] = -1;
  460.     moveto(0,56);
  461.     printf("ACES");
  462.     moveto(6,56);
  463.     printf("DECK");
  464.      }
  465.     for (i = 0; i < 7; i++)
  466.      {
  467.     if (long_hide[i] != lstlen(hidden[i]))
  468.      {
  469.         moveto(2, 8 + i * 5);
  470.         putchar(lstlen(hidden[i]) ? lstlen(hidden[i]) + '0' : ' ');
  471.         long_hide[i] = lstlen(hidden[i]);
  472.      }
  473.      }
  474.     for (i = 0; i < 4; i++)
  475.      {
  476.     if (long_ace[i] != lstlen(ace[i]))
  477.      {
  478.         moveto(2, 49 + i * 5);
  479.         if (ace[i][0] != -1)
  480.           pcard(peek(ace[i]));
  481.         else
  482.           printf("--");
  483.         long_ace[i] = lstlen(ace[i]);
  484.      }
  485.      }
  486.     moveto(8, 55);
  487.     printf(lstlen(deck) ? "##  " : "    ");
  488.     if (lstlen(over))
  489.       pcard(peek(over));
  490.     else
  491.       printf("  ");
  492.     for (i = 0; i < 7; i++)
  493.      {
  494.     if (long_run[i] != lstlen(run[i]) ||
  495.                 (long_run[i] == 1 && base_run[i] != run[i][0]))
  496.      {
  497.         for (j = 0; run[i][j] != -1; j++)
  498.          {
  499.         moveto(j + 4, 7 + i * 5);
  500.         if (run[i][j] == -1)
  501.           printf("  ");
  502.         else
  503.           pcard(run[i][j]);
  504.          }
  505.         while (j < long_run[i])
  506.          {
  507.         moveto(j++ + 4, 7 + i * 5);
  508.         printf("  ");
  509.          }
  510.         long_run[i] = lstlen(run[i]);
  511.         base_run[i] = run[i][0];
  512.      }
  513.      }
  514.     fflush(stdout);
  515.     sleep(1);
  516.  }
  517.  
  518. thumb()
  519.  {
  520.     int i, s;
  521.  
  522.     if (deck[0] == -1)
  523.      {
  524.     if (over[0] == -1)
  525.       return;
  526.     while (over[0] != -1)
  527.       put(deck, get(over));
  528.      }
  529.     for (i = 0; i < 3; i++)
  530.       if (deck[0] != -1)
  531.     put(over,get(deck));
  532.     redraw(FALSE);
  533.     while (getvalue(peek(over)) == 0)
  534.      {
  535.     s = getsuit(peek(over));
  536.     put(ace[s], get(over));
  537.     redraw(FALSE);
  538.      }
  539.     if (over[0] == -1 && deck[0] != -1)
  540.       thumb();
  541.  }
  542.  
  543. pcard(card)
  544.  {
  545.     int i;
  546.  
  547.     if (i = getcolour(card))
  548.       standout();
  549.     printf("%c%c", "A23456789TJQK"[getvalue(card)], "HDSC"[getsuit(card)]);
  550.     if (i)
  551.       standend();
  552.  }
  553.  
  554.  
  555. getvalue(card)
  556.  {
  557.     return(card % 13);
  558.  }
  559.  
  560. getsuit(card)
  561.  {
  562.     return(card / 13);
  563.  }
  564.  
  565. getcolour(card)
  566.  {
  567.     return(card / 26);
  568.  }
  569.  
  570. cardsleft()
  571.  {
  572.     int i, t;
  573.  
  574.     t = lstlen(deck) + lstlen(over);
  575.     for (i = 0; i < 7; i++)
  576.       t += lstlen(hidden[i]);
  577.     return(t);
  578.  }
  579.  
  580. whoops()
  581.  {
  582.     moveto(17,0);
  583.     printf("\007Invalid Command: '%s'\007", cmd);
  584.     fflush(stdout);
  585.     sleep(4);
  586.  }
  587.  
  588.  
  589. disphelp()
  590.  {
  591.     clear();
  592.     printf("\
  593. Commands: t or RETURN     : thumb the deck 3 cards at a time\n\
  594.           m [d1-7] [1-7a] : move cards or runs\n\
  595.           a               : turn on the auto pilot (in case you get stuck)\n\
  596.           s               : shuffle the deck (cheat!)\n\
  597.           p [2-7]         : put a hidden pile into the deck (cheat!)\n\
  598.           d               : print the cards in the deck (cheat!)\n\
  599.           w [2-7]         : print the cards in a hidden pile (cheat!)\n\
  600.           h or ?          : print this command summary\n\
  601.           r               : print the rules of the game\n\
  602.           q               : quit\n\n");
  603.     printf("\
  604. Moving:   1-7, 'd', or 'a' select the source and destination for a move.\n\
  605.           Valid moves are from a run to a run, from the deck to a run,\n\
  606.           from a run to an ace pile, and from the deck to an ace pile.\n\n\
  607. Cheating: Commands that allow cheating are available but they will count\n\
  608.           against you in your next life!\n\n\nHit Return -");
  609.     fflush(stdout);
  610.     getcx();
  611.     redraw(TRUE);
  612.  }
  613.  
  614. disprules()
  615.  {
  616.     clear();
  617.     printf("\
  618. Object:   The object of this game is to get all of the cards in each suit\n\
  619.           in order on the proper ace pile.\n\n\
  620. Rules:    Cards are played on the ace piles in ascending order: A,2,...,K. \n\
  621.           All aces are automatically placed in the correct aces pile as\n\
  622.           they're found in the deck or in a pile of hidden cards.  Once a\n\
  623.           card is placed in an ace pile it can't be removed.\n\n");
  624.     printf("\
  625.           Cards must be played in descending order: K,Q,..,2, on the seven\n\
  626.           runs which are initially dealt.  They must always be played on a\n\
  627.           card of the opposite color.  Runs must always be moved as a\n\
  628.           whole, unless you're moving the lowest card on a run to the\n\
  629.           correct ace pile.\n\n");
  630.     printf("\
  631.           Whenever a whole run is moved, the top hidden card is turned\n\
  632.           over, thus becoming the beginning of a new run.  If there are no\n\
  633.           hidden cards left, a space is created which can only be filled by\n\
  634.           a king.\n\n\
  635.           The rest of the deck is thumbed 3 cards at a time, until you spot a\n\
  636.           valid move.  Whenever the bottom of the deck is reached, the cards\n\
  637.           are turned over and you can continue thumbing.\n\n\nHit Return -");
  638.     fflush(stdout);
  639.     getcx();
  640.     redraw(TRUE);
  641.  }
  642.  
  643. getst(getbuf, pc)
  644. char *getbuf;
  645.  {
  646.     int ch;
  647.     int nc;
  648.  
  649.     *(resgb = getbuf) = nc = 0;
  650.     for (;;)
  651.      {
  652.     if ((((ch = getcx()) >= ' ' && ch <= '~') || ch == '\t') && nc < 40)
  653.      {
  654.         *getbuf++ = ch;
  655.         *getbuf = 0;
  656.         nc++;
  657.         putchar(ch);
  658.         fflush(stdout);
  659.      }
  660.     else if (ch == '\b')
  661.      {
  662.         if (nc--)
  663.          {
  664.             *--getbuf = 0;
  665.         printf("\b \b");
  666.         fflush(stdout);
  667.          }
  668.         else
  669.           return(FALSE);
  670.      }
  671.     else if (ch == 'r' & 0x1f)
  672.       redraw(TRUE);
  673.     else if (ch == '\n')
  674.       return(TRUE);
  675.      }
  676.  }
  677.  
  678. shuffle(cards)
  679. int *cards;
  680.  {
  681.     int i, j, k, t;
  682.  
  683.     for (i = 0; cards[i] != -1; i++)
  684.       ;
  685.     if (i)
  686.      {
  687.     for (j = 0; j < i; j++)
  688.      {
  689.         do
  690.           k = rnd(i);
  691.          while (k == j);
  692.         t = cards[j];
  693.         cards[j] = cards[k];
  694.         cards[k] = t;
  695.      }
  696.      }
  697.  }
  698.  
  699. rnd(n)
  700.  {
  701.     return(((rand() >> 12 | rand() >> 24) & (unsigned) ~0 >> 1) % n);
  702.  }
  703.  
  704. lstlen(list)
  705. int *list;
  706.  {
  707.     int i;
  708.  
  709.     for (i = 0; list[i] != -1; i++)
  710.       ;
  711.     return(i);
  712.  }
  713.  
  714. restart()
  715.  {
  716.     setsig(TRUE);
  717.     rawmode(TRUE);
  718.     redraw(TRUE);
  719.     moveto(17, 0);
  720.     printf("cmd:%d> ", cnt);
  721.     cleol();
  722.     printf("%s", resgb);
  723.     fflush(stdout);
  724.  }
  725.  
  726. setsig(on)
  727.  {
  728.     signal(SIGINT, on ? SIG_IGN : SIG_DFL);
  729.     signal(SIGQUIT, on ? SIG_IGN : SIG_DFL);
  730.     signal(SIGTERM, on ? SIG_IGN : SIG_DFL);
  731.     signal(SIGTSTP, on ? SIG_IGN : SIG_DFL);
  732.     signal(SIGCONT, on ? restart : SIG_DFL);
  733.  }
  734.  
  735. stop()
  736.  {
  737.     setsig(FALSE);
  738.     rawmode(FALSE);
  739.     signal(SIGCONT, restart);
  740.     kill(getpid(), SIGTSTP);
  741.  }
  742.  
  743. getcx()
  744.  {
  745.     char ch;
  746.  
  747.     signal(SIGTSTP, stop);
  748.     read(0, &ch, 1);
  749.     signal(SIGTSTP, SIG_IGN);
  750.     return(ch);
  751.  }
  752.  
  753. rawmode(on)
  754.  {
  755.     struct sgttyb s;
  756.     static struct sgttyb saveterm;
  757.     static int saved = FALSE;
  758.  
  759.     if (on)
  760.      {
  761.     ioctl(0, TIOCGETP, &s);
  762.     if (saved == FALSE)
  763.      {
  764.         saveterm = s;
  765.         saved = TRUE;
  766.      }
  767.     ospeed = s.sg_ospeed;
  768.     s.sg_flags |= CBREAK;
  769.     s.sg_flags &= ~(ECHO|XTABS);
  770.      }
  771.     else
  772.       s = saveterm;
  773.     ioctl(0, TIOCSETN, &s);
  774.  }
  775.  
  776. getterm()
  777.  {
  778.     char termbuf[1024];
  779.     char *sp;
  780.     static char sbuf[150];
  781.     char *tgetstr();
  782.  
  783.     tgetent(termbuf, getenv("TERM"));
  784.     sp = sbuf;
  785.  
  786.     PC = *(tgetstr("pc", &sp));
  787.     *UP = *(tgetstr("pc", &sp));
  788.     UP[1] = 0;
  789.     *BC = (sc_bs = tgetstr("bs", &sp)) ? *sc_bs : '\b';
  790.     BC[1] = 0;
  791.     sc_init = tgetstr("ti", &sp);
  792.     sc_deinit = tgetstr("te", &sp);
  793.     sc_cleol = tgetstr("ce", &sp);
  794.     sc_clear = tgetstr("cl", &sp);
  795.     sc_move = tgetstr("cm", &sp);
  796.     sc_s_in = tgetstr("so", &sp);
  797.     sc_s_out = tgetstr("se", &sp);
  798.  }
  799.  
  800. xputc(c)
  801. register c;
  802.  {
  803.     putchar(c);
  804.  }
  805.  
  806. init()
  807.  {
  808.     tputs(sc_init, 24, xputc);
  809.  }
  810.  
  811. deinit()
  812.  {
  813.     tputs(sc_deinit, 24, xputc);
  814.  }
  815.  
  816. clear()
  817.  {
  818.     tputs(sc_clear, 24, xputc);
  819.  }
  820.  
  821. cleol()
  822.  {
  823.     tputs(sc_cleol, 1, xputc);
  824.  }
  825.  
  826. standout()
  827.  {
  828.     tputs(sc_s_in, 1, xputc);
  829.  }
  830.  
  831. standend()
  832.  {
  833.     tputs(sc_s_out, 1, xputc);
  834.  }
  835.  
  836. moveto(y, x)
  837.  {
  838.     char *tgoto();
  839.  
  840.     tputs(tgoto(sc_move, x, y), 1, xputc);
  841.  }
  842.  
  843. put(dest, item)
  844. int *dest;
  845.  {
  846.     if (item == -1)
  847.       return;
  848.     while (*dest != -1)
  849.       dest++;
  850.     *dest++ = item;
  851.     *dest = -1;
  852.  }
  853.  
  854. get(source)
  855. int *source;
  856.  {
  857.     int i;
  858.  
  859.     for (i = 0; *source != -1; i++, source++)
  860.       ;
  861.     if (i)
  862.      {
  863.     i = *--source;
  864.     *source = -1;
  865.     return(i);
  866.      }
  867.     return(-1);
  868.  }
  869.  
  870. peek(source)
  871. int *source;
  872.  {
  873.     int i;
  874.  
  875.     for (i = 0; source[i] != -1; i++)
  876.       ;
  877.     return(i ? source[i - 1] : -1);
  878.  }
  879.