| 1 | #include <stdio.h> |
| 2 | |
| 3 | #define NL '\n' |
| 4 | #define CLEAR 014 |
| 5 | #define HOME 031 |
| 6 | #define BACK 037 |
| 7 | #define BUFMAX 16384 |
| 8 | #define PAGMAX 64 |
| 9 | |
| 10 | int file; |
| 11 | int cp, hp; |
| 12 | int page[PAGMAX]; |
| 13 | int eof; |
| 14 | |
| 15 | char buf[BUFMAX]; |
| 16 | int bptr, bnxt, bend; |
| 17 | |
| 18 | |
| 19 | #define incr(a,b) (a++, a&=b-1) |
| 20 | #define decr(a,b) (a--, a&=b-1) |
| 21 | |
| 22 | |
| 23 | main(argc,argv) |
| 24 | char *argv[]; |
| 25 | { |
| 26 | int n=1; |
| 27 | if(argc<2) |
| 28 | print(); |
| 29 | else { |
| 30 | while(argv[n]!=-1) { |
| 31 | if((file=open(argv[n],0))>=0) { |
| 32 | print(); |
| 33 | close(file); |
| 34 | } else printf("dk: `%s' cannot open\n",argv[n]); |
| 35 | n++; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | print() |
| 41 | { |
| 42 | register int nlc; |
| 43 | char buf[2]; |
| 44 | |
| 45 | hp=0; cp=0; |
| 46 | bptr=bnxt=bend=0; |
| 47 | putchar(CLEAR); |
| 48 | for(;;) { |
| 49 | setpage(); |
| 50 | nlc=0; |
| 51 | putchar(BACK); |
| 52 | while(nlc<20) { |
| 53 | char c; |
| 54 | c = readchar(); |
| 55 | if(eof) |
| 56 | return; |
| 57 | if(c==NL) nlc++; |
| 58 | putchar(c); |
| 59 | } |
| 60 | while(read(2,buf,1)==1 && buf[0]!=NL) { |
| 61 | switch(buf[0]) { |
| 62 | case '/': |
| 63 | case HOME: |
| 64 | putchar(CLEAR); |
| 65 | getpage(0); |
| 66 | break; |
| 67 | case '-': |
| 68 | case BACK: |
| 69 | getpage(-1); |
| 70 | putchar(CLEAR); |
| 71 | break; |
| 72 | case '!': |
| 73 | shell(); buf[0]=NL; break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | #include <signal.h> |
| 81 | #define EOR '\n' |
| 82 | shell() |
| 83 | { |
| 84 | int rc, status, unixpid; |
| 85 | if( (unixpid=fork())==0 ) { |
| 86 | close(0); dup(2); |
| 87 | execl("/bin/sh", "sh", "-t", 0); |
| 88 | exit(255); |
| 89 | } |
| 90 | else if(unixpid == -1) |
| 91 | return(0); |
| 92 | else{ signal(SIGINT,SIG_IGN); signal(SIGQUIT,SIG_IGN); |
| 93 | while( (rc = wait(&status)) != unixpid && rc != -1 ) ; |
| 94 | signal(SIGINT,SIG_DFL); signal(SIGQUIT,SIG_DFL); |
| 95 | return(1); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | readchar() |
| 100 | { |
| 101 | register char c; |
| 102 | if(bnxt==bend) |
| 103 | fillbuf(); |
| 104 | if(eof) |
| 105 | return(-1); |
| 106 | c=buf[bnxt]; |
| 107 | incr(bnxt,BUFMAX); |
| 108 | return(c); |
| 109 | } |
| 110 | |
| 111 | fillbuf() |
| 112 | { |
| 113 | register int r; |
| 114 | register int len; |
| 115 | ensure(); |
| 116 | if(eof) |
| 117 | return; |
| 118 | r=read(file,&buf[bend],512); |
| 119 | if(r==-1) |
| 120 | return; |
| 121 | bend += r; bend &= BUFMAX-1; |
| 122 | if(r==0) |
| 123 | eof++; |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | ensure() |
| 128 | { |
| 129 | for(;;) { |
| 130 | if(bptr<=bend) |
| 131 | return(BUFMAX-bend-1); |
| 132 | if(bptr-bend>=512) |
| 133 | return(512); |
| 134 | losepage(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | losepage() |
| 139 | { |
| 140 | if(hp<cp) |
| 141 | bptr=page[incr(hp,PAGMAX)]; |
| 142 | } |
| 143 | |
| 144 | setpage() |
| 145 | { |
| 146 | incr(cp,PAGMAX); |
| 147 | if(cp==hp) |
| 148 | incr(hp,PAGMAX); |
| 149 | page[cp]=bnxt; |
| 150 | } |
| 151 | |
| 152 | getpage(i) |
| 153 | { |
| 154 | if(i==0) |
| 155 | cp=hp; |
| 156 | else if(cp!=hp) |
| 157 | decr(cp,PAGMAX); |
| 158 | bnxt=page[cp]; |
| 159 | } |