summaryrefslogtreecommitdiffstats
path: root/manpage.c
blob: e1ef6a2743a3c62fb5a270f54c0c9181028ac574 (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*	$Id$ */
/*
 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
#include <getopt.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "manpath.h"
#include "mansearch.h"

static	void	 show(const char *, const char *);

int
main(int argc, char *argv[])
{
	int		 ch, term;
	size_t		 i, sz, len;
	struct mansearch search;
	struct manpage	*res;
	char		*conf_file, *defpaths, *auxpaths, *cp;
	char		 buf[PATH_MAX];
	const char	*cmd;
	struct manpaths	 paths;
	char		*progname;
	extern char	*optarg;
	extern int	 optind;

	term = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);

	progname = strrchr(argv[0], '/');
	if (progname == NULL)
		progname = argv[0];
	else
		++progname;

	auxpaths = defpaths = conf_file = NULL;
	memset(&paths, 0, sizeof(struct manpaths));
	memset(&search, 0, sizeof(struct mansearch));

	while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
		switch (ch) {
		case ('C'):
			conf_file = optarg;
			break;
		case ('M'):
			defpaths = optarg;
			break;
		case ('m'):
			auxpaths = optarg;
			break;
		case ('S'):
			search.arch = optarg;
			break;
		case ('s'):
			search.sec = optarg;
			break;
		default:
			goto usage;
		}

	argc -= optind;
	argv += optind;

	if (0 == argc)
		goto usage;

	search.deftype = TYPE_Nm | TYPE_Nd;

	manpath_parse(&paths, conf_file, defpaths, auxpaths);
	ch = mansearch(&search, &paths, argc, argv, &res, &sz);
	manpath_free(&paths);

	if (0 == ch)
		goto usage;

	if (0 == sz) {
		free(res);
		return(EXIT_FAILURE);
	} else if (1 == sz && term) {
		i = 1;
		goto show;
	} else if (NULL == res)
		return(EXIT_FAILURE);

	for (i = 0; i < sz; i++) {
		printf("%6zu  %s: %s\n", 
			i + 1, res[i].file, res[i].desc);
		free(res[i].desc);
	}

	if (0 == term) {
		free(res);
		return(EXIT_SUCCESS);
	}

	i = 1;
	printf("Enter a choice [1]: ");
	fflush(stdout);

	if (NULL != (cp = fgetln(stdin, &len)))
		if ('\n' == cp[--len] && len > 0) {
			cp[len] = '\0';
			if ((i = atoi(cp)) < 1 || i > sz)
				i = 0;
		}

	if (0 == i) {
		free(res);
		return(EXIT_SUCCESS);
	}
show:
	cmd = res[i - 1].form ? "mandoc" : "cat";
	strlcpy(buf, res[i - 1].file, PATH_MAX);
	free(res);

	show(cmd, buf);
	/* NOTREACHED */
usage:
	fprintf(stderr, "usage: %s [-C conf] "
			 	  "[-M paths] "
				  "[-m paths] "
				  "[-S arch] "
				  "[-s section] "
			          "expr ...\n", 
				  progname);
	return(EXIT_FAILURE);
}

static void
show(const char *cmd, const char *file)
{
	int		 fds[2];
	pid_t		 pid;

	if (-1 == pipe(fds)) {
		perror(NULL);
		exit(EXIT_FAILURE);
	}

	if (-1 == (pid = fork())) {
		perror(NULL);
		exit(EXIT_FAILURE);
	} else if (pid > 0) {
		dup2(fds[0], STDIN_FILENO);
		close(fds[1]);
		cmd = NULL != getenv("MANPAGER") ? 
			getenv("MANPAGER") :
			(NULL != getenv("PAGER") ? 
			 getenv("PAGER") : "more");
		execlp(cmd, cmd, (char *)NULL);
		perror(cmd);
		exit(EXIT_FAILURE);
	}

	dup2(fds[1], STDOUT_FILENO);
	close(fds[0]);
	execlp(cmd, cmd, file, (char *)NULL);
	perror(cmd);
	exit(EXIT_FAILURE);
}