1 /*- 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 36 __FBSDID("$FreeBSD$"); 37 38 #ifndef lint 39 static const char copyright[] = 40 "@(#) Copyright (c) 1980, 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif 43 44 #ifndef lint 45 static const char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93"; 46 #endif 47 48 /* 49 * unexpand - put tabs into a file replacing blanks 50 */ 51 #include <err.h> 52 #include <limits.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 int all; 59 int nstops; 60 int tabstops[100]; 61 62 static void getstops(const char *); 63 static void usage(void); 64 static void tabify(void); 65 66 int 67 main(argc, argv) 68 int argc; 69 char *argv[]; 70 { 71 int ch, failed; 72 char *filename; 73 74 nstops = 1; 75 tabstops[0] = 8; 76 while ((ch = getopt(argc, argv, "at:")) != -1) { 77 switch (ch) { 78 case 'a': /* Un-expand all spaces, not just leading. */ 79 all = 1; 80 break; 81 case 't': /* Specify tab list, implies -a. */ 82 getstops(optarg); 83 all = 1; 84 break; 85 default: 86 usage(); 87 /*NOTREACHED*/ 88 } 89 } 90 argc -= optind; 91 argv += optind; 92 93 failed = 0; 94 if (argc == 0) 95 tabify(); 96 else { 97 while ((filename = *argv++) != NULL) { 98 if (freopen(filename, "r", stdin) == NULL) { 99 warn("%s", filename); 100 failed++; 101 } else 102 tabify(); 103 } 104 } 105 exit(failed != 0); 106 } 107 108 static void 109 usage() 110 { 111 fprintf(stderr, "usage: unexpand [-a] [-t tablist] [file ...]\n"); 112 exit(1); 113 } 114 115 static void 116 tabify() 117 { 118 int ch, dcol, doneline, limit, n, ocol; 119 120 limit = nstops == 1 ? INT_MAX : tabstops[nstops - 1] - 1; 121 122 doneline = ocol = dcol = 0; 123 while ((ch = getchar()) != EOF) { 124 if (ch == ' ' && !doneline) { 125 if (++dcol >= limit) 126 doneline = 1; 127 continue; 128 } else if (ch == '\t') { 129 if (nstops == 1) { 130 dcol = (1 + dcol / tabstops[0]) * 131 tabstops[0]; 132 continue; 133 } else { 134 for (n = 0; tabstops[n] - 1 < dcol && 135 n < nstops; n++) 136 ; 137 if (n < nstops - 1 && tabstops[n] - 1 < limit) { 138 dcol = tabstops[n]; 139 continue; 140 } 141 doneline = 1; 142 } 143 } 144 145 /* Output maximal number of tabs. */ 146 if (nstops == 1) { 147 while (((ocol + tabstops[0]) / tabstops[0]) 148 <= (dcol / tabstops[0])) { 149 if (dcol - ocol < 2) 150 break; 151 putchar('\t'); 152 ocol = (1 + ocol / tabstops[0]) * 153 tabstops[0]; 154 } 155 } else { 156 for (n = 0; tabstops[n] - 1 < ocol && n < nstops; n++) 157 ; 158 while (ocol < dcol && n < nstops && ocol < limit) { 159 putchar('\t'); 160 ocol = tabstops[n++]; 161 } 162 } 163 164 /* Then spaces. */ 165 while (ocol < dcol && ocol < limit) { 166 putchar(' '); 167 ocol++; 168 } 169 170 if (ch == '\b') { 171 putchar('\b'); 172 if (ocol > 0) 173 ocol--, dcol--; 174 } else if (ch == '\n') { 175 putchar('\n'); 176 doneline = ocol = dcol = 0; 177 } else if (ch != ' ' || dcol > limit) { 178 putchar(ch); 179 ocol++, dcol++; 180 } 181 182 /* 183 * Only processing leading blanks or we've gone past the 184 * last tab stop. Emit remainder of this line unchanged. 185 */ 186 if (!all || dcol >= limit) { 187 while ((ch = getchar()) != '\n' && ch != EOF) 188 putchar(ch); 189 if (ch == '\n') 190 putchar('\n'); 191 doneline = ocol = dcol = 0; 192 } 193 } 194 } 195 196 static void 197 getstops(cp) 198 const char *cp; 199 { 200 int i; 201 202 nstops = 0; 203 for (;;) { 204 i = 0; 205 while (*cp >= '0' && *cp <= '9') 206 i = i * 10 + *cp++ - '0'; 207 if (i <= 0) 208 errx(1, "bad tab stop spec"); 209 if (nstops > 0 && i <= tabstops[nstops-1]) 210 errx(1, "bad tab stop spec"); 211 if (nstops == sizeof(tabstops) / sizeof(*tabstops)) 212 errx(1, "too many tab stops"); 213 tabstops[nstops++] = i; 214 if (*cp == 0) 215 break; 216 if (*cp != ',' && *cp != ' ') 217 errx(1, "bad tab stop spec"); 218 cp++; 219 } 220 } 221