These are changes that apparently bring the version number to 2.0.

However, judging from the comments at the top of the source file,
the v2.0 changes were based on more v1.31 rather than 1.4.  When
this program is updated, these diffs should be evaluated.  (Unless,
of course, the current version is tossed in favor of a recent BSD
derivative.)

Here are the comments for the changes from v1.31 to v1.4 that may have
been dropped while moving to v2.0.  See the cvs diffs for details:

> v1.4  - Termcap support has been added and tested.  Look for a soon to be
>         faster version coming soon to a ~/bin directory near you!
>
> v1.32 - uses isatty(x) instead of fstat.  more portable (opinion).
>         soon to be added, TERMCAP support, won't that be nice?
This commit is contained in:
gdr-ftp 1998-04-10 19:10:01 +00:00
parent 861f178146
commit e51b60e6cf
2 changed files with 136 additions and 112 deletions

5
bin/more/Makefile Normal file
View File

@ -0,0 +1,5 @@
more.root: more.cc
purge;compile more.cc keep=more
more: more.root
link more keep=more

View File

@ -3,43 +3,37 @@
Revision history: Revision history:
v1.4 - Termcap support has been added and tested. Look for a soon to be v2.0 - now sets tty to cbreak mode, since TTY's cooked mode has
faster version coming soon to a ~/bin directory near you! changed.
v1.32 - uses isatty(x) instead of fstat. more portable (opinion).
soon to be added, TERMCAP support, won't that be nice?
v1.31 - uses fstat to check whether output is a tty, instead of v1.31 - uses fstat to check whether output is a tty, instead of
_Direction _Direction
v1.3 - now prints name of file in block if multiple files specified v1.3 - now prints name of file in block if multiple files specified
*/ */
#pragma optimize 8 #pragma optimize -1
#pragma stacksize 3072 #pragma stacksize 1024
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <types.h> #include <types.h>
#include <signal.h> #include <sys/signal.h>
#include <gno/gno.h> #include <gno/gno.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <ioctl.h> #include <sys/ioctl.h>
#include <shell.h> #include <shell.h>
#include <gsos.h> #include <gsos.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <fcntl.h>
#include <texttool.h> #include <texttool.h>
#include <orca.h> #include <orca.h>
#include <termcap.h>
#pragma lint -1 #pragma lint -1
#define MAX_LINE 23 #define MAX_LINE 23
#define MAX_COL 80 #define MAX_COL 80
int tprchar(char c);
void PrintFileName(char *s) void PrintFileName(char *s)
{ {
char *r; char *r;
@ -48,20 +42,15 @@ char *r;
if ((r = strrchr(s,'/')) != NULL) if ((r = strrchr(s,'/')) != NULL)
printf("%s\n",r+1); printf("%s\n",r+1);
else else printf("%s\n",s);
printf("%s\n",s);
} }
char getcharacter(int fd) char getcharacter(int fd)
{ {
char c; char c;
IORecGS r = {4, fd, (void *)&c, 1l};
if (fd == -1)
c = ReadChar(0);
else
ReadGS(&r);
if (fd == -1) c = ReadChar(0);
else read(fd,&c,1);
return c; return c;
} }
@ -76,14 +65,70 @@ void inthndl(int sig, int code)
exit(sig); exit(sig);
} }
char inv[20], nor[20]; char i_buf[1024];
char *pterm, *pcap, *ps; word b_ind = 0;
char termcap[1030], capability[100]; word b_size = 0;
int b_ref;
int b_pushback = EOF;
word b_eof;
longword b_mark;
int buf_open(char *name)
{
int fd;
fd = open(name,O_RDONLY);
b_ind = 0;
b_size = 0;
b_mark = 0l;
b_eof = 0;
return (b_ref = fd);
}
void buf_fd_open(int fd)
{
b_ind = 0;
b_size = 0;
b_mark = 0l;
b_ref = fd;
b_eof = 0;
}
int buf_getc(void)
{
char c;
if (b_pushback != EOF) { c = b_pushback; b_pushback = EOF; return c; }
if (b_ind >= b_size) {
if (b_eof) return EOF;
b_size = read(b_ref,i_buf,1024);
if (!b_size) b_eof = 1;
b_ind = 0;
return buf_getc();
}
else {
c = i_buf[b_ind++];
b_mark = b_mark + 1l;
return c;
}
}
#define OBUFSIZE 1024
char o_buf[OBUFSIZE];
word b_oind = 0;
void buf_flush(void)
{ write(STDOUT_FILENO,o_buf,b_oind); b_oind = 0; }
void buf_putc(char c)
{
if (b_oind == OBUFSIZE) buf_flush();
o_buf[b_oind++] = c;
}
int main(int argc,char *argv[]) int main(int argc,char *argv[])
{ {
FILE *file; int file;
int line,col; word line,col;
int i; int i;
int c,quit,abort; int c,quit,abort;
int pipeFlag; /* 1 means input is piped in or redirected */ int pipeFlag; /* 1 means input is piped in or redirected */
@ -94,126 +139,104 @@ char *truncated;
static struct stat sb; static struct stat sb;
extern int _INITGNOSTDIO(void); extern int _INITGNOSTDIO(void);
if (!_INITGNOSTDIO()) /*if (!_INITGNOSTDIO()) {
{
fprintf(stderr,"'more' requires GNO/ME.\n"); fprintf(stderr,"'more' requires GNO/ME.\n");
exit(1); exit(1);
} }*/
if ((pterm = getenv ("TERM"))
&& (tgetent (termcap, pterm) == 1))
{
pcap = capability;
if (ps = tgetstr ("so", &pcap))
{
/*
* sun has padding in here. this is NOT portable.
* better to use tputs() to strip it...
*/
strcpy (inv, ps);
}
else { fprintf(stderr,"couldn't get standout mode\n");
exit(1); }
if (ps = tgetstr ("se", &pcap))
{
strcpy (nor, ps);
}
}
setvbuf(stdout,NULL,_IOLBF,256); setvbuf(stdout,NULL,_IOLBF,256);
tty = fopen(".tty","r"); tty = fopen(".tty","r");
/* turn off echo mode */ /* turn off echo mode */
ioctl(tty->_file, TIOCGETP, &sg); ioctl(tty->_file, TIOCGETP, &sg);
signal(SIGINT, inthndl); signal(SIGINT, inthndl);
/*putchar(6);*/ buf_putc(6);
if (!isatty(2)) fstat(STDOUT_FILENO,&sb);
standardOut = 0; if (sb.st_mode & S_IFCHR) standardOut = 1;
else else standardOut = 0;
standardOut = 1;
pipeFlag = (argc == 1) ? 1 : 0; pipeFlag = (argc == 1) ? 1 : 0;
if (argc == 1) if (argc == 1) argc = 2;
argc = 2;
quit = 0; quit = 0;
abort = 0; abort = 0;
oldsg_flags = sg.sg_flags; oldsg_flags = sg.sg_flags;
sg.sg_flags &= ~ECHO; sg.sg_flags &= ~ECHO;
sg.sg_flags |= CBREAK;
ioctl(tty->_file, TIOCSETP, &sg); ioctl(tty->_file, TIOCSETP, &sg);
for (i = 1; (i < argc && !abort); i++) for (i = 1; (i < argc && !abort); i++)
{ {
quit = 0; quit = 0;
if (pipeFlag) if (pipeFlag) {
{ file = STDIN_FILENO;
file = stdin; buf_fd_open(file);
} }
else else {
{
truncated = strrchr(argv[i],'/'); truncated = strrchr(argv[i],'/');
if (truncated == NULL) truncated = argv[i]; if (truncated == NULL) truncated = argv[i];
else truncated++; else truncated++;
file = fopen(argv[i],"rb"); stat(argv[i],&sb);
if (file == NULL) if (sb.st_mode & S_IFDIR) {
{ fprintf(stderr,"more: %s is a directory\n",argv[i]);
fflush(stderr);
continue;
}
file = buf_open(argv[i]);
if (file < 0) {
perror(argv[i]); perror(argv[i]);
/*putchar(5);*/ exit(1); buf_putc(5); buf_flush(); exit(1);
} }
eofs.pCount = 2; eofs.pCount = 2;
eofs.refNum = file->_file; eofs.refNum = file;
GetEOFGS(&eofs); GetEOFGS(&eofs);
if (toolerror()) if (toolerror()) {
{
printf("GS/OS Error $%X\n",toolerror()); printf("GS/OS Error $%X\n",toolerror());
/*putchar(5);*/ exit(-1); buf_putc(5); buf_flush(); exit(-1);
} }
} }
if (argc > 2) if (argc > 2) {
{
printf("::::::::::::::::\n"); printf("::::::::::::::::\n");
PrintFileName(argv[i]); PrintFileName(argv[i]);
printf("::::::::::::::::\n"); printf("::::::::::::::::\n");
line = 3; col = 1; line = 3; col = 1;
} }
else else {line = 1; col = 1;}
{
line = 1; col = 1;
}
c = getc(file); /* wierdness fix */ c = buf_getc(); /* wierdness fix */
if (c == EOF) quit = 1; if (c == EOF) quit = 1;
c &= 0xFF; /* c &= 0xFF; */
if (c == 0x04) quit = 1; if (c == 0x04) quit = 1;
while (!quit && !abort) while (!quit && !abort)
{ {
int k; int k;
if (c == 0x0c) if (c == 0x0c) { buf_flush(); printf("^L\n");
{ line = MAX_LINE; col = 1; }
printf("^L\n"); else if (c == '\n') {
line = MAX_LINE; col = 1; buf_putc('\r');
} col = 1; line++;
else if (c == '\r') }
{ else if (c == '\r') {
if ((k = getc(file)) == '\n') /* IBM silly CR & LF EOL */ if ((k = buf_getc()) == '\n') /* IBM silly CR & LF EOL */
putchar('\n'); buf_putc('\r');
else else {
{ b_pushback = k;
ungetc(k,file); putchar('\n'); b_mark = b_mark-1;
buf_putc('\r');
} }
col = 1; line++; col = 1; line++;
} }
else if (c == '\n') else if (c == '\n')
{ {
putchar('\n'); buf_putc('\n');
col = 1; line++; col = 1; line++;
} }
else else
{ {
putchar(c); buf_putc(c);
col++; if (c == 8) col--;
else if (c > 32) col++;
if (col > MAX_COL) if (col > MAX_COL)
{ {
col = 1; line++; col = 1; line++;
@ -221,15 +244,16 @@ extern int _INITGNOSTDIO(void);
} }
if ((line == MAX_LINE) && standardOut) if ((line == MAX_LINE) && standardOut)
{ long percent; { long percent;
tputs(inv,1,tprchar); buf_flush();
putchar(15);
if (!pipeFlag) if (!pipeFlag)
{ {
percent = (ftell(file) * 100) / eofs.eof; percent = (b_mark * 100) / eofs.eof;
printf(" - %s (%2ld%%) - ",truncated,percent); printf(" - %s (%2ld%%) - ",truncated,percent);
} }
else else
printf(" - (more) - "); printf(" - (more) - ");
tputs(nor,1,tprchar); putchar(14);
fflush(stdout); fflush(stdout);
c = getcharacter(tty->_file); c = getcharacter(tty->_file);
c = c & 0x7f; c = c & 0x7f;
@ -245,22 +269,21 @@ extern int _INITGNOSTDIO(void);
putchar(' '); putchar(' ');
putchar(8); putchar(8);
} }
fflush(stdout);
} }
c = getc(file); c = buf_getc();
if (c == EOF) quit = 1; if (c == EOF) quit = 1;
c &= 0xFF; /*c &= 0xFF; */
if (c == 0x04) quit = 1; if (c == 0x04) quit = 1;
} }
if (!pipeFlag) if (!pipeFlag) close(file);
fclose(file); else abort = 1; /* we are DONE if this was a pipe */
else
abort = 1; /* we are DONE if this was a pipe */
if (!abort && (argc > 2) && (i != argc) && (line < MAX_LINE) if (!abort && (argc > 2) && (i != argc) && (line < MAX_LINE)
&& standardOut) && standardOut)
{ {
tputs(inv,1,tprchar); putchar(15);
printf("hit a key for next file"); printf("hit a key for next file");
tputs(nor,1,tprchar); putchar(14);
fflush(stdout); fflush(stdout);
c = getcharacter(tty->_file); c = getcharacter(tty->_file);
for (c = 0; c < 23; c++) for (c = 0; c < 23; c++)
@ -273,13 +296,9 @@ extern int _INITGNOSTDIO(void);
if (c == 27) abort = 1; if (c == 27) abort = 1;
} }
} }
/*putchar(5);*/ buf_flush();
putchar(5);
sg.sg_flags = oldsg_flags; sg.sg_flags = oldsg_flags;
ioctl(tty->_file,TIOCSETP,&sg); ioctl(tty->_file,TIOCSETP,&sg);
exit(0); exit(0);
} }
int tprchar(char c)
{
putchar( c );
}