-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentab.c
90 lines (86 loc) · 1.84 KB
/
entab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Write a program entab that replaces strings of blanks by the minimum number
* of tabs and blanks to achieve the same spacing. Use the same tab stops as for
* detab.
*
* Exercise 5-11. Modify the program entab and detab (written as exercises in
* Chapter 1) to accept a list of tab stops as arguments. Use the default tab
* settings if there are no arguments.*/
#include "stdio.h"
#include "stdlib.h"
#define RB '#' // for replacing of blank
#define RT '@' // for replacing of tab
int main(int argc, char *argv[]) {
const char *usage = "usage: entab TABSTOP\n";
unsigned char ts; // tab stop
int c, bep, p, nb, nt;
enum STATUS {IN, OUT};
char status;
// default tab stop
if (argc == 1)
ts = 4;
else if (argc == 2)
ts = atoi(*++argv);
else {
printf("error: invalid argument\n%s", usage);
return 1;
}
bep = 0; // blank entrance position
p = 0; // current position in a line
nb = 0; // the number of blanks
nt = 0; // the number of tabs
status = OUT; // outside blank block
while ((c = getchar()) != EOF) {
switch (status) {
case IN:
switch (c) {
case ' ':
break;
case '\t':
p += ts - 1;
break;
case '\n':
putchar('\n');
nb = 0;
nt = 0;
p = -1;
status = OUT;
break;
default: // non-blank
bep -= bep % ts;
nb = (p - bep) % ts;
nt = (p - bep) / ts;
while (nt--)
putchar(RT);
while (nb--)
putchar(RB);
nt = 0;
nb = 0;
putchar(c);
status = OUT;
break;
}
break;
case OUT:
switch (c) {
case ' ':
bep = p;
status = IN;
break;
case '\t':
bep = p;
p += ts - 1;
status = IN;
break;
case '\n':
putchar('\n');
p = -1;
break;
default:
putchar(c);
break;
}
break;
}
++p;
}
}