1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-20 10:51:48 +02:00
Files
coreutils/src/expand.c

456 lines
10 KiB
C
Raw Normal View History

1992-11-08 02:50:43 +00:00
/* expand - convert tabs to spaces
Copyright (C) 89, 91, 1995-2004 Free Software Foundation, Inc.
1992-11-08 02:50:43 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
1997-01-25 06:11:22 +00:00
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
1992-11-08 02:50:43 +00:00
/* By default, convert all tabs to spaces.
Preserves backspace characters in the output; they decrement the
column count for tab calculations.
The default action is equivalent to -8.
Options:
--tabs=tab1[,tab2[,...]]
-t tab1[,tab2[,...]]
-tab1[,tab2[,...]] If only one tab stop is given, set the tabs tab1
columns apart instead of the default 8. Otherwise,
1992-11-08 02:50:43 +00:00
set the tabs at columns tab1, tab2, etc. (numbered from
0); replace any tabs beyond the tab stops given with
1992-11-08 02:50:43 +00:00
single spaces.
--initial
-i Only convert initial tabs on each line to spaces.
1992-11-19 21:03:49 +00:00
David MacKenzie <djm@gnu.ai.mit.edu> */
1992-11-08 02:50:43 +00:00
1993-10-21 22:08:53 +00:00
#include <config.h>
1992-11-08 02:50:43 +00:00
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#include "error.h"
#include "posixver.h"
#include "quote.h"
#include "xstrndup.h"
1992-11-08 02:50:43 +00:00
1999-04-03 05:01:48 +00:00
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "expand"
#define AUTHORS "David MacKenzie"
1999-04-03 05:22:05 +00:00
1992-11-08 02:50:43 +00:00
/* The number of bytes added at a time to the amount of memory
allocated for the output line. */
1992-11-08 02:50:43 +00:00
#define OUTPUT_BLOCK 256
/* The name this program was run with. */
1993-04-29 05:26:22 +00:00
char *program_name;
/* If true, convert blanks even after nonblank characters have been
read on the line. */
static bool convert_entire_line;
1992-11-08 02:50:43 +00:00
/* If nonzero, the size of all tab stops. If zero, use `tab_list' instead. */
static uintmax_t tab_size;
1992-11-08 02:50:43 +00:00
/* Array of the explicit column numbers of the tab stops;
after `tab_list' is exhausted, each additional tab is replaced
by a space. The first column is column 0. */
static uintmax_t *tab_list;
/* The number of allocated entries in `tab_list'. */
static size_t n_tabs_allocated;
1992-11-08 02:50:43 +00:00
/* The index of the first invalid element of `tab_list',
where the next element can be added. */
static size_t first_free_tab;
1992-11-08 02:50:43 +00:00
/* Null-terminated array of input filenames. */
static char **file_list;
1992-11-08 02:50:43 +00:00
/* Default for `file_list' if no files are given on the command line. */
static char *stdin_argv[] =
1992-11-08 02:50:43 +00:00
{
"-", NULL
};
/* True if we have ever read standard input. */
static bool have_read_stdin;
1992-11-08 02:50:43 +00:00
/* The desired exit status. */
static int exit_status;
1992-11-08 02:50:43 +00:00
static struct option const longopts[] =
1992-11-08 02:50:43 +00:00
{
{"tabs", required_argument, NULL, 't'},
{"initial", no_argument, NULL, 'i'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
1992-11-08 02:50:43 +00:00
{NULL, 0, NULL, 0}
};
1999-01-14 18:25:16 +00:00
void
1995-10-17 13:47:48 +00:00
usage (int status)
1992-11-08 02:50:43 +00:00
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
1993-05-22 05:06:39 +00:00
{
printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
"),
program_name);
fputs (_("\
Convert tabs in each FILE to spaces, writing to standard output.\n\
With no FILE, or when FILE is -, read standard input.\n\
\n\
"), stdout);
fputs (_("\
Mandatory arguments to long options are mandatory for short options too.\n\
"), stdout);
fputs (_("\
-i, --initial do not convert tabs after non blanks\n\
-t, --tabs=NUMBER have tabs NUMBER characters apart, not 8\n\
"), stdout);
fputs (_("\
-t, --tabs=LIST use comma separated list of explicit tab positions\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1993-05-22 05:06:39 +00:00
}
exit (status);
}
1993-04-29 05:26:22 +00:00
/* Add tab stop TABVAL to the end of `tab_list'. */
1992-11-08 02:50:43 +00:00
static void
add_tab_stop (uintmax_t tabval)
{
if (first_free_tab == n_tabs_allocated)
tab_list = x2nrealloc (tab_list, &n_tabs_allocated, sizeof *tab_list);
tab_list[first_free_tab++] = tabval;
1992-11-08 02:50:43 +00:00
}
/* Add the comma or blank separated list of tab stops STOPS
to the list of tab stops. */
1992-11-08 02:50:43 +00:00
static void
parse_tab_stops (char const *stops)
1992-11-08 02:50:43 +00:00
{
bool have_tabval = false;
uintmax_t tabval IF_LINT (= 0);
char const *num_start IF_LINT (= NULL);
bool ok = true;
1992-11-08 02:50:43 +00:00
for (; *stops; stops++)
{
if (*stops == ',' || ISBLANK (to_uchar (*stops)))
1992-11-08 02:50:43 +00:00
{
if (have_tabval)
add_tab_stop (tabval);
have_tabval = false;
1992-11-08 02:50:43 +00:00
}
else if (ISDIGIT (*stops))
{
if (!have_tabval)
{
tabval = 0;
have_tabval = true;
num_start = stops;
}
{
/* Detect overflow. */
uintmax_t new_t = 10 * tabval + *stops - '0';
if (UINTMAX_MAX / 10 < tabval || new_t < tabval * 10)
{
size_t len = strspn (num_start, "0123456789");
char *bad_num = xstrndup (num_start, len);
error (0, 0, _("tab stop is too large %s"), quote (bad_num));
free (bad_num);
ok = false;
stops = num_start + len - 1;
}
tabval = new_t;
}
1992-11-08 02:50:43 +00:00
}
else
{
error (0, 0, _("tab size contains invalid character(s): %s"),
quote (stops));
ok = false;
break;
}
1992-11-08 02:50:43 +00:00
}
if (!ok)
exit (EXIT_FAILURE);
if (have_tabval)
add_tab_stop (tabval);
1992-11-08 02:50:43 +00:00
}
/* Check that the list of tab stops TABS, with ENTRIES entries,
contains only nonzero, ascending values. */
1992-11-08 02:50:43 +00:00
static void
validate_tab_stops (uintmax_t const *tabs, size_t entries)
1992-11-08 02:50:43 +00:00
{
uintmax_t prev_tab = 0;
size_t i;
1993-10-21 17:19:34 +00:00
1992-11-08 02:50:43 +00:00
for (i = 0; i < entries; i++)
{
if (tabs[i] == 0)
error (EXIT_FAILURE, 0, _("tab size cannot be 0"));
1992-11-08 02:50:43 +00:00
if (tabs[i] <= prev_tab)
error (EXIT_FAILURE, 0, _("tab sizes must be ascending"));
1992-11-08 02:50:43 +00:00
prev_tab = tabs[i];
}
}
/* Close the old stream pointer FP if it is non-NULL,
and return a new one opened to read the next input file.
Open a filename of `-' as the standard input.
Return NULL if there are no more input files. */
static FILE *
1995-10-17 13:47:48 +00:00
next_file (FILE *fp)
{
static char *prev_file;
char *file;
if (fp)
{
if (ferror (fp))
{
error (0, errno, "%s", prev_file);
exit_status = EXIT_FAILURE;
}
if (fp == stdin)
clearerr (fp); /* Also clear EOF. */
else if (fclose (fp) != 0)
{
error (0, errno, "%s", prev_file);
exit_status = EXIT_FAILURE;
}
}
while ((file = *file_list++) != NULL)
{
if (file[0] == '-' && file[1] == '\0')
{
have_read_stdin = true;
prev_file = file;
return stdin;
}
fp = fopen (file, "r");
if (fp)
{
prev_file = file;
return fp;
}
error (0, errno, "%s", file);
exit_status = EXIT_FAILURE;
}
return NULL;
}
1992-11-08 02:50:43 +00:00
/* Change tabs to spaces, writing to stdout.
Read each file in `file_list', in order. */
1992-11-08 02:50:43 +00:00
static void
1995-10-17 13:47:48 +00:00
expand (void)
1992-11-08 02:50:43 +00:00
{
/* Input stream. */
FILE *fp = next_file (NULL);
if (!fp)
1994-03-25 23:38:00 +00:00
return;
/* Binary I/O will preserve the original EOL style (DOS/Unix) of files. */
SET_BINARY2 (fileno (fp), STDOUT_FILENO);
1992-11-08 02:50:43 +00:00
for (;;)
{
/* Input character, or EOF. */
int c;
1992-11-08 02:50:43 +00:00
/* If true, perform translations. */
bool convert = true;
/* The following variables have valid values only when CONVERT
is true: */
/* Column of next input character. */
uintmax_t column = 0;
/* Index in TAB_LIST of next tab stop to examine. */
size_t tab_index = 0;
/* Convert a line of text. */
do
1992-11-08 02:50:43 +00:00
{
while ((c = getc (fp)) < 0 && (fp = next_file (fp)))
SET_BINARY2 (fileno (fp), STDOUT_FILENO);
1992-11-08 02:50:43 +00:00
if (convert)
{
if (c == '\t')
{
/* Column the next input tab stop is on. */
uintmax_t next_tab_column;
if (tab_size)
next_tab_column = column + (tab_size - column % tab_size);
else
for (;;)
if (tab_index == first_free_tab)
{
next_tab_column = column + 1;
break;
}
else
{
uintmax_t tab = tab_list[tab_index++];
if (column < tab)
{
next_tab_column = tab;
break;
}
}
if (next_tab_column < column)
error (EXIT_FAILURE, 0, _("input line is too long"));
while (++column < next_tab_column)
if (putchar (' ') < 0)
error (EXIT_FAILURE, errno, _("write error"));
2004-09-22 20:00:37 +00:00
c = ' ';
}
else if (c == '\b')
1992-11-08 02:50:43 +00:00
{
/* Go back one column, and force recalculation of the
next tab stop. */
column -= !!column;
tab_index -= !!tab_index;
1992-11-08 02:50:43 +00:00
}
else
{
column++;
if (!column)
error (EXIT_FAILURE, 0, _("input line is too long"));
1992-11-08 02:50:43 +00:00
}
convert &= convert_entire_line | ISBLANK (c);
1992-11-08 02:50:43 +00:00
}
if (c < 0)
return;
if (putchar (c) < 0)
error (EXIT_FAILURE, errno, _("write error"));
1992-11-08 02:50:43 +00:00
}
while (c != '\n');
1992-11-08 02:50:43 +00:00
}
}
int
1995-10-17 13:47:48 +00:00
main (int argc, char **argv)
1992-11-08 02:50:43 +00:00
{
bool have_tabval = false;
uintmax_t tabval IF_LINT (= 0);
int c;
1992-11-08 02:50:43 +00:00
bool obsolete_tablist = false;
2003-06-17 18:13:23 +00:00
initialize_main (&argc, &argv);
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
have_read_stdin = false;
exit_status = EXIT_SUCCESS;
convert_entire_line = true;
tab_list = NULL;
first_free_tab = 0;
while ((c = getopt_long (argc, argv, "it:,0123456789", longopts, NULL))
!= -1)
1992-11-08 02:50:43 +00:00
{
switch (c)
1992-11-08 02:50:43 +00:00
{
case '?':
usage (EXIT_FAILURE);
case 'i':
convert_entire_line = false;
break;
case 't':
parse_tab_stops (optarg);
break;
case ',':
if (have_tabval)
add_tab_stop (tabval);
have_tabval = false;
obsolete_tablist = true;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
if (!have_tabval)
{
tabval = 0;
have_tabval = true;
}
tabval = tabval * 10 + c - '0';
obsolete_tablist = true;
break;
1992-11-08 02:50:43 +00:00
}
}
if (obsolete_tablist && 200112 <= posix2_version ())
{
error (0, 0, _("`-LIST' option is obsolete; use `-t LIST'"));
usage (EXIT_FAILURE);
}
if (have_tabval)
add_tab_stop (tabval);
validate_tab_stops (tab_list, first_free_tab);
if (first_free_tab == 0)
tab_size = 8;
else if (first_free_tab == 1)
tab_size = tab_list[0];
1993-10-23 15:37:19 +00:00
else
tab_size = 0;
2003-05-10 19:58:04 +00:00
file_list = (optind < argc ? &argv[optind] : stdin_argv);
expand ();
if (have_read_stdin && fclose (stdin) != 0)
error (EXIT_FAILURE, errno, "-");
exit (exit_status);
1992-11-08 02:50:43 +00:00
}