format_completion_bash.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <ctype.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | |||
| 4 | #include "ap_internal.h" | ||
| 5 | |||
| 6 | 16626 | static bool action_takes_no_value(ap_action action) { | |
| 7 |
4/4✓ Branch 0 taken 11120 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 11096 times.
✓ Branch 3 taken 24 times.
|
11132 | return action == AP_ACTION_STORE_TRUE || action == AP_ACTION_STORE_FALSE || |
| 8 |
4/4✓ Branch 0 taken 11132 times.
✓ Branch 1 taken 5494 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 11084 times.
|
27758 | action == AP_ACTION_COUNT || action == AP_ACTION_STORE_CONST; |
| 9 | } | ||
| 10 | |||
| 11 | 16676 | static bool option_takes_value(const ap_arg_def *def) { | |
| 12 |
5/6✓ Branch 0 taken 16676 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16626 times.
✓ Branch 3 taken 50 times.
✓ Branch 5 taken 11084 times.
✓ Branch 6 taken 5542 times.
|
16676 | return def && def->is_optional && !action_takes_no_value(def->opts.action); |
| 13 | } | ||
| 14 | |||
| 15 | 3074 | static ap_completion_kind option_completion_kind(const ap_arg_def *def) { | |
| 16 |
2/2✓ Branch 1 taken 994 times.
✓ Branch 2 taken 2080 times.
|
3074 | if (!option_takes_value(def)) { |
| 17 | 994 | return AP_COMPLETION_KIND_NONE; | |
| 18 | } | ||
| 19 |
2/2✓ Branch 0 taken 588 times.
✓ Branch 1 taken 1492 times.
|
2080 | if (def->opts.completion_kind != AP_COMPLETION_KIND_NONE) { |
| 20 | 588 | return def->opts.completion_kind; | |
| 21 | } | ||
| 22 |
3/4✓ Branch 0 taken 1002 times.
✓ Branch 1 taken 490 times.
✓ Branch 2 taken 1002 times.
✗ Branch 3 not taken.
|
1492 | if (def->opts.choices.items && def->opts.choices.count > 0) { |
| 23 | 1002 | return AP_COMPLETION_KIND_CHOICES; | |
| 24 | } | ||
| 25 | 490 | return AP_COMPLETION_KIND_NONE; | |
| 26 | } | ||
| 27 | |||
| 28 | 852 | static bool option_has_dynamic_completion(const ap_arg_def *def) { | |
| 29 |
3/4✓ Branch 1 taken 852 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 116 times.
✓ Branch 4 taken 736 times.
|
852 | return option_takes_value(def) && def->opts.completion_callback != NULL; |
| 30 | } | ||
| 31 | |||
| 32 | 680 | static int option_value_count(const ap_arg_def *def) { | |
| 33 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 680 times.
|
680 | if (!option_takes_value(def)) { |
| 34 | ✗ | return 0; | |
| 35 | } | ||
| 36 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 676 times.
|
680 | if (def->opts.nargs == AP_NARGS_FIXED) { |
| 37 | 4 | return def->opts.nargs_count; | |
| 38 | } | ||
| 39 | 676 | return 1; | |
| 40 | } | ||
| 41 | |||
| 42 | 1024 | static const char *option_value_mode(const ap_arg_def *def) { | |
| 43 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1024 times.
|
1024 | if (!option_takes_value(def)) { |
| 44 | ✗ | return "none"; | |
| 45 | } | ||
| 46 |
4/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1014 times.
|
1024 | switch (def->opts.nargs) { |
| 47 | 2 | case AP_NARGS_OPTIONAL: | |
| 48 | 2 | return "optional"; | |
| 49 | 4 | case AP_NARGS_ZERO_OR_MORE: | |
| 50 | case AP_NARGS_ONE_OR_MORE: | ||
| 51 | 4 | return "multi"; | |
| 52 | 4 | case AP_NARGS_FIXED: | |
| 53 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return def->opts.nargs_count > 1 ? "fixed" : "single"; |
| 54 | 1014 | case AP_NARGS_ONE: | |
| 55 | default: | ||
| 56 | 1014 | return "single"; | |
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | 852 | static const char *completion_dispatch_kind_name(const ap_arg_def *def) { | |
| 61 |
2/2✓ Branch 1 taken 116 times.
✓ Branch 2 taken 736 times.
|
852 | if (option_has_dynamic_completion(def)) { |
| 62 | 116 | return "dynamic"; | |
| 63 | } | ||
| 64 |
5/5✓ Branch 1 taken 370 times.
✓ Branch 2 taken 118 times.
✓ Branch 3 taken 114 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 132 times.
|
736 | switch (option_completion_kind(def)) { |
| 65 | 370 | case AP_COMPLETION_KIND_CHOICES: | |
| 66 | 370 | return "choices"; | |
| 67 | 118 | case AP_COMPLETION_KIND_FILE: | |
| 68 | 118 | return "file"; | |
| 69 | 114 | case AP_COMPLETION_KIND_DIRECTORY: | |
| 70 | 114 | return "directory"; | |
| 71 | 2 | case AP_COMPLETION_KIND_COMMAND: | |
| 72 | 2 | return "command"; | |
| 73 | 132 | case AP_COMPLETION_KIND_NONE: | |
| 74 | default: | ||
| 75 | 132 | return "none"; | |
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | 1600 | static int append_single_quoted(ap_string_builder *sb, const char *text) { | |
| 80 |
1/2✓ Branch 0 taken 1600 times.
✗ Branch 1 not taken.
|
1600 | const unsigned char *p = (const unsigned char *)(text ? text : ""); |
| 81 | |||
| 82 |
2/2✓ Branch 1 taken 24 times.
✓ Branch 2 taken 1576 times.
|
1600 | if (ap_sb_appendf(sb, "'") != 0) { |
| 83 | 24 | return -1; | |
| 84 | } | ||
| 85 |
2/2✓ Branch 0 taken 8262 times.
✓ Branch 1 taken 1434 times.
|
9696 | while (*p != '\0') { |
| 86 |
2/2✓ Branch 0 taken 190 times.
✓ Branch 1 taken 8072 times.
|
8262 | if (*p == '\'') { |
| 87 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 188 times.
|
190 | if (ap_sb_appendf(sb, "'\\''") != 0) { |
| 88 | 2 | return -1; | |
| 89 | } | ||
| 90 |
2/2✓ Branch 1 taken 140 times.
✓ Branch 2 taken 7932 times.
|
8072 | } else if (ap_sb_appendf(sb, "%c", (char)*p) != 0) { |
| 91 | 140 | return -1; | |
| 92 | } | ||
| 93 | 8120 | p++; | |
| 94 | } | ||
| 95 | 1434 | return ap_sb_appendf(sb, "'"); | |
| 96 | } | ||
| 97 | |||
| 98 | 770 | static int append_identifier(ap_string_builder *sb, const char *text) { | |
| 99 | size_t i; | ||
| 100 |
1/2✓ Branch 0 taken 770 times.
✗ Branch 1 not taken.
|
770 | const char *value = text ? text : "argparse_c"; |
| 101 | |||
| 102 |
2/2✓ Branch 0 taken 4234 times.
✓ Branch 1 taken 726 times.
|
4960 | for (i = 0; value[i] != '\0'; i++) { |
| 103 | 4234 | unsigned char c = (unsigned char)value[i]; | |
| 104 |
4/4✓ Branch 0 taken 398 times.
✓ Branch 1 taken 3836 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 394 times.
|
4234 | if (isalnum(c) || c == '_') { |
| 105 |
2/2✓ Branch 1 taken 40 times.
✓ Branch 2 taken 3800 times.
|
3840 | if (ap_sb_appendf(sb, "%c", (char)c) != 0) { |
| 106 | 40 | return -1; | |
| 107 | } | ||
| 108 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 390 times.
|
394 | } else if (ap_sb_appendf(sb, "_") != 0) { |
| 109 | 4 | return -1; | |
| 110 | } | ||
| 111 | } | ||
| 112 | 726 | return 0; | |
| 113 | } | ||
| 114 | |||
| 115 | 6024 | static int append_parser_key(ap_string_builder *sb, const ap_parser *parser) { | |
| 116 |
3/4✓ Branch 0 taken 6024 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4648 times.
✓ Branch 3 taken 1376 times.
|
6024 | if (!parser || !parser->parent) { |
| 117 | 4648 | return ap_sb_appendf(sb, "root"); | |
| 118 | } | ||
| 119 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 1362 times.
|
1376 | if (append_parser_key(sb, parser->parent) != 0) { |
| 120 | 14 | return -1; | |
| 121 | } | ||
| 122 | 1362 | return ap_sb_appendf(sb, "/%s", parser->command_name); | |
| 123 | } | ||
| 124 | |||
| 125 | 1424 | static int append_load_parser_cases(ap_string_builder *sb, | |
| 126 | const ap_parser *parser) { | ||
| 127 | int i; | ||
| 128 | int j; | ||
| 129 | bool first; | ||
| 130 | |||
| 131 |
6/6✓ Branch 1 taken 1414 times.
✓ Branch 2 taken 10 times.
✓ Branch 4 taken 1398 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 1388 times.
|
2822 | if (ap_sb_appendf(sb, " ") != 0 || append_parser_key(sb, parser) != 0 || |
| 132 | 1398 | ap_sb_appendf(sb, ")\n parser_subcommands=") != 0) { | |
| 133 | 36 | return -1; | |
| 134 | } | ||
| 135 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1378 times.
|
1388 | if (ap_sb_appendf(sb, "'") != 0) { |
| 136 | 10 | return -1; | |
| 137 | } | ||
| 138 |
2/2✓ Branch 0 taken 938 times.
✓ Branch 1 taken 1370 times.
|
2308 | for (i = 0; i < parser->subcommands_count; i++) { |
| 139 |
4/4✓ Branch 0 taken 304 times.
✓ Branch 1 taken 634 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 302 times.
|
938 | if (i > 0 && ap_sb_appendf(sb, " ") != 0) { |
| 140 | 2 | return -1; | |
| 141 | } | ||
| 142 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 930 times.
|
936 | if (ap_sb_appendf(sb, "%s", parser->subcommands[i].name) != 0) { |
| 143 | 6 | return -1; | |
| 144 | } | ||
| 145 | } | ||
| 146 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1360 times.
|
1370 | if (ap_sb_appendf(sb, "'\n parser_options='") != 0) { |
| 147 | 10 | return -1; | |
| 148 | } | ||
| 149 | 1360 | first = true; | |
| 150 |
2/2✓ Branch 0 taken 3426 times.
✓ Branch 1 taken 1302 times.
|
4728 | for (i = 0; i < parser->defs_count; i++) { |
| 151 | 3426 | const ap_arg_def *def = &parser->defs[i]; | |
| 152 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 3416 times.
|
3426 | if (!def->is_optional) { |
| 153 | 10 | continue; | |
| 154 | } | ||
| 155 |
2/2✓ Branch 0 taken 4770 times.
✓ Branch 1 taken 3358 times.
|
8128 | for (j = 0; j < def->flags_count; j++) { |
| 156 |
4/4✓ Branch 0 taken 3410 times.
✓ Branch 1 taken 1360 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 3386 times.
|
4770 | if (!first && ap_sb_appendf(sb, " ") != 0) { |
| 157 | 24 | return -1; | |
| 158 | } | ||
| 159 | 4746 | first = false; | |
| 160 |
2/2✓ Branch 1 taken 34 times.
✓ Branch 2 taken 4712 times.
|
4746 | if (ap_sb_appendf(sb, "%s", def->flags[j]) != 0) { |
| 161 | 34 | return -1; | |
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1292 times.
|
1302 | if (ap_sb_appendf(sb, "'\n parser_value_options='") != 0) { |
| 166 | 10 | return -1; | |
| 167 | } | ||
| 168 | 1292 | first = true; | |
| 169 |
2/2✓ Branch 0 taken 3276 times.
✓ Branch 1 taken 1270 times.
|
4546 | for (i = 0; i < parser->defs_count; i++) { |
| 170 | 3276 | const ap_arg_def *def = &parser->defs[i]; | |
| 171 |
2/2✓ Branch 1 taken 1324 times.
✓ Branch 2 taken 1952 times.
|
3276 | if (!option_takes_value(def)) { |
| 172 | 1324 | continue; | |
| 173 | } | ||
| 174 |
2/2✓ Branch 0 taken 1952 times.
✓ Branch 1 taken 1930 times.
|
3882 | for (j = 0; j < def->flags_count; j++) { |
| 175 |
4/4✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 828 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1116 times.
|
1952 | if (!first && ap_sb_appendf(sb, " ") != 0) { |
| 176 | 8 | return -1; | |
| 177 | } | ||
| 178 | 1944 | first = false; | |
| 179 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 1930 times.
|
1944 | if (ap_sb_appendf(sb, "%s", def->flags[j]) != 0) { |
| 180 | 14 | return -1; | |
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1260 times.
|
1270 | if (ap_sb_appendf(sb, "'\n parser_flag_only_options='") != 0) { |
| 185 | 10 | return -1; | |
| 186 | } | ||
| 187 | 1260 | first = true; | |
| 188 |
2/2✓ Branch 0 taken 3142 times.
✓ Branch 1 taken 1230 times.
|
4372 | for (i = 0; i < parser->defs_count; i++) { |
| 189 | 3142 | const ap_arg_def *def = &parser->defs[i]; | |
| 190 |
4/4✓ Branch 0 taken 3132 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 1850 times.
✓ Branch 4 taken 1282 times.
|
3142 | if (!def->is_optional || option_takes_value(def)) { |
| 191 | 1860 | continue; | |
| 192 | } | ||
| 193 |
2/2✓ Branch 0 taken 2536 times.
✓ Branch 1 taken 1252 times.
|
3788 | for (j = 0; j < def->flags_count; j++) { |
| 194 |
4/4✓ Branch 0 taken 1276 times.
✓ Branch 1 taken 1260 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 1266 times.
|
2536 | if (!first && ap_sb_appendf(sb, " ") != 0) { |
| 195 | 10 | return -1; | |
| 196 | } | ||
| 197 | 2526 | first = false; | |
| 198 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 2506 times.
|
2526 | if (ap_sb_appendf(sb, "%s", def->flags[j]) != 0) { |
| 199 | 20 | return -1; | |
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1220 times.
|
1230 | if (ap_sb_appendf(sb, "'\n ;;\n") != 0) { |
| 204 | 10 | return -1; | |
| 205 | } | ||
| 206 | |||
| 207 |
2/2✓ Branch 0 taken 770 times.
✓ Branch 1 taken 1124 times.
|
1894 | for (i = 0; i < parser->subcommands_count; i++) { |
| 208 |
2/2✓ Branch 1 taken 96 times.
✓ Branch 2 taken 674 times.
|
770 | if (append_load_parser_cases(sb, parser->subcommands[i].parser) != 0) { |
| 209 | 96 | return -1; | |
| 210 | } | ||
| 211 | } | ||
| 212 | 1124 | return 0; | |
| 213 | } | ||
| 214 | |||
| 215 | 962 | static int append_choice_cases(ap_string_builder *sb, const ap_parser *parser) { | |
| 216 | int i; | ||
| 217 | int j; | ||
| 218 | int k; | ||
| 219 | |||
| 220 |
2/2✓ Branch 0 taken 2338 times.
✓ Branch 1 taken 848 times.
|
3186 | for (i = 0; i < parser->defs_count; i++) { |
| 221 | 2338 | const ap_arg_def *def = &parser->defs[i]; | |
| 222 |
2/2✓ Branch 1 taken 636 times.
✓ Branch 2 taken 1702 times.
|
2338 | if (option_completion_kind(def) != AP_COMPLETION_KIND_CHOICES || |
| 223 |
2/4✓ Branch 0 taken 636 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 636 times.
|
636 | !def->opts.choices.items || def->opts.choices.count <= 0) { |
| 224 | 1702 | continue; | |
| 225 | } | ||
| 226 |
2/2✓ Branch 0 taken 636 times.
✓ Branch 1 taken 522 times.
|
1158 | for (j = 0; j < def->flags_count; j++) { |
| 227 |
4/4✓ Branch 1 taken 630 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 622 times.
✓ Branch 4 taken 8 times.
|
1266 | if (ap_sb_appendf(sb, " ") != 0 || |
| 228 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 616 times.
|
1252 | append_parser_key(sb, parser) != 0 || |
| 229 | 622 | ap_sb_appendf(sb, ":%s) printf '%%s\\n' ", def->flags[j]) != 0) { | |
| 230 | 20 | return -1; | |
| 231 | } | ||
| 232 |
2/2✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 528 times.
|
1736 | for (k = 0; k < def->opts.choices.count; k++) { |
| 233 |
4/4✓ Branch 0 taken 592 times.
✓ Branch 1 taken 616 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 586 times.
|
1208 | if (k > 0 && ap_sb_appendf(sb, " ") != 0) { |
| 234 | 6 | return -1; | |
| 235 | } | ||
| 236 |
2/2✓ Branch 1 taken 82 times.
✓ Branch 2 taken 1120 times.
|
1202 | if (append_single_quoted(sb, def->opts.choices.items[k]) != 0) { |
| 237 | 82 | return -1; | |
| 238 | } | ||
| 239 | } | ||
| 240 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 522 times.
|
528 | if (ap_sb_appendf(sb, ";;\n") != 0) { |
| 241 | 6 | return -1; | |
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 |
2/2✓ Branch 0 taken 516 times.
✓ Branch 1 taken 812 times.
|
1328 | for (i = 0; i < parser->subcommands_count; i++) { |
| 246 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 2 taken 480 times.
|
516 | if (append_choice_cases(sb, parser->subcommands[i].parser) != 0) { |
| 247 | 36 | return -1; | |
| 248 | } | ||
| 249 | } | ||
| 250 | 812 | return 0; | |
| 251 | } | ||
| 252 | |||
| 253 | 748 | static int append_value_mode_cases(ap_string_builder *sb, | |
| 254 | const ap_parser *parser) { | ||
| 255 | int i; | ||
| 256 | int j; | ||
| 257 | |||
| 258 |
2/2✓ Branch 0 taken 1834 times.
✓ Branch 1 taken 704 times.
|
2538 | for (i = 0; i < parser->defs_count; i++) { |
| 259 | 1834 | const ap_arg_def *def = &parser->defs[i]; | |
| 260 |
2/2✓ Branch 1 taken 780 times.
✓ Branch 2 taken 1054 times.
|
1834 | if (!option_takes_value(def)) { |
| 261 | 780 | continue; | |
| 262 | } | ||
| 263 |
2/2✓ Branch 0 taken 1054 times.
✓ Branch 1 taken 1010 times.
|
2064 | for (j = 0; j < def->flags_count; j++) { |
| 264 |
4/4✓ Branch 1 taken 1040 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 1024 times.
✓ Branch 4 taken 16 times.
|
2094 | if (ap_sb_appendf(sb, " ") != 0 || |
| 265 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 1010 times.
|
2064 | append_parser_key(sb, parser) != 0 || |
| 266 | 1024 | ap_sb_appendf(sb, ":%s) printf '%%s\\n' '%s' ;;\n", def->flags[j], | |
| 267 | option_value_mode(def)) != 0) { | ||
| 268 | 44 | return -1; | |
| 269 | } | ||
| 270 | } | ||
| 271 | } | ||
| 272 |
2/2✓ Branch 0 taken 420 times.
✓ Branch 1 taken 696 times.
|
1116 | for (i = 0; i < parser->subcommands_count; i++) { |
| 273 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 412 times.
|
420 | if (append_value_mode_cases(sb, parser->subcommands[i].parser) != 0) { |
| 274 | 8 | return -1; | |
| 275 | } | ||
| 276 | } | ||
| 277 | 696 | return 0; | |
| 278 | } | ||
| 279 | |||
| 280 | 632 | static int append_completion_kind_cases(ap_string_builder *sb, | |
| 281 | const ap_parser *parser) { | ||
| 282 | int i; | ||
| 283 | int j; | ||
| 284 | |||
| 285 |
2/2✓ Branch 0 taken 1546 times.
✓ Branch 1 taken 588 times.
|
2134 | for (i = 0; i < parser->defs_count; i++) { |
| 286 | 1546 | const ap_arg_def *def = &parser->defs[i]; | |
| 287 |
2/2✓ Branch 1 taken 664 times.
✓ Branch 2 taken 882 times.
|
1546 | if (!option_takes_value(def)) { |
| 288 | 664 | continue; | |
| 289 | } | ||
| 290 |
2/2✓ Branch 0 taken 882 times.
✓ Branch 1 taken 838 times.
|
1720 | for (j = 0; j < def->flags_count; j++) { |
| 291 |
4/4✓ Branch 1 taken 868 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 852 times.
✓ Branch 4 taken 16 times.
|
1750 | if (ap_sb_appendf(sb, " ") != 0 || |
| 292 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 838 times.
|
1720 | append_parser_key(sb, parser) != 0 || |
| 293 | 852 | ap_sb_appendf(sb, ":%s) printf '%%s\\n' '%s' ;;\n", def->flags[j], | |
| 294 | completion_dispatch_kind_name(def)) != 0) { | ||
| 295 | 44 | return -1; | |
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 580 times.
|
932 | for (i = 0; i < parser->subcommands_count; i++) { |
| 300 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 344 times.
|
352 | if (append_completion_kind_cases(sb, parser->subcommands[i].parser) != 0) { |
| 301 | 8 | return -1; | |
| 302 | } | ||
| 303 | } | ||
| 304 | 580 | return 0; | |
| 305 | } | ||
| 306 | |||
| 307 | 516 | static int append_value_count_cases(ap_string_builder *sb, | |
| 308 | const ap_parser *parser) { | ||
| 309 | int i; | ||
| 310 | int j; | ||
| 311 | |||
| 312 |
2/2✓ Branch 0 taken 1258 times.
✓ Branch 1 taken 472 times.
|
1730 | for (i = 0; i < parser->defs_count; i++) { |
| 313 | 1258 | const ap_arg_def *def = &parser->defs[i]; | |
| 314 |
2/2✓ Branch 1 taken 548 times.
✓ Branch 2 taken 710 times.
|
1258 | if (!option_takes_value(def)) { |
| 315 | 548 | continue; | |
| 316 | } | ||
| 317 |
2/2✓ Branch 0 taken 710 times.
✓ Branch 1 taken 666 times.
|
1376 | for (j = 0; j < def->flags_count; j++) { |
| 318 |
4/4✓ Branch 1 taken 696 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 680 times.
✓ Branch 4 taken 16 times.
|
1406 | if (ap_sb_appendf(sb, " ") != 0 || |
| 319 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 666 times.
|
1376 | append_parser_key(sb, parser) != 0 || |
| 320 | 680 | ap_sb_appendf(sb, ":%s) printf '%%d\\n' %d ;;\n", def->flags[j], | |
| 321 | option_value_count(def)) != 0) { | ||
| 322 | 44 | return -1; | |
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 |
2/2✓ Branch 0 taken 284 times.
✓ Branch 1 taken 464 times.
|
748 | for (i = 0; i < parser->subcommands_count; i++) { |
| 327 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 276 times.
|
284 | if (append_value_count_cases(sb, parser->subcommands[i].parser) != 0) { |
| 328 | 8 | return -1; | |
| 329 | } | ||
| 330 | } | ||
| 331 | 464 | return 0; | |
| 332 | } | ||
| 333 | |||
| 334 | 688 | char *ap_bash_completion_build(const ap_parser *parser) { | |
| 335 | ap_string_builder sb; | ||
| 336 | const char *prog; | ||
| 337 | |||
| 338 |
5/6✓ Branch 0 taken 686 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 686 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 684 times.
|
688 | if (!parser || !parser->prog || parser->prog[0] == '\0') { |
| 339 | 4 | return NULL; | |
| 340 | } | ||
| 341 |
2/4✓ Branch 0 taken 684 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 684 times.
|
684 | prog = parser->command_name && parser->command_name[0] != '\0' |
| 342 | ? parser->command_name | ||
| 343 | : parser->prog; | ||
| 344 | |||
| 345 | 684 | ap_sb_init(&sb); | |
| 346 |
4/4✓ Branch 1 taken 680 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 658 times.
✓ Branch 4 taken 22 times.
|
1364 | if (ap_sb_appendf(&sb, "# bash completion for %s\n_", parser->prog) != 0 || |
| 347 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 654 times.
|
1338 | append_identifier(&sb, prog) != 0 || |
| 348 | 658 | ap_sb_appendf( | |
| 349 | &sb, "() {\n" | ||
| 350 | " local cur parser_key parser_subcommands parser_options " | ||
| 351 | "parser_value_options parser_flag_only_options\n" | ||
| 352 | " local pending_option pending_mode pending_fixed_remaining " | ||
| 353 | "option_name value_prefix token choices completion_kind\n" | ||
| 354 | " local -a filtered candidates\n" | ||
| 355 | " cur=\"${COMP_WORDS[COMP_CWORD]}\"\n" | ||
| 356 | " parser_key='root'\n" | ||
| 357 | " pending_option=''\n" | ||
| 358 | " pending_mode=''\n" | ||
| 359 | " pending_fixed_remaining=0\n\n" | ||
| 360 | " __ap_completion_load_parser() {\n" | ||
| 361 | " case \"$1\" in\n") != 0) { | ||
| 362 | 30 | ap_sb_free(&sb); | |
| 363 | 30 | return NULL; | |
| 364 | } | ||
| 365 |
4/4✓ Branch 1 taken 450 times.
✓ Branch 2 taken 204 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 446 times.
|
1104 | if (append_load_parser_cases(&sb, parser) != 0 || |
| 366 | 450 | ap_sb_appendf(&sb, " *) return 1 ;;\n" | |
| 367 | " esac\n" | ||
| 368 | " }\n\n" | ||
| 369 | " __ap_completion_choice_words() {\n" | ||
| 370 | " case \"$1\" in\n") != 0) { | ||
| 371 | 208 | ap_sb_free(&sb); | |
| 372 | 208 | return NULL; | |
| 373 | } | ||
| 374 |
4/4✓ Branch 1 taken 332 times.
✓ Branch 2 taken 114 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 328 times.
|
778 | if (append_choice_cases(&sb, parser) != 0 || |
| 375 | 332 | ap_sb_appendf(&sb, " *) return 1 ;;\n" | |
| 376 | " esac\n" | ||
| 377 | " }\n\n" | ||
| 378 | " __ap_completion_option_value_mode() {\n" | ||
| 379 | " case \"$1\" in\n") != 0) { | ||
| 380 | 118 | ap_sb_free(&sb); | |
| 381 | 118 | return NULL; | |
| 382 | } | ||
| 383 |
4/4✓ Branch 1 taken 284 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 280 times.
|
612 | if (append_value_mode_cases(&sb, parser) != 0 || |
| 384 | 284 | ap_sb_appendf(&sb, " *) return 1 ;;\n" | |
| 385 | " esac\n" | ||
| 386 | " }\n\n" | ||
| 387 | " __ap_completion_option_completion_kind() {\n" | ||
| 388 | " case \"$1\" in\n") != 0) { | ||
| 389 | 48 | ap_sb_free(&sb); | |
| 390 | 48 | return NULL; | |
| 391 | } | ||
| 392 |
4/4✓ Branch 1 taken 236 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 232 times.
|
516 | if (append_completion_kind_cases(&sb, parser) != 0 || |
| 393 | 236 | ap_sb_appendf(&sb, " *) return 1 ;;\n" | |
| 394 | " esac\n" | ||
| 395 | " }\n\n" | ||
| 396 | " __ap_completion_option_value_count() {\n" | ||
| 397 | " case \"$1\" in\n") != 0) { | ||
| 398 | 48 | ap_sb_free(&sb); | |
| 399 | 48 | return NULL; | |
| 400 | } | ||
| 401 |
4/4✓ Branch 1 taken 188 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 184 times.
✓ Branch 4 taken 4 times.
|
420 | if (append_value_count_cases(&sb, parser) != 0 || |
| 402 | 188 | ap_sb_appendf(&sb, " *) return 1 ;;\n" | |
| 403 | " esac\n" | ||
| 404 | " }\n\n" | ||
| 405 | " __ap_completion_has_word() {\n" | ||
| 406 | " case \" $2 \" in\n" | ||
| 407 | " *\" $1 \"*) return 0 ;;\n" | ||
| 408 | " *) return 1 ;;\n" | ||
| 409 | " esac\n" | ||
| 410 | " }\n\n" | ||
| 411 | " __ap_completion_filter_mutex_candidates() {\n" | ||
| 412 | " local parser_name=$1\n" | ||
| 413 | " shift\n" | ||
| 414 | " printf '%%s\\n' \"$@\"\n" | ||
| 415 | " }\n\n" | ||
| 416 | " __ap_completion_dynamic_query() {\n" | ||
| 417 |
2/2✓ Branch 0 taken 154 times.
✓ Branch 1 taken 30 times.
|
184 | " ") != 0 || |
| 418 |
4/4✓ Branch 2 taken 150 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 102 times.
✓ Branch 5 taken 48 times.
|
488 | append_single_quoted(&sb, prog) != 0 || ap_sb_appendf(&sb, " ") != 0 || |
| 419 |
2/2✓ Branch 2 taken 98 times.
✓ Branch 3 taken 4 times.
|
252 | append_single_quoted(&sb, ap_parser_completion_entrypoint(parser)) != 0 || |
| 420 | 102 | ap_sb_appendf( | |
| 421 | &sb, | ||
| 422 | " --shell bash -- \"${COMP_WORDS[@]:1}\"\n" | ||
| 423 | " }\n\n" | ||
| 424 | " __ap_completion_load_parser \"$parser_key\" || return 0\n" | ||
| 425 | " local i=1\n" | ||
| 426 | " while (( i < COMP_CWORD )); do\n" | ||
| 427 | " token=\"${COMP_WORDS[i]}\"\n" | ||
| 428 | " if [[ -n \"$pending_option\" ]]; then\n" | ||
| 429 | " case \"$pending_mode\" in\n" | ||
| 430 | " single)\n" | ||
| 431 | " pending_option=''\n" | ||
| 432 | " pending_mode=''\n" | ||
| 433 | " ;;&\n" | ||
| 434 | " fixed)\n" | ||
| 435 | " pending_fixed_remaining=$((pending_fixed_remaining - 1))\n" | ||
| 436 | " if (( pending_fixed_remaining <= 0 )); then\n" | ||
| 437 | " pending_option=''\n" | ||
| 438 | " pending_mode=''\n" | ||
| 439 | " pending_fixed_remaining=0\n" | ||
| 440 | " fi\n" | ||
| 441 | " ;;&\n" | ||
| 442 | " optional|multi)\n" | ||
| 443 | " if [[ \"$token\" != -* ]]; then\n" | ||
| 444 | " if [[ \"$pending_mode\" == optional ]]; then\n" | ||
| 445 | " pending_option=''\n" | ||
| 446 | " pending_mode=''\n" | ||
| 447 | " fi\n" | ||
| 448 | " else\n" | ||
| 449 | " pending_option=''\n" | ||
| 450 | " pending_mode=''\n" | ||
| 451 | " fi\n" | ||
| 452 | " ;;\n" | ||
| 453 | " esac\n" | ||
| 454 | " fi\n" | ||
| 455 | " if [[ \"$token\" == --*=* ]]; then\n" | ||
| 456 | " option_name=${token%%=*}\n" | ||
| 457 | " if __ap_completion_has_word \"$option_name\" " | ||
| 458 | "\"$parser_value_options\"; then\n" | ||
| 459 | " :\n" | ||
| 460 | " fi\n" | ||
| 461 | " elif [[ \"$token\" == -* ]]; then\n" | ||
| 462 | " if __ap_completion_has_word \"$token\" " | ||
| 463 | "\"$parser_value_options\"; then\n" | ||
| 464 | " pending_option=$token\n" | ||
| 465 | " pending_mode=$(__ap_completion_option_value_mode " | ||
| 466 | "\"$parser_key:$token\")\n" | ||
| 467 | " " | ||
| 468 | "pending_fixed_remaining=$(__ap_completion_option_value_count " | ||
| 469 | "\"$parser_key:$token\")\n" | ||
| 470 | " fi\n" | ||
| 471 | " elif __ap_completion_has_word \"$token\" " | ||
| 472 | "\"$parser_subcommands\"; then\n" | ||
| 473 | " if [[ \"$parser_key\" == root ]]; then\n" | ||
| 474 | " parser_key=\"root/$token\"\n" | ||
| 475 | " else\n" | ||
| 476 | " parser_key=\"$parser_key/$token\"\n" | ||
| 477 | " fi\n" | ||
| 478 | " pending_option=''\n" | ||
| 479 | " pending_mode=''\n" | ||
| 480 | " pending_fixed_remaining=0\n" | ||
| 481 | " __ap_completion_load_parser \"$parser_key\" || return 0\n" | ||
| 482 | " fi\n" | ||
| 483 | " i=$((i + 1))\n" | ||
| 484 |
2/2✓ Branch 0 taken 94 times.
✓ Branch 1 taken 4 times.
|
98 | " done\n\n") != 0 || |
| 485 | 98 | ap_sb_appendf( | |
| 486 | &sb, | ||
| 487 | " if [[ \"$cur\" == --*=* ]]; then\n" | ||
| 488 | " option_name=${cur%%=*}\n" | ||
| 489 | " value_prefix=${cur#*=}\n" | ||
| 490 | " completion_kind=$(__ap_completion_option_completion_kind " | ||
| 491 | "\"$parser_key:$option_name\" 2>/dev/null || printf '%%s' none)\n" | ||
| 492 | " case \"$completion_kind\" in\n" | ||
| 493 | " choices)\n" | ||
| 494 | " if choices=$(__ap_completion_choice_words " | ||
| 495 | "\"$parser_key:$option_name\" 2>/dev/null); then\n" | ||
| 496 | " COMPREPLY=( $(compgen -W \"$choices\" -- " | ||
| 497 | "\"$value_prefix\") )\n" | ||
| 498 | " fi\n" | ||
| 499 | " ;;\n" | ||
| 500 | " dynamic)\n" | ||
| 501 | " mapfile -t COMPREPLY < <(__ap_completion_dynamic_query)\n" | ||
| 502 | " ;;\n" | ||
| 503 | " file)\n" | ||
| 504 | " COMPREPLY=( $(compgen -f -- \"$value_prefix\") )\n" | ||
| 505 | " ;;\n" | ||
| 506 | " directory)\n" | ||
| 507 | " COMPREPLY=( $(compgen -d -- \"$value_prefix\") )\n" | ||
| 508 | " ;;\n" | ||
| 509 | " command)\n" | ||
| 510 | " COMPREPLY=( $(compgen -c -- \"$value_prefix\") )\n" | ||
| 511 | " ;;\n" | ||
| 512 | " esac\n" | ||
| 513 | " if (( ${#COMPREPLY[@]} > 0 )); then\n" | ||
| 514 | " local idx\n" | ||
| 515 | " for idx in \"${!COMPREPLY[@]}\"; do\n" | ||
| 516 | " COMPREPLY[idx]=\"$option_name=${COMPREPLY[idx]}\"\n" | ||
| 517 | " done\n" | ||
| 518 | " return 0\n" | ||
| 519 | " fi\n" | ||
| 520 | " fi\n\n" | ||
| 521 | " if [[ -n \"$pending_option\" ]]; then\n" | ||
| 522 | " completion_kind=$(__ap_completion_option_completion_kind " | ||
| 523 | "\"$parser_key:$pending_option\" 2>/dev/null || printf '%%s' none)\n" | ||
| 524 | " case \"$completion_kind\" in\n" | ||
| 525 | " choices)\n" | ||
| 526 | " if choices=$(__ap_completion_choice_words " | ||
| 527 | "\"$parser_key:$pending_option\" 2>/dev/null); then\n" | ||
| 528 | " COMPREPLY=( $(compgen -W \"$choices\" -- \"$cur\") )\n" | ||
| 529 | " fi\n" | ||
| 530 | " ;;\n" | ||
| 531 | " dynamic)\n" | ||
| 532 | " mapfile -t COMPREPLY < <(__ap_completion_dynamic_query)\n" | ||
| 533 | " ;;\n" | ||
| 534 | " file)\n" | ||
| 535 | " COMPREPLY=( $(compgen -f -- \"$cur\") )\n" | ||
| 536 | " ;;\n" | ||
| 537 | " directory)\n" | ||
| 538 | " COMPREPLY=( $(compgen -d -- \"$cur\") )\n" | ||
| 539 | " ;;\n" | ||
| 540 | " command)\n" | ||
| 541 | " COMPREPLY=( $(compgen -c -- \"$cur\") )\n" | ||
| 542 | " ;;\n" | ||
| 543 | " esac\n" | ||
| 544 | " if (( ${#COMPREPLY[@]} > 0 )); then\n" | ||
| 545 | " return 0\n" | ||
| 546 | " fi\n" | ||
| 547 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 4 times.
|
94 | " fi\n\n") != 0 || |
| 548 | 94 | ap_sb_appendf( | |
| 549 | &sb, | ||
| 550 | " if [[ \"$cur\" == -* ]]; then\n" | ||
| 551 | " filtered=()\n" | ||
| 552 | " while IFS= read -r token; do\n" | ||
| 553 | " [[ -n \"$token\" ]] && filtered+=(\"$token\")\n" | ||
| 554 | " done < <(__ap_completion_filter_mutex_candidates " | ||
| 555 | "\"$parser_key\" $parser_options)\n" | ||
| 556 | " COMPREPLY=( $(compgen -W \"${filtered[*]}\" -- \"$cur\") )\n" | ||
| 557 | " return 0\n" | ||
| 558 | " fi\n\n" | ||
| 559 | " mapfile -t COMPREPLY < <(__ap_completion_dynamic_query)\n" | ||
| 560 | " if (( ${#COMPREPLY[@]} > 0 )); then\n" | ||
| 561 | " return 0\n" | ||
| 562 | " fi\n\n" | ||
| 563 | " candidates=()\n" | ||
| 564 | " while IFS= read -r token; do\n" | ||
| 565 | " [[ -n \"$token\" ]] && candidates+=(\"$token\")\n" | ||
| 566 | " done < <(printf '%%s\\n' $parser_subcommands)\n" | ||
| 567 | " COMPREPLY=( $(compgen -W \"${candidates[*]}\" -- \"$cur\") )\n" | ||
| 568 | " return 0\n" | ||
| 569 | "}\n\n" | ||
| 570 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 22 times.
|
90 | "complete -F _") != 0 || |
| 571 |
4/4✓ Branch 2 taken 64 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 34 times.
✓ Branch 5 taken 30 times.
|
222 | append_identifier(&sb, prog) != 0 || ap_sb_appendf(&sb, " ") != 0 || |
| 572 |
2/2✓ Branch 2 taken 4 times.
✓ Branch 3 taken 30 times.
|
98 | append_single_quoted(&sb, prog) != 0 || ap_sb_appendf(&sb, "\n") != 0) { |
| 573 | 202 | ap_sb_free(&sb); | |
| 574 | 202 | return NULL; | |
| 575 | } | ||
| 576 | |||
| 577 | 30 | return sb.data; | |
| 578 | } | ||
| 579 |