"Fossies" - the Fresh Open Source Software Archive

Member "argbash-2.10.0/src/utilities.m4" (22 Sep 2020, 11822 Bytes) of package /linux/privat/argbash-2.10.0.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "utilities.m4": 2.9.0_vs_2.10.0.

    1 
    2 m4_set_delete([__FILES_ALREADY_INCLUDED__])
    3 m4_set_add([__FILES_ALREADY_INCLUDED__], __file__)
    4 dnl
    5 dnl $1: The filename to include
    6 m4_define([m4_include_once], [m4_do(
    7 	[m4_set_contains([__FILES_ALREADY_INCLUDED__], [$1], [], 
    8 		[m4_set_add([__FILES_ALREADY_INCLUDED__], [$1])m4_include([$1])])],
    9 )])
   10 
   11 
   12 m4_include_once([list.m4])
   13 
   14 dnl
   15 dnl $1: nargs
   16 dnl $2: index
   17 dnl $3: actual value
   18 dnl $4: default
   19 m4_define([_DEFAULT_IF_NARGS_GREATER_THAN], [m4_do(
   20 	[dnl                                  too little args        enough args
   21 ],
   22 	[m4_if(m4_eval([$1 <= $2]), 1, [m4_default_quoted([$3], [$4])], [$3])],
   23 )])
   24 
   25 m4_define([_ENDL_],
   26 	[m4_for(_, 1, m4_default([$1], 1), 1, [
   27 ])])
   28 
   29 m4_define([_IF_DIY_MODE],
   30 	[m4_if(_DIY_MODE, 1, [$1], [$2])])
   31 
   32 
   33 m4_define([_IF_HAVE_POSITIONAL_ARGS],
   34 	[m4_if(HAVE_POSITIONAL, 1, [$1], [$2])])
   35 
   36 
   37 m4_define([_IF_SOME_POSITIONAL_VALUES_ARE_EXPECTED],
   38 	[m4_if(_MINIMAL_POSITIONAL_VALUES_COUNT, 0, [$2], [$1])])
   39 
   40 
   41 m4_define([_IF_HAVE_OPTIONAL_ARGS],
   42 	[m4_if(HAVE_OPTIONAL, 1, [$1], [$2])])
   43 
   44 
   45 dnl
   46 dnl Get the last component of a filename
   47 m4_define([_GET_BASENAME],
   48 	[m4_bpatsubst([$1], [.*/\([^/]+\)], [\1])])
   49 
   50 
   51 dnl
   52 dnl The helper macro for _CHECK_INTEGER_TYPE
   53 dnl $1: The caller name
   54 dnl $2: The arg position
   55 dnl $3: The arg value
   56 dnl $4: The error message (optional)
   57 m4_define([__CHECK_INTEGER_TYPE], [[m4_do(
   58 	[m4_bmatch([$2], [^[0-9]+$], ,
   59 		[m4_fatal([The ]m4_case([$3], 1, 1st, 2, 2nd, 3, 3rd, $3th)[ argument of '$1' has to be a number]m4_ifnblank([$4], [[ ($4)]])[, got '$2'])])],
   60 )]])
   61 
   62 
   63 dnl
   64 dnl Checks that the n-th argument is an integer.
   65 dnl Should be called upon the macro definition outside of quotes, e.g. m4_define([FOO], _CHECK_INTEGER_TYPE(1)[m4_eval(2 + $1)])
   66 dnl $1: The argument number
   67 dnl $2: The error message (optional)
   68 m4_define([_CHECK_INTEGER_TYPE],
   69 	[__CHECK_INTEGER_TYPE([$][0], m4_quote($][$1), [$1], m4_quote($][2))])
   70 
   71 
   72 dnl
   73 dnl If first char of string is not ' or " enclose it into ""
   74 dnl and escape " with \".
   75 dnl
   76 dnl The string is also []-quoted
   77 dnl Property: Quoting a blank input results in blank result
   78 dnl to AVOID it, pass string like ""ls -l or "ls" -l
   79 dnl
   80 dnl $1: String to quote
   81 m4_define([_sh_quote], [m4_do(
   82 	[m4_if(
   83 		[$1], , ,
   84 		m4_dquote(_sh_quote_also_blanks([$1])))],
   85 )])
   86 
   87 
   88 dnl
   89 dnl Same as _sh_quote, except quoting a blank input results in pair of quotes
   90 dnl $1: String to quote
   91 m4_define([_sh_quote_also_blanks], [m4_do(
   92 	[m4_if(
   93 		m4_index([$1], [']), 0, [[$1]],
   94 		m4_index([$1], ["]), 0, [[$1]],
   95 		[["$1"]])],
   96 )])
   97 
   98 dnl
   99 dnl Define a macro that is part of the public API
  100 dnl Ensure the replication and also add the macro name to a list of allowed macros
  101 m4_define([argbash_api], [_argbash_persistent([$1], [$2])])
  102 m4_define([_argbash_persistent], [m4_set_add([_KNOWN_MACROS],[$1])m4_define([$1], [$2])])
  103 
  104 m4_define([argbash_arg_api], [m4_do(
  105 	[_argbash_api([$1], [_CHECK_PASSED_ARGS_COUNT([$2], [$3])[[m4_do(
  106 		[_CHECK_ARGUMENT_NAME_IS_VALID([$1])],
  107 		[m4_list_contains([BLACKLIST], m4_quote($][1), , m4_dquote([$1($][@)])$4)],
  108 	)]])]],
  109 )])
  110 
  111 dnl
  112 dnl $1: this comm block ID
  113 dnl $2: where it is defined
  114 dnl $3: indentation
  115 dnl $4, ....: comment lines
  116 dnl
  117 dnl If the comment ID has been defined earlier, don't display the comment, but point to the definition.
  118 dnl Otherwise, act like _COMM_BLOCK
  119 m4_define([_POSSIBLY_REPEATED_COMMENT_BLOCK], [m4_ifndef([_COMMENT_$1_LOCATION], [m4_do(
  120 	[m4_define([_COMMENT_$1_LOCATION], [[$2]])],
  121 	[_COMM_BLOCK($3, m4_shiftn(3, $@))],
  122 )], [m4_do(m4_ifblank([$2], ,
  123 		[_COMM_BLOCK([$3], m4_quote([# ]m4_indir([_COMMENT_$1_LOCATION])))]),
  124 )])])
  125 
  126 
  127 m4_define([_COMMENT_PREFIX_NOTHING], [[$1],])
  128 m4_define([_COMMENT_PREFIX_HASH], [[# $1],])
  129 
  130 
  131 m4_define([_COMM_BLOCK], [__COMM_BLOCK([_COMMENT_PREFIX_NOTHING], $@)])
  132 m4_define([_COMM_BLOCK_HASH], [__COMM_BLOCK([_COMMENT_PREFIX_HASH], $@)])
  133 
  134 m4_define([_IF_COMMENTED_OUTPUT], [m4_ifdef([COMMENT_OUTPUT], [[$1]], [[$2]])])
  135 m4_define([__COMM_BLOCK], _CHECK_INTEGER_TYPE(2, [depth of indentation])[m4_ifdef([COMMENT_OUTPUT], [_JOIN_INDENTED([$2], m4_map_args([$1], m4_shiftn(2, m4_dquote_elt($@))))])])
  136 m4_define([_COMMENT_CHAIN], [m4_ifdef([COMMENT_OUTPUT], [$@])])
  137 m4_define([_COMMENT], [m4_ifdef([COMMENT_OUTPUT], [$1])])
  138 
  139 
  140 dnl
  141 dnl $1: The text to substitute
  142 dnl The indentation is a display indentation - not source code one.
  143 m4_define([_SUBSTITUTE_LF_FOR_NEWLINE_WITH_DISPLAY_INDENT_AND_ESCAPE_DOUBLEQUOTES],
  144 	[SUBSTITUTE_LF_FOR_NEWLINE_WITH_INDENT_AND_ESCAPE_DOUBLEQUOTES([$1], [		])])
  145 
  146 
  147 dnl
  148 dnl $1: The text to substitute
  149 dnl $2: The width of space indentation
  150 dnl The indentation is a display indentation - not source code one.
  151 m4_define([_SUBSTITUTE_LF_FOR_NEWLINE_WITH_SPACE_INDENT_AND_ESCAPE_DOUBLEQUOTES],
  152 	[SUBSTITUTE_LF_FOR_NEWLINE_WITH_INDENT_AND_ESCAPE_DOUBLEQUOTES([$1], m4_if([$2], 0, [], [m4_for(_, 1, [$2], 1, [ ])]))])
  153 
  154 
  155 dnl
  156 dnl $1: The text to substitute
  157 dnl $2: The indent for the new line.
  158 dnl Regexp: Find beginning of backslashes, match for pairs, and if \\n is left, then substitute it for literal newline.
  159 dnl The indentation is a display indentation - not source code one.
  160 m4_define([SUBSTITUTE_LF_FOR_NEWLINE_WITH_INDENT_AND_ESCAPE_DOUBLEQUOTES],
  161 	[m4_bpatsubsts([[$1]], 
  162 		[\([^\\]\)\(\\\\\)*\\n], m4_expand([[\1\2]_ENDL_()$2]),
  163 		[\([^\]\)"], [\1\\"])])
  164 
  165 
  166 m4_define([_CHECK_PASSED_ARGS_COUNT_TOO_FEW],
  167 	[m4_fatal([You have passed $2 arguments to macro $1, while it requires at least $3.]m4_ifnblank([$4], [ Call it like: $4]))])
  168 
  169 
  170 m4_define([_CHECK_PASSED_ARGS_COUNT_TOO_MANY],
  171 	[m4_fatal([You have passed $2 arguments to macro $1, while it accepts at most $3.]m4_ifnblank([$4], [ Call it like: $4]))])
  172 
  173 dnl
  174 dnl $1: Name of the macro
  175 dnl $2: The actual argc
  176 dnl $3: argc lower bound
  177 dnl $4: argc upper bound
  178 dnl $5: The calling signature
  179 m4_define([__CHECK_PASSED_ARGS_COUNT], [[m4_do(
  180 	[m4_pushdef([_maybe_signature_$1], [m4_ifnblank([$5], [[$1($5)]])])],
  181 	[m4_if(
  182 		m4_eval($2 < $3), 1, [_CHECK_PASSED_ARGS_COUNT_TOO_FEW([$1], [$2], [$3], m4_quote(m4_indir([_maybe_signature_$1])))],
  183 		m4_eval($2 > $4), 1, [_CHECK_PASSED_ARGS_COUNT_TOO_MANY([$1], [$2], [$4], m4_quote(m4_indir([_maybe_signature_$1])))],
  184 	)],
  185 	[m4_popdef([_maybe_signature_$1])],
  186 )]])
  187 
  188 
  189 dnl Check thath the correct number of arguments has been passed, and display the calling signature if it is not the case
  190 dnl $1: The minimal amount of args > 0 (due to m4's $# behaior)
  191 dnl $2: The highest possible arguments count (optional, defaults to no upper bound behavior)
  192 dnl $3: The arguments part of the calling signature (optional)
  193 m4_define([_CHECK_PASSED_ARGS_COUNT], m4_if([$1], 0, [m4_fatal([The minimal amount of args must be non-negative.])])[__CHECK_PASSED_ARGS_COUNT([$]0, $[#], [$1], m4_default([$2], [$[#]]), [$3])])
  194 
  195 
  196 dnl
  197 dnl
  198 dnl Blank args to this macro are totally ignored, use @&t@ to get over that --- @&t@ is a quadrigraph that expands to nothing in the later phase
  199 dnl $1: How many indents
  200 dnl $2, $3, ...: What to put there
  201 m4_define([_JOIN_INDENTED], _CHECK_INTEGER_TYPE(1, [depth of indentation])[m4_do(
  202 	[m4_pushdef([_current_indentation_level], [$1])],
  203 	[m4_foreach([line], [m4_shift($@)], [m4_ifnblank(m4_quote(line), _INDENT_([$1])[]m4_dquote(line)
  204 )])],
  205 	[m4_popdef([_current_indentation_level])],
  206 )])
  207 
  208 
  209 dnl
  210 dnl $1, $2, ...: What to put there
  211 dnl
  212 dnl Takes arguments, returns them, but there is an extra _INDENT_() in the beginning of them
  213 m4_define([_INDENT_MORE], [m4_do(
  214 	[m4_list_ifempty([_TLIST], , [m4_fatal([Internal error: List '_TLIST' should be empty, contains ]m4_list_contents([_TLIST])[ instead])])],
  215 	[m4_foreach([line], [$@], [m4_list_append([_TLIST], m4_expand([_INDENT_()line]))])],
  216 	[m4_list_contents([_TLIST])],
  217 	[m4_list_destroy([_TLIST])],
  218 )])
  219 
  220 
  221 m4_define([_SET_INDENT], [__SET_INDENT([$1], $[]1)])
  222 
  223 
  224 dnl
  225 dnl defines _INDENT_
  226 dnl $1: How many times to indent (default 1)
  227 dnl $2, ...: Ignored, but you can use those to make the code look somewhat better.
  228 m4_define([__SET_INDENT], [m4_define([_INDENT_], [m4_if([$2], 0, ,
  229 	[m4_for(_, 1, m4_default([$2], 1), 1,
  230 		[[$1]])])])])
  231 
  232 
  233 dnl
  234 dnl Sets the indentation character(s) in the parsing code
  235 dnl $1: The indentation character(s)
  236 argbash_api([ARGBASH_SET_INDENT],
  237 	[m4_bmatch(m4_expand([_W_FLAGS]), [I], ,[[$0($@)]_SET_INDENT([$1])])])
  238 
  239 
  240 m4_define([m4_lists_foreach_optional], [m4_lists_foreach([$1][,_ARGS_POS_OR_OPT], [$2][,_arg_pos_or_opt], [m4_if(_arg_pos_or_opt, optional, [$3])])])
  241 m4_define([m4_lists_foreach_positional], [m4_lists_foreach([$1][,_ARGS_POS_OR_OPT], [$2][,_arg_pos_or_opt], [m4_if(_arg_pos_or_opt, positional, [$3])])])
  242 
  243 
  244 m4_define([_ASSIGN_VALUE_TO_VAR], [[$3=]_MAYBE_VALIDATE_VALUE([$1], [$2])_IF_ARG_IS_TYPED([$1], [ || exit 1])])
  245 m4_define([_APPEND_VALUE_TO_ARRAY], [[$3+=](_MAYBE_VALIDATE_VALUE([$1], [$2]))_IF_ARG_IS_TYPED([$1], [ || exit 1])])
  246 dnl m4_define([_ASSIGN_VALUE_TO_VAR], [[$2="$1"]])
  247 dnl m4_define([_APPEND_VALUE_TO_ARRAY], [[$2+=("$1")]])
  248 
  249 
  250 dnl Do something depending on whether there is already infinitely many args possible or not
  251 m4_define([IF_POSITIONALS_INF],
  252 	[m4_if(m4_quote(_POSITIONALS_INF), 1, [$1], [$2])])
  253 
  254 
  255 dnl Do something depending on whether there have been optional positional args declared beforehand or not
  256 m4_define([IF_VARIABLE_NUMBER_OF_ARGUMENTS_BEFOREHAND],
  257 	[m4_if(m4_quote(HAVE_POSITIONAL_VARNUM), 1, [$1], [$2])])
  258 
  259 
  260 dnl
  261 dnl Output some text depending on what strict mode we find ourselves in
  262 m4_define([_CASE_RESTRICT_VALUES], [m4_case(_RESTRICT_VALUES,
  263 	[none], [$1],
  264 	[no-local-options], [$2],
  265 	[no-any-options], [$3])])
  266 
  267 
  268 dnl
  269 dnl A very private macro --- return name of the macro containing description for the given type ID
  270 dnl $1: Type ID
  271 m4_define([__type_str], [[_type_str_$1]])
  272 
  273 dnl
  274 dnl Return type description for the given argname
  275 dnl $1: Argument ID
  276 m4_define([_GET_VALUE_DESC], [m4_expand(__type_str(_GET_VALUE_TYPE([$1])))])
  277 
  278 dnl
  279 dnl Given an argname, return the argument group name (i.e. type string) or 'arg'
  280 dnl
  281 dnl $1: argname
  282 m4_define([_GET_VALUE_STR], [m4_do(
  283 	[m4_ifdef([$1_VAL_GROUP], [m4_indir([$1_VAL_GROUP])], [arg])],
  284 )])
  285 
  286 
  287 m4_define([DEFINE_MINIMAL_POSITIONAL_VALUES_COUNT],
  288 	[m4_if(m4_cmp(0, m4_list_len([_POSITIONALS_MINS])), 1,
  289 		m4_define([_MINIMAL_POSITIONAL_VALUES_COUNT], [m4_list_sum(_POSITIONALS_MINS)]))])
  290 
  291 
  292 dnl $1: Error
  293 m4_define([INFERRED_BASENAME],
  294 	[m4_ifdef([OUTPUT_BASENAME], [_STRIP_SUFFIX(OUTPUT_BASENAME)],
  295 		[m4_ifdef([INPUT_BASENAME], [[_STRIP_SUFFIX(INPUT_BASENAME)]], [$1])])])
  296 
  297 
  298 m4_define([INFERRED_BASENAME_NOERROR],
  299 	[INFERRED_BASENAME([m4_errprintn([We need to know the basename, and we couldn't infer it, so we resort to generic 'script'. It is likely that you read from stdin and write to stdout, please prefer to use at least one filename either for input or for output.])[[script]]])])
  300 
  301 
  302 dnl
  303 dnl \1: The name and leading [
  304 dnl \2: The ending ] (group regexp is complicated because square brackets have to match. For reference, [][]* matches for zero or more [ or ])
  305 m4_define([_STRIP_SUFFIX], [m4_bpatsubst([[$1]], [\(.*\)\.m4\([][]*\)$], [\1\2])])
  306 
  307 
  308 dnl
  309 dnl $1: List name
  310 m4_define([_LIST_LONGEST_TEXT_LENGTH], [m4_do(
  311 	[m4_pushdef([_longest_label_len], 0)],
  312 	[m4_list_foreach([$1], [_item], [m4_if(m4_eval(_longest_label_len < m4_len(_item)), 1, [m4_define([_longest_label_len], m4_len(_item))])])],
  313 	[_longest_label_len],
  314 	[m4_popdef([_longest_label_len])],
  315 )])
  316 
  317 
  318 m4_define([_CAPITALIZE], [m4_translit([[$1]], [a-z], [A-Z])])
  319 
  320 
  321 dnl
  322 dnl $1: The message - will be double-quoted
  323 dnl $2: The return code (default = 1)
  324 m4_define([_INLINE_DIE_BLOCK], [m4_do(
  325 	[[{ echo "$1" >&2; exit ]m4_default_quoted([$2], 1); }],
  326 )])
  327 
  328 
  329 dnl
  330 dnl $1: What to underline
  331 dnl $2: By what to underline
  332 dnl $3: By what to overline (optional)
  333 m4_define([UNDERLINE], [m4_do(
  334 	[m4_if(m4_len([$1]), 0, , [m4_if([$3], , , [m4_do(
  335 		[m4_for(idx, 1, m4_len([$1]), 1, [$3])],
  336 		[_ENDL_()],
  337 	)])])],
  338 	[[$1]],
  339 	[_ENDL_()],
  340 	[m4_if(m4_len([$1]), 0, ,
  341 		[m4_for(idx, 1, m4_len([$1]), 1, [$2])])],
  342 )])