man.c revision 1.36 1 1.36 lukem /* $NetBSD: man.c,v 1.36 2007/10/05 07:38:52 lukem 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.36 lukem __RCSID("$NetBSD: man.c,v 1.36 2007/10/05 07:38:52 lukem 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.1 cgd /*
477 1.1 cgd * manual --
478 1.1 cgd * Search the manuals for the pages.
479 1.1 cgd */
480 1.1 cgd static int
481 1.33 chuck manual(char *page, struct manstate *mp, glob_t *pg)
482 1.1 cgd {
483 1.33 chuck ENTRY *suffix, *mdir;
484 1.32 christos int anyfound, error, found;
485 1.32 christos size_t cnt;
486 1.27 groo char *p, buf[MAXPATHLEN], *escpage, *eptr;
487 1.27 groo static const char escglob[] = "\\~?*{}[]";
488 1.1 cgd
489 1.1 cgd anyfound = 0;
490 1.1 cgd
491 1.27 groo /*
492 1.27 groo * Fixup page which may contain glob(3) special characters, e.g.
493 1.27 groo * the famous "No man page for [" FAQ.
494 1.27 groo */
495 1.27 groo if ((escpage = malloc((2 * strlen(page)) + 1)) == NULL) {
496 1.27 groo warn("malloc");
497 1.27 groo (void)cleanup();
498 1.27 groo exit(1);
499 1.27 groo }
500 1.27 groo
501 1.27 groo p = page;
502 1.27 groo eptr = escpage;
503 1.27 groo
504 1.27 groo while (*p) {
505 1.27 groo if (strchr(escglob, *p) != NULL) {
506 1.27 groo *eptr++ = '\\';
507 1.27 groo *eptr++ = *p++;
508 1.27 groo } else
509 1.27 groo *eptr++ = *p++;
510 1.27 groo }
511 1.27 groo
512 1.27 groo *eptr = '\0';
513 1.27 groo
514 1.33 chuck /* For each man directory in mymanpath ... */
515 1.33 chuck TAILQ_FOREACH(mdir, &mp->mymanpath->entrylist, q) {
516 1.33 chuck
517 1.33 chuck /*
518 1.33 chuck * use glob(3) to look in the filesystem for matching files.
519 1.33 chuck * match any suffix here, as we will check that later.
520 1.33 chuck */
521 1.33 chuck (void)snprintf(buf, sizeof(buf), "%s/%s.*", mdir->s, escpage);
522 1.12 kleink if ((error = glob(buf,
523 1.12 kleink GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT, NULL, pg)) != 0) {
524 1.12 kleink if (error == GLOB_NOMATCH)
525 1.12 kleink continue;
526 1.12 kleink else {
527 1.12 kleink warn("globbing");
528 1.12 kleink (void)cleanup();
529 1.12 kleink exit(1);
530 1.12 kleink }
531 1.1 cgd }
532 1.1 cgd if (pg->gl_matchc == 0)
533 1.1 cgd continue;
534 1.1 cgd
535 1.33 chuck /*
536 1.33 chuck * start going through the matches glob(3) just found and
537 1.33 chuck * use m.pathsearch (if present) to filter out pages we
538 1.33 chuck * don't want. then verify the suffix is valid, and build
539 1.33 chuck * the page if we have a _build suffix.
540 1.33 chuck */
541 1.1 cgd for (cnt = pg->gl_pathc - pg->gl_matchc;
542 1.1 cgd cnt < pg->gl_pathc; ++cnt) {
543 1.1 cgd
544 1.33 chuck /* filter on directory path name */
545 1.33 chuck if (mp->pathsearch) {
546 1.33 chuck p = strstr(pg->gl_pathv[cnt], mp->pathsearch);
547 1.23 jdolecek if (!p || strchr(p, '/') == NULL) {
548 1.33 chuck pg->gl_pathv[cnt] = ""; /* zap! */
549 1.23 jdolecek continue;
550 1.23 jdolecek }
551 1.23 jdolecek }
552 1.23 jdolecek
553 1.1 cgd /*
554 1.1 cgd * Try the _suffix key words first.
555 1.1 cgd *
556 1.1 cgd * XXX
557 1.1 cgd * Older versions of man.conf didn't have the suffix
558 1.1 cgd * key words, it was assumed that everything was a .0.
559 1.1 cgd * We just test for .0 first, it's fast and probably
560 1.1 cgd * going to hit.
561 1.1 cgd */
562 1.27 groo (void)snprintf(buf, sizeof(buf), "*/%s.0", escpage);
563 1.1 cgd if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
564 1.3 cgd goto next;
565 1.1 cgd
566 1.28 lukem found = 0;
567 1.33 chuck TAILQ_FOREACH(suffix, &mp->suffixlist->entrylist, q) {
568 1.1 cgd (void)snprintf(buf,
569 1.27 groo sizeof(buf), "*/%s%s", escpage,
570 1.33 chuck suffix->s);
571 1.1 cgd if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
572 1.1 cgd found = 1;
573 1.1 cgd break;
574 1.1 cgd }
575 1.1 cgd }
576 1.3 cgd if (found)
577 1.3 cgd goto next;
578 1.1 cgd
579 1.1 cgd /* Try the _build key words next. */
580 1.28 lukem found = 0;
581 1.33 chuck TAILQ_FOREACH(suffix, &mp->buildlist->entrylist, q) {
582 1.33 chuck for (p = suffix->s;
583 1.28 lukem *p != '\0' && !isspace((unsigned char)*p);
584 1.28 lukem ++p)
585 1.28 lukem continue;
586 1.1 cgd if (*p == '\0')
587 1.1 cgd continue;
588 1.1 cgd *p = '\0';
589 1.1 cgd (void)snprintf(buf,
590 1.27 groo sizeof(buf), "*/%s%s", escpage,
591 1.33 chuck suffix->s);
592 1.1 cgd if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
593 1.33 chuck if (!mp->where)
594 1.1 cgd build_page(p + 1,
595 1.33 chuck &pg->gl_pathv[cnt], mp);
596 1.1 cgd *p = ' ';
597 1.1 cgd found = 1;
598 1.1 cgd break;
599 1.1 cgd }
600 1.1 cgd *p = ' ';
601 1.1 cgd }
602 1.1 cgd if (found) {
603 1.3 cgd next: anyfound = 1;
604 1.33 chuck if (!mp->all) {
605 1.3 cgd /* Delete any other matches. */
606 1.3 cgd while (++cnt< pg->gl_pathc)
607 1.3 cgd pg->gl_pathv[cnt] = "";
608 1.1 cgd break;
609 1.3 cgd }
610 1.1 cgd continue;
611 1.1 cgd }
612 1.1 cgd
613 1.1 cgd /* It's not a man page, forget about it. */
614 1.1 cgd pg->gl_pathv[cnt] = "";
615 1.1 cgd }
616 1.1 cgd
617 1.33 chuck if (anyfound && !mp->all)
618 1.1 cgd break;
619 1.1 cgd }
620 1.1 cgd
621 1.1 cgd /* If not found, enter onto the missing list. */
622 1.1 cgd if (!anyfound) {
623 1.33 chuck if (addentry(mp->missinglist, page, 0) < 0) {
624 1.9 mikel warn("malloc");
625 1.1 cgd (void)cleanup();
626 1.1 cgd exit(1);
627 1.1 cgd }
628 1.1 cgd }
629 1.27 groo
630 1.27 groo free(escpage);
631 1.1 cgd return (anyfound);
632 1.1 cgd }
633 1.1 cgd
634 1.1 cgd /*
635 1.1 cgd * build_page --
636 1.1 cgd * Build a man page for display.
637 1.1 cgd */
638 1.1 cgd static void
639 1.33 chuck build_page(char *fmt, char **pathp, struct manstate *mp)
640 1.1 cgd {
641 1.1 cgd static int warned;
642 1.33 chuck int olddir, fd, n, tmpdirlen;
643 1.4 jtc char *p, *b;
644 1.19 kleink char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[MAXPATHLEN];
645 1.19 kleink const char *tmpdir;
646 1.1 cgd
647 1.1 cgd /* Let the user know this may take awhile. */
648 1.1 cgd if (!warned) {
649 1.1 cgd warned = 1;
650 1.1 cgd warnx("Formatting manual page...");
651 1.1 cgd }
652 1.1 cgd
653 1.7 tls /*
654 1.7 tls * Historically man chdir'd to the root of the man tree.
655 1.7 tls * This was used in man pages that contained relative ".so"
656 1.7 tls * directives (including other man pages for command aliases etc.)
657 1.7 tls * It even went one step farther, by examining the first line
658 1.7 tls * of the man page and parsing the .so filename so it would
659 1.7 tls * make hard(?) links to the cat'ted man pages for space savings.
660 1.7 tls * (We don't do that here, but we could).
661 1.7 tls */
662 1.7 tls
663 1.7 tls /* copy and find the end */
664 1.7 tls for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
665 1.7 tls continue;
666 1.7 tls
667 1.33 chuck /*
668 1.33 chuck * skip the last two path components, page name and man[n] ...
669 1.33 chuck * (e.g. buf will be "/usr/share/man" and p will be "man1/man.1")
670 1.33 chuck * we also save a pointer to our current directory so that we
671 1.33 chuck * can fchdir() back to it. this allows relative MANDIR paths
672 1.33 chuck * to work with multiple man pages... e.g. consider:
673 1.33 chuck * cd /usr/share && man -M ./man cat ls
674 1.33 chuck * when no "cat1" subdir files are present.
675 1.33 chuck */
676 1.33 chuck olddir = -1;
677 1.10 tv for (--b, --p, n = 2; b != buf; b--, p--)
678 1.10 tv if (*b == '/')
679 1.10 tv if (--n == 0) {
680 1.10 tv *b = '\0';
681 1.33 chuck olddir = open(".", O_RDONLY);
682 1.10 tv (void) chdir(buf);
683 1.10 tv p++;
684 1.10 tv break;
685 1.10 tv }
686 1.4 jtc
687 1.4 jtc
688 1.33 chuck /* advance fmt pass the suffix spec to the printf format string */
689 1.15 christos for (; *fmt && isspace((unsigned char)*fmt); ++fmt)
690 1.15 christos continue;
691 1.1 cgd
692 1.1 cgd /*
693 1.1 cgd * Get a temporary file and build a version of the file
694 1.1 cgd * to display. Replace the old file name with the new one.
695 1.1 cgd */
696 1.19 kleink if ((tmpdir = getenv("TMPDIR")) == NULL)
697 1.19 kleink tmpdir = _PATH_TMP;
698 1.33 chuck tmpdirlen = strlen(tmpdir);
699 1.33 chuck (void)snprintf(tpath, sizeof (tpath), "%s%s%s", tmpdir,
700 1.33 chuck (tmpdirlen && tmpdir[tmpdirlen-1] == '/') ? "" : "/", TMPFILE);
701 1.1 cgd if ((fd = mkstemp(tpath)) == -1) {
702 1.1 cgd warn("%s", tpath);
703 1.1 cgd (void)cleanup();
704 1.1 cgd exit(1);
705 1.1 cgd }
706 1.1 cgd (void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
707 1.10 tv (void)snprintf(cmd, sizeof(cmd), buf, p);
708 1.1 cgd (void)system(cmd);
709 1.1 cgd (void)close(fd);
710 1.1 cgd if ((*pathp = strdup(tpath)) == NULL) {
711 1.9 mikel warn("malloc");
712 1.1 cgd (void)cleanup();
713 1.1 cgd exit(1);
714 1.1 cgd }
715 1.1 cgd
716 1.1 cgd /* Link the built file into the remove-when-done list. */
717 1.33 chuck if (addentry(mp->intmp, *pathp, 0) < 0) {
718 1.9 mikel warn("malloc");
719 1.1 cgd (void)cleanup();
720 1.1 cgd exit(1);
721 1.1 cgd }
722 1.33 chuck
723 1.33 chuck /* restore old directory so relative manpaths still work */
724 1.33 chuck if (olddir != -1) {
725 1.33 chuck fchdir(olddir);
726 1.33 chuck close(olddir);
727 1.33 chuck }
728 1.1 cgd }
729 1.1 cgd
730 1.1 cgd /*
731 1.1 cgd * how --
732 1.1 cgd * display how information
733 1.1 cgd */
734 1.1 cgd static void
735 1.33 chuck how(char *fname)
736 1.1 cgd {
737 1.1 cgd FILE *fp;
738 1.1 cgd
739 1.1 cgd int lcnt, print;
740 1.1 cgd char *p, buf[256];
741 1.1 cgd
742 1.1 cgd if (!(fp = fopen(fname, "r"))) {
743 1.1 cgd warn("%s", fname);
744 1.1 cgd (void)cleanup();
745 1.1 cgd exit (1);
746 1.1 cgd }
747 1.1 cgd #define S1 "SYNOPSIS"
748 1.1 cgd #define S2 "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
749 1.1 cgd #define D1 "DESCRIPTION"
750 1.1 cgd #define D2 "D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
751 1.1 cgd for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
752 1.1 cgd if (!strncmp(buf, S1, sizeof(S1) - 1) ||
753 1.1 cgd !strncmp(buf, S2, sizeof(S2) - 1)) {
754 1.1 cgd print = 1;
755 1.1 cgd continue;
756 1.1 cgd } else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
757 1.34 christos !strncmp(buf, D2, sizeof(D2) - 1)) {
758 1.34 christos if (fp)
759 1.34 christos (void)fclose(fp);
760 1.1 cgd return;
761 1.34 christos }
762 1.1 cgd if (!print)
763 1.1 cgd continue;
764 1.1 cgd if (*buf == '\n')
765 1.1 cgd ++lcnt;
766 1.1 cgd else {
767 1.1 cgd for(; lcnt; --lcnt)
768 1.1 cgd (void)putchar('\n');
769 1.15 christos for (p = buf; isspace((unsigned char)*p); ++p)
770 1.15 christos continue;
771 1.1 cgd (void)fputs(p, stdout);
772 1.1 cgd }
773 1.1 cgd }
774 1.1 cgd (void)fclose(fp);
775 1.1 cgd }
776 1.1 cgd
777 1.1 cgd /*
778 1.1 cgd * cat --
779 1.1 cgd * cat out the file
780 1.1 cgd */
781 1.1 cgd static void
782 1.33 chuck cat(char *fname)
783 1.1 cgd {
784 1.1 cgd int fd, n;
785 1.1 cgd char buf[2048];
786 1.1 cgd
787 1.1 cgd if ((fd = open(fname, O_RDONLY, 0)) < 0) {
788 1.1 cgd warn("%s", fname);
789 1.1 cgd (void)cleanup();
790 1.1 cgd exit(1);
791 1.1 cgd }
792 1.1 cgd while ((n = read(fd, buf, sizeof(buf))) > 0)
793 1.1 cgd if (write(STDOUT_FILENO, buf, n) != n) {
794 1.1 cgd warn("write");
795 1.1 cgd (void)cleanup();
796 1.1 cgd exit (1);
797 1.1 cgd }
798 1.1 cgd if (n == -1) {
799 1.1 cgd warn("read");
800 1.1 cgd (void)cleanup();
801 1.1 cgd exit(1);
802 1.1 cgd }
803 1.1 cgd (void)close(fd);
804 1.1 cgd }
805 1.1 cgd
806 1.1 cgd /*
807 1.1 cgd * check_pager --
808 1.1 cgd * check the user supplied page information
809 1.1 cgd */
810 1.23 jdolecek static const char *
811 1.33 chuck check_pager(const char *name)
812 1.1 cgd {
813 1.24 thorpej const char *p;
814 1.1 cgd
815 1.1 cgd /*
816 1.1 cgd * if the user uses "more", we make it "more -s"; watch out for
817 1.1 cgd * PAGER = "mypager /usr/ucb/more"
818 1.1 cgd */
819 1.15 christos for (p = name; *p && !isspace((unsigned char)*p); ++p)
820 1.15 christos continue;
821 1.1 cgd for (; p > name && *p != '/'; --p);
822 1.1 cgd if (p != name)
823 1.1 cgd ++p;
824 1.1 cgd
825 1.1 cgd /* make sure it's "more", not "morex" */
826 1.15 christos if (!strncmp(p, "more", 4) && (!p[4] || isspace((unsigned char)p[4]))){
827 1.23 jdolecek char *newname;
828 1.24 thorpej (void)asprintf(&newname, "%s %s", p, "-s");
829 1.23 jdolecek name = newname;
830 1.1 cgd }
831 1.23 jdolecek
832 1.23 jdolecek return (name);
833 1.1 cgd }
834 1.1 cgd
835 1.1 cgd /*
836 1.1 cgd * jump --
837 1.1 cgd * strip out flag argument and jump
838 1.1 cgd */
839 1.1 cgd static void
840 1.33 chuck jump(char **argv, char *flag, char *name)
841 1.1 cgd {
842 1.1 cgd char **arg;
843 1.1 cgd
844 1.1 cgd argv[0] = name;
845 1.1 cgd for (arg = argv + 1; *arg; ++arg)
846 1.1 cgd if (!strcmp(*arg, flag))
847 1.1 cgd break;
848 1.1 cgd for (; *arg; ++arg)
849 1.1 cgd arg[0] = arg[1];
850 1.1 cgd execvp(name, argv);
851 1.1 cgd (void)fprintf(stderr, "%s: Command not found.\n", name);
852 1.1 cgd exit(1);
853 1.1 cgd }
854 1.1 cgd
855 1.1 cgd /*
856 1.1 cgd * onsig --
857 1.1 cgd * If signaled, delete the temporary files.
858 1.1 cgd */
859 1.1 cgd static void
860 1.33 chuck onsig(int signo)
861 1.1 cgd {
862 1.18 itohy
863 1.1 cgd (void)cleanup();
864 1.1 cgd
865 1.36 lukem (void)raise_default_signal(signo);
866 1.1 cgd
867 1.1 cgd /* NOTREACHED */
868 1.1 cgd exit (1);
869 1.1 cgd }
870 1.1 cgd
871 1.1 cgd /*
872 1.1 cgd * cleanup --
873 1.1 cgd * Clean up temporary files, show any error messages.
874 1.1 cgd */
875 1.1 cgd static int
876 1.1 cgd cleanup()
877 1.1 cgd {
878 1.1 cgd TAG *intmpp, *missp;
879 1.1 cgd ENTRY *ep;
880 1.1 cgd int rval;
881 1.1 cgd
882 1.1 cgd rval = 0;
883 1.33 chuck /*
884 1.33 chuck * note that _missing and _intmp were created by main(), so
885 1.33 chuck * gettag() cannot return NULL here.
886 1.33 chuck */
887 1.33 chuck missp = gettag("_missing", 0); /* missing man pages */
888 1.33 chuck intmpp = gettag("_intmp", 0); /* tmp files we need to unlink */
889 1.33 chuck
890 1.33 chuck TAILQ_FOREACH(ep, &missp->entrylist, q) {
891 1.33 chuck warnx("no entry for %s in the manual.", ep->s);
892 1.33 chuck rval = 1;
893 1.33 chuck }
894 1.33 chuck
895 1.33 chuck TAILQ_FOREACH(ep, &intmpp->entrylist, q)
896 1.33 chuck (void)unlink(ep->s);
897 1.1 cgd
898 1.1 cgd return (rval);
899 1.1 cgd }
900 1.1 cgd
901 1.1 cgd /*
902 1.1 cgd * usage --
903 1.1 cgd * print usage message and die
904 1.1 cgd */
905 1.1 cgd static void
906 1.1 cgd usage()
907 1.1 cgd {
908 1.33 chuck (void)fprintf(stderr, "usage: %s [-acw|-h] [-C cfg] [-M path] "
909 1.33 chuck "[-m path] [-S srch] [[-s] sect] name ...\n", getprogname());
910 1.33 chuck (void)fprintf(stderr,
911 1.33 chuck "usage: %s -k [-C cfg] [-M path] [-m path] keyword ...\n",
912 1.33 chuck getprogname());
913 1.1 cgd exit(1);
914 1.1 cgd }
915