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 >
Wrap
C/C++ Source or Header
|
1992-08-16
|
3KB
|
178 lines
/*
pager.c : Copyright Paul Healy, EI9GL, 1992.
911218: Added this header
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <dir.h>
#include <string.h>
#include <io.h>
#include <conio.h>
#include <process.h>
#include "pager.h"
#include "rc.h"
#include "misc.h"
#ifdef BMH
#define main pager_main
#endif
static int
x = -1,
max_x = 23, max_y = 79,
tty = 0,
fin = 0;
int
page_setup(void)
{
x = 0;
max_x = atoi(getrc(maxrow)) - 1;
max_y = atoi(getrc(maxcol)) - 1;
tty = isatty(fileno(stdout));
fin = 0;
return 0;
}
static int
ask(void)
{
printf("More?");
fflush(stdout);
while (1)
switch ( tolower(getch()) ) {
case 'h':
printf("\r\n");
printf(" <space> - move forward one page\n");
printf(" <return> - move down one line of text\n");
printf(" q - finish viewing\n\n");
fflush(stdout);
break;
case 'q':
case 'n':
fin = -1;
return -1;
case 'y':
case ' ':
printf("\r \r"); /* clear 'More?' prompt */
fflush(stdout);
x = 0;
return 0;
case '\r':
printf("\r \r");
fflush(stdout);
x--;
return 0;
default:
putchar(7);
fflush(stdout);
break;
}
}
/*
Write a string to the console with paged output. The string should not be
terminated with a newline character (\n). Embedded \n's should be ok.
*/
int
page_puts(char *s)
{
int y = 0;
if (fin)
return -1;
if (!tty) {
puts(s);
return 0;
}
if ( (x >= max_x) && (ask() == -1))
return -1;
while (*s) {
putchar(*s);
y++;
if ( (y>max_y) || (*s == '\n') ) {
y = 0;
x++;
if ( (x >= max_x) && (ask() == -1))
return -1;
}
s++;
}
x++;
putchar('\n');
return 0;
}
static int
view_file(char *filename)
{
char *lister = getrc(viewer);
if ( lister == NULL)
return -1;
if (spawnlp(P_WAIT, lister, lister, filename, NULL) != 0) {
fprintf(stderr, "bmh: error calling file viewer '%s %s'\n", lister, filename);
perror("bmh");
return -2;
}
return 0;
}
int
page_file(char *filename)
{
FILE *fp;
char s[1024];
if (view_file(filename) != -1)
return 0;
if ( (fp=fopen(filename, "r")) == NULL) {
fprintf(stderr, "pager: can't open %s\n", filename);
return -1;
}
page_setup();
while (fgets(s, sizeof(s), fp) != NULL) {
char *p = s;
for (; *p; p++)
if (*p == '\n') {
*p = '\0';
break;
}
if ( page_puts(s) != 0 )
break;
}
fclose(fp);
return 0;
}
#ifdef PAGER_PROG
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: pager <file>\n");
return -1;
}
if (setupbm()==-1)
return -1;
return page_file(argv[1]);
}
#endif