|
| 1 | +#!/usr/bin/env bash |
| 2 | +_pinctrl () |
| 3 | +{ |
| 4 | + local cur prev words cword split cmd pins pincomp ALLPINS CHIPS chip pinmode=false prefix arg i func pull; |
| 5 | + local opts="no op ip a0 a1 a2 a3 a4 a5 a6 a7 a8 dh dl pu pd pn "; |
| 6 | + _init_completion -s || return; |
| 7 | + |
| 8 | + i=1 |
| 9 | + while [[ ${COMP_WORDS[$i]} =~ ^- ]]; do |
| 10 | + arg=${COMP_WORDS[$i]} |
| 11 | + if [[ "$arg" == "-c" ]]; then |
| 12 | + chip=${COMP_WORDS[$((i + 1))]} |
| 13 | + i=$((i + 2)) |
| 14 | + else |
| 15 | + if [[ "$arg" == "-p" ]]; then |
| 16 | + pinmode=true |
| 17 | + fi |
| 18 | + i=$((i + 1)) |
| 19 | + fi |
| 20 | + done |
| 21 | + |
| 22 | + if [[ ${COMP_WORDS[$i]} =~ ^(get|set|funcs|poll|help) ]]; then |
| 23 | + cmd=${COMP_WORDS[$i]} |
| 24 | + i=$((i + 1)) |
| 25 | + elif [[ ${COMP_WORDS[$i]} =~ ^[A-Z0-9] ]]; then |
| 26 | + cmd="set" |
| 27 | + fi |
| 28 | + |
| 29 | + if [[ "$cmd" != "" && $i -lt $cword ]]; then |
| 30 | + pins=${COMP_WORDS[$i]}; |
| 31 | + i=$((i + 1)) |
| 32 | + while [[ $i -lt $cword && ${COMP_WORDS[$i]} =~ ^[-,] ]]; do |
| 33 | + pins="$pins${COMP_WORDS[$i]}" |
| 34 | + i=$((i + 1)) |
| 35 | + done |
| 36 | + fi |
| 37 | + |
| 38 | + if [[ "$pins" != "" ]]; then |
| 39 | + while [[ $i -lt $cword ]]; do |
| 40 | + arg=${COMP_WORDS[$i]} |
| 41 | + if [[ $arg =~ ^(no|ip|op|a[0-8])$ ]]; then |
| 42 | + func=$arg |
| 43 | + opts=$(echo "$opts" | sed -E "s/(no|ip|op|a[0-8]) //g") |
| 44 | + if [[ $func != "op" ]]; then |
| 45 | + opts=$(echo "$opts" | sed -E "s/(dh|dl) //g") |
| 46 | + fi |
| 47 | + elif [[ $arg =~ ^(pu|pd|pn)$ ]]; then |
| 48 | + pull=$arg |
| 49 | + opts=$(echo "$opts" | sed -E "s/(pu|pd|pn) //g") |
| 50 | + fi |
| 51 | + i=$((i + 1)) |
| 52 | + done |
| 53 | + fi |
| 54 | + |
| 55 | + if [[ "$cmd" != "" && ( "$pins" == "" || "${cur}" =~ [-,] ) ]]; then |
| 56 | + prefix=$(echo $cur | sed 's/[^-,]*$//') |
| 57 | + if $pinmode; then |
| 58 | + ALLPINS={1..40} |
| 59 | + else |
| 60 | + ALLPINS=($(pinctrl get | cut -d'/' -f4 | cut -d' ' -f1) $(pinctrl get | cut -d'/' -f3 | cut -d' ' -f2)) |
| 61 | + fi |
| 62 | + pincomp="${ALLPINS[@]/#/$prefix}" |
| 63 | + COMPREPLY+=($(compgen -W "$pincomp" -- $cur)) |
| 64 | + elif [[ "$cmd" != "" ]]; then |
| 65 | + if [[ "$cmd" == "set" ]]; then |
| 66 | + COMPREPLY+=($(compgen -W "$opts" -- $cur)) |
| 67 | + fi |
| 68 | + else |
| 69 | + if [[ "$prev" == "-c" ]]; then |
| 70 | + CHIPS=($(pinctrl -v -p 0 | grep 'gpios)' | cut -d' ' -f4 | sort | uniq)) |
| 71 | + chips="${CHIPS[@]}" |
| 72 | + COMPREPLY+=($(compgen -W "$chips" -- $cur)) |
| 73 | + elif [[ "$cur" == "-" ]]; then |
| 74 | + COMPREPLY+=($(compgen -W "-p -h -v -c" -- $cur)) |
| 75 | + elif [[ "$chip" == "" ]]; then |
| 76 | + COMPREPLY+=($(compgen -W "get set poll funcs help" -- $cur)) |
| 77 | + else |
| 78 | + COMPREPLY+=($(compgen -W "funcs help" -- $cur)) |
| 79 | + fi |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +complete -F _pinctrl pinctrl |
0 commit comments