Last active
February 6, 2019 14:26
-
-
Save Ferdi265/864aa8cd5a692355998384d82cef6fab to your computer and use it in GitHub Desktop.
Script to expand macros and enums from C headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CC=${CC:-gcc} | |
# check usage | |
if [[ $# -eq 0 ]]; then | |
echo "usage: cenum [headers..] ENUM" >&2 | |
exit 1 | |
fi | |
# generate temporary file | |
f=$(mktemp --suffix=.c) | |
# add headers | |
while [[ $# -gt 1 ]]; do | |
echo '#include <'"$1"'>' >> "$f" | |
shift | |
done | |
echo '#include <stdio.h>' >> "$f" | |
echo 'int main() { printf("%1$d (0x%1$lx)\n", '"$1"'); }' >> "$f" | |
# compile and execute | |
"$CC" $CFLAGS "$f" -o "$f.elf" && "$f.elf" | |
# remove temporary files | |
rm -f "$f" "$f.elf" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CC=${CC:-gcc} | |
# check usage | |
if [[ $# -eq 0 ]]; then | |
echo "usage: cmacro [headers..] MACRO" >&2 | |
exit 1 | |
fi | |
# generate temporary file | |
f=$(mktemp --suffix=.c) | |
# add headers | |
while [[ $# -gt 1 ]]; do | |
echo '#include <'"$1"'>' >> "$f" | |
shift | |
done | |
# add marker | |
marker="================================================================================" | |
echo "$marker" >> "$f" | |
# add macro calls | |
echo "$1" >> "$f" | |
# expand macros and strip marker | |
"$CC" $CFLAGS -P -E "$f" | sed -n -e '/'"$marker"'/,$p' | tail -n+2 | |
# remove temporary file | |
rm "$f" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cmacro sys/mman.h "PROT_READ RPOT_WRITE PROT_EXEC" | |
0x1 0x2 0x4 | |
$ cmacro unistd.h STDIN_FILENO | |
0 | |
$ cmacro stdio.h EOF | |
(-1) | |
$ cmacro elf.h Val_GNU_MIPS_ABI_FP_ANY | |
Val_GNU_MIPS_ABI_FP_ANY | |
$ cenum elf.h Val_GNU_MIPS_ABI_FP_ANY | |
0 (0x0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment