]> git.cameronkatri.com Git - mandoc.git/blobdiff - mandocdb.c
Merge OpenBSD rev. 1.108 by sthen@; original commit message:
[mandoc.git] / mandocdb.c
index 71a233da3d1ed0dba1f31db83559dcad17eea007..2798efcafa0a72d1d7670659ed72ee938fbf00a9 100644 (file)
@@ -1,4 +1,4 @@
-/*     $Id: mandocdb.c,v 1.145 2014/04/25 12:13:15 schwarze Exp $ */
+/*     $Id: mandocdb.c,v 1.149 2014/06/18 19:34:04 schwarze Exp $ */
 /*
  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -512,7 +512,7 @@ main(int argc, char *argv[])
                                goto out;
 
                        mpages_merge(mc, mp);
-                       if (warnings &&
+                       if (warnings && !nodb &&
                            ! (MPARSE_QUICK & mparse_options))
                                names_check();
                        dbclose(0);
@@ -1996,6 +1996,18 @@ dbadd(struct mpage *mpage, struct mchars *mc)
        mlink = mpage->mlinks;
 
        if (nodb) {
+               for (key = ohash_first(&names, &slot); NULL != key;
+                    key = ohash_next(&names, &slot)) {
+                       if (key->rendered != key->key)
+                               free(key->rendered);
+                       free(key);
+               }
+               for (key = ohash_first(&strings, &slot); NULL != key;
+                    key = ohash_next(&strings, &slot)) {
+                       if (key->rendered != key->key)
+                               free(key->rendered);
+                       free(key);
+               }
                if (0 == debug)
                        return;
                while (NULL != mlink) {
@@ -2024,13 +2036,22 @@ dbadd(struct mpage *mpage, struct mchars *mc)
        if (debug)
                say(mlink->file, "Adding to database");
 
+       i = strlen(mpage->desc) + 1;
+       key = mandoc_calloc(1, sizeof(struct str) + i);
+       memcpy(key->key, mpage->desc, i);
+       render_key(mc, key);
+
        i = 1;
-       SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, mpage->desc);
+       SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, key->rendered);
        SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form);
        SQL_STEP(stmts[STMT_INSERT_PAGE]);
        mpage->pageid = sqlite3_last_insert_rowid(db);
        sqlite3_reset(stmts[STMT_INSERT_PAGE]);
 
+       if (key->rendered != key->key)
+               free(key->rendered);
+       free(key);
+
        while (NULL != mlink) {
                dbadd_mlink(mlink);
                mlink = mlink->next;
@@ -2209,7 +2230,8 @@ dbopen(int real)
                rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL);
                if (SQLITE_OK != rc) {
                        exitcode = (int)MANDOCLEVEL_SYSERR;
-                       say(MANDOC_DB, "%s", sqlite3_errmsg(db));
+                       if (SQLITE_CANTOPEN != rc)
+                               say(MANDOC_DB, "%s", sqlite3_errstr(rc));
                        return(0);
                }
                goto prepare_statements;
@@ -2223,7 +2245,7 @@ dbopen(int real)
                goto create_tables;
        if (MPARSE_QUICK & mparse_options) {
                exitcode = (int)MANDOCLEVEL_SYSERR;
-               say(MANDOC_DB "~", "%s", sqlite3_errmsg(db));
+               say(MANDOC_DB "~", "%s", sqlite3_errstr(rc));
                return(0);
        }
 
@@ -2239,7 +2261,7 @@ dbopen(int real)
        rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL);
        if (SQLITE_OK != rc) {
                exitcode = (int)MANDOCLEVEL_SYSERR;
-               say("", "%s: %s", tempfilename, sqlite3_errmsg(db));
+               say("", "%s: %s", tempfilename, sqlite3_errstr(rc));
                return(0);
        }
 
@@ -2277,11 +2299,20 @@ create_tables:
        if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
                exitcode = (int)MANDOCLEVEL_SYSERR;
                say(MANDOC_DB, "%s", sqlite3_errmsg(db));
+               sqlite3_close(db);
                return(0);
        }
 
 prepare_statements:
-       SQL_EXEC("PRAGMA foreign_keys = ON");
+       if (SQLITE_OK != sqlite3_exec(db,
+           "PRAGMA foreign_keys = ON", NULL, NULL, NULL)) {
+               exitcode = (int)MANDOCLEVEL_SYSERR;
+               say(MANDOC_DB, "PRAGMA foreign_keys: %s",
+                   sqlite3_errmsg(db));
+               sqlite3_close(db);
+               return(0);
+       }
+
        sql = "DELETE FROM mpages WHERE pageid IN "
                "(SELECT pageid FROM mlinks WHERE "
                "sec=? AND arch=? AND name=?)";
@@ -2305,8 +2336,14 @@ prepare_statements:
         * synchronous mode for much better performance.
         */
 
-       if (real)
-               SQL_EXEC("PRAGMA synchronous = OFF");
+       if (real && SQLITE_OK != sqlite3_exec(db,
+           "PRAGMA synchronous = OFF", NULL, NULL, NULL)) {
+               exitcode = (int)MANDOCLEVEL_SYSERR;
+               say(MANDOC_DB, "PRAGMA synchronous: %s",
+               sqlite3_errmsg(db));
+               sqlite3_close(db);
+               return(0);
+       }
 #endif
 
        return(1);