=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.150 retrieving revision 1.161 diff -u -p -r1.150 -r1.161 --- mandoc/mandocdb.c 2014/06/19 00:45:37 1.150 +++ mandoc/mandocdb.c 2014/09/03 18:09:14 1.161 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.150 2014/06/19 00:45:37 schwarze Exp $ */ +/* $OpenBSD: mandocdb.c,v 1.161 2014/09/03 18:09:14 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze @@ -15,18 +15,21 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif #include +#include #include #include #include #include #include +#if HAVE_FTS #include +#else +#include "compat_fts.h" +#endif #include #include #include @@ -36,7 +39,7 @@ #include #include -#ifdef HAVE_OHASH +#if HAVE_OHASH #include #else #include "compat_ohash.h" @@ -80,12 +83,6 @@ enum op { OP_TEST /* change no databases, report potential problems */ }; -enum form { - FORM_NONE, /* format is unknown */ - FORM_SRC, /* format is -man or -mdoc */ - FORM_CAT /* format is cat */ -}; - struct str { char *rendered; /* key in UTF-8 or ASCII form */ const struct mpage *mpage; /* if set, the owning parse */ @@ -101,24 +98,24 @@ struct inodev { struct mpage { struct inodev inodev; /* used for hashing routine */ int64_t pageid; /* pageid in mpages SQL table */ - enum form form; /* format from file content */ char *sec; /* section from file content */ char *arch; /* architecture from file content */ char *title; /* title from file content */ char *desc; /* description from file content */ struct mlink *mlinks; /* singly linked list */ + int form; /* format from file content */ }; struct mlink { char file[PATH_MAX]; /* filename rel. to manpath */ - enum form dform; /* format from directory */ - enum form fform; /* format from file name suffix */ char *dsec; /* section from directory */ char *arch; /* architecture from directory */ char *name; /* name from file name (not empty) */ char *fsec; /* section from file name suffix */ struct mlink *next; /* singly linked list */ struct mpage *mpage; /* parent */ + int dform; /* format from directory */ + int fform; /* format from file name suffix */ int gzip; /* filename has a .gz suffix */ }; @@ -145,8 +142,8 @@ static int dbopen(int); static void dbprune(void); static void filescan(const char *); static void *hash_alloc(size_t, void *); -static void hash_free(void *, size_t, void *); -static void *hash_halloc(size_t, void *); +static void hash_free(void *, void *); +static void *hash_calloc(size_t, size_t, void *); static void mlink_add(struct mlink *, const struct stat *); static void mlink_check(struct mpage *, struct mlink *); static void mlink_free(struct mlink *); @@ -336,8 +333,8 @@ main(int argc, char *argv[]) memset(&dirs, 0, sizeof(struct manpaths)); mpages_info.alloc = mlinks_info.alloc = hash_alloc; - mpages_info.halloc = mlinks_info.halloc = hash_halloc; - mpages_info.hfree = mlinks_info.hfree = hash_free; + mpages_info.calloc = mlinks_info.calloc = hash_calloc; + mpages_info.free = mlinks_info.free = hash_free; mpages_info.key_offset = offsetof(struct mpage, inodev); mlinks_info.key_offset = offsetof(struct mlink, file); @@ -355,7 +352,8 @@ main(int argc, char *argv[]) */ #define CHECKOP(_op, _ch) do \ if (OP_DEFAULT != (_op)) { \ - fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \ + fprintf(stderr, "%s: -%c: Conflicting option\n", \ + progname, (_ch)); \ goto usage; \ } while (/*CONSTCOND*/0) @@ -391,8 +389,9 @@ main(int argc, char *argv[]) break; case 'T': if (strcmp(optarg, "utf8")) { - fprintf(stderr, "-T%s: Unsupported " - "output format\n", optarg); + fprintf(stderr, "%s: -T%s: " + "Unsupported output format\n", + progname, optarg); goto usage; } write_utf8 = 1; @@ -419,7 +418,8 @@ main(int argc, char *argv[]) argv += optind; if (OP_CONFFILE == op && argc > 0) { - fprintf(stderr, "-C: Too many arguments\n"); + fprintf(stderr, "%s: -C: Too many arguments\n", + progname); goto usage; } @@ -506,8 +506,6 @@ main(int argc, char *argv[]) goto out; if (0 == treescan()) goto out; - if (0 == set_basedir(dirs.paths[j])) - goto out; if (0 == dbopen(0)) goto out; @@ -834,6 +832,7 @@ filescan(const char *file) } mlink = mandoc_calloc(1, sizeof(struct mlink)); + mlink->dform = FORM_NONE; if (strlcpy(mlink->file, start, sizeof(mlink->file)) >= sizeof(mlink->file)) { say(start, "Filename too long"); @@ -1090,8 +1089,8 @@ mpages_merge(struct mchars *mc, struct mparse *mp) enum mandoclevel lvl; str_info.alloc = hash_alloc; - str_info.halloc = hash_halloc; - str_info.hfree = hash_free; + str_info.calloc = hash_calloc; + str_info.free = hash_free; str_info.key_offset = offsetof(struct str, key); if (0 == nodb) @@ -1197,8 +1196,9 @@ mpages_merge(struct mchars *mc, struct mparse *mp) goto nextpage; } else if (NULL != mdoc) { mpage->form = FORM_SRC; - mpage->sec = - mandoc_strdup(mdoc_meta(mdoc)->msec); + mpage->sec = mdoc_meta(mdoc)->msec; + mpage->sec = mandoc_strdup( + NULL == mpage->sec ? "" : mpage->sec); mpage->arch = mdoc_meta(mdoc)->arch; mpage->arch = mandoc_strdup( NULL == mpage->arch ? "" : mpage->arch); @@ -1221,6 +1221,8 @@ mpages_merge(struct mchars *mc, struct mparse *mp) mpage->title = mandoc_strdup(mpage->mlinks->name); } + if (mpage->mlinks->gzip) + mpage->form |= FORM_GZ; putkey(mpage, mpage->sec, TYPE_sec); putkey(mpage, '\0' == *mpage->arch ? any : mpage->arch, TYPE_arch); @@ -1305,10 +1307,10 @@ names_check(void) say("", "%s", sqlite3_errmsg(db)); while (SQLITE_ROW == (irc = sqlite3_step(stmt))) { - name = sqlite3_column_text(stmt, 0); - sec = sqlite3_column_text(stmt, 1); - arch = sqlite3_column_text(stmt, 2); - key = sqlite3_column_text(stmt, 3); + name = (const char *)sqlite3_column_text(stmt, 0); + sec = (const char *)sqlite3_column_text(stmt, 1); + arch = (const char *)sqlite3_column_text(stmt, 2); + key = (const char *)sqlite3_column_text(stmt, 3); say("", "%s(%s%s%s) lacks mlink \"%s\"", name, sec, '\0' == *arch ? "" : "/", '\0' == *arch ? "" : arch, key); @@ -1327,6 +1329,8 @@ parse_cat(struct mpage *mpage, int fd) fopen(mpage->mlinks->file, "r") : fdopen(fd, "r"); if (NULL == stream) { + if (-1 != fd) + close(fd); if (warnings) say(mpage->mlinks->file, "&fopen"); return; @@ -1977,6 +1981,13 @@ dbadd_mlink(const struct mlink *mlink) SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, mlink->mpage->pageid); SQL_STEP(stmts[STMT_INSERT_LINK]); sqlite3_reset(stmts[STMT_INSERT_LINK]); + + i = 1; + SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, NAME_FILE); + SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, mlink->name); + SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mlink->mpage->pageid); + SQL_STEP(stmts[STMT_INSERT_NAME]); + sqlite3_reset(stmts[STMT_INSERT_NAME]); } /* @@ -2043,7 +2054,7 @@ dbadd(struct mpage *mpage, struct mchars *mc) i = 1; SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, key->rendered); - SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form); + SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, mpage->form); SQL_STEP(stmts[STMT_INSERT_PAGE]); mpage->pageid = sqlite3_last_insert_rowid(db); sqlite3_reset(stmts[STMT_INSERT_PAGE]); @@ -2350,10 +2361,10 @@ prepare_statements: } static void * -hash_halloc(size_t sz, void *arg) +hash_calloc(size_t n, size_t sz, void *arg) { - return(mandoc_calloc(1, sz)); + return(mandoc_calloc(n, sz)); } static void * @@ -2364,7 +2375,7 @@ hash_alloc(size_t sz, void *arg) } static void -hash_free(void *p, size_t sz, void *arg) +hash_free(void *p, void *arg) { free(p); @@ -2374,39 +2385,56 @@ static int set_basedir(const char *targetdir) { static char startdir[PATH_MAX]; - static int fd; + static int getcwd_status; /* 1 = ok, 2 = failure */ + static int chdir_status; /* 1 = changed directory */ char *cp; /* - * Remember where we started by keeping a fd open to the origin - * path component: throughout this utility, we chdir() a lot to - * handle relative paths, and by doing this, we can return to - * the starting point. + * Remember the original working directory, if possible. + * This will be needed if the second or a later directory + * on the command line is given as a relative path. + * Do not error out if the current directory is not + * searchable: Maybe it won't be needed after all. */ - if ('\0' == *startdir) { - if (NULL == getcwd(startdir, PATH_MAX)) { + if (0 == getcwd_status) { + if (NULL == getcwd(startdir, sizeof(startdir))) { + getcwd_status = 2; + (void)strlcpy(startdir, strerror(errno), + sizeof(startdir)); + } else + getcwd_status = 1; + } + + /* + * We are leaving the old base directory. + * Do not use it any longer, not even for messages. + */ + *basedir = '\0'; + + /* + * If and only if the directory was changed earlier and + * the next directory to process is given as a relative path, + * first go back, or bail out if that is impossible. + */ + if (chdir_status && '/' != *targetdir) { + if (2 == getcwd_status) { exitcode = (int)MANDOCLEVEL_SYSERR; - say("", "&getcwd"); + say("", "getcwd: %s", startdir); return(0); } - if (-1 == (fd = open(startdir, O_RDONLY, 0))) { + if (-1 == chdir(startdir)) { exitcode = (int)MANDOCLEVEL_SYSERR; - say("", "&open %s", startdir); - return(0); - } - } else { - if (-1 == fd) - return(0); - if (-1 == fchdir(fd)) { - close(fd); - basedir[0] = '\0'; - exitcode = (int)MANDOCLEVEL_SYSERR; say("", "&chdir %s", startdir); return(0); } } + + /* + * Always resolve basedir to the canonicalized absolute + * pathname and append a trailing slash, such that + * we can reliably check whether files are inside. + */ if (NULL == realpath(targetdir, basedir)) { - basedir[0] = '\0'; exitcode = (int)MANDOCLEVEL_BADARG; say("", "&%s: realpath", targetdir); return(0); @@ -2415,6 +2443,7 @@ set_basedir(const char *targetdir) say("", "&chdir"); return(0); } + chdir_status = 1; cp = strchr(basedir, '\0'); if ('/' != cp[-1]) { if (cp - basedir >= PATH_MAX - 1) { @@ -2437,7 +2466,7 @@ say(const char *file, const char *format, ...) if ('\0' != *basedir) fprintf(stderr, "%s", basedir); if ('\0' != *basedir && '\0' != *file) - fputs("//", stderr); + fputc('/', stderr); if ('\0' != *file) fprintf(stderr, "%s", file);