1
0

shell_tinyrl.c 12 KB

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