Automatic add

This commit is contained in:
Timo Schmidt 2023-05-09 02:08:01 +02:00
parent 58df9f5969
commit 7a5c14c3c5
1 changed files with 212 additions and 212 deletions

424
.bashrc
View File

@ -19,64 +19,64 @@ alias z='\zellij options --disable-mouse-mode'
alias zellij='\zellij options --disable-mouse-mode' alias zellij='\zellij options --disable-mouse-mode'
__ftest_file () { __ftest_file () {
file="${1}" file="${1}"
tmp_dir="${2}" tmp_dir="${2}"
test_file_name="$($(type -P basename) -- "${file%.c}_test.c")" test_file_name="$($(type -P basename) -- "${file%.c}_test.c")"
test_file="$(find "${tmp_dir}" -name "${test_file_name}" -type f)" test_file="$(find "${tmp_dir}" -name "${test_file_name}" -type f)"
if [ -z "${test_file}" ]; then if [ -z "${test_file}" ]; then
printf '\033[31m%s\033[m\n' "No test file for file '${file}'" printf '\033[31m%s\033[m\n' "No test file for file '${file}'"
return 1 return 1
fi fi
${test_file} ${test_file}
} }
ftest () { ftest () {
err="0" err="0"
if [ -z "${tmp_dir}" ]; then if [ -z "${tmp_dir}" ]; then
tmp_dir="$(mktemp -dt -- '42tests-tischmid.XXXXXXXX')" tmp_dir="$(mktemp -dt -- '42tests-tischmid.XXXXXXXX')"
git clone --quiet 'https://git.timo.one/42berlin/42tests-tischmid.git' "${tmp_dir}" git clone --quiet 'https://git.timo.one/42berlin/42tests-tischmid.git' "${tmp_dir}"
fi fi
if [ -z "${@}" ]; then if [ -z "${@}" ]; then
for file in $(find . -mindepth 1 -type f -name '*.c'); do for file in $(find . -mindepth 1 -type f -name '*.c'); do
__ftest_file "${file}" "${tmp_dir}" || err="1" __ftest_file "${file}" "${tmp_dir}" || err="1"
done done
else else
while [ -z "${1}" ]; do while [ -z "${1}" ]; do
if [ -f "${1}" ]; then if [ -f "${1}" ]; then
__ftest_file "${1}" "${tmp_dir}" || err="1" __ftest_file "${1}" "${tmp_dir}" || err="1"
else else
printf '\033[31m%s\033[m\n' "File ${1} does not exist" printf '\033[31m%s\033[m\n' "File ${1} does not exist"
fi fi
shift shift
done done
fi fi
rm -rf -- "/tmp/42tests-tischmid.*" rm -rf -- "/tmp/42tests-tischmid.*"
unset -v -- "tmp_dir" unset -v -- "tmp_dir"
return -- "${err}" return -- "${err}"
} }
dapt () { dapt () {
ACTION="${1}" ACTION="${1}"
shift shift
CMD="RUN apt ${ACTION} -yy ${@}" CMD="RUN apt ${ACTION} -yy ${@}"
echo "${CMD}" >> ~/repos-other/docker/Dockerfile echo "${CMD}" >> ~/repos-other/docker/Dockerfile
docker build -t ubuntu-bloated ~/repos-other/docker/ docker build -t ubuntu-bloated ~/repos-other/docker/
docker rmi $(docker images -f "dangling=true" -q) 2>/dev/null docker rmi $(docker images -f "dangling=true" -q) 2>/dev/null
} }
norminette () { norminette () {
version="$($(type -P norminette) -v | cut -d" " -f2)" version="$($(type -P norminette) -v | cut -d" " -f2)"
if [ ! "${version}" = "3.3.50" ]; then if [ ! "${version}" = "3.3.50" ]; then
printf "%s\n" "Your norminette version is not 3.3.50, but ${version}, which will not catch some indentation errors. Please up-/downgrade" printf "%s\n" "Your norminette version is not 3.3.50, but ${version}, which will not catch some indentation errors. Please up-/downgrade"
fi fi
$(type -P norminette) -R CheckForbiddenSourceHeader "$@" $(type -P norminette) -R CheckForbiddenSourceHeader "$@"
} }
jmp () { jmp () {
cd "$(find . -name "${1}" -not -wholename "*mouli*" -print -quit)" cd "$(find . -name "${1}" -not -wholename "*mouli*" -print -quit)"
if [ "${?}" = "0" ] ; then if [ "${?}" = "0" ] ; then
cd "$(find .. -name "${1}" -not -wholename "*mouli*" -print -quit)" cd "$(find .. -name "${1}" -not -wholename "*mouli*" -print -quit)"
fi fi
} }
xset r rate 200 60 xset r rate 200 60
@ -85,258 +85,258 @@ PATH="${PATH}:~/.local/bin"
GIT_PROMPT="1" GIT_PROMPT="1"
if [ ! -f ~/git-prompt.sh ] && [ "${GIT_PROMPT}" = "1" ] ; then if [ ! -f ~/git-prompt.sh ] && [ "${GIT_PROMPT}" = "1" ] ; then
curl --silent --location 'https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh' -o ~/git-prompt.sh curl --silent --location 'https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh' -o ~/git-prompt.sh
fi fi
__norm () { __norm () {
[ -z "${PWD##*ft*}" ] || { return 1; } [ -z "${PWD##*ft*}" ] || { return 1; }
norminette 1>/dev/null 2>&1 && printf ' \033[92m%s\033[m' "[Norm: OK]" || printf ' \033[101;37m%s\033[m' "[䝝誒 ‼ NORM ‼ 屌誒]" norminette 1>/dev/null 2>&1 && printf ' \033[92m%s\033[m' "[Norm: OK]" || printf ' \033[101;37m%s\033[m' "[䝝誒 ‼ NORM ‼ 屌誒]"
return 0 return 0
} }
if [ -f ~/git-prompt.sh ] && [ "${GIT_PROMPT}" = "1" ] ; then if [ -f ~/git-prompt.sh ] && [ "${GIT_PROMPT}" = "1" ] ; then
. ~/git-prompt.sh . ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1 export GIT_PS1_SHOWDIRTYSTATE=1
PS1='\[\033[31m\]\u\[\033[m\]@\[\033[32m\]\h\[\033[m\] \[\033[33m\][\w]\[\033[m\]\[\033[36m\]$(__git_ps1 " (%s)")\[\033[m\]\[\033[36m\]$(__norm)\[\033[m\]\n\[\033[35m\]~>\[\033[m\] ' PS1='\[\033[31m\]\u\[\033[m\]@\[\033[32m\]\h\[\033[m\] \[\033[33m\][\w]\[\033[m\]\[\033[36m\]$(__git_ps1 " (%s)")\[\033[m\]\[\033[36m\]$(__norm)\[\033[m\]\n\[\033[35m\]~>\[\033[m\] '
else else
PS1='\[\033[31m\]\u\[\033[m\]@\[\033[32m\]\h\[\033[m\] \[\033[33m\][\w]\[\033[m\]\[\033[36m\]$(__norm)\[\033[m\]\n\[\033[36m\]~>\[\033[m\] ' PS1='\[\033[31m\]\u\[\033[m\]@\[\033[32m\]\h\[\033[m\] \[\033[33m\][\w]\[\033[m\]\[\033[36m\]$(__norm)\[\033[m\]\n\[\033[36m\]~>\[\033[m\] '
fi fi
##----------------------------------------------------- ##-----------------------------------------------------
## synth-shell-greeter.sh ## synth-shell-greeter.sh
if [ -f ~/.config/synth-shell/synth-shell-greeter.sh ] && [ -n "$( echo $- | grep i )" ] ; then if [ -f ~/.config/synth-shell/synth-shell-greeter.sh ] && [ -n "$( echo $- | grep i )" ] ; then
: :
# source ~/.config/synth-shell/synth-shell-greeter.sh # source ~/.config/synth-shell/synth-shell-greeter.sh
fi fi
##----------------------------------------------------- ##-----------------------------------------------------
## synth-shell-prompt.sh ## synth-shell-prompt.sh
if [ -f ~/.config/synth-shell/synth-shell-prompt.sh ] && [ -n "$( echo $- | grep i )" ] ; then if [ -f ~/.config/synth-shell/synth-shell-prompt.sh ] && [ -n "$( echo $- | grep i )" ] ; then
: :
# source ~/.config/synth-shell/synth-shell-prompt.sh # source ~/.config/synth-shell/synth-shell-prompt.sh
fi fi
##----------------------------------------------------- ##-----------------------------------------------------
## better-ls ## better-ls
if [ -f ~/.config/synth-shell/better-ls.sh ] && [ -n "$( echo $- | grep i )" ] ; then if [ -f ~/.config/synth-shell/better-ls.sh ] && [ -n "$( echo $- | grep i )" ] ; then
: :
# source ~/.config/synth-shell/better-ls.sh # source ~/.config/synth-shell/better-ls.sh
fi fi
##----------------------------------------------------- ##-----------------------------------------------------
## alias ## alias
if [ -f ~/.config/synth-shell/alias.sh ] && [ -n "$( echo $- | grep i )" ] ; then if [ -f ~/.config/synth-shell/alias.sh ] && [ -n "$( echo $- | grep i )" ] ; then
source ~/.config/synth-shell/alias.sh source ~/.config/synth-shell/alias.sh
fi fi
##----------------------------------------------------- ##-----------------------------------------------------
## better-history ## better-history
if [ -f ~/.config/synth-shell/better-history.sh ] && [ -n "$( echo $- | grep i )" ] ; then if [ -f ~/.config/synth-shell/better-history.sh ] && [ -n "$( echo $- | grep i )" ] ; then
source ~/.config/synth-shell/better-history.sh source ~/.config/synth-shell/better-history.sh
fi fi
check () { check () {
# Set params # Set params
URL="${1}" URL="${1}"
DIR="/tmp/tmp_repo_$(date +%s)" DIR="/tmp/tmp_repo_$(date +%s)"
# If no URL, get it from current repo # If no URL, get it from current repo
if [ -z "${URL}" ] ; then if [ -z "${URL}" ] ; then
URL="$(git remote get-url origin)" URL="$(git remote get-url origin)"
fi fi
# Clone in temp folder # Clone in temp folder
git clone --quiet "${URL}" "${DIR}" git clone --quiet "${URL}" "${DIR}"
# Only proceed if clone success # Only proceed if clone success
if [ -d "${DIR}" ] ; then if [ -d "${DIR}" ] ; then
# Check the norm and print beautiful message # Check the norm and print beautiful message
norminette -R CheckForbiddenSourceHeader "${DIR}" && printf '\033[30;102m%s\033[m\n' "Norminette success" || printf '\033[30;101m%s\033[m\n' "Norminette fail!!!" norminette -R CheckForbiddenSourceHeader "${DIR}" && printf '\033[30;102m%s\033[m\n' "Norminette success" || printf '\033[30;101m%s\033[m\n' "Norminette fail!!!"
# Remove temp folder # Remove temp folder
rm -rf "${DIR}" rm -rf "${DIR}"
else else
printf '\033[30;101m%s\033[m\n' "Could not clone the repo" printf '\033[30;101m%s\033[m\n' "Could not clone the repo"
fi fi
} }
safe_guard_name () { safe_guard_name () {
printf '%s' "${1}" | tr \\'!@#$%^&*()_+~{}|:">[]?<, ./ ;=\t\-'\'\` '__________________________________' printf '%s' "${1}" | tr \\'!@#$%^&*()_+~{}|:">[]?<, ./ ;=\t\-'\'\` '__________________________________'
} }
timoulinette () { timoulinette () {
# Set params # Set params
subject="${1}" subject="${1}"
ex="${2}" ex="${2}"
extras="${3}" extras="${3}"
# Arghandle # Arghandle
if [ -z "${subject}" ] ; then if [ -z "${subject}" ] ; then
printf '%s\n' "Must provide at least one argument. Exiting" printf '%s\n' "Must provide at least one argument. Exiting"
return 1 return 1
fi fi
# If exercise was not provided, get it from current folder name # If exercise was not provided, get it from current folder name
if [ -z "${ex}" ] ; then if [ -z "${ex}" ] ; then
ex="$(basename -- "${PWD}")" ex="$(basename -- "${PWD}")"
fi fi
# Search for a compiler # Search for a compiler
if command -v cc >/dev/null 2>&1 ; then if command -v cc >/dev/null 2>&1 ; then
CC="$(command -v cc)" CC="$(command -v cc)"
elif command -v gcc >/dev/null 2>&1 ; then elif command -v gcc >/dev/null 2>&1 ; then
CC="$(command -v gcc)" CC="$(command -v gcc)"
elif command -v clang >/dev/null 2>&1 ; then elif command -v clang >/dev/null 2>&1 ; then
CC="$(command -v clang)" CC="$(command -v clang)"
else else
printf '%s\n' "Could not find a compiler. Exiting" printf '%s\n' "Could not find a compiler. Exiting"
return return
fi fi
git_base="https://git.timo.one/42berlin/piscine-timo-moulinette/raw/branch/master" git_base="https://git.timo.one/42berlin/piscine-timo-moulinette/raw/branch/master"
# Find the first C file # Find the first C file
file_name="$(find . -mindepth 1 -maxdepth 1 -name '*.c' -not -name '*mouli*' -type f -exec basename {} \; -quit)" file_name="$(find . -mindepth 1 -maxdepth 1 -name '*.c' -not -name '*mouli*' -type f -exec basename {} \; -quit)"
# If no C files found, exit # If no C files found, exit
if [ -z "${file_name}" ] ; then if [ -z "${file_name}" ] ; then
printf '%s\n' "Could not find any C files in the current directory. Exiting" printf '%s\n' "Could not find any C files in the current directory. Exiting"
return 2 return 2
fi fi
# Testfiles have the "_test.c" suffix # Testfiles have the "_test.c" suffix
test_file_name="$(printf '%s' "${file_name}" | sed 's/\.c$/_test.c/g')" test_file_name="$(printf '%s' "${file_name}" | sed 's/\.c$/_test.c/g')"
# Full git url to raw file # Full git url to raw file
git_full="${git_base}/${subject}/${ex}/${test_file_name}" git_full="${git_base}/${subject}/${ex}/${test_file_name}"
# Name of the C testfile # Name of the C testfile
download_file_name="./timoulinette${subject}_${ex}_${test_file_name}" download_file_name="./timoulinette${subject}_${ex}_${test_file_name}"
# Download tesfile # Download tesfile
curl -sL "${git_full}" -o "${download_file_name}" curl -sL "${git_full}" -o "${download_file_name}"
if [ ! "${?}" = "0" ] ; then if [ ! "${?}" = "0" ] ; then
printf '%s\n' "There's now testfile for this task, sorry!" printf '%s\n' "There's now testfile for this task, sorry!"
printf '%s\n' "Download url is" printf '%s\n' "Download url is"
printf '%s\n' "${git_full}" printf '%s\n' "${git_full}"
rm -f -- "${download_file_name}" rm -f -- "${download_file_name}"
return return
fi fi
# Preprocess source file (remove comments) # Preprocess source file (remove comments)
${CC} -dD -E -o "${file_name}.nocomments" -- "${file_name}" ${CC} -dD -E -o "${file_name}.nocomments" -- "${file_name}"
# Extract function definitions from preprocessed file and append semicolon # Extract function definitions from preprocessed file and append semicolon
prototypes="$(cat -- "${file_name}.nocomments" | grep -v '#' | grep ')$' | grep -v ' (' | grep -v ' ' | grep '[a-zA-Z0-9](' | grep -v 'int[[:space:]]\+main(' | sed 's/$/ ;/')" prototypes="$(cat -- "${file_name}.nocomments" | grep -v '#' | grep ')$' | grep -v ' (' | grep -v ' ' | grep '[a-zA-Z0-9](' | grep -v 'int[[:space:]]\+main(' | sed 's/$/ ;/')"
# Extract macros from file (ignore the ones with line continuations) # Extract macros from file (ignore the ones with line continuations)
macros="$(cat -- "${file_name}" | grep '^#' | grep -v '\\$')" macros="$(cat -- "${file_name}" | grep '^#' | grep -v '\\$')"
# Remove preprocessed file # Remove preprocessed file
rm -f -- "${file_name}.nocomments" rm -f -- "${file_name}.nocomments"
# Write prototypes to beginning of testfile # Write prototypes to beginning of testfile
IFS=" IFS="
" "
for proto in ${prototypes} ; do for proto in ${prototypes} ; do
printf '%s\n' "${proto}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" printf '%s\n' "${proto}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
done done
# Write macros to beginning of testfile # Write macros to beginning of testfile
IFS=" IFS="
" "
for macro in ${macros} ; do for macro in ${macros} ; do
guard_name="$(safe_guard_name "${macro}")" guard_name="$(safe_guard_name "${macro}")"
# Endif include guard # Endif include guard
printf '%s\n\n' "#endif" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" printf '%s\n\n' "#endif" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
# The macro itself # The macro itself
printf '%s\n' "${macro}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" printf '%s\n' "${macro}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
# Define include guard # Define include guard
printf '%s\n' "# define ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" printf '%s\n' "# define ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
# Include guard # Include guard
printf '%s\n' "#ifndef ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" printf '%s\n' "#ifndef ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c"
mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" mv -- ".tmpaddfirstlinefile.c" "${download_file_name}"
done done
IFS="$(printf ' \n\t')" IFS="$(printf ' \n\t')"
# Compile # Compile
out_binary="timoulinette${subject}_${ex}_$(basename -- "${test_file_name}" .c).out" out_binary="timoulinette${subject}_${ex}_$(basename -- "${test_file_name}" .c).out"
${CC} -Wall -Wextra -Werror -o "${out_binary}" -xc -- *.c ${CC} -Wall -Wextra -Werror -o "${out_binary}" -xc -- *.c
err_code="${?}" err_code="${?}"
if [ ! "${err_code}" = 0 ] ; then if [ ! "${err_code}" = 0 ] ; then
printf '%s\n' "Could not compile. Exiting" printf '%s\n' "Could not compile. Exiting"
return return
fi fi
# Delete testfile # Delete testfile
if [ "$(printf '%s' "${extras}" | sed 's/keep-test//g')" = "${extras}" ] ; then if [ "$(printf '%s' "${extras}" | sed 's/keep-test//g')" = "${extras}" ] ; then
rm -f -- "${download_file_name}" rm -f -- "${download_file_name}"
fi fi
# Run binary # Run binary
./${out_binary} ./${out_binary}
if [ "${?}" = "0" ] && [ "$(printf '%s' "${extras}" | sed 's/keep-binary//g')" = "${extras}" ] ; then if [ "${?}" = "0" ] && [ "$(printf '%s' "${extras}" | sed 's/keep-binary//g')" = "${extras}" ] ; then
rm -f -- "./${out_binary}" rm -f -- "./${out_binary}"
fi fi
} }
prev () { prev () {
if [ -z "${1}" ] ; then if [ -z "${1}" ] ; then
ex="$(basename "${PWD}")" ex="$(basename "${PWD}")"
ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')" ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')"
next_ex_num="$(( ${ex_num} - 1 ))" next_ex_num="$(( ${ex_num} - 1 ))"
else else
next_ex_num="${1}" next_ex_num="${1}"
fi fi
builtin printf -v padded '%02d' "${next_ex_num}" builtin printf -v padded '%02d' "${next_ex_num}"
next_dir="ex${padded}" next_dir="ex${padded}"
builtin cd "../${next_dir}" builtin cd "../${next_dir}"
} }
next () { next () {
# If no arg, calculate next num # If no arg, calculate next num
if [ -z "${1}" ] ; then if [ -z "${1}" ] ; then
# Exercise name # Exercise name
ex="$(basename "${PWD}")" ex="$(basename "${PWD}")"
# Exercise num # Exercise num
ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')" ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')"
# + 1 for next # + 1 for next
next_ex_num="$(( ${ex_num} + 1 ))" next_ex_num="$(( ${ex_num} + 1 ))"
else else
next_ex_num="${1}" next_ex_num="${1}"
fi fi
# Pad to two 0's and save in variable "padded" # Pad to two 0's and save in variable "padded"
builtin printf -v padded '%02d' "${next_ex_num}" builtin printf -v padded '%02d' "${next_ex_num}"
next_dir="ex${padded}" next_dir="ex${padded}"
builtin cd "../${next_dir}" builtin cd "../${next_dir}"
} }
clone42 () { clone42 () {
folder="${1}" folder="${1}"
repo_url="${2}" repo_url="${2}"
\unset -f norminette \unset -f norminette
git clone --quiet "${repo_url}" ${folder} git clone --quiet "${repo_url}" ${folder}
if [ "${?}" = "0" ]; then if [ "${?}" = "0" ]; then
cd "${folder}" cd "${folder}"
\norminette -R CheckForbiddenSourceHeader "." \norminette -R CheckForbiddenSourceHeader "."
else else
printf '%s\n' "Could not clone repo!" printf '%s\n' "Could not clone repo!"
fi fi
} }
export MAIL='timo42@proton.me' export MAIL='timo42@proton.me'