1 /* expand-common - common functionality for expand/unexapnd
2 Copyright (C) 1989-2022 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 #include <sys/types.h>
28 #include "expand-common.h"
30 /* If true, convert blanks even after nonblank characters have been
32 bool convert_entire_line
= false;
34 /* If nonzero, the size of all tab stops. If zero, use 'tab_list' instead. */
35 static uintmax_t tab_size
= 0;
37 /* If nonzero, the size of all tab stops after the last specifed. */
38 static uintmax_t extend_size
= 0;
40 /* If nonzero, an increment for additional tab stops after the last specified.*/
41 static uintmax_t increment_size
= 0;
43 /* The maximum distance between tab stops. */
44 size_t max_column_width
;
46 /* Array of the explicit column numbers of the tab stops;
47 after 'tab_list' is exhausted, each additional tab is replaced
48 by a space. The first column is column 0. */
49 static uintmax_t *tab_list
= NULL
;
51 /* The number of allocated entries in 'tab_list'. */
52 static size_t n_tabs_allocated
= 0;
54 /* The index of the first invalid element of 'tab_list',
55 where the next element can be added. */
56 static size_t first_free_tab
= 0;
58 /* Null-terminated array of input filenames. */
59 static char **file_list
= NULL
;
61 /* Default for 'file_list' if no files are given on the command line. */
62 static char *stdin_argv
[] =
67 /* True if we have ever read standard input. */
68 static bool have_read_stdin
= false;
70 /* The desired exit status. */
71 int exit_status
= EXIT_SUCCESS
;
75 /* Add tab stop TABVAL to the end of 'tab_list'. */
77 add_tab_stop (uintmax_t tabval
)
79 uintmax_t prev_column
= first_free_tab
? tab_list
[first_free_tab
- 1] : 0;
80 uintmax_t column_width
= prev_column
<= tabval
? tabval
- prev_column
: 0;
82 if (first_free_tab
== n_tabs_allocated
)
83 tab_list
= X2NREALLOC (tab_list
, &n_tabs_allocated
);
84 tab_list
[first_free_tab
++] = tabval
;
86 if (max_column_width
< column_width
)
88 if (SIZE_MAX
< column_width
)
89 die (EXIT_FAILURE
, 0, _("tabs are too far apart"));
90 max_column_width
= column_width
;
95 set_extend_size (uintmax_t tabval
)
102 _("'/' specifier only allowed"
103 " with the last value"));
106 extend_size
= tabval
;
112 set_increment_size (uintmax_t tabval
)
119 _("'+' specifier only allowed"
120 " with the last value"));
123 increment_size
= tabval
;
128 /* Add the comma or blank separated list of tab stops STOPS
129 to the list of tab stops. */
131 parse_tab_stops (char const *stops
)
133 bool have_tabval
= false;
134 uintmax_t tabval
= 0;
135 bool extend_tabval
= false;
136 bool increment_tabval
= false;
137 char const *num_start
= NULL
;
140 for (; *stops
; stops
++)
142 if (*stops
== ',' || isblank (to_uchar (*stops
)))
148 if (! set_extend_size (tabval
))
154 else if (increment_tabval
)
156 if (! set_increment_size (tabval
))
163 add_tab_stop (tabval
);
167 else if (*stops
== '/')
171 error (0, 0, _("'/' specifier not at start of number: %s"),
175 extend_tabval
= true;
176 increment_tabval
= false;
178 else if (*stops
== '+')
182 error (0, 0, _("'+' specifier not at start of number: %s"),
186 increment_tabval
= true;
187 extend_tabval
= false;
189 else if (ISDIGIT (*stops
))
198 /* Detect overflow. */
199 if (!DECIMAL_DIGIT_ACCUMULATE (tabval
, *stops
- '0', uintmax_t))
201 size_t len
= strspn (num_start
, "0123456789");
202 char *bad_num
= ximemdup0 (num_start
, len
);
203 error (0, 0, _("tab stop is too large %s"), quote (bad_num
));
206 stops
= num_start
+ len
- 1;
211 error (0, 0, _("tab size contains invalid character(s): %s"),
218 if (ok
&& have_tabval
)
221 ok
&= set_extend_size (tabval
);
222 else if (increment_tabval
)
223 ok
&= set_increment_size (tabval
);
225 add_tab_stop (tabval
);
232 /* Check that the list of tab stops TABS, with ENTRIES entries,
233 contains only nonzero, ascending values. */
236 validate_tab_stops (uintmax_t const *tabs
, size_t entries
)
238 uintmax_t prev_tab
= 0;
240 for (size_t i
= 0; i
< entries
; i
++)
243 die (EXIT_FAILURE
, 0, _("tab size cannot be 0"));
244 if (tabs
[i
] <= prev_tab
)
245 die (EXIT_FAILURE
, 0, _("tab sizes must be ascending"));
249 if (increment_size
&& extend_size
)
250 die (EXIT_FAILURE
, 0, _("'/' specifier is mutually exclusive with '+'"));
253 /* Called after all command-line options have been parsed,
254 and add_tab_stop/parse_tab_stops have been called.
255 Will validate the tab-stop values,
256 and set the final values to:
257 tab-stops = 8 (if no tab-stops given on command line)
258 tab-stops = N (if value N specified as the only value).
259 tab-stops = distinct values given on command line (if multiple values given).
262 finalize_tab_stops (void)
264 validate_tab_stops (tab_list
, first_free_tab
);
266 if (first_free_tab
== 0)
267 tab_size
= max_column_width
= extend_size
268 ? extend_size
: increment_size
269 ? increment_size
: 8;
270 else if (first_free_tab
== 1 && ! extend_size
&& ! increment_size
)
271 tab_size
= tab_list
[0];
278 get_next_tab_column (const uintmax_t column
, size_t *tab_index
,
283 /* single tab-size - return multiples of it */
285 return column
+ (tab_size
- column
% tab_size
);
287 /* multiple tab-sizes - iterate them until the tab position is beyond
288 the current input column. */
289 for ( ; *tab_index
< first_free_tab
; (*tab_index
)++ )
291 uintmax_t tab
= tab_list
[*tab_index
];
296 /* relative last tab - return multiples of it */
298 return column
+ (extend_size
- column
% extend_size
);
300 /* incremental last tab - add increment_size to the previous tab stop */
303 uintmax_t end_tab
= tab_list
[first_free_tab
- 1];
305 return column
+ (increment_size
- ((column
- end_tab
) % increment_size
));
315 /* Sets new file-list */
317 set_file_list (char **list
)
319 have_read_stdin
= false;
322 file_list
= stdin_argv
;
327 /* Close the old stream pointer FP if it is non-NULL,
328 and return a new one opened to read the next input file.
329 Open a filename of '-' as the standard input.
330 Return NULL if there are no more input files. */
335 static char *prev_file
;
344 if (STREQ (prev_file
, "-"))
345 clearerr (fp
); /* Also clear EOF. */
346 else if (fclose (fp
) != 0)
350 error (0, err
, "%s", quotef (prev_file
));
351 exit_status
= EXIT_FAILURE
;
355 while ((file
= *file_list
++) != NULL
)
357 if (STREQ (file
, "-"))
359 have_read_stdin
= true;
363 fp
= fopen (file
, "r");
367 fadvise (fp
, FADVISE_SEQUENTIAL
);
370 error (0, errno
, "%s", quotef (file
));
371 exit_status
= EXIT_FAILURE
;
378 cleanup_file_list_stdin (void)
380 if (have_read_stdin
&& fclose (stdin
) != 0)
381 die (EXIT_FAILURE
, errno
, "-");
386 emit_tab_list_info (void)
388 /* suppress syntax check for emit_mandatory_arg_note() */
390 -t, --tabs=LIST use comma separated list of tab positions.\n\
393 The last specified position can be prefixed with '/'\n\
394 to specify a tab size to use after the last\n\
395 explicitly specified tab stop. Also a prefix of '+'\n\
396 can be used to align remaining tab stops relative to\n\
397 the last specified tab stop instead of the first column\n\