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