1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1980, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #endif /* not lint */ 40 #include <sys/cdefs.h> 41 #include <ctype.h> 42 #include <err.h> 43 #include <locale.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <unistd.h> 47 #include <wchar.h> 48 #include <wctype.h> 49 50 /* 51 * expand - expand tabs to equivalent spaces 52 */ 53 static int nstops; 54 static int tabstops[100]; 55 56 static void getstops(char *); 57 static void usage(void) __dead2; 58 59 int 60 main(int argc, char *argv[]) 61 { 62 const char *curfile; 63 wint_t wc; 64 int c, column; 65 int n; 66 int rval; 67 int width; 68 69 setlocale(LC_CTYPE, ""); 70 71 /* handle obsolete syntax */ 72 while (argc > 1 && argv[1][0] == '-' && 73 isdigit((unsigned char)argv[1][1])) { 74 getstops(&argv[1][1]); 75 argc--; argv++; 76 } 77 78 while ((c = getopt (argc, argv, "t:")) != -1) { 79 switch (c) { 80 case 't': 81 getstops(optarg); 82 break; 83 case '?': 84 default: 85 usage(); 86 /* NOTREACHED */ 87 } 88 } 89 argc -= optind; 90 argv += optind; 91 92 rval = 0; 93 do { 94 if (argc > 0) { 95 if (freopen(argv[0], "r", stdin) == NULL) { 96 warn("%s", argv[0]); 97 rval = 1; 98 argc--, argv++; 99 continue; 100 } 101 curfile = argv[0]; 102 argc--, argv++; 103 } else 104 curfile = "stdin"; 105 column = 0; 106 while ((wc = getwchar()) != WEOF) { 107 switch (wc) { 108 case '\t': 109 if (nstops == 0) { 110 do { 111 putwchar(' '); 112 column++; 113 } while (column & 07); 114 continue; 115 } 116 if (nstops == 1) { 117 do { 118 putwchar(' '); 119 column++; 120 } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1)); 121 continue; 122 } 123 for (n = 0; n < nstops; n++) 124 if (tabstops[n] > column) 125 break; 126 if (n == nstops) { 127 putwchar(' '); 128 column++; 129 continue; 130 } 131 while (column < tabstops[n]) { 132 putwchar(' '); 133 column++; 134 } 135 continue; 136 137 case '\b': 138 if (column) 139 column--; 140 putwchar('\b'); 141 continue; 142 143 default: 144 putwchar(wc); 145 if ((width = wcwidth(wc)) > 0) 146 column += width; 147 continue; 148 149 case '\n': 150 putwchar(wc); 151 column = 0; 152 continue; 153 } 154 } 155 if (ferror(stdin)) { 156 warn("%s", curfile); 157 rval = 1; 158 } 159 } while (argc > 0); 160 exit(rval); 161 } 162 163 static void 164 getstops(char *cp) 165 { 166 int i; 167 168 nstops = 0; 169 for (;;) { 170 i = 0; 171 while (*cp >= '0' && *cp <= '9') 172 i = i * 10 + *cp++ - '0'; 173 if (i <= 0) 174 errx(1, "bad tab stop spec"); 175 if (nstops > 0 && i <= tabstops[nstops-1]) 176 errx(1, "bad tab stop spec"); 177 if (nstops == sizeof(tabstops) / sizeof(*tabstops)) 178 errx(1, "too many tab stops"); 179 tabstops[nstops++] = i; 180 if (*cp == 0) 181 break; 182 if (*cp != ',' && !isblank((unsigned char)*cp)) 183 errx(1, "bad tab stop spec"); 184 cp++; 185 } 186 } 187 188 static void 189 usage(void) 190 { 191 (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n"); 192 exit(1); 193 } 194