command.c (15856B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2017-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <stdio.h> 29 #include <string.h> 30 #include <getopt.h> 31 #include <ell/ell.h> 32 #include <readline/readline.h> 33 34 #include "client/command.h" 35 #include "client/display.h" 36 37 static struct l_queue *command_families; 38 static struct l_queue *command_options; 39 static int exit_status; 40 static bool interactive_mode; 41 static struct command_noninteractive { 42 char **argv; 43 int argc; 44 } command_noninteractive; 45 46 struct command_option { 47 const char *name; 48 char *value; 49 }; 50 51 static void command_options_destroy(void *data) 52 { 53 struct command_option *option = data; 54 55 l_free(option->value); 56 l_free(option); 57 } 58 59 bool command_option_get(const char *name, const char **value_out) 60 { 61 const struct l_queue_entry *entry; 62 63 for (entry = l_queue_get_entries(command_options); entry; 64 entry = entry->next) { 65 const struct command_option *option = entry->data; 66 67 if (strcmp(option->name, name)) 68 continue; 69 70 if (value_out) 71 *value_out = option->value; 72 73 return true; 74 } 75 76 return false; 77 } 78 79 bool command_needs_no_agent(void) 80 { 81 return command_option_get(COMMAND_OPTION_DONTASK, NULL) && 82 (l_queue_length(command_options) == 1); 83 } 84 85 static enum cmd_status cmd_version(const char *entity, 86 char **argv, int argc) 87 { 88 display("IWD version %s\n", VERSION); 89 90 return CMD_STATUS_DONE; 91 } 92 93 static enum cmd_status cmd_quit(const char *entity, 94 char **argv, int argc) 95 { 96 display_quit(); 97 98 l_main_quit(); 99 100 return CMD_STATUS_DONE; 101 } 102 103 static const struct command misc_commands[] = { 104 { NULL, "version", NULL, cmd_version, "Display version" }, 105 { NULL, "quit", NULL, cmd_quit, "Quit program" }, 106 { NULL, "exit", NULL, cmd_quit }, 107 { NULL, "help" }, 108 { } 109 }; 110 111 static char *cmd_generator(const char *text, int state) 112 { 113 static const struct l_queue_entry *entry; 114 static size_t index; 115 static size_t len; 116 const char *cmd; 117 118 if (!state) { 119 len = strlen(text); 120 index = 0; 121 entry = l_queue_get_entries(command_families); 122 } 123 124 while (entry) { 125 const struct command_family *family = entry->data; 126 127 entry = entry->next; 128 129 if (strncmp(family->name, text, len)) 130 continue; 131 132 return l_strdup(family->name); 133 } 134 135 while ((cmd = misc_commands[index].cmd)) { 136 index++; 137 138 if (strncmp(cmd, text, len)) 139 continue; 140 141 return l_strdup(cmd); 142 } 143 144 return NULL; 145 } 146 147 static bool cmd_completion_cmd_has_arg(const char *cmd, 148 const struct command_family *family) 149 { 150 size_t i; 151 char **matches = NULL; 152 bool status; 153 154 for (i = 0; family->command_list[i].cmd; i++) { 155 if (strcmp(family->command_list[i].cmd, cmd)) 156 continue; 157 158 return family->command_list[i].arg ? true : false; 159 } 160 161 if (!family->family_arg_completion) 162 return false; 163 164 matches = rl_completion_matches(cmd, family->family_arg_completion); 165 if (!matches) 166 return false; 167 168 status = false; 169 170 for (i = 0; matches[i]; i++) { 171 if (strcmp(matches[i], cmd)) 172 continue; 173 174 status = true; 175 176 break; 177 } 178 179 l_strfreev(matches); 180 181 return status; 182 } 183 184 static bool find_next_token(int *i, const char *token, int token_len) 185 { 186 char *line = rl_line_buffer; 187 188 while (*i && line[*i] == ' ') 189 (*i)--; 190 191 while (*i && line[*i] != ' ') 192 (*i)--; 193 194 return !strncmp(line + (line[*i] == ' ' ? *i + 1 : *i), 195 token, token_len); 196 } 197 198 bool command_line_find_token(const char *token, uint8_t num_to_inspect) 199 { 200 int i = rl_point - 1; 201 int len = strlen(token); 202 203 if (!len) 204 return false; 205 206 while (i && num_to_inspect) { 207 if (find_next_token(&i, token, len)) 208 return true; 209 210 num_to_inspect--; 211 } 212 213 return false; 214 } 215 216 /* 217 * Work around readline limitations of not being able to pass a context pointer 218 * to match functions. Set the command match function/entity to these globals 219 * and call a generic match function which can call the _real_ match function 220 * and include the entity. 221 */ 222 static command_completion_func_t cmd_current_completion_func = NULL; 223 static const char *cmd_current_entity = NULL; 224 225 static char *cmd_completion_generic(const char *text, int state) 226 { 227 return cmd_current_completion_func(text, state, cmd_current_entity); 228 } 229 230 static char **cmd_completion_match_entity_cmd(const char *cmd, const char *text, 231 const struct command *cmd_list) 232 { 233 char **matches = NULL; 234 size_t i; 235 char *family = NULL; 236 char *entity = NULL; 237 char *prompt = NULL; 238 239 for (i = 0; cmd_list[i].cmd; i++) { 240 char *tmp; 241 242 if (strcmp(cmd_list[i].cmd, cmd)) 243 continue; 244 245 if (!cmd_list[i].completion) 246 break; 247 248 if (cmd_list[i].entity) { 249 prompt = rl_copy_text(0, rl_end); 250 251 family = strtok_r(prompt, " ", &tmp); 252 if (!family) 253 goto done; 254 255 entity = strtok_r(NULL, " ", &tmp); 256 } 257 258 done: 259 cmd_current_completion_func = cmd_list[i].completion; 260 cmd_current_entity = entity; 261 262 matches = rl_completion_matches(text, cmd_completion_generic); 263 264 l_free(prompt); 265 cmd_current_completion_func = NULL; 266 cmd_current_entity = NULL; 267 268 break; 269 } 270 271 return matches; 272 } 273 274 static char **cmd_completion_match_family_cmd(const char *cmd_family, 275 char *args, const char *text, 276 bool ends_with_space) 277 { 278 const struct l_queue_entry *entry; 279 const char *arg1; 280 const char *arg2; 281 const char *arg3; 282 char **matches = NULL; 283 284 for (entry = l_queue_get_entries(command_families); entry; 285 entry = entry->next) { 286 const struct command_family *family = entry->data; 287 288 if (strcmp(family->name, cmd_family)) 289 continue; 290 291 arg1 = strtok_r(NULL, " ", &args); 292 if (!arg1) { 293 if (!family->family_arg_completion) 294 break; 295 296 matches = rl_completion_matches(text, 297 family->family_arg_completion); 298 299 break; 300 } 301 302 arg2 = strtok_r(NULL, " ", &args); 303 if (!arg2 && !ends_with_space) { 304 if (!family->family_arg_completion) 305 break; 306 307 matches = rl_completion_matches(text, 308 family->family_arg_completion); 309 break; 310 } else if (!arg2 && ends_with_space) { 311 if (!cmd_completion_cmd_has_arg(arg1, family)) 312 break; 313 314 if (!family->entity_arg_completion) 315 break; 316 317 matches = rl_completion_matches(text, 318 family->entity_arg_completion); 319 break; 320 } 321 322 arg3 = strtok_r(NULL, " ", &args); 323 if (!arg3 && !ends_with_space) { 324 if (!family->entity_arg_completion) 325 break; 326 327 matches = rl_completion_matches(text, 328 family->entity_arg_completion); 329 break; 330 } 331 332 matches = cmd_completion_match_entity_cmd(arg2, text, 333 family->command_list); 334 335 break; 336 } 337 338 return matches; 339 } 340 341 char **command_completion(const char *text, int start, int end) 342 { 343 char **matches = NULL; 344 const char *family; 345 char *args = NULL; 346 char *prompt = NULL; 347 bool ends_with_space = false; 348 349 if (display_agent_is_active()) { 350 rl_attempted_completion_over = 1; 351 return NULL; 352 } 353 354 if (!start) { 355 matches = rl_completion_matches(text, cmd_generator); 356 357 goto done; 358 } 359 360 prompt = rl_copy_text(0, rl_end); 361 362 family = strtok_r(prompt, " ", &args); 363 if (!family) 364 goto done; 365 366 if (args) { 367 int len = strlen(args); 368 369 if (len > 0 && args[len - 1] == ' ') 370 ends_with_space = true; 371 } 372 373 matches = cmd_completion_match_family_cmd(family, args, text, 374 ends_with_space); 375 376 done: 377 l_free(prompt); 378 379 if (!matches) 380 rl_attempted_completion_over = 1; 381 382 return matches; 383 } 384 385 char *command_entity_arg_completion(const char *text, int state, 386 const struct command *command_list) 387 { 388 static size_t index; 389 static size_t len; 390 const char *cmd; 391 392 if (!state) { 393 index = 0; 394 len = strlen(text); 395 } 396 397 while ((cmd = command_list[index].cmd)) { 398 if (!command_list[index++].entity) 399 continue; 400 401 if (strncmp(cmd, text, len)) 402 continue; 403 404 return l_strdup(cmd); 405 } 406 407 return NULL; 408 } 409 410 static void execute_cmd(const char *family, const char *entity, 411 const struct command *cmd, 412 char **argv, int argc) 413 { 414 enum cmd_status status; 415 416 display_refresh_set_cmd(family, entity, cmd, argv, argc); 417 418 status = cmd->function(entity, argv, argc); 419 420 if (status != CMD_STATUS_TRIGGERED && status != CMD_STATUS_DONE) 421 goto error; 422 423 if (status == CMD_STATUS_DONE && !interactive_mode) { 424 l_main_quit(); 425 426 return; 427 } 428 429 if (!interactive_mode) 430 return; 431 432 if (cmd->refreshable) 433 display_refresh_timeout_set(); 434 435 return; 436 437 error: 438 switch (status) { 439 case CMD_STATUS_INVALID_ARGS: 440 display("Invalid command. Use the following pattern:\n"); 441 display_command_line(family, cmd); 442 break; 443 444 case CMD_STATUS_INVALID_VALUE: 445 break; 446 447 case CMD_STATUS_UNSUPPORTED: 448 display_refresh_reset(); 449 450 display("Unsupported command\n"); 451 break; 452 453 case CMD_STATUS_FAILED: 454 goto failure; 455 456 case CMD_STATUS_TRIGGERED: 457 case CMD_STATUS_DONE: 458 l_error("Unknown command status."); 459 break; 460 } 461 462 if (interactive_mode) 463 return; 464 465 failure: 466 exit_status = EXIT_FAILURE; 467 468 l_main_quit(); 469 } 470 471 static bool match_cmd(const char *family, const char *param, 472 char **argv, int argc, 473 const struct command *command_list) 474 { 475 size_t i; 476 477 for (i = 0; command_list[i].cmd; i++) { 478 const char *entity; 479 const char *cmd; 480 int offset; 481 482 if (command_list[i].entity) { 483 if (argc < 1) 484 continue; 485 486 entity = param; 487 cmd = argv[0]; 488 offset = 1; 489 } else { 490 entity = NULL; 491 cmd = param; 492 offset = 0; 493 } 494 495 if (strcmp(command_list[i].cmd, cmd)) 496 continue; 497 498 if (!command_list[i].function) 499 return false; 500 501 execute_cmd(family, entity, &command_list[i], 502 argv + offset, argc - offset); 503 504 return true; 505 } 506 507 return false; 508 } 509 510 static bool match_cmd_family(char **argv, int argc) 511 { 512 const struct l_queue_entry *entry; 513 514 if (argc < 2) 515 return false; 516 517 for (entry = l_queue_get_entries(command_families); entry; 518 entry = entry->next) { 519 const struct command_family *family = entry->data; 520 521 if (strcmp(family->name, argv[0])) 522 continue; 523 524 return match_cmd(family->name, argv[1], argv + 2, argc - 2, 525 family->command_list); 526 } 527 528 return false; 529 } 530 531 static void list_commands(const char *command_family, 532 const struct command *cmd_list) 533 { 534 size_t i; 535 536 for (i = 0; cmd_list[i].cmd; i++) { 537 if (!cmd_list[i].desc) 538 continue; 539 540 display_command_line(command_family, &cmd_list[i]); 541 } 542 } 543 544 static void list_cmd_options(void) 545 { 546 display(MARGIN "--%-*s %s\n", 48, COMMAND_OPTION_USERNAME, 547 "Provide username"); 548 display(MARGIN "--%-*s %s\n", 48, COMMAND_OPTION_PASSWORD, 549 "Provide password"); 550 display(MARGIN "--%-*s %s\n", 48, COMMAND_OPTION_PASSPHRASE, 551 "Provide passphrase"); 552 display(MARGIN "--%-*s %s\n", 48, COMMAND_OPTION_DONTASK, 553 "Don't ask for missing\n" 554 "\t\t\t\t\t\t credentials"); 555 display(MARGIN "--%-*s %s\n", 48, "help", "Display help"); 556 } 557 558 static void list_cmd_families(void) 559 { 560 const struct l_queue_entry *entry; 561 562 for (entry = l_queue_get_entries(command_families); entry; 563 entry = entry->next) { 564 const struct command_family *family = entry->data; 565 566 display("\n%s:\n", family->caption); 567 list_commands(family->name, family->command_list); 568 } 569 } 570 571 static void command_display_help(void) 572 { 573 display("\n"); 574 display_table_header("iwctl version " VERSION, MARGIN "%-*s", 575 5, "Usage"); 576 display(MARGIN "iwctl [--options] [commands]\n"); 577 display_table_footer(); 578 579 display_table_header("Available options", MARGIN "%-*s %-*s", 580 50, "Options", 28, "Description"); 581 list_cmd_options(); 582 display_table_footer(); 583 584 display_table_header("Available commands", MARGIN "%-*s %-*s", 585 50, "Commands", 28, "Description"); 586 list_cmd_families(); 587 display_table_footer(); 588 589 if (!interactive_mode) 590 return; 591 592 display("\nMiscellaneous:\n"); 593 594 list_commands(NULL, misc_commands); 595 } 596 597 static bool command_match_misc_commands(char **argv, int argc) 598 { 599 if (match_cmd(NULL, argv[0], argv + 1, argc - 1, misc_commands)) 600 return true; 601 602 if (strcmp(argv[0], "help")) 603 return false; 604 605 command_display_help(); 606 607 return true; 608 } 609 610 void command_process_prompt(char **argv, int argc) 611 { 612 if (argc == 0) 613 return; 614 615 if (match_cmd_family(argv, argc)) 616 return; 617 618 if (!interactive_mode) { 619 if (command_match_misc_commands(argv, argc)) { 620 exit_status = EXIT_SUCCESS; 621 goto quit; 622 } 623 624 display_error("Invalid command\n"); 625 exit_status = EXIT_FAILURE; 626 quit: 627 l_main_quit(); 628 return; 629 } 630 631 display_refresh_reset(); 632 633 if (command_match_misc_commands(argv, argc)) 634 return; 635 636 display_error("Invalid command\n"); 637 } 638 639 void command_noninteractive_trigger(void) 640 { 641 if (!command_noninteractive.argc) 642 return; 643 644 command_process_prompt(command_noninteractive.argv, 645 command_noninteractive.argc); 646 } 647 648 bool command_is_interactive_mode(void) 649 { 650 return interactive_mode; 651 } 652 653 void command_set_exit_status(int status) 654 { 655 exit_status = status; 656 } 657 658 int command_get_exit_status(void) 659 { 660 return exit_status; 661 } 662 663 void command_family_register(const struct command_family *family) 664 { 665 l_queue_push_tail(command_families, (void *) family); 666 } 667 668 void command_family_unregister(const struct command_family *family) 669 { 670 l_queue_remove(command_families, (void *) family); 671 } 672 673 static const struct option command_opts[] = { 674 { COMMAND_OPTION_USERNAME, required_argument, NULL, 'u' }, 675 { COMMAND_OPTION_PASSWORD, required_argument, NULL, 'p' }, 676 { COMMAND_OPTION_PASSPHRASE, required_argument, NULL, 'P' }, 677 { COMMAND_OPTION_DONTASK, no_argument, NULL, 'd' }, 678 { "help", no_argument, NULL, 'h' }, 679 { } 680 }; 681 682 extern struct command_family_desc __start___command[]; 683 extern struct command_family_desc __stop___command[]; 684 685 bool command_init(char **argv, int argc) 686 { 687 struct command_family_desc *desc; 688 int opt; 689 690 command_families = l_queue_new(); 691 command_options = l_queue_new(); 692 693 for (desc = __start___command; desc < __stop___command; desc++) { 694 if (!desc->init) 695 continue; 696 697 desc->init(); 698 } 699 700 for (;;) { 701 struct command_option *option; 702 703 opt = getopt_long(argc, argv, "u:p:P:dh", command_opts, NULL); 704 705 switch (opt) { 706 case 'u': 707 option = l_new(struct command_option, 1); 708 option->name = COMMAND_OPTION_USERNAME; 709 option->value = l_strdup(optarg); 710 711 l_queue_push_tail(command_options, option); 712 713 break; 714 case 'p': 715 option = l_new(struct command_option, 1); 716 option->name = COMMAND_OPTION_PASSWORD; 717 option->value = l_strdup(optarg); 718 719 l_queue_push_tail(command_options, option); 720 721 break; 722 case 'P': 723 option = l_new(struct command_option, 1); 724 option->name = COMMAND_OPTION_PASSPHRASE; 725 option->value = l_strdup(optarg); 726 727 l_queue_push_tail(command_options, option); 728 729 break; 730 case 'd': 731 option = l_new(struct command_option, 1); 732 option->name = COMMAND_OPTION_DONTASK; 733 734 l_queue_push_tail(command_options, option); 735 736 break; 737 case 'h': 738 command_display_help(); 739 740 l_main_quit(); 741 742 return true; 743 case -1: 744 goto options_parsed; 745 case '?': 746 exit_status = EXIT_FAILURE; 747 748 return true; 749 } 750 } 751 752 options_parsed: 753 argv += optind; 754 argc -= optind; 755 756 if (argc < 1) { 757 interactive_mode = true; 758 return false; 759 } 760 761 command_noninteractive.argv = argv; 762 command_noninteractive.argc = argc; 763 764 return false; 765 } 766 767 void command_exit(void) 768 { 769 struct command_family_desc *desc; 770 771 for (desc = __start___command; desc < __stop___command; desc++) { 772 if (!desc->exit) 773 continue; 774 775 desc->exit(); 776 } 777 778 l_queue_destroy(command_families, NULL); 779 command_families = NULL; 780 781 l_queue_destroy(command_options, command_options_destroy); 782 command_options = NULL; 783 }