cksum: use AVX2 and AVX512 for speedup
[coreutils.git] / src / expand-common.c
blobe3f9ea84eccfc0cb4ae622ba02fd392ed4006f76
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 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[] =
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 (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"));
91 static bool
92 set_extend_size (colno tabval)
94 bool ok = true;
96 if (extend_size)
98 error (0, 0,
99 _("'/' specifier only allowed"
100 " with the last value"));
101 ok = false;
103 extend_size = tabval;
105 return ok;
108 static bool
109 set_increment_size (colno tabval)
111 bool ok = true;
113 if (increment_size)
115 error (0,0,
116 _("'+' specifier only allowed"
117 " with the last value"));
118 ok = false;
120 increment_size = tabval;
122 return ok;
125 /* Add the comma or blank separated list of tab stops STOPS
126 to the list of tab stops. */
127 extern void
128 parse_tab_stops (char const *stops)
130 bool have_tabval = false;
131 colno tabval = 0;
132 bool extend_tabval = false;
133 bool increment_tabval = false;
134 char const *num_start = nullptr;
135 bool ok = true;
137 for (; *stops; stops++)
139 if (*stops == ',' || isblank (to_uchar (*stops)))
141 if (have_tabval)
143 if (extend_tabval)
145 if (! set_extend_size (tabval))
147 ok = false;
148 break;
151 else if (increment_tabval)
153 if (! set_increment_size (tabval))
155 ok = false;
156 break;
159 else
160 add_tab_stop (tabval);
162 have_tabval = false;
164 else if (*stops == '/')
166 if (have_tabval)
168 error (0, 0, _("'/' specifier not at start of number: %s"),
169 quote (stops));
170 ok = false;
172 extend_tabval = true;
173 increment_tabval = false;
175 else if (*stops == '+')
177 if (have_tabval)
179 error (0, 0, _("'+' specifier not at start of number: %s"),
180 quote (stops));
181 ok = false;
183 increment_tabval = true;
184 extend_tabval = false;
186 else if (ISDIGIT (*stops))
188 if (!have_tabval)
190 tabval = 0;
191 have_tabval = true;
192 num_start = 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));
201 free (bad_num);
202 ok = false;
203 stops = num_start + len - 1;
206 else
208 error (0, 0, _("tab size contains invalid character(s): %s"),
209 quote (stops));
210 ok = false;
211 break;
215 if (ok && have_tabval)
217 if (extend_tabval)
218 ok &= set_extend_size (tabval);
219 else if (increment_tabval)
220 ok &= set_increment_size (tabval);
221 else
222 add_tab_stop (tabval);
225 if (! ok)
226 exit (EXIT_FAILURE);
229 /* Check that the list of tab stops TABS, with ENTRIES entries,
230 contains only nonzero, ascending values. */
232 static void
233 validate_tab_stops (colno const *tabs, idx_t entries)
235 colno prev_tab = 0;
237 for (idx_t i = 0; i < entries; i++)
239 if (tabs[i] == 0)
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"));
243 prev_tab = tabs[i];
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).
258 extern void
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];
269 else
270 tab_size = 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. */
278 extern colno
279 get_next_tab_column (colno column, idx_t *tab_index, bool *last_tab)
281 *last_tab = false;
282 colno tab_distance;
284 /* single tab-size - return multiples of it */
285 if (tab_size)
286 tab_distance = tab_size - column % tab_size;
287 else
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];
294 if (column < tab)
295 return tab;
298 /* relative last tab - return multiples of it */
299 if (extend_size)
300 tab_distance = extend_size - column % extend_size;
301 else if (increment_size)
303 /* incremental last tab - add increment_size to the previous
304 tab stop */
305 colno end_tab = tab_list[first_free_tab - 1];
306 tab_distance = increment_size - ((column - end_tab) % increment_size);
308 else
310 *last_tab = true;
311 tab_distance = 1;
315 colno tab_stop;
316 if (ckd_add (&tab_stop, column, tab_distance))
317 error (EXIT_FAILURE, 0, _("input line is too long"));
318 return tab_stop;
322 /* Sets new file-list */
323 extern void
324 set_file_list (char **list)
326 have_read_stdin = false;
328 if (!list)
329 file_list = stdin_argv;
330 else
331 file_list = list;
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. */
339 extern FILE *
340 next_file (FILE *fp)
342 static char *prev_file;
343 char *file;
345 if (fp)
347 int err = errno;
348 if (!ferror (fp))
349 err = 0;
350 if (STREQ (prev_file, "-"))
351 clearerr (fp); /* Also clear EOF. */
352 else if (fclose (fp) != 0)
353 err = errno;
354 if (err)
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;
366 fp = stdin;
368 else
369 fp = fopen (file, "r");
370 if (fp)
372 prev_file = file;
373 fadvise (fp, FADVISE_SEQUENTIAL);
374 return fp;
376 error (0, errno, "%s", quotef (file));
377 exit_status = EXIT_FAILURE;
379 return nullptr;
382 /* */
383 extern void
384 cleanup_file_list_stdin (void)
386 if (have_read_stdin && fclose (stdin) != 0)
387 error (EXIT_FAILURE, errno, "-");
391 extern void
392 emit_tab_list_info (void)
394 /* suppress syntax check for emit_mandatory_arg_note() */
395 fputs (_("\
396 -t, --tabs=LIST use comma separated list of tab positions.\n\
397 "), stdout);
398 fputs (_("\
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\
404 "), stdout);