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