Skip to content

Commit

Permalink
fix: use XPG4 awk in Solaris
Browse files Browse the repository at this point in the history
Solaris awk lacks many POSIX features as listed below.  When any of
them is used, we use XPG4 awk at /usr/xpg4/bin/awk instead of Solaris
awk at /usr/bin/awk.

- Solaris awk does not support ENVIRON.
- Solaris awk does not support the function `tolower`.
- Solaris awk does not support user-defined functions.
- Solaris awk does not support `-F ERE' but only supports -F<char>.
- Solaris awk does not accept an option argument for `-F` in a
  separate word.  The option argument needs to be specified in the
  same command-line word as `-F<char>`.
- Solaris awk does not support the command-line option `-v var=value`

Co-authored-by: Ville Skyttä <ville.skytta@iki.fi>
  • Loading branch information
akinomyoga and scop committed Nov 27, 2023
1 parent 178f518 commit cdd6da9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 12 deletions.
16 changes: 16 additions & 0 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -3207,6 +3207,22 @@ _comp_xfunc()
"$xfunc_name" "${@:3}"
}
# Call a POSIX-compatible awk. Solaris awk is not POSIX-compliant, but Solaris
# provides a POSIX-compatible version through /usr/xpg4/bin/awk. We switch the
# implementation to /usr/xpg4/bin/awk in Solaris if any.
# @since 2.12
if [[ $OSTYPE == *solaris* && -x /usr/xpg4/bin/awk ]]; then
_comp_awk()
{
/usr/xpg4/bin/awk "$@"
}
else
_comp_awk()
{
command awk "$@"
}
fi
# source compat completion directory definitions
_comp__init_compat_dirs=()
if [[ ${BASH_COMPLETION_COMPAT_DIR-} ]]; then
Expand Down
8 changes: 4 additions & 4 deletions completions/_nmcli
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
_comp_cmd_nmcli__con_id()
{
_comp_compgen_split -l -- "$(nmcli con list 2>/dev/null |
tail -n +2 | awk -F ' {2,}' '{print $1 }')"
tail -n +2 | _comp_awk -F ' {2,}' '{print $1}')"
}

_comp_cmd_nmcli__con_uuid()
{
_comp_compgen_split -- "$(nmcli con list 2>/dev/null |
tail -n +2 | awk -F ' {2,}' '{print $2}')"
tail -n +2 | _comp_awk -F ' {2,}' '{print $2}')"
}

_comp_cmd_nmcli__ap_ssid()
{
_comp_compgen_split -l -- "$(nmcli dev wifi list 2>/dev/null |
tail -n +2 | awk -F ' {2,}' '{print $1}')"
tail -n +2 | _comp_awk -F ' {2,}' '{print $1}')"
}

_comp_cmd_nmcli__ap_bssid()
{
_comp_compgen_split -- "$(nmcli dev wifi list 2>/dev/null |
tail -n +2 | awk -F ' {2,}' '{print $2}')"
tail -n +2 | _comp_awk -F ' {2,}' '{print $2}')"
}

_comp_cmd_nmcli()
Expand Down
2 changes: 1 addition & 1 deletion completions/convert
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ _comp_cmd_convert__common_options()
return
;;
-format)
_comp_compgen_split -- "$(convert -list format | awk \
_comp_compgen_split -- "$(convert -list format | _comp_awk \
'/ [r-][w-][+-] / { sub("[*]$","",$1); print tolower($1) }')"
return
;;
Expand Down
4 changes: 2 additions & 2 deletions completions/dpkg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _comp_xfunc_dpkg_compgen_installed_packages()
grep-status -P -e "^${cur-}" -a \
-FStatus 'ok installed' \
-n -s Package 2>/dev/null ||
command awk -F '\n' -v RS="" "
_comp_awk -F '\n' -v RS="" "
index(\$1, \"Package: ${cur-}\") == 1 &&
\$2 ~ /ok installed|half-installed|unpacked|half-configured|^Essential: yes/ {
print(substr(\$1, 10));
Expand All @@ -22,7 +22,7 @@ _comp_xfunc_dpkg_compgen_purgeable_packages()
grep-status -P -e "^${cur-}" -a \
-FStatus 'ok installed' -o -FStatus 'ok config-files' \
-n -s Package 2>/dev/null ||
command awk -F '\n' -v RS="" "
_comp_awk -F '\n' -v RS="" "
index(\$1, \"Package: ${cur-}\") == 1 &&
\$2 ~ /ok installed|half-installed|unpacked|half-configured|config-files|^Essential: yes/ {
print(substr(\$1, 10));
Expand Down
4 changes: 2 additions & 2 deletions completions/iwconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _comp_cmd_iwconfig()
_comp_compgen -- -W 'on off any'
if [[ ${BASH_COMPLETION_CMD_IWCONFIG_SCAN-${COMP_IWLIST_SCAN-}} ]]; then
_comp_compgen -a split -- "$(iwlist "${words[1]}" scan |
awk -F'\"' '/ESSID/ {print $2}')"
_comp_awk -F '\"' '/ESSID/ {print $2}')"
fi
return
;;
Expand All @@ -38,7 +38,7 @@ _comp_cmd_iwconfig()
_comp_compgen -- -W 'on off any'
if [[ ${BASH_COMPLETION_CMD_IWCONFIG_SCAN-${COMP_IWLIST_SCAN-}} ]]; then
_comp_compgen -a split -- "$(iwlist "${words[1]}" scan |
awk -F ': ' '/Address/ {print $2}')"
_comp_awk -F ': ' '/Address/ {print $2}')"
fi
return
;;
Expand Down
2 changes: 1 addition & 1 deletion completions/make
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _comp_cmd_make__extract_targets()
[[ $mode == -d && $prefix == */* ]] &&
prefix_replace=${prefix##*/}

awk -f "${BASH_SOURCE[0]%/*}/../helpers/make-extract-targets.awk"
_comp_awk -f "${BASH_SOURCE[0]%/*}/../helpers/make-extract-targets.awk"
}

# Truncate the non-unique filepaths in COMPREPLY to only generate unique
Expand Down
2 changes: 1 addition & 1 deletion completions/portinstall
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _comp_cmd_portinstall()
indexfile=$portsdir/INDEX
[[ -f $indexfile && -r $indexfile ]] || return

_comp_compgen_split -l -- "$(awk -F '|' '
_comp_compgen_split -l -- "$(_comp_awk -F '|' '
BEGIN { portsdir = ENVIRON["portsdir"]; len = length(portsdir) }
{ print $1 }
substr($2, 1, len) == portsdir { print substr($2, len + 1) }
Expand Down
2 changes: 1 addition & 1 deletion test/runLint
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ filter_out=
gitgrep $cmdstart"awk\b.*-F([[:space:]]|[[:space:]]*[\"'][^\"']{2,})" \
'awk with -F char or -F ERE, use -Fchar instead (Solaris)'

gitgrep $cmdstart"awk\b.*\[:[a-z]*:\]" \
gitgrep $cmdstart"(_comp_)?awk\b.*\[:[a-z]*:\]" \
'awk with POSIX character class not supported in mawk (Debian/Ubuntu)'

gitgrep $cmdstart'sed\b.*\\[?+]' \
Expand Down

0 comments on commit cdd6da9

Please sign in to comment.