home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / LESS.ZIP / LESS.C < prev    next >
Text File  |  1991-03-24  |  4KB  |  122 lines

  1. /*
  2.  * Name:        less.c
  3.  * Synopsis:    less < file_name ... >
  4.  * Description: This program is modeled after the "more" program.
  5.  *              Input is from STDIN or from file_name.
  6.  *                   
  7.  *                  q - flushes the input stream
  8.  *              space - scrolls next screen
  9.  *              enter - scrolls one line
  10.  * For all you MeSsy DOS folks, the ESC key works like q
  11.  */
  12.  
  13. #define  INCL_SUB
  14. #include <os2.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #define  HANDLE   0
  18. #define  WAIT     0
  19. #define  ESC      27
  20.  
  21. int i, cols, lines, rows, c;
  22. unsigned  top, left, bottom, right, num_lines, num_chars;
  23. unsigned  cur_row, cur_col;
  24. char buffer[1024];
  25. char less[256];
  26. char b_line[81];
  27. FILE *fdi;
  28. FILE *fdo;
  29. struct _VIOMODEINFO modedata;
  30. struct _KBDKEYINFO  keydata;
  31. unsigned rc, prt_sw;
  32. unsigned blank = 0x0720;
  33.  
  34. main(int argc, char *argv[], char *envp[])
  35. {
  36.     for (i=0;i<80;i++) *(b_line+i) = ' ';
  37.     *(b_line+80) = '\0'; 
  38.     lines = 2;
  39.     prt_sw = 0;
  40.     /* Get the window size                          */
  41.     modedata.cb = sizeof(modedata);
  42.     VioGetMode( &modedata, HANDLE);
  43.     rows = modedata.row;
  44.     cols = modedata.col;
  45.     /* Set the scrolling information                */
  46.     top = 0;
  47.     left = 0;
  48.     bottom = rows - 2;
  49.     right = cols;
  50.     num_lines = 1;
  51.  
  52.     strcpy(less,"-- --");
  53.     fdo = stdout;
  54.     for (i=1;i<argc;i++) {
  55.         if ( fdi = fopen ( argv[i],"r" ) ) {
  56.             sprintf(less,"-- %s --",argv[i]);
  57.             printf("%s\n",less);
  58.             ScrollOut(argv[i]);
  59.         }
  60.         else {
  61.             printf("Open error on %s.\n",argv[i]);
  62.             exit(1);
  63.         }
  64.     }
  65.     if (argc == 1) {
  66.         fdi = stdin;
  67.         ScrollOut("");
  68.     }
  69.     exit(0);
  70. }
  71.  
  72. ScrollOut(char *file_name)
  73. {
  74.     while (fgets(buffer, sizeof(buffer), fdi) ) {
  75.         if ( prt_sw ) continue;
  76.         /* fputs( buffer, fdo ); */
  77.         VioGetCurPos((PUSHORT)&cur_row, (PUSHORT)&cur_col, HANDLE);
  78.         if (cur_row > bottom) {
  79.             cur_row--;
  80.             if ( rc = VioScrollUp(top, left, bottom, right, num_lines, (char far *)&blank, HANDLE) ) {
  81.                 printf("VioScrollUp error = %u\n",rc);
  82.                 exit(1);
  83.             }
  84.         }
  85.         num_chars = strlen(buffer);
  86.         if (*(buffer+num_chars-1) == '\n') num_chars--;
  87.         if (num_chars > cols) num_chars = cols;
  88.         if ( rc = VioWrtCharStr( buffer, num_chars, cur_row, left, HANDLE) ) {
  89.             printf("VioWrtCharStr error = %u\n", rc);
  90.             exit(1);
  91.         }
  92.         cur_row++;
  93.         VioSetCurPos(cur_row, 0, HANDLE);
  94.         if (lines < rows) lines++;
  95.         else {
  96.             /* fputs("-- Less --", fdo); */
  97.             if ( rc = VioWrtCharStr( less, strlen(less), bottom + 1, left, HANDLE) ) {
  98.                 printf("VioWrtCharStr error = %u\n", rc);
  99.                 exit(1);
  100.             }
  101.             if (rc=KbdCharIn( &keydata, WAIT, HANDLE) ) {
  102.                 printf("KbdCharIn error = %u\n", rc);
  103.                 exit(1);
  104.             }
  105.             c = keydata.chChar;
  106.             switch (c) {
  107.                 case 'q': prt_sw = 1; break;
  108.                 case ESC: prt_sw = 1; break;
  109.                 case ' ': lines = 2;
  110.                           if ( rc = VioWrtCharStr( b_line, 80, bottom + 1, left, HANDLE) ) {
  111.                               printf("VioWrtCharStr error = %u\n", rc);
  112.                               exit(1);
  113.                           }
  114.                           break;
  115.                 case '\n': break;
  116.             }
  117.             /* fputs("\n", fdo); */
  118.         }
  119.     }
  120.     if (fdi != stdin) fclose(fdi);
  121.     lines++;
  122. }