This is a template for a make-based "tr" macro which transliterates from listA to listB in the text.
Before running "tr" I intend to run some validations on the arguments to "tr". The macro "-tr-validate" does that task and it is here that the number of arguments to a function's need was sorely felt. Also in this macro is it possible to factor in the $0 in the exit-code macro itself, so that we dont need to specify it in the invocation of exit-code macro everytime.? I tried various alternatives using $(eval.... but nothing worked. ################# gnu make code for ############## # % make --version # GNU Make 4.0 # Built for i686-pc-cygwin ############################################### SHELL := /bin/sh COMMA := , [a-z] := a b c d e f g h i j k l m n o p q r s t u v w x y z # [A-Z] := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z # .PHONY: t # message macro for the wrong argc define tr-usage-1 BAD ARGS *** incorrect number of arguments to function 'tr'. *** valid number of arguments is 3 to function 'tr'. *** Usage: \$$(call tr,source_list,target_list,string) *** Ex: \$$(call tr,a b c d,A B C D,string_of_text) endef # message macro for an empty argv define tr-usage-2 INVALID ARGS *** invalid argument to function 'tr'. *** cannot pass an empty argument to function 'tr'. *** Usage: \$$(call tr,source_list,target_list,string) *** Ex: \$$(call tr,a b c d,A B C D,text) endef # message macro for mismatched source & target argv define tr-usage-3 SIZE MISMATCHED ARGS *** mismatch sized arguments to function 'tr'. *** first & second arguments size need to contain same number of elements endef # exit codes E_BAD_ARGS := 1 E_INVALID_ARGS := 2 E_MISMATCHED_ARGS := 3 #$(call exit-code,$0,E_BAD_ARGS) exit-code = $(error $($(join $(word 1,$(subst -, ,$1)),-usage-$($2)))) define -tr-validate $(if $1$2$3,$(if $2$3,$(if $3$1,$(if $1$2,$(if $3,$(if $1,$(if $2,$(if $(filter-out $(words $1),$(words $2)),$(call exit-code,$0,E_MISMATCHED_ARGS)),$(call exit-code,$0,E_INVALID_ARGS)),$(call exit-code,$0,E_INVALID_ARGS)),$(call exit-code,$0,E_BAD_ARGS)),$(call exit-code,$0,E_INVALID_ARGS)),$(call exit-code,$0,E_INVALID_ARGS)),$(call exit-code,$0,E_BAD_ARGS)),$(call exit-code,$0,E_BAD_ARGS)) endef define -tr-body $(if $1,$$(subst $(firstword $1),$(call -tr-body,$(wordlist 2,$(words $1),$1),$2)),$2) endef #$(call tr,listA,listB,text) define tr $(strip \ $(call -tr-validate,$1,$2,$3)\ $(eval result := $(call -tr-body,$(join $(addsuffix $(COMMA),$1),$2),$3))\ $(result)) endef lc = $(call tr,$([A-Z]),$([a-z]),$1) uc = $(call tr,$([a-z]),$([A-Z]),$1) L := $([a-z]) M := $([A-Z]) S := MiXed_CaSe ## Test-suite for the "tr" macro # # AAA # rrr # ggg # 123 # x := $(call tr) # xxx <--- 0 args --> E_BAD_ARGS # x := $(call tr,$L) # 1xx <--- 1 arg --> E_BAD_ARGS # x := $(call tr,,$M) # x1x <--- 1 arg --> E_INVALID_ARGS # x := $(call tr,,,$S) # xx1 <--- 1 arg --> E_INVALID_ARGS # x := $(call tr,$L,$M) # 11x <--- 2 args --> E_BAD_ARGS # x := $(call tr,,$M,$S) # x11 <--- 2 args --> E_INVALID_ARGS # x := $(call tr,$L,,$S) # 1x1 <--- 2 args --> E_INVALID_ARGS x := $(call tr,$L,$M,$S) # 111 <--- 3 args --> E_MISMATCHED_ARGS or success t: @echo "b4 transliteration=$(S)"; @echo "af transliteration=$(x)"; ####################################################################### > Subject: Re: Number of arguments, a. la $# in gnu make? > From: psm...@gnu.org > To: sharma...@hotmail.com > CC: help-make@gnu.org > Date: Sat, 15 Feb 2014 09:46:58 -0500 > > On Sat, 2014-02-15 at 02:44 -0800, Rakesh Sharma wrote: > > Is there a function/command for determining the number of arguments > > passed to a user-defined function (== macro) in GNU make? > > Unfortunately nothing like that exists today :-( > > It's not exactly clear how one would use such a thing since the looping > constructs in GNU make are quite anemic. But we'd be interested to hear > about your needs. > _______________________________________________ Help-make mailing list Help-make@gnu.org https://lists.gnu.org/mailman/listinfo/help-make