source: git/src/cmdline.c @ fc6e101

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 fc6e101 was a580dc1, checked in by Olly Betts <olly@…>, 24 years ago

Extract messages from cmdline.c and getopt.c for translation.

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

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