This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
fb-adb.bashsrc.in
1056 lines (1000 loc) · 29.9 KB
/
fb-adb.bashsrc.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Copyright (c) 2014, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in
# the LICENSE file in the root directory of this source tree. An
# additional grant of patent rights can be found in the PATENTS file
# in the same directory.
if [[ -z $_fb_adb_print_messages ]]; then
_fb_adb_print_messages=no
fi
: ${_fb_adb:=fb-adb}
if ! type -p "$_fb_adb" >/dev/null 2>&1; then
printf >&2 'WARNING: fb-adb not found\n'
printf >&2 'fb-adb completion requires fb-adb be on $PATH\n'
fi
_fb_adb_msg_need_newline=no
_fb_adb_msg() {
if [[ $_fb_adb_print_messages = yes ]]; then
if [[ $_fb_adb_msg_need_newline = yes ]]; then
printf >&2 '\n'
_fb_adb_msg_need_newline=no
fi
printf >&2 'fb-adb-completion: '"$1"'\n' "${@:2}"
fi
}
# # List of known fb-adb commands
# declare -a _fb_adb_command_list=(
# [REAL LIST GENERATED BELOW]
# foo
# bar
# )
# #$1 = command
# #$2 = name of argument
# #type in $result on success
# _fb_adb_opt_type_1() {
# [REAL CODE GENERATED BELOW]
# }
#$1 = command
#$2 = name of argument
#type in $result on success
#completion-relevant flag in $result2
#flag
_fb_adb_opt_type() {
if ! _fb_adb_opt_type_1 "$@"; then
return 1
fi
if [[ $result = [0-9]* ]]; then
result2="${result:0:1}"
result="${result:1}"
else
result2=0
fi
}
# # $1 = command
# # list of arguments in $aresult on success
# _fb_adb_opt_list() {
# [REAL CODE GENERATED BELOW]
# }
# # $1 = command
# # $2 = ordinal number of argument
# # type in $result on success
# _fb_adb_arg_type() {
# [REAL CODE GENERATED BELOW]
# }
declare -a _fb_adb_mksh_builtins=(
alias bg bind break builtin cat cd chdir command continue echo \
eval exec exit export false fc fg getopts global hash \
jobs kill let mknod print printf pwd read readonly \
realpath rename return set shift sleep source suspend \
test time times trap true global typeset ulimit umask \
unalias unset wait whence
)
_fb_adb_complete_next_word() {
wordno=$((wordno+1))
charno=0
}
# Add completion candidates to COMPREPLY, subject
# to munging and filtering. Uses getopt-style
# argument parsing.
#
# -f
# Each option starts with "N" or "F" to indicate whether we do or
# do not, respectively, expect further input on the word we
# just completed.
#
# -t TEMPLATE
# Original escaped form of the word we're completing. We try to
# provide completions escaped in the same style.
#
# -p PREFIX
# Limit candidates to ones matching prefix $PREFIX
#
# Remaining arguments are completion candidates.
#
_fb_adb_add_candidates() {
local OPTIND=1
local OPTARG=
local prefix=
local escaped_prefix=
local template=
local have_template=0
local -i finalization=0
local c
while getopts ":fp:P:t:" c; do
case "$c" in
't')
template=$OPTARG
have_template=1
;;
'f')
finalization=1
;;
'p')
prefix=$OPTARG
;;
'P')
escaped_prefix=$OPTARG
;;
*)
_fb_adb_getopts_default "$c"
return 1
;;
esac
done
shift $((OPTIND-1))
local candidate
local -a candidates=("$@")
local -a filtered_candidates=()
for candidate in "${candidates[@]}"; do
if ((!finalization)); then
candidate="F$candidate"
fi
if ! [[ $candidate = [FN]* ]]; then
_fb_adb_msg 'bad candidate syntax %q' "$candidate"
return 1
fi
local munged_prefix="${candidate:0:1}$prefix"
if [[ $candidate = "$munged_prefix"* ]]; then
filtered_candidates+=("$candidate")
fi
done
if ((finalization)) && ((${#filtered_candidates[@]} == 1)); then
candidate=${filtered_candidates[0]}
if [[ $candidate = F* ]]; then
compopt +o nospace
elif [[ $candidate = N* ]]; then
compopt -o nospace
else
_fb_adb_msg 'internal error: bad candidate %q' "$candidate"
return 1
fi
fi
for candidate in "${filtered_candidates[@]}"; do
local -a reescape_args=()
if [[ $candidate = F* ]]; then
reescape_args+=(-i)
fi
if ((have_template)); then
reescape_args+=(-t "$template")
fi
if ! _fb_adb_reescape \
"${reescape_args[@]}" -- "${candidate:1}";
then
return 1
fi
COMPREPLY+=("$result")
done
}
# Split a string
# $1 - IFS value for split
# $2 - Value to split
# result on success in $aresult
_fb_adb_split() {
local IFS=$1
read -ra aresult <<<"$2"
}
# Internal helper for _fb_adb_device_ls. Run only in a subshell.
_fb_adb_device_ls_1() {
set -o pipefail
"$_fb_adb" \
finfo-json "${slurped_adb_options[@]}" -i "ls:$1" "${@:2}" \
2>/dev/null | \
jq -r '(.[].ls.result[]?)|(.d_name,.stat.result.modes[0:1],.execp.result)|@sh'
}
_fb_adb_getopts_default() {
case "$1" in
'?')
_fb_adb_msg 'invalid option %s' "$OPTARG"
;;
':')
_fb_adb_msg 'missing argument for option %s' "$OPTARG"
;;
*)
_fb_adb_msg 'internal error'
;;
esac
}
# Run finfo on device and decode the JSON reply.
#
# -e
# Query using access(2) whether each file can be executed.
#
# Positional arguments are paths to list. Results are in $aresult on
# success and are triples of d_name, ftype, and execp results.
_fb_adb_device_ls() {
local OPTIND=1
local OPTARG=
local ls_opts=stat
while getopts ":e" c; do
case "$c" in
'e')
ls_opts+=+execp
;;
*)
_fb_adb_getopts_default "$c"
return 1
;;
esac
done
shift $((OPTIND-1))
# Pop quiz: what's $? after the command `local foo="$(false)"'?
# What about local foo; foo="$(false)"? Fuck, I love shell.
local data
data="$(_fb_adb_device_ls_1 "$ls_opts" "$@")"
if (($? != 0)); then
_fb_adb_msg 'fb-adb file listing failed'
return 1
fi
eval "aresult=($data)" # actually safe: jq shell-quotes for us
}
# Helper for _fb_adb_device_properties; run in subshell only.
_fb_adb_device_properties_1() {
set -o pipefail
"$_fb_adb" getprop "${slurped_adb_options[@]}" \
2>/dev/null | \
jq -r -e '.|keys|@sh'
}
# Retrieve names of device properties.
# Result list is in $aresult.
_fb_adb_device_properties() {
local data
data="$(_fb_adb_device_properties_1 "$@")"
if (($? != 0)); then
_fb_adb_msg 'fb-adb property listing failed'
return 1
fi
eval "aresult=($data)" # actually safe: jq shell-quotes for us
}
# Try to extract raw value of a shell word. $1 - escaped shell word
# (may be truncated) On success, result in $result and parse state in
# $result2. (See code for possible parse state names.)
_fb_adb_unescape() {
local escaped=$1
local i=0
local state=normal
for ((i=0; i<${#escaped}; ++i)); do
local c=${escaped:$i:1}
case "$state" in
normal)
case "$c" in
'`'|'!')
_fb_adb_msg 'unsafe string [%s]' "$escaped"
return 1
;;
"\\")
state=escaped
;;
'$')
state=dollar
;;
"'")
state=single-quoted
;;
'"')
state=double-quoted
;;
'|'|'&'|';'|'('|')'|'<'|'>'|'#')
_fb_adb_msg 'unescaped metacharacters in [%s]' \
"$escaped"
return 1
;;
' '|$'\t')
_fb_adb_msg 'whitespace should be pre-escaped?!'
return 1
;;
esac
;;
escaped)
state=normal
;;
dollar)
case "$c" in
'(')
_fb_adb_msg 'unsafe string [%s]' "$escaped"
return 1
;;
"'")
state=ansi-c-quoted
;;
'$')
state=normal
;;
*)
i=$((i-1)) # Reconsider character
state=normal
;;
esac
;;
single-quoted)
case "$c" in
"'")
state=normal
;;
esac
;;
double-quoted)
case "$c" in
'`')
_fb_adb_msg 'unsafe string [%s]' "$escaped"
return 1
;;
"\\")
state=double-quoted-backslash
;;
'"')
state=normal
;;
'$')
state=double-quoted-dollar
;;
esac
;;
double-quoted-dollar)
case "$c" in
'(')
_fb_adb_msg 'unsafe string [%s]' "$escaped"
return 1
;;
*)
state=double-quoted
;;
esac
;;
double-quoted-backslash)
state=double-quoted
;;
ansi-c-quoted)
case "$c" in
"\\")
state=ansi-c-quoted-backslash
;;
'"')
state=normal
;;
esac
;;
ansi-c-quoted-backslash)
state=ansi-c-quoted
;;
*)
_fb_adb_msg 'unexpected state [%s]' "$state"
return 1
;;
esac
done
case "$state" in
normal|dollar)
true
;;
single-quoted|ansi-c-quoted)
escaped="$escaped'"
;;
double-quoted|double-quoted-dollar)
escaped="$escaped\""
;;
*)
_fb_adb_msg 'unexpected closing state [%s]' "$state"
return 1
;;
esac
eval "result=$escaped" # Yuck
result2=$state
}
# Escape a shell word; less uglifying than %q
#
# -i
# Incomplete mode: leave our trailing quotes
#
# -t ESCAPED_WORD
# Try to escape $1 in the style in which ESCAPED_WORD is escaped
#
# $1 - shell word to escape
# On success, result in $result
_fb_adb_reescape() {
local OPTIND=1
local OPTARG=
local template=
local -i incomplete=0
while getopts ":it:" c; do
case "$c" in
'i')
incomplete=1
;;
't')
template=$OPTARG
;;
*)
_fb_adb_getopts_default "$c"
return 1
;;
esac
done
shift $((OPTIND-1))
if (($#>1)); then
_fb_adb_msg 'too many arguments to _fb_adb_reescape'
return 1
fi
local raw=$1
case "$template" in
\"*)
result=
local i
for ((i=0; i < ${#raw}; ++i)); do
local c=${raw:$i:1}
case "$c" in
'"'|"\\"|'`'|'$')
result="$result\\$c"
;;
'!')
result="$result\"'!'\""
;;
*)
result="$result$c"
;;
esac
done
if ((incomplete)); then
result="\"$result"
else
result="\"$result\""
fi
;;
\'*)
result=
local i
for ((i=0; i < ${#raw}; ++i)); do
local c=${raw:$i:1}
case "$c" in
"'")
result="$result'\"'\"'"
;;
*)
result="$result$c"
;;
esac
done
if ((incomplete)); then
result="'$result"
else
result="'$result'"
fi
;;
*)
printf -v result '%q' "$1"
;;
esac
}
# Complete a file on device; optionally restricted to executables.
#
# -e
# Complete only executables and directories.
#
# -p
# Permit completing words from PATH
#
# -b
# Add shell builtins to the completion set.
#
# $1 is the word to complete. $2 is the original, unescaped word.
#
# On success, completions in COMPREPLY.
#
_fb_adb_complete_device_path() {
local OPTIND=1
local OPTARG=
local c
local -i exe_only=0
local -i path_search=0
local -i add_builtins=0
while getopts ":epb" c; do
case "$c" in
'b')
add_builtins=1
;;
'e')
exe_only=1
;;
'p')
path_search=1
;;
*)
_fb_adb_getopts_default "$c"
return 1
;;
esac
done
shift $((OPTIND-1))
if (($# != 2)); then
_fb_adb_msg 'wrong number of arguments'
return 1
fi
local word=$1
local orig_escaped_word=$2
_fb_adb_msg 'cdp word:[%s] exe_only:[%s] path_search:[%s] builtins:[%s]' \
"$word" "$exe_only" "$path_search" "$add_builtins"
if ((path_search)) && [[ $word = */* ]]; then
# Match execvp semantics: do not search PATH when word
# contains a slash.
path_search=0
fi
if ((!path_search)); then
local basename=${word##*/}
local dprefix=${word%"$basename"}
_fb_adb_msg 'basename:[%s] dprefix:[%s]' \
"$basename" "$dprefix"
local -a ls_opts=()
if ((exe_only)); then
ls_opts+=('-e')
fi
if ! _fb_adb_device_ls "${ls_opts[@]}" -- "${dprefix:-.}"; then
return 1
fi
else
local device_path=$("$_fb_adb" shell \
"${slurped_adb_options[@]}" \
'echo $PATH' 2>/dev/null)
if (($?!=0)); then
_fb_adb_msg 'cannot get PATH'
return 1
fi
_fb_adb_split ':' "$device_path"
if ! _fb_adb_device_ls -e -- "${aresult[@]}"; then
return 1
fi
fi
local i
local -a candidates=()
for ((i=0; i < ${#aresult[@]}; i+=3)); do
local d_name=${aresult[((i+0))]}
local ftype=${aresult[((i+1))]}
local execp=${aresult[((i+2))]}
_fb_adb_msg 'd_name:[%s] ftype:[%s] execp:[%s]' \
"$d_name" "$ftype" "$execp"
if ((path_search)) && [[ $ftype = 'd' ]]; then
_fb_adb_msg 'skipping directory in path-search mode'
continue
fi
if ((exe_only)) && [[ $execp != 'true' ]]; then
if [[ $ftype = '-' ]] && [[ $execp = 'true' ]]; then
true
elif [[ $ftype = 'd' ]]; then
true
else
_fb_adb_msg 'skipping non-executable in exe_only mode'
continue
fi
fi
if [[ $ftype = 'd' ]]; then
candidates+=("N$dprefix$d_name/")
else
candidates+=("F$dprefix$d_name")
fi
done
if ((add_builtins)); then
candidates+=("${_fb_adb_mksh_builtins[@]/#/F}")
fi
if ! _fb_adb_add_candidates \
-p "$word" \
-t "$orig_escaped_word" \
-f -- "${candidates[@]}";
then
return 1
fi
}
# Complete a value of some type.
# $1 - type
# $2 - word to complete
# $3 - offset within word where completion should start
_fb_adb_complete_value() {
local type=$1
local word=$2
local incomplete=0
local saved_prefix=${word:0:$3}
local orig_escaped_word=${word:$3}
if ! _fb_adb_unescape "$orig_escaped_word"; then
return 1
fi
local word=$result
case "$type" in
enum:*)
_fb_adb_split ';' "${type:5}"
local value
local -a enum_values=()
for value in "${aresult[@]}"; do
if [[ $value = *\* ]]; then
enum_values+=("N${value/%\*/}")
else
enum_values+=("F${value}")
fi
done
if ! _fb_adb_add_candidates \
-p "$word" \
-t "$orig_escaped_word" \
-f -- "${enum_values[@]}";
then
return 1
fi
;;
device-path)
if ! _fb_adb_complete_device_path \
-- "$word" "$orig_escaped_word";
then
return 1
fi
;;
device-exe)
if ! _fb_adb_complete_device_path \
-ep -- "$word" "$orig_escaped_word";
then
return 1
fi
;;
device-command)
if ! _fb_adb_complete_device_path \
-epb -- "$word" "$orig_escaped_word";
then
return 1
fi
;;
device-property)
if ! _fb_adb_device_properties; then
return 1
fi
if ! _fb_adb_add_candidates \
-p "$word" \
-t "$orig_escaped_word" \
-- "${aresult[@]}";
then
return 1
fi
;;
host-path)
compopt -o default
;;
hostname)
local hostname
local -a hostnames=()
while read -r hostname; do
hostnames+=("$hostname")
done < <(compgen -A hostname)
if ! _fb_adb_add_candidates \
-p "$word" \
-t "$orig_escaped_word" \
-- "${hostnames[@]}";
then
return 1
fi
;;
fbadb-command)
if ! _fb_adb_add_candidates \
-p "$word" \
-- "${_fb_adb_command_list[@]}"; then
return 1
fi
;;
*)
_fb_adb_msg 'unknown completion type %s' "$type"
return 1
;;
esac
COMPREPLY=("${COMPREPLY[@]/#/$saved_prefix}")
}
# Complete a command
# array of completion works; of which the last is the word
# being completed.
_fb_adb_complete_command() {
local command=$1
local -ir nwords=${#words[@]}
local word=
local -i wordno=0
local -i charno=0
local -i noopt=0 # Saw "--" argument
local state=word
local result=
local result2=
local -a aresult=()
local -i optarg_start=0
local optopt=
local optarg_type=
local optarg=
local optarg_length=
local optopt_for_completion=
local -i argno=0
local c=
while ((wordno<nwords)); do
word=${words[$wordno]}
c=${word:$charno:1}
if [[ $c = "" ]] && ((wordno+1==nwords)); then
break
fi
_fb_adb_msg 'wordno:%s charno:%s c:%-2q state:%s' \
$wordno $charno "$c" "$state"
case "$state" in
word)
case "$c" in
'-')
if ((!noopt && charno==0)); then
state=short-opt
fi
charno=$((charno+1))
;;
'')
_fb_adb_msg 'got word %q argno:%d' "$word" "$argno"
argno=$((argno+1))
noopt=1
_fb_adb_complete_next_word
;;
*)
charno=$((charno+1))
;;
esac
;;
short-opt)
case "$c" in
'-')
charno=$((charno+1))
state=maybe-long-opt
;;
'')
if ((charno==1)); then
# Treat word containing only "-" as arg
state=word
else
_fb_adb_complete_next_word
state=word
fi
;;
*)
optopt="-$c"
if ! _fb_adb_opt_type "$command" "$optopt"; then
return 1
fi
optopt_for_completion=$result2
if [[ $result = "none" ]]; then
_fb_adb_msg \
'got option %s (no arg) (comprel:%s)' \
"$optopt" "$optopt_for_completion"
if ((optopt_for_completion)); then
slurped_adb_options+=("$optopt")
fi
else
optarg_type=$result
optarg_start=$((charno+1))
state=opt-arg
fi
charno=$((charno+1))
;;
esac
;;
opt-arg)
case "$c" in
'')
if ((optarg_start==charno && charno>0)); then
optarg_start=0
state=opt-arg
else
optarg_length=$((charno-optarg_start))
optarg="${word:$optarg_start:$optarg_length}"
_fb_adb_msg 'got option %s arg:%q (comprel:%s)' \
"$optopt" "$optarg" \
"$optopt_for_completion"
if ((optopt_for_completion)); then
slurped_adb_options+=("$optopt" "$optarg")
fi
state=word
fi
_fb_adb_complete_next_word
;;
*)
charno=$((charno+1))
;;
esac
;;
maybe-long-opt)
case "$c" in
'')
_fb_adb_complete_next_word
noopt=1
state=word
;;
*)
state=long-opt
charno=$((charno+1))
;;
esac
;;
long-opt-arg)
case "$c" in
'=')
# Sometimes "=" splits a word
state=long-opt-arg-2
charno=$((charno+1))
;;
*)
state=opt-arg
;;
esac
;;
long-opt-arg-2)
case "$c" in
'')
optarg_start=0
state=opt-arg
_fb_adb_complete_next_word
;;
*)
state=opt-arg;
;;
esac
;;
long-opt)
case "$c" in
'=')
# Sometimes "=" doesn't split a word
optopt=${word:0:$charno}
if ! _fb_adb_opt_type "$command" "$optopt"; then
return 1
fi
if [[ $result = "none" ]]; then
_fb_adb_msg "option %s takes no arguments!" \
"$optopt"
return 1
fi
optarg_type=$result
optarg_start=$((charno+1))
charno=$((charno+1))
state=opt-arg
;;
'')
optopt=$word
if ! _fb_adb_opt_type "$command" "$optopt"; then
return 1
fi
optopt_for_completion=$result2
if [[ $result = "none" ]]; then
_fb_adb_msg \
'got option %s (no arg) (comprel:%s)' \
"$optopt" "$optopt_for_completion"
state=word
if ((optopt_for_completion)); then
slurped_adb_options+=("$optopt")
fi
else
optarg_type=$result
optarg_start=0
state=long-opt-arg
fi
_fb_adb_complete_next_word
;;
*)
charno=$((charno+1))
;;
esac
;;
*)
printf >&2 'fb-adb completion: illegal state %q' "$state"
return 1
;;
esac
done
_fb_adb_msg 'final parsing state:%s' "$state"
# It's not worth completing short options. Pretend that if
# (looking-back "\\_<-"), we're completing a long option.
if [[ $state = short-opt ]] && ((charno==1)); then
state=long-opt
fi
case "$state" in
word)
if ! _fb_adb_arg_type "$command" "$argno"; then
return 1
fi
_fb_adb_complete_value "$result" "$word" 0
;;
opt-arg)
_fb_adb_complete_value \
"$optarg_type" \
"$word" \
"$optarg_start"
;;
long-opt-arg-2)
_fb_adb_complete_value \
"$optarg_type" \
"${word:1}" \
"$optarg_start"
;;
short-opt)
COMPREPLY=("$word") # Auto-inserted space makes new shell word
;;
long-opt|maybe-long-opt)
if ! _fb_adb_opt_list "$command"; then
return 1
fi
local opt=
local -a options=()
for opt in "${aresult[@]}"; do
if [[ $opt = --* ]]; then
if ! _fb_adb_opt_type "$command" "$opt"; then
return 1
fi
if [[ $result = "none" ]]; then
options+=("F$opt")
else
compopt -o nospace
options+=("N$opt=")
fi
fi
done
if ! _fb_adb_add_candidates -p "$word" -f -- "${options[@]}";
then
return 1
fi
;;
*)
_fb_adb_msg 'cannot complete in state %s' "$state"
return 1
;;
esac
}
# Main entry point for completion machinery
_fb_adb_complete() {
_fb_adb_msg_need_newline=yes
compopt +o nospace
compopt +o bashdefault
compopt +o default
local i=
local command=
local word=
local command_index=