File: macro.c

package info (click to toggle)
nn 6.7.3-2.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,488 kB
  • ctags: 3,198
  • sloc: ansic: 32,032; sh: 1,491; awk: 138; makefile: 96
file content (842 lines) | stat: -rw-r--r-- 15,431 bytes parent folder | download | duplicates (7)
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *      Copyright (c) 1996-2005 Michael T Pins.  All rights reserved.
 *
 *	Macro parsing and execution.
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include "global.h"
#include "db.h"
#include "init.h"
#include "keymap.h"
#include "macro.h"
#include "menu.h"
#include "nn_term.h"
#include "variable.h"

/* macro.c */

static void     m_error(char *fmt, char *arg);
static void     m_new(int t);
static int      parse_word(char *w);
static int      parse_line(char *lp);
static struct macro *m_call(int who);
static void     macro_dbg(void);


int             in_menu_mode = 0;
int             get_from_macro = 0;
int             macro_debug = 0;
char           *dflt_enter_macro = NULL;
char           *start_up_macro = NULL;

#ifdef M_BREAK
#undef M_BREAK
#endif

#define M_DUMMY		0	/* do nothing (end of branch)	 */

#define	M_COMMAND	1	/* a command (get_c()) 		 */
#define M_KEY		2	/* a key stroke (get_c()) 	 */
#define M_STRING	3	/* a string (get_s())		 */

#define M_INPUT		4	/* take input from keyboard (get_c/get_s) */

#define M_YES		5	/* answer yes to confirmation	 */
#define M_NO		6	/* answer no to confirmation and break */
 /* -- if neither are present, take input */

#define M_PUTS		7	/* puts "..."			 */
#define M_PROMPT	8	/* prompt(...)	 		 */
#define M_ECHO		9	/* msg(...)			 */

#define M_IS_MENU	10	/* in menu mode ?		 */
#define M_IS_SHOW	11	/* in reading mode ?		 */
#define M_IS_GROUP	12	/* are we in a news group ?	 */
#define M_IS_FOLDER	13	/* are we in a folder ?		 */
#define M_CONFIRM	14	/* ask for confirmation to procede	 */
#define M_REJECT	15	/* ask for !confirmation to procede	 */
#define M_VARTEST	16	/* test value of variable	 */
#define M_BREAK		17	/* exit from macroes		 */
#define	M_RETURN	18	/* return from this macro	 */

#define	M_SET_COMMAND	19	/* set/unset command		 */


struct macro {
    int             m_type;	/* entry type */
    union {
	int             mu_int;	/* command or char */
	char           *mu_string;	/* string for get_s */
	struct macro   *mu_branch;	/* false conditional */
    }               m_value;
    struct macro   *m_next;	/* next macro element */
};

#define m_int		m_value.mu_int
#define m_string	m_value.mu_string
#define m_branch	m_value.mu_branch

#define NUM_MACRO 101		/* numbered macros */
#define ANON_MACRO 100		/* anonymous macroes */
#define NMACRO (NUM_MACRO + ANON_MACRO)

#define MSTACK 5		/* max nesting level */

static struct macro *macro[NMACRO + 1];	/* macro table */
 /* the extra slot is for entry macroes */

static struct macro *mstack[MSTACK];	/* macro stack */
static int      cstack[MSTACK + 1];
static int      m_level = 0;

static struct macro *m = NULL;	/* current macro */
static int      no_advance = 0;
static int      changed_prompt = 0;

static int      cur_m;

#define MERROR ((struct macro *)1)

static void
m_error(char *fmt, char *arg)
{
    char            buf[80];

    if (arg) {
	sprintf(buf, fmt, arg);
	fmt = buf;
    }
    init_message("Error in macro %d: %s", cur_m, fmt);
}

void
init_macro(void)
{
    int             n;

    for (n = 0; n <= NMACRO; n++)
	macro[n] = NULL;
}

static void
m_new(int t)
{
    struct macro   *m1;

    m1 = newobj(struct macro, 1);

    if (m == NULL)
	m = macro[cur_m] = m1;
    else {
	m->m_next = m1;
	m = m1;
    }
    m->m_type = t;
    m->m_next = NULL;
}


/*
 *	Define macro "id" reading from file f until "end"
 *
 *	Macro definition syntax:
 *		define <id>
 *		   <body>
 *		end
 *
 *	Id string interpretation:
 *	NULL 		use next free numbered macro
 *			Return: pointer to macro
 *	"nnn" nnn>=0	Numbered macro nnn
 *			Return: pointer to macro
 *	"-1"		entry macro
 *			Return: pointer to macro
 *	"-2"		anonymous macro
 *			Return: K_MACRO code or K_UNBOUND on error
 */

static int      initial_set_commands;

static int
parse_word(char *w)
{
    int             cmd;
    register struct macro *m1;

    if (m && m->m_type == M_COMMAND && m->m_int == (GETC_COMMAND | K_MACRO)) {
	if (isdigit(*w)) {
	    m->m_int |= atoi(w);
	    goto ok;
	}
	m_error("macro number missing", (char *) NULL);
	return 1;
    }
    if (*w == '"') {
	if (m == NULL || (m->m_type != M_PROMPT && m->m_type != M_ECHO && m->m_type != M_PUTS))
	    m_new(M_STRING);
	m->m_string = copy_str(w + 1);
	goto ok;
    }
    if (*w == '\'') {
	m_new(M_KEY);
	m->m_int = parse_key(w + 1);
	goto ok;
    }
    if (*w == '?') {
	if (strchr(w, '=')) {
	    m->m_type = M_VARTEST;
	    m1 = m;
	    m_new(M_DUMMY);
	    m->m_branch = m1->m_branch;
	    m1->m_string = copy_str(w + 1);
	    goto ok;
	}
	switch (w[1]) {
	    case 'f':		/* ?folder */
		cmd = M_IS_FOLDER;
		break;
	    case 'g':		/* ?group */
		cmd = M_IS_GROUP;
		break;
	    case 'm':		/* ?menu */
		cmd = M_IS_MENU;
		break;
	    case 'n':		/* ?no */
		cmd = M_REJECT;
		break;
	    case 's':		/* ?show */
		cmd = M_IS_SHOW;
		break;
	    case 'y':		/* ?yes */
		cmd = M_CONFIRM;
		break;
	    default:
		m_error("unknown conditional %s", w - 1);
		return 1;
	}
	m->m_type = cmd;
	goto ok;
    }
    if ((cmd = lookup_command(w, (K_ONLY_MENU | K_ONLY_MORE))) > K_INVALID) {
	m_new(M_COMMAND);
	m->m_int = GETC_COMMAND | cmd;
	goto ok;
    }
    if (strcmp(w, "prompt") == 0) {
	m_new(M_PROMPT);
	m->m_string = "?";
	goto ok;
    }
    if (strcmp(w, "echo") == 0) {
	m_new(M_ECHO);
	m->m_string = "ups";
	goto ok;
    }
    if (strcmp(w, "puts") == 0) {
	m_new(M_PUTS);
	m->m_string = "";
	goto ok;
    }
    if (strcmp(w, "input") == 0) {
	m_new(M_INPUT);
	goto ok;
    }
    if (strcmp(w, "yes") == 0) {
	m_new(M_YES);
	goto ok;
    }
    if (strcmp(w, "no") == 0) {
	m_new(M_NO);
	goto ok;
    }
    if (strcmp(w, "break") == 0) {
	m_new(M_BREAK);
	goto ok;
    }
    if (strcmp(w, "return") == 0) {
	m_new(M_RETURN);
	goto ok;
    }
    m_error("Unknown word >>%s<<", w);
    return 1;

ok:
    return 0;
}

static int
parse_line(char *lp)
{
    char           *word;
    struct macro   *m1, *branch = NULL;

    while (*lp) {
	if (*lp == '#')
	    break;

	if (*lp == ':') {
	    lp++;
	    if (initial_set_commands) {

#ifdef REL_640_COMPAT
		if (strncmp(lp, "local", 5) == 0 ||
		    strncmp(lp, "set", 3) == 0 ||
		    strncmp(lp, "unset", 5) == 0) {
#else
		if (lp[0] == ':')
		    lp++;
		else {
#endif

		    m_new(M_SET_COMMAND);
		    m->m_string = copy_str(lp);
		    break;
		}
		initial_set_commands = 0;
	    }
	    m_new(M_COMMAND);
	    m->m_int = GETC_COMMAND | K_EXTENDED_CMD;
	    m_new(M_STRING);
	    m->m_string = copy_str(lp);
	    break;
	}
	initial_set_commands = 0;

	if (*lp == '?') {
	    m_new(M_IS_MENU);
	    if (branch == NULL) {
		m1 = m;
		m_new(M_DUMMY);
		branch = m;
		m = m1;
	    }
	    m->m_branch = branch;
	}
	word = lp;
	if (*lp == '"') {
	    do
		lp++;
	    while (*lp && *lp != '"');
	} else if (*lp == '\'') {
	    do
		lp++;
	    while (*lp && *lp != '\'');
	} else
	    while (*lp && !isspace(*lp))
		lp++;
	if (*lp) {
	    *lp++ = NUL;
	    while (*lp && isspace(*lp))
		lp++;
	}
	if (parse_word(word))
	    return 1;
    }

    if (branch) {
	m->m_next = branch;
	m = branch;
    }
    return 0;
}

char           *
m_define(char *id, FILE * f)
{
    char            line[1024], *lp, skip;
    int             type = 0;

    if (id) {
	cur_m = atoi(id);
	if (cur_m == -1) {
	    cur_m = NMACRO;	/* special slot for this purpose */
	} else if (cur_m == -2) {
	    for (cur_m = NUM_MACRO; cur_m < NMACRO; cur_m++)
		if (macro[cur_m] == NULL)
		    break;
	    if (cur_m == NMACRO) {
		init_message("No unused macro slots");
		return (char *) K_UNBOUND;
	    }
	    type = 1;
	} else if (cur_m < 0 || cur_m >= NUM_MACRO) {
	    m_error("macro number out of range\n", id);
	    return (char *) 0;
	}
    } else {
	for (cur_m = 0; cur_m < NUM_MACRO; cur_m++)
	    if (macro[cur_m] == NULL)
		break;
	if (cur_m == NUM_MACRO) {
	    init_message("No unused macro numbers");
	    return (char *) 0;
	}
    }

    if (f == NULL) {
	clrdisp();
	tprintf("DEFINE %sMACRO %d -- END WITH 'end'\n\n\r",
		cur_m >= NUM_MACRO ? "ANONYMOUS " : "",
		cur_m >= NUM_MACRO ? cur_m - NUM_MACRO : cur_m);
	unset_raw();
	f = stdin;
    }
    m = NULL;
    skip = 0;
    initial_set_commands = (cur_m == NMACRO);

    while (fgets_multi(line, 1024, f)) {
	for (lp = line; *lp && isspace(*lp); lp++);
	if (*lp == NUL)
	    continue;
	if (*lp == ')' || strncmp(lp, "end", 3) == 0)
	    goto out;
	if (!skip && parse_line(lp)) {
	    macro[cur_m] = NULL;
	    skip++;
	}
    }

    if (f != stdin)
	m_error("end missing", (char *) NULL);

out:
    if (f == stdin)
	nn_raw();
    m = NULL;
    return type == 0 ? (char *) macro[cur_m] : (char *) (K_MACRO | cur_m);
}

static char    *
m_get_macro(char *id)
{
    if (id) {
	cur_m = atoi(id);
	if (cur_m < 0 || cur_m >= NMACRO) {
	    m_error("macro number out of range\n", id);
	    return (char *) 0;
	}
    }
    return (char *) macro[cur_m];
}

char           *
parse_enter_macro(FILE * f, register int c)
{
    register char  *gp;
    char            other[FILENAME];
    group_header   *gh;
    static char    *last_defined = NULL;

    while (c != EOF && c != NL && (!isascii(c) || isspace(c)))
	c = getc(f);

    if (c == ')')
	return last_defined;

    if (c == EOF)
	return (char *) NULL;

    if (c == NL)
	return last_defined = m_define("-1", f);

    gp = other;
    do {
	*gp++ = c;
	c = getc(f);
    } while (c != EOF && c != ')' && isascii(c) && !isspace(c));

    *gp = NUL;
    if ((gh = lookup(other)))
	return gh->enter_macro;

    return m_get_macro(other);
}

/*
 *	Invoke macro # N
 */

void
m_invoke(int n)
{
    if (n == -2) {
	n = NMACRO;
	if ((macro[n] = (struct macro *) start_up_macro) == NULL)
	    return;
    } else if (n < 0) {
	n = NMACRO;
	if ((macro[n] = (struct macro *) (current_group->enter_macro)) == NULL)
	    if ((macro[n] = (struct macro *) dflt_enter_macro) == NULL)
		return;
    } else if (n >= NMACRO || macro[n] == NULL) {
	msg("undefined macro %d", n);
	return;
    }
    if (m_level == 0)
	no_advance = 0;
    else if (m == NULL)
	m_level--;
    else {
	if (m_level > MSTACK) {
	    msg("Macro stack overflow");
	    m_break();
	    return;
	}
	mstack[m_level] = m;
	cstack[m_level] = cur_m;
    }
    m_level++;

    cur_m = n;
    m = macro[cur_m];
    while (m && m->m_type == M_SET_COMMAND) {
	char            buffer[128];
	strcpy(buffer, m->m_string);
	if (macro_debug) {
	    msg(":%s", buffer);
	    user_delay(1);
	}
	parse_command(buffer, AC_UNCHANGED, (FILE *) NULL);
	m = m->m_next;
    }
}

void
m_startinput(void)
{
    no_advance = 1;
}

void
m_endinput(void)
{
    if (no_advance) {
	no_advance = 0;
	if (m && m->m_type == M_INPUT)
	    m = m->m_next;
    }
}

void
m_advinput(void)
{
    if (m && m->m_type == M_INPUT)
	m = m->m_next;
}

static struct macro *
m_call(int who)
{
    struct macro   *m1;

    for (;;) {
	while (m == NULL && m_level > 1) {
	    m_level--;
	    m = mstack[m_level];
	    cur_m = cstack[m_level];
	}
	if (m == NULL) {
	    if (macro_debug)
		msg("end");
	    m_break();
	    return NULL;
	}
	if (macro_debug)
	    macro_dbg();

	if (who == 3) {
	    if (m->m_type == M_YES || m->m_type == M_NO)
		goto out;
	    return NULL;
	}
	switch (m->m_type) {
	    case M_COMMAND:
		if (m->m_int == (GETC_COMMAND | K_REDRAW))
		    changed_prompt = 0;

		/* FALLTHRU */
	    case M_KEY:
		if (who == 1)
		    goto out;
		goto err;

	    case M_STRING:
		if (who == 2)
		    goto out;
		goto err;

	    case M_INPUT:
		if (no_advance)
		    return m;
		goto out;

	    case M_YES:
	    case M_NO:
	    case M_DUMMY:
		break;

	    case M_PUTS:
		tprintf("%s", m->m_string);
		fl;
		break;

	    case M_PROMPT:
		if (m->m_string[0] == NUL) {
		    changed_prompt = 0;
		    break;
		}
		if (!changed_prompt)
		    prompt(P_SAVE);
		changed_prompt = 1;
		prompt("\1%s\1 ", m->m_string);
		break;

	    case M_ECHO:
		msg(m->m_string);
		restore_xy();
		break;

	    case M_IS_MENU:
		if (!in_menu_mode)
		    m = m->m_branch;
		break;
	    case M_IS_SHOW:
		if (in_menu_mode)
		    m = m->m_branch;
		break;
	    case M_IS_GROUP:
		if (current_group->group_flag & G_FOLDER)
		    m = m->m_branch;
		break;
	    case M_IS_FOLDER:
		if ((current_group->group_flag & G_FOLDER) == 0)
		    m = m->m_branch;
		break;
	    case M_CONFIRM:
		if (yes(0) == 0)
		    m = m->m_branch;
		break;
	    case M_REJECT:
		if (yes(0) == 1)
		    m = m->m_branch;
		break;

	    case M_VARTEST:
		m1 = m;
		m = m->m_next;

		switch (test_variable(m1->m_string)) {
		    case 0:
			m = m->m_branch;
			break;
		    case -1:
			goto err1;
		}
		break;

	    case M_RETURN:
		m = NULL;
		continue;

	    case M_BREAK:
		goto term;
	}

	if (m)
	    m = m->m_next;
    }

out:
    m1 = m;
    m = m->m_next;
    no_advance = 0;
    return m1;

err:
    msg("Error in macro %d", cur_m);
err1:
    user_delay(1);
    m_break();
    return MERROR;

term:
    m_break();
    return NULL;
}

void
m_break_entry(void)
{
    if (current_group->enter_macro || dflt_enter_macro)
	m = NULL;
}

void
m_break(void)
{
    if (changed_prompt)
	prompt(P_RESTORE);
    changed_prompt = 0;
    m = NULL;
    m_level = 0;
}

static void
macro_dbg(void)
{
    char           *name = NULL;

    switch (m->m_type) {
	case M_COMMAND:
	    msg("COMMAND: %s", command_name(m->m_int));
	    goto delay;

	case M_KEY:
	    msg("KEY: %s", key_name((key_type) (m->m_int)));
	    goto delay;

	case M_STRING:
	    msg("STRING: %s", m->m_string);
	    goto delay;

	case M_INPUT:
	    name = "input";
	    break;

	case M_YES:
	    name = "yes";
	    break;

	case M_NO:
	    name = "no";
	    break;

	case M_DUMMY:
	    name = "dummy";
	    break;

	case M_PROMPT:
	    msg("PROMPT: %s", m->m_string);
	    goto delay;

	case M_ECHO:
	    msg("ECHO: %s", m->m_string);
	    goto delay;

	case M_IS_MENU:
	    msg("?menu => %d", in_menu_mode);
	    goto delay;

	case M_IS_SHOW:
	    msg("?show => %d", !in_menu_mode);
	    goto delay;

	case M_IS_GROUP:
	    msg("?group => %d", (current_group->group_flag & G_FOLDER) == 0);
	    goto delay;

	case M_IS_FOLDER:
	    msg("?group => %d", (current_group->group_flag & G_FOLDER));
	    goto delay;

	case M_CONFIRM:
	    name = "?yes";
	    break;

	case M_REJECT:
	    name = "?no";
	    break;

	case M_VARTEST:
	    msg("?%s => %d", m->m_string, test_variable(m->m_string));
	    goto delay;

	case M_RETURN:
	    name = "return";
	    break;

	case M_BREAK:
	    name = "break";
	    break;
    }
    msg(name);

delay:
    user_delay(1);
}

/*
 *	Macro processing for get_c()
 */

int
m_getc(int *cp)
{
    struct macro   *m1;

    get_from_macro = 0;
    if (m_level && (m1 = m_call(1))) {
	if (m1 == MERROR)
	    return 2;
	if (m1->m_type == M_INPUT)
	    return 0;
	*cp = m1->m_int;
	get_from_macro = 1;
	return 1;
    }
    return 0;
}

/*
 *	Macro processing for get_s()
 */

int
m_gets(char *s)
{
    struct macro   *m1;

    get_from_macro = 0;
    if (m_level && (m1 = m_call(2))) {
	if (m1 == MERROR)
	    return 2;
	if (m1->m_type == M_INPUT)
	    return 0;
	strcpy(s, m1->m_string);
	get_from_macro = 1;
	return 1;
    }
    return 0;
}

/*
 *	Macro processing for yes()
 */

int
m_yes(void)
{
    struct macro   *m1;

    if (m)
	if (m->m_type == M_CONFIRM || m->m_type == M_REJECT)
	    return 3;

    if (m_level) {
	if ((m1 = m_call(3))) {
	    if (m1->m_type == M_NO)
		return 1;
	    else
		return 2;
	} else {
	    return 3;
	}
    }
    return 0;
}