man.c revision 1.37.10.1 1 1.37.10.1 matt /* $NetBSD: man.c,v 1.37.10.1 2010/04/21 05:27:11 matt Exp $ */
2 1.7 tls
3 1.1 cgd /*
4 1.7 tls * Copyright (c) 1987, 1993, 1994, 1995
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.30 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.9 mikel #include <sys/cdefs.h>
33 1.9 mikel
34 1.1 cgd #ifndef lint
35 1.37 lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\
36 1.37 lukem The Regents of the University of California. All rights reserved.");
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.1 cgd #ifndef lint
40 1.7 tls #if 0
41 1.7 tls static char sccsid[] = "@(#)man.c 8.17 (Berkeley) 1/31/95";
42 1.7 tls #else
43 1.37.10.1 matt __RCSID("$NetBSD: man.c,v 1.37.10.1 2010/04/21 05:27:11 matt Exp $");
44 1.7 tls #endif
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd #include <sys/param.h>
48 1.1 cgd #include <sys/queue.h>
49 1.22 tsutsui #include <sys/utsname.h>
50 1.1 cgd
51 1.1 cgd #include <ctype.h>
52 1.1 cgd #include <err.h>
53 1.1 cgd #include <errno.h>
54 1.1 cgd #include <fcntl.h>
55 1.1 cgd #include <fnmatch.h>
56 1.1 cgd #include <glob.h>
57 1.1 cgd #include <signal.h>
58 1.1 cgd #include <stdio.h>
59 1.1 cgd #include <stdlib.h>
60 1.1 cgd #include <string.h>
61 1.1 cgd #include <unistd.h>
62 1.36 lukem #include <util.h>
63 1.1 cgd
64 1.29 thorpej #include "manconf.h"
65 1.1 cgd #include "pathnames.h"
66 1.1 cgd
67 1.33 chuck #ifndef MAN_DEBUG
68 1.33 chuck #define MAN_DEBUG 0 /* debug path output */
69 1.33 chuck #endif
70 1.1 cgd
71 1.33 chuck /*
72 1.33 chuck * manstate: structure collecting the current global state so we can
73 1.33 chuck * easily identify it and pass it to helper functions in one arg.
74 1.33 chuck */
75 1.33 chuck struct manstate {
76 1.33 chuck /* command line flags */
77 1.33 chuck int all; /* -a: show all matches rather than first */
78 1.33 chuck int cat; /* -c: do not use a pager */
79 1.33 chuck char *conffile; /* -C: use alternate config file */
80 1.33 chuck int how; /* -h: show SYNOPSIS only */
81 1.33 chuck char *manpath; /* -M: alternate MANPATH */
82 1.33 chuck char *addpath; /* -m: add these dirs to front of manpath */
83 1.33 chuck char *pathsearch; /* -S: path of man must contain this string */
84 1.33 chuck char *sectionname; /* -s: limit search to a given man section */
85 1.33 chuck int where; /* -w: just show paths of all matching files */
86 1.33 chuck
87 1.33 chuck /* important tags from the config file */
88 1.33 chuck TAG *defaultpath; /* _default: default MANPATH */
89 1.33 chuck TAG *subdirs; /* _subdir: default subdir search list */
90 1.33 chuck TAG *suffixlist; /* _suffix: for files that can be cat()'d */
91 1.33 chuck TAG *buildlist; /* _build: for files that must be built */
92 1.33 chuck
93 1.33 chuck /* tags for internal use */
94 1.33 chuck TAG *intmp; /* _intmp: tmp files we must cleanup */
95 1.33 chuck TAG *missinglist; /* _missing: pages we couldn't find */
96 1.33 chuck TAG *mymanpath; /* _new_path: final version of MANPATH */
97 1.33 chuck TAG *section; /* <sec>: tag for m.sectionname */
98 1.33 chuck
99 1.33 chuck /* other misc stuff */
100 1.33 chuck const char *pager; /* pager to use */
101 1.33 chuck size_t pagerlen; /* length of the above */
102 1.33 chuck };
103 1.33 chuck
104 1.33 chuck /*
105 1.33 chuck * prototypes
106 1.33 chuck */
107 1.33 chuck int main(int, char **);
108 1.33 chuck static void build_page(char *, char **, struct manstate *);
109 1.33 chuck static void cat(char *);
110 1.33 chuck static const char *check_pager(const char *);
111 1.33 chuck static int cleanup(void);
112 1.33 chuck static void how(char *);
113 1.33 chuck static void jump(char **, char *, char *);
114 1.33 chuck static int manual(char *, struct manstate *, glob_t *);
115 1.33 chuck static void onsig(int);
116 1.33 chuck static void usage(void);
117 1.1 cgd
118 1.33 chuck /*
119 1.33 chuck * main function
120 1.33 chuck */
121 1.1 cgd int
122 1.33 chuck main(int argc, char **argv)
123 1.1 cgd {
124 1.33 chuck static struct manstate m = { 0 }; /* init to zero */
125 1.33 chuck int ch, abs_section, found;
126 1.33 chuck const char *machine;
127 1.33 chuck ENTRY *esubd, *epath;
128 1.33 chuck char *p, **ap, *cmd, buf[MAXPATHLEN * 2];
129 1.33 chuck size_t len;
130 1.1 cgd glob_t pg;
131 1.1 cgd
132 1.33 chuck /*
133 1.33 chuck * parse command line...
134 1.33 chuck */
135 1.25 simonb while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:s:S:w")) != -1)
136 1.1 cgd switch (ch) {
137 1.1 cgd case 'a':
138 1.33 chuck m.all = 1;
139 1.1 cgd break;
140 1.1 cgd case 'C':
141 1.33 chuck m.conffile = optarg;
142 1.1 cgd break;
143 1.1 cgd case 'c':
144 1.33 chuck case '-': /* XXX: '-' is a deprecated version of '-c' */
145 1.33 chuck m.cat = 1;
146 1.1 cgd break;
147 1.1 cgd case 'h':
148 1.33 chuck m.how = 1;
149 1.1 cgd break;
150 1.1 cgd case 'm':
151 1.33 chuck m.addpath = optarg;
152 1.1 cgd break;
153 1.1 cgd case 'M':
154 1.33 chuck case 'P': /* -P for backward compatibility */
155 1.33 chuck m.manpath = strdup(optarg);
156 1.1 cgd break;
157 1.1 cgd /*
158 1.1 cgd * The -f and -k options are backward compatible,
159 1.1 cgd * undocumented ways of calling whatis(1) and apropos(1).
160 1.1 cgd */
161 1.1 cgd case 'f':
162 1.1 cgd jump(argv, "-f", "whatis");
163 1.1 cgd /* NOTREACHED */
164 1.1 cgd case 'k':
165 1.1 cgd jump(argv, "-k", "apropos");
166 1.1 cgd /* NOTREACHED */
167 1.25 simonb case 's':
168 1.33 chuck if (m.sectionname != NULL)
169 1.25 simonb usage();
170 1.33 chuck m.sectionname = optarg;
171 1.25 simonb break;
172 1.23 jdolecek case 'S':
173 1.33 chuck m.pathsearch = optarg;
174 1.23 jdolecek break;
175 1.1 cgd case 'w':
176 1.33 chuck m.all = m.where = 1;
177 1.1 cgd break;
178 1.1 cgd case '?':
179 1.1 cgd default:
180 1.1 cgd usage();
181 1.1 cgd }
182 1.1 cgd argc -= optind;
183 1.1 cgd argv += optind;
184 1.1 cgd
185 1.23 jdolecek if (!argc)
186 1.1 cgd usage();
187 1.1 cgd
188 1.33 chuck /*
189 1.33 chuck * read the configuration file and collect any other information
190 1.33 chuck * we will need (machine type, pager, section [if specified
191 1.33 chuck * without '-s'], and MANPATH through the environment).
192 1.33 chuck */
193 1.33 chuck config(m.conffile); /* exits on error ... */
194 1.1 cgd
195 1.22 tsutsui if ((machine = getenv("MACHINE")) == NULL) {
196 1.22 tsutsui struct utsname utsname;
197 1.22 tsutsui
198 1.22 tsutsui if (uname(&utsname) == -1) {
199 1.22 tsutsui perror("uname");
200 1.22 tsutsui exit(1);
201 1.22 tsutsui }
202 1.22 tsutsui machine = utsname.machine;
203 1.22 tsutsui }
204 1.1 cgd
205 1.33 chuck if (!m.cat && !m.how && !m.where) { /* if we need a pager ... */
206 1.33 chuck if (!isatty(STDOUT_FILENO)) {
207 1.33 chuck m.cat = 1;
208 1.33 chuck } else {
209 1.33 chuck if ((m.pager = getenv("PAGER")) != NULL &&
210 1.33 chuck m.pager[0] != '\0')
211 1.33 chuck m.pager = check_pager(m.pager);
212 1.33 chuck else
213 1.33 chuck m.pager = _PATH_PAGER;
214 1.33 chuck m.pagerlen = strlen(m.pager);
215 1.33 chuck }
216 1.33 chuck }
217 1.33 chuck
218 1.33 chuck /* do we need to set m.section to a non-null value? */
219 1.33 chuck if (m.sectionname) {
220 1.1 cgd
221 1.33 chuck m.section = gettag(m.sectionname, 0); /* -s must be a section */
222 1.33 chuck if (m.section == NULL)
223 1.33 chuck errx(1, "unknown section: %s", m.sectionname);
224 1.23 jdolecek
225 1.33 chuck } else if (argc > 1) {
226 1.33 chuck
227 1.33 chuck m.section = gettag(*argv, 0); /* might be a section? */
228 1.33 chuck if (m.section) {
229 1.25 simonb argv++;
230 1.25 simonb argc--;
231 1.25 simonb }
232 1.1 cgd
233 1.33 chuck }
234 1.33 chuck
235 1.33 chuck if (m.manpath == NULL)
236 1.33 chuck m.manpath = getenv("MANPATH"); /* note: -M overrides getenv */
237 1.23 jdolecek
238 1.23 jdolecek
239 1.23 jdolecek /*
240 1.33 chuck * get default values from config file, plus create the tags we
241 1.33 chuck * use for keeping internal state. make sure all our mallocs
242 1.33 chuck * go through.
243 1.33 chuck */
244 1.33 chuck /* from cfg file */
245 1.33 chuck m.defaultpath = gettag("_default", 1);
246 1.33 chuck m.subdirs = gettag("_subdir", 1);
247 1.33 chuck m.suffixlist = gettag("_suffix", 1);
248 1.33 chuck m.buildlist = gettag("_build", 1);
249 1.33 chuck /* internal use */
250 1.33 chuck m.mymanpath = gettag("_new_path", 1);
251 1.33 chuck m.missinglist = gettag("_missing", 1);
252 1.33 chuck m.intmp = gettag("_intmp", 1);
253 1.33 chuck if (!m.defaultpath || !m.subdirs || !m.suffixlist || !m.buildlist ||
254 1.33 chuck !m.mymanpath || !m.missinglist || !m.intmp)
255 1.33 chuck errx(1, "malloc failed");
256 1.33 chuck
257 1.33 chuck /*
258 1.33 chuck * are we using a section whose elements are all absolute paths?
259 1.33 chuck * (we only need to look at the first entry on the section list,
260 1.33 chuck * as config() will ensure that any additional entries will match
261 1.33 chuck * the first one.)
262 1.33 chuck */
263 1.33 chuck abs_section = (m.section != NULL &&
264 1.33 chuck !TAILQ_EMPTY(&m.section->entrylist) &&
265 1.33 chuck *(TAILQ_FIRST(&m.section->entrylist)->s) == '/');
266 1.33 chuck
267 1.33 chuck /*
268 1.33 chuck * now that we have all the data we need, we must determine the
269 1.33 chuck * manpath we are going to use to find the requested entries using
270 1.33 chuck * the following steps...
271 1.33 chuck *
272 1.33 chuck * [1] if the user specified a section and that section's elements
273 1.33 chuck * from the config file are all absolute paths, then we override
274 1.33 chuck * defaultpath and -M/MANPATH with the section's absolute paths.
275 1.23 jdolecek */
276 1.23 jdolecek if (abs_section) {
277 1.33 chuck m.manpath = NULL; /* ignore -M/MANPATH */
278 1.33 chuck m.defaultpath = m.section; /* overwrite _default path */
279 1.33 chuck m.section = NULL; /* promoted to defaultpath */
280 1.1 cgd }
281 1.1 cgd
282 1.23 jdolecek /*
283 1.33 chuck * [2] section can now only be non-null if the user asked for
284 1.33 chuck * a section and that section's elements did not have
285 1.33 chuck * absolute paths. in this case we use the section's
286 1.33 chuck * elements to override _subdir from the config file.
287 1.33 chuck *
288 1.33 chuck * after this step, we are done processing "m.section"...
289 1.23 jdolecek */
290 1.33 chuck if (m.section)
291 1.33 chuck m.subdirs = m.section;
292 1.23 jdolecek
293 1.1 cgd /*
294 1.33 chuck * [3] we need to setup the path we want to use (m.mymanpath).
295 1.33 chuck * if the user gave us a path (m.manpath) use it, otherwise
296 1.33 chuck * go with the default. in either case we need to append
297 1.33 chuck * the subdir and machine spec to each element of the path.
298 1.23 jdolecek *
299 1.33 chuck * for absolute section paths that come from the config file,
300 1.33 chuck * we only append the subdir spec if the path ends in
301 1.33 chuck * a '/' --- elements that do not end in '/' are assumed to
302 1.33 chuck * not have subdirectories. this is mainly for backward compat,
303 1.33 chuck * but it allows non-subdir configs like:
304 1.33 chuck * sect3 /usr/share/man/{old/,}cat3
305 1.33 chuck * doc /usr/{pkg,share}/doc/{sendmail/op,sendmail/intro}
306 1.33 chuck *
307 1.33 chuck * note that we try and be careful to not put double slashes
308 1.33 chuck * in the path (e.g. we want /usr/share/man/man1, not
309 1.33 chuck * /usr/share/man//man1) because "more" will put the filename
310 1.33 chuck * we generate in its prompt and the double slashes look ugly.
311 1.33 chuck */
312 1.33 chuck if (m.manpath) {
313 1.33 chuck
314 1.33 chuck /* note: strtok is going to destroy m.manpath */
315 1.33 chuck for (p = strtok(m.manpath, ":") ; p ; p = strtok(NULL, ":")) {
316 1.33 chuck len = strlen(p);
317 1.33 chuck if (len < 1)
318 1.33 chuck continue;
319 1.33 chuck TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) {
320 1.33 chuck snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
321 1.33 chuck p, (p[len-1] == '/') ? "" : "/",
322 1.33 chuck esubd->s, machine);
323 1.33 chuck if (addentry(m.mymanpath, buf, 0) < 0)
324 1.33 chuck errx(1, "malloc failed");
325 1.1 cgd }
326 1.1 cgd }
327 1.33 chuck
328 1.23 jdolecek } else {
329 1.23 jdolecek
330 1.33 chuck TAILQ_FOREACH(epath, &m.defaultpath->entrylist, q) {
331 1.23 jdolecek /* handle trailing "/" magic here ... */
332 1.23 jdolecek if (abs_section &&
333 1.33 chuck epath->s[epath->len - 1] != '/') {
334 1.1 cgd
335 1.1 cgd (void)snprintf(buf, sizeof(buf),
336 1.33 chuck "%s{/%s,}", epath->s, machine);
337 1.33 chuck if (addentry(m.mymanpath, buf, 0) < 0)
338 1.33 chuck errx(1, "malloc failed");
339 1.1 cgd continue;
340 1.1 cgd }
341 1.23 jdolecek
342 1.33 chuck TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) {
343 1.23 jdolecek snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
344 1.33 chuck epath->s,
345 1.33 chuck (epath->s[epath->len-1] == '/') ? ""
346 1.33 chuck : "/",
347 1.33 chuck esubd->s, machine);
348 1.33 chuck if (addentry(m.mymanpath, buf, 0) < 0)
349 1.33 chuck errx(1, "malloc failed");
350 1.23 jdolecek }
351 1.23 jdolecek }
352 1.23 jdolecek
353 1.33 chuck }
354 1.23 jdolecek
355 1.23 jdolecek /*
356 1.33 chuck * [4] finally, prepend the "-m" m.addpath to mymanpath if it
357 1.33 chuck * was specified. subdirs and machine are always applied to
358 1.33 chuck * m.addpath.
359 1.33 chuck */
360 1.33 chuck if (m.addpath) {
361 1.33 chuck
362 1.33 chuck /* note: strtok is going to destroy m.addpath */
363 1.33 chuck for (p = strtok(m.addpath, ":") ; p ; p = strtok(NULL, ":")) {
364 1.33 chuck len = strlen(p);
365 1.33 chuck if (len < 1)
366 1.33 chuck continue;
367 1.33 chuck TAILQ_FOREACH(esubd, &m.subdirs->entrylist, q) {
368 1.33 chuck snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
369 1.33 chuck p, (p[len-1] == '/') ? "" : "/",
370 1.33 chuck esubd->s, machine);
371 1.33 chuck /* add at front */
372 1.33 chuck if (addentry(m.mymanpath, buf, 1) < 0)
373 1.33 chuck errx(1, "malloc failed");
374 1.1 cgd }
375 1.1 cgd }
376 1.33 chuck
377 1.1 cgd }
378 1.1 cgd
379 1.33 chuck /*
380 1.33 chuck * now m.mymanpath is complete!
381 1.33 chuck */
382 1.35 jwise #if MAN_DEBUG
383 1.35 jwise printf("mymanpath:\n");
384 1.35 jwise TAILQ_FOREACH(epath, &m.mymanpath->entrylist, q) {
385 1.35 jwise printf("\t%s\n", epath->s);
386 1.33 chuck }
387 1.35 jwise #endif
388 1.23 jdolecek
389 1.1 cgd /*
390 1.33 chuck * start searching for matching files and format them if necessary.
391 1.33 chuck * setup an interrupt handler so that we can ensure that temporary
392 1.33 chuck * files go away.
393 1.1 cgd */
394 1.1 cgd (void)signal(SIGINT, onsig);
395 1.1 cgd (void)signal(SIGHUP, onsig);
396 1.17 itohy (void)signal(SIGPIPE, onsig);
397 1.1 cgd
398 1.1 cgd memset(&pg, 0, sizeof(pg));
399 1.1 cgd for (found = 0; *argv; ++argv)
400 1.33 chuck if (manual(*argv, &m, &pg)) {
401 1.1 cgd found = 1;
402 1.33 chuck }
403 1.1 cgd
404 1.33 chuck /* if nothing found, we're done. */
405 1.1 cgd if (!found) {
406 1.1 cgd (void)cleanup();
407 1.1 cgd exit (1);
408 1.1 cgd }
409 1.1 cgd
410 1.33 chuck /*
411 1.33 chuck * handle the simple display cases first (m.cat, m.how, m.where)
412 1.33 chuck */
413 1.33 chuck if (m.cat) {
414 1.1 cgd for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
415 1.1 cgd if (**ap == '\0')
416 1.1 cgd continue;
417 1.1 cgd cat(*ap);
418 1.1 cgd }
419 1.1 cgd exit (cleanup());
420 1.1 cgd }
421 1.33 chuck if (m.how) {
422 1.1 cgd for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
423 1.1 cgd if (**ap == '\0')
424 1.1 cgd continue;
425 1.1 cgd how(*ap);
426 1.1 cgd }
427 1.1 cgd exit(cleanup());
428 1.1 cgd }
429 1.33 chuck if (m.where) {
430 1.1 cgd for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
431 1.1 cgd if (**ap == '\0')
432 1.1 cgd continue;
433 1.1 cgd (void)printf("%s\n", *ap);
434 1.1 cgd }
435 1.1 cgd exit(cleanup());
436 1.1 cgd }
437 1.1 cgd
438 1.1 cgd /*
439 1.33 chuck * normal case - we display things in a single command, so
440 1.33 chuck * build a list of things to display. first compute total
441 1.33 chuck * length of buffer we will need so we can malloc it.
442 1.1 cgd */
443 1.33 chuck for (ap = pg.gl_pathv, len = m.pagerlen + 1; *ap != NULL; ++ap) {
444 1.1 cgd if (**ap == '\0')
445 1.1 cgd continue;
446 1.1 cgd len += strlen(*ap) + 1;
447 1.1 cgd }
448 1.1 cgd if ((cmd = malloc(len)) == NULL) {
449 1.9 mikel warn("malloc");
450 1.1 cgd (void)cleanup();
451 1.1 cgd exit(1);
452 1.1 cgd }
453 1.33 chuck
454 1.33 chuck /* now build the command string... */
455 1.1 cgd p = cmd;
456 1.33 chuck len = m.pagerlen;
457 1.33 chuck memcpy(p, m.pager, len);
458 1.1 cgd p += len;
459 1.1 cgd *p++ = ' ';
460 1.1 cgd for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
461 1.1 cgd if (**ap == '\0')
462 1.1 cgd continue;
463 1.1 cgd len = strlen(*ap);
464 1.33 chuck memcpy(p, *ap, len);
465 1.1 cgd p += len;
466 1.1 cgd *p++ = ' ';
467 1.1 cgd }
468 1.14 wsanchez *--p = '\0';
469 1.1 cgd
470 1.1 cgd /* Use system(3) in case someone's pager is "pager arg1 arg2". */
471 1.1 cgd (void)system(cmd);
472 1.1 cgd
473 1.1 cgd exit(cleanup());
474 1.1 cgd }
475 1.1 cgd
476 1.37.10.1 matt static int
477 1.37.10.1 matt manual_find_buildkeyword(char *escpage, const char *fmt,
478 1.37.10.1 matt struct manstate *mp, glob_t *pg, size_t cnt)
479 1.37.10.1 matt {
480 1.37.10.1 matt ENTRY *suffix;
481 1.37.10.1 matt int found;
482 1.37.10.1 matt char *p, buf[MAXPATHLEN];
483 1.37.10.1 matt
484 1.37.10.1 matt found = 0;
485 1.37.10.1 matt /* Try the _build key words next. */
486 1.37.10.1 matt TAILQ_FOREACH(suffix, &mp->buildlist->entrylist, q) {
487 1.37.10.1 matt for (p = suffix->s;
488 1.37.10.1 matt *p != '\0' && !isspace((unsigned char)*p);
489 1.37.10.1 matt ++p)
490 1.37.10.1 matt continue;
491 1.37.10.1 matt if (*p == '\0')
492 1.37.10.1 matt continue;
493 1.37.10.1 matt
494 1.37.10.1 matt *p = '\0';
495 1.37.10.1 matt (void)snprintf(buf, sizeof(buf), fmt, escpage, suffix->s);
496 1.37.10.1 matt if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
497 1.37.10.1 matt if (!mp->where)
498 1.37.10.1 matt build_page(p + 1, &pg->gl_pathv[cnt], mp);
499 1.37.10.1 matt *p = ' ';
500 1.37.10.1 matt found = 1;
501 1.37.10.1 matt break;
502 1.37.10.1 matt }
503 1.37.10.1 matt *p = ' ';
504 1.37.10.1 matt }
505 1.37.10.1 matt
506 1.37.10.1 matt return found;
507 1.37.10.1 matt }
508 1.37.10.1 matt
509 1.1 cgd /*
510 1.1 cgd * manual --
511 1.1 cgd * Search the manuals for the pages.
512 1.1 cgd */
513 1.1 cgd static int
514 1.33 chuck manual(char *page, struct manstate *mp, glob_t *pg)
515 1.1 cgd {
516 1.33 chuck ENTRY *suffix, *mdir;
517 1.32 christos int anyfound, error, found;
518 1.32 christos size_t cnt;
519 1.27 groo char *p, buf[MAXPATHLEN], *escpage, *eptr;
520 1.27 groo static const char escglob[] = "\\~?*{}[]";
521 1.1 cgd
522 1.1 cgd anyfound = 0;
523 1.1 cgd
524 1.27 groo /*
525 1.27 groo * Fixup page which may contain glob(3) special characters, e.g.
526 1.27 groo * the famous "No man page for [" FAQ.
527 1.27 groo */
528 1.27 groo if ((escpage = malloc((2 * strlen(page)) + 1)) == NULL) {
529 1.27 groo warn("malloc");
530 1.27 groo (void)cleanup();
531 1.27 groo exit(1);
532 1.27 groo }
533 1.27 groo
534 1.27 groo p = page;
535 1.27 groo eptr = escpage;
536 1.27 groo
537 1.27 groo while (*p) {
538 1.27 groo if (strchr(escglob, *p) != NULL) {
539 1.27 groo *eptr++ = '\\';
540 1.27 groo *eptr++ = *p++;
541 1.27 groo } else
542 1.27 groo *eptr++ = *p++;
543 1.27 groo }
544 1.27 groo
545 1.27 groo *eptr = '\0';
546 1.27 groo
547 1.37.10.1 matt /*
548 1.37.10.1 matt * If 'page' is given with a full or relative path
549 1.37.10.1 matt * then interpret it as a file specification.
550 1.37.10.1 matt */
551 1.37.10.1 matt if ((page[0] == '/') || (page[0] == '.')) {
552 1.37.10.1 matt /* check if file actually exists */
553 1.37.10.1 matt (void)strlcpy(buf, escpage, sizeof(buf));
554 1.37.10.1 matt error = glob(buf, GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg);
555 1.37.10.1 matt if (error != 0) {
556 1.37.10.1 matt if (error == GLOB_NOMATCH) {
557 1.37.10.1 matt goto notfound;
558 1.37.10.1 matt } else {
559 1.37.10.1 matt errx(EXIT_FAILURE, "glob failed");
560 1.37.10.1 matt }
561 1.37.10.1 matt }
562 1.37.10.1 matt
563 1.37.10.1 matt if (pg->gl_matchc == 0)
564 1.37.10.1 matt goto notfound;
565 1.37.10.1 matt
566 1.37.10.1 matt /* clip suffix for the suffix check below */
567 1.37.10.1 matt p = strrchr(escpage, '.');
568 1.37.10.1 matt if (p && p[0] == '.' && isdigit((unsigned char)p[1]))
569 1.37.10.1 matt p[0] = '\0';
570 1.37.10.1 matt
571 1.37.10.1 matt found = 0;
572 1.37.10.1 matt for (cnt = pg->gl_pathc - pg->gl_matchc;
573 1.37.10.1 matt cnt < pg->gl_pathc; ++cnt)
574 1.37.10.1 matt {
575 1.37.10.1 matt found = manual_find_buildkeyword(escpage, "%s%s",
576 1.37.10.1 matt mp, pg, cnt);
577 1.37.10.1 matt if (found) {
578 1.37.10.1 matt anyfound = 1;
579 1.37.10.1 matt if (!mp->all) {
580 1.37.10.1 matt /* Delete any other matches. */
581 1.37.10.1 matt while (++cnt< pg->gl_pathc)
582 1.37.10.1 matt pg->gl_pathv[cnt] = "";
583 1.37.10.1 matt break;
584 1.37.10.1 matt }
585 1.37.10.1 matt continue;
586 1.37.10.1 matt }
587 1.37.10.1 matt
588 1.37.10.1 matt /* It's not a man page, forget about it. */
589 1.37.10.1 matt pg->gl_pathv[cnt] = "";
590 1.37.10.1 matt }
591 1.37.10.1 matt
592 1.37.10.1 matt notfound:
593 1.37.10.1 matt if (!anyfound) {
594 1.37.10.1 matt if (addentry(mp->missinglist, page, 0) < 0) {
595 1.37.10.1 matt warn("malloc");
596 1.37.10.1 matt (void)cleanup();
597 1.37.10.1 matt exit(EXIT_FAILURE);
598 1.37.10.1 matt }
599 1.37.10.1 matt }
600 1.37.10.1 matt free(escpage);
601 1.37.10.1 matt return anyfound;
602 1.37.10.1 matt }
603 1.37.10.1 matt
604 1.33 chuck /* For each man directory in mymanpath ... */
605 1.33 chuck TAILQ_FOREACH(mdir, &mp->mymanpath->entrylist, q) {
606 1.33 chuck
607 1.33 chuck /*
608 1.33 chuck * use glob(3) to look in the filesystem for matching files.
609 1.33 chuck * match any suffix here, as we will check that later.
610 1.33 chuck */
611 1.33 chuck (void)snprintf(buf, sizeof(buf), "%s/%s.*", mdir->s, escpage);
612 1.12 kleink if ((error = glob(buf,
613 1.12 kleink GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
614 1.12 kleink if (error == GLOB_NOMATCH)
615 1.12 kleink continue;
616 1.12 kleink else {
617 1.12 kleink warn("globbing");
618 1.12 kleink (void)cleanup();
619 1.12 kleink exit(1);
620 1.12 kleink }
621 1.1 cgd }
622 1.1 cgd if (pg->gl_matchc == 0)
623 1.1 cgd continue;
624 1.1 cgd
625 1.33 chuck /*
626 1.33 chuck * start going through the matches glob(3) just found and
627 1.33 chuck * use m.pathsearch (if present) to filter out pages we
628 1.33 chuck * don't want. then verify the suffix is valid, and build
629 1.33 chuck * the page if we have a _build suffix.
630 1.33 chuck */
631 1.1 cgd for (cnt = pg->gl_pathc - pg->gl_matchc;
632 1.1 cgd cnt < pg->gl_pathc; ++cnt) {
633 1.1 cgd
634 1.33 chuck /* filter on directory path name */
635 1.33 chuck if (mp->pathsearch) {
636 1.33 chuck p = strstr(pg->gl_pathv[cnt], mp->pathsearch);
637 1.23 jdolecek if (!p || strchr(p, '/') == NULL) {
638 1.33 chuck pg->gl_pathv[cnt] = ""; /* zap! */
639 1.23 jdolecek continue;
640 1.23 jdolecek }
641 1.23 jdolecek }
642 1.23 jdolecek
643 1.1 cgd /*
644 1.1 cgd * Try the _suffix key words first.
645 1.1 cgd *
646 1.1 cgd * XXX
647 1.1 cgd * Older versions of man.conf didn't have the suffix
648 1.1 cgd * key words, it was assumed that everything was a .0.
649 1.1 cgd * We just test for .0 first, it's fast and probably
650 1.1 cgd * going to hit.
651 1.1 cgd */
652 1.27 groo (void)snprintf(buf, sizeof(buf), "*/%s.0", escpage);
653 1.1 cgd if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
654 1.3 cgd goto next;
655 1.1 cgd
656 1.28 lukem found = 0;
657 1.33 chuck TAILQ_FOREACH(suffix, &mp->suffixlist->entrylist, q) {
658 1.1 cgd (void)snprintf(buf,
659 1.27 groo sizeof(buf), "*/%s%s", escpage,
660 1.33 chuck suffix->s);
661 1.1 cgd if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
662 1.1 cgd found = 1;
663 1.1 cgd break;
664 1.1 cgd }
665 1.1 cgd }
666 1.3 cgd if (found)
667 1.3 cgd goto next;
668 1.1 cgd
669 1.1 cgd /* Try the _build key words next. */
670 1.37.10.1 matt found = manual_find_buildkeyword(escpage, "*/%s%s",
671 1.37.10.1 matt mp, pg, cnt);
672 1.1 cgd if (found) {
673 1.3 cgd next: anyfound = 1;
674 1.33 chuck if (!mp->all) {
675 1.3 cgd /* Delete any other matches. */
676 1.3 cgd while (++cnt< pg->gl_pathc)
677 1.3 cgd pg->gl_pathv[cnt] = "";
678 1.1 cgd break;
679 1.3 cgd }
680 1.1 cgd continue;
681 1.1 cgd }
682 1.1 cgd
683 1.1 cgd /* It's not a man page, forget about it. */
684 1.1 cgd pg->gl_pathv[cnt] = "";
685 1.1 cgd }
686 1.1 cgd
687 1.33 chuck if (anyfound && !mp->all)
688 1.1 cgd break;
689 1.1 cgd }
690 1.1 cgd
691 1.1 cgd /* If not found, enter onto the missing list. */
692 1.1 cgd if (!anyfound) {
693 1.33 chuck if (addentry(mp->missinglist, page, 0) < 0) {
694 1.9 mikel warn("malloc");
695 1.1 cgd (void)cleanup();
696 1.1 cgd exit(1);
697 1.1 cgd }
698 1.1 cgd }
699 1.27 groo
700 1.27 groo free(escpage);
701 1.1 cgd return (anyfound);
702 1.1 cgd }
703 1.1 cgd
704 1.1 cgd /*
705 1.1 cgd * build_page --
706 1.1 cgd * Build a man page for display.
707 1.1 cgd */
708 1.1 cgd static void
709 1.33 chuck build_page(char *fmt, char **pathp, struct manstate *mp)
710 1.1 cgd {
711 1.1 cgd static int warned;
712 1.33 chuck int olddir, fd, n, tmpdirlen;
713 1.4 jtc char *p, *b;
714 1.19 kleink char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
715 1.19 kleink const char *tmpdir;
716 1.1 cgd
717 1.1 cgd /* Let the user know this may take awhile. */
718 1.1 cgd if (!warned) {
719 1.1 cgd warned = 1;
720 1.1 cgd warnx("Formatting manual page...");
721 1.1 cgd }
722 1.1 cgd
723 1.7 tls /*
724 1.7 tls * Historically man chdir'd to the root of the man tree.
725 1.7 tls * This was used in man pages that contained relative ".so"
726 1.7 tls * directives (including other man pages for command aliases etc.)
727 1.7 tls * It even went one step farther, by examining the first line
728 1.7 tls * of the man page and parsing the .so filename so it would
729 1.7 tls * make hard(?) links to the cat'ted man pages for space savings.
730 1.7 tls * (We don't do that here, but we could).
731 1.7 tls */
732 1.7 tls
733 1.7 tls /* copy and find the end */
734 1.7 tls for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
735 1.7 tls continue;
736 1.7 tls
737 1.33 chuck /*
738 1.33 chuck * skip the last two path components, page name and man[n] ...
739 1.33 chuck * (e.g. buf will be "/usr/share/man" and p will be "man1/man.1")
740 1.33 chuck * we also save a pointer to our current directory so that we
741 1.33 chuck * can fchdir() back to it. this allows relative MANDIR paths
742 1.33 chuck * to work with multiple man pages... e.g. consider:
743 1.33 chuck * cd /usr/share && man -M ./man cat ls
744 1.33 chuck * when no "cat1" subdir files are present.
745 1.33 chuck */
746 1.33 chuck olddir = -1;
747 1.10 tv for (--b, --p, n = 2; b != buf; b--, p--)
748 1.10 tv if (*b == '/')
749 1.10 tv if (--n == 0) {
750 1.10 tv *b = '\0';
751 1.33 chuck olddir = open(".", O_RDONLY);
752 1.10 tv (void) chdir(buf);
753 1.10 tv p++;
754 1.10 tv break;
755 1.10 tv }
756 1.4 jtc
757 1.4 jtc
758 1.33 chuck /* advance fmt pass the suffix spec to the printf format string */
759 1.15 christos for (; *fmt && isspace((unsigned char)*fmt); ++fmt)
760 1.15 christos continue;
761 1.1 cgd
762 1.1 cgd /*
763 1.1 cgd * Get a temporary file and build a version of the file
764 1.1 cgd * to display. Replace the old file name with the new one.
765 1.1 cgd */
766 1.19 kleink if ((tmpdir = getenv("TMPDIR")) == NULL)
767 1.19 kleink tmpdir = _PATH_TMP;
768 1.33 chuck tmpdirlen = strlen(tmpdir);
769 1.33 chuck (void)snprintf(tpath, sizeof (tpath), "%s%s%s", tmpdir,
770 1.33 chuck (tmpdirlen && tmpdir[tmpdirlen-1] == '/') ? "" : "/", TMPFILE);
771 1.1 cgd if ((fd = mkstemp(tpath)) == -1) {
772 1.1 cgd warn("%s", tpath);
773 1.1 cgd (void)cleanup();
774 1.1 cgd exit(1);
775 1.1 cgd }
776 1.1 cgd (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
777 1.10 tv (void)snprintf(cmd, sizeof(cmd), buf, p);
778 1.1 cgd (void)system(cmd);
779 1.1 cgd (void)close(fd);
780 1.1 cgd if ((*pathp = strdup(tpath)) == NULL) {
781 1.9 mikel warn("malloc");
782 1.1 cgd (void)cleanup();
783 1.1 cgd exit(1);
784 1.1 cgd }
785 1.1 cgd
786 1.1 cgd /* Link the built file into the remove-when-done list. */
787 1.33 chuck if (addentry(mp->intmp, *pathp, 0) < 0) {
788 1.9 mikel warn("malloc");
789 1.1 cgd (void)cleanup();
790 1.1 cgd exit(1);
791 1.1 cgd }
792 1.33 chuck
793 1.33 chuck /* restore old directory so relative manpaths still work */
794 1.33 chuck if (olddir != -1) {
795 1.33 chuck fchdir(olddir);
796 1.33 chuck close(olddir);
797 1.33 chuck }
798 1.1 cgd }
799 1.1 cgd
800 1.1 cgd /*
801 1.1 cgd * how --
802 1.1 cgd * display how information
803 1.1 cgd */
804 1.1 cgd static void
805 1.33 chuck how(char *fname)
806 1.1 cgd {
807 1.1 cgd FILE *fp;
808 1.1 cgd
809 1.1 cgd int lcnt, print;
810 1.1 cgd char *p, buf[256];
811 1.1 cgd
812 1.1 cgd if (!(fp = fopen(fname, "r"))) {
813 1.1 cgd warn("%s", fname);
814 1.1 cgd (void)cleanup();
815 1.1 cgd exit (1);
816 1.1 cgd }
817 1.1 cgd #define S1 "SYNOPSIS"
818 1.1 cgd #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
819 1.1 cgd #define D1 "DESCRIPTION"
820 1.1 cgd #define D2 "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
821 1.1 cgd for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
822 1.1 cgd if (!strncmp(buf, S1, sizeof(S1) - 1) ||
823 1.1 cgd !strncmp(buf, S2, sizeof(S2) - 1)) {
824 1.1 cgd print = 1;
825 1.1 cgd continue;
826 1.1 cgd } else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
827 1.34 christos !strncmp(buf, D2, sizeof(D2) - 1)) {
828 1.34 christos if (fp)
829 1.34 christos (void)fclose(fp);
830 1.1 cgd return;
831 1.34 christos }
832 1.1 cgd if (!print)
833 1.1 cgd continue;
834 1.1 cgd if (*buf == '\n')
835 1.1 cgd ++lcnt;
836 1.1 cgd else {
837 1.1 cgd for(; lcnt; --lcnt)
838 1.1 cgd (void)putchar('\n');
839 1.15 christos for (p = buf; isspace((unsigned char)*p); ++p)
840 1.15 christos continue;
841 1.1 cgd (void)fputs(p, stdout);
842 1.1 cgd }
843 1.1 cgd }
844 1.1 cgd (void)fclose(fp);
845 1.1 cgd }
846 1.1 cgd
847 1.1 cgd /*
848 1.1 cgd * cat --
849 1.1 cgd * cat out the file
850 1.1 cgd */
851 1.1 cgd static void
852 1.33 chuck cat(char *fname)
853 1.1 cgd {
854 1.1 cgd int fd, n;
855 1.1 cgd char buf[2048];
856 1.1 cgd
857 1.1 cgd if ((fd = open(fname, O_RDONLY, 0)) < 0) {
858 1.1 cgd warn("%s", fname);
859 1.1 cgd (void)cleanup();
860 1.1 cgd exit(1);
861 1.1 cgd }
862 1.1 cgd while ((n = read(fd, buf, sizeof(buf))) > 0)
863 1.1 cgd if (write(STDOUT_FILENO, buf, n) != n) {
864 1.1 cgd warn("write");
865 1.1 cgd (void)cleanup();
866 1.1 cgd exit (1);
867 1.1 cgd }
868 1.1 cgd if (n == -1) {
869 1.1 cgd warn("read");
870 1.1 cgd (void)cleanup();
871 1.1 cgd exit(1);
872 1.1 cgd }
873 1.1 cgd (void)close(fd);
874 1.1 cgd }
875 1.1 cgd
876 1.1 cgd /*
877 1.1 cgd * check_pager --
878 1.1 cgd * check the user supplied page information
879 1.1 cgd */
880 1.23 jdolecek static const char *
881 1.33 chuck check_pager(const char *name)
882 1.1 cgd {
883 1.24 thorpej const char *p;
884 1.1 cgd
885 1.1 cgd /*
886 1.1 cgd * if the user uses "more", we make it "more -s"; watch out for
887 1.1 cgd * PAGER = "mypager /usr/ucb/more"
888 1.1 cgd */
889 1.15 christos for (p = name; *p && !isspace((unsigned char)*p); ++p)
890 1.15 christos continue;
891 1.1 cgd for (; p > name && *p != '/'; --p);
892 1.1 cgd if (p != name)
893 1.1 cgd ++p;
894 1.1 cgd
895 1.1 cgd /* make sure it's "more", not "morex" */
896 1.15 christos if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){
897 1.23 jdolecek char *newname;
898 1.24 thorpej (void)asprintf(&newname, "%s %s", p, "-s");
899 1.23 jdolecek name = newname;
900 1.1 cgd }
901 1.23 jdolecek
902 1.23 jdolecek return (name);
903 1.1 cgd }
904 1.1 cgd
905 1.1 cgd /*
906 1.1 cgd * jump --
907 1.1 cgd * strip out flag argument and jump
908 1.1 cgd */
909 1.1 cgd static void
910 1.33 chuck jump(char **argv, char *flag, char *name)
911 1.1 cgd {
912 1.1 cgd char **arg;
913 1.1 cgd
914 1.1 cgd argv[0] = name;
915 1.1 cgd for (arg = argv + 1; *arg; ++arg)
916 1.1 cgd if (!strcmp(*arg, flag))
917 1.1 cgd break;
918 1.1 cgd for (; *arg; ++arg)
919 1.1 cgd arg[0] = arg[1];
920 1.1 cgd execvp(name, argv);
921 1.1 cgd (void)fprintf(stderr, "%s: Command not found.\n", name);
922 1.1 cgd exit(1);
923 1.1 cgd }
924 1.1 cgd
925 1.1 cgd /*
926 1.1 cgd * onsig --
927 1.1 cgd * If signaled, delete the temporary files.
928 1.1 cgd */
929 1.1 cgd static void
930 1.33 chuck onsig(int signo)
931 1.1 cgd {
932 1.18 itohy
933 1.1 cgd (void)cleanup();
934 1.1 cgd
935 1.36 lukem (void)raise_default_signal(signo);
936 1.1 cgd
937 1.1 cgd /* NOTREACHED */
938 1.1 cgd exit (1);
939 1.1 cgd }
940 1.1 cgd
941 1.1 cgd /*
942 1.1 cgd * cleanup --
943 1.1 cgd * Clean up temporary files, show any error messages.
944 1.1 cgd */
945 1.1 cgd static int
946 1.1 cgd cleanup()
947 1.1 cgd {
948 1.1 cgd TAG *intmpp, *missp;
949 1.1 cgd ENTRY *ep;
950 1.1 cgd int rval;
951 1.1 cgd
952 1.1 cgd rval = 0;
953 1.33 chuck /*
954 1.33 chuck * note that _missing and _intmp were created by main(), so
955 1.33 chuck * gettag() cannot return NULL here.
956 1.33 chuck */
957 1.33 chuck missp = gettag("_missing", 0); /* missing man pages */
958 1.33 chuck intmpp = gettag("_intmp", 0); /* tmp files we need to unlink */
959 1.33 chuck
960 1.33 chuck TAILQ_FOREACH(ep, &missp->entrylist, q) {
961 1.33 chuck warnx("no entry for %s in the manual.", ep->s);
962 1.33 chuck rval = 1;
963 1.33 chuck }
964 1.33 chuck
965 1.33 chuck TAILQ_FOREACH(ep, &intmpp->entrylist, q)
966 1.33 chuck (void)unlink(ep->s);
967 1.1 cgd
968 1.1 cgd return (rval);
969 1.1 cgd }
970 1.1 cgd
971 1.1 cgd /*
972 1.1 cgd * usage --
973 1.1 cgd * print usage message and die
974 1.1 cgd */
975 1.1 cgd static void
976 1.1 cgd usage()
977 1.1 cgd {
978 1.33 chuck (void)fprintf(stderr, "usage: %s [-acw|-h] [-C cfg] [-M path] "
979 1.33 chuck "[-m path] [-S srch] [[-s] sect] name ...\n", getprogname());
980 1.33 chuck (void)fprintf(stderr,
981 1.33 chuck "usage: %s -k [-C cfg] [-M path] [-m path] keyword ...\n",
982 1.33 chuck getprogname());
983 1.1 cgd exit(1);
984 1.1 cgd }
985