Skip to content

Commit

Permalink
Implement read -n, and a placeholder for -r.
Browse files Browse the repository at this point in the history
Addresses issue #26.
  • Loading branch information
Andy Chu committed Sep 6, 2017
1 parent 42e9dec commit 4e3e9a7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
26 changes: 24 additions & 2 deletions core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 32 additions & 0 deletions gold/nix.sh
Original file line number Diff line number Diff line change
@@ -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
}

"$@"
12 changes: 12 additions & 0 deletions spec/builtins.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/gold.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ glob() {
_compare gold/glob.sh
}

nix() {
_compare gold/nix.sh isElfSimpleWithStdin
}

all() {
version-text
count
Expand Down

0 comments on commit 4e3e9a7

Please sign in to comment.