shell_tinyrl.c 14 KB

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