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