Created
December 28, 2018 18:32
-
-
Save Ferdi265/9b0ede4c0ad68441cef8542984170056 to your computer and use it in GitHub Desktop.
Script to expand macros 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 | |
# 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 | |
gcc -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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
superseded by newer cmacro and cenum gist