Codebase list mg / fresh-snapshots/main interpreter.c
fresh-snapshots/main

Tree @fresh-snapshots/main (Download .tar.gz)

interpreter.c @fresh-snapshots/mainraw · history · blame

  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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/*      $OpenBSD: interpreter.c,v 1.34 2022/01/28 06:18:41 guenther Exp $	*/
/*
 * This file is in the public domain.
 *
 * Author: Mark Lumsden <mark@showcomplex.com>
 */

/*
 * This file attempts to add some 'scripting' functionality into mg.
 *
 * The initial goal is to give mg the ability to use its existing functions
 * and structures in a linked-up way. Hopefully resulting in user definable
 * functions. The syntax is 'scheme' like but currently it is not a scheme
 * interpreter.
 *
 * At the moment there is no manual page reference to this file. The code below
 * is liable to change, so use at your own risk!
 *
 * If you do want to do some testing, you can add some lines to your .mg file
 * like:
 * 
 * 1. Give multiple arguments to a function that usually would accept only one:
 * (find-file "a.txt" "b.txt" "c.txt")
 *
 * 2. Define a single value variable:
 * (define myfile "d.txt")
 *
 * 3. Define a list:
 * (define myfiles(list "e.txt" "f.txt"))
 *
 * 4. Use the previously defined variable or list:
 * (find-file myfiles)
 *
 * To do:
 * 1. multiline parsing - currently only single lines supported.
 * 2. parsing for '(' and ')' throughout whole string and evaluate correctly.
 * 3. conditional execution.
 * 4. have memory allocated dynamically for variable values.
 * 5. do symbol names need more complex regex patterns? [A-Za-z][.0-9_A-Z+a-z-]
 *    at the moment.
 * 6. display line numbers with parsing errors.
 * 7. oh so many things....
 * [...]
 * n. implement user definable functions.
 * 
 * Notes:
 * - Currently calls to excline() from this file have the line length and
 *   line number set to zero.
 *   That's because excline() uses '\0' as the end of line indicator
 *   and only the call to foundparen() within excline() uses excline's 2nd
 *   and 3rd arguments.
 *   Importantly, any lines sent to there from here will not be
 *   coming back here.
 */
#include <sys/queue.h>

#include <ctype.h>
#include <limits.h>
#include <regex.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "def.h"
#include "funmap.h"

#ifdef  MGLOG
#include "kbd.h"
#include "log.h"
#endif

static int	 multiarg(char *, char *, int);
static int	 isvar(char **, char **, int);
/*static int	 dofunc(char **, char **, int);*/
static int	 founddef(char *, int, int, int, int);
static int	 foundlst(char *, int, int, int);
static int	 expandvals(char *, char *, char *);
static int	 foundfun(char *, int);
static int	 doregex(char *, char *);
static void	 clearexp(void);
static int	 parse(char *, const char *, const char *, int, int, int, int);
static int	 parsdef(char *, const char *, const char *, int, int, int);
static int	 parsval(char *, const char *, const char *, int, int, int);
static int	 parsexp(char *, const char *, const char *, int, int, int);

static int	 exitinterpreter(char *, char *, int);

TAILQ_HEAD(exphead, expentry) ehead;
struct expentry {
	TAILQ_ENTRY(expentry) eentry;
	char	*fun;		/* The 1st string found between parens.   */
	char	 funbuf[BUFSIZE];
	const char	*par1;	/* Parenthesis at start of string	  */
	const char	*par2;	/* Parenthesis at end of string		  */
	int	 expctr;	/* An incremental counter:+1 for each exp */
	int	 blkid;		/* Which block are we in?		  */
};

/*
 * Structure for scheme keywords. 
 */
#define NUMSCHKEYS	4
#define MAXLENSCHKEYS	17	/* 17 = longest keyword (16)  + 1 */

char scharkey[NUMSCHKEYS][MAXLENSCHKEYS] =
	{ 
		"define",
	  	"list",
	  	"if",
	  	"lambda"
	};

static const char 	 lp = '(';
static const char 	 rp = ')';
static char 		*defnam = NULL;
static int		 lnm;

/*
 * Line has a '(' as the first non-white char.
 * Do some very basic parsing of line.
 * Multi-line not supported at the moment, To do.
 */
int
foundparen(char *funstr, int llen, int lnum)
{
	const char	*lrp = NULL;
	char		*p, *begp = NULL, *endp = NULL, *prechr = NULL;
	char		*lastchr = NULL;
	int     	 i, ret = 255, pctr, expctr, blkid, inquote, esc;
	int		 elen, spc, ns;

	pctr = expctr = inquote = esc = elen = spc = ns = 0;
	blkid = 1;
	lnm = lnum;

	/*
	 * load expressions into a list called 'expentry', to be processd
	 * when all are obtained.
	 * Not really live code at the moment. Just part of the process of
	 * working out what needs to be done.
	 */
	TAILQ_INIT(&ehead);

	/*
	 * Check for blocks of code with opening and closing ().
	 * One block = (cmd p a r a m)
	 * Two blocks = (cmd p a r a m s)(hola)
	 * Two blocks = (cmd p a r (list a m s))(hola)
	 * Only single line at moment, but more for multiline.
	 */
	p = funstr;

	for (i = 0; i < llen; ++i, p++) {
		if (pctr == 0 && *p != ' ' && *p != '\t' && *p != '(') {
			if (*p == ')')
				return(dobeep_num("Extra ')' found on line:",
				    lnm));
			return(dobeep_num("Error line:", lnm));
		}
		if (begp != NULL)
			elen++;

		if (*p == '\\') {
			esc = 1;
		} else if (*p == '(') {
			if (lastchr != NULL && *lastchr == '(')
				return(dobeep_num("Multiple consecutive "\
				    "left parantheses line", lnm));
			if (inquote == 0) {
				if (begp != NULL) {
					if (*prechr == ' ')
						ns--;
					if (endp == NULL)
						*p = '\0';
					else
						*endp = '\0';

					ret = parse(begp, lrp, &lp, blkid,
					    ++expctr, elen - spc, ns);
					if (!ret) {
						cleanup();
						return(ret);
					}
					elen = 0;
				}
				lrp = &lp;
				begp = endp = NULL;
				pctr++;
			} else if (inquote != 1) {
				cleanup();
				return(dobeep_num("Opening and closing quote "\
				    "char error line:", lnm));
			}
			esc = spc = 0;
		} else if (*p == ')') {
			if (lastchr != NULL && *lastchr == '(')
				return(dobeep_num("Empty parenthesis "\
				    "not supported line", lnm));
			if (inquote == 0) {
				if (begp != NULL) {
					if (*prechr == ' ')
						ns--;
					if (endp == NULL)
						*p = '\0';
					else
						*endp = '\0';

					ret = parse(begp, lrp, &rp, blkid,
					    ++expctr, elen - spc, ns);
					if (!ret) {
						cleanup();
						return(ret);
					}
					elen = 0;
				}
				lrp = &rp;
				begp = endp = NULL;
				pctr--;
			} else if (inquote != 1) {
				cleanup();
				return(dobeep_num("Opening and closing quote "\
				    "char error line:", lnm));
			}
			esc = spc = 0;
		} else if (*p != ' ' && *p != '\t') {
			if (begp == NULL) {
				begp = p;
				if (*begp == '"' || isdigit(*begp))
					return(dobeep_num("First char of "\
					    "expression error line:", lnm));
			}
			if (*p == '"') {
				if (inquote == 0 && esc == 0) {
					if (*prechr != ' ' && *prechr != '\t')
						return(dobeep_num("Parse error"\
						    " line:", lnm));
					inquote++;
				} else if (inquote > 0 && esc == 1)
					esc = 0;
				else
					inquote--;
			} else if (*prechr == '"' && inquote == 0) {
				return(dobeep_num("Parse error line:", lnm));
			}
			endp = NULL;
			spc = 0;
		} else if (endp == NULL && (*p == ' ' || *p == '\t')) {
			if (inquote == 0) {
				*p = ' ';
				endp = p;
				spc++;
				if (begp != NULL)
					ns++;
			}
			esc = 0;
		} else if (*p == '\t' || *p == ' ') {
			if (inquote == 0) {
				*p = ' ';
				spc++;
			}
			esc = 0;
		}
		if (*p != '\t' && *p != ' ' && inquote == 0)
			lastchr = p;

		if (pctr == 0) {
			blkid++;
			expctr = 0;
			defnam = NULL;
		}
		prechr = p;
	}

	if (pctr != 0) {
		cleanup();
		return(dobeep_num("Opening and closing parentheses error line:",
		    lnm));
	}
	if (ret == FALSE)
		cleanup();
	else
		clearexp();	/* leave lists but remove expressions */

	return (ret);
}


static int
parse(char *begp, const char *par1, const char *par2, int blkid, int expctr,
    int elen, int ns)
{
	char    *regs;
	int 	 ret = FALSE;

	if (strncmp(begp, "define", 6) == 0) {
		ret = parsdef(begp, par1, par2, blkid, expctr, elen);
		if (ret == TRUE || ret == FALSE)
			return (ret);
	} else if (strncmp(begp, "list", 4) == 0)
		return(parsval(begp, par1, par2, blkid, expctr, elen));

	regs = "^exit$";
	if (doregex(regs, begp))
		return(exitinterpreter(NULL, NULL, FALSE));

	/* mg function name regex */	
	regs = "^[A-Za-z-]+$";
        if (doregex(regs, begp))
		return(excline(begp, 0, 0));

	/* Corner case 1 */
	if (strncmp(begp, "global-set-key ", 15) == 0)
		/* function name as 2nd param screws up multiarg. */
		return(excline(begp, 0, 0));

	/* Corner case 2 */
	if (strncmp(begp, "define-key ", 11) == 0)
		/* function name as 3rd param screws up multiarg. */
		return(excline(begp, 0, 0));

	return (parsexp(begp, par1, par2, blkid, expctr, elen));
}

static int
parsdef(char *begp, const char *par1, const char *par2, int blkid, int expctr,
    int elen)
{
	char    *regs;

	if ((defnam == NULL) && (expctr != 1))
		return(dobeep_num("'define' incorrectly used line:", lnm));

        /* Does the line have a incorrect variable 'define' like: */
        /* (define i y z) */
        regs = "^define[ ]+[A-Za-z][.0-9_A-Z+a-z-]*[ ]+.+[ ]+.+$";
        if (doregex(regs, begp))
                return(dobeep_num("Invalid use of define line:", lnm));

        /* Does the line have a single variable 'define' like: */
        /* (define i 0) */
        regs = "^define[ ]+[A-Za-z][.0-9_A-Z+a-z-]*[ ]+.*$";
        if (doregex(regs, begp)) {
		if (par1 == &lp && par2 == &rp && expctr == 1)
			return(founddef(begp, blkid, expctr, 1, elen));
		return(dobeep_num("Invalid use of define line:", lnm));
	}
	/* Does the line have  '(define i(' */
        regs = "^define[ ]+[A-Za-z][.0-9_A-Z+a-z-]*[ ]*$";
        if (doregex(regs, begp)) {
		if (par1 == &lp && par2 == &lp && expctr == 1)
                	return(founddef(begp, blkid, expctr, 0, elen));
		return(dobeep_num("Invalid use of 'define' line:", lnm));
	}
	/* Does the line have  '(define (' */
	regs = "^define$";
	if (doregex(regs, begp)) {
		if (par1 == &lp && par2 == &lp && expctr == 1)
			return(foundfun(begp, expctr));
		return(dobeep_num("Invalid use of 'define' line:", lnm));
	}

	return (ABORT);
}

static int
parsval(char *begp, const char *par1, const char *par2, int blkid, int expctr,
    int elen)
{
	char    *regs;

	/* Does the line have 'list' */
	regs = "^list$";
	if (doregex(regs, begp))
		return(dobeep_num("Invalid use of list line:", lnm));

        /* Does the line have a 'list' like: */
        /* (list "a" "b") */
        regs = "^list[ ]+.*$";
        if (doregex(regs, begp)) {
		if (expctr == 1)
			return(dobeep_num("list with no-where to go.", lnm));

		if (par1 == &lp && expctr > 1)
			return(foundlst(begp, blkid, expctr, elen));

		return(dobeep_num("Invalid use of list line:", lnm));
	}
	return (FALSE);
}

static int
parsexp(char *begp, const char *par1, const char *par2, int blkid, int expctr,
    int elen)
{
	struct expentry *e1 = NULL;
	PF		 funcp;
	char		*cmdp, *fendp, *valp, *fname, *funb = NULL;;
	int		 numparams, ret;

	cmdp = begp;
	fendp = strchr(cmdp, ' ');
	*fendp = '\0';

	/*
	 * If no extant mg command found, just return.
	 */
	if ((funcp = name_function(cmdp)) == NULL)
		return (dobeep_msgs("Unknown command: ", cmdp));

	numparams = numparams_function(funcp);
	if (numparams == 0)
		return (dobeep_msgs("Command takes no arguments:", cmdp));

	if (numparams == -1)
		return (dobeep_msgs("Interactive command found:", cmdp));

	if ((e1 = malloc(sizeof(struct expentry))) == NULL) {
		cleanup();
		return (dobeep_msg("malloc Error"));
	}
	TAILQ_INSERT_HEAD(&ehead, e1, eentry);
	if ((e1->fun = strndup(cmdp, BUFSIZE)) == NULL) {
		cleanup();
		return(dobeep_msg("strndup error"));
	}
	cmdp = e1->fun;
	fname = e1->fun;
	e1->funbuf[0] = '\0';
	funb = e1->funbuf;
	e1->expctr = expctr;
	e1->blkid = blkid;
	/* need to think about these two */
	e1->par1 = par1;
	e1->par2 = par2;

	*fendp = ' ';
	valp = fendp + 1;

	ret = expandvals(cmdp, valp, funb);
	if (!ret)
		return (ret);

	return (multiarg(fname, funb, numparams));
}

/*
 * Pass a list of arguments to a function.
 */
static int
multiarg(char *cmdp, char *argbuf, int numparams)
{
	char	 excbuf[BUFSIZE];
	char	*argp, *p, *s = " ";
	char	*regs;
	int	 spc, numspc;
	int	 fin, inquote;

	argp = argbuf;
	spc = 1; /* initially fake a space so we find first argument */
	numspc = fin = inquote = 0;

	for (p = argbuf; *p != '\0'; p++) {
		if (*(p + 1) == '\0')
			fin = 1;

		if (*p != ' ') {
			if (*p == '"') {
				if (inquote == 1)
					inquote = 0;	
				else
					inquote = 1;
			}
			if (spc == 1)
				if ((numspc % numparams) == 0) {
					argp = p;
				}
			spc = 0;
		}
		if ((*p == ' ' && inquote == 0) || fin) {
			if (spc == 1)/* || (numspc % numparams == 0))*/
				continue;
			if ((numspc % numparams) != (numparams - 1)) {
				numspc++;
				continue;
			}
			if (*p == ' ') {
				*p = '\0';		/* terminate arg string */
			}
			excbuf[0] = '\0';
			regs = "[\"]+.*[\"]+";

       			if (!doregex(regs, argp)) {
				const char *errstr;

				strtonum(argp, 0, INT_MAX, &errstr);
				if (errstr != NULL)
					return (dobeep_msgs("Var not found:",
					    argp));
			}

			if (strlcpy(excbuf, cmdp, sizeof(excbuf))
			    >= sizeof(excbuf))
				return (dobeep_msg("strlcpy error"));
			if (strlcat(excbuf, s, sizeof(excbuf))
			    >= sizeof(excbuf))
				return (dobeep_msg("strlcat error"));
			if (strlcat(excbuf, argp, sizeof(excbuf))
			    >= sizeof(excbuf))
				return (dobeep_msg("strlcat error"));

			excline(excbuf, 0, 0);

			if (fin)
				break;

			*p = ' ';		/* unterminate arg string */
			numspc++;
			spc = 1;
		}
	}
	return (TRUE);
}

/*
 * Is an item a value or a variable?
 */
static int
isvar(char **argp, char **varbuf, int sizof)
{
	struct varentry *v1 = NULL;

	if (SLIST_EMPTY(&varhead))
		return (FALSE);
#ifdef  MGLOG
	mglog_isvar(*varbuf, *argp, sizof);
#endif
	SLIST_FOREACH(v1, &varhead, entry) {
		if (strcmp(*argp, v1->v_name) == 0) {
			(void)(strlcpy(*varbuf, v1->v_buf, sizof) >= sizof);
			return (TRUE);
		}
	}
	return (FALSE);
}


static int
foundfun(char *defstr, int expctr)
{
	return (TRUE);
}

static int
foundlst(char *defstr, int blkid, int expctr, int elen)
{
	char		*p;

	p = strstr(defstr, " ");
	p = skipwhite(p);
	expandvals(NULL, p, defnam);

	return (TRUE);
}

/*
 * 'define' strings follow the regex in parsdef().
 */
static int
founddef(char *defstr, int blkid, int expctr, int hasval, int elen)
{
	struct varentry *vt, *v1 = NULL;
	char		*p, *vnamep, *vendp = NULL, *valp;

	p = strstr(defstr, " ");        /* move to first ' ' char.    */
	vnamep = skipwhite(p);		/* find first char of var name. */
	vendp = vnamep;

	/* now find the end of the define/list name */
	while (1) {
		++vendp;
		if (*vendp == ' ')
			break;
	}
	*vendp = '\0';

	/*
	 * Check list name is not an existing mg function.
	 */
	if (name_function(vnamep) != NULL)
		return(dobeep_msgs("Variable/function name clash:", vnamep));

	if (!SLIST_EMPTY(&varhead)) {
		SLIST_FOREACH_SAFE(v1, &varhead, entry, vt) {
			if (strcmp(vnamep, v1->v_name) == 0)
				SLIST_REMOVE(&varhead, v1, varentry, entry);
		}
	}
	if ((v1 = malloc(sizeof(struct varentry))) == NULL)
		return (ABORT);
	SLIST_INSERT_HEAD(&varhead, v1, entry);
	if ((v1->v_name = strndup(vnamep, BUFSIZE)) == NULL)
		return(dobeep_msg("strndup error"));
	vnamep = v1->v_name;
	v1->v_count = 0;
	v1->v_vals = NULL;
	v1->v_buf[0] = '\0';

	defnam = v1->v_buf;

	if (hasval) {
		valp = skipwhite(vendp + 1);

		expandvals(NULL, valp, defnam);
		defnam = NULL;
	}
	*vendp = ' ';	
	return (TRUE);
}


static int
expandvals(char *cmdp, char *valp, char *bp)
{
	char	 excbuf[BUFSIZE], argbuf[BUFSIZE];
	char	 contbuf[BUFSIZE], varbuf[BUFSIZE];
	char	*argp, *endp, *p, *v, *s = " ";
	char	*regs;
	int	 spc, cnt;
	int	 sizof, fin, inquote;

	/* now find the first argument */
	p = skipwhite(valp);

	if (strlcpy(argbuf, p, sizeof(argbuf)) >= sizeof(argbuf))
		return (dobeep_msg("strlcpy error"));
	argp = argbuf;
	spc = 1; /* initially fake a space so we find first argument */
	fin = inquote = cnt = spc = 0;

	for (p = argbuf; *p != '\0'; p++) {
		if (*(p + 1) == '\0')
			fin = 1;

		if (*p != ' ') {
			if (*p == '"') {
				if (inquote == 1)
					inquote = 0;	
				else
					inquote = 1;
			}
			if (spc == 1)
				argp = p;
			spc = 0;
		}
		if ((*p == ' ' && inquote == 0) || fin) {
			if (spc == 1)
				continue;
			/* terminate arg string */
			if (*p == ' ') {
				*p = '\0';		
			}
			endp = p + 1;
			excbuf[0] = '\0';
			varbuf[0] = '\0';
			contbuf[0] = '\0';			
			sizof = sizeof(varbuf);
			v = varbuf;
			regs = "[\"]+.*[\"]+";
       			if (doregex(regs, argp))
				;			/* found quotes */
			else if (isvar(&argp, &v, sizof)) {

				(void)(strlcat(varbuf, " ",
                                    sizof) >= sizof);

				*p = ' ';
				(void)(strlcpy(contbuf, endp,
				    sizeof(contbuf)) >= sizeof(contbuf));

				(void)(strlcat(varbuf, contbuf,
				    sizof) >= sizof);
				
				argbuf[0] = ' ';
				argbuf[1] = '\0';
				(void)(strlcat(argbuf, varbuf,
				    sizof) >= sizof);

				p = argp = argbuf;
				spc = 1;
				fin = 0;
				continue;
			} else {
				const char *errstr;

				strtonum(argp, 0, INT_MAX, &errstr);
				if (errstr != NULL)
					return (dobeep_msgs("Var not found:",
					    argp));
			}
#ifdef  MGLOG
        mglog_misc("x|%s|%p|%d|\n", bp, defnam, BUFSIZE);
#endif
			if (*bp != '\0') {
				if (strlcat(bp, s, BUFSIZE) >= BUFSIZE)
					return (dobeep_msg("strlcat error"));
			}
			if (strlcat(bp, argp, BUFSIZE) >= BUFSIZE) {
				return (dobeep_msg("strlcat error"));
			}
/*			v1->v_count++;*/
			
			if (fin)
				break;

			*p = ' ';		/* unterminate arg string */
			spc = 1;
		}
	}
	return (TRUE);
}

/*
 * Finished with buffer evaluation, so clean up any vars.
 * Perhaps keeps them in mg even after use,...
 */
/*static int
clearvars(void)
{
	struct varentry	*v1 = NULL;

	while (!SLIST_EMPTY(&varhead)) {
		v1 = SLIST_FIRST(&varhead);
		SLIST_REMOVE_HEAD(&varhead, entry);
		free(v1->v_name);
		free(v1);
	}
	return (FALSE);
}
*/
/*
 * Finished with block evaluation, so clean up any expressions.
 */
static void
clearexp(void)
{
	struct expentry	*e1 = NULL;

	while (!TAILQ_EMPTY(&ehead)) {
		e1 = TAILQ_FIRST(&ehead);
		TAILQ_REMOVE(&ehead, e1, eentry);
		free(e1->fun);
		free(e1);
	}
	return;
}

/*
 * Cleanup before leaving.
 */
void
cleanup(void)
{
	defnam = NULL;

	clearexp();
/*	clearvars();*/
}

/*
 * Test a string against a regular expression.
 */
static int
doregex(char *r, char *e)
{
	regex_t  regex_buff;

	if (regcomp(&regex_buff, r, REG_EXTENDED)) {
		regfree(&regex_buff);
		return(dobeep_num("Regex compilation error line:", lnm));
	}
	if (!regexec(&regex_buff, e, 0, NULL, 0)) {
		regfree(&regex_buff);
		return(TRUE);
	}
	regfree(&regex_buff);
	return(FALSE);
}

/*
 * Display a message so it is apparent that this is the method which stopped
 * execution.
 */
static int
exitinterpreter(char *ptr, char *dobuf, int dosiz)
{
	cleanup();
	if (batch == 0)
		return(dobeep_msg("Interpreter exited via exit command."));
	return(FALSE);
}

/*
 * All code below commented out (until end of file).
 *
 * Need to think about how interpreter functions are done.
 * Probably don't have a choice with string-append().

static int 	 getenvironmentvariable(char *, char *, int);
static int	 stringappend(char *, char *, int);

typedef int	 (*PFI)(char *, char *, int);


struct ifunmap {
	PFI		 fn_funct;
	const char 	*fn_name;
	struct ifunmap	*fn_next;
};
static struct ifunmap *ifuns;

static struct ifunmap ifunctnames[] = {
	{exitinterpreter, "exit"},
	{getenvironmentvariable, "get-environment-variable"},
	{stringappend, "string-append"},
	{NULL, NULL}
};

void
ifunmap_init(void)
{
	struct ifunmap *fn;

	for (fn = ifunctnames; fn->fn_name != NULL; fn++) {
		fn->fn_next = ifuns;
		ifuns = fn;
	}
}

PFI
name_ifun(const char *ifname)
{
	struct ifunmap 	*fn;

	for (fn = ifuns; fn != NULL; fn = fn->fn_next) {
		if (strcmp(fn->fn_name, ifname) == 0)
			return (fn->fn_funct);
	}

	return (NULL);
}


int
dofunc(char **ifname, char **tmpbuf, int sizof)
{
	PFI 	 fnc;
	char	*p, *tmp;

	p = strstr(*ifname, " ");
	*p = '\0';

	fnc = name_ifun(*ifname);
	if (fnc == NULL)
		return (FALSE);

	*p = ' ';

	tmp = *tmpbuf;

	fnc(p, tmp, sizof);

	return (TRUE);
}

static int
getenvironmentvariable(char *ptr, char *dobuf, int dosiz)
{
	char		*t;
	char		*tmp;
	const char	*q = "\"";

	t = skipwhite(ptr);

	if (t[0] == *q || t[strlen(t) - 1] == *q)
		return (dobeep_msgs("Please remove '\"' around:", t));
	if ((tmp = getenv(t)) == NULL || *tmp == '\0')
		return(dobeep_msgs("Envar not found:", t));

	dobuf[0] = '\0';
	if (strlcat(dobuf, q, dosiz) >= dosiz)
		return (dobeep_msg("strlcat error"));
	if (strlcat(dobuf, tmp, dosiz) >= dosiz)
		return (dobeep_msg("strlcat error"));
	if (strlcat(dobuf, q, dosiz) >= dosiz)
		return (dobeep_msg("strlcat error"));
		
	return (TRUE);
}

static int
stringappend(char *ptr, char *dobuf, int dosiz)
{
	char		 varbuf[BUFSIZE], funbuf[BUFSIZE];
	char            *p, *f, *v, *vendp;
	int		 sizof, fin = 0;

	varbuf[0] = funbuf[0] = '\0';
	f = funbuf;
	v = varbuf;
	sizof = sizeof(varbuf);
	*dobuf = '\0';

	p = skipwhite(ptr);

	while (*p != '\0') {
		vendp = p;
		while (1) {
			if (*vendp == ' ') {
				break;
			} else if (*vendp == '\0') {
				fin = 1;
				break;
			}
			++vendp;
		}
        	*vendp = '\0';

		if (isvar(&p, &v, sizof)) {
			if (v[0] == '"' && v[strlen(v) - 1] == '"' ) {
				v[strlen(v) - 1] = '\0';
				v = v + 1;
			}
			if (strlcat(f, v, sizof) >= sizof)
				return (dobeep_msg("strlcat error"));		
		} else {
			if (p[0] == '"' && p[strlen(p) - 1] == '"' ) {
				p[strlen(p) - 1] = '\0';
				p = p + 1;
			}
			if (strlcat(f, p, sizof) >= sizof)
				return (dobeep_msg("strlcat error"));		
		}
		if (fin)
			break;
		vendp++;
		if (*vendp == '\0')
			break;
		p = skipwhite(vendp);
	}

	(void)snprintf(dobuf, dosiz, "\"%s\"", f);

	return (TRUE);
}

Index: main.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/main.c,v
retrieving revision 1.89
diff -u -p -u -p -r1.89 main.c
--- main.c      20 Mar 2021 09:00:49 -0000      1.89
+++ main.c      12 Apr 2021 17:58:52 -0000
@@ -133,10 +133,12 @@ main(int argc, char **argv)
                extern void grep_init(void);
                extern void cmode_init(void);
                extern void dired_init(void);
+               extern void ifunmap_init(void);

                dired_init();
                grep_init();
                cmode_init();
+               ifunmap_init();
        }


*/