home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
LESS.ZIP
/
LESS.C
< prev
next >
Wrap
Text File
|
1991-03-24
|
4KB
|
122 lines
/*
* Name: less.c
* Synopsis: less < file_name ... >
* Description: This program is modeled after the "more" program.
* Input is from STDIN or from file_name.
*
* q - flushes the input stream
* space - scrolls next screen
* enter - scrolls one line
* For all you MeSsy DOS folks, the ESC key works like q
*/
#define INCL_SUB
#include <os2.h>
#include <stdio.h>
#include <string.h>
#define HANDLE 0
#define WAIT 0
#define ESC 27
int i, cols, lines, rows, c;
unsigned top, left, bottom, right, num_lines, num_chars;
unsigned cur_row, cur_col;
char buffer[1024];
char less[256];
char b_line[81];
FILE *fdi;
FILE *fdo;
struct _VIOMODEINFO modedata;
struct _KBDKEYINFO keydata;
unsigned rc, prt_sw;
unsigned blank = 0x0720;
main(int argc, char *argv[], char *envp[])
{
for (i=0;i<80;i++) *(b_line+i) = ' ';
*(b_line+80) = '\0';
lines = 2;
prt_sw = 0;
/* Get the window size */
modedata.cb = sizeof(modedata);
VioGetMode( &modedata, HANDLE);
rows = modedata.row;
cols = modedata.col;
/* Set the scrolling information */
top = 0;
left = 0;
bottom = rows - 2;
right = cols;
num_lines = 1;
strcpy(less,"-- --");
fdo = stdout;
for (i=1;i<argc;i++) {
if ( fdi = fopen ( argv[i],"r" ) ) {
sprintf(less,"-- %s --",argv[i]);
printf("%s\n",less);
ScrollOut(argv[i]);
}
else {
printf("Open error on %s.\n",argv[i]);
exit(1);
}
}
if (argc == 1) {
fdi = stdin;
ScrollOut("");
}
exit(0);
}
ScrollOut(char *file_name)
{
while (fgets(buffer, sizeof(buffer), fdi) ) {
if ( prt_sw ) continue;
/* fputs( buffer, fdo ); */
VioGetCurPos((PUSHORT)&cur_row, (PUSHORT)&cur_col, HANDLE);
if (cur_row > bottom) {
cur_row--;
if ( rc = VioScrollUp(top, left, bottom, right, num_lines, (char far *)&blank, HANDLE) ) {
printf("VioScrollUp error = %u\n",rc);
exit(1);
}
}
num_chars = strlen(buffer);
if (*(buffer+num_chars-1) == '\n') num_chars--;
if (num_chars > cols) num_chars = cols;
if ( rc = VioWrtCharStr( buffer, num_chars, cur_row, left, HANDLE) ) {
printf("VioWrtCharStr error = %u\n", rc);
exit(1);
}
cur_row++;
VioSetCurPos(cur_row, 0, HANDLE);
if (lines < rows) lines++;
else {
/* fputs("-- Less --", fdo); */
if ( rc = VioWrtCharStr( less, strlen(less), bottom + 1, left, HANDLE) ) {
printf("VioWrtCharStr error = %u\n", rc);
exit(1);
}
if (rc=KbdCharIn( &keydata, WAIT, HANDLE) ) {
printf("KbdCharIn error = %u\n", rc);
exit(1);
}
c = keydata.chChar;
switch (c) {
case 'q': prt_sw = 1; break;
case ESC: prt_sw = 1; break;
case ' ': lines = 2;
if ( rc = VioWrtCharStr( b_line, 80, bottom + 1, left, HANDLE) ) {
printf("VioWrtCharStr error = %u\n", rc);
exit(1);
}
break;
case '\n': break;
}
/* fputs("\n", fdo); */
}
}
if (fdi != stdin) fclose(fdi);
lines++;
}