shell_tinyrl.c 13 KB

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