-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnon_zero.sh
executable file
·77 lines (72 loc) · 2.15 KB
/
non_zero.sh
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
#!/bin/sh
set -e -u -f
LC_ALL=C
: $((__t1 = n = 0))
_main() {
let n; let __t1
n=0
while :; do
printf "Enter a non-zero single-digit number:\r\n"
_getchar n
while _getchar __t1; [ $__NEWLINE__ != $__t1 ]; do
:
done
[ $n = $__0__ ] || [ $((!((n >= __0__) && (n <= __9__)))) != 0 ] || break
done
printf "You entered "
printf \\$((n/64))$((n/8%8))$((n%8))
printf ": bye bye!\r\n"
endlet $1 __t1 n
}
# Character constants
readonly __NEWLINE__=10
readonly __0__=48
readonly __9__=57
# Runtime library
__stdin_buf=
__stdin_line_ending=0 # Line ending, either -1 (EOF) or 10 ('\n')
_getchar() {
if [ -z "$__stdin_buf" ]; then # need to get next line when buffer empty
if [ $__stdin_line_ending != 0 ]; then # Line is empty, return line ending
: $(($1 = __stdin_line_ending))
__stdin_line_ending=0 # Reset line ending for next getchar call
return
fi
IFS= # don't split input
if read -r __stdin_buf ; then # read next line into $__stdin_buf
if [ -z "$__stdin_buf" ] ; then # an empty line implies a newline character
: $(($1 = 10)) # next getchar call will read next line
return
fi
__stdin_line_ending=10
else
if [ -z "$__stdin_buf" ] ; then # EOF reached when read fails
: $(($1 = -1))
return
else
__stdin_line_ending=-1
fi
fi
fi
__c=$(printf "%d" "'${__stdin_buf%"${__stdin_buf#?}"}"); __c=$((__c > 0 ? __c : 256 + __c))
: $(($1 = __c))
__stdin_buf="${__stdin_buf#?}" # remove the current char from $__stdin_buf
}
# Local variables
__=0
__SP=0
let() { # $1: variable name, $2: value (optional)
: $((__SP += 1)) $((__$__SP=$1)) # Push
: $(($1=${2-0})) # Init
}
endlet() { # $1: return variable
# $2...: function local variables
__ret=$1 # Don't overwrite return value
: $((__tmp = $__ret))
while [ $# -ge 2 ]; do
: $(($2 = __$__SP)) $((__SP -= 1)); # Pop
shift;
done
: $(($__ret=__tmp)) # Restore return value
}
_main __