|
1 #include <stdio.h> |
|
2 #include <stdlib.h> |
|
3 |
|
4 |
|
5 #define MAXTABSTOPS 50 |
|
6 #define MAXLINECHARS 10000 |
|
7 |
|
8 |
|
9 void strpart(char * dest, const char *src, const int begin, const int end) |
|
10 { |
|
11 int i; |
|
12 i=begin; |
|
13 while (i<=end) |
|
14 { |
|
15 dest[i-begin] = src[i]; |
|
16 i++; |
|
17 } |
|
18 |
|
19 dest[i-begin] = '\0'; |
|
20 } |
|
21 |
|
22 int main(int numarg, char **args) |
|
23 { |
|
24 int tabstops[MAXTABSTOPS+1]; |
|
25 int i,j; |
|
26 char original[MAXLINECHARS]; |
|
27 char word[MAXLINECHARS]; |
|
28 int this_tab_stop; |
|
29 int last_char; |
|
30 int reverse=0; /* Funcionament normal */ |
|
31 int num_tab_stops; |
|
32 int output_column; |
|
33 |
|
34 |
|
35 if (numarg<2) |
|
36 { |
|
37 printf("Bad Usage.\n"); |
|
38 exit(1); |
|
39 } |
|
40 |
|
41 tabstops[0] = 0; |
|
42 tabstops[numarg] = MAXLINECHARS; |
|
43 num_tab_stops = 1; |
|
44 |
|
45 for (i=1; i<numarg; i++) |
|
46 { |
|
47 if (args[i][0] == '-') |
|
48 switch(args[i][1]) |
|
49 { |
|
50 case 'r': |
|
51 reverse=1; |
|
52 } |
|
53 else |
|
54 tabstops[num_tab_stops++] = atoi(args[i])-1; |
|
55 /* Usually columns start at '1' */ |
|
56 /* Not error-free in atoi! */ |
|
57 } |
|
58 |
|
59 while(!feof(stdin)) |
|
60 { |
|
61 fgets(original, MAXLINECHARS, stdin); |
|
62 if (feof(stdin)) break; |
|
63 |
|
64 if (reverse==0) |
|
65 this_tab_stop=0; |
|
66 else |
|
67 this_tab_stop=1; |
|
68 |
|
69 last_char=-1; |
|
70 |
|
71 if(reverse==0) |
|
72 { |
|
73 for(i=0; i<strlen(original); i++) |
|
74 { |
|
75 if(i == tabstops[this_tab_stop+1]) |
|
76 { |
|
77 strpart(word, original, tabstops[this_tab_stop], last_char); |
|
78 printf("%s", word); |
|
79 if(this_tab_stop != num_tab_stops) /* Ultim tab stop */ |
|
80 putchar('\t'); |
|
81 |
|
82 /* A pel proxim tab */ |
|
83 last_char = tabstops[++this_tab_stop]-1; |
|
84 } |
|
85 |
|
86 if(original[i] != ' ') |
|
87 last_char = i; |
|
88 } |
|
89 /* Final de linia */ |
|
90 strpart(word, original, tabstops[this_tab_stop], last_char); |
|
91 printf("%s", word); |
|
92 } |
|
93 else |
|
94 { |
|
95 this_tab_stop=1; |
|
96 output_column=1; |
|
97 for(i=0; i<strlen(original); i++) |
|
98 { |
|
99 if(original[i]=='\t' && this_tab_stop < num_tab_stops) |
|
100 { |
|
101 for(j=output_column; j<=tabstops[this_tab_stop]; j++) |
|
102 { |
|
103 putchar(' '); |
|
104 } |
|
105 output_column = tabstops[this_tab_stop]; |
|
106 this_tab_stop++; |
|
107 } |
|
108 else |
|
109 putchar(original[i]); |
|
110 |
|
111 output_column++; |
|
112 } |
|
113 } |
|
114 |
|
115 } |
|
116 |
|
117 exit(0); |
|
118 } |