1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-05-18 09:53:22 -07:00
rc/sh/noccom
Connor Olding 44b0272097 switch from ~/opt/local/bin to ~/.local/bin
this is where systemd already expects things to be,
so this complements the OS instead of fighting it.
2024-03-25 15:32:23 -07:00

58 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env sh
# YES_ZSH
# YES_BASH
# YES_DASH
# YES_ASH
# TODO: what's the minimum version of perl required for this?
noccom() { ### @-
### strip C-like comments; both multi-line and single-line.
# 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 ~/.local/bin/noccom ] || cat > ~/.local/bin/noccom <<EOF
#!/usr/bin/env perl
\$/ = undef;
\$_ = <>;
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
perl ~/.local/bin/noccom "$@"
}
[ -n "${preload+-}" ] || noccom "$@"