source: git/src/cmdline.c @ 0f92548

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 0f92548 was aadc86e, checked in by Olly Betts <olly@…>, 26 years ago

Initial revision

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

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/* > cmdline.c
2 * Wrapper for GNU getopt which deals with standard options
3 * Copyright (C) 1998 Olly Betts
4 */
5
6#ifdef HAVE_CONFIG_H
7#include <config.h>
8#endif
9
10#include <ctype.h>
11#include <stdio.h>
12
13#include "cmdline.h"
14
15/* It might be useful to be able to disable all long options on small
16 * platforms like pre-386 DOS and some PDAs...
17 */
18#if 0
19# define getopt_long(ARGC, ARGV, STR, OPTS, PTR) getopt(ARGC, ARGV, STR)
20#endif
21
22/*
23 * bad command line give:
24 * <problem>
25 *
26 * <short synatx>
27 *
28 * --help gives:
29 * <version>
30 *
31 * <short syntax>
32 *
33 * <table>
34 *
35 * <blurb>
36 *
37 * --version gives:
38 * <version>
39 */
40
41/*
42 * want to cope with optional/required parameters on long options
43 * and also parameters on short options
44*/
45
46static const char newline_tabs[] = "\n\t\t\t\t";
47
48static const char *argv0 = "<program>";
49
50static void
51display_help(const struct help_msg *help, const struct option *opts)
52{
53   while (help->opt) {
54      const char *longopt = 0;
55      int opt = help->opt;
56      const struct option *o = 0;
57
58      if (HLP_ISLONG(opt)) {
59         o = opts + HLP_DECODELONG(opt);
60         longopt = o->name;
61         opt = o->val;
62      }
63
64      if (isalnum(opt))
65         printf("  -%c,", opt);
66      else
67         fputs("     ", stdout);
68
69      if (longopt) {
70         int len = strlen(longopt);
71         printf(" --%s", longopt);
72         if (o && o->has_arg) {
73            const char *p;
74            len += len + 1;
75
76            if (o->has_arg == optional_argument) {
77               putchar('[');
78               len += 2;
79            }
80
81            putchar('=');
82
83            for (p = longopt; *p ; p++) putchar(toupper(*p));
84
85            if (o->has_arg == optional_argument) putchar(']');
86         }
87         len = (len >> 3) + 2;
88         if (len > 4) len = 0;
89         fputs(newline_tabs + len, stdout);
90      } else {
91         fputs(newline_tabs + 1, stdout);
92      }
93
94      puts(help->msg);
95      help++;
96   }
97   /* FIXME TRANSLATE */
98   puts("      --help\t\t\tdisplay this help and exit\n"
99        "      --version\t\t\toutput version information and exit");
100 
101   exit(0);
102}
103
104static void
105display_version(void)
106{
107   printf("%s - "PACKAGE" "VERSION"\n", argv0);
108}
109
110static void
111display_syntax(void)
112{
113   printf("\nSyntax: %s [OPTION]... [FILE]...\n", argv0);
114}
115
116extern int
117my_getopt_long(int argc, char *const *argv, const char *shortopts,
118               const struct option *longopts, int *longind,
119               const struct help_msg *help, int min_args)
120{
121   int opt;
122   
123   argv0 = argv[0]; /* FIXME tidy up argv0 (remove path and extension) */
124   
125   opt = getopt_long(argc, argv, shortopts, longopts, longind);
126
127   if (opt == EOF) {
128      /* check minimum # of args given - if not give help message */
129      if (argc - optind < min_args) opt = HLP_HELP;
130   }
131
132   switch (opt) {
133    case ':': /* parameter missing */
134    case '?': /* unknown opt, ambiguous match, or extraneous param */
135      /* getopt displays a message for us (unless we set opterr to 0) */
136      display_syntax();
137      exit(0);
138    case HLP_VERSION: /* --version */
139      display_version();
140      exit(0);
141    case HLP_HELP: /* --help */
142      display_version();
143      display_syntax();
144      putchar('\n');
145      display_help(help, longopts);
146      exit(0);
147   }
148   return opt;
149}
Note: See TracBrowser for help on using the repository browser.