2002-05-22 23:14:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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 <ctype.h>
|
|
|
|
|
#include <err.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"
|
|
|
|
|
|
2002-05-23 14:58:22 +00:00
|
|
|
|
/*
|
|
|
|
|
* The file we check if all else fails.
|
|
|
|
|
*/
|
|
|
|
|
#define _PATH_DEFAULT _PATH_LIBHELP "/default"
|
|
|
|
|
|
2002-06-04 06:14:11 +00:00
|
|
|
|
/*
|
|
|
|
|
* The file we check for command help.
|
|
|
|
|
*/
|
|
|
|
|
#define _PATH_COMMANDS _PATH_LIBHELP "/cmds"
|
|
|
|
|
|
2002-05-22 23:14:17 +00:00
|
|
|
|
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: ");
|
2002-05-23 03:03:44 +00:00
|
|
|
|
if ((key = fgetln(stdin, &len)) == NULL) {
|
2002-05-22 23:14:17 +00:00
|
|
|
|
err(1, NULL);
|
2002-05-23 03:03:44 +00:00
|
|
|
|
}
|
2002-05-22 23:14:17 +00:00
|
|
|
|
key[len - 1] = '\0';
|
|
|
|
|
return help(key);
|
|
|
|
|
}
|
|
|
|
|
argc--;
|
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
|
/*
|
|
|
|
|
* If no error occurred this time, rv becomes 1.
|
|
|
|
|
*/
|
2002-05-23 03:03:44 +00:00
|
|
|
|
if (help(argv[i]) == 0) {
|
2002-05-22 23:14:17 +00:00
|
|
|
|
rv = 1;
|
2002-05-23 03:03:44 +00:00
|
|
|
|
}
|
2002-05-22 23:14:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2002-05-23 03:03:44 +00:00
|
|
|
|
* Return 0 if at least one help() worked. Return 1 else.
|
2002-05-22 23:14:17 +00:00
|
|
|
|
*/
|
|
|
|
|
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';
|
2002-06-04 06:14:11 +00:00
|
|
|
|
numlen = strlen(keynumber);
|
2002-05-23 14:58:22 +00:00
|
|
|
|
/*
|
|
|
|
|
* Try the default help file if we have a numeric key.
|
2002-06-04 06:14:11 +00:00
|
|
|
|
* If we have no numeric part of the key, use the command help.
|
2002-05-23 14:58:22 +00:00
|
|
|
|
* Or else, use the non-numeric part of the key.
|
|
|
|
|
*/
|
|
|
|
|
if (strlen(keybase) == 0) {
|
|
|
|
|
strlcpy(path, _PATH_DEFAULT, sizeof(path));
|
2002-06-04 06:14:11 +00:00
|
|
|
|
} else if (numlen == 0) {
|
|
|
|
|
keynumber = keybase;
|
|
|
|
|
numlen = strlen(keynumber);
|
|
|
|
|
strlcpy(path, _PATH_COMMANDS, sizeof(path));
|
2002-05-23 14:58:22 +00:00
|
|
|
|
} else {
|
|
|
|
|
snprintf(path, sizeof(path), _PATH_LIBHELP "/%s", keybase);
|
|
|
|
|
}
|
2002-05-22 23:14:17 +00:00
|
|
|
|
|
|
|
|
|
helpfile = fopen(path, "r");
|
|
|
|
|
if (helpfile == NULL) {
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
while (!feof(helpfile) && (p = fgetln(helpfile, &len)) != NULL) {
|
|
|
|
|
switch (*p) {
|
|
|
|
|
case '*':
|
|
|
|
|
continue;
|
|
|
|
|
case '-':
|
2002-06-04 06:14:11 +00:00
|
|
|
|
if (len < numlen) {
|
2002-05-22 23:14:17 +00:00
|
|
|
|
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);
|
2002-06-04 06:14:11 +00:00
|
|
|
|
if (keybase != NULL) {
|
|
|
|
|
free(keybase);
|
|
|
|
|
}
|
2002-05-22 23:14:17 +00:00
|
|
|
|
if (found) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
fail:
|
2002-06-04 06:14:11 +00:00
|
|
|
|
if (keybase != NULL) {
|
|
|
|
|
free(keybase);
|
|
|
|
|
}
|
2002-05-22 23:14:17 +00:00
|
|
|
|
printf("Key '%s' not found.\n", key);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|