[6974a34] | 1 | /* readval.c |
---|
[0395657] | 2 | * Routines to read a prefix or number from the current input file |
---|
[5b9b9d4] | 3 | * Copyright (C) 1991-2024 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[d333899] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
[4c83f84] | 20 | #include <config.h> |
---|
[d1b1380] | 21 | |
---|
[c00c74a9] | 22 | #include <limits.h> |
---|
[be97baf] | 23 | #include <stddef.h> /* for offsetof */ |
---|
[c00c74a9] | 24 | |
---|
[a420b49] | 25 | #include "cavern.h" |
---|
[5a0ab6a] | 26 | #include "commands.h" /* For match_tok(), etc */ |
---|
[1ee204e] | 27 | #include "date.h" |
---|
[dd5b1a8] | 28 | #include "debug.h" |
---|
[a1706d1] | 29 | #include "filename.h" |
---|
| 30 | #include "message.h" |
---|
[d1b1380] | 31 | #include "readval.h" |
---|
| 32 | #include "datain.h" |
---|
[fb2e93c] | 33 | #include "netbits.h" |
---|
[8cf2e04] | 34 | #include "osalloc.h" |
---|
[76bb277a] | 35 | #include "str.h" |
---|
[d1b1380] | 36 | |
---|
[c86cc71] | 37 | int root_depr_count = 0; |
---|
| 38 | |
---|
[710ecc1] | 39 | static prefix * |
---|
[a2c33ae] | 40 | new_anon_station(void) |
---|
[710ecc1] | 41 | { |
---|
[a2c33ae] | 42 | prefix *name = osnew(prefix); |
---|
| 43 | name->pos = NULL; |
---|
[ba84079] | 44 | name->ident.p = NULL; |
---|
[a2c33ae] | 45 | name->shape = 0; |
---|
| 46 | name->stn = NULL; |
---|
| 47 | name->up = pcs->Prefix; |
---|
| 48 | name->down = NULL; |
---|
| 49 | name->filename = file.filename; |
---|
| 50 | name->line = file.line; |
---|
[5fc783b] | 51 | name->min_export = name->max_export = 0; |
---|
[a2c33ae] | 52 | name->sflags = BIT(SFLAGS_ANON); |
---|
| 53 | /* Keep linked list of anon stations for node stats. */ |
---|
| 54 | name->right = anon_list; |
---|
| 55 | anon_list = name; |
---|
| 56 | return name; |
---|
[710ecc1] | 57 | } |
---|
| 58 | |
---|
[ba84079] | 59 | static char *id = NULL; |
---|
| 60 | static size_t id_len = 0; |
---|
| 61 | |
---|
[c458cf7] | 62 | /* if prefix is omitted: if PFX_OPT set return NULL, otherwise use longjmp */ |
---|
| 63 | extern prefix * |
---|
| 64 | read_prefix(unsigned pfx_flags) |
---|
[a420b49] | 65 | { |
---|
[c458cf7] | 66 | bool f_optional = !!(pfx_flags & PFX_OPT); |
---|
| 67 | bool fSurvey = !!(pfx_flags & PFX_SURVEY); |
---|
| 68 | bool fSuspectTypo = !!(pfx_flags & PFX_SUSPECT_TYPO); |
---|
[4a6a094] | 69 | prefix *back_ptr, *ptr; |
---|
[eb18f4d] | 70 | size_t i; |
---|
[647407d] | 71 | bool fNew; |
---|
[63d4f07] | 72 | bool fImplicitPrefix = true; |
---|
[932f7e9] | 73 | int depth = -1; |
---|
[d3fa693] | 74 | filepos here; |
---|
[5b4085b] | 75 | filepos fp_firstsep; |
---|
[a420b49] | 76 | |
---|
| 77 | skipblanks(); |
---|
[d3fa693] | 78 | get_pos(&here); |
---|
[7f1ab95] | 79 | #ifndef NO_DEPRECATED |
---|
[a420b49] | 80 | if (isRoot(ch)) { |
---|
[c458cf7] | 81 | if (!(pfx_flags & PFX_ALLOW_ROOT)) { |
---|
[cab0f26] | 82 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*ROOT is deprecated*/25); |
---|
[fdd4ed76] | 83 | longjmp(jbSkipLine, 1); |
---|
[84c60fc] | 84 | } |
---|
[c86cc71] | 85 | if (root_depr_count < 5) { |
---|
[cab0f26] | 86 | compile_diagnostic(DIAG_WARN|DIAG_COL, /*ROOT is deprecated*/25); |
---|
[c86cc71] | 87 | if (++root_depr_count == 5) |
---|
[64544daf] | 88 | compile_diagnostic(DIAG_INFO, /*Further uses of this deprecated feature will not be reported*/95); |
---|
[c86cc71] | 89 | } |
---|
[d1b1380] | 90 | nextch(); |
---|
[a420b49] | 91 | ptr = root; |
---|
| 92 | if (!isNames(ch)) { |
---|
| 93 | if (!isSep(ch)) return ptr; |
---|
[27b8b59] | 94 | /* Allow optional SEPARATOR after ROOT */ |
---|
[5b4085b] | 95 | get_pos(&fp_firstsep); |
---|
[a420b49] | 96 | nextch(); |
---|
| 97 | } |
---|
[63d4f07] | 98 | fImplicitPrefix = false; |
---|
[710ecc1] | 99 | #else |
---|
| 100 | if (0) { |
---|
| 101 | #endif |
---|
[a420b49] | 102 | } else { |
---|
[dcbcae0] | 103 | if ((pfx_flags & PFX_ANON) && |
---|
| 104 | (isSep(ch) || (pcs->dash_for_anon_wall_station && ch == '-'))) { |
---|
| 105 | int first_ch = ch; |
---|
[710ecc1] | 106 | nextch(); |
---|
[1cb22b5] | 107 | if (isBlank(ch) || isComm(ch) || isEol(ch)) { |
---|
[dcbcae0] | 108 | if (!isSep(first_ch)) |
---|
| 109 | goto anon_wall_station; |
---|
| 110 | /* A single separator alone ('.' by default) is an anonymous |
---|
| 111 | * station which is on a point inside the passage and implies |
---|
| 112 | * the leg to it is a splay. |
---|
| 113 | */ |
---|
| 114 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
---|
[9f55538] | 115 | set_pos(&here); |
---|
[d0be687d] | 116 | compile_diagnostic(DIAG_ERR|DIAG_WORD, /*Can't have a leg between two anonymous stations*/3); |
---|
[fdd4ed76] | 117 | longjmp(jbSkipLine, 1); |
---|
[710ecc1] | 118 | } |
---|
[dcbcae0] | 119 | pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY); |
---|
[a2c33ae] | 120 | return new_anon_station(); |
---|
[710ecc1] | 121 | } |
---|
[dcbcae0] | 122 | if (isSep(first_ch) && ch == first_ch) { |
---|
| 123 | nextch(); |
---|
[1cb22b5] | 124 | if (isBlank(ch) || isComm(ch) || isEol(ch)) { |
---|
[dcbcae0] | 125 | /* A double separator ('..' by default) is an anonymous station |
---|
| 126 | * which is on the wall and implies the leg to it is a splay. |
---|
| 127 | */ |
---|
| 128 | prefix * pfx; |
---|
| 129 | anon_wall_station: |
---|
| 130 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
---|
[9f55538] | 131 | set_pos(&here); |
---|
[d0be687d] | 132 | compile_diagnostic(DIAG_ERR|DIAG_WORD, /*Can't have a leg between two anonymous stations*/3); |
---|
[fdd4ed76] | 133 | longjmp(jbSkipLine, 1); |
---|
[dcbcae0] | 134 | } |
---|
[da65891] | 135 | pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY); |
---|
[dcbcae0] | 136 | pfx = new_anon_station(); |
---|
| 137 | pfx->sflags |= BIT(SFLAGS_WALL); |
---|
| 138 | return pfx; |
---|
| 139 | } |
---|
| 140 | if (ch == first_ch) { |
---|
| 141 | nextch(); |
---|
[1cb22b5] | 142 | if (isBlank(ch) || isComm(ch) || isEol(ch)) { |
---|
[dcbcae0] | 143 | /* A triple separator ('...' by default) is an anonymous |
---|
| 144 | * station, but otherwise not handled specially (e.g. for |
---|
| 145 | * a single leg down an unexplored side passage to a station |
---|
| 146 | * which isn't refindable). |
---|
| 147 | */ |
---|
| 148 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
---|
[9f55538] | 149 | set_pos(&here); |
---|
[d0be687d] | 150 | compile_diagnostic(DIAG_ERR|DIAG_WORD, /*Can't have a leg between two anonymous stations*/3); |
---|
[fdd4ed76] | 151 | longjmp(jbSkipLine, 1); |
---|
[dcbcae0] | 152 | } |
---|
| 153 | pcs->flags |= BIT(FLAGS_ANON_ONE_END); |
---|
| 154 | return new_anon_station(); |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | } |
---|
[710ecc1] | 158 | set_pos(&here); |
---|
| 159 | } |
---|
[a420b49] | 160 | ptr = pcs->Prefix; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | i = 0; |
---|
| 164 | do { |
---|
[63d4f07] | 165 | fNew = false; |
---|
[ba84079] | 166 | if (id == NULL) { |
---|
| 167 | /* Allocate buffer the first time */ |
---|
| 168 | id_len = 256; |
---|
| 169 | id = osmalloc(id_len); |
---|
[be97baf] | 170 | } |
---|
[a420b49] | 171 | /* i==0 iff this is the first pass */ |
---|
| 172 | if (i) { |
---|
| 173 | i = 0; |
---|
| 174 | nextch(); |
---|
| 175 | } |
---|
| 176 | while (isNames(ch)) { |
---|
| 177 | if (i < pcs->Truncate) { |
---|
| 178 | /* truncate name */ |
---|
[ba84079] | 179 | id[i++] = (pcs->Case == LOWER ? tolower(ch) : |
---|
| 180 | (pcs->Case == OFF ? ch : toupper(ch))); |
---|
| 181 | if (i >= id_len) { |
---|
| 182 | id_len *= 2; |
---|
| 183 | id = osrealloc(id, id_len); |
---|
[a420b49] | 184 | } |
---|
[8cf2e04] | 185 | } |
---|
[a420b49] | 186 | nextch(); |
---|
[8cf2e04] | 187 | } |
---|
[5b4085b] | 188 | if (isSep(ch)) { |
---|
[63d4f07] | 189 | fImplicitPrefix = false; |
---|
[5b4085b] | 190 | get_pos(&fp_firstsep); |
---|
| 191 | } |
---|
[a420b49] | 192 | if (i == 0) { |
---|
[21c226e] | 193 | if (!f_optional) { |
---|
[ee1d81f] | 194 | if (isEol(ch)) { |
---|
[84c60fc] | 195 | if (fSurvey) { |
---|
[cab0f26] | 196 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Expecting survey name*/89); |
---|
[84c60fc] | 197 | } else { |
---|
[cab0f26] | 198 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Expecting station name*/28); |
---|
[84c60fc] | 199 | } |
---|
[ee1d81f] | 200 | } else { |
---|
[736f7df] | 201 | /* TRANSLATORS: Here "station" is a survey station, not a train station. */ |
---|
[cab0f26] | 202 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Character “%c” not allowed in station name (use *SET NAMES to set allowed characters)*/7, ch); |
---|
[ee1d81f] | 203 | } |
---|
[fdd4ed76] | 204 | longjmp(jbSkipLine, 1); |
---|
[647407d] | 205 | } |
---|
[a420b49] | 206 | return (prefix *)NULL; |
---|
[d1b1380] | 207 | } |
---|
[a420b49] | 208 | |
---|
[ba84079] | 209 | id[i++] = '\0'; |
---|
[cb3d1e2] | 210 | |
---|
[a420b49] | 211 | back_ptr = ptr; |
---|
| 212 | ptr = ptr->down; |
---|
| 213 | if (ptr == NULL) { |
---|
| 214 | /* Special case first time around at each level */ |
---|
| 215 | ptr = osnew(prefix); |
---|
[ba84079] | 216 | ptr->sflags = BIT(SFLAGS_SURVEY); |
---|
| 217 | if (i <= sizeof(ptr->ident.i)) { |
---|
| 218 | memcpy(ptr->ident.i, id, i); |
---|
| 219 | ptr->sflags |= BIT(SFLAGS_IDENT_INLINE); |
---|
| 220 | } else { |
---|
| 221 | char *new_id = osmalloc(i); |
---|
| 222 | memcpy(new_id, id, i); |
---|
| 223 | ptr->ident.p = new_id; |
---|
| 224 | } |
---|
[a420b49] | 225 | ptr->right = ptr->down = NULL; |
---|
| 226 | ptr->pos = NULL; |
---|
[6adb88c] | 227 | ptr->shape = 0; |
---|
[a420b49] | 228 | ptr->stn = NULL; |
---|
| 229 | ptr->up = back_ptr; |
---|
[d333899] | 230 | ptr->filename = file.filename; |
---|
| 231 | ptr->line = file.line; |
---|
[932f7e9] | 232 | ptr->min_export = ptr->max_export = 0; |
---|
[95c3272] | 233 | if (fSuspectTypo && !fImplicitPrefix) |
---|
| 234 | ptr->sflags |= BIT(SFLAGS_SUSPECTTYPO); |
---|
[a420b49] | 235 | back_ptr->down = ptr; |
---|
[63d4f07] | 236 | fNew = true; |
---|
[a420b49] | 237 | } else { |
---|
[be97baf] | 238 | /* Use caching to speed up adding an increasing sequence to a |
---|
| 239 | * large survey */ |
---|
| 240 | static prefix *cached_survey = NULL, *cached_station = NULL; |
---|
[a420b49] | 241 | prefix *ptrPrev = NULL; |
---|
[76bb277a] | 242 | int cmp = 1; /* result of strcmp ( -ve for <, 0 for =, +ve for > ) */ |
---|
[be97baf] | 243 | if (cached_survey == back_ptr) { |
---|
[ba84079] | 244 | cmp = strcmp(prefix_ident(cached_station), id); |
---|
[be97baf] | 245 | if (cmp <= 0) ptr = cached_station; |
---|
| 246 | } |
---|
[ba84079] | 247 | while (ptr && (cmp = strcmp(prefix_ident(ptr), id)) < 0) { |
---|
[a420b49] | 248 | ptrPrev = ptr; |
---|
| 249 | ptr = ptr->right; |
---|
| 250 | } |
---|
| 251 | if (cmp) { |
---|
| 252 | /* ie we got to one that was higher, or the end */ |
---|
[ba84079] | 253 | prefix *newptr = osnew(prefix); |
---|
| 254 | newptr->sflags = BIT(SFLAGS_SURVEY); |
---|
| 255 | if (strlen(id) < sizeof(newptr->ident.i)) { |
---|
| 256 | memcpy(newptr->ident.i, id, i); |
---|
| 257 | newptr->sflags |= BIT(SFLAGS_IDENT_INLINE); |
---|
| 258 | } else { |
---|
| 259 | char *new_id = osmalloc(i); |
---|
| 260 | memcpy(new_id, id, i); |
---|
| 261 | newptr->ident.p = new_id; |
---|
| 262 | } |
---|
[a420b49] | 263 | if (ptrPrev == NULL) |
---|
[647407d] | 264 | back_ptr->down = newptr; |
---|
[a420b49] | 265 | else |
---|
[647407d] | 266 | ptrPrev->right = newptr; |
---|
| 267 | newptr->right = ptr; |
---|
| 268 | newptr->down = NULL; |
---|
| 269 | newptr->pos = NULL; |
---|
[6adb88c] | 270 | newptr->shape = 0; |
---|
[647407d] | 271 | newptr->stn = NULL; |
---|
| 272 | newptr->up = back_ptr; |
---|
[d333899] | 273 | newptr->filename = file.filename; |
---|
| 274 | newptr->line = file.line; |
---|
[421b7d2] | 275 | newptr->min_export = newptr->max_export = 0; |
---|
[95c3272] | 276 | if (fSuspectTypo && !fImplicitPrefix) |
---|
| 277 | newptr->sflags |= BIT(SFLAGS_SUSPECTTYPO); |
---|
[647407d] | 278 | ptr = newptr; |
---|
[63d4f07] | 279 | fNew = true; |
---|
[a420b49] | 280 | } |
---|
[be97baf] | 281 | cached_survey = back_ptr; |
---|
| 282 | cached_station = ptr; |
---|
[d1b1380] | 283 | } |
---|
[fb2e93c] | 284 | depth++; |
---|
[63d4f07] | 285 | f_optional = false; /* disallow after first level */ |
---|
[d3fa693] | 286 | if (isSep(ch)) { |
---|
| 287 | get_pos(&fp_firstsep); |
---|
| 288 | if (!TSTBIT(ptr->sflags, SFLAGS_SURVEY)) { |
---|
| 289 | /* TRANSLATORS: Here "station" is a survey station, not a train station. |
---|
| 290 | * |
---|
| 291 | * Here "survey" is a "cave map" rather than list of questions - it should be |
---|
| 292 | * translated to the terminology that cavers using the language would use. |
---|
| 293 | */ |
---|
| 294 | compile_diagnostic(DIAG_ERR|DIAG_FROM(here), /*“%s” can’t be both a station and a survey*/27, |
---|
| 295 | sprint_prefix(ptr)); |
---|
| 296 | } |
---|
| 297 | } |
---|
[a420b49] | 298 | } while (isSep(ch)); |
---|
[be97baf] | 299 | |
---|
[d40f787] | 300 | /* don't warn about a station that is referred to twice */ |
---|
[95c3272] | 301 | if (!fNew) ptr->sflags &= ~BIT(SFLAGS_SUSPECTTYPO); |
---|
[421b7d2] | 302 | |
---|
[6430f0e] | 303 | if (fNew) { |
---|
[95c3272] | 304 | /* fNew means SFLAGS_SURVEY is currently set */ |
---|
[4c07c51] | 305 | SVX_ASSERT(TSTBIT(ptr->sflags, SFLAGS_SURVEY)); |
---|
[c00c74a9] | 306 | if (!fSurvey) { |
---|
| 307 | ptr->sflags &= ~BIT(SFLAGS_SURVEY); |
---|
[27b8b59] | 308 | if (TSTBIT(pcs->infer, INFER_EXPORTS)) ptr->min_export = USHRT_MAX; |
---|
[c00c74a9] | 309 | } |
---|
[6430f0e] | 310 | } else { |
---|
| 311 | /* check that the same name isn't being used for a survey and station */ |
---|
[95c3272] | 312 | if (fSurvey ^ TSTBIT(ptr->sflags, SFLAGS_SURVEY)) { |
---|
[0b8c321] | 313 | /* TRANSLATORS: Here "station" is a survey station, not a train station. |
---|
| 314 | * |
---|
| 315 | * Here "survey" is a "cave map" rather than list of questions - it should be |
---|
| 316 | * translated to the terminology that cavers using the language would use. |
---|
| 317 | */ |
---|
[d3fa693] | 318 | compile_diagnostic(DIAG_ERR|DIAG_FROM(here), /*“%s” can’t be both a station and a survey*/27, |
---|
[cab0f26] | 319 | sprint_prefix(ptr)); |
---|
[6430f0e] | 320 | } |
---|
[27b8b59] | 321 | if (!fSurvey && TSTBIT(pcs->infer, INFER_EXPORTS)) ptr->min_export = USHRT_MAX; |
---|
[051a72b] | 322 | } |
---|
| 323 | |
---|
[932f7e9] | 324 | /* check the export level */ |
---|
| 325 | #if 0 |
---|
| 326 | printf("R min %d max %d depth %d pfx %s\n", |
---|
| 327 | ptr->min_export, ptr->max_export, depth, sprint_prefix(ptr)); |
---|
| 328 | #endif |
---|
[c00c74a9] | 329 | if (ptr->min_export == 0 || ptr->min_export == USHRT_MAX) { |
---|
[932f7e9] | 330 | if (depth > ptr->max_export) ptr->max_export = depth; |
---|
| 331 | } else if (ptr->max_export < depth) { |
---|
[dd5b1a8] | 332 | prefix *survey = ptr; |
---|
| 333 | char *s; |
---|
[93e3492] | 334 | const char *p; |
---|
[dd5b1a8] | 335 | int level; |
---|
| 336 | for (level = ptr->max_export + 1; level; level--) { |
---|
| 337 | survey = survey->up; |
---|
[4c07c51] | 338 | SVX_ASSERT(survey); |
---|
[dd5b1a8] | 339 | } |
---|
| 340 | s = osstrdup(sprint_prefix(survey)); |
---|
[93e3492] | 341 | p = sprint_prefix(ptr); |
---|
[016068a] | 342 | if (survey->filename) { |
---|
[cab0f26] | 343 | compile_diagnostic_pfx(DIAG_ERR, survey, |
---|
| 344 | /*Station “%s” not exported from survey “%s”*/26, |
---|
| 345 | p, s); |
---|
[93e3492] | 346 | } else { |
---|
[cab0f26] | 347 | compile_diagnostic(DIAG_ERR, /*Station “%s” not exported from survey “%s”*/26, p, s); |
---|
[016068a] | 348 | } |
---|
[dd5b1a8] | 349 | osfree(s); |
---|
[932f7e9] | 350 | #if 0 |
---|
| 351 | printf(" *** pfx %s warning not exported enough depth %d " |
---|
[421b7d2] | 352 | "ptr->max_export %d\n", sprint_prefix(ptr), |
---|
[932f7e9] | 353 | depth, ptr->max_export); |
---|
| 354 | #endif |
---|
| 355 | } |
---|
[0fa7aac] | 356 | if (!fImplicitPrefix && (pfx_flags & PFX_WARN_SEPARATOR)) { |
---|
[5b4085b] | 357 | filepos fp_tmp; |
---|
| 358 | get_pos(&fp_tmp); |
---|
| 359 | set_pos(&fp_firstsep); |
---|
| 360 | compile_diagnostic(DIAG_WARN|DIAG_COL, /*Separator in survey name*/392); |
---|
| 361 | set_pos(&fp_tmp); |
---|
[0fa7aac] | 362 | } |
---|
[a420b49] | 363 | return ptr; |
---|
[d1b1380] | 364 | } |
---|
| 365 | |
---|
[725d3b1] | 366 | char * |
---|
| 367 | read_walls_prefix(void) |
---|
| 368 | { |
---|
| 369 | string name = S_INIT; |
---|
| 370 | skipblanks(); |
---|
| 371 | if (!isNames(ch)) |
---|
| 372 | return NULL; |
---|
| 373 | do { |
---|
[94d9f64] | 374 | s_appendch(&name, ch); |
---|
[725d3b1] | 375 | nextch(); |
---|
| 376 | } while (isNames(ch)); |
---|
| 377 | return s_steal(&name); |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | prefix * |
---|
[202e357] | 381 | read_walls_station(char * const walls_prefix[3], bool anon_allowed, bool *p_new) |
---|
[725d3b1] | 382 | { |
---|
[202e357] | 383 | if (p_new) *p_new = false; |
---|
[725d3b1] | 384 | // bool f_optional = false; //!!(pfx_flags & PFX_OPT); |
---|
| 385 | // bool fSuspectTypo = false; //!!(pfx_flags & PFX_SUSPECT_TYPO); |
---|
| 386 | // prefix *back_ptr, *ptr; |
---|
| 387 | string component = S_INIT; |
---|
| 388 | // size_t i; |
---|
| 389 | // bool fNew; |
---|
| 390 | // bool fImplicitPrefix = true; |
---|
| 391 | // int depth = -1; |
---|
| 392 | // filepos fp_firstsep; |
---|
| 393 | |
---|
| 394 | filepos fp; |
---|
| 395 | get_pos(&fp); |
---|
| 396 | |
---|
| 397 | skipblanks(); |
---|
| 398 | if (anon_allowed && ch == '-') { |
---|
| 399 | // - or -- is an anonymous wall point in a shot, but in #Fix they seem |
---|
| 400 | // to just be treated as ordinary station names. |
---|
| 401 | // FIXME: Issue warning for such a useless station? |
---|
| 402 | // |
---|
| 403 | // Not yet checked, but you can presumably use - and -- as a prefix |
---|
| 404 | // (FIXME check this). |
---|
| 405 | nextch(); |
---|
| 406 | int dashes = 1; |
---|
| 407 | if (ch == '-') { |
---|
| 408 | ++dashes; |
---|
| 409 | nextch(); |
---|
| 410 | } |
---|
| 411 | if (!isNames(ch) && ch != ':') { |
---|
| 412 | // An anonymous station implies the leg it is on is a splay. |
---|
| 413 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
---|
| 414 | set_pos(&fp); |
---|
| 415 | // Walls also rejects this case. |
---|
| 416 | compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Can't have a leg between two anonymous stations*/3); |
---|
[fdd4ed76] | 417 | longjmp(jbSkipLine, 1); |
---|
[725d3b1] | 418 | } |
---|
| 419 | pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY); |
---|
| 420 | prefix *pfx = new_anon_station(); |
---|
| 421 | pfx->sflags |= BIT(SFLAGS_WALL); |
---|
[202e357] | 422 | // An anonymous station is always new. |
---|
| 423 | if (p_new) *p_new = true; |
---|
[725d3b1] | 424 | return pfx; |
---|
| 425 | } |
---|
[94d9f64] | 426 | s_appendn(&component, dashes, '-'); |
---|
[725d3b1] | 427 | } |
---|
| 428 | |
---|
| 429 | char *w_prefix[3] = { NULL, NULL, NULL }; |
---|
| 430 | int explicit_prefix_levels = 0; |
---|
| 431 | while (true) { |
---|
| 432 | while (isNames(ch)) { |
---|
[94d9f64] | 433 | s_appendch(&component, ch); |
---|
[725d3b1] | 434 | nextch(); |
---|
| 435 | } |
---|
| 436 | //printf("component = '%s'\n", s_str(&component)); |
---|
| 437 | if (ch == ':') { |
---|
| 438 | nextch(); |
---|
| 439 | |
---|
| 440 | if (++explicit_prefix_levels > 3) { |
---|
| 441 | // FIXME Make this a proper error |
---|
| 442 | printf("too many prefix levels\n"); |
---|
| 443 | s_free(&component); |
---|
| 444 | for (int i = 0; i < 3; ++i) osfree(w_prefix[i]); |
---|
[fdd4ed76] | 445 | longjmp(jbSkipLine, 1); |
---|
[725d3b1] | 446 | } |
---|
| 447 | |
---|
| 448 | if (!s_empty(&component)) { |
---|
| 449 | // printf("w_prefix[%d] = '%s'\n", explicit_prefix_levels - 1, s_str(&component)); |
---|
| 450 | w_prefix[explicit_prefix_levels - 1] = s_steal(&component); |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | continue; |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | // printf("explicit_prefix_levels=%d %s:%s:%s\n", explicit_prefix_levels, w_prefix[0], w_prefix[1], w_prefix[2]); |
---|
| 457 | |
---|
| 458 | // component is the station name itself. |
---|
| 459 | if (s_empty(&component)) { |
---|
[1f34f48] | 460 | if (explicit_prefix_levels == 0) { |
---|
| 461 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Expecting station name*/28); |
---|
| 462 | s_free(&component); |
---|
| 463 | for (int i = 0; i < 3; ++i) osfree(w_prefix[i]); |
---|
[fdd4ed76] | 464 | longjmp(jbSkipLine, 1); |
---|
[1f34f48] | 465 | } |
---|
| 466 | // Walls allows an empty station name if there's an explicit prefix. |
---|
| 467 | // This seems unlikely to be intended, so warn about it. |
---|
| 468 | compile_diagnostic(DIAG_WARN|DIAG_COL, /*Expecting station name*/28); |
---|
| 469 | // Use a name with a space in so it can't collide with a real |
---|
| 470 | // Walls station name. |
---|
[94d9f64] | 471 | s_append(&component, "empty name"); |
---|
[725d3b1] | 472 | } |
---|
| 473 | int len = s_len(&component); |
---|
| 474 | char *p = s_steal(&component); |
---|
| 475 | // Apply case treatment. |
---|
| 476 | switch (pcs->Case) { |
---|
| 477 | case LOWER: |
---|
| 478 | for (int i = 0; i < len; ++i) |
---|
| 479 | p[i] = tolower((unsigned char)p[i]); |
---|
| 480 | break; |
---|
| 481 | case UPPER: |
---|
| 482 | for (int i = 0; i < len; ++i) |
---|
| 483 | p[i] = toupper((unsigned char)p[i]); |
---|
| 484 | break; |
---|
| 485 | case OFF: |
---|
| 486 | // Avoid unhandled enum warning. |
---|
| 487 | break; |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | prefix *ptr = root; |
---|
| 491 | for (int i = 0; i < 4; ++i) { |
---|
[ba84079] | 492 | char *name; |
---|
[725d3b1] | 493 | int sflag = BIT(SFLAGS_SURVEY); |
---|
| 494 | if (i == 3) { |
---|
| 495 | name = p; |
---|
[1bb4ba1] | 496 | sflag = 0; |
---|
[725d3b1] | 497 | } else { |
---|
| 498 | if (i < 3 - explicit_prefix_levels) { |
---|
| 499 | name = walls_prefix[i]; |
---|
| 500 | // printf("using walls_prefix[%d] = '%s'\n", 2 - i, name); |
---|
| 501 | } else { |
---|
| 502 | name = w_prefix[i - (3 - explicit_prefix_levels)]; // FIXME: Could steal wprefix[i]. |
---|
| 503 | // printf("using w_prefix[%d] = '%s'\n", i - (3 - explicit_prefix_levels), name); |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | if (name == NULL) { |
---|
| 507 | // FIXME: This means :X::Y is treated as the same as |
---|
| 508 | // ::X:Y but is that right? Walls docs don't really |
---|
| 509 | // say. Need to test (and is they're different then |
---|
| 510 | // probably use a character not valid in Walls station |
---|
| 511 | // names for the empty prefix level (e.g. space or |
---|
| 512 | // `#`). |
---|
| 513 | // |
---|
| 514 | // Also, does Walls allow :::X as a station and |
---|
| 515 | // ::X:Y which would mean X is a station and survey? |
---|
| 516 | // If so, we probably want to keep every empty level. |
---|
| 517 | continue; |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | prefix *back_ptr = ptr; |
---|
| 521 | ptr = ptr->down; |
---|
| 522 | if (ptr == NULL) { |
---|
| 523 | /* Special case first time around at each level */ |
---|
[202e357] | 524 | /* No need to check if we're at the station level - if the |
---|
| 525 | * prefix is new the station must be. */ |
---|
| 526 | if (p_new) *p_new = true; |
---|
[725d3b1] | 527 | ptr = osnew(prefix); |
---|
[ba84079] | 528 | ptr->sflags = sflag; |
---|
| 529 | if (strlen(name) < sizeof(ptr->ident.i)) { |
---|
| 530 | strcpy(ptr->ident.i, name); |
---|
| 531 | ptr->sflags |= BIT(SFLAGS_IDENT_INLINE); |
---|
| 532 | if (i >= 3) osfree(name); |
---|
| 533 | } else { |
---|
| 534 | ptr->ident.p = (i < 3 ? osstrdup(name) : name); |
---|
| 535 | } |
---|
[725d3b1] | 536 | name = NULL; |
---|
| 537 | ptr->right = ptr->down = NULL; |
---|
| 538 | ptr->pos = NULL; |
---|
| 539 | ptr->shape = 0; |
---|
| 540 | ptr->stn = NULL; |
---|
| 541 | ptr->up = back_ptr; |
---|
| 542 | ptr->filename = file.filename; // FIXME: Or location of #Prefix, etc for it? |
---|
| 543 | ptr->line = file.line; // FIXME: Or location of #Prefix, etc for it? |
---|
| 544 | ptr->min_export = ptr->max_export = 0; |
---|
| 545 | back_ptr->down = ptr; |
---|
| 546 | } else { |
---|
| 547 | /* Use caching to speed up adding an increasing sequence to a |
---|
| 548 | * large survey */ |
---|
| 549 | static prefix *cached_survey = NULL, *cached_station = NULL; |
---|
| 550 | prefix *ptrPrev = NULL; |
---|
| 551 | int cmp = 1; /* result of strcmp ( -ve for <, 0 for =, +ve for > ) */ |
---|
| 552 | if (cached_survey == back_ptr) { |
---|
[ba84079] | 553 | cmp = strcmp(prefix_ident(cached_station), name); |
---|
[725d3b1] | 554 | if (cmp <= 0) ptr = cached_station; |
---|
| 555 | } |
---|
[ba84079] | 556 | while (ptr && (cmp = strcmp(prefix_ident(ptr), name))<0) { |
---|
[725d3b1] | 557 | ptrPrev = ptr; |
---|
| 558 | ptr = ptr->right; |
---|
| 559 | } |
---|
| 560 | if (cmp) { |
---|
| 561 | /* ie we got to one that was higher, or the end */ |
---|
[202e357] | 562 | if (p_new) *p_new = true; |
---|
| 563 | prefix *newptr = osnew(prefix); |
---|
[ba84079] | 564 | newptr->sflags = sflag; |
---|
| 565 | if (strlen(name) < sizeof(newptr->ident.i)) { |
---|
| 566 | strcpy(newptr->ident.i, name); |
---|
| 567 | newptr->sflags |= BIT(SFLAGS_IDENT_INLINE); |
---|
| 568 | if (i >= 3) osfree(name); |
---|
| 569 | } else { |
---|
| 570 | newptr->ident.p = (i < 3 ? osstrdup(name) : name); |
---|
| 571 | } |
---|
[725d3b1] | 572 | name = NULL; |
---|
| 573 | if (ptrPrev == NULL) |
---|
| 574 | back_ptr->down = newptr; |
---|
| 575 | else |
---|
| 576 | ptrPrev->right = newptr; |
---|
| 577 | newptr->right = ptr; |
---|
| 578 | newptr->down = NULL; |
---|
| 579 | newptr->pos = NULL; |
---|
| 580 | newptr->shape = 0; |
---|
| 581 | newptr->stn = NULL; |
---|
| 582 | newptr->up = back_ptr; |
---|
| 583 | newptr->filename = file.filename; // FIXME |
---|
| 584 | newptr->line = file.line; |
---|
| 585 | newptr->min_export = newptr->max_export = 0; |
---|
| 586 | ptr = newptr; |
---|
| 587 | } else { |
---|
[116b485] | 588 | ptr->sflags |= sflag; |
---|
[725d3b1] | 589 | } |
---|
[1bb4ba1] | 590 | if (!TSTBIT(ptr->sflags, SFLAGS_SURVEY)) { |
---|
| 591 | ptr->min_export = USHRT_MAX; |
---|
| 592 | } |
---|
[725d3b1] | 593 | cached_survey = back_ptr; |
---|
| 594 | cached_station = ptr; |
---|
| 595 | } |
---|
| 596 | if (name == p) osfree(p); |
---|
| 597 | } |
---|
| 598 | |
---|
[1bb4ba1] | 599 | // Do the equivalent of "*infer exports" for Walls stations with an |
---|
| 600 | // explicit prefix. |
---|
| 601 | if (ptr->min_export == 0 || ptr->min_export == USHRT_MAX) { |
---|
| 602 | if (explicit_prefix_levels > ptr->max_export) |
---|
| 603 | ptr->max_export = explicit_prefix_levels; |
---|
| 604 | } |
---|
[725d3b1] | 605 | |
---|
| 606 | for (int i = 0; i < 3; ++i) osfree(w_prefix[i]); |
---|
| 607 | |
---|
| 608 | return ptr; |
---|
| 609 | } |
---|
| 610 | } |
---|
| 611 | |
---|
[21c226e] | 612 | /* if numeric expr is omitted: if f_optional return HUGE_REAL, else longjmp */ |
---|
[725d3b1] | 613 | real |
---|
[5a0ab6a] | 614 | read_number(bool f_optional, bool f_unsigned) |
---|
[a420b49] | 615 | { |
---|
[63d4f07] | 616 | bool fPositive = true, fDigits = false; |
---|
[a420b49] | 617 | real n = (real)0.0; |
---|
[c80bd34] | 618 | filepos fp; |
---|
| 619 | int ch_old; |
---|
[a420b49] | 620 | |
---|
[c80bd34] | 621 | get_pos(&fp); |
---|
| 622 | ch_old = ch; |
---|
[5a0ab6a] | 623 | if (!f_unsigned) { |
---|
| 624 | fPositive = !isMinus(ch); |
---|
| 625 | if (isSign(ch)) nextch(); |
---|
| 626 | } |
---|
[a420b49] | 627 | |
---|
| 628 | while (isdigit(ch)) { |
---|
[6974a34] | 629 | n = n * (real)10.0 + (char)(ch - '0'); |
---|
[d1b1380] | 630 | nextch(); |
---|
[63d4f07] | 631 | fDigits = true; |
---|
[a420b49] | 632 | } |
---|
| 633 | |
---|
| 634 | if (isDecimal(ch)) { |
---|
| 635 | real mult = (real)1.0; |
---|
| 636 | nextch(); |
---|
| 637 | while (isdigit(ch)) { |
---|
| 638 | mult *= (real).1; |
---|
| 639 | n += (char)(ch - '0') * mult; |
---|
[63d4f07] | 640 | fDigits = true; |
---|
[a420b49] | 641 | nextch(); |
---|
| 642 | } |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | /* !'fRead' => !fDigits so fDigits => 'fRead' */ |
---|
| 646 | if (fDigits) return (fPositive ? n : -n); |
---|
| 647 | |
---|
| 648 | /* didn't read a valid number. If it's optional, reset filepos & return */ |
---|
[4fb15a1] | 649 | set_pos(&fp); |
---|
[21c226e] | 650 | if (f_optional) { |
---|
[a420b49] | 651 | return HUGE_REAL; |
---|
| 652 | } |
---|
| 653 | |
---|
[c80bd34] | 654 | if (isOmit(ch_old)) { |
---|
[cab0f26] | 655 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Field may not be omitted*/8); |
---|
[647407d] | 656 | } else { |
---|
[cab0f26] | 657 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found “%s”*/9); |
---|
[647407d] | 658 | } |
---|
[fdd4ed76] | 659 | longjmp(jbSkipLine, 1); |
---|
[d1b1380] | 660 | } |
---|
[647407d] | 661 | |
---|
[725d3b1] | 662 | real |
---|
[4ca3219] | 663 | read_quadrant(bool f_optional) |
---|
| 664 | { |
---|
[5a0ab6a] | 665 | enum { |
---|
| 666 | POINT_N = 0, |
---|
| 667 | POINT_E = 1, |
---|
| 668 | POINT_S = 2, |
---|
| 669 | POINT_W = 3, |
---|
| 670 | POINT_NONE = -1 |
---|
| 671 | }; |
---|
| 672 | static const sztok pointtab[] = { |
---|
| 673 | {"E", POINT_E }, |
---|
| 674 | {"N", POINT_N }, |
---|
| 675 | {"S", POINT_S }, |
---|
| 676 | {"W", POINT_W }, |
---|
| 677 | {NULL, POINT_NONE } |
---|
| 678 | }; |
---|
| 679 | static const sztok pointewtab[] = { |
---|
| 680 | {"E", POINT_E }, |
---|
| 681 | {"W", POINT_W }, |
---|
| 682 | {NULL, POINT_NONE } |
---|
| 683 | }; |
---|
| 684 | if (f_optional && isOmit(ch)) { |
---|
| 685 | return HUGE_REAL; |
---|
| 686 | } |
---|
[9d9bee9] | 687 | const int quad = 90; |
---|
| 688 | filepos fp; |
---|
| 689 | get_pos(&fp); |
---|
[e1cbc0d] | 690 | get_token_legacy_no_blanks(); |
---|
[5a0ab6a] | 691 | int first_point = match_tok(pointtab, TABSIZE(pointtab)); |
---|
| 692 | if (first_point == POINT_NONE) { |
---|
| 693 | set_pos(&fp); |
---|
| 694 | if (isOmit(ch)) { |
---|
| 695 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Field may not be omitted*/8); |
---|
| 696 | } |
---|
| 697 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting quadrant bearing, found “%s”*/483); |
---|
[fdd4ed76] | 698 | longjmp(jbSkipLine, 1); |
---|
[9d9bee9] | 699 | } |
---|
[63d4f07] | 700 | real r = read_number(true, true); |
---|
[5a0ab6a] | 701 | if (r == HUGE_REAL) { |
---|
| 702 | if (isSign(ch) || isDecimal(ch)) { |
---|
| 703 | /* Give better errors for S-0E, N+10W, N.E, etc. */ |
---|
[9d9bee9] | 704 | set_pos(&fp); |
---|
[728874f9] | 705 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting quadrant bearing, found “%s”*/483); |
---|
[fdd4ed76] | 706 | longjmp(jbSkipLine, 1); |
---|
[9d9bee9] | 707 | } |
---|
[5a0ab6a] | 708 | /* N, S, E or W. */ |
---|
| 709 | return first_point * quad; |
---|
| 710 | } |
---|
| 711 | if (first_point == POINT_E || first_point == POINT_W) { |
---|
[9d9bee9] | 712 | set_pos(&fp); |
---|
[728874f9] | 713 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting quadrant bearing, found “%s”*/483); |
---|
[fdd4ed76] | 714 | longjmp(jbSkipLine, 1); |
---|
[4ca3219] | 715 | } |
---|
[5a0ab6a] | 716 | |
---|
[e1cbc0d] | 717 | get_token_legacy_no_blanks(); |
---|
[eb429bf] | 718 | int second_point = match_tok(pointewtab, TABSIZE(pointewtab)); |
---|
| 719 | if (second_point == POINT_NONE) { |
---|
[5a0ab6a] | 720 | set_pos(&fp); |
---|
| 721 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting quadrant bearing, found “%s”*/483); |
---|
[fdd4ed76] | 722 | longjmp(jbSkipLine, 1); |
---|
[4ca3219] | 723 | } |
---|
[5a0ab6a] | 724 | |
---|
[eb429bf] | 725 | if (r > quad) { |
---|
[5a0ab6a] | 726 | set_pos(&fp); |
---|
[501dd27] | 727 | compile_diagnostic_token_show(DIAG_ERR, /*Suspicious compass reading*/59); |
---|
[fdd4ed76] | 728 | longjmp(jbSkipLine, 1); |
---|
[4ca3219] | 729 | } |
---|
[5a0ab6a] | 730 | |
---|
| 731 | if (first_point == POINT_N) { |
---|
| 732 | if (second_point == POINT_W) { |
---|
| 733 | r = quad * 4 - r; |
---|
| 734 | } |
---|
| 735 | } else { |
---|
| 736 | if (second_point == POINT_W) { |
---|
| 737 | r += quad * 2; |
---|
| 738 | } else { |
---|
| 739 | r = quad * 2 - r; |
---|
| 740 | } |
---|
| 741 | } |
---|
| 742 | return r; |
---|
[4ca3219] | 743 | } |
---|
| 744 | |
---|
[21c226e] | 745 | extern real |
---|
[d3241e6] | 746 | read_numeric(bool f_optional) |
---|
| 747 | { |
---|
| 748 | skipblanks(); |
---|
[63d4f07] | 749 | return read_number(f_optional, false); |
---|
[d3241e6] | 750 | } |
---|
| 751 | |
---|
| 752 | extern real |
---|
[61513b57] | 753 | read_numeric_multi(bool f_optional, bool f_quadrants, int *p_n_readings) |
---|
[21c226e] | 754 | { |
---|
| 755 | size_t n_readings = 0; |
---|
| 756 | real tot = (real)0.0; |
---|
| 757 | |
---|
| 758 | skipblanks(); |
---|
| 759 | if (!isOpen(ch)) { |
---|
[4699c40] | 760 | real r = 0; |
---|
[05b9de76] | 761 | if (!f_quadrants) { |
---|
[63d4f07] | 762 | r = read_number(f_optional, false); |
---|
[05b9de76] | 763 | } else { |
---|
[5a0ab6a] | 764 | r = read_quadrant(f_optional); |
---|
[05b9de76] | 765 | if (r != HUGE_REAL) |
---|
| 766 | do_legacy_token_warning(); |
---|
| 767 | } |
---|
[21c226e] | 768 | if (p_n_readings) *p_n_readings = (r == HUGE_REAL ? 0 : 1); |
---|
| 769 | return r; |
---|
| 770 | } |
---|
| 771 | nextch(); |
---|
| 772 | |
---|
| 773 | skipblanks(); |
---|
| 774 | do { |
---|
[05b9de76] | 775 | if (!f_quadrants) { |
---|
[63d4f07] | 776 | tot += read_number(false, false); |
---|
[05b9de76] | 777 | } else { |
---|
[63d4f07] | 778 | tot += read_quadrant(false); |
---|
[05b9de76] | 779 | do_legacy_token_warning(); |
---|
| 780 | } |
---|
[21c226e] | 781 | ++n_readings; |
---|
| 782 | skipblanks(); |
---|
| 783 | } while (!isClose(ch)); |
---|
| 784 | nextch(); |
---|
| 785 | |
---|
| 786 | if (p_n_readings) *p_n_readings = n_readings; |
---|
| 787 | /* FIXME: special averaging for bearings ... */ |
---|
| 788 | /* And for percentage gradient */ |
---|
| 789 | return tot / n_readings; |
---|
| 790 | } |
---|
| 791 | |
---|
[2aa2f3f] | 792 | /* read numeric expr or omit (return HUGE_REAL); else longjmp */ |
---|
| 793 | extern real |
---|
[00b10c1] | 794 | read_bearing_multi_or_omit(bool f_quadrants, int *p_n_readings) |
---|
[2aa2f3f] | 795 | { |
---|
[9d9bee9] | 796 | real v; |
---|
[63d4f07] | 797 | v = read_numeric_multi(true, f_quadrants, p_n_readings); |
---|
[9d9bee9] | 798 | if (v == HUGE_REAL) { |
---|
| 799 | if (!isOmit(ch)) { |
---|
| 800 | compile_diagnostic_token_show(DIAG_ERR, /*Expecting numeric field, found “%s”*/9); |
---|
[fdd4ed76] | 801 | longjmp(jbSkipLine, 1); |
---|
[2aa2f3f] | 802 | } |
---|
| 803 | nextch(); |
---|
| 804 | } |
---|
| 805 | return v; |
---|
| 806 | } |
---|
| 807 | |
---|
[950a829] | 808 | /* Don't skip blanks, variable error code */ |
---|
| 809 | static unsigned int |
---|
[75e67ab] | 810 | read_uint_internal(int errmsg, const filepos *fp) |
---|
[647407d] | 811 | { |
---|
[421b7d2] | 812 | unsigned int n = 0; |
---|
[647407d] | 813 | if (!isdigit(ch)) { |
---|
[029cc3f] | 814 | if (fp) set_pos(fp); |
---|
[cab0f26] | 815 | compile_diagnostic_token_show(DIAG_ERR, errmsg); |
---|
[fdd4ed76] | 816 | longjmp(jbSkipLine, 1); |
---|
[647407d] | 817 | } |
---|
| 818 | while (isdigit(ch)) { |
---|
| 819 | n = n * 10 + (char)(ch - '0'); |
---|
| 820 | nextch(); |
---|
| 821 | } |
---|
| 822 | return n; |
---|
| 823 | } |
---|
[76bb277a] | 824 | |
---|
[950a829] | 825 | extern unsigned int |
---|
| 826 | read_uint(void) |
---|
| 827 | { |
---|
| 828 | skipblanks(); |
---|
[0804fbe] | 829 | return read_uint_internal(/*Expecting numeric field, found “%s”*/9, NULL); |
---|
[950a829] | 830 | } |
---|
| 831 | |
---|
[5b9b9d4] | 832 | extern int |
---|
| 833 | read_int(int min_val, int max_val) |
---|
| 834 | { |
---|
| 835 | skipblanks(); |
---|
| 836 | unsigned n = 0; |
---|
| 837 | filepos fp; |
---|
| 838 | |
---|
| 839 | get_pos(&fp); |
---|
[1000d9d] | 840 | bool negated = isMinus(ch); |
---|
| 841 | unsigned limit; |
---|
| 842 | if (negated) { |
---|
| 843 | limit = (unsigned)(min_val == INT_MIN ? INT_MIN : -min_val); |
---|
| 844 | } else { |
---|
| 845 | limit = (unsigned)max_val; |
---|
| 846 | } |
---|
[5b9b9d4] | 847 | if (isSign(ch)) nextch(); |
---|
| 848 | |
---|
| 849 | if (!isdigit(ch)) { |
---|
| 850 | bad_value: |
---|
| 851 | set_pos(&fp); |
---|
| 852 | /* TRANSLATORS: The first %d will be replaced by the (inclusive) lower |
---|
| 853 | * bound and the second by the (inclusive) upper bound, for example: |
---|
| 854 | * Expecting integer in range -60 to 60 |
---|
| 855 | */ |
---|
| 856 | compile_diagnostic(DIAG_ERR|DIAG_NUM, /*Expecting integer in range %d to %d*/489); |
---|
[fdd4ed76] | 857 | longjmp(jbSkipLine, 1); |
---|
[5b9b9d4] | 858 | } |
---|
| 859 | |
---|
| 860 | while (isdigit(ch)) { |
---|
| 861 | unsigned old_n = n; |
---|
| 862 | n = n * 10 + (char)(ch - '0'); |
---|
| 863 | if (n > limit || n < old_n) { |
---|
| 864 | goto bad_value; |
---|
| 865 | } |
---|
| 866 | nextch(); |
---|
| 867 | } |
---|
| 868 | if (isDecimal(ch)) goto bad_value; |
---|
| 869 | |
---|
| 870 | if (negated) { |
---|
| 871 | if (n > (unsigned)INT_MAX) { |
---|
| 872 | // Avoid unportable casting. |
---|
| 873 | return INT_MIN; |
---|
| 874 | } |
---|
| 875 | return -(int)n; |
---|
| 876 | } |
---|
| 877 | return (int)n; |
---|
| 878 | } |
---|
| 879 | |
---|
[76bb277a] | 880 | extern void |
---|
[0532954] | 881 | read_string(string *pstr) |
---|
[76bb277a] | 882 | { |
---|
[0532954] | 883 | s_clear(pstr); |
---|
[76bb277a] | 884 | |
---|
| 885 | skipblanks(); |
---|
| 886 | if (ch == '\"') { |
---|
[1157304] | 887 | /* String quoted in "" */ |
---|
[76bb277a] | 888 | nextch(); |
---|
[457a390] | 889 | while (1) { |
---|
| 890 | if (isEol(ch)) { |
---|
[cab0f26] | 891 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Missing \"*/69); |
---|
[fdd4ed76] | 892 | longjmp(jbSkipLine, 1); |
---|
[457a390] | 893 | } |
---|
| 894 | |
---|
| 895 | if (ch == '\"') break; |
---|
| 896 | |
---|
[94d9f64] | 897 | s_appendch(pstr, ch); |
---|
[457a390] | 898 | nextch(); |
---|
| 899 | } |
---|
[66f5fc4] | 900 | nextch(); |
---|
[457a390] | 901 | } else { |
---|
[1157304] | 902 | /* Unquoted string */ |
---|
[457a390] | 903 | while (1) { |
---|
| 904 | if (isEol(ch) || isComm(ch)) { |
---|
[0532954] | 905 | if (s_empty(pstr)) { |
---|
[cab0f26] | 906 | compile_diagnostic(DIAG_ERR|DIAG_COL, /*Expecting string field*/121); |
---|
[fdd4ed76] | 907 | longjmp(jbSkipLine, 1); |
---|
[b97d134] | 908 | } |
---|
[457a390] | 909 | return; |
---|
[76bb277a] | 910 | } |
---|
| 911 | |
---|
[457a390] | 912 | if (isBlank(ch)) break; |
---|
[76bb277a] | 913 | |
---|
[94d9f64] | 914 | s_appendch(pstr, ch); |
---|
[457a390] | 915 | nextch(); |
---|
| 916 | } |
---|
[76bb277a] | 917 | } |
---|
| 918 | } |
---|
[950a829] | 919 | |
---|
| 920 | extern void |
---|
[b5a3219] | 921 | read_date(int *py, int *pm, int *pd) |
---|
[950a829] | 922 | { |
---|
[734f5f4] | 923 | unsigned int y = 0, m = 0, d = 0; |
---|
[1d74718] | 924 | filepos fp_date; |
---|
[950a829] | 925 | |
---|
| 926 | skipblanks(); |
---|
[029cc3f] | 927 | |
---|
[1d74718] | 928 | get_pos(&fp_date); |
---|
| 929 | y = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
[e0c7cd1] | 930 | /* Two digit year is 19xx. */ |
---|
[734f5f4] | 931 | if (y < 100) { |
---|
| 932 | filepos fp_save; |
---|
| 933 | get_pos(&fp_save); |
---|
| 934 | y += 1900; |
---|
| 935 | set_pos(&fp_date); |
---|
| 936 | /* TRANSLATORS: %d will be replaced by the assumed year, e.g. 1918 */ |
---|
| 937 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Assuming 2 digit year is %d*/76, y); |
---|
| 938 | set_pos(&fp_save); |
---|
| 939 | } |
---|
[1ee204e] | 940 | if (y < 1900 || y > 2078) { |
---|
[1d74718] | 941 | set_pos(&fp_date); |
---|
[c270761] | 942 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid year (< 1900 or > 2078)*/58); |
---|
[fdd4ed76] | 943 | longjmp(jbSkipLine, 1); |
---|
[e0c7cd1] | 944 | } |
---|
[950a829] | 945 | if (ch == '.') { |
---|
[1d74718] | 946 | filepos fp; |
---|
[950a829] | 947 | nextch(); |
---|
[1d74718] | 948 | get_pos(&fp); |
---|
| 949 | m = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
[e0c7cd1] | 950 | if (m < 1 || m > 12) { |
---|
[1d74718] | 951 | set_pos(&fp); |
---|
[c270761] | 952 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid month*/86); |
---|
[fdd4ed76] | 953 | longjmp(jbSkipLine, 1); |
---|
[79d84d9] | 954 | } |
---|
[950a829] | 955 | if (ch == '.') { |
---|
| 956 | nextch(); |
---|
[1d74718] | 957 | get_pos(&fp); |
---|
| 958 | d = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
[e0c7cd1] | 959 | if (d < 1 || d > last_day(y, m)) { |
---|
[1d74718] | 960 | set_pos(&fp); |
---|
[736f7df] | 961 | /* TRANSLATORS: e.g. 31st of April, or 32nd of any month */ |
---|
[c270761] | 962 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid day of the month*/87); |
---|
[fdd4ed76] | 963 | longjmp(jbSkipLine, 1); |
---|
[e0c7cd1] | 964 | } |
---|
[950a829] | 965 | } |
---|
| 966 | } |
---|
[b5a3219] | 967 | if (py) *py = y; |
---|
| 968 | if (pm) *pm = m; |
---|
| 969 | if (pd) *pd = d; |
---|
[950a829] | 970 | } |
---|
[725d3b1] | 971 | |
---|
| 972 | extern void |
---|
| 973 | read_walls_srv_date(int *py, int *pm, int *pd) |
---|
| 974 | { |
---|
| 975 | skipblanks(); |
---|
| 976 | |
---|
| 977 | filepos fp_date; |
---|
| 978 | get_pos(&fp_date); |
---|
| 979 | unsigned y = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
| 980 | int separator = -2; |
---|
| 981 | if (ch == '-' || ch == '/') { |
---|
| 982 | separator = ch; |
---|
| 983 | nextch(); |
---|
| 984 | } |
---|
| 985 | filepos fp_month; |
---|
| 986 | get_pos(&fp_month); |
---|
| 987 | unsigned m = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
| 988 | if (ch == separator) { |
---|
| 989 | nextch(); |
---|
| 990 | } |
---|
| 991 | filepos fp_day; |
---|
| 992 | get_pos(&fp_day); |
---|
| 993 | unsigned d = read_uint_internal(/*Expecting date, found “%s”*/198, &fp_date); |
---|
| 994 | |
---|
| 995 | filepos fp_year; |
---|
| 996 | if (y < 100) { |
---|
| 997 | // Walls recommends ISO 8601 date format (yyyy-mm-dd and seemingly the |
---|
| 998 | // non-standard variant yyyy/mm/dd), but also accepts "some date formats |
---|
| 999 | // common in the U.S. (mm/dd/yy, mm-dd-yyyy, etc.)" |
---|
| 1000 | unsigned tmp = y; |
---|
| 1001 | y = d; |
---|
| 1002 | fp_year = fp_day; |
---|
| 1003 | d = m; |
---|
| 1004 | fp_day = fp_month; |
---|
| 1005 | m = tmp; |
---|
| 1006 | fp_month = fp_date; |
---|
| 1007 | |
---|
| 1008 | if (y < 100) { |
---|
| 1009 | // FIXME: Are all 2 digit years 19xx? |
---|
| 1010 | y += 1900; |
---|
| 1011 | |
---|
| 1012 | filepos fp_save; |
---|
| 1013 | get_pos(&fp_save); |
---|
| 1014 | set_pos(&fp_year); |
---|
| 1015 | /* TRANSLATORS: %d will be replaced by the assumed year, e.g. 1918 */ |
---|
| 1016 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Assuming 2 digit year is %d*/76, y); |
---|
| 1017 | set_pos(&fp_save); |
---|
| 1018 | } |
---|
| 1019 | } else { |
---|
| 1020 | if (y < 1900 || y > 2078) { |
---|
| 1021 | set_pos(&fp_date); |
---|
| 1022 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid year (< 1900 or > 2078)*/58); |
---|
[fdd4ed76] | 1023 | longjmp(jbSkipLine, 1); |
---|
[725d3b1] | 1024 | } |
---|
| 1025 | fp_year = fp_date; |
---|
| 1026 | } |
---|
| 1027 | |
---|
| 1028 | if (m < 1 || m > 12) { |
---|
| 1029 | set_pos(&fp_month); |
---|
| 1030 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid month*/86); |
---|
[fdd4ed76] | 1031 | longjmp(jbSkipLine, 1); |
---|
[725d3b1] | 1032 | } |
---|
| 1033 | |
---|
| 1034 | if (d < 1 || d > last_day(y, m)) { |
---|
| 1035 | set_pos(&fp_day); |
---|
| 1036 | /* TRANSLATORS: e.g. 31st of April, or 32nd of any month */ |
---|
| 1037 | compile_diagnostic(DIAG_WARN|DIAG_UINT, /*Invalid day of the month*/87); |
---|
[fdd4ed76] | 1038 | longjmp(jbSkipLine, 1); |
---|
[725d3b1] | 1039 | } |
---|
| 1040 | |
---|
| 1041 | if (py) *py = y; |
---|
| 1042 | if (pm) *pm = m; |
---|
| 1043 | if (pd) *pd = d; |
---|
| 1044 | } |
---|