Skip to content

fix(_umount,feh,sbopkg): check diretory name for _comp_compgen -C#1297

Merged
akinomyoga merged 2 commits intoscop:mainfrom
akinomyoga:compgen-cwd
Mar 12, 2025
Merged

fix(_umount,feh,sbopkg): check diretory name for _comp_compgen -C#1297
akinomyoga merged 2 commits intoscop:mainfrom
akinomyoga:compgen-cwd

Conversation

@akinomyoga
Copy link
Collaborator

@akinomyoga akinomyoga commented Dec 30, 2024

Fixes #1260

This is an alternative fix to #1290. The fix suggested in #1290 was just to remove the error message, which I believe is not the right fix. This PR fixes it by checking the arguments at the caller side.

This includes an additional fix e9e665e for feh. In the current main branch, an error message is printed on the TAB completion with the following command line:

$ set -u
$ feh --font x[TAB] --fontpath
bash: words[i + 1]: unbound variable

In the current master, an error message is printed on the TAB
completion with the following command line (where the cursor position
is placed before the marker "[TAB]"):

$ set -u
$ feh --font x[TAB] --fontpath
bash: words[i + 1]: unbound variable
* fix(_umount): specify the correct path as a directory
* fix(feh): check the directory name before attempting path completion
* fix(sbopkg): check the QUEUEDIR value before generating *.sqf
-f -X "!*.@([tT][tT][fF])" -S /
if [[ -d $font_path ]]; then
_comp_compgen -aC "$font_path" -- \
-f -X "!*.@([tT][tT][fF])" -S /
Copy link
Collaborator Author

@akinomyoga akinomyoga Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the present topic, but I'm wondering about the reasoning for using -f -X "!globpat" instead of simply -G "globpat". In the codebase, we only have two uses of -G globpat:

./completions/mdtool:35:                _comp_compgen -- -o filenames -G"*.mds"
./completions/rcs:24:    _comp_compgen -aR -- -G "$dir/$file*,v"

while we have many for -f -X "!globpat":

./completions/explodepkg:3:complete -o plusdirs -f -X '!*.t[bglx]z' explodepkg
./completions/feh:32:            # _comp_compgen -C "$font_path" -- -f -X "!*.@([tT][tT][fF])" -S /
./completions/feh:38:                            -f -X "!*.@([tT][tT][fF])" -S /
./completions/gdb:41:        _comp_compgen -a -- -f -X '!?(*/)core?(.?*)' -o plusdirs
./completions/java:87:            _comp_compgen -av tmp -c "$i/$cur" -- -f -X '!*.class'
./completions/lzip:47:    _comp_compgen -- -f -X "*.lz" -o plusdirs
./completions/sbopkg:67:        _comp_compgen -aC "$QUEUEDIR" -- -f -X "!*.sqf"
./completions/_slackpkg:68:                _comp_compgen -C "$confdir/templates" -- -f -X \
                    "!?*.template" && COMPREPLY=("${COMPREPLY[@]%.template}")
./completions/upgradepkg:18:        _comp_compgen -- -P "$prev%" -f -X "!*.@(t[bgxl]z)" || nofiles=set

edit: One possibile explanation is that they derived from the pattern -f -X "$_xspec" used in _comp_compgen_filedir. In that case, we can probably use -G.

edit: I also checked the history of the support for -G by the compgen builtin, but the option -G seems to have existed from the beginning when compgen was introduced in Bash 2.04 (1999).

Copy link
Collaborator Author

@akinomyoga akinomyoga Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were introduced in the following commits, where I don't find any discussions about using -f -X over -G.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think the following @(...) are not needed (where ... does not contain |):

./completions/feh:32:            # _comp_compgen -C "$font_path" -- -f -X "!*.@([tT][tT][fF])" -S /
./completions/feh:38:                            -f -X "!*.@([tT][tT][fF])" -S /
./completions/upgradepkg:18:        _comp_compgen -- -P "$prev%" -f -X "!*.@(t[bgxl]z)" || nofiles=set
./completions/valgrind:10:        if [[ ${words[i]} != @([-=])* ]]; then

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think the following @(...) are not needed (where ... does not contain |):

./completions/feh:32:            # _comp_compgen -C "$font_path" -- -f -X "!*.@([tT][tT][fF])" -S /
./completions/feh:38:                            -f -X "!*.@([tT][tT][fF])" -S /
./completions/upgradepkg:18:        _comp_compgen -- -P "$prev%" -f -X "!*.@(t[bgxl]z)" || nofiles=set
./completions/valgrind:10:        if [[ ${words[i]} != @([-=])* ]]; then

Yeah, it would be nicer like that

About -f -X vs -G, I don't really have a strong opinion, but seems fine to change where possible to -G (note the lzip example you showed doesn't have a !)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I'll submit the fixes later.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re the redundant @(...), filed mvdan/sh#1137

Copy link
Collaborator Author

@akinomyoga akinomyoga Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for -f -X '!pat' vs -G 'pat', there does seem to be a difference. Although they are consistent when it generates the filename in the current directory:

$ compgen -G '*.md'
CHANGELOG.md
CONTRIBUTING.md
README.md
style.md
$ compgen -f -X '!*.md'
README.md
CONTRIBUTING.md
CHANGELOG.md
style.md

they produce different results when they are used to generate completions in a subdirectory:

$ compgen -G '*.md' -- doc/
CHANGELOG.md
CONTRIBUTING.md
README.md
style.md
$ compgen -f -X '!*.md' -- doc/
doc/api-and-naming.md
doc/styleguide.md
doc/testing.md
doc/configuration.md
(This hidden part was my misunderstanding)

We may still write it as

$ cur=doc/
$ compgen -G '*.md' -P "${cur%%+([^/])}" -- "$cur"
doc/CHANGELOG.md
doc/CONTRIBUTING.md
doc/README.md
doc/style.md

but it is even more non-trivial than -f -X '!pat'.

edit: The -G option only generates the filenames in the current directory even when we try to generate the filenames in a subdirectory. In addition, the filtering by cur does not seem to be applied to the completions generated by -X.

@akinomyoga akinomyoga changed the title Compgen cwd fix(_umount,feh,sbopkg): check diretory name for _comp_compgen -C Dec 30, 2024
@akinomyoga akinomyoga requested a review from yedayak March 3, 2025 13:23
@akinomyoga akinomyoga merged commit ff9e1d3 into scop:main Mar 12, 2025
7 checks passed
@akinomyoga akinomyoga deleted the compgen-cwd branch March 12, 2025 21:01
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 6, 2025
scop#1297 (comment)

The compgen option `-X '!pat'` has a problem that it only generates
the filenames in the current directory and that it does not perform
the filtering by the "cur" specified to the argument of `compgen`:

    $ compgen -G '*.md' -- doc/
    CHANGELOG.md
    CONTRIBUTING.md
    README.md
    style.md

In this patch, we use `-f -X '!pat'`, which does not have those
limitations.
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 6, 2025
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 6, 2025
scop#1297 (comment)

The compgen option `-X '!pat'` has a problem that it only generates
the filenames in the current directory and that it does not perform
the filtering by the "cur" specified to the argument of `compgen`:

    $ compgen -G '*.md' -- doc/
    CHANGELOG.md
    CONTRIBUTING.md
    README.md
    style.md

In this patch, we use `-f -X '!pat'`, which does not have those
limitations.
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 6, 2025
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
scop#1297 (comment)

The compgen option `-X '!pat'` has a problem that it only generates
the filenames in the current directory and that it does not perform
the filtering by the "cur" specified to the argument of `compgen`:

    $ compgen -G '*.md' -- doc/
    CHANGELOG.md
    CONTRIBUTING.md
    README.md
    style.md

In this patch, we use `-f -X '!pat'`, which does not have those
limitations.
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
scop#1297 (comment)

The compgen option `-X '!pat'` has a problem that it only generates
the filenames in the current directory and that it does not perform
the filtering by the "cur" specified to the argument of `compgen`:

    $ compgen -G '*.md' -- doc/
    CHANGELOG.md
    CONTRIBUTING.md
    README.md
    style.md

In this patch, we use `-f -X '!pat'`, which does not have those
limitations.
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error occurred when completing the "umount" command

3 participants