shell_tinyrl.c 14 KB

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