1 /* 2 * Copyright (c) 1980 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 char copyright[] = 36 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\ 37 All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 /*static char sccsid[] = "from: @(#)expand.c 5.3 (Berkeley) 6/1/90";*/ 42 static char rcsid[] = "$Id: expand.c,v 1.4 1993/10/13 18:34:09 jtc Exp $"; 43 #endif /* not lint */ 44 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <ctype.h> 48 #include <unistd.h> 49 50 /* 51 * expand - expand tabs to equivalent spaces 52 */ 53 int nstops; 54 int tabstops[100]; 55 56 static void getstops(); 57 static void usage(); 58 59 int 60 main(argc, argv) 61 int argc; 62 char *argv[]; 63 { 64 register int c, column; 65 register int n; 66 67 /* handle obsolete syntax */ 68 while (argc > 1 && argv[1][0] && isdigit(argv[1][1])) { 69 getstops(&argv[1][1]); 70 argc--; argv++; 71 } 72 73 while ((c = getopt (argc, argv, "t:")) != -1) { 74 switch (c) { 75 case 't': 76 getstops(optarg); 77 break; 78 case '?': 79 default: 80 usage(); 81 /* NOTREACHED */ 82 } 83 } 84 argc -= optind; 85 argv += optind; 86 87 do { 88 if (argc > 0) { 89 if (freopen(argv[0], "r", stdin) == NULL) { 90 perror(argv[0]); 91 exit(1); 92 } 93 argc--, argv++; 94 } 95 column = 0; 96 while ((c = getchar()) != EOF) { 97 switch (c) { 98 case '\t': 99 if (nstops == 0) { 100 do { 101 putchar(' '); 102 column++; 103 } while (column & 07); 104 continue; 105 } 106 if (nstops == 1) { 107 do { 108 putchar(' '); 109 column++; 110 } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1)); 111 continue; 112 } 113 for (n = 0; n < nstops; n++) 114 if (tabstops[n] > column) 115 break; 116 if (n == nstops) { 117 putchar(' '); 118 column++; 119 continue; 120 } 121 while (column < tabstops[n]) { 122 putchar(' '); 123 column++; 124 } 125 continue; 126 127 case '\b': 128 if (column) 129 column--; 130 putchar('\b'); 131 continue; 132 133 default: 134 putchar(c); 135 column++; 136 continue; 137 138 case '\n': 139 putchar(c); 140 column = 0; 141 continue; 142 } 143 } 144 } while (argc > 0); 145 exit(0); 146 } 147 148 static void 149 getstops(cp) 150 register char *cp; 151 { 152 register int i; 153 154 nstops = 0; 155 for (;;) { 156 i = 0; 157 while (*cp >= '0' && *cp <= '9') 158 i = i * 10 + *cp++ - '0'; 159 if (i <= 0 || i > 256) { 160 bad: 161 fprintf(stderr, "Bad tab stop spec\n"); 162 exit(1); 163 } 164 if (nstops > 0 && i <= tabstops[nstops-1]) 165 goto bad; 166 tabstops[nstops++] = i; 167 if (*cp == 0) 168 break; 169 if (*cp != ',' && *cp != ' ') 170 goto bad; 171 cp++; 172 } 173 } 174 175 static void 176 usage() 177 { 178 (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n"); 179 exit(1); 180 } 181