literal.c revision 1.3.4.2 1 1.3.4.2 snj /* $NetBSD: literal.c,v 1.3.4.2 2017/07/23 14:41:26 snj Exp $ */
2 1.3.4.2 snj
3 1.3.4.2 snj /*-
4 1.3.4.2 snj * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 1.3.4.2 snj * All rights reserved.
6 1.3.4.2 snj *
7 1.3.4.2 snj * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 snj * by Christos Zoulas.
9 1.3.4.2 snj *
10 1.3.4.2 snj * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 snj * modification, are permitted provided that the following conditions
12 1.3.4.2 snj * are met:
13 1.3.4.2 snj * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 snj * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 snj * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 snj * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 snj * documentation and/or other materials provided with the distribution.
18 1.3.4.2 snj *
19 1.3.4.2 snj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.3.4.2 snj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.3.4.2 snj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.3.4.2 snj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.3.4.2 snj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.3.4.2 snj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.3.4.2 snj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.3.4.2 snj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.3.4.2 snj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.3.4.2 snj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.3.4.2 snj * SUCH DAMAGE.
30 1.3.4.2 snj */
31 1.3.4.2 snj
32 1.3.4.2 snj #include "config.h"
33 1.3.4.2 snj #if !defined(lint) && !defined(SCCSID)
34 1.3.4.2 snj __RCSID("$NetBSD: literal.c,v 1.3.4.2 2017/07/23 14:41:26 snj Exp $");
35 1.3.4.2 snj #endif /* not lint && not SCCSID */
36 1.3.4.2 snj
37 1.3.4.2 snj /*
38 1.3.4.2 snj * literal.c: Literal sequences handling.
39 1.3.4.2 snj */
40 1.3.4.2 snj #include <assert.h>
41 1.3.4.2 snj #include <stdio.h>
42 1.3.4.2 snj #include <stdlib.h>
43 1.3.4.2 snj #include <string.h>
44 1.3.4.2 snj #include "el.h"
45 1.3.4.2 snj
46 1.3.4.2 snj libedit_private void
47 1.3.4.2 snj literal_init(EditLine *el)
48 1.3.4.2 snj {
49 1.3.4.2 snj el_literal_t *l = &el->el_literal;
50 1.3.4.2 snj
51 1.3.4.2 snj memset(l, 0, sizeof(*l));
52 1.3.4.2 snj }
53 1.3.4.2 snj
54 1.3.4.2 snj libedit_private void
55 1.3.4.2 snj literal_end(EditLine *el)
56 1.3.4.2 snj {
57 1.3.4.2 snj literal_clear(el);
58 1.3.4.2 snj }
59 1.3.4.2 snj
60 1.3.4.2 snj libedit_private void
61 1.3.4.2 snj literal_clear(EditLine *el)
62 1.3.4.2 snj {
63 1.3.4.2 snj el_literal_t *l = &el->el_literal;
64 1.3.4.2 snj size_t i;
65 1.3.4.2 snj
66 1.3.4.2 snj if (l->l_len == 0)
67 1.3.4.2 snj return;
68 1.3.4.2 snj
69 1.3.4.2 snj for (i = 0; i < l->l_idx; i++)
70 1.3.4.2 snj el_free(l->l_buf[i]);
71 1.3.4.2 snj el_free(l->l_buf);
72 1.3.4.2 snj l->l_buf = NULL;
73 1.3.4.2 snj l->l_len = 0;
74 1.3.4.2 snj l->l_idx = 0;
75 1.3.4.2 snj }
76 1.3.4.2 snj
77 1.3.4.2 snj libedit_private wint_t
78 1.3.4.2 snj literal_add(EditLine *el, const wchar_t *buf, const wchar_t *end, int *wp)
79 1.3.4.2 snj {
80 1.3.4.2 snj el_literal_t *l = &el->el_literal;
81 1.3.4.2 snj size_t i, len;
82 1.3.4.2 snj ssize_t w, n;
83 1.3.4.2 snj char *b;
84 1.3.4.2 snj
85 1.3.4.2 snj w = wcwidth(end[1]); /* column width of the visible char */
86 1.3.4.2 snj *wp = (int)w;
87 1.3.4.2 snj
88 1.3.4.2 snj if (w <= 0) /* we require something to be printed */
89 1.3.4.2 snj return 0;
90 1.3.4.2 snj
91 1.3.4.2 snj len = (size_t)(end - buf);
92 1.3.4.2 snj for (w = 0, i = 0; i < len; i++)
93 1.3.4.2 snj w += ct_enc_width(buf[i]);
94 1.3.4.2 snj w += ct_enc_width(end[1]);
95 1.3.4.2 snj
96 1.3.4.2 snj b = el_malloc((size_t)(w + 1));
97 1.3.4.2 snj if (b == NULL)
98 1.3.4.2 snj return 0;
99 1.3.4.2 snj
100 1.3.4.2 snj for (n = 0, i = 0; i < len; i++)
101 1.3.4.2 snj n += ct_encode_char(b + n, w - n, buf[i]);
102 1.3.4.2 snj n += ct_encode_char(b + n, w - n, end[1]);
103 1.3.4.2 snj b[n] = '\0';
104 1.3.4.2 snj
105 1.3.4.2 snj /*
106 1.3.4.2 snj * Then save this literal string in the list of such strings,
107 1.3.4.2 snj * and return a "magic character" to put into the terminal buffer.
108 1.3.4.2 snj * When that magic char is 'printed' the saved string (which includes
109 1.3.4.2 snj * the char that belongs in that position) gets sent instead.
110 1.3.4.2 snj */
111 1.3.4.2 snj if (l->l_idx == l->l_len) {
112 1.3.4.2 snj char **bp;
113 1.3.4.2 snj
114 1.3.4.2 snj l->l_len += 4;
115 1.3.4.2 snj bp = el_realloc(l->l_buf, sizeof(*l->l_buf) * l->l_len);
116 1.3.4.2 snj if (bp == NULL) {
117 1.3.4.2 snj free(b);
118 1.3.4.2 snj l->l_len -= 4;
119 1.3.4.2 snj return 0;
120 1.3.4.2 snj }
121 1.3.4.2 snj l->l_buf = bp;
122 1.3.4.2 snj }
123 1.3.4.2 snj l->l_buf[l->l_idx++] = b;
124 1.3.4.2 snj return EL_LITERAL | (wint_t)(l->l_idx - 1);
125 1.3.4.2 snj }
126 1.3.4.2 snj
127 1.3.4.2 snj libedit_private const char *
128 1.3.4.2 snj literal_get(EditLine *el, wint_t idx)
129 1.3.4.2 snj {
130 1.3.4.2 snj el_literal_t *l = &el->el_literal;
131 1.3.4.2 snj
132 1.3.4.2 snj assert(idx & EL_LITERAL);
133 1.3.4.2 snj idx &= ~EL_LITERAL;
134 1.3.4.2 snj assert(l->l_idx > (size_t)idx);
135 1.3.4.2 snj return l->l_buf[idx];
136 1.3.4.2 snj }
137