aboutsummaryrefslogtreecommitdiffstats
path: root/make/scripts/strip-c-comments.awk
diff options
context:
space:
mode:
Diffstat (limited to 'make/scripts/strip-c-comments.awk')
-rwxr-xr-xmake/scripts/strip-c-comments.awk59
1 files changed, 59 insertions, 0 deletions
diff --git a/make/scripts/strip-c-comments.awk b/make/scripts/strip-c-comments.awk
new file mode 100755
index 000000000..4f35452b2
--- /dev/null
+++ b/make/scripts/strip-c-comments.awk
@@ -0,0 +1,59 @@
+#!/usr/bin/awk
+
+BEGIN { ORS = "" ; C99=1 }
+
+{ code = code $0 "\n" }
+
+END {
+ while ( length(code) )
+ code = process( code )
+}
+
+function process( text )
+{
+ if ( C99 ) {
+ if ( match( text, /"|'|\/\*|\/\// ) ) {
+ return span( text )
+ }
+ } else if ( match( text, /"|'|\/\*/ ) ) {
+ return span( text )
+ }
+ print text
+ return ""
+}
+
+function span( text , starter )
+{
+ print substr( text, 1, RSTART - 1 )
+ starter = substr( text, RSTART, RLENGTH )
+ text = substr( text, RSTART + RLENGTH )
+
+ if ( "\"" == starter || "'" == starter ) {
+ return quoted( text, starter )
+ }
+ if ( "//" == starter ) {
+ return remove( text, "\n", "\n" )
+ }
+ ## Allow for
+ ## /* foo *\
+ ## /
+ return remove( text, "\\*(\\\\\n)?/", " " )
+}
+
+function remove( text, ender, replacement )
+{
+ print replacement
+ return substr( text, match(text, ender) + RLENGTH )
+}
+
+function quoted( text, starter )
+{
+ if ( "'" == starter ) {
+ match( text, /^(\\.|[^'])*'/ )
+ } else {
+ match( text, /^(\\.|\?\?\/.|[^"])*"/ )
+ }
+ print starter substr( text, 1, RLENGTH )
+ return substr( text, RSTART + RLENGTH )
+}
+