]>
git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
1 /* $Id: mandocdb.c,v 1.213 2016/01/08 02:53:13 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011-2016 Ingo Schwarze <schwarze@openbsd.org>
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.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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.
20 #include <sys/types.h>
34 #include "compat_fts.h"
47 #include "mandoc_aux.h"
48 #include "mandoc_ohash.h"
54 #include "mansearch.h"
56 extern int mansearch_keymax
;
57 extern const char *const mansearch_keynames
[];
59 #define SQL_EXEC(_v) \
60 if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
61 say("", "%s: %s", (_v), sqlite3_errmsg(db))
62 #define SQL_BIND_TEXT(_s, _i, _v) \
63 if (SQLITE_OK != sqlite3_bind_text \
64 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
65 say(mlink->file, "%s", sqlite3_errmsg(db))
66 #define SQL_BIND_INT(_s, _i, _v) \
67 if (SQLITE_OK != sqlite3_bind_int \
68 ((_s), (_i)++, (_v))) \
69 say(mlink->file, "%s", sqlite3_errmsg(db))
70 #define SQL_BIND_INT64(_s, _i, _v) \
71 if (SQLITE_OK != sqlite3_bind_int64 \
72 ((_s), (_i)++, (_v))) \
73 say(mlink->file, "%s", sqlite3_errmsg(db))
74 #define SQL_STEP(_s) \
75 if (SQLITE_DONE != sqlite3_step((_s))) \
76 say(mlink->file, "%s", sqlite3_errmsg(db))
79 OP_DEFAULT
= 0, /* new dbs from dir list or default config */
80 OP_CONFFILE
, /* new databases from custom config file */
81 OP_UPDATE
, /* delete/add entries in existing database */
82 OP_DELETE
, /* delete entries from existing database */
83 OP_TEST
/* change no databases, report potential problems */
87 const struct mpage
*mpage
; /* if set, the owning parse */
88 uint64_t mask
; /* bitmask in sequence */
89 char key
[]; /* rendered text */
98 struct inodev inodev
; /* used for hashing routine */
99 int64_t pageid
; /* pageid in mpages SQL table */
100 char *sec
; /* section from file content */
101 char *arch
; /* architecture from file content */
102 char *title
; /* title from file content */
103 char *desc
; /* description from file content */
104 struct mlink
*mlinks
; /* singly linked list */
105 int form
; /* format from file content */
110 char file
[PATH_MAX
]; /* filename rel. to manpath */
111 char *dsec
; /* section from directory */
112 char *arch
; /* architecture from directory */
113 char *name
; /* name from file name (not empty) */
114 char *fsec
; /* section from file name suffix */
115 struct mlink
*next
; /* singly linked list */
116 struct mpage
*mpage
; /* parent */
117 int dform
; /* format from directory */
118 int fform
; /* format from file name suffix */
119 int gzip
; /* filename has a .gz suffix */
123 STMT_DELETE_PAGE
= 0, /* delete mpage */
124 STMT_INSERT_PAGE
, /* insert mpage */
125 STMT_INSERT_LINK
, /* insert mlink */
126 STMT_INSERT_NAME
, /* insert name */
127 STMT_SELECT_NAME
, /* retrieve existing name flags */
128 STMT_INSERT_KEY
, /* insert parsed key */
132 typedef int (*mdoc_fp
)(struct mpage
*, const struct roff_meta
*,
133 const struct roff_node
*);
135 struct mdoc_handler
{
136 mdoc_fp fp
; /* optional handler */
137 uint64_t mask
; /* set unless handler returns 0 */
140 static void dbclose(int);
141 static void dbadd(struct mpage
*);
142 static void dbadd_mlink(const struct mlink
*mlink
);
143 static void dbadd_mlink_name(const struct mlink
*mlink
);
144 static int dbopen(int);
145 static void dbprune(void);
146 static void filescan(const char *);
147 static void mlink_add(struct mlink
*, const struct stat
*);
148 static void mlink_check(struct mpage
*, struct mlink
*);
149 static void mlink_free(struct mlink
*);
150 static void mlinks_undupe(struct mpage
*);
151 static void mpages_free(void);
152 static void mpages_merge(struct mparse
*);
153 static void names_check(void);
154 static void parse_cat(struct mpage
*, int);
155 static void parse_man(struct mpage
*, const struct roff_meta
*,
156 const struct roff_node
*);
157 static void parse_mdoc(struct mpage
*, const struct roff_meta
*,
158 const struct roff_node
*);
159 static int parse_mdoc_head(struct mpage
*, const struct roff_meta
*,
160 const struct roff_node
*);
161 static int parse_mdoc_Fd(struct mpage
*, const struct roff_meta
*,
162 const struct roff_node
*);
163 static void parse_mdoc_fname(struct mpage
*, const struct roff_node
*);
164 static int parse_mdoc_Fn(struct mpage
*, const struct roff_meta
*,
165 const struct roff_node
*);
166 static int parse_mdoc_Fo(struct mpage
*, const struct roff_meta
*,
167 const struct roff_node
*);
168 static int parse_mdoc_Nd(struct mpage
*, const struct roff_meta
*,
169 const struct roff_node
*);
170 static int parse_mdoc_Nm(struct mpage
*, const struct roff_meta
*,
171 const struct roff_node
*);
172 static int parse_mdoc_Sh(struct mpage
*, const struct roff_meta
*,
173 const struct roff_node
*);
174 static int parse_mdoc_Va(struct mpage
*, const struct roff_meta
*,
175 const struct roff_node
*);
176 static int parse_mdoc_Xr(struct mpage
*, const struct roff_meta
*,
177 const struct roff_node
*);
178 static void putkey(const struct mpage
*, char *, uint64_t);
179 static void putkeys(const struct mpage
*, char *, size_t, uint64_t);
180 static void putmdockey(const struct mpage
*,
181 const struct roff_node
*, uint64_t);
182 static int render_string(char **, size_t *);
183 static void say(const char *, const char *, ...);
184 static int set_basedir(const char *, int);
185 static int treescan(void);
186 static size_t utf8(unsigned int, char [7]);
188 static char tempfilename
[32];
189 static int nodb
; /* no database changes */
190 static int mparse_options
; /* abort the parse early */
191 static int use_all
; /* use all found files */
192 static int debug
; /* print what we're doing */
193 static int warnings
; /* warn about crap */
194 static int write_utf8
; /* write UTF-8 output; else ASCII */
195 static int exitcode
; /* to be returned by main */
196 static enum op op
; /* operational mode */
197 static char basedir
[PATH_MAX
]; /* current base directory */
198 static struct ohash mpages
; /* table of distinct manual pages */
199 static struct ohash mlinks
; /* table of directory entries */
200 static struct ohash names
; /* table of all names */
201 static struct ohash strings
; /* table of all strings */
202 static sqlite3
*db
= NULL
; /* current database */
203 static sqlite3_stmt
*stmts
[STMT__MAX
]; /* current statements */
204 static uint64_t name_mask
;
206 static const struct mdoc_handler mdocs
[MDOC_MAX
] = {
207 { NULL
, 0 }, /* Ap */
208 { NULL
, 0 }, /* Dd */
209 { NULL
, 0 }, /* Dt */
210 { NULL
, 0 }, /* Os */
211 { parse_mdoc_Sh
, TYPE_Sh
}, /* Sh */
212 { parse_mdoc_head
, TYPE_Ss
}, /* Ss */
213 { NULL
, 0 }, /* Pp */
214 { NULL
, 0 }, /* D1 */
215 { NULL
, 0 }, /* Dl */
216 { NULL
, 0 }, /* Bd */
217 { NULL
, 0 }, /* Ed */
218 { NULL
, 0 }, /* Bl */
219 { NULL
, 0 }, /* El */
220 { NULL
, 0 }, /* It */
221 { NULL
, 0 }, /* Ad */
222 { NULL
, TYPE_An
}, /* An */
223 { NULL
, TYPE_Ar
}, /* Ar */
224 { NULL
, TYPE_Cd
}, /* Cd */
225 { NULL
, TYPE_Cm
}, /* Cm */
226 { NULL
, TYPE_Dv
}, /* Dv */
227 { NULL
, TYPE_Er
}, /* Er */
228 { NULL
, TYPE_Ev
}, /* Ev */
229 { NULL
, 0 }, /* Ex */
230 { NULL
, TYPE_Fa
}, /* Fa */
231 { parse_mdoc_Fd
, 0 }, /* Fd */
232 { NULL
, TYPE_Fl
}, /* Fl */
233 { parse_mdoc_Fn
, 0 }, /* Fn */
234 { NULL
, TYPE_Ft
}, /* Ft */
235 { NULL
, TYPE_Ic
}, /* Ic */
236 { NULL
, TYPE_In
}, /* In */
237 { NULL
, TYPE_Li
}, /* Li */
238 { parse_mdoc_Nd
, 0 }, /* Nd */
239 { parse_mdoc_Nm
, 0 }, /* Nm */
240 { NULL
, 0 }, /* Op */
241 { NULL
, 0 }, /* Ot */
242 { NULL
, TYPE_Pa
}, /* Pa */
243 { NULL
, 0 }, /* Rv */
244 { NULL
, TYPE_St
}, /* St */
245 { parse_mdoc_Va
, TYPE_Va
}, /* Va */
246 { parse_mdoc_Va
, TYPE_Vt
}, /* Vt */
247 { parse_mdoc_Xr
, 0 }, /* Xr */
248 { NULL
, 0 }, /* %A */
249 { NULL
, 0 }, /* %B */
250 { NULL
, 0 }, /* %D */
251 { NULL
, 0 }, /* %I */
252 { NULL
, 0 }, /* %J */
253 { NULL
, 0 }, /* %N */
254 { NULL
, 0 }, /* %O */
255 { NULL
, 0 }, /* %P */
256 { NULL
, 0 }, /* %R */
257 { NULL
, 0 }, /* %T */
258 { NULL
, 0 }, /* %V */
259 { NULL
, 0 }, /* Ac */
260 { NULL
, 0 }, /* Ao */
261 { NULL
, 0 }, /* Aq */
262 { NULL
, TYPE_At
}, /* At */
263 { NULL
, 0 }, /* Bc */
264 { NULL
, 0 }, /* Bf */
265 { NULL
, 0 }, /* Bo */
266 { NULL
, 0 }, /* Bq */
267 { NULL
, TYPE_Bsx
}, /* Bsx */
268 { NULL
, TYPE_Bx
}, /* Bx */
269 { NULL
, 0 }, /* Db */
270 { NULL
, 0 }, /* Dc */
271 { NULL
, 0 }, /* Do */
272 { NULL
, 0 }, /* Dq */
273 { NULL
, 0 }, /* Ec */
274 { NULL
, 0 }, /* Ef */
275 { NULL
, TYPE_Em
}, /* Em */
276 { NULL
, 0 }, /* Eo */
277 { NULL
, TYPE_Fx
}, /* Fx */
278 { NULL
, TYPE_Ms
}, /* Ms */
279 { NULL
, 0 }, /* No */
280 { NULL
, 0 }, /* Ns */
281 { NULL
, TYPE_Nx
}, /* Nx */
282 { NULL
, TYPE_Ox
}, /* Ox */
283 { NULL
, 0 }, /* Pc */
284 { NULL
, 0 }, /* Pf */
285 { NULL
, 0 }, /* Po */
286 { NULL
, 0 }, /* Pq */
287 { NULL
, 0 }, /* Qc */
288 { NULL
, 0 }, /* Ql */
289 { NULL
, 0 }, /* Qo */
290 { NULL
, 0 }, /* Qq */
291 { NULL
, 0 }, /* Re */
292 { NULL
, 0 }, /* Rs */
293 { NULL
, 0 }, /* Sc */
294 { NULL
, 0 }, /* So */
295 { NULL
, 0 }, /* Sq */
296 { NULL
, 0 }, /* Sm */
297 { NULL
, 0 }, /* Sx */
298 { NULL
, TYPE_Sy
}, /* Sy */
299 { NULL
, TYPE_Tn
}, /* Tn */
300 { NULL
, 0 }, /* Ux */
301 { NULL
, 0 }, /* Xc */
302 { NULL
, 0 }, /* Xo */
303 { parse_mdoc_Fo
, 0 }, /* Fo */
304 { NULL
, 0 }, /* Fc */
305 { NULL
, 0 }, /* Oo */
306 { NULL
, 0 }, /* Oc */
307 { NULL
, 0 }, /* Bk */
308 { NULL
, 0 }, /* Ek */
309 { NULL
, 0 }, /* Bt */
310 { NULL
, 0 }, /* Hf */
311 { NULL
, 0 }, /* Fr */
312 { NULL
, 0 }, /* Ud */
313 { NULL
, TYPE_Lb
}, /* Lb */
314 { NULL
, 0 }, /* Lp */
315 { NULL
, TYPE_Lk
}, /* Lk */
316 { NULL
, TYPE_Mt
}, /* Mt */
317 { NULL
, 0 }, /* Brq */
318 { NULL
, 0 }, /* Bro */
319 { NULL
, 0 }, /* Brc */
320 { NULL
, 0 }, /* %C */
321 { NULL
, 0 }, /* Es */
322 { NULL
, 0 }, /* En */
323 { NULL
, TYPE_Dx
}, /* Dx */
324 { NULL
, 0 }, /* %Q */
325 { NULL
, 0 }, /* br */
326 { NULL
, 0 }, /* sp */
327 { NULL
, 0 }, /* %U */
328 { NULL
, 0 }, /* Ta */
329 { NULL
, 0 }, /* ll */
334 mandocdb(int argc
, char *argv
[])
338 const char *path_arg
, *progname
;
343 if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL
) == -1) {
345 return (int)MANDOCLEVEL_SYSERR
;
349 memset(&conf
, 0, sizeof(conf
));
350 memset(stmts
, 0, STMT__MAX
* sizeof(sqlite3_stmt
*));
353 * We accept a few different invocations.
354 * The CHECKOP macro makes sure that invocation styles don't
355 * clobber each other.
357 #define CHECKOP(_op, _ch) do \
358 if (OP_DEFAULT != (_op)) { \
359 warnx("-%c: Conflicting option", (_ch)); \
361 } while (/*CONSTCOND*/0)
366 while (-1 != (ch
= getopt(argc
, argv
, "aC:Dd:npQT:tu:v")))
391 mparse_options
|= MPARSE_QUICK
;
394 if (strcmp(optarg
, "utf8")) {
395 warnx("-T%s: Unsupported output format",
403 dup2(STDOUT_FILENO
, STDERR_FILENO
);
413 /* Compatibility with espie@'s makewhatis. */
424 if (pledge("stdio rpath", NULL
) == -1) {
426 return (int)MANDOCLEVEL_SYSERR
;
431 if (OP_CONFFILE
== op
&& argc
> 0) {
432 warnx("-C: Too many arguments");
436 exitcode
= (int)MANDOCLEVEL_OK
;
438 mp
= mparse_alloc(mparse_options
, MANDOCLEVEL_BADARG
, NULL
, NULL
);
439 mandoc_ohash_init(&mpages
, 6, offsetof(struct mpage
, inodev
));
440 mandoc_ohash_init(&mlinks
, 6, offsetof(struct mlink
, file
));
442 if (OP_UPDATE
== op
|| OP_DELETE
== op
|| OP_TEST
== op
) {
445 * Most of these deal with a specific directory.
446 * Jump into that directory first.
448 if (OP_TEST
!= op
&& 0 == set_basedir(path_arg
, 1))
453 * The existing database is usable. Process
454 * all files specified on the command-line.
458 if (pledge("stdio rpath wpath cpath fattr flock", NULL
) == -1) {
460 exitcode
= (int)MANDOCLEVEL_SYSERR
;
466 for (i
= 0; i
< argc
; i
++)
472 * Database missing or corrupt.
473 * Recreate from scratch.
475 exitcode
= (int)MANDOCLEVEL_OK
;
484 dbclose(OP_DEFAULT
== op
? 0 : 1);
487 * If we have arguments, use them as our manpaths.
488 * If we don't, grok from manpath(1) or however else
489 * manconf_parse() wants to do it.
492 conf
.manpath
.paths
= mandoc_reallocarray(NULL
,
493 argc
, sizeof(char *));
494 conf
.manpath
.sz
= (size_t)argc
;
495 for (i
= 0; i
< argc
; i
++)
496 conf
.manpath
.paths
[i
] = mandoc_strdup(argv
[i
]);
498 manconf_parse(&conf
, path_arg
, NULL
, NULL
);
500 if (conf
.manpath
.sz
== 0) {
501 exitcode
= (int)MANDOCLEVEL_BADARG
;
502 say("", "Empty manpath");
506 * First scan the tree rooted at a base directory, then
507 * build a new database and finally move it into place.
508 * Ignore zero-length directories and strip trailing
511 for (j
= 0; j
< conf
.manpath
.sz
; j
++) {
512 sz
= strlen(conf
.manpath
.paths
[j
]);
513 if (sz
&& conf
.manpath
.paths
[j
][sz
- 1] == '/')
514 conf
.manpath
.paths
[j
][--sz
] = '\0';
519 mandoc_ohash_init(&mpages
, 6,
520 offsetof(struct mpage
, inodev
));
521 mandoc_ohash_init(&mlinks
, 6,
522 offsetof(struct mlink
, file
));
525 if ( ! set_basedir(conf
.manpath
.paths
[j
], argc
> 0))
533 if (warnings
&& !nodb
&&
534 ! (MPARSE_QUICK
& mparse_options
))
538 if (j
+ 1 < conf
.manpath
.sz
) {
540 ohash_delete(&mpages
);
541 ohash_delete(&mlinks
);
550 ohash_delete(&mpages
);
551 ohash_delete(&mlinks
);
554 progname
= getprogname();
555 fprintf(stderr
, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
556 " %s [-aDnpQ] [-Tutf8] dir ...\n"
557 " %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
558 " %s [-Dnp] -u dir [file ...]\n"
559 " %s [-Q] -t file ...\n",
560 progname
, progname
, progname
, progname
, progname
);
562 return (int)MANDOCLEVEL_BADARG
;
566 * Scan a directory tree rooted at "basedir" for manpages.
567 * We use fts(), scanning directory parts along the way for clues to our
568 * section and architecture.
570 * If use_all has been specified, grok all files.
571 * If not, sanitise paths to the following:
573 * [./]man*[/<arch>]/<name>.<section>
575 * [./]cat<section>[/<arch>]/<name>.0
577 * TODO: accomodate for multi-language directories.
587 char *dsec
, *arch
, *fsec
, *cp
;
592 argv
[1] = (char *)NULL
;
594 f
= fts_open((char * const *)argv
,
595 FTS_PHYSICAL
| FTS_NOCHDIR
, NULL
);
597 exitcode
= (int)MANDOCLEVEL_SYSERR
;
598 say("", "&fts_open");
605 while ((ff
= fts_read(f
)) != NULL
) {
606 path
= ff
->fts_path
+ 2;
607 switch (ff
->fts_info
) {
610 * Symbolic links require various sanity checks,
611 * then get handled just like regular files.
614 if (realpath(path
, buf
) == NULL
) {
616 say(path
, "&realpath");
619 if (strstr(buf
, basedir
) != buf
621 && strstr(buf
, HOMEBREWDIR
) != buf
624 if (warnings
) say("",
625 "%s: outside base directory", buf
);
628 /* Use logical inode to avoid mpages dupe. */
629 if (stat(path
, ff
->fts_statp
) == -1) {
637 * If we're a regular file, add an mlink by using the
638 * stored directory data and handling the filename.
641 if ( ! strcmp(path
, MANDOC_DB
))
643 if ( ! use_all
&& ff
->fts_level
< 2) {
645 say(path
, "Extraneous file");
650 while (fsec
== NULL
) {
651 fsec
= strrchr(ff
->fts_name
, '.');
652 if (fsec
== NULL
|| strcmp(fsec
+1, "gz"))
662 "No filename suffix");
665 } else if ( ! strcmp(++fsec
, "html")) {
667 say(path
, "Skip html");
669 } else if ( ! strcmp(fsec
, "ps")) {
671 say(path
, "Skip ps");
673 } else if ( ! strcmp(fsec
, "pdf")) {
675 say(path
, "Skip pdf");
677 } else if ( ! use_all
&&
678 ((dform
== FORM_SRC
&&
679 strncmp(fsec
, dsec
, strlen(dsec
))) ||
680 (dform
== FORM_CAT
&& strcmp(fsec
, "0")))) {
682 say(path
, "Wrong filename suffix");
687 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
688 if (strlcpy(mlink
->file
, path
,
689 sizeof(mlink
->file
)) >=
690 sizeof(mlink
->file
)) {
691 say(path
, "Filename too long");
695 mlink
->dform
= dform
;
698 mlink
->name
= ff
->fts_name
;
701 mlink_add(mlink
, ff
->fts_statp
);
710 say(path
, "Not a regular file");
714 switch (ff
->fts_level
) {
716 /* Ignore the root directory. */
720 * This might contain manX/ or catX/.
721 * Try to infer this from the name.
722 * If we're not in use_all, enforce it.
725 if (ff
->fts_info
== FTS_DP
) {
731 if ( ! strncmp(cp
, "man", 3)) {
734 } else if ( ! strncmp(cp
, "cat", 3)) {
742 if (dsec
!= NULL
|| use_all
)
746 say(path
, "Unknown directory part");
747 fts_set(f
, ff
, FTS_SKIP
);
751 * Possibly our architecture.
752 * If we're descending, keep tabs on it.
754 if (ff
->fts_info
!= FTS_DP
&& dsec
!= NULL
)
760 if (ff
->fts_info
== FTS_DP
|| use_all
)
763 say(path
, "Extraneous directory part");
764 fts_set(f
, ff
, FTS_SKIP
);
774 * Add a file to the mlinks table.
775 * Do not verify that it's a "valid" looking manpage (we'll do that
778 * Try to infer the manual section, architecture, and page name from the
779 * path, assuming it looks like
781 * [./]man*[/<arch>]/<name>.<section>
783 * [./]cat<section>[/<arch>]/<name>.0
785 * See treescan() for the fts(3) version of this.
788 filescan(const char *file
)
797 if (0 == strncmp(file
, "./", 2))
801 * We have to do lstat(2) before realpath(3) loses
802 * the information whether this is a symbolic link.
803 * We need to know that because for symbolic links,
804 * we want to use the orginal file name, while for
805 * regular files, we want to use the real path.
807 if (-1 == lstat(file
, &st
)) {
808 exitcode
= (int)MANDOCLEVEL_BADARG
;
811 } else if (0 == ((S_IFREG
| S_IFLNK
) & st
.st_mode
)) {
812 exitcode
= (int)MANDOCLEVEL_BADARG
;
813 say(file
, "Not a regular file");
818 * We have to resolve the file name to the real path
819 * in any case for the base directory check.
821 if (NULL
== realpath(file
, buf
)) {
822 exitcode
= (int)MANDOCLEVEL_BADARG
;
823 say(file
, "&realpath");
829 else if (strstr(buf
, basedir
) == buf
)
830 start
= buf
+ strlen(basedir
);
832 else if (strstr(buf
, HOMEBREWDIR
) == buf
)
836 exitcode
= (int)MANDOCLEVEL_BADARG
;
837 say("", "%s: outside base directory", buf
);
842 * Now we are sure the file is inside our tree.
843 * If it is a symbolic link, ignore the real path
844 * and use the original name.
845 * This implies passing stuff like "cat1/../man1/foo.1"
846 * on the command line won't work. So don't do that.
847 * Note the stat(2) can still fail if the link target
850 if (S_IFLNK
& st
.st_mode
) {
851 if (-1 == stat(buf
, &st
)) {
852 exitcode
= (int)MANDOCLEVEL_BADARG
;
856 if (strlcpy(buf
, file
, sizeof(buf
)) >= sizeof(buf
)) {
857 say(file
, "Filename too long");
861 if (OP_TEST
!= op
&& strstr(buf
, basedir
) == buf
)
862 start
+= strlen(basedir
);
865 mlink
= mandoc_calloc(1, sizeof(struct mlink
));
866 mlink
->dform
= FORM_NONE
;
867 if (strlcpy(mlink
->file
, start
, sizeof(mlink
->file
)) >=
868 sizeof(mlink
->file
)) {
869 say(start
, "Filename too long");
875 * First try to guess our directory structure.
876 * If we find a separator, try to look for man* or cat*.
877 * If we find one of these and what's underneath is a directory,
878 * assume it's an architecture.
880 if (NULL
!= (p
= strchr(start
, '/'))) {
882 if (0 == strncmp(start
, "man", 3)) {
883 mlink
->dform
= FORM_SRC
;
884 mlink
->dsec
= start
+ 3;
885 } else if (0 == strncmp(start
, "cat", 3)) {
886 mlink
->dform
= FORM_CAT
;
887 mlink
->dsec
= start
+ 3;
891 if (NULL
!= mlink
->dsec
&& NULL
!= (p
= strchr(start
, '/'))) {
899 * Now check the file suffix.
900 * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
902 p
= strrchr(start
, '\0');
903 while (p
-- > start
&& '/' != *p
&& '.' != *p
)
912 * Now try to parse the name.
913 * Use the filename portion of the path.
916 if (NULL
!= (p
= strrchr(start
, '/'))) {
920 mlink_add(mlink
, &st
);
924 mlink_add(struct mlink
*mlink
, const struct stat
*st
)
926 struct inodev inodev
;
930 assert(NULL
!= mlink
->file
);
932 mlink
->dsec
= mandoc_strdup(mlink
->dsec
? mlink
->dsec
: "");
933 mlink
->arch
= mandoc_strdup(mlink
->arch
? mlink
->arch
: "");
934 mlink
->name
= mandoc_strdup(mlink
->name
? mlink
->name
: "");
935 mlink
->fsec
= mandoc_strdup(mlink
->fsec
? mlink
->fsec
: "");
937 if ('0' == *mlink
->fsec
) {
939 mlink
->fsec
= mandoc_strdup(mlink
->dsec
);
940 mlink
->fform
= FORM_CAT
;
941 } else if ('1' <= *mlink
->fsec
&& '9' >= *mlink
->fsec
)
942 mlink
->fform
= FORM_SRC
;
944 mlink
->fform
= FORM_NONE
;
946 slot
= ohash_qlookup(&mlinks
, mlink
->file
);
947 assert(NULL
== ohash_find(&mlinks
, slot
));
948 ohash_insert(&mlinks
, slot
, mlink
);
950 memset(&inodev
, 0, sizeof(inodev
)); /* Clear padding. */
951 inodev
.st_ino
= st
->st_ino
;
952 inodev
.st_dev
= st
->st_dev
;
953 slot
= ohash_lookup_memory(&mpages
, (char *)&inodev
,
954 sizeof(struct inodev
), inodev
.st_ino
);
955 mpage
= ohash_find(&mpages
, slot
);
957 mpage
= mandoc_calloc(1, sizeof(struct mpage
));
958 mpage
->inodev
.st_ino
= inodev
.st_ino
;
959 mpage
->inodev
.st_dev
= inodev
.st_dev
;
960 ohash_insert(&mpages
, slot
, mpage
);
962 mlink
->next
= mpage
->mlinks
;
963 mpage
->mlinks
= mlink
;
964 mlink
->mpage
= mpage
;
968 mlink_free(struct mlink
*mlink
)
985 mpage
= ohash_first(&mpages
, &slot
);
986 while (NULL
!= mpage
) {
987 while (NULL
!= (mlink
= mpage
->mlinks
)) {
988 mpage
->mlinks
= mlink
->next
;
996 mpage
= ohash_next(&mpages
, &slot
);
1001 * For each mlink to the mpage, check whether the path looks like
1002 * it is formatted, and if it does, check whether a source manual
1003 * exists by the same name, ignoring the suffix.
1004 * If both conditions hold, drop the mlink.
1007 mlinks_undupe(struct mpage
*mpage
)
1010 struct mlink
**prev
;
1011 struct mlink
*mlink
;
1014 mpage
->form
= FORM_CAT
;
1015 prev
= &mpage
->mlinks
;
1016 while (NULL
!= (mlink
= *prev
)) {
1017 if (FORM_CAT
!= mlink
->dform
) {
1018 mpage
->form
= FORM_NONE
;
1021 (void)strlcpy(buf
, mlink
->file
, sizeof(buf
));
1022 bufp
= strstr(buf
, "cat");
1023 assert(NULL
!= bufp
);
1024 memcpy(bufp
, "man", 3);
1025 if (NULL
!= (bufp
= strrchr(buf
, '.')))
1027 (void)strlcat(buf
, mlink
->dsec
, sizeof(buf
));
1028 if (NULL
== ohash_find(&mlinks
,
1029 ohash_qlookup(&mlinks
, buf
)))
1032 say(mlink
->file
, "Man source exists: %s", buf
);
1035 *prev
= mlink
->next
;
1039 prev
= &(*prev
)->next
;
1044 mlink_check(struct mpage
*mpage
, struct mlink
*mlink
)
1050 * Check whether the manual section given in a file
1051 * agrees with the directory where the file is located.
1052 * Some manuals have suffixes like (3p) on their
1053 * section number either inside the file or in the
1054 * directory name, some are linked into more than one
1055 * section, like encrypt(1) = makekey(8).
1058 if (FORM_SRC
== mpage
->form
&&
1059 strcasecmp(mpage
->sec
, mlink
->dsec
))
1060 say(mlink
->file
, "Section \"%s\" manual in %s directory",
1061 mpage
->sec
, mlink
->dsec
);
1064 * Manual page directories exist for each kernel
1065 * architecture as returned by machine(1).
1066 * However, many manuals only depend on the
1067 * application architecture as returned by arch(1).
1068 * For example, some (2/ARM) manuals are shared
1069 * across the "armish" and "zaurus" kernel
1071 * A few manuals are even shared across completely
1072 * different architectures, for example fdformat(1)
1073 * on amd64, i386, sparc, and sparc64.
1076 if (strcasecmp(mpage
->arch
, mlink
->arch
))
1077 say(mlink
->file
, "Architecture \"%s\" manual in "
1078 "\"%s\" directory", mpage
->arch
, mlink
->arch
);
1082 * parse_cat() doesn't set NAME_TITLE yet.
1085 if (FORM_CAT
== mpage
->form
)
1089 * Check whether this mlink
1090 * appears as a name in the NAME section.
1093 slot
= ohash_qlookup(&names
, mlink
->name
);
1094 str
= ohash_find(&names
, slot
);
1095 assert(NULL
!= str
);
1096 if ( ! (NAME_TITLE
& str
->mask
))
1097 say(mlink
->file
, "Name missing in NAME section");
1101 * Run through the files in the global vector "mpages"
1102 * and add them to the database specified in "basedir".
1104 * This handles the parsing scheme itself, using the cues of directory
1105 * and filename to determine whether the file is parsable or not.
1108 mpages_merge(struct mparse
*mp
)
1111 struct mpage
*mpage
, *mpage_dest
;
1112 struct mlink
*mlink
, *mlink_dest
;
1113 struct roff_man
*man
;
1120 SQL_EXEC("BEGIN TRANSACTION");
1122 mpage
= ohash_first(&mpages
, &pslot
);
1123 while (mpage
!= NULL
) {
1124 mlinks_undupe(mpage
);
1125 if ((mlink
= mpage
->mlinks
) == NULL
) {
1126 mpage
= ohash_next(&mpages
, &pslot
);
1130 name_mask
= NAME_MASK
;
1131 mandoc_ohash_init(&names
, 4, offsetof(struct str
, key
));
1132 mandoc_ohash_init(&strings
, 6, offsetof(struct str
, key
));
1137 if ((fd
= mparse_open(mp
, mlink
->file
)) == -1) {
1138 say(mlink
->file
, "&open");
1143 * Interpret the file as mdoc(7) or man(7) source
1144 * code, unless it is known to be formatted.
1146 if (mlink
->dform
!= FORM_CAT
|| mlink
->fform
!= FORM_CAT
) {
1147 mparse_readfd(mp
, fd
, mlink
->file
);
1149 mparse_result(mp
, &man
, &sodest
);
1152 if (sodest
!= NULL
) {
1153 mlink_dest
= ohash_find(&mlinks
,
1154 ohash_qlookup(&mlinks
, sodest
));
1155 if (mlink_dest
== NULL
) {
1156 mandoc_asprintf(&cp
, "%s.gz", sodest
);
1157 mlink_dest
= ohash_find(&mlinks
,
1158 ohash_qlookup(&mlinks
, cp
));
1161 if (mlink_dest
!= NULL
) {
1163 /* The .so target exists. */
1165 mpage_dest
= mlink_dest
->mpage
;
1167 mlink
->mpage
= mpage_dest
;
1170 * If the target was already
1171 * processed, add the links
1172 * to the database now.
1173 * Otherwise, this will
1174 * happen when we come
1178 if (mpage_dest
->pageid
)
1179 dbadd_mlink_name(mlink
);
1181 if (mlink
->next
== NULL
)
1183 mlink
= mlink
->next
;
1186 /* Move all links to the target. */
1188 mlink
->next
= mlink_dest
->next
;
1189 mlink_dest
->next
= mpage
->mlinks
;
1190 mpage
->mlinks
= NULL
;
1193 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
) {
1195 mpage
->form
= FORM_SRC
;
1196 mpage
->sec
= man
->meta
.msec
;
1197 mpage
->sec
= mandoc_strdup(
1198 mpage
->sec
== NULL
? "" : mpage
->sec
);
1199 mpage
->arch
= man
->meta
.arch
;
1200 mpage
->arch
= mandoc_strdup(
1201 mpage
->arch
== NULL
? "" : mpage
->arch
);
1202 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1203 } else if (man
!= NULL
&& man
->macroset
== MACROSET_MAN
) {
1205 mpage
->form
= FORM_SRC
;
1206 mpage
->sec
= mandoc_strdup(man
->meta
.msec
);
1207 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1208 mpage
->title
= mandoc_strdup(man
->meta
.title
);
1210 mpage
->form
= FORM_CAT
;
1211 mpage
->sec
= mandoc_strdup(mlink
->dsec
);
1212 mpage
->arch
= mandoc_strdup(mlink
->arch
);
1213 mpage
->title
= mandoc_strdup(mlink
->name
);
1215 putkey(mpage
, mpage
->sec
, TYPE_sec
);
1216 if (*mpage
->arch
!= '\0')
1217 putkey(mpage
, mpage
->arch
, TYPE_arch
);
1219 for ( ; mlink
!= NULL
; mlink
= mlink
->next
) {
1220 if ('\0' != *mlink
->dsec
)
1221 putkey(mpage
, mlink
->dsec
, TYPE_sec
);
1222 if ('\0' != *mlink
->fsec
)
1223 putkey(mpage
, mlink
->fsec
, TYPE_sec
);
1224 putkey(mpage
, '\0' == *mlink
->arch
?
1225 any
: mlink
->arch
, TYPE_arch
);
1226 putkey(mpage
, mlink
->name
, NAME_FILE
);
1229 assert(mpage
->desc
== NULL
);
1230 if (man
!= NULL
&& man
->macroset
== MACROSET_MDOC
)
1231 parse_mdoc(mpage
, &man
->meta
, man
->first
);
1232 else if (man
!= NULL
)
1233 parse_man(mpage
, &man
->meta
, man
->first
);
1235 parse_cat(mpage
, fd
);
1236 if (mpage
->desc
== NULL
)
1237 mpage
->desc
= mandoc_strdup(mpage
->mlinks
->name
);
1239 if (warnings
&& !use_all
)
1240 for (mlink
= mpage
->mlinks
; mlink
;
1241 mlink
= mlink
->next
)
1242 mlink_check(mpage
, mlink
);
1245 mlink
= mpage
->mlinks
;
1248 ohash_delete(&strings
);
1249 ohash_delete(&names
);
1250 mpage
= ohash_next(&mpages
, &pslot
);
1254 SQL_EXEC("END TRANSACTION");
1261 const char *name
, *sec
, *arch
, *key
;
1263 sqlite3_prepare_v2(db
,
1264 "SELECT name, sec, arch, key FROM ("
1265 "SELECT name AS key, pageid FROM names "
1266 "WHERE bits & ? AND NOT EXISTS ("
1267 "SELECT pageid FROM mlinks "
1268 "WHERE mlinks.pageid == names.pageid "
1269 "AND mlinks.name == names.name"
1272 "SELECT sec, arch, name, pageid FROM mlinks "
1274 ") USING (pageid);",
1277 if (sqlite3_bind_int64(stmt
, 1, NAME_TITLE
) != SQLITE_OK
)
1278 say("", "%s", sqlite3_errmsg(db
));
1280 while (sqlite3_step(stmt
) == SQLITE_ROW
) {
1281 name
= (const char *)sqlite3_column_text(stmt
, 0);
1282 sec
= (const char *)sqlite3_column_text(stmt
, 1);
1283 arch
= (const char *)sqlite3_column_text(stmt
, 2);
1284 key
= (const char *)sqlite3_column_text(stmt
, 3);
1285 say("", "%s(%s%s%s) lacks mlink \"%s\"", name
, sec
,
1286 '\0' == *arch
? "" : "/",
1287 '\0' == *arch
? "" : arch
, key
);
1289 sqlite3_finalize(stmt
);
1293 parse_cat(struct mpage
*mpage
, int fd
)
1296 char *line
, *p
, *title
;
1297 size_t linesz
, plen
, titlesz
;
1301 stream
= (-1 == fd
) ?
1302 fopen(mpage
->mlinks
->file
, "r") :
1304 if (NULL
== stream
) {
1308 say(mpage
->mlinks
->file
, "&fopen");
1315 /* Skip to first blank line. */
1317 while (getline(&line
, &linesz
, stream
) != -1)
1322 * Assume the first line that is not indented
1323 * is the first section header. Skip to it.
1326 while (getline(&line
, &linesz
, stream
) != -1)
1327 if (*line
!= '\n' && *line
!= ' ')
1331 * Read up until the next section into a buffer.
1332 * Strip the leading and trailing newline from each read line,
1333 * appending a trailing space.
1334 * Ignore empty (whitespace-only) lines.
1340 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1344 while (isspace((unsigned char)line
[offs
]))
1346 if (line
[offs
] == '\0')
1348 title
= mandoc_realloc(title
, titlesz
+ len
- offs
);
1349 memcpy(title
+ titlesz
, line
+ offs
, len
- offs
);
1350 titlesz
+= len
- offs
;
1351 title
[titlesz
- 1] = ' ';
1356 * If no page content can be found, or the input line
1357 * is already the next section header, or there is no
1358 * trailing newline, reuse the page title as the page
1362 if (NULL
== title
|| '\0' == *title
) {
1364 say(mpage
->mlinks
->file
,
1365 "Cannot find NAME section");
1371 title
[titlesz
- 1] = '\0';
1374 * Skip to the first dash.
1375 * Use the remaining line as the description (no more than 70
1379 if (NULL
!= (p
= strstr(title
, "- "))) {
1380 for (p
+= 2; ' ' == *p
|| '\b' == *p
; p
++)
1381 /* Skip to next word. */ ;
1384 say(mpage
->mlinks
->file
,
1385 "No dash in title line");
1391 /* Strip backspace-encoding from line. */
1393 while (NULL
!= (line
= memchr(p
, '\b', plen
))) {
1396 memmove(line
, line
+ 1, plen
--);
1399 memmove(line
- 1, line
+ 1, plen
- len
);
1403 mpage
->desc
= mandoc_strdup(p
);
1409 * Put a type/word pair into the word database for this particular file.
1412 putkey(const struct mpage
*mpage
, char *value
, uint64_t type
)
1416 assert(NULL
!= value
);
1417 if (TYPE_arch
== type
)
1418 for (cp
= value
; *cp
; cp
++)
1419 if (isupper((unsigned char)*cp
))
1420 *cp
= _tolower((unsigned char)*cp
);
1421 putkeys(mpage
, value
, strlen(value
), type
);
1425 * Grok all nodes at or below a certain mdoc node into putkey().
1428 putmdockey(const struct mpage
*mpage
,
1429 const struct roff_node
*n
, uint64_t m
)
1432 for ( ; NULL
!= n
; n
= n
->next
) {
1433 if (NULL
!= n
->child
)
1434 putmdockey(mpage
, n
->child
, m
);
1435 if (n
->type
== ROFFT_TEXT
)
1436 putkey(mpage
, n
->string
, m
);
1441 parse_man(struct mpage
*mpage
, const struct roff_meta
*meta
,
1442 const struct roff_node
*n
)
1444 const struct roff_node
*head
, *body
;
1445 char *start
, *title
;
1453 * We're only searching for one thing: the first text child in
1454 * the BODY of a NAME section. Since we don't keep track of
1455 * sections in -man, run some hoops to find out whether we're in
1456 * the correct section or not.
1459 if (n
->type
== ROFFT_BODY
&& n
->tok
== MAN_SH
) {
1461 assert(body
->parent
);
1462 if (NULL
!= (head
= body
->parent
->head
) &&
1463 1 == head
->nchild
&&
1464 NULL
!= (head
= (head
->child
)) &&
1465 head
->type
== ROFFT_TEXT
&&
1466 0 == strcmp(head
->string
, "NAME") &&
1467 NULL
!= body
->child
) {
1470 * Suck the entire NAME section into memory.
1471 * Yes, we might run away.
1472 * But too many manuals have big, spread-out
1473 * NAME sections over many lines.
1477 deroff(&title
, body
);
1482 * Go through a special heuristic dance here.
1483 * Conventionally, one or more manual names are
1484 * comma-specified prior to a whitespace, then a
1485 * dash, then a description. Try to puzzle out
1486 * the name parts here.
1491 sz
= strcspn(start
, " ,");
1492 if ('\0' == start
[sz
])
1499 * Assume a stray trailing comma in the
1500 * name list if a name begins with a dash.
1503 if ('-' == start
[0] ||
1504 ('\\' == start
[0] && '-' == start
[1]))
1507 putkey(mpage
, start
, NAME_TITLE
);
1508 if ( ! (mpage
->name_head_done
||
1509 strcasecmp(start
, meta
->title
))) {
1510 putkey(mpage
, start
, NAME_HEAD
);
1511 mpage
->name_head_done
= 1;
1519 assert(',' == byte
);
1521 while (' ' == *start
)
1525 if (start
== title
) {
1526 putkey(mpage
, start
, NAME_TITLE
);
1527 if ( ! (mpage
->name_head_done
||
1528 strcasecmp(start
, meta
->title
))) {
1529 putkey(mpage
, start
, NAME_HEAD
);
1530 mpage
->name_head_done
= 1;
1536 while (isspace((unsigned char)*start
))
1539 if (0 == strncmp(start
, "-", 1))
1541 else if (0 == strncmp(start
, "\\-\\-", 4))
1543 else if (0 == strncmp(start
, "\\-", 2))
1545 else if (0 == strncmp(start
, "\\(en", 4))
1547 else if (0 == strncmp(start
, "\\(em", 4))
1550 while (' ' == *start
)
1553 mpage
->desc
= mandoc_strdup(start
);
1559 for (n
= n
->child
; n
; n
= n
->next
) {
1560 if (NULL
!= mpage
->desc
)
1562 parse_man(mpage
, meta
, n
);
1567 parse_mdoc(struct mpage
*mpage
, const struct roff_meta
*meta
,
1568 const struct roff_node
*n
)
1572 for (n
= n
->child
; NULL
!= n
; n
= n
->next
) {
1579 if (NULL
!= mdocs
[n
->tok
].fp
)
1580 if (0 == (*mdocs
[n
->tok
].fp
)(mpage
, meta
, n
))
1582 if (mdocs
[n
->tok
].mask
)
1583 putmdockey(mpage
, n
->child
,
1584 mdocs
[n
->tok
].mask
);
1587 assert(n
->type
!= ROFFT_ROOT
);
1590 if (NULL
!= n
->child
)
1591 parse_mdoc(mpage
, meta
, n
);
1596 parse_mdoc_Fd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1597 const struct roff_node
*n
)
1602 if (SEC_SYNOPSIS
!= n
->sec
||
1603 NULL
== (n
= n
->child
) ||
1604 n
->type
!= ROFFT_TEXT
)
1608 * Only consider those `Fd' macro fields that begin with an
1609 * "inclusion" token (versus, e.g., #define).
1612 if (strcmp("#include", n
->string
))
1615 if ((n
= n
->next
) == NULL
|| n
->type
!= ROFFT_TEXT
)
1619 * Strip away the enclosing angle brackets and make sure we're
1624 if ('<' == *start
|| '"' == *start
)
1627 if (0 == (sz
= strlen(start
)))
1630 end
= &start
[(int)sz
- 1];
1631 if ('>' == *end
|| '"' == *end
)
1635 putkeys(mpage
, start
, end
- start
+ 1, TYPE_In
);
1640 parse_mdoc_fname(struct mpage
*mpage
, const struct roff_node
*n
)
1645 if (n
->type
!= ROFFT_TEXT
)
1648 /* Skip function pointer punctuation. */
1651 while (*cp
== '(' || *cp
== '*')
1653 sz
= strcspn(cp
, "()");
1655 putkeys(mpage
, cp
, sz
, TYPE_Fn
);
1656 if (n
->sec
== SEC_SYNOPSIS
)
1657 putkeys(mpage
, cp
, sz
, NAME_SYN
);
1661 parse_mdoc_Fn(struct mpage
*mpage
, const struct roff_meta
*meta
,
1662 const struct roff_node
*n
)
1665 if (n
->child
== NULL
)
1668 parse_mdoc_fname(mpage
, n
->child
);
1670 for (n
= n
->child
->next
; n
!= NULL
; n
= n
->next
)
1671 if (n
->type
== ROFFT_TEXT
)
1672 putkey(mpage
, n
->string
, TYPE_Fa
);
1678 parse_mdoc_Fo(struct mpage
*mpage
, const struct roff_meta
*meta
,
1679 const struct roff_node
*n
)
1682 if (n
->type
!= ROFFT_HEAD
)
1685 if (n
->child
!= NULL
)
1686 parse_mdoc_fname(mpage
, n
->child
);
1692 parse_mdoc_Va(struct mpage
*mpage
, const struct roff_meta
*meta
,
1693 const struct roff_node
*n
)
1697 if (n
->type
!= ROFFT_ELEM
&& n
->type
!= ROFFT_BODY
)
1700 if (n
->nchild
== 1 && n
->child
->type
== ROFFT_TEXT
)
1706 putkey(mpage
, cp
, TYPE_Vt
| (n
->tok
== MDOC_Va
||
1707 n
->type
== ROFFT_BODY
? TYPE_Va
: 0));
1715 parse_mdoc_Xr(struct mpage
*mpage
, const struct roff_meta
*meta
,
1716 const struct roff_node
*n
)
1720 if (NULL
== (n
= n
->child
))
1723 if (NULL
== n
->next
) {
1724 putkey(mpage
, n
->string
, TYPE_Xr
);
1728 mandoc_asprintf(&cp
, "%s(%s)", n
->string
, n
->next
->string
);
1729 putkey(mpage
, cp
, TYPE_Xr
);
1735 parse_mdoc_Nd(struct mpage
*mpage
, const struct roff_meta
*meta
,
1736 const struct roff_node
*n
)
1739 if (n
->type
== ROFFT_BODY
)
1740 deroff(&mpage
->desc
, n
);
1745 parse_mdoc_Nm(struct mpage
*mpage
, const struct roff_meta
*meta
,
1746 const struct roff_node
*n
)
1749 if (SEC_NAME
== n
->sec
)
1750 putmdockey(mpage
, n
->child
, NAME_TITLE
);
1751 else if (n
->sec
== SEC_SYNOPSIS
&& n
->type
== ROFFT_HEAD
) {
1752 if (n
->child
== NULL
)
1753 putkey(mpage
, meta
->name
, NAME_SYN
);
1755 putmdockey(mpage
, n
->child
, NAME_SYN
);
1757 if ( ! (mpage
->name_head_done
||
1758 n
->child
== NULL
|| n
->child
->string
== NULL
||
1759 strcasecmp(n
->child
->string
, meta
->title
))) {
1760 putkey(mpage
, n
->child
->string
, ROFFT_HEAD
);
1761 mpage
->name_head_done
= 1;
1767 parse_mdoc_Sh(struct mpage
*mpage
, const struct roff_meta
*meta
,
1768 const struct roff_node
*n
)
1771 return n
->sec
== SEC_CUSTOM
&& n
->type
== ROFFT_HEAD
;
1775 parse_mdoc_head(struct mpage
*mpage
, const struct roff_meta
*meta
,
1776 const struct roff_node
*n
)
1779 return n
->type
== ROFFT_HEAD
;
1783 * Add a string to the hash table for the current manual.
1784 * Each string has a bitmask telling which macros it belongs to.
1785 * When we finish the manual, we'll dump the table.
1788 putkeys(const struct mpage
*mpage
, char *cp
, size_t sz
, uint64_t v
)
1799 mustfree
= render_string(&cp
, &sz
);
1805 name_mask
&= ~NAME_FIRST
;
1807 say(mpage
->mlinks
->file
,
1808 "Adding name %*s, bits=%d", sz
, cp
, v
);
1812 for (i
= 0; i
< mansearch_keymax
; i
++)
1813 if ((uint64_t)1 << i
& v
)
1814 say(mpage
->mlinks
->file
,
1815 "Adding key %s=%*s",
1816 mansearch_keynames
[i
], sz
, cp
);
1820 slot
= ohash_qlookupi(htab
, cp
, &end
);
1821 s
= ohash_find(htab
, slot
);
1823 if (NULL
!= s
&& mpage
== s
->mpage
) {
1826 } else if (NULL
== s
) {
1827 s
= mandoc_calloc(1, sizeof(struct str
) + sz
+ 1);
1828 memcpy(s
->key
, cp
, sz
);
1829 ohash_insert(htab
, slot
, s
);
1839 * Take a Unicode codepoint and produce its UTF-8 encoding.
1840 * This isn't the best way to do this, but it works.
1841 * The magic numbers are from the UTF-8 packaging.
1842 * They're not as scary as they seem: read the UTF-8 spec for details.
1845 utf8(unsigned int cp
, char out
[7])
1850 if (cp
<= 0x0000007F) {
1853 } else if (cp
<= 0x000007FF) {
1855 out
[0] = (cp
>> 6 & 31) | 192;
1856 out
[1] = (cp
& 63) | 128;
1857 } else if (cp
<= 0x0000FFFF) {
1859 out
[0] = (cp
>> 12 & 15) | 224;
1860 out
[1] = (cp
>> 6 & 63) | 128;
1861 out
[2] = (cp
& 63) | 128;
1862 } else if (cp
<= 0x001FFFFF) {
1864 out
[0] = (cp
>> 18 & 7) | 240;
1865 out
[1] = (cp
>> 12 & 63) | 128;
1866 out
[2] = (cp
>> 6 & 63) | 128;
1867 out
[3] = (cp
& 63) | 128;
1868 } else if (cp
<= 0x03FFFFFF) {
1870 out
[0] = (cp
>> 24 & 3) | 248;
1871 out
[1] = (cp
>> 18 & 63) | 128;
1872 out
[2] = (cp
>> 12 & 63) | 128;
1873 out
[3] = (cp
>> 6 & 63) | 128;
1874 out
[4] = (cp
& 63) | 128;
1875 } else if (cp
<= 0x7FFFFFFF) {
1877 out
[0] = (cp
>> 30 & 1) | 252;
1878 out
[1] = (cp
>> 24 & 63) | 128;
1879 out
[2] = (cp
>> 18 & 63) | 128;
1880 out
[3] = (cp
>> 12 & 63) | 128;
1881 out
[4] = (cp
>> 6 & 63) | 128;
1882 out
[5] = (cp
& 63) | 128;
1891 * If the string contains escape sequences,
1892 * replace it with an allocated rendering and return 1,
1893 * such that the caller can free it after use.
1894 * Otherwise, do nothing and return 0.
1897 render_string(char **public, size_t *psz
)
1899 const char *src
, *scp
, *addcp
, *seq
;
1901 size_t ssz
, dsz
, addsz
;
1902 char utfbuf
[7], res
[6];
1903 int seqlen
, unicode
;
1907 res
[2] = ASCII_NBRSP
;
1908 res
[3] = ASCII_HYPH
;
1909 res
[4] = ASCII_BREAK
;
1912 src
= scp
= *public;
1917 while (scp
< src
+ *psz
) {
1919 /* Leave normal characters unchanged. */
1921 if (strchr(res
, *scp
) == NULL
) {
1929 * Found something that requires replacing,
1930 * make sure we have a destination buffer.
1934 dst
= mandoc_malloc(ssz
+ 1);
1936 memcpy(dst
, src
, dsz
);
1939 /* Handle single-char special characters. */
1960 * Found an escape sequence.
1961 * Read past the slash, then parse it.
1962 * Ignore everything except characters.
1966 if (mandoc_escape(&scp
, &seq
, &seqlen
) != ESCAPE_SPECIAL
)
1970 * Render the special character
1971 * as either UTF-8 or ASCII.
1975 unicode
= mchars_spec2cp(seq
, seqlen
);
1978 addsz
= utf8(unicode
, utfbuf
);
1983 addcp
= mchars_spec2str(seq
, seqlen
, &addsz
);
1986 if (*addcp
== ASCII_NBRSP
) {
1992 /* Copy the rendered glyph into the stream. */
1995 dst
= mandoc_realloc(dst
, ssz
+ 1);
1996 memcpy(dst
+ dsz
, addcp
, addsz
);
2004 /* Trim trailing whitespace and NUL-terminate. */
2006 while (*psz
> 0 && (*public)[*psz
- 1] == ' ')
2009 (*public)[*psz
] = '\0';
2016 dbadd_mlink(const struct mlink
*mlink
)
2021 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->dsec
);
2022 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->arch
);
2023 SQL_BIND_TEXT(stmts
[STMT_INSERT_LINK
], i
, mlink
->name
);
2024 SQL_BIND_INT64(stmts
[STMT_INSERT_LINK
], i
, mlink
->mpage
->pageid
);
2025 SQL_STEP(stmts
[STMT_INSERT_LINK
]);
2026 sqlite3_reset(stmts
[STMT_INSERT_LINK
]);
2030 dbadd_mlink_name(const struct mlink
*mlink
)
2038 SQL_BIND_INT64(stmts
[STMT_SELECT_NAME
], i
, mlink
->mpage
->pageid
);
2039 bits
= NAME_FILE
& NAME_MASK
;
2040 if (sqlite3_step(stmts
[STMT_SELECT_NAME
]) == SQLITE_ROW
) {
2041 bits
|= sqlite3_column_int64(stmts
[STMT_SELECT_NAME
], 0);
2042 sqlite3_reset(stmts
[STMT_SELECT_NAME
]);
2046 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, bits
);
2047 SQL_BIND_TEXT(stmts
[STMT_INSERT_NAME
], i
, mlink
->name
);
2048 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, mlink
->mpage
->pageid
);
2049 SQL_STEP(stmts
[STMT_INSERT_NAME
]);
2050 sqlite3_reset(stmts
[STMT_INSERT_NAME
]);
2054 * Flush the current page's terms (and their bits) into the database.
2055 * Wrap the entire set of additions in a transaction to make sqlite be a
2057 * Also, handle escape sequences at the last possible moment.
2060 dbadd(struct mpage
*mpage
)
2062 struct mlink
*mlink
;
2069 mlink
= mpage
->mlinks
;
2072 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2073 key
= ohash_next(&names
, &slot
))
2075 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2076 key
= ohash_next(&strings
, &slot
))
2080 while (NULL
!= mlink
) {
2081 fputs(mlink
->name
, stdout
);
2082 if (NULL
== mlink
->next
||
2083 strcmp(mlink
->dsec
, mlink
->next
->dsec
) ||
2084 strcmp(mlink
->fsec
, mlink
->next
->fsec
) ||
2085 strcmp(mlink
->arch
, mlink
->next
->arch
)) {
2087 if ('\0' == *mlink
->dsec
)
2088 fputs(mlink
->fsec
, stdout
);
2090 fputs(mlink
->dsec
, stdout
);
2091 if ('\0' != *mlink
->arch
)
2092 printf("/%s", mlink
->arch
);
2095 mlink
= mlink
->next
;
2097 fputs(", ", stdout
);
2099 printf(" - %s\n", mpage
->desc
);
2104 say(mlink
->file
, "Adding to database");
2108 mustfree
= render_string(&cp
, &i
);
2110 SQL_BIND_TEXT(stmts
[STMT_INSERT_PAGE
], i
, cp
);
2111 SQL_BIND_INT(stmts
[STMT_INSERT_PAGE
], i
, mpage
->form
);
2112 SQL_STEP(stmts
[STMT_INSERT_PAGE
]);
2113 mpage
->pageid
= sqlite3_last_insert_rowid(db
);
2114 sqlite3_reset(stmts
[STMT_INSERT_PAGE
]);
2118 while (NULL
!= mlink
) {
2120 mlink
= mlink
->next
;
2122 mlink
= mpage
->mlinks
;
2124 for (key
= ohash_first(&names
, &slot
); NULL
!= key
;
2125 key
= ohash_next(&names
, &slot
)) {
2126 assert(key
->mpage
== mpage
);
2128 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, key
->mask
);
2129 SQL_BIND_TEXT(stmts
[STMT_INSERT_NAME
], i
, key
->key
);
2130 SQL_BIND_INT64(stmts
[STMT_INSERT_NAME
], i
, mpage
->pageid
);
2131 SQL_STEP(stmts
[STMT_INSERT_NAME
]);
2132 sqlite3_reset(stmts
[STMT_INSERT_NAME
]);
2135 for (key
= ohash_first(&strings
, &slot
); NULL
!= key
;
2136 key
= ohash_next(&strings
, &slot
)) {
2137 assert(key
->mpage
== mpage
);
2139 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, key
->mask
);
2140 SQL_BIND_TEXT(stmts
[STMT_INSERT_KEY
], i
, key
->key
);
2141 SQL_BIND_INT64(stmts
[STMT_INSERT_KEY
], i
, mpage
->pageid
);
2142 SQL_STEP(stmts
[STMT_INSERT_KEY
]);
2143 sqlite3_reset(stmts
[STMT_INSERT_KEY
]);
2151 struct mpage
*mpage
;
2152 struct mlink
*mlink
;
2157 SQL_EXEC("BEGIN TRANSACTION");
2159 for (mpage
= ohash_first(&mpages
, &slot
); NULL
!= mpage
;
2160 mpage
= ohash_next(&mpages
, &slot
)) {
2161 mlink
= mpage
->mlinks
;
2163 say(mlink
->file
, "Deleting from database");
2166 for ( ; NULL
!= mlink
; mlink
= mlink
->next
) {
2168 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2170 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2172 SQL_BIND_TEXT(stmts
[STMT_DELETE_PAGE
],
2174 SQL_STEP(stmts
[STMT_DELETE_PAGE
]);
2175 sqlite3_reset(stmts
[STMT_DELETE_PAGE
]);
2180 SQL_EXEC("END TRANSACTION");
2184 * Close an existing database and its prepared statements.
2185 * If "real" is not set, rename the temporary file into the real one.
2197 for (i
= 0; i
< STMT__MAX
; i
++) {
2198 sqlite3_finalize(stmts
[i
]);
2208 if ('\0' == *tempfilename
) {
2209 if (-1 == rename(MANDOC_DB
"~", MANDOC_DB
)) {
2210 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2211 say(MANDOC_DB
, "&rename");
2216 switch (child
= fork()) {
2218 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2219 say("", "&fork cmp");
2222 execlp("cmp", "cmp", "-s",
2223 tempfilename
, MANDOC_DB
, (char *)NULL
);
2224 say("", "&exec cmp");
2229 if (-1 == waitpid(child
, &status
, 0)) {
2230 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2231 say("", "&wait cmp");
2232 } else if (WIFSIGNALED(status
)) {
2233 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2234 say("", "cmp died from signal %d", WTERMSIG(status
));
2235 } else if (WEXITSTATUS(status
)) {
2236 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2238 "Data changed, but cannot replace database");
2241 *strrchr(tempfilename
, '/') = '\0';
2242 switch (child
= fork()) {
2244 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2245 say("", "&fork rm");
2248 execlp("rm", "rm", "-rf", tempfilename
, (char *)NULL
);
2249 say("", "&exec rm");
2250 exit((int)MANDOCLEVEL_SYSERR
);
2254 if (-1 == waitpid(child
, &status
, 0)) {
2255 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2256 say("", "&wait rm");
2257 } else if (WIFSIGNALED(status
) || WEXITSTATUS(status
)) {
2258 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2259 say("", "%s: Cannot remove temporary directory",
2265 * This is straightforward stuff.
2266 * Open a database connection to a "temporary" database, then open a set
2267 * of prepared statements we'll use over and over again.
2268 * If "real" is set, we use the existing database; if not, we truncate a
2270 * Must be matched by dbclose().
2281 *tempfilename
= '\0';
2282 ofl
= SQLITE_OPEN_READWRITE
;
2285 rc
= sqlite3_open_v2(MANDOC_DB
, &db
, ofl
, NULL
);
2286 if (SQLITE_OK
!= rc
) {
2287 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2288 if (SQLITE_CANTOPEN
!= rc
)
2289 say(MANDOC_DB
, "%s", sqlite3_errstr(rc
));
2292 goto prepare_statements
;
2295 ofl
|= SQLITE_OPEN_CREATE
| SQLITE_OPEN_EXCLUSIVE
;
2297 remove(MANDOC_DB
"~");
2298 rc
= sqlite3_open_v2(MANDOC_DB
"~", &db
, ofl
, NULL
);
2299 if (SQLITE_OK
== rc
)
2301 if (MPARSE_QUICK
& mparse_options
) {
2302 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2303 say(MANDOC_DB
"~", "%s", sqlite3_errstr(rc
));
2307 (void)strlcpy(tempfilename
, "/tmp/mandocdb.XXXXXX",
2308 sizeof(tempfilename
));
2309 if (NULL
== mkdtemp(tempfilename
)) {
2310 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2311 say("", "&%s", tempfilename
);
2314 (void)strlcat(tempfilename
, "/" MANDOC_DB
,
2315 sizeof(tempfilename
));
2316 rc
= sqlite3_open_v2(tempfilename
, &db
, ofl
, NULL
);
2317 if (SQLITE_OK
!= rc
) {
2318 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2319 say("", "%s: %s", tempfilename
, sqlite3_errstr(rc
));
2324 sql
= "CREATE TABLE \"mpages\" (\n"
2325 " \"desc\" TEXT NOT NULL,\n"
2326 " \"form\" INTEGER NOT NULL,\n"
2327 " \"pageid\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
2330 "CREATE TABLE \"mlinks\" (\n"
2331 " \"sec\" TEXT NOT NULL,\n"
2332 " \"arch\" TEXT NOT NULL,\n"
2333 " \"name\" TEXT NOT NULL,\n"
2334 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2335 "ON DELETE CASCADE\n"
2337 "CREATE INDEX mlinks_pageid_idx ON mlinks (pageid);\n"
2339 "CREATE TABLE \"names\" (\n"
2340 " \"bits\" INTEGER NOT NULL,\n"
2341 " \"name\" TEXT NOT NULL,\n"
2342 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2343 "ON DELETE CASCADE,\n"
2344 " UNIQUE (\"name\", \"pageid\") ON CONFLICT REPLACE\n"
2347 "CREATE TABLE \"keys\" (\n"
2348 " \"bits\" INTEGER NOT NULL,\n"
2349 " \"key\" TEXT NOT NULL,\n"
2350 " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
2351 "ON DELETE CASCADE\n"
2353 "CREATE INDEX keys_pageid_idx ON keys (pageid);\n";
2355 if (SQLITE_OK
!= sqlite3_exec(db
, sql
, NULL
, NULL
, NULL
)) {
2356 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2357 say(MANDOC_DB
, "%s", sqlite3_errmsg(db
));
2363 if (SQLITE_OK
!= sqlite3_exec(db
,
2364 "PRAGMA foreign_keys = ON", NULL
, NULL
, NULL
)) {
2365 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2366 say(MANDOC_DB
, "PRAGMA foreign_keys: %s",
2367 sqlite3_errmsg(db
));
2372 sql
= "DELETE FROM mpages WHERE pageid IN "
2373 "(SELECT pageid FROM mlinks WHERE "
2374 "sec=? AND arch=? AND name=?)";
2375 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_DELETE_PAGE
], NULL
);
2376 sql
= "INSERT INTO mpages "
2377 "(desc,form) VALUES (?,?)";
2378 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_PAGE
], NULL
);
2379 sql
= "INSERT INTO mlinks "
2380 "(sec,arch,name,pageid) VALUES (?,?,?,?)";
2381 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_LINK
], NULL
);
2382 sql
= "SELECT bits FROM names where pageid = ?";
2383 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_SELECT_NAME
], NULL
);
2384 sql
= "INSERT INTO names "
2385 "(bits,name,pageid) VALUES (?,?,?)";
2386 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_NAME
], NULL
);
2387 sql
= "INSERT INTO keys "
2388 "(bits,key,pageid) VALUES (?,?,?)";
2389 sqlite3_prepare_v2(db
, sql
, -1, &stmts
[STMT_INSERT_KEY
], NULL
);
2393 * When opening a new database, we can turn off
2394 * synchronous mode for much better performance.
2397 if (real
&& SQLITE_OK
!= sqlite3_exec(db
,
2398 "PRAGMA synchronous = OFF", NULL
, NULL
, NULL
)) {
2399 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2400 say(MANDOC_DB
, "PRAGMA synchronous: %s",
2401 sqlite3_errmsg(db
));
2411 set_basedir(const char *targetdir
, int report_baddir
)
2413 static char startdir
[PATH_MAX
];
2414 static int getcwd_status
; /* 1 = ok, 2 = failure */
2415 static int chdir_status
; /* 1 = changed directory */
2419 * Remember the original working directory, if possible.
2420 * This will be needed if the second or a later directory
2421 * on the command line is given as a relative path.
2422 * Do not error out if the current directory is not
2423 * searchable: Maybe it won't be needed after all.
2425 if (0 == getcwd_status
) {
2426 if (NULL
== getcwd(startdir
, sizeof(startdir
))) {
2428 (void)strlcpy(startdir
, strerror(errno
),
2435 * We are leaving the old base directory.
2436 * Do not use it any longer, not even for messages.
2441 * If and only if the directory was changed earlier and
2442 * the next directory to process is given as a relative path,
2443 * first go back, or bail out if that is impossible.
2445 if (chdir_status
&& '/' != *targetdir
) {
2446 if (2 == getcwd_status
) {
2447 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2448 say("", "getcwd: %s", startdir
);
2451 if (-1 == chdir(startdir
)) {
2452 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2453 say("", "&chdir %s", startdir
);
2459 * Always resolve basedir to the canonicalized absolute
2460 * pathname and append a trailing slash, such that
2461 * we can reliably check whether files are inside.
2463 if (NULL
== realpath(targetdir
, basedir
)) {
2464 if (report_baddir
|| errno
!= ENOENT
) {
2465 exitcode
= (int)MANDOCLEVEL_BADARG
;
2466 say("", "&%s: realpath", targetdir
);
2469 } else if (-1 == chdir(basedir
)) {
2470 if (report_baddir
|| errno
!= ENOENT
) {
2471 exitcode
= (int)MANDOCLEVEL_BADARG
;
2477 cp
= strchr(basedir
, '\0');
2478 if ('/' != cp
[-1]) {
2479 if (cp
- basedir
>= PATH_MAX
- 1) {
2480 exitcode
= (int)MANDOCLEVEL_SYSERR
;
2481 say("", "Filename too long");
2491 say(const char *file
, const char *format
, ...)
2496 if ('\0' != *basedir
)
2497 fprintf(stderr
, "%s", basedir
);
2498 if ('\0' != *basedir
&& '\0' != *file
)
2501 fprintf(stderr
, "%s", file
);
2504 if (NULL
!= format
) {
2517 if (NULL
!= format
) {
2518 if ('\0' != *basedir
|| '\0' != *file
)
2519 fputs(": ", stderr
);
2520 va_start(ap
, format
);
2521 vfprintf(stderr
, format
, ap
);
2525 if ('\0' != *basedir
|| '\0' != *file
|| NULL
!= format
)
2526 fputs(": ", stderr
);
2529 fputc('\n', stderr
);