Print this page
9718 update mandoc to 1.14.4
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mandoc/mandocdb.c
+++ new/usr/src/cmd/mandoc/mandocdb.c
1 -/* $Id: mandocdb.c,v 1.253 2017/07/28 14:48:25 schwarze Exp $ */
1 +/* $Id: mandocdb.c,v 1.258 2018/02/23 18:25:57 schwarze Exp $ */
2 2 /*
3 3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 4 * Copyright (c) 2011-2017 Ingo Schwarze <schwarze@openbsd.org>
5 5 * Copyright (c) 2016 Ed Maste <emaste@freebsd.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/mman.h>
22 23 #include <sys/stat.h>
23 -#include <sys/wait.h>
24 24
25 25 #include <assert.h>
26 26 #include <ctype.h>
27 27 #if HAVE_ERR
28 28 #include <err.h>
29 29 #endif
30 30 #include <errno.h>
31 31 #include <fcntl.h>
32 32 #if HAVE_FTS
33 33 #include <fts.h>
34 34 #else
35 35 #include "compat_fts.h"
36 36 #endif
37 37 #include <limits.h>
38 38 #if HAVE_SANDBOX_INIT
39 39 #include <sandbox.h>
40 40 #endif
41 41 #include <stdarg.h>
42 42 #include <stddef.h>
43 43 #include <stdio.h>
44 44 #include <stdint.h>
45 45 #include <stdlib.h>
46 46 #include <string.h>
47 47 #include <unistd.h>
48 48
49 49 #include "mandoc_aux.h"
50 50 #include "mandoc_ohash.h"
51 51 #include "mandoc.h"
52 52 #include "roff.h"
53 53 #include "mdoc.h"
54 54 #include "man.h"
55 55 #include "manconf.h"
56 56 #include "mansearch.h"
57 57 #include "dba_array.h"
58 58 #include "dba.h"
59 59
60 60 extern const char *const mansearch_keynames[];
61 61
62 62 enum op {
63 63 OP_DEFAULT = 0, /* new dbs from dir list or default config */
64 64 OP_CONFFILE, /* new databases from custom config file */
65 65 OP_UPDATE, /* delete/add entries in existing database */
66 66 OP_DELETE, /* delete entries from existing database */
67 67 OP_TEST /* change no databases, report potential problems */
68 68 };
69 69
70 70 struct str {
71 71 const struct mpage *mpage; /* if set, the owning parse */
72 72 uint64_t mask; /* bitmask in sequence */
73 73 char key[]; /* rendered text */
74 74 };
75 75
76 76 struct inodev {
77 77 ino_t st_ino;
78 78 dev_t st_dev;
79 79 };
80 80
81 81 struct mpage {
82 82 struct inodev inodev; /* used for hashing routine */
83 83 struct dba_array *dba;
84 84 char *sec; /* section from file content */
85 85 char *arch; /* architecture from file content */
86 86 char *title; /* title from file content */
87 87 char *desc; /* description from file content */
88 88 struct mpage *next; /* singly linked list */
89 89 struct mlink *mlinks; /* singly linked list */
90 90 int name_head_done;
91 91 enum form form; /* format from file content */
92 92 };
93 93
94 94 struct mlink {
95 95 char file[PATH_MAX]; /* filename rel. to manpath */
96 96 char *dsec; /* section from directory */
97 97 char *arch; /* architecture from directory */
98 98 char *name; /* name from file name (not empty) */
99 99 char *fsec; /* section from file name suffix */
100 100 struct mlink *next; /* singly linked list */
101 101 struct mpage *mpage; /* parent */
102 102 int gzip; /* filename has a .gz suffix */
103 103 enum form dform; /* format from directory */
104 104 enum form fform; /* format from file name suffix */
105 105 };
106 106
107 107 typedef int (*mdoc_fp)(struct mpage *, const struct roff_meta *,
108 108 const struct roff_node *);
109 109
110 110 struct mdoc_handler {
111 111 mdoc_fp fp; /* optional handler */
112 112 uint64_t mask; /* set unless handler returns 0 */
113 113 int taboo; /* node flags that must not be set */
114 114 };
115 115
116 116
117 117 int mandocdb(int, char *[]);
118 118
119 119 static void dbadd(struct dba *, struct mpage *);
120 120 static void dbadd_mlink(const struct mlink *mlink);
121 121 static void dbprune(struct dba *);
122 122 static void dbwrite(struct dba *);
123 123 static void filescan(const char *);
124 124 #if HAVE_FTS_COMPARE_CONST
125 125 static int fts_compare(const FTSENT *const *, const FTSENT *const *);
126 126 #else
127 127 static int fts_compare(const FTSENT **, const FTSENT **);
128 128 #endif
129 129 static void mlink_add(struct mlink *, const struct stat *);
130 130 static void mlink_check(struct mpage *, struct mlink *);
131 131 static void mlink_free(struct mlink *);
↓ open down ↓ |
98 lines elided |
↑ open up ↑ |
132 132 static void mlinks_undupe(struct mpage *);
133 133 static void mpages_free(void);
134 134 static void mpages_merge(struct dba *, struct mparse *);
135 135 static void parse_cat(struct mpage *, int);
136 136 static void parse_man(struct mpage *, const struct roff_meta *,
137 137 const struct roff_node *);
138 138 static void parse_mdoc(struct mpage *, const struct roff_meta *,
139 139 const struct roff_node *);
140 140 static int parse_mdoc_head(struct mpage *, const struct roff_meta *,
141 141 const struct roff_node *);
142 +static int parse_mdoc_Fa(struct mpage *, const struct roff_meta *,
143 + const struct roff_node *);
142 144 static int parse_mdoc_Fd(struct mpage *, const struct roff_meta *,
143 145 const struct roff_node *);
144 146 static void parse_mdoc_fname(struct mpage *, const struct roff_node *);
145 147 static int parse_mdoc_Fn(struct mpage *, const struct roff_meta *,
146 148 const struct roff_node *);
147 149 static int parse_mdoc_Fo(struct mpage *, const struct roff_meta *,
148 150 const struct roff_node *);
149 151 static int parse_mdoc_Nd(struct mpage *, const struct roff_meta *,
150 152 const struct roff_node *);
151 153 static int parse_mdoc_Nm(struct mpage *, const struct roff_meta *,
152 154 const struct roff_node *);
153 155 static int parse_mdoc_Sh(struct mpage *, const struct roff_meta *,
154 156 const struct roff_node *);
155 157 static int parse_mdoc_Va(struct mpage *, const struct roff_meta *,
156 158 const struct roff_node *);
157 159 static int parse_mdoc_Xr(struct mpage *, const struct roff_meta *,
158 160 const struct roff_node *);
159 161 static void putkey(const struct mpage *, char *, uint64_t);
160 162 static void putkeys(const struct mpage *, char *, size_t, uint64_t);
161 163 static void putmdockey(const struct mpage *,
162 164 const struct roff_node *, uint64_t, int);
163 165 static int render_string(char **, size_t *);
164 166 static void say(const char *, const char *, ...)
165 167 __attribute__((__format__ (__printf__, 2, 3)));
166 168 static int set_basedir(const char *, int);
167 169 static int treescan(void);
168 170 static size_t utf8(unsigned int, char [7]);
169 171
170 172 static int nodb; /* no database changes */
171 173 static int mparse_options; /* abort the parse early */
172 174 static int use_all; /* use all found files */
173 175 static int debug; /* print what we're doing */
174 176 static int warnings; /* warn about crap */
175 177 static int write_utf8; /* write UTF-8 output; else ASCII */
176 178 static int exitcode; /* to be returned by main */
177 179 static enum op op; /* operational mode */
178 180 static char basedir[PATH_MAX]; /* current base directory */
179 181 static struct mpage *mpage_head; /* list of distinct manual pages */
180 182 static struct ohash mpages; /* table of distinct manual pages */
181 183 static struct ohash mlinks; /* table of directory entries */
182 184 static struct ohash names; /* table of all names */
183 185 static struct ohash strings; /* table of all strings */
184 186 static uint64_t name_mask;
185 187
186 188 static const struct mdoc_handler __mdocs[MDOC_MAX - MDOC_Dd] = {
187 189 { NULL, 0, NODE_NOPRT }, /* Dd */
188 190 { NULL, 0, NODE_NOPRT }, /* Dt */
189 191 { NULL, 0, NODE_NOPRT }, /* Os */
190 192 { parse_mdoc_Sh, TYPE_Sh, 0 }, /* Sh */
191 193 { parse_mdoc_head, TYPE_Ss, 0 }, /* Ss */
192 194 { NULL, 0, 0 }, /* Pp */
193 195 { NULL, 0, 0 }, /* D1 */
194 196 { NULL, 0, 0 }, /* Dl */
195 197 { NULL, 0, 0 }, /* Bd */
196 198 { NULL, 0, 0 }, /* Ed */
197 199 { NULL, 0, 0 }, /* Bl */
198 200 { NULL, 0, 0 }, /* El */
199 201 { NULL, 0, 0 }, /* It */
↓ open down ↓ |
48 lines elided |
↑ open up ↑ |
200 202 { NULL, 0, 0 }, /* Ad */
201 203 { NULL, TYPE_An, 0 }, /* An */
202 204 { NULL, 0, 0 }, /* Ap */
203 205 { NULL, TYPE_Ar, 0 }, /* Ar */
204 206 { NULL, TYPE_Cd, 0 }, /* Cd */
205 207 { NULL, TYPE_Cm, 0 }, /* Cm */
206 208 { NULL, TYPE_Dv, 0 }, /* Dv */
207 209 { NULL, TYPE_Er, 0 }, /* Er */
208 210 { NULL, TYPE_Ev, 0 }, /* Ev */
209 211 { NULL, 0, 0 }, /* Ex */
210 - { NULL, TYPE_Fa, 0 }, /* Fa */
212 + { parse_mdoc_Fa, 0, 0 }, /* Fa */
211 213 { parse_mdoc_Fd, 0, 0 }, /* Fd */
212 214 { NULL, TYPE_Fl, 0 }, /* Fl */
213 215 { parse_mdoc_Fn, 0, 0 }, /* Fn */
214 - { NULL, TYPE_Ft, 0 }, /* Ft */
216 + { NULL, TYPE_Ft | TYPE_Vt, 0 }, /* Ft */
215 217 { NULL, TYPE_Ic, 0 }, /* Ic */
216 218 { NULL, TYPE_In, 0 }, /* In */
217 219 { NULL, TYPE_Li, 0 }, /* Li */
218 220 { parse_mdoc_Nd, 0, 0 }, /* Nd */
219 221 { parse_mdoc_Nm, 0, 0 }, /* Nm */
220 222 { NULL, 0, 0 }, /* Op */
221 223 { NULL, 0, 0 }, /* Ot */
222 224 { NULL, TYPE_Pa, NODE_NOSRC }, /* Pa */
223 225 { NULL, 0, 0 }, /* Rv */
224 226 { NULL, TYPE_St, 0 }, /* St */
225 227 { parse_mdoc_Va, TYPE_Va, 0 }, /* Va */
226 228 { parse_mdoc_Va, TYPE_Vt, 0 }, /* Vt */
227 229 { parse_mdoc_Xr, 0, 0 }, /* Xr */
228 230 { NULL, 0, 0 }, /* %A */
229 231 { NULL, 0, 0 }, /* %B */
230 232 { NULL, 0, 0 }, /* %D */
231 233 { NULL, 0, 0 }, /* %I */
232 234 { NULL, 0, 0 }, /* %J */
233 235 { NULL, 0, 0 }, /* %N */
234 236 { NULL, 0, 0 }, /* %O */
235 237 { NULL, 0, 0 }, /* %P */
236 238 { NULL, 0, 0 }, /* %R */
237 239 { NULL, 0, 0 }, /* %T */
238 240 { NULL, 0, 0 }, /* %V */
239 241 { NULL, 0, 0 }, /* Ac */
240 242 { NULL, 0, 0 }, /* Ao */
241 243 { NULL, 0, 0 }, /* Aq */
242 244 { NULL, TYPE_At, 0 }, /* At */
243 245 { NULL, 0, 0 }, /* Bc */
244 246 { NULL, 0, 0 }, /* Bf */
245 247 { NULL, 0, 0 }, /* Bo */
246 248 { NULL, 0, 0 }, /* Bq */
247 249 { NULL, TYPE_Bsx, NODE_NOSRC }, /* Bsx */
248 250 { NULL, TYPE_Bx, NODE_NOSRC }, /* Bx */
249 251 { NULL, 0, 0 }, /* Db */
250 252 { NULL, 0, 0 }, /* Dc */
251 253 { NULL, 0, 0 }, /* Do */
252 254 { NULL, 0, 0 }, /* Dq */
253 255 { NULL, 0, 0 }, /* Ec */
254 256 { NULL, 0, 0 }, /* Ef */
255 257 { NULL, TYPE_Em, 0 }, /* Em */
256 258 { NULL, 0, 0 }, /* Eo */
257 259 { NULL, TYPE_Fx, NODE_NOSRC }, /* Fx */
258 260 { NULL, TYPE_Ms, 0 }, /* Ms */
259 261 { NULL, 0, 0 }, /* No */
260 262 { NULL, 0, 0 }, /* Ns */
261 263 { NULL, TYPE_Nx, NODE_NOSRC }, /* Nx */
262 264 { NULL, TYPE_Ox, NODE_NOSRC }, /* Ox */
263 265 { NULL, 0, 0 }, /* Pc */
264 266 { NULL, 0, 0 }, /* Pf */
265 267 { NULL, 0, 0 }, /* Po */
266 268 { NULL, 0, 0 }, /* Pq */
267 269 { NULL, 0, 0 }, /* Qc */
268 270 { NULL, 0, 0 }, /* Ql */
269 271 { NULL, 0, 0 }, /* Qo */
270 272 { NULL, 0, 0 }, /* Qq */
271 273 { NULL, 0, 0 }, /* Re */
272 274 { NULL, 0, 0 }, /* Rs */
273 275 { NULL, 0, 0 }, /* Sc */
274 276 { NULL, 0, 0 }, /* So */
275 277 { NULL, 0, 0 }, /* Sq */
276 278 { NULL, 0, 0 }, /* Sm */
277 279 { NULL, 0, 0 }, /* Sx */
278 280 { NULL, TYPE_Sy, 0 }, /* Sy */
279 281 { NULL, TYPE_Tn, 0 }, /* Tn */
280 282 { NULL, 0, NODE_NOSRC }, /* Ux */
281 283 { NULL, 0, 0 }, /* Xc */
282 284 { NULL, 0, 0 }, /* Xo */
283 285 { parse_mdoc_Fo, 0, 0 }, /* Fo */
284 286 { NULL, 0, 0 }, /* Fc */
285 287 { NULL, 0, 0 }, /* Oo */
286 288 { NULL, 0, 0 }, /* Oc */
287 289 { NULL, 0, 0 }, /* Bk */
288 290 { NULL, 0, 0 }, /* Ek */
289 291 { NULL, 0, 0 }, /* Bt */
290 292 { NULL, 0, 0 }, /* Hf */
291 293 { NULL, 0, 0 }, /* Fr */
292 294 { NULL, 0, 0 }, /* Ud */
293 295 { NULL, TYPE_Lb, NODE_NOSRC }, /* Lb */
294 296 { NULL, 0, 0 }, /* Lp */
295 297 { NULL, TYPE_Lk, 0 }, /* Lk */
296 298 { NULL, TYPE_Mt, NODE_NOSRC }, /* Mt */
297 299 { NULL, 0, 0 }, /* Brq */
298 300 { NULL, 0, 0 }, /* Bro */
299 301 { NULL, 0, 0 }, /* Brc */
300 302 { NULL, 0, 0 }, /* %C */
301 303 { NULL, 0, 0 }, /* Es */
302 304 { NULL, 0, 0 }, /* En */
303 305 { NULL, TYPE_Dx, NODE_NOSRC }, /* Dx */
304 306 { NULL, 0, 0 }, /* %Q */
305 307 { NULL, 0, 0 }, /* %U */
306 308 { NULL, 0, 0 }, /* Ta */
307 309 };
308 310 static const struct mdoc_handler *const mdocs = __mdocs - MDOC_Dd;
309 311
310 312
311 313 int
↓ open down ↓ |
87 lines elided |
↑ open up ↑ |
312 314 mandocdb(int argc, char *argv[])
313 315 {
314 316 struct manconf conf;
315 317 struct mparse *mp;
316 318 struct dba *dba;
317 319 const char *path_arg, *progname;
318 320 size_t j, sz;
319 321 int ch, i;
320 322
321 323 #if HAVE_PLEDGE
322 - if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) {
324 + if (pledge("stdio rpath wpath cpath", NULL) == -1) {
323 325 warn("pledge");
324 326 return (int)MANDOCLEVEL_SYSERR;
325 327 }
326 328 #endif
327 329
328 330 #if HAVE_SANDBOX_INIT
329 331 if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1) {
330 332 warnx("sandbox_init");
331 333 return (int)MANDOCLEVEL_SYSERR;
332 334 }
333 335 #endif
334 336
335 337 memset(&conf, 0, sizeof(conf));
336 338
337 339 /*
338 340 * We accept a few different invocations.
339 341 * The CHECKOP macro makes sure that invocation styles don't
340 342 * clobber each other.
341 343 */
342 344 #define CHECKOP(_op, _ch) do \
343 345 if (OP_DEFAULT != (_op)) { \
344 346 warnx("-%c: Conflicting option", (_ch)); \
345 347 goto usage; \
346 348 } while (/*CONSTCOND*/0)
347 349
348 350 path_arg = NULL;
349 351 op = OP_DEFAULT;
350 352
351 353 while (-1 != (ch = getopt(argc, argv, "aC:Dd:npQT:tu:v")))
352 354 switch (ch) {
353 355 case 'a':
354 356 use_all = 1;
355 357 break;
356 358 case 'C':
357 359 CHECKOP(op, ch);
358 360 path_arg = optarg;
359 361 op = OP_CONFFILE;
360 362 break;
361 363 case 'D':
362 364 debug++;
363 365 break;
364 366 case 'd':
365 367 CHECKOP(op, ch);
366 368 path_arg = optarg;
367 369 op = OP_UPDATE;
368 370 break;
369 371 case 'n':
370 372 nodb = 1;
371 373 break;
372 374 case 'p':
373 375 warnings = 1;
374 376 break;
375 377 case 'Q':
376 378 mparse_options |= MPARSE_QUICK;
377 379 break;
378 380 case 'T':
379 381 if (strcmp(optarg, "utf8")) {
380 382 warnx("-T%s: Unsupported output format",
381 383 optarg);
382 384 goto usage;
383 385 }
384 386 write_utf8 = 1;
385 387 break;
386 388 case 't':
387 389 CHECKOP(op, ch);
388 390 dup2(STDOUT_FILENO, STDERR_FILENO);
389 391 op = OP_TEST;
390 392 nodb = warnings = 1;
391 393 break;
392 394 case 'u':
393 395 CHECKOP(op, ch);
394 396 path_arg = optarg;
395 397 op = OP_DELETE;
396 398 break;
397 399 case 'v':
398 400 /* Compatibility with espie@'s makewhatis. */
399 401 break;
400 402 default:
401 403 goto usage;
402 404 }
403 405
404 406 argc -= optind;
405 407 argv += optind;
406 408
407 409 #if HAVE_PLEDGE
408 410 if (nodb) {
409 411 if (pledge("stdio rpath", NULL) == -1) {
410 412 warn("pledge");
411 413 return (int)MANDOCLEVEL_SYSERR;
412 414 }
413 415 }
414 416 #endif
415 417
416 418 if (OP_CONFFILE == op && argc > 0) {
417 419 warnx("-C: Too many arguments");
418 420 goto usage;
419 421 }
420 422
421 423 exitcode = (int)MANDOCLEVEL_OK;
422 424 mchars_alloc();
423 425 mp = mparse_alloc(mparse_options, MANDOCERR_MAX, NULL,
424 426 MANDOC_OS_OTHER, NULL);
425 427 mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev));
426 428 mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file));
427 429
428 430 if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
429 431
430 432 /*
431 433 * Most of these deal with a specific directory.
432 434 * Jump into that directory first.
↓ open down ↓ |
100 lines elided |
↑ open up ↑ |
433 435 */
434 436 if (OP_TEST != op && 0 == set_basedir(path_arg, 1))
435 437 goto out;
436 438
437 439 dba = nodb ? dba_new(128) : dba_read(MANDOC_DB);
438 440 if (dba != NULL) {
439 441 /*
440 442 * The existing database is usable. Process
441 443 * all files specified on the command-line.
442 444 */
443 -#if HAVE_PLEDGE
444 - if (!nodb) {
445 - if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1) {
446 - warn("pledge");
447 - exitcode = (int)MANDOCLEVEL_SYSERR;
448 - goto out;
449 - }
450 - }
451 -#endif
452 445 use_all = 1;
453 446 for (i = 0; i < argc; i++)
454 447 filescan(argv[i]);
455 448 if (nodb == 0)
456 449 dbprune(dba);
457 450 } else {
458 451 /* Database missing or corrupt. */
459 452 if (op != OP_UPDATE || errno != ENOENT)
460 453 say(MANDOC_DB, "%s: Automatically recreating"
461 454 " from scratch", strerror(errno));
462 455 exitcode = (int)MANDOCLEVEL_OK;
463 456 op = OP_DEFAULT;
464 457 if (0 == treescan())
465 458 goto out;
466 459 dba = dba_new(128);
467 460 }
468 461 if (OP_DELETE != op)
469 462 mpages_merge(dba, mp);
470 463 if (nodb == 0)
471 464 dbwrite(dba);
472 465 dba_free(dba);
473 466 } else {
474 467 /*
475 468 * If we have arguments, use them as our manpaths.
476 469 * If we don't, use man.conf(5).
477 470 */
478 471 if (argc > 0) {
479 472 conf.manpath.paths = mandoc_reallocarray(NULL,
480 473 argc, sizeof(char *));
481 474 conf.manpath.sz = (size_t)argc;
482 475 for (i = 0; i < argc; i++)
483 476 conf.manpath.paths[i] = mandoc_strdup(argv[i]);
484 477 } else
485 478 manconf_parse(&conf, path_arg, NULL, NULL);
486 479
487 480 if (conf.manpath.sz == 0) {
488 481 exitcode = (int)MANDOCLEVEL_BADARG;
489 482 say("", "Empty manpath");
490 483 }
491 484
492 485 /*
493 486 * First scan the tree rooted at a base directory, then
494 487 * build a new database and finally move it into place.
495 488 * Ignore zero-length directories and strip trailing
496 489 * slashes.
497 490 */
498 491 for (j = 0; j < conf.manpath.sz; j++) {
499 492 sz = strlen(conf.manpath.paths[j]);
500 493 if (sz && conf.manpath.paths[j][sz - 1] == '/')
501 494 conf.manpath.paths[j][--sz] = '\0';
502 495 if (0 == sz)
503 496 continue;
504 497
505 498 if (j) {
506 499 mandoc_ohash_init(&mpages, 6,
507 500 offsetof(struct mpage, inodev));
508 501 mandoc_ohash_init(&mlinks, 6,
509 502 offsetof(struct mlink, file));
510 503 }
511 504
512 505 if ( ! set_basedir(conf.manpath.paths[j], argc > 0))
513 506 continue;
514 507 if (0 == treescan())
515 508 continue;
516 509 dba = dba_new(128);
517 510 mpages_merge(dba, mp);
518 511 if (nodb == 0)
519 512 dbwrite(dba);
520 513 dba_free(dba);
521 514
522 515 if (j + 1 < conf.manpath.sz) {
523 516 mpages_free();
524 517 ohash_delete(&mpages);
525 518 ohash_delete(&mlinks);
526 519 }
527 520 }
528 521 }
529 522 out:
530 523 manconf_free(&conf);
531 524 mparse_free(mp);
532 525 mchars_free();
533 526 mpages_free();
534 527 ohash_delete(&mpages);
535 528 ohash_delete(&mlinks);
536 529 return exitcode;
537 530 usage:
538 531 progname = getprogname();
539 532 fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
540 533 " %s [-aDnpQ] [-Tutf8] dir ...\n"
541 534 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
542 535 " %s [-Dnp] -u dir [file ...]\n"
543 536 " %s [-Q] -t file ...\n",
544 537 progname, progname, progname, progname, progname);
545 538
546 539 return (int)MANDOCLEVEL_BADARG;
547 540 }
548 541
549 542 /*
550 543 * To get a singly linked list in alpha order while inserting entries
551 544 * at the beginning, process directory entries in reverse alpha order.
552 545 */
553 546 static int
554 547 #if HAVE_FTS_COMPARE_CONST
555 548 fts_compare(const FTSENT *const *a, const FTSENT *const *b)
556 549 #else
557 550 fts_compare(const FTSENT **a, const FTSENT **b)
558 551 #endif
559 552 {
560 553 return -strcmp((*a)->fts_name, (*b)->fts_name);
561 554 }
562 555
563 556 /*
564 557 * Scan a directory tree rooted at "basedir" for manpages.
565 558 * We use fts(), scanning directory parts along the way for clues to our
566 559 * section and architecture.
567 560 *
568 561 * If use_all has been specified, grok all files.
569 562 * If not, sanitise paths to the following:
570 563 *
571 564 * [./]man*[/<arch>]/<name>.<section>
572 565 * or
573 566 * [./]cat<section>[/<arch>]/<name>.0
574 567 *
575 568 * TODO: accommodate for multi-language directories.
576 569 */
577 570 static int
578 571 treescan(void)
579 572 {
580 573 char buf[PATH_MAX];
581 574 FTS *f;
582 575 FTSENT *ff;
583 576 struct mlink *mlink;
584 577 int gzip;
585 578 enum form dform;
586 579 char *dsec, *arch, *fsec, *cp;
587 580 const char *path;
588 581 const char *argv[2];
589 582
590 583 argv[0] = ".";
591 584 argv[1] = NULL;
592 585
593 586 f = fts_open((char * const *)argv, FTS_PHYSICAL | FTS_NOCHDIR,
594 587 fts_compare);
595 588 if (f == NULL) {
596 589 exitcode = (int)MANDOCLEVEL_SYSERR;
597 590 say("", "&fts_open");
598 591 return 0;
599 592 }
600 593
601 594 dsec = arch = NULL;
602 595 dform = FORM_NONE;
603 596
604 597 while ((ff = fts_read(f)) != NULL) {
605 598 path = ff->fts_path + 2;
606 599 switch (ff->fts_info) {
607 600
608 601 /*
609 602 * Symbolic links require various sanity checks,
610 603 * then get handled just like regular files.
611 604 */
612 605 case FTS_SL:
613 606 if (realpath(path, buf) == NULL) {
614 607 if (warnings)
615 608 say(path, "&realpath");
616 609 continue;
617 610 }
618 611 if (strstr(buf, basedir) != buf
619 612 #ifdef HOMEBREWDIR
620 613 && strstr(buf, HOMEBREWDIR) != buf
621 614 #endif
622 615 ) {
623 616 if (warnings) say("",
624 617 "%s: outside base directory", buf);
625 618 continue;
626 619 }
627 620 /* Use logical inode to avoid mpages dupe. */
628 621 if (stat(path, ff->fts_statp) == -1) {
629 622 if (warnings)
630 623 say(path, "&stat");
631 624 continue;
632 625 }
633 626 /* FALLTHROUGH */
634 627
635 628 /*
636 629 * If we're a regular file, add an mlink by using the
637 630 * stored directory data and handling the filename.
638 631 */
639 632 case FTS_F:
640 633 if ( ! strcmp(path, MANDOC_DB))
641 634 continue;
642 635 if ( ! use_all && ff->fts_level < 2) {
643 636 if (warnings)
644 637 say(path, "Extraneous file");
645 638 continue;
646 639 }
647 640 gzip = 0;
648 641 fsec = NULL;
649 642 while (fsec == NULL) {
650 643 fsec = strrchr(ff->fts_name, '.');
651 644 if (fsec == NULL || strcmp(fsec+1, "gz"))
652 645 break;
653 646 gzip = 1;
654 647 *fsec = '\0';
655 648 fsec = NULL;
656 649 }
657 650 if (fsec == NULL) {
658 651 if ( ! use_all) {
659 652 if (warnings)
660 653 say(path,
661 654 "No filename suffix");
662 655 continue;
663 656 }
664 657 } else if ( ! strcmp(++fsec, "html")) {
665 658 if (warnings)
666 659 say(path, "Skip html");
667 660 continue;
668 661 } else if ( ! strcmp(fsec, "ps")) {
669 662 if (warnings)
670 663 say(path, "Skip ps");
671 664 continue;
672 665 } else if ( ! strcmp(fsec, "pdf")) {
673 666 if (warnings)
674 667 say(path, "Skip pdf");
675 668 continue;
676 669 } else if ( ! use_all &&
677 670 ((dform == FORM_SRC &&
678 671 strncmp(fsec, dsec, strlen(dsec))) ||
679 672 (dform == FORM_CAT && strcmp(fsec, "0")))) {
680 673 if (warnings)
681 674 say(path, "Wrong filename suffix");
682 675 continue;
683 676 } else
684 677 fsec[-1] = '\0';
685 678
686 679 mlink = mandoc_calloc(1, sizeof(struct mlink));
687 680 if (strlcpy(mlink->file, path,
688 681 sizeof(mlink->file)) >=
689 682 sizeof(mlink->file)) {
690 683 say(path, "Filename too long");
691 684 free(mlink);
692 685 continue;
693 686 }
694 687 mlink->dform = dform;
695 688 mlink->dsec = dsec;
696 689 mlink->arch = arch;
697 690 mlink->name = ff->fts_name;
698 691 mlink->fsec = fsec;
699 692 mlink->gzip = gzip;
700 693 mlink_add(mlink, ff->fts_statp);
701 694 continue;
702 695
703 696 case FTS_D:
704 697 case FTS_DP:
705 698 break;
706 699
707 700 default:
708 701 if (warnings)
709 702 say(path, "Not a regular file");
710 703 continue;
711 704 }
712 705
713 706 switch (ff->fts_level) {
714 707 case 0:
715 708 /* Ignore the root directory. */
716 709 break;
717 710 case 1:
718 711 /*
719 712 * This might contain manX/ or catX/.
720 713 * Try to infer this from the name.
721 714 * If we're not in use_all, enforce it.
722 715 */
723 716 cp = ff->fts_name;
724 717 if (ff->fts_info == FTS_DP) {
725 718 dform = FORM_NONE;
726 719 dsec = NULL;
727 720 break;
728 721 }
729 722
730 723 if ( ! strncmp(cp, "man", 3)) {
731 724 dform = FORM_SRC;
732 725 dsec = cp + 3;
733 726 } else if ( ! strncmp(cp, "cat", 3)) {
734 727 dform = FORM_CAT;
735 728 dsec = cp + 3;
736 729 } else {
737 730 dform = FORM_NONE;
738 731 dsec = NULL;
739 732 }
740 733
741 734 if (dsec != NULL || use_all)
742 735 break;
743 736
744 737 if (warnings)
745 738 say(path, "Unknown directory part");
746 739 fts_set(f, ff, FTS_SKIP);
747 740 break;
748 741 case 2:
749 742 /*
750 743 * Possibly our architecture.
751 744 * If we're descending, keep tabs on it.
752 745 */
753 746 if (ff->fts_info != FTS_DP && dsec != NULL)
754 747 arch = ff->fts_name;
755 748 else
756 749 arch = NULL;
757 750 break;
758 751 default:
759 752 if (ff->fts_info == FTS_DP || use_all)
760 753 break;
761 754 if (warnings)
762 755 say(path, "Extraneous directory part");
763 756 fts_set(f, ff, FTS_SKIP);
764 757 break;
765 758 }
766 759 }
767 760
768 761 fts_close(f);
769 762 return 1;
770 763 }
771 764
772 765 /*
773 766 * Add a file to the mlinks table.
774 767 * Do not verify that it's a "valid" looking manpage (we'll do that
775 768 * later).
776 769 *
777 770 * Try to infer the manual section, architecture, and page name from the
778 771 * path, assuming it looks like
779 772 *
780 773 * [./]man*[/<arch>]/<name>.<section>
781 774 * or
782 775 * [./]cat<section>[/<arch>]/<name>.0
783 776 *
784 777 * See treescan() for the fts(3) version of this.
785 778 */
786 779 static void
787 780 filescan(const char *file)
788 781 {
789 782 char buf[PATH_MAX];
790 783 struct stat st;
791 784 struct mlink *mlink;
792 785 char *p, *start;
793 786
794 787 assert(use_all);
795 788
796 789 if (0 == strncmp(file, "./", 2))
797 790 file += 2;
798 791
799 792 /*
800 793 * We have to do lstat(2) before realpath(3) loses
801 794 * the information whether this is a symbolic link.
802 795 * We need to know that because for symbolic links,
803 796 * we want to use the orginal file name, while for
804 797 * regular files, we want to use the real path.
805 798 */
806 799 if (-1 == lstat(file, &st)) {
807 800 exitcode = (int)MANDOCLEVEL_BADARG;
808 801 say(file, "&lstat");
809 802 return;
810 803 } else if (0 == ((S_IFREG | S_IFLNK) & st.st_mode)) {
811 804 exitcode = (int)MANDOCLEVEL_BADARG;
812 805 say(file, "Not a regular file");
813 806 return;
814 807 }
815 808
816 809 /*
817 810 * We have to resolve the file name to the real path
818 811 * in any case for the base directory check.
819 812 */
820 813 if (NULL == realpath(file, buf)) {
821 814 exitcode = (int)MANDOCLEVEL_BADARG;
822 815 say(file, "&realpath");
823 816 return;
824 817 }
825 818
826 819 if (OP_TEST == op)
827 820 start = buf;
828 821 else if (strstr(buf, basedir) == buf)
829 822 start = buf + strlen(basedir);
830 823 #ifdef HOMEBREWDIR
831 824 else if (strstr(buf, HOMEBREWDIR) == buf)
832 825 start = buf;
833 826 #endif
834 827 else {
835 828 exitcode = (int)MANDOCLEVEL_BADARG;
836 829 say("", "%s: outside base directory", buf);
837 830 return;
838 831 }
839 832
840 833 /*
841 834 * Now we are sure the file is inside our tree.
842 835 * If it is a symbolic link, ignore the real path
843 836 * and use the original name.
844 837 * This implies passing stuff like "cat1/../man1/foo.1"
845 838 * on the command line won't work. So don't do that.
846 839 * Note the stat(2) can still fail if the link target
847 840 * doesn't exist.
848 841 */
849 842 if (S_IFLNK & st.st_mode) {
850 843 if (-1 == stat(buf, &st)) {
851 844 exitcode = (int)MANDOCLEVEL_BADARG;
852 845 say(file, "&stat");
853 846 return;
854 847 }
855 848 if (strlcpy(buf, file, sizeof(buf)) >= sizeof(buf)) {
856 849 say(file, "Filename too long");
857 850 return;
858 851 }
859 852 start = buf;
860 853 if (OP_TEST != op && strstr(buf, basedir) == buf)
861 854 start += strlen(basedir);
862 855 }
863 856
864 857 mlink = mandoc_calloc(1, sizeof(struct mlink));
865 858 mlink->dform = FORM_NONE;
866 859 if (strlcpy(mlink->file, start, sizeof(mlink->file)) >=
867 860 sizeof(mlink->file)) {
868 861 say(start, "Filename too long");
869 862 free(mlink);
870 863 return;
871 864 }
872 865
873 866 /*
874 867 * In test mode or when the original name is absolute
875 868 * but outside our tree, guess the base directory.
876 869 */
877 870
878 871 if (op == OP_TEST || (start == buf && *start == '/')) {
879 872 if (strncmp(buf, "man/", 4) == 0)
880 873 start = buf + 4;
881 874 else if ((start = strstr(buf, "/man/")) != NULL)
882 875 start += 5;
883 876 else
884 877 start = buf;
885 878 }
886 879
887 880 /*
888 881 * First try to guess our directory structure.
889 882 * If we find a separator, try to look for man* or cat*.
890 883 * If we find one of these and what's underneath is a directory,
891 884 * assume it's an architecture.
892 885 */
893 886 if (NULL != (p = strchr(start, '/'))) {
894 887 *p++ = '\0';
895 888 if (0 == strncmp(start, "man", 3)) {
896 889 mlink->dform = FORM_SRC;
897 890 mlink->dsec = start + 3;
898 891 } else if (0 == strncmp(start, "cat", 3)) {
899 892 mlink->dform = FORM_CAT;
900 893 mlink->dsec = start + 3;
901 894 }
902 895
903 896 start = p;
904 897 if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) {
905 898 *p++ = '\0';
906 899 mlink->arch = start;
907 900 start = p;
908 901 }
909 902 }
910 903
911 904 /*
912 905 * Now check the file suffix.
913 906 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
914 907 */
915 908 p = strrchr(start, '\0');
916 909 while (p-- > start && '/' != *p && '.' != *p)
917 910 /* Loop. */ ;
918 911
919 912 if ('.' == *p) {
920 913 *p++ = '\0';
921 914 mlink->fsec = p;
922 915 }
923 916
924 917 /*
925 918 * Now try to parse the name.
926 919 * Use the filename portion of the path.
927 920 */
928 921 mlink->name = start;
929 922 if (NULL != (p = strrchr(start, '/'))) {
930 923 mlink->name = p + 1;
931 924 *p = '\0';
932 925 }
933 926 mlink_add(mlink, &st);
934 927 }
935 928
936 929 static void
937 930 mlink_add(struct mlink *mlink, const struct stat *st)
938 931 {
939 932 struct inodev inodev;
940 933 struct mpage *mpage;
941 934 unsigned int slot;
942 935
943 936 assert(NULL != mlink->file);
944 937
945 938 mlink->dsec = mandoc_strdup(mlink->dsec ? mlink->dsec : "");
946 939 mlink->arch = mandoc_strdup(mlink->arch ? mlink->arch : "");
947 940 mlink->name = mandoc_strdup(mlink->name ? mlink->name : "");
948 941 mlink->fsec = mandoc_strdup(mlink->fsec ? mlink->fsec : "");
949 942
950 943 if ('0' == *mlink->fsec) {
951 944 free(mlink->fsec);
952 945 mlink->fsec = mandoc_strdup(mlink->dsec);
953 946 mlink->fform = FORM_CAT;
954 947 } else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec)
955 948 mlink->fform = FORM_SRC;
956 949 else
957 950 mlink->fform = FORM_NONE;
958 951
959 952 slot = ohash_qlookup(&mlinks, mlink->file);
960 953 assert(NULL == ohash_find(&mlinks, slot));
961 954 ohash_insert(&mlinks, slot, mlink);
962 955
963 956 memset(&inodev, 0, sizeof(inodev)); /* Clear padding. */
964 957 inodev.st_ino = st->st_ino;
965 958 inodev.st_dev = st->st_dev;
966 959 slot = ohash_lookup_memory(&mpages, (char *)&inodev,
967 960 sizeof(struct inodev), inodev.st_ino);
968 961 mpage = ohash_find(&mpages, slot);
969 962 if (NULL == mpage) {
970 963 mpage = mandoc_calloc(1, sizeof(struct mpage));
971 964 mpage->inodev.st_ino = inodev.st_ino;
972 965 mpage->inodev.st_dev = inodev.st_dev;
973 966 mpage->form = FORM_NONE;
974 967 mpage->next = mpage_head;
975 968 mpage_head = mpage;
976 969 ohash_insert(&mpages, slot, mpage);
977 970 } else
978 971 mlink->next = mpage->mlinks;
979 972 mpage->mlinks = mlink;
980 973 mlink->mpage = mpage;
981 974 }
982 975
983 976 static void
984 977 mlink_free(struct mlink *mlink)
985 978 {
986 979
987 980 free(mlink->dsec);
988 981 free(mlink->arch);
989 982 free(mlink->name);
990 983 free(mlink->fsec);
991 984 free(mlink);
992 985 }
993 986
994 987 static void
995 988 mpages_free(void)
996 989 {
997 990 struct mpage *mpage;
998 991 struct mlink *mlink;
999 992
1000 993 while ((mpage = mpage_head) != NULL) {
1001 994 while ((mlink = mpage->mlinks) != NULL) {
1002 995 mpage->mlinks = mlink->next;
1003 996 mlink_free(mlink);
1004 997 }
1005 998 mpage_head = mpage->next;
1006 999 free(mpage->sec);
1007 1000 free(mpage->arch);
1008 1001 free(mpage->title);
1009 1002 free(mpage->desc);
1010 1003 free(mpage);
1011 1004 }
1012 1005 }
1013 1006
1014 1007 /*
1015 1008 * For each mlink to the mpage, check whether the path looks like
1016 1009 * it is formatted, and if it does, check whether a source manual
1017 1010 * exists by the same name, ignoring the suffix.
1018 1011 * If both conditions hold, drop the mlink.
1019 1012 */
1020 1013 static void
1021 1014 mlinks_undupe(struct mpage *mpage)
1022 1015 {
1023 1016 char buf[PATH_MAX];
1024 1017 struct mlink **prev;
1025 1018 struct mlink *mlink;
1026 1019 char *bufp;
1027 1020
1028 1021 mpage->form = FORM_CAT;
1029 1022 prev = &mpage->mlinks;
1030 1023 while (NULL != (mlink = *prev)) {
1031 1024 if (FORM_CAT != mlink->dform) {
1032 1025 mpage->form = FORM_NONE;
1033 1026 goto nextlink;
1034 1027 }
1035 1028 (void)strlcpy(buf, mlink->file, sizeof(buf));
1036 1029 bufp = strstr(buf, "cat");
1037 1030 assert(NULL != bufp);
1038 1031 memcpy(bufp, "man", 3);
1039 1032 if (NULL != (bufp = strrchr(buf, '.')))
1040 1033 *++bufp = '\0';
1041 1034 (void)strlcat(buf, mlink->dsec, sizeof(buf));
1042 1035 if (NULL == ohash_find(&mlinks,
1043 1036 ohash_qlookup(&mlinks, buf)))
1044 1037 goto nextlink;
1045 1038 if (warnings)
1046 1039 say(mlink->file, "Man source exists: %s", buf);
1047 1040 if (use_all)
1048 1041 goto nextlink;
1049 1042 *prev = mlink->next;
1050 1043 mlink_free(mlink);
1051 1044 continue;
1052 1045 nextlink:
1053 1046 prev = &(*prev)->next;
1054 1047 }
1055 1048 }
1056 1049
1057 1050 static void
1058 1051 mlink_check(struct mpage *mpage, struct mlink *mlink)
1059 1052 {
1060 1053 struct str *str;
1061 1054 unsigned int slot;
1062 1055
1063 1056 /*
1064 1057 * Check whether the manual section given in a file
1065 1058 * agrees with the directory where the file is located.
1066 1059 * Some manuals have suffixes like (3p) on their
1067 1060 * section number either inside the file or in the
1068 1061 * directory name, some are linked into more than one
1069 1062 * section, like encrypt(1) = makekey(8).
1070 1063 */
1071 1064
1072 1065 if (FORM_SRC == mpage->form &&
1073 1066 strcasecmp(mpage->sec, mlink->dsec))
1074 1067 say(mlink->file, "Section \"%s\" manual in %s directory",
1075 1068 mpage->sec, mlink->dsec);
1076 1069
1077 1070 /*
1078 1071 * Manual page directories exist for each kernel
1079 1072 * architecture as returned by machine(1).
1080 1073 * However, many manuals only depend on the
1081 1074 * application architecture as returned by arch(1).
1082 1075 * For example, some (2/ARM) manuals are shared
1083 1076 * across the "armish" and "zaurus" kernel
1084 1077 * architectures.
1085 1078 * A few manuals are even shared across completely
1086 1079 * different architectures, for example fdformat(1)
1087 1080 * on amd64, i386, and sparc64.
1088 1081 */
1089 1082
1090 1083 if (strcasecmp(mpage->arch, mlink->arch))
1091 1084 say(mlink->file, "Architecture \"%s\" manual in "
1092 1085 "\"%s\" directory", mpage->arch, mlink->arch);
1093 1086
1094 1087 /*
1095 1088 * XXX
1096 1089 * parse_cat() doesn't set NAME_TITLE yet.
1097 1090 */
1098 1091
1099 1092 if (FORM_CAT == mpage->form)
1100 1093 return;
1101 1094
1102 1095 /*
1103 1096 * Check whether this mlink
1104 1097 * appears as a name in the NAME section.
1105 1098 */
1106 1099
1107 1100 slot = ohash_qlookup(&names, mlink->name);
1108 1101 str = ohash_find(&names, slot);
1109 1102 assert(NULL != str);
1110 1103 if ( ! (NAME_TITLE & str->mask))
1111 1104 say(mlink->file, "Name missing in NAME section");
1112 1105 }
1113 1106
1114 1107 /*
1115 1108 * Run through the files in the global vector "mpages"
1116 1109 * and add them to the database specified in "basedir".
1117 1110 *
1118 1111 * This handles the parsing scheme itself, using the cues of directory
1119 1112 * and filename to determine whether the file is parsable or not.
1120 1113 */
1121 1114 static void
1122 1115 mpages_merge(struct dba *dba, struct mparse *mp)
1123 1116 {
1124 1117 struct mpage *mpage, *mpage_dest;
1125 1118 struct mlink *mlink, *mlink_dest;
1126 1119 struct roff_man *man;
1127 1120 char *sodest;
1128 1121 char *cp;
1129 1122 int fd;
1130 1123
1131 1124 for (mpage = mpage_head; mpage != NULL; mpage = mpage->next) {
1132 1125 mlinks_undupe(mpage);
1133 1126 if ((mlink = mpage->mlinks) == NULL)
1134 1127 continue;
1135 1128
1136 1129 name_mask = NAME_MASK;
1137 1130 mandoc_ohash_init(&names, 4, offsetof(struct str, key));
1138 1131 mandoc_ohash_init(&strings, 6, offsetof(struct str, key));
1139 1132 mparse_reset(mp);
1140 1133 man = NULL;
1141 1134 sodest = NULL;
1142 1135
1143 1136 if ((fd = mparse_open(mp, mlink->file)) == -1) {
1144 1137 say(mlink->file, "&open");
1145 1138 goto nextpage;
1146 1139 }
1147 1140
1148 1141 /*
1149 1142 * Interpret the file as mdoc(7) or man(7) source
1150 1143 * code, unless it is known to be formatted.
1151 1144 */
1152 1145 if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) {
1153 1146 mparse_readfd(mp, fd, mlink->file);
1154 1147 close(fd);
1155 1148 fd = -1;
1156 1149 mparse_result(mp, &man, &sodest);
1157 1150 }
1158 1151
1159 1152 if (sodest != NULL) {
1160 1153 mlink_dest = ohash_find(&mlinks,
1161 1154 ohash_qlookup(&mlinks, sodest));
1162 1155 if (mlink_dest == NULL) {
1163 1156 mandoc_asprintf(&cp, "%s.gz", sodest);
1164 1157 mlink_dest = ohash_find(&mlinks,
1165 1158 ohash_qlookup(&mlinks, cp));
1166 1159 free(cp);
1167 1160 }
1168 1161 if (mlink_dest != NULL) {
1169 1162
1170 1163 /* The .so target exists. */
1171 1164
1172 1165 mpage_dest = mlink_dest->mpage;
1173 1166 while (1) {
1174 1167 mlink->mpage = mpage_dest;
1175 1168
1176 1169 /*
1177 1170 * If the target was already
1178 1171 * processed, add the links
1179 1172 * to the database now.
1180 1173 * Otherwise, this will
1181 1174 * happen when we come
1182 1175 * to the target.
1183 1176 */
1184 1177
1185 1178 if (mpage_dest->dba != NULL)
1186 1179 dbadd_mlink(mlink);
1187 1180
1188 1181 if (mlink->next == NULL)
1189 1182 break;
1190 1183 mlink = mlink->next;
1191 1184 }
1192 1185
1193 1186 /* Move all links to the target. */
1194 1187
1195 1188 mlink->next = mlink_dest->next;
1196 1189 mlink_dest->next = mpage->mlinks;
1197 1190 mpage->mlinks = NULL;
1198 1191 }
1199 1192 goto nextpage;
1200 1193 } else if (man != NULL && man->macroset == MACROSET_MDOC) {
1201 1194 mdoc_validate(man);
1202 1195 mpage->form = FORM_SRC;
1203 1196 mpage->sec = man->meta.msec;
1204 1197 mpage->sec = mandoc_strdup(
1205 1198 mpage->sec == NULL ? "" : mpage->sec);
1206 1199 mpage->arch = man->meta.arch;
1207 1200 mpage->arch = mandoc_strdup(
1208 1201 mpage->arch == NULL ? "" : mpage->arch);
1209 1202 mpage->title = mandoc_strdup(man->meta.title);
1210 1203 } else if (man != NULL && man->macroset == MACROSET_MAN) {
1211 1204 man_validate(man);
1212 1205 if (*man->meta.msec != '\0' ||
1213 1206 *man->meta.title != '\0') {
1214 1207 mpage->form = FORM_SRC;
1215 1208 mpage->sec = mandoc_strdup(man->meta.msec);
1216 1209 mpage->arch = mandoc_strdup(mlink->arch);
1217 1210 mpage->title = mandoc_strdup(man->meta.title);
1218 1211 } else
1219 1212 man = NULL;
1220 1213 }
1221 1214
1222 1215 assert(mpage->desc == NULL);
1223 1216 if (man == NULL) {
1224 1217 mpage->form = FORM_CAT;
1225 1218 mpage->sec = mandoc_strdup(mlink->dsec);
1226 1219 mpage->arch = mandoc_strdup(mlink->arch);
1227 1220 mpage->title = mandoc_strdup(mlink->name);
1228 1221 parse_cat(mpage, fd);
1229 1222 } else if (man->macroset == MACROSET_MDOC)
1230 1223 parse_mdoc(mpage, &man->meta, man->first);
1231 1224 else
1232 1225 parse_man(mpage, &man->meta, man->first);
1233 1226 if (mpage->desc == NULL) {
1234 1227 mpage->desc = mandoc_strdup(mlink->name);
1235 1228 if (warnings)
1236 1229 say(mlink->file, "No one-line description, "
1237 1230 "using filename \"%s\"", mlink->name);
1238 1231 }
1239 1232
1240 1233 for (mlink = mpage->mlinks;
1241 1234 mlink != NULL;
1242 1235 mlink = mlink->next) {
1243 1236 putkey(mpage, mlink->name, NAME_FILE);
1244 1237 if (warnings && !use_all)
1245 1238 mlink_check(mpage, mlink);
1246 1239 }
1247 1240
1248 1241 dbadd(dba, mpage);
1249 1242
1250 1243 nextpage:
1251 1244 ohash_delete(&strings);
1252 1245 ohash_delete(&names);
1253 1246 }
1254 1247 }
1255 1248
1256 1249 static void
1257 1250 parse_cat(struct mpage *mpage, int fd)
1258 1251 {
1259 1252 FILE *stream;
1260 1253 struct mlink *mlink;
1261 1254 char *line, *p, *title, *sec;
1262 1255 size_t linesz, plen, titlesz;
1263 1256 ssize_t len;
1264 1257 int offs;
1265 1258
1266 1259 mlink = mpage->mlinks;
1267 1260 stream = fd == -1 ? fopen(mlink->file, "r") : fdopen(fd, "r");
1268 1261 if (stream == NULL) {
1269 1262 if (fd != -1)
1270 1263 close(fd);
1271 1264 if (warnings)
1272 1265 say(mlink->file, "&fopen");
1273 1266 return;
1274 1267 }
1275 1268
1276 1269 line = NULL;
1277 1270 linesz = 0;
1278 1271
1279 1272 /* Parse the section number from the header line. */
1280 1273
1281 1274 while (getline(&line, &linesz, stream) != -1) {
1282 1275 if (*line == '\n')
1283 1276 continue;
1284 1277 if ((sec = strchr(line, '(')) == NULL)
1285 1278 break;
1286 1279 if ((p = strchr(++sec, ')')) == NULL)
1287 1280 break;
1288 1281 free(mpage->sec);
1289 1282 mpage->sec = mandoc_strndup(sec, p - sec);
1290 1283 if (warnings && *mlink->dsec != '\0' &&
1291 1284 strcasecmp(mpage->sec, mlink->dsec))
1292 1285 say(mlink->file,
1293 1286 "Section \"%s\" manual in %s directory",
1294 1287 mpage->sec, mlink->dsec);
1295 1288 break;
1296 1289 }
1297 1290
1298 1291 /* Skip to first blank line. */
1299 1292
1300 1293 while (line == NULL || *line != '\n')
1301 1294 if (getline(&line, &linesz, stream) == -1)
1302 1295 break;
1303 1296
1304 1297 /*
1305 1298 * Assume the first line that is not indented
1306 1299 * is the first section header. Skip to it.
1307 1300 */
1308 1301
1309 1302 while (getline(&line, &linesz, stream) != -1)
1310 1303 if (*line != '\n' && *line != ' ')
1311 1304 break;
1312 1305
1313 1306 /*
1314 1307 * Read up until the next section into a buffer.
1315 1308 * Strip the leading and trailing newline from each read line,
1316 1309 * appending a trailing space.
1317 1310 * Ignore empty (whitespace-only) lines.
1318 1311 */
1319 1312
1320 1313 titlesz = 0;
1321 1314 title = NULL;
1322 1315
1323 1316 while ((len = getline(&line, &linesz, stream)) != -1) {
1324 1317 if (*line != ' ')
1325 1318 break;
1326 1319 offs = 0;
1327 1320 while (isspace((unsigned char)line[offs]))
1328 1321 offs++;
1329 1322 if (line[offs] == '\0')
1330 1323 continue;
1331 1324 title = mandoc_realloc(title, titlesz + len - offs);
1332 1325 memcpy(title + titlesz, line + offs, len - offs);
1333 1326 titlesz += len - offs;
1334 1327 title[titlesz - 1] = ' ';
1335 1328 }
1336 1329 free(line);
1337 1330
1338 1331 /*
1339 1332 * If no page content can be found, or the input line
1340 1333 * is already the next section header, or there is no
1341 1334 * trailing newline, reuse the page title as the page
1342 1335 * description.
1343 1336 */
1344 1337
1345 1338 if (NULL == title || '\0' == *title) {
1346 1339 if (warnings)
1347 1340 say(mlink->file, "Cannot find NAME section");
1348 1341 fclose(stream);
1349 1342 free(title);
1350 1343 return;
1351 1344 }
1352 1345
1353 1346 title[titlesz - 1] = '\0';
1354 1347
1355 1348 /*
1356 1349 * Skip to the first dash.
1357 1350 * Use the remaining line as the description (no more than 70
1358 1351 * bytes).
1359 1352 */
1360 1353
1361 1354 if (NULL != (p = strstr(title, "- "))) {
1362 1355 for (p += 2; ' ' == *p || '\b' == *p; p++)
1363 1356 /* Skip to next word. */ ;
1364 1357 } else {
1365 1358 if (warnings)
1366 1359 say(mlink->file, "No dash in title line, "
1367 1360 "reusing \"%s\" as one-line description", title);
1368 1361 p = title;
1369 1362 }
1370 1363
1371 1364 plen = strlen(p);
1372 1365
1373 1366 /* Strip backspace-encoding from line. */
1374 1367
↓ open down ↓ |
913 lines elided |
↑ open up ↑ |
1375 1368 while (NULL != (line = memchr(p, '\b', plen))) {
1376 1369 len = line - p;
1377 1370 if (0 == len) {
1378 1371 memmove(line, line + 1, plen--);
1379 1372 continue;
1380 1373 }
1381 1374 memmove(line - 1, line + 1, plen - len);
1382 1375 plen -= 2;
1383 1376 }
1384 1377
1385 - mpage->desc = mandoc_strdup(p);
1378 + /*
1379 + * Cut off excessive one-line descriptions.
1380 + * Bad pages are not worth better heuristics.
1381 + */
1382 +
1383 + mpage->desc = mandoc_strndup(p, 150);
1386 1384 fclose(stream);
1387 1385 free(title);
1388 1386 }
1389 1387
1390 1388 /*
1391 1389 * Put a type/word pair into the word database for this particular file.
1392 1390 */
1393 1391 static void
1394 1392 putkey(const struct mpage *mpage, char *value, uint64_t type)
1395 1393 {
1396 1394 putkeys(mpage, value, strlen(value), type);
1397 1395 }
1398 1396
1399 1397 /*
1400 1398 * Grok all nodes at or below a certain mdoc node into putkey().
1401 1399 */
1402 1400 static void
1403 1401 putmdockey(const struct mpage *mpage,
1404 1402 const struct roff_node *n, uint64_t m, int taboo)
1405 1403 {
1406 1404
1407 1405 for ( ; NULL != n; n = n->next) {
1408 1406 if (n->flags & taboo)
1409 1407 continue;
1410 1408 if (NULL != n->child)
1411 1409 putmdockey(mpage, n->child, m, taboo);
1412 1410 if (n->type == ROFFT_TEXT)
1413 1411 putkey(mpage, n->string, m);
1414 1412 }
1415 1413 }
1416 1414
1417 1415 static void
1418 1416 parse_man(struct mpage *mpage, const struct roff_meta *meta,
1419 1417 const struct roff_node *n)
1420 1418 {
1421 1419 const struct roff_node *head, *body;
1422 1420 char *start, *title;
1423 1421 char byte;
1424 1422 size_t sz;
1425 1423
1426 1424 if (n == NULL)
1427 1425 return;
1428 1426
1429 1427 /*
1430 1428 * We're only searching for one thing: the first text child in
1431 1429 * the BODY of a NAME section. Since we don't keep track of
1432 1430 * sections in -man, run some hoops to find out whether we're in
1433 1431 * the correct section or not.
1434 1432 */
1435 1433
1436 1434 if (n->type == ROFFT_BODY && n->tok == MAN_SH) {
1437 1435 body = n;
1438 1436 if ((head = body->parent->head) != NULL &&
1439 1437 (head = head->child) != NULL &&
1440 1438 head->next == NULL &&
1441 1439 head->type == ROFFT_TEXT &&
1442 1440 strcmp(head->string, "NAME") == 0 &&
1443 1441 body->child != NULL) {
1444 1442
1445 1443 /*
1446 1444 * Suck the entire NAME section into memory.
1447 1445 * Yes, we might run away.
1448 1446 * But too many manuals have big, spread-out
1449 1447 * NAME sections over many lines.
1450 1448 */
1451 1449
1452 1450 title = NULL;
1453 1451 deroff(&title, body);
1454 1452 if (NULL == title)
1455 1453 return;
1456 1454
1457 1455 /*
1458 1456 * Go through a special heuristic dance here.
1459 1457 * Conventionally, one or more manual names are
1460 1458 * comma-specified prior to a whitespace, then a
1461 1459 * dash, then a description. Try to puzzle out
1462 1460 * the name parts here.
1463 1461 */
1464 1462
1465 1463 start = title;
1466 1464 for ( ;; ) {
1467 1465 sz = strcspn(start, " ,");
1468 1466 if ('\0' == start[sz])
1469 1467 break;
1470 1468
1471 1469 byte = start[sz];
1472 1470 start[sz] = '\0';
1473 1471
1474 1472 /*
1475 1473 * Assume a stray trailing comma in the
1476 1474 * name list if a name begins with a dash.
1477 1475 */
1478 1476
1479 1477 if ('-' == start[0] ||
1480 1478 ('\\' == start[0] && '-' == start[1]))
1481 1479 break;
1482 1480
1483 1481 putkey(mpage, start, NAME_TITLE);
1484 1482 if ( ! (mpage->name_head_done ||
1485 1483 strcasecmp(start, meta->title))) {
1486 1484 putkey(mpage, start, NAME_HEAD);
1487 1485 mpage->name_head_done = 1;
1488 1486 }
1489 1487
1490 1488 if (' ' == byte) {
1491 1489 start += sz + 1;
1492 1490 break;
1493 1491 }
1494 1492
1495 1493 assert(',' == byte);
1496 1494 start += sz + 1;
1497 1495 while (' ' == *start)
1498 1496 start++;
1499 1497 }
1500 1498
1501 1499 if (start == title) {
1502 1500 putkey(mpage, start, NAME_TITLE);
1503 1501 if ( ! (mpage->name_head_done ||
1504 1502 strcasecmp(start, meta->title))) {
1505 1503 putkey(mpage, start, NAME_HEAD);
1506 1504 mpage->name_head_done = 1;
1507 1505 }
1508 1506 free(title);
1509 1507 return;
1510 1508 }
1511 1509
1512 1510 while (isspace((unsigned char)*start))
1513 1511 start++;
1514 1512
1515 1513 if (0 == strncmp(start, "-", 1))
1516 1514 start += 1;
1517 1515 else if (0 == strncmp(start, "\\-\\-", 4))
1518 1516 start += 4;
↓ open down ↓ |
123 lines elided |
↑ open up ↑ |
1519 1517 else if (0 == strncmp(start, "\\-", 2))
1520 1518 start += 2;
1521 1519 else if (0 == strncmp(start, "\\(en", 4))
1522 1520 start += 4;
1523 1521 else if (0 == strncmp(start, "\\(em", 4))
1524 1522 start += 4;
1525 1523
1526 1524 while (' ' == *start)
1527 1525 start++;
1528 1526
1529 - mpage->desc = mandoc_strdup(start);
1527 + /*
1528 + * Cut off excessive one-line descriptions.
1529 + * Bad pages are not worth better heuristics.
1530 + */
1531 +
1532 + mpage->desc = mandoc_strndup(start, 150);
1530 1533 free(title);
1531 1534 return;
1532 1535 }
1533 1536 }
1534 1537
1535 1538 for (n = n->child; n; n = n->next) {
1536 1539 if (NULL != mpage->desc)
1537 1540 break;
1538 1541 parse_man(mpage, meta, n);
1539 1542 }
1540 1543 }
1541 1544
1542 1545 static void
1543 1546 parse_mdoc(struct mpage *mpage, const struct roff_meta *meta,
1544 1547 const struct roff_node *n)
1545 1548 {
1546 1549
1547 1550 for (n = n->child; n != NULL; n = n->next) {
1548 1551 if (n->tok == TOKEN_NONE ||
1549 1552 n->tok < ROFF_MAX ||
1550 1553 n->flags & mdocs[n->tok].taboo)
1551 1554 continue;
1552 1555 assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX);
1553 1556 switch (n->type) {
1554 1557 case ROFFT_ELEM:
1555 1558 case ROFFT_BLOCK:
1556 1559 case ROFFT_HEAD:
1557 1560 case ROFFT_BODY:
1558 1561 case ROFFT_TAIL:
1559 1562 if (mdocs[n->tok].fp != NULL &&
1560 1563 (*mdocs[n->tok].fp)(mpage, meta, n) == 0)
1561 1564 break;
1562 1565 if (mdocs[n->tok].mask)
1563 1566 putmdockey(mpage, n->child,
1564 1567 mdocs[n->tok].mask, mdocs[n->tok].taboo);
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
1565 1568 break;
1566 1569 default:
1567 1570 continue;
1568 1571 }
1569 1572 if (NULL != n->child)
1570 1573 parse_mdoc(mpage, meta, n);
1571 1574 }
1572 1575 }
1573 1576
1574 1577 static int
1578 +parse_mdoc_Fa(struct mpage *mpage, const struct roff_meta *meta,
1579 + const struct roff_node *n)
1580 +{
1581 + uint64_t mask;
1582 +
1583 + mask = TYPE_Fa;
1584 + if (n->sec == SEC_SYNOPSIS)
1585 + mask |= TYPE_Vt;
1586 +
1587 + putmdockey(mpage, n->child, mask, 0);
1588 + return 0;
1589 +}
1590 +
1591 +static int
1575 1592 parse_mdoc_Fd(struct mpage *mpage, const struct roff_meta *meta,
1576 1593 const struct roff_node *n)
1577 1594 {
1578 1595 char *start, *end;
1579 1596 size_t sz;
1580 1597
1581 1598 if (SEC_SYNOPSIS != n->sec ||
1582 1599 NULL == (n = n->child) ||
1583 1600 n->type != ROFFT_TEXT)
1584 1601 return 0;
1585 1602
1586 1603 /*
1587 1604 * Only consider those `Fd' macro fields that begin with an
1588 1605 * "inclusion" token (versus, e.g., #define).
1589 1606 */
1590 1607
1591 1608 if (strcmp("#include", n->string))
1592 1609 return 0;
1593 1610
1594 1611 if ((n = n->next) == NULL || n->type != ROFFT_TEXT)
1595 1612 return 0;
1596 1613
1597 1614 /*
1598 1615 * Strip away the enclosing angle brackets and make sure we're
1599 1616 * not zero-length.
1600 1617 */
1601 1618
1602 1619 start = n->string;
1603 1620 if ('<' == *start || '"' == *start)
1604 1621 start++;
1605 1622
1606 1623 if (0 == (sz = strlen(start)))
1607 1624 return 0;
1608 1625
1609 1626 end = &start[(int)sz - 1];
1610 1627 if ('>' == *end || '"' == *end)
1611 1628 end--;
1612 1629
1613 1630 if (end > start)
1614 1631 putkeys(mpage, start, end - start + 1, TYPE_In);
1615 1632 return 0;
1616 1633 }
1617 1634
1618 1635 static void
1619 1636 parse_mdoc_fname(struct mpage *mpage, const struct roff_node *n)
1620 1637 {
1621 1638 char *cp;
1622 1639 size_t sz;
1623 1640
1624 1641 if (n->type != ROFFT_TEXT)
1625 1642 return;
1626 1643
1627 1644 /* Skip function pointer punctuation. */
1628 1645
1629 1646 cp = n->string;
1630 1647 while (*cp == '(' || *cp == '*')
1631 1648 cp++;
1632 1649 sz = strcspn(cp, "()");
↓ open down ↓ |
48 lines elided |
↑ open up ↑ |
1633 1650
1634 1651 putkeys(mpage, cp, sz, TYPE_Fn);
1635 1652 if (n->sec == SEC_SYNOPSIS)
1636 1653 putkeys(mpage, cp, sz, NAME_SYN);
1637 1654 }
1638 1655
1639 1656 static int
1640 1657 parse_mdoc_Fn(struct mpage *mpage, const struct roff_meta *meta,
1641 1658 const struct roff_node *n)
1642 1659 {
1660 + uint64_t mask;
1643 1661
1644 1662 if (n->child == NULL)
1645 1663 return 0;
1646 1664
1647 1665 parse_mdoc_fname(mpage, n->child);
1648 1666
1649 - for (n = n->child->next; n != NULL; n = n->next)
1650 - if (n->type == ROFFT_TEXT)
1651 - putkey(mpage, n->string, TYPE_Fa);
1667 + n = n->child->next;
1668 + if (n != NULL && n->type == ROFFT_TEXT) {
1669 + mask = TYPE_Fa;
1670 + if (n->sec == SEC_SYNOPSIS)
1671 + mask |= TYPE_Vt;
1672 + putmdockey(mpage, n, mask, 0);
1673 + }
1652 1674
1653 1675 return 0;
1654 1676 }
1655 1677
1656 1678 static int
1657 1679 parse_mdoc_Fo(struct mpage *mpage, const struct roff_meta *meta,
1658 1680 const struct roff_node *n)
1659 1681 {
1660 1682
1661 1683 if (n->type != ROFFT_HEAD)
1662 1684 return 1;
1663 1685
1664 1686 if (n->child != NULL)
1665 1687 parse_mdoc_fname(mpage, n->child);
1666 1688
1667 1689 return 0;
1668 1690 }
1669 1691
1670 1692 static int
1671 1693 parse_mdoc_Va(struct mpage *mpage, const struct roff_meta *meta,
1672 1694 const struct roff_node *n)
1673 1695 {
1674 1696 char *cp;
1675 1697
1676 1698 if (n->type != ROFFT_ELEM && n->type != ROFFT_BODY)
1677 1699 return 0;
1678 1700
1679 1701 if (n->child != NULL &&
1680 1702 n->child->next == NULL &&
1681 1703 n->child->type == ROFFT_TEXT)
1682 1704 return 1;
1683 1705
1684 1706 cp = NULL;
1685 1707 deroff(&cp, n);
1686 1708 if (cp != NULL) {
1687 1709 putkey(mpage, cp, TYPE_Vt | (n->tok == MDOC_Va ||
1688 1710 n->type == ROFFT_BODY ? TYPE_Va : 0));
1689 1711 free(cp);
1690 1712 }
1691 1713
1692 1714 return 0;
1693 1715 }
1694 1716
1695 1717 static int
1696 1718 parse_mdoc_Xr(struct mpage *mpage, const struct roff_meta *meta,
1697 1719 const struct roff_node *n)
1698 1720 {
1699 1721 char *cp;
1700 1722
1701 1723 if (NULL == (n = n->child))
1702 1724 return 0;
1703 1725
1704 1726 if (NULL == n->next) {
1705 1727 putkey(mpage, n->string, TYPE_Xr);
1706 1728 return 0;
1707 1729 }
1708 1730
1709 1731 mandoc_asprintf(&cp, "%s(%s)", n->string, n->next->string);
1710 1732 putkey(mpage, cp, TYPE_Xr);
1711 1733 free(cp);
1712 1734 return 0;
1713 1735 }
1714 1736
1715 1737 static int
1716 1738 parse_mdoc_Nd(struct mpage *mpage, const struct roff_meta *meta,
1717 1739 const struct roff_node *n)
1718 1740 {
1719 1741
1720 1742 if (n->type == ROFFT_BODY)
1721 1743 deroff(&mpage->desc, n);
1722 1744 return 0;
1723 1745 }
1724 1746
1725 1747 static int
1726 1748 parse_mdoc_Nm(struct mpage *mpage, const struct roff_meta *meta,
1727 1749 const struct roff_node *n)
1728 1750 {
1729 1751
1730 1752 if (SEC_NAME == n->sec)
1731 1753 putmdockey(mpage, n->child, NAME_TITLE, 0);
1732 1754 else if (n->sec == SEC_SYNOPSIS && n->type == ROFFT_HEAD) {
1733 1755 if (n->child == NULL)
1734 1756 putkey(mpage, meta->name, NAME_SYN);
1735 1757 else
1736 1758 putmdockey(mpage, n->child, NAME_SYN, 0);
1737 1759 }
1738 1760 if ( ! (mpage->name_head_done ||
1739 1761 n->child == NULL || n->child->string == NULL ||
1740 1762 strcasecmp(n->child->string, meta->title))) {
1741 1763 putkey(mpage, n->child->string, NAME_HEAD);
1742 1764 mpage->name_head_done = 1;
1743 1765 }
1744 1766 return 0;
1745 1767 }
1746 1768
1747 1769 static int
1748 1770 parse_mdoc_Sh(struct mpage *mpage, const struct roff_meta *meta,
1749 1771 const struct roff_node *n)
1750 1772 {
1751 1773
1752 1774 return n->sec == SEC_CUSTOM && n->type == ROFFT_HEAD;
1753 1775 }
1754 1776
1755 1777 static int
1756 1778 parse_mdoc_head(struct mpage *mpage, const struct roff_meta *meta,
1757 1779 const struct roff_node *n)
1758 1780 {
1759 1781
1760 1782 return n->type == ROFFT_HEAD;
1761 1783 }
1762 1784
1763 1785 /*
1764 1786 * Add a string to the hash table for the current manual.
1765 1787 * Each string has a bitmask telling which macros it belongs to.
1766 1788 * When we finish the manual, we'll dump the table.
1767 1789 */
1768 1790 static void
1769 1791 putkeys(const struct mpage *mpage, char *cp, size_t sz, uint64_t v)
1770 1792 {
1771 1793 struct ohash *htab;
1772 1794 struct str *s;
1773 1795 const char *end;
1774 1796 unsigned int slot;
1775 1797 int i, mustfree;
1776 1798
1777 1799 if (0 == sz)
1778 1800 return;
1779 1801
1780 1802 mustfree = render_string(&cp, &sz);
1781 1803
1782 1804 if (TYPE_Nm & v) {
1783 1805 htab = &names;
1784 1806 v &= name_mask;
1785 1807 if (v & NAME_FIRST)
1786 1808 name_mask &= ~NAME_FIRST;
1787 1809 if (debug > 1)
1788 1810 say(mpage->mlinks->file,
1789 1811 "Adding name %*s, bits=0x%llx", (int)sz, cp,
1790 1812 (unsigned long long)v);
1791 1813 } else {
1792 1814 htab = &strings;
1793 1815 if (debug > 1)
1794 1816 for (i = 0; i < KEY_MAX; i++)
1795 1817 if ((uint64_t)1 << i & v)
1796 1818 say(mpage->mlinks->file,
1797 1819 "Adding key %s=%*s",
1798 1820 mansearch_keynames[i], (int)sz, cp);
1799 1821 }
1800 1822
1801 1823 end = cp + sz;
1802 1824 slot = ohash_qlookupi(htab, cp, &end);
1803 1825 s = ohash_find(htab, slot);
1804 1826
1805 1827 if (NULL != s && mpage == s->mpage) {
1806 1828 s->mask |= v;
1807 1829 return;
1808 1830 } else if (NULL == s) {
1809 1831 s = mandoc_calloc(1, sizeof(struct str) + sz + 1);
1810 1832 memcpy(s->key, cp, sz);
1811 1833 ohash_insert(htab, slot, s);
1812 1834 }
1813 1835 s->mpage = mpage;
1814 1836 s->mask = v;
1815 1837
1816 1838 if (mustfree)
1817 1839 free(cp);
1818 1840 }
1819 1841
1820 1842 /*
1821 1843 * Take a Unicode codepoint and produce its UTF-8 encoding.
1822 1844 * This isn't the best way to do this, but it works.
1823 1845 * The magic numbers are from the UTF-8 packaging.
1824 1846 * They're not as scary as they seem: read the UTF-8 spec for details.
1825 1847 */
1826 1848 static size_t
1827 1849 utf8(unsigned int cp, char out[7])
1828 1850 {
1829 1851 size_t rc;
1830 1852
1831 1853 rc = 0;
1832 1854 if (cp <= 0x0000007F) {
1833 1855 rc = 1;
1834 1856 out[0] = (char)cp;
1835 1857 } else if (cp <= 0x000007FF) {
1836 1858 rc = 2;
1837 1859 out[0] = (cp >> 6 & 31) | 192;
1838 1860 out[1] = (cp & 63) | 128;
1839 1861 } else if (cp <= 0x0000FFFF) {
1840 1862 rc = 3;
1841 1863 out[0] = (cp >> 12 & 15) | 224;
1842 1864 out[1] = (cp >> 6 & 63) | 128;
1843 1865 out[2] = (cp & 63) | 128;
1844 1866 } else if (cp <= 0x001FFFFF) {
1845 1867 rc = 4;
1846 1868 out[0] = (cp >> 18 & 7) | 240;
1847 1869 out[1] = (cp >> 12 & 63) | 128;
1848 1870 out[2] = (cp >> 6 & 63) | 128;
1849 1871 out[3] = (cp & 63) | 128;
1850 1872 } else if (cp <= 0x03FFFFFF) {
1851 1873 rc = 5;
1852 1874 out[0] = (cp >> 24 & 3) | 248;
1853 1875 out[1] = (cp >> 18 & 63) | 128;
1854 1876 out[2] = (cp >> 12 & 63) | 128;
1855 1877 out[3] = (cp >> 6 & 63) | 128;
1856 1878 out[4] = (cp & 63) | 128;
1857 1879 } else if (cp <= 0x7FFFFFFF) {
1858 1880 rc = 6;
1859 1881 out[0] = (cp >> 30 & 1) | 252;
1860 1882 out[1] = (cp >> 24 & 63) | 128;
1861 1883 out[2] = (cp >> 18 & 63) | 128;
1862 1884 out[3] = (cp >> 12 & 63) | 128;
1863 1885 out[4] = (cp >> 6 & 63) | 128;
1864 1886 out[5] = (cp & 63) | 128;
1865 1887 } else
1866 1888 return 0;
1867 1889
1868 1890 out[rc] = '\0';
1869 1891 return rc;
1870 1892 }
1871 1893
1872 1894 /*
1873 1895 * If the string contains escape sequences,
1874 1896 * replace it with an allocated rendering and return 1,
1875 1897 * such that the caller can free it after use.
1876 1898 * Otherwise, do nothing and return 0.
1877 1899 */
1878 1900 static int
1879 1901 render_string(char **public, size_t *psz)
1880 1902 {
1881 1903 const char *src, *scp, *addcp, *seq;
1882 1904 char *dst;
1883 1905 size_t ssz, dsz, addsz;
1884 1906 char utfbuf[7], res[6];
1885 1907 int seqlen, unicode;
1886 1908
1887 1909 res[0] = '\\';
1888 1910 res[1] = '\t';
1889 1911 res[2] = ASCII_NBRSP;
1890 1912 res[3] = ASCII_HYPH;
1891 1913 res[4] = ASCII_BREAK;
1892 1914 res[5] = '\0';
1893 1915
1894 1916 src = scp = *public;
1895 1917 ssz = *psz;
1896 1918 dst = NULL;
1897 1919 dsz = 0;
1898 1920
1899 1921 while (scp < src + *psz) {
1900 1922
1901 1923 /* Leave normal characters unchanged. */
1902 1924
1903 1925 if (strchr(res, *scp) == NULL) {
1904 1926 if (dst != NULL)
1905 1927 dst[dsz++] = *scp;
1906 1928 scp++;
1907 1929 continue;
1908 1930 }
1909 1931
1910 1932 /*
1911 1933 * Found something that requires replacing,
1912 1934 * make sure we have a destination buffer.
1913 1935 */
1914 1936
1915 1937 if (dst == NULL) {
1916 1938 dst = mandoc_malloc(ssz + 1);
1917 1939 dsz = scp - src;
1918 1940 memcpy(dst, src, dsz);
1919 1941 }
1920 1942
1921 1943 /* Handle single-char special characters. */
1922 1944
1923 1945 switch (*scp) {
1924 1946 case '\\':
1925 1947 break;
1926 1948 case '\t':
1927 1949 case ASCII_NBRSP:
1928 1950 dst[dsz++] = ' ';
1929 1951 scp++;
1930 1952 continue;
1931 1953 case ASCII_HYPH:
1932 1954 dst[dsz++] = '-';
1933 1955 /* FALLTHROUGH */
1934 1956 case ASCII_BREAK:
1935 1957 scp++;
1936 1958 continue;
1937 1959 default:
1938 1960 abort();
1939 1961 }
1940 1962
1941 1963 /*
1942 1964 * Found an escape sequence.
1943 1965 * Read past the slash, then parse it.
1944 1966 * Ignore everything except characters.
1945 1967 */
1946 1968
1947 1969 scp++;
1948 1970 if (mandoc_escape(&scp, &seq, &seqlen) != ESCAPE_SPECIAL)
1949 1971 continue;
1950 1972
1951 1973 /*
1952 1974 * Render the special character
1953 1975 * as either UTF-8 or ASCII.
1954 1976 */
1955 1977
1956 1978 if (write_utf8) {
1957 1979 unicode = mchars_spec2cp(seq, seqlen);
1958 1980 if (unicode <= 0)
1959 1981 continue;
1960 1982 addsz = utf8(unicode, utfbuf);
1961 1983 if (addsz == 0)
1962 1984 continue;
1963 1985 addcp = utfbuf;
1964 1986 } else {
1965 1987 addcp = mchars_spec2str(seq, seqlen, &addsz);
1966 1988 if (addcp == NULL)
1967 1989 continue;
1968 1990 if (*addcp == ASCII_NBRSP) {
1969 1991 addcp = " ";
1970 1992 addsz = 1;
1971 1993 }
1972 1994 }
1973 1995
1974 1996 /* Copy the rendered glyph into the stream. */
1975 1997
1976 1998 ssz += addsz;
1977 1999 dst = mandoc_realloc(dst, ssz + 1);
1978 2000 memcpy(dst + dsz, addcp, addsz);
1979 2001 dsz += addsz;
1980 2002 }
1981 2003 if (dst != NULL) {
1982 2004 *public = dst;
1983 2005 *psz = dsz;
1984 2006 }
1985 2007
1986 2008 /* Trim trailing whitespace and NUL-terminate. */
1987 2009
1988 2010 while (*psz > 0 && (*public)[*psz - 1] == ' ')
1989 2011 --*psz;
1990 2012 if (dst != NULL) {
1991 2013 (*public)[*psz] = '\0';
1992 2014 return 1;
1993 2015 } else
1994 2016 return 0;
1995 2017 }
1996 2018
1997 2019 static void
1998 2020 dbadd_mlink(const struct mlink *mlink)
1999 2021 {
2000 2022 dba_page_alias(mlink->mpage->dba, mlink->name, NAME_FILE);
2001 2023 dba_page_add(mlink->mpage->dba, DBP_SECT, mlink->dsec);
2002 2024 dba_page_add(mlink->mpage->dba, DBP_SECT, mlink->fsec);
2003 2025 dba_page_add(mlink->mpage->dba, DBP_ARCH, mlink->arch);
2004 2026 dba_page_add(mlink->mpage->dba, DBP_FILE, mlink->file);
2005 2027 }
2006 2028
2007 2029 /*
2008 2030 * Flush the current page's terms (and their bits) into the database.
2009 2031 * Also, handle escape sequences at the last possible moment.
2010 2032 */
2011 2033 static void
2012 2034 dbadd(struct dba *dba, struct mpage *mpage)
2013 2035 {
2014 2036 struct mlink *mlink;
2015 2037 struct str *key;
2016 2038 char *cp;
2017 2039 uint64_t mask;
2018 2040 size_t i;
2019 2041 unsigned int slot;
2020 2042 int mustfree;
2021 2043
2022 2044 mlink = mpage->mlinks;
2023 2045
2024 2046 if (nodb) {
2025 2047 for (key = ohash_first(&names, &slot); NULL != key;
2026 2048 key = ohash_next(&names, &slot))
2027 2049 free(key);
2028 2050 for (key = ohash_first(&strings, &slot); NULL != key;
2029 2051 key = ohash_next(&strings, &slot))
2030 2052 free(key);
2031 2053 if (0 == debug)
2032 2054 return;
2033 2055 while (NULL != mlink) {
2034 2056 fputs(mlink->name, stdout);
2035 2057 if (NULL == mlink->next ||
2036 2058 strcmp(mlink->dsec, mlink->next->dsec) ||
2037 2059 strcmp(mlink->fsec, mlink->next->fsec) ||
2038 2060 strcmp(mlink->arch, mlink->next->arch)) {
2039 2061 putchar('(');
2040 2062 if ('\0' == *mlink->dsec)
2041 2063 fputs(mlink->fsec, stdout);
2042 2064 else
2043 2065 fputs(mlink->dsec, stdout);
2044 2066 if ('\0' != *mlink->arch)
2045 2067 printf("/%s", mlink->arch);
2046 2068 putchar(')');
2047 2069 }
2048 2070 mlink = mlink->next;
2049 2071 if (NULL != mlink)
2050 2072 fputs(", ", stdout);
2051 2073 }
2052 2074 printf(" - %s\n", mpage->desc);
2053 2075 return;
2054 2076 }
2055 2077
2056 2078 if (debug)
2057 2079 say(mlink->file, "Adding to database");
2058 2080
2059 2081 cp = mpage->desc;
2060 2082 i = strlen(cp);
2061 2083 mustfree = render_string(&cp, &i);
2062 2084 mpage->dba = dba_page_new(dba->pages,
2063 2085 *mpage->arch == '\0' ? mlink->arch : mpage->arch,
2064 2086 cp, mlink->file, mpage->form);
2065 2087 if (mustfree)
2066 2088 free(cp);
2067 2089 dba_page_add(mpage->dba, DBP_SECT, mpage->sec);
2068 2090
2069 2091 while (mlink != NULL) {
2070 2092 dbadd_mlink(mlink);
2071 2093 mlink = mlink->next;
2072 2094 }
2073 2095
2074 2096 for (key = ohash_first(&names, &slot); NULL != key;
2075 2097 key = ohash_next(&names, &slot)) {
2076 2098 assert(key->mpage == mpage);
2077 2099 dba_page_alias(mpage->dba, key->key, key->mask);
2078 2100 free(key);
2079 2101 }
2080 2102 for (key = ohash_first(&strings, &slot); NULL != key;
2081 2103 key = ohash_next(&strings, &slot)) {
2082 2104 assert(key->mpage == mpage);
2083 2105 i = 0;
2084 2106 for (mask = TYPE_Xr; mask <= TYPE_Lb; mask *= 2) {
2085 2107 if (key->mask & mask)
2086 2108 dba_macro_add(dba->macros, i,
2087 2109 key->key, mpage->dba);
2088 2110 i++;
2089 2111 }
2090 2112 free(key);
2091 2113 }
2092 2114 }
2093 2115
2094 2116 static void
2095 2117 dbprune(struct dba *dba)
2096 2118 {
2097 2119 struct dba_array *page, *files;
2098 2120 char *file;
2099 2121
2100 2122 dba_array_FOREACH(dba->pages, page) {
2101 2123 files = dba_array_get(page, DBP_FILE);
2102 2124 dba_array_FOREACH(files, file) {
2103 2125 if (*file < ' ')
2104 2126 file++;
2105 2127 if (ohash_find(&mlinks, ohash_qlookup(&mlinks,
2106 2128 file)) != NULL) {
2107 2129 if (debug)
2108 2130 say(file, "Deleting from database");
2109 2131 dba_array_del(dba->pages);
2110 2132 break;
2111 2133 }
↓ open down ↓ |
450 lines elided |
↑ open up ↑ |
2112 2134 }
2113 2135 }
2114 2136 }
2115 2137
2116 2138 /*
2117 2139 * Write the database from memory to disk.
2118 2140 */
2119 2141 static void
2120 2142 dbwrite(struct dba *dba)
2121 2143 {
2122 - char tfn[32];
2123 - int status;
2124 - pid_t child;
2144 + struct stat sb1, sb2;
2145 + char tfn[33], *cp1, *cp2;
2146 + off_t i;
2147 + int fd1, fd2;
2125 2148
2126 2149 /*
2127 2150 * Do not write empty databases, and delete existing ones
2128 2151 * when makewhatis -u causes them to become empty.
2129 2152 */
2130 2153
2131 2154 dba_array_start(dba->pages);
2132 2155 if (dba_array_next(dba->pages) == NULL) {
2133 2156 if (unlink(MANDOC_DB) == -1 && errno != ENOENT)
2134 2157 say(MANDOC_DB, "&unlink");
2135 2158 return;
2136 2159 }
2137 2160
2138 2161 /*
2139 2162 * Build the database in a temporary file,
2140 2163 * then atomically move it into place.
2141 2164 */
2142 2165
2143 2166 if (dba_write(MANDOC_DB "~", dba) != -1) {
2144 2167 if (rename(MANDOC_DB "~", MANDOC_DB) == -1) {
2145 2168 exitcode = (int)MANDOCLEVEL_SYSERR;
2146 2169 say(MANDOC_DB, "&rename");
2147 2170 unlink(MANDOC_DB "~");
2148 2171 }
2149 2172 return;
2150 2173 }
2151 2174
2152 2175 /*
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
2153 2176 * We lack write permission and cannot replace the database
2154 2177 * file, but let's at least check whether the data changed.
2155 2178 */
2156 2179
2157 2180 (void)strlcpy(tfn, "/tmp/mandocdb.XXXXXXXX", sizeof(tfn));
2158 2181 if (mkdtemp(tfn) == NULL) {
2159 2182 exitcode = (int)MANDOCLEVEL_SYSERR;
2160 2183 say("", "&%s", tfn);
2161 2184 return;
2162 2185 }
2163 -
2186 + cp1 = cp2 = MAP_FAILED;
2187 + fd1 = fd2 = -1;
2164 2188 (void)strlcat(tfn, "/" MANDOC_DB, sizeof(tfn));
2165 2189 if (dba_write(tfn, dba) == -1) {
2166 - exitcode = (int)MANDOCLEVEL_SYSERR;
2167 2190 say(tfn, "&dba_write");
2168 - goto out;
2191 + goto err;
2169 2192 }
2170 -
2171 - switch (child = fork()) {
2172 - case -1:
2173 - exitcode = (int)MANDOCLEVEL_SYSERR;
2174 - say("", "&fork cmp");
2175 - return;
2176 - case 0:
2177 - execlp("cmp", "cmp", "-s", tfn, MANDOC_DB, (char *)NULL);
2178 - say("", "&exec cmp");
2179 - exit(0);
2180 - default:
2181 - break;
2193 + if ((fd1 = open(MANDOC_DB, O_RDONLY, 0)) == -1) {
2194 + say(MANDOC_DB, "&open");
2195 + goto err;
2182 2196 }
2183 - if (waitpid(child, &status, 0) == -1) {
2184 - exitcode = (int)MANDOCLEVEL_SYSERR;
2185 - say("", "&wait cmp");
2186 - } else if (WIFSIGNALED(status)) {
2187 - exitcode = (int)MANDOCLEVEL_SYSERR;
2188 - say("", "cmp died from signal %d", WTERMSIG(status));
2189 - } else if (WEXITSTATUS(status)) {
2190 - exitcode = (int)MANDOCLEVEL_SYSERR;
2191 - say(MANDOC_DB,
2192 - "Data changed, but cannot replace database");
2197 + if ((fd2 = open(tfn, O_RDONLY, 0)) == -1) {
2198 + say(tfn, "&open");
2199 + goto err;
2193 2200 }
2201 + if (fstat(fd1, &sb1) == -1) {
2202 + say(MANDOC_DB, "&fstat");
2203 + goto err;
2204 + }
2205 + if (fstat(fd2, &sb2) == -1) {
2206 + say(tfn, "&fstat");
2207 + goto err;
2208 + }
2209 + if (sb1.st_size != sb2.st_size)
2210 + goto err;
2211 + if ((cp1 = mmap(NULL, sb1.st_size, PROT_READ, MAP_PRIVATE,
2212 + fd1, 0)) == MAP_FAILED) {
2213 + say(MANDOC_DB, "&mmap");
2214 + goto err;
2215 + }
2216 + if ((cp2 = mmap(NULL, sb2.st_size, PROT_READ, MAP_PRIVATE,
2217 + fd2, 0)) == MAP_FAILED) {
2218 + say(tfn, "&mmap");
2219 + goto err;
2220 + }
2221 + for (i = 0; i < sb1.st_size; i++)
2222 + if (cp1[i] != cp2[i])
2223 + goto err;
2224 + goto out;
2194 2225
2226 +err:
2227 + exitcode = (int)MANDOCLEVEL_SYSERR;
2228 + say(MANDOC_DB, "Data changed, but cannot replace database");
2229 +
2195 2230 out:
2231 + if (cp1 != MAP_FAILED)
2232 + munmap(cp1, sb1.st_size);
2233 + if (cp2 != MAP_FAILED)
2234 + munmap(cp2, sb2.st_size);
2235 + if (fd1 != -1)
2236 + close(fd1);
2237 + if (fd2 != -1)
2238 + close(fd2);
2239 + unlink(tfn);
2196 2240 *strrchr(tfn, '/') = '\0';
2197 - switch (child = fork()) {
2198 - case -1:
2199 - exitcode = (int)MANDOCLEVEL_SYSERR;
2200 - say("", "&fork rm");
2201 - return;
2202 - case 0:
2203 - execlp("rm", "rm", "-rf", tfn, (char *)NULL);
2204 - say("", "&exec rm");
2205 - exit((int)MANDOCLEVEL_SYSERR);
2206 - default:
2207 - break;
2208 - }
2209 - if (waitpid(child, &status, 0) == -1) {
2210 - exitcode = (int)MANDOCLEVEL_SYSERR;
2211 - say("", "&wait rm");
2212 - } else if (WIFSIGNALED(status) || WEXITSTATUS(status)) {
2213 - exitcode = (int)MANDOCLEVEL_SYSERR;
2214 - say("", "%s: Cannot remove temporary directory", tfn);
2215 - }
2241 + rmdir(tfn);
2216 2242 }
2217 2243
2218 2244 static int
2219 2245 set_basedir(const char *targetdir, int report_baddir)
2220 2246 {
2221 2247 static char startdir[PATH_MAX];
2222 2248 static int getcwd_status; /* 1 = ok, 2 = failure */
2223 2249 static int chdir_status; /* 1 = changed directory */
2224 2250 char *cp;
2225 2251
2226 2252 /*
2227 2253 * Remember the original working directory, if possible.
2228 2254 * This will be needed if the second or a later directory
2229 2255 * on the command line is given as a relative path.
2230 2256 * Do not error out if the current directory is not
2231 2257 * searchable: Maybe it won't be needed after all.
2232 2258 */
2233 2259 if (0 == getcwd_status) {
2234 2260 if (NULL == getcwd(startdir, sizeof(startdir))) {
2235 2261 getcwd_status = 2;
2236 2262 (void)strlcpy(startdir, strerror(errno),
2237 2263 sizeof(startdir));
2238 2264 } else
2239 2265 getcwd_status = 1;
2240 2266 }
2241 2267
2242 2268 /*
2243 2269 * We are leaving the old base directory.
2244 2270 * Do not use it any longer, not even for messages.
2245 2271 */
2246 2272 *basedir = '\0';
2247 2273
2248 2274 /*
2249 2275 * If and only if the directory was changed earlier and
2250 2276 * the next directory to process is given as a relative path,
2251 2277 * first go back, or bail out if that is impossible.
2252 2278 */
2253 2279 if (chdir_status && '/' != *targetdir) {
2254 2280 if (2 == getcwd_status) {
2255 2281 exitcode = (int)MANDOCLEVEL_SYSERR;
2256 2282 say("", "getcwd: %s", startdir);
2257 2283 return 0;
2258 2284 }
2259 2285 if (-1 == chdir(startdir)) {
2260 2286 exitcode = (int)MANDOCLEVEL_SYSERR;
2261 2287 say("", "&chdir %s", startdir);
2262 2288 return 0;
2263 2289 }
2264 2290 }
2265 2291
2266 2292 /*
2267 2293 * Always resolve basedir to the canonicalized absolute
2268 2294 * pathname and append a trailing slash, such that
2269 2295 * we can reliably check whether files are inside.
2270 2296 */
2271 2297 if (NULL == realpath(targetdir, basedir)) {
2272 2298 if (report_baddir || errno != ENOENT) {
2273 2299 exitcode = (int)MANDOCLEVEL_BADARG;
2274 2300 say("", "&%s: realpath", targetdir);
2275 2301 }
2276 2302 return 0;
2277 2303 } else if (-1 == chdir(basedir)) {
2278 2304 if (report_baddir || errno != ENOENT) {
2279 2305 exitcode = (int)MANDOCLEVEL_BADARG;
2280 2306 say("", "&chdir");
2281 2307 }
2282 2308 return 0;
2283 2309 }
2284 2310 chdir_status = 1;
2285 2311 cp = strchr(basedir, '\0');
2286 2312 if ('/' != cp[-1]) {
2287 2313 if (cp - basedir >= PATH_MAX - 1) {
2288 2314 exitcode = (int)MANDOCLEVEL_SYSERR;
2289 2315 say("", "Filename too long");
2290 2316 return 0;
2291 2317 }
2292 2318 *cp++ = '/';
2293 2319 *cp = '\0';
2294 2320 }
2295 2321 return 1;
2296 2322 }
2297 2323
2298 2324 static void
2299 2325 say(const char *file, const char *format, ...)
2300 2326 {
2301 2327 va_list ap;
2302 2328 int use_errno;
2303 2329
2304 2330 if ('\0' != *basedir)
2305 2331 fprintf(stderr, "%s", basedir);
2306 2332 if ('\0' != *basedir && '\0' != *file)
2307 2333 fputc('/', stderr);
2308 2334 if ('\0' != *file)
2309 2335 fprintf(stderr, "%s", file);
2310 2336
2311 2337 use_errno = 1;
2312 2338 if (NULL != format) {
2313 2339 switch (*format) {
2314 2340 case '&':
2315 2341 format++;
2316 2342 break;
2317 2343 case '\0':
2318 2344 format = NULL;
2319 2345 break;
2320 2346 default:
2321 2347 use_errno = 0;
2322 2348 break;
2323 2349 }
2324 2350 }
2325 2351 if (NULL != format) {
2326 2352 if ('\0' != *basedir || '\0' != *file)
2327 2353 fputs(": ", stderr);
2328 2354 va_start(ap, format);
2329 2355 vfprintf(stderr, format, ap);
2330 2356 va_end(ap);
2331 2357 }
2332 2358 if (use_errno) {
2333 2359 if ('\0' != *basedir || '\0' != *file || NULL != format)
2334 2360 fputs(": ", stderr);
2335 2361 perror(NULL);
2336 2362 } else
2337 2363 fputc('\n', stderr);
2338 2364 }
↓ open down ↓ |
113 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX