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/>. */
20 #include <sys/types.h>
28 #include "expand-common.h"
30 /* If true, convert blanks even after nonblank characters have been
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
[] =
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'. */
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
;
92 set_extend_size (uintmax_t tabval
)
99 _("'/' specifier only allowed"
100 " with the last value"));
103 extend_size
= tabval
;
108 /* Add the comma or blank separated list of tab stops STOPS
109 to the list of tab stops. */
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
;
119 for (; *stops
; stops
++)
121 if (*stops
== ',' || isblank (to_uchar (*stops
)))
127 if (! set_extend_size (tabval
))
134 add_tab_stop (tabval
);
138 else if (*stops
== '/')
142 error (0, 0, _("'/' specifier not at start of number: %s"),
146 extend_tabval
= true;
148 else if (ISDIGIT (*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
));
165 stops
= num_start
+ len
- 1;
170 error (0, 0, _("tab size contains invalid character(s): %s"),
177 if (ok
&& have_tabval
)
180 ok
&= set_extend_size (tabval
);
182 add_tab_stop (tabval
);
189 /* Check that the list of tab stops TABS, with ENTRIES entries,
190 contains only nonzero, ascending values. */
193 validate_tab_stops (uintmax_t const *tabs
, size_t entries
)
195 uintmax_t prev_tab
= 0;
198 for (i
= 0; i
< entries
; i
++)
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"));
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).
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];
231 get_next_tab_column (const uintmax_t column
, size_t* tab_index
,
236 /* single tab-size - return multiples of it */
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
];
249 /* relative last tab - return multiples of it */
251 return column
+ (extend_size
- column
% extend_size
);
260 /* Sets new file-list */
262 set_file_list (char **list
)
264 have_read_stdin
= false;
267 file_list
= stdin_argv
;
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. */
280 static char *prev_file
;
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;
307 fp
= fopen (file
, "r");
311 fadvise (fp
, FADVISE_SEQUENTIAL
);
314 error (0, errno
, "%s", quotef (file
));
315 exit_status
= EXIT_FAILURE
;
322 cleanup_file_list_stdin (void)
324 if (have_read_stdin
&& fclose (stdin
) != 0)
325 die (EXIT_FAILURE
, errno
, "-");