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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXTAB 10
#define TABSIZE 8
#define MAXTEXT 500
#define MAXTABLIST 10
#define GETTEXT(s) \
({ \
int i, c; \
for (i = 0; i < MAXTEXT && (c = getchar()) != EOF; i++) \
s[i] = c; \
s[i] = 0; \
i; \
})
int gettablist(char *s, int *t);
void entab(char *s, char *t, int *tablist, int tablistc);
void detab(char *s, char *t, int *tablist, int tablistc);
int main(int argc, char *argv[]) {
int colv[MAXTABLIST], colc;
char op, s[MAXTEXT + 1], t[MAXTEXT + 1];
if ((argc != 2 && argc != 3) || ((op = argv[1][1]) != 'e' && op != 'd')) {
printf("Usage: -e 5,3...\n");
return 1;
}
if (argc == 3) {
if (!(colc = gettablist(argv[2], colv))) {
printf("Error: invalid tablist\n");
return 1;
}
} else {
colv[0] = TABSIZE;
colc = 1;
}
printf("Enter text to %s and press CTRL+D\n", op == 'e' ? "entab" : "detab");
if (!GETTEXT(s)) {
printf("Did not receive text!\n");
return 0;
}
switch (op) {
case 'e':
entab(s, t, colv, colc);
break;
case 'd':
// detab(s, t, colv, colc);
break;
default:
printf("Error: invalid operation.\n");
return 1;
}
printf("%s text:\n%s\n", op == 'e' ? "entabbed" : "detabbed", t);
return 0;
}
int gettablist(char *s, int *t) {
int i, j, k;
char col[MAXTAB];
for (i = 0, j = 0, k = 0; j < MAXTABLIST; i++) {
if (k >= MAXTAB) {
printf("Error: tablist entry too large\n");
return 0;
}
if (s[i] == ' ' || s[i] == ',' || s[i] == 0) {
col[k] = 0;
t[j++] = atoi(col);
if (s[i] == 0)
break;
else
k = 0;
} else if (isdigit(s[i]))
col[k++] = s[i];
else {
printf("Error: invalid char %c in tablist\n", s[i]);
return 0;
}
}
return j;
}
void entab(char *s, char *t, int *tablist, int tablistc) {
int i, j, k;
if (tablistc == 1) {
for (i = 0, j = 0; i < MAXTEXT && j < MAXTEXT && (t[j] = s[i]) != 0; i++) {
if (s[i] == '\t') {
for (k = 0; k < TABSIZE && j < MAXTEXT; k++, j++)
t[j] = ' ';
} else
j++;
}
}
t[j] = 0;
}
|