literal.c revision 1.2 1 /* $NetBSD: literal.c,v 1.2 2017/06/29 02:54:40 kre Exp $ */
2
3 /*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include "config.h"
33 #if !defined(lint) && !defined(SCCSID)
34 __RCSID("$NetBSD: literal.c,v 1.2 2017/06/29 02:54:40 kre Exp $");
35 #endif /* not lint && not SCCSID */
36
37 /*
38 * literal.c: Literal sequences handling.
39 */
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include "el.h"
45
46 libedit_private void
47 literal_init(EditLine *el)
48 {
49 el_literal_t *l = &el->el_literal;
50 memset(l, 0, sizeof(*l));
51 }
52
53 libedit_private void
54 literal_end(EditLine *el)
55 {
56 el_literal_t *l = &el->el_literal;
57 literal_clear(el);
58 el_free(l->l_buf);
59 }
60
61 libedit_private void
62 literal_clear(EditLine *el)
63 {
64 el_literal_t *l = &el->el_literal;
65 size_t i;
66 for (i = 0; i < l->l_idx; i++)
67 el_free(l->l_buf[i]);
68 l->l_len = 0;
69 l->l_idx = 0;
70 }
71
72 libedit_private wint_t
73 literal_add(EditLine *el, const wchar_t *buf, const wchar_t *end)
74 {
75 // XXX: Only for narrow chars now.
76 el_literal_t *l = &el->el_literal;
77 size_t i, len;
78 char *b;
79
80 len = (size_t)(end - buf);
81 b = el_malloc(len + 2);
82 if (b == NULL)
83 return 0;
84 for (i = 0; i < len; i++)
85 b[i] = (char)buf[i];
86 b[len] = (char)end[1];
87 b[len + 1] = '\0';
88 if (l->l_idx == l->l_len) {
89 l->l_len += 10;
90 char **bp = el_realloc(l->l_buf, sizeof(*l->l_buf) * l->l_len);
91 if (bp == NULL) {
92 free(b);
93 return 0;
94 }
95 l->l_buf = bp;
96 }
97 l->l_buf[l->l_idx++] = b;
98 return EL_LITERAL | (wint_t)(l->l_idx - 1);
99 }
100
101 libedit_private const char *
102 literal_get(EditLine *el, wint_t idx)
103 {
104 el_literal_t *l = &el->el_literal;
105 assert(idx & EL_LITERAL);
106 idx &= ~EL_LITERAL;
107 assert(l->l_idx > (size_t)idx);
108 return l->l_buf[idx];
109 }
110