1: /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
2: /* $Header: retab.c,v 1.1 84/07/04 17:57:16 timo Exp $ */
3:
4: /*
5: * retab [ [-ain] [-t N] ] [ file ]
6: *
7: * replace all tabs by appropriate number of spaces,
8: * and replace these spaces by optimal number of tabs.
9: *
10: * -i: replace only initial spaces
11: * -n: no spaces are replaced
12: * -a: all spaces are replaced (Default)
13: * -t N: tab positions are multiples of N; default N=8
14: *
15: */
16:
17: #define OPTIONS "-[a|i|n] -tN"
18: #include "par.h"
19: #include <ctype.h>
20:
21: #define NL '\n'
22: #define TAB '\t'
23: #define BS '\b'
24: #define SP ' '
25:
26: int tabwidth = 8;
27: int rpos, wpos;
28: char opt = 'a';
29:
30: int
31: options(c, v) char *v[]; {
32: char ch = v[0][1];
33:
34: VOID(c);
35: switch (ch) {
36: case 'a':
37: case 'i':
38: case 'n':
39: opt = ch;
40: return 1;
41: case 't':
42: tabwidth = atoi(&v[0][2]);
43: if (tabwidth <= 1)
44: return ERR;
45: return 1;
46: default:
47: return ERR;
48: }
49: }
50:
51: process() {
52: register int ch;
53:
54: rpos = wpos = 0;
55: while ((ch = getc(ifile)) NE EOF)
56: switch (ch) {
57: case NL:
58: rpos = 0;
59: wpos = 0;
60: putchar(NL);
61: break;
62: case BS:
63: rpos--;
64: break;
65: case TAB:
66: rpos = ntab(rpos);
67: break;
68: case SP:
69: rpos++;
70: break;
71: default:
72: if (isprint(ch))
73: rpos++;
74: outchar(ch);
75: break;
76: }
77: }
78:
79: outchar(ch) char ch; {
80:
81: while (wpos > rpos-1) {
82: putchar(BS);
83: wpos--;
84: }
85: if (opt EQ 'a' || (opt EQ 'i' && wpos EQ 0))
86: while (rpos - wpos > 2 && ntab(wpos) < rpos) {
87: putchar(ntab(wpos) EQ wpos+2 ? SP : TAB);
88: wpos = ntab(wpos);
89: }
90: while (wpos < rpos-1) {
91: putchar(SP);
92: wpos++;
93: }
94: putchar(ch);
95: wpos++;
96: }
97:
98: int
99: ntab(n) {
100: return (n + tabwidth) / tabwidth * tabwidth;
101: }
Defined functions
ntab
defined in line
98; used 4 times
Defined variables
opt
defined in line
28; used 3 times
rpos
defined in line
27; used 11 times
wpos
defined in line
27; used 14 times
Defined macros
BS
defined in line
23; used 1 times
NL
defined in line
21; used 1 times
SP
defined in line
24; used 2 times
TAB
defined in line
22; used 1 times