pg: Add the late V7 "pg" command
authorAlan Cox <alan@linux.intel.com>
Tue, 20 Feb 2018 18:31:19 +0000 (18:31 +0000)
committerAlan Cox <alan@linux.intel.com>
Tue, 20 Feb 2018 18:31:19 +0000 (18:31 +0000)
Applications/V7/cmd/Makefile.6502
Applications/V7/cmd/Makefile.68000
Applications/V7/cmd/Makefile.6809
Applications/V7/cmd/Makefile.z80
Applications/V7/cmd/fuzix-cmd.pkg
Applications/V7/cmd/pg.c [new file with mode: 0644]

index 90b8067..576429e 100644 (file)
@@ -12,7 +12,7 @@ CRT0NS = ../../../Library/libs/crt0nostdio_6502.o
 
 SRCS  = ac.c accton.c at.c atrun.c col.c comm.c cron.c crypt.c dc.c dd.c \
        deroff.c diff3.c diff.c diffh.c join.c look.c makekey.c mesg.c \
-       newgrp.c pr.c ptx.c rev.c split.c su.c sum.c test.c time.c tsort.c \
+       newgrp.c pg.c pr.c ptx.c rev.c split.c su.c sum.c test.c time.c tsort.c \
        wall.c
 
 SRCSNS = ed.c
index 6665e5a..6501bd0 100644 (file)
@@ -18,6 +18,7 @@ SRCS  = ac.c col.c dc.c diff.c makekey.c ptx.c sum.c wall.c
 SRCS += accton.c  comm.c   dd.c      diffh.c  mesg.c     rev.c    test.c
 SRCS += at.c      cron.c   deroff.c  join.c   newgrp.c   split.c  time.c
 SRCS += atrun.c   crypt.c  diff3.c   look.c   pr.c       su.c     tsort.c
+SRCS += pg.c
 
 OBJS = $(SRCS:.c=.o)
 
index 96e5070..04e1069 100644 (file)
@@ -18,6 +18,7 @@ SRCS  = ac.c col.c dc.c diff.c makekey.c ptx.c sum.c wall.c
 SRCS += accton.c  comm.c   dd.c      diffh.c  mesg.c     rev.c    test.c
 SRCS += at.c      cron.c   deroff.c  join.c   newgrp.c   split.c  time.c
 SRCS += atrun.c   crypt.c  diff3.c   look.c   pr.c       su.c     tsort.c
+SRCS += pg.c
 
 OBJS = $(SRCS:.c=.o)
 
index 55636fd..e19e33e 100644 (file)
@@ -12,7 +12,7 @@ PROGLOAD=`(cat ../../../Kernel/platform/config.h; echo PROGLOAD) | cpp -E | tail
 
 SRCS  = ac.c accton.c at.c atrun.c col.c comm.c cpio.c cron.c crypt.c dc.c dd.c \
        deroff.c diff3.c diff.c diffh.c join.c look.c makekey.c mesg.c \
-       newgrp.c pr.c ptx.c rev.c split.c su.c sum.c test.c time.c tsort.c \
+       newgrp.c pg.c pr.c ptx.c rev.c split.c su.c sum.c test.c time.c tsort.c \
        wall.c
 
 SRCSNS = ed.c
index d1e5561..c291b14 100644 (file)
@@ -30,6 +30,7 @@ f 0755 /usr/bin/sum           sum
 f 0755 /usr/bin/time          time
 f 0755 /usr/bin/tsort         tsort
 f 0755 /usr/bin/wall          wall
+f 0755 /usr/bin/pg            pg
 
 # Man pages - in a separate package so that storage-limited targets can
 # disable it.
diff --git a/Applications/V7/cmd/pg.c b/Applications/V7/cmd/pg.c
new file mode 100644 (file)
index 0000000..a80926d
--- /dev/null
@@ -0,0 +1,165 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <wait.h>
+
+#define EOR '\n'
+#define NL '\n'
+#define CLEAR 014
+#define HOME 031
+#define BACK 037
+#define BUFMAX 16384
+#define PAGMAX 64
+
+int file;
+int cp, hp;
+int page[PAGMAX];
+int eof;
+
+char buf[BUFMAX];
+int bptr, bnxt, bend;
+
+
+#define incr(a,b) (a++, a&=b-1)
+#define decr(a,b) (a--, a&=b-1)
+
+
+void losepage(void)
+{
+       if(hp<cp)
+               bptr=page[incr(hp,PAGMAX)];
+}
+
+int ensure(void)
+{
+       for(;;) {
+               if(bptr<=bend)
+                       return(BUFMAX-bend-1);
+               if(bptr-bend>=512)
+                       return(512);
+               losepage();
+       }
+}
+
+
+void fillbuf(void)
+{
+       register int r;
+       ensure();
+       if(eof)
+               return;
+       r=read(file,&buf[bend],512);
+       if(r==-1)
+               return;
+       bend += r; bend &= BUFMAX-1;
+       if(r==0)
+               eof++;
+}
+
+int readchar(void)
+{
+       register char c;
+       if(bnxt==bend)
+               fillbuf();
+       if(eof)
+               return(-1);
+       c=buf[bnxt];
+       incr(bnxt,BUFMAX);
+       return(c);
+}
+
+void setpage(void)
+{
+       incr(cp,PAGMAX);
+       if(cp==hp)
+               incr(hp,PAGMAX);
+       page[cp]=bnxt;
+}
+
+void getpage(int i)
+{
+       if(i==0)
+               cp=hp;
+       else    if(cp!=hp)
+                       decr(cp,PAGMAX);
+       bnxt=page[cp];
+}
+
+
+int shell(void)
+{
+       int rc, status, unixpid;
+       if( (unixpid=fork())==0 ) {
+               close(0); dup(2);
+               execl("/bin/sh", "sh", "-t", 0);
+               exit(255);
+       }
+       else if(unixpid == -1)
+               return(0);
+       else{   
+               signal(SIGCHLD, SIG_DFL);
+               signal(SIGINT,SIG_IGN); signal(SIGQUIT,SIG_IGN);
+               while( (rc = wait(&status)) != unixpid && rc != -1 ) ;
+               signal(SIGINT,SIG_DFL); signal(SIGQUIT,SIG_DFL);
+               return(1);
+       }
+}
+
+void print(void)
+{
+       register int nlc;
+       char buf[2];
+
+       hp=0; cp=0;
+       bptr=bnxt=bend=0;
+       putchar(CLEAR);
+       for(;;) {
+               setpage();
+               nlc=0;
+               putchar(BACK);
+               while(nlc<20) {
+                       char c;
+                       c = readchar();
+                       if(eof)
+                               return;
+                       if(c==NL) nlc++;
+                       putchar(c);
+               }
+               while(read(2,buf,1)==1 && buf[0]!=NL) {
+                       switch(buf[0]) {
+                               case '/':
+                               case HOME:
+                                       putchar(CLEAR);
+                                       getpage(0);
+                                       break;
+                               case '-':
+                               case BACK:
+                                       getpage(-1);
+                                       putchar(CLEAR);
+                                       break;
+                               case '!':
+                                       shell(); buf[0]=NL; break;
+                       }
+               }
+       }
+}
+
+
+int main(int argc, char *argv[])
+{
+       int n=1;
+       if(argc<2)
+               print();
+       else {
+               while(argv[n] != NULL) {
+                       if((file=open(argv[n],0))>=0) {
+                               print();
+                               close(file);
+                       } else  printf("pg: `%s' cannot open\n",argv[n]);
+                       n++;
+               }
+       }
+       return 0;
+}