xref: /freebsd/contrib/nvi/ex/ex_usage.c (revision 110d525ec6188f3c9dc4f54c4bc1cced2f7184cd)
1b8ba871bSPeter Wemm /*-
2b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994
3b8ba871bSPeter Wemm  *	The Regents of the University of California.  All rights reserved.
4b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5b8ba871bSPeter Wemm  *	Keith Bostic.  All rights reserved.
6b8ba871bSPeter Wemm  *
7b8ba871bSPeter Wemm  * See the LICENSE file for redistribution information.
8b8ba871bSPeter Wemm  */
9b8ba871bSPeter Wemm 
10b8ba871bSPeter Wemm #include "config.h"
11b8ba871bSPeter Wemm 
12b8ba871bSPeter Wemm #include <sys/types.h>
13b8ba871bSPeter Wemm #include <sys/queue.h>
14b8ba871bSPeter Wemm #include <sys/time.h>
15b8ba871bSPeter Wemm 
16b8ba871bSPeter Wemm #include <bitstring.h>
17b8ba871bSPeter Wemm #include <ctype.h>
18b8ba871bSPeter Wemm #include <limits.h>
19b8ba871bSPeter Wemm #include <stdio.h>
20b8ba871bSPeter Wemm #include <stdlib.h>
21b8ba871bSPeter Wemm #include <string.h>
22b8ba871bSPeter Wemm 
23b8ba871bSPeter Wemm #include "../common/common.h"
24b8ba871bSPeter Wemm #include "../vi/vi.h"
25b8ba871bSPeter Wemm 
26b8ba871bSPeter Wemm /*
27b8ba871bSPeter Wemm  * ex_help -- :help
28b8ba871bSPeter Wemm  *	Display help message.
29b8ba871bSPeter Wemm  *
30c271fa92SBaptiste Daroussin  * PUBLIC: int ex_help(SCR *, EXCMD *);
31b8ba871bSPeter Wemm  */
32b8ba871bSPeter Wemm int
ex_help(SCR * sp,EXCMD * cmdp)33f0957ccaSPeter Wemm ex_help(SCR *sp, EXCMD *cmdp)
34b8ba871bSPeter Wemm {
35b8ba871bSPeter Wemm 	(void)ex_puts(sp,
36b8ba871bSPeter Wemm 	    "To see the list of vi commands, enter \":viusage<CR>\"\n");
37b8ba871bSPeter Wemm 	(void)ex_puts(sp,
38b8ba871bSPeter Wemm 	    "To see the list of ex commands, enter \":exusage<CR>\"\n");
39b8ba871bSPeter Wemm 	(void)ex_puts(sp,
40b8ba871bSPeter Wemm 	    "For an ex command usage statement enter \":exusage [cmd]<CR>\"\n");
41b8ba871bSPeter Wemm 	(void)ex_puts(sp,
42b8ba871bSPeter Wemm 	    "For a vi key usage statement enter \":viusage [key]<CR>\"\n");
43b8ba871bSPeter Wemm 	(void)ex_puts(sp, "To exit, enter \":q!\"\n");
44b8ba871bSPeter Wemm 	return (0);
45b8ba871bSPeter Wemm }
46b8ba871bSPeter Wemm 
47b8ba871bSPeter Wemm /*
48b8ba871bSPeter Wemm  * ex_usage -- :exusage [cmd]
49b8ba871bSPeter Wemm  *	Display ex usage strings.
50b8ba871bSPeter Wemm  *
51c271fa92SBaptiste Daroussin  * PUBLIC: int ex_usage(SCR *, EXCMD *);
52b8ba871bSPeter Wemm  */
53b8ba871bSPeter Wemm int
ex_usage(SCR * sp,EXCMD * cmdp)54f0957ccaSPeter Wemm ex_usage(SCR *sp, EXCMD *cmdp)
55b8ba871bSPeter Wemm {
56b8ba871bSPeter Wemm 	ARGS *ap;
57b8ba871bSPeter Wemm 	EXCMDLIST const *cp;
58b8ba871bSPeter Wemm 	int newscreen;
59f0957ccaSPeter Wemm 	CHAR_T *name, *p, nb[MAXCMDNAMELEN + 5];
60b8ba871bSPeter Wemm 
61b8ba871bSPeter Wemm 	switch (cmdp->argc) {
62b8ba871bSPeter Wemm 	case 1:
63b8ba871bSPeter Wemm 		ap = cmdp->argv[0];
64f0957ccaSPeter Wemm 		if (ISUPPER(ap->bp[0])) {
65b8ba871bSPeter Wemm 			newscreen = 1;
66f0957ccaSPeter Wemm 			ap->bp[0] = TOLOWER(ap->bp[0]);
67b8ba871bSPeter Wemm 		} else
68b8ba871bSPeter Wemm 			newscreen = 0;
69b8ba871bSPeter Wemm 		for (cp = cmds; cp->name != NULL &&
70f0957ccaSPeter Wemm 		    MEMCMP(ap->bp, cp->name, ap->len); ++cp);
71b8ba871bSPeter Wemm 		if (cp->name == NULL ||
72f0957ccaSPeter Wemm 		    (newscreen && !F_ISSET(cp, E_NEWSCREEN))) {
73b8ba871bSPeter Wemm 			if (newscreen)
74f0957ccaSPeter Wemm 				ap->bp[0] = TOUPPER(ap->bp[0]);
75f0957ccaSPeter Wemm 			(void)ex_printf(sp, "The "WVS" command is unknown\n",
76b8ba871bSPeter Wemm 			    (int)ap->len, ap->bp);
77b8ba871bSPeter Wemm 		} else {
78b8ba871bSPeter Wemm 			(void)ex_printf(sp,
79b8ba871bSPeter Wemm 			    "Command: %s\n  Usage: %s\n", cp->help, cp->usage);
80b8ba871bSPeter Wemm 			/*
81b8ba871bSPeter Wemm 			 * !!!
82b8ba871bSPeter Wemm 			 * The "visual" command has two modes, one from ex,
83b8ba871bSPeter Wemm 			 * one from the vi colon line.  Don't ask.
84b8ba871bSPeter Wemm 			 */
85b8ba871bSPeter Wemm 			if (cp != &cmds[C_VISUAL_EX] &&
86b8ba871bSPeter Wemm 			    cp != &cmds[C_VISUAL_VI])
87b8ba871bSPeter Wemm 				break;
88b8ba871bSPeter Wemm 			if (cp == &cmds[C_VISUAL_EX])
89b8ba871bSPeter Wemm 				cp = &cmds[C_VISUAL_VI];
90b8ba871bSPeter Wemm 			else
91b8ba871bSPeter Wemm 				cp = &cmds[C_VISUAL_EX];
92b8ba871bSPeter Wemm 			(void)ex_printf(sp,
93b8ba871bSPeter Wemm 			    "Command: %s\n  Usage: %s\n", cp->help, cp->usage);
94b8ba871bSPeter Wemm 		}
95b8ba871bSPeter Wemm 		break;
96b8ba871bSPeter Wemm 	case 0:
97b8ba871bSPeter Wemm 		for (cp = cmds; cp->name != NULL && !INTERRUPTED(sp); ++cp) {
98b8ba871bSPeter Wemm 			/*
99b8ba871bSPeter Wemm 			 * The ^D command has an unprintable name.
100b8ba871bSPeter Wemm 			 *
101b8ba871bSPeter Wemm 			 * XXX
102b8ba871bSPeter Wemm 			 * We display both capital and lower-case versions of
103b8ba871bSPeter Wemm 			 * the appropriate commands -- no need to add in extra
104b8ba871bSPeter Wemm 			 * room, they're all short names.
105b8ba871bSPeter Wemm 			 */
106b8ba871bSPeter Wemm 			if (cp == &cmds[C_SCROLL])
107f0957ccaSPeter Wemm 				name = L("^D");
108b8ba871bSPeter Wemm 			else if (F_ISSET(cp, E_NEWSCREEN)) {
109b8ba871bSPeter Wemm 				nb[0] = '[';
110f0957ccaSPeter Wemm 				nb[1] = TOUPPER(cp->name[0]);
111b8ba871bSPeter Wemm 				nb[2] = cp->name[0];
112b8ba871bSPeter Wemm 				nb[3] = ']';
113b8ba871bSPeter Wemm 				for (name = cp->name + 1,
114b8ba871bSPeter Wemm 				    p = nb + 4; (*p++ = *name++) != '\0';);
115b8ba871bSPeter Wemm 				name = nb;
116b8ba871bSPeter Wemm 			} else
117b8ba871bSPeter Wemm 				name = cp->name;
118b8ba871bSPeter Wemm 			(void)ex_printf(sp,
119f0957ccaSPeter Wemm 			    WVS": %s\n", MAXCMDNAMELEN, name, cp->help);
120b8ba871bSPeter Wemm 		}
121b8ba871bSPeter Wemm 		break;
122b8ba871bSPeter Wemm 	default:
123b8ba871bSPeter Wemm 		abort();
124b8ba871bSPeter Wemm 	}
125b8ba871bSPeter Wemm 	return (0);
126b8ba871bSPeter Wemm }
127b8ba871bSPeter Wemm 
128b8ba871bSPeter Wemm /*
129b8ba871bSPeter Wemm  * ex_viusage -- :viusage [key]
130b8ba871bSPeter Wemm  *	Display vi usage strings.
131b8ba871bSPeter Wemm  *
132c271fa92SBaptiste Daroussin  * PUBLIC: int ex_viusage(SCR *, EXCMD *);
133b8ba871bSPeter Wemm  */
134b8ba871bSPeter Wemm int
ex_viusage(SCR * sp,EXCMD * cmdp)135f0957ccaSPeter Wemm ex_viusage(SCR *sp, EXCMD *cmdp)
136b8ba871bSPeter Wemm {
137b8ba871bSPeter Wemm 	GS *gp;
138b8ba871bSPeter Wemm 	VIKEYS const *kp;
139b8ba871bSPeter Wemm 	int key;
140b8ba871bSPeter Wemm 
141b8ba871bSPeter Wemm 	gp = sp->gp;
142b8ba871bSPeter Wemm 	switch (cmdp->argc) {
143b8ba871bSPeter Wemm 	case 1:
144b8ba871bSPeter Wemm 		if (cmdp->argv[0]->len != 1) {
145b8ba871bSPeter Wemm 			ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
146b8ba871bSPeter Wemm 			return (1);
147b8ba871bSPeter Wemm 		}
148b8ba871bSPeter Wemm 		key = cmdp->argv[0]->bp[0];
149b8ba871bSPeter Wemm 		if (key > MAXVIKEY)
150b8ba871bSPeter Wemm 			goto nokey;
151b8ba871bSPeter Wemm 
152b8ba871bSPeter Wemm 		/* Special case: '[' and ']' commands. */
153b8ba871bSPeter Wemm 		if ((key == '[' || key == ']') && cmdp->argv[0]->bp[1] != key)
154b8ba871bSPeter Wemm 			goto nokey;
155b8ba871bSPeter Wemm 
156b8ba871bSPeter Wemm 		/* Special case: ~ command. */
157b8ba871bSPeter Wemm 		if (key == '~' && O_ISSET(sp, O_TILDEOP))
158b8ba871bSPeter Wemm 			kp = &tmotion;
159b8ba871bSPeter Wemm 		else
160b8ba871bSPeter Wemm 			kp = &vikeys[key];
161b8ba871bSPeter Wemm 
162b8ba871bSPeter Wemm 		if (kp->usage == NULL)
163b8ba871bSPeter Wemm nokey:			(void)ex_printf(sp,
164b8ba871bSPeter Wemm 			    "The %s key has no current meaning\n",
165b8ba871bSPeter Wemm 			    KEY_NAME(sp, key));
166b8ba871bSPeter Wemm 		else
167b8ba871bSPeter Wemm 			(void)ex_printf(sp,
168b8ba871bSPeter Wemm 			    "  Key:%s%s\nUsage: %s\n",
169*110d525eSBaptiste Daroussin 			    isblank((u_char)*kp->help) ? "" : " ",
170*110d525eSBaptiste Daroussin 			    kp->help, kp->usage);
171b8ba871bSPeter Wemm 		break;
172b8ba871bSPeter Wemm 	case 0:
173b8ba871bSPeter Wemm 		for (key = 0; key <= MAXVIKEY && !INTERRUPTED(sp); ++key) {
174b8ba871bSPeter Wemm 			/* Special case: ~ command. */
175b8ba871bSPeter Wemm 			if (key == '~' && O_ISSET(sp, O_TILDEOP))
176b8ba871bSPeter Wemm 				kp = &tmotion;
177b8ba871bSPeter Wemm 			else
178b8ba871bSPeter Wemm 				kp = &vikeys[key];
179b8ba871bSPeter Wemm 			if (kp->help != NULL)
180b8ba871bSPeter Wemm 				(void)ex_printf(sp, "%s\n", kp->help);
181b8ba871bSPeter Wemm 		}
182b8ba871bSPeter Wemm 		break;
183b8ba871bSPeter Wemm 	default:
184b8ba871bSPeter Wemm 		abort();
185b8ba871bSPeter Wemm 	}
186b8ba871bSPeter Wemm 	return (0);
187b8ba871bSPeter Wemm }
188