shell_tinyrl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. case CLISH_BAD_CMD:
  255. case CLISH_BAD_PARAM:
  256. case CLISH_LINE_PARTIAL:
  257. tinyrl_crlf(this);
  258. fprintf(stderr, "Error. Illegal command line.\n");
  259. tinyrl_crlf(this);
  260. tinyrl_reset_line_state(this);
  261. break;
  262. }
  263. }
  264. } else {
  265. /* nothing to pass simply move down the screen */
  266. tinyrl_crlf(this);
  267. tinyrl_reset_line_state(this);
  268. result = BOOL_TRUE;
  269. }
  270. if ((BOOL_FALSE == result) && (BOOL_FALSE == tinyrl__get_isatty(this))) {
  271. /* we've found an error in a script */
  272. context->shell->state = SHELL_STATE_SCRIPT_ERROR;
  273. }
  274. /* keep the compiler happy */
  275. key = key;
  276. return result;
  277. }
  278. /*-------------------------------------------------------- */
  279. /* This is the completion function provided for CLISH */
  280. static tinyrl_completion_func_t clish_shell_tinyrl_completion;
  281. static char **clish_shell_tinyrl_completion(tinyrl_t * this,
  282. const char *line,
  283. unsigned start, unsigned end)
  284. {
  285. char **matches;
  286. /* don't bother to resort to filename completion */
  287. tinyrl_completion_over(this);
  288. /* perform the matching */
  289. matches = tinyrl_completion(this,
  290. line,
  291. start,
  292. end, clish_shell_tinyrl_word_generator);
  293. return matches;
  294. }
  295. /*-------------------------------------------------------- */
  296. static void clish_shell_tinyrl_init(tinyrl_t * this)
  297. {
  298. bool_t status;
  299. /* bind the '?' key to the help function */
  300. status = tinyrl_bind_key(this, '?', clish_shell_tinyrl_key_help);
  301. assert(BOOL_TRUE == status);
  302. /* bind the <RET> key to the help function */
  303. status = tinyrl_bind_key(this, '\r', clish_shell_tinyrl_key_enter);
  304. assert(BOOL_TRUE == status);
  305. status = tinyrl_bind_key(this, '\n', clish_shell_tinyrl_key_enter);
  306. assert(BOOL_TRUE == status);
  307. /* bind the <SPACE> key to auto-complete if necessary */
  308. status = tinyrl_bind_key(this, ' ', clish_shell_tinyrl_key_space);
  309. assert(BOOL_TRUE == status);
  310. }
  311. /*-------------------------------------------------------- */
  312. /*
  313. * Create an instance of the specialised class
  314. */
  315. tinyrl_t *clish_shell_tinyrl_new(FILE * istream,
  316. FILE * ostream, unsigned stifle)
  317. {
  318. /* call the parent constructor */
  319. tinyrl_t *this = tinyrl_new(istream,
  320. ostream,
  321. stifle,
  322. clish_shell_tinyrl_completion);
  323. if (NULL != this) {
  324. /* now call our own constructor */
  325. clish_shell_tinyrl_init(this);
  326. }
  327. return this;
  328. }
  329. /*-------------------------------------------------------- */
  330. void clish_shell_tinyrl_fini(tinyrl_t * this)
  331. {
  332. /* nothing to do... yet */
  333. this = this;
  334. }
  335. /*-------------------------------------------------------- */
  336. void clish_shell_tinyrl_delete(tinyrl_t * this)
  337. {
  338. /* call our destructor */
  339. clish_shell_tinyrl_fini(this);
  340. /* and call the parent destructor */
  341. tinyrl_delete(this);
  342. }
  343. /*-------------------------------------------------------- */
  344. bool_t clish_shell_line(clish_shell_t * this, const char *prompt,
  345. const clish_command_t ** cmd, clish_pargv_t ** pargv, const char *str)
  346. {
  347. char *line = NULL;
  348. bool_t result = BOOL_FALSE;
  349. context_t context;
  350. /* set up the context */
  351. context.command = NULL;
  352. context.pargv = NULL;
  353. context.shell = this;
  354. if (SHELL_STATE_CLOSING != this->state) {
  355. this->state = SHELL_STATE_READY;
  356. if (str)
  357. line = tinyrl_forceline(this->tinyrl, prompt, &context, str);
  358. else
  359. line = tinyrl_readline(this->tinyrl, prompt, &context);
  360. if (NULL != line) {
  361. tinyrl_history_t *history =
  362. tinyrl__get_history(this->tinyrl);
  363. if (tinyrl__get_isatty(this->tinyrl)) {
  364. /* deal with the history list */
  365. tinyrl_history_add(history, line);
  366. }
  367. if (this->client_hooks->cmd_line_fn) {
  368. /* now let the client know the command line has been entered */
  369. this->client_hooks->cmd_line_fn(this, line);
  370. }
  371. free(line);
  372. result = BOOL_TRUE;
  373. *cmd = context.command;
  374. *pargv = context.pargv;
  375. }
  376. }
  377. return result;
  378. }
  379. /*-------------------------------------------------------- */
  380. bool_t clish_shell_execline(clish_shell_t *this, const char *line, char ** out)
  381. {
  382. const clish_command_t *cmd;
  383. char *prompt = NULL;
  384. clish_pargv_t *pargv = NULL;
  385. const clish_view_t *view;
  386. bool_t running = BOOL_TRUE;
  387. assert(this);
  388. if (!line && !tinyrl__get_istream(this->tinyrl))
  389. return BOOL_FALSE;
  390. /* obtain the prompt */
  391. view = clish_shell__get_view(this);
  392. assert(view);
  393. prompt = clish_view__get_prompt(view,
  394. clish_shell__get_viewid(this));
  395. assert(prompt);
  396. if (line) {
  397. /* push the specified line */
  398. running = clish_shell_line(this, prompt,
  399. &cmd, &pargv, line);
  400. } else {
  401. running = clish_shell_line(this, prompt,
  402. &cmd, &pargv, NULL);
  403. }
  404. lub_string_free(prompt);
  405. /* execute the provided command */
  406. if (running && cmd && pargv) {
  407. if (BOOL_FALSE == clish_shell_execute(this, cmd, pargv, out)) {
  408. if((!this->current_file && line) ||
  409. (this->current_file &&
  410. this->current_file->stop_on_error)) {
  411. this->state = SHELL_STATE_SCRIPT_ERROR;
  412. }
  413. }
  414. }
  415. if (NULL != pargv)
  416. clish_pargv_delete(pargv);
  417. return running;
  418. }
  419. /*-------------------------------------------------------- */
  420. bool_t clish_shell_forceline(clish_shell_t *this, const char *line, char ** out)
  421. {
  422. return clish_shell_execline(this, line, out);
  423. }
  424. /*-------------------------------------------------------- */
  425. bool_t clish_shell_readline(clish_shell_t *this, char ** out)
  426. {
  427. return clish_shell_execline(this, NULL, out);
  428. }
  429. /*-------------------------------------------------------- */
  430. FILE * clish_shell__get_istream(const clish_shell_t * this)
  431. {
  432. return tinyrl__get_istream(this->tinyrl);
  433. }
  434. /*-------------------------------------------------------- */
  435. FILE * clish_shell__get_ostream(const clish_shell_t * this)
  436. {
  437. return tinyrl__get_ostream(this->tinyrl);
  438. }
  439. /*-------------------------------------------------------- */
  440. void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
  441. {
  442. assert(this);
  443. this->interactive = interactive;
  444. }
  445. /*-------------------------------------------------------- */
  446. bool_t clish_shell__get_interactive(const clish_shell_t * this)
  447. {
  448. assert(this);
  449. return this->interactive;
  450. }
  451. /*-------------------------------------------------------- */
  452. bool_t clish_shell__get_utf8(const clish_shell_t * this)
  453. {
  454. assert(this);
  455. return tinyrl__get_utf8(this->tinyrl);
  456. }
  457. /*-------------------------------------------------------- */
  458. void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
  459. {
  460. assert(this);
  461. tinyrl__set_utf8(this->tinyrl, utf8);
  462. }
  463. /*-------------------------------------------------------- */