home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / less2 / part2 / output.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.8 KB  |  202 lines

  1. /*
  2.  * High level routines dealing with the output to the screen.
  3.  */
  4.  
  5. #include "less.h"
  6.  
  7. extern int sigs;
  8. extern int sc_width, sc_height;
  9. extern int bo_width, be_width;
  10. extern int ul_width, ue_width;
  11. extern int so_width, se_width;
  12. extern int tabstop;
  13. extern int twiddle;
  14. extern char *line;
  15. extern char *first_cmd;
  16.  
  17. /*
  18.  * Display the line which is in the line buffer.
  19.  */
  20.     public void
  21. put_line()
  22. {
  23.     register char *p;
  24.     register int c;
  25.     register int column;
  26.     extern int auto_wrap, ignaw;
  27.  
  28.     if (sigs)
  29.         /*
  30.          * Don't output if a signal is pending.
  31.          */
  32.         return;
  33.  
  34.     if (line == NULL)
  35.         line = (twiddle) ? "~" : "";
  36.  
  37.     column = 0;
  38.     for (p = line;  *p != '\0';  p++)
  39.     {
  40.         switch (c = *p)
  41.         {
  42.         case UL_CHAR:
  43.             ul_enter();
  44.             column += ul_width;
  45.             break;
  46.         case UE_CHAR:
  47.             ul_exit();
  48.             column += ue_width;
  49.             break;
  50.         case BO_CHAR:
  51.             bo_enter();
  52.             column += bo_width;
  53.             break;
  54.         case BE_CHAR:
  55.             bo_exit();
  56.             column += be_width;
  57.             break;
  58.         case '\t':
  59.             do
  60.             {
  61.                 putc(' ');
  62.                 column++;
  63.             } while ((column % tabstop) != 0);
  64.             break;
  65.         case '\b':
  66.             putbs();
  67.             column--;
  68.             break;
  69.         default:
  70.             if (c & 0200)
  71.             {
  72.                 putc('^');
  73.                 putc(c & 0177);
  74.                 column += 2;
  75.             } else
  76.             {
  77.                 putc(c);
  78.                 column++;
  79.             }
  80.         }
  81.     }
  82.     if (column < sc_width || !auto_wrap || ignaw)
  83.         putc('\n');
  84. }
  85.  
  86. /*
  87.  * Is a given character a "control" character?
  88.  * {{ ASCII DEPENDENT }}
  89.  */
  90.     public int
  91. control_char(c)
  92.     int c;
  93. {
  94.     return (c < ' ' || c == '\177');
  95. }
  96.  
  97. /*
  98.  * Return the printable character used to identify a control character
  99.  * (printed after a carat; e.g. '\3' => "^C").
  100.  * {{ ASCII DEPENDENT }}
  101.  */
  102.     public int
  103. carat_char(c)
  104.     int c;
  105. {
  106.     return ((c == '\177') ? '?' : (c | 0100));
  107. }
  108.  
  109.  
  110. static char obuf[1024];
  111. static char *ob = obuf;
  112.  
  113. /*
  114.  * Flush buffered output.
  115.  */
  116.     public void
  117. flush()
  118. {
  119.     write(1, obuf, ob-obuf);
  120.     ob = obuf;
  121. }
  122.  
  123. /*
  124.  * Discard buffered output.
  125.  */
  126.     public void
  127. dropout()
  128. {
  129.     ob = obuf;
  130. }
  131.  
  132. /*
  133.  * Output a character.
  134.  */
  135.     public void
  136. putc(c)
  137.     int c;
  138. {
  139.     if (ob >= &obuf[sizeof(obuf)])
  140.         flush();
  141.     *ob++ = c;
  142. }
  143.  
  144. /*
  145.  * Output a string.
  146.  */
  147.     public void
  148. puts(s)
  149.     register char *s;
  150. {
  151.     while (*s != '\0')
  152.         putc(*s++);
  153. }
  154.  
  155. /*
  156.  * Output a message in the lower left corner of the screen
  157.  * and wait for carriage return.
  158.  */
  159.  
  160. static char return_to_continue[] = "  (press RETURN)";
  161.  
  162.     public void
  163. error(s)
  164.     char *s;
  165. {
  166.     register int c;
  167.     static char buf[2];
  168.  
  169.     lower_left();
  170.     clear_eol();
  171.     so_enter();
  172.     puts(s);
  173.     puts(return_to_continue);
  174.     so_exit();
  175.  
  176. #if ONLY_RETURN
  177.     while ((c = getc()) != '\n' && c != '\r')
  178.         bell();
  179. #else
  180.     c = getc();
  181.     if (c != '\n' && c != '\r' && c != ' ')
  182.     {
  183.         buf[0] = c;
  184.         first_cmd = buf;
  185.     }
  186. #endif
  187.  
  188.     if (strlen(s) > sc_width)
  189.         repaint();
  190. }
  191.  
  192.     public int
  193. error_width()
  194. {
  195.     /*
  196.      * Don't use the last position, because some terminals
  197.      * will scroll if you write in the last char of the last line.
  198.      */
  199.     return (sc_width - 
  200.         (sizeof(return_to_continue) + so_width + se_width + 1));
  201. }
  202.