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