summaryrefslogtreecommitdiff
path: root/autoopts/test/getopt.test
blob: 383c6fce6b1edcd9784a1bfb5c2f0cc411f4ec06 (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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#! /bin/sh
#  -*- Mode: Shell-script -*-
#
# getopt.test ---  test getopt_long argument processing
#
# Time-stamp:        "2012-05-12 19:54:26 bkorb"
# Author:            Bruce Korb <bkorb@gnu.org>
#
##  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 pkg/libopts/COPYING.gplv3
##  06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
##  66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd

. ./defs

set -x
PS4='>${FUNCNAME:-go}> '

CC="${CC} ${CFLAGS} ${INC}"
CFLAGS=''
INC=''
export AUTOGEN_TEMPL_DIRS=${TMPDIR}

compile_getopt() {
  run_ag opts -b${testname}-bn ${testname}.def || \
    failure "AutoGen could not process ${testname}.def #${1}"

  # Remove the variable comments so we can test against the expected result
  #
  ${SED} '2,/and the template file/d
      /\$''Id.*\$/d
      /Last template edit:/d' getopt-test_${testname}.c \
	> ${testname}${1}-getopt.c

  # Finally, compile this thing:
  #
  ${CC} ${CFLAGS} -c ${testname}${1}-getopt.c || \
    failure "could not compile ${testname}1-getopt.c"

  ${SED} "${sed_omit_license}"'
	/Packaged by /d
	s@^Report .* bugs to.*"@\\n"@
	s@^please send bug.*"@\\n"@
	s@\\n\\n\\n\\$@\\n\\n\\@
	s@\(and the flag character\.\\n.\)n\\$@\1@' ${testname}${1}-getopt.c \
         >  ${testname}${1}-res.c
  cmp -s ${testname}${1}-base.c ${testname}${1}-res.c || { set +x ; \
    failure "`diff -c ${testname}${1}-base.c ${testname}${1}-res.c`" ; }
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
#  Fix up a test-specific include directory:
#
# The getopt template presumes that everything has been installed.
# However, we have to work with the local stuff.  So, remove the
# "autoopts-config" probing and replace with local stuff:
#
go_init()
{
  mkdir -p ${TMPDIR}/autoopts

  CFLAGS="-I${TMPDIR}"
  LDFLAGS="${LDFLAGS} ${LIB}"
  export CFLAGS LDFLAGS TMPDIR

  aolib=`find ${top_builddir}/autoopts -type f -name libopts.a`
  case " ${LDFLAGS} " in
  *' -lgen '* ) aolib=${aolib}\ -lgen ;;
  esac

  cd ${top_srcdir}/autoopts/tpl
  cp *.* ${TMPDIR}/.
  chmod u+w ${TMPDIR}/*

  cd ${top_builddir}/autoopts/tpl
  cp *.* ${TMPDIR}/.
  chmod u+w ${TMPDIR}/*

  cd ${builddir}/testdir
  ${SED} -e "/^cflags=/s@=.*@='-I${TMPDIR}/autoopts -I${top_builddir}'@" \
    -e "/^ldflags=/s@=.*@='${aolib}'@" \
    ${TMPDIR}/usage.tlib > ${TMPDIR}/usage.tlib-XX
  mv -f ${TMPDIR}/usage.tlib-XX ${TMPDIR}/usage.tlib

  # In order to compile correctly, we have to temporarily install the options.h
  # header in our TMPDIR.  We also must find that header first.  Tweak CFLAGS:
  #
  DESTdestdir=${TMPDIR}/autoopts \
  top_builddir=${top_builddir} \
    CONFIG_SHELL="${SHELL}" \
    ${SHELLX} -x ${top_srcdir}/autoopts/install-hook.sh
  AUTOGEN_TEMPL_DIRS=${TMPDIR}
  export AUTOGEN_TEMPL_DIRS
}

# # # # # # # # # # DEFINITIONS FILE # # # # # # # # #
go_samples() {

echo "creating ${testname}.def in `pwd`"

cat >> ${testname}.def <<_EOF_
AutoGen definitions getopt;

prog-name  = "test_${testname}";
prog-title = "Test AutoOpts for ${testname}";
test-main  = 'yes';
config-header = 'config.h';

settable;
version = '1.2.3';
help-value = 'h';
gnu-usage;
no-libopts;

copyright = {
    date  = "2003-2012";
    owner = "Odyssey Computing Concepts, Inc.";
    author= "Bruce Korb";
    eaddr = "bkorb@gnu.org";
    type  = lgpl;
};

flag = {
    name = "option";
    descrip = "The option option descrip";
    value = 'o';
    arg_type = string; arg_default = 'opt init';
};

flag = {
    name = "second";
    descrip = "The second option descrip";
    value = 's';
    arg_type = string; arg_default = '020';
};

flag = {
   name    = no_val;
   descrip = 'option with no flag';
   value   = 'F'; /* REMOVE */
   flags-must = max_val;
};

flag = {
   name    = max_val;
   descrip = 'option with max ct';
   value   = 'X'; /* REMOVE */
   max     = '5';
};

flag = {
   name    = min2_val;
   descrip = 'option with min ct';
   value   = 'M'; /* REMOVE */
   max     = '50';
   flags-cant = max_val;
   min     = '5';
};

flag = {
   name    = min_val;
   descrip = 'option with min ct';
   value   = '2'; /* REMOVE */
   max     = '50';
   min     = '5';
};
_EOF_

# # # # # # # # # # BASE-1 OUTPUT FILE # # # # # # # # #

echo creating ${testname}1-base.c

cat > ${testname}1-base.c <<\_EndOfSample_
#include "getopt-test_getopt.h"

#include <sys/types.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "getopt-bn.h"

#ifndef DIRCH
#  if defined(_WIN32) && !defined(__CYGWIN__)
#    define DIRCH                  '\\'
#  else
#    define DIRCH                  '/'
#  endif
#endif
/*
 *  Option flag character list
 */
static char z_opts[] = "o:s:FXM2hv";

/*
 *  AutoOpts library replacement routines:
 */
void
optionUsage (tOptions* pOptions, int status)
{
  if (status != 0)
    fprintf (stderr, _("Try `%s -h' for more information.\n"),
             test_getoptOptions.pzProgName);
  else
    {
      fputs (_("test_getopt - Test AutoOpts for getopt - Ver. 1.2.3\n\
USAGE:  test_getopt { -<flag> [<val>] }...\n\n\
   -o str     The option option descrip\n\
   -s str     The second option descrip\n\
   -F         option with no flag\n\
   -X         option with max ct\n\
   -M         option with min ct\n\
   -2         option with min ct\n\
   -v         Output version information and exit\n\
   -h         Display extended usage information and exit\n\n\
\n"), stdout);
    }

  exit (status);
}

void
optionPrintVersion(
    tOptions*   pOptions,
    tOptDesc*   pOptDesc )
{
  char const * pz_by =
    _("test_getopt 1.2.3\n\
Written by Bruce Korb.\n\n\
copyright (c) 2003-2012 Odyssey Computing Concepts, Inc.\n\
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.\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 =
    _("test_getopt error: the '%s' option appears more than %d times\n");
  fprintf (stderr, pz, pOptDesc->pz_Name, pOptDesc->optMaxCt);
  USAGE(EXIT_FAILURE);
}

/*
 *  There is at least one option that must appear.
 */
static void
usage_too_few (tOptDesc* pOptDesc)
{
  char const * pz =
    _("test_getopt error: the '%s' option must appear %d times\n");
  fprintf (stderr, pz, pOptDesc->pz_Name, pOptDesc->optMinCt);
  USAGE(EXIT_FAILURE);
}

/*
 *  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 =
    _("test_getopt error: the `%s' option conflicts with the `%s' option.\n");
  fprintf (stderr, pz, pz_what, pz_cant);
  USAGE(EXIT_FAILURE);
}

/*
 *  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 =
    _("test_getopt error: the `%s' option requires the `%s' option.\n");
  fprintf (stderr, pz, pz_what, pz_must);
  USAGE(EXIT_FAILURE);
}

/*
 *  Process the options for the "test_getopt" program.
 *  This function was generated to use the getopt(3posix) function.
 *  There are 8 options for this program,
 * including "help (usage)" and "version".
 */
int
process_test_getopt_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 *)&(test_getoptOptions.pzProgName);

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

  for (;;) {
    switch (getopt (argc, argv, z_opts)) {
    case  -1: goto leave_processing;
    case   0: break;

    case VALUE_OPT_OPTION:
      if (HAVE_OPT( OPTION ))
        usage_too_many (&DESC(OPTION));
      SET_OPT_OPTION(optarg);
      break;

    case VALUE_OPT_SECOND:
      if (HAVE_OPT( SECOND ))
        usage_too_many (&DESC(SECOND));
      SET_OPT_SECOND(optarg);
      break;

    case VALUE_OPT_NO_VAL:
      if (HAVE_OPT( NO_VAL ))
        usage_too_many (&DESC(NO_VAL));
      SET_OPT_NO_VAL;
      break;

    case VALUE_OPT_MAX_VAL:
      if (DESC(MAX_VAL).optOccCt++ >= DESC(MAX_VAL).optMaxCt)
        usage_too_many (&DESC(MAX_VAL));
      SET_OPT_MAX_VAL;
      break;

    case VALUE_OPT_MIN2_VAL:
      if (DESC(MIN2_VAL).optOccCt++ >= DESC(MIN2_VAL).optMaxCt)
        usage_too_many (&DESC(MIN2_VAL));
      SET_OPT_MIN2_VAL;
      break;

    case VALUE_OPT_MIN_VAL:
      if (DESC(MIN_VAL).optOccCt++ >= DESC(MIN_VAL).optMaxCt)
        usage_too_many (&DESC(MIN_VAL));
      SET_OPT_MIN_VAL;
      break;

    case VALUE_OPT_HELP:
      USAGE(EXIT_SUCCESS);
      /* NOTREACHED */

    case VALUE_OPT_VERSION:
      optionPrintVersion (&test_getoptOptions, &DESC(VERSION));
      /* NOTREACHED */

    default:
      USAGE(EXIT_FAILURE);
    }
  } leave_processing:;

  if (HAVE_OPT( NO_VAL )) {
    if (! HAVE_OPT( MAX_VAL ))
      usage_must (DESC(NO_VAL).pz_Name, DESC(MAX_VAL).pz_Name);
  }

  if (HAVE_OPT( MIN2_VAL )) {
    if (HAVE_OPT( MAX_VAL ))
      usage_cannot (DESC(MIN2_VAL).pz_Name, DESC(MAX_VAL).pz_Name);
    if (DESC(MIN2_VAL).optOccCt < DESC(MIN2_VAL).optMinCt)
      usage_too_few (&DESC(MIN2_VAL));
  }
  else
    usage_too_few (&DESC(MIN2_VAL));

  if (DESC(MIN_VAL).optOccCt < DESC(MIN_VAL).optMinCt)
    usage_too_few (&DESC(MIN_VAL));

  return 0;
}
_EndOfSample_

# # # # # # # # # # BASE-2 OUTPUT FILE # # # # # # # # #

echo creating ${testname}2-base.c

cat > ${testname}2-base.c <<\_EndOfSample_
#include "getopt-test_getopt.h"

#include <sys/types.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "getopt-bn.h"

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

/*
 *  getopt_long option descriptor
 */
static struct option a_long_opts[] = {
  { "option",            1, NULL, VALUE_OPT_OPTION },
  { "second",            1, NULL, VALUE_OPT_SECOND },
  { "no_val",            0, NULL, VALUE_OPT_NO_VAL },
  { "max_val",           0, NULL, VALUE_OPT_MAX_VAL },
  { "min2_val",          0, NULL, VALUE_OPT_MIN2_VAL },
  { "min_val",           0, NULL, VALUE_OPT_MIN_VAL },
  { "help",              0, NULL, VALUE_OPT_HELP },
  { "version",           0, NULL, VALUE_OPT_VERSION },
  { NULL,                0, NULL, 0 }
};

/*
 *  Option flag character list
 */
static char z_opts[] = "o:s:h";

/*
 *  AutoOpts library replacement routines:
 */
void
optionUsage (tOptions* pOptions, int status)
{
  if (status != 0)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
             test_getoptOptions.pzProgName);
  else
    {
      fputs (_("test_getopt - Test AutoOpts for getopt - Ver. 1.2.3\n\
USAGE:  test_getopt { -<flag> [<val>] | --<name>[{=| }<val>] }...\n\n\
   -o, --option=str           The option option descrip\n\
   -s, --second=str           The second option descrip\n\
       --no-val               option with no flag\n\
       --max-val              option with max ct\n\
       --min2-val             option with min ct\n\
       --min-val              option with min ct\n\
       --version              Output version information and exit\n\
   -h, --help                 Display extended usage information and exit\n\n\
Options are specified by doubled hyphens and their name or by a single\n\
hyphen and the flag character.\n\
\n"), stdout);
    }

  exit (status);
}

void
optionPrintVersion(
    tOptions*   pOptions,
    tOptDesc*   pOptDesc )
{
  char const * pz_by =
    _("test_getopt 1.2.3\n\
Written by Bruce Korb.\n\n\
copyright (c) 2003-2012 Odyssey Computing Concepts, Inc.\n\
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.\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 =
    _("test_getopt error: the '%s' option appears more than %d times\n");
  fprintf (stderr, pz, pOptDesc->pz_Name, pOptDesc->optMaxCt);
  USAGE(EXIT_FAILURE);
}

/*
 *  There is at least one option that must appear.
 */
static void
usage_too_few (tOptDesc* pOptDesc)
{
  char const * pz =
    _("test_getopt error: the '%s' option must appear %d times\n");
  fprintf (stderr, pz, pOptDesc->pz_Name, pOptDesc->optMinCt);
  USAGE(EXIT_FAILURE);
}

/*
 *  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 =
    _("test_getopt error: the `%s' option conflicts with the `%s' option.\n");
  fprintf (stderr, pz, pz_what, pz_cant);
  USAGE(EXIT_FAILURE);
}

/*
 *  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 =
    _("test_getopt error: the `%s' option requires the `%s' option.\n");
  fprintf (stderr, pz, pz_what, pz_must);
  USAGE(EXIT_FAILURE);
}

/*
 *  Process the options for the "test_getopt" program.
 *  This function was generated to use the getopt_long(3GNU) function.
 *  There are 8 options for this program,
 * including "help (usage)" and "version".
 */
int
process_test_getopt_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 *)&(test_getoptOptions.pzProgName);

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

  for (;;) {
    switch (getopt_long (argc, argv, z_opts, a_long_opts, NULL)) {
    case  -1: goto leave_processing;
    case   0: break;

    case VALUE_OPT_OPTION:
      if (HAVE_OPT( OPTION ))
        usage_too_many (&DESC(OPTION));
      SET_OPT_OPTION(optarg);
      break;

    case VALUE_OPT_SECOND:
      if (HAVE_OPT( SECOND ))
        usage_too_many (&DESC(SECOND));
      SET_OPT_SECOND(optarg);
      break;

    case VALUE_OPT_NO_VAL:
      if (HAVE_OPT( NO_VAL ))
        usage_too_many (&DESC(NO_VAL));
      SET_OPT_NO_VAL;
      break;

    case VALUE_OPT_MAX_VAL:
      if (DESC(MAX_VAL).optOccCt++ >= DESC(MAX_VAL).optMaxCt)
        usage_too_many (&DESC(MAX_VAL));
      SET_OPT_MAX_VAL;
      break;

    case VALUE_OPT_MIN2_VAL:
      if (DESC(MIN2_VAL).optOccCt++ >= DESC(MIN2_VAL).optMaxCt)
        usage_too_many (&DESC(MIN2_VAL));
      SET_OPT_MIN2_VAL;
      break;

    case VALUE_OPT_MIN_VAL:
      if (DESC(MIN_VAL).optOccCt++ >= DESC(MIN_VAL).optMaxCt)
        usage_too_many (&DESC(MIN_VAL));
      SET_OPT_MIN_VAL;
      break;

    case VALUE_OPT_HELP:
      USAGE(EXIT_SUCCESS);
      /* NOTREACHED */

    case VALUE_OPT_VERSION:
      optionPrintVersion (&test_getoptOptions, &DESC(VERSION));
      /* NOTREACHED */

    default:
      USAGE(EXIT_FAILURE);
    }
  } leave_processing:;

  if (HAVE_OPT( NO_VAL )) {
    if (! HAVE_OPT( MAX_VAL ))
      usage_must (DESC(NO_VAL).pz_Name, DESC(MAX_VAL).pz_Name);
  }

  if (HAVE_OPT( MIN2_VAL )) {
    if (HAVE_OPT( MAX_VAL ))
      usage_cannot (DESC(MIN2_VAL).pz_Name, DESC(MAX_VAL).pz_Name);
    if (DESC(MIN2_VAL).optOccCt < DESC(MIN2_VAL).optMinCt)
      usage_too_few (&DESC(MIN2_VAL));
  }
  else
    usage_too_few (&DESC(MIN2_VAL));

  if (DESC(MIN_VAL).optOccCt < DESC(MIN_VAL).optMinCt)
    usage_too_few (&DESC(MIN_VAL));

  return 0;
}
_EndOfSample_
}

# # # # # # # # # # RESULTS TESTING # # # # # # # # #

go_init
go_samples
compile_getopt 1

${GREP} 'getopt_long' /usr/include/getopt.h >/dev/null && {
    CFLAGS="${CFLAGS} -D_GNU_SOURCE=1"
    ${SED} '/REMOVE/d;$a\
long-opts;\
version-value;
' ${testname}.def > ${testname}2.def
    mv -f ${testname}2.def ${testname}.def

    compile_getopt 2
}

cleanup

## Local Variables:
## mode: shell-script
## indent-tabs-mode: nil
## sh-indentation: 2
## sh-basic-offset: 2
## End:

# end of getopt.test