shell_help.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_argv_wordcount(line);
  42. unsigned idx = lub_argv_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. context.shell = this;
  59. context.cmd = cmd;
  60. context.pargv = pargv;
  61. status = clish_shell_parse_pargv(pargv, cmd, &context,
  62. clish_command__get_paramv(cmd),
  63. argv, &idx, completion, index);
  64. clish_pargv_delete(pargv);
  65. cnt = clish_pargv__get_count(completion);
  66. /* Calculate the longest name */
  67. for (i = 0; i < cnt; i++) {
  68. const clish_param_t *param;
  69. const char *name;
  70. unsigned clen = 0;
  71. param = clish_pargv__get_param(completion, i);
  72. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(param))
  73. name = clish_param__get_value(param);
  74. else
  75. name = clish_ptype__get_text(clish_param__get_ptype(param));
  76. if (name)
  77. clen = strlen(name);
  78. if (max_width && (clen > *max_width))
  79. *max_width = clen;
  80. clish_param_help(param, help);
  81. }
  82. clish_pargv_delete(completion);
  83. lub_argv_delete(argv);
  84. /* It's a completed command */
  85. if (CLISH_LINE_OK == status)
  86. return 0;
  87. /* Incompleted command */
  88. return -1;
  89. }
  90. /*--------------------------------------------------------- */
  91. void clish_shell_help(clish_shell_t *this, const char *line)
  92. {
  93. clish_help_t help;
  94. size_t max_width = 0;
  95. const clish_command_t *cmd;
  96. int i;
  97. help.name = lub_argv_new(NULL, 0);
  98. help.help = lub_argv_new(NULL, 0);
  99. help.detail = lub_argv_new(NULL, 0);
  100. /* Get COMMAND completions */
  101. available_commands(this, &help, line, &max_width);
  102. /* Resolve a command */
  103. cmd = clish_shell_resolve_command(this, line);
  104. /* Search for PARAM completion */
  105. if (cmd) {
  106. size_t width = 0;
  107. int status;
  108. status = available_params(this, &help, cmd, line, &width);
  109. if (width > max_width)
  110. max_width = width;
  111. /* Add <cr> if command is completed */
  112. if (!status) {
  113. lub_argv_add(help.name, "<cr>");
  114. lub_argv_add(help.help, NULL);
  115. lub_argv_add(help.detail, NULL);
  116. }
  117. }
  118. if (lub_argv__get_count(help.name) == 0)
  119. goto end;
  120. /* Print help messages */
  121. for (i = 0; i < lub_argv__get_count(help.name); i++) {
  122. fprintf(stderr, " %-*s %s\n", (int)max_width,
  123. lub_argv__get_arg(help.name, i),
  124. lub_argv__get_arg(help.help, i) ?
  125. lub_argv__get_arg(help.help, i) : "");
  126. }
  127. /* Print details */
  128. if ((lub_argv__get_count(help.name) == 1) &&
  129. (SHELL_STATE_HELPING == this->state)) {
  130. const char *detail = lub_argv__get_arg(help.detail, 0);
  131. if (detail)
  132. fprintf(stderr, "%s\n", detail);
  133. }
  134. /* update the state */
  135. if (this->state == SHELL_STATE_HELPING)
  136. this->state = SHELL_STATE_OK;
  137. else
  138. this->state = SHELL_STATE_HELPING;
  139. end:
  140. lub_argv_delete(help.name);
  141. lub_argv_delete(help.help);
  142. lub_argv_delete(help.detail);
  143. }
  144. /*--------------------------------------------------------- */