kernel: scheduling fix for ARM
[minix.git] / commands / expand / expand.c
blobc7d9613498d775106b301d391ffa7b788442aa6f
1 /* expand - expand tabs to spaces Author: Terrence W. Holm */
3 /* Usage: expand [ -tab1,tab2,tab3,... ] [ file ... ] */
5 #include <stddef.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdio.h>
10 #define MAX_TABS 32
12 int column = 0; /* Current column, retained between files */
14 int main(int argc, char **argv);
15 void Expand(FILE *f, int tab_index, int tabs []);
17 int main(argc, argv)
18 int argc;
19 char *argv[];
21 int tabs[MAX_TABS];
22 int tab_index = 0; /* Default one tab */
23 int i;
24 FILE *f;
26 tabs[0] = 8; /* Default tab stop */
28 if (argc > 1 && argv[1][0] == '-') {
29 char *p = argv[1];
30 int last_tab_stop = 0;
32 for (tab_index = 0; tab_index < MAX_TABS; ++tab_index) {
33 if ((tabs[tab_index] = atoi(p + 1)) <= last_tab_stop) {
34 fprintf(stderr, "Bad tab stop spec\n");
35 exit(1);
37 last_tab_stop = tabs[tab_index];
39 if ((p = strchr(p + 1, ',')) == NULL) break;
42 --argc;
43 ++argv;
45 if (argc == 1)
46 Expand(stdin, tab_index, tabs);
47 else
48 for (i = 1; i < argc; ++i) {
49 if ((f = fopen(argv[i], "r")) == NULL) {
50 perror(argv[i]);
51 exit(1);
53 Expand(f, tab_index, tabs);
54 fclose(f);
57 return(0);
61 void Expand(f, tab_index, tabs)
62 FILE *f;
63 int tab_index;
64 int tabs[];
66 int next;
67 int c;
68 int i;
70 while ((c = getc(f)) != EOF) {
71 if (c == '\t') {
72 if (tab_index == 0)
73 next = (column / tabs[0] + 1) * tabs[0];
74 else {
75 for (i = 0; i <= tab_index && tabs[i] <= column; ++i);
77 if (i > tab_index)
78 next = column + 1;
79 else
80 next = tabs[i];
83 do {
84 ++column;
85 putchar(' ');
86 } while (column < next);
88 continue;
90 if (c == '\b')
91 column = column > 0 ? column - 1 : 0;
92 else if (c == '\n' || c == '\r')
93 column = 0;
94 else
95 ++column;
97 putchar(c);