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
RevLine 
[aadc86e]1/* > cmdline.c
2 * Wrapper for GNU getopt which deals with standard options
[bd1913f]3 * Copyright (C) 1998-2001 Olly Betts
[846746e]4 *
[89231c4]5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
[846746e]9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
[89231c4]12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
[846746e]14 *
[89231c4]15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
[aadc86e]18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <ctype.h>
[3b9ec3f]25#include <errno.h>
[dd6f74e]26#include <float.h>
[aadc86e]27#include <stdio.h>
[e05bdb8]28#include <string.h>
[f2122f0]29#include <limits.h>
[aadc86e]30
31#include "cmdline.h"
[e05bdb8]32#include "filename.h"
[aadc86e]33
[d227ee5]34#include "message.h"
35
[aadc86e]36/* It might be useful to be able to disable all long options on small
[3b9ec3f]37 * platforms like pre-386 DOS and perhaps some PDAs...
[aadc86e]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>
[cb3d1e2]46 *
[e05bdb8]47 * <short syntax>
[cb3d1e2]48 *
[aadc86e]49 * --help gives:
50 * <version>
[cb3d1e2]51 *
[aadc86e]52 * <short syntax>
53 *
54 * <table>
[cb3d1e2]55 *
[aadc86e]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
[3b9ec3f]65 */
[aadc86e]66
67static const char newline_tabs[] = "\n\t\t\t\t";
68
[3b9ec3f]69static int argc;
[647407d]70static char * const *argv;
[3b9ec3f]71static const char *shortopts;
72static const struct option *longopts;
73static int *longind;
74static const struct help_msg *help;
75static int min_args, max_args;
[acc20b1]76static const char *args_msg = NULL, *extra_msg = NULL;
[3b9ec3f]77
78static const char *argv0 = NULL;
[aadc86e]79
[3b9ec3f]80void
81cmdline_help(void)
[aadc86e]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)) {
[3b9ec3f]89         o = longopts + HLP_DECODELONG(opt);
[aadc86e]90         longopt = o->name;
91         opt = o->val;
92      }
93
94      if (isalnum(opt))
[5fbfd8d]95         printf("  -%c%c", opt, longopt ? ',' : ' ');
[aadc86e]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   }
[a580dc1]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));
[acc20b1]129
130   if (extra_msg) {
131      putnl();
132      puts(extra_msg);
133   }
[647407d]134
[aadc86e]135   exit(0);
136}
137
[3b9ec3f]138void
139cmdline_version(void)
[aadc86e]140{
141   printf("%s - "PACKAGE" "VERSION"\n", argv0);
142}
143
[3b9ec3f]144void
145cmdline_syntax(void)
[cb3d1e2]146{
[6dc9a36]147   printf("\n%s: %s", msg(/*Syntax*/49), argv0);
[a580dc1]148   if (help->opt) printf(" [%s]...", msg(/*OPTION*/153));
[acc20b1]149   if (args_msg) {
150      putchar(' ');
151      puts(args_msg);
152      return;
153   }
[3b9ec3f]154   if (min_args) {
155      int i = min_args;
[a580dc1]156      while (i--) printf(" %s", msg(/*FILE*/124));
[3b9ec3f]157   }
[647407d]158   if (max_args == -1) {
[a580dc1]159      if (!min_args) printf(" [%s]", msg(/*FILE*/124));
[647407d]160      fputs("...", stdout);
161   } else if (max_args > min_args) {
162      int i = max_args - min_args;
[a580dc1]163      while (i--) printf(" [%s]", msg(/*FILE*/124));
[647407d]164   }
[3b9ec3f]165   putnl();
166}
167
[acc20b1]168void
169cmdline_set_syntax_message(const char *args, const char *extra)
170{
171   args_msg = args;
172   extra_msg = extra;
173}
174
[3b9ec3f]175int
176cmdline_int_arg(void)
[aadc86e]177{
[f2122f0]178   long result;
[3b9ec3f]179   char *endptr;
[cb3d1e2]180
[3b9ec3f]181   errno = 0;
182
183   result = strtol(optarg, &endptr, 10);
184
[f2122f0]185   if (errno == ERANGE || result > INT_MAX || result < INT_MIN) {
[a580dc1]186      fprintf(stderr, "%s: ", argv0);
187      fprintf(stderr, msg(/*numeric argument `%s' out of range*/185), optarg);
188      fputnl(stderr);
[3b9ec3f]189      cmdline_syntax();
[acc20b1]190      exit(1);
[3b9ec3f]191   } else if (*optarg == '\0' || *endptr != '\0') {
[a580dc1]192      fprintf(stderr, "%s: ", argv0);
193      fprintf(stderr, msg(/*argument `%s' not an integer*/186), optarg);
194      fputnl(stderr);
[3b9ec3f]195      cmdline_syntax();
[acc20b1]196      exit(1);
[3b9ec3f]197   }
[cb3d1e2]198
[f2122f0]199   return (int)result;
[aadc86e]200}
201
[acc20b1]202double
203cmdline_double_arg(void)
[aadc86e]204{
[acc20b1]205   double result;
[3b9ec3f]206   char *endptr;
[cb3d1e2]207
[3b9ec3f]208   errno = 0;
209
210   result = strtod(optarg, &endptr);
211
212   if (errno == ERANGE) {
[a580dc1]213      fprintf(stderr, "%s: ", argv0);
214      fprintf(stderr, msg(/*numeric argument `%s' out of range*/185), optarg);
215      fputnl(stderr);
[3b9ec3f]216      cmdline_syntax();
[acc20b1]217      exit(1);
[3b9ec3f]218   } else if (*optarg == '\0' || *endptr != '\0') {
[a580dc1]219      fprintf(stderr, "%s: ", argv0);
220      fprintf(stderr, msg(/*argument `%s' not a number*/187), optarg);
221      fputnl(stderr);
[3b9ec3f]222      cmdline_syntax();
[acc20b1]223      exit(1);
[3b9ec3f]224   }
[cb3d1e2]225
[3b9ec3f]226   return result;
227}
228
[acc20b1]229float
230cmdline_float_arg(void)
[cb3d1e2]231{
[acc20b1]232   double result = cmdline_double_arg();
233   if (fabs(result) > FLT_MAX) {
[a580dc1]234      fprintf(stderr, "%s: ", argv0);
235      fprintf(stderr, msg(/*numeric argument `%s' out of range*/185), optarg);
236      fputnl(stderr);
[acc20b1]237      cmdline_syntax();
238      exit(1);
239   }
240   return (float)result;
241}
242
[3b9ec3f]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{
[647407d]249   if (!argv0) argv0 = argv_[0];
[3b9ec3f]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);
[aadc86e]265
266   if (opt == EOF) {
[a580dc1]267      /* check valid # of args given - if not give syntax message */
[3b9ec3f]268      if (argc - optind < min_args) {
[a580dc1]269         fprintf(stderr, "%s: %s\n", argv0, msg(/*too few arguments*/122));
[3b9ec3f]270         opt = '?';
271      } else if (max_args >= 0 && argc - optind > max_args) {
[a580dc1]272         fprintf(stderr, "%s: %s\n", argv0, msg(/*too many arguments*/123));
[3b9ec3f]273         opt = '?';
274      }
[aadc86e]275   }
276
277   switch (opt) {
278    case ':': /* parameter missing */
279    case '?': /* unknown opt, ambiguous match, or extraneous param */
[a580dc1]280      /* getopt displays a message for us */
[3b9ec3f]281      cmdline_syntax();
[a580dc1]282      fprintf(stderr, msg(/*Try `%s --help' for more information.\n*/157),
283              argv0);
[acc20b1]284      exit(1);
[aadc86e]285    case HLP_VERSION: /* --version */
[3b9ec3f]286      cmdline_version();
[aadc86e]287      exit(0);
288    case HLP_HELP: /* --help */
[3b9ec3f]289      cmdline_version();
290      cmdline_syntax();
[aadc86e]291      putchar('\n');
[3b9ec3f]292      cmdline_help();
[aadc86e]293      exit(0);
294   }
295   return opt;
296}
Note: See TracBrowser for help on using the repository browser.