-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcat.sh
executable file
·301 lines (276 loc) · 8.11 KB
/
cat.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
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
#!/bin/sh
set -e -u -f
LC_ALL=C
__ALLOC=1 # Starting heap at 1 because 0 is the null pointer.
_malloc() { # $2 = object size
: $((_$__ALLOC = $2)) # Track object size
: $(($1 = $__ALLOC + 1))
: $((__ALLOC += $2 + 1))
}
defarr() { _malloc $1 $2; }
defarr _buf 1024
: $((__t1 = n = fd = 0))
_cat_fd() { let fd $2
let n; let __t1
n=1024
while [ $n = 1024 ]; do
_read n $fd $_buf 1024
if [ $n -lt 0 ] || { _write __t1 1 $_buf $n; [ $__t1 != $n ]; } ; then
exit 1
fi
done
endlet $1 __t1 n fd
}
: $((fd = filename = 0))
_cat_file() { let filename $2
let fd
_open fd $filename 0
if [ $fd -lt 0 ] ; then
exit 1
fi
_cat_fd __ $fd
_close __ $fd
endlet $1 fd filename
}
: $((i = myargv = argc = 0))
_main() { let argc $2; let myargv $3
let i
if [ $argc -ge 2 ] ; then
i=1
while [ $i -lt $argc ]; do
if [ $((_$((_$((myargv + i)) + 0)))) = $__MINUS__ ] && [ $((_$((_$((myargv + i)) + 1)))) = $__NUL__ ] ; then
_cat_fd __ 0
else
_cat_file __ $((_$((myargv + i))))
fi
: $((i += 1))
done
else
_cat_fd __ 0
fi
: $(($1 = 0))
endlet $1 i myargv argc
}
# Character constants
readonly __NUL__=0
readonly __MINUS__=45
# Runtime library
_free() { # $2 = object to free
__ptr=$(($2 - 1)) # Start of object
__end=$((__ptr + _$__ptr)) # End of object
while [ $__ptr -lt $__end ]; do
unset "_$__ptr"
: $((__ptr += 1))
done
: $(($1 = 0)) # Return 0
}
# Unpack a Shell string into an appropriately sized buffer
unpack_line() { # $1: Shell string, $2: Buffer, $3: Ends with EOF?
__fgetc_buf=$1
__buffer=$2
__ends_with_eof=$3
while [ ! -z "$__fgetc_buf" ]; do
__c=$(printf "%d" "'${__fgetc_buf%"${__fgetc_buf#?}"}"); __c=$((__c > 0 ? __c : 256 + __c))
: $((_$__buffer = __c))
__fgetc_buf=${__fgetc_buf#?} # Remove the first character
: $((__buffer += 1)) # Move to the next buffer position
done
if [ $__ends_with_eof -eq 0 ]; then # Ends with newline and not EOF?
: $((_$__buffer = 10)) # Line ends with newline
: $((__buffer += 1))
fi
: $((_$__buffer = 0)) # Then \0
}
refill_buffer() { # $1: fd
__fd=$1
__buffer=$((__buffer_fd$__fd))
IFS=
__ends_with_eof=0
read -r __temp_buf <&$__fd || __ends_with_eof=1
# Check that the buffer is large enough to unpack the line
__buflen=$((__buflen_fd$__fd - 2)) # Minus 2 to account for newline and \0
__len=${#__temp_buf}
if [ $__len -gt $__buflen ]; then
# Free buffer and reallocate a new one double the line size
__buflen=$((__len * 2))
_free __ $__buffer
_malloc __buffer $__buflen
: $((__buffer_fd$__fd = __buffer))
: $((__buflen_fd$__fd = __buflen))
fi
unpack_line "$__temp_buf" $__buffer $__ends_with_eof
}
read_byte() { # $2: fd
__fd=$2
: $((__buffer=__buffer_fd$__fd))
: $((__cursor=__cursor_fd$__fd))
# The cursor is at the end of the buffer, we need to read the next line
if [ $((_$((__buffer + __cursor)))) -eq 0 ]; then
# Buffer has been read completely, read next line
refill_buffer $__fd
__cursor=0 # Reset cursor and reload buffer
: $((__buffer=__buffer_fd$__fd))
if [ $((_$((__buffer + __cursor)))) -eq 0 ]; then
: $(($1 = -1)) # EOF
return
fi
fi
: $(($1 = _$((__buffer + __cursor))))
: $((__cursor_fd$__fd = __cursor + 1)) # Increment cursor
}
_put_pstr() {
: $(($1 = 0)); shift # Return 0
__addr=$1; shift
while [ $((__c = _$__addr)) != 0 ]; do
printf \\$((__c/64))$((__c/8%8))$((__c%8))
: $((__addr += 1))
done
}
_malloc __buffer_fd0 1000 # Allocate buffer
: $((_$__buffer_fd0 = 0)) # Init buffer to ""
: $((__cursor_fd0 = 0)) # Make buffer empty
: $((__buflen_fd0 = 1000)) # Init buffer length
__state_fd0=0 # stdin
__state_fd1=1 # stdout
__state_fd2=2 # stderr
__state_fd3=-1
__state_fd4=-1
__state_fd5=-1
__state_fd6=-1
__state_fd7=-1
__state_fd8=-1
__state_fd9=-1
_open() { # $2: filename, $3: flags, $4: mode
# Get available fd
__fd=0
while [ $__fd -lt 10 ]; do
if [ $((__state_fd$__fd)) -lt 0 ]; then
break
fi
: $((__fd += 1))
done
if [ $__fd -gt 9 ] ; then
# Some shells don't support fd > 9
echo "No more file descriptors available to open $(_put_pstr __ $2)" ; exit 1
else
# Because the file must be read line-by-line, and string
# values can't be assigned to dynamic variables, each line
# is read and then unpacked in the buffer.
_malloc __addr 1000 # Allocate buffer
: $((_$__addr = 0)) # Init buffer to ""
: $((__buffer_fd$__fd = __addr)) # Buffer address
: $((__cursor_fd$__fd = 0)) # Buffer cursor
: $((__buflen_fd$__fd = 1000)) # Buffer length
: $((__state_fd$__fd = $3)) # Mark the fd as opened
__res=$(_put_pstr __ $2)
if [ $3 = 0 ] ; then
case $__fd in
0) exec 0< "$__res" ;; 1) exec 1< "$__res" ;; 2) exec 2< "$__res" ;;
3) exec 3< "$__res" ;; 4) exec 4< "$__res" ;; 5) exec 5< "$__res" ;;
6) exec 6< "$__res" ;; 7) exec 7< "$__res" ;; 8) exec 8< "$__res" ;;
9) exec 9< "$__res" ;;
esac
elif [ $3 = 1 ] ; then
case $__fd in
0) exec 0> "$__res" ;; 1) exec 1> "$__res" ;; 2) exec 2> "$__res" ;;
3) exec 3> "$__res" ;; 4) exec 4> "$__res" ;; 5) exec 5> "$__res" ;;
6) exec 6> "$__res" ;; 7) exec 7> "$__res" ;; 8) exec 8> "$__res" ;;
9) exec 9> "$__res" ;;
esac
elif [ $3 = 2 ] ; then
case $__fd in
0) exec 0>> "$__res" ;; 1) exec 1>> "$__res" ;; 2) exec 2>> "$__res" ;;
3) exec 3>> "$__res" ;; 4) exec 4>> "$__res" ;; 5) exec 5>> "$__res" ;;
6) exec 6>> "$__res" ;; 7) exec 7>> "$__res" ;; 8) exec 8>> "$__res" ;;
9) exec 9>> "$__res" ;;
esac
else
echo "Unknown file mode" ; exit 1
fi
fi
: $(($1 = __fd))
}
_read() { : $((__fd = $2)) $((__buf = $3)) $((__count = $4))
: $((__i = 0))
while [ $__i -lt $__count ] ; do
read_byte __byte $__fd
if [ $__byte -lt 0 ] ; then break; fi
: $((_$((__buf + __i)) = __byte))
: $((__i += 1))
done
: $(($1 = __i))
}
_write() { : $((__fd = $2)) $((__buf = $3)) $((__count = $4))
: $((__i = 0))
while [ $__i -lt $__count ] ; do
: $((__byte = _$((__buf+__i))))
printf \\$(($__byte/64))$(($__byte/8%8))$(($__byte%8)) >&$__fd
: $((__i += 1))
done
: $(($1 = __count))
}
_close() { # $2: fd
__fd=$2
__buf=$((__buffer_fd$__fd)) # Get buffer
_free __ $__buf # Release buffer
: $((__state_fd$__fd = -1)) # Mark the fd as closed
case $__fd in
0) exec 0<&- ;; 1) exec 1<&- ;; 2) exec 2<&- ;;
3) exec 3<&- ;; 4) exec 4<&- ;; 5) exec 5<&- ;;
6) exec 6<&- ;; 7) exec 7<&- ;; 8) exec 8<&- ;;
9) exec 9<&- ;;
esac
: $(($1 = 0))
}
# Convert a Shell string to a C string
unpack_string() {
__str="$2"
_malloc $1 $((${#__str} + 1))
__ptr=$(($1))
while [ -n "$__str" ] ; do
# Remove first char from string
__tail="${__str#?}"
# Remove all but first char
__char="${__str%"$__tail"}"
# Convert char to ASCII
__c=$(printf "%d" "'$__char"); __c=$((__c > 0 ? __c : 256 + __c))
# Write character to memory
: $((_$__ptr = __c))
# Continue with rest of string
: $((__ptr += 1))
__str="$__tail"
done
: $((_$__ptr = 0))
}
make_argv() {
__argc=$1; shift;
_malloc __argv $__argc # Allocate enough space for all elements. No need to initialize.
__argv_ptr=$__argv
while [ $# -ge 1 ]; do
unpack_string _$__argv_ptr "$1"
: $((__argv_ptr += 1))
shift
done
}
# 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
}
# Setup argc, argv
__argc_for_main=$(($# + 1))
make_argv $__argc_for_main "$0" "$@"; __argv_for_main=$__argv
__code=0; # Success exit code
_main __code $__argc_for_main $__argv_for_main; exit $__code