shell_help.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * shell_help.c
  3. */
  4. #include "private.h"
  5. #include "clish/types.h"
  6. #include "lub/string.h"
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. /*--------------------------------------------------------- */
  11. /*
  12. * Provide a detailed list of the possible command completions
  13. */
  14. static void available_commands(clish_shell_t *this,
  15. clish_help_t *help, const char *line, size_t *max_width)
  16. {
  17. const clish_command_t *cmd;
  18. clish_shell_iterator_t iter;
  19. if (max_width)
  20. *max_width = 0;
  21. /* Search for COMMAND completions */
  22. clish_shell_iterator_init(&iter, CLISH_NSPACE_HELP);
  23. while ((cmd = clish_shell_find_next_completion(this, line, &iter))) {
  24. size_t width;
  25. const char *name = clish_command__get_suffix(cmd);
  26. if (max_width) {
  27. width = strlen(name);
  28. if (width > *max_width)
  29. *max_width = width;
  30. }
  31. lub_argv_add(help->name, name);
  32. lub_argv_add(help->help, clish_command__get_text(cmd));
  33. lub_argv_add(help->detail, clish_command__get_detail(cmd));
  34. }
  35. }
  36. /*--------------------------------------------------------- */
  37. static int available_params(clish_shell_t *this,
  38. clish_help_t *help, const clish_command_t *cmd,
  39. const char *line, size_t *max_width)
  40. {
  41. unsigned index = lub_string_wordcount(line);
  42. unsigned idx = lub_string_wordcount(clish_command__get_name(cmd));
  43. lub_argv_t *argv;
  44. clish_pargv_t *completion, *pargv;
  45. unsigned i;
  46. unsigned cnt = 0;
  47. clish_pargv_status_t status = CLISH_LINE_OK;
  48. clish_context_t context;
  49. /* Empty line */
  50. if (0 == index)
  51. return -1;
  52. if (line[strlen(line) - 1] != ' ')
  53. index--;
  54. argv = lub_argv_new(line, 0);
  55. /* get the parameter definition */
  56. completion = clish_pargv_new();
  57. pargv = clish_pargv_new();
  58. /* Prepare context */
  59. clish_context_init(&context, this);
  60. clish_context__set_cmd(&context, cmd);
  61. clish_context__set_pargv(&context, pargv);
  62. status = clish_shell_parse_pargv(pargv, cmd, &context,
  63. clish_command__get_paramv(cmd),
  64. argv, &idx, completion, index);
  65. clish_pargv_delete(pargv);
  66. cnt = clish_pargv__get_count(completion);
  67. /* Calculate the longest name */
  68. for (i = 0; i < cnt; i++) {
  69. const clish_param_t *param;
  70. const char *name;
  71. unsigned clen = 0;
  72. param = clish_pargv__get_param(completion, i);
  73. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(param))
  74. name = clish_param__get_value(param);
  75. else
  76. name = clish_ptype__get_text(clish_param__get_ptype(param));
  77. if (name)
  78. clen = strlen(name);
  79. if (max_width && (clen > *max_width))
  80. *max_width = clen;
  81. clish_param_help(param, help);
  82. }
  83. clish_pargv_delete(completion);
  84. lub_argv_delete(argv);
  85. /* It's a completed command */
  86. if (CLISH_LINE_OK == status)
  87. return 0;
  88. /* Incompleted command */
  89. return -1;
  90. }
  91. /*--------------------------------------------------------- */
  92. void clish_shell_help(clish_shell_t *this, const char *line)
  93. {
  94. clish_help_t help;
  95. size_t max_width = 0;
  96. const clish_command_t *cmd;
  97. int i;
  98. help.name = lub_argv_new(NULL, 0);
  99. help.help = lub_argv_new(NULL, 0);
  100. help.detail = lub_argv_new(NULL, 0);
  101. /* Get COMMAND completions */
  102. available_commands(this, &help, line, &max_width);
  103. /* Resolve a command */
  104. cmd = clish_shell_resolve_command(this, line);
  105. /* Search for PARAM completion */
  106. if (cmd) {
  107. size_t width = 0;
  108. int status;
  109. status = available_params(this, &help, cmd, line, &width);
  110. if (width > max_width)
  111. max_width = width;
  112. /* Add <cr> if command is completed */
  113. if (!status) {
  114. lub_argv_add(help.name, "<cr>");
  115. lub_argv_add(help.help, NULL);
  116. lub_argv_add(help.detail, NULL);
  117. }
  118. }
  119. if (lub_argv__get_count(help.name) == 0)
  120. goto end;
  121. /* Print help messages */
  122. for (i = 0; i < lub_argv__get_count(help.name); i++) {
  123. fprintf(stderr, " %-*s %s\n", (int)max_width,
  124. lub_argv__get_arg(help.name, i),
  125. lub_argv__get_arg(help.help, i) ?
  126. lub_argv__get_arg(help.help, i) : "");
  127. }
  128. /* Print details */
  129. if ((lub_argv__get_count(help.name) == 1) &&
  130. (SHELL_STATE_HELPING == this->state)) {
  131. const char *detail = lub_argv__get_arg(help.detail, 0);
  132. if (detail)
  133. fprintf(stderr, "%s\n", detail);
  134. }
  135. /* update the state */
  136. if (this->state == SHELL_STATE_HELPING)
  137. this->state = SHELL_STATE_OK;
  138. else
  139. this->state = SHELL_STATE_HELPING;
  140. end:
  141. lub_argv_delete(help.name);
  142. lub_argv_delete(help.help);
  143. lub_argv_delete(help.detail);
  144. }
  145. /*--------------------------------------------------------- */