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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
/* $Id: func.c,v 1.27 2003/09/26 17:59:51 ukai Exp $ */
/*
* w3m func.c
*/
#include <stdio.h>
#include "fm.h"
#include "func.h"
#include "myctype.h"
#include "regex.h"
#include "funcname.c"
#include "functable.c"
#define KEYDATA_HASH_SIZE 16
static Hash_iv *keyData = NULL;
static char keymap_initialized = FALSE;
static struct stat sys_current_keymap_file;
static struct stat current_keymap_file;
void
setKeymap(char *p, int lineno, int verbose)
{
unsigned char *map = NULL;
char *s, *emsg;
int c, f;
s = getQWord(&p);
c = getKey(s);
if (c < 0) { /* error */
if (lineno > 0)
/* FIXME: gettextize? */
emsg = Sprintf("line %d: unknown key '%s'", lineno, s)->ptr;
else
/* FIXME: gettextize? */
emsg = Sprintf("defkey: unknown key '%s'", s)->ptr;
record_err_message(emsg);
if (verbose)
disp_message_nsec(emsg, FALSE, 1, TRUE, FALSE);
return;
}
s = getWord(&p);
f = getFuncList(s);
if (f < 0) {
if (lineno > 0)
/* FIXME: gettextize? */
emsg = Sprintf("line %d: invalid command '%s'", lineno, s)->ptr;
else
/* FIXME: gettextize? */
emsg = Sprintf("defkey: invalid command '%s'", s)->ptr;
record_err_message(emsg);
if (verbose)
disp_message_nsec(emsg, FALSE, 1, TRUE, FALSE);
return;
}
if (c & K_MULTI) {
unsigned char **mmap = NULL;
int i, j, m = MULTI_KEY(c);
if (m & K_ESCD)
map = EscDKeymap;
else if (m & K_ESCB)
map = EscBKeymap;
else if (m & K_ESC)
map = EscKeymap;
else
map = GlobalKeymap;
if (map[m & 0x7F] == FUNCNAME_multimap)
mmap = (unsigned char **)getKeyData(m);
else
map[m & 0x7F] = FUNCNAME_multimap;
if (!mmap) {
mmap = New_N(unsigned char *, 4);
for (i = 0; i < 4; i++) {
mmap[i] = New_N(unsigned char, 128);
for (j = 0; j < 128; j++)
mmap[i][j] = FUNCNAME_nulcmd;
}
mmap[0][ESC_CODE] = FUNCNAME_escmap;
mmap[1]['['] = FUNCNAME_escbmap;
mmap[1]['O'] = FUNCNAME_escbmap;
}
if (keyData == NULL)
keyData = newHash_iv(KEYDATA_HASH_SIZE);
putHash_iv(keyData, m, (void *)mmap);
if (c & K_ESCD)
map = mmap[3];
else if (c & K_ESCB)
map = mmap[2];
else if (c & K_ESC)
map = mmap[1];
else
map = mmap[0];
}
else {
if (c & K_ESCD)
map = EscDKeymap;
else if (c & K_ESCB)
map = EscBKeymap;
else if (c & K_ESC)
map = EscKeymap;
else
map = GlobalKeymap;
}
map[c & 0x7F] = f;
s = getQWord(&p);
if (*s) {
if (keyData == NULL)
keyData = newHash_iv(KEYDATA_HASH_SIZE);
putHash_iv(keyData, c, (void *)s);
}
else if (getKeyData(c))
putHash_iv(keyData, c, NULL);
}
static void
interpret_keymap(FILE * kf, struct stat *current, int force)
{
int fd;
struct stat kstat;
Str line;
char *p, *s, *emsg;
int lineno;
#ifdef USE_M17N
wc_ces charset = SystemCharset;
#endif
int verbose = 1;
extern int str_to_bool(char *value, int old);
if ((fd = fileno(kf)) < 0 || fstat(fd, &kstat) ||
(!force &&
kstat.st_mtime == current->st_mtime &&
kstat.st_dev == current->st_dev &&
kstat.st_ino == current->st_ino && kstat.st_size == current->st_size))
return;
*current = kstat;
lineno = 0;
while (!feof(kf)) {
line = Strfgets(kf);
lineno++;
Strchop(line);
Strremovefirstspaces(line);
if (line->length == 0)
continue;
#ifdef USE_M17N
line = wc_Str_conv(line, charset, InnerCharset);
#endif
p = line->ptr;
s = getWord(&p);
if (*s == '#') /* comment */
continue;
if (!strcmp(s, "keymap")) ;
#ifdef USE_M17N
else if (!strcmp(s, "charset") || !strcmp(s, "encoding")) {
s = getQWord(&p);
if (*s)
charset = wc_guess_charset(s, charset);
continue;
}
#endif
else if (!strcmp(s, "verbose")) {
s = getWord(&p);
if (*s)
verbose = str_to_bool(s, verbose);
continue;
}
else { /* error */
emsg = Sprintf("line %d: syntax error '%s'", lineno, s)->ptr;
record_err_message(emsg);
if (verbose)
disp_message_nsec(emsg, FALSE, 1, TRUE, FALSE);
continue;
}
setKeymap(p, lineno, verbose);
}
}
void
initKeymap(int force)
{
FILE *kf;
if ((kf = fopen(confFile(KEYMAP_FILE), "rt")) != NULL) {
interpret_keymap(kf, &sys_current_keymap_file,
force || !keymap_initialized);
fclose(kf);
}
if ((kf = fopen(rcFile(keymap_file), "rt")) != NULL) {
interpret_keymap(kf, ¤t_keymap_file,
force || !keymap_initialized);
fclose(kf);
}
keymap_initialized = TRUE;
}
int
getFuncList(char *id)
{
return getHash_si(&functable, id, -1);
}
char *
getKeyData(int key)
{
if (keyData == NULL)
return NULL;
return (char *)getHash_iv(keyData, key, NULL);
}
static int
getKey2(char **str)
{
char *s = *str;
int c, esc = 0, ctrl = 0;
if (s == NULL || *s == '\0')
return -1;
if (strcasecmp(s, "UP") == 0) { /* ^[[A */
*str = s + 2;
return K_ESCB | 'A';
}
else if (strcasecmp(s, "DOWN") == 0) { /* ^[[B */
*str = s + 4;
return K_ESCB | 'B';
}
else if (strcasecmp(s, "RIGHT") == 0) { /* ^[[C */
*str = s + 5;
return K_ESCB | 'C';
}
else if (strcasecmp(s, "LEFT") == 0) { /* ^[[D */
*str = s + 4;
return K_ESCB | 'D';
}
if (strncasecmp(s, "ESC-", 4) == 0 || strncasecmp(s, "ESC ", 4) == 0) { /* ^[ */
s += 4;
esc = K_ESC;
}
else if (strncasecmp(s, "M-", 2) == 0 || strncasecmp(s, "\\E", 2) == 0) { /* ^[ */
s += 2;
esc = K_ESC;
}
else if (*s == ESC_CODE) { /* ^[ */
s++;
esc = K_ESC;
}
if (strncasecmp(s, "C-", 2) == 0) { /* ^, ^[^ */
s += 2;
ctrl = 1;
}
else if (*s == '^' && *(s + 1)) { /* ^, ^[^ */
s++;
ctrl = 1;
}
if (!esc && ctrl && *s == '[') { /* ^[ */
s++;
ctrl = 0;
esc = K_ESC;
}
if (esc && !ctrl) {
if (*s == '[' || *s == 'O') { /* ^[[, ^[O */
s++;
esc = K_ESCB;
}
if (strncasecmp(s, "C-", 2) == 0) { /* ^[^, ^[[^ */
s += 2;
ctrl = 1;
}
else if (*s == '^' && *(s + 1)) { /* ^[^, ^[[^ */
s++;
ctrl = 1;
}
}
if (ctrl) {
*str = s + 1;
if (*s >= '@' && *s <= '_') /* ^@ .. ^_ */
return esc | (*s - '@');
else if (*s >= 'a' && *s <= 'z') /* ^a .. ^z */
return esc | (*s - 'a' + 1);
else if (*s == '?') /* ^? */
return esc | DEL_CODE;
else
return -1;
}
if (esc == K_ESCB && IS_DIGIT(*s)) {
c = (int)(*s - '0');
s++;
if (IS_DIGIT(*s)) {
c = c * 10 + (int)(*s - '0');
s++;
}
*str = s + 1;
if (*s == '~')
return K_ESCD | c;
else
return -1;
}
if (strncasecmp(s, "SPC", 3) == 0) { /* ' ' */
*str = s + 3;
return esc | ' ';
}
else if (strncasecmp(s, "TAB", 3) == 0) { /* ^i */
*str = s + 3;
return esc | '\t';
}
else if (strncasecmp(s, "DEL", 3) == 0) { /* ^? */
*str = s + 3;
return esc | DEL_CODE;
}
if (*s == '\\' && *(s + 1) != '\0') {
s++;
*str = s + 1;
switch (*s) {
case 'a': /* ^g */
return esc | CTRL_G;
case 'b': /* ^h */
return esc | CTRL_H;
case 't': /* ^i */
return esc | CTRL_I;
case 'n': /* ^j */
return esc | CTRL_J;
case 'r': /* ^m */
return esc | CTRL_M;
case 'e': /* ^[ */
return esc | ESC_CODE;
case '^': /* ^ */
return esc | '^';
case '\\': /* \ */
return esc | '\\';
default:
return -1;
}
}
*str = s + 1;
if (IS_ASCII(*s)) /* Ascii */
return esc | *s;
else
return -1;
}
int
getKey(char *s)
{
int c, c2;
c = getKey2(&s);
if (c < 0)
return -1;
if (*s == ' ' || *s == '-')
s++;
if (*s) {
c2 = getKey2(&s);
if (c2 < 0)
return -1;
c = K_MULTI | (c << 16) | c2;
}
return c;
}
char *
getWord(char **str)
{
char *p, *s;
p = *str;
SKIP_BLANKS(p);
for (s = p; *p && !IS_SPACE(*p) && *p != ';'; p++) ;
*str = p;
return Strnew_charp_n(s, p - s)->ptr;
}
char *
getQWord(char **str)
{
Str tmp = Strnew();
char *p;
int in_q = 0, in_dq = 0, esc = 0;
p = *str;
SKIP_BLANKS(p);
for (; *p; p++) {
if (esc) {
if (in_q) {
if (*p != '\\' && *p != '\'') /* '..\\..', '..\'..' */
Strcat_char(tmp, '\\');
}
else if (in_dq) {
if (*p != '\\' && *p != '"') /* "..\\..", "..\".." */
Strcat_char(tmp, '\\');
}
else {
if (*p != '\\' && *p != '\'' && /* ..\\.., ..\'.. */
*p != '"' && !IS_SPACE(*p)) /* ..\".., ..\.. */
Strcat_char(tmp, '\\');
}
Strcat_char(tmp, *p);
esc = 0;
}
else if (*p == '\\') {
esc = 1;
}
else if (in_q) {
if (*p == '\'')
in_q = 0;
else
Strcat_char(tmp, *p);
}
else if (in_dq) {
if (*p == '"')
in_dq = 0;
else
Strcat_char(tmp, *p);
}
else if (*p == '\'') {
in_q = 1;
}
else if (*p == '"') {
in_dq = 1;
}
else if (IS_SPACE(*p) || *p == ';') {
break;
}
else {
Strcat_char(tmp, *p);
}
}
*str = p;
return tmp->ptr;
}
/* This extracts /regex/i or m@regex@i from the given string.
* Then advances *str to the end of regex.
* If the input does not seems to be a regex, this falls back to getQWord().
*
* Returns a word (no matter whether regex or not) in the give string.
* If regex_ret is non-NULL, compiles the regex and stores there.
*
* XXX: Actually this is unrelated to func.c.
*/
char *
getRegexWord(const char **str, Regex **regex_ret)
{
char *word = NULL;
const char *p, *headp, *bodyp, *tailp;
char delimiter;
int esc;
int igncase = 0;
p = *str;
SKIP_BLANKS(p);
headp = p;
/* Get the opening delimiter */
if (p[0] == 'm' && IS_PRINT(p[1]) && !IS_ALNUM(p[1]) && p[1] != '\\') {
delimiter = p[1];
p += 2;
}
else if (p[0] == '/') {
delimiter = '/';
p += 1;
}
else {
goto not_regex;
}
bodyp = p;
/* Scan the end of the expression */
for (esc = 0; *p; ++p) {
if (esc) {
esc = 0;
} else {
if (*p == delimiter)
break;
else if (*p == '\\')
esc = 1;
}
}
if (!*p && *headp == '/')
goto not_regex;
tailp = p;
/* Check the modifiers */
if (*p == delimiter) {
while (*++p && !IS_SPACE(*p)) {
switch (*p) {
case 'i':
igncase = 1;
break;
}
/* ignore unknown modifiers */
}
}
/* Save the expression */
word = allocStr(headp, p - headp);
/* Compile */
if (regex_ret) {
if (*tailp == delimiter)
word[tailp - headp] = 0;
*regex_ret = newRegex(word + (bodyp - headp), igncase, NULL, NULL);
if (*tailp == delimiter)
word[tailp - headp] = delimiter;
}
goto last;
not_regex:
p = headp;
word = getQWord((char **)&p);
if (regex_ret)
*regex_ret = NULL;
last:
*str = p;
return word;
}
#ifdef USE_MOUSE
static MouseAction default_mouse_action = {
NULL,
"<=UpDn",
0, 6, FALSE, 0, 0,
{{movMs, NULL}, {backBf, NULL}, {menuMs, NULL}}, /* default */
{{NULL, NULL}, {NULL, NULL}, {NULL, NULL}}, /* anchor */
{{followA, NULL}, {NULL, NULL}, {NULL, NULL}}, /* active */
{{tabMs, NULL}, {closeTMs, NULL}, {NULL, NULL}}, /* tab */
{NULL, NULL, NULL}, /* menu */
{NULL, NULL, NULL} /* lastline */
};
static MouseActionMap default_lastline_action[6] = {
{backBf, NULL},
{backBf, NULL},
{pgBack, NULL},
{pgBack, NULL},
{pgFore, NULL},
{pgFore, NULL}
};
static void
setMouseAction0(char **str, int *width, MouseActionMap ** map, char *p)
{
char *s;
int b, w, x;
s = getQWord(&p);
if (!*s) {
*str = NULL;
width = 0;
for (b = 0; b < 3; b++)
map[b] = NULL;
return;
}
w = *width;
*str = s;
*width = get_strwidth(s);
if (*width >= LIMIT_MOUSE_MENU)
*width = LIMIT_MOUSE_MENU;
if (*width <= w)
return;
for (b = 0; b < 3; b++) {
if (!map[b])
continue;
map[b] = New_Reuse(MouseActionMap, map[b], *width);
for (x = w + 1; x < *width; x++) {
map[b][x].func = NULL;
map[b][x].data = NULL;
}
}
}
static void
setMouseAction1(MouseActionMap ** map, int width, char *p)
{
char *s;
int x, x2, f;
if (!*map) {
*map = New_N(MouseActionMap, width);
for (x = 0; x < width; x++) {
(*map)[x].func = NULL;
(*map)[x].data = NULL;
}
}
s = getWord(&p);
x = atoi(s);
if (!(IS_DIGIT(*s) && x >= 0 && x < width))
return; /* error */
s = getWord(&p);
x2 = atoi(s);
if (!(IS_DIGIT(*s) && x2 >= 0 && x2 < width))
return; /* error */
s = getWord(&p);
f = getFuncList(s);
s = getQWord(&p);
if (!*s)
s = NULL;
for (; x <= x2; x++) {
(*map)[x].func = (f >= 0) ? w3mFuncList[f].func : NULL;
(*map)[x].data = s;
}
}
static void
setMouseAction2(MouseActionMap * map, char *p)
{
char *s;
int f;
s = getWord(&p);
f = getFuncList(s);
s = getQWord(&p);
if (!*s)
s = NULL;
map->func = (f >= 0) ? w3mFuncList[f].func : NULL;
map->data = s;
}
static void
interpret_mouse_action(FILE * mf)
{
Str line;
char *p, *s;
int b;
while (!feof(mf)) {
line = Strfgets(mf);
Strchop(line);
Strremovefirstspaces(line);
if (line->length == 0)
continue;
p = conv_from_system(line->ptr);
s = getWord(&p);
if (*s == '#') /* comment */
continue;
if (!strcmp(s, "menu")) {
setMouseAction0(&mouse_action.menu_str, &mouse_action.menu_width,
mouse_action.menu_map, p);
continue;
}
else if (!strcmp(s, "lastline")) {
setMouseAction0(&mouse_action.lastline_str,
&mouse_action.lastline_width,
mouse_action.lastline_map, p);
continue;
}
if (strcmp(s, "button"))
continue; /* error */
s = getWord(&p);
b = atoi(s) - 1;
if (!(b >= 0 && b <= 2))
continue; /* error */
SKIP_BLANKS(p);
if (IS_DIGIT(*p))
s = "menu";
else
s = getWord(&p);
if (!strcasecmp(s, "menu")) {
if (!mouse_action.menu_str)
continue;
setMouseAction1(&mouse_action.menu_map[b], mouse_action.menu_width,
p);
}
else if (!strcasecmp(s, "lastline")) {
if (!mouse_action.lastline_str)
continue;
setMouseAction1(&mouse_action.lastline_map[b],
mouse_action.lastline_width, p);
}
else if (!strcasecmp(s, "default"))
setMouseAction2(&mouse_action.default_map[b], p);
else if (!strcasecmp(s, "anchor"))
setMouseAction2(&mouse_action.anchor_map[b], p);
else if (!strcasecmp(s, "active"))
setMouseAction2(&mouse_action.active_map[b], p);
else if (!strcasecmp(s, "tab"))
setMouseAction2(&mouse_action.tab_map[b], p);
}
}
void
initMouseAction(void)
{
FILE *mf;
bcopy((void *)&default_mouse_action, (void *)&mouse_action,
sizeof(default_mouse_action));
mouse_action.lastline_map[0] = New_N(MouseActionMap, 6);
bcopy((void *)&default_lastline_action,
(void *)mouse_action.lastline_map[0],
sizeof(default_lastline_action));
{
#ifdef USE_M17N
int w = 0;
char **symbol = get_symbol(DisplayCharset, &w);
#else
char **symbol = get_symbol();
#endif
mouse_action.lastline_str =
Strnew_charp(symbol[N_GRAPH_SYMBOL + 13])->ptr;
}
if ((mf = fopen(confFile(MOUSE_FILE), "rt")) != NULL) {
interpret_mouse_action(mf);
fclose(mf);
}
if ((mf = fopen(rcFile(MOUSE_FILE), "rt")) != NULL) {
interpret_mouse_action(mf);
fclose(mf);
}
}
#endif
|