summaryrefslogtreecommitdiff
path: root/autoopts/test/enums.test
blob: d3edf55cf02de603c1f065f824be0cb16a50519a (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
#! /bin/sh
#  -*- Mode: shell-script -*-
# ----------------------------------------------------------------------
# enums.test ---  test enums program attribute
#                    make sure that when it is not specified
#                    then option processing consumes all args.
#
# Time-stamp:        "2012-08-11 08:31:49 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

# # # # # # # # # # DEFINITIONS FILE # # # # # # # # #

cat > ${testname}.def <<-  _EOF_
	autogen definitions options;

	prog-name   = ${testname}-test;
	prog-title  = 'test complex main procedure';
	config-header = config.h;

	argument    = '[ <node> ]';
	long-opts;

	flag = {
	   name     = print;
	   value    = p;
	   descrip  = 'Print operational info';
	   arg-type = keyword;
	   arg-name = type;
	   min      = 1;
	   equivalence = print;
	   keyword  = one, two, three, four, five, six, seven, eight, nine, ten;
	};

	flag = {
	   name     = dump-log;
	   value    = D;
	   descrip  = 'Dump the program log';
	   equivalence = print;
	};

	flag = {
	   name     = all-dump;
	   value    = A;
	   equivalence = print;
	   descrip  = 'Dump everything we\'ve got';
	};

	flag = {
	   name     = set;
	   value    = s;
	   descrip  = 'set options';
	   arg-type = set;
	   arg-name = 'opt[, ...]';
	   arg-default = first,fifth,ninth,thirteenth;
	   equivalence = print;
	   keyword  = first, second, third, fourth, fifth, sixth, seventh,
	              eighth, ninth, tenth, eleventh, twelfth, thirteenth,
	              fourteenth, fifteenth, sixteenth;
	};

	flag = {
	   name     = unset;
	   value    = u;
	   descrip  = 'unset debug options';
	   arg-type = string;
	   arg-name = 'opt[, ...]';
	   equivalence = print;
	   flag-proc   = set;
	};

	flag = {
	   name     = msg-num;
	   value    = m;
	   descrip  = 'message number';
	   no-preset;
	   arg-type = string;
	   arg-name = id;
	   max      = NOLIMIT;

	   flag-code = <<- _EndOfFlagCode_
		    /*
		     *  'set' and 'unset' must be acted upon immediately
		     *  -- we may get more of them.
		     */
		    switch (WHICH_IDX_PRINT) {
		    case NO_EQUIVALENT:
		    case INDEX_OPT_PRINT:
		    case INDEX_OPT_DUMP_LOG:
		    case INDEX_OPT_ALL_DUMP:
		        if (COUNT_OPT( MSG_NUM ) > 1) {
		            fputs("Except for 'set' and 'unset' functions, "
		                  "only one 'msg-num' is allowed\n", stderr);
		            USAGE(EXIT_FAILURE);
		        }
		        break;
		    case INDEX_OPT_SET:
		        set_options(1, pOptDesc->pzLastArg);
		        break;
		    case INDEX_OPT_UNSET:
		        set_options(0, pOptDesc->pzLastArg);
		        break;
		    };
		_EndOfFlagCode_;
	};
	/*
	cat <<_EOF_
	 * for emacs */

	export = "#include <sys/types.h>\n"
		 "#include <sys/stat.h>\n"
		 "#include <stdio.h>\n"
		 "#include <stdlib.h>\n"
		 "#include <fcntl.h>\n"
		 "#include \"config.h\"\n"
		 "#include \"compat/compat.h\"";

		include = 'void set_options(int mode, char const* pzArg);';

	main = {
	   main-type = include;
	   tpl       = ${testname}.tpl;
	};
	_EOF_

# # # # # # # # # # TEMPLATE FILE # # # # # # # # #
#
#  In one case we must not use built in echo.
#
cat > ${testname}.tpl <<- \__EOF
	[= AutoGen5 Template -*- Mode: C -*- =]
	[=(define proc-list "")=]
	typedef int (do_proc_t)(void);
	extern do_proc_t
	[=  (set! proc-list (string-append "do_print_undefined,\ndo_print_"
	                    (join ",\ndo_print_" (stack "flag[0].keyword"))  ))
	    (set! proc-list
	         (shell (string-append "${CLexe} -I4 --spread=1 <<_EOF_\n"
	            proc-list "\n_EOF_")) )
	    proc-list =];
	do_proc_t* do_proc[] = {
	[= (. proc-list) =] };

	[=(shellf "procs='%s' ; ix=0 ; for p in ${procs}
	do
	  p=`echo $p | sed s/,//`
	  echo int ${p}'(void) {'
	  printf '    fputs(\"'${p}'\\\\n\", stdout);\n'
	  echo   \"    return ${ix}; }\"
	  ix=`expr $ix + 1`
	done" proc-list) =]

	int
	do_dump_log(void)
	{
	    return WHICH_IDX_PRINT != INDEX_OPT_DUMP_LOG;
	}

	int
	do_all_dump(void)
	{
	    return WHICH_IDX_PRINT != INDEX_OPT_ALL_DUMP;
	}

	int
	do_set(int which_way)
	{
	    printf("PRINT = 0x%lX\n", (unsigned long)DESC(PRINT).optCookie);
	    printf("SET   = 0x%lX\n", (unsigned long)DESC(SET).optCookie);
	    if (which_way)
	        printf("0x%lX\n", OPT_VALUE_SET);
	    else
	        printf("0x%lX\n", (~ OPT_VALUE_SET) & SET_MEMBERSHIP_MASK);
	    return 0;
	}

	void
	set_options(int mode, char const* pzArg)
	{
	    exit(atoi(pzArg));
	}
	__EOF

cat >> ${testname}.tpl <<- __EOF
	int
	main( int argc, char** argv )
	{
	    {
	        int ct = optionProcess(&${testname}_testOptions, argc, argv);
	        argc -= ct;
	        argv += ct;
	    }

	    if (argc > 1)
	        return EXIT_FAILURE;

	    /*
	     *  Invoke the proper operational procedure.
	     */
	    {
	        int res = 0;
	        switch (WHICH_IDX_PRINT) {
	        case INDEX_OPT_PRINT:    res = do_proc[OPT_VALUE_PRINT](); break;
	        case INDEX_OPT_DUMP_LOG: res = do_dump_log();  break;
	        case INDEX_OPT_ALL_DUMP: res = do_all_dump();  break;
	        case INDEX_OPT_SET:      res = do_set(1);    break;
	        case INDEX_OPT_UNSET:    res = do_set(0);    break;
	        }
	        return res;
	    }
	}
	__EOF

# # # # # # # # # # CREATE PROGRAM # # # # # # # # #

echo ${AG_L} ${testname}.def
${AG_L} ${testname}.def || \
  failure AutoGen could not process

compile "-?"

# # # # # # # # # # HELP OUTPUT FILE # # # # # # # # #

basehlp=${testname}.hlp
echo creating ${basehlp}
clean_help > ${basehlp} <<_EOF_
${testname}-test - test complex main procedure
USAGE:  ${testname} { -<flag> [<val>] | --<name>[{=| }<val>] }... [ <node> ]
  Flg Arg Option-Name   Req?  Description
   -p KWd print          YES  Print operational info
   -D no  dump-log       opt  Dump the program log
				- an alternate for print
   -A no  all-dump       opt  Dump everything we've got
				- an alternate for print
   -s Mbr set            opt  set options
				- an alternate for print
   -u Str unset          opt  unset debug options
				- an alternate for print
   -m Str msg-num        opt  message number
				- may not be preset
				- may appear multiple times
   -? no  help           opt  Display extended usage information and exit
   -! no  more-help      opt  Extended usage information passed thru pager

Options are specified by doubled hyphens and their name or by a single
hyphen and the flag character.

The valid "print" option keywords are:
  one two three four five six seven eight nine ten
  or an integer from 1 through 10
The valid "set" option keywords are:
  first      second     third      fourth     fifth      sixth      seventh
  eighth     ninth      tenth      eleventh   twelfth    thirteenth fourteenth
  fifteenth  sixteenth
  or an integer mask with any of the lower 16 bits set
or you may use a numeric representation.  Preceding these with a '!' will
clear the bits, specifying 'none' will clear all bits, and 'all' will set them
all.  Multiple entries may be passed as an option argument list.
_EOF_

cmp -s ${testname}.h*lp || \
  failure "`diff ${basehlp} ${testname}.help`"

# # # # # # # # # # TEST OPERATION # # # # # # # # # #

ix=0
for f in one two three four five six seven eight nine ten
do
  ix=`expr $ix + 1`
  txt=`./${testname} -p $f`
  test $? -eq $ix || \
    failure "'./${testname} -p $f' did not yield $ix"
  test "${txt}" = "do_print_${f}" || \
    failure "'./${testname} -p $f' did not print 'do_print_${f}'"
done

cleanup

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

# end of enums.test