source: git/src/commands.c @ 9fcc81a

RELEASE/1.2debug-cidebug-ci-sanitiserswalls-data
Last change on this file since 9fcc81a was 9699215, checked in by Olly Betts <olly@…>, 5 years ago

Update PROJ project name and website

The name is now "PROJ" (all caps and no ".4" suffix) and the website
is: https://proj.org/

  • Property mode set to 100644
File size: 67.9 KB
Line 
1/* commands.c
2 * Code for directives
3 * Copyright (C) 1991-2003,2004,2005,2006,2010,2011,2012,2013,2014,2015,2016,2019 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 <assert.h>
25#include <limits.h>
26#include <stddef.h> /* for offsetof */
27#include <string.h>
28
29#ifdef HAVE_PROJ_H
30/* Work around broken check in proj.h:
31 * https://github.com/OSGeo/PROJ/issues/1523
32 */
33# ifndef PROJ_H
34#  include <proj.h>
35# endif
36#endif
37#define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H 1
38#include <proj_api.h>
39
40#include "cavern.h"
41#include "commands.h"
42#include "datain.h"
43#include "date.h"
44#include "debug.h"
45#include "filename.h"
46#include "message.h"
47#include "netbits.h"
48#include "netskel.h"
49#include "out.h"
50#include "readval.h"
51#include "str.h"
52
53#ifndef HAVE_PROJ_H
54/*** Extracted from PROJ 4.x projects.h (yuck, but grass also does this): */
55struct DERIVS {
56    double x_l, x_p; /* derivatives of x for lambda-phi */
57    double y_l, y_p; /* derivatives of y for lambda-phi */
58};
59
60struct FACTORS {
61    struct DERIVS der;
62    double h, k;        /* meridinal, parallel scales */
63    double omega, thetap;       /* angular distortion, theta prime */
64    double conv;        /* convergence */
65    double s;           /* areal scale factor */
66    double a, b;        /* max-min scale error */
67    int code;           /* info as to analytics, see following */
68};
69
70int pj_factors(projLP, projPJ *, double, struct FACTORS *);
71/***/
72#endif
73
74static projPJ proj_wgs84;
75
76static void
77default_grade(settings *s)
78{
79   /* Values correspond to those in bcra5.svx */
80   s->Var[Q_POS] = (real)sqrd(0.05);
81   s->Var[Q_LENGTH] = (real)sqrd(0.05);
82   s->Var[Q_BACKLENGTH] = (real)sqrd(0.05);
83   s->Var[Q_COUNT] = (real)sqrd(0.05);
84   s->Var[Q_DX] = s->Var[Q_DY] = s->Var[Q_DZ] = (real)sqrd(0.05);
85   s->Var[Q_BEARING] = (real)sqrd(rad(0.5));
86   s->Var[Q_GRADIENT] = (real)sqrd(rad(0.5));
87   s->Var[Q_BACKBEARING] = (real)sqrd(rad(0.5));
88   s->Var[Q_BACKGRADIENT] = (real)sqrd(rad(0.5));
89   /* SD of plumbed legs (0.25 degrees?) */
90   s->Var[Q_PLUMB] = (real)sqrd(rad(0.25));
91   /* SD of level legs (0.25 degrees?) */
92   s->Var[Q_LEVEL] = (real)sqrd(rad(0.25));
93   s->Var[Q_DEPTH] = (real)sqrd(0.05);
94}
95
96static void
97default_truncate(settings *s)
98{
99   s->Truncate = INT_MAX;
100}
101
102static void
103default_case(settings *s)
104{
105   s->Case = LOWER;
106}
107
108static reading default_order[] = { Fr, To, Tape, Comp, Clino, End };
109
110static void
111default_style(settings *s)
112{
113   s->style = STYLE_NORMAL;
114   s->ordering = default_order;
115   s->dash_for_anon_wall_station = fFalse;
116}
117
118static void
119default_prefix(settings *s)
120{
121   s->Prefix = root;
122}
123
124static void
125default_translate(settings *s)
126{
127   int i;
128   short *t;
129   if (s->next && s->next->Translate == s->Translate) {
130      t = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
131      memcpy(t - 1, s->Translate - 1, sizeof(short) * 257);
132      s->Translate = t;
133   }
134/*  SVX_ASSERT(EOF==-1);*/ /* important, since we rely on this */
135   t = s->Translate;
136   memset(t - 1, 0, sizeof(short) * 257);
137   for (i = '0'; i <= '9'; i++) t[i] = SPECIAL_NAMES;
138   for (i = 'A'; i <= 'Z'; i++) t[i] = SPECIAL_NAMES;
139   for (i = 'a'; i <= 'z'; i++) t[i] = SPECIAL_NAMES;
140
141   t['\t'] |= SPECIAL_BLANK;
142   t[' '] |= SPECIAL_BLANK;
143   t[','] |= SPECIAL_BLANK;
144   t[';'] |= SPECIAL_COMMENT;
145   t['\032'] |= SPECIAL_EOL; /* Ctrl-Z, so olde DOS text files are handled ok */
146   t[EOF] |= SPECIAL_EOL;
147   t['\n'] |= SPECIAL_EOL;
148   t['\r'] |= SPECIAL_EOL;
149   t['*'] |= SPECIAL_KEYWORD;
150   t['-'] |= SPECIAL_OMIT;
151   t['\\'] |= SPECIAL_ROOT;
152   t['.'] |= SPECIAL_SEPARATOR;
153   t['_'] |= SPECIAL_NAMES;
154   t['-'] |= SPECIAL_NAMES; /* Added in 0.97 prerelease 4 */
155   t['.'] |= SPECIAL_DECIMAL;
156   t['-'] |= SPECIAL_MINUS;
157   t['+'] |= SPECIAL_PLUS;
158#if 0 /* FIXME */
159   t['{'] |= SPECIAL_OPEN;
160   t['}'] |= SPECIAL_CLOSE;
161#endif
162}
163
164void
165default_units(settings *s)
166{
167   int quantity;
168   for (quantity = 0; quantity < Q_MAC; quantity++) {
169      if (TSTBIT(ANG_QMASK, quantity))
170         s->units[quantity] = (real)(M_PI / 180.0); /* degrees */
171      else
172         s->units[quantity] = (real)1.0; /* metres */
173   }
174   s->f_clino_percent = s->f_backclino_percent = fFalse;
175}
176
177void
178default_calib(settings *s)
179{
180   int quantity;
181   for (quantity = 0; quantity < Q_MAC; quantity++) {
182      s->z[quantity] = (real)0.0;
183      s->sc[quantity] = (real)1.0;
184   }
185}
186
187static void
188default_flags(settings *s)
189{
190   s->flags = 0;
191}
192
193extern void
194default_all(settings *s)
195{
196   default_truncate(s);
197   s->infer = 0;
198   default_case(s);
199   default_style(s);
200   default_prefix(s);
201   default_translate(s);
202   default_grade(s);
203   default_units(s);
204   default_calib(s);
205   default_flags(s);
206}
207
208char *buffer = NULL;
209static int buf_len;
210
211static char *ucbuffer = NULL;
212
213/* read token */
214extern void
215get_token(void)
216{
217   skipblanks();
218   get_token_no_blanks();
219}
220
221extern void
222get_token_no_blanks(void)
223{
224   int i = -1;
225
226   s_zero(&buffer);
227   osfree(ucbuffer);
228   while (isalpha(ch)) {
229      s_catchar(&buffer, &buf_len, (char)ch);
230      nextch();
231   }
232
233   if (!buffer) s_catchar(&buffer, &buf_len, '\0');
234
235   ucbuffer = osmalloc(buf_len);
236   do {
237      i++;
238      ucbuffer[i] = toupper(buffer[i]);
239   } while (buffer[i]);
240#if 0
241   printf("get_token_no_blanks() got “%s”\n", buffer);
242#endif
243}
244
245/* read word */
246static void
247get_word(void)
248{
249   s_zero(&buffer);
250   skipblanks();
251   while (!isBlank(ch) && !isEol(ch)) {
252      s_catchar(&buffer, &buf_len, (char)ch);
253      nextch();
254   }
255
256   if (!buffer) s_catchar(&buffer, &buf_len, '\0');
257#if 0
258   printf("get_word() got “%s”\n", buffer);
259#endif
260}
261
262/* match_tok() now uses binary chop
263 * tab argument should be alphabetically sorted (ascending)
264 */
265extern int
266match_tok(const sztok *tab, int tab_size)
267{
268   int a = 0, b = tab_size - 1, c;
269   int r;
270   assert(tab_size > 0); /* catch empty table */
271/*  printf("[%d,%d]",a,b); */
272   while (a <= b) {
273      c = (unsigned)(a + b) / 2;
274/*     printf(" %d",c); */
275      r = strcmp(tab[c].sz, ucbuffer);
276      if (r == 0) return tab[c].tok; /* match */
277      if (r < 0)
278         a = c + 1;
279      else
280         b = c - 1;
281   }
282   return tab[tab_size].tok; /* no match */
283}
284
285typedef enum {
286   CMD_NULL = -1, CMD_ALIAS, CMD_BEGIN, CMD_CALIBRATE, CMD_CASE, CMD_COPYRIGHT,
287   CMD_CS, CMD_DATA, CMD_DATE, CMD_DECLINATION, CMD_DEFAULT, CMD_END,
288   CMD_ENTRANCE, CMD_EQUATE, CMD_EXPORT, CMD_FIX, CMD_FLAGS, CMD_INCLUDE,
289   CMD_INFER, CMD_INSTRUMENT, CMD_PREFIX, CMD_REF, CMD_REQUIRE, CMD_SD,
290   CMD_SET, CMD_SOLVE, CMD_TEAM, CMD_TITLE, CMD_TRUNCATE, CMD_UNITS
291} cmds;
292
293static const sztok cmd_tab[] = {
294     {"ALIAS",     CMD_ALIAS},
295     {"BEGIN",     CMD_BEGIN},
296     {"CALIBRATE", CMD_CALIBRATE},
297     {"CASE",      CMD_CASE},
298     {"COPYRIGHT", CMD_COPYRIGHT},
299     {"CS",        CMD_CS},
300     {"DATA",      CMD_DATA},
301     {"DATE",      CMD_DATE},
302     {"DECLINATION", CMD_DECLINATION},
303#ifndef NO_DEPRECATED
304     {"DEFAULT",   CMD_DEFAULT},
305#endif
306     {"END",       CMD_END},
307     {"ENTRANCE",  CMD_ENTRANCE},
308     {"EQUATE",    CMD_EQUATE},
309     {"EXPORT",    CMD_EXPORT},
310     {"FIX",       CMD_FIX},
311     {"FLAGS",     CMD_FLAGS},
312     {"INCLUDE",   CMD_INCLUDE},
313     {"INFER",     CMD_INFER},
314     {"INSTRUMENT",CMD_INSTRUMENT},
315#ifndef NO_DEPRECATED
316     {"PREFIX",    CMD_PREFIX},
317#endif
318     {"REF",       CMD_REF},
319     {"REQUIRE",   CMD_REQUIRE},
320     {"SD",        CMD_SD},
321     {"SET",       CMD_SET},
322     {"SOLVE",     CMD_SOLVE},
323     {"TEAM",      CMD_TEAM},
324     {"TITLE",     CMD_TITLE},
325     {"TRUNCATE",  CMD_TRUNCATE},
326     {"UNITS",     CMD_UNITS},
327     {NULL,        CMD_NULL}
328};
329
330/* masks for units which are length and angles respectively */
331#define LEN_UMASK (BIT(UNITS_METRES) | BIT(UNITS_FEET) | BIT(UNITS_YARDS))
332#define ANG_UMASK (BIT(UNITS_DEGS) | BIT(UNITS_GRADS) | BIT(UNITS_MINUTES))
333
334/* ordering must be the same as the units enum */
335const real factor_tab[] = {
336   1.0, METRES_PER_FOOT, (METRES_PER_FOOT*3.0),
337   (M_PI/180.0), (M_PI/200.0), 0.01, (M_PI/180.0/60.0)
338};
339
340const int units_to_msgno[] = {
341    /*m*/424,
342    /*ft*/428,
343    -1, /* yards */
344    /*°*/344,
345    /*ᵍ*/345,
346    /*%*/96,
347    -1 /* minutes */
348};
349
350int get_length_units(int quantity) {
351    double factor = pcs->units[quantity];
352    if (fabs(factor - METRES_PER_FOOT) <= REAL_EPSILON ||
353        fabs(factor - METRES_PER_FOOT * 3.0) <= REAL_EPSILON) {
354        return UNITS_FEET;
355    }
356    return UNITS_METRES;
357}
358
359int get_angle_units(int quantity) {
360    double factor = pcs->units[quantity];
361    if (fabs(factor - M_PI / 200.0) <= REAL_EPSILON) {
362        return UNITS_GRADS;
363    }
364    return UNITS_DEGS;
365}
366
367static int
368get_units(unsigned long qmask, bool percent_ok)
369{
370   static const sztok utab[] = {
371        {"DEGREES",       UNITS_DEGS },
372        {"DEGS",          UNITS_DEGS },
373        {"FEET",          UNITS_FEET },
374        {"GRADS",         UNITS_GRADS },
375        {"METERS",        UNITS_METRES },
376        {"METRES",        UNITS_METRES },
377        {"METRIC",        UNITS_METRES },
378        {"MILS",          UNITS_DEPRECATED_ALIAS_FOR_GRADS },
379        {"MINUTES",       UNITS_MINUTES },
380        {"PERCENT",       UNITS_PERCENT },
381        {"PERCENTAGE",    UNITS_PERCENT },
382        {"YARDS",         UNITS_YARDS },
383        {NULL,            UNITS_NULL }
384   };
385   int units;
386   get_token();
387   units = match_tok(utab, TABSIZE(utab));
388   if (units == UNITS_NULL) {
389      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Unknown units “%s”*/35, buffer);
390      return UNITS_NULL;
391   }
392   /* Survex has long misdefined "mils" as an alias for "grads", of which
393    * there are 400 in a circle.  There are several definitions of "mils"
394    * with a circle containing 2000π SI milliradians, 6400 NATO mils, 6000
395    * Warsaw Pact mils, and 6300 Swedish streck, and they aren't in common
396    * use by cave surveyors, so we now just warn if mils are used.
397    */
398   if (units == UNITS_DEPRECATED_ALIAS_FOR_GRADS) {
399      compile_diagnostic(DIAG_WARN|DIAG_BUF|DIAG_SKIP,
400                         /*Units “%s” are deprecated, assuming “grads” - see manual for details*/479,
401                         buffer);
402      units = UNITS_GRADS;
403   }
404   if (units == UNITS_PERCENT && percent_ok &&
405       !(qmask & ~(BIT(Q_GRADIENT)|BIT(Q_BACKGRADIENT)))) {
406      return units;
407   }
408   if (((qmask & LEN_QMASK) && !TSTBIT(LEN_UMASK, units)) ||
409       ((qmask & ANG_QMASK) && !TSTBIT(ANG_UMASK, units))) {
410      /* TRANSLATORS: Note: In English you talk about the *units* of a single
411       * measurement, but the correct term in other languages may be singular.
412       */
413      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Invalid units “%s” for quantity*/37, buffer);
414      return UNITS_NULL;
415   }
416   return units;
417}
418
419/* returns mask with bit x set to indicate quantity x specified */
420static unsigned long
421get_qlist(unsigned long mask_bad)
422{
423   static const sztok qtab[] = {
424        {"ALTITUDE",     Q_DZ },
425        {"BACKBEARING",  Q_BACKBEARING },
426        {"BACKCLINO",    Q_BACKGRADIENT },    /* alternative name */
427        {"BACKCOMPASS",  Q_BACKBEARING },     /* alternative name */
428        {"BACKGRADIENT", Q_BACKGRADIENT },
429        {"BACKLENGTH",   Q_BACKLENGTH },
430        {"BACKTAPE",     Q_BACKLENGTH },    /* alternative name */
431        {"BEARING",      Q_BEARING },
432        {"CEILING",      Q_UP },          /* alternative name */
433        {"CLINO",        Q_GRADIENT },    /* alternative name */
434        {"COMPASS",      Q_BEARING },     /* alternative name */
435        {"COUNT",        Q_COUNT },
436        {"COUNTER",      Q_COUNT },       /* alternative name */
437        {"DECLINATION",  Q_DECLINATION },
438        {"DEFAULT",      Q_DEFAULT }, /* not a real quantity... */
439        {"DEPTH",        Q_DEPTH },
440        {"DOWN",         Q_DOWN },
441        {"DX",           Q_DX },          /* alternative name */
442        {"DY",           Q_DY },          /* alternative name */
443        {"DZ",           Q_DZ },          /* alternative name */
444        {"EASTING",      Q_DX },
445        {"FLOOR",        Q_DOWN },        /* alternative name */
446        {"GRADIENT",     Q_GRADIENT },
447        {"LEFT",         Q_LEFT },
448        {"LENGTH",       Q_LENGTH },
449        {"LEVEL",        Q_LEVEL},
450        {"NORTHING",     Q_DY },
451        {"PLUMB",        Q_PLUMB},
452        {"POSITION",     Q_POS },
453        {"RIGHT",        Q_RIGHT },
454        {"TAPE",         Q_LENGTH },      /* alternative name */
455        {"UP",           Q_UP },
456        {NULL,           Q_NULL }
457   };
458   unsigned long qmask = 0;
459   int tok;
460   filepos fp;
461
462   while (1) {
463      get_pos(&fp);
464      get_token();
465      tok = match_tok(qtab, TABSIZE(qtab));
466      if (tok == Q_DEFAULT && !(mask_bad & BIT(Q_DEFAULT))) {
467          /* Only recognise DEFAULT if it is the first quantity, and then don't
468           * look for any more. */
469          if (qmask == 0)
470              return BIT(Q_DEFAULT);
471          break;
472      }
473      /* bail out if we reach the table end with no match */
474      if (tok == Q_NULL) break;
475      qmask |= BIT(tok);
476      if (qmask & mask_bad) {
477         compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Unknown instrument “%s”*/39, buffer);
478         return 0;
479      }
480   }
481
482   if (qmask == 0) {
483      /* TRANSLATORS: A "quantity" is something measured like "LENGTH",
484       * "BEARING", "ALTITUDE", etc. */
485      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Unknown quantity “%s”*/34, buffer);
486   } else {
487      set_pos(&fp);
488   }
489
490   return qmask;
491}
492
493#define SPECIAL_UNKNOWN 0
494static void
495cmd_set(void)
496{
497   static const sztok chartab[] = {
498        {"BLANK",     SPECIAL_BLANK },
499/*FIXME {"CLOSE",     SPECIAL_CLOSE }, */
500        {"COMMENT",   SPECIAL_COMMENT },
501        {"DECIMAL",   SPECIAL_DECIMAL },
502        {"EOL",       SPECIAL_EOL }, /* EOL won't work well */
503        {"KEYWORD",   SPECIAL_KEYWORD },
504        {"MINUS",     SPECIAL_MINUS },
505        {"NAMES",     SPECIAL_NAMES },
506        {"OMIT",      SPECIAL_OMIT },
507/*FIXME {"OPEN",      SPECIAL_OPEN }, */
508        {"PLUS",      SPECIAL_PLUS },
509#ifndef NO_DEPRECATED
510        {"ROOT",      SPECIAL_ROOT },
511#endif
512        {"SEPARATOR", SPECIAL_SEPARATOR },
513        {NULL,        SPECIAL_UNKNOWN }
514   };
515   int mask;
516   int i;
517
518   get_token();
519   mask = match_tok(chartab, TABSIZE(chartab));
520
521   if (mask == SPECIAL_UNKNOWN) {
522      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Unknown character class “%s”*/42, buffer);
523      return;
524   }
525
526#ifndef NO_DEPRECATED
527   if (mask == SPECIAL_ROOT) {
528      if (root_depr_count < 5) {
529         /* TRANSLATORS: Use of the ROOT character (which is "\" by default) is
530          * deprecated, so this error would be generated by:
531          *
532          * *equate \foo.7 1
533          *
534          * If you're unsure what "deprecated" means, see:
535          * https://en.wikipedia.org/wiki/Deprecation */
536         compile_diagnostic(DIAG_WARN|DIAG_BUF, /*ROOT is deprecated*/25);
537         if (++root_depr_count == 5)
538             /* TRANSLATORS: If you're unsure what "deprecated" means, see:
539              * https://en.wikipedia.org/wiki/Deprecation */
540            compile_diagnostic(DIAG_WARN, /*Further uses of this deprecated feature will not be reported*/95);
541      }
542   }
543#endif
544
545   /* if we're currently using an inherited translation table, allocate a new
546    * table, and copy old one into it */
547   if (pcs->next && pcs->next->Translate == pcs->Translate) {
548      short *p;
549      p = ((short*)osmalloc(ossizeof(short) * 257)) + 1;
550      memcpy(p - 1, pcs->Translate - 1, sizeof(short) * 257);
551      pcs->Translate = p;
552   }
553
554   skipblanks();
555
556   /* clear this flag for all non-alphanums */
557   for (i = 0; i < 256; i++)
558      if (!isalnum(i)) pcs->Translate[i] &= ~mask;
559
560   /* now set this flag for all specified chars */
561   while (!isEol(ch)) {
562      if (!isalnum(ch)) {
563         pcs->Translate[ch] |= mask;
564      } else if (tolower(ch) == 'x') {
565         int hex;
566         filepos fp;
567         get_pos(&fp);
568         nextch();
569         if (!isxdigit(ch)) {
570            set_pos(&fp);
571            break;
572         }
573         hex = isdigit(ch) ? ch - '0' : tolower(ch) - 'a';
574         nextch();
575         if (!isxdigit(ch)) {
576            set_pos(&fp);
577            break;
578         }
579         hex = hex << 4 | (isdigit(ch) ? ch - '0' : tolower(ch) - 'a');
580         pcs->Translate[hex] |= mask;
581      } else {
582         break;
583      }
584      nextch();
585   }
586}
587
588static void
589check_reentry(prefix *survey, const filepos* fpos_ptr)
590{
591   /* Don't try to check "*prefix \" or "*begin \" */
592   if (!survey->up) return;
593   if (TSTBIT(survey->sflags, SFLAGS_PREFIX_ENTERED)) {
594      static int reenter_depr_count = 0;
595      filepos fp_tmp;
596
597      if (reenter_depr_count >= 5)
598         return;
599
600      get_pos(&fp_tmp);
601      set_pos(fpos_ptr);
602      /* TRANSLATORS: The first of two warnings given when a survey which has
603       * already been completed is reentered.  This example file crawl.svx:
604       *
605       * *begin crawl     ; <- second warning here
606       * 1 2 9.45 234 -01
607       * *end crawl
608       * *begin crawl     ; <- first warning here
609       * 2 3 7.67 223 -03
610       * *end crawl
611       *
612       * Would lead to:
613       *
614       * crawl.svx:4: Reentering an existing survey is deprecated
615       * crawl.svx:1: Originally entered here
616       *
617       * If you're unsure what "deprecated" means, see:
618       * https://en.wikipedia.org/wiki/Deprecation */
619      compile_diagnostic(DIAG_WARN|DIAG_TOKEN, /*Reentering an existing survey is deprecated*/29);
620      set_pos(&fp_tmp);
621      /* TRANSLATORS: The second of two warnings given when a survey which has
622       * already been completed is reentered.  This example file crawl.svx:
623       *
624       * *begin crawl
625       * 1 2 9.45 234 -01 # <- second warning here
626       * *end crawl
627       * *begin crawl     # <- first warning here
628       * 2 3 7.67 223 -03
629       * *end crawl
630       *
631       * Would lead to:
632       *
633       * crawl.svx:3: Reentering an existing survey is deprecated
634       * crawl.svx:1: Originally entered here
635       *
636       * If you're unsure what "deprecated" means, see:
637       * https://en.wikipedia.org/wiki/Deprecation */
638      compile_diagnostic_pfx(DIAG_WARN, survey, /*Originally entered here*/30);
639      if (++reenter_depr_count == 5) {
640         /* After we've warned about 5 uses of the same deprecated feature, we
641          * give up for the rest of the current processing run.
642          *
643          * If you're unsure what "deprecated" means, see:
644          * https://en.wikipedia.org/wiki/Deprecation */
645         compile_diagnostic(DIAG_WARN, /*Further uses of this deprecated feature will not be reported*/95);
646      }
647   } else {
648      survey->sflags |= BIT(SFLAGS_PREFIX_ENTERED);
649      survey->filename = file.filename;
650      survey->line = file.line;
651   }
652}
653
654#ifndef NO_DEPRECATED
655static void
656cmd_prefix(void)
657{
658   static int prefix_depr_count = 0;
659   prefix *survey;
660   filepos fp;
661   /* Issue warning first, so "*prefix \" warns first that *prefix is
662    * deprecated and then that ROOT is...
663    */
664   if (prefix_depr_count < 5) {
665      /* TRANSLATORS: If you're unsure what "deprecated" means, see:
666       * https://en.wikipedia.org/wiki/Deprecation */
667      compile_diagnostic(DIAG_WARN|DIAG_BUF, /**prefix is deprecated - use *begin and *end instead*/6);
668      if (++prefix_depr_count == 5)
669         compile_diagnostic(DIAG_WARN, /*Further uses of this deprecated feature will not be reported*/95);
670   }
671   get_pos(&fp);
672   survey = read_prefix(PFX_SURVEY|PFX_ALLOW_ROOT);
673   pcs->Prefix = survey;
674   check_reentry(survey, &fp);
675}
676#endif
677
678static void
679cmd_alias(void)
680{
681   /* Currently only two forms are supported:
682    * *alias station - ..
683    * *alias station -
684    */
685   get_token();
686   if (strcmp(ucbuffer, "STATION") != 0)
687      goto bad;
688   get_word();
689   if (strcmp(buffer, "-") != 0)
690      goto bad;
691   get_word();
692   if (*buffer && strcmp(buffer, "..") != 0)
693      goto bad;
694   pcs->dash_for_anon_wall_station = (*buffer != '\0');
695   return;
696bad:
697   compile_diagnostic(DIAG_ERR|DIAG_SKIP, /*Bad *alias command*/397);
698}
699
700static void
701cmd_begin(void)
702{
703   settings *pcsNew;
704
705   pcsNew = osnew(settings);
706   *pcsNew = *pcs; /* copy contents */
707   pcsNew->begin_lineno = file.line;
708   pcsNew->next = pcs;
709   pcs = pcsNew;
710
711   skipblanks();
712   pcs->begin_survey = NULL;
713   if (!isEol(ch) && !isComm(ch)) {
714      filepos fp;
715      prefix *survey;
716      get_pos(&fp);
717      survey = read_prefix(PFX_SURVEY|PFX_ALLOW_ROOT|PFX_WARN_SEPARATOR);
718      pcs->begin_survey = survey;
719      pcs->Prefix = survey;
720      check_reentry(survey, &fp);
721      f_export_ok = fTrue;
722   }
723}
724
725extern void
726free_settings(settings *p) {
727   /* don't free default ordering or ordering used by parent */
728   const reading *order = p->ordering;
729   if (order != default_order && (!p->next || order != p->next->ordering))
730      osfree((reading*)order);
731
732   /* free Translate if not used by parent */
733   if (!p->next || p->Translate != p->next->Translate)
734      osfree(p->Translate - 1);
735
736   /* free meta if not used by parent, or in this block */
737   if (p->meta && (!p->next || p->meta != p->next->meta) && p->meta->ref_count == 0)
738       osfree(p->meta);
739
740   /* free proj if not used by parent, or as the output projection */
741   if (p->proj && (!p->next || p->proj != p->next->proj) && p->proj != proj_out)
742       pj_free(p->proj);
743
744   osfree(p);
745}
746
747static void
748cmd_end(void)
749{
750   settings *pcsParent;
751   prefix *survey, *begin_survey;
752   filepos fp;
753
754   pcsParent = pcs->next;
755
756   if (pcs->begin_lineno == 0) {
757      if (pcsParent == NULL) {
758         /* more ENDs than BEGINs */
759         compile_diagnostic(DIAG_ERR|DIAG_SKIP, /*No matching BEGIN*/192);
760      } else {
761         compile_diagnostic(DIAG_ERR|DIAG_SKIP, /*END with no matching BEGIN in this file*/22);
762      }
763      return;
764   }
765
766   begin_survey = pcs->begin_survey;
767
768   SVX_ASSERT(pcsParent);
769   free_settings(pcs);
770   pcs = pcsParent;
771
772   /* note need to read using root *before* BEGIN */
773   skipblanks();
774   if (isEol(ch) || isComm(ch)) {
775      survey = NULL;
776   } else {
777      get_pos(&fp);
778      survey = read_prefix(PFX_SURVEY|PFX_ALLOW_ROOT);
779   }
780
781   if (survey != begin_survey) {
782      if (survey) {
783         set_pos(&fp);
784         if (!begin_survey) {
785            /* TRANSLATORS: Used when a BEGIN command has no survey, but the
786             * END command does, e.g.:
787             *
788             * *begin
789             * 1 2 10.00 178 -01
790             * *end entrance      <--[Message given here] */
791            compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Matching BEGIN command has no survey name*/36);
792         } else {
793            /* TRANSLATORS: *BEGIN <survey> and *END <survey> should have the
794             * same <survey> if it’s given at all */
795            compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Survey name doesn’t match BEGIN*/193);
796         }
797         skipline();
798      } else {
799         /* TRANSLATORS: Used when a BEGIN command has a survey name, but the
800          * END command omits it, e.g.:
801          *
802          * *begin entrance
803          * 1 2 10.00 178 -01
804          * *end     <--[Message given here] */
805         compile_diagnostic(DIAG_WARN|DIAG_COL, /*Survey name omitted from END*/194);
806      }
807   }
808}
809
810static void
811cmd_entrance(void)
812{
813   prefix *pfx = read_prefix(PFX_STATION);
814   pfx->sflags |= BIT(SFLAGS_ENTRANCE);
815}
816
817static const prefix * first_fix_name = NULL;
818static const char * first_fix_filename;
819static unsigned first_fix_line;
820
821static void
822cmd_fix(void)
823{
824   prefix *fix_name;
825   node *stn = NULL;
826   static prefix *name_omit_already = NULL;
827   static const char * name_omit_already_filename = NULL;
828   static unsigned int name_omit_already_line;
829   real x, y, z;
830   filepos fp;
831
832   fix_name = read_prefix(PFX_STATION|PFX_ALLOW_ROOT);
833   fix_name->sflags |= BIT(SFLAGS_FIXED);
834
835   get_pos(&fp);
836   get_token();
837   if (strcmp(ucbuffer, "REFERENCE") == 0) {
838      /* suppress "unused fixed point" warnings for this station */
839      fix_name->sflags |= BIT(SFLAGS_USED);
840   } else {
841      if (*ucbuffer) set_pos(&fp);
842   }
843
844   x = read_numeric(fTrue);
845   if (x == HUGE_REAL) {
846      /* If the end of the line isn't blank, read a number after all to
847       * get a more helpful error message */
848      if (!isEol(ch) && !isComm(ch)) x = read_numeric(fFalse);
849   }
850   if (x == HUGE_REAL) {
851      if (pcs->proj || proj_out) {
852         compile_diagnostic(DIAG_ERR|DIAG_COL|DIAG_SKIP, /*Coordinates can't be omitted when coordinate system has been specified*/439);
853         return;
854      }
855
856      if (fix_name == name_omit_already) {
857         compile_diagnostic(DIAG_WARN|DIAG_COL, /*Same station fixed twice with no coordinates*/61);
858         return;
859      }
860
861      /* TRANSLATORS: " *fix a " gives this message: */
862      compile_diagnostic(DIAG_WARN|DIAG_COL, /*FIX command with no coordinates - fixing at (0,0,0)*/54);
863
864      if (name_omit_already) {
865         /* TRANSLATORS: Emitted after second and subsequent "FIX command with
866          * no coordinates - fixing at (0,0,0)" warnings.
867          */
868         compile_diagnostic_at(DIAG_ERR|DIAG_COL,
869                               name_omit_already_filename,
870                               name_omit_already_line,
871                               /*Already had FIX command with no coordinates for station “%s”*/441,
872                               sprint_prefix(name_omit_already));
873      } else {
874         name_omit_already = fix_name;
875         name_omit_already_filename = file.filename;
876         name_omit_already_line = file.line;
877      }
878
879      x = y = z = (real)0.0;
880   } else {
881      real sdx;
882      y = read_numeric(fFalse);
883      z = read_numeric(fFalse);
884
885      if (pcs->proj && proj_out) {
886         if (pj_is_latlong(pcs->proj)) {
887            /* PROJ expects lat and long in radians. */
888            x = rad(x);
889            y = rad(y);
890         }
891         int r = pj_transform(pcs->proj, proj_out, 1, 1, &x, &y, &z);
892         if (r != 0) {
893            compile_diagnostic(DIAG_ERR, /*Failed to convert coordinates: %s*/436, pj_strerrno(r));
894         }
895      } else if (pcs->proj) {
896         compile_diagnostic(DIAG_ERR, /*The input projection is set but the output projection isn't*/437);
897      } else if (proj_out) {
898         compile_diagnostic(DIAG_ERR, /*The output projection is set but the input projection isn't*/438);
899      }
900
901      get_pos(&fp);
902      sdx = read_numeric(fTrue);
903      if (sdx <= 0) {
904          set_pos(&fp);
905          compile_diagnostic(DIAG_ERR|DIAG_SKIP|DIAG_NUM, /*Standard deviation must be positive*/48);
906          return;
907      }
908      if (sdx != HUGE_REAL) {
909         real sdy, sdz;
910         real cxy = 0, cyz = 0, czx = 0;
911         get_pos(&fp);
912         sdy = read_numeric(fTrue);
913         if (sdy == HUGE_REAL) {
914            /* only one variance given */
915            sdy = sdz = sdx;
916         } else {
917            if (sdy <= 0) {
918               set_pos(&fp);
919               compile_diagnostic(DIAG_ERR|DIAG_SKIP|DIAG_NUM, /*Standard deviation must be positive*/48);
920               return;
921            }
922            get_pos(&fp);
923            sdz = read_numeric(fTrue);
924            if (sdz == HUGE_REAL) {
925               /* two variances given - horizontal & vertical */
926               sdz = sdy;
927               sdy = sdx;
928            } else {
929               if (sdz <= 0) {
930                  set_pos(&fp);
931                  compile_diagnostic(DIAG_ERR|DIAG_SKIP|DIAG_NUM, /*Standard deviation must be positive*/48);
932                  return;
933               }
934               cxy = read_numeric(fTrue);
935               if (cxy != HUGE_REAL) {
936                  /* covariances given */
937                  cyz = read_numeric(fFalse);
938                  czx = read_numeric(fFalse);
939               } else {
940                  cxy = 0;
941               }
942            }
943         }
944         stn = StnFromPfx(fix_name);
945         if (!fixed(stn)) {
946            node *fixpt = osnew(node);
947            prefix *name;
948            name = osnew(prefix);
949            name->pos = osnew(pos);
950            name->ident = NULL;
951            name->shape = 0;
952            fixpt->name = name;
953            name->stn = fixpt;
954            name->up = NULL;
955            if (TSTBIT(pcs->infer, INFER_EXPORTS)) {
956               name->min_export = USHRT_MAX;
957            } else {
958               name->min_export = 0;
959            }
960            name->max_export = 0;
961            name->sflags = 0;
962            add_stn_to_list(&stnlist, fixpt);
963            POS(fixpt, 0) = x;
964            POS(fixpt, 1) = y;
965            POS(fixpt, 2) = z;
966            fix(fixpt);
967            fixpt->leg[0] = fixpt->leg[1] = fixpt->leg[2] = NULL;
968            addfakeleg(fixpt, stn, 0, 0, 0,
969                       sdx * sdx, sdy * sdy, sdz * sdz
970#ifndef NO_COVARIANCES
971                       , cxy, cyz, czx
972#endif
973                       );
974         }
975
976         if (!first_fix_name) {
977            /* We track if we've fixed a station yet, and if so what the name
978             * of the first fix was, so that we can issue an error if the
979             * output coordinate system is set after fixing a station. */
980            first_fix_name = fix_name;
981            first_fix_filename = file.filename;
982            first_fix_line = file.line;
983         }
984
985         return;
986      }
987   }
988
989   if (!first_fix_name) {
990      /* We track if we've fixed a station yet, and if so what the name of the
991       * first fix was, so that we can issue an error if the output coordinate
992       * system is set after fixing a station. */
993      first_fix_name = fix_name;
994      first_fix_filename = file.filename;
995      first_fix_line = file.line;
996   }
997
998   stn = StnFromPfx(fix_name);
999   if (!fixed(stn)) {
1000      POS(stn, 0) = x;
1001      POS(stn, 1) = y;
1002      POS(stn, 2) = z;
1003      fix(stn);
1004      return;
1005   }
1006
1007   if (x != POS(stn, 0) || y != POS(stn, 1) || z != POS(stn, 2)) {
1008      compile_diagnostic(DIAG_ERR, /*Station already fixed or equated to a fixed point*/46);
1009      return;
1010   }
1011   /* TRANSLATORS: *fix a 1 2 3 / *fix a 1 2 3 */
1012   compile_diagnostic(DIAG_WARN, /*Station already fixed at the same coordinates*/55);
1013}
1014
1015static void
1016cmd_flags(void)
1017{
1018   static const sztok flagtab[] = {
1019        {"DUPLICATE", FLAGS_DUPLICATE },
1020        {"NOT",       FLAGS_NOT },
1021        {"SPLAY",     FLAGS_SPLAY },
1022        {"SURFACE",   FLAGS_SURFACE },
1023        {NULL,        FLAGS_UNKNOWN }
1024   };
1025   bool fNot = fFalse;
1026   bool fEmpty = fTrue;
1027   while (1) {
1028      int flag;
1029      get_token();
1030      /* If buffer is empty, it could mean end of line, or maybe
1031       * some non-letter junk which is better reported later */
1032      if (!buffer[0]) break;
1033
1034      fEmpty = fFalse;
1035      flag = match_tok(flagtab, TABSIZE(flagtab));
1036      /* treat the second NOT in "NOT NOT" as an unknown flag */
1037      if (flag == FLAGS_UNKNOWN || (fNot && flag == FLAGS_NOT)) {
1038         compile_diagnostic(DIAG_ERR|DIAG_BUF, /*FLAG “%s” unknown*/68, buffer);
1039         /* Recover from “*FLAGS NOT BOGUS SURFACE” by ignoring "NOT BOGUS" */
1040         fNot = fFalse;
1041      } else if (flag == FLAGS_NOT) {
1042         fNot = fTrue;
1043      } else if (fNot) {
1044         pcs->flags &= ~BIT(flag);
1045         fNot = fFalse;
1046      } else {
1047         pcs->flags |= BIT(flag);
1048      }
1049   }
1050
1051   if (fNot) {
1052      compile_diagnostic(DIAG_ERR|DIAG_BUF, /*Expecting “DUPLICATE”, “SPLAY”, or “SURFACE”*/188);
1053   } else if (fEmpty) {
1054      compile_diagnostic(DIAG_ERR|DIAG_BUF, /*Expecting “NOT”, “DUPLICATE”, “SPLAY”, or “SURFACE”*/189);
1055   }
1056}
1057
1058static void
1059cmd_equate(void)
1060{
1061   prefix *name1, *name2;
1062   bool fOnlyOneStn = fTrue; /* to trap eg *equate entrance.6 */
1063   filepos fp;
1064
1065   get_pos(&fp);
1066   name1 = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_SUSPECT_TYPO);
1067   while (fTrue) {
1068      name2 = name1;
1069      skipblanks();
1070      if (isEol(ch) || isComm(ch)) {
1071         if (fOnlyOneStn) {
1072            set_pos(&fp);
1073            /* TRANSLATORS: EQUATE is a command name, so shouldn’t be
1074             * translated.
1075             *
1076             * Here "station" is a survey station, not a train station.
1077             */
1078            compile_diagnostic(DIAG_ERR|DIAG_SKIP|DIAG_TOKEN, /*Only one station in EQUATE command*/33);
1079         }
1080         return;
1081      }
1082
1083      name1 = read_prefix(PFX_STATION|PFX_ALLOW_ROOT|PFX_SUSPECT_TYPO);
1084      process_equate(name1, name2);
1085      fOnlyOneStn = fFalse;
1086   }
1087}
1088
1089static void
1090report_missing_export(prefix *pfx, int depth)
1091{
1092   char *s;
1093   const char *p;
1094   prefix *survey = pfx;
1095   int i;
1096   for (i = depth + 1; i; i--) {
1097      survey = survey->up;
1098      SVX_ASSERT(survey);
1099   }
1100   s = osstrdup(sprint_prefix(survey));
1101   p = sprint_prefix(pfx);
1102   if (survey->filename) {
1103      /* TRANSLATORS: A station must be exported out of each level it is in, so
1104       * this would give "Station “\outer.inner.1” not exported from survey
1105       * “\outer”)":
1106       *
1107       * *equate entrance outer.inner.1
1108       * *begin outer
1109       * *begin inner
1110       * *export 1
1111       * 1 2 1.23 045 -6
1112       * *end inner
1113       * *end outer
1114       *
1115       * Here "survey" is a "cave map" rather than list of questions - it should be
1116       * translated to the terminology that cavers using the language would use.
1117       */
1118      compile_diagnostic_pfx(DIAG_ERR, survey,
1119                             /*Station “%s” not exported from survey “%s”*/26, p, s);
1120   } else {
1121      compile_diagnostic(DIAG_ERR, /*Station “%s” not exported from survey “%s”*/26, p, s);
1122   }
1123   osfree(s);
1124}
1125
1126static void
1127cmd_export(void)
1128{
1129   prefix *pfx;
1130
1131   fExportUsed = fTrue;
1132   do {
1133      int depth = 0;
1134      pfx = read_prefix(PFX_STATION|PFX_NEW);
1135      if (pfx == NULL) {
1136         /* The argument was an existing station. */
1137         /* FIXME */
1138      } else {
1139         prefix *p = pfx;
1140         while (p != NULL && p != pcs->Prefix) {
1141            depth++;
1142            p = p->up;
1143         }
1144         /* Something like: *export \foo, but we've excluded use of root */
1145         SVX_ASSERT(p);
1146      }
1147      /* *export \ or similar bogus stuff */
1148      SVX_ASSERT(depth);
1149#if 0
1150      printf("C min %d max %d depth %d pfx %s\n",
1151             pfx->min_export, pfx->max_export, depth, sprint_prefix(pfx));
1152#endif
1153      if (pfx->min_export == 0) {
1154         /* not encountered *export for this name before */
1155         if (pfx->max_export > depth) report_missing_export(pfx, depth);
1156         pfx->min_export = pfx->max_export = depth;
1157      } else if (pfx->min_export != USHRT_MAX) {
1158         /* FIXME: what to do if a station is marked for inferred exports
1159          * but is then explicitly exported?  Currently we just ignore the
1160          * explicit export... */
1161         if (pfx->min_export - 1 > depth) {
1162            report_missing_export(pfx, depth);
1163         } else if (pfx->min_export - 1 < depth) {
1164            /* TRANSLATORS: Here "station" is a survey station, not a train station.
1165             *
1166             * Exporting a station twice gives this error:
1167             *
1168             * *begin example
1169             * *export 1
1170             * *export 1
1171             * 1 2 1.24 045 -6
1172             * *end example */
1173            compile_diagnostic(DIAG_ERR, /*Station “%s” already exported*/66,
1174                               sprint_prefix(pfx));
1175         }
1176         pfx->min_export = depth;
1177      }
1178      skipblanks();
1179   } while (!isEol(ch) && !isComm(ch));
1180}
1181
1182static void
1183cmd_data(void)
1184{
1185   static const sztok dtab[] = {
1186        {"ALTITUDE",     Dz },
1187        {"BACKBEARING",  BackComp },
1188        {"BACKCLINO",    BackClino }, /* alternative name */
1189        {"BACKCOMPASS",  BackComp }, /* alternative name */
1190        {"BACKGRADIENT", BackClino },
1191        {"BACKLENGTH",   BackTape },
1192        {"BACKTAPE",     BackTape }, /* alternative name */
1193        {"BEARING",      Comp },
1194        {"CEILING",      Up }, /* alternative name */
1195        {"CLINO",        Clino }, /* alternative name */
1196        {"COMPASS",      Comp }, /* alternative name */
1197        {"COUNT",        Count }, /* FrCount&ToCount in multiline */
1198        {"DEPTH",        Depth }, /* FrDepth&ToDepth in multiline */
1199        {"DEPTHCHANGE",  DepthChange },
1200        {"DIRECTION",    Dir },
1201        {"DOWN",         Down },
1202        {"DX",           Dx },
1203        {"DY",           Dy },
1204        {"DZ",           Dz },
1205        {"EASTING",      Dx },
1206        {"FLOOR",        Down }, /* alternative name */
1207        {"FROM",         Fr },
1208        {"FROMCOUNT",    FrCount },
1209        {"FROMDEPTH",    FrDepth },
1210        {"GRADIENT",     Clino },
1211        {"IGNORE",       Ignore },
1212        {"IGNOREALL",    IgnoreAll },
1213        {"LEFT",         Left },
1214        {"LENGTH",       Tape },
1215        {"NEWLINE",      Newline },
1216        {"NORTHING",     Dy },
1217        {"RIGHT",        Right },
1218        {"STATION",      Station }, /* Fr&To in multiline */
1219        {"TAPE",         Tape }, /* alternative name */
1220        {"TO",           To },
1221        {"TOCOUNT",      ToCount },
1222        {"TODEPTH",      ToDepth },
1223        {"UP",           Up },
1224        {NULL,           End }
1225   };
1226
1227#define MASK_stns BIT(Fr) | BIT(To) | BIT(Station)
1228#define MASK_tape BIT(Tape) | BIT(BackTape) | BIT(FrCount) | BIT(ToCount) | BIT(Count)
1229#define MASK_dpth BIT(FrDepth) | BIT(ToDepth) | BIT(Depth) | BIT(DepthChange)
1230#define MASK_comp BIT(Comp) | BIT(BackComp)
1231#define MASK_clin BIT(Clino) | BIT(BackClino)
1232
1233#define MASK_NORMAL MASK_stns | BIT(Dir) | MASK_tape | MASK_comp | MASK_clin
1234#define MASK_DIVING MASK_NORMAL | MASK_dpth
1235#define MASK_CARTESIAN MASK_stns | BIT(Dx) | BIT(Dy) | BIT(Dz)
1236#define MASK_CYLPOLAR  MASK_stns | BIT(Dir) | MASK_tape | MASK_comp | MASK_dpth
1237#define MASK_NOSURVEY MASK_stns
1238#define MASK_PASSAGE BIT(Station) | BIT(Left) | BIT(Right) | BIT(Up) | BIT(Down)
1239
1240   /* readings which may be given for each style */
1241   static const unsigned long mask[] = {
1242      MASK_NORMAL, MASK_DIVING, MASK_CARTESIAN, MASK_CYLPOLAR, MASK_NOSURVEY,
1243      MASK_PASSAGE
1244   };
1245
1246   /* readings which may be omitted for each style */
1247   static const unsigned long mask_optional[] = {
1248      BIT(Dir) | BIT(Clino) | BIT(BackClino),
1249      BIT(Dir) | BIT(Clino) | BIT(BackClino),
1250      0,
1251      BIT(Dir),
1252      0,
1253      0 /* BIT(Left) | BIT(Right) | BIT(Up) | BIT(Down), */
1254   };
1255
1256   /* all valid readings */
1257   static const unsigned long mask_all[] = {
1258      MASK_NORMAL | BIT(Newline) | BIT(Ignore) | BIT(IgnoreAll) | BIT(End),
1259      MASK_DIVING | BIT(Newline) | BIT(Ignore) | BIT(IgnoreAll) | BIT(End),
1260      MASK_CARTESIAN | BIT(Newline) | BIT(Ignore) | BIT(IgnoreAll) | BIT(End),
1261      MASK_CYLPOLAR | BIT(Newline) | BIT(Ignore) | BIT(IgnoreAll) | BIT(End),
1262      MASK_NOSURVEY | BIT(Ignore) | BIT(IgnoreAll) | BIT(End),
1263      MASK_PASSAGE | BIT(Ignore) | BIT(IgnoreAll) | BIT(End)
1264   };
1265#define STYLE_DEFAULT   -2
1266#define STYLE_UNKNOWN   -1
1267
1268   static const sztok styletab[] = {
1269        {"CARTESIAN",    STYLE_CARTESIAN },
1270        {"CYLPOLAR",     STYLE_CYLPOLAR },
1271        {"DEFAULT",      STYLE_DEFAULT },
1272        {"DIVING",       STYLE_DIVING },
1273        {"NORMAL",       STYLE_NORMAL },
1274        {"NOSURVEY",     STYLE_NOSURVEY },
1275        {"PASSAGE",      STYLE_PASSAGE },
1276        {"TOPOFIL",      STYLE_NORMAL },
1277        {NULL,           STYLE_UNKNOWN }
1278   };
1279
1280#define m_multi (BIT(Station) | BIT(Count) | BIT(Depth))
1281
1282   int style, k = 0, kMac;
1283   reading *new_order, d;
1284   unsigned long mUsed = 0;
1285   char *style_name;
1286   int old_style = pcs->style;
1287
1288   /* after a bad *data command ignore survey data until the next
1289    * *data command to avoid an avalanche of errors */
1290   pcs->style = STYLE_IGNORE;
1291
1292   kMac = 6; /* minimum for NORMAL style */
1293   new_order = osmalloc(kMac * sizeof(reading));
1294
1295   get_token();
1296   style = match_tok(styletab, TABSIZE(styletab));
1297
1298   if (style == STYLE_DEFAULT) {
1299      default_style(pcs);
1300      return;
1301   }
1302
1303   if (style == STYLE_UNKNOWN) {
1304      if (!buffer[0]) {
1305         /* "*data" reinitialises the current style - for *data passage that
1306          * breaks the passage.
1307          */
1308         style = old_style;
1309         goto reinit_style;
1310      }
1311      /* TRANSLATORS: e.g. trying to refer to an invalid FNORD data style */
1312      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Data style “%s” unknown*/65, buffer);
1313      return;
1314   }
1315
1316   skipblanks();
1317#ifndef NO_DEPRECATED
1318   /* Olde syntax had optional field for survey grade, so allow an omit
1319    * but issue a warning about it */
1320   if (isOmit(ch)) {
1321      static int data_depr_count = 0;
1322      if (data_depr_count < 5) {
1323         compile_diagnostic(DIAG_WARN|DIAG_BUF, /*“*data %s %c …” is deprecated - use “*data %s …” instead*/104,
1324                            buffer, ch, buffer);
1325         if (++data_depr_count == 5)
1326            compile_diagnostic(DIAG_WARN, /*Further uses of this deprecated feature will not be reported*/95);
1327      }
1328      nextch();
1329   }
1330#endif
1331
1332   style_name = osstrdup(buffer);
1333   do {
1334      filepos fp;
1335      get_pos(&fp);
1336      get_token();
1337      d = match_tok(dtab, TABSIZE(dtab));
1338      /* only token allowed after IGNOREALL is NEWLINE */
1339      if (k && new_order[k - 1] == IgnoreAll && d != Newline) {
1340         set_pos(&fp);
1341         break;
1342      }
1343      /* Note: an unknown token is reported as trailing garbage */
1344      if (!TSTBIT(mask_all[style], d)) {
1345         /* TRANSLATORS: a data "style" is something like NORMAL, DIVING, etc.
1346          * a "reading" is one of FROM, TO, TAPE, COMPASS, CLINO for NORMAL
1347          * neither style nor reading is a keyword in the program This error
1348          * complains about a depth gauge reading in normal style, for example
1349          */
1350         compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP,
1351                            /*Reading “%s” not allowed in data style “%s”*/63,
1352                            buffer, style_name);
1353         osfree(style_name);
1354         osfree(new_order);
1355         return;
1356      }
1357      if (TSTBIT(mUsed, Newline) && TSTBIT(m_multi, d)) {
1358         /* TRANSLATORS: caused by e.g.
1359          *
1360          * *data diving station newline depth tape compass
1361          *
1362          * ("depth" needs to occur before "newline"). */
1363         compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP,
1364                            /*Reading “%s” must occur before NEWLINE*/225, buffer);
1365         osfree(style_name);
1366         osfree(new_order);
1367         return;
1368      }
1369      /* Check for duplicates unless it's a special reading:
1370       *   IGNOREALL,IGNORE (duplicates allowed) ; END (not possible)
1371       */
1372      if (!((BIT(Ignore) | BIT(End) | BIT(IgnoreAll)) & BIT(d))) {
1373         if (TSTBIT(mUsed, d)) {
1374            /* TRANSLATORS: complains about a situation like trying to define
1375             * two from stations per leg */
1376            compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Duplicate reading “%s”*/67, buffer);
1377            osfree(style_name);
1378            osfree(new_order);
1379            return;
1380         } else {
1381            /* Check for previously listed readings which are incompatible
1382             * with this one - e.g. Count vs FrCount */
1383            bool fBad = fFalse;
1384            switch (d) {
1385             case Station:
1386               if (mUsed & (BIT(Fr) | BIT(To))) fBad = fTrue;
1387               break;
1388             case Fr: case To:
1389               if (TSTBIT(mUsed, Station)) fBad = fTrue;
1390               break;
1391             case Count:
1392               if (mUsed & (BIT(FrCount) | BIT(ToCount) | BIT(Tape)))
1393                  fBad = fTrue;
1394               break;
1395             case FrCount: case ToCount:
1396               if (mUsed & (BIT(Count) | BIT(Tape)))
1397                  fBad = fTrue;
1398               break;
1399             case Depth:
1400               if (mUsed & (BIT(FrDepth) | BIT(ToDepth) | BIT(DepthChange)))
1401                  fBad = fTrue;
1402               break;
1403             case FrDepth: case ToDepth:
1404               if (mUsed & (BIT(Depth) | BIT(DepthChange))) fBad = fTrue;
1405               break;
1406             case DepthChange:
1407               if (mUsed & (BIT(FrDepth) | BIT(ToDepth) | BIT(Depth)))
1408                  fBad = fTrue;
1409               break;
1410             case Newline:
1411               if (mUsed & ~m_multi) {
1412                  /* TRANSLATORS: e.g.
1413                   *
1414                   * *data normal from to tape newline compass clino */
1415                  compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*NEWLINE can only be preceded by STATION, DEPTH, and COUNT*/226);
1416                  osfree(style_name);
1417                  osfree(new_order);
1418                  return;
1419               }
1420               if (k == 0) {
1421                  /* TRANSLATORS: error from:
1422                   *
1423                   * *data normal newline from to tape compass clino */
1424                  compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*NEWLINE can’t be the first reading*/222);
1425                  osfree(style_name);
1426                  osfree(new_order);
1427                  return;
1428               }
1429               break;
1430             default: /* avoid compiler warnings about unhandled enums */
1431               break;
1432            }
1433            if (fBad) {
1434               /* Not entirely happy with phrasing this... */
1435               /* TRANSLATORS: This is an error from the *DATA command.  It
1436                * means that a reading (which will appear where %s is isn't
1437                * valid as the list of readings has already included the same
1438                * reading, or an equivalent one (e.g. you can't have both
1439                * DEPTH and DEPTHCHANGE together). */
1440               compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Reading “%s” duplicates previous reading(s)*/77,
1441                                         buffer);
1442               osfree(style_name);
1443               osfree(new_order);
1444               return;
1445            }
1446            mUsed |= BIT(d); /* used to catch duplicates */
1447         }
1448      }
1449      if (k && new_order[k - 1] == IgnoreAll) {
1450         SVX_ASSERT(d == Newline);
1451         k--;
1452         d = IgnoreAllAndNewLine;
1453      }
1454      if (k >= kMac) {
1455         kMac = kMac * 2;
1456         new_order = osrealloc(new_order, kMac * sizeof(reading));
1457      }
1458      new_order[k++] = d;
1459   } while (d != End);
1460
1461   if (k >= 2 && new_order[k - 2] == Newline) {
1462      /* TRANSLATORS: error from:
1463       *
1464       * *data normal from to tape compass clino newline */
1465      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*NEWLINE can’t be the last reading*/223);
1466      osfree(style_name);
1467      osfree(new_order);
1468      return;
1469   }
1470
1471   if (style == STYLE_NOSURVEY) {
1472      if (TSTBIT(mUsed, Station)) {
1473         if (k >= kMac) {
1474            kMac = kMac * 2;
1475            new_order = osrealloc(new_order, kMac * sizeof(reading));
1476         }
1477         new_order[k - 1] = Newline;
1478         new_order[k++] = End;
1479      }
1480   } else if (style == STYLE_PASSAGE) {
1481      /* Station doesn't mean "multiline" for STYLE_PASSAGE. */
1482   } else if (!TSTBIT(mUsed, Newline) && (m_multi & mUsed)) {
1483      /* TRANSLATORS: Error given by something like:
1484       *
1485       * *data normal station tape compass clino
1486       *
1487       * ("station" signifies interleaved data). */
1488      compile_diagnostic(DIAG_ERR|DIAG_SKIP, /*Interleaved readings, but no NEWLINE*/224);
1489      osfree(style_name);
1490      osfree(new_order);
1491      return;
1492   }
1493
1494#if 0
1495   printf("mUsed = 0x%x\n", mUsed);
1496#endif
1497
1498   /* Check the supplied readings form a sufficient set. */
1499   if (style != STYLE_PASSAGE) {
1500       if ((mUsed & (BIT(Fr) | BIT(To))) == (BIT(Fr) | BIT(To)))
1501           mUsed |= BIT(Station);
1502       else if (TSTBIT(mUsed, Station))
1503           mUsed |= BIT(Fr) | BIT(To);
1504   }
1505
1506   if (mUsed & (BIT(Comp) | BIT(BackComp)))
1507      mUsed |= BIT(Comp) | BIT(BackComp);
1508
1509   if (mUsed & (BIT(Clino) | BIT(BackClino)))
1510      mUsed |= BIT(Clino) | BIT(BackClino);
1511
1512   if ((mUsed & (BIT(FrDepth) | BIT(ToDepth))) == (BIT(FrDepth) | BIT(ToDepth)))
1513      mUsed |= BIT(Depth) | BIT(DepthChange);
1514   else if (mUsed & (BIT(Depth) | BIT(DepthChange)))
1515      mUsed |= BIT(FrDepth) | BIT(ToDepth) | BIT(Depth) | BIT(DepthChange);
1516
1517   if ((mUsed & (BIT(FrCount) | BIT(ToCount))) == (BIT(FrCount) | BIT(ToCount)))
1518      mUsed |= BIT(Count) | BIT(Tape) | BIT(BackTape);
1519   else if (mUsed & (BIT(Count) | BIT(Tape) | BIT(BackTape)))
1520      mUsed |= BIT(FrCount) | BIT(ToCount) | BIT(Count) | BIT(Tape) | BIT(BackTape);
1521
1522#if 0
1523   printf("mUsed = 0x%x, opt = 0x%x, mask = 0x%x\n", mUsed,
1524          mask_optional[style], mask[style]);
1525#endif
1526
1527   if (((mUsed &~ BIT(Newline)) | mask_optional[style]) != mask[style]) {
1528      /* Test should only fail with too few bits set, not too many */
1529      SVX_ASSERT((((mUsed &~ BIT(Newline)) | mask_optional[style])
1530              &~ mask[style]) == 0);
1531      /* TRANSLATORS: i.e. not enough readings for the style. */
1532      compile_diagnostic(DIAG_ERR|DIAG_SKIP, /*Too few readings for data style “%s”*/64, style_name);
1533      osfree(style_name);
1534      osfree(new_order);
1535      return;
1536   }
1537
1538   /* don't free default ordering or ordering used by parent */
1539   if (pcs->ordering != default_order &&
1540       !(pcs->next && pcs->next->ordering == pcs->ordering))
1541      osfree((reading*)pcs->ordering);
1542
1543   pcs->style = style;
1544   pcs->ordering = new_order;
1545
1546   osfree(style_name);
1547
1548reinit_style:
1549   if (style == STYLE_PASSAGE) {
1550      lrudlist * new_psg = osnew(lrudlist);
1551      new_psg->tube = NULL;
1552      new_psg->next = model;
1553      model = new_psg;
1554      next_lrud = &(new_psg->tube);
1555   }
1556}
1557
1558static void
1559cmd_units(void)
1560{
1561   int units, quantity;
1562   unsigned long qmask;
1563   unsigned long m; /* mask with bit x set to indicate quantity x specified */
1564   real factor;
1565   filepos fp;
1566
1567   qmask = get_qlist(BIT(Q_POS)|BIT(Q_PLUMB)|BIT(Q_LEVEL));
1568
1569   if (!qmask) return;
1570   if (qmask == BIT(Q_DEFAULT)) {
1571      default_units(pcs);
1572      return;
1573   }
1574
1575   get_pos(&fp);
1576   factor = read_numeric(fTrue);
1577   if (factor == 0.0) {
1578      set_pos(&fp);
1579      /* TRANSLATORS: error message given by "*units tape 0 feet" - it’s
1580       * meaningless to say your tape is marked in "0 feet" (but you might
1581       * measure distance by counting knots on a diving line, and tie them
1582       * every "2 feet"). */
1583      compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /**UNITS factor must be non-zero*/200);
1584      skipline();
1585      return;
1586   }
1587
1588   units = get_units(qmask, fTrue);
1589   if (units == UNITS_NULL) return;
1590   if (TSTBIT(qmask, Q_GRADIENT))
1591      pcs->f_clino_percent = (units == UNITS_PERCENT);
1592   if (TSTBIT(qmask, Q_BACKGRADIENT))
1593      pcs->f_backclino_percent = (units == UNITS_PERCENT);
1594
1595   if (factor == HUGE_REAL) {
1596      factor = factor_tab[units];
1597   } else {
1598      factor *= factor_tab[units];
1599   }
1600
1601   for (quantity = 0, m = BIT(quantity); m <= qmask; quantity++, m <<= 1)
1602      if (qmask & m) pcs->units[quantity] = factor;
1603}
1604
1605static void
1606cmd_calibrate(void)
1607{
1608   real sc, z;
1609   unsigned long qmask, m;
1610   int quantity;
1611   filepos fp;
1612
1613   qmask = get_qlist(BIT(Q_POS)|BIT(Q_PLUMB)|BIT(Q_LEVEL));
1614   if (!qmask) return; /* error already reported */
1615
1616   if (qmask == BIT(Q_DEFAULT)) {
1617      default_calib(pcs);
1618      return;
1619   }
1620
1621   if (((qmask & LEN_QMASK)) && ((qmask & ANG_QMASK))) {
1622      /* TRANSLATORS: e.g.
1623       *
1624       * *calibrate tape compass 1 1
1625       */
1626      compile_diagnostic(DIAG_ERR|DIAG_SKIP, /*Can’t calibrate angular and length quantities together*/227);
1627      return;
1628   }
1629
1630   z = read_numeric(fFalse);
1631   get_pos(&fp);
1632   sc = read_numeric(fTrue);
1633   if (sc == HUGE_REAL) {
1634      if (isalpha(ch)) {
1635         int units = get_units(qmask, fFalse);
1636         if (units == UNITS_NULL) {
1637            return;
1638         }
1639         z *= factor_tab[units];
1640         sc = read_numeric(fTrue);
1641         if (sc == HUGE_REAL) {
1642            sc = (real)1.0;
1643         } else {
1644            /* Adjustment applied is: (reading - z) * sc
1645             * We want: reading * sc - z
1646             * So divide z by sc so the applied adjustment does what we want.
1647             */
1648            z /= sc;
1649         }
1650      } else {
1651         sc = (real)1.0;
1652      }
1653   }
1654
1655   if (sc == HUGE_REAL) sc = (real)1.0;
1656   /* check for declination scale */
1657   if (TSTBIT(qmask, Q_DECLINATION) && sc != 1.0) {
1658      set_pos(&fp);
1659      /* TRANSLATORS: DECLINATION is a built-in keyword, so best not to
1660       * translate */
1661      compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Scale factor must be 1.0 for DECLINATION*/40);
1662      skipline();
1663      return;
1664   }
1665   if (sc == 0.0) {
1666      set_pos(&fp);
1667      /* TRANSLATORS: If the scale factor for an instrument is zero, then any
1668       * reading would be mapped to zero, which doesn't make sense. */
1669      compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Scale factor must be non-zero*/391);
1670      skipline();
1671      return;
1672   }
1673   for (quantity = 0, m = BIT(quantity); m <= qmask; quantity++, m <<= 1) {
1674      if (qmask & m) {
1675         pcs->z[quantity] = pcs->units[quantity] * z;
1676         pcs->sc[quantity] = sc;
1677      }
1678   }
1679}
1680
1681static void
1682cmd_declination(void)
1683{
1684    real v = read_numeric(fTrue);
1685    if (v == HUGE_REAL) {
1686        get_token_no_blanks();
1687        if (strcmp(ucbuffer, "AUTO") != 0) {
1688            compile_diagnostic(DIAG_ERR|DIAG_SKIP|DIAG_COL, /*Expected number or “AUTO”*/309);
1689            return;
1690        }
1691        /* *declination auto X Y Z */
1692        real x = read_numeric(fFalse);
1693        real y = read_numeric(fFalse);
1694        real z = read_numeric(fFalse);
1695        if (!pcs->proj) {
1696            compile_diagnostic(DIAG_ERR, /*Input coordinate system must be specified for “*DECLINATION AUTO”*/301);
1697            return;
1698        }
1699        if (!proj_wgs84) {
1700            proj_wgs84 = pj_init_plus("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
1701        }
1702        /* Convert to WGS84 lat long. */
1703        if (pj_is_latlong(pcs->proj)) {
1704            /* PROJ expects lat and long in radians. */
1705            x = rad(x);
1706            y = rad(y);
1707        }
1708        int r = pj_transform(pcs->proj, proj_wgs84, 1, 1, &x, &y, &z);
1709        if (r != 0) {
1710            compile_diagnostic(DIAG_ERR, /*Failed to convert coordinates: %s*/436, pj_strerrno(r));
1711            return;
1712        }
1713        pcs->z[Q_DECLINATION] = HUGE_REAL;
1714        pcs->dec_x = x;
1715        pcs->dec_y = y;
1716        pcs->dec_z = z;
1717        /* Invalidate cached declination. */
1718        pcs->declination = HUGE_REAL;
1719        {
1720#ifdef HAVE_PROJ_H
1721            PJ_COORD lp;
1722            lp.lp.lam = x;
1723            lp.lp.phi = y;
1724            PJ_FACTORS factors = proj_factors(proj_out, lp);
1725            pcs->convergence = factors.meridian_convergence;
1726#else
1727            projLP lp = { x, y };
1728            struct FACTORS factors;
1729            memset(&factors, 0, sizeof(factors));
1730            pj_factors(lp, proj_out, 0.0, &factors);
1731            pcs->convergence = factors.conv;
1732#endif
1733        }
1734    } else {
1735        /* *declination D UNITS */
1736        int units = get_units(BIT(Q_DECLINATION), fFalse);
1737        if (units == UNITS_NULL) {
1738            return;
1739        }
1740        pcs->z[Q_DECLINATION] = -v * factor_tab[units];
1741        pcs->convergence = 0;
1742    }
1743}
1744
1745#ifndef NO_DEPRECATED
1746static void
1747cmd_default(void)
1748{
1749   static const sztok defaulttab[] = {
1750      { "CALIBRATE", CMD_CALIBRATE },
1751      { "DATA",      CMD_DATA },
1752      { "UNITS",     CMD_UNITS },
1753      { NULL,        CMD_NULL }
1754   };
1755   static int default_depr_count = 0;
1756
1757   if (default_depr_count < 5) {
1758      /* TRANSLATORS: If you're unsure what "deprecated" means, see:
1759       * https://en.wikipedia.org/wiki/Deprecation */
1760      compile_diagnostic(DIAG_WARN|DIAG_COL, /**DEFAULT is deprecated - use *CALIBRATE/DATA/SD/UNITS with argument DEFAULT instead*/20);
1761      if (++default_depr_count == 5)
1762         compile_diagnostic(DIAG_WARN, /*Further uses of this deprecated feature will not be reported*/95);
1763   }
1764
1765   get_token();
1766   switch (match_tok(defaulttab, TABSIZE(defaulttab))) {
1767    case CMD_CALIBRATE:
1768      default_calib(pcs);
1769      break;
1770    case CMD_DATA:
1771      default_style(pcs);
1772      default_grade(pcs);
1773      break;
1774    case CMD_UNITS:
1775      default_units(pcs);
1776      break;
1777    default:
1778      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Unknown setting “%s”*/41, buffer);
1779   }
1780}
1781#endif
1782
1783static void
1784cmd_include(void)
1785{
1786   char *pth, *fnm = NULL;
1787   int fnm_len;
1788#ifndef NO_DEPRECATED
1789   prefix *root_store;
1790#endif
1791   int ch_store;
1792
1793   pth = path_from_fnm(file.filename);
1794
1795   read_string(&fnm, &fnm_len);
1796
1797#ifndef NO_DEPRECATED
1798   /* Since *begin / *end nesting cannot cross file boundaries we only
1799    * need to preserve the prefix if the deprecated *prefix command
1800    * can be used */
1801   root_store = root;
1802   root = pcs->Prefix; /* Root for include file is current prefix */
1803#endif
1804   ch_store = ch;
1805
1806   data_file(pth, fnm);
1807
1808#ifndef NO_DEPRECATED
1809   root = root_store; /* and restore root */
1810#endif
1811   ch = ch_store;
1812
1813   s_free(&fnm);
1814   osfree(pth);
1815}
1816
1817static void
1818cmd_sd(void)
1819{
1820   real sd, variance;
1821   int units;
1822   unsigned long qmask, m;
1823   int quantity;
1824   qmask = get_qlist(BIT(Q_DECLINATION));
1825   if (!qmask) return; /* no quantities found - error already reported */
1826
1827   if (qmask == BIT(Q_DEFAULT)) {
1828      default_grade(pcs);
1829      return;
1830   }
1831   sd = read_numeric(fFalse);
1832   if (sd <= (real)0.0) {
1833      compile_diagnostic(DIAG_ERR|DIAG_SKIP|DIAG_COL, /*Standard deviation must be positive*/48);
1834      return;
1835   }
1836   units = get_units(qmask, fFalse);
1837   if (units == UNITS_NULL) return;
1838
1839   sd *= factor_tab[units];
1840   variance = sqrd(sd);
1841
1842   for (quantity = 0, m = BIT(quantity); m <= qmask; quantity++, m <<= 1)
1843      if (qmask & m) pcs->Var[quantity] = variance;
1844}
1845
1846static void
1847cmd_title(void)
1848{
1849   if (!fExplicitTitle && pcs->Prefix == root) {
1850       /* If we don't have an explicit title yet, and we're currently in the
1851        * root prefix, use this title explicitly. */
1852      fExplicitTitle = fTrue;
1853      read_string(&survey_title, &survey_title_len);
1854   } else {
1855      /* parse and throw away this title (but still check rest of line) */
1856      char *s = NULL;
1857      int len;
1858      read_string(&s, &len);
1859      s_free(&s);
1860   }
1861}
1862
1863static const sztok case_tab[] = {
1864     {"PRESERVE", OFF},
1865     {"TOLOWER",  LOWER},
1866     {"TOUPPER",  UPPER},
1867     {NULL,       -1}
1868};
1869
1870static void
1871cmd_case(void)
1872{
1873   int setting;
1874   get_token();
1875   setting = match_tok(case_tab, TABSIZE(case_tab));
1876   if (setting != -1) {
1877      pcs->Case = setting;
1878   } else {
1879      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Found “%s”, expecting “PRESERVE”, “TOUPPER”, or “TOLOWER”*/10, buffer);
1880   }
1881}
1882
1883typedef enum {
1884    CS_NONE = -1,
1885    CS_CUSTOM,
1886    CS_EPSG,
1887    CS_ESRI,
1888    CS_EUR,
1889    CS_IJTSK,
1890    CS_JTSK,
1891    CS_LAT,
1892    CS_LOCAL,
1893    CS_LONG,
1894    CS_OSGB,
1895    CS_S_MERC,
1896    CS_UTM
1897} cs_class;
1898
1899static const sztok cs_tab[] = {
1900     {"CUSTOM", CS_CUSTOM},
1901     {"EPSG",   CS_EPSG},       /* EPSG:<number> */
1902     {"ESRI",   CS_ESRI},       /* ESRI:<number> */
1903     {"EUR",    CS_EUR},        /* EUR79Z30 */
1904     {"IJTSK",  CS_IJTSK},      /* IJTSK or IJTSK03 */
1905     {"JTSK",   CS_JTSK},       /* JTSK or JTSK03 */
1906     {"LAT",    CS_LAT},        /* LAT-LONG */
1907     {"LOCAL",  CS_LOCAL},
1908     {"LONG",   CS_LONG},       /* LONG-LAT */
1909     {"OSGB",   CS_OSGB},       /* OSGB:<H, N, O, S or T><A-Z except I> */
1910     {"S",      CS_S_MERC},     /* S-MERC */
1911     {"UTM",    CS_UTM},        /* UTM<zone><N or S or nothing> */
1912     {NULL,     CS_NONE}
1913};
1914
1915static void
1916cmd_cs(void)
1917{
1918   char * proj_str = NULL;
1919   int proj_str_len;
1920   cs_class cs;
1921   int cs_sub = INT_MIN;
1922   filepos fp;
1923   bool output = fFalse;
1924   enum { YES, NO, MAYBE } ok_for_output = YES;
1925   static bool had_cs = fFalse;
1926
1927   if (!had_cs) {
1928      had_cs = fTrue;
1929      if (first_fix_name) {
1930         compile_diagnostic_at(DIAG_ERR,
1931                               first_fix_filename, first_fix_line,
1932                               /*Station “%s” fixed before CS command first used*/442,
1933                               sprint_prefix(first_fix_name));
1934      }
1935   }
1936
1937   get_pos(&fp);
1938   /* Note get_token() only accepts letters - it'll stop at digits so "UTM12"
1939    * will give token "UTM". */
1940   get_token();
1941   if (strcmp(ucbuffer, "OUT") == 0) {
1942      output = fTrue;
1943      get_pos(&fp);
1944      get_token();
1945   }
1946   cs = match_tok(cs_tab, TABSIZE(cs_tab));
1947   switch (cs) {
1948      case CS_NONE:
1949         break;
1950      case CS_CUSTOM:
1951         ok_for_output = MAYBE;
1952         get_pos(&fp);
1953         read_string(&proj_str, &proj_str_len);
1954         cs_sub = 0;
1955         break;
1956      case CS_EPSG: case CS_ESRI:
1957         ok_for_output = MAYBE;
1958         if (ch == ':' && isdigit(nextch())) {
1959            unsigned n = read_uint();
1960            if (n < 1000000) {
1961               cs_sub = (int)n;
1962            }
1963         }
1964         break;
1965      case CS_EUR:
1966         if (isdigit(ch) &&
1967             read_uint() == 79 &&
1968             (ch == 'Z' || ch == 'z') &&
1969             isdigit(nextch()) &&
1970             read_uint() == 30) {
1971            cs_sub = 7930;
1972         }
1973         break;
1974      case CS_JTSK:
1975         ok_for_output = NO;
1976         /* FALLTHRU */
1977      case CS_IJTSK:
1978         if (ch == '0') {
1979            if (nextch() == '3') {
1980               nextch();
1981               cs_sub = 3;
1982            }
1983         } else {
1984            cs_sub = 0;
1985         }
1986         break;
1987      case CS_LAT: case CS_LONG:
1988         ok_for_output = NO;
1989         if (ch == '-') {
1990            nextch();
1991            get_token_no_blanks();
1992            cs_class cs2 = match_tok(cs_tab, TABSIZE(cs_tab));
1993            if ((cs ^ cs2) == (CS_LAT ^ CS_LONG)) {
1994                cs_sub = 0;
1995            }
1996         }
1997         break;
1998      case CS_LOCAL:
1999         cs_sub = 0;
2000         break;
2001      case CS_OSGB:
2002         if (ch == ':') {
2003            int uch1 = toupper(nextch());
2004            if (strchr("HNOST", uch1)) {
2005               int uch2 = toupper(nextch());
2006               if (uch2 >= 'A' && uch2 <= 'Z' && uch2 != 'I') {
2007                  int x, y;
2008                  nextch();
2009                  if (uch1 > 'I') --uch1;
2010                  uch1 -= 'A';
2011                  if (uch2 > 'I') --uch2;
2012                  uch2 -= 'A';
2013                  x = uch1 % 5;
2014                  y = uch1 / 5;
2015                  x = (x * 5) + uch2 % 5;
2016                  y = (y * 5) + uch2 / 5;
2017                  cs_sub = y * 25 + x;
2018               }
2019            }
2020         }
2021         break;
2022      case CS_S_MERC:
2023         if (ch == '-') {
2024            nextch();
2025            get_token_no_blanks();
2026            if (strcmp(ucbuffer, "MERC") == 0) {
2027               cs_sub = 0;
2028            }
2029         }
2030         break;
2031      case CS_UTM:
2032         if (isdigit(ch)) {
2033            unsigned n = read_uint();
2034            if (n >= 1 && n <= 60) {
2035               int uch = toupper(ch);
2036               cs_sub = (int)n;
2037               if (uch == 'S') {
2038                  nextch();
2039                  cs_sub = -cs_sub;
2040               } else if (uch == 'N') {
2041                  nextch();
2042               }
2043            }
2044         }
2045         break;
2046   }
2047   if (cs_sub == INT_MIN || isalnum(ch)) {
2048      set_pos(&fp);
2049      compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Unknown coordinate system*/434);
2050      skipline();
2051      return;
2052   }
2053   /* Actually handle the cs */
2054   switch (cs) {
2055      case CS_NONE:
2056         break;
2057      case CS_CUSTOM:
2058         /* proj_str already set */
2059         break;
2060      case CS_EPSG:
2061         proj_str = osmalloc(32);
2062         sprintf(proj_str, "+init=epsg:%d +no_defs", cs_sub);
2063         break;
2064      case CS_ESRI:
2065         proj_str = osmalloc(32);
2066         sprintf(proj_str, "+init=esri:%d +no_defs", cs_sub);
2067         break;
2068      case CS_EUR:
2069         proj_str = osstrdup("+proj=utm +zone=30 +ellps=intl +towgs84=-86,-98,-119,0,0,0,0 +no_defs");
2070         break;
2071      case CS_IJTSK:
2072         if (cs_sub == 0)
2073            proj_str = osstrdup("+proj=krovak +ellps=bessel +towgs84=570.8285,85.6769,462.842,4.9984,1.5867,5.2611,3.5623 +no_defs");
2074         else
2075            proj_str = osstrdup("+proj=krovak +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0 +no_defs");
2076         break;
2077      case CS_JTSK:
2078         if (cs_sub == 0)
2079            proj_str = osstrdup("+proj=krovak +czech +ellps=bessel +towgs84=570.8285,85.6769,462.842,4.9984,1.5867,5.2611,3.5623 +no_defs");
2080         else
2081            proj_str = osstrdup("+proj=krovak +czech +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0 +no_defs");
2082         break;
2083      case CS_LAT:
2084         /* FIXME: Requires PROJ >= 4.8.0 for +axis, and the SDs will be
2085          * misapplied, so we may want to swap ourselves.  Also, while
2086          * therion supports lat-long, I'm not totally convinced that it is
2087          * sensible to do so - people often say "lat-long", but probably
2088          * don't think that that's actually "Northing, Easting".  This
2089          * seems like it'll result in people accidentally getting X and Y
2090          * swapped in their fixed points...
2091          */
2092#if 0
2093         proj_str = osstrdup("+proj=longlat +ellps=WGS84 +datum=WGS84 +axis=neu +no_defs");
2094#endif
2095         break;
2096      case CS_LOCAL:
2097         /* FIXME: Is it useful to be able to explicitly specify this? */
2098         break;
2099      case CS_LONG:
2100         proj_str = osstrdup("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
2101         break;
2102      case CS_OSGB: {
2103         int x = 14 - (cs_sub % 25);
2104         int y = (cs_sub / 25) - 20;
2105         proj_str = osmalloc(160);
2106         sprintf(proj_str, "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=%d +y_0=%d +ellps=airy +datum=OSGB36 +units=m +no_defs", x * 100000, y * 100000);
2107         break;
2108      }
2109      case CS_S_MERC:
2110         proj_str = osstrdup("+proj=merc +lat_ts=0 +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +units=m +nadgrids=@null +no_defs");
2111         break;
2112      case CS_UTM:
2113         proj_str = osmalloc(74);
2114         if (cs_sub > 0) {
2115            sprintf(proj_str, "+proj=utm +ellps=WGS84 +datum=WGS84 +units=m +zone=%d +no_defs", cs_sub);
2116         } else {
2117            sprintf(proj_str, "+proj=utm +ellps=WGS84 +datum=WGS84 +units=m +zone=%d +south +no_defs", -cs_sub);
2118         }
2119         break;
2120   }
2121
2122   if (!proj_str) {
2123      /* printf("CS %d:%d\n", (int)cs, cs_sub); */
2124      set_pos(&fp);
2125      compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Unknown coordinate system*/434);
2126      skipline();
2127      return;
2128   }
2129
2130   if (output) {
2131      if (ok_for_output == NO) {
2132         set_pos(&fp);
2133         compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Coordinate system unsuitable for output*/435);
2134         skipline();
2135         return;
2136      }
2137
2138      /* If the output projection is already set, we still need to create the
2139       * projection object for a custom projection, so we can report errors.
2140       * But if the string is identical, we know it's valid.
2141       */
2142      if (!proj_out ||
2143          (ok_for_output == MAYBE && strcmp(proj_str, proj_str_out) != 0)) {
2144         projPJ pj = pj_init_plus(proj_str);
2145         if (!pj) {
2146            set_pos(&fp);
2147            compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Invalid coordinate system: %s*/443,
2148                               pj_strerrno(pj_errno));
2149            skipline();
2150            return;
2151         }
2152         if (ok_for_output == MAYBE && pj_is_latlong(pj)) {
2153            set_pos(&fp);
2154            compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Coordinate system unsuitable for output*/435);
2155            skipline();
2156            return;
2157         }
2158         if (proj_out) {
2159            pj_free(pj);
2160            osfree(proj_str);
2161         } else {
2162            proj_out = pj;
2163            proj_str_out = proj_str;
2164         }
2165      }
2166   } else {
2167      projPJ pj;
2168      if (proj_str_out && strcmp(proj_str, proj_str_out) == 0) {
2169         /* Same as the current output projection. */
2170         pj = proj_out;
2171      } else {
2172         pj = pj_init_plus(proj_str);
2173         if (!pj) {
2174            set_pos(&fp);
2175            compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*Invalid coordinate system: %s*/443,
2176                               pj_strerrno(pj_errno));
2177            skipline();
2178            return;
2179         }
2180      }
2181
2182      /* Free proj if not used by parent, or as the output projection. */
2183      settings * p = pcs;
2184      if (p->proj && (!p->next || p->proj != p->next->proj))
2185         if (p->proj != proj_out)
2186            pj_free(p->proj);
2187      p->proj = pj;
2188   }
2189}
2190
2191static const sztok infer_tab[] = {
2192     { "EQUATES",       INFER_EQUATES },
2193     { "EXPORTS",       INFER_EXPORTS },
2194     { "PLUMBS",        INFER_PLUMBS },
2195#if 0 /* FIXME */
2196     { "SUBSURVEYS",    INFER_SUBSURVEYS },
2197#endif
2198     { NULL,            INFER_NULL }
2199};
2200
2201static const sztok onoff_tab[] = {
2202     { "OFF", 0 },
2203     { "ON",  1 },
2204     { NULL, -1 }
2205};
2206
2207static void
2208cmd_infer(void)
2209{
2210   infer_what setting;
2211   int on;
2212   get_token();
2213   setting = match_tok(infer_tab, TABSIZE(infer_tab));
2214   if (setting == INFER_NULL) {
2215      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Found “%s”, expecting “EQUATES”, “EXPORTS”, or “PLUMBS”*/31, buffer);
2216      return;
2217   }
2218   get_token();
2219   on = match_tok(onoff_tab, TABSIZE(onoff_tab));
2220   if (on == -1) {
2221      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Found “%s”, expecting “ON” or “OFF”*/32, buffer);
2222      return;
2223   }
2224
2225   if (on) {
2226      pcs->infer |= BIT(setting);
2227      if (setting == INFER_EXPORTS) fExportUsed = fTrue;
2228   } else {
2229      pcs->infer &= ~BIT(setting);
2230   }
2231}
2232
2233static void
2234cmd_truncate(void)
2235{
2236   unsigned int truncate_at = 0; /* default is no truncation */
2237   filepos fp;
2238
2239   get_pos(&fp);
2240
2241   get_token();
2242   if (strcmp(ucbuffer, "OFF") != 0) {
2243      if (*ucbuffer) set_pos(&fp);
2244      truncate_at = read_uint();
2245   }
2246   /* for backward compatibility, "*truncate 0" means "*truncate off" */
2247   pcs->Truncate = (truncate_at == 0) ? INT_MAX : truncate_at;
2248}
2249
2250static void
2251cmd_ref(void)
2252{
2253   /* Just syntax check for now. */
2254   char *ref = NULL;
2255   int ref_len;
2256   read_string(&ref, &ref_len);
2257   s_free(&ref);
2258}
2259
2260static void
2261cmd_require(void)
2262{
2263   const unsigned int version[] = {COMMAVERSION};
2264   const unsigned int *ver = version;
2265   filepos fp;
2266
2267   skipblanks();
2268   get_pos(&fp);
2269   while (1) {
2270      int diff = *ver++ - read_uint();
2271      if (diff > 0) break;
2272      if (diff < 0) {
2273         size_t i, len;
2274         char *v;
2275         filepos fp_tmp;
2276
2277         /* find end of version number */
2278         while (isdigit(ch) || ch == '.') nextch();
2279         get_pos(&fp_tmp);
2280         len = (size_t)(fp_tmp.offset - fp.offset);
2281         v = osmalloc(len + 1);
2282         set_pos(&fp);
2283         for (i = 0; i < len; i++) {
2284            v[i] = ch;
2285            nextch();
2286         }
2287         v[i] = '\0';
2288         /* TRANSLATORS: Feel free to translate as "or newer" instead of "or
2289          * greater" if that gives a more natural translation.  It's
2290          * technically not quite right when there are parallel active release
2291          * series (e.g. Survex 1.0.40 was released *after* 1.2.0), but this
2292          * seems unlikely to confuse users.  "Survex" is the name of the
2293          * software, so should not be translated.
2294          *
2295          * Here "survey" is a "cave map" rather than list of questions - it should be
2296          * translated to the terminology that cavers using the language would use.
2297          */
2298         fatalerror_in_file(file.filename, file.line, /*Survex version %s or greater required to process this survey data.*/2, v);
2299      }
2300      if (ch != '.') break;
2301      nextch();
2302      if (!isdigit(ch) || ver == version + sizeof(version) / sizeof(*version))
2303         break;
2304   }
2305   /* skip rest of version number */
2306   while (isdigit(ch) || ch == '.') nextch();
2307}
2308
2309/* allocate new meta_data if need be */
2310void
2311copy_on_write_meta(settings *s)
2312{
2313   if (!s->meta || s->meta->ref_count != 0) {
2314       meta_data * meta_new = osnew(meta_data);
2315       if (!s->meta) {
2316           meta_new->days1 = meta_new->days2 = -1;
2317       } else {
2318           *meta_new = *(s->meta);
2319       }
2320       meta_new->ref_count = 0;
2321       s->meta = meta_new;
2322   }
2323}
2324
2325static void
2326cmd_date(void)
2327{
2328    int year, month, day;
2329    int days1, days2;
2330    bool implicit_range = fFalse;
2331    filepos fp, fp2;
2332
2333    get_pos(&fp);
2334    read_date(&year, &month, &day);
2335    days1 = days_since_1900(year, month ? month : 1, day ? day : 1);
2336
2337    if (days1 > current_days_since_1900) {
2338        set_pos(&fp);
2339        compile_diagnostic(DIAG_WARN|DIAG_DATE, /*Date is in the future!*/80);
2340    }
2341
2342    skipblanks();
2343    if (ch == '-') {
2344        nextch();
2345        get_pos(&fp2);
2346        read_date(&year, &month, &day);
2347    } else {
2348        if (month && day) {
2349            days2 = days1;
2350            goto read;
2351        }
2352        implicit_range = fTrue;
2353    }
2354
2355    if (month == 0) month = 12;
2356    if (day == 0) day = last_day(year, month);
2357    days2 = days_since_1900(year, month, day);
2358
2359    if (!implicit_range && days2 > current_days_since_1900) {
2360        set_pos(&fp2);
2361        compile_diagnostic(DIAG_WARN|DIAG_DATE, /*Date is in the future!*/80);
2362    }
2363
2364    if (days2 < days1) {
2365        set_pos(&fp);
2366        compile_diagnostic(DIAG_ERR|DIAG_TOKEN, /*End of date range is before the start*/81);
2367        int tmp = days1;
2368        days1 = days2;
2369        days2 = tmp;
2370    }
2371
2372read:
2373    if (!pcs->meta || pcs->meta->days1 != days1 || pcs->meta->days2 != days2) {
2374        copy_on_write_meta(pcs);
2375        pcs->meta->days1 = days1;
2376        pcs->meta->days2 = days2;
2377        /* Invalidate cached declination. */
2378        pcs->declination = HUGE_REAL;
2379    }
2380}
2381
2382typedef void (*cmd_fn)(void);
2383
2384static const cmd_fn cmd_funcs[] = {
2385   cmd_alias,
2386   cmd_begin,
2387   cmd_calibrate,
2388   cmd_case,
2389   skipline, /*cmd_copyright,*/
2390   cmd_cs,
2391   cmd_data,
2392   cmd_date,
2393   cmd_declination,
2394#ifndef NO_DEPRECATED
2395   cmd_default,
2396#endif
2397   cmd_end,
2398   cmd_entrance,
2399   cmd_equate,
2400   cmd_export,
2401   cmd_fix,
2402   cmd_flags,
2403   cmd_include,
2404   cmd_infer,
2405   skipline, /*cmd_instrument,*/
2406#ifndef NO_DEPRECATED
2407   cmd_prefix,
2408#endif
2409   cmd_ref,
2410   cmd_require,
2411   cmd_sd,
2412   cmd_set,
2413   solve_network,
2414   skipline, /*cmd_team,*/
2415   cmd_title,
2416   cmd_truncate,
2417   cmd_units
2418};
2419
2420extern void
2421handle_command(void)
2422{
2423   int cmdtok;
2424   get_token();
2425   cmdtok = match_tok(cmd_tab, TABSIZE(cmd_tab));
2426
2427   if (cmdtok < 0 || cmdtok >= (int)(sizeof(cmd_funcs) / sizeof(cmd_fn))) {
2428      compile_diagnostic(DIAG_ERR|DIAG_BUF|DIAG_SKIP, /*Unknown command “%s”*/12, buffer);
2429      return;
2430   }
2431
2432   switch (cmdtok) {
2433    case CMD_EXPORT:
2434      if (!f_export_ok)
2435         /* TRANSLATORS: The *EXPORT command is only valid just after *BEGIN
2436          * <SURVEY>, so this would generate this error:
2437          *
2438          * *begin fred
2439          * 1 2 1.23 045 -6
2440          * *export 2
2441          * *end fred */
2442         compile_diagnostic(DIAG_ERR, /**EXPORT must immediately follow “*BEGIN <SURVEY>”*/57);
2443      break;
2444    case CMD_ALIAS:
2445    case CMD_CALIBRATE:
2446    case CMD_CASE:
2447    case CMD_COPYRIGHT:
2448    case CMD_CS:
2449    case CMD_DATA:
2450    case CMD_DATE:
2451    case CMD_DECLINATION:
2452    case CMD_DEFAULT:
2453    case CMD_FLAGS:
2454    case CMD_INFER:
2455    case CMD_INSTRUMENT:
2456    case CMD_REF:
2457    case CMD_REQUIRE:
2458    case CMD_SD:
2459    case CMD_SET:
2460    case CMD_TEAM:
2461    case CMD_TITLE:
2462    case CMD_TRUNCATE:
2463    case CMD_UNITS:
2464      /* These can occur between *begin and *export */
2465      break;
2466    default:
2467      /* NB: additional handling for "*begin <survey>" in cmd_begin */
2468      f_export_ok = fFalse;
2469      break;
2470   }
2471
2472   cmd_funcs[cmdtok]();
2473}
Note: See TracBrowser for help on using the repository browser.