source: git/src/readval.c @ 0b8c321

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 0b8c321 was 0b8c321, checked in by Olly Betts <olly@…>, 9 years ago

lib/survex.pot,src/: Add more TRANSLATOR comments.

  • Property mode set to 100644
File size: 14.3 KB
RevLine 
[6974a34]1/* readval.c
[0395657]2 * Routines to read a prefix or number from the current input file
[736f7df]3 * Copyright (C) 1991-2003,2005,2006,2010,2011,2012,2013,2014 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
[a420b49]20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
[d1b1380]23
[c00c74a9]24#include <limits.h>
[be97baf]25#include <stddef.h> /* for offsetof */
[c00c74a9]26
[a420b49]27#include "cavern.h"
[1ee204e]28#include "date.h"
[dd5b1a8]29#include "debug.h"
[a1706d1]30#include "filename.h"
31#include "message.h"
[d1b1380]32#include "readval.h"
33#include "datain.h"
[fb2e93c]34#include "netbits.h"
[8cf2e04]35#include "osalloc.h"
[76bb277a]36#include "str.h"
[d1b1380]37
[7d5f3c0]38#ifdef HAVE_SETJMP_H
[647407d]39# define LONGJMP(JB) longjmp((JB), 1)
40#else
41# define LONGJMP(JB) exit(1)
42#endif
43
[c86cc71]44int root_depr_count = 0;
45
[710ecc1]46static prefix *
[a2c33ae]47new_anon_station(void)
[710ecc1]48{
[a2c33ae]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;
[710ecc1]69}
70
[c458cf7]71/* if prefix is omitted: if PFX_OPT set return NULL, otherwise use longjmp */
72extern prefix *
73read_prefix(unsigned pfx_flags)
[a420b49]74{
[c458cf7]75   bool f_optional = !!(pfx_flags & PFX_OPT);
76   bool fSurvey = !!(pfx_flags & PFX_SURVEY);
77   bool fSuspectTypo = !!(pfx_flags & PFX_SUSPECT_TYPO);
[4a6a094]78   prefix *back_ptr, *ptr;
[a420b49]79   char *name;
80   size_t name_len = 32;
[eb18f4d]81   size_t i;
[647407d]82   bool fNew;
[d6274ba]83   bool fImplicitPrefix = fTrue;
[932f7e9]84   int depth = -1;
[a420b49]85
86   skipblanks();
[7f1ab95]87#ifndef NO_DEPRECATED
[a420b49]88   if (isRoot(ch)) {
[c458cf7]89      if (!(pfx_flags & PFX_ALLOW_ROOT)) {
[da96015]90         compile_error(-/*ROOT is deprecated*/25);
[421b7d2]91         LONGJMP(file.jbSkipLine);
[84c60fc]92      }
[c86cc71]93      if (root_depr_count < 5) {
[da96015]94         compile_warning(-/*ROOT is deprecated*/25);
[c86cc71]95         if (++root_depr_count == 5)
[c40038a]96            compile_warning(/*Further uses of this deprecated feature will not be reported*/95);
[c86cc71]97      }
[d1b1380]98      nextch();
[a420b49]99      ptr = root;
100      if (!isNames(ch)) {
101         if (!isSep(ch)) return ptr;
[27b8b59]102         /* Allow optional SEPARATOR after ROOT */
[a420b49]103         nextch();
104      }
[647407d]105      fImplicitPrefix = fFalse;
[710ecc1]106#else
107   if (0) {
108#endif
[a420b49]109   } else {
[dcbcae0]110      if ((pfx_flags & PFX_ANON) &&
111          (isSep(ch) || (pcs->dash_for_anon_wall_station && ch == '-'))) {
112         int first_ch = ch;
[710ecc1]113         filepos here;
114         get_pos(&here);
115         nextch();
116         if (isBlank(ch) || isEol(ch)) {
[dcbcae0]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)) {
[710ecc1]124               compile_error(-/*Can't have a leg between two anonymous stations*/3);
[dcbcae0]125               LONGJMP(file.jbSkipLine);
[710ecc1]126            }
[dcbcae0]127            pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY);
[a2c33ae]128            return new_anon_station();
[710ecc1]129         }
[dcbcae0]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;
137anon_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               }
[da65891]142               pcs->flags |= BIT(FLAGS_ANON_ONE_END) | BIT(FLAGS_IMPLICIT_SPLAY);
[dcbcae0]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         }
[710ecc1]164         set_pos(&here);
165      }
[a420b49]166      ptr = pcs->Prefix;
167   }
168
169   i = 0;
[be97baf]170   name = NULL;
[a420b49]171   do {
[647407d]172      fNew = fFalse;
[be97baf]173      if (name == NULL) {
174         /* Need a new name buffer */
175         name = osmalloc(name_len);
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 */
[1157304]185            name[i++] = (pcs->Case == LOWER ? tolower(ch) :
186                         (pcs->Case == OFF ? ch : toupper(ch)));
[a420b49]187            if (i >= name_len) {
188               name_len = name_len + name_len;
189               name = osrealloc(name, name_len);
190            }
[8cf2e04]191         }
[a420b49]192         nextch();
[8cf2e04]193      }
[335f37a]194      if (isSep(ch)) fImplicitPrefix = fFalse;
[a420b49]195      if (i == 0) {
196         osfree(name);
[21c226e]197         if (!f_optional) {
[ee1d81f]198            if (isEol(ch)) {
[84c60fc]199               if (fSurvey) {
[4fb15a1]200                  compile_error(-/*Expecting survey name*/89);
[84c60fc]201               } else {
[4fb15a1]202                  compile_error(-/*Expecting station name*/28);
[84c60fc]203               }
[ee1d81f]204            } else {
[736f7df]205               /* TRANSLATORS: Here "station" is a survey station, not a train station. */
[4fb15a1]206               compile_error(-/*Character “%c” not allowed in station name (use *SET NAMES to set allowed characters)*/7, ch);
[ee1d81f]207            }
[647407d]208            LONGJMP(file.jbSkipLine);
209         }
[a420b49]210         return (prefix *)NULL;
[d1b1380]211      }
[a420b49]212
[76bb277a]213      name[i++] = '\0';
[cb3d1e2]214
[a420b49]215      back_ptr = ptr;
216      ptr = ptr->down;
217      if (ptr == NULL) {
218         /* Special case first time around at each level */
[be97baf]219         name = osrealloc(name, i);
[a420b49]220         ptr = osnew(prefix);
221         ptr->ident = name;
[be97baf]222         name = NULL;
[a420b49]223         ptr->right = ptr->down = NULL;
224         ptr->pos = NULL;
[6adb88c]225         ptr->shape = 0;
[a420b49]226         ptr->stn = NULL;
227         ptr->up = back_ptr;
[d333899]228         ptr->filename = file.filename;
229         ptr->line = file.line;
[932f7e9]230         ptr->min_export = ptr->max_export = 0;
[95c3272]231         ptr->sflags = BIT(SFLAGS_SURVEY);
232         if (fSuspectTypo && !fImplicitPrefix)
233            ptr->sflags |= BIT(SFLAGS_SUSPECTTYPO);
[a420b49]234         back_ptr->down = ptr;
[647407d]235         fNew = fTrue;
[a420b49]236      } else {
[be97baf]237         /* Use caching to speed up adding an increasing sequence to a
238          * large survey */
239         static prefix *cached_survey = NULL, *cached_station = NULL;
[a420b49]240         prefix *ptrPrev = NULL;
[76bb277a]241         int cmp = 1; /* result of strcmp ( -ve for <, 0 for =, +ve for > ) */
[be97baf]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) {
[a420b49]247            ptrPrev = ptr;
248            ptr = ptr->right;
249         }
250         if (cmp) {
251            /* ie we got to one that was higher, or the end */
[be97baf]252            prefix *newptr;
253            name = osrealloc(name, i);
254            newptr = osnew(prefix);
[647407d]255            newptr->ident = name;
[be97baf]256            name = NULL;
[a420b49]257            if (ptrPrev == NULL)
[647407d]258               back_ptr->down = newptr;
[a420b49]259            else
[647407d]260               ptrPrev->right = newptr;
261            newptr->right = ptr;
262            newptr->down = NULL;
263            newptr->pos = NULL;
[6adb88c]264            newptr->shape = 0;
[647407d]265            newptr->stn = NULL;
266            newptr->up = back_ptr;
[d333899]267            newptr->filename = file.filename;
268            newptr->line = file.line;
[421b7d2]269            newptr->min_export = newptr->max_export = 0;
[95c3272]270            newptr->sflags = BIT(SFLAGS_SURVEY);
271            if (fSuspectTypo && !fImplicitPrefix)
272               newptr->sflags |= BIT(SFLAGS_SUSPECTTYPO);
[647407d]273            ptr = newptr;
274            fNew = fTrue;
[a420b49]275         }
[be97baf]276         cached_survey = back_ptr;
277         cached_station = ptr;
[d1b1380]278      }
[fb2e93c]279      depth++;
[21c226e]280      f_optional = fFalse; /* disallow after first level */
[a420b49]281   } while (isSep(ch));
[be97baf]282   if (name) osfree(name);
283
[d40f787]284   /* don't warn about a station that is referred to twice */
[95c3272]285   if (!fNew) ptr->sflags &= ~BIT(SFLAGS_SUSPECTTYPO);
[421b7d2]286
[6430f0e]287   if (fNew) {
[95c3272]288      /* fNew means SFLAGS_SURVEY is currently set */
[4c07c51]289      SVX_ASSERT(TSTBIT(ptr->sflags, SFLAGS_SURVEY));
[c00c74a9]290      if (!fSurvey) {
291         ptr->sflags &= ~BIT(SFLAGS_SURVEY);
[27b8b59]292         if (TSTBIT(pcs->infer, INFER_EXPORTS)) ptr->min_export = USHRT_MAX;
[c00c74a9]293      }
[6430f0e]294   } else {
295      /* check that the same name isn't being used for a survey and station */
[95c3272]296      if (fSurvey ^ TSTBIT(ptr->sflags, SFLAGS_SURVEY)) {
[0b8c321]297         /* TRANSLATORS: Here "station" is a survey station, not a train station.
298          *
299          * Here "survey" is a "cave map" rather than list of questions - it should be
300          * translated to the terminology that cavers using the language would use.
301          */
[ee7511a]302         compile_error(/*“%s” can’t be both a station and a survey*/27,
[6430f0e]303                       sprint_prefix(ptr));
304      }
[27b8b59]305      if (!fSurvey && TSTBIT(pcs->infer, INFER_EXPORTS)) ptr->min_export = USHRT_MAX;
[051a72b]306   }
307
[932f7e9]308   /* check the export level */
309#if 0
310   printf("R min %d max %d depth %d pfx %s\n",
311          ptr->min_export, ptr->max_export, depth, sprint_prefix(ptr));
312#endif
[c00c74a9]313   if (ptr->min_export == 0 || ptr->min_export == USHRT_MAX) {
[932f7e9]314      if (depth > ptr->max_export) ptr->max_export = depth;
315   } else if (ptr->max_export < depth) {
[dd5b1a8]316      prefix *survey = ptr;
317      char *s;
[93e3492]318      const char *p;
[dd5b1a8]319      int level;
320      for (level = ptr->max_export + 1; level; level--) {
321         survey = survey->up;
[4c07c51]322         SVX_ASSERT(survey);
[dd5b1a8]323      }
324      s = osstrdup(sprint_prefix(survey));
[93e3492]325      p = sprint_prefix(ptr);
[016068a]326      if (survey->filename) {
[93e3492]327         compile_error_pfx(survey,
328                           /*Station “%s” not exported from survey “%s”*/26,
329                           p, s);
330      } else {
331         compile_error(/*Station “%s” not exported from survey “%s”*/26, p, s);
[016068a]332      }
[dd5b1a8]333      osfree(s);
[932f7e9]334#if 0
335      printf(" *** pfx %s warning not exported enough depth %d "
[421b7d2]336             "ptr->max_export %d\n", sprint_prefix(ptr),
[932f7e9]337             depth, ptr->max_export);
338#endif
339   }
[0fa7aac]340   if (!fImplicitPrefix && (pfx_flags & PFX_WARN_SEPARATOR)) {
341      compile_warning(/*Separator in survey name*/392);
342   }
[a420b49]343   return ptr;
[d1b1380]344}
345
[21c226e]346/* if numeric expr is omitted: if f_optional return HUGE_REAL, else longjmp */
347static real
348read_number(bool f_optional)
[a420b49]349{
350   bool fPositive, fDigits = fFalse;
351   real n = (real)0.0;
[c80bd34]352   filepos fp;
353   int ch_old;
[a420b49]354
[c80bd34]355   get_pos(&fp);
356   ch_old = ch;
[a420b49]357   fPositive = !isMinus(ch);
358   if (isSign(ch)) nextch();
359
360   while (isdigit(ch)) {
[6974a34]361      n = n * (real)10.0 + (char)(ch - '0');
[d1b1380]362      nextch();
[a420b49]363      fDigits = fTrue;
364   }
365
366   if (isDecimal(ch)) {
367      real mult = (real)1.0;
368      nextch();
369      while (isdigit(ch)) {
370         mult *= (real).1;
371         n += (char)(ch - '0') * mult;
372         fDigits = fTrue;
373         nextch();
374      }
375   }
376
377   /* !'fRead' => !fDigits so fDigits => 'fRead' */
378   if (fDigits) return (fPositive ? n : -n);
379
380   /* didn't read a valid number.  If it's optional, reset filepos & return */
[4fb15a1]381   set_pos(&fp);
[21c226e]382   if (f_optional) {
[a420b49]383      return HUGE_REAL;
384   }
385
[c80bd34]386   if (isOmit(ch_old)) {
[4fb15a1]387      compile_error(-/*Field may not be omitted*/8);
[647407d]388   } else {
[da96015]389      compile_error_token(-/*Expecting numeric field, found “%s”*/9);
[647407d]390   }
391   LONGJMP(file.jbSkipLine);
[a420b49]392   return 0.0; /* for brain-fried compilers */
[d1b1380]393}
[647407d]394
[21c226e]395extern real
396read_numeric(bool f_optional, int *p_n_readings)
397{
398   size_t n_readings = 0;
399   real tot = (real)0.0;
400
401   skipblanks();
402   if (!isOpen(ch)) {
403      real r = read_number(f_optional);
404      if (p_n_readings) *p_n_readings = (r == HUGE_REAL ? 0 : 1);
405      return r;
406   }
407   nextch();
408
409   skipblanks();
410   do {
411      tot += read_number(fFalse);
412      ++n_readings;
413      skipblanks();
414   } while (!isClose(ch));
415   nextch();
416
417   if (p_n_readings) *p_n_readings = n_readings;
418   /* FIXME: special averaging for bearings ... */
419   /* And for percentage gradient */
420   return tot / n_readings;
421}
422
[2aa2f3f]423/* read numeric expr or omit (return HUGE_REAL); else longjmp */
424extern real
[21c226e]425read_numeric_or_omit(int *p_n_readings)
[2aa2f3f]426{
[21c226e]427   real v = read_numeric(fTrue, p_n_readings);
[2aa2f3f]428   if (v == HUGE_REAL) {
429      if (!isOmit(ch)) {
[da96015]430         compile_error_token(-/*Expecting numeric field, found “%s”*/9);
[2aa2f3f]431         LONGJMP(file.jbSkipLine);
432         return 0.0; /* for brain-fried compilers */
433      }
434      nextch();
435   }
436   return v;
437}
438
[950a829]439/* Don't skip blanks, variable error code */
440static unsigned int
[75e67ab]441read_uint_internal(int errmsg, const filepos *fp)
[647407d]442{
[421b7d2]443   unsigned int n = 0;
[647407d]444   if (!isdigit(ch)) {
[029cc3f]445      if (fp) set_pos(fp);
[da96015]446      compile_error_token(-errmsg);
[647407d]447      LONGJMP(file.jbSkipLine);
448   }
449   while (isdigit(ch)) {
450      n = n * 10 + (char)(ch - '0');
451      nextch();
452   }
453   return n;
454}
[76bb277a]455
[950a829]456extern unsigned int
457read_uint(void)
458{
459   skipblanks();
[0804fbe]460   return read_uint_internal(/*Expecting numeric field, found “%s”*/9, NULL);
[950a829]461}
462
[76bb277a]463extern void
464read_string(char **pstr, int *plen)
465{
466   s_zero(pstr);
467
468   skipblanks();
469   if (ch == '\"') {
[1157304]470      /* String quoted in "" */
[76bb277a]471      nextch();
[457a390]472      while (1) {
473         if (isEol(ch)) {
[4fb15a1]474            compile_error(-/*Missing \"*/69);
475            LONGJMP(file.jbSkipLine);
[457a390]476         }
477
478         if (ch == '\"') break;
479
480         s_catchar(pstr, plen, ch);
481         nextch();
482      }
483   } else {
[1157304]484      /* Unquoted string */
[457a390]485      while (1) {
486         if (isEol(ch) || isComm(ch)) {
[b97d134]487            if (!*pstr || !(*pstr)[0]) {
[4fb15a1]488               compile_error(-/*Expecting string field*/121);
[6f61f83]489               LONGJMP(file.jbSkipLine);
[b97d134]490            }
[457a390]491            return;
[76bb277a]492         }
493
[457a390]494         if (isBlank(ch)) break;
[76bb277a]495
[457a390]496         s_catchar(pstr, plen, ch);
497         nextch();
498      }
[76bb277a]499   }
[421b7d2]500
[76bb277a]501   nextch();
502}
[950a829]503
504extern void
[b5a3219]505read_date(int *py, int *pm, int *pd)
[950a829]506{
507   int y = 0, m = 0, d = 0;
[029cc3f]508   filepos fp;
[950a829]509
510   skipblanks();
[029cc3f]511
512   get_pos(&fp);
[0804fbe]513   y = read_uint_internal(/*Expecting date, found “%s”*/198, &fp);
[e0c7cd1]514   /* Two digit year is 19xx. */
515   if (y < 100) y += 1900;
[1ee204e]516   if (y < 1900 || y > 2078) {
[ce8f81c]517      compile_warning(/*Invalid year (< 1900 or > 2078)*/58);
[e0c7cd1]518      LONGJMP(file.jbSkipLine);
519      return; /* for brain-fried compilers */
520   }
[950a829]521   if (ch == '.') {
522      nextch();
[0804fbe]523      m = read_uint_internal(/*Expecting date, found “%s”*/198, &fp);
[e0c7cd1]524      if (m < 1 || m > 12) {
[ce8f81c]525         compile_warning(/*Invalid month*/86);
[e0c7cd1]526         LONGJMP(file.jbSkipLine);
527         return; /* for brain-fried compilers */
[79d84d9]528      }
[950a829]529      if (ch == '.') {
530         nextch();
[0804fbe]531         d = read_uint_internal(/*Expecting date, found “%s”*/198, &fp);
[e0c7cd1]532         if (d < 1 || d > last_day(y, m)) {
[736f7df]533            /* TRANSLATORS: e.g. 31st of April, or 32nd of any month */
[ce8f81c]534            compile_warning(/*Invalid day of the month*/87);
[e0c7cd1]535            LONGJMP(file.jbSkipLine);
536            return; /* for brain-fried compilers */
537         }
[950a829]538      }
539   }
[b5a3219]540   if (py) *py = y;
541   if (pm) *pm = m;
542   if (pd) *pd = d;
[950a829]543}
Note: See TracBrowser for help on using the repository browser.