home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / PAGER.C < prev    next >
C/C++ Source or Header  |  1992-08-16  |  3KB  |  178 lines

  1. /*
  2.    pager.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    911218: Added this header
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9. #include <dir.h>
  10. #include <string.h>
  11. #include <io.h>
  12. #include <conio.h>
  13. #include <process.h>
  14. #include "pager.h"
  15. #include "rc.h"
  16. #include "misc.h"
  17.  
  18. #ifdef BMH
  19. #define main pager_main
  20. #endif
  21.  
  22. static int
  23.      x = -1,
  24.      max_x = 23, max_y = 79,
  25.      tty = 0,
  26.      fin = 0;
  27.  
  28. int
  29. page_setup(void)
  30. {
  31.    x = 0;
  32.    max_x = atoi(getrc(maxrow)) - 1;
  33.    max_y = atoi(getrc(maxcol)) - 1;
  34.  
  35.    tty = isatty(fileno(stdout));
  36.    fin = 0;
  37.  
  38.    return 0;
  39. }
  40.  
  41. static int
  42. ask(void)
  43. {
  44.    printf("More?");
  45.    fflush(stdout);
  46.  
  47.    while (1)
  48.       switch ( tolower(getch()) ) {
  49.          case 'h':
  50.             printf("\r\n");
  51.             printf(" <space>  - move forward one page\n");
  52.             printf(" <return> - move down one line of text\n");
  53.             printf(" q        - finish viewing\n\n");
  54.             fflush(stdout);
  55.             break;
  56.          case 'q':
  57.          case 'n':
  58.             fin = -1;
  59.             return -1;
  60.          case 'y':
  61.          case ' ':
  62.             printf("\r     \r"); /* clear 'More?' prompt */
  63.             fflush(stdout);
  64.             x = 0;
  65.             return 0;
  66.          case '\r':
  67.             printf("\r     \r");
  68.             fflush(stdout);
  69.             x--;
  70.             return 0;
  71.          default:
  72.             putchar(7);
  73.             fflush(stdout);
  74.             break;
  75.          }
  76. }
  77.  
  78. /*
  79.    Write a string to the console with paged output. The string should not be
  80.    terminated with a newline character (\n). Embedded \n's should be ok.
  81. */
  82. int
  83. page_puts(char *s)
  84. {
  85.    int y = 0;
  86.  
  87.    if (fin)
  88.       return -1;
  89.  
  90.    if (!tty) {
  91.       puts(s);
  92.       return 0;
  93.       }
  94.  
  95.    if ( (x >= max_x) && (ask() == -1))
  96.       return -1;
  97.  
  98.    while (*s) {
  99.       putchar(*s);
  100.       y++;
  101.       if ( (y>max_y) || (*s == '\n') ) {
  102.          y = 0;
  103.          x++;
  104.          if ( (x >= max_x) && (ask() == -1))
  105.             return -1;
  106.          }
  107.       s++;
  108.       }
  109.    x++;
  110.    putchar('\n');
  111.    return 0;
  112. }
  113.  
  114. static int
  115. view_file(char *filename)
  116. {
  117.    char *lister = getrc(viewer);
  118.  
  119.    if ( lister == NULL)
  120.       return -1;
  121.  
  122.    if (spawnlp(P_WAIT, lister, lister, filename, NULL) != 0) {
  123.       fprintf(stderr, "bmh: error calling file viewer '%s %s'\n", lister, filename);
  124.       perror("bmh");
  125.       return -2;
  126.       }
  127.  
  128.    return 0;
  129. }
  130.  
  131. int
  132. page_file(char *filename)
  133. {
  134.    FILE *fp;
  135.    char s[1024];
  136.  
  137.    if (view_file(filename) != -1)
  138.       return 0;
  139.  
  140.    if ( (fp=fopen(filename, "r")) == NULL) {
  141.       fprintf(stderr, "pager: can't open %s\n", filename);
  142.       return -1;
  143.       }
  144.    page_setup();
  145.  
  146.    while (fgets(s, sizeof(s), fp) != NULL) {
  147.       char *p = s;
  148.  
  149.       for (; *p; p++)
  150.          if (*p == '\n') {
  151.             *p = '\0';
  152.             break;
  153.             }
  154.  
  155.       if ( page_puts(s) != 0 )
  156.          break;
  157.       }
  158.    fclose(fp);
  159.  
  160.    return 0;
  161. }
  162.  
  163. #ifdef PAGER_PROG
  164. int
  165. main(int argc, char *argv[])
  166. {
  167.    if (argc != 2) {
  168.       fprintf(stderr, "Usage: pager <file>\n");
  169.       return -1;
  170.       }
  171.  
  172.    if (setupbm()==-1)
  173.       return -1;
  174.  
  175.    return page_file(argv[1]);
  176. }
  177. #endif
  178.