"Fossies" - the Fresh Open Source Software Archive

Member "argbash-2.10.0/src/list.m4" (22 Sep 2020, 6258 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 "list.m4": 2.9.0_vs_2.10.0.

    1 dnl
    2 dnl $1: List name
    3 dnl $2: Variable name
    4 dnl $3: Foreach body
    5 m4_define([m4_list_foreach], [m4_list_ifempty([$1], ,
    6 	[m4_foreach([$2], m4_quote(m4_dquote_elt(m4_list_contents([$1]))), [$3])])])
    7 
    8 
    9 dnl $1: List names
   10 dnl $2: If yes
   11 dnl $3: If not
   12 m4_define([_lists_same_len], [m4_if(m4_count($1), 1, [$2],
   13 	[m4_if(m4_list_len(m4_car($1)), m4_list_len(m4_argn(2, $1)),
   14 		[_lists_same_len(m4_expand([m4_shift($1)]), [$2], [$3])], [$3])])])
   15 
   16 
   17 dnl
   18 dnl $1: List of list names
   19 dnl $2: List of var names
   20 dnl $3: What
   21 m4_define([m4_lists_foreach], [_lists_same_len([$1],
   22 	[m4_do(
   23 		[m4_pushdef([_varnames], m4_quote(m4_dquote_elt($2)))],
   24 		[dnl How long is the first list
   25 ],
   26 		[m4_pushdef([l_len], [m4_list_len(m4_car($1))])],
   27 		[dnl How many of lists they are (on input)
   28 ],
   29 		[m4_pushdef([l_count], [m4_count($1)])],
   30 		[m4_if(l_count, 0, ,
   31 			[m4_if(l_len, 0, , [m4_for([_idx_item], 1, l_len, 1, [m4_do(
   32 				[dnl Go through all varnames and push the corresponding list value into it
   33 ],
   34 				[m4_for([_idx_list], 1, l_count, 1, [m4_do(
   35 					[m4_pushdef(m4_argn(_idx_list, _varnames), m4_dquote(m4_list_nth(m4_argn(_idx_list, $1), _idx_item)))],
   36 				)])],
   37 				[$3],
   38 				[dnl After we do the loop body, pop all of the definitions
   39 ],
   40 				[m4_for([_idx_list], 1, l_count, 1, [m4_do(
   41 					[m4_popdef(m4_argn(_idx_list, _varnames))],
   42 				)])],
   43 		)])])])],
   44 		[m4_popdef([l_count])],
   45 		[m4_popdef([l_len])],
   46 		[m4_popdef([_varnames])],
   47 	)], [m4_fatal([Lists $1 don't have the same length.])])])
   48 
   49 dnl
   50 dnl $1: The list's ID
   51 dnl $2, ... Items to be appended: DON'T QUOTE items too much before you add them, quotes will be escaped (m4_escape) and therefore ineffective in m4sugar!
   52 m4_define([m4_list_append], [m4_do(
   53 	[m4_for([idx], 2, $#, 1,
   54 		[_m4_list_append_single($1, m4_argn(idx, $@))])],
   55 )])
   56 
   57 m4_define([_m4_list_append_single], [m4_do(
   58 	[m4_pushdef([_LIST_NAME], [[_LIST_$1]])],
   59 	[m4_ifndef(_LIST_NAME,
   60 		[m4_define(_LIST_NAME, m4_dquote(m4_escape([$2])))],
   61 		[m4_define(_LIST_NAME, m4_quote(m4_list_contents([$1]),)m4_dquote(m4_escape([$2])))],
   62 	)],
   63 	[m4_popdef([_LIST_NAME])],
   64 )])
   65 
   66 m4_define([m4_list_len], [m4_do(
   67 	[m4_pushdef([_LIST_NAME], [[_LIST_$1]])],
   68 	[m4_ifndef(_LIST_NAME, 0, [m4_count(m4_indir(_LIST_NAME))])],
   69 	[m4_popdef([_LIST_NAME])],
   70 )])
   71 
   72 dnl
   73 dnl Pefrorm an action if a given list is empty
   74 dnl $1: The list's ID
   75 dnl $2: The action to do if the list is empty or not even defined
   76 dnl $3: The action to do if the list is defined and non-empty
   77 m4_define([m4_list_ifempty], [m4_if(m4_list_len([$1]), 0, [$2], [$3])])
   78 
   79 dnl
   80 dnl Given a list name, it expands to its contents, suitable to use e.g. in m4_foreach
   81 dnl TODO: It produces a list of double-quoted items, which we maybe don't want
   82 m4_define([m4_list_contents], [m4_do(
   83 	[m4_if($#, 1, , [m4_fatal([$0: Expected exactly one argument, got $# instead (others were: ]m4_quote(m4_shift($@))[)])])],
   84 	[m4_pushdef([_LIST_NAME], [[_LIST_$1]])],
   85 	[m4_ifndef(_LIST_NAME, [], [m4_dquote_elt(m4_indir(_LIST_NAME))])],
   86 	[m4_popdef([_LIST_NAME])],
   87 )])
   88 
   89 dnl	[m4_ifndef(_LIST_NAME, [], m4_expand([m4_dquote_elt(m4_indir(_LIST_NAME))]))],
   90 
   91 dnl
   92 dnl Given a list name and an element, it returns list of indices of the element in the list
   93 dnl or nothing if it has not been found
   94 m4_define([m4_list_indices], [m4_do(
   95 	[m4_define([_FOUND_IDX], 1)],
   96 	[m4_define([_FOUND_RESULT], [])],
   97 	[m4_foreach([elem], [m4_list_contents([$1])], [m4_do(
   98 		[m4_if(elem, [$2], [m4_define([_FOUND_RESULT], m4_expand([_FOUND_RESULT,_FOUND_IDX]))])],
   99 		[m4_define([_FOUND_IDX], m4_incr(_FOUND_IDX))],
  100 	)])],
  101 	[m4_expand(m4_cdr(_FOUND_RESULT))],
  102 	[m4_undefine([_FOUND_RESULT])],
  103 	[m4_undefine([_FOUND_IDX])],
  104 )])
  105 
  106 
  107 dnl
  108 dnl Do something if the item is (isn't) in the list
  109 dnl $1: List name
  110 dnl $2: What
  111 dnl $3: If it is there
  112 dnl $4: If it is not there
  113 m4_define([m4_list_contains],
  114 	[m4_ifnblank(m4_list_indices([$1], [$2]), [$3], [$4])])
  115 
  116 m4_define([m4_list_sum], [m4_do(
  117 	[m4_eval(m4_quote(m4_join(+, m4_unquote(m4_list_contents([$1])))))],
  118 )])
  119 
  120 
  121 dnl
  122 dnl $1: list name
  123 dnl $2: With what to join
  124 dnl $3: left quote
  125 dnl $4: right quote
  126 dnl $5: last join
  127 m4_define([m4_list_join], [m4_do(
  128 	[m4_pushdef([listlen], m4_list_len([$1]))],
  129 	[m4_if(m4_cmp(listlen - 2, 0), 1,
  130 		[m4_for([idx], 1, m4_eval(listlen - 2), 1, [[$3]m4_list_nth([$1], idx)[$4$2]])],
  131 	)],
  132 	[m4_if(m4_cmp(listlen - 1, 0), 1,
  133 		[[$3]m4_list_nth([$1], m4_decr(listlen))[$4]m4_default([$5], [$2])],
  134 	)],
  135 	[m4_if(m4_cmp(listlen - 0, 0), 1,
  136 		[[$3]m4_list_nth([$1], listlen)[$4]],
  137 	)],
  138 	[m4_popdef([listlen])],
  139 )])
  140 
  141 
  142 dnl
  143 dnl $1: list name
  144 dnl $2: last join (separator by default)
  145 dnl $3: quote ("'" by default)
  146 dnl $4: separator (", " by default)
  147 m4_define([m4_list_format_sequence], [m4_do(
  148 	[m4_pushdef([_quote], m4_dquote(m4_default_quoted([$3], ['])))],
  149 	[m4_pushdef([_sep], m4_dquote(m4_default_quoted([$4], [, ])))],
  150 	[m4_list_join([$1], _sep, _quote, _quote, m4_dquote(m4_default_quoted([$2], _sep)))],
  151 	[m4_popdef([_sep])],
  152 	[m4_popdef([_quote])],
  153 )])
  154 
  155 dnl
  156 dnl Returns its n-th element, first item has index of 1.
  157 dnl If the element index is wrong, return $3
  158 m4_define([m4_list_nth], [m4_do(
  159 	[m4_bmatch([$2], [[1-9][0-9]*], [m4_do(
  160 		[m4_pushdef([_listlen], m4_list_len([$1]))],
  161 		[m4_if(m4_cmp([$2], _listlen), 1, [m4_ifnblank([$3], [$3], [m4_fatal([The list '$1' has length of ]_listlen[, so element No. $2 is not available])])])],
  162 		[m4_popdef([_listlen])],
  163 		[m4_expand(m4_argn([$2], m4_list_contents([$1])))],
  164 	)], [m4_ifnblank([$3], [$3], [m4_fatal([Requesting element $2 from list '$1': Only positive indices are available])])])],
  165 )])
  166 
  167 dnl
  168 dnl The list loses its 1st element, which is also expanded by this macro.
  169 m4_define([m4_list_pop_front], [m4_do(
  170 	[m4_pushdef([_LIST_NAME], [[_LIST_$1]])],
  171 	[m4_car(m4_unquote(_LIST_NAME))],
  172 	[m4_define(_LIST_NAME, m4_cdr(m4_unquote(_LIST_NAME)))],
  173 	[m4_popdef([_LIST_NAME])],
  174 )])
  175 
  176 dnl
  177 dnl The list loses its last element, which is also expanded by this macro.
  178 m4_define([m4_list_pop_back], [m4_do(
  179 	[m4_pushdef([_LIST_NAME], [[_LIST_$1]])],
  180 	[m4_define(_LIST_NAME, m4_dquote(m4_reverse(m4_unquote(_LIST_NAME))))],
  181 	[m4_list_pop_front([$1])],
  182 	[m4_define(_LIST_NAME, m4_dquote(m4_reverse(m4_unquote(_LIST_NAME))))],
  183 	[m4_popdef([_LIST_NAME])],
  184 )])
  185 
  186 m4_define([m4_list_destroy], [m4_ifdef([_LIST_$1], [m4_undefine([_LIST_$1])])])