source: git/src/cmdline.c @ 350b2a6

RELEASE/1.0RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 350b2a6 was 647407d, checked in by Olly Betts <olly@…>, 25 years ago

Lots of fixes from CUCC 2000 Expo:

  • cavern: *fix with error values (one for same all round, two for horizontal and vertical, three for x, y, z)
  • Fixed RISC OS not treating "foo." as a directory
  • Improved behaviour when sgmltools not installed
  • cavern: Merged patches for generating .3dx files for chasm
  • Miscellaneous code tidying
  • Moved unresolvable issues from BUGS to ZOMBIES
  • cavern: Fixed articulation code bug (handling of some cases of components with multiple fixed points); added regression test
  • cavern: "*begin" / "*end foo" now gives more explicit error
  • cavern: More work on gross error detection
  • xcaverot: you can now set environmental variables XCAVEROT_FONTNAME, XCAVEROT_INDICATOR_RADIUS
  • extend: now starts from highest station with only one leg. If no such station exists then revert to the previous behaviour of starting from the highest station (but complain if we have no legs at all).
  • cavern: line numbers now correct for .svx files with Mac style lineends
  • cavern: improved error reporting in several cases
  • caverot: on RISC OS now fall back to using 2 or even just one screen bank if we can't find a mode where we can create 3 banks [FIXME: need to test this works]
  • cavern: added data styles "TOPOFIL" (like tape/compass/clino but with a counter instead of the tape), "CARTESIAN" (dx, dy, dz), and "NOSURVEY" (for unsurveyed connections between surveys)
  • cavern: can now suppress "unused fixed point" error on a station by station basis by using: "*fix <station> reference <coords>"
  • cavern: new command "*require <version>" to allow survey data to specify the minimum survex version required to process it
  • print*: now search multiple print.ini files so you can override settings in the shipped file rather than having to modify it. Search order is: ~/.survex/print.ini (unix only), $SURVEXHOME/myprint.ini, $SURVEXHOME/print.ini
  • Changed xcaverot rotation/zoom to be the same way round as caverot
  • diffpos, extend, xcaverot, caverot: Now use cmdline library to parse command line arguments and so support --help, --version, etc.
  • hto2svx, svx2hto, and survex wrapper now all support --help and --version
  • `configure --disable-aven' now disables building of aven
  • caverot: on RISC OS fall back to using 2 or just 1 screen bank if there's not enough video memory allocated for 3.
  • Documentation brought more up to date.
  • If a station is only referenced once, and that reference is in a *equate and with an explicit prefix (e.g. "*equate 1 possibletypo.6"), a warning is issued.
  • configure now autoprobes compiler for 16 and 32 bit types so osdepend.h no longer needs to list them for each platform.
  • Removed pointless check for floating point support - it was only implemented for RISC OS and without FP the program falls over during initialisation and never gets to the check.
  • Rearranged caverot sources to make use of cvrotimg from other programs cleaner.
  • Minor enhancements to cmdline library.
  • Strings read by get_token now reported with original case in error messages.
  • Handle LANG=C (Mandrake Linux) and LANG=en_US (RedHat? 6.1).

git-svn-id: file:///home/survex-svn/survex/trunk@460 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/* > cmdline.c
2 * Wrapper for GNU getopt which deals with standard options
3 * Copyright (C) 1998-2000 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <ctype.h>
25#include <errno.h>
26#include <stdio.h>
27#include <string.h>
28
29#include "cmdline.h"
30#include "filename.h"
31
32/* It might be useful to be able to disable all long options on small
33 * platforms like pre-386 DOS and perhaps some PDAs...
34 */
35#if 0
36# define getopt_long(ARGC, ARGV, STR, OPTS, PTR) getopt(ARGC, ARGV, STR)
37#endif
38
39/*
40 * bad command line give:
41 * <problem>
42 *
43 * <short syntax>
44 *
45 * --help gives:
46 * <version>
47 *
48 * <short syntax>
49 *
50 * <table>
51 *
52 * <blurb>
53 *
54 * --version gives:
55 * <version>
56 */
57
58/*
59 * want to cope with optional/required parameters on long options
60 * and also parameters on short options
61 */
62
63static const char newline_tabs[] = "\n\t\t\t\t";
64
65static int argc;
66static char * const *argv;
67static const char *shortopts;
68static const struct option *longopts;
69static int *longind;
70static const struct help_msg *help;
71static int min_args, max_args;
72static const char *args_msg = NULL, *extra_msg = NULL;
73
74static const char *argv0 = NULL;
75
76void
77cmdline_help(void)
78{
79   while (help->opt) {
80      const char *longopt = 0;
81      int opt = help->opt;
82      const struct option *o = 0;
83
84      if (HLP_ISLONG(opt)) {
85         o = longopts + HLP_DECODELONG(opt);
86         longopt = o->name;
87         opt = o->val;
88      }
89
90      if (isalnum(opt))
91         printf("  -%c,", opt);
92      else
93         fputs("     ", stdout);
94
95      if (longopt) {
96         int len = strlen(longopt);
97         printf(" --%s", longopt);
98         if (o && o->has_arg) {
99            const char *p;
100            len += len + 1;
101
102            if (o->has_arg == optional_argument) {
103               putchar('[');
104               len += 2;
105            }
106
107            putchar('=');
108
109            for (p = longopt; *p ; p++) putchar(toupper(*p));
110
111            if (o->has_arg == optional_argument) putchar(']');
112         }
113         len = (len >> 3) + 2;
114         if (len > 4) len = 0;
115         fputs(newline_tabs + len, stdout);
116      } else {
117         fputs(newline_tabs + 1, stdout);
118      }
119
120      puts(help->msg);
121      help++;
122   }
123   /* TRANSLATE */
124   puts("      --help\t\t\tdisplay this help and exit\n"
125        "      --version\t\t\toutput version information and exit");
126
127   if (extra_msg) {
128      putnl();
129      puts(extra_msg);
130   }
131
132   exit(0);
133}
134
135void
136cmdline_version(void)
137{
138   printf("%s - "PACKAGE" "VERSION"\n", argv0);
139}
140
141void
142cmdline_syntax(void)
143
144   printf("\nSyntax: %s", argv0);
145   if (help->opt) fputs(" [OPTION]...", stdout);
146   if (args_msg) {
147      putchar(' ');
148      puts(args_msg);
149      return;
150   }
151   if (min_args) {
152      int i = min_args;
153      while (i--) fputs(" FILE", stdout);
154   }
155   if (max_args == -1) {
156      if (!min_args) fputs(" [FILE]", stdout);
157      fputs("...", stdout);
158   } else if (max_args > min_args) {
159      int i = max_args - min_args;
160      while (i--) fputs(" [FILE]", stdout);
161   }
162   putnl();
163}
164
165void
166cmdline_set_syntax_message(const char *args, const char *extra)
167{
168   args_msg = args;
169   extra_msg = extra;
170}
171
172int
173cmdline_int_arg(void)
174{
175   int result;
176   char *endptr;
177   
178   errno = 0;
179
180   result = strtol(optarg, &endptr, 10);
181
182   if (errno == ERANGE) {
183      fprintf(stderr, "%s: numeric argument `%s' out of range\n", argv0, optarg);
184      cmdline_syntax();
185      exit(1);
186   } else if (*optarg == '\0' || *endptr != '\0') {
187      fprintf(stderr, "%s: argument `%s' not an integer\n", argv0, optarg);
188      cmdline_syntax();
189      exit(1);
190   }
191   
192   return result;
193}
194
195double
196cmdline_double_arg(void)
197{
198   double result;
199   char *endptr;
200   
201   errno = 0;
202
203   result = strtod(optarg, &endptr);
204
205   if (errno == ERANGE) {
206      fprintf(stderr, "%s: numeric argument `%s' out of range\n", argv0, optarg);
207      cmdline_syntax();
208      exit(1);
209   } else if (*optarg == '\0' || *endptr != '\0') {
210      fprintf(stderr, "%s: argument `%s' not a number\n", argv0, optarg);
211      cmdline_syntax();
212      exit(1);
213   }
214   
215   return result;
216}
217
218float
219cmdline_float_arg(void)
220{   
221   double result = cmdline_double_arg();
222   if (fabs(result) > FLT_MAX) {
223      fprintf(stderr, "%s: numeric argument `%s' out of range\n", argv0, optarg);
224      cmdline_syntax();
225      exit(1);
226   }
227   return (float)result;
228}
229
230void
231cmdline_init(int argc_, char *const *argv_, const char *shortopts_,
232             const struct option *longopts_, int *longind_,
233             const struct help_msg *help_,
234             int min_args_, int max_args_)
235{
236   if (!argv0) argv0 = argv_[0];
237
238   argc = argc_;
239   argv = argv_;
240   shortopts = shortopts_;
241   longopts = longopts_;
242   longind = longind_;
243   help = help_;
244   min_args = min_args_;
245   max_args = max_args_;
246}
247
248int
249cmdline_getopt(void)
250{
251   int opt = getopt_long(argc, argv, shortopts, longopts, longind);
252
253   if (opt == EOF) {
254      /* check minimum # of args given - if not give syntax message */
255      if (argc - optind < min_args) {
256         /* TRANSLATE */
257         fprintf(stderr, "%s: too few arguments\n", argv0);
258         opt = '?';
259      } else if (max_args >= 0 && argc - optind > max_args) {
260         /* TRANSLATE */
261         fprintf(stderr, "%s: too many arguments\n", argv0);
262         opt = '?';
263      }
264   }
265
266   switch (opt) {
267    case ':': /* parameter missing */
268    case '?': /* unknown opt, ambiguous match, or extraneous param */
269      /* getopt displays a message for us (unless we set opterr to 0) */
270      /* FIXME: set opterr to 0 so we can translate messages? */
271      cmdline_syntax();
272      /* TRANSLATE */
273      fprintf(stderr, "Try `%s --help' for more information.\n", argv0);
274      exit(1);
275    case HLP_VERSION: /* --version */
276      cmdline_version();
277      exit(0);
278    case HLP_HELP: /* --help */
279      cmdline_version();
280      cmdline_syntax();
281      putchar('\n');
282      cmdline_help();
283      exit(0);
284   }
285   return opt;
286}
Note: See TracBrowser for help on using the repository browser.