maint: remove unnecessary inttostr usage in printf
[coreutils.git] / src / expand-common.c
blobc95998dc690cb4b630a8df0c02cb7bdf619dc4ef
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/>. */
17 #include <config.h>
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include "system.h"
23 #include "fadvise.h"
24 #include "quote.h"
26 #include "expand-common.h"
28 /* If true, convert blanks even after nonblank characters have been
29 read on the line. */
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[] =
62 (char *) "-", nullptr
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'. */
74 extern void
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;
92 static bool
93 set_extend_size (uintmax_t tabval)
95 bool ok = true;
97 if (extend_size)
99 error (0, 0,
100 _("'/' specifier only allowed"
101 " with the last value"));
102 ok = false;
104 extend_size = tabval;
106 return ok;
109 static bool
110 set_increment_size (uintmax_t tabval)
112 bool ok = true;
114 if (increment_size)
116 error (0,0,
117 _("'+' specifier only allowed"
118 " with the last value"));
119 ok = false;
121 increment_size = tabval;
123 return ok;
126 /* Add the comma or blank separated list of tab stops STOPS
127 to the list of tab stops. */
128 extern void
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;
136 bool ok = true;
138 for (; *stops; stops++)
140 if (*stops == ',' || isblank (to_uchar (*stops)))
142 if (have_tabval)
144 if (extend_tabval)
146 if (! set_extend_size (tabval))
148 ok = false;
149 break;
152 else if (increment_tabval)
154 if (! set_increment_size (tabval))
156 ok = false;
157 break;
160 else
161 add_tab_stop (tabval);
163 have_tabval = false;
165 else if (*stops == '/')
167 if (have_tabval)
169 error (0, 0, _("'/' specifier not at start of number: %s"),
170 quote (stops));
171 ok = false;
173 extend_tabval = true;
174 increment_tabval = false;
176 else if (*stops == '+')
178 if (have_tabval)
180 error (0, 0, _("'+' specifier not at start of number: %s"),
181 quote (stops));
182 ok = false;
184 increment_tabval = true;
185 extend_tabval = false;
187 else if (ISDIGIT (*stops))
189 if (!have_tabval)
191 tabval = 0;
192 have_tabval = true;
193 num_start = stops;
196 /* Detect overflow. */
197 if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0'))
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));
202 free (bad_num);
203 ok = false;
204 stops = num_start + len - 1;
207 else
209 error (0, 0, _("tab size contains invalid character(s): %s"),
210 quote (stops));
211 ok = false;
212 break;
216 if (ok && have_tabval)
218 if (extend_tabval)
219 ok &= set_extend_size (tabval);
220 else if (increment_tabval)
221 ok &= set_increment_size (tabval);
222 else
223 add_tab_stop (tabval);
226 if (! ok)
227 exit (EXIT_FAILURE);
230 /* Check that the list of tab stops TABS, with ENTRIES entries,
231 contains only nonzero, ascending values. */
233 static void
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++)
240 if (tabs[i] == 0)
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"));
244 prev_tab = tabs[i];
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).
259 extern void
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];
270 else
271 tab_size = 0;
275 extern uintmax_t
276 get_next_tab_column (const uintmax_t column, size_t *tab_index,
277 bool *last_tab)
279 *last_tab = false;
281 /* single tab-size - return multiples of it */
282 if (tab_size)
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];
290 if (column < tab)
291 return tab;
294 /* relative last tab - return multiples of it */
295 if (extend_size)
296 return column + (extend_size - column % extend_size);
298 /* incremental last tab - add increment_size to the previous tab stop */
299 if (increment_size)
301 uintmax_t end_tab = tab_list[first_free_tab - 1];
303 return column + (increment_size - ((column - end_tab) % increment_size));
306 *last_tab = true;
307 return 0;
313 /* Sets new file-list */
314 extern void
315 set_file_list (char **list)
317 have_read_stdin = false;
319 if (!list)
320 file_list = stdin_argv;
321 else
322 file_list = list;
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. */
330 extern FILE *
331 next_file (FILE *fp)
333 static char *prev_file;
334 char *file;
336 if (fp)
338 int err = errno;
339 if (!ferror (fp))
340 err = 0;
341 if (STREQ (prev_file, "-"))
342 clearerr (fp); /* Also clear EOF. */
343 else if (fclose (fp) != 0)
344 err = errno;
345 if (err)
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;
357 fp = stdin;
359 else
360 fp = fopen (file, "r");
361 if (fp)
363 prev_file = file;
364 fadvise (fp, FADVISE_SEQUENTIAL);
365 return fp;
367 error (0, errno, "%s", quotef (file));
368 exit_status = EXIT_FAILURE;
370 return nullptr;
373 /* */
374 extern void
375 cleanup_file_list_stdin (void)
377 if (have_read_stdin && fclose (stdin) != 0)
378 error (EXIT_FAILURE, errno, "-");
382 extern void
383 emit_tab_list_info (void)
385 /* suppress syntax check for emit_mandatory_arg_note() */
386 fputs (_("\
387 -t, --tabs=LIST use comma separated list of tab positions.\n\
388 "), stdout);
389 fputs (_("\
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\
395 "), stdout);