home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / LES177AS.ZIP / OUTPUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-04  |  5.8 KB  |  357 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. public int need_clr;
  9.  
  10. extern int sigs;
  11. extern int sc_width;
  12. extern int so_s_width, so_e_width;
  13. extern int screen_trashed;
  14. extern int any_display;
  15. #if __MSDOS__
  16. #ifndef TURBOC
  17. extern int output_mode;
  18. #endif
  19. #endif
  20.  
  21. /*
  22.  * Display the line which is in the line buffer.
  23.  */
  24.     public void
  25. put_line()
  26. {
  27.     register int c;
  28.     register int i;
  29.     int a;
  30.     int curr_attr;
  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.     curr_attr = NORMAL;
  42.  
  43.     for (i = 0;  (c = gline(i, &a)) != '\0';  i++)
  44.     {
  45.         if (a != curr_attr)
  46.         {
  47.             /*
  48.              * Changing attributes.
  49.              * Display the exit sequence for the old attribute
  50.              * and the enter sequence for the new one.
  51.              */
  52.             switch (curr_attr)
  53.             {
  54.             case UNDERLINE:    ul_exit();    break;
  55.             case BOLD:    bo_exit();    break;
  56.             case BLINKING:    bl_exit();    break;
  57. #ifdef TURBOC
  58.             case BOLD2:    bo2_exit();    break;
  59. #endif
  60.             }
  61.             switch (a)
  62.             {
  63.             case UNDERLINE:    ul_enter();    break;
  64.             case BOLD:    bo_enter();    break;
  65.             case BLINKING:    bl_enter();    break;
  66. #ifdef TURBOC
  67.             case BOLD2:    bo2_enter();    break;
  68. #endif
  69.             }
  70.             curr_attr = a;
  71.         }
  72.         if (curr_attr == INVIS)
  73.             continue;
  74.         if (c == '\b')
  75.             putbs();
  76.         else
  77.             putchr(c);
  78.     }
  79. }
  80.  
  81. static char obuf[1024];
  82. static char *ob = obuf;
  83.  
  84. /*
  85.  * Flush buffered output.
  86.  *
  87.  * If we haven't displayed any file data yet,
  88.  * output messages on error output (file descriptor 2),
  89.  * otherwise output on standard output (file descriptor 1).
  90.  *
  91.  * This has the desirable effect of producing all
  92.  * error messages on error output if standard output
  93.  * is directed to a file.  It also does the same if
  94.  * we never produce any real output; for example, if
  95.  * the input file(s) cannot be opened.  If we do
  96.  * eventually produce output, code in edit() makes
  97.  * sure these messages can be seen before they are
  98.  * overwritten or scrolled away.
  99.  */
  100.  
  101.     public void
  102. flush()
  103. {
  104. #ifdef TURBOC
  105.  
  106.     *ob = '\0';
  107.     cputs(obuf);
  108.     ob = obuf;
  109.  
  110. #else
  111.  
  112.     register int n;
  113.     register int fd;
  114.  
  115. #if __MSDOS__
  116.     if (output_mode == 0)
  117.     {
  118.         *ob = '\0';
  119.         cputs(obuf);
  120.         ob = obuf;
  121.         return;
  122.     }
  123. #endif
  124.     n = ob - obuf;
  125.     if (n == 0)
  126.         return;
  127.     fd = (any_display) ? 1 : 2;
  128.     if (write(fd, obuf, n) != n)
  129.         screen_trashed = 1;
  130.     ob = obuf;
  131.  
  132. #endif    /* TURBOC */
  133. }
  134.  
  135. /*
  136.  * Output a character.
  137.  */
  138.     public void
  139. putchr(c)
  140.     int c;
  141. {
  142.     if (ob >= &obuf[sizeof(obuf)])
  143.         flush();
  144.     if (need_clr)
  145.     {
  146.         need_clr = 0;
  147.         lower_left();
  148.         clear_eol();
  149.     }
  150. #if __MSDOS__
  151.     if (c == '\n')
  152.         *ob++ = '\r';
  153. #endif
  154.     *ob++ = c;
  155. }
  156.  
  157. /*
  158.  * Output a string.
  159.  */
  160.     public void
  161. putstr(s)
  162.     register char *s;
  163. {
  164.     while (*s != '\0')
  165.         putchr(*s++);
  166. }
  167.  
  168.  
  169. /*
  170.  * Output an integer in a given radix.
  171.  */
  172.     static int
  173. iprintnum(num, radix)
  174.     int num;
  175.     int radix;
  176. {
  177.     register char *s;
  178.     int r;
  179.     int neg;
  180.     char buf[10];
  181.  
  182.     if (neg = (num < 0))
  183.         num = -num;
  184.  
  185.     s = buf;
  186.     do
  187.     {
  188.         *s++ = (num % radix) + '0';
  189.     } while ((num /= radix) != 0);
  190.  
  191.     if (neg)
  192.         *s++ = '-';
  193.     r = s - buf;
  194.  
  195.     while (s > buf)
  196.         putchr(*--s);
  197.     return (r);
  198. }
  199.  
  200. /*
  201.  * This function implements printf-like functionality
  202.  * using a more portable argument list mechanism than printf's.
  203.  */
  204.     static int
  205. iprintf(fmt, parg)
  206.     register char *fmt;
  207.     PARG *parg;
  208. {
  209.     register char *s;
  210.     register int n;
  211.     register int col;
  212.  
  213.     col = 0;
  214.     while (*fmt != '\0')
  215.     {
  216.         if (*fmt != '%')
  217.         {
  218.             putchr(*fmt++);
  219.             col++;
  220.         } else
  221.         {
  222.             ++fmt;
  223.             switch (*fmt++) {
  224.             case 's':
  225.                 s = parg->p_string;
  226.                 parg++;
  227.                 while (*s != '\0')
  228.                 {
  229.                     putchr(*s++);
  230.                     col++;
  231.                 }
  232.                 break;
  233.             case 'd':
  234.                 n = parg->p_int;
  235.                 parg++;
  236.                 col += iprintnum(n, 10);
  237.                 break;
  238.             }
  239.         }
  240.     }
  241.     return (col);
  242. }
  243.  
  244. /*
  245.  * Output a message in the lower left corner of the screen
  246.  * and wait for carriage return.
  247.  */
  248.     public void
  249. error(fmt, parg)
  250.     char *fmt;
  251.     PARG *parg;
  252. {
  253.     int c;
  254.     int col = 0;
  255.     static char return_to_continue[] = "  (press RETURN)";
  256.  
  257.     errmsgs++;
  258.  
  259.     if (any_display)
  260.     {
  261.         lower_left();
  262.         clear_eol();
  263.         so_enter();
  264.         col += so_s_width;
  265.     }
  266.  
  267.     col += iprintf(fmt, parg);
  268.  
  269.     if (!any_display)
  270.     {
  271.         putchr('\n');
  272.         return;
  273.     }
  274.  
  275.     putstr(return_to_continue);
  276.     so_exit();
  277.     col += sizeof(return_to_continue) + so_e_width;
  278.  
  279. #if ONLY_RETURN
  280.     while ((c = getchr()) != '\n' && c != '\r')
  281.         bell();
  282. #else
  283.     c = getchr();
  284.     if (c != '\n' && c != '\r' && c != ' ' && c != READ_INTR)
  285.         ungetcc(c);
  286. #endif
  287.     lower_left();
  288.  
  289.     if (col >= sc_width)
  290.         /*
  291.          * Printing the message has probably scrolled the screen.
  292.          * {{ Unless the terminal doesn't have auto margins,
  293.          *    in which case we just hammered on the right margin. }}
  294.          */
  295.         screen_trashed = 1;
  296.  
  297.     flush();
  298. }
  299.  
  300. static char intr_to_abort[] = "... (interrupt to abort)";
  301.  
  302. /*
  303.  * Output a message in the lower left corner of the screen
  304.  * and don't wait for carriage return.
  305.  * Usually used to warn that we are beginning a potentially
  306.  * time-consuming operation.
  307.  */
  308.     public void
  309. ierror(fmt, parg)
  310.     char *fmt;
  311.     PARG *parg;
  312. {
  313.     lower_left();
  314.     clear_eol();
  315.     so_enter();
  316.     (void) iprintf(fmt, parg);
  317.     putstr(intr_to_abort);
  318.     so_exit();
  319.     flush();
  320.     need_clr = 1;
  321. }
  322.  
  323. /*
  324.  * Output a message in the lower left corner of the screen
  325.  * and return a single-character response.
  326.  */
  327.     public int
  328. query(fmt, parg)
  329.     char *fmt;
  330.     PARG *parg;
  331. {
  332.     register int c;
  333.     int col = 0;
  334.  
  335.     if (any_display)
  336.     {
  337.         lower_left();
  338.         clear_eol();
  339.     }
  340.  
  341.     (void) iprintf(fmt, parg);
  342.     c = getchr();
  343.  
  344.     if (!any_display)
  345.     {
  346.         putchr('\n');
  347.         return (c);
  348.     }
  349.  
  350.     lower_left();
  351.     if (col >= sc_width)
  352.         screen_trashed = 1;
  353.     flush();
  354.  
  355.     return (c);
  356. }
  357.