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