expand: avoid an extraneous warning on 32 bit
[coreutils.git] / src / expand-common.c
blob0728e70d0d9b59cfc3615d139428ad6a92b897c4
1 /* expand-common - common functionality for expand/unexapnd
2 Copyright (C) 1989-2017 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 <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include "system.h"
22 #include "die.h"
23 #include "error.h"
24 #include "fadvise.h"
25 #include "quote.h"
26 #include "xstrndup.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 /* The maximum distance between tab stops. */
41 size_t max_column_width;
43 /* Array of the explicit column numbers of the tab stops;
44 after 'tab_list' is exhausted, each additional tab is replaced
45 by a space. The first column is column 0. */
46 static uintmax_t *tab_list = NULL;
48 /* The number of allocated entries in 'tab_list'. */
49 static size_t n_tabs_allocated = 0;
51 /* The index of the first invalid element of 'tab_list',
52 where the next element can be added. */
53 static size_t first_free_tab = 0;
55 /* Null-terminated array of input filenames. */
56 static char **file_list = NULL;
58 /* Default for 'file_list' if no files are given on the command line. */
59 static char *stdin_argv[] =
61 (char *) "-", NULL
64 /* True if we have ever read standard input. */
65 static bool have_read_stdin = false;
67 /* The desired exit status. */
68 int exit_status = EXIT_SUCCESS;
72 /* Add tab stop TABVAL to the end of 'tab_list'. */
73 extern void
74 add_tab_stop (uintmax_t tabval)
76 uintmax_t prev_column = first_free_tab ? tab_list[first_free_tab - 1] : 0;
77 uintmax_t column_width = prev_column <= tabval ? tabval - prev_column : 0;
79 if (first_free_tab == n_tabs_allocated)
80 tab_list = X2NREALLOC (tab_list, &n_tabs_allocated);
81 tab_list[first_free_tab++] = tabval;
83 if (max_column_width < column_width)
85 if (SIZE_MAX < column_width)
86 die (EXIT_FAILURE, 0, _("tabs are too far apart"));
87 max_column_width = column_width;
91 static bool
92 set_extend_size (uintmax_t 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 /* Add the comma or blank separated list of tab stops STOPS
109 to the list of tab stops. */
110 extern void
111 parse_tab_stops (char const *stops)
113 bool have_tabval = false;
114 uintmax_t tabval = 0;
115 bool extend_tabval = false;
116 char const *num_start = NULL;
117 bool ok = true;
119 for (; *stops; stops++)
121 if (*stops == ',' || isblank (to_uchar (*stops)))
123 if (have_tabval)
125 if (extend_tabval)
127 if (! set_extend_size (tabval))
129 ok = false;
130 break;
133 else
134 add_tab_stop (tabval);
136 have_tabval = false;
138 else if (*stops == '/')
140 if (have_tabval)
142 error (0, 0, _("'/' specifier not at start of number: %s"),
143 quote (stops));
144 ok = false;
146 extend_tabval = true;
148 else if (ISDIGIT (*stops))
150 if (!have_tabval)
152 tabval = 0;
153 have_tabval = true;
154 num_start = stops;
157 /* Detect overflow. */
158 if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0', uintmax_t))
160 size_t len = strspn (num_start, "0123456789");
161 char *bad_num = xstrndup (num_start, len);
162 error (0, 0, _("tab stop is too large %s"), quote (bad_num));
163 free (bad_num);
164 ok = false;
165 stops = num_start + len - 1;
168 else
170 error (0, 0, _("tab size contains invalid character(s): %s"),
171 quote (stops));
172 ok = false;
173 break;
177 if (ok && have_tabval)
179 if (extend_tabval)
180 ok &= set_extend_size (tabval);
181 else
182 add_tab_stop (tabval);
185 if (! ok)
186 exit (EXIT_FAILURE);
189 /* Check that the list of tab stops TABS, with ENTRIES entries,
190 contains only nonzero, ascending values. */
192 static void
193 validate_tab_stops (uintmax_t const *tabs, size_t entries)
195 uintmax_t prev_tab = 0;
196 size_t i;
198 for (i = 0; i < entries; i++)
200 if (tabs[i] == 0)
201 die (EXIT_FAILURE, 0, _("tab size cannot be 0"));
202 if (tabs[i] <= prev_tab)
203 die (EXIT_FAILURE, 0, _("tab sizes must be ascending"));
204 prev_tab = tabs[i];
208 /* Called after all command-line options have been parsed,
209 and add_tab_stop/parse_tab_stops have been called.
210 Will validate the tab-stop values,
211 and set the final values to:
212 tab-stops = 8 (if no tab-stops given on command line)
213 tab-stops = N (if value N specified as the only value).
214 tab-stops = distinct values given on command line (if multiple values given).
216 extern void
217 finalize_tab_stops (void)
219 validate_tab_stops (tab_list, first_free_tab);
221 if (first_free_tab == 0)
222 tab_size = max_column_width = extend_size ? extend_size : 8;
223 else if (first_free_tab == 1 && ! extend_size)
224 tab_size = tab_list[0];
225 else
226 tab_size = 0;
230 extern uintmax_t
231 get_next_tab_column (const uintmax_t column, size_t* tab_index,
232 bool* last_tab)
234 *last_tab = false;
236 /* single tab-size - return multiples of it */
237 if (tab_size)
238 return column + (tab_size - column % tab_size);
240 /* multiple tab-sizes - iterate them until the tab position is beyond
241 the current input column. */
242 for ( ; *tab_index < first_free_tab ; (*tab_index)++ )
244 uintmax_t tab = tab_list[*tab_index];
245 if (column < tab)
246 return tab;
249 /* relative last tab - return multiples of it */
250 if (extend_size)
251 return column + (extend_size - column % extend_size);
253 *last_tab = true;
254 return 0;
260 /* Sets new file-list */
261 extern void
262 set_file_list (char **list)
264 have_read_stdin = false;
266 if (!list)
267 file_list = stdin_argv;
268 else
269 file_list = list;
272 /* Close the old stream pointer FP if it is non-NULL,
273 and return a new one opened to read the next input file.
274 Open a filename of '-' as the standard input.
275 Return NULL if there are no more input files. */
277 extern FILE *
278 next_file (FILE *fp)
280 static char *prev_file;
281 char *file;
283 if (fp)
285 if (ferror (fp))
287 error (0, errno, "%s", quotef (prev_file));
288 exit_status = EXIT_FAILURE;
290 if (STREQ (prev_file, "-"))
291 clearerr (fp); /* Also clear EOF. */
292 else if (fclose (fp) != 0)
294 error (0, errno, "%s", quotef (prev_file));
295 exit_status = EXIT_FAILURE;
299 while ((file = *file_list++) != NULL)
301 if (STREQ (file, "-"))
303 have_read_stdin = true;
304 fp = stdin;
306 else
307 fp = fopen (file, "r");
308 if (fp)
310 prev_file = file;
311 fadvise (fp, FADVISE_SEQUENTIAL);
312 return fp;
314 error (0, errno, "%s", quotef (file));
315 exit_status = EXIT_FAILURE;
317 return NULL;
320 /* */
321 extern void
322 cleanup_file_list_stdin (void)
324 if (have_read_stdin && fclose (stdin) != 0)
325 die (EXIT_FAILURE, errno, "-");