1 /* expand-common - common functionality for expand/unexpand
2 Copyright (C) 1989-2023 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>
26 #include "expand-common.h"
28 /* If true, convert blanks even after nonblank characters have been
30 bool convert_entire_line
= false;
32 /* If nonzero, the size of all tab stops. If zero, use 'tab_list' instead. */
33 static uintmax_t tab_size
= 0;
35 /* If nonzero, the size of all tab stops after the last specified. */
36 static uintmax_t extend_size
= 0;
38 /* If nonzero, an increment for additional tab stops after the last specified.*/
39 static uintmax_t increment_size
= 0;
41 /* The maximum distance between tab stops. */
42 size_t max_column_width
;
44 /* Array of the explicit column numbers of the tab stops;
45 after 'tab_list' is exhausted, each additional tab is replaced
46 by a space. The first column is column 0. */
47 static uintmax_t *tab_list
= nullptr;
49 /* The number of allocated entries in 'tab_list'. */
50 static size_t n_tabs_allocated
= 0;
52 /* The index of the first invalid element of 'tab_list',
53 where the next element can be added. */
54 static size_t first_free_tab
= 0;
56 /* Null-terminated array of input filenames. */
57 static char **file_list
= nullptr;
59 /* Default for 'file_list' if no files are given on the command line. */
60 static char *stdin_argv
[] =
65 /* True if we have ever read standard input. */
66 static bool have_read_stdin
= false;
68 /* The desired exit status. */
69 int exit_status
= EXIT_SUCCESS
;
73 /* Add tab stop TABVAL to the end of 'tab_list'. */
75 add_tab_stop (uintmax_t tabval
)
77 uintmax_t prev_column
= first_free_tab
? tab_list
[first_free_tab
- 1] : 0;
78 uintmax_t column_width
= prev_column
<= tabval
? tabval
- prev_column
: 0;
80 if (first_free_tab
== n_tabs_allocated
)
81 tab_list
= X2NREALLOC (tab_list
, &n_tabs_allocated
);
82 tab_list
[first_free_tab
++] = tabval
;
84 if (max_column_width
< column_width
)
86 if (SIZE_MAX
< column_width
)
87 error (EXIT_FAILURE
, 0, _("tabs are too far apart"));
88 max_column_width
= column_width
;
93 set_extend_size (uintmax_t tabval
)
100 _("'/' specifier only allowed"
101 " with the last value"));
104 extend_size
= tabval
;
110 set_increment_size (uintmax_t tabval
)
117 _("'+' specifier only allowed"
118 " with the last value"));
121 increment_size
= tabval
;
126 /* Add the comma or blank separated list of tab stops STOPS
127 to the list of tab stops. */
129 parse_tab_stops (char const *stops
)
131 bool have_tabval
= false;
132 uintmax_t tabval
= 0;
133 bool extend_tabval
= false;
134 bool increment_tabval
= false;
135 char const *num_start
= nullptr;
138 for (; *stops
; stops
++)
140 if (*stops
== ',' || isblank (to_uchar (*stops
)))
146 if (! set_extend_size (tabval
))
152 else if (increment_tabval
)
154 if (! set_increment_size (tabval
))
161 add_tab_stop (tabval
);
165 else if (*stops
== '/')
169 error (0, 0, _("'/' specifier not at start of number: %s"),
173 extend_tabval
= true;
174 increment_tabval
= false;
176 else if (*stops
== '+')
180 error (0, 0, _("'+' specifier not at start of number: %s"),
184 increment_tabval
= true;
185 extend_tabval
= false;
187 else if (ISDIGIT (*stops
))
196 /* Detect overflow. */
197 if (!DECIMAL_DIGIT_ACCUMULATE (tabval
, *stops
- '0', uintmax_t))
199 size_t len
= strspn (num_start
, "0123456789");
200 char *bad_num
= ximemdup0 (num_start
, len
);
201 error (0, 0, _("tab stop is too large %s"), quote (bad_num
));
204 stops
= num_start
+ len
- 1;
209 error (0, 0, _("tab size contains invalid character(s): %s"),
216 if (ok
&& have_tabval
)
219 ok
&= set_extend_size (tabval
);
220 else if (increment_tabval
)
221 ok
&= set_increment_size (tabval
);
223 add_tab_stop (tabval
);
230 /* Check that the list of tab stops TABS, with ENTRIES entries,
231 contains only nonzero, ascending values. */
234 validate_tab_stops (uintmax_t const *tabs
, size_t entries
)
236 uintmax_t prev_tab
= 0;
238 for (size_t i
= 0; i
< entries
; i
++)
241 error (EXIT_FAILURE
, 0, _("tab size cannot be 0"));
242 if (tabs
[i
] <= prev_tab
)
243 error (EXIT_FAILURE
, 0, _("tab sizes must be ascending"));
247 if (increment_size
&& extend_size
)
248 error (EXIT_FAILURE
, 0, _("'/' specifier is mutually exclusive with '+'"));
251 /* Called after all command-line options have been parsed,
252 and add_tab_stop/parse_tab_stops have been called.
253 Will validate the tab-stop values,
254 and set the final values to:
255 tab-stops = 8 (if no tab-stops given on command line)
256 tab-stops = N (if value N specified as the only value).
257 tab-stops = distinct values given on command line (if multiple values given).
260 finalize_tab_stops (void)
262 validate_tab_stops (tab_list
, first_free_tab
);
264 if (first_free_tab
== 0)
265 tab_size
= max_column_width
= extend_size
266 ? extend_size
: increment_size
267 ? increment_size
: 8;
268 else if (first_free_tab
== 1 && ! extend_size
&& ! increment_size
)
269 tab_size
= tab_list
[0];
276 get_next_tab_column (const uintmax_t column
, size_t *tab_index
,
281 /* single tab-size - return multiples of it */
283 return column
+ (tab_size
- column
% tab_size
);
285 /* multiple tab-sizes - iterate them until the tab position is beyond
286 the current input column. */
287 for ( ; *tab_index
< first_free_tab
; (*tab_index
)++ )
289 uintmax_t tab
= tab_list
[*tab_index
];
294 /* relative last tab - return multiples of it */
296 return column
+ (extend_size
- column
% extend_size
);
298 /* incremental last tab - add increment_size to the previous tab stop */
301 uintmax_t end_tab
= tab_list
[first_free_tab
- 1];
303 return column
+ (increment_size
- ((column
- end_tab
) % increment_size
));
313 /* Sets new file-list */
315 set_file_list (char **list
)
317 have_read_stdin
= false;
320 file_list
= stdin_argv
;
325 /* Close the old stream pointer FP if it is non-null,
326 and return a new one opened to read the next input file.
327 Open a filename of '-' as the standard input.
328 Return nullptr if there are no more input files. */
333 static char *prev_file
;
341 if (STREQ (prev_file
, "-"))
342 clearerr (fp
); /* Also clear EOF. */
343 else if (fclose (fp
) != 0)
347 error (0, err
, "%s", quotef (prev_file
));
348 exit_status
= EXIT_FAILURE
;
352 while ((file
= *file_list
++) != nullptr)
354 if (STREQ (file
, "-"))
356 have_read_stdin
= true;
360 fp
= fopen (file
, "r");
364 fadvise (fp
, FADVISE_SEQUENTIAL
);
367 error (0, errno
, "%s", quotef (file
));
368 exit_status
= EXIT_FAILURE
;
375 cleanup_file_list_stdin (void)
377 if (have_read_stdin
&& fclose (stdin
) != 0)
378 error (EXIT_FAILURE
, errno
, "-");
383 emit_tab_list_info (void)
385 /* suppress syntax check for emit_mandatory_arg_note() */
387 -t, --tabs=LIST use comma separated list of tab positions.\n\
390 The last specified position can be prefixed with '/'\n\
391 to specify a tab size to use after the last\n\
392 explicitly specified tab stop. Also a prefix of '+'\n\
393 can be used to align remaining tab stops relative to\n\
394 the last specified tab stop instead of the first column\n\