commit:     91f7b072c851e69abefafed1b43902772fb4058c
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sun Jun  1 15:58:10 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Thu Jun  5 11:22:04 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=91f7b072

isolated-functions.sh: improve the ERE detecting -j in MAKEOPTS

In many ways, to parse -j and/or --jobs from MAKEOPTS is a fool's
errand. There is no way of going about it correctly, short of
implementing an option parser that behaves precisely as does that of
make(1) itself.

Still, the regular expression that is presently employed can be made
less worse, so to speak. This commit revises it to be as follows.

.*[[:space:]]
(
        # short options other than -j, if any, leading up to -j,
        # optionally followed by whitespace
        -[^j]*j[[:space:]]*
        |
        # --jobs followed by either = or whitespace
        --jobs(=|[[:space:]]+)
)
([0-9]+)    # the job count ...
[[:space:]] # which must not have any trailing garbage

The following table shows the increased strictness of this expression.

SUBSTRING  BEFORE   AFTER
--jobs= 4  Valid    Invalid
--jobs 4x  Valid    Invalid
-j4x       Valid    Invalid
-jj4       Valid    Invalid

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 bin/isolated-functions.sh | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh
index aab0f667ba..fefc0d2ab7 100644
--- a/bin/isolated-functions.sh
+++ b/bin/isolated-functions.sh
@@ -496,11 +496,8 @@ fi
 ___makeopts_jobs() {
        local jobs
 
-       # Copied from multiprocessing.eclass:makeopts_jobs
-       # This assumes the first .* will be more greedy than the second .*
-       # since POSIX doesn't specify a non-greedy match (i.e. ".*?").
-       if [[ " ${MAKEOPTS} " =~ 
.*[[:space:]](-[a-z]*j|--jobs[=[:space:]])[[:space:]]*([0-9]+).* ]]; then
-               jobs=${BASH_REMATCH[2]}
+       if [[ " ${MAKEOPTS} " =~ 
.*[[:space:]](-[^j]*j[[:space:]]*|--jobs(=|[[:space:]]+))([0-9]+)[[:space:]] 
]]; then
+               jobs=${BASH_REMATCH[3]}
        elif jobs=$({ getconf _NPROCESSORS_ONLN || sysctl -n hw.ncpu; } 
2>/dev/null); then
                :
        else

Reply via email to