From 7a5c14c3c50c5c4152d110e1bebfeec1d5d8f23f Mon Sep 17 00:00:00 2001 From: Timo Schmidt Date: Tue, 9 May 2023 02:08:01 +0200 Subject: [PATCH] Automatic add --- .bashrc | 424 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 212 insertions(+), 212 deletions(-) diff --git a/.bashrc b/.bashrc index cf4851b..9017e29 100644 --- a/.bashrc +++ b/.bashrc @@ -19,64 +19,64 @@ alias z='\zellij options --disable-mouse-mode' alias zellij='\zellij options --disable-mouse-mode' __ftest_file () { - file="${1}" - tmp_dir="${2}" + file="${1}" + tmp_dir="${2}" - test_file_name="$($(type -P basename) -- "${file%.c}_test.c")" - test_file="$(find "${tmp_dir}" -name "${test_file_name}" -type f)" - if [ -z "${test_file}" ]; then - printf '\033[31m%s\033[m\n' "No test file for file '${file}'" - return 1 - fi - ${test_file} + test_file_name="$($(type -P basename) -- "${file%.c}_test.c")" + test_file="$(find "${tmp_dir}" -name "${test_file_name}" -type f)" + if [ -z "${test_file}" ]; then + printf '\033[31m%s\033[m\n' "No test file for file '${file}'" + return 1 + fi + ${test_file} } ftest () { - err="0" - if [ -z "${tmp_dir}" ]; then - tmp_dir="$(mktemp -dt -- '42tests-tischmid.XXXXXXXX')" - git clone --quiet 'https://git.timo.one/42berlin/42tests-tischmid.git' "${tmp_dir}" - fi - if [ -z "${@}" ]; then - for file in $(find . -mindepth 1 -type f -name '*.c'); do - __ftest_file "${file}" "${tmp_dir}" || err="1" - done - else - while [ -z "${1}" ]; do - if [ -f "${1}" ]; then - __ftest_file "${1}" "${tmp_dir}" || err="1" - else - printf '\033[31m%s\033[m\n' "File ${1} does not exist" - fi - shift - done - fi - rm -rf -- "/tmp/42tests-tischmid.*" - unset -v -- "tmp_dir" - return -- "${err}" + err="0" + if [ -z "${tmp_dir}" ]; then + tmp_dir="$(mktemp -dt -- '42tests-tischmid.XXXXXXXX')" + git clone --quiet 'https://git.timo.one/42berlin/42tests-tischmid.git' "${tmp_dir}" + fi + if [ -z "${@}" ]; then + for file in $(find . -mindepth 1 -type f -name '*.c'); do + __ftest_file "${file}" "${tmp_dir}" || err="1" + done + else + while [ -z "${1}" ]; do + if [ -f "${1}" ]; then + __ftest_file "${1}" "${tmp_dir}" || err="1" + else + printf '\033[31m%s\033[m\n' "File ${1} does not exist" + fi + shift + done + fi + rm -rf -- "/tmp/42tests-tischmid.*" + unset -v -- "tmp_dir" + return -- "${err}" } dapt () { - ACTION="${1}" - shift - CMD="RUN apt ${ACTION} -yy ${@}" - echo "${CMD}" >> ~/repos-other/docker/Dockerfile - docker build -t ubuntu-bloated ~/repos-other/docker/ - docker rmi $(docker images -f "dangling=true" -q) 2>/dev/null + ACTION="${1}" + shift + CMD="RUN apt ${ACTION} -yy ${@}" + echo "${CMD}" >> ~/repos-other/docker/Dockerfile + docker build -t ubuntu-bloated ~/repos-other/docker/ + docker rmi $(docker images -f "dangling=true" -q) 2>/dev/null } norminette () { - version="$($(type -P norminette) -v | cut -d" " -f2)" - 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" - fi - $(type -P norminette) -R CheckForbiddenSourceHeader "$@" + version="$($(type -P norminette) -v | cut -d" " -f2)" + 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" + fi + $(type -P norminette) -R CheckForbiddenSourceHeader "$@" } jmp () { - cd "$(find . -name "${1}" -not -wholename "*mouli*" -print -quit)" - if [ "${?}" = "0" ] ; then - cd "$(find .. -name "${1}" -not -wholename "*mouli*" -print -quit)" - fi + cd "$(find . -name "${1}" -not -wholename "*mouli*" -print -quit)" + if [ "${?}" = "0" ] ; then + cd "$(find .. -name "${1}" -not -wholename "*mouli*" -print -quit)" + fi } xset r rate 200 60 @@ -85,258 +85,258 @@ PATH="${PATH}:~/.local/bin" GIT_PROMPT="1" 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 __norm () { - [ -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 ‼ 屌誒]" - return 0 + [ -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 ‼ 屌誒]" + return 0 } if [ -f ~/git-prompt.sh ] && [ "${GIT_PROMPT}" = "1" ] ; then - . ~/git-prompt.sh - 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\] ' + . ~/git-prompt.sh + 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\] ' 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 ##----------------------------------------------------- ## synth-shell-greeter.sh 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 ##----------------------------------------------------- ## synth-shell-prompt.sh 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 ##----------------------------------------------------- ## better-ls 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 ##----------------------------------------------------- ## alias 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 ##----------------------------------------------------- ## better-history 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 check () { - # Set params - URL="${1}" - DIR="/tmp/tmp_repo_$(date +%s)" + # Set params + URL="${1}" + DIR="/tmp/tmp_repo_$(date +%s)" - # If no URL, get it from current repo - if [ -z "${URL}" ] ; then - URL="$(git remote get-url origin)" - fi + # If no URL, get it from current repo + if [ -z "${URL}" ] ; then + URL="$(git remote get-url origin)" + fi - # Clone in temp folder - git clone --quiet "${URL}" "${DIR}" + # Clone in temp folder + git clone --quiet "${URL}" "${DIR}" - # Only proceed if clone success - if [ -d "${DIR}" ] ; then - # 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!!!" + # Only proceed if clone success + if [ -d "${DIR}" ] ; then + # 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!!!" - # Remove temp folder - rm -rf "${DIR}" - else - printf '\033[30;101m%s\033[m\n' "Could not clone the repo" - fi + # Remove temp folder + rm -rf "${DIR}" + else + printf '\033[30;101m%s\033[m\n' "Could not clone the repo" + fi } safe_guard_name () { - printf '%s' "${1}" | tr \\'!@#$%^&*()_+~{}|:">[]?<, ./ ;=\t\-'\'\` '__________________________________' + printf '%s' "${1}" | tr \\'!@#$%^&*()_+~{}|:">[]?<, ./ ;=\t\-'\'\` '__________________________________' } timoulinette () { - # Set params - subject="${1}" - ex="${2}" - extras="${3}" + # Set params + subject="${1}" + ex="${2}" + extras="${3}" - # Arghandle - if [ -z "${subject}" ] ; then - printf '%s\n' "Must provide at least one argument. Exiting" - return 1 - fi + # Arghandle + if [ -z "${subject}" ] ; then + printf '%s\n' "Must provide at least one argument. Exiting" + return 1 + fi - # If exercise was not provided, get it from current folder name - if [ -z "${ex}" ] ; then - ex="$(basename -- "${PWD}")" - fi + # If exercise was not provided, get it from current folder name + if [ -z "${ex}" ] ; then + ex="$(basename -- "${PWD}")" + fi - # Search for a compiler - if command -v cc >/dev/null 2>&1 ; then - CC="$(command -v cc)" - elif command -v gcc >/dev/null 2>&1 ; then - CC="$(command -v gcc)" - elif command -v clang >/dev/null 2>&1 ; then - CC="$(command -v clang)" - else - printf '%s\n' "Could not find a compiler. Exiting" - return - fi + # Search for a compiler + if command -v cc >/dev/null 2>&1 ; then + CC="$(command -v cc)" + elif command -v gcc >/dev/null 2>&1 ; then + CC="$(command -v gcc)" + elif command -v clang >/dev/null 2>&1 ; then + CC="$(command -v clang)" + else + printf '%s\n' "Could not find a compiler. Exiting" + return + 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 - file_name="$(find . -mindepth 1 -maxdepth 1 -name '*.c' -not -name '*mouli*' -type f -exec basename {} \; -quit)" + # Find the first C file + file_name="$(find . -mindepth 1 -maxdepth 1 -name '*.c' -not -name '*mouli*' -type f -exec basename {} \; -quit)" - # If no C files found, exit - if [ -z "${file_name}" ] ; then - printf '%s\n' "Could not find any C files in the current directory. Exiting" - return 2 - fi + # If no C files found, exit + if [ -z "${file_name}" ] ; then + printf '%s\n' "Could not find any C files in the current directory. Exiting" + return 2 + fi - # Testfiles have the "_test.c" suffix - test_file_name="$(printf '%s' "${file_name}" | sed 's/\.c$/_test.c/g')" + # Testfiles have the "_test.c" suffix + test_file_name="$(printf '%s' "${file_name}" | sed 's/\.c$/_test.c/g')" - # Full git url to raw file - git_full="${git_base}/${subject}/${ex}/${test_file_name}" + # Full git url to raw file + git_full="${git_base}/${subject}/${ex}/${test_file_name}" - # Name of the C testfile - download_file_name="./timoulinette${subject}_${ex}_${test_file_name}" + # Name of the C testfile + download_file_name="./timoulinette${subject}_${ex}_${test_file_name}" - # Download tesfile - curl -sL "${git_full}" -o "${download_file_name}" + # Download tesfile + curl -sL "${git_full}" -o "${download_file_name}" - if [ ! "${?}" = "0" ] ; then - printf '%s\n' "There's now testfile for this task, sorry!" - printf '%s\n' "Download url is" - printf '%s\n' "${git_full}" - rm -f -- "${download_file_name}" - return - fi + if [ ! "${?}" = "0" ] ; then + printf '%s\n' "There's now testfile for this task, sorry!" + printf '%s\n' "Download url is" + printf '%s\n' "${git_full}" + rm -f -- "${download_file_name}" + return + fi - # Preprocess source file (remove comments) - ${CC} -dD -E -o "${file_name}.nocomments" -- "${file_name}" + # Preprocess source file (remove comments) + ${CC} -dD -E -o "${file_name}.nocomments" -- "${file_name}" - # 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/$/ ;/')" + # 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/$/ ;/')" - # Extract macros from file (ignore the ones with line continuations) - macros="$(cat -- "${file_name}" | grep '^#' | grep -v '\\$')" + # Extract macros from file (ignore the ones with line continuations) + macros="$(cat -- "${file_name}" | grep '^#' | grep -v '\\$')" - # Remove preprocessed file - rm -f -- "${file_name}.nocomments" + # Remove preprocessed file + rm -f -- "${file_name}.nocomments" - # Write prototypes to beginning of testfile - IFS=" + # Write prototypes to beginning of testfile + IFS=" " - for proto in ${prototypes} ; do - printf '%s\n' "${proto}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" - mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" - done + for proto in ${prototypes} ; do + printf '%s\n' "${proto}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" + mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" + done - # Write macros to beginning of testfile - IFS=" + # Write macros to beginning of testfile + IFS=" " - for macro in ${macros} ; do - guard_name="$(safe_guard_name "${macro}")" + for macro in ${macros} ; do + guard_name="$(safe_guard_name "${macro}")" - # Endif include guard - printf '%s\n\n' "#endif" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" - mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" + # Endif include guard + printf '%s\n\n' "#endif" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" + mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" - # The macro itself - printf '%s\n' "${macro}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" - mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" + # The macro itself + printf '%s\n' "${macro}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" + mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" - # Define include guard - printf '%s\n' "# define ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" - mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" + # Define include guard + printf '%s\n' "# define ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" + mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" - # Include guard - printf '%s\n' "#ifndef ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" - mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" - done - IFS="$(printf ' \n\t')" + # Include guard + printf '%s\n' "#ifndef ${guard_name}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" + mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" + done + IFS="$(printf ' \n\t')" - # Compile - out_binary="timoulinette${subject}_${ex}_$(basename -- "${test_file_name}" .c).out" - ${CC} -Wall -Wextra -Werror -o "${out_binary}" -xc -- *.c - err_code="${?}" + # Compile + out_binary="timoulinette${subject}_${ex}_$(basename -- "${test_file_name}" .c).out" + ${CC} -Wall -Wextra -Werror -o "${out_binary}" -xc -- *.c + err_code="${?}" - if [ ! "${err_code}" = 0 ] ; then - printf '%s\n' "Could not compile. Exiting" - return - fi + if [ ! "${err_code}" = 0 ] ; then + printf '%s\n' "Could not compile. Exiting" + return + fi - # Delete testfile - if [ "$(printf '%s' "${extras}" | sed 's/keep-test//g')" = "${extras}" ] ; then - rm -f -- "${download_file_name}" - fi + # Delete testfile + if [ "$(printf '%s' "${extras}" | sed 's/keep-test//g')" = "${extras}" ] ; then + rm -f -- "${download_file_name}" + fi - # Run binary - ./${out_binary} - if [ "${?}" = "0" ] && [ "$(printf '%s' "${extras}" | sed 's/keep-binary//g')" = "${extras}" ] ; then - rm -f -- "./${out_binary}" - fi + # Run binary + ./${out_binary} + if [ "${?}" = "0" ] && [ "$(printf '%s' "${extras}" | sed 's/keep-binary//g')" = "${extras}" ] ; then + rm -f -- "./${out_binary}" + fi } prev () { - if [ -z "${1}" ] ; then - ex="$(basename "${PWD}")" - ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')" - next_ex_num="$(( ${ex_num} - 1 ))" - else - next_ex_num="${1}" - fi - builtin printf -v padded '%02d' "${next_ex_num}" - next_dir="ex${padded}" - builtin cd "../${next_dir}" + if [ -z "${1}" ] ; then + ex="$(basename "${PWD}")" + ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')" + next_ex_num="$(( ${ex_num} - 1 ))" + else + next_ex_num="${1}" + fi + builtin printf -v padded '%02d' "${next_ex_num}" + next_dir="ex${padded}" + builtin cd "../${next_dir}" } next () { - # If no arg, calculate next num - if [ -z "${1}" ] ; then - # Exercise name - ex="$(basename "${PWD}")" + # If no arg, calculate next num + if [ -z "${1}" ] ; then + # Exercise name + ex="$(basename "${PWD}")" - # Exercise num - ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')" + # Exercise num + ex_num="$(printf '%s' "${ex}" | cut -c3- | sed 's/^0*//g')" - # + 1 for next - next_ex_num="$(( ${ex_num} + 1 ))" - else - next_ex_num="${1}" - fi + # + 1 for next + next_ex_num="$(( ${ex_num} + 1 ))" + else + next_ex_num="${1}" + fi - # Pad to two 0's and save in variable "padded" - builtin printf -v padded '%02d' "${next_ex_num}" + # Pad to two 0's and save in variable "padded" + builtin printf -v padded '%02d' "${next_ex_num}" - next_dir="ex${padded}" - builtin cd "../${next_dir}" + next_dir="ex${padded}" + builtin cd "../${next_dir}" } clone42 () { - folder="${1}" - repo_url="${2}" - \unset -f norminette + folder="${1}" + repo_url="${2}" + \unset -f norminette - git clone --quiet "${repo_url}" ${folder} - if [ "${?}" = "0" ]; then - cd "${folder}" - \norminette -R CheckForbiddenSourceHeader "." - else - printf '%s\n' "Could not clone repo!" - fi + git clone --quiet "${repo_url}" ${folder} + if [ "${?}" = "0" ]; then + cd "${folder}" + \norminette -R CheckForbiddenSourceHeader "." + else + printf '%s\n' "Could not clone repo!" + fi } export MAIL='timo42@proton.me'