source: trunk/minix/commands/simple/expand.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.7 KB
Line 
1/* expand - expand tabs to spaces Author: Terrence W. Holm */
2
3/* Usage: expand [ -tab1,tab2,tab3,... ] [ file ... ] */
4
5#include <stddef.h>
6#include <string.h>
7#include <stdlib.h>
8#include <stdio.h>
9
10#define MAX_TABS 32
11
12int column = 0; /* Current column, retained between files */
13
14_PROTOTYPE(int main, (int argc, char **argv));
15_PROTOTYPE(void Expand, (FILE *f, int tab_index, int tabs []));
16
17int main(argc, argv)
18int argc;
19char *argv[];
20{
21 int tabs[MAX_TABS];
22 int tab_index = 0; /* Default one tab */
23 int i;
24 FILE *f;
25
26 tabs[0] = 8; /* Default tab stop */
27
28 if (argc > 1 && argv[1][0] == '-') {
29 char *p = argv[1];
30 int last_tab_stop = 0;
31
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);
36 }
37 last_tab_stop = tabs[tab_index];
38
39 if ((p = strchr(p + 1, ',')) == NULL) break;
40 }
41
42 --argc;
43 ++argv;
44 }
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);
52 }
53 Expand(f, tab_index, tabs);
54 fclose(f);
55 }
56
57 return(0);
58}
59
60
61void Expand(f, tab_index, tabs)
62FILE *f;
63int tab_index;
64int tabs[];
65{
66 int next;
67 int c;
68 int i;
69
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);
76
77 if (i > tab_index)
78 next = column + 1;
79 else
80 next = tabs[i];
81 }
82
83 do {
84 ++column;
85 putchar(' ');
86 } while (column < next);
87
88 continue;
89 }
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;
96
97 putchar(c);
98 }
99}
Note: See TracBrowser for help on using the repository browser.