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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "maps.h"
#include "string.h"
#include "macros.h"
#include "color.h"
#include "conf.h"
#include "sc.h"
#include "block.h"
#include "utils/string.h"
static map * maps;
static int mapdepth = 0;
int len_maps = 0;
int replace_maps (struct block * b) {
int r = 0;
if (++mapdepth == 1000) {
sc_error("recursive mapping");
flush_buf(b);
mapdepth = 0;
return 0;
}
// Traverse session mappings
map * m = maps;
while (m != NULL) {
// Check if a mapping already exists in 'b' buffer
int pos = block_in_block(b, m->in);
if (pos != -1 && m->mode == curmode) {
// Replace m->in with m->out in 'b' list
if (replace_block_in_block(b, m->in, m->out) == -1) {
sc_error("error replacing maps");
return -1;
}
r = 1;
break;
}
m = m->psig;
}
if (r && m->recursive) replace_maps(b); // recursive mapping here!
return r;
}
// create list of blocks based on map strings
struct block * get_mapbuf_str (char * str) {
struct block * buffer = create_buf();
unsigned short l = strlen(str);
unsigned short i, j;
unsigned short is_specialkey = 0;
char sk[MAXSC+1];
sk[0] = '\0';
for (i=0; i<l; i++) {
// Add special keys
if (str[i] == '<') {
is_specialkey = 1;
} else if (str[i] == '>') {
is_specialkey = 0;
if (! strcmp(sk, "CR")) // CR - ENTER key
addto_buf(buffer, OKEY_ENTER);
else if (! strcmp(sk, "TAB")) // TAB
addto_buf(buffer, OKEY_TAB);
else if (! strcmp(sk, "LEFT")) // LEFT
addto_buf(buffer, OKEY_LEFT);
else if (! strcmp(sk, "RIGHT")) // RIGHT
addto_buf(buffer, OKEY_RIGHT);
else if (! strcmp(sk, "DOWN")) // DOWN
addto_buf(buffer, OKEY_DOWN);
else if (! strcmp(sk, "UP")) // UP
addto_buf(buffer, OKEY_UP);
else if (! strcmp(sk, "DEL")) // DEL
addto_buf(buffer, OKEY_DEL);
else if (! strcmp(sk, "BS")) // BS
addto_buf(buffer, OKEY_BS);
else if (! strcmp(sk, "HOME")) // HOME
addto_buf(buffer, OKEY_HOME);
else if (! strcmp(sk, "END")) // END
addto_buf(buffer, OKEY_END);
else if (! strcmp(sk, "PGDOWN")) // PGDOWN
addto_buf(buffer, OKEY_PGDOWN);
else if (! strcmp(sk, "PGUP")) // PGUP
addto_buf(buffer, OKEY_PGUP);
else if (! strncmp(sk, "C-", 2) && strlen(sk) == 3 // C-X
&& ( (sk[2] > 64 && sk[2] < 91) || (sk[2] > 96 && sk[2] < 123)) )
addto_buf(buffer, ctl(tolower(sk[2])));
sk[0]='\0';
} else if (is_specialkey && strlen(sk) < MAXSC-1) {
add_char(sk, str[i], strlen(sk));
// Add some other characters
} else {
addto_buf(buffer, (int) str[i]);
}
}
// If the buffer lacks the trailing '>', insert it
if (is_specialkey && i == l) {
j = strlen(sk);
addto_buf(buffer, '<');
for (i=0; i<j; i++) addto_buf(buffer, (int) str[l-j+i]);
}
return buffer;
}
// Remove mappings and free corresponding memory
void del_maps () {
map * m = maps;
map * e = m;
while (m != NULL) {
e = m->psig;
erase_buf(m->out);
erase_buf(m->in);
free(m);
m = e;
}
return;
}
// Returns the las mapping of the current session
map * get_last_map() {
map * m = maps;
map * e = m;
while (m != NULL) {
e = m;
m = m->psig;
}
return e;
}
// Returns the position of a mapping for some mode if it already exists, -1
// otherwise
int exists_map(char * in, int mode) {
map * m = maps;
char str_in[MAXMAPITEM] = "";
int pos = -1;
while (m != NULL) {
pos++;
get_mapstr_buf(m->in, str_in);
if ( ! strcmp(in, str_in) && m->mode == mode) {
return pos;
}
m = m->psig;
}
return -1;
}
// Append a mapping to the current session
void add_map(char * in, char * out, int mode, short recursive) {
map * m;
// If the mapping already exists, replace its content, saving it position
int exists = exists_map(in, mode);
if (exists == -1) {
m = (map *) malloc (sizeof(map));
} else {
m = maps;
while (exists--) m = m->psig;
erase_buf(m->in);
erase_buf(m->out);
exists = TRUE;
}
m->out = (struct block *) get_mapbuf_str(out);
m->in = (struct block *) get_mapbuf_str(in);
m->mode = mode;
m->recursive = recursive;
if (exists == TRUE) return; // in case a map was updated and not created!
// Insert at the beginning
// m->psig = maps == NULL ? NULL : maps;
// maps = m;
// Insert at the end
m->psig= NULL;
if (maps == NULL) maps = m;
else
((map *) get_last_map())->psig = m;
len_maps++;
return;
}
// Remove a mapping from a specific MODE
void del_map(char * in, int mode) {
map * ant;
map * m = maps;
if (m == NULL) return;
// If the node is the fist one
char strin [MAXSC * get_bufsize(m->in)];
get_mapstr_buf(m->in, strin);
if ( ! strcmp(in, strin) && mode == m->mode) {
maps = m->psig;
erase_buf(m->out);
erase_buf(m->in);
free(m);
len_maps--;
return;
}
// If the node is in the middle or at the end
ant = m;
m = m->psig;
while (m != NULL) {
char strin [MAXSC * get_bufsize(m->in)];
get_mapstr_buf(m->in, strin);
if ( ! strcmp(in, strin) && mode == m->mode)<
|