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