home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / less5 / part01 / output.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-22  |  4.1 KB  |  257 lines

  1. /*
  2.  * High level routines dealing with the output to the screen.
  3.  */
  4.  
  5. #include "less.h"
  6.  
  7. public int errmsgs;    /* Count of messages displayed by error() */
  8.  
  9. extern int sigs;
  10. extern int sc_width, sc_height;
  11. extern int ul_width, ue_width;
  12. extern int so_width, se_width;
  13. extern int bo_width, be_width;
  14. extern int tabstop;
  15. extern int twiddle;
  16. extern int screen_trashed;
  17. extern int any_display;
  18. extern char *line;
  19. extern char *first_cmd;
  20.  
  21. /*
  22.  * Display the line which is in the line buffer.
  23.  */
  24.     public void
  25. put_line()
  26. {
  27.     register char *p;
  28.     register int c;
  29.     register int column;
  30.     extern int auto_wrap, ignaw;
  31.  
  32.     if (sigs)
  33.     {
  34.         /*
  35.          * Don't output if a signal is pending.
  36.          */
  37.         screen_trashed = 1;
  38.         return;
  39.     }
  40.  
  41.     if (line == NULL)
  42.         line = (twiddle) ? "~" : "";
  43.  
  44.     column = 0;
  45.     for (p = line;  *p != '\0';  p++)
  46.     {
  47.         switch (c = *p)
  48.         {
  49.         case UL_CHAR:
  50.             ul_enter();
  51.             column += ul_width;
  52.             break;
  53.         case UE_CHAR:
  54.             ul_exit();
  55.             column += ue_width;
  56.             break;
  57.         case BO_CHAR:
  58.             bo_enter();
  59.             column += bo_width;
  60.             break;
  61.         case BE_CHAR:
  62.             bo_exit();
  63.             column += be_width;
  64.             break;
  65.         case '\t':
  66.             do
  67.             {
  68.                 putchr(' ');
  69.                 column++;
  70.             } while ((column % tabstop) != 0);
  71.             break;
  72.         case '\b':
  73.             putbs();
  74.             column--;
  75.             break;
  76.         default:
  77.             if (c & 0200)
  78.             {
  79.                 /*
  80.                  * Control characters arrive here as the
  81.                  * normal character [carat_char(c)] with
  82.                  * the 0200 bit set.  See pappend().
  83.                  */
  84.                 putchr('^');
  85.                 putchr(c & 0177);
  86.                 column += 2;
  87.             } else
  88.             {
  89.                 putchr(c);
  90.                 column++;
  91.             }
  92.         }
  93.     }
  94.     if (column < sc_width || !auto_wrap || ignaw)
  95.         putchr('\n');
  96. }
  97.  
  98. /*
  99.  * Is a given character a "control" character?
  100.  * {{ ASCII DEPENDENT }}
  101.  */
  102.     public int
  103. control_char(c)
  104.     int c;
  105. {
  106.     return (c < ' ' || c == '\177');
  107. }
  108.  
  109. /*
  110.  * Return the printable character used to identify a control character
  111.  * (printed after a carat; e.g. '\3' => "^C").
  112.  * {{ ASCII DEPENDENT }}
  113.  */
  114.     public int
  115. carat_char(c)
  116.     int c;
  117. {
  118.     return ((c == '\177') ? '?' : (c | 0100));
  119. }
  120.  
  121.  
  122. static char obuf[1024];
  123. static char *ob = obuf;
  124.  
  125. /*
  126.  * Flush buffered output.
  127.  */
  128.     public void
  129. flush()
  130. {
  131.     register int n;
  132.  
  133.     n = ob - obuf;
  134.     if (n == 0)
  135.         return;
  136.     if (write(1, obuf, n) != n)
  137.         screen_trashed = 1;
  138.     ob = obuf;
  139. }
  140.  
  141. /*
  142.  * Discard buffered output.
  143.  */
  144.     public void
  145. dropout()
  146. {
  147.     ob = obuf;
  148. }
  149.  
  150. /*
  151.  * Output a character.
  152.  */
  153.     public void
  154. putchr(c)
  155.     int c;
  156. {
  157.     if (ob >= &obuf[sizeof(obuf)])
  158.         flush();
  159.     *ob++ = c;
  160. }
  161.  
  162. /*
  163.  * Output a string.
  164.  */
  165.     public void
  166. putstr(s)
  167.     register char *s;
  168. {
  169.     while (*s != '\0')
  170.         putchr(*s++);
  171. }
  172.  
  173. /*
  174.  * Output a message in the lower left corner of the screen
  175.  * and wait for carriage return.
  176.  */
  177.  
  178. static char return_to_continue[] = "  (press RETURN)";
  179.  
  180.     public void
  181. error(s)
  182.     char *s;
  183. {
  184.     register int c;
  185.     static char buf[2];
  186.  
  187.     errmsgs++;
  188.     if (!any_display)
  189.     {
  190.         /*
  191.          * Nothing has been displayed yet.
  192.          * Output this message on error output (file
  193.          * descriptor 2) and don't wait for a keystroke
  194.          * to continue.
  195.          *
  196.          * This has the desirable effect of producing all
  197.          * error messages on error output if standard output
  198.          * is directed to a file.  It also does the same if
  199.          * we never produce any real output; for example, if
  200.          * the input file(s) cannot be opened.  If we do
  201.          * eventually produce output, code in edit() makes
  202.          * sure these messages can be seen before they are
  203.          * overwritten or scrolled away.
  204.          */
  205.         write(2, s, strlen(s));
  206.         write(2, "\n", 1);
  207.         return;
  208.     }
  209.  
  210.     lower_left();
  211.     clear_eol();
  212.     so_enter();
  213.     putstr(s);
  214.     putstr(return_to_continue);
  215.     so_exit();
  216.  
  217. #if ONLY_RETURN
  218.     while ((c = getchr()) != '\n' && c != '\r')
  219.         bell();
  220. #else
  221.     c = getchr();
  222.     if (c != '\n' && c != '\r' && c != ' ' && c != READ_INTR)
  223.     {
  224.         buf[0] = c;
  225.         first_cmd = buf;
  226.     }
  227. #endif
  228.     lower_left();
  229.  
  230.     if (strlen(s) + sizeof(return_to_continue) + 
  231.         so_width + se_width + 1 > sc_width)
  232.         /*
  233.          * Printing the message has probably scrolled the screen.
  234.          * {{ Unless the terminal doesn't have auto margins,
  235.          *    in which case we just hammered on the right margin. }}
  236.          */
  237.         repaint();
  238.  
  239.     flush();
  240. }
  241.  
  242. static char intr_to_abort[] = "... (interrupt to abort)";
  243.  
  244.     public void
  245. ierror(s)
  246.     char *s;
  247. {
  248.  
  249.     lower_left();
  250.     clear_eol();
  251.     so_enter();
  252.     putstr(s);
  253.     putstr(intr_to_abort);
  254.     so_exit();
  255.     flush();
  256. }
  257.