source: git/src/cmdline.c @ d8dbdff

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

lib/codes.po,src/: cmdline_set_syntax_message() now takes message
numbers, plus an optional string argument.

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

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