diff options
author | jmallett <jmallett@FreeBSD.org> | 2002-05-22 23:14:17 +0000 |
---|---|---|
committer | jmallett <jmallett@FreeBSD.org> | 2002-05-22 23:14:17 +0000 |
commit | df0fe27516a8624d9b50dd2c0b5580e4616e5dc8 (patch) | |
tree | 022675ad484fb380eee34fe7431ca5ebe39a1310 /usr.bin/help | |
parent | 0bfc6876ce3ed05eedb6395706f4c15966ee1d70 (diff) | |
download | FreeBSD-src-df0fe27516a8624d9b50dd2c0b5580e4616e5dc8.zip FreeBSD-src-df0fe27516a8624d9b50dd2c0b5580e4616e5dc8.tar.gz |
Add my PD implementation of the SCCS help command, which prints help from
files in the format used by SCCS, given a key. It behaves exactly like the
``proper'' SCCS help command, from what testing I can do.
Diffstat (limited to 'usr.bin/help')
-rw-r--r-- | usr.bin/help/Makefile | 6 | ||||
-rw-r--r-- | usr.bin/help/help.c | 140 |
2 files changed, 146 insertions, 0 deletions
diff --git a/usr.bin/help/Makefile b/usr.bin/help/Makefile new file mode 100644 index 0000000..67b34e9 --- /dev/null +++ b/usr.bin/help/Makefile @@ -0,0 +1,6 @@ +# $FreeBSD$ + +PROG= help +NOMAN= noman + +.include <bsd.prog.mk> diff --git a/usr.bin/help/help.c b/usr.bin/help/help.c new file mode 100644 index 0000000..beeb1e9 --- /dev/null +++ b/usr.bin/help/help.c @@ -0,0 +1,140 @@ +/* + * Author: J. Mallett <jmallett@FreeBSD.org> + * Date: May 22, 2002 + * Program: help + * Description: + * Displays help from files in the format used by SCCS. + * + * This file is in the public domain. + * + * $FreeBSD$ + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/types.h> +#include <sys/stat.h> +#include <ctype.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +/* + * Base path to the help files. + */ +#define _PATH_LIBHELP "/usr/lib/help" + +int help(const char *); + +int +main(int argc, char *argv[]) +{ + char *key; + int i, rv; + + rv = 0; + + if (argc == 1) { + size_t len; + + (void)printf("Enter the message number or SCCS command name: "); + if ((key = fgetln(stdin, &len)) == NULL) + err(1, NULL); + key[len - 1] = '\0'; + return help(key); + } + argc--; + argv++; + + for (i = 0; i < argc; i++) { + /* + * If no error occurred this time, rv becomes 1. + */ + if (help(argv[i]) == 0) + rv = 1; + } + + /* + * Return 0 if at least one help() worked. Return 1 else; + */ + return rv ? 0 : 1; +} + +/* + * Function: help + * Returns: 0 if no error occurrs, 1 otherwise. + * Arguments: key -- The key we are looking up help for. + * Description: + * Looks up the help for a given key. + */ +int +help(const char *key) +{ + FILE *helpfile; + char path[PATH_MAX]; + char *keybase, *p; + const char *keyname, *keynumber; + int helping, found; + size_t len, numlen; + + found = helping = 0; + + keyname = key; + keybase = strdup(keyname); + if (keybase == NULL) { + err(1, NULL); + } + p = keybase; + while (!isnumber(*p) && *p != '\0') { + ++key; + ++p; + } + keynumber = key; + key = keyname; + *p = '\0'; + snprintf(path, sizeof(path), _PATH_LIBHELP "/%s", keybase); + free(keybase); + numlen = strlen(keynumber); + + helpfile = fopen(path, "r"); + if (helpfile == NULL) { + goto fail; + } + while (!feof(helpfile) && (p = fgetln(helpfile, &len)) != NULL) { + switch (*p) { + case '*': + continue; + case '-': + if (len < numlen + 1) { + continue; + } + if (strncmp(++p, keynumber, numlen) == 0) { + found = 1; + helping = 1; + printf("\n%s:\n", key); + } else { + helping = 0; + } + continue; + default: + if (helping) { + p[len - 1] = '\0'; + printf("%s\n", p); + } + continue; + } + } + fclose(helpfile); + if (found) { + return 0; + } +fail: + printf("Key '%s' not found.\n", key); + return 1; +} |