Given the list of field or byte range specifications FIELDSTR, allocate and initialize the FRP array. FIELDSTR should be composed of one or more numbers or ranges of numbers, separated by blanks or commas. Incomplete ranges may be given: '-m' means '1-m'; 'n-' means 'n' through end of line. n=0 and n>=UINTMAX_MAX values will trigger an error. if SETFLD_ALLOW_DASH option is used
| 133 | { .lo = UINTMAX_MAX, .hi = UINTMAX_MAX } ]; |
| 134 | */ |
| 135 | void |
| 136 | set_fields (char const *fieldstr, unsigned int options) |
| 137 | { |
| 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. */ |
| 143 | |
| 144 | bool in_digits = false; |
| 145 | |
| 146 | /* Collect and store in RP the range end points. */ |
| 147 | |
| 148 | /* Special case: '--field=-' means all fields, emulate '--field=1-' . */ |
| 149 | if ((options & SETFLD_ALLOW_DASH) && streq (fieldstr,"-")) |
| 150 | { |
| 151 | value = 1; |
| 152 | lhs_specified = true; |
| 153 | dash_found = true; |
| 154 | fieldstr++; |
| 155 | } |
| 156 | |
| 157 | while (true) |
| 158 | { |
| 159 | if (*fieldstr == '-') |
| 160 | { |
| 161 | in_digits = false; |
| 162 | /* Starting a range. */ |
| 163 | if (dash_found) |
| 164 | FATAL_ERROR ((options & SETFLD_ERRMSG_USE_POS) |
| 165 | ? _("invalid byte or character range") |
| 166 | : _("invalid field range")); |
| 167 | |
| 168 | dash_found = true; |
| 169 | fieldstr++; |
| 170 | |
| 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")); |
| 175 | |
| 176 | initial = (lhs_specified ? value : 1); |
| 177 | value = 0; |
| 178 | } |
| 179 | else if (*fieldstr == ',' |
| 180 | || isblank (to_uchar (*fieldstr)) || *fieldstr == '\0') |
| 181 | { |
| 182 | in_digits = false; |
| 183 | /* Ending the string, or this field/byte sublist. */ |
| 184 | if (dash_found) |
| 185 | { |
| 186 | dash_found = false; |
| 187 | |
| 188 | if (!lhs_specified && !rhs_specified) |
| 189 | { |
| 190 | /* if a lone dash is allowed, emulate '1-' for all fields */ |
| 191 | if (options & SETFLD_ALLOW_DASH) |
| 192 | initial = 1; |
no test coverage detected