1 m4_include([general.m4sh])m4_divert_push([KILL]) -*- Autoconf -*-
2 # getopt.m4sh -- getopt helper functions
4 # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 # Written by Gary V. Vaughan, 2004
8 # This file is part of GNU Libtool.
10 # GNU Libtool is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; either version 2 of
13 # the License, or (at your option) any later version.
15 # As a special exception to the GNU General Public License, if you
16 # distribute this file as part of a program or library that contains
17 # a configuration script generated by Autoconf, you may include this
18 # file under the same distribution terms that you use for the rest
21 # GNU Libtool is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNES FOR A PARTICULAR PURPOSE. See the GNU
24 # General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with GNU Libtool; see the file COPYING. If not, a copy
28 # can be downloaded from http://www.gnu.org/licenses/gpl.html,
29 # or obtained by writing to the Free Software Foundation, Inc.,
30 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 # This file provides the M4SH_GETOPTS option processing compiler, and
33 # all necessary support at the m4 and shell-script levels.
35 # An m4sh script can include this file, and an M4SH_GETOPTS invocation
36 # that expands to a shell script option processing loop with similar
37 # abilites to a C program the uses getopt_long() to process it's command
38 # line options - although, unlike the C API, M4SH_GETOPTS also supplies
39 # the loop to step through and process the options.
41 # See the comment above M4SH_GETOPTS, below, for details.
44 # All internal macros begin with `m4go_'.
45 m4_pattern_forbid([^_?m4go_])
48 ## --------------------------- ##
49 ## 1. Backwards compatibility. ##
50 ## --------------------------- ##
52 # We prefer m4sugar.m4 from Autoconf-2.64, but have fallbacks in this
53 # section that work back as far as Autoconf-2.62. This file is used
54 # at bootstrap time to generate the shell processing loop for ltmain.sh
55 # and libtoolize.in, so it's okay for the requirement to be tighter
56 # than the configure time Autoconf prerequisite version.
57 m4_version_prereq([2.62])
61 # m4_chomp was not introduced until Autoconf-2.64. Currently we
62 # only use it indirectly via m4go_expand, below. This implementation
63 # is taken from Autoconf-2.65.
65 [m4_define([m4_chomp],
66 [m4_format([[%.*s]], m4_index(m4_translit([[$1]], [
67 /.], [/ ])[./.], [/.]), [$1])])])
71 # M4SH_GETOPTS wants to pass unbalanced parentheses to m4_expand to
72 # build the branches of a shell `case' statement. That is only
73 # supported by the implementation of m4_expand in Autoconf-2.64 and
74 # newer. Since we want to be compatible back to at least
75 # Autoconf-2.62, reimplement our own 2.64 based m4_expand in the
76 # m4go_ namespace so that we can be compatible with Autoconf versions
77 # supporting either semantic.
78 m4_define([m4go_expand],
82 m4_define([_m4go_expand], [$0_([$1], [(], -=<{($1)}>=-, [}>=-])])
84 m4_define([_m4go_ignore])
86 m4_define([_m4go_expand_],
88 [m4_changequote([-=<{$2], [)}>=-])$3m4_changequote([, ])],
89 [$0([$1], [($2], -=<{($2$1)}>=-, [}>=-])_m4go_ignore$2])])
92 ## --------------------------------- ##
93 ## 2. Low level string manipulation. ##
94 ## --------------------------------- ##
96 # m4go_slice(STRING, BEGIN, END)
97 # ------------------------------
98 # Just like m4_substr(), except that both the BEGIN and END of the
99 # substring are given as offsets from the beginning of STRING, as
100 # returned by m4_index().
101 m4_define([m4go_slice],
102 [m4_substr([$1], [$2], m4_eval([$3-$2]))])
105 # -------------------
106 # Trim all leading and trailing newlines from STRING.
108 # Inspite of careful quoting, this macro is NOT robust to active
110 # | m4_define(active, ACTV)
112 # | start active finish
115 # The interaction between index counting and macro expansion also
116 # results in overtrimming in this example by 2 characters (the
117 # difference between the lengths of `active' and `ACTV'). Translation
118 # into French is just a coincidence.
120 # The implementation is surprisingly finicky: We use m4go_slice() to
121 # extract the middle section of the string, while using m4_bregexp() to
122 # return offset counts for the first and last non-newlines in STRING.
123 # But there are a number of corner cases:
124 # 1. Double quoted STRING works, (i.e m4go_trimn([[\nthe string\n]]. We
125 # achieve that by transliterating quote characters as well as
126 # newlines to the same character before counting off the offsets.
127 # 2. This needs a slightly different transliteration for each
128 # m4_bregexp() invocation, because we need to maintain balanced
129 # quotes in the argument and we don't want to swallow leading ']' or
130 # trailing '[' characters. So, we turn the quote we don't want to
131 # strip into a harmless ' '.
132 # 3. Because we're now effectively stripping the quotation with the
133 # m4_translit() calls, embedded commas can fool m4_bregexp() into
134 # thinking there are more arguments than we intended, so we turn
135 # them into spaces too.
136 # 4. Unbalanced parentheses would also confuse m4_bregexp(), so we also
137 # turn them into spaces. The upshot of that if STRING contains a
138 # macro invocation with arguments, it will be expanded in the result
139 # as if no arguments had been passed.
140 # 5. Comments are not ignored after stripping quote marks, so we have
141 # to turn them off for the duration.
142 # 6. Finally, we need to requote the result to account for the quotes
143 # we probably stripped. m4_quote() doesn't handle commas well, so
144 # we use m4go_expand() to requote without losing whitespace after
145 # any embedded commas.
146 m4_define([m4go_trimn],
147 [m4_changecom()m4go_expand([m4go_slice([$1], m4_bregexp(m4_translit([$1],[
148 [/,()]], [// ]), [[^/]]), m4_bregexp(m4_translit([$1], [
149 [/,()]], [/ /]), [/*$]))])[]m4_changecom([#])])
153 # Trim leading TABs from each line of STRING.
154 m4_define([m4go_untab],
155 [m4_bpatsubst([$1], [^[ ]*], [])])
157 # m4go_unindent(STRING)
158 # ---------------------
159 # Completely unindent STRING: Remove the leading TABs from each line of
160 # STRING; trim leading newlines and trailing whitespace from STRING as
162 m4_define([m4go_unindent],
163 [m4_ifval([$1], [m4go_untab([m4go_trimn([$1])])])])
166 ## ------------------------------ ##
167 ## 3. Option processing compiler. ##
168 ## ------------------------------ ##
170 # Shell fragments are piecemeal added to these macros for each
171 # invocation of m4go_option; eventually to be expanded into the compiled
172 # option parsing shell script by M4SH_GETOPTS:
174 m4_define([m4go_defaults], []) # initial shell option variable setings
175 m4_define([m4go_branches], []) # case branches to process options
177 # The initial entries in m4go_shortnoargs represent the (non-argument)
178 # options that are always accepted by the expanded processing loop. We
179 # also keep a list of short options that accept an option argument in
180 # the macro `m4go_shortargs', but we use `m4_append([m4go_shortargs],
181 # OPTION, [|])' to insert the separator pipe symbols - which requires
182 # that `m4go_shortargs' be undefined if the first option appended is not
183 # prefixed by a leading `|'.
184 m4_define([m4go_shortnoargs], [-\?*|-h*])
187 # M4SH_GETOPTS(SHORT-SPEC1, LONG-MATCH1, DEF1, INIT1,
188 # SHORT-SPEC2, LONG-MATCH2, DEF2, INIT2, ... [VALIDATION])
189 # ---------------------------------------------------------------------
190 # Declare a series of command line options with one letter (`-m') or
191 # long form `--message' formats, along with optional default values
192 # incase a given option is not provided by the user when the script is
193 # invoked, and validation code, for example to prevent specifying
194 # mutually exclusive options or omitting required options.
196 # After this macro has been called, the value of each option is
197 # available in a shell variable named `opt_' followed by the first part
198 # (i.e. up to the first '|' symbol) of the LONG-MATCHn argument with the
199 # leading `--` removed, and any further `-' converted to `_', or else if
200 # no long form option was provided, simple `opt_' followed by the short
201 # option letter. For example, the value supplied by the user as an
202 # argument to an option `--gpg-key-id' will be available in the shell
203 # variable $opt_gpg_key_id, `-c' with no long form option will be
204 # available as $opt_c and so on. Where an option doesn't take an
205 # argument, then the shell variable will be set to either `:' or `false'
206 # depending on whether the user set that option on the script command
207 # line - with one important exception: If the long form option name
208 # begins with `--no-', and does not require an option argument, then the
209 # variable name will be `opt_' followed by the rest of the option name
210 # with the leading `no_' removed; and it's value will be `false' if
211 # `--no-foo' was given on the command line, and `:' otherwise.
213 # Each option is declared by a set of 4 arguments as follows:
216 # The short option letter, if any, for the nth option followed by
217 # any flags to denote special processing for this option. For
218 # example, `f?@'. See below for a list of the supported flags and
219 # their meaning. If you specify `h', `v' or `x', then the
220 # automatic processing of those short options for `--help',
221 # `--version' and `--debug' (resp.) will be overridden.
223 # The long option list, including leading dashes for the nth
224 # option. The value for this option is used directly as a shell
225 # `case' branch to match the option, so you can specify multiple
226 # matches. For example, `--message|--mes*|--msg'. If you specify
227 # neither this argument nor a short option letter with
228 # SHORT-SPECn, then invalid shell code will be generated.
229 # DEFn If there is a default value for the nth option to take when it
230 # is not given on the command line when the script is executed,
231 # specify it here. Double quotes are added in the expanded shell
232 # script, so it is safe to use shell variables. For example,
234 # INITn Any option specific initialisation for the nth option should be
235 # specified in this argument. The shell code it contains is added
236 # directly to the case branch that matches the nth option, after
237 # the `opt_NAME' variable has been set, with the option argument
238 # (if any) still left in `$1'. This allows neat tricks such as
239 # injecting new arguments into the command line before the
240 # processing loop terminates. For example:
242 # `eval set -- `cat $rcfile` ${1+"$@"}'
244 # Note that, because we look inside the content of INITn to
245 # determine whether there are newlines to be stripped, double
246 # quoting the whele thing doesn't work. Instead, you'll need to
247 # quote active symbols yourself. Generally, you'll only need to
248 # worry about $n and $@, although if you use symbols that can be
249 # expanded by m4 you'll need to quote those too.
251 # The final argument, if any, should contain shell code to
252 # validate correctness of the processed options. This code is
253 # expanded after the option processing loop has exited, and before
254 # the conditional script exit if errors have been found. Try to
255 # show as many errors as possible before exiting the shell rather
256 # than bailing out on the first error discovered so that the user
257 # can correct all of them at once rather than just one between
258 # each reinvocation of the script.
260 # In addition to an option short form letter (e.g. `m'), each
261 # SHORT-SPECn argument can also list one or more of the following flags
262 # to place additional constraints on that option (only one of `?', `+'
263 # and `@' can be given in any SHORT-SPECn):
266 # The option does not take an argument, but when specified on the
267 # command line the `opt_' variable is set to STRING.
268 # ? The option takes an optional argument. Unless the next command
269 # line argument begins with a `-' it will be the value stored in
270 # this option's `opt_' shell variable. Otherwise the `opt_'
271 # variable will contain the INITn value.
272 # ! The option requires an argument. The next command line argument
273 # will be stored in this option's `opt_' shell variable, or else
274 # the INITn value, if any, will be stored if this option is not
275 # given on the command line.
276 # ; The same as `!', except that when the argument is given multiple
277 # times on the command line, each argument is appended to the
278 # `opt_' shell variable, along with a new-line to separate it from
279 # the previous argument.
280 # + The same as `!', except that each time the argument is supplied
281 # on the command line, it's value is stored in an `opt_' variable
282 # with `_n' appended to the variable name - where `n' is `1' for
283 # the first argument, `2, for the second and so on.
284 # @ The option argument must point to an existing file. The
285 # processing loop will automatically contain an additional check
286 # to ensure that the named file exists. `@' can be added to a
287 # SHORT-SPECn argument in addition to any other flags.
288 # ^ The value stored in the `opt_' variable is quoted by passing it
289 # through the shell function `func_quote_for_eval'.
291 # The M4SH_GETOPTS macro is implemented by first delegating to
292 # `_M4SH_GETOPTS', a shift4-loop that repeatedly calls `m4go_options',
293 # shifts away the 4 processed arguments, checks the number of remaining
294 # args and loops again until only 1 argument (VALIDATION) or 0 arguments
295 # (no VALIDATION code) remain. When all the processing is complete, we
296 # expand 'm4go_printopts' to write out the complete command line
297 # processing shell loop.
299 # Generally, you can combine the SHORT-SPECn flags in sensible ways,
300 # but no error checking is done. If you choose a combination that makes
301 # no sense, you'll probably end up with broken shell code.
302 m4_define([M4SH_GETOPTS],
303 [_$0($@)[]m4go_printopts])
304 m4_define([_M4SH_GETOPTS],
306 [$#], 1, [m4_define([m4go_validation],[$1])],
307 [$#], 2, [m4_fatal([$0: too few arguments: $#: $2])],
308 [$#], 3, [m4_fatal([$0: too few arguments: $#: $3])],
309 [m4go_option($@)[]$0(m4_shiftn(4, $@))])])
312 # m4go_option(SHORT-SPEC, LONG-MATCH, DEFAULT, INIT)
313 # --------------------------------------------------
314 # This macro is a wrapper for `_m4go_option', which first extracts the
315 # short option letter (if any) from SHORT-SPEC, and then calculates the
316 # full `opt_' shell variable name for this option before delegating
317 # those results along with all of its own arguments to `_m4go_option'.
319 # Note that when the LONG-MATCH begins with `--no-', we add `~' to the
320 # list of SHORT-SPEC flags before calling `_m4go_option' to denote that
321 # the name of the `opt_' variable is reversed in the sense of the option
322 # name itself.That is, we want to start with the option being true, and
323 # set it to false if `--no-foo' is given on the command line.
325 # `m4_do' is used here to separate out the pushdef and popdef of the
326 # temporary `_short' macro used to held the extracted short option
328 m4_define([m4go_option],
330 [m4_pushdef([_short],
332 [[?!;+@^]], m4_bpatsubst([$1], [[?!;+@^]*], []),
336 [_$0(opt_[]m4_ifval([$2],
337 m4_translit(m4_bpatsubst([$2], [^--\(no-\)?\([^|]+\).*$],
341 [$1]m4_bmatch([$2], [^--no-], [~]),
345 [m4_popdef([_short])])])
348 # _m4go_option(OPTION-NAME, SHORT-OPTION, SHORT-SPEC, LONG-MATCH,
350 #----------------------------------------------------------------
351 # For this option, append the appropriate shell code fragments to:
353 # A shell script fragment containing `opt_' variable
354 # initialisation according to DEFAULT, if necessary;
356 # The case branch to match any SHORT-OPTION or LONG-MATCH command
357 # line option, along with any automatic processing implied by
358 # SHORT-SPEC flags, and additional code from INIT;
360 # This match string accumulates all of the short options that
361 # accept option arguments, so that we can generate some additional
362 # code to split apart compacted option strings (`-xfoo' will be
363 # treated as if `-x foo' had been passed) in `m4go_printopts'.
365 # Similarly, accumulate short options that do not take option
366 # arguments, so that we can generate the code to split apart
367 # compacted options strings in `m4go_printopts' (`-xfoo' will be
368 # treated as if `-x -f -o -o' had been passed).
370 # The core of this macro switches the `m4go_branches' processing to an
371 # appropriate macro depending on what flags are present in SHORT-SPEC.
373 m4_define([_m4go_option],
375 [m4_append([m4go_defaults],
377 [[?!;+@^]], [m4_ifval([$5], [m4_n([$1="$5"])])],
379 [m4_n([$1=false])]))],
380 [m4_append([m4go_branches], [[]dnl (
381 m4_join([|], [$4], m4_ifval([$2], [-$2])))
383 [m4_append([m4go_branches],
384 [m4_bmatch([$3], [[!+@]],
385 [ test [$]# = 0 && func_missing_arg $opt && break
386 ])m4_n(m4_bmatch([$3],
387 [\^], [ func_quote_for_eval "[$]1"
388 optarg="$func_quote_for_eval_result"],
389 [[?!;+@]], [ optarg="[$]1"]))[]dnl
391 [+], [ $1_num=`expr 1 + ${$1_num-0}`
392 eval $1_${$1_num}=\"$optarg\"],
393 [?], [m4_bmatch([$3],
394 [@], [m4go_expand([m4go_optional_file_arg([$1])])],
395 [m4go_expand([m4go_optional_arg([$1])])])],
396 [[!@]], [ $1="$optarg"],
397 [;], [ $1="${$1+[$]$1
400 [=.], [ $1="m4_bpatsubst([$3], [^.*=], [])"],
402 dnl only write the file_arg fragment when we didn't already write opt_file_arg:
403 m4_bmatch([$3], [@], [m4_bmatch([$3], [?], [],
404 [m4go_expand([m4go_file_arg([$1])])
405 ])])m4_n(m4go_unindent([$6]))[]dnl
406 m4_bmatch([$3], [[!+;]], [ shift
411 [[?!;+@^~]], [m4_append([m4go_shortargs], [-$2*], [|])],
412 [m4_append([m4go_shortnoargs], [-$2*],
416 # m4go_optional_arg(OPTION-NAME)
417 # ------------------------------
418 # Expand to the case branch core code for processing a flag that takes
419 # an optional argument, and sets the `opt_' variable named by
420 # OPTION-NAME appropriately.
421 m4_define([m4go_optional_arg],
422 [ if test [$]# -gt 0; then
425 *) $1="$optarg"; shift ;;
429 # m4go_file_arg(OPTION-NAME)
430 # --------------------------
431 # As above, but for flags that require the name of an existing file as
433 m4_define([m4go_file_arg],
434 [ test -r "$optarg" || {
435 func_error "$opt: cannot read file \`$optarg'."
439 # m4go_optional_file_arg(OPTION-NAME)
440 # -----------------------------------
441 # As above, but for options that optionally takes the name of an
442 # existing file as its argument.
443 m4_define([m4go_optional_file_arg],
444 [ if test [$]# -gt 0; then
448 test -r "$optarg" || {
449 func_error "$opt: cannot read file \`$optarg'."
459 # This macro expands to the complete command line option processing
460 # loop, providing for user declared options from `M4SH_GETOPTS' as well
461 # as support for `-x|--debug', `-\?|-h|--help' and `--version'. The
462 # latter two extract their output from a stylized comment at the start
463 # of the script, and will not work correctly if the format is not
464 # followed precisely.
465 m4_define([m4go_printopts],
471 # Parse options once, thoroughly. This comes as soon as possible in the
472 # script to make things like `--version' happen as quickly as we can.
474 # this just eases exit handling
475 while test [$]# -gt 0; do
479 --debug|-x) opt_debug='set -x'
480 func_echo "enabling shell trace mode"
484 -\?|-h) func_usage ;;
486 --version) func_version ;;
488 # Separate optargs to long options:
490 func_split_long_opt "$opt"
491 set dummy "$func_split_long_opt_name" "$func_split_long_opt_arg" ${1+"[$]@"}
495 m4_ifset([m4go_shortargs], dnl (
496 [ # Separate optargs to short options:
498 func_split_short_opt "$opt"
499 set dummy "$func_split_short_opt_name" "$func_split_short_opt_arg" ${1+"[$]@"}
503 ])m4_ifset([m4go_shortnoargs], dnl (
504 [ # Separate non-argument short options:
506 func_split_short_opt "$opt"
507 set dummy "$func_split_short_opt_name" "-$func_split_short_opt_arg" ${1+"[$]@"}
512 -*) func_fatal_help "unrecognized option \`$opt'" ;;
513 *) set dummy "$opt" ${1+"[$]@"}; shift; break ;;
516 m4_ifset([m4go_validation],
521 # Bail if the options were screwed
522 $exit_cmd $EXIT_FAILURE
527 ## ------------------------- ##
528 ## 4. Supporting Shell Code. ##
529 ## ------------------------- ##
531 # The shell functions below are expanded verbatim into the shell script
532 # at `m4_include([getopt.m4sh]', which are necessary for the correct
533 # operation of the automatic `--version' and `--help' options, among
536 m4_divert_pop([KILL])M4SH_VERBATIM([[
538 # Echo version message to standard output and exit.
551 /^# '$PROGRAM' (GNU /,/# warranty; / {
554 s/\((C)\)[ 0-9,-]*\( [1-9][0-9]*\)/\1\2/
561 # Echo short help message to standard output and exit.
566 $SED -n '/^# Usage:/,/^# *.*--help/ {
569 s/\$progname/'$progname'/
573 $ECHO "run \`$progname --help | more' for full usage"
578 # Echo long help message to standard output and exit,
579 # unless 'noexit' is passed as argument.
584 $SED -n '/^# Usage:/,/# Report bugs to/ {
588 s*\$progname*'$progname'*
590 s*\$SHELL*'"$SHELL"'*
592 s*\$LTCFLAGS*'"$LTCFLAGS"'*
594 s/\$with_gnu_ld/'"$with_gnu_ld"'/
595 s/\$automake_version/'"`(${AUTOMAKE-automake} --version) 2>/dev/null |$SED 1q`"'/
596 s/\$autoconf_version/'"`(${AUTOCONF-autoconf} --version) 2>/dev/null |$SED 1q`"'/
600 /^# .* home page:/b print
601 /^# General help using/b print
604 if test -z "$1"; then
609 # func_missing_arg argname
610 # Echo program name prefixed message to standard error and set global
616 func_error "missing argument for $1."
621 # func_split_short_opt shortopt
622 # Set func_split_short_opt_name and func_split_short_opt_arg shell
623 # variables after splitting SHORTOPT after the 2nd character.
624 func_split_short_opt ()
626 my_sed_short_opt='1s/^\(..\).*$/\1/;q'
627 my_sed_short_rest='1s/^..\(.*\)$/\1/;q'
629 func_split_short_opt_name=`$ECHO "$1" | $SED "$my_sed_short_opt"`
630 func_split_short_opt_arg=`$ECHO "$1" | $SED "$my_sed_short_rest"`
631 } # func_split_short_opt may be replaced by extended shell implementation
634 # func_split_long_opt longopt
635 # Set func_split_long_opt_name and func_split_long_opt_arg shell
636 # variables after splitting LONGOPT at the `=' sign.
637 func_split_long_opt ()
639 my_sed_long_opt='1s/^\(--[^=]*\)=.*/\1/;q'
640 my_sed_long_arg='1s/^--[^=]*=//'
642 func_split_long_opt_name=`$ECHO "$1" | $SED "$my_sed_long_opt"`
643 func_split_long_opt_arg=`$ECHO "$1" | $SED "$my_sed_long_arg"`
644 } # func_split_long_opt may be replaced by extended shell implementation