Annotation of src/usr.bin/mg/help.c, Revision 1.16
1.16 ! vincent 1: /* $OpenBSD: help.c,v 1.15 2001/11/25 07:34:17 deraadt Exp $ */
1.4 niklas 2:
1.3 millert 3: /*
1.12 mickey 4: * Help functions for Mg 2
1.3 millert 5: */
1.1 deraadt 6:
7: #include "def.h"
1.14 art 8: #include "funmap.h"
1.1 deraadt 9:
10: #ifndef NO_HELP
11: #include "kbd.h"
12: #include "key.h"
13: #ifndef NO_MACRO
14: #include "macro.h"
1.3 millert 15: #endif /* !NO_MACRO */
1.2 millert 16:
1.8 art 17: static int showall(BUFFER *, KEYMAP *, char *);
1.11 mickey 18: static int findbind(KEYMAP *, PF, char *, size_t);
1.1 deraadt 19:
20: /*
1.2 millert 21: * Read a key from the keyboard, and look it up in the keymap.
22: * Display the name of the function currently bound to the key.
1.1 deraadt 23: */
1.2 millert 24: /* ARGSUSED */
25: int
1.1 deraadt 26: desckey(f, n)
1.3 millert 27: int f, n;
1.1 deraadt 28: {
1.3 millert 29: KEYMAP *curmap;
30: PF funct;
31: int c, m, i;
32: char *pep;
33: char prompt[80];
1.1 deraadt 34:
35: #ifndef NO_MACRO
1.2 millert 36: if (inmacro)
37: return TRUE; /* ignore inside keyboard macro */
1.3 millert 38: #endif /* !NO_MACRO */
1.11 mickey 39: pep = prompt + strlcpy(prompt, "Describe key briefly: ", sizeof(prompt));
1.2 millert 40: key.k_count = 0;
41: m = curbp->b_nmodes;
42: curmap = curbp->b_modes[m]->p_map;
43: for (;;) {
44: for (;;) {
45: ewprintf("%s", prompt);
46: pep[-1] = ' ';
1.11 mickey 47: pep = keyname(pep, sizeof(prompt) - (pep - prompt),
48: key.k_chars[key.k_count++] = c = getkey(FALSE));
1.6 art 49: if ((funct = doscan(curmap, c, &curmap)) != NULL)
1.2 millert 50: break;
51: *pep++ = '-';
52: *pep = '\0';
53: }
54: if (funct != rescan)
55: break;
56: if (ISUPPER(key.k_chars[key.k_count - 1])) {
57: funct = doscan(curmap,
1.6 art 58: TOLOWER(key.k_chars[key.k_count - 1]), &curmap);
1.5 art 59: if (funct == NULL) {
1.2 millert 60: *pep++ = '-';
61: *pep = '\0';
62: continue;
63: }
64: if (funct != rescan)
65: break;
66: }
67: nextmode:
68: if (--m < 0)
69: break;
70: curmap = curbp->b_modes[m]->p_map;
71: for (i = 0; i < key.k_count; i++) {
1.6 art 72: funct = doscan(curmap, key.k_chars[i], &curmap);
1.5 art 73: if (funct != NULL) {
1.2 millert 74: if (i == key.k_count - 1 && funct != rescan)
75: goto found;
76: funct = rescan;
77: goto nextmode;
78: }
79: }
1.1 deraadt 80: *pep++ = '-';
81: *pep = '\0';
82: }
83: found:
1.7 art 84: if (funct == rescan || funct == selfinsert)
1.2 millert 85: ewprintf("%k is not bound to any function");
1.16 ! vincent 86: else if ((pep = (char *)function_name(funct)) != NULL)
1.2 millert 87: ewprintf("%k runs the command %s", pep);
88: else
89: ewprintf("%k is bound to an unnamed function");
90: return TRUE;
1.1 deraadt 91: }
92:
93: /*
1.2 millert 94: * This function creates a table, listing all of the command
95: * keys and their current bindings, and stores the table in the
96: * *help* pop-up buffer. This lets Mg produce it's own wall chart.
1.1 deraadt 97: */
1.2 millert 98: /* ARGSUSED */
99: int
1.1 deraadt 100: wallchart(f, n)
1.3 millert 101: int f, n;
1.1 deraadt 102: {
1.3 millert 103: int m;
1.8 art 104: BUFFER *bp;
1.1 deraadt 105:
106: bp = bfind("*help*", TRUE);
1.2 millert 107: if (bclear(bp) != TRUE)
1.3 millert 108: /* clear it out */
109: return FALSE;
1.2 millert 110: for (m = curbp->b_nmodes; m > 0; m--) {
1.8 art 111: if ((addlinef(bp, "Local keybindings for mode %s:",
112: curbp->b_modes[m]->p_name) == FALSE) ||
113: (showall(bp, curbp->b_modes[m]->p_map, "") == FALSE) ||
1.2 millert 114: (addline(bp, "") == FALSE))
115: return FALSE;
1.1 deraadt 116: }
1.2 millert 117: if ((addline(bp, "Global bindings:") == FALSE) ||
1.9 art 118: (showall(bp, fundamental_map, "") == FALSE))
1.2 millert 119: return FALSE;
1.1 deraadt 120: return popbuftop(bp);
121: }
122:
1.2 millert 123: static int
1.8 art 124: showall(BUFFER *bp, KEYMAP *map, char *prefix)
1.1 deraadt 125: {
1.8 art 126: KEYMAP *newmap;
127: char buf[80], key[16];
128: PF fun;
129: int c;
1.1 deraadt 130:
1.2 millert 131: if (addline(bp, "") == FALSE)
132: return FALSE;
1.8 art 133:
134: /* XXX - 256 ? */
135: for (c = 0; c < 256; c++) {
136: fun = doscan(map, c, &newmap);
137: if (fun == rescan || fun == selfinsert)
138: continue;
1.13 art 139: keyname(buf, sizeof(buf), c);
1.15 deraadt 140: snprintf(key, sizeof key, "%s%s ", prefix, buf);
1.8 art 141: if (fun == NULL) {
142: if (showall(bp, newmap, key) == FALSE)
1.2 millert 143: return FALSE;
1.8 art 144: } else {
145: if (addlinef(bp, "%-16s%s", key,
146: function_name(fun)) == FALSE)
1.2 millert 147: return FALSE;
1.1 deraadt 148: }
149: }
1.8 art 150:
1.1 deraadt 151: return TRUE;
152: }
153:
1.2 millert 154: int
1.1 deraadt 155: help_help(f, n)
1.3 millert 156: int f, n;
1.1 deraadt 157: {
1.3 millert 158: KEYMAP *kp;
159: PF funct;
1.1 deraadt 160:
1.2 millert 161: if ((kp = name_map("help")) == NULL)
162: return FALSE;
163: ewprintf("a b c: ");
164: do {
1.6 art 165: funct = doscan(kp, getkey(FALSE), NULL);
1.2 millert 166: } while (funct == NULL || funct == help_help);
1.1 deraadt 167: #ifndef NO_MACRO
1.2 millert 168: if (macrodef && macrocount < MAXMACRO)
169: macro[macrocount - 1].m_funct = funct;
1.3 millert 170: #endif /* !NO_MACRO */
171: return (*funct)(f, n);
1.1 deraadt 172: }
173:
1.2 millert 174: /* ARGSUSED */
175: int
1.1 deraadt 176: apropos_command(f, n)
1.3 millert 177: int f, n;
1.1 deraadt 178: {
1.3 millert 179: BUFFER *bp;
1.8 art 180: LIST *fnames, *el;
1.3 millert 181: char string[32];
1.1 deraadt 182:
1.2 millert 183: if (eread("apropos: ", string, sizeof(string), EFNEW) == ABORT)
184: return ABORT;
1.1 deraadt 185: /* FALSE means we got a 0 character string, which is fine */
1.2 millert 186: bp = bfind("*help*", TRUE);
187: if (bclear(bp) == FALSE)
188: return FALSE;
1.8 art 189:
190: fnames = complete_function_list("", NULL);
191: for (el = fnames; el != NULL; el = el->l_next) {
192: char buf[32];
1.9 art 193:
1.8 art 194: if (strstr(el->l_name, string) == NULL)
195: continue;
1.9 art 196:
1.8 art 197: buf[0] = '\0';
1.11 mickey 198: findbind(fundamental_map, name_function(el->l_name),
199: buf, sizeof(buf));
1.8 art 200:
201: if (addlinef(bp, "%-32s%s", el->l_name, buf) == FALSE) {
202: free_file_list(fnames);
203: return FALSE;
1.2 millert 204: }
1.1 deraadt 205: }
1.8 art 206: free_file_list(fnames);
1.2 millert 207: return popbuftop(bp);
1.1 deraadt 208: }
209:
1.8 art 210: static int
1.11 mickey 211: findbind(KEYMAP *map, PF fun, char *buf, size_t len)
1.1 deraadt 212: {
1.8 art 213: KEYMAP *newmap;
214: PF nfun;
215: char buf2[16], key[16];
216: int c;
217:
218: /* XXX - 256 ? */
219: for (c = 0; c < 256; c++) {
220: nfun = doscan(map, c, &newmap);
221: if (nfun == fun) {
1.13 art 222: keyname(buf, len, c);
1.8 art 223: return TRUE;
1.2 millert 224: }
1.8 art 225: if (nfun == NULL) {
1.11 mickey 226: if (findbind(newmap, fun, buf2, sizeof(buf2)) == TRUE) {
1.13 art 227: keyname(key, sizeof(key), c);
1.11 mickey 228: snprintf(buf, len, "%s %s", key, buf2);
1.8 art 229: return TRUE;
1.2 millert 230: }
231: }
1.1 deraadt 232: }
233:
1.12 mickey 234: return FALSE;
1.1 deraadt 235: }
1.3 millert 236: #endif /* !NO_HELP */