1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 | /*
* Copyright (c) 2013, 2014, 2016, 2024 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#define MAX_TERMSEQ_SIZE 16
struct editor_input
{
mbstate_t ps;
char termseq[MAX_TERMSEQ_SIZE];
size_t termseq_used;
size_t termseq_seen;
};
struct terminal_sequence
{
const char* sequence;
int kbkey;
bool control;
bool shift;
};
#define KBKEY_UP (0x80 + 0x48)
struct terminal_sequence terminal_sequences[] =
{
{ "\e[1;2A", KBKEY_UP, false, true },
};
void editor_emulate_kbkey(int kbkey,
bool control,
bool shift);
void editor_input_process_byte(struct editor_input* editor_input)
{
unsigned char uc;
if ( read(0, &uc, 1) != 1 )
return;
if ( editor_input->termseq_used < MAX_TERMSEQ_SIZE )
// gcc -std=gnu11 -O2 -g -Werror -Werror=strict-prototypes -Wall -Wextra -c repro.c -o repro.o -D_GNU_SOURCE
// repro.c:61:71: error: dangling pointer to ‘uc’ may be used [-Werror=dangling-pointer=]
editor_input->termseq[editor_input->termseq_used++] = (char) uc;
// ^~~~~~~~~
size_t num_seqs = sizeof(terminal_sequences) / sizeof(terminal_sequences[0]);
while ( editor_input->termseq_seen < editor_input->termseq_used )
{
size_t match = 0;
size_t match_size = 0;
bool full_match = false;
bool partial_match = false;
for ( size_t i = 0; i < num_seqs; i++ )
{
struct terminal_sequence* terminal_sequence = &terminal_sequences[i];
const char* sequence = terminal_sequence->sequence;
bool potential_partial_match = false;
for ( size_t n = 0; n < editor_input->termseq_used; n++ )
{
if ( sequence[n] != editor_input->termseq[n] )
{
potential_partial_match = false;
break;
}
if ( sequence[n+1] == '\0' )
{
potential_partial_match = false;
full_match = true;
match = i;
match_size = n + 1;
break;
}
potential_partial_match = true;
}
if ( potential_partial_match )
partial_match = true;
}
if ( full_match )
{
editor_emulate_kbkey(terminal_sequences[match].kbkey,
terminal_sequences[match].control,
terminal_sequences[match].shift);
memmove(editor_input->termseq,
editor_input->termseq + match_size,
editor_input->termseq_used - match_size);
editor_input->termseq_used -= match_size;
editor_input->termseq_seen = 0;
continue;
}
if ( partial_match )
{
editor_input->termseq_seen = editor_input->termseq_used;
continue;
}
char input = editor_input->termseq[0];
if ( 1 <= input && input <= 26 && input != '\t' && input != '\n' )
{
}
else
{
wchar_t wc;
size_t amount = mbrtowc(&wc, &input, 1, &editor_input->ps);
if ( amount == (size_t) -1 )
memset(&editor_input->ps, 0, sizeof(editor_input->ps));
}
memmove(editor_input->termseq,
editor_input->termseq + 1,
editor_input->termseq_used - 1);
editor_input->termseq_used--;
editor_input->termseq_seen = 0;
}
}
void editor_input_process(struct editor_input* editor_input)
{
editor_input_process_byte(editor_input);
}
|