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