Skip to content

Commit

Permalink
Merge pull request #209 from ErezBinyamin/master
Browse files Browse the repository at this point in the history
Chmod and OEIS bug fixes.
  • Loading branch information
chubin authored Jun 11, 2020
2 parents a15563e + 5c27a3b commit 27dddd3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/adapter/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ def _get_command(self, topic, request_options=None):
cmd[0] = _get_abspath(cmd[0])

# cut oeis/ off
# Replace all non (numeric, '-') chars with Spaces to delimit args to oeis.sh
# Replace all non (alphanumeric, '-') chars with Spaces to delimit args to oeis.sh
if topic.startswith("oeis/"):
topic = topic[5:]
topic = re.sub('[^0-9-]', ' ', topic)
topic = re.sub('[^a-zA-Z0-9-]', ' ', topic)

return cmd + [topic]

Expand Down
45 changes: 28 additions & 17 deletions share/adapters/chmod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ chmod_calc(){
setuid=' '
setgid=' '
sticky=' '
# If permission number is given calc string
if [[ $1 =~ ^-?[0-9]+$ && ${#1} -ge 2 && ${#1} -le 4 ]]
# If permission number is given -> calc string
if [[ $1 =~ ^-?[0-9]+$ && ${#1} -ge 1 && ${#1} -le 4 ]]
then
p_n=$1
for (( i=0; i<${#1}; i++ ))
p_n=$(printf "%04s\n" "$1" | tr ' ' '0')
echo $p_n | grep -q '[8-9]' && return 1
for (( i=0; i<${#p_n}; i++ ))
do
num=$(echo "obase=2;${1:$i:1}" | bc | xargs printf '%03d')
# If 4 digit input, process specials
if [[ ${#1} -eq 4 && $i -eq 0 ]]
num=$(echo "obase=2;${p_n:$i:1}" | bc | xargs printf '%03d')
# If 4 digit input -> process specials
if [ $i -eq 0 ]
then
[ ${num:0:1} -eq 1 ] && setuid='X' || setuid=' '
[ ${num:1:1} -eq 1 ] && setgid='X' || setgid=' '
Expand Down Expand Up @@ -52,8 +53,8 @@ chmod_calc(){
[ ${num:2:1} -eq 1 ] && X+=('X') || X+=(' ')
fi
done
# If permission string is given calc number
elif [[ ${#1} -le 9 && $(( ${#1} % 3 )) -eq 0 && $1 =~ ^[r,s,S,t,T,w,x,-]+$ ]]
# If permission string is given -> calc number
elif [[ ${#1} -eq 9 && $1 =~ ^[r,s,S,t,T,w,x,-]+$ ]]
then
p_s=$1
num=0
Expand All @@ -63,21 +64,30 @@ chmod_calc(){
[[ 'tT' =~ ${p_s:8:1} ]] && sticky='X' && num=$((num+1))
[ ${num} -gt 0 ] && p_n+="$num"
# Calculate rest of p_n number while populating arrays for table
for (( i=0; i<${#1}; i+=0 ))
for (( i=0; i<${#p_s}; i+=0 ))
do
num=0
[[ ${1:$i:1} == 'r' ]] && R+=('X') || R+=(' ')
[[ ${1:$((i++)):1} == 'r' ]] && let num++
[[ "r-" =~ ${p_s:$i:1} ]] || return 1
[[ ${p_s:$i:1} == 'r' ]] && R+=('X') || R+=(' ')
[[ ${p_s:$((i++)):1} == 'r' ]] && let num++
num=$(( num << 1 ))
[[ ${1:$i:1} == 'w' ]] && W+=('X') || W+=(' ')
[[ ${1:$((i++)):1} == 'w' ]] && let num++
[[ "w-" =~ ${p_s:$i:1} ]] || return 1
[[ ${p_s:$i:1} == 'w' ]] && W+=('X') || W+=(' ')
[[ ${p_s:$((i++)):1} == 'w' ]] && let num++
num=$(( num << 1 ))
[[ 'stx' =~ ${1:$i:1} ]] && X+=('X') || X+=(' ')
[[ 'stx' =~ ${1:$((i++)):1} ]] && let num++
if [ $i -lt 6 ]
then
[[ "sSx-" =~ ${p_s:$i:1} ]] || return 1
[[ "sx" =~ ${p_s:$i:1} ]] && X+=('X') || X+=(' ')
[[ "sx" =~ ${p_s:$((i++)):1} ]] && let num++
else
[[ "tTx-" =~ ${p_s:$i:1} ]] || return 1
[[ "tx" =~ ${p_s:$i:1} ]] && X+=('X') || X+=(' ')
[[ "tx" =~ ${p_s:$((i++)):1} ]] && let num++
fi
p_n+="$num"
done
else
printf "Invalid permissions string: $1"
return 1
fi
# Print Final results table
Expand All @@ -95,3 +105,4 @@ Sticky bit [$sticky]\tExecute [${X[0]}]\tExecute [${X[1]}]\tExecute [${X[2]}]
}

chmod_calc $@
[ $? -ne 0 ] && printf "Invalid permissions string: $@\n"
9 changes: 5 additions & 4 deletions share/adapters/oeis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ oeis() (
# . oeis <SEQ_ID>
# . oeis <SEQ_ID> <LANGUAGE>
# . oeis <LANGUAGE> <SEQ_ID>
if [ $# -lt 3 ]
isNum='^[0-9]+$'
if [ $# -lt 3 ] && [[ ${1:1} =~ $isNum || ${2:1} =~ $isNum || ${1} =~ $isNum || ${2} =~ $isNum ]] && ! echo $1 | grep -q '[0-9]' || ! echo $2 | grep -q '[0-9]'
then
# Arg-Parse ID, Generate URL
if echo ${1^^} | grep -q '[B-Z]'
then
ID=$2
ID=${2^^}
LANGUAGE=$1
else
ID=$1
ID=${1^^}
LANGUAGE=$2
fi
[[ ${ID:0:1} == 'A' ]] && ID=${ID:1}
Expand Down Expand Up @@ -103,7 +104,7 @@ oeis() (
# Search unknown sequence
else
# Build URL
URL+="/search?q=signed%3A$(echo $@ | grep -v [a-z] | grep -v [A-Z] | tr ' ' ',')"
URL+="/search?q=signed%3A$(echo $@ | tr -sc '[:digit:]-' ',')"
curl $URL 2>/dev/null > $DOC
# Sequence IDs
grep -o '=id:.*&' $DOC \
Expand Down

0 comments on commit 27dddd3

Please sign in to comment.