1
0
This commit is contained in:
dtb 2022-06-27 09:04:28 -04:00
parent d7eb2b3d43
commit 5048212260

72
bin/seq
View File

@ -1,44 +1,52 @@
#!/bin/sh
# There are many bugs and limitations with this implementation as opposed to
# others, particularly NetBSD's. This should probably be rewritten in C.
argv0="$0"
nai() {
printf "$argv0: $1: Not an integer\n"
doublecheck() {
! str isdigit "$1" \
&& printf "%s: %s: Not an unsigned integer.\n" \
"$argv0" "$1" 1>&2 \
&& exit 1 \
|| return 0
}
error() {
printf "%s: Internal error.\n"
"$argv0" 1>&2
exit 1
}
usage() {
printf "Usage:\n \
$argv0 [last]
$argv0 [first] [last]
$argv0 [first] [increment] [last]\n" \
>/dev/stderr
printf "\
Usage:
%s [last]
%s [first] [last]
%s [first] [increment] [last]
" "$argv0" "$argv0" "$argv0" 1>&2
exit 1
}
[ -n "$1" ] || usage
if [ -n "$3" ]; then
stris int "$3" \
|| nai "$3" \
&& last=$3
stris int "$2" \
|| nai "$2" \
&& increment=$2
stris int "$1" \
|| nai "$1" \
&& first=$1
elif [ -n "$2" ]; then
stris int "$2" \
|| nai "$2" \
&& last=$2
stris int "$1" \
|| nai "$1" \
&& first=$1
increment=1
else
stris int "$1" \
|| nai "$1" \
&& last=$1
first=1
increment=1
test -n "$1" && test -z "$4" || usage
test -n "$3" && doublecheck "$3"
test -n "$2" && doublecheck "$2"
test -n "$1" && doublecheck "$1"
# all args are guaranteed to be positive integers; no need to quote variables
if test -n "$3"; then first=$1; increment=$2; last=$3
elif test -n "$2"; then first=$1; increment=1; last=$2
else first=1; increment=1; last=$1
fi
i=$first
while true; do
printf "%s\n" $i
i=$(printf "%s %s + p\n" $i $increment | dc)
printf "%s %s - p\n" $last $i | dc | grep '\-' >/dev/null 2>&1 \
&& break \
|| continue
done