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