mirror of
https://github.com/chubin/cheat.sh.git
synced 2026-06-20 13:16:44 +02:00
Merge pull request #204 from ErezBinyamin/master
Adapters OEIS, and CHMOD updated
This commit is contained in:
+1
-4
@@ -131,13 +131,10 @@ class AdapterOeis(CommandAdapter):
|
||||
|
||||
# cut oeis/ off
|
||||
if topic.startswith("oeis/"):
|
||||
topic = topic[4:]
|
||||
topic = topic[5:]
|
||||
|
||||
return cmd + [topic]
|
||||
|
||||
def _get_list(self, prefix=None):
|
||||
return list("oeis/%s" % x for x in range(1, 8649))
|
||||
|
||||
def is_found(self, topic):
|
||||
return True
|
||||
|
||||
|
||||
+40
-14
@@ -3,6 +3,7 @@
|
||||
# Contributed by Erez Binyamin (github.com/ErezBinyamin)
|
||||
|
||||
# Translate between chmod string and number
|
||||
# Inspired by http://permissions-calculator.org/
|
||||
# Contrib to chubin - cheat.sh
|
||||
chmod_calc(){
|
||||
p_s=""
|
||||
@@ -10,23 +11,42 @@ chmod_calc(){
|
||||
R=()
|
||||
W=()
|
||||
X=()
|
||||
S=()
|
||||
setuid=' '
|
||||
setgid=' '
|
||||
sticky=' '
|
||||
# If permission number is given calc string
|
||||
if [[ $1 =~ ^-?[0-9]+$ ]]
|
||||
if [[ $1 =~ ^-?[0-9]+$ && ${#1} -ge 2 && ${#1} -le 4 ]]
|
||||
then
|
||||
p_n=$1
|
||||
for (( i=0; i<${#1}; i++ ))
|
||||
do
|
||||
num=$(echo "obase=2;${1:$i:1}" | bc | xargs printf '%03d')
|
||||
[ ${num:0:1} -eq 1 ] && p_s+='r' || p_s+='-'
|
||||
[ ${num:1:1} -eq 1 ] && p_s+='w' || p_s+='-'
|
||||
[ ${num:2:1} -eq 1 ] && p_s+='x' || p_s+='-'
|
||||
[ ${num:0:1} -eq 1 ] && R+=('X') || R+=(' ')
|
||||
[ ${num:1:1} -eq 1 ] && W+=('X') || W+=(' ')
|
||||
[ ${num:2:1} -eq 1 ] && X+=('X') || X+=(' ')
|
||||
if [[ ${#1} -eq 4 && $i -eq 0 ]]
|
||||
then
|
||||
[ ${num:0:1} -eq 1 ] && setuid='X' || setuid=' '
|
||||
[ ${num:1:1} -eq 1 ] && setgid='X' || setgid=' '
|
||||
[ ${num:2:1} -eq 1 ] && sticky='X' || sticky=' '
|
||||
else
|
||||
[ ${num:0:1} -eq 1 ] && p_s+='r' || p_s+='-'
|
||||
[ ${num:1:1} -eq 1 ] && p_s+='w' || p_s+='-'
|
||||
if [[ $sticky == 'X' ]]
|
||||
then
|
||||
[ ${num:2:1} -eq 1 ] && p_s+='s' || p_s+='S'
|
||||
else
|
||||
[ ${num:2:1} -eq 1 ] && p_s+='x' || p_s+='-'
|
||||
fi
|
||||
[ ${num:0:1} -eq 1 ] && R+=('X') || R+=(' ')
|
||||
[ ${num:1:1} -eq 1 ] && W+=('X') || W+=(' ')
|
||||
[ ${num:2:1} -eq 1 ] && X+=('X') || X+=(' ')
|
||||
fi
|
||||
done
|
||||
# If permission string is given calc number
|
||||
else
|
||||
elif [[ ${#1} -le 9 && $(( ${#1} % 3 )) -eq 0 && $1 =~ ^[r,w,x,s,S,-]+$ ]]
|
||||
then
|
||||
p_s=$1
|
||||
[[ ${p_s,,} =~ 's' ]] && p_n+="1" || p_n+="0"
|
||||
[[ ${p_s,,} =~ 's' ]] && sticky='X'
|
||||
for (( i=0; i<${#1}; i+=0 ))
|
||||
do
|
||||
num=0
|
||||
@@ -36,20 +56,26 @@ chmod_calc(){
|
||||
[[ ${1:$i:1} == 'w' ]] && W+=('X') || W+=(' ')
|
||||
[[ ${1:$((i++)):1} == 'w' ]] && let num++
|
||||
num=$(( num << 1 ))
|
||||
[[ ${1:$i:1} == 'x' ]] && X+=('X') || X+=(' ')
|
||||
[[ ${1:$((i++)):1} == 'x' ]] && let num++
|
||||
[[ 'xs' =~ ${1:$i:1} ]] && X+=('X') || X+=(' ')
|
||||
[[ 'Ss' =~ ${1:$i:1} ]] && S+=('X') || S+=(' ')
|
||||
[[ 'xs' =~ ${1:$((i++)):1} ]] && let num++
|
||||
p_n+="$num"
|
||||
done
|
||||
else
|
||||
printf "Invalid permissions string: $1"
|
||||
return 1
|
||||
fi
|
||||
# Print Final results table
|
||||
printf "
|
||||
Linux Permissions String:\t${p_s}
|
||||
Linux Permissions Number:\t${p_n}
|
||||
|
||||
Owner\t\tGroup\t\tPublic
|
||||
Special\t\tOwner\t\tGroup\t\tPublic
|
||||
|
||||
Setuid [$setuid]\tRead [${R[0]}]\tRead [${R[1]}]\tRead [${R[2]}]
|
||||
Setgid [$setgid]\tWrite [${W[0]}]\tWrite [${W[1]}]\tWrite [${W[2]}]
|
||||
Sticky bit [$sticky]\tExecute [${X[0]}]\tExecute [${X[1]}]\tExecute [${X[2]}]
|
||||
|
||||
Read [${R[0]}]\tRead [${R[1]}]\tRead [${R[2]}]
|
||||
Write [${W[0]}]\tWrite [${W[1]}]\tWrite [${W[2]}]
|
||||
Execute [${X[0]}]\tExecute [${X[1]}]\tExecute [${X[2]}]
|
||||
"
|
||||
}
|
||||
|
||||
|
||||
Regular → Executable
+73
-68
@@ -3,108 +3,113 @@
|
||||
# Written by Erez Binyamin (github.com/ErezBinyamin)
|
||||
|
||||
# Search for an integer sequence
|
||||
oeis() {
|
||||
# USAGE:
|
||||
# oeis <language> <sequence ID>
|
||||
# oeis <sequence ID> <language>
|
||||
# oeis <val_a, val_b, val_c, ...>
|
||||
oeis() (
|
||||
local URL='https://oeis.org'
|
||||
local TMP=/tmp/oeis
|
||||
local DOC=/tmp/oeis/doc.html
|
||||
local MAX_TERMS=10
|
||||
mkdir -p $TMP
|
||||
# Get short description of a sequence
|
||||
get_desc() {
|
||||
grep -A 1 '<td valign=top align=left>' $DOC \
|
||||
| sed '/<td valign=top align=left>/d; /--/d; s/^[ \t]*//; s/<[^>]*>//g;' \
|
||||
| sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</</g; s/"/"/g'
|
||||
return $?
|
||||
}
|
||||
# Print out the first MAX_TERMS terms of a sequence
|
||||
get_seq() {
|
||||
local MAX_TERMS=${1}
|
||||
grep -o '<tt>.*, .*[0-9]</tt>' $DOC \
|
||||
| sed 's/<[^>]*>//g' \
|
||||
| grep -v '[a-z]' \
|
||||
| grep -v ':' \
|
||||
| cut -d ',' -f 1-${MAX_TERMS}
|
||||
return $?
|
||||
}
|
||||
# Sample code parser INPUT arg: is grep regex <FROM*TO>
|
||||
parse_code() {
|
||||
local GREP_REGEX="${1}"
|
||||
cat $DOC \
|
||||
| tr '\n' '`' \
|
||||
| grep -o "${GREP_REGEX}" \
|
||||
| tr '`' '\n' \
|
||||
| sed 's/^[ \t]*//; s/<[^>]*>//g; /^\s*$/d;' \
|
||||
| sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</</g; s/"/"/g'
|
||||
return $?
|
||||
}
|
||||
# Search sequence by ID
|
||||
if [ $# -eq 1 ]
|
||||
if [ $# -lt 3 ]
|
||||
then
|
||||
# Generate URL
|
||||
[[ ${1:0:1} == 'A' ]] && ID=${1:1} || ID=${1}
|
||||
# Arg-Parse ID, Generate URL
|
||||
echo $1 | grep -q -e [a-z] -e [B-Z] && ID=$2 || ID=$1
|
||||
echo $1 | grep -q -e [a-z] -e [B-Z] && LANG=$1 || LANG=$2
|
||||
[[ ${ID:0:1} == 'A' ]] && ID=${ID:1}
|
||||
ID=$(bc <<< "$ID")
|
||||
ID="A$(printf '%06d' ${ID})"
|
||||
URL+="/${ID}"
|
||||
curl $URL 2>/dev/null > $DOC
|
||||
# ID
|
||||
# Print ID
|
||||
printf "ID: ${ID}\n"
|
||||
# Description
|
||||
grep -A 1 '<td valign=top align=left>' $DOC \
|
||||
| sed '/<td valign=top align=left>/d; /--/d; s/^[ \t]*//; s/<[^>]*>//g;' \
|
||||
| sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</</g; s/"/"/g'
|
||||
# Print Description
|
||||
get_desc
|
||||
printf "\n"
|
||||
# Sequence
|
||||
grep -o '<tt>.*, .*[0-9]</tt>' $DOC \
|
||||
| sed 's/<[^>]*>//g' \
|
||||
| grep -v '[a-z]' \
|
||||
| grep -v ':'
|
||||
# Print Sequence sample limited by $MAX_TERMS
|
||||
get_seq ${MAX_TERMS}
|
||||
printf "\n"
|
||||
# Code
|
||||
if grep -q 'MAPLE' $DOC
|
||||
# Print Code Sample
|
||||
if [[ ${LANG^^} == 'MAPLE' ]] && grep -q 'MAPLE' $DOC
|
||||
then
|
||||
GREP_REGEX='MAPLE.*CROSSREFS'
|
||||
grep -q 'PROG' $DOC && GREP_REGEX='MAPLE.*PROG'
|
||||
grep -q 'MATHEMATICA' $DOC && GREP_REGEX='MAPLE.*MATHEMATICA'
|
||||
cat $DOC \
|
||||
| tr '\n' '`' \
|
||||
| grep -o "${GREP_REGEX}" \
|
||||
| tr '`' '\n' \
|
||||
| sed 's/^[ \t]*//; s/<[^>]*>//g; /^\s*$/d;' \
|
||||
| sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</</g; s/"/"/g' \
|
||||
| sed 's/MAPLE/(MAPLE)/; /MATHEMATICA/d; /PROG/d; /CROSSREFS/d' \
|
||||
| pygmentize -f terminal256 -g -l python -P style=monokai
|
||||
printf "\n"
|
||||
GREP_REGEX='MAPLE.*CROSSREFS'
|
||||
grep -q 'PROG' $DOC && GREP_REGEX='MAPLE.*PROG'
|
||||
grep -q 'MATHEMATICA' $DOC && GREP_REGEX='MAPLE.*MATHEMATICA'
|
||||
parse_code "${GREP_REGEX}" \
|
||||
| sed 's/MAPLE/(MAPLE)/; /MATHEMATICA/d; /PROG/d; /CROSSREFS/d'
|
||||
fi
|
||||
if grep -q 'MATHEMATICA' $DOC
|
||||
if [[ ${LANG^^} == 'MATHEMATICA' ]] && grep -q 'MATHEMATICA' $DOC
|
||||
then
|
||||
GREP_REGEX='MATHEMATICA.*CROSSREFS'
|
||||
grep -q 'PROG' $DOC && GREP_REGEX='MATHEMATICA.*PROG'
|
||||
cat $DOC \
|
||||
| tr '\n' '`' \
|
||||
| grep -o "${GREP_REGEX}" \
|
||||
| tr '`' '\n' \
|
||||
| sed 's/^[ \t]*//; s/<[^>]*>//g; /^\s*$/d;' \
|
||||
| sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</</g; s/"/"/g' \
|
||||
| sed 's/MATHEMATICA/(MATHEMATICA)/; /PROG/d; /CROSSREFS/d' \
|
||||
| pygmentize -f terminal256 -g -l mathematica -P style=monokai
|
||||
printf "\n"
|
||||
GREP_REGEX='MATHEMATICA.*CROSSREFS'
|
||||
grep -q 'PROG' $DOC && GREP_REGEX='MATHEMATICA.*PROG'
|
||||
parse_code "${GREP_REGEX}" \
|
||||
| sed 's/MATHEMATICA/(MATHEMATICA)/; /PROG/d; /CROSSREFS/d'
|
||||
fi
|
||||
# PROG section language support
|
||||
cat $DOC \
|
||||
| tr '\n' '`' \
|
||||
| grep -o "PROG.*CROSSREFS" \
|
||||
| tr '`' '\n' \
|
||||
| sed 's/^[ \t]*//; s/<[^>]*>//g; /^\s*$/d;' \
|
||||
| sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</</g; s/"/"/g' \
|
||||
| sed '/PROG/d; /CROSSREFS/d' > ${TMP}/lang
|
||||
langs=("Axiom" "MAGMA" "PARI" "Python" "Sage" "Haskell" "Julia" "GAP" "Scala")
|
||||
for L in ${langs[@]}
|
||||
do
|
||||
echo "foo" | pygmentize -l ${L,,} &>/dev/null && PYG="${L,,}" || PYG="c"
|
||||
if grep -q "(${L})" $DOC
|
||||
then
|
||||
awk -v tgt="${L}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/lang \
|
||||
| pygmentize -f terminal256 -g -P style=monokai -l ${PYG}
|
||||
printf "\n"
|
||||
fi
|
||||
done
|
||||
# PROG section contains more code samples (Non Mathematica or Maple)
|
||||
parse_code "PROG.*CROSSREFS" \
|
||||
| sed '/PROG/d; /CROSSREFS/d' > ${TMP}/prog
|
||||
# Print out code sample for specified language
|
||||
rm -f ${TMP}/code_snippet
|
||||
awk -v tgt="${LANG^^}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet
|
||||
L="${LANG:0:1}"
|
||||
LANG="${LANG:1}"
|
||||
LANG="${L^^}${LANG,,}"
|
||||
[ $(wc -c < $TMP/code_snippet) -eq 0 ] && awk -v tgt="${LANG}" -F'[()]' '/^\(/{f=(tgt==$2)} f' ${TMP}/prog > ${TMP}/code_snippet
|
||||
cat ${TMP}/code_snippet
|
||||
# Search unknown sequence
|
||||
else
|
||||
# Build URL
|
||||
URL+="/search?q=signed%3A$(echo $@ | tr ' ' ',')"
|
||||
URL+="/search?q=signed%3A$(echo $@ | grep -v [a-z] | grep -v [A-Z] | tr ' ' ',')"
|
||||
curl $URL 2>/dev/null > $DOC
|
||||
# Sequence IDs
|
||||
grep -o '=id:.*&' $DOC \
|
||||
| sed 's/=id://; s/&//' > $TMP/id
|
||||
# Descriptions
|
||||
grep -A 1 '<td valign=top align=left>' $DOC \
|
||||
| sed '/<td valign=top align=left>/d; /--/d; s/^[ \t]*//; s/<[^>]*>//g' \
|
||||
| sed 's/ / /g; s/\&/\&/g; s/>/>/g; s/</</g; s/"/"/g' > $TMP/nam
|
||||
get_desc > $TMP/desc
|
||||
# Sequences
|
||||
grep -o '<tt>.*<b.*</tt>' $DOC \
|
||||
| sed 's/<[^>]*>//g' > $TMP/seq
|
||||
get_seq ${MAX_TERMS} > $TMP/seq
|
||||
# Print data for all
|
||||
readarray -t ID < $TMP/id
|
||||
readarray -t NAM < $TMP/nam
|
||||
readarray -t DESC < $TMP/desc
|
||||
readarray -t SEQ < $TMP/seq
|
||||
for i in ${!ID[@]}
|
||||
do
|
||||
printf "${ID[$i]}: ${NAM[$i]}\n"
|
||||
printf "${ID[$i]}: ${DESC[$i]}\n"
|
||||
echo ${SEQ[$i]}
|
||||
printf "\n"
|
||||
done
|
||||
fi
|
||||
}
|
||||
)
|
||||
|
||||
oeis $@
|
||||
|
||||
Reference in New Issue
Block a user