-
-
Save ajgappmark/5e4f47162cd671d67ccc22b96a6e0737 to your computer and use it in GitHub Desktop.
Script to strip all C comments
This file contains hidden or 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
# Strip C comments | |
# by Stewart Ravenhall <[email protected]> -- 4 October 2000 | |
# Un-Korn-ized by Paolo Bonzini <[email protected]> -- 24 November 2000 | |
# Strip everything between /* and */ inclusive | |
# Copes with multi-line comments, | |
# disassociated end comment symbols, | |
# disassociated start comment symbols, | |
# multiple comments per line | |
# Check given file exists | |
program=`echo $0|sed -e 's:.*/::'` | |
if [ "$#" = 1 ] && [ "$1" != "-" ] && [ ! -f "$1" ]; then | |
print "$program: $1 does not exist" | |
exit 2 | |
fi | |
# Create shell variables for ASCII 1 (control-a) | |
# and ASCII 2 (control-b) | |
a="`echo | tr '\012' '\001'`" | |
b="`echo | tr '\012' '\002'`" | |
sed ' | |
# If no start comment then go to end of script | |
/\/\*/!b | |
:a | |
s:/\*:'"$a"':g | |
s:\*/:'"$b"':g | |
# If no end comment | |
/'"$b"'/!{ | |
:b | |
# If not last line then read in next one | |
$!{ | |
N | |
ba | |
} | |
# If last line then remove from start | |
# comment to end of line | |
# then go to end of script | |
s:'"$a[^$b]"'*$:: | |
bc | |
} | |
# Remove comments | |
'"s:$a[^$b]*$b"'::g | |
/'"$a"'/ bb | |
:c | |
s:'"$a"':/*:g | |
s:'"$b"':*/:g | |
' $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment