shell_tinyrl.c 14 KB

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