el.c revision 1.97 1 1.97 christos /* $NetBSD: el.c,v 1.97 2018/11/18 17:09:39 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.97 christos __RCSID("$NetBSD: el.c,v 1.97 2018/11/18 17:09:39 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.82 christos #include <ctype.h>
50 1.84 christos #include <langinfo.h>
51 1.84 christos #include <locale.h>
52 1.82 christos #include <stdarg.h>
53 1.82 christos #include <stdlib.h>
54 1.1 cgd #include <string.h>
55 1.75 christos
56 1.1 cgd #include "el.h"
57 1.81 christos #include "parse.h"
58 1.89 christos #include "read.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.88 christos EditLine *
64 1.19 lukem el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
65 1.1 cgd {
66 1.72 christos return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
67 1.72 christos fileno(ferr));
68 1.72 christos }
69 1.72 christos
70 1.95 christos libedit_private EditLine *
71 1.95 christos el_init_internal(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
72 1.95 christos int fdin, int fdout, int fderr, int flags)
73 1.72 christos {
74 1.67 christos EditLine *el = el_malloc(sizeof(*el));
75 1.1 cgd
76 1.19 lukem if (el == NULL)
77 1.68 christos return NULL;
78 1.1 cgd
79 1.19 lukem memset(el, 0, sizeof(EditLine));
80 1.1 cgd
81 1.44 christos el->el_infile = fin;
82 1.19 lukem el->el_outfile = fout;
83 1.19 lukem el->el_errfile = ferr;
84 1.44 christos
85 1.72 christos el->el_infd = fdin;
86 1.72 christos el->el_outfd = fdout;
87 1.72 christos el->el_errfd = fderr;
88 1.44 christos
89 1.86 christos el->el_prog = wcsdup(ct_decode_string(prog, &el->el_scratch));
90 1.56 christos if (el->el_prog == NULL) {
91 1.36 christos el_free(el);
92 1.36 christos return NULL;
93 1.36 christos }
94 1.19 lukem
95 1.19 lukem /*
96 1.19 lukem * Initialize all the modules. Order is important!!!
97 1.19 lukem */
98 1.95 christos el->el_flags = flags;
99 1.19 lukem
100 1.64 christos if (terminal_init(el) == -1) {
101 1.36 christos el_free(el->el_prog);
102 1.25 christos el_free(el);
103 1.25 christos return NULL;
104 1.25 christos }
105 1.66 christos (void) keymacro_init(el);
106 1.19 lukem (void) map_init(el);
107 1.19 lukem if (tty_init(el) == -1)
108 1.19 lukem el->el_flags |= NO_TTY;
109 1.19 lukem (void) ch_init(el);
110 1.19 lukem (void) search_init(el);
111 1.19 lukem (void) hist_init(el);
112 1.19 lukem (void) prompt_init(el);
113 1.19 lukem (void) sig_init(el);
114 1.94 christos (void) literal_init(el);
115 1.89 christos if (read_init(el) == -1) {
116 1.89 christos el_end(el);
117 1.89 christos return NULL;
118 1.89 christos }
119 1.68 christos return el;
120 1.19 lukem }
121 1.1 cgd
122 1.95 christos EditLine *
123 1.95 christos el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
124 1.95 christos int fdin, int fdout, int fderr)
125 1.95 christos {
126 1.95 christos return el_init_internal(prog, fin, fout, ferr, fdin, fdout, fderr, 0);
127 1.95 christos }
128 1.1 cgd
129 1.1 cgd /* el_end():
130 1.1 cgd * Clean up.
131 1.1 cgd */
132 1.88 christos void
133 1.19 lukem el_end(EditLine *el)
134 1.1 cgd {
135 1.1 cgd
136 1.19 lukem if (el == NULL)
137 1.19 lukem return;
138 1.1 cgd
139 1.19 lukem el_reset(el);
140 1.19 lukem
141 1.64 christos terminal_end(el);
142 1.66 christos keymacro_end(el);
143 1.19 lukem map_end(el);
144 1.74 christos if (!(el->el_flags & NO_TTY))
145 1.96 christos tty_end(el, TCSAFLUSH);
146 1.19 lukem ch_end(el);
147 1.92 christos read_end(el->el_read);
148 1.19 lukem search_end(el);
149 1.19 lukem hist_end(el);
150 1.19 lukem prompt_end(el);
151 1.19 lukem sig_end(el);
152 1.94 christos literal_end(el);
153 1.19 lukem
154 1.67 christos el_free(el->el_prog);
155 1.90 christos el_free(el->el_visual.cbuff);
156 1.90 christos el_free(el->el_visual.wbuff);
157 1.67 christos el_free(el->el_scratch.cbuff);
158 1.67 christos el_free(el->el_scratch.wbuff);
159 1.67 christos el_free(el->el_lgcyconv.cbuff);
160 1.67 christos el_free(el->el_lgcyconv.wbuff);
161 1.67 christos el_free(el);
162 1.19 lukem }
163 1.1 cgd
164 1.1 cgd
165 1.1 cgd /* el_reset():
166 1.1 cgd * Reset the tty and the parser
167 1.1 cgd */
168 1.88 christos void
169 1.19 lukem el_reset(EditLine *el)
170 1.1 cgd {
171 1.19 lukem
172 1.19 lukem tty_cookedmode(el);
173 1.92 christos ch_reset(el); /* XXX: Do we want that? */
174 1.1 cgd }
175 1.1 cgd
176 1.1 cgd
177 1.1 cgd /* el_set():
178 1.1 cgd * set the editline parameters
179 1.1 cgd */
180 1.88 christos int
181 1.86 christos el_wset(EditLine *el, int op, ...)
182 1.1 cgd {
183 1.44 christos va_list ap;
184 1.27 christos int rv = 0;
185 1.1 cgd
186 1.19 lukem if (el == NULL)
187 1.68 christos return -1;
188 1.44 christos va_start(ap, op);
189 1.22 wiz
190 1.19 lukem switch (op) {
191 1.19 lukem case EL_PROMPT:
192 1.51 christos case EL_RPROMPT: {
193 1.51 christos el_pfunc_t p = va_arg(ap, el_pfunc_t);
194 1.52 christos
195 1.56 christos rv = prompt_set(el, p, 0, op, 1);
196 1.52 christos break;
197 1.52 christos }
198 1.52 christos
199 1.60 christos case EL_RESIZE: {
200 1.60 christos el_zfunc_t p = va_arg(ap, el_zfunc_t);
201 1.60 christos void *arg = va_arg(ap, void *);
202 1.60 christos rv = ch_resizefun(el, p, arg);
203 1.60 christos break;
204 1.60 christos }
205 1.60 christos
206 1.73 christos case EL_ALIAS_TEXT: {
207 1.73 christos el_afunc_t p = va_arg(ap, el_afunc_t);
208 1.73 christos void *arg = va_arg(ap, void *);
209 1.73 christos rv = ch_aliasfun(el, p, arg);
210 1.73 christos break;
211 1.73 christos }
212 1.73 christos
213 1.52 christos case EL_PROMPT_ESC:
214 1.52 christos case EL_RPROMPT_ESC: {
215 1.52 christos el_pfunc_t p = va_arg(ap, el_pfunc_t);
216 1.56 christos int c = va_arg(ap, int);
217 1.51 christos
218 1.87 christos rv = prompt_set(el, p, (wchar_t)c, op, 1);
219 1.50 christos break;
220 1.51 christos }
221 1.13 simonb
222 1.19 lukem case EL_TERMINAL:
223 1.64 christos rv = terminal_set(el, va_arg(ap, char *));
224 1.19 lukem break;
225 1.1 cgd
226 1.19 lukem case EL_EDITOR:
227 1.87 christos rv = map_set_editor(el, va_arg(ap, wchar_t *));
228 1.1 cgd break;
229 1.1 cgd
230 1.19 lukem case EL_SIGNAL:
231 1.44 christos if (va_arg(ap, int))
232 1.19 lukem el->el_flags |= HANDLE_SIGNALS;
233 1.19 lukem else
234 1.19 lukem el->el_flags &= ~HANDLE_SIGNALS;
235 1.1 cgd break;
236 1.1 cgd
237 1.19 lukem case EL_BIND:
238 1.19 lukem case EL_TELLTC:
239 1.19 lukem case EL_SETTC:
240 1.19 lukem case EL_ECHOTC:
241 1.19 lukem case EL_SETTY:
242 1.19 lukem {
243 1.87 christos const wchar_t *argv[20];
244 1.19 lukem int i;
245 1.19 lukem
246 1.70 christos for (i = 1; i < (int)__arraycount(argv); i++)
247 1.87 christos if ((argv[i] = va_arg(ap, wchar_t *)) == NULL)
248 1.19 lukem break;
249 1.19 lukem
250 1.19 lukem switch (op) {
251 1.19 lukem case EL_BIND:
252 1.86 christos argv[0] = L"bind";
253 1.19 lukem rv = map_bind(el, i, argv);
254 1.19 lukem break;
255 1.19 lukem
256 1.19 lukem case EL_TELLTC:
257 1.86 christos argv[0] = L"telltc";
258 1.64 christos rv = terminal_telltc(el, i, argv);
259 1.19 lukem break;
260 1.19 lukem
261 1.19 lukem case EL_SETTC:
262 1.86 christos argv[0] = L"settc";
263 1.64 christos rv = terminal_settc(el, i, argv);
264 1.19 lukem break;
265 1.19 lukem
266 1.19 lukem case EL_ECHOTC:
267 1.86 christos argv[0] = L"echotc";
268 1.64 christos rv = terminal_echotc(el, i, argv);
269 1.19 lukem break;
270 1.19 lukem
271 1.19 lukem case EL_SETTY:
272 1.86 christos argv[0] = L"setty";
273 1.19 lukem rv = tty_stty(el, i, argv);
274 1.19 lukem break;
275 1.19 lukem
276 1.19 lukem default:
277 1.19 lukem rv = -1;
278 1.20 christos EL_ABORT((el->el_errfile, "Bad op %d\n", op));
279 1.19 lukem break;
280 1.19 lukem }
281 1.1 cgd break;
282 1.19 lukem }
283 1.19 lukem
284 1.19 lukem case EL_ADDFN:
285 1.19 lukem {
286 1.87 christos wchar_t *name = va_arg(ap, wchar_t *);
287 1.87 christos wchar_t *help = va_arg(ap, wchar_t *);
288 1.44 christos el_func_t func = va_arg(ap, el_func_t);
289 1.1 cgd
290 1.19 lukem rv = map_addfunc(el, name, help, func);
291 1.1 cgd break;
292 1.19 lukem }
293 1.19 lukem
294 1.19 lukem case EL_HIST:
295 1.19 lukem {
296 1.44 christos hist_fun_t func = va_arg(ap, hist_fun_t);
297 1.67 christos void *ptr = va_arg(ap, void *);
298 1.1 cgd
299 1.19 lukem rv = hist_set(el, func, ptr);
300 1.97 christos if (MB_CUR_MAX == 1)
301 1.59 christos el->el_flags &= ~NARROW_HISTORY;
302 1.1 cgd break;
303 1.19 lukem }
304 1.1 cgd
305 1.19 lukem case EL_EDITMODE:
306 1.44 christos if (va_arg(ap, int))
307 1.19 lukem el->el_flags &= ~EDIT_DISABLED;
308 1.19 lukem else
309 1.19 lukem el->el_flags |= EDIT_DISABLED;
310 1.19 lukem rv = 0;
311 1.1 cgd break;
312 1.13 simonb
313 1.23 christos case EL_GETCFN:
314 1.23 christos {
315 1.44 christos el_rfunc_t rc = va_arg(ap, el_rfunc_t);
316 1.89 christos rv = el_read_setfn(el->el_read, rc);
317 1.23 christos break;
318 1.23 christos }
319 1.23 christos
320 1.24 christos case EL_CLIENTDATA:
321 1.44 christos el->el_data = va_arg(ap, void *);
322 1.24 christos break;
323 1.24 christos
324 1.34 christos case EL_UNBUFFERED:
325 1.44 christos rv = va_arg(ap, int);
326 1.34 christos if (rv && !(el->el_flags & UNBUFFERED)) {
327 1.34 christos el->el_flags |= UNBUFFERED;
328 1.34 christos read_prepare(el);
329 1.34 christos } else if (!rv && (el->el_flags & UNBUFFERED)) {
330 1.34 christos el->el_flags &= ~UNBUFFERED;
331 1.34 christos read_finish(el);
332 1.34 christos }
333 1.35 christos rv = 0;
334 1.35 christos break;
335 1.35 christos
336 1.35 christos case EL_PREP_TERM:
337 1.44 christos rv = va_arg(ap, int);
338 1.35 christos if (rv)
339 1.38 christos (void) tty_rawmode(el);
340 1.35 christos else
341 1.38 christos (void) tty_cookedmode(el);
342 1.34 christos rv = 0;
343 1.34 christos break;
344 1.34 christos
345 1.44 christos case EL_SETFP:
346 1.44 christos {
347 1.44 christos FILE *fp;
348 1.44 christos int what;
349 1.44 christos
350 1.44 christos what = va_arg(ap, int);
351 1.44 christos fp = va_arg(ap, FILE *);
352 1.44 christos
353 1.44 christos rv = 0;
354 1.44 christos switch (what) {
355 1.44 christos case 0:
356 1.44 christos el->el_infile = fp;
357 1.44 christos el->el_infd = fileno(fp);
358 1.44 christos break;
359 1.44 christos case 1:
360 1.44 christos el->el_outfile = fp;
361 1.61 christos el->el_outfd = fileno(fp);
362 1.44 christos break;
363 1.44 christos case 2:
364 1.44 christos el->el_errfile = fp;
365 1.61 christos el->el_errfd = fileno(fp);
366 1.44 christos break;
367 1.44 christos default:
368 1.44 christos rv = -1;
369 1.44 christos break;
370 1.44 christos }
371 1.44 christos break;
372 1.44 christos }
373 1.44 christos
374 1.45 christos case EL_REFRESH:
375 1.45 christos re_clear_display(el);
376 1.45 christos re_refresh(el);
377 1.64 christos terminal__flush(el);
378 1.45 christos break;
379 1.45 christos
380 1.19 lukem default:
381 1.19 lukem rv = -1;
382 1.27 christos break;
383 1.1 cgd }
384 1.1 cgd
385 1.44 christos va_end(ap);
386 1.68 christos return rv;
387 1.19 lukem }
388 1.1 cgd
389 1.1 cgd
390 1.10 lukem /* el_get():
391 1.10 lukem * retrieve the editline parameters
392 1.10 lukem */
393 1.88 christos int
394 1.86 christos el_wget(EditLine *el, int op, ...)
395 1.10 lukem {
396 1.42 christos va_list ap;
397 1.19 lukem int rv;
398 1.10 lukem
399 1.42 christos if (el == NULL)
400 1.42 christos return -1;
401 1.42 christos
402 1.42 christos va_start(ap, op);
403 1.42 christos
404 1.19 lukem switch (op) {
405 1.19 lukem case EL_PROMPT:
406 1.51 christos case EL_RPROMPT: {
407 1.51 christos el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
408 1.56 christos rv = prompt_get(el, p, 0, op);
409 1.56 christos break;
410 1.56 christos }
411 1.56 christos case EL_PROMPT_ESC:
412 1.56 christos case EL_RPROMPT_ESC: {
413 1.56 christos el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
414 1.87 christos wchar_t *c = va_arg(ap, wchar_t *);
415 1.51 christos
416 1.51 christos rv = prompt_get(el, p, c, op);
417 1.19 lukem break;
418 1.51 christos }
419 1.10 lukem
420 1.19 lukem case EL_EDITOR:
421 1.87 christos rv = map_get_editor(el, va_arg(ap, const wchar_t **));
422 1.10 lukem break;
423 1.10 lukem
424 1.19 lukem case EL_SIGNAL:
425 1.42 christos *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
426 1.19 lukem rv = 0;
427 1.10 lukem break;
428 1.10 lukem
429 1.19 lukem case EL_EDITMODE:
430 1.42 christos *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
431 1.19 lukem rv = 0;
432 1.10 lukem break;
433 1.10 lukem
434 1.19 lukem case EL_TERMINAL:
435 1.64 christos terminal_get(el, va_arg(ap, const char **));
436 1.33 christos rv = 0;
437 1.10 lukem break;
438 1.10 lukem
439 1.42 christos case EL_GETTC:
440 1.19 lukem {
441 1.42 christos static char name[] = "gettc";
442 1.42 christos char *argv[20];
443 1.19 lukem int i;
444 1.10 lukem
445 1.82 christos for (i = 1; i < (int)__arraycount(argv); i++)
446 1.42 christos if ((argv[i] = va_arg(ap, char *)) == NULL)
447 1.19 lukem break;
448 1.19 lukem
449 1.69 christos argv[0] = name;
450 1.69 christos rv = terminal_gettc(el, i, argv);
451 1.10 lukem break;
452 1.10 lukem }
453 1.13 simonb
454 1.23 christos case EL_GETCFN:
455 1.89 christos *va_arg(ap, el_rfunc_t *) = el_read_getfn(el->el_read);
456 1.24 christos rv = 0;
457 1.24 christos break;
458 1.24 christos
459 1.24 christos case EL_CLIENTDATA:
460 1.42 christos *va_arg(ap, void **) = el->el_data;
461 1.34 christos rv = 0;
462 1.34 christos break;
463 1.34 christos
464 1.34 christos case EL_UNBUFFERED:
465 1.71 christos *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
466 1.23 christos rv = 0;
467 1.23 christos break;
468 1.19 lukem
469 1.44 christos case EL_GETFP:
470 1.44 christos {
471 1.44 christos int what;
472 1.44 christos FILE **fpp;
473 1.44 christos
474 1.44 christos what = va_arg(ap, int);
475 1.44 christos fpp = va_arg(ap, FILE **);
476 1.44 christos rv = 0;
477 1.44 christos switch (what) {
478 1.44 christos case 0:
479 1.44 christos *fpp = el->el_infile;
480 1.44 christos break;
481 1.44 christos case 1:
482 1.44 christos *fpp = el->el_outfile;
483 1.44 christos break;
484 1.44 christos case 2:
485 1.44 christos *fpp = el->el_errfile;
486 1.44 christos break;
487 1.44 christos default:
488 1.44 christos rv = -1;
489 1.44 christos break;
490 1.44 christos }
491 1.44 christos break;
492 1.44 christos }
493 1.19 lukem default:
494 1.19 lukem rv = -1;
495 1.44 christos break;
496 1.19 lukem }
497 1.42 christos va_end(ap);
498 1.10 lukem
499 1.68 christos return rv;
500 1.19 lukem }
501 1.10 lukem
502 1.10 lukem
503 1.1 cgd /* el_line():
504 1.1 cgd * Return editing info
505 1.1 cgd */
506 1.88 christos const LineInfoW *
507 1.86 christos el_wline(EditLine *el)
508 1.1 cgd {
509 1.19 lukem
510 1.86 christos return (const LineInfoW *)(void *)&el->el_line;
511 1.1 cgd }
512 1.1 cgd
513 1.1 cgd
514 1.1 cgd /* el_source():
515 1.1 cgd * Source a file
516 1.1 cgd */
517 1.88 christos int
518 1.19 lukem el_source(EditLine *el, const char *fname)
519 1.1 cgd {
520 1.19 lukem FILE *fp;
521 1.19 lukem size_t len;
522 1.77 christos ssize_t slen;
523 1.27 christos char *ptr;
524 1.64 christos char *path = NULL;
525 1.87 christos const wchar_t *dptr;
526 1.64 christos int error = 0;
527 1.19 lukem
528 1.19 lukem fp = NULL;
529 1.19 lukem if (fname == NULL) {
530 1.29 christos #ifdef HAVE_ISSETUGID
531 1.19 lukem if (issetugid())
532 1.68 christos return -1;
533 1.93 kre
534 1.93 kre if ((fname = getenv("EDITRC")) == NULL) {
535 1.93 kre static const char elpath[] = "/.editrc";
536 1.93 kre size_t plen = sizeof(elpath);
537 1.93 kre
538 1.93 kre if ((ptr = getenv("HOME")) == NULL)
539 1.93 kre return -1;
540 1.93 kre plen += strlen(ptr);
541 1.93 kre if ((path = el_malloc(plen * sizeof(*path))) == NULL)
542 1.93 kre return -1;
543 1.93 kre (void)snprintf(path, plen, "%s%s", ptr,
544 1.93 kre elpath + (*ptr == '\0'));
545 1.93 kre fname = path;
546 1.93 kre }
547 1.27 christos #else
548 1.27 christos /*
549 1.27 christos * If issetugid() is missing, always return an error, in order
550 1.27 christos * to keep from inadvertently opening up the user to a security
551 1.27 christos * hole.
552 1.27 christos */
553 1.68 christos return -1;
554 1.27 christos #endif
555 1.19 lukem }
556 1.93 kre if (fname[0] == '\0')
557 1.93 kre return -1;
558 1.93 kre
559 1.19 lukem if (fp == NULL)
560 1.19 lukem fp = fopen(fname, "r");
561 1.64 christos if (fp == NULL) {
562 1.64 christos el_free(path);
563 1.68 christos return -1;
564 1.64 christos }
565 1.19 lukem
566 1.77 christos ptr = NULL;
567 1.77 christos len = 0;
568 1.77 christos while ((slen = getline(&ptr, &len, fp)) != -1) {
569 1.63 christos if (*ptr == '\n')
570 1.63 christos continue; /* Empty line. */
571 1.77 christos if (slen > 0 && ptr[--slen] == '\n')
572 1.77 christos ptr[slen] = '\0';
573 1.77 christos
574 1.56 christos dptr = ct_decode_string(ptr, &el->el_scratch);
575 1.56 christos if (!dptr)
576 1.56 christos continue;
577 1.55 christos /* loop until first non-space char or EOL */
578 1.85 christos while (*dptr != '\0' && iswspace(*dptr))
579 1.56 christos dptr++;
580 1.56 christos if (*dptr == '#')
581 1.55 christos continue; /* ignore, this is a comment line */
582 1.64 christos if ((error = parse_line(el, dptr)) == -1)
583 1.64 christos break;
584 1.1 cgd }
585 1.76 christos free(ptr);
586 1.1 cgd
587 1.64 christos el_free(path);
588 1.19 lukem (void) fclose(fp);
589 1.68 christos return error;
590 1.1 cgd }
591 1.1 cgd
592 1.1 cgd
593 1.1 cgd /* el_resize():
594 1.1 cgd * Called from program when terminal is resized
595 1.1 cgd */
596 1.88 christos void
597 1.19 lukem el_resize(EditLine *el)
598 1.1 cgd {
599 1.19 lukem int lins, cols;
600 1.19 lukem sigset_t oset, nset;
601 1.19 lukem
602 1.19 lukem (void) sigemptyset(&nset);
603 1.19 lukem (void) sigaddset(&nset, SIGWINCH);
604 1.19 lukem (void) sigprocmask(SIG_BLOCK, &nset, &oset);
605 1.19 lukem
606 1.19 lukem /* get the correct window size */
607 1.64 christos if (terminal_get_size(el, &lins, &cols))
608 1.64 christos terminal_change_size(el, lins, cols);
609 1.1 cgd
610 1.19 lukem (void) sigprocmask(SIG_SETMASK, &oset, NULL);
611 1.9 christos }
612 1.14 lukem
613 1.9 christos
614 1.9 christos /* el_beep():
615 1.9 christos * Called from the program to beep
616 1.9 christos */
617 1.88 christos void
618 1.19 lukem el_beep(EditLine *el)
619 1.9 christos {
620 1.19 lukem
621 1.64 christos terminal_beep(el);
622 1.1 cgd }
623 1.10 lukem
624 1.10 lukem
625 1.10 lukem /* el_editmode()
626 1.10 lukem * Set the state of EDIT_DISABLED from the `edit' command.
627 1.10 lukem */
628 1.91 christos libedit_private int
629 1.10 lukem /*ARGSUSED*/
630 1.87 christos el_editmode(EditLine *el, int argc, const wchar_t **argv)
631 1.10 lukem {
632 1.87 christos const wchar_t *how;
633 1.10 lukem
634 1.19 lukem if (argv == NULL || argc != 2 || argv[1] == NULL)
635 1.68 christos return -1;
636 1.10 lukem
637 1.19 lukem how = argv[1];
638 1.86 christos if (wcscmp(how, L"on") == 0) {
639 1.19 lukem el->el_flags &= ~EDIT_DISABLED;
640 1.39 christos tty_rawmode(el);
641 1.86 christos } else if (wcscmp(how, L"off") == 0) {
642 1.39 christos tty_cookedmode(el);
643 1.19 lukem el->el_flags |= EDIT_DISABLED;
644 1.39 christos }
645 1.19 lukem else {
646 1.85 christos (void) fprintf(el->el_errfile, "edit: Bad value `%ls'.\n",
647 1.56 christos how);
648 1.68 christos return -1;
649 1.19 lukem }
650 1.68 christos return 0;
651 1.19 lukem }
652