From e3060ac8ff2412ac75127da3da3c25824826b50d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 7 May 2018 17:14:21 +0200 Subject: [PATCH 01/10] Completion for Mbed TLS developer scripts all.sh, compat.sh, config.pl, ssl-opt.sh, test-ref-configs.pl Signed-off-by: Gilles Peskine --- tools/zsh/_all.sh | 37 +++++++++++++++++++++++++++ tools/zsh/_config.pl | 46 ++++++++++++++++++++++++++++++++++ tools/zsh/_ssl-opt.sh | 46 ++++++++++++++++++++++++++++++++++ tools/zsh/_test-ref-configs.pl | 14 +++++++++++ 4 files changed, 143 insertions(+) create mode 100644 tools/zsh/_all.sh create mode 100644 tools/zsh/_config.pl create mode 100644 tools/zsh/_ssl-opt.sh create mode 100644 tools/zsh/_test-ref-configs.pl diff --git a/tools/zsh/_all.sh b/tools/zsh/_all.sh new file mode 100644 index 0000000..aa54a39 --- /dev/null +++ b/tools/zsh/_all.sh @@ -0,0 +1,37 @@ +#compdef all.sh +## Completion for tests/scripts/all.sh in Mbed TLS. + +_all_sh () { + local line opt spec specs + specs=() + for line in "${(@M)${(@f)$(_call_program help $words[1] --help)}:# #-*}"; do + line=${line## ##} + opt=(${(s:|:)${line%%[= ]*}}) + spec= + line=${line#"${line%%[= ]*}"} + if [[ $line == \=* ]]; then + spec=":${${line#\=}%% *}:" + if [[ $spec == ":<"*">:" ]]; then + spec=":${${spec#??}%??}:" + fi + case $spec in + *_dir_path:) spec="${spec}_dirs";; + *_path:) spec="${spec}_files -g \"*(.*)\"";; + esac + elif [[ $opt[1] == '-s' || $opt[1] == '--seed' ]]; then + spec=":seed:" + fi + line=${line##[^ ]# ##} + spec="[$line]${spec}" + specs+=($^opt$spec) + done + _arguments : $specs +} + +_all_sh "%@" + +# Local Variables: +# mode: sh +# sh-shell: zsh +# End: + diff --git a/tools/zsh/_config.pl b/tools/zsh/_config.pl new file mode 100644 index 0000000..4b15ae3 --- /dev/null +++ b/tools/zsh/_config.pl @@ -0,0 +1,46 @@ +#compdef config.pl +## Completion for scripts/config.pl in Mbed TLS. + +_config_pl_symbols () { + local -a identifiers + #identifiers=("${(@f)$(_call_program get_symbols sed -n 's!^/*\**#define \(MBEDTLS_[0-9A-Z_a-z][0-9A-Z_a-z]*\).*!\1!p' $config_h)}") + identifiers=("${(@f)$(sed -n 's!^/*\**#define \(MBEDTLS_[0-9A-Z_a-z][0-9A-Z_a-z]*\).*!\1!p' $config_h)}") + _describe -t symbols 'config.h symbols' identifiers +} + +_config_pl () { + local context state state_descr line + typeset -A opt_args + local ret=0 config_h=$words[1]:h/../include/mbedtls/config.h + local -a commands_with_descriptions + commands_with_descriptions=( + 'baremetal:All features for bare metal platforms' + 'get:Get symbol value' + 'full:All features' + 'realfull:Uncomment all #define'\''' + 'set:Actvate symbol' + 'unset:Deactivate symbol' + ) + _arguments \ + {'-f','--file'}'[file to edit]:file to edit:_files -g "*.h"' \ + {'-o','--force'}'[define symbol even if not present]' \ + '1:config.pl command:->command' \ + '*::config.pl commands:->param' + case $state in + (command) + _describe -t commands 'config.pl command' commands_with_descriptions;; + (param) + case $words[1]:$CURRENT in + ((get|set|unset):2) _call_function ret _config_pl_symbols;; + esac;; + esac + return $ret +} + +_config_pl "%@" + +# Local Variables: +# mode: sh +# sh-shell: zsh +# End: + diff --git a/tools/zsh/_ssl-opt.sh b/tools/zsh/_ssl-opt.sh new file mode 100644 index 0000000..4b9225b --- /dev/null +++ b/tools/zsh/_ssl-opt.sh @@ -0,0 +1,46 @@ +#compdef ssl-opt.sh compat.sh +## Completion for tests/ssl-opt.sh and tests/compat.sh in Mbed TLS. + +_compat_sh_set_values () { + spec="$spec:$1: _values -s ' ' '$1' ${${line%'*}##*'}" +} + +_ssl_opt_sh () { + local line opt spec specs + specs=() + for line in "${(@M)${(@)${(@f)$(_call_program help $words[1] --help)}//$'\t'##/ }:# #-*}"; do + line=${line## ##} + opt=(${(s:|:)${line%%[= ]*}}) + spec= + line=${line#"${line%%[= ]*}"} + line=${line##[^ ]# ##} + spec="[$line]${spec}" + case $words[1]:t:r in + compat) + case $opt[1] in + -V) _compat_sh_set_values 'Verification modes';; + -e) spec="${spec}:Exclude matching ciphersuites (BRE):";; + -f) spec="${spec}:Restrict to matching ciphersuites (BRE):";; + -m) _compat_sh_set_values 'Protocol versions';; + -p) _compat_sh_set_values 'Peers';; + -t) _compat_sh_set_values 'Key exchange types';; + esac;; + ssl-opt) + case $opt[1] in + -e|-f) spec="${spec}:BRE:";; + --port|--*-port) spec="${spec}:port number:";; + --seed) spec="${spec}:seed:";; + esac;; + esac + specs+=($^opt$spec) + done + _arguments : $specs +} + +_ssl_opt_sh "%@" + +# Local Variables: +# mode: sh +# sh-shell: zsh +# End: + diff --git a/tools/zsh/_test-ref-configs.pl b/tools/zsh/_test-ref-configs.pl new file mode 100644 index 0000000..137a63a --- /dev/null +++ b/tools/zsh/_test-ref-configs.pl @@ -0,0 +1,14 @@ +#compdef test-ref-configs.pl +## Completion for tests/test-ref-configs.pl in Mbed TLS. + +_test_ref_configs_pl () { + compadd $(sed -n '/%configs *=/,/;/ s/^ *'\''\([^'\'']*\)'\'' => {$/\1/p' $words[1]) +} + +_test_ref_configs_pl "%@" + +# Local Variables: +# mode: sh +# sh-shell: zsh +# End: + From ea3e7f67040f2292d57ea2f89e05e97aa275820c Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 21 Dec 2018 14:44:36 +0100 Subject: [PATCH 02/10] Support completion for all.sh components Signed-off-by: Gilles Peskine --- tools/zsh/_all.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tools/zsh/_all.sh b/tools/zsh/_all.sh index aa54a39..7ba2a76 100644 --- a/tools/zsh/_all.sh +++ b/tools/zsh/_all.sh @@ -2,10 +2,11 @@ ## Completion for tests/scripts/all.sh in Mbed TLS. _all_sh () { - local line opt spec specs + local components line opt spec specs + components=() specs=() for line in "${(@M)${(@f)$(_call_program help $words[1] --help)}:# #-*}"; do - line=${line## ##} + line=${line##[ ]##} opt=(${(s:|:)${line%%[= ]*}}) spec= line=${line#"${line%%[= ]*}"} @@ -21,10 +22,16 @@ _all_sh () { elif [[ $opt[1] == '-s' || $opt[1] == '--seed' ]]; then spec=":seed:" fi - line=${line##[^ ]# ##} + line=${line##[^ ]#[ ]##} spec="[$line]${spec}" specs+=($^opt$spec) + if ((${#${(M)opt:#--list-components}})); then + components=("${(@M)${(@f)$(_call_program list-components $words[1] --list-components)}}") + fi done + if (($#components)); then + specs+=("*:component:($components)") + fi _arguments : $specs } From 4592e7414bdcdbcf2618aaed25227fa1d95f9f6d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 18 Jun 2019 18:29:12 +0200 Subject: [PATCH 03/10] Complete tests_all like all.sh Signed-off-by: Gilles Peskine --- tools/zsh/_all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/zsh/_all.sh b/tools/zsh/_all.sh index 7ba2a76..0f5a97d 100644 --- a/tools/zsh/_all.sh +++ b/tools/zsh/_all.sh @@ -1,4 +1,4 @@ -#compdef all.sh +#compdef all.sh tests_all ## Completion for tests/scripts/all.sh in Mbed TLS. _all_sh () { From 5e4207d26b999c0be5580f07933c30ba3b55624d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 18 Jul 2019 22:20:20 +0200 Subject: [PATCH 04/10] Completion for mbedtls-prepare-build Signed-off-by: Gilles Peskine --- tools/zsh/_mbedtls-prepare-build | 76 ++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tools/zsh/_mbedtls-prepare-build diff --git a/tools/zsh/_mbedtls-prepare-build b/tools/zsh/_mbedtls-prepare-build new file mode 100644 index 0000000..2683470 --- /dev/null +++ b/tools/zsh/_mbedtls-prepare-build @@ -0,0 +1,76 @@ +#compdef mbedtls-prepare-build + +_mbedtls_prepare_build_symbols () { + local -a identifiers + identifiers=("${(@f)$(sed -n 's!^/*\**#define \(MBEDTLS_[0-9A-Z_a-z][0-9A-Z_a-z]*\).*!\1!p' $source_dir/include/mbedtls/config.h 2>/dev/null)}") + if ((!$#identifiers)); then return 1; fi + _values -s , 'config.h symbols' $identifiers +} + +_mbedtls_prepare_build () { + local context state state_descr line + typeset -A opt_args + local ret=0 + local prefix spec specs + local source_dir=. args + specs=() + args=($words[2,-1]) + + while (($#args)); do + case $args[1] in + (-s|--source) if (($#args >= 2)); then source_dir=$args[2]; fi;; + (--*=*) :;; + -*) if (($#args >= 2)); then shift args; fi;; + esac + shift args + done + + for line in "${(@f)${$(export COLUMNS=9999; + _call_program help $words[1] --help)//$'\n '/ }}"; do + # 1=opt1 3=?arg1 5=?opt2 7=descr + if [[ $line =~ " (-[^ ]+)( ([^- ][^ ]*))?(, (-[^ ]+)( [^- ][^ ]*)?)* +(.*)" ]]; then + spec="[${match[7]//\]/\\\]}]" + if [[ -n $match[3] ]]; then + spec="=$spec:$match[3]:->${match[1]}" + fi + case $match[1] in + (--config-set|--config-unset|--var) prefix='*';; + (*) prefix=;; + esac + if [[ -n $match[5] ]]; then + specs+=($prefix{$match[1],$match[5]}$spec) + else + specs+=($prefix$match[1]$spec) + fi + fi + done + line= + _arguments $specs + + case $state in + (--ar|--cc|--cp|--perl|--python|--rm|--valgrind) + _call_function ret _command_names -e;; + (--config-file) + _call_function ret _files -g "*.h";; + (--config-name) + local -a config_names + config_names=( + 'baremetal:All features for bare metal platforms' + 'full:All features' + 'realfull:Uncomment all #define'\''' + ) + _call_function ret _describe -t config_name 'Preset configuration name' config_names;; + (--config-set|--config-unset) + _call_function ret _mbedtls_prepare_build_symbols;; + (--dir|--source) + _call_function ret _dirs;; + esac + return $ret +} + +_mbedtls_prepare_build "$@" + +# Local Variables: +# mode: sh +# sh-shell: zsh +# End: From 6c19750da392438a8cdee29763bb3ccaaa3aa22d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 22 Jul 2019 20:54:34 +0200 Subject: [PATCH 05/10] Add support for presets Add support for presets, which set default values for certain options. This allows creating a build directory for a commonly-used target by passing a single command line option. Signed-off-by: Gilles Peskine --- tools/zsh/_mbedtls-prepare-build | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tools/zsh/_mbedtls-prepare-build b/tools/zsh/_mbedtls-prepare-build index 2683470..63c6883 100644 --- a/tools/zsh/_mbedtls-prepare-build +++ b/tools/zsh/_mbedtls-prepare-build @@ -11,10 +11,11 @@ _mbedtls_prepare_build () { local context state state_descr line typeset -A opt_args local ret=0 - local prefix spec specs - local source_dir=. args - specs=() - args=($words[2,-1]) + local prefix spec + local -a specs; specs=() + local source_dir=. + local -a args; args=($words[2,-1]) + local -a presets; presets=() while (($#args)); do case $args[1] in @@ -25,10 +26,12 @@ _mbedtls_prepare_build () { shift args done + state='options' for line in "${(@f)${$(export COLUMNS=9999; _call_program help $words[1] --help)//$'\n '/ }}"; do # 1=opt1 3=?arg1 5=?opt2 7=descr - if [[ $line =~ " (-[^ ]+)( ([^- ][^ ]*))?(, (-[^ ]+)( [^- ][^ ]*)?)* +(.*)" ]]; then + if [[ $state == 'options' && + $line =~ ' (-[^ ]+)( ([^- ][^ ]*))?(, (-[^ ]+)( [^- ][^ ]*)?)* +(.*)' ]]; then spec="[${match[7]//\]/\\\]}]" if [[ -n $match[3] ]]; then spec="=$spec:$match[3]:->${match[1]}" @@ -42,6 +45,11 @@ _mbedtls_prepare_build () { else specs+=($prefix$match[1]$spec) fi + elif [[ $state == 'presets' && + $line =~ '([-0-9A-Z_a-z]+): *(.*)' ]]; then + presets+=($match[1]:$match[2]) + elif [[ $line == 'Presets:' ]]; then + state='presets' fi done line= @@ -57,13 +65,15 @@ _mbedtls_prepare_build () { config_names=( 'baremetal:All features for bare metal platforms' 'full:All features' - 'realfull:Uncomment all #define'\''' + 'realfull:Uncomment all #define'\''s' ) _call_function ret _describe -t config_name 'Preset configuration name' config_names;; (--config-set|--config-unset) _call_function ret _mbedtls_prepare_build_symbols;; (--dir|--source) _call_function ret _dirs;; + (--preset) + _call_function ret _describe -t preset 'Preset build name' presets;; esac return $ret } From 50fa56804e605e9edecf1910098cebcd92f25993 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 13 Sep 2019 18:07:31 +0200 Subject: [PATCH 06/10] Mbed TLS config.pl: also support config.py Signed-off-by: Gilles Peskine --- tools/zsh/_config.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/zsh/_config.pl b/tools/zsh/_config.pl index 4b15ae3..7bab094 100644 --- a/tools/zsh/_config.pl +++ b/tools/zsh/_config.pl @@ -1,5 +1,5 @@ -#compdef config.pl -## Completion for scripts/config.pl in Mbed TLS. +#compdef config.pl config.py +## Completion for scripts/config.pl and scripts/config.pl in Mbed TLS. _config_pl_symbols () { local -a identifiers From cf4641ec3937e518cd922c5a424ed3c4d34d9749 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 13 Sep 2019 18:10:03 +0200 Subject: [PATCH 07/10] Mbed TLS config.pl: fix detection of configurable symbols Signed-off-by: Gilles Peskine --- tools/zsh/_config.pl | 3 +-- tools/zsh/_mbedtls-prepare-build | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/zsh/_config.pl b/tools/zsh/_config.pl index 7bab094..cb48b65 100644 --- a/tools/zsh/_config.pl +++ b/tools/zsh/_config.pl @@ -3,8 +3,7 @@ _config_pl_symbols () { local -a identifiers - #identifiers=("${(@f)$(_call_program get_symbols sed -n 's!^/*\**#define \(MBEDTLS_[0-9A-Z_a-z][0-9A-Z_a-z]*\).*!\1!p' $config_h)}") - identifiers=("${(@f)$(sed -n 's!^/*\**#define \(MBEDTLS_[0-9A-Z_a-z][0-9A-Z_a-z]*\).*!\1!p' $config_h)}") + identifiers=("${(@f)$(_call_program _config_pl_symbols sed -n \''s!^/*\**#define \(MBEDTLS_[0-9A-Z_a-z][0-9A-Z_a-z]*\).*!\1!p'\' \$config_h)}") _describe -t symbols 'config.h symbols' identifiers } diff --git a/tools/zsh/_mbedtls-prepare-build b/tools/zsh/_mbedtls-prepare-build index 63c6883..83786a1 100644 --- a/tools/zsh/_mbedtls-prepare-build +++ b/tools/zsh/_mbedtls-prepare-build @@ -2,7 +2,7 @@ _mbedtls_prepare_build_symbols () { local -a identifiers - identifiers=("${(@f)$(sed -n 's!^/*\**#define \(MBEDTLS_[0-9A-Z_a-z][0-9A-Z_a-z]*\).*!\1!p' $source_dir/include/mbedtls/config.h 2>/dev/null)}") + identifiers=("${(@f)$(_call_program _config_pl_symbols sed -n \''s!^/*\**#define \(MBEDTLS_[0-9A-Z_a-z][0-9A-Z_a-z]*\).*!\1!p'\' \$source_dir/include/mbedtls/config.h)}") if ((!$#identifiers)); then return 1; fi _values -s , 'config.h symbols' $identifiers } From b9fd3dbcfaa4a8e89470ef1c21520b5f2f7c5b1a Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 28 Apr 2020 23:02:00 +0200 Subject: [PATCH 08/10] Fix completion for directories _dirs is completion for the dirs command, not completion of directories. Oops. Signed-off-by: Gilles Peskine --- tools/zsh/_mbedtls-prepare-build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/zsh/_mbedtls-prepare-build b/tools/zsh/_mbedtls-prepare-build index 83786a1..e73d0dd 100644 --- a/tools/zsh/_mbedtls-prepare-build +++ b/tools/zsh/_mbedtls-prepare-build @@ -71,7 +71,7 @@ _mbedtls_prepare_build () { (--config-set|--config-unset) _call_function ret _mbedtls_prepare_build_symbols;; (--dir|--source) - _call_function ret _dirs;; + _call_function ret _files -/;; (--preset) _call_function ret _describe -t preset 'Preset build name' presets;; esac From b2c42d35d6960ccb82bb2eb121cb7bb73c1d6728 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 28 Apr 2020 23:06:03 +0200 Subject: [PATCH 09/10] Support completion of --config-mode Signed-off-by: Gilles Peskine --- tools/zsh/_mbedtls-prepare-build | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/zsh/_mbedtls-prepare-build b/tools/zsh/_mbedtls-prepare-build index e73d0dd..9ca9696 100644 --- a/tools/zsh/_mbedtls-prepare-build +++ b/tools/zsh/_mbedtls-prepare-build @@ -60,6 +60,13 @@ _mbedtls_prepare_build () { _call_function ret _command_names -e;; (--config-file) _call_function ret _files -g "*.h";; + (--config-mode) + local -a config_modes + config_modes=( + 'copy:Copy config.h' + 'include:#include ${SOURCE_DIR}/config.h' + ) + _call_function ret _describe -t config_mode 'Handling of config.h' config_modes;; (--config-name) local -a config_names config_names=( From 934962f360e966aa0a63510c6d1aef935502df42 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 28 Apr 2020 23:06:29 +0200 Subject: [PATCH 10/10] Fix completion for arguments detected from --help output Signed-off-by: Gilles Peskine --- tools/zsh/_mbedtls-prepare-build | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tools/zsh/_mbedtls-prepare-build b/tools/zsh/_mbedtls-prepare-build index 9ca9696..962710e 100644 --- a/tools/zsh/_mbedtls-prepare-build +++ b/tools/zsh/_mbedtls-prepare-build @@ -32,16 +32,28 @@ _mbedtls_prepare_build () { # 1=opt1 3=?arg1 5=?opt2 7=descr if [[ $state == 'options' && $line =~ ' (-[^ ]+)( ([^- ][^ ]*))?(, (-[^ ]+)( [^- ][^ ]*)?)* +(.*)' ]]; then - spec="[${match[7]//\]/\\\]}]" + spec="[${${match[7]//\]/\\\]}//:/\\:}]" if [[ -n $match[3] ]]; then spec="=$spec:$match[3]:->${match[1]}" fi case $match[1] in (--config-set|--config-unset|--var) prefix='*';; - (*) prefix=;; + (*) prefix="($match[1] $match[5])";; esac if [[ -n $match[5] ]]; then - specs+=($prefix{$match[1],$match[5]}$spec) + if [[ -n $spec ]]; then + if [[ $match[1] == --* ]]; then + match[1]="$match[1]=" + else + match[1]="$match[1]+" + fi + if [[ $match[5] == --* ]]; then + match[5]="$match[5]=" + else + match[5]="$match[5]+" + fi + fi + specs+=($prefix{$match[1],$match[5]}${spec#\=}) else specs+=($prefix$match[1]$spec) fi @@ -53,6 +65,7 @@ _mbedtls_prepare_build () { fi done line= + s=($specs) _arguments $specs case $state in