Skip to content

Instantly share code, notes, and snippets.

@Ferdi265
Created December 28, 2018 18:32
Show Gist options
  • Save Ferdi265/9b0ede4c0ad68441cef8542984170056 to your computer and use it in GitHub Desktop.
Save Ferdi265/9b0ede4c0ad68441cef8542984170056 to your computer and use it in GitHub Desktop.
Script to expand macros from C headers
#!/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"
$ cmacro sys/mman.h "PROT_READ RPOT_WRITE PROT_EXEC"
0x1 0x2 0x4
$ cmacro unistd.h STDIN_FILENO
0
$ cmacro stdio.h EOF
(-1)
@Ferdi265
Copy link
Author

Ferdi265 commented Feb 6, 2019

superseded by newer cmacro and cenum gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment