1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-12-11 23:18:25 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
1997-08-20 11:05:28 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
2001-12-11 23:18:25 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#ifndef lint
|
2001-12-11 23:18:25 +00:00
|
|
|
static const char sccsid[] = "@(#)unexpand.c 8.1 (Berkeley) 6/6/93";
|
1997-08-20 11:05:28 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* unexpand - put tabs into a file replacing blanks
|
|
|
|
*/
|
2002-06-15 10:16:39 +00:00
|
|
|
#include <ctype.h>
|
1997-08-20 11:05:28 +00:00
|
|
|
#include <err.h>
|
2002-04-23 07:15:09 +00:00
|
|
|
#include <limits.h>
|
2002-06-15 10:16:39 +00:00
|
|
|
#include <locale.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
2001-06-24 19:41:18 +00:00
|
|
|
#include <stdlib.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <string.h>
|
2002-04-23 07:15:09 +00:00
|
|
|
#include <unistd.h>
|
2004-06-24 13:48:28 +00:00
|
|
|
#include <wchar.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <wctype.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int all;
|
2002-04-23 07:15:09 +00:00
|
|
|
int nstops;
|
|
|
|
int tabstops[100];
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-23 07:15:09 +00:00
|
|
|
static void getstops(const char *);
|
2002-03-22 01:42:45 +00:00
|
|
|
static void usage(void);
|
2004-06-24 13:48:28 +00:00
|
|
|
static int tabify(const char *);
|
1997-08-20 11:05:28 +00:00
|
|
|
|
1997-08-26 11:08:24 +00:00
|
|
|
int
|
2002-09-04 23:29:10 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-04-25 23:04:52 +00:00
|
|
|
int ch, failed;
|
|
|
|
char *filename;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-06-15 10:16:39 +00:00
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
|
2002-04-23 07:15:09 +00:00
|
|
|
nstops = 1;
|
|
|
|
tabstops[0] = 8;
|
|
|
|
while ((ch = getopt(argc, argv, "at:")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'a': /* Un-expand all spaces, not just leading. */
|
|
|
|
all = 1;
|
|
|
|
break;
|
|
|
|
case 't': /* Specify tab list, implies -a. */
|
|
|
|
getstops(optarg);
|
|
|
|
all = 1;
|
|
|
|
break;
|
|
|
|
default:
|
1997-08-20 11:05:28 +00:00
|
|
|
usage();
|
2002-04-23 07:15:09 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-04-23 07:15:09 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2002-04-25 23:04:52 +00:00
|
|
|
failed = 0;
|
|
|
|
if (argc == 0)
|
2004-06-24 13:48:28 +00:00
|
|
|
failed |= tabify("stdin");
|
2002-04-25 23:04:52 +00:00
|
|
|
else {
|
|
|
|
while ((filename = *argv++) != NULL) {
|
|
|
|
if (freopen(filename, "r", stdin) == NULL) {
|
|
|
|
warn("%s", filename);
|
2004-06-24 13:48:28 +00:00
|
|
|
failed = 1;
|
2002-04-25 23:04:52 +00:00
|
|
|
} else
|
2004-06-24 13:48:28 +00:00
|
|
|
failed |= tabify(filename);
|
2002-04-25 23:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(failed != 0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
1997-08-20 11:05:28 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
usage(void)
|
1997-08-20 11:05:28 +00:00
|
|
|
{
|
2006-10-13 16:22:25 +00:00
|
|
|
fprintf(stderr, "usage: unexpand [-a | -t tablist] [file ...]\n");
|
1997-08-20 11:05:28 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-06-24 13:48:28 +00:00
|
|
|
static int
|
|
|
|
tabify(const char *curfile)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-06-24 13:48:28 +00:00
|
|
|
int dcol, doneline, limit, n, ocol, width;
|
|
|
|
wint_t ch;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-23 07:15:09 +00:00
|
|
|
limit = nstops == 1 ? INT_MAX : tabstops[nstops - 1] - 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-23 07:15:09 +00:00
|
|
|
doneline = ocol = dcol = 0;
|
2004-06-24 13:48:28 +00:00
|
|
|
while ((ch = getwchar()) != WEOF) {
|
2002-04-25 23:04:52 +00:00
|
|
|
if (ch == ' ' && !doneline) {
|
2002-04-23 07:15:09 +00:00
|
|
|
if (++dcol >= limit)
|
|
|
|
doneline = 1;
|
|
|
|
continue;
|
|
|
|
} else if (ch == '\t') {
|
|
|
|
if (nstops == 1) {
|
|
|
|
dcol = (1 + dcol / tabstops[0]) *
|
|
|
|
tabstops[0];
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
for (n = 0; tabstops[n] - 1 < dcol &&
|
|
|
|
n < nstops; n++)
|
|
|
|
;
|
|
|
|
if (n < nstops - 1 && tabstops[n] - 1 < limit) {
|
|
|
|
dcol = tabstops[n];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
doneline = 1;
|
|
|
|
}
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-23 07:15:09 +00:00
|
|
|
/* Output maximal number of tabs. */
|
|
|
|
if (nstops == 1) {
|
|
|
|
while (((ocol + tabstops[0]) / tabstops[0])
|
|
|
|
<= (dcol / tabstops[0])) {
|
|
|
|
if (dcol - ocol < 2)
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2004-06-24 13:48:28 +00:00
|
|
|
putwchar('\t');
|
2002-04-23 07:15:09 +00:00
|
|
|
ocol = (1 + ocol / tabstops[0]) *
|
|
|
|
tabstops[0];
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-04-23 07:15:09 +00:00
|
|
|
} else {
|
|
|
|
for (n = 0; tabstops[n] - 1 < ocol && n < nstops; n++)
|
|
|
|
;
|
|
|
|
while (ocol < dcol && n < nstops && ocol < limit) {
|
2004-06-24 13:48:28 +00:00
|
|
|
putwchar('\t');
|
2002-04-23 07:15:09 +00:00
|
|
|
ocol = tabstops[n++];
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-04-23 07:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then spaces. */
|
|
|
|
while (ocol < dcol && ocol < limit) {
|
2004-06-24 13:48:28 +00:00
|
|
|
putwchar(' ');
|
2002-04-23 07:15:09 +00:00
|
|
|
ocol++;
|
|
|
|
}
|
|
|
|
|
2002-04-25 23:04:52 +00:00
|
|
|
if (ch == '\b') {
|
2004-06-24 13:48:28 +00:00
|
|
|
putwchar('\b');
|
2002-04-25 23:04:52 +00:00
|
|
|
if (ocol > 0)
|
|
|
|
ocol--, dcol--;
|
|
|
|
} else if (ch == '\n') {
|
2004-06-24 13:48:28 +00:00
|
|
|
putwchar('\n');
|
2002-04-25 23:04:52 +00:00
|
|
|
doneline = ocol = dcol = 0;
|
2002-10-04 16:45:25 +00:00
|
|
|
continue;
|
2002-04-25 23:04:52 +00:00
|
|
|
} else if (ch != ' ' || dcol > limit) {
|
2004-06-24 13:48:28 +00:00
|
|
|
putwchar(ch);
|
|
|
|
if ((width = wcwidth(ch)) > 0)
|
|
|
|
ocol += width, dcol += width;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-04-23 07:15:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only processing leading blanks or we've gone past the
|
|
|
|
* last tab stop. Emit remainder of this line unchanged.
|
|
|
|
*/
|
|
|
|
if (!all || dcol >= limit) {
|
2004-06-24 13:48:28 +00:00
|
|
|
while ((ch = getwchar()) != '\n' && ch != WEOF)
|
|
|
|
putwchar(ch);
|
2002-04-23 07:15:09 +00:00
|
|
|
if (ch == '\n')
|
2004-06-24 13:48:28 +00:00
|
|
|
putwchar('\n');
|
2002-04-25 23:04:52 +00:00
|
|
|
doneline = ocol = dcol = 0;
|
2002-04-23 07:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
2004-06-24 13:48:28 +00:00
|
|
|
if (ferror(stdin)) {
|
|
|
|
warn("%s", curfile);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
2002-04-23 07:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
getstops(const char *cp)
|
2002-04-23 07:15:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
nstops = 0;
|
|
|
|
for (;;) {
|
|
|
|
i = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '9')
|
|
|
|
i = i * 10 + *cp++ - '0';
|
|
|
|
if (i <= 0)
|
|
|
|
errx(1, "bad tab stop spec");
|
|
|
|
if (nstops > 0 && i <= tabstops[nstops-1])
|
|
|
|
errx(1, "bad tab stop spec");
|
|
|
|
if (nstops == sizeof(tabstops) / sizeof(*tabstops))
|
|
|
|
errx(1, "too many tab stops");
|
|
|
|
tabstops[nstops++] = i;
|
|
|
|
if (*cp == 0)
|
|
|
|
break;
|
2002-06-15 10:16:39 +00:00
|
|
|
if (*cp != ',' && !isblank((unsigned char)*cp))
|
2002-04-23 07:15:09 +00:00
|
|
|
errx(1, "bad tab stop spec");
|
1994-05-27 12:33:43 +00:00
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|