1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-06-26 09:07:12 -07:00
rc/sh/noccom

58 lines
1.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env sh
2021-07-29 00:37:35 -07:00
# YES_ZSH
# YES_BASH
# YES_DASH
2021-09-23 06:48:05 -07:00
# YES_ASH
# TODO: what's the minimum version of perl required for this?
2021-07-29 00:37:35 -07:00
2021-07-30 17:57:08 -07:00
noccom() { ### @-
### strip C-like comments; both multi-line and single-line.
2021-09-23 06:48:05 -07:00
# the first expression is taken from this FAQ:
# https://perldoc.perl.org/perlfaq6.html#How-do-I-use-a-regular-expression-to-strip-C-style-comments-from-a-file%3f
[ -s ~/opt/local/bin/noccom ] || cat > ~/opt/local/bin/noccom <<EOF
#!/usr/bin/env perl
2019-07-27 08:01:48 -07:00
\$/ = undef;
\$_ = <>;
2019-07-27 08:01:48 -07:00
s{
/\\*[^*]*\\*+([^/*][^*]*\\*+)*/
|
//([^\\\\]
|
[^\\n][\\n]?)*?\\n
|
(
"(?:\\\\.|[^"\\\\])*"
|
'(?:\\\\.|[^'\\\\])*'
|
.[^/"'\\\\]*
)
}{defined \$3 ? \$3 : ""}gxse;
s{
\\\\?\\n\\s*([{}])?\\s*(;*)\\s*(?=\\\\?\\n)
|
(
\\#[^\\n]*\\n
|
"(?:\\\\.|[^"\\\\])*"
|
'(?:\\\\.|[^'\\\\])*'
|
.[^#/"'\\\\\\n]*
)
}{defined \$3 ? \$3 : (defined \$1 ? " ".\$1.\$2 : \$2)}gxse;
s#(^|\\n)\\s+#\$1#gs;
print;
EOF
2019-07-27 08:01:48 -07:00
perl ~/opt/local/bin/noccom "$@"
}
2021-07-29 00:37:35 -07:00
[ -n "${preload+-}" ] || noccom "$@"