shell_tinyrl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * shell_tinyrl.c
  3. *
  4. * This is a specialisation of the tinyrl_t class which maps the readline
  5. * functionality to the CLISH environment.
  6. */
  7. #include "private.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <assert.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <ctype.h>
  14. #include "tinyrl/tinyrl.h"
  15. #include "tinyrl/history.h"
  16. #include "lub/string.h"
  17. /*-------------------------------------------------------- */
  18. static bool_t clish_shell_tinyrl_key_help(tinyrl_t * this, int key)
  19. {
  20. bool_t result = BOOL_TRUE;
  21. if (BOOL_TRUE == tinyrl_is_quoting(this)) {
  22. /* if we are in the middle of a quote then simply enter a space */
  23. result = tinyrl_insert_text(this, "?");
  24. } else {
  25. /* get the context */
  26. clish_context_t *context = tinyrl__get_context(this);
  27. tinyrl_crlf(this);
  28. clish_shell_help(context->shell, tinyrl__get_line(this));
  29. tinyrl_crlf(this);
  30. tinyrl_reset_line_state(this);
  31. }
  32. /* keep the compiler happy */
  33. key = key;
  34. return result;
  35. }
  36. /*lint +e818 */
  37. /*-------------------------------------------------------- */
  38. /*
  39. * Expand the current line with any history substitutions
  40. */
  41. static clish_pargv_status_t clish_shell_tinyrl_expand(tinyrl_t * this)
  42. {
  43. clish_pargv_status_t status = CLISH_LINE_OK;
  44. int rtn;
  45. char *buffer;
  46. /* first of all perform any history substitutions */
  47. rtn = tinyrl_history_expand(tinyrl__get_history(this),
  48. tinyrl__get_line(this), &buffer);
  49. switch (rtn) {
  50. case -1:
  51. /* error in expansion */
  52. status = CLISH_BAD_HISTORY;
  53. break;
  54. case 0:
  55. /*no expansion */
  56. break;
  57. case 1:
  58. /* expansion occured correctly */
  59. tinyrl_replace_line(this, buffer, 1);
  60. break;
  61. case 2:
  62. /* just display line */
  63. fprintf(tinyrl__get_ostream(this), "\n%s", buffer);
  64. free(buffer);
  65. buffer = NULL;
  66. break;
  67. default:
  68. break;
  69. }
  70. free(buffer);
  71. return status;
  72. }
  73. /*-------------------------------------------------------- */
  74. /*
  75. * This is a CLISH specific completion function.
  76. * If the current prefix is not a recognised prefix then
  77. * an error is flagged.
  78. * If it is a recognisable prefix then possible completions are displayed
  79. * or a unique completion is inserted.
  80. */
  81. static tinyrl_match_e clish_shell_tinyrl_complete(tinyrl_t * this)
  82. {
  83. /* context_t *context = tinyrl__get_context(this); */
  84. tinyrl_match_e status;
  85. /* first of all perform any history expansion */
  86. (void)clish_shell_tinyrl_expand(this);
  87. /* perform normal completion */
  88. status = tinyrl_complete(this);
  89. switch (status) {
  90. case TINYRL_NO_MATCH:
  91. if (BOOL_FALSE == tinyrl_is_completion_error_over(this)) {
  92. /* The user hasn't even entered a valid prefix!!! */
  93. /* tinyrl_crlf(this);
  94. clish_shell_help(context->shell,
  95. tinyrl__get_line(this));
  96. tinyrl_crlf(this);
  97. tinyrl_reset_line_state(this);
  98. */ }
  99. break;
  100. default:
  101. /* the default completion function will have prompted for completions as
  102. * necessary
  103. */
  104. break;
  105. }
  106. return status;
  107. }
  108. /*--------------------------------------------------------- */
  109. static bool_t clish_shell_tinyrl_key_space(tinyrl_t * this, int key)
  110. {
  111. bool_t result = BOOL_FALSE;
  112. tinyrl_match_e status;
  113. clish_context_t *context = tinyrl__get_context(this);
  114. const char *line = tinyrl__get_line(this);
  115. clish_pargv_status_t arg_status;
  116. const clish_command_t *cmd = NULL;
  117. clish_pargv_t *pargv = NULL;
  118. if (BOOL_TRUE == tinyrl_is_quoting(this)) {
  119. /* if we are in the middle of a quote then simply enter a space */
  120. result = BOOL_TRUE;
  121. } else {
  122. /* Find out if current line is legal. It can be
  123. * fully completed or partially completed.
  124. */
  125. arg_status = clish_shell_parse(context->shell,
  126. line, &cmd, &pargv);
  127. if (pargv)
  128. clish_pargv_delete(pargv);
  129. switch (arg_status) {
  130. case CLISH_LINE_OK:
  131. case CLISH_LINE_PARTIAL:
  132. if (' ' != line[strlen(line) - 1])
  133. result = BOOL_TRUE;
  134. break;
  135. default:
  136. break;
  137. }
  138. /* If current line is illegal try to make auto-comletion. */
  139. if (!result) {
  140. /* perform word completion */
  141. status = clish_shell_tinyrl_complete(this);
  142. switch (status) {
  143. case TINYRL_NO_MATCH:
  144. case TINYRL_AMBIGUOUS:
  145. /* ambiguous result signal an issue */
  146. break;
  147. case TINYRL_COMPLETED_AMBIGUOUS:
  148. /* perform word completion again in case we just did case
  149. modification the first time */
  150. status = clish_shell_tinyrl_complete(this);
  151. if (status == TINYRL_MATCH_WITH_EXTENSIONS) {
  152. /* all is well with the world just enter a space */
  153. result = BOOL_TRUE;
  154. }
  155. break;
  156. case TINYRL_MATCH:
  157. case TINYRL_MATCH_WITH_EXTENSIONS:
  158. case TINYRL_COMPLETED_MATCH:
  159. /* all is well with the world just enter a space */
  160. result = BOOL_TRUE;
  161. break;
  162. }
  163. }
  164. }
  165. if (result)
  166. result = tinyrl_insert_text(this, " ");
  167. /* keep compiler happy */
  168. key = key;
  169. return result;
  170. }
  171. /*-------------------------------------------------------- */
  172. static bool_t clish_shell_tinyrl_key_enter(tinyrl_t * this, int key)
  173. {
  174. clish_context_t *context = tinyrl__get_context(this);
  175. const clish_command_t *cmd = NULL;
  176. const char *line = tinyrl__get_line(this);
  177. bool_t result = BOOL_FALSE;
  178. /* nothing to pass simply move down the screen */
  179. if (!*line) {
  180. tinyrl_crlf(this);
  181. tinyrl_reset_line_state(this);
  182. return BOOL_TRUE;
  183. }
  184. /* try and parse the command */
  185. cmd = clish_shell_resolve_command(context->shell, line);
  186. if (!cmd) {
  187. tinyrl_match_e status = clish_shell_tinyrl_complete(this);
  188. switch (status) {
  189. case TINYRL_MATCH:
  190. case TINYRL_MATCH_WITH_EXTENSIONS:
  191. case TINYRL_COMPLETED_MATCH:
  192. /* re-fetch the line as it may have changed
  193. * due to auto-completion
  194. */
  195. line = tinyrl__get_line(this);
  196. /* get the command to parse? */
  197. cmd = clish_shell_resolve_command(context->shell, line);
  198. /*
  199. * We have had a match but it is not a command
  200. * so add a space so as not to confuse the user
  201. */
  202. if (!cmd)
  203. result = tinyrl_insert_text(this, " ");
  204. break;
  205. default:
  206. /* failed to get a unique match... */
  207. break;
  208. }
  209. }
  210. if (cmd) {
  211. clish_pargv_status_t arg_status;
  212. tinyrl_crlf(this);
  213. /* we've got a command so check the syntax */
  214. arg_status = clish_shell_parse(context->shell,
  215. line, &context->cmd, &context->pargv);
  216. switch (arg_status) {
  217. case CLISH_LINE_OK:
  218. tinyrl_done(this);
  219. result = BOOL_TRUE;
  220. break;
  221. case CLISH_BAD_HISTORY:
  222. fprintf(stderr, "Error: Bad history entry.\n");
  223. break;
  224. case CLISH_BAD_CMD:
  225. fprintf(stderr, "Error: Illegal command line.\n");
  226. break;
  227. case CLISH_BAD_PARAM:
  228. fprintf(stderr, "Error: Illegal parameter.\n");
  229. break;
  230. case CLISH_LINE_PARTIAL:
  231. fprintf(stderr, "Error: The command is not completed.\n");
  232. break;
  233. }
  234. if (CLISH_LINE_OK != arg_status)
  235. tinyrl_reset_line_state(this);
  236. }
  237. /* keep the compiler happy */
  238. key = key;
  239. return result;
  240. }
  241. /*-------------------------------------------------------- */
  242. /* This is the completion function provided for CLISH */
  243. tinyrl_completion_func_t clish_shell_tinyrl_completion;
  244. char **clish_shell_tinyrl_completion(tinyrl_t * tinyrl,
  245. const char *line, unsigned start, unsigned end)
  246. {
  247. lub_argv_t *matches = lub_argv_new(NULL, 0);
  248. clish_context_t *context = tinyrl__get_context(tinyrl);
  249. clish_shell_t *this = context->shell;
  250. clish_shell_iterator_t iter;
  251. const clish_command_t *cmd = NULL;
  252. char *text = lub_string_dupn(line, end);
  253. char **result = NULL;
  254. /* Don't bother to resort to filename completion */
  255. tinyrl_completion_over(tinyrl);
  256. /* Search for COMMAND completions */
  257. clish_shell_iterator_init(&iter, CLISH_NSPACE_COMPLETION);
  258. while ((cmd = clish_shell_find_next_completion(this, text, &iter)))
  259. lub_argv_add(matches, clish_command__get_suffix(cmd));
  260. /* Try and resolve a command */
  261. cmd = clish_shell_resolve_command(this, text);
  262. /* Search for PARAM completion */
  263. if (cmd)
  264. clish_shell_param_generator(this, matches, cmd, text, start);
  265. lub_string_free(text);
  266. /* Matches were found */
  267. if (lub_argv__get_count(matches) > 0) {
  268. unsigned i;
  269. char *subst = lub_string_dup(lub_argv__get_arg(matches, 0));
  270. /* Find out substitution */
  271. for (i = 1; i < lub_argv__get_count(matches); i++) {
  272. char *p = subst;
  273. const char *match = lub_argv__get_arg(matches, i);
  274. size_t match_len = strlen(p);
  275. /* identify the common prefix */
  276. while ((tolower(*p) == tolower(*match)) && match_len--) {
  277. p++;
  278. match++;
  279. }
  280. /* Terminate the prefix string */
  281. *p = '\0';
  282. }
  283. result = lub_argv__get_argv(matches, subst);
  284. lub_string_free(subst);
  285. }
  286. lub_argv_delete(matches);
  287. return result;
  288. }
  289. /*-------------------------------------------------------- */
  290. static void clish_shell_tinyrl_init(tinyrl_t * this)
  291. {
  292. bool_t status;
  293. /* bind the '?' key to the help function */
  294. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  295. assert(status);
  296. /* bind the <RET> key to the help function */
  297. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  298. assert(status);
  299. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  300. assert(status);
  301. /* bind the <SPACE> key to auto-complete if necessary */
  302. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  303. assert(status);
  304. }
  305. /*-------------------------------------------------------- */
  306. /*
  307. * Create an instance of the specialised class
  308. */
  309. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  310. FILE * ostream, unsigned stifle)
  311. {
  312. /* call the parent constructor */
  313. tinyrl_t *this = tinyrl_new(istream,
  314. ostream, stifle, clish_shell_tinyrl_completion);
  315. /* now call our own constructor */
  316. if (this)
  317. clish_shell_tinyrl_init(this);
  318. return this;
  319. }
  320. /*-------------------------------------------------------- */
  321. void clish_shell_tinyrl_fini(tinyrl_t * this)
  322. {
  323. /* nothing to do... yet */
  324. this = this;
  325. }
  326. /*-------------------------------------------------------- */
  327. void clish_shell_tinyrl_delete(tinyrl_t * this)
  328. {
  329. /* call our destructor */
  330. clish_shell_tinyrl_fini(this);
  331. /* and call the parent destructor */
  332. tinyrl_delete(this);
  333. }
  334. /*-------------------------------------------------------- */
  335. bool_t clish_shell_execline(clish_shell_t *this, const char *line, char **out)
  336. {
  337. char *prompt = NULL;
  338. const clish_view_t *view;
  339. char *str;
  340. clish_context_t context;
  341. tinyrl_history_t *history;
  342. int lerror = 0;
  343. assert(this);
  344. this->state = SHELL_STATE_OK;
  345. if (!line && !tinyrl__get_istream(this->tinyrl)) {
  346. this->state = SHELL_STATE_SYSTEM_ERROR;
  347. return BOOL_FALSE;
  348. }
  349. /* Set up the context for tinyrl */
  350. context.cmd = NULL;
  351. context.pargv = NULL;
  352. context.shell = this;
  353. /* Obtain the prompt */
  354. view = clish_shell__get_view(this);
  355. assert(view);
  356. prompt = clish_view__get_prompt(view, &context);
  357. assert(prompt);
  358. /* Push the specified line or interactive line */
  359. if (line)
  360. str = tinyrl_forceline(this->tinyrl, prompt, &context, line);
  361. else
  362. str = tinyrl_readline(this->tinyrl, prompt, &context);
  363. lerror = errno;
  364. lub_string_free(prompt);
  365. if (!str) {
  366. switch (lerror) {
  367. case ENODATA:
  368. this->state = SHELL_STATE_EOF;
  369. break;
  370. case EBADMSG:
  371. this->state = SHELL_STATE_SYNTAX_ERROR;
  372. break;
  373. default:
  374. this->state = SHELL_STATE_SYSTEM_ERROR;
  375. break;
  376. };
  377. return BOOL_FALSE;
  378. }
  379. /* Deal with the history list */
  380. if (tinyrl__get_isatty(this->tinyrl)) {
  381. history = tinyrl__get_history(this->tinyrl);
  382. tinyrl_history_add(history, str);
  383. }
  384. /* Let the client know the command line has been entered */
  385. if (this->client_hooks->cmd_line_fn)
  386. this->client_hooks->cmd_line_fn(this, str);
  387. free(str);
  388. /* Execute the provided command */
  389. if (context.cmd && context.pargv) {
  390. if (!clish_shell_execute(&context, out)) {
  391. this->state = SHELL_STATE_SCRIPT_ERROR;
  392. if (context.pargv)
  393. clish_pargv_delete(context.pargv);
  394. return BOOL_FALSE;
  395. }
  396. }
  397. if (context.pargv)
  398. clish_pargv_delete(context.pargv);
  399. return BOOL_TRUE;
  400. }
  401. /*-------------------------------------------------------- */
  402. bool_t clish_shell_forceline(clish_shell_t *this, const char *line, char ** out)
  403. {
  404. return clish_shell_execline(this, line, out);
  405. }
  406. /*-------------------------------------------------------- */
  407. bool_t clish_shell_readline(clish_shell_t *this, char ** out)
  408. {
  409. return clish_shell_execline(this, NULL, out);
  410. }
  411. /*-------------------------------------------------------- */
  412. FILE * clish_shell__get_istream(const clish_shell_t * this)
  413. {
  414. return tinyrl__get_istream(this->tinyrl);
  415. }
  416. /*-------------------------------------------------------- */
  417. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  418. {
  419. return tinyrl__get_ostream(this->tinyrl);
  420. }
  421. /*-------------------------------------------------------- */
  422. void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
  423. {
  424. assert(this);
  425. this->interactive = interactive;
  426. }
  427. /*-------------------------------------------------------- */
  428. bool_t clish_shell__get_interactive(const clish_shell_t * this)
  429. {
  430. assert(this);
  431. return this->interactive;
  432. }
  433. /*-------------------------------------------------------- */
  434. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  435. {
  436. assert(this);
  437. return tinyrl__get_utf8(this->tinyrl);
  438. }
  439. /*-------------------------------------------------------- */
  440. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  441. {
  442. assert(this);
  443. tinyrl__set_utf8(this->tinyrl, utf8);
  444. }
  445. /*--------------------------------------------------------- */
  446. tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * this)
  447. {
  448. return this->tinyrl;
  449. }
  450. /*-------------------------------------------------------- */