source: git/src/readval.c @ 756da0a

warn-only-for-hanging-survey
Last change on this file since 756da0a was 116b485, checked in by Olly Betts <olly@…>, 12 months ago

Flag stations with explicit Walls prefix as exported

Suggested by Eric C. Landgraf.

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