aboutsummaryrefslogtreecommitdiff
path: root/5.11.c
blob: 4834d1b8d5f29dea42b943840909887b1e422ad3 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define DEFTAB 8
#define TRUE 1
#define FALSE 0
#define MAXSTOPS 100

int stops[MAXSTOPS];

void detab(int *stops, int limit);
void entab(int *stops, int limit);

int main(int argc, char **argv)
{
        int entab_bool = FALSE;
        int detab_bool = FALSE;
        int stop_position = 0;

        while (--argc > 0 && **++argv == '-') {
                while (*++*argv) {
                        switch (**argv) {
			case 'e' :
				entab_bool = TRUE;
				break;
			case 'd' :
				detab_bool = TRUE;
				break;
			case 's' :
				if (*++*argv != '=')
					break;
				
				while (*++*argv && stop_position < MAXSTOPS) {
					stops[stop_position++] = atoi(*argv);
					printf("loooop -- %d\n", stop_position);

					while (isdigit(**argv))
						++*argv;

					if (**argv == '\0')
						--*argv;
				}
				break;
                        }
                }
        }

        stops[stop_position] = DEFTAB;

        if (entab_bool ^ detab_bool) {
                if (entab_bool)
                        entab(stops, stop_position);
                else if (detab_bool)
                        detab(stops, stop_position);
        } else {
                printf("usage: tab -[ed] -s=s1,s2,s3,...,s3\n");
        }

        return 0;
}

void detab(int *stops, int limit)
{
        char input;
        int space, count = 0, stop_position = 0;

        while((input = getchar()) != EOF) {
                switch (input) {
		case '\t' :
			for (space = count%stops[stop_position]; space <
			  stops[stop_position]; space++)
				putchar(' ');
			count = 0;

			if (stop_position < limit)
				stop_position++;

			break;
		case '\n' :  
			count = 0;
			break;
		default :
			putchar(input);
			count++;
			break;
			
                }
        }
}

void entab(int *stops, int limit)
{
        char input;
        int spaces = 0, stop_position = 0, count = 0;

        while((input = getchar()) != EOF) {
                switch (input) {
                        case ' ' :
                                spaces++;
                                break;
                        case '\n' :
                                count = 0;
                                putchar('\n');
                                break;
                        default :
                                // add spaces misssed by the first tab
                                if (spaces >= stops[stop_position])
                                        spaces += count%stops[stop_position];

                                while (spaces/stops[stop_position]) {
                                        spaces = spaces - stops[stop_position]; 

                                        putchar('\t');

                                        if (stop_position < limit)
                                                stop_position++;
                                }

                                while (spaces--)
                                        putchar(' ');

                                spaces = 0;
                                count++;
                                putchar(input);
                                break;
                }
        }
}