Annotation of src/usr.bin/mg/extend.c, Revision 1.6
1.6 ! niklas 1: /* $OpenBSD$ */
! 2:
1.1 deraadt 3: /*
1.5 millert 4: * Extended (M-X) commands, rebinding, and startup file processing.
1.1 deraadt 5: */
1.5 millert 6:
7: #include "def.h"
8: #include "kbd.h"
1.1 deraadt 9:
10: #ifndef NO_MACRO
1.5 millert 11: #include "macro.h"
12: #endif /* !NO_MACRO */
1.1 deraadt 13:
14: #ifdef FKEYS
1.5 millert 15: #include "key.h"
1.1 deraadt 16: #ifndef NO_STARTUP
17: #ifndef BINDKEY
1.4 millert 18: #define BINDKEY /* bindkey is used by FKEYS startup code */
1.5 millert 19: #endif /* !BINDKEY */
20: #endif /* !NO_STARTUP */
21: #endif /* FKEYS */
22:
23: static int remap __P((KEYMAP *, int, PF, KEYMAP *));
24: static KEYMAP *realocmap __P((KEYMAP *));
25: static VOID fixmap __P((KEYMAP *, KEYMAP *, KEYMAP *));
26: static int dobind __P((KEYMAP *, char *, int));
27: static char *skipwhite __P((char *));
28: static char *parsetoken __P((char *));
29: static int bindkey __P((KEYMAP **, char *, KCHAR *, int));
1.1 deraadt 30:
1.5 millert 31: /*
32: * Insert a string, mainly for use from macros (created by selfinsert)
33: */
1.4 millert 34: /* ARGSUSED */
35: int
1.1 deraadt 36: insert(f, n)
1.4 millert 37: int f, n;
1.1 deraadt 38: {
1.5 millert 39: char *cp;
40: char buf[128];
1.1 deraadt 41: #ifndef NO_MACRO
1.5 millert 42: int count, c;
1.1 deraadt 43:
1.4 millert 44: if (inmacro) {
45: while (--n >= 0) {
46: for (count = 0; count < maclcur->l_used; count++) {
47: if ((((c = maclcur->l_text[count]) == '\n')
48: ? lnewline() : linsert(1, c)) != TRUE)
49: return FALSE;
50: }
51: }
52: maclcur = maclcur->l_fp;
53: return TRUE;
1.1 deraadt 54: }
1.4 millert 55: if (n == 1)
1.5 millert 56: /* CFINS means selfinsert can tack on the end */
57: thisflag |= CFINS;
58: #endif /* !NO_MACRO */
1.4 millert 59: if (eread("Insert: ", buf, sizeof(buf), EFNEW) == FALSE)
1.1 deraadt 60: return FALSE;
1.4 millert 61: while (--n >= 0) {
62: cp = buf;
63: while (*cp) {
64: if (((*cp == '\n') ? lnewline() : linsert(1, *cp))
65: != TRUE)
66: return FALSE;
67: cp++;
68: }
1.1 deraadt 69: }
1.4 millert 70: return TRUE;
1.1 deraadt 71: }
72:
73: /*
74: * Bind a key to a function. Cases range from the trivial (replacing an
75: * existing binding) to the extremly complex (creating a new prefix in a
76: * map_element that already has one, so the map_element must be split,
77: * but the keymap doesn't have enough room for another map_element, so
78: * the keymap is reallocated). No attempt is made to reclaim space no
79: * longer used, if this is a problem flags must be added to indicate
80: * malloced verses static storage in both keymaps and map_elements.
81: * Structure assignments would come in real handy, but K&R based compilers
82: * don't have them. Care is taken so running out of memory will leave
83: * the keymap in a usable state.
84: */
1.4 millert 85: static int
86: remap(curmap, c, funct, pref_map)
87: KEYMAP *curmap; /* pointer to the map being changed */
88: int c; /* character being changed */
89: PF funct; /* function being changed to */
90: KEYMAP *pref_map; /* if funct==prefix, map to bind to or
91: NULL for new */
1.1 deraadt 92: {
1.5 millert 93: int i, n1, n2, nold;
94: KEYMAP *mp;
95: PF *pfp;
96: MAP_ELEMENT *mep;
1.4 millert 97:
98: if (ele >= &curmap->map_element[curmap->map_num] || c < ele->k_base) {
99: if (ele > &curmap->map_element[0] && (funct != prefix ||
1.5 millert 100: (ele - 1)->k_prefmap == NULL))
1.4 millert 101: n1 = c - (ele - 1)->k_num;
1.5 millert 102: else
1.4 millert 103: n1 = HUGE;
104: if (ele < &curmap->map_element[curmap->map_num] &&
1.5 millert 105: (funct != prefix || ele->k_prefmap == NULL))
1.4 millert 106: n2 = ele->k_base - c;
1.5 millert 107: else
1.4 millert 108: n2 = HUGE;
109: if (n1 <= MAPELEDEF && n1 <= n2) {
110: ele--;
1.5 millert 111: if ((pfp = (PF *)malloc((c - ele->k_base + 1) *
1.4 millert 112: sizeof(PF))) == NULL) {
113: ewprintf("Out of memory");
114: return FALSE;
115: }
116: nold = ele->k_num - ele->k_base + 1;
117: for (i = 0; i < nold; i++)
118: pfp[i] = ele->k_funcp[i];
119: while (--n1)
120: pfp[i++] = curmap->map_default;
121: pfp[i] = funct;
122: ele->k_num = c;
123: ele->k_funcp = pfp;
124: } else if (n2 <= MAPELEDEF) {
1.5 millert 125: if ((pfp = (PF *)malloc((ele->k_num - c + 1) *
126: sizeof(PF))) == NULL) {
1.4 millert 127: ewprintf("Out of memory");
128: return FALSE;
129: }
130: nold = ele->k_num - ele->k_base + 1;
131: for (i = 0; i < nold; i++)
132: pfp[i + n2] = ele->k_funcp[i];
133: while (--n2)
134: pfp[n2] = curmap->map_default;
135: pfp[0] = funct;
136: ele->k_base = c;
137: ele->k_funcp = pfp;
1.1 deraadt 138: } else {
1.4 millert 139: if (curmap->map_num >= curmap->map_max &&
140: (curmap = realocmap(curmap)) == NULL)
141: return FALSE;
1.5 millert 142: if ((pfp = (PF *)malloc(sizeof(PF))) == NULL) {
1.4 millert 143: ewprintf("Out of memory");
144: return FALSE;
145: }
146: pfp[0] = funct;
147: for (mep = &curmap->map_element[curmap->map_num];
148: mep > ele; mep--) {
149: mep->k_base = (mep - 1)->k_base;
150: mep->k_num = (mep - 1)->k_num;
151: mep->k_funcp = (mep - 1)->k_funcp;
152: mep->k_prefmap = (mep - 1)->k_prefmap;
153: }
154: ele->k_base = c;
155: ele->k_num = c;
156: ele->k_funcp = pfp;
157: ele->k_prefmap = NULL;
158: curmap->map_num++;
159: }
160: if (funct == prefix) {
161: if (pref_map != NULL) {
162: ele->k_prefmap = pref_map;
163: } else {
1.5 millert 164: if (!(mp = (KEYMAP *)malloc(sizeof(KEYMAP) +
1.4 millert 165: (MAPINIT - 1) * sizeof(MAP_ELEMENT)))) {
166: ewprintf("Out of memory");
167: ele->k_funcp[c - ele->k_base] =
168: curmap->map_default;
169: return FALSE;
170: }
171: mp->map_num = 0;
172: mp->map_max = MAPINIT;
173: mp->map_default = rescan;
174: ele->k_prefmap = mp;
175: }
1.1 deraadt 176: }
177: } else {
1.4 millert 178: n1 = c - ele->k_base;
179: if (ele->k_funcp[n1] == funct && (funct != prefix ||
180: pref_map == NULL || pref_map == ele->k_prefmap))
1.5 millert 181: /* no change */
182: return TRUE;
1.4 millert 183: if (funct != prefix || ele->k_prefmap == NULL) {
184: if (ele->k_funcp[n1] == prefix)
185: ele->k_prefmap = (KEYMAP *) NULL;
1.5 millert 186: /* easy case */
187: ele->k_funcp[n1] = funct;
1.4 millert 188: if (funct == prefix) {
189: if (pref_map != NULL)
190: ele->k_prefmap = pref_map;
191: else {
192: if (!(mp = malloc(sizeof(KEYMAP) +
1.5 millert 193: (MAPINIT - 1) *
194: sizeof(MAP_ELEMENT)))) {
1.4 millert 195: ewprintf("Out of memory");
196: ele->k_funcp[c - ele->k_base] =
197: curmap->map_default;
198: return FALSE;
199: }
200: mp->map_num = 0;
201: mp->map_max = MAPINIT;
202: mp->map_default = rescan;
203: ele->k_prefmap = mp;
204: }
205: }
206: } else {
207: /*
1.5 millert 208: * This case is the splits.
209: * Determine which side of the break c goes on
1.4 millert 210: * 0 = after break; 1 = before break
211: */
212: n2 = 1;
213: for (i = 0; n2 && i < n1; i++)
214: n2 &= ele->k_funcp[i] != prefix;
215: if (curmap->map_num >= curmap->map_max &&
216: (curmap = realocmap(curmap)) == NULL)
217: return FALSE;
1.5 millert 218: if ((pfp = malloc((ele->k_num - c + !n2) *
219: sizeof(PF))) == NULL) {
1.4 millert 220: ewprintf("Out of memory");
221: return FALSE;
222: }
223: ele->k_funcp[n1] = prefix;
224: for (i = n1 + n2; i <= ele->k_num - ele->k_base; i++)
225: pfp[i - n1 - n2] = ele->k_funcp[i];
226: for (mep = &curmap->map_element[curmap->map_num];
227: mep > ele; mep--) {
228: mep->k_base = (mep - 1)->k_base;
229: mep->k_num = (mep - 1)->k_num;
230: mep->k_funcp = (mep - 1)->k_funcp;
231: mep->k_prefmap = (mep - 1)->k_prefmap;
232: }
233: ele->k_num = c - !n2;
234: (ele + 1)->k_base = c + n2;
235: (ele + 1)->k_funcp = pfp;
236: ele += !n2;
237: ele->k_prefmap = NULL;
238: curmap->map_num++;
239: if (pref_map == NULL) {
240: if ((mp = malloc(sizeof(KEYMAP) + (MAPINIT - 1)
241: * sizeof(MAP_ELEMENT))) == NULL) {
242: ewprintf("Out of memory");
243: ele->k_funcp[c - ele->k_base] =
244: curmap->map_default;
245: return FALSE;
246: }
247: mp->map_num = 0;
248: mp->map_max = MAPINIT;
249: mp->map_default = rescan;
250: ele->k_prefmap = mp;
251: } else
252: ele->k_prefmap = pref_map;
253: }
1.1 deraadt 254: }
255: return TRUE;
256: }
257:
1.4 millert 258: /*
259: * Reallocate a keymap, used above.
260: */
261: static KEYMAP *
262: realocmap(curmap)
1.5 millert 263: KEYMAP *curmap;
1.4 millert 264: {
1.5 millert 265: KEYMAP *mp;
266: int i;
1.4 millert 267:
1.5 millert 268: if ((mp = (KEYMAP *)malloc((unsigned)(sizeof(KEYMAP) +
269: (curmap->map_max + (MAPGROW - 1)) *
270: sizeof(MAP_ELEMENT)))) == NULL) {
1.4 millert 271: ewprintf("Out of memory");
272: return NULL;
273: }
274: mp->map_num = curmap->map_num;
275: mp->map_max = curmap->map_max + MAPGROW;
276: mp->map_default = curmap->map_default;
277: for (i = curmap->map_num; i--;) {
278: mp->map_element[i].k_base = curmap->map_element[i].k_base;
279: mp->map_element[i].k_num = curmap->map_element[i].k_num;
280: mp->map_element[i].k_funcp = curmap->map_element[i].k_funcp;
281: mp->map_element[i].k_prefmap = curmap->map_element[i].k_prefmap;
282: }
283: for (i = nmaps; i--;) {
284: if (map_table[i].p_map == curmap)
285: map_table[i].p_map = mp;
286: else
287: fixmap(curmap, mp, map_table[i].p_map);
288: }
289: ele = &mp->map_element[ele - &curmap->map_element[0]];
290: return mp;
291: }
292:
293: /*
294: * Fix references to a reallocated keymap (recursive).
295: */
296: static VOID
297: fixmap(curmap, mp, mt)
298: KEYMAP *mt;
299: KEYMAP *curmap;
300: KEYMAP *mp;
301: {
1.5 millert 302: int i;
1.4 millert 303:
304: for (i = mt->map_num; i--;) {
305: if (mt->map_element[i].k_prefmap != NULL) {
306: if (mt->map_element[i].k_prefmap == curmap)
307: mt->map_element[i].k_prefmap = mp;
308: else
309: fixmap(curmap, mp, mt->map_element[i].k_prefmap);
310: }
1.1 deraadt 311: }
312: }
313:
314: /*
315: * do the input for local-set-key, global-set-key and define-key
316: * then call remap to do the work.
317: */
1.4 millert 318: static int
319: dobind(curmap, p, unbind)
320: KEYMAP *curmap;
321: char *p;
322: int unbind;
323: {
1.5 millert 324: KEYMAP *pref_map = NULL;
325: PF funct;
326: char prompt[80];
327: char *pep;
328: int c, s;
1.1 deraadt 329:
330: #ifndef NO_MACRO
1.4 millert 331: if (macrodef) {
332: /*
333: * Keystrokes aren't collected. Not hard, but pretty useless.
334: * Would not work for function keys in any case.
335: */
336: ewprintf("Can't rebind key in macro");
337: return FALSE;
1.1 deraadt 338: }
339: #ifndef NO_STARTUP
1.4 millert 340: if (inmacro) {
341: for (s = 0; s < maclcur->l_used - 1; s++) {
342: if (doscan(curmap, c = CHARMASK(maclcur->l_text[s]))
343: != prefix) {
1.5 millert 344: if (remap(curmap, c, prefix, (KEYMAP *)NULL)
1.4 millert 345: != TRUE)
346: return FALSE;
347: }
348: curmap = ele->k_prefmap;
1.1 deraadt 349: }
1.5 millert 350: (VOID)doscan(curmap, c = maclcur->l_text[s]);
1.4 millert 351: maclcur = maclcur->l_fp;
1.1 deraadt 352: } else {
1.5 millert 353: #endif /* !NO_STARTUP */
354: #endif /* !NO_MACRO */
355: (VOID)strcpy(prompt, p);
1.4 millert 356: pep = prompt + strlen(prompt);
357: for (;;) {
358: ewprintf("%s", prompt);
359: pep[-1] = ' ';
360: pep = keyname(pep, c = getkey(FALSE));
361: if (doscan(curmap, c) != prefix)
362: break;
363: *pep++ = '-';
364: *pep = '\0';
365: curmap = ele->k_prefmap;
366: }
1.1 deraadt 367: #ifndef NO_STARTUP
368: }
1.5 millert 369: #endif /* !NO_STARTUP */
1.4 millert 370: if (unbind)
371: funct = rescan;
1.1 deraadt 372: else {
1.4 millert 373: if ((s = eread("%s to command: ", prompt, 80, EFFUNC | EFNEW,
374: prompt)) != TRUE)
375: return s;
376: if (((funct = name_function(prompt)) == prefix) ?
377: (pref_map = name_map(prompt)) == NULL : funct == NULL) {
378: ewprintf("[No match]");
379: return FALSE;
380: }
1.1 deraadt 381: }
382: return remap(curmap, c, funct, pref_map);
383: }
384:
385: /*
1.5 millert 386: * bindkey: bind key sequence to a function in the specified map. Used by
387: * excline so it can bind function keys. To close to release to change
388: * calling sequence, should just pass KEYMAP *curmap rather than
389: * KEYMAP **mapp.
390: */
1.1 deraadt 391: #ifdef BINDKEY
1.5 millert 392: static int
1.1 deraadt 393: bindkey(mapp, fname, keys, kcount)
1.4 millert 394: KEYMAP **mapp;
1.5 millert 395: char *fname;
396: KCHAR *keys;
397: int kcount;
398: {
399: KEYMAP *curmap = *mapp;
400: KEYMAP *pref_map = NULL;
401: PF funct;
402: int c;
1.1 deraadt 403:
1.4 millert 404: if (fname == NULL)
405: funct = rescan;
1.1 deraadt 406: else if (((funct = name_function(fname)) == prefix) ?
1.5 millert 407: (pref_map = name_map(fname)) == NULL : funct == NULL) {
1.4 millert 408: ewprintf("[No match: %s]", fname);
409: return FALSE;
410: }
411: while (--kcount) {
412: if (doscan(curmap, c = *keys++) != prefix) {
1.5 millert 413: if (remap(curmap, c, prefix, (KEYMAP *)NULL) != TRUE)
1.4 millert 414: return FALSE;
415: }
416: curmap = ele->k_prefmap;
1.1 deraadt 417: }
1.5 millert 418: (VOID)doscan(curmap, c = *keys);
1.1 deraadt 419: return remap(curmap, c, funct, pref_map);
420: }
1.3 millert 421:
422: #ifdef FKEYS
423: /*
424: * Wrapper for bindkey() that converts escapes.
425: */
426: int
427: dobindkey(map, func, str)
428: KEYMAP *map;
1.4 millert 429: char *func;
430: char *str;
1.3 millert 431: {
1.5 millert 432: int i;
1.3 millert 433:
434: for (i = 0; *str && i < MAXKEY; i++) {
435: /* XXX - convert numbers w/ strol()? */
436: if (*str != '\\')
437: key.k_chars[i] = *str;
438: else {
1.4 millert 439: switch (*++str) {
440: case 't':
441: case 'T':
1.3 millert 442: key.k_chars[i] = '\t';
443: break;
1.4 millert 444: case 'n':
445: case 'N':
1.3 millert 446: key.k_chars[i] = '\n';
447: break;
1.4 millert 448: case 'r':
449: case 'R':
1.3 millert 450: key.k_chars[i] = '\r';
451: break;
1.4 millert 452: case 'e':
453: case 'E':
1.3 millert 454: key.k_chars[i] = CCHR('[');
455: break;
456: }
457: }
458: str++;
459: }
460: key.k_count = i;
1.4 millert 461: return (bindkey(&map, func, key.k_chars, key.k_count));
1.3 millert 462: }
1.5 millert 463: #endif /* FKEYS */
464: #endif /* BINDKEY */
1.1 deraadt 465:
466: /*
467: * This function modifies the fundamental keyboard map.
468: */
1.4 millert 469: /* ARGSUSED */
470: int
1.1 deraadt 471: bindtokey(f, n)
1.4 millert 472: int f, n;
1.1 deraadt 473: {
1.4 millert 474: return dobind(map_table[0].p_map, "Global set key: ", FALSE);
1.1 deraadt 475: }
476:
477: /*
478: * This function modifies the current mode's keyboard map.
479: */
1.4 millert 480: /* ARGSUSED */
481: int
1.1 deraadt 482: localbind(f, n)
1.4 millert 483: int f, n;
1.1 deraadt 484: {
1.5 millert 485: return dobind(curbp->b_modes[curbp->b_nmodes]->p_map,
486: "Local set key: ", FALSE);
1.1 deraadt 487: }
488:
489: /*
490: * This function redefines a key in any keymap.
491: */
1.4 millert 492: /* ARGSUSED */
493: int
1.1 deraadt 494: define_key(f, n)
1.4 millert 495: int f, n;
1.1 deraadt 496: {
1.5 millert 497: static char buf[48] = "Define key map: ";
498: MAPS *mp;
1.4 millert 499:
500: buf[16] = '\0';
501: if (eread(buf, &buf[16], 48 - 16, EFNEW) != TRUE)
502: return FALSE;
503: if ((mp = name_mode(&buf[16])) == NULL) {
504: ewprintf("Unknown map %s", &buf[16]);
505: return FALSE;
506: }
1.5 millert 507: (VOID)strncat(&buf[16], " key: ", 48 - 16 - 1);
1.4 millert 508: return dobind(mp->p_map, buf, FALSE);
1.1 deraadt 509: }
510:
1.4 millert 511: int
1.1 deraadt 512: unbindtokey(f, n)
1.4 millert 513: int f, n;
1.1 deraadt 514: {
1.4 millert 515: return dobind(map_table[0].p_map, "Global unset key: ", TRUE);
1.1 deraadt 516: }
517:
1.4 millert 518: int
1.1 deraadt 519: localunbind(f, n)
1.4 millert 520: int f, n;
1.1 deraadt 521: {
1.4 millert 522: return dobind(curbp->b_modes[curbp->b_nmodes]->p_map,
523: "Local unset key: ", TRUE);
1.1 deraadt 524: }
525:
526: /*
1.5 millert 527: * Extended command. Call the message line routine to read in the command
528: * name and apply autocompletion to it. When it comes back, look the name
529: * up in the symbol table and run the command if it is found. Print an
530: * error if there is anything wrong.
1.1 deraadt 531: */
1.4 millert 532: int
1.1 deraadt 533: extend(f, n)
1.5 millert 534: int f, n;
1.1 deraadt 535: {
1.5 millert 536: PF funct;
537: int s;
538: char xname[NXNAME];
1.4 millert 539:
540: if (!(f & FFARG))
541: s = eread("M-x ", xname, NXNAME, EFNEW | EFFUNC);
542: else
543: s = eread("%d M-x ", xname, NXNAME, EFNEW | EFFUNC, n);
544: if (s != TRUE)
545: return s;
546: if ((funct = name_function(xname)) != NULL) {
1.1 deraadt 547: #ifndef NO_MACRO
1.4 millert 548: if (macrodef) {
1.5 millert 549: LINE *lp = maclcur;
1.4 millert 550: macro[macrocount - 1].m_funct = funct;
551: maclcur = lp->l_bp;
552: maclcur->l_fp = lp->l_fp;
1.5 millert 553: free((char *)lp);
1.4 millert 554: }
1.5 millert 555: #endif /* !NO_MACRO */
556: return (*funct)(f, n);
1.1 deraadt 557: }
558: ewprintf("[No match]");
559: return FALSE;
560: }
561:
562: #ifndef NO_STARTUP
563: /*
564: * Define the commands needed to do startup-file processing.
565: * This code is mostly a kludge just so we can get startup-file processing.
566: *
567: * If you're serious about having this code, you should rewrite it.
568: * To wit:
569: * It has lots of funny things in it to make the startup-file look
570: * like a GNU startup file; mostly dealing with parens and semicolons.
571: * This should all vanish.
572: *
573: * We define eval-expression because it's easy. It can make
574: * *-set-key or define-key set an arbitrary key sequence, so it isn't
575: * useless.
576: */
577:
578: /*
579: * evalexpr - get one line from the user, and run it.
580: */
1.4 millert 581: /* ARGSUSED */
582: int
1.1 deraadt 583: evalexpr(f, n)
1.5 millert 584: int f, n;
1.1 deraadt 585: {
1.5 millert 586: int s;
587: char exbuf[128];
1.1 deraadt 588:
589: if ((s = ereply("Eval: ", exbuf, 128)) != TRUE)
590: return s;
591: return excline(exbuf);
592: }
1.4 millert 593:
1.1 deraadt 594: /*
1.5 millert 595: * evalbuffer - evaluate the current buffer as line commands. Useful for
596: * testing startup files.
1.1 deraadt 597: */
1.4 millert 598: /* ARGSUSED */
599: int
1.1 deraadt 600: evalbuffer(f, n)
1.5 millert 601: int f, n;
1.1 deraadt 602: {
1.5 millert 603: LINE *lp;
604: BUFFER *bp = curbp;
605: int s;
606: static char excbuf[128];
1.1 deraadt 607:
608: for (lp = lforw(bp->b_linep); lp != bp->b_linep; lp = lforw(lp)) {
1.4 millert 609: if (llength(lp) >= 128)
610: return FALSE;
1.5 millert 611: (VOID)strncpy(excbuf, ltext(lp), llength(lp));
612:
613: /* make sure it's terminated */
614: excbuf[llength(lp)] = '\0';
1.4 millert 615: if ((s = excline(excbuf)) != TRUE)
616: return s;
1.1 deraadt 617: }
618: return TRUE;
619: }
1.4 millert 620:
1.1 deraadt 621: /*
622: * evalfile - go get a file and evaluate it as line commands. You can
623: * go get your own startup file if need be.
624: */
1.4 millert 625: /* ARGSUSED */
626: int
1.1 deraadt 627: evalfile(f, n)
1.5 millert 628: int f, n;
1.1 deraadt 629: {
1.5 millert 630: int s;
631: char fname[NFILEN];
1.1 deraadt 632:
633: if ((s = ereply("Load file: ", fname, NFILEN)) != TRUE)
634: return s;
635: return load(fname);
636: }
637:
638: /*
639: * load - go load the file name we got passed.
640: */
1.4 millert 641: int
642: load(fname)
1.5 millert 643: char *fname;
1.4 millert 644: {
1.5 millert 645: int s = TRUE;
646: int nbytes;
647: char excbuf[128];
1.1 deraadt 648:
649: if ((fname = adjustname(fname)) == NULL)
1.5 millert 650: /* just to be careful */
651: return FALSE;
1.1 deraadt 652:
1.5 millert 653: if (ffropen(fname, (BUFFER *)NULL) != FIOSUC)
1.4 millert 654: return FALSE;
1.5 millert 655:
1.4 millert 656: while ((s = ffgetline(excbuf, sizeof(excbuf) - 1, &nbytes)) == FIOSUC) {
1.1 deraadt 657: excbuf[nbytes] = '\0';
658: if (excline(excbuf) != TRUE) {
659: s = FIOERR;
660: ewprintf("Error loading file %s", fname);
661: break;
662: }
663: }
1.5 millert 664: (VOID)ffclose((BUFFER *)NULL);
1.1 deraadt 665: excbuf[nbytes] = '\0';
1.4 millert 666: if (s != FIOEOF || (nbytes && excline(excbuf) != TRUE))
1.1 deraadt 667: return FALSE;
668: return TRUE;
669: }
670:
671: /*
1.5 millert 672: * excline - run a line from a load file or eval-expression. if FKEYS is
673: * defined, duplicate functionallity of dobind so function key values don't
674: * have to fit in type char.
1.1 deraadt 675: */
1.4 millert 676: int
1.1 deraadt 677: excline(line)
1.5 millert 678: char *line;
1.1 deraadt 679: {
1.5 millert 680: PF fp;
681: LINE *lp, *np;
682: int status, c, f, n;
683: char *funcp;
684: char *argp = NULL;
685: #ifdef FKEYS
686: int bind;
687: KEYMAP *curmap;
688: MAPS *mp;
689: #define BINDARG 0 /* this arg is key to bind (local/global set key) */
690: #define BINDNO 1 /* not binding or non-quoted BINDARG */
691: #define BINDNEXT 2 /* next arg " (define-key) */
692: #define BINDDO 3 /* already found key to bind */
693: #define BINDEXT 1 /* space for trailing \0 */
694: #else /* FKEYS */
695: #define BINDEXT 0
696: #endif /* FKEYS */
697:
698: lp = NULL;
1.1 deraadt 699:
1.4 millert 700: if (macrodef || inmacro) {
1.1 deraadt 701: ewprintf("Not now!");
702: return FALSE;
703: }
704: f = 0;
705: n = 1;
706: funcp = skipwhite(line);
1.4 millert 707: if (*funcp == '\0')
708: return TRUE; /* No error on blank lines */
1.1 deraadt 709: line = parsetoken(funcp);
710: if (*line != '\0') {
711: *line++ = '\0';
712: line = skipwhite(line);
713: if ((*line >= '0' && *line <= '9') || *line == '-') {
714: argp = line;
715: line = parsetoken(line);
716: }
717: }
718: if (argp != NULL) {
719: f = FFARG;
720: n = atoi(argp);
721: }
1.4 millert 722: if ((fp = name_function(funcp)) == NULL) {
723: ewprintf("Unknown function: %s", funcp);
724: return FALSE;
1.1 deraadt 725: }
726: #ifdef FKEYS
1.4 millert 727: if (fp == bindtokey || fp == unbindtokey) {
1.1 deraadt 728: bind = BINDARG;
729: curmap = map_table[0].p_map;
1.4 millert 730: } else if (fp == localbind || fp == localunbind) {
1.1 deraadt 731: bind = BINDARG;
732: curmap = curbp->b_modes[curbp->b_nmodes]->p_map;
1.4 millert 733: } else if (fp == define_key)
734: bind = BINDNEXT;
735: else
736: bind = BINDNO;
1.5 millert 737: #endif /* FKEYS */
738: /* Pack away all the args now... */
1.4 millert 739: if ((np = lalloc(0)) == FALSE)
740: return FALSE;
1.1 deraadt 741: np->l_fp = np->l_bp = maclcur = np;
742: while (*line != '\0') {
743: argp = skipwhite(line);
1.4 millert 744: if (*argp == '\0')
745: break;
1.1 deraadt 746: line = parsetoken(argp);
747: if (*argp != '"') {
1.4 millert 748: if (*argp == '\'')
749: ++argp;
750: if (!(lp = lalloc((int) (line - argp) + BINDEXT))) {
751: status = FALSE;
752: goto cleanup;
753: }
1.5 millert 754: bcopy(argp, ltext(lp), (int)(line - argp));
1.1 deraadt 755: #ifdef FKEYS
1.5 millert 756: /* don't count BINDEXT */
757: lp->l_used--;
1.4 millert 758: if (bind == BINDARG)
759: bind = BINDNO;
1.5 millert 760: #endif /* FKEYS */
761: } else {
762: /* quoted strings are special */
1.4 millert 763: ++argp;
1.1 deraadt 764: #ifdef FKEYS
1.4 millert 765: if (bind != BINDARG) {
1.5 millert 766: #endif /* FKEYS */
767: lp = lalloc((int)(line - argp) + BINDEXT);
1.4 millert 768: if (lp == NULL) {
769: status = FALSE;
770: goto cleanup;
771: }
772: lp->l_used = 0;
1.1 deraadt 773: #ifdef FKEYS
1.4 millert 774: } else {
775: key.k_count = 0;
1.1 deraadt 776: }
1.5 millert 777: #endif /* FKEYS */
1.4 millert 778: while (*argp != '"' && *argp != '\0') {
779: if (*argp != '\\')
780: c = *argp++;
781: else {
782: switch (*++argp) {
783: case 't':
784: case 'T':
785: c = CCHR('I');
786: break;
787: case 'n':
788: case 'N':
789: c = CCHR('J');
790: break;
791: case 'r':
792: case 'R':
793: c = CCHR('M');
794: break;
795: case 'e':
796: case 'E':
797: c = CCHR('[');
798: break;
799: case '^':
800: /*
801: * split into two statements
802: * due to bug in OSK cpp
803: */
804: c = CHARMASK(*++argp);
805: c = ISLOWER(c) ?
806: CCHR(TOUPPER(c)) : CCHR(c);
807: break;
808: case '0':
809: case '1':
810: case '2':
811: case '3':
812: case '4':
813: case '5':
814: case '6':
815: case '7':
816: c = *argp - '0';
1.5 millert 817: if (argp[1] <= '7' &&
818: argp[1] >= '0') {
1.4 millert 819: c <<= 3;
820: c += *++argp - '0';
821: if (argp[1] <= '7' &&
822: argp[1] >= '0') {
823: c <<= 3;
824: c += *++argp
825: - '0';
826: }
827: }
828: break;
829: #ifdef FKEYS
830: case 'f':
831: case 'F':
832: c = *++argp - '0';
833: if (ISDIGIT(argp[1])) {
834: c *= 10;
835: c += *++argp - '0';
836: }
837: c += KFIRST;
838: break;
1.5 millert 839: #endif /* FKEYS */
1.4 millert 840: default:
841: c = CHARMASK(*argp);
842: break;
843: }
844: argp++;
845: }
1.1 deraadt 846: #ifdef FKEYS
1.4 millert 847: if (bind == BINDARG)
848: key.k_chars[key.k_count++] = c;
849: else
1.5 millert 850: #endif /* FKEYS */
1.4 millert 851: lp->l_text[lp->l_used++] = c;
852: }
853: if (*line)
854: line++;
1.1 deraadt 855: }
856: #ifdef FKEYS
1.4 millert 857: switch (bind) {
858: case BINDARG:
1.1 deraadt 859: bind = BINDDO;
860: break;
1.4 millert 861: case BINDNEXT:
1.1 deraadt 862: lp->l_text[lp->l_used] = '\0';
1.4 millert 863: if ((mp = name_mode(lp->l_text)) == NULL) {
864: ewprintf("No such mode: %s", lp->l_text);
865: status = FALSE;
1.5 millert 866: free((char *)lp);
1.4 millert 867: goto cleanup;
1.1 deraadt 868: }
869: curmap = mp->p_map;
1.5 millert 870: free((char *)lp);
1.1 deraadt 871: bind = BINDARG;
872: break;
1.4 millert 873: default:
1.5 millert 874: #endif /* FKEYS */
1.1 deraadt 875: lp->l_fp = np->l_fp;
876: lp->l_bp = np;
877: np->l_fp = lp;
878: np = lp;
879: #ifdef FKEYS
880: }
1.5 millert 881: #endif /* FKEYS */
1.1 deraadt 882: }
883: #ifdef FKEYS
1.4 millert 884: switch (bind) {
885: default:
1.1 deraadt 886: ewprintf("Bad args to set key");
887: status = FALSE;
888: break;
1.4 millert 889: case BINDDO:
890: if (fp != unbindtokey && fp != localunbind) {
891: lp->l_text[lp->l_used] = '\0';
892: status = bindkey(&curmap, lp->l_text, key.k_chars,
893: key.k_count);
894: } else
1.5 millert 895: status = bindkey(&curmap, (char *)NULL, key.k_chars,
1.4 millert 896: key.k_count);
1.1 deraadt 897: break;
1.4 millert 898: case BINDNO:
1.5 millert 899: #endif /* FKEYS */
1.1 deraadt 900: inmacro = TRUE;
901: maclcur = maclcur->l_fp;
1.5 millert 902: status = (*fp)(f, n);
1.1 deraadt 903: inmacro = FALSE;
904: #ifdef FKEYS
905: }
1.5 millert 906: #endif /* FKEYS */
1.1 deraadt 907: cleanup:
908: lp = maclcur->l_fp;
1.4 millert 909: while (lp != maclcur) {
910: np = lp->l_fp;
1.5 millert 911: free((char *)lp);
1.4 millert 912: lp = np;
1.1 deraadt 913: }
1.5 millert 914: free((char *)lp);
1.1 deraadt 915: return status;
916: }
917:
918: /*
919: * a pair of utility functions for the above
920: */
921: static char *
922: skipwhite(s)
1.5 millert 923: char *s;
1.1 deraadt 924: {
1.4 millert 925: while (*s == ' ' || *s == '\t' || *s == ')' || *s == '(')
926: s++;
927: if (*s == ';')
928: *s = '\0';
1.1 deraadt 929: return s;
930: }
931:
932: static char *
933: parsetoken(s)
1.4 millert 934: char *s;
1.1 deraadt 935: {
936: if (*s != '"') {
1.4 millert 937: while (*s && *s != ' ' && *s != '\t' && *s != ')' && *s != '(')
938: s++;
939: if (*s == ';')
940: *s = '\0';
1.1 deraadt 941: } else
1.5 millert 942: do {
943: /*
944: * Strings get special treatment.
945: * Beware: You can \ out the end of the string!
946: */
1.4 millert 947: if (*s == '\\')
948: ++s;
949: } while (*++s != '"' && *s != '\0');
1.1 deraadt 950: return s;
951: }
1.5 millert 952: #endif /* !NO_STARTUP */