home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MOREPROC.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  103 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  MOREPROC.C - Display lines to the screen using more-style processing
  5. **
  6. **  public domain demo by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include "more.h"
  11.  
  12. static int rows, cols;
  13.  
  14. /*
  15. **  more_proc() - Display a line using more-style processing
  16. **
  17. **  Parameters: 1 - Line to display
  18. **
  19. **  Returns: Key_ESC ('\x1b') if ESC pressed, else 0
  20. **
  21. **  Notes: Reads screen size from BIOS
  22. **         Handles long line wrapping
  23. **         Space to single step
  24. **         ESC to exit via calling function
  25. **         Any other key to continue until screen full
  26. */
  27.  
  28. int more_proc(char *str)
  29. {
  30.       static int linecnt = 0;
  31.       char linebuf[256];
  32.  
  33.       if (!rows || !cols)
  34.       {
  35.             rows = SCREENROWS;
  36.             cols = SCREENCOLS;
  37.       }
  38.  
  39.       if (strlen(str) == ((size_t)cols + 1) && LAST_CHAR(str) == '\n')
  40.                   LAST_CHAR(str) = NUL;
  41.  
  42.       while (strlen(str) > (size_t)cols)
  43.       {
  44.             strn1cpy(linebuf, str, cols);
  45.             linebuf[cols] = NUL;
  46.             more_proc(linebuf);
  47.             strMove(str, str + cols);
  48.       }
  49.  
  50.       if ((rows - (++linecnt)) < 2)
  51.       {
  52.             int ch;
  53.  
  54.             fputs("[...more...]", stderr);
  55.             fflush(stderr);
  56.  
  57.             switch (ch = ext_getch())
  58.             {
  59.             case ' ':
  60.                   --linecnt;
  61.                   break;
  62.  
  63.             case Key_ESC:
  64.                   fputs("\r[...Aborted...]\n", stderr);
  65.                   return ch;
  66.  
  67.             default:
  68.                   linecnt = 0;
  69.             }
  70.             fputs("\r            \r", stderr);
  71.             fflush(stderr);
  72.       }
  73.  
  74.       fputs(str, stderr);
  75.       return 0;
  76. }
  77.  
  78. #ifdef TEST
  79.  
  80. #include "errors.h"                             /* For cant()           */
  81.  
  82. main(int argc, char *argv[])
  83. {
  84.       FILE *fp;
  85.       char buf[512];
  86.  
  87.       while (--argc)
  88.       {
  89.             fp = cant(*++argv, "r");
  90.  
  91.             while (!feof(fp))
  92.             {
  93.                   if (NULL == fgets(buf,  512, fp))
  94.                         break;
  95.                   if (Key_ESC == more_proc(buf))
  96.                         break;
  97.             }
  98.       }
  99.       return 0;
  100. }
  101.  
  102. #endif /* TEST */
  103.