el.c revision 1.23 1 1.23 christos /* $NetBSD: el.c,v 1.23 2001/09/27 19:29:50 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.7 christos #include <sys/cdefs.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.23 christos __RCSID("$NetBSD: el.c,v 1.23 2001/09/27 19:29:50 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.h"
52 1.1 cgd
53 1.1 cgd #include <sys/types.h>
54 1.1 cgd #include <sys/param.h>
55 1.1 cgd #include <string.h>
56 1.1 cgd #include <stdlib.h>
57 1.19 lukem #include <stdarg.h>
58 1.1 cgd #include "el.h"
59 1.1 cgd
60 1.1 cgd /* el_init():
61 1.1 cgd * Initialize editline and set default parameters.
62 1.1 cgd */
63 1.1 cgd public EditLine *
64 1.19 lukem el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
65 1.1 cgd {
66 1.18 chuck
67 1.19 lukem EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
68 1.1 cgd #ifdef DEBUG
69 1.19 lukem char *tty;
70 1.1 cgd #endif
71 1.1 cgd
72 1.19 lukem if (el == NULL)
73 1.19 lukem return (NULL);
74 1.1 cgd
75 1.19 lukem memset(el, 0, sizeof(EditLine));
76 1.1 cgd
77 1.19 lukem el->el_infd = fileno(fin);
78 1.19 lukem el->el_outfile = fout;
79 1.19 lukem el->el_errfile = ferr;
80 1.19 lukem el->el_prog = strdup(prog);
81 1.19 lukem
82 1.19 lukem /*
83 1.19 lukem * Initialize all the modules. Order is important!!!
84 1.19 lukem */
85 1.19 lukem el->el_flags = 0;
86 1.19 lukem
87 1.19 lukem (void) term_init(el);
88 1.19 lukem (void) key_init(el);
89 1.19 lukem (void) map_init(el);
90 1.19 lukem if (tty_init(el) == -1)
91 1.19 lukem el->el_flags |= NO_TTY;
92 1.19 lukem (void) ch_init(el);
93 1.19 lukem (void) search_init(el);
94 1.19 lukem (void) hist_init(el);
95 1.19 lukem (void) prompt_init(el);
96 1.19 lukem (void) sig_init(el);
97 1.23 christos (void) read_init(el);
98 1.1 cgd
99 1.19 lukem return (el);
100 1.19 lukem }
101 1.1 cgd
102 1.1 cgd
103 1.1 cgd /* el_end():
104 1.1 cgd * Clean up.
105 1.1 cgd */
106 1.1 cgd public void
107 1.19 lukem el_end(EditLine *el)
108 1.1 cgd {
109 1.1 cgd
110 1.19 lukem if (el == NULL)
111 1.19 lukem return;
112 1.1 cgd
113 1.19 lukem el_reset(el);
114 1.19 lukem
115 1.19 lukem term_end(el);
116 1.19 lukem key_end(el);
117 1.19 lukem map_end(el);
118 1.19 lukem tty_end(el);
119 1.19 lukem ch_end(el);
120 1.19 lukem search_end(el);
121 1.19 lukem hist_end(el);
122 1.19 lukem prompt_end(el);
123 1.19 lukem sig_end(el);
124 1.19 lukem
125 1.19 lukem el_free((ptr_t) el->el_prog);
126 1.19 lukem el_free((ptr_t) el);
127 1.19 lukem }
128 1.1 cgd
129 1.1 cgd
130 1.1 cgd /* el_reset():
131 1.1 cgd * Reset the tty and the parser
132 1.1 cgd */
133 1.1 cgd public void
134 1.19 lukem el_reset(EditLine *el)
135 1.1 cgd {
136 1.19 lukem
137 1.19 lukem tty_cookedmode(el);
138 1.19 lukem ch_reset(el); /* XXX: Do we want that? */
139 1.1 cgd }
140 1.1 cgd
141 1.1 cgd
142 1.1 cgd /* el_set():
143 1.1 cgd * set the editline parameters
144 1.1 cgd */
145 1.1 cgd public int
146 1.1 cgd el_set(EditLine *el, int op, ...)
147 1.1 cgd {
148 1.19 lukem va_list va;
149 1.19 lukem int rv;
150 1.1 cgd
151 1.19 lukem if (el == NULL)
152 1.19 lukem return (-1);
153 1.22 wiz va_start(va, op);
154 1.22 wiz
155 1.19 lukem switch (op) {
156 1.19 lukem case EL_PROMPT:
157 1.19 lukem case EL_RPROMPT:
158 1.19 lukem rv = prompt_set(el, va_arg(va, el_pfunc_t), op);
159 1.19 lukem break;
160 1.13 simonb
161 1.19 lukem case EL_TERMINAL:
162 1.19 lukem rv = term_set(el, va_arg(va, char *));
163 1.19 lukem break;
164 1.1 cgd
165 1.19 lukem case EL_EDITOR:
166 1.19 lukem rv = map_set_editor(el, va_arg(va, char *));
167 1.1 cgd break;
168 1.1 cgd
169 1.19 lukem case EL_SIGNAL:
170 1.19 lukem if (va_arg(va, int))
171 1.19 lukem el->el_flags |= HANDLE_SIGNALS;
172 1.19 lukem else
173 1.19 lukem el->el_flags &= ~HANDLE_SIGNALS;
174 1.19 lukem rv = 0;
175 1.1 cgd break;
176 1.1 cgd
177 1.19 lukem case EL_BIND:
178 1.19 lukem case EL_TELLTC:
179 1.19 lukem case EL_SETTC:
180 1.19 lukem case EL_ECHOTC:
181 1.19 lukem case EL_SETTY:
182 1.19 lukem {
183 1.19 lukem char *argv[20];
184 1.19 lukem int i;
185 1.19 lukem
186 1.19 lukem for (i = 1; i < 20; i++)
187 1.19 lukem if ((argv[i] = va_arg(va, char *)) == NULL)
188 1.19 lukem break;
189 1.19 lukem
190 1.19 lukem switch (op) {
191 1.19 lukem case EL_BIND:
192 1.19 lukem argv[0] = "bind";
193 1.19 lukem rv = map_bind(el, i, argv);
194 1.19 lukem break;
195 1.19 lukem
196 1.19 lukem case EL_TELLTC:
197 1.19 lukem argv[0] = "telltc";
198 1.19 lukem rv = term_telltc(el, i, argv);
199 1.19 lukem break;
200 1.19 lukem
201 1.19 lukem case EL_SETTC:
202 1.19 lukem argv[0] = "settc";
203 1.19 lukem rv = term_settc(el, i, argv);
204 1.19 lukem break;
205 1.19 lukem
206 1.19 lukem case EL_ECHOTC:
207 1.19 lukem argv[0] = "echotc";
208 1.19 lukem rv = term_echotc(el, i, argv);
209 1.19 lukem break;
210 1.19 lukem
211 1.19 lukem case EL_SETTY:
212 1.19 lukem argv[0] = "setty";
213 1.19 lukem rv = tty_stty(el, i, argv);
214 1.19 lukem break;
215 1.19 lukem
216 1.19 lukem default:
217 1.19 lukem rv = -1;
218 1.20 christos EL_ABORT((el->el_errfile, "Bad op %d\n", op));
219 1.19 lukem break;
220 1.19 lukem }
221 1.1 cgd break;
222 1.19 lukem }
223 1.19 lukem
224 1.19 lukem case EL_ADDFN:
225 1.19 lukem {
226 1.19 lukem char *name = va_arg(va, char *);
227 1.19 lukem char *help = va_arg(va, char *);
228 1.19 lukem el_func_t func = va_arg(va, el_func_t);
229 1.1 cgd
230 1.19 lukem rv = map_addfunc(el, name, help, func);
231 1.1 cgd break;
232 1.19 lukem }
233 1.19 lukem
234 1.19 lukem case EL_HIST:
235 1.19 lukem {
236 1.19 lukem hist_fun_t func = va_arg(va, hist_fun_t);
237 1.19 lukem ptr_t ptr = va_arg(va, char *);
238 1.1 cgd
239 1.19 lukem rv = hist_set(el, func, ptr);
240 1.1 cgd break;
241 1.19 lukem }
242 1.1 cgd
243 1.19 lukem case EL_EDITMODE:
244 1.19 lukem if (va_arg(va, int))
245 1.19 lukem el->el_flags &= ~EDIT_DISABLED;
246 1.19 lukem else
247 1.19 lukem el->el_flags |= EDIT_DISABLED;
248 1.19 lukem rv = 0;
249 1.1 cgd break;
250 1.13 simonb
251 1.23 christos case EL_GETCFN:
252 1.23 christos {
253 1.23 christos el_rfunc_t rc = va_arg(va, el_rfunc_t);
254 1.23 christos rv = el_read_setfn(el, rc);
255 1.23 christos break;
256 1.23 christos }
257 1.23 christos
258 1.19 lukem default:
259 1.19 lukem rv = -1;
260 1.1 cgd }
261 1.1 cgd
262 1.19 lukem va_end(va);
263 1.19 lukem return (rv);
264 1.19 lukem }
265 1.1 cgd
266 1.1 cgd
267 1.10 lukem /* el_get():
268 1.10 lukem * retrieve the editline parameters
269 1.10 lukem */
270 1.10 lukem public int
271 1.19 lukem el_get(EditLine *el, int op, void *ret)
272 1.10 lukem {
273 1.19 lukem int rv;
274 1.10 lukem
275 1.19 lukem if (el == NULL || ret == NULL)
276 1.19 lukem return (-1);
277 1.19 lukem switch (op) {
278 1.19 lukem case EL_PROMPT:
279 1.19 lukem case EL_RPROMPT:
280 1.19 lukem rv = prompt_get(el, (el_pfunc_t *) & ret, op);
281 1.19 lukem break;
282 1.10 lukem
283 1.19 lukem case EL_EDITOR:
284 1.19 lukem rv = map_get_editor(el, (const char **) &ret);
285 1.10 lukem break;
286 1.10 lukem
287 1.19 lukem case EL_SIGNAL:
288 1.19 lukem *((int *) ret) = (el->el_flags & HANDLE_SIGNALS);
289 1.19 lukem rv = 0;
290 1.10 lukem break;
291 1.10 lukem
292 1.19 lukem case EL_EDITMODE:
293 1.19 lukem *((int *) ret) = (!(el->el_flags & EDIT_DISABLED));
294 1.19 lukem rv = 0;
295 1.10 lukem break;
296 1.10 lukem
297 1.19 lukem #if 0 /* XXX */
298 1.19 lukem case EL_TERMINAL:
299 1.19 lukem rv = term_get(el, (const char *) &ret);
300 1.10 lukem break;
301 1.10 lukem
302 1.19 lukem case EL_BIND:
303 1.19 lukem case EL_TELLTC:
304 1.19 lukem case EL_SETTC:
305 1.19 lukem case EL_ECHOTC:
306 1.19 lukem case EL_SETTY:
307 1.19 lukem {
308 1.19 lukem char *argv[20];
309 1.19 lukem int i;
310 1.10 lukem
311 1.19 lukem for (i = 1; i < 20; i++)
312 1.19 lukem if ((argv[i] = va_arg(va, char *)) == NULL)
313 1.19 lukem break;
314 1.19 lukem
315 1.19 lukem switch (op) {
316 1.19 lukem case EL_BIND:
317 1.19 lukem argv[0] = "bind";
318 1.19 lukem rv = map_bind(el, i, argv);
319 1.19 lukem break;
320 1.19 lukem
321 1.19 lukem case EL_TELLTC:
322 1.19 lukem argv[0] = "telltc";
323 1.19 lukem rv = term_telltc(el, i, argv);
324 1.19 lukem break;
325 1.19 lukem
326 1.19 lukem case EL_SETTC:
327 1.19 lukem argv[0] = "settc";
328 1.19 lukem rv = term_settc(el, i, argv);
329 1.19 lukem break;
330 1.19 lukem
331 1.19 lukem case EL_ECHOTC:
332 1.19 lukem argv[0] = "echotc";
333 1.19 lukem rv = term_echotc(el, i, argv);
334 1.19 lukem break;
335 1.19 lukem
336 1.19 lukem case EL_SETTY:
337 1.19 lukem argv[0] = "setty";
338 1.19 lukem rv = tty_stty(el, i, argv);
339 1.19 lukem break;
340 1.19 lukem
341 1.19 lukem default:
342 1.19 lukem rv = -1;
343 1.20 christos EL_ABORT((el->errfile, "Bad op %d\n", op));
344 1.19 lukem break;
345 1.19 lukem }
346 1.10 lukem break;
347 1.10 lukem }
348 1.13 simonb
349 1.19 lukem case EL_ADDFN:
350 1.10 lukem {
351 1.19 lukem char *name = va_arg(va, char *);
352 1.19 lukem char *help = va_arg(va, char *);
353 1.19 lukem el_func_t func = va_arg(va, el_func_t);
354 1.19 lukem
355 1.19 lukem rv = map_addfunc(el, name, help, func);
356 1.19 lukem break;
357 1.10 lukem }
358 1.10 lukem
359 1.19 lukem case EL_HIST:
360 1.19 lukem {
361 1.19 lukem hist_fun_t func = va_arg(va, hist_fun_t);
362 1.19 lukem ptr_t ptr = va_arg(va, char *);
363 1.19 lukem rv = hist_set(el, func, ptr);
364 1.19 lukem }
365 1.19 lukem break;
366 1.19 lukem #endif /* XXX */
367 1.23 christos
368 1.23 christos case EL_GETCFN:
369 1.23 christos *((el_rfunc_t *)ret) = el_read_getfn(el);
370 1.23 christos rv = 0;
371 1.23 christos break;
372 1.19 lukem
373 1.19 lukem default:
374 1.19 lukem rv = -1;
375 1.19 lukem }
376 1.10 lukem
377 1.19 lukem return (rv);
378 1.19 lukem }
379 1.10 lukem
380 1.10 lukem
381 1.1 cgd /* el_line():
382 1.1 cgd * Return editing info
383 1.1 cgd */
384 1.1 cgd public const LineInfo *
385 1.19 lukem el_line(EditLine *el)
386 1.1 cgd {
387 1.19 lukem
388 1.19 lukem return (const LineInfo *) (void *) &el->el_line;
389 1.1 cgd }
390 1.1 cgd
391 1.1 cgd static const char elpath[] = "/.editrc";
392 1.1 cgd
393 1.1 cgd /* el_source():
394 1.1 cgd * Source a file
395 1.1 cgd */
396 1.1 cgd public int
397 1.19 lukem el_source(EditLine *el, const char *fname)
398 1.1 cgd {
399 1.19 lukem FILE *fp;
400 1.19 lukem size_t len;
401 1.19 lukem char *ptr, path[MAXPATHLEN];
402 1.19 lukem
403 1.19 lukem fp = NULL;
404 1.19 lukem if (fname == NULL) {
405 1.19 lukem if (issetugid())
406 1.19 lukem return (-1);
407 1.19 lukem if ((ptr = getenv("HOME")) == NULL)
408 1.19 lukem return (-1);
409 1.19 lukem if (strlcpy(path, ptr, sizeof(path)) >= sizeof(path))
410 1.19 lukem return (-1);
411 1.19 lukem if (strlcat(path, elpath, sizeof(path)) >= sizeof(path))
412 1.19 lukem return (-1);
413 1.19 lukem fname = path;
414 1.19 lukem }
415 1.19 lukem if (fp == NULL)
416 1.19 lukem fp = fopen(fname, "r");
417 1.19 lukem if (fp == NULL)
418 1.19 lukem return (-1);
419 1.19 lukem
420 1.19 lukem while ((ptr = fgetln(fp, &len)) != NULL) {
421 1.19 lukem if (len > 0 && ptr[len - 1] == '\n')
422 1.19 lukem --len;
423 1.19 lukem ptr[len] = '\0';
424 1.19 lukem if (parse_line(el, ptr) == -1) {
425 1.19 lukem (void) fclose(fp);
426 1.19 lukem return (-1);
427 1.19 lukem }
428 1.1 cgd }
429 1.1 cgd
430 1.19 lukem (void) fclose(fp);
431 1.19 lukem return (0);
432 1.1 cgd }
433 1.1 cgd
434 1.1 cgd
435 1.1 cgd /* el_resize():
436 1.1 cgd * Called from program when terminal is resized
437 1.1 cgd */
438 1.1 cgd public void
439 1.19 lukem el_resize(EditLine *el)
440 1.1 cgd {
441 1.19 lukem int lins, cols;
442 1.19 lukem sigset_t oset, nset;
443 1.19 lukem
444 1.19 lukem (void) sigemptyset(&nset);
445 1.19 lukem (void) sigaddset(&nset, SIGWINCH);
446 1.19 lukem (void) sigprocmask(SIG_BLOCK, &nset, &oset);
447 1.19 lukem
448 1.19 lukem /* get the correct window size */
449 1.19 lukem if (term_get_size(el, &lins, &cols))
450 1.19 lukem term_change_size(el, lins, cols);
451 1.1 cgd
452 1.19 lukem (void) sigprocmask(SIG_SETMASK, &oset, NULL);
453 1.9 christos }
454 1.14 lukem
455 1.9 christos
456 1.9 christos /* el_beep():
457 1.9 christos * Called from the program to beep
458 1.9 christos */
459 1.9 christos public void
460 1.19 lukem el_beep(EditLine *el)
461 1.9 christos {
462 1.19 lukem
463 1.19 lukem term_beep(el);
464 1.1 cgd }
465 1.10 lukem
466 1.10 lukem
467 1.10 lukem /* el_editmode()
468 1.10 lukem * Set the state of EDIT_DISABLED from the `edit' command.
469 1.10 lukem */
470 1.10 lukem protected int
471 1.10 lukem /*ARGSUSED*/
472 1.19 lukem el_editmode(EditLine *el, int argc, char **argv)
473 1.10 lukem {
474 1.19 lukem const char *how;
475 1.10 lukem
476 1.19 lukem if (argv == NULL || argc != 2 || argv[1] == NULL)
477 1.19 lukem return (-1);
478 1.10 lukem
479 1.19 lukem how = argv[1];
480 1.19 lukem if (strcmp(how, "on") == 0)
481 1.19 lukem el->el_flags &= ~EDIT_DISABLED;
482 1.19 lukem else if (strcmp(how, "off") == 0)
483 1.19 lukem el->el_flags |= EDIT_DISABLED;
484 1.19 lukem else {
485 1.19 lukem (void) fprintf(el->el_errfile, "edit: Bad value `%s'.\n", how);
486 1.19 lukem return (-1);
487 1.19 lukem }
488 1.19 lukem return (0);
489 1.19 lukem }
490