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

  1. /*
  2.  * Dumb terminal output routine.
  3.  * Does no cursor addressing stuff.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <signal.h>
  8. #include "stdarg.h"
  9.  
  10.  
  11. static    int    inputready;        /* nonzero if input now ready */
  12.  
  13. static void    gotinput();
  14. extern void    ttywrite();
  15.  
  16.  
  17. /*
  18.  * Open the terminal and enable for detecting terminal input.
  19.  */
  20. ttyopen()
  21. {
  22.     signal(SIGINT, gotinput);
  23.     return 0;
  24. }
  25.  
  26.  
  27. static void
  28. gotinput()
  29. {
  30.     signal(SIGINT, gotinput);
  31.     inputready = 1;
  32. }
  33.  
  34.  
  35. /*
  36.  * Close the terminal.
  37.  */
  38. void
  39. ttyclose()
  40. {
  41. }
  42.  
  43.  
  44. /*
  45.  * Test to see if a keyboard character is ready.
  46.  * Returns nonzero if so (and clears the ready flag).
  47.  */
  48. ttycheck()
  49. {
  50.     int    result;
  51.  
  52.     result = inputready;
  53.     inputready = 0;
  54.     return result;
  55. }
  56.  
  57.  
  58. /*
  59.  * Print a formatted string to the terminal.
  60.  * The string length is limited to 256 characters.
  61.  */
  62. void
  63. ttyprintf(fmt)
  64.     char    *fmt;
  65. {
  66.     va_list ap;
  67.     static char    buf[256];
  68.  
  69.     va_start(ap, fmt);
  70.     vsprintf(buf, fmt, ap);
  71.     va_end(ap);
  72.     ttywrite(buf, strlen(buf));
  73. }
  74.  
  75.  
  76. /*
  77.  * Print a status message, like printf.
  78.  * The string length is limited to 256 characters.
  79.  */
  80. void
  81. ttystatus(fmt)
  82.     char    *fmt;
  83. {
  84.     va_list ap;
  85.     static char    buf[256];
  86.  
  87.     va_start(ap, fmt);
  88.     vsprintf(buf, fmt, ap);
  89.     va_end(ap);
  90.     ttywrite(buf, strlen(buf));
  91. }
  92.  
  93.  
  94. /*
  95.  * Write the specified number of characters to the terminal.
  96.  */
  97. void
  98. ttywrite(buf, count)
  99.     unsigned char    *buf;        /* buffer to write */
  100.     int    count;            /* number of characters */
  101. {
  102.     unsigned char ch;
  103.  
  104.     while (count-- > 0) {
  105.         ch = *buf++;
  106.         putchar(ch);
  107.     }
  108. }
  109.  
  110.  
  111. void
  112. ttyhome()
  113. {
  114. }
  115.  
  116.  
  117. void
  118. ttyeeop()
  119. {
  120. }
  121.  
  122.  
  123. void
  124. ttyflush()
  125. {
  126.     fflush(stdout);
  127. }
  128.  
  129.  
  130. /*
  131.  * Return a NULL terminated input line (without the final newline).
  132.  * The specified string is printed as a prompt.
  133.  * Returns nonzero (and an empty buffer) on EOF or error.
  134.  */
  135. ttyread(prompt, buf, buflen)
  136.     char    *prompt;
  137.     char    *buf;
  138. {
  139.     int    len;
  140.  
  141.     fputs(prompt, stdout);
  142.     fflush(stdout);
  143.  
  144.     if (fgets(buf, buflen, stdin) == NULL) {
  145.         buf[0] = '\0';
  146.         return -1;
  147.     }
  148.     len = strlen(buf) - 1;
  149.     if ((len >= 0) && (buf[len] == '\n'))
  150.         buf[len] = '\0';
  151.     return 0;
  152. }
  153.  
  154. /* END CODE */
  155.