Skip to content

refactor: use _comp_compgen -C for service name generation#1553

Open
akinomyoga wants to merge 4 commits intoscop:mainfrom
akinomyoga:compgen-C
Open

refactor: use _comp_compgen -C for service name generation#1553
akinomyoga wants to merge 4 commits intoscop:mainfrom
akinomyoga:compgen-C

Conversation

@akinomyoga
Copy link
Collaborator

5e6dc86 and 655371b use _comp_compgen -C "$sysvdir" -- -f instead of first generating filenames with "$svsvdir"/* and stripping the parent directory name by "{tmp[@]#$sysvdir/}".

e21dbb8 and 354f9c5 are additional refactoring.

_comp_compgen -a split -l -- "$(initctl list 2>/dev/null | cut -d' ' -f1)"
fi

((${#services[@]})) || return 1
Copy link
Owner

Choose a reason for hiding this comment

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

On a quick skim it seems removing this changes the behavior of _comp_compgen_services so that it no longer returns 1 when there are no services found. We don't seem to rely on that behavior in any in-tree uses, but should we not preserve it as the function is part of the API?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK, I checked the uses of this function inside bash-completion, but I didn't care about the possibility of external dependencies.

I updated it: c4c79ad..a02d487.

bash_completion Outdated
} 2>/dev/null |
_comp_awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }')
_comp_split -la services "$_generated"
_comp_awk 'sub(/\.service$/, "", $1) { print $1 }')
Copy link
Owner

Choose a reason for hiding this comment

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

I think simple edits like this are usually done using sed instead of awk, maybe do that here?

Copy link
Collaborator Author

@akinomyoga akinomyoga Jan 30, 2026

Choose a reason for hiding this comment

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

The non-trivial part is the extraction of $1. We may still implement it in sed as follows, but I feel it's worse.

Suggested change
_comp_awk 'sub(/\.service$/, "", $1) { print $1 }')
command sed -n 's/^[[:blank:]]*\([^[:blank:]]\{1,\}\)\.service\([[:blank:]].*\)\{0,1\}$/\1/p'

Another option is to use both, but I'd rather do it in a single invocation of awk.

Suggested change
_comp_awk 'sub(/\.service$/, "", $1) { print $1 }')
_comp_awk '{print $1}' | command sed -n 's/\.service$//p'

However, I noticed in the output of systemctl list-units --full --all in my system that the service name isn't necessarily included as $1. Running services are prefixed by . For example,

$ systemctl list-units --full --all | grep -F iptables.service
● iptables.service                                       not-found inactive   dead      iptables.service

The current implementation fails to extract those lines.

Alternative implementations are ...
Suggested change
_comp_awk 'sub(/\.service$/, "", $1) { print $1 }')
_comp_awk '{ for (i = 1; i <= 2; i++) if (sub(/\.service$/, "", $i)) { print $i; next } }')
Suggested change
_comp_awk 'sub(/\.service$/, "", $1) { print $1 }')
_comp_awk '{ if (sub(/\.service$/, "", $1)) print $1; else if (sub(/\.service$/, "", $2)) print $2 }')
Suggested change
_comp_awk 'sub(/\.service$/, "", $1) { print $1 }')
_comp_awk 'sub(/\.service$/, "", $1) { print $1; next } sub(/\.service$/, "", $2) { print $2 }')
Suggested change
_comp_awk 'sub(/\.service$/, "", $1) { print $1 }')
command sed -n 's/^\([^[:blank:]]\{1,\}[[:blank:]]\)\{0,1\}[[:blank:]]*\([^[:blank:]]\{1,\}\)\.service\([[:blank:]].*\)\{0,1\}$/\2/p')
Suggested change
_comp_awk 'sub(/\.service$/, "", $1) { print $1 }')
_comp_awk 'sub(/\.service([[:blank:]].*)?$/, "") && NF <= 2 { print $NF }')

Copy link
Collaborator Author

@akinomyoga akinomyoga Jan 30, 2026

Choose a reason for hiding this comment

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

I decided to use this: a02d487..8082cf8

@akinomyoga akinomyoga force-pushed the compgen-C branch 4 times, most recently from 8082cf8 to 809b7db Compare January 31, 2026 03:36
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.

2 participants