summaryrefslogtreecommitdiff
path: root/autoopts/tpl/getopt.tpl
blob: 374c8b6be52fdc35d59552bd678c64058f8e43fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
[+ AutoGen5 Template  -*- Mode: C -*-

   h=%s-temp.h
   c=%s-temp.c  +][+

`stamp=\`sed 's,.*stamp: *",,;s,".*,,' <<\_EOF_
  Time-stamp:        "2012-04-29 08:54:45 bkorb"
_EOF_
\` `            +][+

;;  This file is part of AutoOpts, a companion to AutoGen.
;;  AutoOpts is free software.
;;  AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
;;
;;  AutoOpts is available under any one of two licenses.  The license
;;  in use must be one of these two and the choice is under the control
;;  of the user of the license.
;;
;;   The GNU Lesser General Public License, version 3 or later
;;      See the files "COPYING.lgplv3" and "COPYING.gplv3"
;;
;;   The Modified Berkeley Software Distribution License
;;      See the file "COPYING.mbsd"
;;
;;  These files have the following md5sums:
;;
;;  43b91e8ca915626ed3818ffb1b71248b COPYING.gplv3
;;  06a1a2e4760c90ea5e1dad8dfaac4d39 COPYING.lgplv3
;;  66a5cedaf62c4b2637025f049f9b826f COPYING.mbsd

  (if (not (exist? "settable"))
      (error "'settable' must be specified globally for getopt_long\n"))
  (if (not (exist? "no-libopts"))
      (error "'no-libopts' must be specified globally to use getopt\n"))
  (define prog-name (string->c-name! (get "prog-name")))
  (define PROG-NAME (string-upcase prog-name))
  (out-move (string-append "getopt-" prog-name "." (suffix)))
  (dne " *  " "/* " ) +]
 *[+

  IF (exist? "copyright") +]
 *
[+
    CASE copyright.type   +][+
    == ""   +] *  licensing type not specified.[+
    = note  +][+ (prefix " *  "  (get "copyright.text")) +][+
    *       +][+
      (license-description (get "copyright.type") prog-name " *  "
          (get "copyright.owner"))     +][+
    ESAC    +][+
  ENDIF     +]
 *
 *  Last template edit: [+ `echo $stamp` +]
 */[+
CASE (suffix) +][+
== h          +][+
 (define header-file (out-name))
 (out-push-new) \+]
{
    test -x "${AGexe}" || die "AGexe not properly set:  ${AGexe}"
[+ # START-BUILDTREE-ISMS:

#  The following code is sedded away in install-hook.sh.
#  The goal is to ensure we use build tree templates in testing mode, and
#  remove them when installing this file.

\+]
    aobdir=`echo ${AGexe} | sed 's@/[^/]*$@@'`

    if ! test -x ${aobdir}/autoopts-config
    then
        # Check for autoopts-config in build directory layout
        #
        aobdir=`echo ${aobdir} | sed 's@/[^/]*$@@'`/autoopts
        test -x ${aobdir}/autoopts-config || {
            aobdir=`echo ${aobdir} | sed 's@/[^/]*/autoopts$@@'`/autoopts
            test -x ${aobdir}/autoopts-config || \
                die 'cannot locate autoopts-config'
        }
    fi

    agopts=`dirname [+ (tpl-file #t) +]`
    agopts="-L${aobdir}/tpl -L$agopts "
    tarfile=`set -- ${aobdir}/libopts*.tar.*
        test -f $1 || {
            cd ${aobdir}
            ${MAKE:-make} libsrc
            cd -
            set -- ${aobdir}/libopts*.tar.*
            test -f $1 || die 'libopts tarball not built'
        } >&2
        echo $1`[+

# END-BUILDTREE-ISMS the following code is for installed version:
    agopts=
    aocfg=`echo ${AGexe} | sed 's@/[^/]*$@@'`/autoopts-config
    tarfile=`${aocfg} libsrc`

# END-INSTALL-ONLY-CODE +]
    if test -n "${AG_Tracing}"
    then
        AG_Dep_File=`dirname "${AG_Tracing}"`/ao-[+ (base-name) +].dep
        agopts="${agopts}-MF${AG_Dep_File} -MT${AG_Dep_File%.dep}.targ"
    fi

    ${AGexe} -b[+ (base-name) +] ${agopts} -Toptions.tpl [+ (def-file) +]
    def_hdr=[+ (base-name) +].h
    sed 's@<autoopts/options.h>@"[+ (. header-file)
        +]"@' $def_hdr > XXX-$$
    mv -f XXX-$$ $def_hdr
    hdrfile=`gunzip -c $tarfile | tar tf - | fgrep /autoopts/options.h`
    gunzip -c $tarfile | tar xf - $hdrfile
    exec 3< $hdrfile
    untardir=`echo $hdrfile | sed 's@/.*@@'`
} >&2

while :
do
    IFS= read -r -u3 line || die "no header guard in $hdrfile"
    case "$line" in
    *AUTOOPTS_OPTIONS_H_GUARD ) break ;;
    esac
done
echo

echo "$line"
IFS= read -r -u3 line || die "short $hdrfile"
case "$line" in
'#define AUTOOPTS_OPTIONS_H_GUARD'* ) : ;;
*) die "invalid header guard in $hdrfile" ;;
esac
echo "$line"
echo '#include "[+
(if (exist? "config-header")
    (get "config-header")
    (error "getopt template requires a \"config-header\" attribute")
)   +]"'

while :
do
    IFS= read -r -u3 line || die "no CPLUSPLUS_CLOSER in $hdrfile"
    case "$line" in
    *'Versions where in various fields first appear'* ) break ;;
    esac
    echo "$line"
done

cat <<- _EOF_
	 * option loop function
	 */
	#ifdef  __cplusplus
	#define CPLUSPLUS_OPENER extern "C" {
	CPLUSPLUS_OPENER
	#define CPLUSPLUS_CLOSER }
	#else
	#define CPLUSPLUS_CLOSER
	#endif

	extern int process_[+(. prog-name)+]_opts(int argc, char** argv);
	extern void optionPrintVersion(tOptions* pOptions, tOptDesc* pOptDesc);

	CPLUSPLUS_CLOSER
	_EOF_

sed '1,/^CPLUSPLUS_CLOSER/d' <&3
exec 3<&-
rm -rf $untardir
[+ (shell (out-pop #t)) +]
[+ == c +]
#include "[+ (. header-file) +]"

#include <sys/types.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <[+ (if (exist? "long-opts") "getopt" "unistd") +].h>
#include "[+ (base-name) +].h"

#ifndef DIRCH
#  if defined(_WIN32) && !defined(__CYGWIN__)
#    define DIRCH                  '\\'
#  else
#    define DIRCH                  '/'
#  endif
#endif[+

IF (exist? "long-opts") +]

/*
 *  getopt_long option descriptor
 */
static struct option a_long_opts[] = {[+

  FOR flag            +][+
    (sprintf

       "\n  { %-20s %d, NULL, VALUE_OPT_%s },"
          (string-append (c-string (get "name")) ",")
          (if (exist? "arg-type") 1 0)
          (string-upcase (string->c-name! (get "name")))
    ) +][+

  ENDFOR flag

+]
  { "help",              0, NULL, VALUE_OPT_HELP },[+
IF (exist? "version") +]
  { "version",           0, NULL, VALUE_OPT_VERSION },[+
ENDIF +]
  { NULL,                0, NULL, 0 }
};
[+ ENDIF +]
/*
 *  Option flag character list
 */
static char z_opts[] = "[+ # close quote for emacs " +][+
    FOR flag            +][+
      CASE value        +][+
      ~ [!-~]           +][+ value +][+

        CASE arg-type   +][+
        =* str          +]:[+
        == ""           +][+
        *               +][+ (error (sprintf
        "error in %s opt: The only allowed arg type is 'string'\n"
        (get "name") )) +][+
        ESAC            +][+

      ESAC              +][+

    ENDFOR flag         +][+

    IF  (not (exist? "help-value")) +]?[+
    ELSE                +][+
      CASE help-value   +][+
      == ""             +][+
      == '"'            +]\"[+
      *                 +][+ help-value  +][+
      ESAC              +][+
    ENDIF               +][+

    IF  (exist? "version")  +][+
      IF  (not (exist? "version-value")) +]v[+
      ELSE              +][+
        CASE version-value +][+
        == ""           +][+
        == '"'          +]\"[+
        *               +][+ version-value +][+
        ESAC            +][+
      ENDIF             +][+
    ENDIF               +][+

    (define help-opt
      (if (exist? "long-opts")        "--help"
      (if (not (exist? "flag.value")) "help"
      (if (exist? "help-value")       (string-append "-" (get "help-value"))
                                      "-?" ))) )
    ;; open quote for emacs " +]";

/*
 *  AutoOpts library replacement routines:
 */
void
optionUsage (tOptions* pOptions, int status)
{
  if (status != 0)
    fprintf (stderr, _("Try `%s [+(. help-opt)+]' for more information.\n"),
             [+ (. prog-name) +]Options.pzProgName);
  else
    {
      fputs (_([+
    INVOKE emit-usage-string  usage-type = short +]), stdout);
    }

  exit (status);
}

void
optionPrintVersion(
    tOptions*   pOptions,
    tOptDesc*   pOptDesc )
{
  char const * pz_by =
    _("[+ # " +][+

  (sprintf "%s%s %s" prog-name
     (if (exist? "prog-group")
         (sprintf " (%s)" (get "prog-group"))
         "" )
     (get "version") ) +]\n\
Written by [+(join ", " (stack "copyright.author"))+].\n\n\
copyright (c) [+ copyright.date +] [+ copyright.owner +]\n[+

CASE copyright.type +][+
*= gpl    +]\
This is free software; see the source for copying conditions.  There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.[+

ESAC +][+ # " +]\n");

  fputs (pz_by, stdout);
  exit (EXIT_SUCCESS);
}

/*
 *  If an option appears more often than is allowed, ...
 */
static void
usage_too_many (tOptDesc* pOptDesc)
{
  char const * pz =
    _("[+(. prog-name)
    +] error: the '%s' option appears more than %d times\n");
  fprintf (stderr, pz, pOptDesc->pz_Name, pOptDesc->optMaxCt);
  USAGE(EXIT_FAILURE);
}
[+
 IF (exist? "flag.min")
+]
/*
 *  There is at least one option that must appear.
 */
static void
usage_too_few (tOptDesc* pOptDesc)
{
  char const * pz =
    _("[+(. prog-name)
    +] error: the '%s' option must appear %d times\n");
  fprintf (stderr, pz, pOptDesc->pz_Name, pOptDesc->optMinCt);
  USAGE(EXIT_FAILURE);
}
[+
 ENDIF
+][+
 IF (exist? "flag.flags-cant")
+]
/*
 *  There is at least one pair of options that may not appear together
 *  on the command line.
 */
static void
usage_cannot (char const* pz_what, char const* pz_cant)
{
  char const * pz =
    _("[+(. prog-name)
    +] error: the `%s' option conflicts with the `%s' option.\n");
  fprintf (stderr, pz, pz_what, pz_cant);
  USAGE(EXIT_FAILURE);
}
[+
 ENDIF
+][+
 IF (exist? "flag.flags-must")
+]
/*
 *  There is at least one pair of options that are required to appear
 *  together on the command line.
 */
static void
usage_must (char const* pz_what, char const* pz_must)
{
  char const * pz =
    _("[+(. prog-name)
    +] error: the `%s' option requires the `%s' option.\n");
  fprintf (stderr, pz, pz_what, pz_must);
  USAGE(EXIT_FAILURE);
}
[+
 ENDIF
+]
/*
 *  Process the options for the "[+(. prog-name)+]" program.
 *  This function was generated to use the getopt[+
 (if (exist? "long-opts") "_long(3GNU)" "(3posix)") +] function.
 *  There are [+ (+ (count "flag") (if (exist? "version") 2 1))
              +] options for this program,
 * including "help (usage)"[+
    IF (exist? "version") +] and "version"[+ ENDIF +].
 */
int
process_[+(. prog-name)+]_opts (int argc, char** argv)
{
  {
    char * pz_prog = strrchr (argv[0], DIRCH);
    /*
     * This violates the const-ness of the pzProgName field.
     * The const-ness is to prevent accidents.  This is not accidental.
     */
    char ** pp = (char **)(void *)&([+ (. prog-name) +]Options.pzProgName);

    if (pz_prog != NULL)
      pz_prog++;
    else
      pz_prog = argv[0];
    *pp = pz_prog;
  }

  for (;;) {
    switch ([+

IF (exist? "long-opts")
      +]getopt_long (argc, argv, z_opts, a_long_opts, NULL)[+
ELSE  +]getopt (argc, argv, z_opts)[+
ENDIF +]) {
    case  -1: goto leave_processing;
    case   0: break;[+
    FOR flag  +][+
      (define OPT-NAME (string-upcase! (string->c-name! (get "name"))))
+]

    case VALUE_OPT_[+ (. OPT-NAME) +]:[+

      IF (not (exist? "max")) +]
      if (HAVE_OPT( [+(. OPT-NAME)+] ))
        usage_too_many (&DESC([+(. OPT-NAME) +]));[+

      ELIF (not (= (get "max") "nolimit"))  +]
      if (DESC([+(. OPT-NAME)+]).optOccCt++ >= DESC([+(. OPT-NAME)+]).optMaxCt)
        usage_too_many (&DESC([+(. OPT-NAME) +]));[+
      ENDIF
+]
      SET_OPT_[+(. OPT-NAME)+][+ (if (exist? "arg-type") "(optarg)") +];
      break;[+

    ENDFOR +]

    case VALUE_OPT_HELP:
      USAGE(EXIT_SUCCESS);
      /* NOTREACHED */
[+ IF (exist? "version") +]
    case VALUE_OPT_VERSION:
      optionPrintVersion (&[+ (. prog-name) +]Options, &DESC(VERSION));
      /* NOTREACHED */
[+ ENDIF +]
    default:
      USAGE(EXIT_FAILURE);
    }
  } leave_processing:;
[+
FOR flag +][+
  IF
     (set! OPT-NAME (string-upcase! (string->c-name! (get "name"))))
     (define check-have-opt (or (exist? "flags-cant") (exist? "flags-must")))
     check-have-opt
+]
  if (HAVE_OPT( [+ (. OPT-NAME) +] )) {[+

    FOR flags-cant      +]
    if (HAVE_OPT( [+ (string-upcase! (string->c-name! (get "flags-cant"))) +] ))
      usage_cannot (DESC([+ (. OPT-NAME) +]).pz_Name, DESC([+
        (string-upcase! (string->c-name! (get "flags-cant"))) +]).pz_Name);[+
    ENDFOR cant         +][+

    FOR flags-must      +]
    if (! HAVE_OPT( [+(string-upcase! (string->c-name! (get "flags-must")))+] ))
      usage_must (DESC([+ (. OPT-NAME) +]).pz_Name, DESC([+
        (string-upcase! (string->c-name! (get "flags-must"))) +]).pz_Name);[+
    ENDFOR must         +][+

    IF (exist? "min")   +][+
      IF (> (string->number (get "min" "0")) 1) +]
    if (DESC([+(. OPT-NAME)+]).optOccCt < DESC([+(. OPT-NAME)+]).optMinCt)
      usage_too_few (&DESC([+(. OPT-NAME) +]));[+

      ENDIF +][+
    ENDIF +]
  }
[+

  ENDIF

+][+

  IF (exist? "min")     +][+
    IF (. check-have-opt)
+]  else[+

    ELSE
+]
  if ([+ #
       We have a minimum count, but we have not checked for option existence
       yet because there are no option interdependencies.  We must therefore
       now check to see if the option has appeared the required number of
       times.  In the absence of a max count, our limit must be one and we
       only check for presence.  If a max count exists, then we will also
       have kept the occurrence count.  Check that against the limit. +][+

      IF (not (exist? "max"))
        +]! HAVE_OPT( [+ (. OPT-NAME) +] )[+
      ELSE  max ct exists
        +]DESC([+(. OPT-NAME)+]).optOccCt < DESC([+(. OPT-NAME)+]).optMinCt[+
      ENDIF +])[+

    ENDIF +]
    usage_too_few (&DESC([+(. OPT-NAME) +]));
[+
  ENDIF  +][+
ENDFOR   +]
  return 0;
}
[+ ESAC  +][+

DEFINE emit-usage-string    +][+

  (out-push-new)            +][+
  INCLUDE "usage.tlib"      +][+
  (kr-string (string-append (shell (string-append
  "sed -e '/version information/s/ -v \\[arg\\]/ -v      /' \
       -e '/: illegal option --/d' \
       -e 's/ --version\\[=arg\\]/ --version      /' <<_EOF_\n"
  (out-pop #t) "\n_EOF_"
  )) "\n" ))  +][+

ENDDEF

# end of getopt.tpl \+]