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