-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert
380 lines (351 loc) · 15.3 KB
/
assert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#! .desc:
# Assert a string
#! .params:
# <$1> - type(
# '-single-quote-escaped-argument' - argument escaped with single quotes
# '-single-quotes-array' - pseudo array of single-quote-escaped arguments
# '-bit' - N in base 2 that represents a valid bit (valid bit position)
# '-eq' - N [0,1,00,01...] (integer) equal to another N
# '-max' - N [0,1,00,01...] (integer) maximum of another N
# '-min' - N [0,1,00,01...] (integer) minimum of another N
# '-n' - N [0,1,00,01...] (integer)
# '-greater-n' - N [0,1,00,01...] (integer) greater than another N
# '-lower-n' - N [0,1,00,01...] (integer) lower than another N
# '-natural-n' - natural N [1...] (positive integer)
# '-whole-n' - whole N [0,1...] (non-negative integer)
# '-portable-name' - portable ("Portable Filename") name
# '-absolute-path' - absolute path (/*)
# '-canonical-path' - lexically canonical (assertable) path
# '-fs-canonical-path' - semantically canonical (assertable) path
# '-relative-path' - relative path
# '-shell-name' - portable shell-parseable name
# .
# )
# <"$2"> - string
# ["$3"] - string
#! .rc:
# (0) true
# (1) false
# (2) false; bad operand
#! .ec:
# (255) bad input
#! .caveats (-bit):
# > `INT_MAX`. The operands and result of a bitwise operation are integers, so
# bitwise operations (and shell arithmetic in general) are subject to the
# system's native integer limits.
#.
assert() {
case "$1" in
# Assert $2 is a valid single-quote-escaped argument. A valid
# single-quote-escaped argument is a shell-parseable string enclosed in
# single quotes. A single-quote inside a single-quote-escaped argument
# is escaped with the syntactical expression `'\''`. This string is
# semantically safe for `eval`.
#
# For more information, refer to:
# > "POSIX.1-2024, Volume: Shell & Utilities, Section: Shell Command
# Language, Subsection: Quoting".
# > "POSIX.1-2024, Volume: Shell & Utilities, Section: Shell Command
# Language, Subsection: Escape Character (Backslash)".
# > "POSIX.1-2024, Volume: Shell & Utilities, Section: Shell Command
# Language, Subsection: Single-Quotes".
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: Argument".
'-single-quote-escaped-argument')
set -- "$2"; while [ "$1" ] || return 1; do
[ "${1#\'}" != "$1" ] && set -- "${1#\'}" || return 1
while [ "$1" ]; do
case "${1#*\'}" in
\\\'\'*) set -- "${1#*\'\\\'\'}"; continue ;;
'') return 0 ;;
esac
return 1
done
done
;;
# Assert $2 is a valid single-quotes array of arguments. A valid
# single-quotes array is a shell-parseable string consisting of at
# least one argument, each enclosed in single quotes and delimited
# by <whitespace>. A single-quote inside a single-quote-escaped
# argument is escaped with the syntactical expression `'\''`. This
# string is semantically safe for `eval`.
#
# For more information, refer to:
# > "POSIX.1-2024, Volume: Shell & Utilities, Section: Shell Command
# Language, Subsection: Quoting".
# > "POSIX.1-2024, Volume: Shell & Utilities, Section: Shell Command
# Language, Subsection: Escape Character (Backslash)".
# > "POSIX.1-2024, Volume: Shell & Utilities, Section: Shell Command
# Language, Subsection: Single-Quotes".
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: Argument".
'-single-quotes-array')
set -- "${2%"${2##*[! ]}"}"; while [ "$1" ] || return 1; do
set -- "${1#"${1%%[! ]*}"}"
[ "${1#\'}" != "$1" ] && set -- "${1#\'}" || return 1
while [ "$1" ]; do
case "${1#*\'}" in
\\\'\'*) set -- "${1#*\'\\\'\'}"; continue ;;
\ *) set -- "${1#*\'}"; continue 2 ;;
'') return 0 ;;
esac
return 1
done
done
;;
# Assert $2 is a number in base 2 that represents a valid bit. A valid
# bit is a natural number that represents a specific bit position in a
# bitmask and is always a power of 2 (`1`, `2`, `4`, `8`, ...). This is
# also known as "valid bit position".
#
# A number is a power of 2 if performing bitwise AND in the expression
# `N & (N - 1)` results in zero (the binary representation turns to all
# zeroes due to the single 1-bit being flipped off, given N was an
# actual power of 2 in base 2).
'-bit')
assert -natural-n "$2" || return 2
[ "$(($2 & ($2 - 1)))" = 0 ] || return 1
;;
# Assert $2 is a number equal to $3. Leading zeroes are ignored, except
# `[0] -eq [0]` is true. This comparison essentually emulates the
# behavior of operator `-eq` of `test` in POSIX shells, just with
# support for extremely large numbers. This comparison is also known as
# "lexicographical numeric comparison".
'-eq')
assert -n "$2" || return 2
assert -n "$3" || return 2
set -- "${2#"${2%%[!0]*}"}" "${3#"${3%%[!0]*}"}"
[ "$1" = "$2" ] || return 1
;;
# Assert $2 is a number maximum of $3. Leading zeroes are ignored,
# except `[0] -eq [0]` is true. This comparison essentually emulates
# the behavior of operator `-le` of `test` in POSIX shells, just with
# support for extremely large numbers. This comparison is also known as
# "lexicographical numeric comparison".
'-max')
assert -lower-n "$2" "$3" || \
assert -eq "$2" "$3" || \
return "$?"
;;
# Assert $2 is a number minimum of $3. Leading zeroes are ignored,
# except `[0] -eq [0]` is true. This comparison essentually emulates
# the behavior of operator `-ge` of `test` in POSIX shells, just with
# support for extremely large numbers. This comparison is also known as
# "lexicographical numeric comparison".
'-min')
assert -greater-n "$2" "$3" || \
assert -eq "$2" "$3" || \
return "$?"
;;
# Assert $2 is a number (`0`, `1`, `00`, `01`, ...). A number is any
# string consisting only of digits. This is also well known as
# "integer".
'-n')
case "${2:-0}${2#*[!0123456789]}" in
"$2$2") return 0 ;;
esac
return 1
;;
# Assert $2 is a number greater than $3. To determine which number is
# greater, leading zeroes are removed, and the numbers are first
# compared by length, followed by digit comparison if the lengths are
# the same. This comparison essentually emulates the behavior of
# operator `-gt` of `test` in POSIX shells, just with support for
# extremely large numbers. This comparison is also known as
# "lexicographical numeric comparison".
'-greater-n')
assert -n "$2" && assert -n "$3" || return 2
set -- "${2#"${2%%[!0]*}"}" "${3#"${3%%[!0]*}"}"
[ ! "$1" = "$2" ] && [ ! "${#1}" -lt "${#2}" ] || return 1
[ ! "${#1}" -gt "${#2}" ] || return 0
while [ "$1" ]; do
case "$1" in
"${2%"${2#?}"}"*) set -- "${1#?}" "${2#?}"; continue ;;
esac
if [ "${1%"${1#?}"}" -gt "${2%"${2#?}"}" ]; then
return 0
fi
return 1
done
;;
# Assert $2 is a number lower than $3. To determine which number is
# lower, leading zeroes are removed, and the numbers are first compared
# by length, followed by digit comparison if the lengths are the same.
# This comparison essentually emulates the behavior of operator `-lt`
# of `test` in POSIX shells, just with support for extremely large
# numbers. This comparison is also known as
# "lexicographical numeric comparison".
'-lower-n')
assert -n "$2" && assert -n "$3" || return 2
set -- "${2#"${2%%[!0]*}"}" "${3#"${3%%[!0]*}"}"
[ ! "$1" = "$2" ] && [ ! "${#1}" -gt "${#2}" ] || return 1
[ ! "${#1}" -lt "${#2}" ] || return 0
while [ "$1" ]; do
case "$1" in
"${2%"${2#?}"}"*) set -- "${1#?}" "${2#?}"; continue ;;
esac
if [ "${1%"${1#?}"}" -lt "${2%"${2#?}"}" ]; then
return 0
fi
return 1
done
;;
# Assert $2 is a natural number (`1`, `2`, ...). A natural number is
# any string consisting only of digits, of which the first is not `0`.
# Also known as "positive integer" in the POSIX standard.
'-natural-n')
case "${2:-0}${2#*[!0123456789]}" in
0*) return 1 ;;
"$2$2") return 0 ;;
esac
return 1
;;
# Assert $2 is a whole number (`0`, `1`, `2`, ...). A whole number is
# `0` and any string consisting only of digits, of which the first is
# not `0`. Also known as "non-negative integer" in the POSIX standard.
'-whole-n')
assert -natural-n "$2" || \
assert -eq "$2" 0 || \
return "$?"
;;
# Assert $2 is a portable ("Portable Filename") name. A portable name
# is a string composed of the characters in the portable filename
# character set, with the following exceptions:
#
# - Should not begin with a <hyphen-minus> character. This exception is
# currently not being asserted.
#
# The character classes have been intentionally expanded to be
# locale-independent.
#
# For more information, refer to:
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: Portable Filename".
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: Portable Filename Character Set".
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: User Name".
# > "POSIX.1-2024, Volume: Base Definitions, Section: Regular
# Expressions, Subsection: RE Bracket Expression".
'-portable-name')
case "$2" in
*[!0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_\.\-]*)
return 1
;;
esac
[ "$2" ] || return 1
;;
# Assert $2 is an absolute (/*) path. An absolute path is any string of
# which the first character is `/`.
#
# For more information, refer to:
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: Absolute Pathname".
'-absolute-path')
case "$2" in
'/'*) return 0 ;;
esac
return 1
;;
# Assert $2 is a lexically canonical (assertable) path. A canonical
# path is largely implementation-defined string that refers to the
# normalized absolute path. Therefore, a lexical canonical path is:
#
# - Assertive: Multiple consecutive slashes (`//`) are removed, `.`
# and `..` are resolved. The only exception is two consecutive
# slashes (`//`) at the beginning of the string, which is specified
# by the POSIX standard as an "implementation-defined" exception.
# - Absolute: The path is always rooted from `/`.
#
# For more information, refer to:
# > "POSIX.1-2024, Volume: Base Definitions, Section: General Concepts,
# Subsection: Pathname Resolution".
'-canonical-path')
case "$2" in
'//'*) set -- "${2#/}" ;;
*) set -- "$2" ;;
esac
case "$1" in
*'//'*) return 1 ;;
'/'*) : ;;
*) return 1 ;;
esac
case "$1/" in
*'/./'* | *'/../'*) return 1 ;;
esac
return 0
;;
# Assert $2 is a semantically canonical (assertable) path. A canonical
# path is largely implementation-defined string that refers to the
# normalized absolute path. Therefore, a semantic canonical path is:
#
# - Assertive: Multiple consecutive slashes (`//`) are removed, `.`
# and `..` are resolved.
# - Semantic: Neither the path nor its segments are a symbolic link on
# the filesystem. The actual existence of the path or its segments is
# ignored.
# - Absolute: The path is always rooted from `/`.
#
# For more information, refer to:
# > "POSIX.1-2024, Volume: Base Definitions, Section: General Concepts,
# Subsection: Pathname Resolution".
'-fs-canonical-path')
case "$2" in
'/') return 0 ;;
*'//'*) return 1 ;;
'/'*) set -- "${2%/}/" ;;
*) return 1 ;;
esac
case "$1" in
*'/./'* | *'/../'*) return 1 ;;
esac
while [ ! -h "${1%/}" ] || return 1; do
[ "${1%/*/}" ] && set -- "${1%/*/}/" || return 0
done
;;
# Assert $2 is a relative (partial) path. A relative path is any string
# of which the first character is not `/`.
#
# For more information, refer to:
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: Relative Pathname".
'-relative-path')
case "$2" in
'/'*) return 1 ;;
esac
[ "$2" ] || return 1
;;
# Assert $2 is a portable shell-parseable name. A portable
# shell-parseable name is a string composed of the characters in
# the portable filename character set, with the following exceptions:
#
# - Cannot begin with a digit.
# - Cannot contain a <hyphen-minus> character.
# - Cannot contain a <period> character.
#
# The character classes have been intentionally expanded to be
# locale-independent.
#
# For more information, refer to:
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: Name".
# > "POSIX.1-2024, Volume: Base Definitions, Section: Definitions,
# Subsection: Portable Filename Character Set".
# > "POSIX.1-2024, Volume: Base Definitions, Section: Regular
# Expressions, Subsection: RE Bracket Expression".
'-shell-name')
case "$2" in
[0123456789]*)
return 1
;;
*[!0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_]*)
return 1
;;
esac
[ "$2" ] || return 1
;;
*)
exit 255
;;
esac
}