unexpand.c revision 131054
117680Spst/*- 239297Sfenner * Copyright (c) 1980, 1993 317680Spst * The Regents of the University of California. All rights reserved. 417680Spst * 517680Spst * Redistribution and use in source and binary forms, with or without 617680Spst * modification, are permitted provided that the following conditions 717680Spst * are met: 817680Spst * 1. Redistributions of source code must retain the above copyright 917680Spst * notice, this list of conditions and the following disclaimer. 1017680Spst * 2. Redistributions in binary form must reproduce the above copyright 1117680Spst * notice, this list of conditions and the following disclaimer in the 1217680Spst * documentation and/or other materials provided with the distribution. 1317680Spst * 3. All advertising materials mentioning features or use of this software 1417680Spst * must display the following acknowledgement: 1517680Spst * This product includes software developed by the University of 1617680Spst * California, Berkeley and its contributors. 1717680Spst * 4. Neither the name of the University nor the names of its contributors 1817680Spst * may be used to endorse or promote products derived from this software 1917680Spst * without specific prior written permission. 2017680Spst * 21147899Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2217680Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2317680Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2417680Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25147899Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2617680Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2717680Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2817680Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2917680Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3017680Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3117680Spst * SUCH DAMAGE. 3217680Spst */ 3375115Sfenner 3417680Spst#include <sys/cdefs.h> 3517680Spst 3617680Spst__FBSDID("$FreeBSD: head/usr.bin/unexpand/unexpand.c 131054 2004-06-24 13:48:28Z tjr $"); 3717680Spst 3817680Spst#ifndef lint 3917680Spststatic const char copyright[] = 4017680Spst"@(#) Copyright (c) 1980, 1993\n\ 4117680Spst The Regents of the University of California. All rights reserved.\n"; 4217680Spst#endif 4317680Spst 4417680Spst#ifndef lint 4517680Spststatic const char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93"; 4675115Sfenner#endif 4775115Sfenner 4875115Sfenner/* 4975115Sfenner * unexpand - put tabs into a file replacing blanks 5017680Spst */ 5175115Sfenner#include <ctype.h> 5275115Sfenner#include <err.h> 5317680Spst#include <limits.h> 5417680Spst#include <locale.h> 5517680Spst#include <stdio.h> 5617680Spst#include <stdlib.h> 5717680Spst#include <string.h> 5817680Spst#include <unistd.h> 5917680Spst#include <wchar.h> 6098524Sfenner#include <wctype.h> 6117680Spst 6217680Spstint all; 6398524Sfennerint nstops; 6417680Spstint tabstops[100]; 6517680Spst 6617680Spststatic void getstops(const char *); 6717680Spststatic void usage(void); 6817680Spststatic int tabify(const char *); 6917680Spst 7017680Spstint 7117680Spstmain(int argc, char *argv[]) 7217680Spst{ 7317680Spst int ch, failed; 7417680Spst char *filename; 7517680Spst 7617680Spst setlocale(LC_CTYPE, ""); 7717680Spst 7817680Spst nstops = 1; 7917680Spst tabstops[0] = 8; 8017680Spst while ((ch = getopt(argc, argv, "at:")) != -1) { 8117680Spst switch (ch) { 8217680Spst case 'a': /* Un-expand all spaces, not just leading. */ 8317680Spst all = 1; 8417680Spst break; 8517680Spst case 't': /* Specify tab list, implies -a. */ 8617680Spst getstops(optarg); 8798524Sfenner all = 1; 8898524Sfenner break; 8998524Sfenner default: 9098524Sfenner usage(); 9198524Sfenner /*NOTREACHED*/ 9298524Sfenner } 9317680Spst } 9417680Spst argc -= optind; 9517680Spst argv += optind; 9698524Sfenner 97127668Sbms failed = 0; 98127668Sbms if (argc == 0) 99127668Sbms failed |= tabify("stdin"); 100127668Sbms else { 101127668Sbms while ((filename = *argv++) != NULL) { 102127668Sbms if (freopen(filename, "r", stdin) == NULL) { 103127668Sbms warn("%s", filename); 104127668Sbms failed = 1; 105127668Sbms } else 106127668Sbms failed |= tabify(filename); 107127668Sbms } 108127668Sbms } 109127668Sbms exit(failed != 0); 110127668Sbms} 111127668Sbms 112127668Sbmsstatic void 113127668Sbmsusage(void) 114127668Sbms{ 115127668Sbms fprintf(stderr, "usage: unexpand [-a] [-t tablist] [file ...]\n"); 116 exit(1); 117} 118 119static int 120tabify(const char *curfile) 121{ 122 int dcol, doneline, limit, n, ocol, width; 123 wint_t ch; 124 125 limit = nstops == 1 ? INT_MAX : tabstops[nstops - 1] - 1; 126 127 doneline = ocol = dcol = 0; 128 while ((ch = getwchar()) != WEOF) { 129 if (ch == ' ' && !doneline) { 130 if (++dcol >= limit) 131 doneline = 1; 132 continue; 133 } else if (ch == '\t') { 134 if (nstops == 1) { 135 dcol = (1 + dcol / tabstops[0]) * 136 tabstops[0]; 137 continue; 138 } else { 139 for (n = 0; tabstops[n] - 1 < dcol && 140 n < nstops; n++) 141 ; 142 if (n < nstops - 1 && tabstops[n] - 1 < limit) { 143 dcol = tabstops[n]; 144 continue; 145 } 146 doneline = 1; 147 } 148 } 149 150 /* Output maximal number of tabs. */ 151 if (nstops == 1) { 152 while (((ocol + tabstops[0]) / tabstops[0]) 153 <= (dcol / tabstops[0])) { 154 if (dcol - ocol < 2) 155 break; 156 putwchar('\t'); 157 ocol = (1 + ocol / tabstops[0]) * 158 tabstops[0]; 159 } 160 } else { 161 for (n = 0; tabstops[n] - 1 < ocol && n < nstops; n++) 162 ; 163 while (ocol < dcol && n < nstops && ocol < limit) { 164 putwchar('\t'); 165 ocol = tabstops[n++]; 166 } 167 } 168 169 /* Then spaces. */ 170 while (ocol < dcol && ocol < limit) { 171 putwchar(' '); 172 ocol++; 173 } 174 175 if (ch == '\b') { 176 putwchar('\b'); 177 if (ocol > 0) 178 ocol--, dcol--; 179 } else if (ch == '\n') { 180 putwchar('\n'); 181 doneline = ocol = dcol = 0; 182 continue; 183 } else if (ch != ' ' || dcol > limit) { 184 putwchar(ch); 185 if ((width = wcwidth(ch)) > 0) 186 ocol += width, dcol += width; 187 } 188 189 /* 190 * Only processing leading blanks or we've gone past the 191 * last tab stop. Emit remainder of this line unchanged. 192 */ 193 if (!all || dcol >= limit) { 194 while ((ch = getwchar()) != '\n' && ch != WEOF) 195 putwchar(ch); 196 if (ch == '\n') 197 putwchar('\n'); 198 doneline = ocol = dcol = 0; 199 } 200 } 201 if (ferror(stdin)) { 202 warn("%s", curfile); 203 return (1); 204 } 205 return (0); 206} 207 208static void 209getstops(const char *cp) 210{ 211 int i; 212 213 nstops = 0; 214 for (;;) { 215 i = 0; 216 while (*cp >= '0' && *cp <= '9') 217 i = i * 10 + *cp++ - '0'; 218 if (i <= 0) 219 errx(1, "bad tab stop spec"); 220 if (nstops > 0 && i <= tabstops[nstops-1]) 221 errx(1, "bad tab stop spec"); 222 if (nstops == sizeof(tabstops) / sizeof(*tabstops)) 223 errx(1, "too many tab stops"); 224 tabstops[nstops++] = i; 225 if (*cp == 0) 226 break; 227 if (*cp != ',' && !isblank((unsigned char)*cp)) 228 errx(1, "bad tab stop spec"); 229 cp++; 230 } 231} 232