diff --git a/.bashrc b/.bashrc index 71a47f6..773769c 100644 --- a/.bashrc +++ b/.bashrc @@ -89,6 +89,10 @@ check () { fi } +safe_guard_name () { + printf '%s' "${1}" | tr \\'!@#$%^&*()_+~{}|:">[]?<, ./;=\t\-'\'\` '__________________________________' +} + timo_moulinette () { # Set params subject="${1}" @@ -144,23 +148,56 @@ timo_moulinette () { ${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/$/;/')" + prototypes="$(cat -- "${file_name}.nocomments" | grep -v '#' | grep ')$' | 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 '\\$')" # Remove preprocessed file rm -f -- "${file_name}.nocomments" # Write prototypes to beginning of testfile - IFS="$(printf '\n')" - for proto in "${prototypes}"; do + IFS=" +" + 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=" +" + for macro in ${macros}; do + guard_name="$(safe_guard_name "${macro}")" + + # Endif include guard + printf '%s\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}" + + # 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')" # Compile out_binary="timo_moulinette_${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 + # Delete testfile rm -f -- "${download_file_name}"