]> diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/support/l.c
Removed --depth 1 from git clone invocation.
[nmh] / docs / historical / mh-nov-1983 / support / l.c
1 /* Rewrite for VAX: BSB 9/9/79 */
2 /* Compiled with V7 cc: BSB 6/2/79 */
3
4 #include <stdio.h>
5 #include <sgtty.h>
6 #include <signal.h>
7 #include <setjmp.h>
8 #include <sys/types.h>
9 #include <stat.h>
10
11 #define CLEARPAGE write(2,"\014\000",2);
12 #define PAUSE 0
13 #define NOTIFY 1
14 #define CLEAR 2
15 #define NOTICLEAR 3
16
17 int spage = 37;
18 int page;
19 short int width = 79;
20 short int noclr = 0;
21 short int first = 0;
22 short int numop = 0;
23 short int flagctl = 1;
24 short int ontty = 1; /* assume we're outputting to a tty */
25 short int delflg; /* a <DEL> was typed. */
26
27 FILE *fin;
28 jmp_buf envir;
29
30 main(argc, argv)
31 int argc;
32 char *argv[];
33 {
34 register int i,k,n;
35 register char *c1, *c2;
36 int j, clrflag;
37 int int2(), getout();
38 struct sgttyb sg;
39 struct stat st;
40 extern char _sibuf[], _sobuf[];
41
42 if(gtty(1, &sg) == -1) {
43 ontty = 0;
44 flagctl = 0;
45 }
46 c1 = c2 = argv[0];
47 do
48 if(*c1++ == '/')
49 c2 = c1;
50 while(*c1);
51 if(*c2 == 'c')
52 noclr++;
53 k=0;
54 for( i=1; i<argc; i++ ) {/* look for - args */
55 if(argv[i][0] == '-' ) {
56 switch( argv[i][1] ) {
57 case 'c': noclr++; break;
58 case 'f': first=num(&argv[i][2]); /* set first line */
59 break;
60 case 'n': numop=1; /* set number option */
61 break;
62 case 'l':
63 case 'p': spage =num(&argv[i][2]); /* set page size */
64 break;
65 case 'w': width =num(&argv[i][2]);
66 break;
67 case 'x': flagctl = flagctl? 0 : 1;
68 break;
69 default: printf("Unknown switch: %s\n", argv[i]);
70 return;
71 }
72 } else
73 argv[k++] = argv[i];
74 }
75
76 if (spage<=0)
77 spage = 0x07fffffff; /* largest positive number! */
78 setbuf(stdout, _sobuf);
79 if(ontty)
80 signal(SIGINT,int2);
81 signal(SIGQUIT,getout);
82 page = spage;
83 if(k == 0) { /* filter */
84 if(!setjmp(envir)) {
85 fin = stdin;
86 setbuf(fin, _sibuf);
87 pfile(0);
88 }
89 getout();
90 }
91 j = 0;
92 setjmp(envir);
93 while((i=j++) < k) {
94 page = spage;
95 clrflag = 0;
96 if(fin != NULL) { fclose(fin); fin = NULL; }
97 if(stat(argv[i], &st) == 0 && (st.st_mode&S_IFMT) == S_IFDIR){
98 printf("%s: Is a directory!\n", argv[i]);
99 continue;
100 }
101 if((fin = fopen(argv[i],"r")) == NULL) {
102 printf("Cannot open \"%s\" for reading!\n", argv[i]);
103 continue;
104 }
105 if(st.st_size == 0) {
106 printf("File \"%s\" is empty.\n", argv[i]);
107 continue;
108 }
109 if(k>1) {
110 if(i && delflg <= 0)
111 printf("\n\n");
112 delflg = 0;
113 if(ontty) {
114 printf("Press <RETURN> to list \"%s\"\n", argv[i]);
115 clrflag = 1;
116 } else
117 printf(">>>>> File \"%s\"\n", argv[i]);
118 }
119
120 pfile(clrflag);
121 }
122 getout();
123 }
124
125 int linpos, line, ct;
126
127 pfile(flg)
128 {
129 register int c;
130
131 if(flg) nextpage(NOTICLEAR);
132 else if(!noclr && ontty)
133 CLEARPAGE;
134 delflg = -1;
135 line = 1;
136 ct = page;
137 while (line < first) /* Dcrocker: skip to first line */
138 if((c = getch()) == EOF)
139 return;
140 else if(c == '\n')
141 line++;
142 linpos = 0;
143
144 while ((c = getch()) != EOF)
145 putch(c);
146
147 fflush(stdout);
148 }
149
150 num(s) /* computes the internal form of a number */
151 register char *s; /* bad chars are ignored */
152 {
153 register int c, i, sign;
154
155 sign=1; i=0;
156 while(c = *s++) {
157 if(c=='-' && sign==1) sign = -1;
158 c -= '0';
159 if(c>=0 && c<=9) i=i*10+c;
160 }
161 return(i*sign);
162 }
163
164 nextpage (clearpage)
165 { char c;
166
167 if(!ontty)
168 return;
169 if (clearpage & NOTIFY)
170 putchar('\007');
171 fflush(stdout);
172 c = 0;
173 while(read(2, &c, 1) && c != '\n') ;
174 if (clearpage & CLEAR && c) {
175 CLEARPAGE;
176 page = spage;
177 } else {
178 page = (spage>>1) + (spage>>3);
179 /*** page = spage * .6; ***/
180 }
181 return;
182 }
183
184 int2()
185 {
186 signal(SIGINT,int2);
187 stdout->_cnt = BUFSIZ;
188 stdout->_ptr = stdout->_base;
189 if(delflg)
190 putchar('\n');
191 delflg++;
192 longjmp(envir, 1);
193 }
194
195 int peekc = -2;
196
197 peekch()
198 {
199 return(peekc = getch());
200 }
201
202 getch()
203 {
204 register int c;
205 static word;
206
207 if(peekc != -2) {
208 c = peekc;
209 peekc = -2;
210 return(c);
211 };
212 c = getc(fin);
213 if(c != EOF)
214 c &= 0177;
215 return(c);
216 }
217
218 putch(c)
219 register int c;
220 {
221 if(linpos == 0 && numop) {
222 printf("%-5d | ");
223 linpos += 8;
224 }
225 if(c < 040 || c == 0177) switch(c) {
226 case '\n': line++;
227 linpos = -1;
228 break;
229 case 014: goto npage;
230 case 011: linpos += 8;
231 linpos &= ~07;
232 linpos--;
233 break;
234 default: if(flagctl) {
235 if(c == '\7')
236 putchar(c);
237 putch('^');
238 if(c != 0177)
239 c += '@';
240 else
241 c = 'd';
242 }
243 }
244 putchar(c);
245 linpos++;
246 if(width && linpos >= width && peekch() != '\n') {
247 putchar('\n'); linpos = 0;
248 }
249 if(linpos == 0 && --ct <= 0) {
250 npage: nextpage(NOTICLEAR);
251 ct = page;
252 }
253 }
254
255
256 getout()
257 {
258 exit(0);
259 }