1 /* set-fields.c -- common functions for parsing field list
2 Copyright (C) 2015-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 /* Extracted from cut.c by Assaf Gordon */
24 #include "set-fields.h"
26 /* Array of `struct field_range_pair' holding all the finite ranges. */
27 struct field_range_pair
*frp
;
29 /* Number of finite ranges specified by the user. */
32 /* Number of `struct field_range_pair's allocated. */
33 static size_t n_frp_allocated
;
35 #define FATAL_ERROR(Message) \
38 error (0, 0, (Message)); \
39 usage (EXIT_FAILURE); \
43 /* Append LOW, HIGH to the list RP of range pairs, allocating additional
44 space if necessary. Update global variable N_FRP. When allocating,
45 update global variable N_FRP_ALLOCATED. */
47 add_range_pair (uintmax_t lo
, uintmax_t hi
)
49 if (n_frp
== n_frp_allocated
)
50 frp
= X2NREALLOC (frp
, &n_frp_allocated
);
57 /* Comparison function for qsort to order the list of
58 struct field_range_pairs. */
60 compare_ranges (const void *a
, const void *b
)
62 struct field_range_pair
const *ap
= a
, *bp
= b
;
63 return (ap
->lo
> bp
->lo
) - (ap
->lo
< bp
->lo
);
66 /* Reallocate Range Pair entries, with corresponding
67 entries outside the range of each specified entry. */
72 struct field_range_pair
*c
= frp
;
80 add_range_pair (1, c
[0].lo
- 1);
82 for (size_t i
= 1; i
< n
; ++i
)
84 if (c
[i
- 1].hi
+ 1 == c
[i
].lo
)
87 add_range_pair (c
[i
- 1].hi
+ 1, c
[i
].lo
- 1);
90 if (c
[n
- 1].hi
< UINTMAX_MAX
)
91 add_range_pair (c
[n
- 1].hi
+ 1, UINTMAX_MAX
);
96 /* Given the list of field or byte range specifications FIELDSTR,
97 allocate and initialize the FRP array. FIELDSTR should
98 be composed of one or more numbers or ranges of numbers, separated
99 by blanks or commas. Incomplete ranges may be given: '-m' means '1-m';
100 'n-' means 'n' through end of line.
101 n=0 and n>=UINTMAX_MAX values will trigger an error.
103 if SETFLD_ALLOW_DASH option is used, a single '-' means all fields
104 (otherwise a single dash triggers an error).
106 if SETFLD_COMPLEMENT option is used, the specified field list
107 is complemented (e.g. '1-3' will result in fields '4-').
109 if SETFLD_ERRMSG_USE_POS option is used, error messages
110 will say 'position' (or 'byte/character positions')
111 instead of fields (used with cut -b/-c).
113 The function terminates on failure.
115 Upon return, the FRP array is initialized to contain
116 a non-overlapping, increasing list of field ranges.
118 N_FRP holds the number of field ranges in the FRP array.
120 The first field is stored as 1 (zero is not used).
121 An open-ended range (i.e., until the last field of the input line)
122 is indicated with hi = UINTMAX_MAX.
124 A sentinel of UINTMAX_MAX/UINTMAX_MAX is always added as the last
128 given '1-2,4', frp = [ { .lo = 1, .hi = 2 },
129 { .lo = 4, .hi = 4 },
130 { .lo = UINTMAX_MAX, .hi = UINTMAX_MAX } ];
132 given '3-', frp = [ { .lo = 3, .hi = UINTMAX_MAX },
133 { .lo = UINTMAX_MAX, .hi = UINTMAX_MAX } ];
136 set_fields (char const *fieldstr
, unsigned int options
)
138 uintmax_t initial
= 1; /* Value of first number in a range. */
139 uintmax_t value
= 0; /* If nonzero, a number being accumulated. */
140 bool lhs_specified
= false;
141 bool rhs_specified
= false;
142 bool dash_found
= false; /* True if a '-' is found in this field. */
144 bool in_digits
= false;
146 /* Collect and store in RP the range end points. */
148 /* Special case: '--field=-' means all fields, emulate '--field=1-' . */
149 if ((options
& SETFLD_ALLOW_DASH
) && STREQ (fieldstr
,"-"))
152 lhs_specified
= true;
159 if (*fieldstr
== '-')
162 /* Starting a range. */
164 FATAL_ERROR ((options
& SETFLD_ERRMSG_USE_POS
)
165 ? _("invalid byte or character range")
166 : _("invalid field range"));
171 if (lhs_specified
&& !value
)
172 FATAL_ERROR ((options
& SETFLD_ERRMSG_USE_POS
)
173 ? _("byte/character positions are numbered from 1")
174 : _("fields are numbered from 1"));
176 initial
= (lhs_specified
? value
: 1);
179 else if (*fieldstr
== ','
180 || isblank (to_uchar (*fieldstr
)) || *fieldstr
== '\0')
183 /* Ending the string, or this field/byte sublist. */
188 if (!lhs_specified
&& !rhs_specified
)
190 /* if a lone dash is allowed, emulate '1-' for all fields */
191 if (options
& SETFLD_ALLOW_DASH
)
194 FATAL_ERROR (_("invalid range with no endpoint: -"));
197 /* A range. Possibilities: -n, m-n, n-.
198 In any case, 'initial' contains the start of the range. */
201 /* 'n-'. From 'initial' to end of line. */
202 add_range_pair (initial
, UINTMAX_MAX
);
206 /* 'm-n' or '-n' (1-n). */
208 FATAL_ERROR (_("invalid decreasing range"));
210 add_range_pair (initial
, value
);
216 /* A simple field number, not a range. */
218 FATAL_ERROR ((options
& SETFLD_ERRMSG_USE_POS
)
219 ? _("byte/character positions are numbered from 1")
220 : _("fields are numbered from 1"));
222 add_range_pair (value
, value
);
226 if (*fieldstr
== '\0')
230 lhs_specified
= false;
231 rhs_specified
= false;
233 else if (ISDIGIT (*fieldstr
))
235 /* Record beginning of digit string, in case we have to
236 complain about it. */
237 static char const *num_start
;
238 if (!in_digits
|| !num_start
)
239 num_start
= fieldstr
;
247 /* Detect overflow. */
248 if (!DECIMAL_DIGIT_ACCUMULATE (value
, *fieldstr
- '0')
249 || value
== UINTMAX_MAX
)
251 /* In case the user specified -c$(echo 2^64|bc),22,
252 complain only about the first number. */
253 /* Determine the length of the offending number. */
254 size_t len
= strspn (num_start
, "0123456789");
255 char *bad_num
= ximemdup0 (num_start
, len
);
256 error (0, 0, (options
& SETFLD_ERRMSG_USE_POS
)
257 ?_("byte/character offset %s is too large")
258 :_("field number %s is too large"),
261 usage (EXIT_FAILURE
);
268 error (0, 0, (options
& SETFLD_ERRMSG_USE_POS
)
269 ?_("invalid byte/character position %s")
270 :_("invalid field value %s"),
272 usage (EXIT_FAILURE
);
277 FATAL_ERROR ((options
&SETFLD_ERRMSG_USE_POS
)
278 ? _("missing list of byte/character positions")
279 : _("missing list of fields"));
281 qsort (frp
, n_frp
, sizeof (frp
[0]), compare_ranges
);
283 /* Merge range pairs (e.g. `2-5,3-4' becomes `2-5'). */
284 for (size_t i
= 0; i
< n_frp
; ++i
)
286 for (size_t j
= i
+ 1; j
< n_frp
; ++j
)
288 if (frp
[j
].lo
<= frp
[i
].hi
)
290 frp
[i
].hi
= MAX (frp
[j
].hi
, frp
[i
].hi
);
291 memmove (frp
+ j
, frp
+ j
+ 1, (n_frp
- j
- 1) * sizeof *frp
);
300 if (options
& SETFLD_COMPLEMENT
)
303 /* After merging, reallocate RP so we release memory to the system.
304 Also add a sentinel at the end of RP, to avoid out of bounds access
305 and for performance reasons. */
307 frp
= xrealloc (frp
, n_frp
* sizeof (struct field_range_pair
));
308 frp
[n_frp
- 1].lo
= frp
[n_frp
- 1].hi
= UINTMAX_MAX
;