-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmenu.c
662 lines (590 loc) · 14.7 KB
/
menu.c
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
/************************************************************************/
/* File : menu.c */
/* Description : Implement a Motif MenuBar into Mxvile */
/* Parse a File (environment variable XVILE_MENU) */
/* and create it at the top of the window */
/* Auteur : Philippe CHASSANY */
/* Date : 20/02/97 */
/************************************************************************/
/*
* $Id: menu.c,v 1.82 2025/01/26 16:40:42 tom Exp $
*/
/* Vile includes */
#include "estruct.h"
#include "edef.h"
#if SYS_VMS
#include <processes.h>
#define fork vfork
#endif
/* Tokens of the rc file */
typedef struct _tok_ {
struct _tok_ *next;
char type;
char label[NSTRING];
char action[NSTRING];
int macro;
} MenuToken;
/*
* state, from successive define-menu-entry calls.
*/
static int count_entries = 0;
static void *my_menu_bar = NULL;
static void *parent_menu = NULL;
static MenuToken *all_tokens = NULL;
/* Actions of the rc file */
typedef struct {
char name[30];
const ActionFunc fact;
} TAct;
/* Common Actions = User defined Actions */
/* Why not use dynamic link library to get functions ? */
static void common_action(char *action);
static TAct Actions[] =
{
{"nop", NULL},
{"new_xvile", common_action},
{"edit_rc", common_action},
{"parse_rc", common_action},
{"edit_mrc", common_action},
{"parse_mrc", common_action},
};
/*
* MenuTokens have to be allocated so their contents will be available later in
* a callback.
*/
static MenuToken *
newToken(void)
{
MenuToken *result = NULL;
beginDisplay();
result = typecalloc(MenuToken);
if (result != NULL) {
result->next = all_tokens;
all_tokens = result;
}
endofDisplay();
return result;
}
static void
free_all_tokens(void)
{
beginDisplay();
while (all_tokens != NULL) {
MenuToken *next = all_tokens->next;
free(all_tokens);
all_tokens = next;
}
endofDisplay();
}
void
init_menus(void)
{
free_all_tokens();
count_entries = 0;
parent_menu = NULL;
}
static char *
menu_filename(void)
{
char *result = cfg_locate(menu_file, LOCATE_SOURCE);
#if SYS_UNIX
/* just for backward compatibility */
if (result == NULL && strcmp(menu_file, "vilemenu.rc")) {
static char *param;
if (param == NULL)
param = strmalloc("vilemenu.rc");
result = cfg_locate(param, FL_STARTPATH | FL_READABLE);
}
#endif
return result;
}
static char *
startup_filename(void)
{
return cfg_locate(startup_file, LOCATE_SOURCE);
}
static void
edit_file(char *fname)
{
if (fname != NULL && *fname != EOS && ffexists(fname)) {
splitwind(FALSE, 1);
getfile(fname, TRUE);
} else
no_file_found();
}
/*
* Reload/parse the menu-file.
*/
static void
parse_menus(char *fname)
{
gui_remove_menus(FALSE, 1);
init_menus();
if (dofile(fname)) {
gui_show_menus(FALSE, 1);
}
}
/************************************************************************/
/* All the common actions are handled by this function */
/************************************************************************/
static void
common_action(char *action)
{
TRACE(("Action=%s\n", action));
if (!strcmp(action, "new_xvile")) {
int pid;
int status;
char path[NFILEN];
pathcat(path, exec_pathname, prog_arg);
if ((pid = fork()) > 0) {
while (wait(&status) >= 0)
/* */ ;
} else if (pid == 0) {
if ((pid = fork()) > 0) {
free_all_leaks();
_exit(0); /* abandon exec'ing process */
} else if (pid == 0) {
newprocessgroup(FALSE, 1);
execl(path,
"xvile",
"-display",
x_get_display_name(),
(void *) 0);
_exit(errno); /* just in case exec-failed */
}
}
} else if (!strcmp(action, "edit_rc")) {
edit_file(startup_filename());
} else if (!strcmp(action, "parse_rc")) {
dofile(startup_filename());
} else if (!strcmp(action, "edit_mrc")) {
edit_file(menu_filename());
} else if (!strcmp(action, "parse_mrc")) {
parse_menus(menu_filename());
}
}
/************************************************************************/
/* Test if the action name is part of the Actions of the rc file */
/************************************************************************/
static int
is_action(char *action)
{
unsigned i;
for (i = 0; i < TABLESIZE(Actions); i++) {
if (!strcmp(Actions[i].name, action))
return 1;
}
return 0;
}
/************************************************************************/
/* Return the function pointer associated with the action name */
/************************************************************************/
ActionFunc
vlmenu_action_func(char *action)
{
unsigned i;
for (i = 0; i < TABLESIZE(Actions); i++) {
if (!strcmp(Actions[i].name, action))
return Actions[i].fact;
}
return NULL;
}
/************************************************************************/
/* Test if the action name is a bind (inspired from vile sources */
/************************************************************************/
int
vlmenu_is_bind(char *action)
{
static TBUFF *tok;
(void) get_token(action, &tok, eol_null, EOS, (int *) 0);
if (tb_length(tok) != 0
&& engl2fnc(tb_values(tok)) != NULL)
return 1;
return 0;
}
/************************************************************************/
/* Test if the action name begins with the keyword "cmd", */
/************************************************************************/
char *
vlmenu_is_cmd(char *action)
{
static TBUFF *tok;
char *result = (char *) get_token(action, &tok, eol_null, EOS, (int *) 0);
if (tb_length(tok) != 0
&& !strcmp(tb_values(tok), "cmd"))
return result;
return NULL;
}
/*
* Parse a menu-entry string, filling in the token if an entry was found.
* If an error occurs, return false.
* If only a comment-line was found, return sort-of-true.
*/
static int
parse_menu_entry(MenuToken * token, const char *source, size_t slen)
{
char *ptr_tok;
int n, tmp;
int result = TRUE;
char buffer[sizeof(MenuToken) + NLINE];
/* make a copy of the source, since strtok modifies it */
if (slen > sizeof(buffer) - 1)
slen = sizeof(buffer) - 1;
memcpy(buffer, source, slen);
buffer[slen] = EOS;
/* Let a tab begin inline comment */
if ((ptr_tok = strchr(buffer, '\t')) != NULL)
*ptr_tok = EOS;
ptr_tok = strtok(buffer, ":");
memset(token, 0, sizeof(*token));
if (ptr_tok != NULL) {
switch (*ptr_tok) {
case ';': /* FALLTHRU */
case '~':
case '"':
case '#':
result = SORTOFTRUE;
break;
case 'C':
token->type = *ptr_tok;
if ((ptr_tok = strtok(NULL, ":\n")) != NULL) {
vl_strncpy(token->label, ptr_tok, sizeof(token->label));
if (strtok(NULL, ":\n") != NULL) {
token->type = 'H';
}
} else {
result = FALSE;
}
break;
case 'S':
token->type = *ptr_tok;
break;
case 'L':
token->type = *ptr_tok;
if ((ptr_tok = strtok(NULL, ":\n")) != NULL) {
vl_strncpy(token->action, ptr_tok, sizeof(token->action));
}
break;
case 'B':
token->type = *ptr_tok;
n = 0;
while ((ptr_tok = strtok(NULL, ":\n")) != NULL) {
switch (n) {
case 0:
vl_strncpy(token->label, ptr_tok, sizeof(token->label));
break;
case 1:
if (isDigit((int) *ptr_tok)) {
tmp = (int) atoi(ptr_tok);
token->macro = tmp;
} else {
if (is_action(ptr_tok)
|| vlmenu_is_bind(ptr_tok)
|| vlmenu_is_cmd(ptr_tok)) {
vl_strncpy(token->action, ptr_tok, sizeof(token->action));
} else {
mlwarn("'%s' is not an action", ptr_tok);
vl_strncpy(token->action, "beep", sizeof(token->action));
result = FALSE;
}
}
break;
}
n++;
}
if (n != 2) {
result = FALSE;
}
break;
}
}
return result;
}
/************************************************************************/
/* Debug function to print the tokens of the rc file */
/************************************************************************/
#if OPT_TRACE
static void
print_token(MenuToken * token)
{
switch (token->type) {
case 'C':
case 'H':
Trace("%c %s\n",
token->type,
token->label);
break;
case 'S':
Trace("\t%c\n",
token->type);
break;
case 'L':
Trace("\t%c %s\n",
token->type,
token->action);
break;
case 'B':
if (token->macro > 0)
Trace("\t%c %s -> Macro-%d\n",
token->type,
token->label,
token->macro);
else
Trace("\t%c %s -> Action-%s\n",
token->type,
token->label,
token->action);
break;
}
}
#endif
static void *
make_header(void *menub, int count)
{
char label[80];
sprintf(label, "Menu%d", count);
return gui_make_menu(menub, label, 'C');
}
static void
define_menu_entry(MenuToken * token)
{
char *accel, *macro_text;
void *pm_w;
#if OPT_TRACE
print_token(token);
#endif
switch (token->type) {
/* HELP CASCADE */
case 'C':
case 'H':
parent_menu = gui_make_menu(my_menu_bar, token->label, token->type);
break;
/* SEPARATOR WIDGET */
case 'S':
if (parent_menu == NULL)
parent_menu = make_header(my_menu_bar, ++count_entries);
gui_add_menu_item(parent_menu, "sep", NULL, token->type);
break;
/* LIST (BUFFER LIST) */
case 'L':
if (!strcmp(token->action, "list_buff")) {
if (parent_menu == NULL)
parent_menu = make_header(my_menu_bar, ++count_entries);
gui_add_list_callback(parent_menu);
}
break;
/* BUTTON WIDGET */
case 'B':
if (parent_menu == NULL)
parent_menu = make_header(my_menu_bar, ++count_entries);
if (token->macro > 0) {
if ((macro_text = castalloc(char, 50)) != NULL) {
sprintf(macro_text, "execute-macro-%d", token->macro);
accel = give_accelerator(macro_text);
pm_w = gui_add_menu_item(parent_menu, token->label, accel, token->type);
gui_add_func_callback(pm_w, macro_text);
}
} else {
accel = give_accelerator(token->action);
pm_w = gui_add_menu_item(parent_menu, token->label, accel, token->type);
gui_add_func_callback(pm_w, token->action);
}
break;
}
}
static void
add_menu_separator(void)
{
MenuToken *myToken = newToken();
if (myToken != NULL) {
myToken->type = 'S';
define_menu_entry(myToken);
}
}
static int
add_menu_entry(const char *text, size_t slen)
{
int result;
MenuToken myToken;
MenuToken *hisToken;
MenuToken *links;
result = parse_menu_entry(&myToken, text, slen);
if (result == FALSE) {
result = FALSE;
} else if (result == TRUE) {
if (myToken.type == 'L') {
add_menu_separator();
}
hisToken = newToken();
links = hisToken->next;
*hisToken = myToken;
hisToken->next = links;
define_menu_entry(hisToken);
}
return result;
}
/* FIXME - merge this with chunk from gettagsfile() */
static BUFFER *
re_read_in(char *fname, BUFFER *bp, int *did_read)
{
#ifdef MDCHK_MODTIME
time_t current = 0;
#endif
*did_read = FALSE;
#ifdef MDCHK_MODTIME
if (strcmp(fname, NonNull(bp->b_fname))) {
bp->b_modtime = 0;
}
if ((global_b_val(MDCHK_MODTIME)
&& get_modtime(bp, ¤t)
&& bp->b_modtime != current)) {
if (readin(fname, FALSE, bp, FALSE) != TRUE) {
zotbuf(bp);
bp = NULL;
} else {
set_modtime(bp, bp->b_fname);
*did_read = TRUE;
}
} else if (!bp->b_active || strcmp(fname, NonNull(bp->b_fname)))
#endif
{
if (readin(fname, FALSE, bp, FALSE) != TRUE) {
zotbuf(bp);
bp = NULL;
} else {
set_modtime(bp, bp->b_fname);
*did_read = TRUE;
}
}
TRACE(("re_read_in(%s) %s\n", fname, *did_read ? "read" : "skipped"));
return bp;
}
/*
* Get a pointer to the menu-buffer. If the external file is newer, read that
* into memory.
*/
static BUFFER *
get_menu_buffer(void)
{
int did_read;
char *menurc;
BUFFER *bp = NULL;
if ((menurc = menu_filename()) == NULL) {
mlforce("No menu-file found");
} else if ((bp = bfind(VILEMENU_BufName, BFEXEC | BFINVS)) != NULL) {
if ((bp = re_read_in(menurc, bp, &did_read)) != NULL) {
b_set_invisible(bp);
set_vilemode(bp);
}
}
TRACE(("get_menu_buffer(%s) %s\n",
menurc,
(bp != 0) ? "success" : "fail"));
return bp;
}
static int
has_expression(LINE *lp)
{
int rc = FALSE;
int len = llength(lp);
int n;
if (len > 0) {
for (n = 0; n < len; ++n) {
int ch = lgetc(lp, n);
if (!isSpace(ch)) {
rc = (ch == '~');
break;
}
}
}
return rc;
}
/************************************************************************/
/* Main function : Take the menu-bar as argument and create all the */
/* Cascades, PullDowns Menus and Buttons read from the rc file */
/************************************************************************/
int
do_menu(void *menub)
{
static int first = TRUE;
BUFFER *bp;
LINE *lp;
int rc = TRUE;
TRACE((T_CALLED "do_menu\n"));
my_menu_bar = menub;
init_menus();
/*
* As called from xvile's initialization, this would be too soon to do
* expression-evaluation. Check if there are any expressions - if so, we
* will delay loading the menus. Otherwise (the old scheme), do it right
* away.
*/
if ((bp = get_menu_buffer()) != NULL) {
delay_menus = FALSE;
for_each_line(lp, bp) {
if (has_expression(lp)) {
static const char *fallback[] =
{
"C:Help:help",
"B:About:version"
};
unsigned n;
/*
* Provide a fallback to simplify making the menubar the
* right height during initialization.
*/
for (n = 0; n < TABLESIZE(fallback); ++n) {
if ((rc = add_menu_entry(fallback[n],
strlen(fallback[n]))) == FALSE) {
break;
}
}
delay_menus = TRUE;
break;
}
}
if (delay_menus && !first) {
gui_remove_menus(FALSE, 1);
dobuf(bp, 1, -1);
} else if (!delay_menus) {
for_each_line(lp, bp) {
rc = add_menu_entry(lvalue(lp), (size_t) llength(lp));
if (rc == FALSE)
break;
}
}
first = FALSE;
}
returnCode(rc);
}
/************************************************************************/
/*
* Define a menu-entry for the current menu-bar.
*/
int
vlmenu_entry(int f, int n)
{
int result;
char buffer[NLINE];
if (clexec) {
result = add_menu_entry(execstr, strlen(execstr));
} else {
*buffer = EOS;
result = mlreply("Menu entry:", buffer, (UINT) sizeof(buffer));
if (result == TRUE)
result = add_menu_entry(buffer, strlen(buffer));
if (result == TRUE)
result = gui_show_menus(f, n);
}
return result;
}
int
vlmenu_load(int f, int n)
{
int result = FALSE;
gui_remove_menus(f, n);
if (gui_create_menus()) {
result = gui_show_menus(f, n);
}
return result;
}