summaryrefslogtreecommitdiffstats
path: root/usr.bin/help/help.c
blob: 05667cee5656196a800020a1160c55d89d288ac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * 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"

/*
 * The file we check if all else fails.
 */
#define	_PATH_DEFAULT	_PATH_LIBHELP "/default"

/*
 * The file we check for command help.
 */
#define	_PATH_COMMANDS	_PATH_LIBHELP "/cmds"

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';
	numlen = strlen(keynumber);
	/*
	 * Try the default help file if we have a numeric key.
	 * If we have no numeric part of the key, use the command help.
	 * Or else, use the non-numeric part of the key.
	 */
	if (strlen(keybase) == 0) {
		strlcpy(path, _PATH_DEFAULT, sizeof(path));
	} else if (numlen == 0) {
		keynumber = keybase;
		numlen = strlen(keynumber);
		strlcpy(path, _PATH_COMMANDS, sizeof(path));
	} else {
		snprintf(path, sizeof(path), _PATH_LIBHELP "/%s", keybase);
	}

	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) {
				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 (keybase != NULL) {
		free(keybase);
	}
	if (found) {
		return 0;
	}
fail:
	if (keybase != NULL) {
		free(keybase);
	}
	printf("Key '%s' not found.\n", key);
	return 1;
}
OpenPOWER on IntegriCloud