source: git/src/cmdline.c @ d8dbdff

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
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
Line 
1/* cmdline.c
2 * Wrapper for GNU getopt which deals with standard options
3 * Copyright (C) 1998-2001,2003,2004,2011 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 <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 "getopt.h"
32
33#include "cmdline.h"
34#include "debug.h"
35#include "filename.h"
36
37#include "message.h"
38
39/* It might be useful to be able to disable all long options on small
40 * platforms like older PDAs.
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>
49 *
50 * <short syntax>
51 *
52 * --help gives:
53 * <version>
54 *
55 * <short syntax>
56 *
57 * <table>
58 *
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
68 */
69
70static const char newline_tabs[] = "\n\t\t\t\t";
71
72static int argc;
73static char * const *argv;
74static const char *shortopts;
75static const struct option *longopts;
76static int *longind;
77static const struct help_msg *help;
78static int min_args, max_args;
79static int msg_args, msg_extra;
80static const char * msg_extra_arg;
81
82void
83cmdline_help(void)
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)) {
91         o = longopts + HLP_DECODELONG(opt);
92         longopt = o->name;
93         opt = o->val;
94      }
95
96      if (isalnum((unsigned char)opt))
97         printf("  -%c%c", opt, longopt ? ',' : ' ');
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
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      }
134      help++;
135   }
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));
140
141   if (msg_extra) {
142      putnl();
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      }
151   }
152
153   exit(0);
154}
155
156void
157cmdline_version(void)
158{
159   printf("%s - "PRETTYPACKAGE" "VERSION"\n", msg_appname());
160}
161
162void
163cmdline_syntax(void)
164{
165   printf("\n%s: %s", msg(/*Syntax*/49), msg_appname());
166   if (help->opt) printf(" [%s]...", msg(/*OPTION*/153));
167   if (msg_args) {
168      putchar(' ');
169      puts(msg(msg_args));
170      return;
171   }
172   if (min_args) {
173      int i = min_args;
174      while (i--) printf(" %s", msg(/*FILE*/124));
175   }
176   if (max_args == -1) {
177      if (!min_args) printf(" [%s]", msg(/*FILE*/124));
178      fputs("...", stdout);
179   } else if (max_args > min_args) {
180      int i = max_args - min_args;
181      while (i--) printf(" [%s]", msg(/*FILE*/124));
182   }
183   putnl();
184}
185
186static void
187syntax_and_help_pointer(void)
188{
189   cmdline_syntax();
190   fprintf(stderr, msg(/*Try `%s --help' for more information.&#10;*/157),
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
219void
220cmdline_set_syntax_message(int msg_args_, int msg_extra_, const char * arg)
221{
222   msg_args = msg_args_;
223   msg_extra = msg_extra_;
224   msg_extra_arg = arg;
225}
226
227int
228cmdline_int_arg(void)
229{
230   long result;
231   char *endptr;
232
233   errno = 0;
234
235   result = strtol(optarg, &endptr, 10);
236
237   if (errno == ERANGE || result > INT_MAX || result < INT_MIN) {
238      moan_and_die(/*numeric argument `%s' out of range*/185);
239   } else if (*optarg == '\0' || *endptr != '\0') {
240      moan_and_die(/*argument `%s' not an integer*/186);
241   }
242
243   return (int)result;
244}
245
246double
247cmdline_double_arg(void)
248{
249   double result;
250   char *endptr;
251
252   errno = 0;
253
254   result = strtod(optarg, &endptr);
255
256   if (errno == ERANGE) {
257      moan_and_die(/*numeric argument `%s' out of range*/185);
258   } else if (*optarg == '\0' || *endptr != '\0') {
259      moan_and_die(/*argument `%s' not a number*/187);
260   }
261
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);
285
286   switch (opt) {
287    case EOF:
288      /* check valid # of args given - if not give syntax message */
289      if (argc - optind < min_args) {
290         cmdline_too_few_args();
291      } else if (max_args >= 0 && argc - optind > max_args) {
292         cmdline_too_many_args();
293      }
294      break;
295    case ':': /* parameter missing */
296    case '?': /* unknown opt, ambiguous match, or extraneous param */
297      /* getopt displays a message for us */
298      syntax_and_help_pointer();
299      break;
300    case HLP_VERSION: /* --version */
301      cmdline_version();
302      exit(0);
303    case HLP_HELP: /* --help */
304      cmdline_version();
305      cmdline_syntax();
306      putchar('\n');
307      cmdline_help();
308      exit(0);
309   }
310   return opt;
311}
Note: See TracBrowser for help on using the repository browser.