sort: pacify GCC 12 false positive
[coreutils.git] / src / expand-common.c
blobdeec1bd67a00043b22a92801056949a2222b92a9
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/>. */
17 #include <config.h>
19 #include <assert.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include "system.h"
23 #include "die.h"
24 #include "error.h"
25 #include "fadvise.h"
26 #include "quote.h"
28 #include "expand-common.h"
30 /* If true, convert blanks even after nonblank characters have been
31 read on the line. */
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[] =
64 (char *) "-", NULL
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'. */
76 extern void
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;
94 static bool
95 set_extend_size (uintmax_t tabval)
97 bool ok = true;
99 if (extend_size)
101 error (0, 0,
102 _("'/' specifier only allowed"
103 " with the last value"));
104 ok = false;
106 extend_size = tabval;
108 return ok;
111 static bool
112 set_increment_size (uintmax_t tabval)
114 bool ok = true;
116 if (increment_size)
118 error (0,0,
119 _("'+' specifier only allowed"
120 " with the last value"));
121 ok = false;
123 increment_size = tabval;
125 return ok;
128 /* Add the comma or blank separated list of tab stops STOPS
129 to the list of tab stops. */
130 extern void
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;
138 bool ok = true;
140 for (; *stops; stops++)
142 if (*stops == ',' || isblank (to_uchar (*stops)))
144 if (have_tabval)
146 if (extend_tabval)
148 if (! set_extend_size (tabval))
150 ok = false;
151 break;
154 else if (increment_tabval)
156 if (! set_increment_size (tabval))
158 ok = false;
159 break;
162 else
163 add_tab_stop (tabval);
165 have_tabval = false;
167 else if (*stops == '/')
169 if (have_tabval)
171 error (0, 0, _("'/' specifier not at start of number: %s"),
172 quote (stops));
173 ok = false;
175 extend_tabval = true;
176 increment_tabval = false;
178 else if (*stops == '+')
180 if (have_tabval)
182 error (0, 0, _("'+' specifier not at start of number: %s"),
183 quote (stops));
184 ok = false;
186 increment_tabval = true;
187 extend_tabval = false;
189 else if (ISDIGIT (*stops))
191 if (!have_tabval)
193 tabval = 0;
194 have_tabval = true;
195 num_start = 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));
204 free (bad_num);
205 ok = false;
206 stops = num_start + len - 1;
209 else
211 error (0, 0, _("tab size contains invalid character(s): %s"),
212 quote (stops));
213 ok = false;
214 break;
218 if (ok && have_tabval)
220 if (extend_tabval)
221 ok &= set_extend_size (tabval);
222 else if (increment_tabval)
223 ok &= set_increment_size (tabval);
224 else
225 add_tab_stop (tabval);
228 if (! ok)
229 exit (EXIT_FAILURE);
232 /* Check that the list of tab stops TABS, with ENTRIES entries,
233 contains only nonzero, ascending values. */
235 static void
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++)
242 if (tabs[i] == 0)
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"));
246 prev_tab = tabs[i];
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).
261 extern void
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];
272 else
273 tab_size = 0;
277 extern uintmax_t
278 get_next_tab_column (const uintmax_t column, size_t *tab_index,
279 bool *last_tab)
281 *last_tab = false;
283 /* single tab-size - return multiples of it */
284 if (tab_size)
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];
292 if (column < tab)
293 return tab;
296 /* relative last tab - return multiples of it */
297 if (extend_size)
298 return column + (extend_size - column % extend_size);
300 /* incremental last tab - add increment_size to the previous tab stop */
301 if (increment_size)
303 uintmax_t end_tab = tab_list[first_free_tab - 1];
305 return column + (increment_size - ((column - end_tab) % increment_size));
308 *last_tab = true;
309 return 0;
315 /* Sets new file-list */
316 extern void
317 set_file_list (char **list)
319 have_read_stdin = false;
321 if (!list)
322 file_list = stdin_argv;
323 else
324 file_list = list;
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. */
332 extern FILE *
333 next_file (FILE *fp)
335 static char *prev_file;
336 char *file;
338 if (fp)
340 assert (prev_file);
341 int err = errno;
342 if (!ferror (fp))
343 err = 0;
344 if (STREQ (prev_file, "-"))
345 clearerr (fp); /* Also clear EOF. */
346 else if (fclose (fp) != 0)
347 err = errno;
348 if (err)
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;
360 fp = stdin;
362 else
363 fp = fopen (file, "r");
364 if (fp)
366 prev_file = file;
367 fadvise (fp, FADVISE_SEQUENTIAL);
368 return fp;
370 error (0, errno, "%s", quotef (file));
371 exit_status = EXIT_FAILURE;
373 return NULL;
376 /* */
377 extern void
378 cleanup_file_list_stdin (void)
380 if (have_read_stdin && fclose (stdin) != 0)
381 die (EXIT_FAILURE, errno, "-");
385 extern void
386 emit_tab_list_info (void)
388 /* suppress syntax check for emit_mandatory_arg_note() */
389 fputs (_("\
390 -t, --tabs=LIST use comma separated list of tab positions.\n\
391 "), stdout);
392 fputs (_("\
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\
398 "), stdout);