1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1980, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 06/09/93"; 16 #endif /* not lint */ 17 18 #include <stdio.h> 19 /* 20 * expand - expand tabs to equivalent spaces 21 */ 22 int nstops; 23 int tabstops[100]; 24 25 main(argc, argv) 26 int argc; 27 char *argv[]; 28 { 29 register int c, column; 30 register int n; 31 32 argc--, argv++; 33 do { 34 while (argc > 0 && argv[0][0] == '-') { 35 getstops(argv[0]); 36 argc--, argv++; 37 } 38 if (argc > 0) { 39 if (freopen(argv[0], "r", stdin) == NULL) { 40 perror(argv[0]); 41 exit(1); 42 } 43 argc--, argv++; 44 } 45 column = 0; 46 for (;;) { 47 c = getc(stdin); 48 if (c == -1) 49 break; 50 switch (c) { 51 52 case '\t': 53 if (nstops == 0) { 54 do { 55 putchar(' '); 56 column++; 57 } while (column & 07); 58 continue; 59 } 60 if (nstops == 1) { 61 do { 62 putchar(' '); 63 column++; 64 } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1)); 65 continue; 66 } 67 for (n = 0; n < nstops; n++) 68 if (tabstops[n] > column) 69 break; 70 if (n == nstops) { 71 putchar(' '); 72 column++; 73 continue; 74 } 75 while (column < tabstops[n]) { 76 putchar(' '); 77 column++; 78 } 79 continue; 80 81 case '\b': 82 if (column) 83 column--; 84 putchar('\b'); 85 continue; 86 87 default: 88 putchar(c); 89 column++; 90 continue; 91 92 case '\n': 93 putchar(c); 94 column = 0; 95 continue; 96 } 97 } 98 } while (argc > 0); 99 exit(0); 100 } 101 102 getstops(cp) 103 register char *cp; 104 { 105 register int i; 106 107 nstops = 0; 108 cp++; 109 for (;;) { 110 i = 0; 111 while (*cp >= '0' && *cp <= '9') 112 i = i * 10 + *cp++ - '0'; 113 if (i <= 0 || i > 256) { 114 bad: 115 fprintf(stderr, "Bad tab stop spec\n"); 116 exit(1); 117 } 118 if (nstops > 0 && i <= tabstops[nstops-1]) 119 goto bad; 120 tabstops[nstops++] = i; 121 if (*cp == 0) 122 break; 123 if (*cp++ != ',') 124 goto bad; 125 } 126 } 127