1 /* $NetBSD: unexpand.c,v 1.14 2008/12/21 02:33:13 christos 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[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93"; 41 #endif 42 __RCSID("$NetBSD: unexpand.c,v 1.14 2008/12/21 02:33:13 christos Exp $"); 43 #endif /* not lint */ 44 45 /* 46 * unexpand - put tabs into a file replacing blanks 47 */ 48 #include <limits.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <errno.h> 54 #include <err.h> 55 #include <util.h> 56 57 58 #define DSTOP 8 59 static int all; 60 static size_t nstops; 61 static size_t maxstops; 62 static size_t *tabstops; 63 64 static void tabify(const char *, size_t); 65 static void usage(void) __attribute__((__noreturn__)); 66 67 static void 68 usage(void) 69 { 70 (void)fprintf(stderr, "Usage: %s [-a] [-t tabstop] [file ...]\n", 71 getprogname()); 72 exit(EXIT_FAILURE); 73 } 74 75 int 76 main(int argc, char **argv) 77 { 78 int c; 79 char *ep, *tab; 80 char *line; 81 size_t len; 82 unsigned long i; 83 84 setprogname(argv[0]); 85 86 while ((c = getopt(argc, argv, "at:")) != -1) { 87 switch (c) { 88 case 'a': 89 if (nstops) 90 usage(); 91 all++; 92 break; 93 case 't': 94 if (all) 95 usage(); 96 while ((tab = strsep(&optarg, ", \t")) != NULL) { 97 if (*tab == '\0') 98 continue; 99 errno = 0; 100 i = strtoul(tab, &ep, 0); 101 if (*ep || (errno == ERANGE && i == ULONG_MAX)) 102 errx(EXIT_FAILURE, 103 "Invalid tabstop `%s'", tab); 104 if (nstops >= maxstops) { 105 maxstops += 20; 106 tabstops = erealloc(tabstops, maxstops); 107 } 108 if (nstops && tabstops[nstops - 1] >= (size_t)i) 109 errx(EXIT_FAILURE, 110 "Bad tabstop spec `%s', must be " 111 "greater than the previous `%zu'", 112 tab, tabstops[nstops - 1]); 113 tabstops[nstops++] = i; 114 } 115 break; 116 case '?': 117 default: 118 usage(); 119 } 120 } 121 argc -= optind; 122 argv += optind; 123 124 for (i = 0; i < nstops; i++) 125 fprintf(stderr, "%lu %zu\n", i, tabstops[i]); 126 127 do { 128 if (argc > 0) { 129 if (freopen(argv[0], "r", stdin) == NULL) 130 err(EXIT_FAILURE, "Cannot open `%s'", argv[0]); 131 argc--, argv++; 132 } 133 while ((line = fgetln(stdin, &len)) != NULL) 134 tabify(line, len); 135 } while (argc > 0); 136 return EXIT_SUCCESS; 137 } 138 139 static void 140 tabify(const char *line, size_t len) 141 { 142 const char *e, *p; 143 size_t dcol, ocol, limit, n; 144 145 dcol = ocol = 0; 146 limit = nstops == 0 ? UINT_MAX : tabstops[nstops - 1] - 1; 147 e = line + len; 148 for (p = line; p < e; p++) { 149 if (*p == ' ') { 150 dcol++; 151 continue; 152 } else if (*p == '\t') { 153 if (nstops == 0) { 154 dcol = (1 + dcol / DSTOP) * DSTOP; 155 continue; 156 } else { 157 for (n = 0; tabstops[n] - 1 < dcol && 158 n < nstops; n++) 159 continue; 160 if (n < nstops - 1 && tabstops[n] - 1 < limit) { 161 dcol = tabstops[n]; 162 continue; 163 } 164 } 165 } 166 167 /* Output our tabs */ 168 if (nstops == 0) { 169 while (((ocol + DSTOP) / DSTOP) <= (dcol / DSTOP)) { 170 if (dcol - ocol < 2) 171 break; 172 if (putchar('\t') == EOF) 173 goto out; 174 ocol = (1 + ocol / DSTOP) * DSTOP; 175 } 176 } else { 177 for (n = 0; tabstops[n] <= ocol && n < nstops; n++) 178 continue; 179 while (tabstops[n] <= dcol && ocol < dcol && 180 n < nstops && ocol < limit) { 181 if (putchar('\t') == EOF) 182 goto out; 183 ocol = tabstops[n++]; 184 } 185 } 186 187 /* Output remaining spaces */ 188 while (ocol < dcol && ocol < limit) { 189 if (putchar(' ') == EOF) 190 goto out; 191 ocol++; 192 } 193 194 /* Output our char */ 195 if (putchar(*p) == EOF) 196 goto out; 197 if (*p == '\b') { 198 if (ocol > 0) { 199 ocol--; 200 dcol--; 201 } 202 } else { 203 ocol++; 204 dcol++; 205 } 206 207 /* Output remainder of line */ 208 if (!all || dcol >= limit) { 209 for (p++; p < e; p++) 210 if (putchar(*p) == EOF) 211 goto out; 212 return; 213 } 214 } 215 return; 216 out: 217 err(EXIT_FAILURE, "write failed"); 218 } 219