shell_tinyrl.c 13 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 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. arg_status = clish_shell_parse(context->shell,
  152. line, &cmd, &pargv);
  153. if (pargv)
  154. clish_pargv_delete(pargv);
  155. switch (arg_status) {
  156. case CLISH_LINE_OK:
  157. case CLISH_LINE_PARTIAL:
  158. if (' ' != line[strlen(line) - 1])
  159. result = BOOL_TRUE;
  160. break;
  161. default:
  162. /* perform word completion */
  163. status = clish_shell_tinyrl_complete(this);
  164. switch (status) {
  165. case TINYRL_NO_MATCH:
  166. case TINYRL_AMBIGUOUS:
  167. /* ambiguous result signal an issue */
  168. break;
  169. case TINYRL_COMPLETED_AMBIGUOUS:
  170. /* perform word completion again in case we just did case
  171. modification the first time */
  172. status = clish_shell_tinyrl_complete(this);
  173. if (status == TINYRL_MATCH_WITH_EXTENSIONS) {
  174. /* all is well with the world just enter a space */
  175. result = BOOL_TRUE;
  176. }
  177. break;
  178. case TINYRL_MATCH:
  179. case TINYRL_MATCH_WITH_EXTENSIONS:
  180. case TINYRL_COMPLETED_MATCH:
  181. /* all is well with the world just enter a space */
  182. result = BOOL_TRUE;
  183. break;
  184. }
  185. break;
  186. }
  187. }
  188. if (result)
  189. result = tinyrl_insert_text(this, " ");
  190. /* keep compiler happy */
  191. key = key;
  192. return result;
  193. }
  194. /*-------------------------------------------------------- */
  195. static bool_t clish_shell_tinyrl_key_enter(tinyrl_t * this, int key)
  196. {
  197. context_t *context = tinyrl__get_context(this);
  198. const clish_command_t *cmd = NULL;
  199. const char *line = tinyrl__get_line(this);
  200. bool_t result = BOOL_FALSE;
  201. if (*line) {
  202. /* try and parse the command */
  203. cmd = clish_shell_resolve_command(context->shell, line);
  204. if (NULL == cmd) {
  205. tinyrl_match_e status =
  206. clish_shell_tinyrl_complete(this);
  207. switch (status) {
  208. case TINYRL_NO_MATCH:
  209. case TINYRL_AMBIGUOUS:
  210. case TINYRL_COMPLETED_AMBIGUOUS:
  211. {
  212. /* failed to get a unique match... */
  213. break;
  214. }
  215. case TINYRL_MATCH:
  216. case TINYRL_MATCH_WITH_EXTENSIONS:
  217. case TINYRL_COMPLETED_MATCH:
  218. {
  219. /* re-fetch the line as it may have changed
  220. * due to auto-completion
  221. */
  222. line = tinyrl__get_line(this);
  223. /* get the command to parse? */
  224. cmd =
  225. clish_shell_resolve_command
  226. (context->shell, line);
  227. if (NULL == cmd) {
  228. /*
  229. * We have had a match but it is not a command
  230. * so add a space so as not to confuse the user
  231. */
  232. result =
  233. tinyrl_insert_text(this,
  234. " ");
  235. }
  236. break;
  237. }
  238. }
  239. }
  240. if (NULL != cmd) {
  241. clish_pargv_status_t arg_status;
  242. tinyrl_crlf(this);
  243. /* we've got a command so check the syntax */
  244. arg_status = clish_shell_parse(context->shell,
  245. line,
  246. &context->command,
  247. &context->pargv);
  248. switch (arg_status) {
  249. case CLISH_LINE_OK:
  250. tinyrl_done(this);
  251. result = BOOL_TRUE;
  252. break;
  253. case CLISH_BAD_HISTORY:
  254. fprintf(stderr, "Error: Bad history entry.\n");
  255. break;
  256. case CLISH_BAD_CMD:
  257. fprintf(stderr, "Error: Illegal command line.\n");
  258. break;
  259. case CLISH_BAD_PARAM:
  260. fprintf(stderr, "Error: Illegal parameter.\n");
  261. break;
  262. case CLISH_LINE_PARTIAL:
  263. fprintf(stderr, "Error: The command is not completed.\n");
  264. break;
  265. }
  266. if (CLISH_LINE_OK != arg_status)
  267. tinyrl_reset_line_state(this);
  268. }
  269. } else {
  270. /* nothing to pass simply move down the screen */
  271. tinyrl_crlf(this);
  272. tinyrl_reset_line_state(this);
  273. result = BOOL_TRUE;
  274. }
  275. if ((BOOL_FALSE == result) && (BOOL_FALSE == tinyrl__get_isatty(this))) {
  276. /* we've found an error in a script */
  277. context->shell->state = SHELL_STATE_SCRIPT_ERROR;
  278. }
  279. /* keep the compiler happy */
  280. key = key;
  281. return result;
  282. }
  283. /*-------------------------------------------------------- */
  284. /* This is the completion function provided for CLISH */
  285. static tinyrl_completion_func_t clish_shell_tinyrl_completion;
  286. static char **clish_shell_tinyrl_completion(tinyrl_t * this,
  287. const char *line,
  288. unsigned start, unsigned end)
  289. {
  290. char **matches;
  291. /* don't bother to resort to filename completion */
  292. tinyrl_completion_over(this);
  293. /* perform the matching */
  294. matches = tinyrl_completion(this,
  295. line,
  296. start,
  297. end, clish_shell_tinyrl_word_generator);
  298. return matches;
  299. }
  300. /*-------------------------------------------------------- */
  301. static void clish_shell_tinyrl_init(tinyrl_t * this)
  302. {
  303. bool_t status;
  304. /* bind the '?' key to the help function */
  305. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  306. assert(BOOL_TRUE == status);
  307. /* bind the <RET> key to the help function */
  308. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  309. assert(BOOL_TRUE == status);
  310. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  311. assert(BOOL_TRUE == status);
  312. /* bind the <SPACE> key to auto-complete if necessary */
  313. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  314. assert(BOOL_TRUE == status);
  315. }
  316. /*-------------------------------------------------------- */
  317. /*
  318. * Create an instance of the specialised class
  319. */
  320. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  321. FILE * ostream, unsigned stifle)
  322. {
  323. /* call the parent constructor */
  324. tinyrl_t *this = tinyrl_new(istream,
  325. ostream,
  326. stifle,
  327. clish_shell_tinyrl_completion);
  328. if (NULL != this) {
  329. /* now call our own constructor */
  330. clish_shell_tinyrl_init(this);
  331. }
  332. return this;
  333. }
  334. /*-------------------------------------------------------- */
  335. void clish_shell_tinyrl_fini(tinyrl_t * this)
  336. {
  337. /* nothing to do... yet */
  338. this = this;
  339. }
  340. /*-------------------------------------------------------- */
  341. void clish_shell_tinyrl_delete(tinyrl_t * this)
  342. {
  343. /* call our destructor */
  344. clish_shell_tinyrl_fini(this);
  345. /* and call the parent destructor */
  346. tinyrl_delete(this);
  347. }
  348. /*-------------------------------------------------------- */
  349. bool_t clish_shell_line(clish_shell_t * this, const char *prompt,
  350. const clish_command_t ** cmd, clish_pargv_t ** pargv, const char *str)
  351. {
  352. char *line = NULL;
  353. bool_t result = BOOL_FALSE;
  354. context_t context;
  355. /* set up the context */
  356. context.command = NULL;
  357. context.pargv = NULL;
  358. context.shell = this;
  359. if (SHELL_STATE_CLOSING != this->state) {
  360. this->state = SHELL_STATE_READY;
  361. if (str)
  362. line = tinyrl_forceline(this->tinyrl, prompt, &context, str);
  363. else
  364. line = tinyrl_readline(this->tinyrl, prompt, &context);
  365. if (NULL != line) {
  366. tinyrl_history_t *history =
  367. tinyrl__get_history(this->tinyrl);
  368. if (tinyrl__get_isatty(this->tinyrl)) {
  369. /* deal with the history list */
  370. tinyrl_history_add(history, line);
  371. }
  372. if (this->client_hooks->cmd_line_fn) {
  373. /* now let the client know the command line has been entered */
  374. this->client_hooks->cmd_line_fn(this, line);
  375. }
  376. free(line);
  377. result = BOOL_TRUE;
  378. *cmd = context.command;
  379. *pargv = context.pargv;
  380. }
  381. }
  382. return result;
  383. }
  384. /*-------------------------------------------------------- */
  385. bool_t clish_shell_execline(clish_shell_t *this, const char *line, char ** out)
  386. {
  387. const clish_command_t *cmd;
  388. char *prompt = NULL;
  389. clish_pargv_t *pargv = NULL;
  390. const clish_view_t *view;
  391. bool_t running = BOOL_TRUE;
  392. assert(this);
  393. if (!line && !tinyrl__get_istream(this->tinyrl))
  394. return BOOL_FALSE;
  395. /* obtain the prompt */
  396. view = clish_shell__get_view(this);
  397. assert(view);
  398. prompt = clish_view__get_prompt(view,
  399. clish_shell__get_viewid(this));
  400. assert(prompt);
  401. if (line) {
  402. /* push the specified line */
  403. running = clish_shell_line(this, prompt,
  404. &cmd, &pargv, line);
  405. } else {
  406. running = clish_shell_line(this, prompt,
  407. &cmd, &pargv, NULL);
  408. }
  409. lub_string_free(prompt);
  410. /* execute the provided command */
  411. if (running && cmd && pargv) {
  412. if (BOOL_FALSE == clish_shell_execute(this, cmd, pargv, out)) {
  413. if((!this->current_file && line) ||
  414. (this->current_file &&
  415. this->current_file->stop_on_error)) {
  416. this->state = SHELL_STATE_SCRIPT_ERROR;
  417. }
  418. }
  419. }
  420. if (NULL != pargv)
  421. clish_pargv_delete(pargv);
  422. return running;
  423. }
  424. /*-------------------------------------------------------- */
  425. bool_t clish_shell_forceline(clish_shell_t *this, const char *line, char ** out)
  426. {
  427. return clish_shell_execline(this, line, out);
  428. }
  429. /*-------------------------------------------------------- */
  430. bool_t clish_shell_readline(clish_shell_t *this, char ** out)
  431. {
  432. return clish_shell_execline(this, NULL, out);
  433. }
  434. /*-------------------------------------------------------- */
  435. FILE * clish_shell__get_istream(const clish_shell_t * this)
  436. {
  437. return tinyrl__get_istream(this->tinyrl);
  438. }
  439. /*-------------------------------------------------------- */
  440. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  441. {
  442. return tinyrl__get_ostream(this->tinyrl);
  443. }
  444. /*-------------------------------------------------------- */
  445. void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
  446. {
  447. assert(this);
  448. this->interactive = interactive;
  449. }
  450. /*-------------------------------------------------------- */
  451. bool_t clish_shell__get_interactive(const clish_shell_t * this)
  452. {
  453. assert(this);
  454. return this->interactive;
  455. }
  456. /*-------------------------------------------------------- */
  457. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  458. {
  459. assert(this);
  460. return tinyrl__get_utf8(this->tinyrl);
  461. }
  462. /*-------------------------------------------------------- */
  463. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  464. {
  465. assert(this);
  466. tinyrl__set_utf8(this->tinyrl, utf8);
  467. }
  468. /*-------------------------------------------------------- */