Print this page
9718 update mandoc to 1.14.4
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mandoc/main.c
+++ new/usr/src/cmd/mandoc/main.c
1 -/* $Id: main.c,v 1.301 2017/07/26 10:21:55 schwarze Exp $ */
1 +/* $Id: main.c,v 1.306 2018/05/14 14:10:23 schwarze Exp $ */
2 2 /*
3 3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 - * Copyright (c) 2010-2012, 2014-2017 Ingo Schwarze <schwarze@openbsd.org>
4 + * Copyright (c) 2010-2012, 2014-2018 Ingo Schwarze <schwarze@openbsd.org>
5 5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
6 6 *
7 7 * Permission to use, copy, modify, and distribute this software for any
8 8 * purpose with or without fee is hereby granted, provided that the above
9 9 * copyright notice and this permission notice appear in all copies.
10 10 *
11 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 18 */
19 19 #include "config.h"
20 20
21 21 #include <sys/types.h>
22 +#include <sys/ioctl.h>
22 23 #include <sys/param.h> /* MACHINE */
24 +#include <sys/termios.h>
23 25 #include <sys/wait.h>
24 26
25 27 #include <assert.h>
26 28 #include <ctype.h>
27 29 #if HAVE_ERR
28 30 #include <err.h>
29 31 #endif
30 32 #include <errno.h>
31 33 #include <fcntl.h>
32 34 #include <glob.h>
33 35 #if HAVE_SANDBOX_INIT
34 36 #include <sandbox.h>
35 37 #endif
36 38 #include <signal.h>
37 39 #include <stdio.h>
38 40 #include <stdint.h>
39 41 #include <stdlib.h>
40 42 #include <string.h>
41 43 #include <time.h>
42 44 #include <unistd.h>
43 45
44 46 #include "mandoc_aux.h"
45 47 #include "mandoc.h"
46 48 #include "mandoc_xr.h"
47 49 #include "roff.h"
48 50 #include "mdoc.h"
49 51 #include "man.h"
50 52 #include "tag.h"
51 53 #include "main.h"
52 54 #include "manconf.h"
53 55 #include "mansearch.h"
54 56
55 57 enum outmode {
56 58 OUTMODE_DEF = 0,
57 59 OUTMODE_FLN,
58 60 OUTMODE_LST,
59 61 OUTMODE_ALL,
60 62 OUTMODE_ONE
61 63 };
62 64
63 65 enum outt {
64 66 OUTT_ASCII = 0, /* -Tascii */
65 67 OUTT_LOCALE, /* -Tlocale */
66 68 OUTT_UTF8, /* -Tutf8 */
67 69 OUTT_TREE, /* -Ttree */
68 70 OUTT_MAN, /* -Tman */
69 71 OUTT_HTML, /* -Thtml */
70 72 OUTT_MARKDOWN, /* -Tmarkdown */
71 73 OUTT_LINT, /* -Tlint */
72 74 OUTT_PS, /* -Tps */
73 75 OUTT_PDF /* -Tpdf */
74 76 };
75 77
76 78 struct curparse {
77 79 struct mparse *mp;
78 80 struct manoutput *outopts; /* output options */
79 81 void *outdata; /* data for output */
80 82 char *os_s; /* operating system for display */
81 83 int wstop; /* stop after a file with a warning */
82 84 enum mandocerr mmin; /* ignore messages below this */
83 85 enum mandoc_os os_e; /* check base system conventions */
84 86 enum outt outtype; /* which output to use */
85 87 };
86 88
87 89
88 90 int mandocdb(int, char *[]);
89 91
90 92 static void check_xr(const char *);
91 93 static int fs_lookup(const struct manpaths *,
92 94 size_t ipath, const char *,
93 95 const char *, const char *,
94 96 struct manpage **, size_t *);
95 97 static int fs_search(const struct mansearch *,
96 98 const struct manpaths *, int, char**,
97 99 struct manpage **, size_t *);
98 100 static int koptions(int *, char *);
99 101 static void moptions(int *, char *);
100 102 static void mmsg(enum mandocerr, enum mandoclevel,
101 103 const char *, int, int, const char *);
102 104 static void outdata_alloc(struct curparse *);
103 105 static void parse(struct curparse *, int, const char *);
104 106 static void passthrough(const char *, int, int);
105 107 static pid_t spawn_pager(struct tag_files *);
106 108 static int toptions(struct curparse *, char *);
107 109 static void usage(enum argmode) __attribute__((__noreturn__));
108 110 static int woptions(struct curparse *, char *);
109 111
110 112 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
111 113 static char help_arg[] = "help";
112 114 static char *help_argv[] = {help_arg, NULL};
↓ open down ↓ |
80 lines elided |
↑ open up ↑ |
113 115 static enum mandoclevel rc;
114 116 static FILE *mmsg_stream;
115 117
116 118
117 119 int
118 120 main(int argc, char *argv[])
119 121 {
120 122 struct manconf conf;
121 123 struct mansearch search;
122 124 struct curparse curp;
125 + struct winsize ws;
123 126 struct tag_files *tag_files;
124 127 struct manpage *res, *resp;
125 128 const char *progname, *sec, *thisarg;
126 129 char *conf_file, *defpaths, *auxpaths;
127 130 char *oarg;
128 131 unsigned char *uc;
129 132 size_t i, sz;
130 133 int prio, best_prio;
131 134 enum outmode outmode;
132 - int fd;
135 + int fd, startdir;
133 136 int show_usage;
134 137 int options;
135 138 int use_pager;
136 139 int status, signum;
137 140 int c;
138 141 pid_t pager_pid, tc_pgid, man_pgid, pid;
139 142
140 143 #if HAVE_PROGNAME
141 144 progname = getprogname();
142 145 #else
143 146 if (argc < 1)
144 147 progname = mandoc_strdup("mandoc");
145 148 else if ((progname = strrchr(argv[0], '/')) == NULL)
146 149 progname = argv[0];
147 150 else
148 151 ++progname;
149 152 setprogname(progname);
150 153 #endif
151 154
152 155 if (strncmp(progname, "mandocdb", 8) == 0 ||
153 156 strcmp(progname, BINM_MAKEWHATIS) == 0)
154 157 return mandocdb(argc, argv);
155 158
156 159 #if HAVE_PLEDGE
157 160 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
158 161 err((int)MANDOCLEVEL_SYSERR, "pledge");
159 162 #endif
160 163
161 164 #if HAVE_SANDBOX_INIT
162 165 if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1)
163 166 errx((int)MANDOCLEVEL_SYSERR, "sandbox_init");
164 167 #endif
165 168
166 169 /* Search options. */
167 170
168 171 memset(&conf, 0, sizeof(conf));
169 172 conf_file = defpaths = NULL;
170 173 auxpaths = NULL;
171 174
172 175 memset(&search, 0, sizeof(struct mansearch));
173 176 search.outkey = "Nd";
174 177 oarg = NULL;
175 178
176 179 if (strcmp(progname, BINM_MAN) == 0)
177 180 search.argmode = ARG_NAME;
178 181 else if (strcmp(progname, BINM_APROPOS) == 0)
179 182 search.argmode = ARG_EXPR;
180 183 else if (strcmp(progname, BINM_WHATIS) == 0)
181 184 search.argmode = ARG_WORD;
182 185 else if (strncmp(progname, "help", 4) == 0)
183 186 search.argmode = ARG_NAME;
184 187 else
185 188 search.argmode = ARG_FILE;
186 189
187 190 /* Parser and formatter options. */
188 191
189 192 memset(&curp, 0, sizeof(struct curparse));
190 193 curp.outtype = OUTT_LOCALE;
191 194 curp.mmin = MANDOCERR_MAX;
192 195 curp.outopts = &conf.output;
193 196 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
194 197 mmsg_stream = stderr;
195 198
196 199 use_pager = 1;
197 200 tag_files = NULL;
198 201 show_usage = 0;
199 202 outmode = OUTMODE_DEF;
200 203
201 204 while ((c = getopt(argc, argv,
202 205 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
203 206 if (c == 'i' && search.argmode == ARG_EXPR) {
204 207 optind--;
205 208 break;
206 209 }
207 210 switch (c) {
208 211 case 'a':
209 212 outmode = OUTMODE_ALL;
210 213 break;
211 214 case 'C':
212 215 conf_file = optarg;
213 216 break;
214 217 case 'c':
215 218 use_pager = 0;
216 219 break;
217 220 case 'f':
218 221 search.argmode = ARG_WORD;
219 222 break;
220 223 case 'h':
221 224 conf.output.synopsisonly = 1;
222 225 use_pager = 0;
223 226 outmode = OUTMODE_ALL;
224 227 break;
225 228 case 'I':
226 229 if (strncmp(optarg, "os=", 3)) {
227 230 warnx("-I %s: Bad argument", optarg);
228 231 return (int)MANDOCLEVEL_BADARG;
229 232 }
230 233 if (curp.os_s != NULL) {
231 234 warnx("-I %s: Duplicate argument", optarg);
232 235 return (int)MANDOCLEVEL_BADARG;
233 236 }
234 237 curp.os_s = mandoc_strdup(optarg + 3);
235 238 break;
236 239 case 'K':
237 240 if ( ! koptions(&options, optarg))
238 241 return (int)MANDOCLEVEL_BADARG;
239 242 break;
240 243 case 'k':
241 244 search.argmode = ARG_EXPR;
242 245 break;
243 246 case 'l':
244 247 search.argmode = ARG_FILE;
245 248 outmode = OUTMODE_ALL;
246 249 break;
247 250 case 'M':
248 251 defpaths = optarg;
249 252 break;
250 253 case 'm':
251 254 auxpaths = optarg;
252 255 break;
253 256 case 'O':
254 257 oarg = optarg;
255 258 break;
256 259 case 'S':
257 260 search.arch = optarg;
258 261 break;
259 262 case 's':
260 263 search.sec = optarg;
261 264 break;
262 265 case 'T':
263 266 if ( ! toptions(&curp, optarg))
264 267 return (int)MANDOCLEVEL_BADARG;
265 268 break;
266 269 case 'W':
267 270 if ( ! woptions(&curp, optarg))
268 271 return (int)MANDOCLEVEL_BADARG;
269 272 break;
270 273 case 'w':
271 274 outmode = OUTMODE_FLN;
272 275 break;
273 276 default:
274 277 show_usage = 1;
275 278 break;
276 279 }
277 280 }
278 281
279 282 if (show_usage)
280 283 usage(search.argmode);
281 284
282 285 /* Postprocess options. */
283 286
284 287 if (outmode == OUTMODE_DEF) {
285 288 switch (search.argmode) {
286 289 case ARG_FILE:
287 290 outmode = OUTMODE_ALL;
288 291 use_pager = 0;
289 292 break;
290 293 case ARG_NAME:
291 294 outmode = OUTMODE_ONE;
292 295 break;
293 296 default:
294 297 outmode = OUTMODE_LST;
295 298 break;
296 299 }
297 300 }
298 301
299 302 if (oarg != NULL) {
300 303 if (outmode == OUTMODE_LST)
301 304 search.outkey = oarg;
302 305 else {
303 306 while (oarg != NULL) {
304 307 thisarg = oarg;
305 308 if (manconf_output(&conf.output,
306 309 strsep(&oarg, ","), 0) == 0)
307 310 continue;
308 311 warnx("-O %s: Bad argument", thisarg);
↓ open down ↓ |
166 lines elided |
↑ open up ↑ |
309 312 return (int)MANDOCLEVEL_BADARG;
310 313 }
311 314 }
312 315 }
313 316
314 317 if (outmode == OUTMODE_FLN ||
315 318 outmode == OUTMODE_LST ||
316 319 !isatty(STDOUT_FILENO))
317 320 use_pager = 0;
318 321
322 + if (use_pager &&
323 + (conf.output.width == 0 || conf.output.indent == 0) &&
324 + ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
325 + ws.ws_col > 1) {
326 + if (conf.output.width == 0 && ws.ws_col < 79)
327 + conf.output.width = ws.ws_col - 1;
328 + if (conf.output.indent == 0 && ws.ws_col < 66)
329 + conf.output.indent = 3;
330 + }
331 +
319 332 #if HAVE_PLEDGE
320 333 if (!use_pager)
321 334 if (pledge("stdio rpath", NULL) == -1)
322 335 err((int)MANDOCLEVEL_SYSERR, "pledge");
323 336 #endif
324 337
325 338 /* Parse arguments. */
326 339
327 340 if (argc > 0) {
328 341 argc -= optind;
329 342 argv += optind;
330 343 }
331 344 resp = NULL;
332 345
333 346 /*
334 347 * Quirks for help(1)
335 348 * and for a man(1) section argument without -s.
336 349 */
337 350
338 351 if (search.argmode == ARG_NAME) {
339 352 if (*progname == 'h') {
340 353 if (argc == 0) {
341 354 argv = help_argv;
342 355 argc = 1;
343 356 }
344 357 } else if (argc > 1 &&
345 358 ((uc = (unsigned char *)argv[0]) != NULL) &&
346 359 ((isdigit(uc[0]) && (uc[1] == '\0' ||
347 360 (isalpha(uc[1]) && uc[2] == '\0'))) ||
348 361 (uc[0] == 'n' && uc[1] == '\0'))) {
349 362 search.sec = (char *)uc;
350 363 argv++;
351 364 argc--;
352 365 }
353 366 if (search.arch == NULL)
354 367 search.arch = getenv("MACHINE");
355 368 #ifdef MACHINE
356 369 if (search.arch == NULL)
357 370 search.arch = MACHINE;
358 371 #endif
359 372 }
360 373
361 374 rc = MANDOCLEVEL_OK;
362 375
363 376 /* man(1), whatis(1), apropos(1) */
364 377
365 378 if (search.argmode != ARG_FILE) {
366 379 if (search.argmode == ARG_NAME &&
↓ open down ↓ |
38 lines elided |
↑ open up ↑ |
367 380 outmode == OUTMODE_ONE)
368 381 search.firstmatch = 1;
369 382
370 383 /* Access the mandoc database. */
371 384
372 385 manconf_parse(&conf, conf_file, defpaths, auxpaths);
373 386 if ( ! mansearch(&search, &conf.manpath,
374 387 argc, argv, &res, &sz))
375 388 usage(search.argmode);
376 389
377 - if (sz == 0) {
378 - if (search.argmode == ARG_NAME)
379 - fs_search(&search, &conf.manpath,
380 - argc, argv, &res, &sz);
381 - else
382 - warnx("nothing appropriate");
390 + if (sz == 0 && search.argmode == ARG_NAME)
391 + fs_search(&search, &conf.manpath,
392 + argc, argv, &res, &sz);
393 +
394 + if (search.argmode == ARG_NAME) {
395 + for (c = 0; c < argc; c++) {
396 + if (strchr(argv[c], '/') == NULL)
397 + continue;
398 + if (access(argv[c], R_OK) == -1) {
399 + warn("%s", argv[c]);
400 + continue;
401 + }
402 + res = mandoc_reallocarray(res,
403 + sz + 1, sizeof(*res));
404 + res[sz].file = mandoc_strdup(argv[c]);
405 + res[sz].names = NULL;
406 + res[sz].output = NULL;
407 + res[sz].ipath = SIZE_MAX;
408 + res[sz].bits = 0;
409 + res[sz].sec = 10;
410 + res[sz].form = FORM_SRC;
411 + sz++;
412 + }
383 413 }
384 414
385 415 if (sz == 0) {
416 + if (search.argmode != ARG_NAME)
417 + warnx("nothing appropriate");
386 418 rc = MANDOCLEVEL_BADARG;
387 419 goto out;
388 420 }
389 421
390 422 /*
391 423 * For standard man(1) and -a output mode,
392 424 * prepare for copying filename pointers
393 425 * into the program parameter array.
394 426 */
395 427
396 428 if (outmode == OUTMODE_ONE) {
397 429 argc = 1;
398 430 best_prio = 20;
399 431 } else if (outmode == OUTMODE_ALL)
400 432 argc = (int)sz;
401 433
402 434 /* Iterate all matching manuals. */
403 435
404 436 resp = res;
405 437 for (i = 0; i < sz; i++) {
406 438 if (outmode == OUTMODE_FLN)
407 439 puts(res[i].file);
408 440 else if (outmode == OUTMODE_LST)
409 441 printf("%s - %s\n", res[i].names,
410 442 res[i].output == NULL ? "" :
411 443 res[i].output);
412 444 else if (outmode == OUTMODE_ONE) {
413 445 /* Search for the best section. */
414 446 sec = res[i].file;
415 447 sec += strcspn(sec, "123456789");
416 448 if (sec[0] == '\0')
417 449 continue;
418 450 prio = sec_prios[sec[0] - '1'];
419 451 if (sec[1] != '/')
420 452 prio += 10;
421 453 if (prio >= best_prio)
422 454 continue;
423 455 best_prio = prio;
424 456 resp = res + i;
425 457 }
426 458 }
427 459
428 460 /*
429 461 * For man(1), -a and -i output mode, fall through
430 462 * to the main mandoc(1) code iterating files
431 463 * and running the parsers on each of them.
432 464 */
433 465
434 466 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
435 467 goto out;
436 468 }
437 469
438 470 /* mandoc(1) */
439 471
440 472 #if HAVE_PLEDGE
441 473 if (use_pager) {
442 474 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
443 475 err((int)MANDOCLEVEL_SYSERR, "pledge");
444 476 } else {
445 477 if (pledge("stdio rpath", NULL) == -1)
446 478 err((int)MANDOCLEVEL_SYSERR, "pledge");
447 479 }
448 480 #endif
449 481
450 482 if (search.argmode == ARG_FILE)
451 483 moptions(&options, auxpaths);
452 484
453 485 mchars_alloc();
454 486 curp.mp = mparse_alloc(options, curp.mmin, mmsg,
455 487 curp.os_e, curp.os_s);
456 488
457 489 /*
458 490 * Conditionally start up the lookaside buffer before parsing.
↓ open down ↓ |
63 lines elided |
↑ open up ↑ |
459 491 */
460 492 if (OUTT_MAN == curp.outtype)
461 493 mparse_keep(curp.mp);
462 494
463 495 if (argc < 1) {
464 496 if (use_pager)
465 497 tag_files = tag_init();
466 498 parse(&curp, STDIN_FILENO, "<stdin>");
467 499 }
468 500
501 + /*
502 + * Remember the original working directory, if possible.
503 + * This will be needed if some names on the command line
504 + * are page names and some are relative file names.
505 + * Do not error out if the current directory is not
506 + * readable: Maybe it won't be needed after all.
507 + */
508 + startdir = open(".", O_RDONLY | O_DIRECTORY);
509 +
469 510 while (argc > 0) {
511 +
512 + /*
513 + * Changing directories is not needed in ARG_FILE mode.
514 + * Do it on a best-effort basis. Even in case of
515 + * failure, some functionality may still work.
516 + */
517 + if (resp != NULL) {
518 + if (resp->ipath != SIZE_MAX)
519 + (void)chdir(conf.manpath.paths[resp->ipath]);
520 + else if (startdir != -1)
521 + (void)fchdir(startdir);
522 + }
523 +
470 524 fd = mparse_open(curp.mp, resp != NULL ? resp->file : *argv);
471 525 if (fd != -1) {
472 526 if (use_pager) {
473 527 tag_files = tag_init();
474 528 use_pager = 0;
475 529 }
476 530
477 531 if (resp == NULL)
478 532 parse(&curp, fd, *argv);
479 - else if (resp->form == FORM_SRC) {
480 - /* For .so only; ignore failure. */
481 - (void)chdir(conf.manpath.paths[resp->ipath]);
533 + else if (resp->form == FORM_SRC)
482 534 parse(&curp, fd, resp->file);
483 - } else
535 + else
484 536 passthrough(resp->file, fd,
485 537 conf.output.synopsisonly);
486 538
539 + if (ferror(stdout)) {
540 + if (tag_files != NULL) {
541 + warn("%s", tag_files->ofn);
542 + tag_unlink();
543 + tag_files = NULL;
544 + } else
545 + warn("stdout");
546 + rc = MANDOCLEVEL_SYSERR;
547 + break;
548 + }
549 +
487 550 if (argc > 1 && curp.outtype <= OUTT_UTF8) {
488 551 if (curp.outdata == NULL)
489 552 outdata_alloc(&curp);
490 553 terminal_sepline(curp.outdata);
491 554 }
492 555 } else if (rc < MANDOCLEVEL_ERROR)
493 556 rc = MANDOCLEVEL_ERROR;
494 557
495 558 if (MANDOCLEVEL_OK != rc && curp.wstop)
496 559 break;
497 560
498 561 if (resp != NULL)
499 562 resp++;
500 563 else
501 564 argv++;
502 565 if (--argc)
503 566 mparse_reset(curp.mp);
504 567 }
568 + if (startdir != -1) {
569 + (void)fchdir(startdir);
570 + close(startdir);
571 + }
505 572
506 573 if (curp.outdata != NULL) {
507 574 switch (curp.outtype) {
508 575 case OUTT_HTML:
509 576 html_free(curp.outdata);
510 577 break;
511 578 case OUTT_UTF8:
512 579 case OUTT_LOCALE:
513 580 case OUTT_ASCII:
514 581 ascii_free(curp.outdata);
515 582 break;
516 583 case OUTT_PDF:
517 584 case OUTT_PS:
518 585 pspdf_free(curp.outdata);
519 586 break;
520 587 default:
521 588 break;
522 589 }
523 590 }
524 591 mandoc_xr_free();
525 592 mparse_free(curp.mp);
526 593 mchars_free();
527 594
528 595 out:
529 596 if (search.argmode != ARG_FILE) {
530 597 manconf_free(&conf);
531 598 mansearch_free(res, sz);
532 599 }
533 600
534 601 free(curp.os_s);
535 602
536 603 /*
537 604 * When using a pager, finish writing both temporary files,
538 605 * fork it, wait for the user to close it, and clean up.
539 606 */
540 607
541 608 if (tag_files != NULL) {
542 609 fclose(stdout);
543 610 tag_write();
544 611 man_pgid = getpgid(0);
545 612 tag_files->tcpgid = man_pgid == getpid() ?
546 613 getpgid(getppid()) : man_pgid;
547 614 pager_pid = 0;
548 615 signum = SIGSTOP;
549 616 for (;;) {
550 617
551 618 /* Stop here until moved to the foreground. */
552 619
553 620 tc_pgid = tcgetpgrp(tag_files->ofd);
554 621 if (tc_pgid != man_pgid) {
555 622 if (tc_pgid == pager_pid) {
556 623 (void)tcsetpgrp(tag_files->ofd,
557 624 man_pgid);
558 625 if (signum == SIGTTIN)
559 626 continue;
560 627 } else
561 628 tag_files->tcpgid = tc_pgid;
562 629 kill(0, signum);
563 630 continue;
564 631 }
565 632
566 633 /* Once in the foreground, activate the pager. */
567 634
568 635 if (pager_pid) {
569 636 (void)tcsetpgrp(tag_files->ofd, pager_pid);
570 637 kill(pager_pid, SIGCONT);
571 638 } else
572 639 pager_pid = spawn_pager(tag_files);
573 640
574 641 /* Wait for the pager to stop or exit. */
575 642
576 643 while ((pid = waitpid(pager_pid, &status,
577 644 WUNTRACED)) == -1 && errno == EINTR)
578 645 continue;
579 646
580 647 if (pid == -1) {
581 648 warn("wait");
582 649 rc = MANDOCLEVEL_SYSERR;
583 650 break;
584 651 }
585 652 if (!WIFSTOPPED(status))
586 653 break;
587 654
588 655 signum = WSTOPSIG(status);
589 656 }
590 657 tag_unlink();
591 658 }
592 659
593 660 return (int)rc;
594 661 }
595 662
596 663 static void
597 664 usage(enum argmode argmode)
598 665 {
599 666
600 667 switch (argmode) {
601 668 case ARG_FILE:
602 669 fputs("usage: mandoc [-ac] [-I os=name] "
603 670 "[-K encoding] [-mdoc | -man] [-O options]\n"
604 671 "\t [-T output] [-W level] [file ...]\n", stderr);
605 672 break;
606 673 case ARG_NAME:
607 674 fputs("usage: man [-acfhklw] [-C file] [-M path] "
608 675 "[-m path] [-S subsection]\n"
609 676 "\t [[-s] section] name ...\n", stderr);
610 677 break;
611 678 case ARG_WORD:
612 679 fputs("usage: whatis [-afk] [-C file] "
613 680 "[-M path] [-m path] [-O outkey] [-S arch]\n"
614 681 "\t [-s section] name ...\n", stderr);
615 682 break;
616 683 case ARG_EXPR:
617 684 fputs("usage: apropos [-afk] [-C file] "
618 685 "[-M path] [-m path] [-O outkey] [-S arch]\n"
619 686 "\t [-s section] expression ...\n", stderr);
620 687 break;
621 688 }
622 689 exit((int)MANDOCLEVEL_BADARG);
623 690 }
624 691
625 692 static int
626 693 fs_lookup(const struct manpaths *paths, size_t ipath,
627 694 const char *sec, const char *arch, const char *name,
628 695 struct manpage **res, size_t *ressz)
629 696 {
630 697 glob_t globinfo;
631 698 struct manpage *page;
632 699 char *file;
633 700 int globres;
634 701 enum form form;
635 702
636 703 form = FORM_SRC;
637 704 mandoc_asprintf(&file, "%s/man%s/%s.%s",
638 705 paths->paths[ipath], sec, name, sec);
639 706 if (access(file, R_OK) != -1)
640 707 goto found;
641 708 free(file);
642 709
643 710 mandoc_asprintf(&file, "%s/cat%s/%s.0",
644 711 paths->paths[ipath], sec, name);
645 712 if (access(file, R_OK) != -1) {
646 713 form = FORM_CAT;
647 714 goto found;
648 715 }
649 716 free(file);
650 717
651 718 if (arch != NULL) {
652 719 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
653 720 paths->paths[ipath], sec, arch, name, sec);
654 721 if (access(file, R_OK) != -1)
655 722 goto found;
656 723 free(file);
657 724 }
658 725
659 726 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
660 727 paths->paths[ipath], sec, name);
661 728 globres = glob(file, 0, NULL, &globinfo);
662 729 if (globres != 0 && globres != GLOB_NOMATCH)
663 730 warn("%s: glob", file);
664 731 free(file);
665 732 if (globres == 0)
666 733 file = mandoc_strdup(*globinfo.gl_pathv);
667 734 globfree(&globinfo);
668 735 if (globres == 0)
669 736 goto found;
670 737 if (res != NULL || ipath + 1 != paths->sz)
671 738 return 0;
672 739
673 740 mandoc_asprintf(&file, "%s.%s", name, sec);
674 741 globres = access(file, R_OK);
675 742 free(file);
676 743 return globres != -1;
677 744
678 745 found:
679 746 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
680 747 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
681 748 if (res == NULL) {
682 749 free(file);
683 750 return 1;
684 751 }
685 752 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
686 753 page = *res + (*ressz - 1);
687 754 page->file = file;
688 755 page->names = NULL;
689 756 page->output = NULL;
690 757 page->ipath = ipath;
691 758 page->bits = NAME_FILE & NAME_MASK;
692 759 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
693 760 page->form = form;
694 761 return 1;
695 762 }
696 763
697 764 static int
698 765 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
699 766 int argc, char **argv, struct manpage **res, size_t *ressz)
700 767 {
701 768 const char *const sections[] =
702 769 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
703 770 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
704 771
705 772 size_t ipath, isec, lastsz;
706 773
707 774 assert(cfg->argmode == ARG_NAME);
708 775
709 776 if (res != NULL)
710 777 *res = NULL;
711 778 *ressz = lastsz = 0;
712 779 while (argc) {
713 780 for (ipath = 0; ipath < paths->sz; ipath++) {
714 781 if (cfg->sec != NULL) {
↓ open down ↓ |
200 lines elided |
↑ open up ↑ |
715 782 if (fs_lookup(paths, ipath, cfg->sec,
716 783 cfg->arch, *argv, res, ressz) &&
717 784 cfg->firstmatch)
718 785 return 1;
719 786 } else for (isec = 0; isec < nsec; isec++)
720 787 if (fs_lookup(paths, ipath, sections[isec],
721 788 cfg->arch, *argv, res, ressz) &&
722 789 cfg->firstmatch)
723 790 return 1;
724 791 }
725 - if (res != NULL && *ressz == lastsz)
792 + if (res != NULL && *ressz == lastsz &&
793 + strchr(*argv, '/') == NULL)
726 794 warnx("No entry for %s in the manual.", *argv);
727 795 lastsz = *ressz;
728 796 argv++;
729 797 argc--;
730 798 }
731 799 return 0;
732 800 }
733 801
734 802 static void
735 803 parse(struct curparse *curp, int fd, const char *file)
736 804 {
737 805 enum mandoclevel rctmp;
738 806 struct roff_man *man;
739 807
740 808 /* Begin by parsing the file itself. */
741 809
742 810 assert(file);
743 811 assert(fd >= 0);
744 812
745 813 rctmp = mparse_readfd(curp->mp, fd, file);
746 814 if (fd != STDIN_FILENO)
747 815 close(fd);
748 816 if (rc < rctmp)
749 817 rc = rctmp;
750 818
751 819 /*
752 820 * With -Wstop and warnings or errors of at least the requested
753 821 * level, do not produce output.
754 822 */
755 823
756 824 if (rctmp != MANDOCLEVEL_OK && curp->wstop)
757 825 return;
758 826
759 827 if (curp->outdata == NULL)
760 828 outdata_alloc(curp);
761 829
762 830 mparse_result(curp->mp, &man, NULL);
763 831
764 832 /* Execute the out device, if it exists. */
765 833
766 834 if (man == NULL)
767 835 return;
768 836 mandoc_xr_reset();
769 837 if (man->macroset == MACROSET_MDOC) {
770 838 if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
771 839 mdoc_validate(man);
772 840 switch (curp->outtype) {
773 841 case OUTT_HTML:
774 842 html_mdoc(curp->outdata, man);
775 843 break;
776 844 case OUTT_TREE:
777 845 tree_mdoc(curp->outdata, man);
778 846 break;
779 847 case OUTT_MAN:
780 848 man_mdoc(curp->outdata, man);
781 849 break;
782 850 case OUTT_PDF:
783 851 case OUTT_ASCII:
784 852 case OUTT_UTF8:
785 853 case OUTT_LOCALE:
786 854 case OUTT_PS:
787 855 terminal_mdoc(curp->outdata, man);
788 856 break;
789 857 case OUTT_MARKDOWN:
790 858 markdown_mdoc(curp->outdata, man);
791 859 break;
792 860 default:
793 861 break;
794 862 }
795 863 }
796 864 if (man->macroset == MACROSET_MAN) {
797 865 if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
798 866 man_validate(man);
799 867 switch (curp->outtype) {
800 868 case OUTT_HTML:
801 869 html_man(curp->outdata, man);
802 870 break;
803 871 case OUTT_TREE:
804 872 tree_man(curp->outdata, man);
805 873 break;
806 874 case OUTT_MAN:
807 875 man_man(curp->outdata, man);
808 876 break;
809 877 case OUTT_PDF:
810 878 case OUTT_ASCII:
811 879 case OUTT_UTF8:
812 880 case OUTT_LOCALE:
813 881 case OUTT_PS:
814 882 terminal_man(curp->outdata, man);
815 883 break;
816 884 default:
817 885 break;
818 886 }
819 887 }
820 888 if (curp->mmin < MANDOCERR_STYLE)
821 889 check_xr(file);
822 890 mparse_updaterc(curp->mp, &rc);
823 891 }
824 892
825 893 static void
826 894 check_xr(const char *file)
827 895 {
828 896 static struct manpaths paths;
829 897 struct mansearch search;
830 898 struct mandoc_xr *xr;
831 899 char *cp;
832 900 size_t sz;
833 901
834 902 if (paths.sz == 0)
835 903 manpath_base(&paths);
836 904
837 905 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
838 906 if (xr->line == -1)
839 907 continue;
840 908 search.arch = NULL;
841 909 search.sec = xr->sec;
842 910 search.outkey = NULL;
843 911 search.argmode = ARG_NAME;
844 912 search.firstmatch = 1;
845 913 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
846 914 continue;
847 915 if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz))
848 916 continue;
849 917 if (xr->count == 1)
850 918 mandoc_asprintf(&cp, "Xr %s %s", xr->name, xr->sec);
851 919 else
852 920 mandoc_asprintf(&cp, "Xr %s %s (%d times)",
853 921 xr->name, xr->sec, xr->count);
854 922 mmsg(MANDOCERR_XR_BAD, MANDOCLEVEL_STYLE,
855 923 file, xr->line, xr->pos + 1, cp);
856 924 free(cp);
857 925 }
858 926 }
859 927
860 928 static void
861 929 outdata_alloc(struct curparse *curp)
862 930 {
863 931 switch (curp->outtype) {
864 932 case OUTT_HTML:
865 933 curp->outdata = html_alloc(curp->outopts);
866 934 break;
867 935 case OUTT_UTF8:
868 936 curp->outdata = utf8_alloc(curp->outopts);
869 937 break;
870 938 case OUTT_LOCALE:
871 939 curp->outdata = locale_alloc(curp->outopts);
872 940 break;
873 941 case OUTT_ASCII:
874 942 curp->outdata = ascii_alloc(curp->outopts);
875 943 break;
876 944 case OUTT_PDF:
877 945 curp->outdata = pdf_alloc(curp->outopts);
878 946 break;
879 947 case OUTT_PS:
880 948 curp->outdata = ps_alloc(curp->outopts);
881 949 break;
882 950 default:
883 951 break;
884 952 }
885 953 }
886 954
887 955 static void
888 956 passthrough(const char *file, int fd, int synopsis_only)
889 957 {
890 958 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
891 959 const char synr[] = "SYNOPSIS";
892 960
893 961 FILE *stream;
894 962 const char *syscall;
895 963 char *line, *cp;
896 964 size_t linesz;
897 965 ssize_t len, written;
898 966 int print;
899 967
900 968 line = NULL;
901 969 linesz = 0;
902 970
903 971 if (fflush(stdout) == EOF) {
904 972 syscall = "fflush";
905 973 goto fail;
906 974 }
907 975
908 976 if ((stream = fdopen(fd, "r")) == NULL) {
909 977 close(fd);
910 978 syscall = "fdopen";
911 979 goto fail;
912 980 }
913 981
914 982 print = 0;
915 983 while ((len = getline(&line, &linesz, stream)) != -1) {
916 984 cp = line;
917 985 if (synopsis_only) {
918 986 if (print) {
919 987 if ( ! isspace((unsigned char)*cp))
920 988 goto done;
921 989 while (isspace((unsigned char)*cp)) {
922 990 cp++;
923 991 len--;
924 992 }
925 993 } else {
926 994 if (strcmp(cp, synb) == 0 ||
927 995 strcmp(cp, synr) == 0)
928 996 print = 1;
929 997 continue;
930 998 }
931 999 }
932 1000 for (; len > 0; len -= written) {
933 1001 if ((written = write(STDOUT_FILENO, cp, len)) != -1)
934 1002 continue;
935 1003 fclose(stream);
936 1004 syscall = "write";
937 1005 goto fail;
938 1006 }
939 1007 }
940 1008
941 1009 if (ferror(stream)) {
942 1010 fclose(stream);
943 1011 syscall = "getline";
944 1012 goto fail;
945 1013 }
946 1014
947 1015 done:
948 1016 free(line);
949 1017 fclose(stream);
950 1018 return;
951 1019
952 1020 fail:
953 1021 free(line);
954 1022 warn("%s: SYSERR: %s", file, syscall);
955 1023 if (rc < MANDOCLEVEL_SYSERR)
956 1024 rc = MANDOCLEVEL_SYSERR;
957 1025 }
958 1026
959 1027 static int
960 1028 koptions(int *options, char *arg)
961 1029 {
962 1030
963 1031 if ( ! strcmp(arg, "utf-8")) {
964 1032 *options |= MPARSE_UTF8;
965 1033 *options &= ~MPARSE_LATIN1;
966 1034 } else if ( ! strcmp(arg, "iso-8859-1")) {
967 1035 *options |= MPARSE_LATIN1;
968 1036 *options &= ~MPARSE_UTF8;
969 1037 } else if ( ! strcmp(arg, "us-ascii")) {
970 1038 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
971 1039 } else {
972 1040 warnx("-K %s: Bad argument", arg);
973 1041 return 0;
974 1042 }
975 1043 return 1;
976 1044 }
977 1045
978 1046 static void
979 1047 moptions(int *options, char *arg)
980 1048 {
981 1049
982 1050 if (arg == NULL)
983 1051 return;
984 1052 if (strcmp(arg, "doc") == 0)
985 1053 *options |= MPARSE_MDOC;
986 1054 else if (strcmp(arg, "an") == 0)
987 1055 *options |= MPARSE_MAN;
988 1056 }
989 1057
990 1058 static int
991 1059 toptions(struct curparse *curp, char *arg)
992 1060 {
993 1061
994 1062 if (0 == strcmp(arg, "ascii"))
995 1063 curp->outtype = OUTT_ASCII;
996 1064 else if (0 == strcmp(arg, "lint")) {
997 1065 curp->outtype = OUTT_LINT;
998 1066 curp->mmin = MANDOCERR_BASE;
999 1067 mmsg_stream = stdout;
1000 1068 } else if (0 == strcmp(arg, "tree"))
1001 1069 curp->outtype = OUTT_TREE;
1002 1070 else if (0 == strcmp(arg, "man"))
1003 1071 curp->outtype = OUTT_MAN;
1004 1072 else if (0 == strcmp(arg, "html"))
1005 1073 curp->outtype = OUTT_HTML;
1006 1074 else if (0 == strcmp(arg, "markdown"))
1007 1075 curp->outtype = OUTT_MARKDOWN;
1008 1076 else if (0 == strcmp(arg, "utf8"))
1009 1077 curp->outtype = OUTT_UTF8;
1010 1078 else if (0 == strcmp(arg, "locale"))
1011 1079 curp->outtype = OUTT_LOCALE;
1012 1080 else if (0 == strcmp(arg, "ps"))
1013 1081 curp->outtype = OUTT_PS;
1014 1082 else if (0 == strcmp(arg, "pdf"))
1015 1083 curp->outtype = OUTT_PDF;
1016 1084 else {
1017 1085 warnx("-T %s: Bad argument", arg);
1018 1086 return 0;
1019 1087 }
1020 1088
1021 1089 return 1;
1022 1090 }
1023 1091
1024 1092 static int
1025 1093 woptions(struct curparse *curp, char *arg)
1026 1094 {
1027 1095 char *v, *o;
1028 1096 const char *toks[11];
1029 1097
1030 1098 toks[0] = "stop";
1031 1099 toks[1] = "all";
1032 1100 toks[2] = "base";
1033 1101 toks[3] = "style";
1034 1102 toks[4] = "warning";
1035 1103 toks[5] = "error";
1036 1104 toks[6] = "unsupp";
1037 1105 toks[7] = "fatal";
1038 1106 toks[8] = "openbsd";
1039 1107 toks[9] = "netbsd";
1040 1108 toks[10] = NULL;
1041 1109
1042 1110 while (*arg) {
1043 1111 o = arg;
1044 1112 switch (getsubopt(&arg, (char * const *)toks, &v)) {
1045 1113 case 0:
1046 1114 curp->wstop = 1;
1047 1115 break;
1048 1116 case 1:
1049 1117 case 2:
1050 1118 curp->mmin = MANDOCERR_BASE;
1051 1119 break;
1052 1120 case 3:
1053 1121 curp->mmin = MANDOCERR_STYLE;
1054 1122 break;
1055 1123 case 4:
1056 1124 curp->mmin = MANDOCERR_WARNING;
1057 1125 break;
1058 1126 case 5:
1059 1127 curp->mmin = MANDOCERR_ERROR;
1060 1128 break;
1061 1129 case 6:
1062 1130 curp->mmin = MANDOCERR_UNSUPP;
1063 1131 break;
1064 1132 case 7:
1065 1133 curp->mmin = MANDOCERR_MAX;
1066 1134 break;
1067 1135 case 8:
1068 1136 curp->mmin = MANDOCERR_BASE;
1069 1137 curp->os_e = MANDOC_OS_OPENBSD;
1070 1138 break;
1071 1139 case 9:
1072 1140 curp->mmin = MANDOCERR_BASE;
1073 1141 curp->os_e = MANDOC_OS_NETBSD;
1074 1142 break;
1075 1143 default:
1076 1144 warnx("-W %s: Bad argument", o);
1077 1145 return 0;
1078 1146 }
1079 1147 }
1080 1148 return 1;
1081 1149 }
1082 1150
1083 1151 static void
1084 1152 mmsg(enum mandocerr t, enum mandoclevel lvl,
1085 1153 const char *file, int line, int col, const char *msg)
1086 1154 {
1087 1155 const char *mparse_msg;
1088 1156
1089 1157 fprintf(mmsg_stream, "%s: %s:", getprogname(),
1090 1158 file == NULL ? "<stdin>" : file);
1091 1159
1092 1160 if (line)
1093 1161 fprintf(mmsg_stream, "%d:%d:", line, col + 1);
1094 1162
1095 1163 fprintf(mmsg_stream, " %s", mparse_strlevel(lvl));
1096 1164
1097 1165 if ((mparse_msg = mparse_strerror(t)) != NULL)
1098 1166 fprintf(mmsg_stream, ": %s", mparse_msg);
1099 1167
1100 1168 if (msg)
1101 1169 fprintf(mmsg_stream, ": %s", msg);
1102 1170
1103 1171 fputc('\n', mmsg_stream);
1104 1172 }
1105 1173
1106 1174 static pid_t
1107 1175 spawn_pager(struct tag_files *tag_files)
1108 1176 {
1109 1177 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1110 1178 #define MAX_PAGER_ARGS 16
1111 1179 char *argv[MAX_PAGER_ARGS];
1112 1180 const char *pager;
1113 1181 char *cp;
1114 1182 size_t cmdlen;
1115 1183 int argc;
1116 1184 pid_t pager_pid;
1117 1185
1118 1186 pager = getenv("MANPAGER");
1119 1187 if (pager == NULL || *pager == '\0')
1120 1188 pager = getenv("PAGER");
1121 1189 if (pager == NULL || *pager == '\0')
1122 1190 pager = "more -s";
1123 1191 cp = mandoc_strdup(pager);
1124 1192
1125 1193 /*
1126 1194 * Parse the pager command into words.
1127 1195 * Intentionally do not do anything fancy here.
1128 1196 */
1129 1197
1130 1198 argc = 0;
1131 1199 while (argc + 4 < MAX_PAGER_ARGS) {
1132 1200 argv[argc++] = cp;
1133 1201 cp = strchr(cp, ' ');
1134 1202 if (cp == NULL)
1135 1203 break;
1136 1204 *cp++ = '\0';
1137 1205 while (*cp == ' ')
1138 1206 cp++;
1139 1207 if (*cp == '\0')
1140 1208 break;
1141 1209 }
1142 1210
1143 1211 /* For less(1), use the tag file. */
1144 1212
1145 1213 if ((cmdlen = strlen(argv[0])) >= 4) {
1146 1214 cp = argv[0] + cmdlen - 4;
1147 1215 if (strcmp(cp, "less") == 0) {
1148 1216 argv[argc++] = mandoc_strdup("-T");
1149 1217 argv[argc++] = tag_files->tfn;
1150 1218 }
1151 1219 }
1152 1220 argv[argc++] = tag_files->ofn;
1153 1221 argv[argc] = NULL;
1154 1222
1155 1223 switch (pager_pid = fork()) {
1156 1224 case -1:
1157 1225 err((int)MANDOCLEVEL_SYSERR, "fork");
1158 1226 case 0:
1159 1227 break;
1160 1228 default:
1161 1229 (void)setpgid(pager_pid, 0);
1162 1230 (void)tcsetpgrp(tag_files->ofd, pager_pid);
1163 1231 #if HAVE_PLEDGE
1164 1232 if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1165 1233 err((int)MANDOCLEVEL_SYSERR, "pledge");
↓ open down ↓ |
430 lines elided |
↑ open up ↑ |
1166 1234 #endif
1167 1235 tag_files->pager_pid = pager_pid;
1168 1236 return pager_pid;
1169 1237 }
1170 1238
1171 1239 /* The child process becomes the pager. */
1172 1240
1173 1241 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1174 1242 err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1175 1243 close(tag_files->ofd);
1176 - close(tag_files->tfd);
1244 + assert(tag_files->tfd == -1);
1177 1245
1178 1246 /* Do not start the pager before controlling the terminal. */
1179 1247
1180 1248 while (tcgetpgrp(STDOUT_FILENO) != getpid())
1181 1249 nanosleep(&timeout, NULL);
1182 1250
1183 1251 execvp(argv[0], argv);
1184 1252 err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);
1185 1253 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX