Permalink
Browse files

Implement read -n, and a placeholder for -r.

Addresses issue #26.
  • Loading branch information...
Andy Chu
Andy Chu committed Sep 6, 2017
1 parent 42e9dec commit 4e3e9a752186ad3923311f0249df7bdd9539a7da
Showing with 72 additions and 2 deletions.
  1. +24 −2 core/builtin.py
  2. +32 −0 gold/nix.sh
  3. +12 −0 spec/builtins.test.sh
  4. +4 −0 test/gold.sh
View
@@ -365,12 +365,32 @@ def Jobs(argv, job_state):
return 0
READ_SPEC = _Register('read')
READ_SPEC.ShortFlag('-r')
READ_SPEC.ShortFlag('-n', args.Int)
def Read(argv, mem):
# TODO:
# - parse flags.
# - Use IFS instead of Python's split().
names = argv
arg, i = READ_SPEC.Parse(argv)
if not arg.r:
util.warn('*** read without -r not implemented ***')
names = argv[i:]
if arg.n is not None:
try:
name = names[0]
except IndexError:
name = 'REPLY' # default variable name
s = os.read(sys.stdin.fileno(), arg.n)
#log('read -n: %s = %s', name, s)
state.SetLocalString(mem, name, s)
# NOTE: Even if we don't get n bytes back, there is no error?
return 0
line = sys.stdin.readline()
if not line: # EOF
return 1
@@ -387,11 +407,13 @@ def Read(argv, mem):
strs = line.split(None, n-1)
# TODO: Use REPLY variable here too?
for i in xrange(n):
try:
s = strs[i]
except IndexError:
s = '' # if there are too many variables
#log('read: %s = %s', names[i], s)
state.SetLocalString(mem, names[i], s)
return status
View
@@ -0,0 +1,32 @@
#!/bin/bash
#
# Usage:
# ./nix.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
# My simpler rewrite.
isElfSimple() {
local path=$1 # double quotes never necessary on RHS
local magic
# read 4 bytes from $path, without escaping, into $magic var
read -r -n 4 magic < "$path"
# Return the exit code of [[
[[ "$magic" =~ ELF ]]
}
isElfSimpleWithStdin() {
seq 3 > /tmp/3.txt
while read line; do
echo $line
isElfSimple /bin/true && echo YES
isElfSimple $0 || echo NO
echo
done < /tmp/3.txt
}
"$@"
View
@@ -202,6 +202,18 @@ echo /$x/$y/$z/
umask | grep '[0-9]\+' # check for digits
# status: 0
### Read -n (with $REPLY)
echo 12345 > $TMP/readn.txt
read -n 4 x < $TMP/readn.txt
read -n 2 < $TMP/readn.txt # Do it again with no variable
argv.py $x $REPLY
# stdout: ['1234', '12']
# N-I dash stdout: []
### get umask
umask | grep '[0-9]\+' # check for digits
# status: 0
### set umask in octal
rm $TMP/umask-one $TMP/umask-two
umask 0002
View
@@ -92,6 +92,10 @@ glob() {
_compare gold/glob.sh
}
nix() {
_compare gold/nix.sh isElfSimpleWithStdin
}
all() {
version-text
count

0 comments on commit 4e3e9a7

Please sign in to comment.