maint: revert "build: update gnulib submodule to latest"
[coreutils/ericb.git] / src / expand.c
bloba5622906390e2aa4aa5dfdda74c7ef0b9328dd44
1 /* expand - convert tabs to spaces
2 Copyright (C) 1989, 1991, 1995-2006, 2008-2011 Free Software Foundation,
3 Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* By default, convert all tabs to spaces.
19 Preserves backspace characters in the output; they decrement the
20 column count for tab calculations.
21 The default action is equivalent to -8.
23 Options:
24 --tabs=tab1[,tab2[,...]]
25 -t tab1[,tab2[,...]]
26 -tab1[,tab2[,...]] If only one tab stop is given, set the tabs tab1
27 columns apart instead of the default 8. Otherwise,
28 set the tabs at columns tab1, tab2, etc. (numbered from
29 0); replace any tabs beyond the tab stops given with
30 single spaces.
31 --initial
32 -i Only convert initial tabs on each line to spaces.
34 David MacKenzie <djm@gnu.ai.mit.edu> */
36 #include <config.h>
38 #include <stdio.h>
39 #include <getopt.h>
40 #include <sys/types.h>
41 #include "system.h"
42 #include "error.h"
43 #include "fadvise.h"
44 #include "quote.h"
45 #include "xstrndup.h"
47 /* The official name of this program (e.g., no `g' prefix). */
48 #define PROGRAM_NAME "expand"
50 #define AUTHORS proper_name ("David MacKenzie")
52 /* If true, convert blanks even after nonblank characters have been
53 read on the line. */
54 static bool convert_entire_line;
56 /* If nonzero, the size of all tab stops. If zero, use `tab_list' instead. */
57 static uintmax_t tab_size;
59 /* Array of the explicit column numbers of the tab stops;
60 after `tab_list' is exhausted, each additional tab is replaced
61 by a space. The first column is column 0. */
62 static uintmax_t *tab_list;
64 /* The number of allocated entries in `tab_list'. */
65 static size_t n_tabs_allocated;
67 /* The index of the first invalid element of `tab_list',
68 where the next element can be added. */
69 static size_t first_free_tab;
71 /* Null-terminated array of input filenames. */
72 static char **file_list;
74 /* Default for `file_list' if no files are given on the command line. */
75 static char *stdin_argv[] =
77 (char *) "-", NULL
80 /* True if we have ever read standard input. */
81 static bool have_read_stdin;
83 /* The desired exit status. */
84 static int exit_status;
86 static char const shortopts[] = "it:0::1::2::3::4::5::6::7::8::9::";
88 static struct option const longopts[] =
90 {"tabs", required_argument, NULL, 't'},
91 {"initial", no_argument, NULL, 'i'},
92 {GETOPT_HELP_OPTION_DECL},
93 {GETOPT_VERSION_OPTION_DECL},
94 {NULL, 0, NULL, 0}
97 void
98 usage (int status)
100 if (status != EXIT_SUCCESS)
101 fprintf (stderr, _("Try `%s --help' for more information.\n"),
102 program_name);
103 else
105 printf (_("\
106 Usage: %s [OPTION]... [FILE]...\n\
108 program_name);
109 fputs (_("\
110 Convert tabs in each FILE to spaces, writing to standard output.\n\
111 With no FILE, or when FILE is -, read standard input.\n\
113 "), stdout);
114 fputs (_("\
115 Mandatory arguments to long options are mandatory for short options too.\n\
116 "), stdout);
117 fputs (_("\
118 -i, --initial do not convert tabs after non blanks\n\
119 -t, --tabs=NUMBER have tabs NUMBER characters apart, not 8\n\
120 "), stdout);
121 fputs (_("\
122 -t, --tabs=LIST use comma separated list of explicit tab positions\n\
123 "), stdout);
124 fputs (HELP_OPTION_DESCRIPTION, stdout);
125 fputs (VERSION_OPTION_DESCRIPTION, stdout);
126 emit_ancillary_info ();
128 exit (status);
131 /* Add tab stop TABVAL to the end of `tab_list'. */
133 static void
134 add_tab_stop (uintmax_t tabval)
136 if (first_free_tab == n_tabs_allocated)
137 tab_list = X2NREALLOC (tab_list, &n_tabs_allocated);
138 tab_list[first_free_tab++] = tabval;
141 /* Add the comma or blank separated list of tab stops STOPS
142 to the list of tab stops. */
144 static void
145 parse_tab_stops (char const *stops)
147 bool have_tabval = false;
148 uintmax_t tabval IF_LINT ( = 0);
149 char const *num_start IF_LINT ( = NULL);
150 bool ok = true;
152 for (; *stops; stops++)
154 if (*stops == ',' || isblank (to_uchar (*stops)))
156 if (have_tabval)
157 add_tab_stop (tabval);
158 have_tabval = false;
160 else if (ISDIGIT (*stops))
162 if (!have_tabval)
164 tabval = 0;
165 have_tabval = true;
166 num_start = stops;
169 /* Detect overflow. */
170 if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0', uintmax_t))
172 size_t len = strspn (num_start, "0123456789");
173 char *bad_num = xstrndup (num_start, len);
174 error (0, 0, _("tab stop is too large %s"), quote (bad_num));
175 free (bad_num);
176 ok = false;
177 stops = num_start + len - 1;
180 else
182 error (0, 0, _("tab size contains invalid character(s): %s"),
183 quote (stops));
184 ok = false;
185 break;
189 if (!ok)
190 exit (EXIT_FAILURE);
192 if (have_tabval)
193 add_tab_stop (tabval);
196 /* Check that the list of tab stops TABS, with ENTRIES entries,
197 contains only nonzero, ascending values. */
199 static void
200 validate_tab_stops (uintmax_t const *tabs, size_t entries)
202 uintmax_t prev_tab = 0;
203 size_t i;
205 for (i = 0; i < entries; i++)
207 if (tabs[i] == 0)
208 error (EXIT_FAILURE, 0, _("tab size cannot be 0"));
209 if (tabs[i] <= prev_tab)
210 error (EXIT_FAILURE, 0, _("tab sizes must be ascending"));
211 prev_tab = tabs[i];
215 /* Close the old stream pointer FP if it is non-NULL,
216 and return a new one opened to read the next input file.
217 Open a filename of `-' as the standard input.
218 Return NULL if there are no more input files. */
220 static FILE *
221 next_file (FILE *fp)
223 static char *prev_file;
224 char *file;
226 if (fp)
228 if (ferror (fp))
230 error (0, errno, "%s", prev_file);
231 exit_status = EXIT_FAILURE;
233 if (STREQ (prev_file, "-"))
234 clearerr (fp); /* Also clear EOF. */
235 else if (fclose (fp) != 0)
237 error (0, errno, "%s", prev_file);
238 exit_status = EXIT_FAILURE;
242 while ((file = *file_list++) != NULL)
244 if (STREQ (file, "-"))
246 have_read_stdin = true;
247 fp = stdin;
249 else
250 fp = fopen (file, "r");
251 if (fp)
253 prev_file = file;
254 fadvise (fp, FADVISE_SEQUENTIAL);
255 return fp;
257 error (0, errno, "%s", file);
258 exit_status = EXIT_FAILURE;
260 return NULL;
263 /* Change tabs to spaces, writing to stdout.
264 Read each file in `file_list', in order. */
266 static void
267 expand (void)
269 /* Input stream. */
270 FILE *fp = next_file (NULL);
272 if (!fp)
273 return;
275 while (true)
277 /* Input character, or EOF. */
278 int c;
280 /* If true, perform translations. */
281 bool convert = true;
284 /* The following variables have valid values only when CONVERT
285 is true: */
287 /* Column of next input character. */
288 uintmax_t column = 0;
290 /* Index in TAB_LIST of next tab stop to examine. */
291 size_t tab_index = 0;
294 /* Convert a line of text. */
298 while ((c = getc (fp)) < 0 && (fp = next_file (fp)))
299 continue;
301 if (convert)
303 if (c == '\t')
305 /* Column the next input tab stop is on. */
306 uintmax_t next_tab_column;
308 if (tab_size)
309 next_tab_column = column + (tab_size - column % tab_size);
310 else
311 while (true)
312 if (tab_index == first_free_tab)
314 next_tab_column = column + 1;
315 break;
317 else
319 uintmax_t tab = tab_list[tab_index++];
320 if (column < tab)
322 next_tab_column = tab;
323 break;
327 if (next_tab_column < column)
328 error (EXIT_FAILURE, 0, _("input line is too long"));
330 while (++column < next_tab_column)
331 if (putchar (' ') < 0)
332 error (EXIT_FAILURE, errno, _("write error"));
334 c = ' ';
336 else if (c == '\b')
338 /* Go back one column, and force recalculation of the
339 next tab stop. */
340 column -= !!column;
341 tab_index -= !!tab_index;
343 else
345 column++;
346 if (!column)
347 error (EXIT_FAILURE, 0, _("input line is too long"));
350 convert &= convert_entire_line || !! isblank (c);
353 if (c < 0)
354 return;
356 if (putchar (c) < 0)
357 error (EXIT_FAILURE, errno, _("write error"));
359 while (c != '\n');
364 main (int argc, char **argv)
366 int c;
368 initialize_main (&argc, &argv);
369 set_program_name (argv[0]);
370 setlocale (LC_ALL, "");
371 bindtextdomain (PACKAGE, LOCALEDIR);
372 textdomain (PACKAGE);
374 atexit (close_stdout);
376 have_read_stdin = false;
377 exit_status = EXIT_SUCCESS;
378 convert_entire_line = true;
379 tab_list = NULL;
380 first_free_tab = 0;
382 while ((c = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
384 switch (c)
386 case 'i':
387 convert_entire_line = false;
388 break;
390 case 't':
391 parse_tab_stops (optarg);
392 break;
394 case '0': case '1': case '2': case '3': case '4':
395 case '5': case '6': case '7': case '8': case '9':
396 if (optarg)
397 parse_tab_stops (optarg - 1);
398 else
400 char tab_stop[2];
401 tab_stop[0] = c;
402 tab_stop[1] = '\0';
403 parse_tab_stops (tab_stop);
405 break;
407 case_GETOPT_HELP_CHAR;
409 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
411 default:
412 usage (EXIT_FAILURE);
416 validate_tab_stops (tab_list, first_free_tab);
418 if (first_free_tab == 0)
419 tab_size = 8;
420 else if (first_free_tab == 1)
421 tab_size = tab_list[0];
422 else
423 tab_size = 0;
425 file_list = (optind < argc ? &argv[optind] : stdin_argv);
427 expand ();
429 if (have_read_stdin && fclose (stdin) != 0)
430 error (EXIT_FAILURE, errno, "-");
432 exit (exit_status);