fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
6aee7bf37
master
1# ip(8) completion for fish 2 3# The difficulty here is that ip allows abbreviating options, so we need to complete "ip a" like "ip address", but not "ip m" like "ip mroute" 4# Also the manpage and even the grammar it accepts is utter shite (options can only be before commands, some things are only in the BNF, others only in the text) 5# It also quite likes the word "dev", even though it needs it less than the BNF specifies 6 7set -l ip_commands link address addrlabel route rule neighbour ntable tunnel tuntap maddr mroute mrule monitor xfrm netns l2tp tcp_metrics 8set -l ip_addr a ad add addr addre addres address 9set -l ip_link l li lin link 10set -l ip_neigh n ne nei neig neigh neighb neighbo neighbor neighbour 11set -l ip_route r ro rou rout route 12set -l ip_rule ru rul rule 13set -l ip_all_commands$ip_commands $ip_addr $ip_link $ip_neigh $ip_route $ip_rule 14 15function __fish_ip_commandwords 16set -l skip0 17set -l cmd( commandline -xpc| string escape) 18# Remove the first word because it's "ip" or an alias for it 19set -e cmd[ 1 ] 20set -l have_command0 21for wordin $cmd 22switch $word 23# Normalize the commands to their full form - `ip a` is `ip address` 24# This can't be just an unambiguity check because there's also `ip addrlabel` 25case a ad add addr addre addres address 26# "addr add" is a thing, so we can only echo "address" if it's actually the command 27if test $have_command = 0 28set have_command1 29echo address 30else 31echo $word 32end 33case l li lin link 34if test $have_command = 0 35set have_command1 36echo link 37else 38echo $word 39end 40case "addrl*" 41if test $have_command = 0 42set have_command1 43echo addrlabel 44else 45echo $word 46end 47case r ro rou rout route 48if test $have_command = 0 49set have_command1 50echo route 51else 52echo $word 53end 54case "ru*" 55if test $have_command = 0 56set have_command1 57echo rule 58else 59echo $word 60end 61case n ne nei neig neigh neighb neighbo neighbor neighbour 62if test $have_command = 0 63set have_command1 64echo neighbour 65else 66echo $word 67end 68case nt 69if test $have_command = 0 70set have_command1 71echo ntable 72else 73echo $word 74end 75case t tu tun tunn tunne tunnel 76if test $have_command = 0 77set have_command1 78echo tunnel 79else 80echo $word 81end 82case "tunt*" 83if test $have_command = 0 84set have_command1 85echo tuntap 86else 87echo $word 88end 89case m ma mad madd maddr maddre maddres maddress 90if test $have_command = 0 91set have_command1 92echo maddress 93else 94echo $word 95end 96case mr"mro*" 97if test $have_command = 0 98set have_command1 99echo mroute 100else 101echo $word 102end 103case "mru*" 104if test $have_command = 0 105set have_command1 106echo mrule 107else 108echo $word 109end 110case "mo*" 111if test $have_command = 0 112set have_command1 113echo monitor 114else 115echo $word 116end 117case "x*" 118if test $have_command = 0 119set have_command1 120echo xfrm 121else 122echo $word 123end 124case "net*" 125if test $have_command = 0 126set have_command1 127echo netns 128else 129echo $word 130end 131case "l*" 132if test $have_command = 0 133set have_command1 134echo l2tp 135else 136echo $word 137end 138case "tc*" 139if test $have_command = 0 140set have_command1 141echo tcp_metrics 142else 143echo $word 144end 145case "to*" 146if test $have_command = 0 147set have_command1 148echo token 149else 150echo $word 151end 152case -n -netns --netns 153if test $have_command = 0 154set skip1 155else 156echo $word 157end 158case '-*' 159test $have_command = 0 160and continue 161echo $word 162case '*' 163if test $skip = 1 164set skip0 165continue 166end 167echo $word 168end 169end 170# Print an empty line if the current token is empty, so we know that the one before it is finished 171# TODO: For some reason it is necessary to always print the current token - why doesn't the above loop catch it? 172set -l token( commandline -ct) 173echo $token 174end 175 176function __fish_ip_device 177command ip -o link show| while read -l a b c 178printf '%s\t%s\n' ( string replace -r'(@.*)?:' '' --$b ) Device 179end 180end 181 182function __fish_ip_scope 183if test -r /etc/iproute2/rt_scopes 184string replace -r'#.*' '' < /etc/iproute2/rt_scopes\ 185| string match -v'^\s*$' \ 186| string replace -r'(\S+)\s*(\S+)' '$1\t$2\n$2\t$1' \ 187| string match -rv'^(global|link|host).*' # Ignore scopes with better descriptions 188end 189# Predefined scopes 190printf '%s\t%s\n' global"Address is globally valid" \ 191 link"Address is link-local, only valid on this device" \ 192 host"Address is only valid on this host" 193end 194 195function __fish_ip_netns_list 196command ip netns list| while read -l a b c 197echo --$a 198end 199end 200 201function __fish_ip_routing_tables 202command ip -d route show table all2> /dev/null| string replace -fr'.*\stable\s(\S+).*' '$1' 203end 204 205function __fish_ip_types 206printf '%s\t%s\n' \ 207 bridge"Ethernet Bridge device" \ 208 bond"Bonding device" \ 209 dummy"Dummy network interface" \ 210 hsr"High-availability Seamless Redundancy device" \ 211 ifb"Intermediate Functional Block device" \ 212 ipoib"IP over Infiniband device" \ 213 macvlan"Virtual interface base on link layer address (MAC)" \ 214 macvtap"Virtual interface based on link layer address (MAC) and TAP." \ 215 vcan"Virtual Controller Area Network interface" \ 216 vxcan"Virtual Controller Area Network tunnel interface" \ 217 veth"Virtual ethernet interface" \ 218 vlan"802.1q tagged virtual LAN interface" \ 219 vxlan"Virtual eXtended LAN" \ 220 ip6tnl"Virtual tunnel interface IPv4|IPv6 over IPv6" \ 221 ipip"Virtual tunnel interface IPv4 over IPv4" \ 222 sit"Virtual tunnel interface IPv6 over IPv4" \ 223 gre"Virtual tunnel interface GRE over IPv4" \ 224 gretap"Virtual L2 tunnel interface GRE over IPv4" \ 225 erspan"Encapsulated Remote SPAN over GRE and IPv4" \ 226 ip6gre"Virtual tunnel interface GRE over IPv6" \ 227 ip6gretap"Virtual L2 tunnel interface GRE over IPv6" \ 228 ip6erspan"Encapsulated Remote SPAN over GRE and IPv6" \ 229 vti"Virtual tunnel interface" \ 230 nlmon"Netlink monitoring device" \ 231 ipvlan"Interface for L3 (IPv6/IPv4) based VLANs" \ 232 ipvtap"Interface for L3 (IPv6/IPv4) based VLANs and TAP" \ 233 lowpan"Interface for 6LoWPAN (IPv6) over IEEE 802.15.4 / Bluetooth" \ 234 geneve"GEneric NEtwork Virtualization Encapsulation" \ 235 bareudp"Bare UDP L3 encapsulation support" \ 236 macsec"Interface for IEEE 802.1AE MAC Security (MACsec)" \ 237 vrf"Interface for L3 VRF domains" \ 238 netdevsim"Interface for netdev API tests" \ 239 rmnet"Qualcomm rmnet device" \ 240 xfrm"Virtual xfrm interface" 241end 242 243function __fish_ip_neigh_states 244printf '%s\t%s\n' \ 245 permanent"entry is valid forever" \ 246 noarp"entry is valid without validation" \ 247 reachable"entry is valid until timeout" \ 248 stale"entry is valid but suspicious" \ 249 none"pseudo state" \ 250 incomplete"entry has not yet been validated" \ 251 delay"entry validation is currently delayed" \ 252 probe"neighbor is being probed" \ 253 failed"neighbor validation has ultimately failed" 254end 255 256function __fish_complete_ip 257set -l cmd( __fish_ip_commandwords ) 258set -l count( count $cmd ) 259switch " $cmd [ 1 ] " 260case address 261# We're still _on_ the second word, which is the subcommand 262if not set -q cmd[ 3 ] 263printf '%s\t%s\n' \ 264 add"Add new protocol address" \ 265 delete"Delete protocol address" \ 266 show"Look at protocol addresses" \ 267 flush"Flush protocol addresses" \ 268 help"Display help" 269else 270switch $cmd [ 2 ] 271# Change and replace are undocumented (apart from mentions in the BNF) 272case add change replace 273switch $count 274case 3 275# __fish_ip_complete_ip 276case '*' 277switch $cmd [ -2 ] 278case dev 279__fish_ip_device 280case scope 281__fish_ip_scope 282# TODO: Figure out how to complete these 283case label 284# Prefix 285case local peer broadcast 286# Address 287case valid_lft preferred_lft 288# Lifetime 289case '*' 290printf '%s\t%s\n' forever"Keep address valid forever" \ 291 home"(Ipv6 only) Designate address as home address" \ 292 nodad"(Ipv6 only) Don't perform duplicate address detection" \ 293 dev"Add address to specified device" \ 294 scope"Set scope of address" \ 295 label"Tag address with label" 296end 297end 298case delete 299switch $count 300case 3 301command ip -o addr show| while read -l a b c d e 302echo $d 303end 304case 4 305# A dev argument is mandatory, but contrary to the BNF, other things (like "scope") are also valid here 306# And yes, unlike e.g. show, this _needs_ the "dev" before the device 307# Otherwise it barfs and says "??? prefix is expected" 308# Anyway, try to steer the user towards supplying a device 309echo dev 310case 5 311switch $cmd [ -2 ] 312case dev 313command ip -o addr show| string match"* $cmd [ 3 ] *" | while read -l a b c 314echo $b 315end 316# TODO: Moar 317end 318end 319case show save flush# These take the same args 320switch $cmd [ -2 ] 321case dev 322__fish_ip_device 323case scope 324__fish_ip_scope 325case to 326# Prefix 327case label 328# Label-pattern 329case '*' 330printf '%s\t%s\n' up"Only active devices" \ 331 dev"Limit to a certain device" \ 332 scope"Limit scope" \ 333 to"Limit prefix" \ 334 label"Limit by label" \ 335 dynamic"(Ipv6 only) Limit to dynamic addresses" \ 336 permanent"(Ipv6 only) Limit to permanent addresses" 337__fish_ip_device 338# TODO: Moar 339end 340end 341end 342case link 343if not set -q cmd[ 3 ] 344printf '%s\t%s\n' \ 345 add"Add virtual link" \ 346 delete"Delete virtual link" \ 347 set"Change device attributes" \ 348 show"Display device attributes" \ 349 help"Display help" 350else 351# TODO: Add moar 352switch $cmd [ 2 ] 353case add 354switch $cmd [ -2 ] 355case link 356__fish_ip_device 357case name# freeform, uncompleteable. 358case type 359__fish_ip_types 360case txqueuelen 361case address 362case broadcast 363case mtu index numtxqueues numrxqueues gso_max_size gso_max_segs 364# These all take some kind of number? 365seq 0 50 366case add'*' 367# We assume if we haven't checked it above, we're back to `ip link add`, with any optionals completed. 368# So we now suggest optionals. 369printf '%s\t%s\n' \ 370 link"" \ 371 type"" \ 372 txqueuelen PACKETS\ 373 address LLADDR\ 374 broadcast LLADDR\ 375 mtu MTU\ 376 index IDX\ 377 numtxqueues QUEUE_COUNT\ 378 numrxqueues QUEUE_COUNT\ 379 gso_max_size BYTES\ 380 gso_max_segs 381end 382case delete 383switch $cmd [ -2 ] 384case delete 385__fish_ip_device 386echo dev 387echo group 388case dev 389__fish_ip_device 390case group 391case type 392__fish_ip_types 393case '*' 394if not set -q cmd[ 6 ] 395echo type 396end 397end 398case set 399switch $cmd [ -2 ] 400case type 401__fish_ip_types 402echo bridge_slave 403echo bond_slave 404case arp dynamic{ all,} multicast pro{ misc, todown} trailers spoofchk query_rss trust 405echo on 406echo off 407case name alias 408case address broadcast 409case mtu txqueuelen 410case netns 411__fish_ip_netns_list 412case link-netnsid 413case vf mac 414case {{ max, min} _tx_,} rate 415case node_guid port_guid 416case state 417echo auto 418echo enable 419echo disable 420case master dev# Yes, the word "dev" is allowed here! 421__fish_ip_device 422case nomaster 423case vrf 424case xdp'*' 425case object section pinned 426case addrgenmode 427case macaddr 428case group 429case set'*' 430__fish_ip_device 431echo group 432echo up 433echo down 434end 435case show 436case help 437__fish_ip_types 438end 439end 440case neighbour 441if not set -q cmd[ 3 ] 442printf '%s\t%s\n' \ 443 add"Add new neighbour entry" \ 444 delete"Delete neighbour entry" \ 445 change"Change neighbour entry" \ 446 replace"Add or change neighbour entry" \ 447 show"List neighbour entries" \ 448 flush"Flush neighbour entries" \ 449 get"Lookup neighbour entry" \ 450 help"Display help" 451else 452switch $cmd [ 2 ] 453case add del delete change replace 454switch $cmd [ -2 ] 455case lladdr 456case nud 457__fish_ip_neigh_states 458case proxy 459case dev 460__fish_ip_device 461case '*' 462echo lladdr 463echo nud 464echo proxy 465echo dev 466echo router 467echo use 468echo managed 469echo extern_learn 470end 471case show flush 472switch $cmd [ -2 ] 473case to 474case dev 475__fish_ip_device 476case vrf 477case nud 478__fish_ip_neigh_states 479echo all 480case '*' 481echo to 482echo dev 483echo vrf 484echo nomaster 485echo proxy 486echo unused 487echo nud 488end 489case get 490switch $cmd [ -2 ] 491case to 492case dev 493__fish_ip_device 494case '*' 495echo proxy 496echo to 497echo dev 498end 499end 500end 501case route 502if not set -q cmd[ 3 ] 503printf '%s\t%s\n' \ 504 add"Add new route" \ 505 change"Change route" \ 506 append"Append route" \ 507 replace"Change or add new route" \ 508 delete"Delete route" \ 509 show"List routes" \ 510 flush"Flush routing tables" \ 511 get"Get a single route" \ 512 save"Save routing table to stdout" \ 513 showdump"Show saved routing table from stdin" \ 514 restore"Restore routing table from stdin" \ 515 help"Display help" 516else 517# TODO: switch on $cmd[2] and complete subcommand specific arguments 518# for now just complete most useful arguments for the last token 519switch $cmd [ -2 ] 520case dev iif oif 521__fish_ip_device 522case table 523__fish_ip_routing_tables 524end 525end 526case rule 527if not set -q cmd[ 3 ] 528printf '%s\t%s\n' \ 529 add"Add new rule" \ 530 delete"Delete rule" \ 531 flush"Flush rules" \ 532 show"List rules" \ 533 save"Save rules to stdout" \ 534 restore"Restore rules from stdin" \ 535 help"Display help" 536else 537# TODO: switch on $cmd[2] and complete subcommand specific arguments 538# for now just complete most useful arguments for the last token 539switch $cmd [ -2 ] 540case iif oif 541__fish_ip_device 542case table 543__fish_ip_routing_tables 544end 545end 546case netns 547if not set -q cmd[ 3 ] 548printf '%s\t%s\n' add"Add network namespace" \ 549 attach"Attach process to network namespace" \ 550 delete"Delete network namespace" \ 551 set"Change network namespace attributes" \ 552 identify"Display network namespace for a process ID" \ 553 pids"Display process IDs of processes running in network namespace" \ 554 monitor"Report as network namespace names are added and deleted" \ 555 exec"Execute command in network namespace" \ 556 help"Display help" \ 557 list"List network namespaces" 558else 559switch $cmd [ 2 ] 560case delete 561if not set -q cmd[ 4 ] 562__fish_ip_netns_list 563end 564case exec 565if not set -q cmd[ 4 ] 566__fish_ip_netns_list 567else 568__fish_complete_subcommand --commandline$cmd [ 4 .. -1 ] 569end 570case pids 571if not set -q cmd[ 4 ] 572__fish_ip_netns_list 573end 574case set 575if not set -q cmd[ 4 ] 576__fish_ip_netns_list 577end 578case attach 579if not set -q cmd[ 4 ] 580__fish_ip_netns_list 581else 582__fish_complete_pids 583end 584case identify 585if not set -q cmd[ 4 ] 586__fish_complete_pids 587end 588end 589end 590case tuntap 591if not set -q cmd[ 3 ] 592printf '%s\t%s\n' \ 593 add"Add a new tun or tap device" \ 594 del"Delete a tun or tap device" \ 595 show"Show tun and tap devices" \ 596 help"Display help" 597else 598switch $cmd [ -2 ] 599case dev 600__fish_ip_device 601case mode 602printf '%s\n' tun tap 603case user 604__fish_complete_users 605case group 606__fish_complete_groups 607case '*' 608printf '%s\n' dev mode user group one_queue pi vnet_hdr multi_queue name 609end 610end 611end 612end 613 614complete -f -c ip 615complete -f -c ip -a'(__fish_complete_ip)' 616complete -f -c ip -n"not __fish_seen_subcommand_from $ip_all_commands " -a" $ip_commands " 617# Yes, ip only takes options before "objects" 618complete -c ip -s h -l human -d"Output statistics with human readable values" -n"not __fish_seen_subcommand_from $ip_commands " 619complete -c ip -s b -l batch -d"Read commands from file or stdin" -rF -n"not __fish_seen_subcommand_from $ip_commands " 620complete -c ip -l force -d"Don't terminate on errors in batch mode" -n"not __fish_seen_subcommand_from $ip_commands " 621complete -c ip -s V -l Version -d"Print the version" -n"not __fish_seen_subcommand_from $ip_commands " 622complete -c ip -f -s s -l stats -d"Output more information" -n"not __fish_seen_subcommand_from $ip_commands " 623complete -c ip -f -s d -l details -d"Output more detailed information" -n"not __fish_seen_subcommand_from $ip_commands " 624complete -c ip -f -s l -l loops -d"Specify maximum number of loops for 'ip addr flush'" -x -n"not __fish_seen_subcommand_from $ip_commands " 625complete -c ip -f -s f -l family -d"The protocol family to use" -xa"inet inet6 bridge ipx dnet mpls link any" -n"not __fish_seen_subcommand_from $ip_commands " 626complete -c ip -f -s4 -d"Short for --family inet" -n"not __fish_seen_subcommand_from $ip_commands " 627complete -c ip -f -s6 -d"Short for --family inet6" -n"not __fish_seen_subcommand_from $ip_commands " 628complete -c ip -f -s B -d"Short for --family bridge" -n"not __fish_seen_subcommand_from $ip_commands " 629complete -c ip -f -s M -d"Short for --family mpls" -n"not __fish_seen_subcommand_from $ip_commands " 630complete -c ip -f -s0 -d"Short for --family link" -n"not __fish_seen_subcommand_from $ip_commands " 631complete -c ip -f -s o -l oneline -d"Output on one line" -n"not __fish_seen_subcommand_from $ip_commands " 632complete -c ip -f -s r -l resolve -d"Resolve names and print them instead of addresses" -n"not __fish_seen_subcommand_from $ip_commands " 633complete -c ip -f -s n -l netns -d"Use specified network namespace" -xa"(__fish_ip_netns_list)" -n"not __fish_seen_subcommand_from $ip_commands " 634complete -c ip -f -s a -l all -d"Execute command for all objects" -n"not __fish_seen_subcommand_from $ip_commands " 635complete -c ip -f -s c -l color -d"Configure color output" -n"not __fish_seen_subcommand_from $ip_commands " 636complete -c ip -f -s t -l timestamp -d"Display current time when using monitor" -n"not __fish_seen_subcommand_from $ip_commands " 637complete -c ip -f -o ts -l tshort -d"Like -timestamp, but shorter format" -n"not __fish_seen_subcommand_from $ip_commands " 638complete -c ip -f -o rc -l rcvbuf -d"Set the netlink socket receive buffer size" -n"not __fish_seen_subcommand_from $ip_commands " 639complete -c ip -f -o iec -d"Print human readable rates in IEC units" -n"not __fish_seen_subcommand_from $ip_commands " 640complete -c ip -f -o br -l brief -d"Print only basic information in a tabular format" -n"not __fish_seen_subcommand_from $ip_commands " 641complete -c ip -f -s j -l json -d"Output results in JSON" -n"not __fish_seen_subcommand_from $ip_commands " 642complete -c ip -f -s p -l pretty -d"Output results in pretty JSON" -n"not __fish_seen_subcommand_from $ip_commands "