ambevar-dotfiles/.scripts/crun

36 lines
764 B
Bash
Executable File

#!/bin/sh
if [ $# -lt 1 ]; then
cat<<EOF
Usage: ${0##*/} FILE [FLAGS]
Simulate a C interpreter by compiling, executing and removing file in one run.
EOF
exit
fi
[ -z "$CC" ] && CC=gcc
FLAGS="-Wall -Wextra -Wshadow -pthread -lm -g3 -O0"
INPUT="$1"
shift
[ $# -ne 0 ] && FLAGS="$@"
FILE="$(mktemp)"
echo "==> $CC \"$INPUT\" -o \"$FILE\" $FLAGS"
## Zsh compatibility. We need it otherwise word splitting of parameter will not
## work.
STATUS="$(set -o | awk '/shwordsplit / {print $2}')"
[ "$STATUS" = "off" ] && set -o shwordsplit
$CC "$INPUT" -o "$FILE" $FLAGS
## Restore Zsh previous options. This will not turn off shwordsplit if it
## was on before calling the function.
[ "$STATUS" = "off" ] && set +o shwordsplit
echo "==> $FILE"
"$FILE"
rm "$FILE"