summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2023-05-23 16:29:34 +0200
committerAlejandro Colomar <alx@kernel.org>2023-05-24 01:13:51 +0200
commit8368feab2657bc3fcae899c1b7f4716fb62fad65 (patch)
tree8f7e7bde407bd89285e4ade26ca0411b3afcaf7c
parent10b550a94c0114548ed3aae696b5ede287661d48 (diff)
bash_aliases: Increase robustness of sed_rm_ccomments()
Thanks to perl(1), we can use a non-greedy match to remove several C89-style comments in the same line. And by splitting (and slightly modifying) the other sed(1) call, we can handle multi-line comments that start in the same line that the previous one ends. Now the only remaining issue is nested comments, but that's rare, so let's ignore it for now. $ cat com.c /* let's see */ int foo/*how about here?*/; // meh int bar; /* Let's see how this behaves */ int baz; /* like this? like that! */ int qwe; // asdasd $ sed_rm_ccomments <com.c int foo; int bar; int baz; int qwe; Here's what can and will be problematic: // /* int foo; /* */ Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--scripts/bash_aliases9
1 files changed, 5 insertions, 4 deletions
diff --git a/scripts/bash_aliases b/scripts/bash_aliases
index 5c0458402..e461707c8 100644
--- a/scripts/bash_aliases
+++ b/scripts/bash_aliases
@@ -23,14 +23,15 @@ EX_USAGE=64;
# C
# sed_rm_ccomments() removes C comments.
-# It can't handle multiple comments in a single line correctly,
-# nor mixed or embedded //... and /*...*/ comments.
+# It can't handle mixed //... and /*...*/ comments.
# Use as a filter (see man_lsfunc() in this file).
sed_rm_ccomments()
{
- sed 's%/\*.*\*/%%' \
- |sed -E '\%/\*%,\%\*/%{\%(\*/|/\*)%!d; s%/\*.*%%; s%.*\*/%%;}' \
+ perl -p -e 's%/\*.*?\*/%%g' \
+ |sed -E '\%/\*%, \%\*/% {\%(\*/|/\*)%!d}' \
+ |sed -E '\%/\*% {s%/\*.*%%; n; s%.*\*/%%;}' \
+ |sed -E '\%/\*% {s%/\*.*%%; n; s%.*\*/%%;}' \
|sed 's%//.*%%';
}