Commit | Line | Data |
---|---|---|
783c5c09 BJ |
1 | /* Copyright (c) 1979 Regents of the University of California */ |
2 | #include <retrofit.h> | |
3 | #include <stdio.h> | |
4 | #include <sgtty.h> | |
5 | /* | |
6 | * cr3 - simulate chuck haley's cr3 | |
7 | * Bill Joy UCB July 1, 1977 | |
8 | * | |
9 | * This filter simulates chuck haleys cr3, | |
10 | * stopping output after each page (22 lines) to wait for | |
11 | * a carriage return, sending 22 more lines, or a EOF, sending 10 more lines. | |
12 | * | |
13 | * Typing in a positive number will cause that many lines to be sent, | |
14 | * a negative number that many lines to be skipped. | |
15 | */ | |
16 | ||
17 | struct sgttyb otty, ntty; | |
18 | int left, nxtleft, onintr(); | |
19 | char ch; | |
20 | char obuf[BUFSIZ]; | |
21 | ||
22 | main(argc, argv) | |
23 | int argc; | |
24 | char *argv[]; | |
25 | { | |
26 | register c; | |
27 | FILE *f; | |
28 | ||
29 | setbuf(stdout, obuf); | |
30 | gtty(1, &otty); | |
31 | gtty(1, &ntty); | |
32 | ntty.sg_flags &= ~ECHO; | |
33 | signal(2, onintr); | |
34 | stty(1, &ntty); | |
35 | left = 23; | |
36 | argc--; | |
37 | argv++; | |
38 | f = stdin; | |
39 | do { | |
40 | if (argc > 0) { | |
41 | close(0); | |
42 | if ((f=fopen(argv[0], "r")) == NULL) { | |
43 | fflush(stdout); | |
44 | perror(argv[0]); | |
45 | fflush(stdout); | |
46 | onintr(); | |
47 | } | |
48 | argc--; | |
49 | argv++; | |
50 | } | |
51 | for (;;) { | |
52 | c = getc(f); | |
53 | if (c == -1) | |
54 | break; | |
55 | if (left < 0) { | |
56 | left++; | |
57 | if (left == 0) | |
58 | left = nxtleft; | |
59 | } else { | |
60 | left--; | |
61 | if (left == 0) { | |
62 | fflush(stdout); | |
63 | getleft(); | |
64 | } | |
65 | } | |
66 | if (left > 0) | |
67 | putchar(c); | |
68 | while (c != '\n') { | |
69 | c = getc(f); | |
70 | if (c == -1) | |
71 | goto endfile; | |
72 | if (left > 0) | |
73 | putchar(c); | |
74 | } | |
75 | fflush(stdout); | |
76 | } | |
77 | endfile: | |
78 | fflush(stdout); | |
79 | } while (argc > 0); | |
80 | stty(1, &otty); | |
81 | exit(0); | |
82 | } | |
83 | ||
84 | onintr() | |
85 | { | |
86 | ||
87 | signal(2, 1); | |
88 | stty(1, &otty); | |
89 | exit(1); | |
90 | } | |
91 | ||
92 | getleft() | |
93 | { | |
94 | int i; | |
95 | ||
96 | i = number(); | |
97 | if (i == 0) { | |
98 | left = 22; | |
99 | return; | |
100 | } | |
101 | left = i; | |
102 | if (i < 0) { | |
103 | if (ch == '\n') | |
104 | nxtleft = 11; | |
105 | else { | |
106 | i = number(); | |
107 | nxtleft = i == 0 ? 11 : i; | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | char ch; | |
113 | ||
114 | number() | |
115 | { | |
116 | int i, sign; | |
117 | ||
118 | i = 0; | |
119 | sign = 0; | |
120 | while (read(2, &ch, 1) == 1) { | |
121 | switch (ch) { | |
122 | case ' ': | |
123 | if (sign == 0) | |
124 | continue; | |
125 | case '\n': | |
126 | return (i * sign); | |
127 | case '-': | |
128 | sign = -1; | |
129 | continue; | |
130 | default: | |
131 | if (ch < '0' || ch > '9') | |
132 | continue; | |
133 | i *= 10; | |
134 | i += ch - '0'; | |
135 | if (sign == 0) | |
136 | sign = 1; | |
137 | continue; | |
138 | } | |
139 | } | |
140 | return (11); | |
141 | } |