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