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