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
|
/* $OpenBSD: extend.c,v 1.22 2002/03/10 13:22:56 ho Exp $ */
/*
* Extended (M-X) commands, rebinding, and startup file processing.
*/
#include "chrdef.h"
#include "def.h"
#include "kbd.h"
#include "funmap.h"
#ifndef NO_MACRO
#include "macro.h"
#endif /* !NO_MACRO */
#ifdef FKEYS
#include "key.h"
#ifndef NO_STARTUP
#ifndef BINDKEY
#define BINDKEY /* bindkey is used by FKEYS startup code */
#endif /* !BINDKEY */
#endif /* !NO_STARTUP */
#endif /* FKEYS */
static int remap(KEYMAP *, int, PF, KEYMAP *);
static KEYMAP *realocmap(KEYMAP *);
static void fixmap(KEYMAP *, KEYMAP *, KEYMAP *);
static int dobind(KEYMAP *, char *, int);
static char *skipwhite(char *);
static char *parsetoken(char *);
static int bindkey(KEYMAP **, char *, KCHAR *, int);
/*
* Insert a string, mainly for use from macros (created by selfinsert)
*/
/* ARGSUSED */
int
insert(f, n)
int f, n;
{
char *cp;
char buf[128];
#ifndef NO_MACRO
int count, c;
if (inmacro) {
while (--n >= 0) {
for (count = 0; count < maclcur->l_used; count++) {
if ((((c = maclcur->l_text[count]) == '\n')
? lnewline() : linsert(1, c)) != TRUE)
return FALSE;
}
}
maclcur = maclcur->l_fp;
return TRUE;
}
if (n == 1)
/* CFINS means selfinsert can tack on the end */
thisflag |= CFINS;
#endif /* !NO_MACRO */
if (eread("Insert: ", buf, sizeof(buf), EFNEW) == FALSE)
return FALSE;
while (--n >= 0) {
cp = buf;
while (*cp) {
if (((*cp == '\n') ? lnewline() : linsert(1, *cp))
!= TRUE)
return FALSE;
cp++;
}
}
return TRUE;
}
/*
* Bind a key to a function. Cases range from the trivial (replacing an
* existing binding) to the extremly complex (creating a new prefix in a
* map_element that already has one, so the map_element must be split,
* but the keymap doesn't have enough room for another map_element, so
* the keymap is reallocated). No attempt is made to reclaim space no
* longer used, if this is a problem flags must be added to indicate
* malloced verses static storage in both keymaps and map_elements.
* Structure assignments would come in real handy, but K&R based compilers
* don't have them. Care is taken so running out of memory will leave
* the keymap in a usable state.
*/
static int
remap(curmap, c, funct, pref_map)
KEYMAP *curmap; /* pointer to the map being changed */
int c; /* character being changed */
PF funct; /* function being changed to */
KEYMAP *pref_map; /* if funct==NULL, map to bind to or
NULL for new */
{
int i, n1, n2, nold;
KEYMAP *mp;
PF *pfp;
MAP_ELEMENT *mep;
if (ele >= &curmap->map_element[curmap->map_num] || c < ele->k_base) {
if (ele > &curmap->map_element[0] && (funct != NULL ||
(ele - 1)->k_prefmap == NULL))
n1 = c - (ele - 1)->k_num;
else
n1 = HUGE;
if (ele < &curmap->map_element[curmap->map_num] &&
(funct != NULL || ele->k_prefmap == NULL))
n2 = ele->k_base - c;
else
n2 = HUGE;
if (n1 <= MAPELEDEF && n1 <= n2) {
ele--;
if ((pfp = (PF *)malloc((c - ele->k_base + 1) *
sizeof(PF))) == NULL) {
ewprintf("Out of memory");
return FALSE;
}
nold = ele->k_num - ele->k_base + 1;
for (i = 0; i < nold; i++)
pfp[i] = ele->k_funcp[i];
while (--n1)
pfp[i++] = curmap->map_default;
pfp[i] = funct;
ele->k_num = c;
ele->k_funcp = pfp;
} else if (n2 <= MAPELEDEF) {
if ((pfp = (PF *)malloc((ele->k_num - c + 1) *
sizeof(PF))) == NULL) {
ewprintf("Out of memory");
return FALSE;
}
nold = ele->k_num - ele->k_base + 1;
for (i = 0; i < nold; i++)
pfp[i + n2] = ele->k_funcp[i];
while (--n2)
pfp[n2] = curmap->map_default;
pfp[0] = funct;
ele->k_base = c;
ele->k_funcp = pfp;
} else {
if (curmap->map_num >= curmap->map_max &&
(curmap = realocmap(curmap)) == NULL)
return FALSE;
if ((pfp = malloc(sizeof(PF))) == NULL) {
ewprintf("Out of memory");
return FALSE;
}
pfp[0] = funct;
for (mep = &curmap->map_element[curmap->map_num];
mep > ele; mep--) {
mep->k_base = (mep - 1)->k_base;
mep->k_num = (mep - 1)->k_num;
mep->k_funcp = (mep - 1)->k_funcp;
mep->k_prefmap = (mep - 1)->k_prefmap;
}
ele->k_base = c;
ele->k_num = c;
ele->k_funcp = pfp;
ele->k_prefmap = NULL;
curmap->map_num++;
}
if (funct == NULL) {
if (pref_map != NULL) {
ele->k_prefmap = pref_map;
} else {
if (!(mp = (KEYMAP *)malloc(sizeof(KEYMAP) +
(MAPINIT - 1) * sizeof(MAP_ELEMENT)))) {
ewprintf("Out of memory");
ele->k_funcp[c - ele->k_base] =
curmap->map_default;
return FALSE;
}
mp->map_num = 0;
mp->map_max = MAPINIT;
mp->map_default = rescan;
ele->k_prefmap = mp;
}
}
} else {
n1 = c - ele->k_base;
if (ele->k_funcp[n1] == funct && (funct != NULL ||
pref_map == NULL || pref_map == ele->k_prefmap))
/* no change */
return TRUE;
if (funct != NULL || ele->k_prefmap == NULL) {
if (ele->k_funcp[n1] == NULL)
ele->k_prefmap = NULL;
/* easy case */
ele->k_funcp[n1] = funct;
if (funct == NULL) {
if (pref_map != NULL)
ele->k_prefmap = pref_map;
else {
if (!(mp = malloc(sizeof(KEYMAP) +
(MAPINIT - 1) *
sizeof(MAP_ELEMENT)))) {
ewprintf("Out of memory");
ele->k_funcp[c - ele->k_base] =
curmap->map_default;
return FALSE;
}
mp->map_num = 0;
mp->map_max = MAPINIT;
mp->map_default = rescan;
ele->k_prefmap = mp;
}
}
} else {
/*
* This case is the splits.
* Determine which side of the break c goes on
* 0 = after break; 1 = before break
*/
n2 = 1;
for (i = 0; n2 && i < n1; i++)
n2 &= ele->k_funcp[i] != NULL;
if (curmap->map_num >= curmap->map_max &&
(curmap = realocmap(curmap)) == NULL)
return FALSE;
if ((pfp = malloc((ele->k_num - c + !n2) *
sizeof(PF))) == NULL) {
ewprintf("Out of memory");
return FALSE;
}
ele->k_funcp[n1] = NULL;
for (i = n1 + n2; i <= ele->k_num - ele->k_base; i++)
pfp[i - n1 - n2] = ele->k_funcp[i];
for (mep = &curmap->map_element[curmap->map_num];
mep > ele; mep--) {
mep->k_base = (mep - 1)->k_base;
mep->k_num = (mep - 1)->k_num;
mep->k_funcp = (mep - 1)->k_funcp;
mep->k_prefmap = (mep - 1)->k_prefmap;
}
ele->k_num = c - !n2;
(ele + 1)->k_base = c + n2;
(ele + 1)->k_funcp = pfp;
ele += !n2;
ele->k_prefmap = NULL;
curmap->map_num++;
if (pref_map == NULL) {
if ((mp = malloc(sizeof(KEYMAP) + (MAPINIT - 1)
* sizeof(MAP_ELEMENT))) == NULL) {
ewprintf("Out of memory");
ele->k_funcp[c - ele->k_base] =
curmap->map_default;
return FALSE;
}
mp->map_num = 0;
mp->map_max = MAPINIT;
mp->map_default = rescan;
ele->k_prefmap = mp;
} else
ele->k_prefmap = pref_map;
}
}
return TRUE;
}
/*
* Reallocate a keymap, used above.
*/
static KEYMAP *
realocmap(curmap)
KEYMAP *curmap;
{
MAPS *mps;
KEYMAP *mp;
int i;
if ((mp = (KEYMAP *)malloc((unsigned)(sizeof(KEYMAP) +
(curmap->map_max + (MAPGROW - 1)) *
sizeof(MAP_ELEMENT)))) == NULL) {
ewprintf("Out of memory");
return NULL;
}
mp->map_num = curmap->map_num;
mp->map_max = curmap->map_max + MAPGROW;
mp->map_default = curmap->map_default;
for (i = curmap->map_num; i--;) {
mp->map_element[i].k_base = curmap->map_element[i].k_base;
mp->map_element[i].k_num = curmap->map_element[i].k_num;
mp->map_element[i].k_funcp = curmap->map_element[i].k_funcp;
mp->map_element[i].k_prefmap = curmap->map_element[i].k_prefmap;
}
for (mps = maps; mps != NULL; mps = mps->p_next) {
if (mps->p_map == curmap)
mps->p_map = mp;
else
fixmap(curmap, mp, mps->p_map);
}
ele = &mp->map_element[ele - &curmap->map_element[0]];
return mp;
}
/*
* Fix references to a reallocated keymap (recursive).
*/
static void
fixmap(curmap, mp, mt)
KEYMAP *mt;
KEYMAP *curmap;
KEYMAP *mp;
{
int i;
for (i = mt->map_num; i--;) {
if (mt->map_element[i].k_prefmap != NULL) {
if (mt->map_element[i].k_prefmap == curmap)
mt->map_element[i].k_prefmap = mp;
else
fixmap(curmap, mp, mt->map_element[i].k_prefmap);
}
}
}
/*
* do the input for local-set-key, global-set-key and define-key
* then call remap to do the work.
*/
static int
dobind(curmap, p, unbind)
KEYMAP *curmap;
char *p;
int unbind;
{
KEYMAP *pref_map = NULL;
PF funct;
char prompt[80];
char *pep;
int c, s;
#ifndef NO_MACRO
if (macrodef) {
/*
* Keystrokes aren't collected. Not hard, but pretty useless.
* Would not work for function keys in any case.
*/
ewprintf("Can't rebind key in macro");
return FALSE;
}
#ifndef NO_STARTUP
if (inmacro) {
for (s = 0; s < maclcur->l_used - 1; s++) {
if (doscan(curmap, c = CHARMASK(maclcur->l_text[s]), &curmap)
!= NULL) {
if (remap(curmap, c, NULL, NULL)
!= TRUE)
return FALSE;
}
}
(void)doscan(curmap, c = maclcur->l_text[s], NULL);
maclcur = maclcur->l_fp;
} else {
#endif /* !NO_STARTUP */
#endif /* !NO_MACRO */
pep = prompt + strlcpy(prompt, p, sizeof(prompt));
for (;;) {
ewprintf("%s", prompt);
pep[-1] = ' ';
pep = keyname(pep, sizeof(prompt) - (pep - prompt),
c = getkey(FALSE));
if (doscan(curmap, c, &curmap) != NULL)
break;
*pep++ = '-';
*pep = '\0';
}
#ifndef NO_STARTUP
}
#endif /* !NO_STARTUP */
if (unbind)
funct = rescan;
else {
if ((s = eread("%s to command: ", prompt, 80, EFFUNC | EFNEW,
prompt)) != TRUE)
return s;
if (((funct = name_function(prompt)) == NULL) ?
(pref_map = name_map(prompt)) == NULL : funct == NULL) {
ewprintf("[No match]");
return FALSE;
}
}
return remap(curmap, c, funct, pref_map);
}
/*
* bindkey: bind key sequence to a function in the specified map. Used by
* excline so it can bind function keys. To close to release to change
* calling sequence, should just pass KEYMAP *curmap rather than
* KEYMAP **mapp.
*/
#ifdef BINDKEY
static int
bindkey(mapp, fname, keys, kcount)
KEYMAP **mapp;
char *fname;
KCHAR *keys;
int kcount;
{
KEYMAP *curmap = *mapp;
KEYMAP *pref_map = NULL;
PF funct;
int c;
if (fname == NULL)
funct = rescan;
else if (((funct = name_function(fname)) == NULL) ?
(pref_map = name_map(fname)) == NULL : funct == NULL) {
ewprintf("[No match: %s]", fname);
return FALSE;
}
while (--kcount) {
if (doscan(curmap, c = *keys++, &curmap) != NULL) {
if (remap(curmap, c, NULL, NULL) != TRUE)
return FALSE;
/*
* XXX - Bizzarreness. remap creates an empty KEYMAP
* that the last key is supposed to point to.
*/
curmap = ele->k_prefmap;
}
}
(void)doscan(curmap, c = *keys, NULL);
return remap(curmap, c, funct, pref_map);
}
#ifdef FKEYS
/*
* Wrapper for bindkey() that converts escapes.
*/
int
dobindkey(map, func, str)
KEYMAP *map;
char *func;
char *str;
{
int i;
for (i = 0; *str && i < MAXKEY; i++) {
/* XXX - convert numbers w/ strol()? */
if (*str != '\\')
key.k_chars[i] = *str;
else {
switch (*++str) {
case 't':
case 'T':
key.k_chars[i] = '\t';
break;
case 'n':
case 'N':
key.k_chars[i] = '\n';
break;
case 'r':
case 'R':
key.k_chars[i] = '\r';
break;
case 'e':
case 'E':
key.k_chars[i] = CCHR('[');
break;
}
}
str++;
}
key.k_count = i;
return (bindkey(&map, func, key.k_chars, key.k_count));
}
#endif /* FKEYS */
#endif /* BINDKEY */
/*
* This function modifies the fundamental keyboard map.
*/
/* ARGSUSED */
int
bindtokey(f, n)
int f, n;
{
return dobind(fundamental_map, "Global set key: ", FALSE);
}
/*
* This function modifies the current mode's keyboard map.
*/
/* ARGSUSED */
int
localbind(f, n)
int f, n;
{
return dobind(curbp->b_modes[curbp->b_nmodes]->p_map,
"Local set key: ", FALSE);
}
/*
* This function redefines a key in any keymap.
*/
/* ARGSUSED */
int
define_key(int f, int n)
{
static char buf[48];
char tmp[32];
KEYMAP *mp;
strlcpy(buf, "Define key map: ", sizeof buf);
if (eread(buf, tmp, sizeof tmp, EFNEW) != TRUE)
return FALSE;
strlcat(buf, tmp, sizeof buf);
if ((mp = name_map(tmp)) == NULL) {
ewprintf("Unknown map %s", tmp);
return FALSE;
}
strlcat(buf, "key: ", sizeof buf);
return dobind(mp, buf, FALSE);
}
int
unbindtokey(f, n)
int f, n;
{
return dobind(fundamental_map, "Global unset key: ", TRUE);
}
int
localunbind(f, n)
int f, n;
{
return dobind(curbp->b_modes[curbp->b_nmodes]->p_map,
"Local unset key: ", TRUE);
}
/*
* Extended command. Call the message line routine to read in the command
* name and apply autocompletion to it. When it comes back, look the name
* up in the symbol table and run the command if it is found. Print an
* error if there is anything wrong.
*/
int
extend(f, n)
int f, n;
{
PF funct;
int s;
char xname[NXNAME];
if (!(f & FFARG))
s = eread("M-x ", xname, NXNAME, EFNEW | EFFUNC);
else
s = eread("%d M-x ", xname, NXNAME, EFNEW | EFFUNC, n);
if (s != TRUE)
return s;
if ((funct = name_function(xname)) != NULL) {
#ifndef NO_MACRO
if (macrodef) {
LINE *lp = maclcur;
macro[macrocount - 1].m_funct = funct;
maclcur = lp->l_bp;
maclcur->l_fp = lp->l_fp;
free((char *)lp);
}
#endif /* !NO_MACRO */
return (*funct)(f, n);
}
ewprintf("[No match]");
return FALSE;
}
#ifndef NO_STARTUP
/*
* Define the commands needed to do startup-file processing.
* This code is mostly a kludge just so we can get startup-file processing.
*
* If you're serious about having this code, you should rewrite it.
* To wit:
* It has lots of funny things in it to make the startup-file look
* like a GNU startup file; mostly dealing with parens and semicolons.
* This should all vanish.
*
* We define eval-expression because it's easy. It can make
* *-set-key or define-key set an arbitrary key sequence, so it isn't
* useless.
*/
/*
* evalexpr - get one line from the user, and run it.
*/
/* ARGSUSED */
int
evalexpr(f, n)
int f, n;
{
int s;
char exbuf[128];
if ((s = ereply("Eval: ", exbuf, 128)) != TRUE)
return s;
return excline(exbuf);
}
/*
* evalbuffer - evaluate the current buffer as line commands. Useful for
* testing startup files.
*/
/* ARGSUSED */
int
evalbuffer(f, n)
int f, n;
{
LINE *lp;
BUFFER *bp = curbp;
int s;
static char excbuf[128];
for (lp = lforw(bp->b_linep); lp != bp->b_linep; lp = lforw(lp)) {
if (llength(lp) >= 128)
return FALSE;
(void)strncpy(excbuf, ltext(lp), llength(lp));
/* make sure it's terminated */
excbuf[llength(lp)] = '\0';
if ((s = excline(excbuf)) != TRUE)
return s;
}
return TRUE;
}
/*
* evalfile - go get a file and evaluate it as line commands. You can
* go get your own startup file if need be.
*/
/* ARGSUSED */
int
evalfile(f, n)
int f, n;
{
int s;
char fname[NFILEN];
if ((s = ereply("Load file: ", fname, NFILEN)) != TRUE)
return s;
return load(fname);
}
/*
* load - go load the file name we got passed.
*/
int
load(fname)
char *fname;
{
int s = TRUE;
int nbytes = 0;
char excbuf[128];
if ((fname = adjustname(fname)) == NULL)
/* just to be careful */
return FALSE;
if (ffropen(fname, NULL) != FIOSUC)
return FALSE;
while ((s = ffgetline(excbuf, sizeof(excbuf) - 1, &nbytes)) == FIOSUC) {
excbuf[nbytes] = '\0';
if (excline(excbuf) != TRUE) {
s = FIOERR;
ewprintf("Error loading file %s", fname);
break;
}
}
(void)ffclose(NULL);
excbuf[nbytes] = '\0';
if (s != FIOEOF || (nbytes && excline(excbuf) != TRUE))
return FALSE;
return TRUE;
}
/*
* excline - run a line from a load file or eval-expression. if FKEYS is
* defined, duplicate functionallity of dobind so function key values don't
* have to fit in type char.
*/
int
excline(line)
char *line;
{
PF fp;
LINE *lp, *np;
int status, c, f, n;
char *funcp, *tmp;
char *argp = NULL;
long nl;
#ifdef FKEYS
int bind;
KEYMAP *curmap;
#define BINDARG 0 /* this arg is key to bind (local/global set key) */
#define BINDNO 1 /* not binding or non-quoted BINDARG */
#define BINDNEXT 2 /* next arg " (define-key) */
#define BINDDO 3 /* already found key to bind */
#define BINDEXT 1 /* space for trailing \0 */
#else /* FKEYS */
#define BINDEXT 0
#endif /* FKEYS */
lp = NULL;
if (macrodef || inmacro) {
ewprintf("Not now!");
return FALSE;
}
f = 0;
n = 1;
funcp = skipwhite(line);
if (*funcp == '\0')
return TRUE; /* No error on blank lines */
line = parsetoken(funcp);
if (*line != '\0') {
*line++ = '\0';
line = skipwhite(line);
if (ISDIGIT(*line) || *line == '-') {
argp = line;
line = parsetoken(line);
}
}
if (argp != NULL) {
f = FFARG;
errno = 0;
nl = strtol(argp, &tmp, 10);
if (*tmp != '\0')
return FALSE;
if (nl >= INT_MAX || nl <= INT_MIN)
return FALSE;
n = (int)nl;
}
if ((fp = name_function(funcp)) == NULL) {
ewprintf("Unknown function: %s", funcp);
return FALSE;
}
#ifdef FKEYS
if (fp == bindtokey || fp == unbindtokey) {
bind = BINDARG;
curmap = fundamental_map;
} else if (fp == localbind || fp == localunbind) {
bind = BINDARG;
curmap = curbp->b_modes[curbp->b_nmodes]->p_map;
} else if (fp == define_key)
bind = BINDNEXT;
else
bind = BINDNO;
#endif /* FKEYS */
/* Pack away all the args now... */
if ((np = lalloc(0)) == FALSE)
return FALSE;
np->l_fp = np->l_bp = maclcur = np;
while (*line != '\0') {
argp = skipwhite(line);
if (*argp == '\0')
break;
line = parsetoken(argp);
if (*argp != '"') {
if (*argp == '\'')
++argp;
if ((lp = lalloc((int) (line - argp) + BINDEXT)) ==
NULL) {
status = FALSE;
goto cleanup;
}
bcopy(argp, ltext(lp), (int)(line - argp));
#ifdef FKEYS
/* don't count BINDEXT */
lp->l_used--;
if (bind == BINDARG)
bind = BINDNO;
#endif /* FKEYS */
} else {
/* quoted strings are special */
++argp;
#ifdef FKEYS
if (bind != BINDARG) {
#endif /* FKEYS */
lp = lalloc((int)(line - argp) + BINDEXT);
if (lp == NULL) {
status = FALSE;
goto cleanup;
}
lp->l_used = 0;
#ifdef FKEYS
} else {
key.k_count = 0;
}
#endif /* FKEYS */
while (*argp != '"' && *argp != '\0') {
if (*argp != '\\')
c = *argp++;
else {
switch (*++argp) {
case 't':
case 'T':
c = CCHR('I');
break;
case 'n':
case 'N':
c = CCHR('J');
break;
case 'r':
case 'R':
c = CCHR('M');
break;
case 'e':
case 'E':
c = CCHR('[');
break;
case '^':
/*
* split into two statements
* due to bug in OSK cpp
*/
c = CHARMASK(*++argp);
c = ISLOWER(c) ?
CCHR(TOUPPER(c)) : CCHR(c);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
c = *argp - '0';
if (argp[1] <= '7' &&
argp[1] >= '0') {
c <<= 3;
c += *++argp - '0';
if (argp[1] <= '7' &&
argp[1] >= '0') {
c <<= 3;
c += *++argp
- '0';
}
}
break;
#ifdef FKEYS
case 'f':
case 'F':
c = *++argp - '0';
if (ISDIGIT(argp[1])) {
c *= 10;
c += *++argp - '0';
}
c += KFIRST;
break;
#endif /* FKEYS */
default:
c = CHARMASK(*argp);
break;
}
argp++;
}
#ifdef FKEYS
if (bind == BINDARG)
key.k_chars[key.k_count++] = c;
else
#endif /* FKEYS */
lp->l_text[lp->l_used++] = c;
}
if (*line)
line++;
}
#ifdef FKEYS
switch (bind) {
case BINDARG:
bind = BINDDO;
break;
case BINDNEXT:
lp->l_text[lp->l_used] = '\0';
if ((curmap = name_map(lp->l_text)) == NULL) {
ewprintf("No such mode: %s", lp->l_text);
status = FALSE;
free((char *)lp);
goto cleanup;
}
free((char *)lp);
bind = BINDARG;
break;
default:
#endif /* FKEYS */
lp->l_fp = np->l_fp;
lp->l_bp = np;
np->l_fp = lp;
np = lp;
#ifdef FKEYS
}
#endif /* FKEYS */
}
#ifdef FKEYS
switch (bind) {
default:
ewprintf("Bad args to set key");
status = FALSE;
break;
case BINDDO:
if (fp != unbindtokey && fp != localunbind) {
lp->l_text[lp->l_used] = '\0';
status = bindkey(&curmap, lp->l_text, key.k_chars,
key.k_count);
} else {
status = bindkey(&curmap, NULL, key.k_chars,
key.k_count);
}
break;
case BINDNO:
#endif /* FKEYS */
inmacro = TRUE;
maclcur = maclcur->l_fp;
status = (*fp)(f, n);
inmacro = FALSE;
#ifdef FKEYS
}
#endif /* FKEYS */
cleanup:
lp = maclcur->l_fp;
while (lp != maclcur) {
np = lp->l_fp;
free((char *)lp);
lp = np;
}
free((char *)lp);
return status;
}
/*
* a pair of utility functions for the above
*/
static char *
skipwhite(s)
char *s;
{
while (*s == ' ' || *s == '\t' || *s == ')' || *s == '(')
s++;
if (*s == ';')
*s = '\0';
return s;
}
static char *
parsetoken(s)
char *s;
{
if (*s != '"') {
while (*s && *s != ' ' && *s != '\t' && *s != ')' && *s != '(')
s++;
if (*s == ';')
*s = '\0';
} else
do {
/*
* Strings get special treatment.
* Beware: You can \ out the end of the string!
*/
if (*s == '\\')
++s;
} while (*++s != '"' && *s != '\0');
return s;
}
#endif /* !NO_STARTUP */
|