aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2018-11-27 15:05:18 +0200
committerSergey Poznyakoff <gray@gnu.org>2018-12-07 20:27:03 +0200
commit9097d5294cf62c4cb7cd5e352e1d0c42d3c04d81 (patch)
tree4e40c0af4dae891f38cfc466edcc7e0a3331b267
parent1202780fd92ff9fc2854bfa2d0ab0c39169eabd5 (diff)
downloadgrecs-9097d5294cf62c4cb7cd5e352e1d0c42d3c04d81.tar.gz
grecs-9097d5294cf62c4cb7cd5e352e1d0c42d3c04d81.tar.bz2
Revise getopt.m4
-rw-r--r--build-aux/getopt.m4240
1 files changed, 177 insertions, 63 deletions
diff --git a/build-aux/getopt.m4 b/build-aux/getopt.m4
index 93d81df..85e930e 100644
--- a/build-aux/getopt.m4
+++ b/build-aux/getopt.m4
@@ -1,5 +1,5 @@
1dnl This file is part of grecs 1dnl This file is part of grecs
2dnl Copyright (C) 2007-2016 Sergey Poznyakoff 2dnl Copyright (C) 2007-2018 Sergey Poznyakoff
3dnl 3dnl
4dnl Grecs is free software; you can redistribute it and/or modify 4dnl Grecs is free software; you can redistribute it and/or modify
5dnl it under the terms of the GNU General Public License as published by 5dnl it under the terms of the GNU General Public License as published by
@@ -17,6 +17,27 @@ divert(-1)
17changequote([<,>]) 17changequote([<,>])
18changecom(/*,*/) 18changecom(/*,*/)
19 19
20dnl Diversion channels
21define([<__DIV_OPTKEYS__>],1)dnl option keys
22define([<__DIV_OPTDECL__>],2)dnl struct option declarations
23define([<__DIV_HELPDECL__>],3)dnl grecs_opthelp declarations
24define([<__DIV_SWITCH__>],4)dnl switch statement body
25define([<__DIV_STATIC__>],5)dnl module static data
26
27dnl Special macros that can be set during expansion
28pushdef([<__GETOPT_DEFINED__>])
29pushdef([<__OPTIONS_END_DEFINED__>])
30pushdef([<__GETOPT_LASTOPT_LABEL__>])
31
32define([<__GETOPT_LONG_TAG>])
33define([<__GETOPT_SHORT_TAG>])
34
35define([<WITH_DIVERSION>],[<ifelse([<$#>],0,[<[<$0>]>],[<dnl
36pushdef([<__GETOPT_DIVERSION__>],divnum)dnl
37divert([<$1>])$2dnl
38divert(__GETOPT_DIVERSION__)dnl
39popdef([<__GETOPT_DIVERSION__>])>])>])
40
20dnl _getopt_mangle_option(NAME) 41dnl _getopt_mangle_option(NAME)
21dnl --------------------------- 42dnl ---------------------------
22dnl Convert NAME to a valid m4 identifier, by replacing invalid characters 43dnl Convert NAME to a valid m4 identifier, by replacing invalid characters
@@ -33,7 +54,7 @@ define([<_getopt_set_option>],
33 [<_getopt_mangle_option(\1),[<\2>]>]))>])>]) 54 [<_getopt_mangle_option(\1),[<\2>]>]))>])>])
34 55
35dnl _getopt_get_option(NAME[,DEFAULT]) 56dnl _getopt_get_option(NAME[,DEFAULT])
36dnl ------------------------------ 57dnl ----------------------------------
37define([<_getopt_get_option>], 58define([<_getopt_get_option>],
38 [<_getopt_if_option_set($1,[<indir(_getopt_mangle_option($1))>],[<$2>])>]) 59 [<_getopt_if_option_set($1,[<indir(_getopt_mangle_option($1))>],[<$2>])>])
39 60
@@ -44,7 +65,7 @@ define([<_getopt_if_option_set>],
44 [<ifdef(_getopt_mangle_option([<$1>]),[<$2>],[<$3>])>]) 65 [<ifdef(_getopt_mangle_option([<$1>]),[<$2>],[<$3>])>])
45 66
46dnl _getopt_if_option_null(NAME,IF-NULL,IF-NOT-NULL) 67dnl _getopt_if_option_null(NAME,IF-NULL,IF-NOT-NULL)
47dnl --------------------------------------------- 68dnl ------------------------------------------------
48dnl Check if option NAME is set. 69dnl Check if option NAME is set.
49define([<_getopt_if_option_null>], 70define([<_getopt_if_option_null>],
50 [<ifelse(indir(_getopt_mangle_option([<$1>])),,[<$2>],[<$3>])>]) 71 [<ifelse(indir(_getopt_mangle_option([<$1>])),,[<$2>],[<$3>])>])
@@ -61,7 +82,7 @@ define([<_getopt_option_switch>],
61 [<_getopt_option_switch(shift(shift($@)))>])>])>]) 82 [<_getopt_option_switch(shift(shift($@)))>])>])>])
62 83
63dnl _getopt_if_option_val(NAME,val,IF-TRUE,IF-FALSE) 84dnl _getopt_if_option_val(NAME,val,IF-TRUE,IF-FALSE)
64dnl --------------------------------------------- 85dnl ------------------------------------------------
65dnl Check if option NAME is set. 86dnl Check if option NAME is set.
66define([<_getopt_if_option_val>], 87define([<_getopt_if_option_val>],
67 [<ifelse(_getopt_get_option([<$1>]),[<$2>],[<$3>],[<$4>])>]) 88 [<ifelse(_getopt_get_option([<$1>]),[<$2>],[<$3>],[<$4>])>])
@@ -89,34 +110,39 @@ define([<_getopt_set_options>],
89 _getopt_set_options(shift($@))>])>]) 110 _getopt_set_options(shift($@))>])>])
90 111
91dnl __getopt_format_authors(name[,name...]) 112dnl __getopt_format_authors(name[,name...])
92dnl ------------------------------ 113dnl ---------------------------------------
93define([<__getopt_format_authors>],dnl 114define([<__getopt_format_authors>],dnl
94 [<ifelse([<$1>],,NULL,[<"$1", 115 [<ifelse([<$1>],,NULL,[<"$1",
95__getopt_format_authors(shift($@))>])>]) 116__getopt_format_authors(shift($@))>])>])
96 117
97dnl __getopt_upcase(ARGS...) 118dnl __getopt_upcase(ARGS...)
119dnl ------------------------
98dnl Concatenate and convert ARGS to upper case. 120dnl Concatenate and convert ARGS to upper case.
99dnl 121dnl
100define([<__getopt_upcase>], [<translit([<$*>], [<a-z>], [<A-Z>])>]) 122define([<__getopt_upcase>], [<translit([<$*>], [<a-z>], [<A-Z>])>])
101 123
102dnl __getopt_concat(ARGS...) 124dnl __getopt_concat(ARGS...)
125dnl ------------------------
103dnl Concatenate arguments, inserting ", " between each pair of them. 126dnl Concatenate arguments, inserting ", " between each pair of them.
104dnl 127dnl
105define([<__getopt_concat>],[<ifelse([<$#>],1,[<$1>],[<$1, __getopt_concat(shift($@))>])>]) 128define([<__getopt_concat>],[<ifelse([<$#>],1,[<$1>],[<$1, __getopt_concat(shift($@))>])>])
106 129
107dnl __getopt_flushleft(ARGS...) 130dnl __getopt_flushleft(ARGS...)
131dnl ---------------------------
108dnl Concatenate ARGS and remove any leading whitespace 132dnl Concatenate ARGS and remove any leading whitespace
109dnl 133dnl
110define([<__getopt_flushleft>], 134define([<__getopt_flushleft>],
111 [<patsubst([<__getopt_concat($*)>], [<^[ ]+>])>]) 135 [<patsubst([<__getopt_concat($*)>], [<^[ ]+>])>])
112 136
113dnl __getopt_chop(ARGS...) 137dnl __getopt_chop(ARGS...)
138dnl ----------------------
114dnl Concatenate ARGS and remove any trailing whitespace 139dnl Concatenate ARGS and remove any trailing whitespace
115dnl 140dnl
116define([<__getopt_chop>], 141define([<__getopt_chop>],
117 [<patsubst([<$*>], [<[ ]+$>])>]) 142 [<patsubst([<$*>], [<[ ]+$>])>])
118 143
119dnl __getopt_escape(ARGS...) 144dnl __getopt_escape(ARGS...)
145dnl ------------------------
120dnl Concatenate ARGS and escape any occurrences of double-quotes with 146dnl Concatenate ARGS and escape any occurrences of double-quotes with
121dnl backslashes. 147dnl backslashes.
122dnl 148dnl
@@ -124,6 +150,7 @@ define([<__getopt_escape>],
124[<patsubst([<__getopt_concat($*)>],[<[\"]>],[<\\\&>])>]) 150[<patsubst([<__getopt_concat($*)>],[<[\"]>],[<\\\&>])>])
125 151
126dnl __getopt_prep(ARG) 152dnl __getopt_prep(ARG)
153dnl ------------------
127dnl Prepare ARG for including in C strings: replace newlines and any 154dnl Prepare ARG for including in C strings: replace newlines and any
128dnl preceding and following whitespace by a single space character, remove 155dnl preceding and following whitespace by a single space character, remove
129dnl leading whitespace, and escape double-quotes. 156dnl leading whitespace, and escape double-quotes.
@@ -133,46 +160,56 @@ define([<__getopt_prep>],
133+[ ]*>],[< >])))>]) 160+[ ]*>],[< >])))>])
134 161
135dnl __GETOPT_SHORT_OPTS 162dnl __GETOPT_SHORT_OPTS
163dnl -------------------
136dnl Accumulator for the 3rd argument of getopt_long 164dnl Accumulator for the 3rd argument of getopt_long
137dnl 165dnl
138define([<__GETOPT_SHORT_OPTS>],[<_getopt_if_option_set([<nopermute>],+)>]) 166define([<__GETOPT_SHORT_OPTS>],[<_getopt_if_option_set([<nopermute>],+)>])
139 167
140dnl GROUP(STRING) 168dnl GROUP(STRING)
169dnl -------------
141dnl Begin a named group of options 170dnl Begin a named group of options
142dnl 171dnl
143define([<GROUP>],[<dnl 172define([<GROUP>],[<ifelse([<$#>],0,[<[<$0>]>],[<dnl
144divert(3) 173WITH_DIVERSION(__DIV_HELPDECL__,[<dnl
145 { NULL, NULL, 0, N_("__getopt_prep([<$1>])") }, 174 { NULL, NULL, 0, N_("__getopt_prep([<$1>])") },
146divert(-1)>]) 175>])>])>])
147 176
148# __getopt_quote(args) - convert args to single-quoted string 177# __getopt_quote(args) - convert args to single-quoted string
149define([<__getopt_quote>], [<ifelse([<$#>], [<0>], [<>], [<[<$*>]>])>]) 178define([<__getopt_quote>], [<ifelse([<$*>], [<>], [<>], [<[<$*>]>])>])
150define([<__getopt_dquote>], [<[<$@>]>]) 179define([<__getopt_dquote>], [<ifelse([<$*>], [<>], [<>], [<[<$@>]>])>])
180
181# __getopt_ifempty(LIST, IF-TRUE [, IF-FALSE])
182# --------------------------------------------
183# Expands to IF-TRUE if LIST is not empty, otherwise to IF-FALSE.
184#
185define([<__getopt_ifempty>], [<ifelse([<$1>],[<>],[<$2>],[<$3>])>])
151 186
152define([<__GATHER_OPTIONS>],[< 187define([<__GATHER_OPTIONS>],[<
153pushdef([<__GETOPT_KEY>],ifelse([<$2>],,[<OPTION_>]__getopt_upcase(patsubst($1,-,_)),'$2')) 188pushdef([<__GETOPT_KEY>],ifelse([<$2>],,[<OPTION_>]__getopt_upcase(patsubst($1,-,_)),'$2'))
154ifelse([<$2>],,[< 189ifelse([<$2>],,[<
155divert(1) 190WITH_DIVERSION(__DIV_OPTKEYS__,[<dnl
156 __GETOPT_KEY, 191 __GETOPT_KEY,
157divert(-1) 192>])>])
158>])
159define([<__GETOPT_SELECTOR>],ifdef([<__GETOPT_SELECTOR>],__GETOPT_SELECTOR) case __GETOPT_KEY:) 193define([<__GETOPT_SELECTOR>],ifdef([<__GETOPT_SELECTOR>],__GETOPT_SELECTOR) case __GETOPT_KEY:)
160ifelse([<$1>],,,[< 194ifelse([<$1>],,,[<
161divert(2) 195WITH_DIVERSION(__DIV_OPTDECL__,[<dnl
162 { "$1", __GETOPT_ARGTYPE, 0, __GETOPT_KEY }, 196 { "$1", __GETOPT_ARGTYPE, 0, __GETOPT_KEY },
163divert(-1)>]) 197>])>])
164dnl 198dnl
165define([<__GETOPT_SHORT_OPTS>],__getopt_dquote(__GETOPT_SHORT_OPTS[<>]dnl 199define([<__GETOPT_SHORT_OPTS>],__getopt_dquote(__GETOPT_SHORT_OPTS[<>]dnl
166ifelse([<$2>],,,[<$2>]ifelse(__GETOPT_ARGTYPE,[<no_argument>],,__GETOPT_ARGTYPE,[<required_argument>],:,__GETOPT_ARGTYPE,[<optional_argument>],::)))) 200ifelse([<$2>],,,[<$2>]ifelse(__GETOPT_ARGTYPE,[<no_argument>],,__GETOPT_ARGTYPE,[<required_argument>],:,__GETOPT_ARGTYPE,[<optional_argument>],::))))
167dnl 201dnl
168ifelse([<$1>],,,dnl 202ifelse([<$1>],,,dnl
169[<define([<__GETOPT_LONG_TAG>],ifelse(__GETOPT_LONG_TAG,,[<--$1>],[<__GETOPT_LONG_TAG; --$1>]))>]) 203[<define([<__GETOPT_LONG_TAG>],
204__getopt_ifempty(__getopt_quote(__GETOPT_LONG_TAG),__getopt_dquote(__getopt_dquote(--$1)),[<__getopt_quote(__getopt_dquote(__GETOPT_LONG_TAG), __getopt_dquote([< --$1>]))>]))>])
170ifelse([<$2>],,,dnl 205ifelse([<$2>],,,dnl
171[<define([<__GETOPT_SHORT_TAG>],ifelse(__GETOPT_SHORT_TAG,,[<-$2>],[<__GETOPT_SHORT_TAG; -$2>]))>]) 206[<define([<__GETOPT_SHORT_TAG>],
207__getopt_ifempty(__getopt_quote(__GETOPT_SHORT_TAG),__getopt_dquote(__getopt_dquote(-$2)),[<__getopt_quote(__getopt_dquote(__GETOPT_SHORT_TAG), __getopt_dquote([< -$2>]))>]))>])
172popdef([<__GETOPT_KEY>]) 208popdef([<__GETOPT_KEY>])
173>]) 209>])
174 210
175dnl OPTION(long-opt, short-opt, [arg], [descr]) 211dnl OPTION(long-opt, short-opt, [arg], [descr])
212dnl -------------------------------------------
176dnl Introduce a command line option. Arguments: 213dnl Introduce a command line option. Arguments:
177dnl long-opt Long option. 214dnl long-opt Long option.
178dnl short-opt Short option (a single char) 215dnl short-opt Short option (a single char)
@@ -189,7 +226,7 @@ dnl
189dnl