1*a8c56be4SSimon J. Gerraty# $NetBSD: varmod-range.mk,v 1.19 2025/06/28 22:39:29 rillig Exp $ 22c3632d1SSimon J. Gerraty# 32c3632d1SSimon J. Gerraty# Tests for the :range variable modifier, which generates sequences 42c3632d1SSimon J. Gerraty# of integers from the given range. 5956e45f6SSimon J. Gerraty# 6956e45f6SSimon J. Gerraty# See also: 7956e45f6SSimon J. Gerraty# modword.mk 8956e45f6SSimon J. Gerraty 9956e45f6SSimon J. Gerraty# The :range modifier generates a sequence of integers, one number per 10d5e0a182SSimon J. Gerraty# word of the expression's value. 11956e45f6SSimon J. Gerraty.if ${a b c:L:range} != "1 2 3" 12956e45f6SSimon J. Gerraty. error 13956e45f6SSimon J. Gerraty.endif 14956e45f6SSimon J. Gerraty 15956e45f6SSimon J. Gerraty# To preserve spaces in a word, they can be enclosed in quotes, just like 16956e45f6SSimon J. Gerraty# everywhere else. 17956e45f6SSimon J. Gerraty.if ${:U first "the second word" third 4 :range} != "1 2 3 4" 18956e45f6SSimon J. Gerraty. error 19956e45f6SSimon J. Gerraty.endif 20956e45f6SSimon J. Gerraty 21956e45f6SSimon J. Gerraty# The :range modifier takes the number of words from the value of the 22d5e0a182SSimon J. Gerraty# expression. If that expression is undefined, the range is 23956e45f6SSimon J. Gerraty# undefined as well. This should not come as a surprise. 24956e45f6SSimon J. Gerraty.if "${:range}" != "" 25956e45f6SSimon J. Gerraty. error 26956e45f6SSimon J. Gerraty.endif 27956e45f6SSimon J. Gerraty 28d5e0a182SSimon J. Gerraty# An empty expression results in a sequence of a single number, even though 29d5e0a182SSimon J. Gerraty# the expression contains 0 words. 30d5e0a182SSimon J. Gerraty.if ${:U:range} != "1" 31d5e0a182SSimon J. Gerraty. error 32d5e0a182SSimon J. Gerraty.endif 33d5e0a182SSimon J. Gerraty 34956e45f6SSimon J. Gerraty# The :range modifier can be given a parameter, which makes the generated 35d5e0a182SSimon J. Gerraty# range independent from the value or the name of the expression. 36956e45f6SSimon J. Gerraty.if "${:range=5}" != "" 37956e45f6SSimon J. Gerraty. error 38956e45f6SSimon J. Gerraty.endif 39d5e0a182SSimon J. Gerraty# XXX: As of 2023-12-17, the ':range=n' modifier does not turn the undefined 40d5e0a182SSimon J. Gerraty# expression into a defined one, even though it does not depend on the value 41d5e0a182SSimon J. Gerraty# of the expression. This looks like an oversight. 42759b177aSSimon J. Gerraty# expect+1: Variable "" is undefined 43d5e0a182SSimon J. Gerraty.if ${:range=5} != "" 44d5e0a182SSimon J. Gerraty. error 45d5e0a182SSimon J. Gerraty.else 46d5e0a182SSimon J. Gerraty. error 47d5e0a182SSimon J. Gerraty.endif 48956e45f6SSimon J. Gerraty 49956e45f6SSimon J. Gerraty# Negative ranges don't make sense. 50956e45f6SSimon J. Gerraty# As of 2020-11-01, they are accepted though, using up all available memory. 51956e45f6SSimon J. Gerraty#.if "${:range=-1}" 52956e45f6SSimon J. Gerraty#. error 53956e45f6SSimon J. Gerraty#.else 54956e45f6SSimon J. Gerraty#. error 55956e45f6SSimon J. Gerraty#.endif 56956e45f6SSimon J. Gerraty 57956e45f6SSimon J. Gerraty# The :range modifier requires a number as parameter. 58956e45f6SSimon J. Gerraty# 59956e45f6SSimon J. Gerraty# Until 2020-11-01, the parser tried to read the 'x' as a number, failed and 60956e45f6SSimon J. Gerraty# stopped there. It then tried to parse the next modifier at that point, 61956e45f6SSimon J. Gerraty# which failed with the message "Unknown modifier". 62956e45f6SSimon J. Gerraty# 63956e45f6SSimon J. Gerraty# Since 2020-11-01, the parser issues a more precise "Invalid number" error 64956e45f6SSimon J. Gerraty# instead. 65*a8c56be4SSimon J. Gerraty# expect+1: Invalid number "x}Rest" != "Rest"" for modifier ":range" 66956e45f6SSimon J. Gerraty.if "${:U:range=x}Rest" != "Rest" 67956e45f6SSimon J. Gerraty. error 68956e45f6SSimon J. Gerraty.else 69956e45f6SSimon J. Gerraty. error 70956e45f6SSimon J. Gerraty.endif 71956e45f6SSimon J. Gerraty 72956e45f6SSimon J. Gerraty# The upper limit of the range must always be given in decimal. 73956e45f6SSimon J. Gerraty# This parse error stops at the 'x', trying to parse it as a variable 74956e45f6SSimon J. Gerraty# modifier. 75759b177aSSimon J. Gerraty# expect+1: Unknown modifier ":x0" 76956e45f6SSimon J. Gerraty.if "${:U:range=0x0}Rest" != "Rest" 77956e45f6SSimon J. Gerraty. error 78956e45f6SSimon J. Gerraty.else 79956e45f6SSimon J. Gerraty. error 80956e45f6SSimon J. Gerraty.endif 81956e45f6SSimon J. Gerraty 82956e45f6SSimon J. Gerraty# As of 2020-11-01, numeric overflow is not detected. 83956e45f6SSimon J. Gerraty# Since strtoul returns ULONG_MAX in such a case, it is interpreted as a 84956e45f6SSimon J. Gerraty# very large number, consuming all available memory. 85956e45f6SSimon J. Gerraty#.if "${:U:range=18446744073709551619}Rest" != "Rest" 86956e45f6SSimon J. Gerraty#. error 87956e45f6SSimon J. Gerraty#.else 88956e45f6SSimon J. Gerraty#. error 89956e45f6SSimon J. Gerraty#.endif 90956e45f6SSimon J. Gerraty 91956e45f6SSimon J. Gerraty# modifier name too short 92759b177aSSimon J. Gerraty# expect+1: Unknown modifier ":rang" 93956e45f6SSimon J. Gerraty.if "${a b c:L:rang}Rest" != "Rest" 94956e45f6SSimon J. Gerraty. error 95956e45f6SSimon J. Gerraty.else 96956e45f6SSimon J. Gerraty. error 97956e45f6SSimon J. Gerraty.endif 98956e45f6SSimon J. Gerraty 99956e45f6SSimon J. Gerraty# misspelled modifier name 100759b177aSSimon J. Gerraty# expect+1: Unknown modifier ":rango" 101956e45f6SSimon J. Gerraty.if "${a b c:L:rango}Rest" != "Rest" 102956e45f6SSimon J. Gerraty. error 103956e45f6SSimon J. Gerraty.else 104956e45f6SSimon J. Gerraty. error 105956e45f6SSimon J. Gerraty.endif 106956e45f6SSimon J. Gerraty 107956e45f6SSimon J. Gerraty# modifier name too long 108759b177aSSimon J. Gerraty# expect+1: Unknown modifier ":ranger" 109956e45f6SSimon J. Gerraty.if "${a b c:L:ranger}Rest" != "Rest" 110956e45f6SSimon J. Gerraty. error 111956e45f6SSimon J. Gerraty.else 112956e45f6SSimon J. Gerraty. error 113956e45f6SSimon J. Gerraty.endif 1142c3632d1SSimon J. Gerraty 1152c3632d1SSimon J. Gerratyall: 116