home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1887 / cursestty.c next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.3 KB  |  197 lines

  1. /*
  2.  * Smart terminal output routine.
  3.  * Uses curses to display the object.
  4.  */
  5. #include <signal.h>
  6. #include <curses.h>
  7. #include "stdarg.h"
  8.  
  9.  
  10. #define    STATUSLINE    22
  11. #define    INPUTLINE    23
  12.  
  13.  
  14. static    int    inputready;
  15.  
  16. static void    gotinput();
  17. extern void    ttywrite();
  18.  
  19.  
  20. /*
  21.  * Open the terminal and enable for detecting terminal input.
  22.  */
  23. ttyopen()
  24. {
  25.     initscr();
  26.     cbreak();
  27.     signal(SIGINT, gotinput);
  28.     return 0;
  29. }
  30.  
  31.  
  32. static void
  33. gotinput()
  34. {
  35.     signal(SIGINT, gotinput);
  36.     inputready = 1;
  37. }
  38.  
  39.  
  40. /*
  41.  * Close the terminal.
  42.  */
  43. void
  44. ttyclose()
  45. {
  46.     refresh();
  47.     endwin();
  48. }
  49.  
  50.  
  51. /*
  52.  * Test to see if a keyboard character is ready.
  53.  * Returns nonzero if so (and clears the ready flag).
  54.  */
  55. ttycheck()
  56. {
  57.     int    result;
  58.  
  59.     result = inputready;
  60.     inputready = 0;
  61.     return result;
  62. }
  63.  
  64.  
  65. /*
  66.  * Print a formatted string to the terminal.
  67.  * The string length is limited to 256 characters.
  68.  */
  69. void
  70. ttyprintf(fmt)
  71.     char    *fmt;
  72. {
  73.     va_list ap;
  74.     static char    buf[256];
  75.  
  76.     va_start(ap, fmt);
  77.     vsprintf(buf, fmt, ap);
  78.     va_end(ap);
  79.     addstr(buf);
  80. }
  81.  
  82.  
  83. /*
  84.  * Print a status line, similar to printf.
  85.  * The string length is limited to 256 characters.
  86.  */
  87. void
  88. ttystatus(fmt)
  89.     char    *fmt;
  90. {
  91.     va_list ap;
  92.     static char    buf[256];
  93.  
  94.     va_start(ap, fmt);
  95.     vsprintf(buf, fmt, ap);
  96.     va_end(ap);
  97.  
  98.     move(STATUSLINE, 0);
  99.     addstr(buf);
  100.     clrtobot();
  101.     move(0, 0);
  102.     refresh();
  103. }
  104.  
  105.  
  106. void
  107. ttywrite(cp, len)
  108.     char    *cp;
  109. {
  110.     while (len-- > 0) {
  111.         addch(*cp);
  112.         cp++;
  113.     }
  114. }
  115.  
  116.  
  117. void
  118. ttyhome()
  119. {
  120.     move(0, 0);
  121. }
  122.  
  123.  
  124. void
  125. ttyeeop()
  126. {
  127.     clrtobot();
  128. }
  129.  
  130.  
  131. void
  132. ttyflush()
  133. {
  134.     refresh();
  135. }
  136.  
  137.  
  138. /*
  139.  * Return a NULL terminated input line (without the final newline).
  140.  * The specified string is printed as a prompt.
  141.  * Returns nonzero (and an empty buffer) on EOF or error.
  142.  */
  143. ttyread(prompt, buf, buflen)
  144.     char    *prompt;
  145.     char    *buf;
  146. {
  147.     int    c;
  148.     char    *cp;
  149.  
  150.     move(INPUTLINE, 0);
  151.     addstr(prompt);
  152.     clrtoeol();
  153.  
  154.     cp = buf;
  155.     for (;;)
  156.     {
  157.         refresh();
  158.         c = fgetc(stdin);
  159.         switch (c)
  160.         {
  161.         case EOF:
  162.             buf[0] = '\0';
  163.             move(INPUTLINE, 0);
  164.             clrtoeol();
  165.             move(0, 0);
  166.             refresh();
  167.             return -1;
  168.  
  169.         default:
  170.             *cp++ = c;
  171.             addch(c);
  172.             if (cp < buf + buflen - 1)
  173.                 break;
  174.             /* fall through... */
  175.  
  176.         case '\n':
  177.         case '\r':
  178.             *cp = 0;
  179.             move(INPUTLINE, 0);
  180.             clrtoeol();
  181.             move(0, 0);
  182.             refresh();
  183.             return 0;
  184.  
  185.         case '\b':
  186.             if (cp == buf)
  187.                 break;
  188.             --cp;
  189.             addch('\b');
  190.             clrtoeol();
  191.             break;
  192.         }
  193.     }
  194. }
  195.  
  196. /* END CODE */
  197.