]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/support/l.c
Updated documentation and comments about sendmail/pipe.
[nmh] / docs / historical / mh-jun-1982 / support / l.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 /* Added termcap (termlib): BSB:3Com 9/17/80 */
9 /* Rewrite for VAX: BSB 9/9/79 */
10 /* Compiled with V7 cc: BSB 6/2/79 */
11
12 #include <stdio.h>
13 #include <sgtty.h>
14 #include <signal.h>
15 #include <setjmp.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18
19 #define YES 1
20 #define NO 0
21
22 #ifdef TERMCAP
23 # define CLEARPAGE { tputs(cl, 1, toutc); putchar('\000'); }
24 #else
25 # define CLEARPAGE putchar('\014');
26 #endif
27 #define discard(io) { io->_cnt = BUFSIZ; io->_ptr = io->_base; }
28
29 #define PAUSE 0
30 #define NOTIFY 1
31 #define CLEAR 2
32 #define NOTICLEAR 3
33
34 int spage;
35 int page;
36 short width;
37 short noclr = 0;
38 short first = 0;
39 short numop = 0;
40 short wrap = 0;
41 short flagctl = 1;
42 short ontty = 1; /* assume we're outputting to a tty */
43 short delflg; /* a <DEL> was typed. */
44 char bp[1024];
45 char cl[40]; /* Clear Screen string */
46 char *clp = cl;
47 int toutc();
48
49 FILE *fin;
50 jmp_buf envir;
51
52 main(argc, argv)
53 int argc;
54 char *argv[];
55 {
56 register int i,k;
57 register char *c1, *c2;
58 int j, clrflag;
59 int int2(), getout();
60 struct sgttyb sg;
61 struct stat st;
62 extern char _sibuf[], _sobuf[];
63 extern char *getenv();
64
65 #ifdef TERMCAP
66 if((c1 = getenv("TERM")) == NULL)
67 c1 = "aa"; /* RAND */
68 if(tgetent(bp, c1) != 1) {
69 fprintf(stderr, "l: TERM env not in termcap\n");
70 exit(1);
71 }
72 if((width = tgetnum("co")) == -1 ||
73 (spage = tgetnum("li")) == -1) {
74 fprintf(stderr, "l: Can't find term width/length\n");
75 exit(1);
76 }
77 tgetstr("cl", &clp);
78 #else
79 width = 80;
80 spage = 24;
81 #endif
82 width--;
83 spage -= 2;
84 if(gtty(1, &sg) == -1) {
85 ontty = 0;
86 flagctl = 0;
87 }
88 c1 = c2 = argv[0];
89 do
90 if(*c1++ == '/')
91 c2 = c1;
92 while(*c1);
93 if(*c2 == 'c')
94 noclr++;
95 k=0;
96 for( i=1; i<argc; i++ ) {/* look for - args */
97 if(argv[i][0] == '-' ) {
98 switch( argv[i][1] ) {
99 case 'c': noclr++; break;
100 case 'f': first=num(&argv[i][2]); /* set first line */
101 break;
102 case 'n': numop=1; /* set number option */
103 break;
104 case 'l':
105 case 'p': spage =num(&argv[i][2]); /* set page size */
106 break;
107 case 'w': width =num(&argv[i][2]);
108 break;
109 case 'x': flagctl = !flagctl;
110 break;
111 default: printf("Unknown switch: %s\n", argv[i]);
112 return;
113 }
114 } else
115 argv[k++] = argv[i];
116 }
117
118 if (spage<=0)
119 spage = 0x07fffffff; /* largest positive number! */
120 setbuf(stdout, _sobuf);
121 if(ontty)
122 signal(SIGINT,int2);
123 signal(SIGQUIT,getout);
124 page = spage;
125 if(k == 0) { /* filter */
126 if(!setjmp(envir)) {
127 fin = stdin;
128 setbuf(fin, _sibuf);
129 pfile(0);
130 }
131 getout();
132 }
133 j = 0;
134 setjmp(envir);
135 while((i=j++) < k) {
136 page = spage;
137 clrflag = 0;
138 if(fin != NULL) { fclose(fin); fin = NULL; }
139 if(stat(argv[i], &st) == 0 && (st.st_mode&S_IFMT) == S_IFDIR){
140 printf("%s: Is a directory!\n", argv[i]);
141 continue;
142 }
143 if((fin = fopen(argv[i],"r")) == NULL) {
144 printf("Cannot open \"%s\" for reading!\n", argv[i]);
145 continue;
146 }
147 if(st.st_size == 0) {
148 printf("File \"%s\" is empty.\n", argv[i]);
149 continue;
150 }
151 if(k>1) {
152 if(i && delflg <= 0)
153 printf("\n\n");
154 delflg = 0;
155 if(ontty) {
156 printf("Press <RETURN> to list \"%s\"\n", argv[i]);
157 clrflag = 1;
158 } else
159 printf(">>>>> File \"%s\"\n", argv[i]);
160 }
161
162 pfile(clrflag);
163 }
164 getout();
165 }
166
167 int linpos, line, ct;
168
169 pfile(flg)
170 {
171 register int c;
172
173 if(flg)
174 nextpage(NOTICLEAR);
175 delflg = -1;
176 line = 1;
177 ct = page;
178 while (line < first) /* Dcrocker: skip to first line */
179 if((c = getch()) == EOF)
180 return;
181 else if(c == '\n')
182 line++;
183 linpos = 0;
184
185 if ((c = getch()) != EOF) {
186 if(!flg && !noclr && ontty)
187 CLEARPAGE;
188 do
189 putch(c);
190 while ((c = getch()) != EOF);
191 }
192 fflush(stdout);
193 }
194
195 num(s) /* computes the internal form of a number */
196 register char *s; /* bad chars are ignored */
197 {
198 register int c, i, sign;
199
200 sign=1; i=0;
201 while(c = *s++) {
202 if(c=='-' && sign==1) sign = -1;
203 c -= '0';
204 if(c>=0 && c<=9) i=i*10+c;
205 }
206 return(i*sign);
207 }
208
209 nextpage (clearpage)
210 { char c;
211
212 if(!ontty)
213 return;
214 if (clearpage & NOTIFY)
215 putchar('\007');
216 fflush(stdout);
217 c = 0;
218 while(read(2, &c, 1) && c != '\n') ;
219 if (clearpage & CLEAR && c) {
220 CLEARPAGE;
221 page = spage;
222 } else {
223 page = (spage>>1) + (spage>>3);
224 /*** page = spage * .6; ***/
225 }
226 return;
227 }
228
229 int2()
230 {
231 signal(SIGINT,int2);
232 discard(stdout);
233 if(delflg)
234 putchar('\n');
235 delflg++;
236 longjmp(envir, 1);
237 }
238
239 int peekc = -2;
240
241 peekch()
242 {
243 return(peekc = getch());
244 }
245
246 getch()
247 {
248 register int c;
249
250 if(peekc != -2) {
251 c = peekc;
252 peekc = -2;
253 return(c);
254 };
255 c = getc(fin);
256 if(c != EOF)
257 c &= 0177;
258 return(c);
259 }
260
261 putch(c)
262 register int c;
263 {
264 if(linpos == 0 && numop) {
265 if(!wrap)
266 printf("%5d\t", line);
267 else
268 printf("\t");
269 linpos += 8;
270 }
271 if(c < 040 || c == 0177) {
272 switch(c) {
273 case '\n':
274 line++;
275 linpos = 0;
276 wrap = 0;
277 putchar(c);
278 break;
279 case '\f':
280 goto npage;
281 case '\t':
282 linpos += 8;
283 linpos &= ~07;
284 putchar(c);
285 break;
286 case '\b': /* ignore backspaces */
287 break;
288 default:
289 if(flagctl) {
290 if(c == '\7')
291 putchar(c);
292 putch('^');
293 if(c != 0177)
294 c += '@';
295 else
296 c = 'd';
297 }
298 putchar(c);
299 break;
300 }
301 }
302 else {
303 putchar(c);
304 linpos++;
305 }
306 if(width && linpos >= width && peekch() != '\n') {
307 putchar('\n'); linpos = 0;
308 }
309 if(linpos == 0 && --ct <= 0) {
310 npage: nextpage(NOTICLEAR);
311 ct = page;
312 }
313 }
314
315
316 getout()
317 {
318 exit(0);
319 }
320
321 toutc(ch)
322 {
323 putchar