| 1 | /* readval.c |
|---|
| 2 | * Routines to read a prefix or number from the current input file |
|---|
| 3 | * Copyright (C) 1991-2003,2005,2006,2010,2011,2012,2013,2014 Olly Betts |
|---|
| 4 | * |
|---|
| 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. |
|---|
| 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 |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 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 |
|---|
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifdef HAVE_CONFIG_H |
|---|
| 21 | # include <config.h> |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | #include <limits.h> |
|---|
| 25 | #include <stddef.h> /* for offsetof */ |
|---|
| 26 | |
|---|
| 27 | #include "cavern.h" |
|---|
| 28 | #include "date.h" |
|---|
| 29 | #include "debug.h" |
|---|
| 30 | #include "filename.h" |
|---|
| 31 | #include "message.h" |
|---|
| 32 | #include "readval.h" |
|---|
| 33 | #include "datain.h" |
|---|
| 34 | #include "netbits.h" |
|---|
| 35 | #include "osalloc.h" |
|---|
| 36 | #include "str.h" |
|---|
| 37 | |
|---|
| 38 | #ifdef HAVE_SETJMP_H |
|---|
| 39 | # define LONGJMP(JB) longjmp((JB), 1) |
|---|
| 40 | #else |
|---|
| 41 | # define LONGJMP(JB) exit(1) |
|---|
| 42 | #endif |
|---|
| 43 | |
|---|
| 44 | int root_depr_count = 0; |
|---|
| 45 | |
|---|
| 46 | static prefix * |
|---|
| 47 | new_anon_station(void) |
|---|
| 48 | { |
|---|
| 49 | prefix *name = osnew(prefix); |
|---|
| 50 | name->pos = NULL; |
|---|
| 51 | name->ident = NULL; |
|---|
| 52 | name->shape = 0; |
|---|
| 53 | name->stn = NULL; |
|---|
| 54 | name->up = pcs->Prefix; |
|---|
| 55 | name->down = NULL; |
|---|
| 56 | name->filename = file.filename; |
|---|
| 57 | name->line = file.line; |
|---|
| 58 | if (TSTBIT(pcs->infer, INFER_EXPORTS)) { |
|---|
| 59 | name->min_export = USHRT_MAX; |
|---|
| 60 | } else { |
|---|
| 61 | name->min_export = 0; |
|---|
| 62 | } |
|---|
| 63 | name->max_export = 0; |
|---|
| 64 | name->sflags = BIT(SFLAGS_ANON); |
|---|
| 65 | /* Keep linked list of anon stations for node stats. */ |
|---|
| 66 | name->right = anon_list; |
|---|
| 67 | anon_list = name; |
|---|
| 68 | return name; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /* if prefix is omitted: if PFX_OPT set return NULL, otherwise use longjmp */ |
|---|
| 72 | extern prefix * |
|---|
| 73 | read_prefix(unsigned pfx_flags) |
|---|
| 74 | { |
|---|
| 75 | bool f_optional = !!(pfx_flags & PFX_OPT); |
|---|
| 76 | bool fSurvey = !!(pfx_flags & PFX_SURVEY); |
|---|
| 77 | bool fSuspectTypo = !!(pfx_flags & PFX_SUSPECT_TYPO); |
|---|
| 78 | prefix *back_ptr, *ptr; |
|---|
| 79 | char *name; |
|---|
| 80 | size_t name_len = 32; |
|---|
| 81 | size_t i; |
|---|
| 82 | bool fNew; |
|---|
| 83 | bool fImplicitPrefix = fTrue; |
|---|
| 84 | int depth = -1; |
|---|
| 85 | |
|---|
| 86 | skipblanks(); |
|---|
| 87 | #ifndef NO_DEPRECATED |
|---|
| 88 | if (isRoot(ch)) { |
|---|
| 89 | if (!(pfx_flags & PFX_ALLOW_ROOT)) { |
|---|
| 90 | compile_error(-/*ROOT is deprecated*/25); |
|---|
| 91 | LONGJMP(file.jbSkipLine); |
|---|
| 92 | } |
|---|
| 93 | if (root_depr_count < 5) { |
|---|
| 94 | compile_warning(-/*ROOT is deprecated*/25); |
|---|
| 95 | if (++root_depr_count == 5) |
|---|
| 96 | compile_warning(/*Further uses of this deprecated feature will not be reported*/95); |
|---|
| 97 | } |
|---|
| 98 | nextch(); |
|---|
| 99 | ptr = root; |
|---|
| 100 | if (!isNames(ch)) { |
|---|
| 101 | if (!isSep(ch)) return ptr; |
|---|
| 102 | /* Allow optional SEPARATOR after ROOT */ |
|---|
| 103 | nextch(); |
|---|
| 104 | } |
|---|
| 105 | fImplicitPrefix = fFalse; |
|---|
| 106 | #else |
|---|
| 107 | if (0) { |
|---|
| 108 | #endif |
|---|
| 109 | } else { |
|---|
| 110 | if ((pfx_flags & PFX_ANON) && |
|---|
| 111 | (isSep(ch) || (pcs->dash_for_anon_wall_station && ch == '-'))) { |
|---|
| 112 | int first_ch = ch; |
|---|
| 113 | filepos here; |
|---|
| 114 | get_pos(&here); |
|---|
| 115 | nextch(); |
|---|
| 116 | if (isBlank(ch) || isEol(ch)) { |
|---|
| 117 | if (!isSep(first_ch)) |
|---|
| 118 | goto anon_wall_station; |
|---|
| 119 | /* A single separator alone ('.' by default) is an anonymous |
|---|
| 120 | * station which is on a point inside the passage and implies |
|---|
| 121 | * the leg to it is a splay. |
|---|
| 122 | */ |
|---|
| 123 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
|---|
| 124 | compile_error(-/*Can't have a leg between two anonymous stations*/3); |
|---|
| 125 | LONGJMP(file.jbSkipLine); |
|---|
| 126 | } |
|---|
| 127 | pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY); |
|---|
| 128 | return new_anon_station(); |
|---|
| 129 | } |
|---|
| 130 | if (isSep(first_ch) && ch == first_ch) { |
|---|
| 131 | nextch(); |
|---|
| 132 | if (isBlank(ch) || isEol(ch)) { |
|---|
| 133 | /* A double separator ('..' by default) is an anonymous station |
|---|
| 134 | * which is on the wall and implies the leg to it is a splay. |
|---|
| 135 | */ |
|---|
| 136 | prefix * pfx; |
|---|
| 137 | anon_wall_station: |
|---|
| 138 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
|---|
| 139 | compile_error(-/*Can't have a leg between two anonymous stations*/3); |
|---|
| 140 | LONGJMP(file.jbSkipLine); |
|---|
| 141 | } |
|---|
| 142 | pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY); |
|---|
| 143 | pfx = new_anon_station(); |
|---|
| 144 | pfx->sflags |= BIT(SFLAGS_WALL); |
|---|
| 145 | return pfx; |
|---|
| 146 | } |
|---|
| 147 | if (ch == first_ch) { |
|---|
| 148 | nextch(); |
|---|
| 149 | if (isBlank(ch) || isEol(ch)) { |
|---|
| 150 | /* A triple separator ('...' by default) is an anonymous |
|---|
| 151 | * station, but otherwise not handled specially (e.g. for |
|---|
| 152 | * a single leg down an unexplored side passage to a station |
|---|
| 153 | * which isn't refindable). |
|---|
| 154 | */ |
|---|
| 155 | if (TSTBIT(pcs->flags, FLAGS_ANON_ONE_END)) { |
|---|
| 156 | compile_error(-/*Can't have a leg between two anonymous stations*/3); |
|---|
| 157 | LONGJMP(file.jbSkipLine); |
|---|
| 158 | } |
|---|
| 159 | pcs->flags |= BIT(FLAGS_ANON_ONE_END); |
|---|
| 160 | return new_anon_station(); |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | set_pos(&here); |
|---|
| 165 | } |
|---|
| 166 | ptr = pcs->Prefix; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | i = 0; |
|---|
| 170 | name = NULL; |
|---|
| 171 | do { |
|---|
| 172 | fNew = fFalse; |
|---|
| 173 | if (name == NULL) { |
|---|
| 174 | /* Need a new name buffer */ |
|---|
| 175 | name = osmalloc(name_len); |
|---|
| 176 | } |
|---|
| 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 */ |
|---|
| 185 | name[i++] = (pcs->Case == LOWER ? tolower(ch) : |
|---|
| 186 | (pcs->Case == OFF ? ch : toupper(ch))); |
|---|
| 187 | if (i >= name_len) { |
|---|
| 188 | name_len = name_len + name_len; |
|---|
| 189 | name = osrealloc(name, name_len); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | nextch(); |
|---|
| 193 | } |
|---|
| 194 | if (isSep(ch)) fImplicitPrefix = fFalse; |
|---|
| 195 | if (i == 0) { |
|---|
| 196 | osfree(name); |
|---|
| 197 | if (!f_optional) { |
|---|
| 198 | if (isEol(ch)) { |
|---|
| 199 | if (fSurvey) { |
|---|
| 200 | compile_error(-/*Expecting survey name*/89); |
|---|
| 201 | } else { |
|---|
| 202 | compile_error(-/*Expecting station name*/28); |
|---|
| 203 | } |
|---|
| 204 | } else { |
|---|
| 205 | /* TRANSLATORS: Here "station" is a survey station, not a train station. */ |
|---|
| 206 | compile_error(-/*Character “%c” not allowed in station name (use *SET NAMES to set allowed characters)*/7, ch); |
|---|
| 207 | } |
|---|
| 208 | LONGJMP(file.jbSkipLine); |
|---|
| 209 | } |
|---|
| 210 | return (prefix *)NULL; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | name[i++] = '\0'; |
|---|
| 214 | |
|---|
| 215 | back_ptr = ptr; |
|---|
| 216 | ptr = ptr->down; |
|---|
| 217 | if (ptr == NULL) { |
|---|
| 218 | /* Special case first time around at each level */ |
|---|
| 219 | name = osrealloc(name, i); |
|---|
| 220 | ptr = osnew(prefix); |
|---|
| 221 | ptr->ident = name; |
|---|
| 222 | name = NULL; |
|---|
| 223 | ptr->right = ptr->down = NULL; |
|---|
| 224 | ptr->pos = NULL; |
|---|
| 225 | ptr->shape = 0; |
|---|
| 226 | ptr->stn = NULL; |
|---|
| 227 | ptr->up = back_ptr; |
|---|
| 228 | ptr->filename = file.filename; |
|---|
| 229 | ptr->line = file.line; |
|---|
| 230 | ptr->min_export = ptr->max_export = 0; |
|---|
| 231 | ptr->sflags = BIT(SFLAGS_SURVEY); |
|---|
| 232 | if (fSuspectTypo && !fImplicitPrefix) |
|---|
| 233 | ptr->sflags |= BIT(SFLAGS_SUSPECTTYPO); |
|---|
| 234 | back_ptr->down = ptr; |
|---|
| 235 | fNew = fTrue; |
|---|
| 236 | } else { |
|---|
| 237 | /* Use caching to speed up adding an increasing sequence to a |
|---|
| 238 | * large survey */ |
|---|
| 239 | static prefix *cached_survey = NULL, *cached_station = NULL; |
|---|
| 240 | prefix *ptrPrev = NULL; |
|---|
| 241 | int cmp = 1; /* result of strcmp ( -ve for <, 0 for =, +ve for > ) */ |
|---|
| 242 | if (cached_survey == back_ptr) { |
|---|
| 243 | cmp = strcmp(cached_station->ident, name); |
|---|
| 244 | if (cmp <= 0) ptr = cached_station; |
|---|
| 245 | } |
|---|
| 246 | while (ptr && (cmp = strcmp(ptr->ident, name))<0) { |
|---|
| 247 | ptrPrev = ptr; |
|---|
| 248 | ptr = ptr->right; |
|---|
| 249 | } |
|---|
| 250 | if (cmp) { |
|---|
| 251 | /* ie we got to one that was higher, or the end */ |
|---|
| 252 | prefix *newptr; |
|---|
| 253 | name = osrealloc(name, i); |
|---|
| 254 | newptr = osnew(prefix); |
|---|
| 255 | newptr->ident = name; |
|---|
| 256 | name = NULL; |
|---|
| 257 | if (ptrPrev == NULL) |
|---|
| 258 | back_ptr->down = newptr; |
|---|
| 259 | else |
|---|
| 260 | ptrPrev->right = newptr; |
|---|
| 261 | newptr->right = ptr; |
|---|
| 262 | newptr->down = NULL; |
|---|
| 263 | newptr->pos = NULL; |
|---|
| 264 | newptr->shape = 0; |
|---|
| 265 | newptr->stn = NULL; |
|---|
| 266 | newptr->up = back_ptr; |
|---|
| 267 | newptr->filename = file.filename; |
|---|
| 268 | newptr->line = file.line; |
|---|
| 269 | newptr->min_export = newptr->max_export = 0; |
|---|
| 270 | newptr->sflags = BIT(SFLAGS_SURVEY); |
|---|
| 271 | if (fSuspectTypo && !fImplicitPrefix) |
|---|
| 272 | newptr->sflags |= BIT(SFLAGS_SUSPECTTYPO); |
|---|
| 273 | ptr = newptr; |
|---|
| 274 | fNew = fTrue; |
|---|
| 275 | } |
|---|
| 276 | cached_survey = back_ptr; |
|---|
| 277 | cached_station = ptr; |
|---|
| 278 | } |
|---|
| 279 | depth++; |
|---|
| 280 | f_optional = fFalse; /* disallow after first level */ |
|---|
| 281 | } while (isSep(ch)); |
|---|
| 282 | if (name) osfree(name); |
|---|
| 283 | |
|---|
| 284 | /* don't warn about a station that is referred to twice */ |
|---|
| 285 | if (!fNew) ptr->sflags &= ~BIT(SFLAGS_SUSPECTTYPO); |
|---|
| 286 | |
|---|
| 287 | if (fNew) { |
|---|
| 288 | /* fNew means SFLAGS_SURVEY is currently set */ |
|---|
| 289 | SVX_ASSERT(TSTBIT(ptr->sflags, SFLAGS_SURVEY)); |
|---|
| 290 | if (!fSurvey) { |
|---|
| 291 | ptr->sflags &= ~BIT(SFLAGS_SURVEY); |
|---|
| 292 | if (TSTBIT(pcs->infer, INFER_EXPORTS)) ptr->min_export = USHRT_MAX; |
|---|
| 293 | } |
|---|
| 294 | } else { |
|---|
| 295 | /* check that the same name isn't being used for a survey and station */ |
|---|
| 296 | if (fSurvey ^ TSTBIT(ptr->sflags, SFLAGS_SURVEY)) { |
|---|
| 297 | compile_error(/*“%s” can’t be both a station and a survey*/27, |
|---|
| 298 | sprint_prefix(ptr)); |
|---|
| 299 | } |
|---|
| 300 | if (!fSurvey && TSTBIT(pcs->infer, INFER_EXPORTS)) ptr->min_export = USHRT_MAX; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | /* check the export level */ |
|---|
| 304 | #if 0 |
|---|
| 305 | printf("R min %d max %d depth %d pfx %s\n", |
|---|
| 306 | ptr->min_export, ptr->max_export, depth, sprint_prefix(ptr)); |
|---|
| 307 | #endif |
|---|
| 308 | if (ptr->min_export == 0 || ptr->min_export == USHRT_MAX) { |
|---|
| 309 | if (depth > ptr->max_export) ptr->max_export = depth; |
|---|
| 310 | } else if (ptr->max_export < depth) { |
|---|
| 311 | const char *filename_store = file.filename; |
|---|
| 312 | unsigned int line_store = file.line; |
|---|
| 313 | prefix *survey = ptr; |
|---|
| 314 | char *s; |
|---|
| 315 | int level; |
|---|
| 316 | for (level = ptr->max_export + 1; level; level--) { |
|---|
| 317 | survey = survey->up; |
|---|
| 318 | SVX_ASSERT(survey); |
|---|
| 319 | } |
|---|
| 320 | s = osstrdup(sprint_prefix(survey)); |
|---|
| 321 | if (survey->filename) { |
|---|
| 322 | file.filename = survey->filename; |
|---|
| 323 | file.line = survey->line; |
|---|
| 324 | } |
|---|
| 325 | compile_error(/*Station “%s” not exported from survey “%s”*/26, |
|---|
| 326 | sprint_prefix(ptr), s); |
|---|
| 327 | if (survey->filename) { |
|---|
| 328 | file.filename = filename_store; |
|---|
| 329 | file.line = line_store; |
|---|
| 330 | } |
|---|
| 331 | osfree(s); |
|---|
| 332 | #if 0 |
|---|
| 333 | printf(" *** pfx %s warning not exported enough depth %d " |
|---|
| 334 | "ptr->max_export %d\n", sprint_prefix(ptr), |
|---|
| 335 | depth, ptr->max_export); |
|---|
| 336 | #endif |
|---|
| 337 | } |
|---|
| 338 | if (!fImplicitPrefix && (pfx_flags & PFX_WARN_SEPARATOR)) { |
|---|
| 339 | compile_warning(/*Separator in survey name*/392); |
|---|
| 340 | } |
|---|
| 341 | return ptr; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | /* if numeric expr is omitted: if f_optional return HUGE_REAL, else longjmp */ |
|---|
| 345 | static real |
|---|
| 346 | read_number(bool f_optional) |
|---|
| 347 | { |
|---|
| 348 | bool fPositive, fDigits = fFalse; |
|---|
| 349 | real n = (real)0.0; |
|---|
| 350 | filepos fp; |
|---|
| 351 | int ch_old; |
|---|
| 352 | |
|---|
| 353 | get_pos(&fp); |
|---|
| 354 | ch_old = ch; |
|---|
| 355 | fPositive = !isMinus(ch); |
|---|
| 356 | if (isSign(ch)) nextch(); |
|---|
| 357 | |
|---|
| 358 | while (isdigit(ch)) { |
|---|
| 359 | n = n * (real)10.0 + (char)(ch - '0'); |
|---|
| 360 | nextch(); |
|---|
| 361 | fDigits = fTrue; |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | if (isDecimal(ch)) { |
|---|
| 365 | real mult = (real)1.0; |
|---|
| 366 | nextch(); |
|---|
| 367 | while (isdigit(ch)) { |
|---|
| 368 | mult *= (real).1; |
|---|
| 369 | n += (char)(ch - '0') * mult; |
|---|
| 370 | fDigits = fTrue; |
|---|
| 371 | nextch(); |
|---|
| 372 | } |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | /* !'fRead' => !fDigits so fDigits => 'fRead' */ |
|---|
| 376 | if (fDigits) return (fPositive ? n : -n); |
|---|
| 377 | |
|---|
| 378 | /* didn't read a valid number. If it's optional, reset filepos & return */ |
|---|
| 379 | set_pos(&fp); |
|---|
| 380 | if (f_optional) { |
|---|
| 381 | return HUGE_REAL; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | if (isOmit(ch_old)) { |
|---|
| 385 | compile_error(-/*Field may not be omitted*/8); |
|---|
| 386 | } else { |
|---|
| 387 | compile_error_token(-/*Expecting numeric field, found “%s”*/9); |
|---|
| 388 | } |
|---|
| 389 | LONGJMP(file.jbSkipLine); |
|---|
| 390 | return 0.0; /* for brain-fried compilers */ |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | extern real |
|---|
| 394 | read_numeric(bool f_optional, int *p_n_readings) |
|---|
| 395 | { |
|---|
| 396 | size_t n_readings = 0; |
|---|
| 397 | real tot = (real)0.0; |
|---|
| 398 | |
|---|
| 399 | skipblanks(); |
|---|
| 400 | if (!isOpen(ch)) { |
|---|
| 401 | real r = read_number(f_optional); |
|---|
| 402 | if (p_n_readings) *p_n_readings = (r == HUGE_REAL ? 0 : 1); |
|---|
| 403 | return r; |
|---|
| 404 | } |
|---|
| 405 | nextch(); |
|---|
| 406 | |
|---|
| 407 | skipblanks(); |
|---|
| 408 | do { |
|---|
| 409 | tot += read_number(fFalse); |
|---|
| 410 | ++n_readings; |
|---|
| 411 | skipblanks(); |
|---|
| 412 | } while (!isClose(ch)); |
|---|
| 413 | nextch(); |
|---|
| 414 | |
|---|
| 415 | if (p_n_readings) *p_n_readings = n_readings; |
|---|
| 416 | /* FIXME: special averaging for bearings ... */ |
|---|
| 417 | /* And for percentage gradient */ |
|---|
| 418 | return tot / n_readings; |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | /* read numeric expr or omit (return HUGE_REAL); else longjmp */ |
|---|
| 422 | extern real |
|---|
| 423 | read_numeric_or_omit(int *p_n_readings) |
|---|
| 424 | { |
|---|
| 425 | real v = read_numeric(fTrue, p_n_readings); |
|---|
| 426 | if (v == HUGE_REAL) { |
|---|
| 427 | if (!isOmit(ch)) { |
|---|
| 428 | compile_error_token(-/*Expecting numeric field, found “%s”*/9); |
|---|
| 429 | LONGJMP(file.jbSkipLine); |
|---|
| 430 | return 0.0; /* for brain-fried compilers */ |
|---|
| 431 | } |
|---|
| 432 | nextch(); |
|---|
| 433 | } |
|---|
| 434 | return v; |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | /* Don't skip blanks, variable error code */ |
|---|
| 438 | static unsigned int |
|---|
| 439 | read_uint_internal(int errmsg, const filepos *fp) |
|---|
| 440 | { |
|---|
| 441 | unsigned int n = 0; |
|---|
| 442 | if (!isdigit(ch)) { |
|---|
| 443 | if (fp) set_pos(fp); |
|---|
| 444 | compile_error_token(-errmsg); |
|---|
| 445 | LONGJMP(file.jbSkipLine); |
|---|
| 446 | } |
|---|
| 447 | while (isdigit(ch)) { |
|---|
| 448 | n = n * 10 + (char)(ch - '0'); |
|---|
| 449 | nextch(); |
|---|
| 450 | } |
|---|
| 451 | return n; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | extern unsigned int |
|---|
| 455 | read_uint(void) |
|---|
| 456 | { |
|---|
| 457 | skipblanks(); |
|---|
| 458 | return read_uint_internal(/*Expecting numeric field, found “%s”*/9, NULL); |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | extern void |
|---|
| 462 | read_string(char **pstr, int *plen) |
|---|
| 463 | { |
|---|
| 464 | s_zero(pstr); |
|---|
| 465 | |
|---|
| 466 | skipblanks(); |
|---|
| 467 | if (ch == '\"') { |
|---|
| 468 | /* String quoted in "" */ |
|---|
| 469 | nextch(); |
|---|
| 470 | while (1) { |
|---|
| 471 | if (isEol(ch)) { |
|---|
| 472 | compile_error(-/*Missing \"*/69); |
|---|
| 473 | LONGJMP(file.jbSkipLine); |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | if (ch == '\"') break; |
|---|
| 477 | |
|---|
| 478 | s_catchar(pstr, plen, ch); |
|---|
| 479 | nextch(); |
|---|
| 480 | } |
|---|
| 481 | } else { |
|---|
| 482 | /* Unquoted string */ |
|---|
| 483 | while (1) { |
|---|
| 484 | if (isEol(ch) || isComm(ch)) { |
|---|
| 485 | if (!*pstr || !(*pstr)[0]) { |
|---|
| 486 | compile_error(-/*Expecting string field*/121); |
|---|
| 487 | LONGJMP(file.jbSkipLine); |
|---|
| 488 | } |
|---|
| 489 | return; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | if (isBlank(ch)) break; |
|---|
| 493 | |
|---|
| 494 | s_catchar(pstr, plen, ch); |
|---|
| 495 | nextch(); |
|---|
| 496 | } |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | nextch(); |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | extern void |
|---|
| 503 | read_date(int *py, int *pm, int *pd) |
|---|
| 504 | { |
|---|
| 505 | int y = 0, m = 0, d = 0; |
|---|
| 506 | filepos fp; |
|---|
| 507 | |
|---|
| 508 | skipblanks(); |
|---|
| 509 | |
|---|
| 510 | get_pos(&fp); |
|---|
| 511 | y = read_uint_internal(/*Expecting date, found “%s”*/198, &fp); |
|---|
| 512 | /* Two digit year is 19xx. */ |
|---|
| 513 | if (y < 100) y += 1900; |
|---|
| 514 | if (y < 1900 || y > 2078) { |
|---|
| 515 | compile_warning(/*Invalid year (< 1900 or > 2078)*/58); |
|---|
| 516 | LONGJMP(file.jbSkipLine); |
|---|
| 517 | return; /* for brain-fried compilers */ |
|---|
| 518 | } |
|---|
| 519 | if (ch == '.') { |
|---|
| 520 | nextch(); |
|---|
| 521 | m = read_uint_internal(/*Expecting date, found “%s”*/198, &fp); |
|---|
| 522 | if (m < 1 || m > 12) { |
|---|
| 523 | compile_warning(/*Invalid month*/86); |
|---|
| 524 | LONGJMP(file.jbSkipLine); |
|---|
| 525 | return; /* for brain-fried compilers */ |
|---|
| 526 | } |
|---|
| 527 | if (ch == '.') { |
|---|
| 528 | nextch(); |
|---|
| 529 | d = read_uint_internal(/*Expecting date, found “%s”*/198, &fp); |
|---|
| 530 | if (d < 1 || d > last_day(y, m)) { |
|---|
| 531 | /* TRANSLATORS: e.g. 31st of April, or 32nd of any month */ |
|---|
| 532 | compile_warning(/*Invalid day of the month*/87); |
|---|
| 533 | LONGJMP(file.jbSkipLine); |
|---|
| 534 | return; /* for brain-fried compilers */ |
|---|
| 535 | } |
|---|
| 536 | } |
|---|
| 537 | } |
|---|
| 538 | if (py) *py = y; |
|---|
| 539 | if (pm) *pm = m; |
|---|
| 540 | if (pd) *pd = d; |
|---|
| 541 | } |
|---|