1 72197f89 2014-10-21 rsc /* col - eliminate reverse line feeds */
2 72197f89 2014-10-21 rsc #include <u.h>
3 72197f89 2014-10-21 rsc #include <libc.h>
4 72197f89 2014-10-21 rsc #include <ctype.h>
5 72197f89 2014-10-21 rsc #include <bio.h>
12 72197f89 2014-10-21 rsc LINELN = 800,
14 72197f89 2014-10-21 rsc Tabstop = 8, /* must be power of 2 */
17 72197f89 2014-10-21 rsc static int bflag, xflag, fflag;
18 72197f89 2014-10-21 rsc static int cp, lp;
19 72197f89 2014-10-21 rsc static int half;
20 72197f89 2014-10-21 rsc static int ll, llh, mustwr;
21 72197f89 2014-10-21 rsc static int pcp = 0;
23 72197f89 2014-10-21 rsc static char *page[PL];
24 72197f89 2014-10-21 rsc static char *line;
25 72197f89 2014-10-21 rsc static char lbuff[LINELN];
26 72197f89 2014-10-21 rsc static Biobuf bin, bout;
28 72197f89 2014-10-21 rsc void emit(char *s, int lineno);
29 72197f89 2014-10-21 rsc void incr(void), decr(void);
30 72197f89 2014-10-21 rsc void outc(Rune);
35 72197f89 2014-10-21 rsc fprint(2, "usage: %s [-bfx]\n", argv0);
36 72197f89 2014-10-21 rsc exits("usage");
40 72197f89 2014-10-21 rsc main(int argc, char **argv)
60 72197f89 2014-10-21 rsc for (ll=0; ll < PL; ll++)
61 72197f89 2014-10-21 rsc page[ll] = nil;
66 72197f89 2014-10-21 rsc line = lbuff;
68 72197f89 2014-10-21 rsc Binit(&bin, 0, OREAD);
69 72197f89 2014-10-21 rsc Binit(&bout, 1, OWRITE);
70 72197f89 2014-10-21 rsc while ((ch = Bgetrune(&bin)) != Beof) {
83 72197f89 2014-10-21 rsc c = Bgetrune(&bin);
85 72197f89 2014-10-21 rsc case '7': /* reverse full line feed */
90 72197f89 2014-10-21 rsc case '8': /* reverse half line feed */
94 72197f89 2014-10-21 rsc if (--half < -1) {
101 72197f89 2014-10-21 rsc case '9': /* forward half line feed */
105 72197f89 2014-10-21 rsc if (++half > 0) {
124 72197f89 2014-10-21 rsc cp = (cp + Tabstop) & -Tabstop;
137 72197f89 2014-10-21 rsc if (!isascii(c) || isprint(c)) {
145 72197f89 2014-10-21 rsc for (i=0; i < PL; i++) {
146 72197f89 2014-10-21 rsc lno = (mustwr+i) % PL;
147 72197f89 2014-10-21 rsc if (page[lno] != 0)
148 72197f89 2014-10-21 rsc emit(page[lno], mustwr+i-PL);
150 72197f89 2014-10-21 rsc emit(" ", (llh + 1) & -2);
155 72197f89 2014-10-21 rsc outc(Rune c)
157 72197f89 2014-10-21 rsc if (lp > cp) {
158 72197f89 2014-10-21 rsc line = lbuff;
162 72197f89 2014-10-21 rsc while (lp < cp) {
163 72197f89 2014-10-21 rsc switch (*line) {
165 72197f89 2014-10-21 rsc *line = ' ';
177 72197f89 2014-10-21 rsc while (*line == '\b')
179 72197f89 2014-10-21 rsc if (bflag || *line == '\0' || *line == ' ')
180 72197f89 2014-10-21 rsc cp += runetochar(line, &c) - 1;
182 72197f89 2014-10-21 rsc char c1, c2, c3;
184 72197f89 2014-10-21 rsc c1 = *++line;
185 72197f89 2014-10-21 rsc *line++ = '\b';
187 72197f89 2014-10-21 rsc *line++ = c;
188 72197f89 2014-10-21 rsc while (c1) {
190 72197f89 2014-10-21 rsc *line++ = c1;
195 72197f89 2014-10-21 rsc line = lbuff;
200 72197f89 2014-10-21 rsc store(int lno)
203 72197f89 2014-10-21 rsc if (page[lno] != nil)
204 72197f89 2014-10-21 rsc free(page[lno]);
205 72197f89 2014-10-21 rsc page[lno] = malloc((unsigned)strlen(lbuff) + 2);
206 72197f89 2014-10-21 rsc if (page[lno] == nil)
207 72197f89 2014-10-21 rsc sysfatal("out of memory");
208 72197f89 2014-10-21 rsc strcpy(page[lno], lbuff);
212 72197f89 2014-10-21 rsc fetch(int lno)
219 72197f89 2014-10-21 rsc *p++ = '\0';
220 72197f89 2014-10-21 rsc line = lbuff;
222 72197f89 2014-10-21 rsc if (page[lno])
223 72197f89 2014-10-21 rsc strcpy(line, page[lno]);
227 72197f89 2014-10-21 rsc emit(char *s, int lineno)
231 72197f89 2014-10-21 rsc static int cline = 0;
234 72197f89 2014-10-21 rsc while (cline < lineno - 1) {
235 72197f89 2014-10-21 rsc Bputc(&bout, '\n');
239 72197f89 2014-10-21 rsc if (cline != lineno) {
240 72197f89 2014-10-21 rsc Bputc(&bout, ESC);
241 72197f89 2014-10-21 rsc Bputc(&bout, '9');
245 72197f89 2014-10-21 rsc Bputc(&bout, '\r');
248 72197f89 2014-10-21 rsc while (*p) {
250 72197f89 2014-10-21 rsc while (*p++ == ' ')
251 72197f89 2014-10-21 rsc if ((++ncp & 7) == 0 && !xflag) {
253 72197f89 2014-10-21 rsc Bputc(&bout, '\t');
257 72197f89 2014-10-21 rsc while (pcp < ncp) {
258 72197f89 2014-10-21 rsc Bputc(&bout, ' ');
261 72197f89 2014-10-21 rsc Bputc(&bout, *p);
262 72197f89 2014-10-21 rsc if (*p++ == '\b')
275 72197f89 2014-10-21 rsc store(ll++);
276 72197f89 2014-10-21 rsc if (ll > llh)
278 72197f89 2014-10-21 rsc lno = ll % PL;
279 72197f89 2014-10-21 rsc if (ll >= mustwr && page[lno]) {
280 72197f89 2014-10-21 rsc emit(page[lno], ll - PL);
282 72197f89 2014-10-21 rsc free(page[lno]);
283 72197f89 2014-10-21 rsc page[lno] = nil;
291 72197f89 2014-10-21 rsc if (ll > mustwr - PL) {
292 72197f89 2014-10-21 rsc store(ll--);