el.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* $NetBSD: el.c,v 1.29 2002/03/18 16:00:52 christos Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Christos Zoulas of Cornell University.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #include "config.h"
  38. #if !defined(lint) && !defined(SCCSID)
  39. #if 0
  40. static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
  41. #else
  42. __RCSID("$NetBSD: el.c,v 1.29 2002/03/18 16:00:52 christos Exp $");
  43. #endif
  44. #endif /* not lint && not SCCSID */
  45. /*
  46. * el.c: EditLine interface functions
  47. */
  48. #include <sys/types.h>
  49. #include <sys/param.h>
  50. #include <string.h>
  51. #include <stdlib.h>
  52. #include <stdarg.h>
  53. #include "el.h"
  54. /* el_init():
  55. * Initialize editline and set default parameters.
  56. */
  57. public EditLine *
  58. el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
  59. {
  60. EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
  61. if (el == NULL)
  62. return (NULL);
  63. memset(el, 0, sizeof(EditLine));
  64. el->el_infd = fileno(fin);
  65. el->el_outfile = fout;
  66. el->el_errfile = ferr;
  67. el->el_prog = strdup(prog);
  68. /*
  69. * Initialize all the modules. Order is important!!!
  70. */
  71. el->el_flags = 0;
  72. if (term_init(el) == -1) {
  73. free(el->el_prog);
  74. el_free(el);
  75. return NULL;
  76. }
  77. (void) key_init(el);
  78. (void) map_init(el);
  79. if (tty_init(el) == -1)
  80. el->el_flags |= NO_TTY;
  81. (void) ch_init(el);
  82. (void) search_init(el);
  83. (void) hist_init(el);
  84. (void) prompt_init(el);
  85. (void) sig_init(el);
  86. (void) read_init(el);
  87. return (el);
  88. }
  89. /* el_end():
  90. * Clean up.
  91. */
  92. public void
  93. el_end(EditLine *el)
  94. {
  95. if (el == NULL)
  96. return;
  97. el_reset(el);
  98. term_end(el);
  99. key_end(el);
  100. map_end(el);
  101. tty_end(el);
  102. ch_end(el);
  103. search_end(el);
  104. hist_end(el);
  105. prompt_end(el);
  106. sig_end(el);
  107. if (el->el_prog)
  108. el_free((ptr_t) el->el_prog);
  109. el_free((ptr_t) el);
  110. }
  111. /* el_reset():
  112. * Reset the tty and the parser
  113. */
  114. public void
  115. el_reset(EditLine *el)
  116. {
  117. tty_cookedmode(el);
  118. ch_reset(el); /* XXX: Do we want that? */
  119. }
  120. /* el_set():
  121. * set the editline parameters
  122. */
  123. public int
  124. el_set(EditLine *el, int op, ...)
  125. {
  126. va_list va;
  127. int rv = 0;
  128. if (el == NULL)
  129. return (-1);
  130. va_start(va, op);
  131. switch (op) {
  132. case EL_PROMPT:
  133. case EL_RPROMPT:
  134. rv = prompt_set(el, va_arg(va, el_pfunc_t), op);
  135. break;
  136. case EL_TERMINAL:
  137. rv = term_set(el, va_arg(va, char *));
  138. break;
  139. case EL_EDITOR:
  140. rv = map_set_editor(el, va_arg(va, char *));
  141. break;
  142. case EL_SIGNAL:
  143. if (va_arg(va, int))
  144. el->el_flags |= HANDLE_SIGNALS;
  145. else
  146. el->el_flags &= ~HANDLE_SIGNALS;
  147. break;
  148. case EL_BIND:
  149. case EL_TELLTC:
  150. case EL_SETTC:
  151. case EL_ECHOTC:
  152. case EL_SETTY:
  153. {
  154. const char *argv[20];
  155. int i;
  156. for (i = 1; i < 20; i++)
  157. if ((argv[i] = va_arg(va, char *)) == NULL)
  158. break;
  159. switch (op) {
  160. case EL_BIND:
  161. argv[0] = "bind";
  162. rv = map_bind(el, i, argv);
  163. break;
  164. case EL_TELLTC:
  165. argv[0] = "telltc";
  166. rv = term_telltc(el, i, argv);
  167. break;
  168. case EL_SETTC:
  169. argv[0] = "settc";
  170. rv = term_settc(el, i, argv);
  171. break;
  172. case EL_ECHOTC:
  173. argv[0] = "echotc";
  174. rv = term_echotc(el, i, argv);
  175. break;
  176. case EL_SETTY:
  177. argv[0] = "setty";
  178. rv = tty_stty(el, i, argv);
  179. break;
  180. default:
  181. rv = -1;
  182. EL_ABORT((el->el_errfile, "Bad op %d\n", op));
  183. break;
  184. }
  185. break;
  186. }
  187. case EL_ADDFN:
  188. {
  189. char *name = va_arg(va, char *);
  190. char *help = va_arg(va, char *);
  191. el_func_t func = va_arg(va, el_func_t);
  192. rv = map_addfunc(el, name, help, func);
  193. break;
  194. }
  195. case EL_HIST:
  196. {
  197. hist_fun_t func = va_arg(va, hist_fun_t);
  198. ptr_t ptr = va_arg(va, char *);
  199. rv = hist_set(el, func, ptr);
  200. break;
  201. }
  202. case EL_EDITMODE:
  203. if (va_arg(va, int))
  204. el->el_flags &= ~EDIT_DISABLED;
  205. else
  206. el->el_flags |= EDIT_DISABLED;
  207. rv = 0;
  208. break;
  209. case EL_GETCFN:
  210. {
  211. el_rfunc_t rc = va_arg(va, el_rfunc_t);
  212. rv = el_read_setfn(el, rc);
  213. break;
  214. }
  215. case EL_CLIENTDATA:
  216. el->el_data = va_arg(va, void *);
  217. break;
  218. default:
  219. rv = -1;
  220. break;
  221. }
  222. va_end(va);
  223. return (rv);
  224. }
  225. /* el_get():
  226. * retrieve the editline parameters
  227. */
  228. public int
  229. el_get(EditLine *el, int op, void *ret)
  230. {
  231. int rv;
  232. if (el == NULL || ret == NULL)
  233. return (-1);
  234. switch (op) {
  235. case EL_PROMPT:
  236. case EL_RPROMPT:
  237. rv = prompt_get(el, (el_pfunc_t *) & ret, op);
  238. break;
  239. case EL_EDITOR:
  240. rv = map_get_editor(el, (const char **) &ret);
  241. break;
  242. case EL_SIGNAL:
  243. *((int *) ret) = (el->el_flags & HANDLE_SIGNALS);
  244. rv = 0;
  245. break;
  246. case EL_EDITMODE:
  247. *((int *) ret) = (!(el->el_flags & EDIT_DISABLED));
  248. rv = 0;
  249. break;
  250. #if 0 /* XXX */
  251. case EL_TERMINAL:
  252. rv = term_get(el, (const char *) &ret);
  253. break;
  254. case EL_BIND:
  255. case EL_TELLTC:
  256. case EL_SETTC:
  257. case EL_ECHOTC:
  258. case EL_SETTY:
  259. {
  260. char *argv[20];
  261. int i;
  262. for (i = 1; i < 20; i++)
  263. if ((argv[i] = va_arg(va, char *)) == NULL)
  264. break;
  265. switch (op) {
  266. case EL_BIND:
  267. argv[0] = "bind";
  268. rv = map_bind(el, i, argv);
  269. break;
  270. case EL_TELLTC:
  271. argv[0] = "telltc";
  272. rv = term_telltc(el, i, argv);
  273. break;
  274. case EL_SETTC:
  275. argv[0] = "settc";
  276. rv = term_settc(el, i, argv);
  277. break;
  278. case EL_ECHOTC:
  279. argv[0] = "echotc";
  280. rv = term_echotc(el, i, argv);
  281. break;
  282. case EL_SETTY:
  283. argv[0] = "setty";
  284. rv = tty_stty(el, i, argv);
  285. break;
  286. default:
  287. rv = -1;
  288. EL_ABORT((el->errfile, "Bad op %d\n", op));
  289. break;
  290. }
  291. break;
  292. }
  293. case EL_ADDFN:
  294. {
  295. char *name = va_arg(va, char *);
  296. char *help = va_arg(va, char *);
  297. el_func_t func = va_arg(va, el_func_t);
  298. rv = map_addfunc(el, name, help, func);
  299. break;
  300. }
  301. case EL_HIST:
  302. {
  303. hist_fun_t func = va_arg(va, hist_fun_t);
  304. ptr_t ptr = va_arg(va, char *);
  305. rv = hist_set(el, func, ptr);
  306. }
  307. break;
  308. #endif /* XXX */
  309. case EL_GETCFN:
  310. *((el_rfunc_t *)ret) = el_read_getfn(el);
  311. rv = 0;
  312. break;
  313. case EL_CLIENTDATA:
  314. *((void **)ret) = el->el_data;
  315. rv = 0;
  316. break;
  317. default:
  318. rv = -1;
  319. }
  320. return (rv);
  321. }
  322. /* el_line():
  323. * Return editing info
  324. */
  325. public const LineInfo *
  326. el_line(EditLine *el)
  327. {
  328. return (const LineInfo *) (void *) &el->el_line;
  329. }
  330. /* el_source():
  331. * Source a file
  332. */
  333. public int
  334. el_source(EditLine *el, const char *fname)
  335. {
  336. FILE *fp;
  337. size_t len;
  338. char *ptr;
  339. fp = NULL;
  340. if (fname == NULL) {
  341. #ifdef HAVE_ISSETUGID
  342. static const char elpath[] = "/.editrc";
  343. char path[MAXPATHLEN];
  344. if (issetugid())
  345. return (-1);
  346. if ((ptr = getenv("HOME")) == NULL)
  347. return (-1);
  348. if (strlcpy(path, ptr, sizeof(path)) >= sizeof(path))
  349. return (-1);
  350. if (strlcat(path, elpath, sizeof(path)) >= sizeof(path))
  351. return (-1);
  352. fname = path;
  353. #else
  354. /*
  355. * If issetugid() is missing, always return an error, in order
  356. * to keep from inadvertently opening up the user to a security
  357. * hole.
  358. */
  359. return (-1);
  360. #endif
  361. }
  362. if (fp == NULL)
  363. fp = fopen(fname, "r");
  364. if (fp == NULL)
  365. return (-1);
  366. while ((ptr = fgetln(fp, &len)) != NULL) {
  367. if (len > 0 && ptr[len - 1] == '\n')
  368. --len;
  369. ptr[len] = '\0';
  370. if (parse_line(el, ptr) == -1) {
  371. (void) fclose(fp);
  372. return (-1);
  373. }
  374. }
  375. (void) fclose(fp);
  376. return (0);
  377. }
  378. /* el_resize():
  379. * Called from program when terminal is resized
  380. */
  381. public void
  382. el_resize(EditLine *el)
  383. {
  384. int lins, cols;
  385. sigset_t oset, nset;
  386. (void) sigemptyset(&nset);
  387. (void) sigaddset(&nset, SIGWINCH);
  388. (void) sigprocmask(SIG_BLOCK, &nset, &oset);
  389. /* get the correct window size */
  390. if (term_get_size(el, &lins, &cols))
  391. term_change_size(el, lins, cols);
  392. (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  393. }
  394. /* el_beep():
  395. * Called from the program to beep
  396. */
  397. public void
  398. el_beep(EditLine *el)
  399. {
  400. term_beep(el);
  401. }
  402. /* el_editmode()
  403. * Set the state of EDIT_DISABLED from the `edit' command.
  404. */
  405. protected int
  406. /*ARGSUSED*/
  407. el_editmode(EditLine *el, int argc, const char **argv)
  408. {
  409. const char *how;
  410. if (argv == NULL || argc != 2 || argv[1] == NULL)
  411. return (-1);
  412. how = argv[1];
  413. if (strcmp(how, "on") == 0)
  414. el->el_flags &= ~EDIT_DISABLED;
  415. else if (strcmp(how, "off") == 0)
  416. el->el_flags |= EDIT_DISABLED;
  417. else {
  418. (void) fprintf(el->el_errfile, "edit: Bad value `%s'.\n", how);
  419. return (-1);
  420. }
  421. return (0);
  422. }