main
help.c
1/* Copyright (c) 1985 Ceriel J.H. Jacobs */
2
3#include "in_all.h"
4#include "help.h"
5#include "machine.h"
6#include "commands.h"
7#include "keys.h"
8#include "output.h"
9#include "prompt.h"
10#include "main.h"
11#include "display.h"
12#include "term.h"
13#include "options.h"
14
15static int h_cnt; /* Count # of lines */
16static struct state *origin; /* Keep track of startstate */
17
18/*
19 * Print a key sequence.
20 * We arrived at an endstate. The s_next link in the state structure now
21 * leads us from "origin" to the current state, so that we can print the key
22 * sequence easily.
23 */
24
25static void
26pr_comm()
27{
28 struct state *p = origin;
29 char *pb;
30 int c;
31 char buf[30];
32 int i = 0; /* How many characters printed? */
33
34 pb = buf;
35 for (;;) {
36 c = p->s_char & 0177;
37 if (c < ' ' || c == 0177) {
38 /*
39 * Will take an extra position
40 */
41 i++;
42 }
43 *pb++ = c;
44 i++;
45 if (!p->s_match)
46 break;
47 p = p->s_next;
48 }
49 do {
50 *pb++ = ' ';
51 } while (++i < 12);
52 *pb = 0;
53 cputline(buf);
54}
55
56/*
57 * Print out a description of the keymap. This is done, by temporarily using
58 * the s_next field in the state structure indicate the state matching the
59 * next character, so that we can walk from "origin" to an endstate.
60 */
61
62static void
63pr_mach(struct state *currstate, struct state *back)
64{
65 struct state *save;
66
67 while (currstate) {
68 if (interrupt)
69 break;
70 if (back) {
71 save = back->s_next; /* Save original link */
72 back->s_next = currstate;
73 }
74 if (!currstate->s_match) {
75 /*
76 * End state, print command
77 */
78 pr_comm();
79 putline(commands[currstate->s_cnt].c_descr);
80 putline("\r\n");
81 if (++h_cnt >= maxpagesize) {
82 ret_to_continue();
83 h_cnt = 0;
84 }
85 } else
86 pr_mach(currstate->s_match, currstate);
87 currstate = currstate->s_next;
88 if (back)
89 back->s_next = save; /* restore */
90 else
91 origin = currstate;
92 }
93}
94
95/*ARGSUSED*/
96int
97do_help(long i)
98{ /* The help command */
99 (void)i;
100
101 startcomm = 0;
102 h_cnt = 2;
103 putline("\r\nSummary of yap commands:\r\n");
104 origin = currmap->k_mach;
105 pr_mach(currmap->k_mach, (struct state *)0);
106 if (h_cnt) {
107 ret_to_continue();
108 }
109 if (!hardcopy && scr_info.currentpos)
110 redraw(1);
111 return 0;
112}