diff --git a/.bashrc b/.bashrc index 0db2151..71a47f6 100644 --- a/.bashrc +++ b/.bashrc @@ -2,8 +2,8 @@ alias v='vim' alias ..='cd ..' alias ...='cd ../..' alias tmux='tmux -2' -alias ft='cc -Wall -Wextra -Werror -o main -xc <(grep -v "////" *.c) && ./main; rm ./main 2>/dev/null; :' -alias ft2='cc -Wall -Wextra -Werror -o main -xc <(grep -v "////" *.c) && ./main' +alias ft='cc -Wall -Wextra -Werror -o main -xc -- <(grep -v "////" *.c) && ./main; rm -f -- ./main; :' +alias ft2='cc -Wall -Wextra -Werror -o main -xc -- <(grep -v "////" *.c) && ./main' alias git='EDITOR=vim git' alias cmatrix='cmatrix -cu3 -Cred' alias gca='git add * && git commit -m *' @@ -65,16 +65,24 @@ if [ -f /nfs/homes/tischmid/.config/synth-shell/better-history.sh ] && [ -n "$( fi check () { + # 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 + # 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 "${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" @@ -82,30 +90,89 @@ check () { } timo_moulinette () { + # Set params subject="${1}" ex="${2}" + + # Arghandle if [ -z "${subject}" ]; then - echo "Must provide at least one argument. Exiting" + 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}")" + 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 + git_base="https://git.timo.one/42berlin/piscine-timo-moulinette/raw/branch/master" - test_file_name="$(find . -mindepth 1 -maxdepth 1 -name '*.c' -type f -exec basename {} \; | sed 's/\.c$/_test.c/g')" - if [ -z "${test_file_name}" ]; then - echo "Could not find any C files in the current directory. Exiting" + # Find the first C file + file_name="$(find . -mindepth 1 -maxdepth 1 -name '*.c' ! -name '*moulinette*' -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 + + # 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}" - echo "${git_full}" + + # Name of the C testfile + download_file_name="./timo_moulinette_${subject}_${ex}_${test_file_name}" + + # Download tesfile + curl -sL "${git_full}" -o "${download_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 '[a-zA-Z0-9](' | sed 's/$/;/')" + + # Remove preprocessed file + rm -f -- "${file_name}.nocomments" + + # Write prototypes to beginning of testfile + IFS="$(printf '\n')" + for proto in "${prototypes}"; do + printf '%s\n' "${proto}" | cat -- - "${download_file_name}" > ".tmpaddfirstlinefile.c" + mv -- ".tmpaddfirstlinefile.c" "${download_file_name}" + done + IFS="$(printf ' \n\t')" + + # Compile + out_binary="timo_moulinette_${subject}_${ex}_$(basename -- "${test_file_name}" .c).out" + ${CC} -Wall -Wextra -Werror -o "${out_binary}" -xc -- *.c + + # Delete testfile + rm -f -- "${download_file_name}" + + # Run binary + ./${out_binary} + rm -f -- "./${out_binary}" } prev () { - ex="$(basename "${PWD}")" - ex_num="$(echo "${ex}" | cut -c3-)" if [ -z "${1}" ]; then + ex="$(basename "${PWD}")" + ex_num="$(printf '%s' "${ex}" | cut -c3-)" next_ex_num="$(( ${ex_num} - 1 ))" else next_ex_num="${1}" @@ -116,14 +183,23 @@ prev () { } next () { - ex="$(basename "${PWD}")" - ex_num="$(echo "${ex}" | cut -c3-)" + # If no arg, calculate next num if [ -z "${1}" ]; then + # Exercise name + ex="$(basename "${PWD}")" + + # Exercise num + ex_num="$(printf '%s' "${ex}" | cut -c3-)" + + # + 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}" + next_dir="ex${padded}" builtin cd "../${next_dir}" }