~ubuntu-branches/ubuntu/vivid/openipmi/vivid

« back to all changes in this revision

Viewing changes to libedit/term.c

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2006-09-15 17:56:24 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060915175624-ljk0mg3xtcm65tvm
Tags: 2.0.7-1
* new upstream release from 2006-06-08
  Thanks to John Wright <john.wright hp.com> for initial work
  (closes: Bug#380149)
* updated Standards Version
* new binaries openipmicmd, openipmish, rmcp_ping

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $NetBSD: term.c,v 1.41 2005/08/08 14:05:37 christos Exp $       */
 
2
 
 
3
/*-
 
4
 * Copyright (c) 1992, 1993
 
5
 *      The Regents of the University of California.  All rights reserved.
 
6
 *
 
7
 * This code is derived from software contributed to Berkeley by
 
8
 * Christos Zoulas of Cornell University.
 
9
 *
 
10
 * Redistribution and use in source and binary forms, with or without
 
11
 * modification, are permitted provided that the following conditions
 
12
 * are met:
 
13
 * 1. Redistributions of source code must retain the above copyright
 
14
 *    notice, this list of conditions and the following disclaimer.
 
15
 * 2. Redistributions in binary form must reproduce the above copyright
 
16
 *    notice, this list of conditions and the following disclaimer in the
 
17
 *    documentation and/or other materials provided with the distribution.
 
18
 * 3. Neither the name of the University nor the names of its contributors
 
19
 *    may be used to endorse or promote products derived from this software
 
20
 *    without specific prior written permission.
 
21
 *
 
22
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
25
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
26
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
27
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
28
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
29
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
31
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
32
 * SUCH DAMAGE.
 
33
 */
 
34
 
 
35
#include "sys.h"
 
36
#if !defined(lint) && !defined(SCCSID)
 
37
#if 0
 
38
static char sccsid[] = "@(#)term.c      8.2 (Berkeley) 4/30/95";
 
39
#else
 
40
__RCSID("$NetBSD: term.c,v 1.41 2005/08/08 14:05:37 christos Exp $");
 
41
#endif
 
42
#endif /* not lint && not SCCSID */
 
43
 
 
44
/*
 
45
 * term.c: Editor/termcap-curses interface
 
46
 *         We have to declare a static variable here, since the
 
47
 *         termcap putchar routine does not take an argument!
 
48
 */
 
49
#include <stdio.h>
 
50
#include <signal.h>
 
51
#include <string.h>
 
52
#include <stdlib.h>
 
53
#include <unistd.h>
 
54
 
 
55
#ifdef HAVE_CURSES_H
 
56
# include <curses.h>
 
57
#elif HAVE_NCURSES_H
 
58
# include <ncurses.h>
 
59
#endif
 
60
 
 
61
/* Solaris's term.h does horrid things. */
 
62
#if (defined(HAVE_TERM_H) && !defined(_SUNOS))
 
63
# include <term.h>
 
64
#endif
 
65
 
 
66
#include <sys/types.h>
 
67
#include <sys/ioctl.h>
 
68
 
 
69
#include "el.h"
 
70
 
 
71
/*
 
72
 * IMPORTANT NOTE: these routines are allowed to look at the current screen
 
73
 * and the current possition assuming that it is correct.  If this is not
 
74
 * true, then the update will be WRONG!  This is (should be) a valid
 
75
 * assumption...
 
76
 */
 
77
 
 
78
#define TC_BUFSIZE      2048
 
79
 
 
80
#define GoodStr(a)      (el->el_term.t_str[a] != NULL && \
 
81
                            el->el_term.t_str[a][0] != '\0')
 
82
#define Str(a)          el->el_term.t_str[a]
 
83
#define Val(a)          el->el_term.t_val[a]
 
84
 
 
85
#ifdef notdef
 
86
private const struct {
 
87
        const char *b_name;
 
88
        int b_rate;
 
89
} baud_rate[] = {
 
90
#ifdef B0
 
91
        { "0", B0 },
 
92
#endif
 
93
#ifdef B50
 
94
        { "50", B50 },
 
95
#endif
 
96
#ifdef B75
 
97
        { "75", B75 },
 
98
#endif
 
99
#ifdef B110
 
100
        { "110", B110 },
 
101
#endif
 
102
#ifdef B134
 
103
        { "134", B134 },
 
104
#endif
 
105
#ifdef B150
 
106
        { "150", B150 },
 
107
#endif
 
108
#ifdef B200
 
109
        { "200", B200 },
 
110
#endif
 
111
#ifdef B300
 
112
        { "300", B300 },
 
113
#endif
 
114
#ifdef B600
 
115
        { "600", B600 },
 
116
#endif
 
117
#ifdef B900
 
118
        { "900", B900 },
 
119
#endif
 
120
#ifdef B1200
 
121
        { "1200", B1200 },
 
122
#endif
 
123
#ifdef B1800
 
124
        { "1800", B1800 },
 
125
#endif
 
126
#ifdef B2400
 
127
        { "2400", B2400 },
 
128
#endif
 
129
#ifdef B3600
 
130
        { "3600", B3600 },
 
131
#endif
 
132
#ifdef B4800
 
133
        { "4800", B4800 },
 
134
#endif
 
135
#ifdef B7200
 
136
        { "7200", B7200 },
 
137
#endif
 
138
#ifdef B9600
 
139
        { "9600", B9600 },
 
140
#endif
 
141
#ifdef EXTA
 
142
        { "19200", EXTA },
 
143
#endif
 
144
#ifdef B19200
 
145
        { "19200", B19200 },
 
146
#endif
 
147
#ifdef EXTB
 
148
        { "38400", EXTB },
 
149
#endif
 
150
#ifdef B38400
 
151
        { "38400", B38400 },
 
152
#endif
 
153
        { NULL, 0 }
 
154
};
 
155
#endif
 
156
 
 
157
private const struct termcapstr {
 
158
        const char *name;
 
159
        const char *long_name;
 
160
} tstr[] = {
 
161
#define T_al    0
 
162
        { "al", "add new blank line" },
 
163
#define T_bl    1
 
164
        { "bl", "audible bell" },
 
165
#define T_cd    2
 
166
        { "cd", "clear to bottom" },
 
167
#define T_ce    3
 
168
        { "ce", "clear to end of line" },
 
169
#define T_ch    4
 
170
        { "ch", "cursor to horiz pos" },
 
171
#define T_cl    5
 
172
        { "cl", "clear screen" },
 
173
#define T_dc    6
 
174
        { "dc", "delete a character" },
 
175
#define T_dl    7
 
176
        { "dl", "delete a line" },
 
177
#define T_dm    8
 
178
        { "dm", "start delete mode" },
 
179
#define T_ed    9
 
180
        { "ed", "end delete mode" },
 
181
#define T_ei    10
 
182
        { "ei", "end insert mode" },
 
183
#define T_fs    11
 
184
        { "fs", "cursor from status line" },
 
185
#define T_ho    12
 
186
        { "ho", "home cursor" },
 
187
#define T_ic    13
 
188
        { "ic", "insert character" },
 
189
#define T_im    14
 
190
        { "im", "start insert mode" },
 
191
#define T_ip    15
 
192
        { "ip", "insert padding" },
 
193
#define T_kd    16
 
194
        { "kd", "sends cursor down" },
 
195
#define T_kl    17
 
196
        { "kl", "sends cursor left" },
 
197
#define T_kr    18
 
198
        { "kr", "sends cursor right" },
 
199
#define T_ku    19
 
200
        { "ku", "sends cursor up" },
 
201
#define T_md    20
 
202
        { "md", "begin bold" },
 
203
#define T_me    21
 
204
        { "me", "end attributes" },
 
205
#define T_nd    22
 
206
        { "nd", "non destructive space" },
 
207
#define T_se    23
 
208
        { "se", "end standout" },
 
209
#define T_so    24
 
210
        { "so", "begin standout" },
 
211
#define T_ts    25
 
212
        { "ts", "cursor to status line" },
 
213
#define T_up    26
 
214
        { "up", "cursor up one" },
 
215
#define T_us    27
 
216
        { "us", "begin underline" },
 
217
#define T_ue    28
 
218
        { "ue", "end underline" },
 
219
#define T_vb    29
 
220
        { "vb", "visible bell" },
 
221
#define T_DC    30
 
222
        { "DC", "delete multiple chars" },
 
223
#define T_DO    31
 
224
        { "DO", "cursor down multiple" },
 
225
#define T_IC    32
 
226
        { "IC", "insert multiple chars" },
 
227
#define T_LE    33
 
228
        { "LE", "cursor left multiple" },
 
229
#define T_RI    34
 
230
        { "RI", "cursor right multiple" },
 
231
#define T_UP    35
 
232
        { "UP", "cursor up multiple" },
 
233
#define T_kh    36
 
234
        { "kh", "send cursor home" },
 
235
#define T_at7   37
 
236
        { "@7", "send cursor end" },
 
237
#define T_str   38
 
238
        { NULL, NULL }
 
239
};
 
240
 
 
241
private const struct termcapval {
 
242
        const char *name;
 
243
        const char *long_name;
 
244
} tval[] = {
 
245
#define T_am    0
 
246
        { "am", "has automatic margins" },
 
247
#define T_pt    1
 
248
        { "pt", "has physical tabs" },
 
249
#define T_li    2
 
250
        { "li", "Number of lines" },
 
251
#define T_co    3
 
252
        { "co", "Number of columns" },
 
253
#define T_km    4
 
254
        { "km", "Has meta key" },
 
255
#define T_xt    5
 
256
        { "xt", "Tab chars destructive" },
 
257
#define T_xn    6
 
258
        { "xn", "newline ignored at right margin" },
 
259
#define T_MT    7
 
260
        { "MT", "Has meta key" },                       /* XXX? */
 
261
#define T_val   8
 
262
        { NULL, NULL, }
 
263
};
 
264
/* do two or more of the attributes use me */
 
265
 
 
266
private void    term_setflags(EditLine *);
 
267
private int     term_rebuffer_display(EditLine *);
 
268
private void    term_free_display(EditLine *);
 
269
private int     term_alloc_display(EditLine *);
 
270
private void    term_alloc(EditLine *, const struct termcapstr *, const char *);
 
271
private void    term_init_arrow(EditLine *);
 
272
private void    term_reset_arrow(EditLine *);
 
273
 
 
274
 
 
275
private FILE *term_outfile = NULL;      /* XXX: How do we fix that? */
 
276
 
 
277
 
 
278
/* term_setflags():
 
279
 *      Set the terminal capability flags
 
280
 */
 
281
private void
 
282
term_setflags(EditLine *el)
 
283
{
 
284
        EL_FLAGS = 0;
 
285
        if (el->el_tty.t_tabs)
 
286
                EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
 
287
 
 
288
        EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
 
289
        EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
 
290
        EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
 
291
        EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
 
292
            TERM_CAN_INSERT : 0;
 
293
        EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
 
294
        EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
 
295
        EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
 
296
 
 
297
        if (GoodStr(T_me) && GoodStr(T_ue))
 
298
                EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
 
299
                    TERM_CAN_ME : 0;
 
300
        else
 
301
                EL_FLAGS &= ~TERM_CAN_ME;
 
302
        if (GoodStr(T_me) && GoodStr(T_se))
 
303
                EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
 
304
                    TERM_CAN_ME : 0;
 
305
 
 
306
 
 
307
#ifdef DEBUG_SCREEN
 
308
        if (!EL_CAN_UP) {
 
309
                (void) fprintf(el->el_errfile,
 
310
                    "WARNING: Your terminal cannot move up.\n");
 
311
                (void) fprintf(el->el_errfile,
 
312
                    "Editing may be odd for long lines.\n");
 
313
        }
 
314
        if (!EL_CAN_CEOL)
 
315
                (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
 
316
        if (!EL_CAN_DELETE)
 
317
                (void) fprintf(el->el_errfile, "no delete char capability.\n");
 
318
        if (!EL_CAN_INSERT)
 
319
                (void) fprintf(el->el_errfile, "no insert char capability.\n");
 
320
#endif /* DEBUG_SCREEN */
 
321
}
 
322
 
 
323
 
 
324
/* term_init():
 
325
 *      Initialize the terminal stuff
 
326
 */
 
327
protected int
 
328
term_init(EditLine *el)
 
329
{
 
330
 
 
331
        el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
 
332
        if (el->el_term.t_buf == NULL)
 
333
                return (-1);
 
334
        el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
 
335
        if (el->el_term.t_cap == NULL)
 
336
                return (-1);
 
337
        el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
 
338
        if (el->el_term.t_fkey == NULL)
 
339
                return (-1);
 
340
        el->el_term.t_loc = 0;
 
341
        el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
 
342
        if (el->el_term.t_str == NULL)
 
343
                return (-1);
 
344
        (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
 
345
        el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
 
346
        if (el->el_term.t_val == NULL)
 
347
                return (-1);
 
348
        (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
 
349
        term_outfile = el->el_outfile;
 
350
        (void) term_set(el, NULL);
 
351
        term_init_arrow(el);
 
352
        return (0);
 
353
}
 
354
 
 
355
/* term_end():
 
356
 *      Clean up the terminal stuff
 
357
 */
 
358
protected void
 
359
term_end(EditLine *el)
 
360
{
 
361
 
 
362
        el_free((ptr_t) el->el_term.t_buf);
 
363
        el->el_term.t_buf = NULL;
 
364
        el_free((ptr_t) el->el_term.t_cap);
 
365
        el->el_term.t_cap = NULL;
 
366
        el->el_term.t_loc = 0;
 
367
        el_free((ptr_t) el->el_term.t_str);
 
368
        el->el_term.t_str = NULL;
 
369
        el_free((ptr_t) el->el_term.t_val);
 
370
        el->el_term.t_val = NULL;
 
371
        el_free((ptr_t) el->el_term.t_fkey);
 
372
        el->el_term.t_fkey = NULL;
 
373
        term_free_display(el);
 
374
}
 
375
 
 
376
 
 
377
/* term_alloc():
 
378
 *      Maintain a string pool for termcap strings
 
379
 */
 
380
private void
 
381
term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
 
382
{
 
383
        char termbuf[TC_BUFSIZE];
 
384
        int tlen, clen;
 
385
        char **tlist = el->el_term.t_str;
 
386
        char **tmp, **str = &tlist[t - tstr];
 
387
 
 
388
        if (cap == NULL || *cap == '\0') {
 
389
                *str = NULL;
 
390
                return;
 
391
        } else
 
392
                clen = strlen(cap);
 
393
 
 
394
        tlen = *str == NULL ? 0 : strlen(*str);
 
395
 
 
396
        /*
 
397
         * New string is shorter; no need to allocate space
 
398
         */
 
399
        if (clen <= tlen) {
 
400
                (void) strcpy(*str, cap);       /* XXX strcpy is safe */
 
401
                return;
 
402
        }
 
403
        /*
 
404
         * New string is longer; see if we have enough space to append
 
405
         */
 
406
        if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
 
407
                                                /* XXX strcpy is safe */
 
408
                (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
 
409
                    cap);
 
410
                el->el_term.t_loc += clen + 1;  /* one for \0 */
 
411
                return;
 
412
        }
 
413
        /*
 
414
         * Compact our buffer; no need to check compaction, cause we know it
 
415
         * fits...
 
416
         */
 
417
        tlen = 0;
 
418
        for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
 
419
                if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
 
420
                        char *ptr;
 
421
 
 
422
                        for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
 
423
                                continue;
 
424
                        termbuf[tlen++] = '\0';
 
425
                }
 
426
        memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
 
427
        el->el_term.t_loc = tlen;
 
428
        if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
 
429
                (void) fprintf(el->el_errfile,
 
430
                    "Out of termcap string space.\n");
 
431
                return;
 
432
        }
 
433
                                        /* XXX strcpy is safe */
 
434
        (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
 
435
        el->el_term.t_loc += clen + 1;  /* one for \0 */
 
436
        return;
 
437
}
 
438
 
 
439
 
 
440
/* term_rebuffer_display():
 
441
 *      Rebuffer the display after the screen changed size
 
442
 */
 
443
private int
 
444
term_rebuffer_display(EditLine *el)
 
445
{
 
446
        coord_t *c = &el->el_term.t_size;
 
447
 
 
448
        term_free_display(el);
 
449
 
 
450
        c->h = Val(T_co);
 
451
        c->v = Val(T_li);
 
452
 
 
453
        if (term_alloc_display(el) == -1)
 
454
                return (-1);
 
455
        return (0);
 
456
}
 
457
 
 
458
 
 
459
/* term_alloc_display():
 
460
 *      Allocate a new display.
 
461
 */
 
462
private int
 
463
term_alloc_display(EditLine *el)
 
464
{
 
465
        int i;
 
466
        char **b;
 
467
        coord_t *c = &el->el_term.t_size;
 
468
 
 
469
        b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
 
470
        if (b == NULL)
 
471
                return (-1);
 
472
        for (i = 0; i < c->v; i++) {
 
473
                b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
 
474
                if (b[i] == NULL)
 
475
                        return (-1);
 
476
        }
 
477
        b[c->v] = NULL;
 
478
        el->el_display = b;
 
479
 
 
480
        b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
 
481
        if (b == NULL)
 
482
                return (-1);
 
483
        for (i = 0; i < c->v; i++) {
 
484
                b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
 
485
                if (b[i] == NULL)
 
486
                        return (-1);
 
487
        }
 
488
        b[c->v] = NULL;
 
489
        el->el_vdisplay = b;
 
490
        return (0);
 
491
}
 
492
 
 
493
 
 
494
/* term_free_display():
 
495
 *      Free the display buffers
 
496
 */
 
497
private void
 
498
term_free_display(EditLine *el)
 
499
{
 
500
        char **b;
 
501
        char **bufp;
 
502
 
 
503
        b = el->el_display;
 
504
        el->el_display = NULL;
 
505
        if (b != NULL) {
 
506
                for (bufp = b; *bufp != NULL; bufp++)
 
507
                        el_free((ptr_t) * bufp);
 
508
                el_free((ptr_t) b);
 
509
        }
 
510
        b = el->el_vdisplay;
 
511
        el->el_vdisplay = NULL;
 
512
        if (b != NULL) {
 
513
                for (bufp = b; *bufp != NULL; bufp++)
 
514
                        el_free((ptr_t) * bufp);
 
515
                el_free((ptr_t) b);
 
516
        }
 
517
}
 
518
 
 
519
 
 
520
/* term_move_to_line():
 
521
 *      move to line <where> (first line == 0)
 
522
 *      as efficiently as possible
 
523
 */
 
524
protected void
 
525
term_move_to_line(EditLine *el, int where)
 
526
{
 
527
        int del;
 
528
 
 
529
        if (where == el->el_cursor.v)
 
530
                return;
 
531
 
 
532
        if (where > el->el_term.t_size.v) {
 
533
#ifdef DEBUG_SCREEN
 
534
                (void) fprintf(el->el_errfile,
 
535
                    "term_move_to_line: where is ridiculous: %d\r\n", where);
 
536
#endif /* DEBUG_SCREEN */
 
537
                return;
 
538
        }
 
539
        if ((del = where - el->el_cursor.v) > 0) {
 
540
                while (del > 0) {
 
541
                        if (EL_HAS_AUTO_MARGINS &&
 
542
                            el->el_display[el->el_cursor.v][0] != '\0') {
 
543
                                /* move without newline */
 
544
                                term_move_to_char(el, el->el_term.t_size.h - 1);
 
545
                                term_overwrite(el,
 
546
                                    &el->el_display[el->el_cursor.v][el->el_cursor.h],
 
547
                                    1);
 
548
                                /* updates Cursor */
 
549
                                del--;
 
550
                        } else {
 
551
                                if ((del > 1) && GoodStr(T_DO)) {
 
552
                                        (void) tputs(tgoto(Str(T_DO), del, del),
 
553
                                            del, term__putc);
 
554
                                        del = 0;
 
555
                                } else {
 
556
                                        for (; del > 0; del--)
 
557
                                                term__putc('\n');
 
558
                                        /* because the \n will become \r\n */
 
559
                                        el->el_cursor.h = 0;
 
560
                                }
 
561
                        }
 
562
                }
 
563
        } else {                /* del < 0 */
 
564
                if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
 
565
                        (void) tputs(tgoto(Str(T_UP), -del, -del), -del,
 
566
                            term__putc);
 
567
                else {
 
568
                        if (GoodStr(T_up))
 
569
                                for (; del < 0; del++)
 
570
                                        (void) tputs(Str(T_up), 1, term__putc);
 
571
                }
 
572
        }
 
573
        el->el_cursor.v = where;/* now where is here */
 
574
}
 
575
 
 
576
 
 
577
/* term_move_to_char():
 
578
 *      Move to the character position specified
 
579
 */
 
580
protected void
 
581
term_move_to_char(EditLine *el, int where)
 
582
{
 
583
        int del, i;
 
584
 
 
585
mc_again:
 
586
        if (where == el->el_cursor.h)
 
587
                return;
 
588
 
 
589
        if (where > el->el_term.t_size.h) {
 
590
#ifdef DEBUG_SCREEN
 
591
                (void) fprintf(el->el_errfile,
 
592
                    "term_move_to_char: where is riduculous: %d\r\n", where);
 
593
#endif /* DEBUG_SCREEN */
 
594
                return;
 
595
        }
 
596
        if (!where) {           /* if where is first column */
 
597
                term__putc('\r');       /* do a CR */
 
598
                el->el_cursor.h = 0;
 
599
                return;
 
600
        }
 
601
        del = where - el->el_cursor.h;
 
602
 
 
603
        if ((del < -4 || del > 4) && GoodStr(T_ch))
 
604
                /* go there directly */
 
605
                (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
 
606
        else {
 
607
                if (del > 0) {  /* moving forward */
 
608
                        if ((del > 4) && GoodStr(T_RI))
 
609
                                (void) tputs(tgoto(Str(T_RI), del, del),
 
610
                                    del, term__putc);
 
611
                        else {
 
612
                                        /* if I can do tabs, use them */
 
613
                                if (EL_CAN_TAB) {
 
614
                                        if ((el->el_cursor.h & 0370) !=
 
615
                                            (where & 0370)) {
 
616
                                                /* if not within tab stop */
 
617
                                                for (i =
 
618
                                                    (el->el_cursor.h & 0370);
 
619
                                                    i < (where & 0370);
 
620
                                                    i += 8)
 
621
                                                        term__putc('\t');       
 
622
                                                        /* then tab over */
 
623
                                                el->el_cursor.h = where & 0370;
 
624
                                        }
 
625
                                }
 
626
                                /*
 
627
                                 * it's usually cheaper to just write the
 
628
                                 * chars, so we do.
 
629
                                 */
 
630
                                /*
 
631
                                 * NOTE THAT term_overwrite() WILL CHANGE
 
632
                                 * el->el_cursor.h!!!
 
633
                                 */
 
634
                                term_overwrite(el,
 
635
                                    &el->el_display[el->el_cursor.v][el->el_cursor.h],
 
636
                                    where - el->el_cursor.h);
 
637
 
 
638
                        }
 
639
                } else {        /* del < 0 := moving backward */
 
640
                        if ((-del > 4) && GoodStr(T_LE))
 
641
                                (void) tputs(tgoto(Str(T_LE), -del, -del),
 
642
                                    -del, term__putc);
 
643
                        else {  /* can't go directly there */
 
644
                                /*
 
645
                                 * if the "cost" is greater than the "cost"
 
646
                                 * from col 0
 
647
                                 */
 
648
                                if (EL_CAN_TAB ?
 
649
                                    ((unsigned int)-del >
 
650
                                    (((unsigned int) where >> 3) +
 
651
                                     (where & 07)))
 
652
                                    : (-del > where)) {
 
653
                                        term__putc('\r');       /* do a CR */
 
654
                                        el->el_cursor.h = 0;
 
655
                                        goto mc_again;  /* and try again */
 
656
                                }
 
657
                                for (i = 0; i < -del; i++)
 
658
                                        term__putc('\b');
 
659
                        }
 
660
                }
 
661
        }
 
662
        el->el_cursor.h = where;                /* now where is here */
 
663
}
 
664
 
 
665
 
 
666
/* term_overwrite():
 
667
 *      Overstrike num characters
 
668
 */
 
669
protected void
 
670
term_overwrite(EditLine *el, const char *cp, int n)
 
671
{
 
672
        if (n <= 0)
 
673
                return;         /* catch bugs */
 
674
 
 
675
        if (n > el->el_term.t_size.h) {
 
676
#ifdef DEBUG_SCREEN
 
677
                (void) fprintf(el->el_errfile,
 
678
                    "term_overwrite: n is riduculous: %d\r\n", n);
 
679
#endif /* DEBUG_SCREEN */
 
680
                return;
 
681
        }
 
682
        do {
 
683
                term__putc(*cp++);
 
684
                el->el_cursor.h++;
 
685
        } while (--n);
 
686
 
 
687
        if (el->el_cursor.h >= el->el_term.t_size.h) {  /* wrap? */
 
688
                if (EL_HAS_AUTO_MARGINS) {      /* yes */
 
689
                        el->el_cursor.h = 0;
 
690
                        el->el_cursor.v++;
 
691
                        if (EL_HAS_MAGIC_MARGINS) {
 
692
                                /* force the wrap to avoid the "magic"
 
693
                                 * situation */
 
694
                                char c;
 
695
                                if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
 
696
                                    != '\0')
 
697
                                        term_overwrite(el, &c, 1);
 
698
                                else
 
699
                                        term__putc(' ');
 
700
                                el->el_cursor.h = 1;
 
701
                        }
 
702
                } else          /* no wrap, but cursor stays on screen */
 
703
                        el->el_cursor.h = el->el_term.t_size.h;
 
704
        }
 
705
}
 
706
 
 
707
 
 
708
/* term_deletechars():
 
709
 *      Delete num characters
 
710
 */
 
711
protected void
 
712
term_deletechars(EditLine *el, int num)
 
713
{
 
714
        if (num <= 0)
 
715
                return;
 
716
 
 
717
        if (!EL_CAN_DELETE) {
 
718
#ifdef DEBUG_EDIT
 
719
                (void) fprintf(el->el_errfile, "   ERROR: cannot delete   \n");
 
720
#endif /* DEBUG_EDIT */
 
721
                return;
 
722
        }
 
723
        if (num > el->el_term.t_size.h) {
 
724
#ifdef DEBUG_SCREEN
 
725
                (void) fprintf(el->el_errfile,
 
726
                    "term_deletechars: num is riduculous: %d\r\n", num);
 
727
#endif /* DEBUG_SCREEN */
 
728
                return;
 
729
        }
 
730
        if (GoodStr(T_DC))      /* if I have multiple delete */
 
731
                if ((num > 1) || !GoodStr(T_dc)) {      /* if dc would be more
 
732
                                                         * expen. */
 
733
                        (void) tputs(tgoto(Str(T_DC), num, num),
 
734
                            num, term__putc);
 
735
                        return;
 
736
                }
 
737
        if (GoodStr(T_dm))      /* if I have delete mode */
 
738
                (void) tputs(Str(T_dm), 1, term__putc);
 
739
 
 
740
        if (GoodStr(T_dc))      /* else do one at a time */
 
741
                while (num--)
 
742
                        (void) tputs(Str(T_dc), 1, term__putc);
 
743
 
 
744
        if (GoodStr(T_ed))      /* if I have delete mode */
 
745
                (void) tputs(Str(T_ed), 1, term__putc);
 
746
}
 
747
 
 
748
 
 
749
/* term_insertwrite():
 
750
 *      Puts terminal in insert character mode or inserts num
 
751
 *      characters in the line
 
752
 */
 
753
protected void
 
754
term_insertwrite(EditLine *el, char *cp, int num)
 
755
{
 
756
        if (num <= 0)
 
757
                return;
 
758
        if (!EL_CAN_INSERT) {
 
759
#ifdef DEBUG_EDIT
 
760
                (void) fprintf(el->el_errfile, "   ERROR: cannot insert   \n");
 
761
#endif /* DEBUG_EDIT */
 
762
                return;
 
763
        }
 
764
        if (num > el->el_term.t_size.h) {
 
765
#ifdef DEBUG_SCREEN
 
766
                (void) fprintf(el->el_errfile,
 
767
                    "StartInsert: num is riduculous: %d\r\n", num);
 
768
#endif /* DEBUG_SCREEN */
 
769
                return;
 
770
        }
 
771
        if (GoodStr(T_IC))      /* if I have multiple insert */
 
772
                if ((num > 1) || !GoodStr(T_ic)) {
 
773
                                /* if ic would be more expensive */
 
774
                        (void) tputs(tgoto(Str(T_IC), num, num),
 
775
                            num, term__putc);
 
776
                        term_overwrite(el, cp, num);
 
777
                                /* this updates el_cursor.h */
 
778
                        return;
 
779
                }
 
780
        if (GoodStr(T_im) && GoodStr(T_ei)) {   /* if I have insert mode */
 
781
                (void) tputs(Str(T_im), 1, term__putc);
 
782
 
 
783
                el->el_cursor.h += num;
 
784
                do
 
785
                        term__putc(*cp++);
 
786
                while (--num);
 
787
 
 
788
                if (GoodStr(T_ip))      /* have to make num chars insert */
 
789
                        (void) tputs(Str(T_ip), 1, term__putc);
 
790
 
 
791
                (void) tputs(Str(T_ei), 1, term__putc);
 
792
                return;
 
793
        }
 
794
        do {
 
795
                if (GoodStr(T_ic))      /* have to make num chars insert */
 
796
                        (void) tputs(Str(T_ic), 1, term__putc);
 
797
                                        /* insert a char */
 
798
 
 
799
                term__putc(*cp++);
 
800
 
 
801
                el->el_cursor.h++;
 
802
 
 
803
                if (GoodStr(T_ip))      /* have to make num chars insert */
 
804
                        (void) tputs(Str(T_ip), 1, term__putc);
 
805
                                        /* pad the inserted char */
 
806
 
 
807
        } while (--num);
 
808
}
 
809
 
 
810
 
 
811
/* term_clear_EOL():
 
812
 *      clear to end of line.  There are num characters to clear
 
813
 */
 
814
protected void
 
815
term_clear_EOL(EditLine *el, int num)
 
816
{
 
817
        int i;
 
818
 
 
819
        if (EL_CAN_CEOL && GoodStr(T_ce))
 
820
                (void) tputs(Str(T_ce), 1, term__putc);
 
821
        else {
 
822
                for (i = 0; i < num; i++)
 
823
                        term__putc(' ');
 
824
                el->el_cursor.h += num; /* have written num spaces */
 
825
        }
 
826
}
 
827
 
 
828
 
 
829
/* term_clear_screen():
 
830
 *      Clear the screen
 
831
 */
 
832
protected void
 
833
term_clear_screen(EditLine *el)
 
834
{                               /* clear the whole screen and home */
 
835
 
 
836
        if (GoodStr(T_cl))
 
837
                /* send the clear screen code */
 
838
                (void) tputs(Str(T_cl), Val(T_li), term__putc);
 
839
        else if (GoodStr(T_ho) && GoodStr(T_cd)) {
 
840
                (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
 
841
                /* clear to bottom of screen */
 
842
                (void) tputs(Str(T_cd), Val(T_li), term__putc);
 
843
        } else {
 
844
                term__putc('\r');
 
845
                term__putc('\n');
 
846
        }
 
847
}
 
848
 
 
849
 
 
850
/* term_beep():
 
851
 *      Beep the way the terminal wants us
 
852
 */
 
853
protected void
 
854
term_beep(EditLine *el)
 
855
{
 
856
        if (GoodStr(T_bl))
 
857
                /* what termcap says we should use */
 
858
                (void) tputs(Str(T_bl), 1, term__putc);
 
859
        else
 
860
                term__putc('\007');     /* an ASCII bell; ^G */
 
861
}
 
862
 
 
863
 
 
864
#ifdef notdef
 
865
/* term_clear_to_bottom():
 
866
 *      Clear to the bottom of the screen
 
867
 */
 
868
protected void
 
869
term_clear_to_bottom(EditLine *el)
 
870
{
 
871
        if (GoodStr(T_cd))
 
872
                (void) tputs(Str(T_cd), Val(T_li), term__putc);
 
873
        else if (GoodStr(T_ce))
 
874
                (void) tputs(Str(T_ce), Val(T_li), term__putc);
 
875
}
 
876
#endif
 
877
 
 
878
protected void
 
879
term_get(EditLine *el, const char **term)
 
880
{
 
881
        *term = el->el_term.t_name;
 
882
}
 
883
 
 
884
 
 
885
/* term_set():
 
886
 *      Read in the terminal capabilities from the requested terminal
 
887
 */
 
888
protected int
 
889
term_set(EditLine *el, const char *term)
 
890
{
 
891
        int i;
 
892
        char buf[TC_BUFSIZE];
 
893
        char *area;
 
894
        const struct termcapstr *t;
 
895
        sigset_t oset, nset;
 
896
        int lins, cols;
 
897
 
 
898
        (void) sigemptyset(&nset);
 
899
        (void) sigaddset(&nset, SIGWINCH);
 
900
        (void) sigprocmask(SIG_BLOCK, &nset, &oset);
 
901
 
 
902
        area = buf;
 
903
 
 
904
 
 
905
        if (term == NULL)
 
906
                term = getenv("TERM");
 
907
 
 
908
        if (!term || !term[0])
 
909
                term = "dumb";
 
910
 
 
911
        if (strcmp(term, "emacs") == 0)
 
912
                el->el_flags |= EDIT_DISABLED;
 
913
 
 
914
        memset(el->el_term.t_cap, 0, TC_BUFSIZE);
 
915
 
 
916
        i = tgetent(el->el_term.t_cap, term);
 
917
 
 
918
        if (i <= 0) {
 
919
                if (i == -1)
 
920
                        (void) fprintf(el->el_errfile,
 
921
                            "Cannot read termcap database;\n");
 
922
                else if (i == 0)
 
923
                        (void) fprintf(el->el_errfile,
 
924
                            "No entry for terminal type \"%s\";\n", term);
 
925
                (void) fprintf(el->el_errfile,
 
926
                    "using dumb terminal settings.\n");
 
927
                Val(T_co) = 80; /* do a dumb terminal */
 
928
                Val(T_pt) = Val(T_km) = Val(T_li) = 0;
 
929
                Val(T_xt) = Val(T_MT);
 
930
                for (t = tstr; t->name != NULL; t++)
 
931
                        term_alloc(el, t, NULL);
 
932
        } else {
 
933
                /* auto/magic margins */
 
934
                Val(T_am) = tgetflag("am");
 
935
                Val(T_xn) = tgetflag("xn");
 
936
                /* Can we tab */
 
937
                Val(T_pt) = tgetflag("pt");
 
938
                Val(T_xt) = tgetflag("xt");
 
939
                /* do we have a meta? */
 
940
                Val(T_km) = tgetflag("km");
 
941
                Val(T_MT) = tgetflag("MT");
 
942
                /* Get the size */
 
943
                Val(T_co) = tgetnum("co");
 
944
                Val(T_li) = tgetnum("li");
 
945
                for (t = tstr; t->name != NULL; t++) {
 
946
                        /* XXX: some systems' tgetstr needs non const */
 
947
                        term_alloc(el, t, tgetstr(strchr(t->name, *t->name),
 
948
                            &area));
 
949
                }
 
950
        }
 
951
 
 
952
        if (Val(T_co) < 2)
 
953
                Val(T_co) = 80; /* just in case */
 
954
        if (Val(T_li) < 1)
 
955
                Val(T_li) = 24;
 
956
 
 
957
        el->el_term.t_size.v = Val(T_co);
 
958
        el->el_term.t_size.h = Val(T_li);
 
959
 
 
960
        term_setflags(el);
 
961
 
 
962
                                /* get the correct window size */
 
963
        (void) term_get_size(el, &lins, &cols);
 
964
        if (term_change_size(el, lins, cols) == -1)
 
965
                return (-1);
 
966
        (void) sigprocmask(SIG_SETMASK, &oset, NULL);
 
967
        term_bind_arrow(el);
 
968
        el->el_term.t_name = term;
 
969
        return (i <= 0 ? -1 : 0);
 
970
}
 
971
 
 
972
 
 
973
/* term_get_size():
 
974
 *      Return the new window size in lines and cols, and
 
975
 *      true if the size was changed.
 
976
 */
 
977
protected int
 
978
term_get_size(EditLine *el, int *lins, int *cols)
 
979
{
 
980
 
 
981
        *cols = Val(T_co);
 
982
        *lins = Val(T_li);
 
983
 
 
984
#ifdef TIOCGWINSZ
 
985
        {
 
986
                struct winsize ws;
 
987
                if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
 
988
                        if (ws.ws_col)
 
989
                                *cols = ws.ws_col;
 
990
                        if (ws.ws_row)
 
991
                                *lins = ws.ws_row;
 
992
                }
 
993
        }
 
994
#endif
 
995
#ifdef TIOCGSIZE
 
996
        {
 
997
                struct ttysize ts;
 
998
                if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
 
999
                        if (ts.ts_cols)
 
1000
                                *cols = ts.ts_cols;
 
1001
                        if (ts.ts_lines)
 
1002
                                *lins = ts.ts_lines;
 
1003
                }
 
1004
        }
 
1005
#endif
 
1006
        return (Val(T_co) != *cols || Val(T_li) != *lins);
 
1007
}
 
1008
 
 
1009
 
 
1010
/* term_change_size():
 
1011
 *      Change the size of the terminal
 
1012
 */
 
1013
protected int
 
1014
term_change_size(EditLine *el, int lins, int cols)
 
1015
{
 
1016
        /*
 
1017
         * Just in case
 
1018
         */
 
1019
        Val(T_co) = (cols < 2) ? 80 : cols;
 
1020
        Val(T_li) = (lins < 1) ? 24 : lins;
 
1021
 
 
1022
        /* re-make display buffers */
 
1023
        if (term_rebuffer_display(el) == -1)
 
1024
                return (-1);
 
1025
        re_clear_display(el);
 
1026
        return (0);
 
1027
}
 
1028
 
 
1029
 
 
1030
/* term_init_arrow():
 
1031
 *      Initialize the arrow key bindings from termcap
 
1032
 */
 
1033
private void
 
1034
term_init_arrow(EditLine *el)
 
1035
{
 
1036
        fkey_t *arrow = el->el_term.t_fkey;
 
1037
 
 
1038
        arrow[A_K_DN].name = "down";
 
1039
        arrow[A_K_DN].key = T_kd;
 
1040
        arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
 
1041
        arrow[A_K_DN].type = XK_CMD;
 
1042
 
 
1043
        arrow[A_K_UP].name = "up";
 
1044
        arrow[A_K_UP].key = T_ku;
 
1045
        arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
 
1046
        arrow[A_K_UP].type = XK_CMD;
 
1047
 
 
1048
        arrow[A_K_LT].name = "left";
 
1049
        arrow[A_K_LT].key = T_kl;
 
1050
        arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
 
1051
        arrow[A_K_LT].type = XK_CMD;
 
1052
 
 
1053
        arrow[A_K_RT].name = "right";
 
1054
        arrow[A_K_RT].key = T_kr;
 
1055
        arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
 
1056
        arrow[A_K_RT].type = XK_CMD;
 
1057
 
 
1058
        arrow[A_K_HO].name = "home";
 
1059
        arrow[A_K_HO].key = T_kh;
 
1060
        arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
 
1061
        arrow[A_K_HO].type = XK_CMD;
 
1062
 
 
1063
        arrow[A_K_EN].name = "end";
 
1064
        arrow[A_K_EN].key = T_at7;
 
1065
        arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
 
1066
        arrow[A_K_EN].type = XK_CMD;
 
1067
}
 
1068
 
 
1069
 
 
1070
/* term_reset_arrow():
 
1071
 *      Reset arrow key bindings
 
1072
 */
 
1073
private void
 
1074
term_reset_arrow(EditLine *el)
 
1075
{
 
1076
        fkey_t *arrow = el->el_term.t_fkey;
 
1077
        static const char strA[] = {033, '[', 'A', '\0'};
 
1078
        static const char strB[] = {033, '[', 'B', '\0'};
 
1079
        static const char strC[] = {033, '[', 'C', '\0'};
 
1080
        static const char strD[] = {033, '[', 'D', '\0'};
 
1081
        static const char strH[] = {033, '[', 'H', '\0'};
 
1082
        static const char strF[] = {033, '[', 'F', '\0'};
 
1083
        static const char stOA[] = {033, 'O', 'A', '\0'};
 
1084
        static const char stOB[] = {033, 'O', 'B', '\0'};
 
1085
        static const char stOC[] = {033, 'O', 'C', '\0'};
 
1086
        static const char stOD[] = {033, 'O', 'D', '\0'};
 
1087
        static const char stOH[] = {033, 'O', 'H', '\0'};
 
1088
        static const char stOF[] = {033, 'O', 'F', '\0'};
 
1089
 
 
1090
        key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
 
1091
        key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
 
1092
        key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
 
1093
        key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
 
1094
        key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
 
1095
        key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
 
1096
        key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
 
1097
        key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
 
1098
        key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
 
1099
        key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
 
1100
        key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
 
1101
        key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
 
1102
 
 
1103
        if (el->el_map.type == MAP_VI) {
 
1104
                key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
 
1105
                key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
 
1106
                key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
 
1107
                key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
 
1108
                key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
 
1109
                key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
 
1110
                key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
 
1111
                key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
 
1112
                key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
 
1113
                key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
 
1114
                key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
 
1115
                key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
 
1116
        }
 
1117
}
 
1118
 
 
1119
 
 
1120
/* term_set_arrow():
 
1121
 *      Set an arrow key binding
 
1122
 */
 
1123
protected int
 
1124
term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
 
1125
{
 
1126
        fkey_t *arrow = el->el_term.t_fkey;
 
1127
        int i;
 
1128
 
 
1129
        for (i = 0; i < A_K_NKEYS; i++)
 
1130
                if (strcmp(name, arrow[i].name) == 0) {
 
1131
                        arrow[i].fun = *fun;
 
1132
                        arrow[i].type = type;
 
1133
                        return (0);
 
1134
                }
 
1135
        return (-1);
 
1136
}
 
1137
 
 
1138
 
 
1139
/* term_clear_arrow():
 
1140
 *      Clear an arrow key binding
 
1141
 */
 
1142
protected int
 
1143
term_clear_arrow(EditLine *el, const char *name)
 
1144
{
 
1145
        fkey_t *arrow = el->el_term.t_fkey;
 
1146
        int i;
 
1147
 
 
1148
        for (i = 0; i < A_K_NKEYS; i++)
 
1149
                if (strcmp(name, arrow[i].name) == 0) {
 
1150
                        arrow[i].type = XK_NOD;
 
1151
                        return (0);
 
1152
                }
 
1153
        return (-1);
 
1154
}
 
1155
 
 
1156
 
 
1157
/* term_print_arrow():
 
1158
 *      Print the arrow key bindings
 
1159
 */
 
1160
protected void
 
1161
term_print_arrow(EditLine *el, const char *name)
 
1162
{
 
1163
        int i;
 
1164
        fkey_t *arrow = el->el_term.t_fkey;
 
1165
 
 
1166
        for (i = 0; i < A_K_NKEYS; i++)
 
1167
                if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
 
1168
                        if (arrow[i].type != XK_NOD)
 
1169
                                key_kprint(el, arrow[i].name, &arrow[i].fun,
 
1170
                                    arrow[i].type);
 
1171
}
 
1172
 
 
1173
 
 
1174
/* term_bind_arrow():
 
1175
 *      Bind the arrow keys
 
1176
 */
 
1177
protected void
 
1178
term_bind_arrow(EditLine *el)
 
1179
{
 
1180
        el_action_t *map;
 
1181
        const el_action_t *dmap;
 
1182
        int i, j;
 
1183
        char *p;
 
1184
        fkey_t *arrow = el->el_term.t_fkey;
 
1185
 
 
1186
        /* Check if the components needed are initialized */
 
1187
        if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
 
1188
                return;
 
1189
 
 
1190
        map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
 
1191
        dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
 
1192
 
 
1193
        term_reset_arrow(el);
 
1194
 
 
1195
        for (i = 0; i < A_K_NKEYS; i++) {
 
1196
                p = el->el_term.t_str[arrow[i].key];
 
1197
                if (p && *p) {
 
1198
                        j = (unsigned char) *p;
 
1199
                        /*
 
1200
                         * Assign the arrow keys only if:
 
1201
                         *
 
1202
                         * 1. They are multi-character arrow keys and the user
 
1203
                         *    has not re-assigned the leading character, or
 
1204
                         *    has re-assigned the leading character to be
 
1205
                         *        ED_SEQUENCE_LEAD_IN
 
1206
                         * 2. They are single arrow keys pointing to an
 
1207
                         *    unassigned key.
 
1208
                         */
 
1209
                        if (arrow[i].type == XK_NOD)
 
1210
                                key_clear(el, map, p);
 
1211
                        else {
 
1212
                                if (p[1] && (dmap[j] == map[j] ||
 
1213
                                        map[j] == ED_SEQUENCE_LEAD_IN)) {
 
1214
                                        key_add(el, p, &arrow[i].fun,
 
1215
                                            arrow[i].type);
 
1216
                                        map[j] = ED_SEQUENCE_LEAD_IN;
 
1217
                                } else if (map[j] == ED_UNASSIGNED) {
 
1218
                                        key_clear(el, map, p);
 
1219
                                        if (arrow[i].type == XK_CMD)
 
1220
                                                map[j] = arrow[i].fun.cmd;
 
1221
                                        else
 
1222
                                                key_add(el, p, &arrow[i].fun,
 
1223
                                                    arrow[i].type);
 
1224
                                }
 
1225
                        }
 
1226
                }
 
1227
        }
 
1228
}
 
1229
 
 
1230
 
 
1231
/* term__putc():
 
1232
 *      Add a character
 
1233
 */
 
1234
protected int
 
1235
term__putc(int c)
 
1236
{
 
1237
 
 
1238
        return (fputc(c, term_outfile));
 
1239
}
 
1240
 
 
1241
 
 
1242
/* term__flush():
 
1243
 *      Flush output
 
1244
 */
 
1245
protected void
 
1246
term__flush(void)
 
1247
{
 
1248
 
 
1249
        (void) fflush(term_outfile);
 
1250
}
 
1251
 
 
1252
 
 
1253
/* term_telltc():
 
1254
 *      Print the current termcap characteristics
 
1255
 */
 
1256
protected int
 
1257
/*ARGSUSED*/
 
1258
term_telltc(EditLine *el, int argc __attribute__((__unused__)), 
 
1259
    const char **argv __attribute__((__unused__)))
 
1260
{
 
1261
        const struct termcapstr *t;
 
1262
        char **ts;
 
1263
        char upbuf[EL_BUFSIZ];
 
1264
 
 
1265
        (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
 
1266
        (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
 
1267
        (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
 
1268
            Val(T_co), Val(T_li));
 
1269
        (void) fprintf(el->el_outfile,
 
1270
            "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
 
1271
        (void) fprintf(el->el_outfile,
 
1272
            "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
 
1273
        (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
 
1274
            EL_HAS_AUTO_MARGINS ? "has" : "does not have");
 
1275
        if (EL_HAS_AUTO_MARGINS)
 
1276
                (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
 
1277
                    EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
 
1278
 
 
1279
        for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
 
1280
                (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
 
1281
                    t->long_name,
 
1282
                    t->name, *ts && **ts ?
 
1283
                    key__decode_str(*ts, upbuf, "") : "(empty)");
 
1284
        (void) fputc('\n', el->el_outfile);
 
1285
        return (0);
 
1286
}
 
1287
 
 
1288
 
 
1289
/* term_settc():
 
1290
 *      Change the current terminal characteristics
 
1291
 */
 
1292
protected int
 
1293
/*ARGSUSED*/
 
1294
term_settc(EditLine *el, int argc __attribute__((__unused__)),
 
1295
    const char **argv)
 
1296
{
 
1297
        const struct termcapstr *ts;
 
1298
        const struct termcapval *tv;
 
1299
        const char *what, *how;
 
1300
 
 
1301
        if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
 
1302
                return (-1);
 
1303
 
 
1304
        what = argv[1];
 
1305
        how = argv[2];
 
1306
 
 
1307
        /*
 
1308
         * Do the strings first
 
1309
         */
 
1310
        for (ts = tstr; ts->name != NULL; ts++)
 
1311
                if (strcmp(ts->name, what) == 0)
 
1312
                        break;
 
1313
 
 
1314
        if (ts->name != NULL) {
 
1315
                term_alloc(el, ts, how);
 
1316
                term_setflags(el);
 
1317
                return (0);
 
1318
        }
 
1319
        /*
 
1320
         * Do the numeric ones second
 
1321
         */
 
1322
        for (tv = tval; tv->name != NULL; tv++)
 
1323
                if (strcmp(tv->name, what) == 0)
 
1324
                        break;
 
1325
 
 
1326
        if (tv->name != NULL) {
 
1327
                if (tv == &tval[T_pt] || tv == &tval[T_km] ||
 
1328
                    tv == &tval[T_am] || tv == &tval[T_xn]) {
 
1329
                        if (strcmp(how, "yes") == 0)
 
1330
                                el->el_term.t_val[tv - tval] = 1;
 
1331
                        else if (strcmp(how, "no") == 0)
 
1332
                                el->el_term.t_val[tv - tval] = 0;
 
1333
                        else {
 
1334
                                (void) fprintf(el->el_errfile,
 
1335
                                    "settc: Bad value `%s'.\n", how);
 
1336
                                return (-1);
 
1337
                        }
 
1338
                        term_setflags(el);
 
1339
                        if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
 
1340
                                return (-1);
 
1341
                        return (0);
 
1342
                } else {
 
1343
                        long i;
 
1344
                        char *ep;
 
1345
 
 
1346
                        i = strtol(how, &ep, 10);
 
1347
                        if (*ep != '\0') {
 
1348
                                (void) fprintf(el->el_errfile,
 
1349
                                    "settc: Bad value `%s'.\n", how);
 
1350
                                return (-1);
 
1351
                        }
 
1352
                        el->el_term.t_val[tv - tval] = (int) i;
 
1353
                        el->el_term.t_size.v = Val(T_co);
 
1354
                        el->el_term.t_size.h = Val(T_li);
 
1355
                        if (tv == &tval[T_co] || tv == &tval[T_li])
 
1356
                                if (term_change_size(el, Val(T_li), Val(T_co))
 
1357
                                    == -1)
 
1358
                                        return (-1);
 
1359
                        return (0);
 
1360
                }
 
1361
        }
 
1362
        return (-1);
 
1363
}
 
1364
 
 
1365
 
 
1366
/* term_echotc():
 
1367
 *      Print the termcap string out with variable substitution
 
1368
 */
 
1369
protected int
 
1370
/*ARGSUSED*/
 
1371
term_echotc(EditLine *el, int argc __attribute__((__unused__)),
 
1372
    const char **argv)
 
1373
{
 
1374
        char *cap, *scap, *ep;
 
1375
        int arg_need, arg_cols, arg_rows;
 
1376
        int verbose = 0, silent = 0;
 
1377
        char *area;
 
1378
        static const char fmts[] = "%s\n", fmtd[] = "%d\n";
 
1379
        const struct termcapstr *t;
 
1380
        char buf[TC_BUFSIZE];
 
1381
        long i;
 
1382
 
 
1383
        area = buf;
 
1384
 
 
1385
        if (argv == NULL || argv[1] == NULL)
 
1386
                return (-1);
 
1387
        argv++;
 
1388
 
 
1389
        if (argv[0][0] == '-') {
 
1390
                switch (argv[0][1]) {
 
1391
                case 'v':
 
1392
                        verbose = 1;
 
1393
                        break;
 
1394
                case 's':
 
1395
                        silent = 1;
 
1396
                        break;
 
1397
                default:
 
1398
                        /* stderror(ERR_NAME | ERR_TCUSAGE); */
 
1399
                        break;
 
1400
                }
 
1401
                argv++;
 
1402
        }
 
1403
        if (!*argv || *argv[0] == '\0')
 
1404
                return (0);
 
1405
        if (strcmp(*argv, "tabs") == 0) {
 
1406
                (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
 
1407
                return (0);
 
1408
        } else if (strcmp(*argv, "meta") == 0) {
 
1409
                (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
 
1410
                return (0);
 
1411
        } else if (strcmp(*argv, "xn") == 0) {
 
1412
                (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
 
1413
                    "yes" : "no");
 
1414
                return (0);
 
1415
        } else if (strcmp(*argv, "am") == 0) {
 
1416
                (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
 
1417
                    "yes" : "no");
 
1418
                return (0);
 
1419
        } else if (strcmp(*argv, "baud") == 0) {
 
1420
#ifdef notdef
 
1421
                int i;
 
1422
 
 
1423
                for (i = 0; baud_rate[i].b_name != NULL; i++)
 
1424
                        if (el->el_tty.t_speed == baud_rate[i].b_rate) {
 
1425
                                (void) fprintf(el->el_outfile, fmts,
 
1426
                                    baud_rate[i].b_name);
 
1427
                                return (0);
 
1428
                        }
 
1429
                (void) fprintf(el->el_outfile, fmtd, 0);
 
1430
#else
 
1431
                (void) fprintf(el->el_outfile, fmtd, (int)el->el_tty.t_speed);
 
1432
#endif
 
1433
                return (0);
 
1434
        } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
 
1435
                (void) fprintf(el->el_outfile, fmtd, Val(T_li));
 
1436
                return (0);
 
1437
        } else if (strcmp(*argv, "cols") == 0) {
 
1438
                (void) fprintf(el->el_outfile, fmtd, Val(T_co));
 
1439
                return (0);
 
1440
        }
 
1441
        /*
 
1442
         * Try to use our local definition first
 
1443
         */
 
1444
        scap = NULL;
 
1445
        for (t = tstr; t->name != NULL; t++)
 
1446
                if (strcmp(t->name, *argv) == 0) {
 
1447
                        scap = el->el_term.t_str[t - tstr];
 
1448
                        break;
 
1449
                }
 
1450
        if (t->name == NULL) {
 
1451
                /* XXX: some systems' tgetstr needs non const */
 
1452
                scap = tgetstr(strchr(*argv, **argv), &area);
 
1453
        }
 
1454
        if (!scap || scap[0] == '\0') {
 
1455
                if (!silent)
 
1456
                        (void) fprintf(el->el_errfile,
 
1457
                            "echotc: Termcap parameter `%s' not found.\n",
 
1458
                            *argv);
 
1459
                return (-1);
 
1460
        }
 
1461
        /*
 
1462
         * Count home many values we need for this capability.
 
1463
         */
 
1464
        for (cap = scap, arg_need = 0; *cap; cap++)
 
1465
                if (*cap == '%')
 
1466
                        switch (*++cap) {
 
1467
                        case 'd':
 
1468
                        case '2':
 
1469
                        case '3':
 
1470
                        case '.':
 
1471
                        case '+':
 
1472
                                arg_need++;
 
1473
                                break;
 
1474
                        case '%':
 
1475
                        case '>':
 
1476
                        case 'i':
 
1477
                        case 'r':
 
1478
                        case 'n':
 
1479
                        case 'B':
 
1480
                        case 'D':
 
1481
                                break;
 
1482
                        default:
 
1483
                                /*
 
1484
                                 * hpux has lot's of them...
 
1485
                                 */
 
1486
                                if (verbose)
 
1487
                                        (void) fprintf(el->el_errfile,
 
1488
                                "echotc: Warning: unknown termcap %% `%c'.\n",
 
1489
                                            *cap);
 
1490
                                /* This is bad, but I won't complain */
 
1491
                                break;
 
1492
                        }
 
1493
 
 
1494
        switch (arg_need) {
 
1495
        case 0:
 
1496
                argv++;
 
1497
                if (*argv && *argv[0]) {
 
1498
                        if (!silent)
 
1499
                                (void) fprintf(el->el_errfile,
 
1500
                                    "echotc: Warning: Extra argument `%s'.\n",
 
1501
                                    *argv);
 
1502
                        return (-1);
 
1503
                }
 
1504
                (void) tputs(scap, 1, term__putc);
 
1505
                break;
 
1506
        case 1:
 
1507
                argv++;
 
1508
                if (!*argv || *argv[0] == '\0') {
 
1509
                        if (!silent)
 
1510
                                (void) fprintf(el->el_errfile,
 
1511
                                    "echotc: Warning: Missing argument.\n");
 
1512
                        return (-1);
 
1513
                }
 
1514
                arg_cols = 0;
 
1515
                i = strtol(*argv, &ep, 10);
 
1516
                if (*ep != '\0' || i < 0) {
 
1517
                        if (!silent)
 
1518
                                (void) fprintf(el->el_errfile,
 
1519
                                    "echotc: Bad value `%s' for rows.\n",
 
1520
                                    *argv);
 
1521
                        return (-1);
 
1522
                }
 
1523
                arg_rows = (int) i;
 
1524
                argv++;
 
1525
                if (*argv && *argv[0]) {
 
1526
                        if (!silent)
 
1527
                                (void) fprintf(el->el_errfile,
 
1528
                                    "echotc: Warning: Extra argument `%s'.\n",
 
1529
                                    *argv);
 
1530
                        return (-1);
 
1531
                }
 
1532
                (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
 
1533
                break;
 
1534
        default:
 
1535
                /* This is wrong, but I will ignore it... */
 
1536
                if (verbose)
 
1537
                        (void) fprintf(el->el_errfile,
 
1538
                         "echotc: Warning: Too many required arguments (%d).\n",
 
1539
                            arg_need);
 
1540
                /* FALLTHROUGH */
 
1541
        case 2:
 
1542
                argv++;
 
1543
                if (!*argv || *argv[0] == '\0') {
 
1544
                        if (!silent)
 
1545
                                (void) fprintf(el->el_errfile,
 
1546
                                    "echotc: Warning: Missing argument.\n");
 
1547
                        return (-1);
 
1548
                }
 
1549
                i = strtol(*argv, &ep, 10);
 
1550
                if (*ep != '\0' || i < 0) {
 
1551
                        if (!silent)
 
1552
                                (void) fprintf(el->el_errfile,
 
1553
                                    "echotc: Bad value `%s' for cols.\n",
 
1554
                                    *argv);
 
1555
                        return (-1);
 
1556
                }
 
1557
                arg_cols = (int) i;
 
1558
                argv++;
 
1559
                if (!*argv || *argv[0] == '\0') {
 
1560
                        if (!silent)
 
1561
                                (void) fprintf(el->el_errfile,
 
1562
                                    "echotc: Warning: Missing argument.\n");
 
1563
                        return (-1);
 
1564
                }
 
1565
                i = strtol(*argv, &ep, 10);
 
1566
                if (*ep != '\0' || i < 0) {
 
1567
                        if (!silent)
 
1568
                                (void) fprintf(el->el_errfile,
 
1569
                                    "echotc: Bad value `%s' for rows.\n",
 
1570
                                    *argv);
 
1571
                        return (-1);
 
1572
                }
 
1573
                arg_rows = (int) i;
 
1574
                if (*ep != '\0') {
 
1575
                        if (!silent)
 
1576
                                (void) fprintf(el->el_errfile,
 
1577
                                    "echotc: Bad value `%s'.\n", *argv);
 
1578
                        return (-1);
 
1579
                }
 
1580
                argv++;
 
1581
                if (*argv && *argv[0]) {
 
1582
                        if (!silent)
 
1583
                                (void) fprintf(el->el_errfile,
 
1584
                                    "echotc: Warning: Extra argument `%s'.\n",
 
1585
                                    *argv);
 
1586
                        return (-1);
 
1587
                }
 
1588
                (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,
 
1589
                    term__putc);
 
1590
                break;
 
1591
        }
 
1592
        return (0);
 
1593
}