1 /* expand-common - common functionality for expand/unexpand
2 Copyright (C) 1989-2024 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 colno tab_size
= 0;
35 /* If nonzero, the size of all tab stops after the last specified. */
36 static colno extend_size
= 0;
38 /* If nonzero, an increment for additional tab stops after the last specified.*/
39 static colno increment_size
= 0;
41 /* The maximum distance between tab stops. */
42 idx_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 colno
*tab_list
= nullptr;
49 /* The number of allocated entries in 'tab_list'. */
50 static idx_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 idx_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 (colno tabval
)
77 colno prev_column
= first_free_tab
? tab_list
[first_free_tab
- 1] : 0;
78 colno column_width
= prev_column
<= tabval
? tabval
- prev_column
: 0;
80 if (first_free_tab
== n_tabs_allocated
)
81 tab_list
= xpalloc (tab_list
, &n_tabs_allocated
, 1, -1, sizeof *tab_list
);
82 tab_list
[first_free_tab
++] = tabval
;
84 if (max_column_width
< column_width
)
86 if (ckd_add (&max_column_width
, column_width
, 0))
87 error (EXIT_FAILURE
, 0, _("tabs are too far apart"));
92 set_extend_size (colno tabval
)
99 _("'/' specifier only allowed"
100 " with the last value"));
103 extend_size
= tabval
;
109 set_increment_size (colno tabval
)
116 _("'+' specifier only allowed"
117 " with the last value"));
120 increment_size
= tabval
;
125 /* Add the comma or blank separated list of tab stops STOPS
126 to the list of tab stops. */
128 parse_tab_stops (char const *stops
)
130 bool have_tabval
= false;
132 bool extend_tabval
= false;
133 bool increment_tabval
= false;
134 char const *num_start
= nullptr;
137 for (; *stops
; stops
++)
139 if (*stops
== ',' || isblank (to_uchar (*stops
)))
145 if (! set_extend_size (tabval
))
151 else if (increment_tabval
)
153 if (! set_increment_size (tabval
))
160 add_tab_stop (tabval
);
164 else if (*stops
== '/')
168 error (0, 0, _("'/' specifier not at start of number: %s"),
172 extend_tabval
= true;
173 increment_tabval
= false;
175 else if (*stops
== '+')
179 error (0, 0, _("'+' specifier not at start of number: %s"),
183 increment_tabval
= true;
184 extend_tabval
= false;
186 else if (ISDIGIT (*stops
))
195 /* Detect overflow. */
196 if (!DECIMAL_DIGIT_ACCUMULATE (tabval
, *stops
- '0'))
198 idx_t len
= strspn (num_start
, "0123456789");
199 char *bad_num
= ximemdup0 (num_start
, len
);
200 error (0, 0, _("tab stop is too large %s"), quote (bad_num
));
203 stops
= num_start
+ len
- 1;
208 error (0, 0, _("tab size contains invalid character(s): %s"),
215 if (ok
&& have_tabval
)
218 ok
&= set_extend_size (tabval
);
219 else if (increment_tabval
)
220 ok
&= set_increment_size (tabval
);
222 add_tab_stop (tabval
);
229 /* Check that the list of tab stops TABS, with ENTRIES entries,
230 contains only nonzero, ascending values. */
233 validate_tab_stops (colno
const *tabs
, idx_t entries
)
237 for (idx_t i
= 0; i
< entries
; i
++)
240 error (EXIT_FAILURE
, 0, _("tab size cannot be 0"));
241 if (tabs
[i
] <= prev_tab
)
242 error (EXIT_FAILURE
, 0, _("tab sizes must be ascending"));
246 if (increment_size
&& extend_size
)
247 error (EXIT_FAILURE
, 0, _("'/' specifier is mutually exclusive with '+'"));
250 /* Called after all command-line options have been parsed,
251 and add_tab_stop/parse_tab_stops have been called.
252 Will validate the tab-stop values,
253 and set the final values to:
254 tab-stops = 8 (if no tab-stops given on command line)
255 tab-stops = N (if value N specified as the only value).
256 tab-stops = distinct values given on command line (if multiple values given).
259 finalize_tab_stops (void)
261 validate_tab_stops (tab_list
, first_free_tab
);
263 if (first_free_tab
== 0)
264 tab_size
= max_column_width
= extend_size
265 ? extend_size
: increment_size
266 ? increment_size
: 8;
267 else if (first_free_tab
== 1 && ! extend_size
&& ! increment_size
)
268 tab_size
= tab_list
[0];
274 /* Return number of first tab stop after COLUMN. TAB_INDEX specifies
275 amny multiple tab-sizes. Set *LAST_TAB depending on whether we are
276 returning COLUMN + 1 merely because we're past the last tab.
277 If the number would overflow, diagnose this and exit. */
279 get_next_tab_column (colno column
, idx_t
*tab_index
, bool *last_tab
)
284 /* single tab-size - return multiples of it */
286 tab_distance
= tab_size
- column
% tab_size
;
289 /* multiple tab-sizes - iterate them until the tab position is beyond
290 the current input column. */
291 for ( ; *tab_index
< first_free_tab
; (*tab_index
)++ )
293 colno tab
= tab_list
[*tab_index
];
298 /* relative last tab - return multiples of it */
300 tab_distance
= extend_size
- column
% extend_size
;
301 else if (increment_size
)
303 /* incremental last tab - add increment_size to the previous
305 colno end_tab
= tab_list
[first_free_tab
- 1];
306 tab_distance
= increment_size
- ((column
- end_tab
) % increment_size
);
316 if (ckd_add (&tab_stop
, column
, tab_distance
))
317 error (EXIT_FAILURE
, 0, _("input line is too long"));
322 /* Sets new file-list */
324 set_file_list (char **list
)
326 have_read_stdin
= false;
329 file_list
= stdin_argv
;
334 /* Close the old stream pointer FP if it is non-null,
335 and return a new one opened to read the next input file.
336 Open a filename of '-' as the standard input.
337 Return nullptr if there are no more input files. */
342 static char *prev_file
;
350 if (STREQ (prev_file
, "-"))
351 clearerr (fp
); /* Also clear EOF. */
352 else if (fclose (fp
) != 0)
356 error (0, err
, "%s", quotef (prev_file
));
357 exit_status
= EXIT_FAILURE
;
361 while ((file
= *file_list
++) != nullptr)
363 if (STREQ (file
, "-"))
365 have_read_stdin
= true;
369 fp
= fopen (file
, "r");
373 fadvise (fp
, FADVISE_SEQUENTIAL
);
376 error (0, errno
, "%s", quotef (file
));
377 exit_status
= EXIT_FAILURE
;
384 cleanup_file_list_stdin (void)
386 if (have_read_stdin
&& fclose (stdin
) != 0)
387 error (EXIT_FAILURE
, errno
, "-");
392 emit_tab_list_info (void)
394 /* suppress syntax check for emit_mandatory_arg_note() */
396 -t, --tabs=LIST use comma separated list of tab positions.\n\
399 The last specified position can be prefixed with '/'\n\
400 to specify a tab size to use after the last\n\
401 explicitly specified tab stop. Also a prefix of '+'\n\
402 can be used to align remaining tab stops relative to\n\
403 the last specified tab stop instead of the first column\n\