1/* $NetBSD: expand.c,v 1.14 2016/09/05 00:40:28 sevan 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 40static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93"; 41#endif 42__RCSID("$NetBSD: expand.c,v 1.14 2016/09/05 00:40:28 sevan 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 */ 54size_t nstops; 55size_t tabstops[100]; 56 57static void getstops(const char *); 58static void usage(void) __dead; 59 60int 61main(int argc, char *argv[]) 62{ 63 int c; 64 size_t n, column; 65 66 setprogname(argv[0]); 67 68 /* handle obsolete syntax */ 69 while (argc > 1 && 70 argv[1][0] == '-' && isdigit((unsigned char)argv[1][1])) { 71 getstops(&argv[1][1]); 72 argc--; argv++; 73 } 74 75 while ((c = getopt (argc, argv, "t:")) != -1) { 76 switch (c) { 77 case 't': 78 getstops(optarg); 79 break; 80 case '?': 81 default: 82 usage(); 83 /* NOTREACHED */ 84 } 85 } 86 argc -= optind; 87 argv += optind; 88 89 do { 90 if (argc > 0) { 91 if (freopen(argv[0], "r", stdin) == NULL) 92 err(EXIT_FAILURE, "Cannot open `%s'", argv[0]); 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]) 111 != (tabstops[0] - 1)); 112 continue; 113 } 114 for (n = 0; n < nstops; n++) 115 if (tabstops[n] > column) 116 break; 117 if (n == nstops) { 118 putchar(' '); 119 column++; 120 continue; 121 } 122 while (column < tabstops[n]) { 123 putchar(' '); 124 column++; 125 } 126 continue; 127 128 case '\b': 129 if (column) 130 column--; 131 putchar('\b'); 132 continue; 133 134 default: 135 putchar(c); 136 column++; 137 continue; 138 139 case '\n': 140 putchar(c); 141 column = 0; 142 continue; 143 } 144 } 145 } while (argc > 0); 146 return EXIT_SUCCESS; 147} 148 149static void 150getstops(const char *spec) 151{ 152 int i; 153 const char *cp = spec; 154 155 nstops = 0; 156 for (;;) { 157 i = 0; 158 while (*cp >= '0' && *cp <= '9') 159 i = i * 10 + *cp++ - '0'; 160 if (i <= 0 || i > 256) 161 errx(EXIT_FAILURE, "Too large tab stop spec `%d'", i); 162 if (nstops > 0 && (size_t)i <= tabstops[nstops-1]) 163 errx(EXIT_FAILURE, "Out of order tabstop spec `%d'", i); 164 if (nstops == sizeof(tabstops) / sizeof(tabstops[0]) - 1) 165 errx(EXIT_FAILURE, "Too many tabstops"); 166 tabstops[nstops++] = i; 167 if (*cp == '\0') 168 break; 169 if (*cp != ',' && *cp != ' ') 170 errx(EXIT_FAILURE, "Illegal tab stop spec `%s'", spec); 171 cp++; 172 } 173} 174 175static void 176usage(void) 177{ 178 179 (void)fprintf(stderr, "Usage: %s [-t tablist] [file ...]\n", 180 getprogname()); 181 exit(EXIT_FAILURE); 182} 183