version 1.172, 2014/12/04 17:36:00 |
version 1.186, 2015/03/13 00:19:41 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
}; |
}; |
|
|
struct str { |
struct str { |
char *rendered; /* key in UTF-8 or ASCII form */ |
|
const struct mpage *mpage; /* if set, the owning parse */ |
const struct mpage *mpage; /* if set, the owning parse */ |
uint64_t mask; /* bitmask in sequence */ |
uint64_t mask; /* bitmask in sequence */ |
char key[]; /* may contain escape sequences */ |
char key[]; /* rendered text */ |
}; |
}; |
|
|
struct inodev { |
struct inodev { |
|
|
char *desc; /* description from file content */ |
char *desc; /* description from file content */ |
struct mlink *mlinks; /* singly linked list */ |
struct mlink *mlinks; /* singly linked list */ |
int form; /* format from file content */ |
int form; /* format from file content */ |
|
int name_head_done; |
}; |
}; |
|
|
struct mlink { |
struct mlink { |
|
|
STMT_INSERT_PAGE, /* insert mpage */ |
STMT_INSERT_PAGE, /* insert mpage */ |
STMT_INSERT_LINK, /* insert mlink */ |
STMT_INSERT_LINK, /* insert mlink */ |
STMT_INSERT_NAME, /* insert name */ |
STMT_INSERT_NAME, /* insert name */ |
|
STMT_SELECT_NAME, /* retrieve existing name flags */ |
STMT_INSERT_KEY, /* insert parsed key */ |
STMT_INSERT_KEY, /* insert parsed key */ |
STMT__MAX |
STMT__MAX |
}; |
}; |
Line 137 struct mdoc_handler { |
|
Line 138 struct mdoc_handler { |
|
}; |
}; |
|
|
static void dbclose(int); |
static void dbclose(int); |
static void dbadd(struct mpage *, struct mchars *); |
static void dbadd(struct mpage *); |
static void dbadd_mlink(const struct mlink *mlink); |
static void dbadd_mlink(const struct mlink *mlink); |
static void dbadd_mlink_name(const struct mlink *mlink); |
static void dbadd_mlink_name(const struct mlink *mlink); |
static int dbopen(int); |
static int dbopen(int); |
Line 151 static void mlink_check(struct mpage *, struct mlink |
|
Line 152 static void mlink_check(struct mpage *, struct mlink |
|
static void mlink_free(struct mlink *); |
static void mlink_free(struct mlink *); |
static void mlinks_undupe(struct mpage *); |
static void mlinks_undupe(struct mpage *); |
static void mpages_free(void); |
static void mpages_free(void); |
static void mpages_merge(struct mchars *, struct mparse *); |
static void mpages_merge(struct mparse *); |
static void names_check(void); |
static void names_check(void); |
static void parse_cat(struct mpage *, int); |
static void parse_cat(struct mpage *, int); |
static void parse_man(struct mpage *, const struct man_node *); |
static void parse_man(struct mpage *, const struct man_meta *, |
|
const struct man_node *); |
static void parse_mdoc(struct mpage *, const struct mdoc_meta *, |
static void parse_mdoc(struct mpage *, const struct mdoc_meta *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
static int parse_mdoc_body(struct mpage *, const struct mdoc_meta *, |
static int parse_mdoc_body(struct mpage *, const struct mdoc_meta *, |
Line 163 static int parse_mdoc_head(struct mpage *, const stru |
|
Line 165 static int parse_mdoc_head(struct mpage *, const stru |
|
const struct mdoc_node *); |
const struct mdoc_node *); |
static int parse_mdoc_Fd(struct mpage *, const struct mdoc_meta *, |
static int parse_mdoc_Fd(struct mpage *, const struct mdoc_meta *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
|
static void parse_mdoc_fname(struct mpage *, const struct mdoc_node *); |
static int parse_mdoc_Fn(struct mpage *, const struct mdoc_meta *, |
static int parse_mdoc_Fn(struct mpage *, const struct mdoc_meta *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
|
static int parse_mdoc_Fo(struct mpage *, const struct mdoc_meta *, |
|
const struct mdoc_node *); |
static int parse_mdoc_Nd(struct mpage *, const struct mdoc_meta *, |
static int parse_mdoc_Nd(struct mpage *, const struct mdoc_meta *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
static int parse_mdoc_Nm(struct mpage *, const struct mdoc_meta *, |
static int parse_mdoc_Nm(struct mpage *, const struct mdoc_meta *, |
Line 174 static int parse_mdoc_Sh(struct mpage *, const struct |
|
Line 179 static int parse_mdoc_Sh(struct mpage *, const struct |
|
static int parse_mdoc_Xr(struct mpage *, const struct mdoc_meta *, |
static int parse_mdoc_Xr(struct mpage *, const struct mdoc_meta *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
static void putkey(const struct mpage *, char *, uint64_t); |
static void putkey(const struct mpage *, char *, uint64_t); |
static void putkeys(const struct mpage *, |
static void putkeys(const struct mpage *, char *, size_t, uint64_t); |
const char *, size_t, uint64_t); |
|
static void putmdockey(const struct mpage *, |
static void putmdockey(const struct mpage *, |
const struct mdoc_node *, uint64_t); |
const struct mdoc_node *, uint64_t); |
static void render_key(struct mchars *, struct str *); |
static int render_string(char **, size_t *); |
static void say(const char *, const char *, ...); |
static void say(const char *, const char *, ...); |
static int set_basedir(const char *, int); |
static int set_basedir(const char *, int); |
static int treescan(void); |
static int treescan(void); |
Line 195 static int write_utf8; /* write UTF-8 output; else A |
|
Line 199 static int write_utf8; /* write UTF-8 output; else A |
|
static int exitcode; /* to be returned by main */ |
static int exitcode; /* to be returned by main */ |
static enum op op; /* operational mode */ |
static enum op op; /* operational mode */ |
static char basedir[PATH_MAX]; /* current base directory */ |
static char basedir[PATH_MAX]; /* current base directory */ |
|
static struct mchars *mchars; /* table of named characters */ |
static struct ohash mpages; /* table of distinct manual pages */ |
static struct ohash mpages; /* table of distinct manual pages */ |
static struct ohash mlinks; /* table of directory entries */ |
static struct ohash mlinks; /* table of directory entries */ |
static struct ohash names; /* table of all names */ |
static struct ohash names; /* table of all names */ |
Line 300 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
Line 305 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
{ NULL, 0 }, /* Ux */ |
{ NULL, 0 }, /* Ux */ |
{ NULL, 0 }, /* Xc */ |
{ NULL, 0 }, /* Xc */ |
{ NULL, 0 }, /* Xo */ |
{ NULL, 0 }, /* Xo */ |
{ parse_mdoc_head, 0 }, /* Fo */ |
{ parse_mdoc_Fo, 0 }, /* Fo */ |
{ NULL, 0 }, /* Fc */ |
{ NULL, 0 }, /* Fc */ |
{ NULL, 0 }, /* Oo */ |
{ NULL, 0 }, /* Oo */ |
{ NULL, 0 }, /* Oc */ |
{ NULL, 0 }, /* Oc */ |
Line 331 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
Line 336 static const struct mdoc_handler mdocs[MDOC_MAX] = { |
|
|
|
|
|
int |
int |
main(int argc, char *argv[]) |
mandocdb(int argc, char *argv[]) |
{ |
{ |
int ch, i; |
int ch, i; |
size_t j, sz; |
size_t j, sz; |
const char *path_arg; |
const char *path_arg; |
struct mchars *mc; |
|
struct manpaths dirs; |
struct manpaths dirs; |
struct mparse *mp; |
struct mparse *mp; |
struct ohash_info mpages_info, mlinks_info; |
struct ohash_info mpages_info, mlinks_info; |
Line 346 main(int argc, char *argv[]) |
|
Line 350 main(int argc, char *argv[]) |
|
|
|
mpages_info.alloc = mlinks_info.alloc = hash_alloc; |
mpages_info.alloc = mlinks_info.alloc = hash_alloc; |
mpages_info.calloc = mlinks_info.calloc = hash_calloc; |
mpages_info.calloc = mlinks_info.calloc = hash_calloc; |
mpages_info.free = mlinks_info.free = hash_free; |
mpages_info.free = mlinks_info.free = hash_free; |
|
mpages_info.data = mlinks_info.data = NULL; |
|
|
mpages_info.key_offset = offsetof(struct mpage, inodev); |
mpages_info.key_offset = offsetof(struct mpage, inodev); |
mlinks_info.key_offset = offsetof(struct mlink, file); |
mlinks_info.key_offset = offsetof(struct mlink, file); |
Line 436 main(int argc, char *argv[]) |
|
Line 441 main(int argc, char *argv[]) |
|
} |
} |
|
|
exitcode = (int)MANDOCLEVEL_OK; |
exitcode = (int)MANDOCLEVEL_OK; |
mc = mchars_alloc(); |
mchars = mchars_alloc(); |
mp = mparse_alloc(mparse_options, MANDOCLEVEL_FATAL, NULL, |
mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, NULL, |
mc, NULL); |
mchars, NULL); |
ohash_init(&mpages, 6, &mpages_info); |
ohash_init(&mpages, 6, &mpages_info); |
ohash_init(&mlinks, 6, &mlinks_info); |
ohash_init(&mlinks, 6, &mlinks_info); |
|
|
Line 474 main(int argc, char *argv[]) |
|
Line 479 main(int argc, char *argv[]) |
|
goto out; |
goto out; |
} |
} |
if (OP_DELETE != op) |
if (OP_DELETE != op) |
mpages_merge(mc, mp); |
mpages_merge(mp); |
dbclose(OP_DEFAULT == op ? 0 : 1); |
dbclose(OP_DEFAULT == op ? 0 : 1); |
} else { |
} else { |
/* |
/* |
Line 521 main(int argc, char *argv[]) |
|
Line 526 main(int argc, char *argv[]) |
|
if (0 == dbopen(0)) |
if (0 == dbopen(0)) |
continue; |
continue; |
|
|
mpages_merge(mc, mp); |
mpages_merge(mp); |
if (warnings && !nodb && |
if (warnings && !nodb && |
! (MPARSE_QUICK & mparse_options)) |
! (MPARSE_QUICK & mparse_options)) |
names_check(); |
names_check(); |
Line 537 main(int argc, char *argv[]) |
|
Line 542 main(int argc, char *argv[]) |
|
out: |
out: |
manpath_free(&dirs); |
manpath_free(&dirs); |
mparse_free(mp); |
mparse_free(mp); |
mchars_free(mc); |
mchars_free(mchars); |
mpages_free(); |
mpages_free(); |
ohash_delete(&mpages); |
ohash_delete(&mpages); |
ohash_delete(&mlinks); |
ohash_delete(&mlinks); |
|
|
say(path, "&realpath"); |
say(path, "&realpath"); |
continue; |
continue; |
} |
} |
if (strstr(buf, basedir) != buf) { |
if (strstr(buf, basedir) != buf |
|
#ifdef HOMEBREWDIR |
|
&& strstr(buf, HOMEBREWDIR) != buf |
|
#endif |
|
) { |
if (warnings) say("", |
if (warnings) say("", |
"%s: outside base directory", buf); |
"%s: outside base directory", buf); |
continue; |
continue; |
|
|
say(path, "Skip pdf"); |
say(path, "Skip pdf"); |
continue; |
continue; |
} else if ( ! use_all && |
} else if ( ! use_all && |
((FORM_SRC == dform && strcmp(fsec, dsec)) || |
((FORM_SRC == dform && |
|
strncmp(fsec, dsec, strlen(dsec))) || |
(FORM_CAT == dform && strcmp(fsec, "0")))) { |
(FORM_CAT == dform && strcmp(fsec, "0")))) { |
if (warnings) |
if (warnings) |
say(path, "Wrong filename suffix"); |
say(path, "Wrong filename suffix"); |
Line 813 filescan(const char *file) |
|
Line 823 filescan(const char *file) |
|
start = buf; |
start = buf; |
else if (strstr(buf, basedir) == buf) |
else if (strstr(buf, basedir) == buf) |
start = buf + strlen(basedir); |
start = buf + strlen(basedir); |
|
#ifdef HOMEBREWDIR |
|
else if (strstr(buf, HOMEBREWDIR) == buf) |
|
start = buf; |
|
#endif |
else { |
else { |
exitcode = (int)MANDOCLEVEL_BADARG; |
exitcode = (int)MANDOCLEVEL_BADARG; |
say("", "%s: outside base directory", buf); |
say("", "%s: outside base directory", buf); |
Line 848 filescan(const char *file) |
|
Line 862 filescan(const char *file) |
|
if (strlcpy(mlink->file, start, sizeof(mlink->file)) >= |
if (strlcpy(mlink->file, start, sizeof(mlink->file)) >= |
sizeof(mlink->file)) { |
sizeof(mlink->file)) { |
say(start, "Filename too long"); |
say(start, "Filename too long"); |
|
free(mlink); |
return; |
return; |
} |
} |
|
|
Line 927 mlink_add(struct mlink *mlink, const struct stat *st) |
|
Line 942 mlink_add(struct mlink *mlink, const struct stat *st) |
|
assert(NULL == ohash_find(&mlinks, slot)); |
assert(NULL == ohash_find(&mlinks, slot)); |
ohash_insert(&mlinks, slot, mlink); |
ohash_insert(&mlinks, slot, mlink); |
|
|
|
memset(&inodev, 0, sizeof(inodev)); /* Clear padding. */ |
inodev.st_ino = st->st_ino; |
inodev.st_ino = st->st_ino; |
inodev.st_dev = st->st_dev; |
inodev.st_dev = st->st_dev; |
slot = ohash_lookup_memory(&mpages, (char *)&inodev, |
slot = ohash_lookup_memory(&mpages, (char *)&inodev, |
Line 1084 mlink_check(struct mpage *mpage, struct mlink *mlink) |
|
Line 1100 mlink_check(struct mpage *mpage, struct mlink *mlink) |
|
* and filename to determine whether the file is parsable or not. |
* and filename to determine whether the file is parsable or not. |
*/ |
*/ |
static void |
static void |
mpages_merge(struct mchars *mc, struct mparse *mp) |
mpages_merge(struct mparse *mp) |
{ |
{ |
char any[] = "any"; |
char any[] = "any"; |
struct ohash_info str_info; |
struct ohash_info str_info; |
Line 1096 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
Line 1112 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
char *cp; |
char *cp; |
int fd; |
int fd; |
unsigned int pslot; |
unsigned int pslot; |
enum mandoclevel lvl; |
|
|
|
str_info.alloc = hash_alloc; |
str_info.alloc = hash_alloc; |
str_info.calloc = hash_calloc; |
str_info.calloc = hash_calloc; |
str_info.free = hash_free; |
str_info.free = hash_free; |
|
str_info.data = NULL; |
str_info.key_offset = offsetof(struct str, key); |
str_info.key_offset = offsetof(struct str, key); |
|
|
if ( ! nodb) |
if ( ! nodb) |
Line 1109 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
Line 1125 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
mpage = ohash_first(&mpages, &pslot); |
mpage = ohash_first(&mpages, &pslot); |
while (mpage != NULL) { |
while (mpage != NULL) { |
mlinks_undupe(mpage); |
mlinks_undupe(mpage); |
if (mpage->mlinks == NULL) { |
if ((mlink = mpage->mlinks) == NULL) { |
mpage = ohash_next(&mpages, &pslot); |
mpage = ohash_next(&mpages, &pslot); |
continue; |
continue; |
} |
} |
Line 1122 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
Line 1138 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
man = NULL; |
man = NULL; |
sodest = NULL; |
sodest = NULL; |
|
|
mparse_open(mp, &fd, mpage->mlinks->file); |
mparse_open(mp, &fd, mlink->file); |
if (fd == -1) { |
if (fd == -1) { |
say(mpage->mlinks->file, "&open"); |
say(mlink->file, "&open"); |
goto nextpage; |
goto nextpage; |
} |
} |
|
|
/* |
/* |
* Try interpreting the file as mdoc(7) or man(7) |
* Interpret the file as mdoc(7) or man(7) source |
* source code, unless it is already known to be |
* code, unless it is known to be formatted. |
* formatted. Fall back to formatted mode. |
|
*/ |
*/ |
if (mpage->mlinks->dform != FORM_CAT || |
if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) { |
mpage->mlinks->fform != FORM_CAT) { |
mparse_readfd(mp, fd, mlink->file); |
lvl = mparse_readfd(mp, fd, mpage->mlinks->file); |
mparse_result(mp, &mdoc, &man, &sodest); |
if (lvl < MANDOCLEVEL_FATAL) |
|
mparse_result(mp, &mdoc, &man, &sodest); |
|
} |
} |
|
|
if (sodest != NULL) { |
if (sodest != NULL) { |
Line 1154 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
Line 1167 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
/* The .so target exists. */ |
/* The .so target exists. */ |
|
|
mpage_dest = mlink_dest->mpage; |
mpage_dest = mlink_dest->mpage; |
mlink = mpage->mlinks; |
|
while (1) { |
while (1) { |
mlink->mpage = mpage_dest; |
mlink->mpage = mpage_dest; |
|
|
Line 1194 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
Line 1206 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
mandoc_strdup(mdoc_meta(mdoc)->title); |
mandoc_strdup(mdoc_meta(mdoc)->title); |
} else if (man != NULL) { |
} else if (man != NULL) { |
mpage->form = FORM_SRC; |
mpage->form = FORM_SRC; |
mpage->sec = |
mpage->sec = mandoc_strdup(man_meta(man)->msec); |
mandoc_strdup(man_meta(man)->msec); |
mpage->arch = mandoc_strdup(mlink->arch); |
mpage->arch = |
mpage->title = mandoc_strdup(man_meta(man)->title); |
mandoc_strdup(mpage->mlinks->arch); |
|
mpage->title = |
|
mandoc_strdup(man_meta(man)->title); |
|
} else { |
} else { |
mpage->form = FORM_CAT; |
mpage->form = FORM_CAT; |
mpage->sec = |
mpage->sec = mandoc_strdup(mlink->dsec); |
mandoc_strdup(mpage->mlinks->dsec); |
mpage->arch = mandoc_strdup(mlink->arch); |
mpage->arch = |
mpage->title = mandoc_strdup(mlink->name); |
mandoc_strdup(mpage->mlinks->arch); |
|
mpage->title = |
|
mandoc_strdup(mpage->mlinks->name); |
|
} |
} |
putkey(mpage, mpage->sec, TYPE_sec); |
putkey(mpage, mpage->sec, TYPE_sec); |
if (*mpage->arch != '\0') |
if (*mpage->arch != '\0') |
putkey(mpage, mpage->arch, TYPE_arch); |
putkey(mpage, mpage->arch, TYPE_arch); |
|
|
for (mlink = mpage->mlinks; mlink; mlink = mlink->next) { |
for ( ; mlink != NULL; mlink = mlink->next) { |
if ('\0' != *mlink->dsec) |
if ('\0' != *mlink->dsec) |
putkey(mpage, mlink->dsec, TYPE_sec); |
putkey(mpage, mlink->dsec, TYPE_sec); |
if ('\0' != *mlink->fsec) |
if ('\0' != *mlink->fsec) |
Line 1223 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
Line 1229 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
putkey(mpage, mlink->name, NAME_FILE); |
putkey(mpage, mlink->name, NAME_FILE); |
} |
} |
|
|
assert(NULL == mpage->desc); |
assert(mpage->desc == NULL); |
if (NULL != mdoc) { |
if (mdoc != NULL) |
if (NULL != (cp = mdoc_meta(mdoc)->name)) |
|
putkey(mpage, cp, NAME_HEAD); |
|
parse_mdoc(mpage, mdoc_meta(mdoc), mdoc_node(mdoc)); |
parse_mdoc(mpage, mdoc_meta(mdoc), mdoc_node(mdoc)); |
} else if (NULL != man) |
else if (man != NULL) |
parse_man(mpage, man_node(man)); |
parse_man(mpage, man_meta(man), man_node(man)); |
else |
else |
parse_cat(mpage, fd); |
parse_cat(mpage, fd); |
if (NULL == mpage->desc) |
if (mpage->desc == NULL) |
mpage->desc = mandoc_strdup(mpage->mlinks->name); |
mpage->desc = mandoc_strdup(mpage->mlinks->name); |
|
|
if (warnings && !use_all) |
if (warnings && !use_all) |
Line 1240 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
Line 1244 mpages_merge(struct mchars *mc, struct mparse *mp) |
|
mlink = mlink->next) |
mlink = mlink->next) |
mlink_check(mpage, mlink); |
mlink_check(mpage, mlink); |
|
|
dbadd(mpage, mc); |
dbadd(mpage); |
|
mlink = mpage->mlinks; |
|
|
nextpage: |
nextpage: |
if (mparse_wait(mp) != MANDOCLEVEL_OK) { |
if (mparse_wait(mp) != MANDOCLEVEL_OK) { |
exitcode = (int)MANDOCLEVEL_SYSERR; |
exitcode = (int)MANDOCLEVEL_SYSERR; |
say(mpage->mlinks->file, "&wait gunzip"); |
say(mlink->file, "&wait gunzip"); |
} |
} |
ohash_delete(&strings); |
ohash_delete(&strings); |
ohash_delete(&names); |
ohash_delete(&names); |
Line 1437 putmdockey(const struct mpage *mpage, |
|
Line 1442 putmdockey(const struct mpage *mpage, |
|
} |
} |
|
|
static void |
static void |
parse_man(struct mpage *mpage, const struct man_node *n) |
parse_man(struct mpage *mpage, const struct man_meta *meta, |
|
const struct man_node *n) |
{ |
{ |
const struct man_node *head, *body; |
const struct man_node *head, *body; |
char *start, *title; |
char *start, *title; |
Line 1503 parse_man(struct mpage *mpage, const struct man_node * |
|
Line 1509 parse_man(struct mpage *mpage, const struct man_node * |
|
break; |
break; |
|
|
putkey(mpage, start, NAME_TITLE); |
putkey(mpage, start, NAME_TITLE); |
|
if ( ! (mpage->name_head_done || |
|
strcasecmp(start, meta->title))) { |
|
putkey(mpage, start, NAME_HEAD); |
|
mpage->name_head_done = 1; |
|
} |
|
|
if (' ' == byte) { |
if (' ' == byte) { |
start += sz + 1; |
start += sz + 1; |
Line 1517 parse_man(struct mpage *mpage, const struct man_node * |
|
Line 1528 parse_man(struct mpage *mpage, const struct man_node * |
|
|
|
if (start == title) { |
if (start == title) { |
putkey(mpage, start, NAME_TITLE); |
putkey(mpage, start, NAME_TITLE); |
|
if ( ! (mpage->name_head_done || |
|
strcasecmp(start, meta->title))) { |
|
putkey(mpage, start, NAME_HEAD); |
|
mpage->name_head_done = 1; |
|
} |
free(title); |
free(title); |
return; |
return; |
} |
} |
Line 1547 parse_man(struct mpage *mpage, const struct man_node * |
|
Line 1563 parse_man(struct mpage *mpage, const struct man_node * |
|
for (n = n->child; n; n = n->next) { |
for (n = n->child; n; n = n->next) { |
if (NULL != mpage->desc) |
if (NULL != mpage->desc) |
break; |
break; |
parse_man(mpage, n); |
parse_man(mpage, meta, n); |
} |
} |
} |
} |
|
|
|
|
parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_meta *meta, |
parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_meta *meta, |
const struct mdoc_node *n) |
const struct mdoc_node *n) |
{ |
{ |
const char *start, *end; |
char *start, *end; |
size_t sz; |
size_t sz; |
|
|
if (SEC_SYNOPSIS != n->sec || |
if (SEC_SYNOPSIS != n->sec || |
Line 1628 parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_m |
|
Line 1644 parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_m |
|
return(0); |
return(0); |
} |
} |
|
|
|
static void |
|
parse_mdoc_fname(struct mpage *mpage, const struct mdoc_node *n) |
|
{ |
|
char *cp; |
|
size_t sz; |
|
|
|
if (n->type != MDOC_TEXT) |
|
return; |
|
|
|
/* Skip function pointer punctuation. */ |
|
|
|
cp = n->string; |
|
while (*cp == '(' || *cp == '*') |
|
cp++; |
|
sz = strcspn(cp, "()"); |
|
|
|
putkeys(mpage, cp, sz, TYPE_Fn); |
|
if (n->sec == SEC_SYNOPSIS) |
|
putkeys(mpage, cp, sz, NAME_SYN); |
|
} |
|
|
static int |
static int |
parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_meta *meta, |
parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_meta *meta, |
const struct mdoc_node *n) |
const struct mdoc_node *n) |
{ |
{ |
char *cp; |
|
|
|
if (NULL == (n = n->child) || MDOC_TEXT != n->type) |
if (n->child == NULL) |
return(0); |
return(0); |
|
|
/* |
parse_mdoc_fname(mpage, n->child); |
* Parse: .Fn "struct type *name" "char *arg". |
|
* First strip away pointer symbol. |
|
* Then store the function name, then type. |
|
* Finally, store the arguments. |
|
*/ |
|
|
|
if (NULL == (cp = strrchr(n->string, ' '))) |
for (n = n->child->next; n != NULL; n = n->next) |
cp = n->string; |
if (n->type == MDOC_TEXT) |
|
putkey(mpage, n->string, TYPE_Fa); |
|
|
while ('*' == *cp) |
return(0); |
cp++; |
} |
|
|
putkey(mpage, cp, TYPE_Fn); |
static int |
|
parse_mdoc_Fo(struct mpage *mpage, const struct mdoc_meta *meta, |
|
const struct mdoc_node *n) |
|
{ |
|
|
if (n->string < cp) |
if (n->type != MDOC_HEAD) |
putkeys(mpage, n->string, cp - n->string, TYPE_Ft); |
return(1); |
|
|
for (n = n->next; NULL != n; n = n->next) |
if (n->child != NULL) |
if (MDOC_TEXT == n->type) |
parse_mdoc_fname(mpage, n->child); |
putkey(mpage, n->string, TYPE_Fa); |
|
|
|
return(0); |
return(0); |
} |
} |
Line 1705 parse_mdoc_Nm(struct mpage *mpage, const struct mdoc_m |
|
Line 1739 parse_mdoc_Nm(struct mpage *mpage, const struct mdoc_m |
|
else |
else |
putmdockey(mpage, n->child, NAME_SYN); |
putmdockey(mpage, n->child, NAME_SYN); |
} |
} |
|
if ( ! (mpage->name_head_done || |
|
n->child == NULL || n->child->string == NULL || |
|
strcasecmp(n->child->string, meta->title))) { |
|
putkey(mpage, n->child->string, NAME_HEAD); |
|
mpage->name_head_done = 1; |
|
} |
return(0); |
return(0); |
} |
} |
|
|
Line 1738 parse_mdoc_body(struct mpage *mpage, const struct mdoc |
|
Line 1778 parse_mdoc_body(struct mpage *mpage, const struct mdoc |
|
* When we finish the manual, we'll dump the table. |
* When we finish the manual, we'll dump the table. |
*/ |
*/ |
static void |
static void |
putkeys(const struct mpage *mpage, |
putkeys(const struct mpage *mpage, char *cp, size_t sz, uint64_t v) |
const char *cp, size_t sz, uint64_t v) |
|
{ |
{ |
struct ohash *htab; |
struct ohash *htab; |
struct str *s; |
struct str *s; |
const char *end; |
const char *end; |
unsigned int slot; |
unsigned int slot; |
int i; |
int i, mustfree; |
|
|
if (0 == sz) |
if (0 == sz) |
return; |
return; |
|
|
|
mustfree = render_string(&cp, &sz); |
|
|
if (TYPE_Nm & v) { |
if (TYPE_Nm & v) { |
htab = &names; |
htab = &names; |
v &= name_mask; |
v &= name_mask; |
Line 1757 putkeys(const struct mpage *mpage, |
|
Line 1798 putkeys(const struct mpage *mpage, |
|
name_mask &= ~NAME_FIRST; |
name_mask &= ~NAME_FIRST; |
if (debug > 1) |
if (debug > 1) |
say(mpage->mlinks->file, |
say(mpage->mlinks->file, |
"Adding name %*s", sz, cp); |
"Adding name %*s, bits=%d", sz, cp, v); |
} else { |
} else { |
htab = &strings; |
htab = &strings; |
if (debug > 1) |
if (debug > 1) |
Line 1782 putkeys(const struct mpage *mpage, |
|
Line 1823 putkeys(const struct mpage *mpage, |
|
} |
} |
s->mpage = mpage; |
s->mpage = mpage; |
s->mask = v; |
s->mask = v; |
|
|
|
if (mustfree) |
|
free(cp); |
} |
} |
|
|
/* |
/* |
Line 1837 utf8(unsigned int cp, char out[7]) |
|
Line 1881 utf8(unsigned int cp, char out[7]) |
|
} |
} |
|
|
/* |
/* |
* Store the rendered version of a key, or alias the pointer |
* If the string contains escape sequences, |
* if the key contains no escape sequences. |
* replace it with an allocated rendering and return 1, |
|
* such that the caller can free it after use. |
|
* Otherwise, do nothing and return 0. |
*/ |
*/ |
static void |
static int |
render_key(struct mchars *mc, struct str *key) |
render_string(char **public, size_t *psz) |
{ |
{ |
size_t sz, bsz, pos; |
const char *src, *scp, *addcp, *seq; |
|
char *dst; |
|
size_t ssz, dsz, addsz; |
char utfbuf[7], res[6]; |
char utfbuf[7], res[6]; |
char *buf; |
int seqlen, unicode; |
const char *seq, *cpp, *val; |
|
int len, u; |
|
enum mandoc_esc esc; |
|
|
|
assert(NULL == key->rendered); |
|
|
|
res[0] = '\\'; |
res[0] = '\\'; |
res[1] = '\t'; |
res[1] = '\t'; |
res[2] = ASCII_NBRSP; |
res[2] = ASCII_NBRSP; |
Line 1859 render_key(struct mchars *mc, struct str *key) |
|
Line 1902 render_key(struct mchars *mc, struct str *key) |
|
res[4] = ASCII_BREAK; |
res[4] = ASCII_BREAK; |
res[5] = '\0'; |
res[5] = '\0'; |
|
|
val = key->key; |
src = scp = *public; |
bsz = strlen(val); |
ssz = *psz; |
|
dst = NULL; |
|
dsz = 0; |
|
|
/* |
while (scp < src + *psz) { |
* Pre-check: if we have no stop-characters, then set the |
|
* pointer as ourselvse and get out of here. |
|
*/ |
|
if (strcspn(val, res) == bsz) { |
|
key->rendered = key->key; |
|
return; |
|
} |
|
|
|
/* Pre-allocate by the length of the input */ |
/* Leave normal characters unchanged. */ |
|
|
buf = mandoc_malloc(++bsz); |
if (strchr(res, *scp) == NULL) { |
pos = 0; |
if (dst != NULL) |
|
dst[dsz++] = *scp; |
|
scp++; |
|
continue; |
|
} |
|
|
while ('\0' != *val) { |
|
/* |
/* |
* Halt on the first escape sequence. |
* Found something that requires replacing, |
* This also halts on the end of string, in which case |
* make sure we have a destination buffer. |
* we just copy, fallthrough, and exit the loop. |
|
*/ |
*/ |
if ((sz = strcspn(val, res)) > 0) { |
|
memcpy(&buf[pos], val, sz); |
if (dst == NULL) { |
pos += sz; |
dst = mandoc_malloc(ssz + 1); |
val += sz; |
dsz = scp - src; |
|
memcpy(dst, src, dsz); |
} |
} |
|
|
switch (*val) { |
/* Handle single-char special characters. */ |
case ASCII_HYPH: |
|
buf[pos++] = '-'; |
switch (*scp) { |
val++; |
case '\\': |
continue; |
break; |
case '\t': |
case '\t': |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case ASCII_NBRSP: |
case ASCII_NBRSP: |
buf[pos++] = ' '; |
dst[dsz++] = ' '; |
val++; |
scp++; |
|
continue; |
|
case ASCII_HYPH: |
|
dst[dsz++] = '-'; |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case ASCII_BREAK: |
case ASCII_BREAK: |
|
scp++; |
continue; |
continue; |
default: |
default: |
break; |
abort(); |
} |
} |
if ('\\' != *val) |
|
break; |
|
|
|
/* Read past the slash. */ |
|
|
|
val++; |
|
|
|
/* |
/* |
* Parse the escape sequence and see if it's a |
* Found an escape sequence. |
* predefined character or special character. |
* Read past the slash, then parse it. |
|
* Ignore everything except characters. |
*/ |
*/ |
|
|
esc = mandoc_escape((const char **)&val, |
scp++; |
&seq, &len); |
if (mandoc_escape(&scp, &seq, &seqlen) != ESCAPE_SPECIAL) |
if (ESCAPE_ERROR == esc) |
|
break; |
|
if (ESCAPE_SPECIAL != esc) |
|
continue; |
continue; |
|
|
/* |
/* |
Line 1929 render_key(struct mchars *mc, struct str *key) |
|
Line 1966 render_key(struct mchars *mc, struct str *key) |
|
*/ |
*/ |
|
|
if (write_utf8) { |
if (write_utf8) { |
if ((u = mchars_spec2cp(mc, seq, len)) <= 0) |
unicode = mchars_spec2cp(mchars, seq, seqlen); |
|
if (unicode <= 0) |
continue; |
continue; |
cpp = utfbuf; |
addsz = utf8(unicode, utfbuf); |
if (0 == (sz = utf8(u, utfbuf))) |
if (addsz == 0) |
continue; |
continue; |
sz = strlen(cpp); |
addcp = utfbuf; |
} else { |
} else { |
cpp = mchars_spec2str(mc, seq, len, &sz); |
addcp = mchars_spec2str(mchars, seq, seqlen, &addsz); |
if (NULL == cpp) |
if (addcp == NULL) |
continue; |
continue; |
if (ASCII_NBRSP == *cpp) { |
if (*addcp == ASCII_NBRSP) { |
cpp = " "; |
addcp = " "; |
sz = 1; |
addsz = 1; |
} |
} |
} |
} |
|
|
/* Copy the rendered glyph into the stream. */ |
/* Copy the rendered glyph into the stream. */ |
|
|
bsz += sz; |
ssz += addsz; |
buf = mandoc_realloc(buf, bsz); |
dst = mandoc_realloc(dst, ssz + 1); |
memcpy(&buf[pos], cpp, sz); |
memcpy(dst + dsz, addcp, addsz); |
pos += sz; |
dsz += addsz; |
} |
} |
|
if (dst != NULL) { |
|
*public = dst; |
|
*psz = dsz; |
|
} |
|
|
buf[pos] = '\0'; |
/* Trim trailing whitespace and NUL-terminate. */ |
key->rendered = buf; |
|
|
while (*psz > 0 && (*public)[*psz - 1] == ' ') |
|
--*psz; |
|
if (dst != NULL) { |
|
(*public)[*psz] = '\0'; |
|
return(1); |
|
} else |
|
return(0); |
} |
} |
|
|
static void |
static void |
Line 1974 dbadd_mlink(const struct mlink *mlink) |
|
Line 2023 dbadd_mlink(const struct mlink *mlink) |
|
static void |
static void |
dbadd_mlink_name(const struct mlink *mlink) |
dbadd_mlink_name(const struct mlink *mlink) |
{ |
{ |
|
uint64_t bits; |
size_t i; |
size_t i; |
|
|
dbadd_mlink(mlink); |
dbadd_mlink(mlink); |
|
|
i = 1; |
i = 1; |
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, NAME_FILE & NAME_MASK); |
SQL_BIND_INT64(stmts[STMT_SELECT_NAME], i, mlink->mpage->pageid); |
|
bits = NAME_FILE & NAME_MASK; |
|
if (sqlite3_step(stmts[STMT_SELECT_NAME]) == SQLITE_ROW) { |
|
bits |= sqlite3_column_int64(stmts[STMT_SELECT_NAME], 0); |
|
sqlite3_reset(stmts[STMT_SELECT_NAME]); |
|
} |
|
|
|
i = 1; |
|
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, bits); |
SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, mlink->name); |
SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, mlink->name); |
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mlink->mpage->pageid); |
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mlink->mpage->pageid); |
SQL_STEP(stmts[STMT_INSERT_NAME]); |
SQL_STEP(stmts[STMT_INSERT_NAME]); |
Line 1993 dbadd_mlink_name(const struct mlink *mlink) |
|
Line 2051 dbadd_mlink_name(const struct mlink *mlink) |
|
* Also, handle escape sequences at the last possible moment. |
* Also, handle escape sequences at the last possible moment. |
*/ |
*/ |
static void |
static void |
dbadd(struct mpage *mpage, struct mchars *mc) |
dbadd(struct mpage *mpage) |
{ |
{ |
struct mlink *mlink; |
struct mlink *mlink; |
struct str *key; |
struct str *key; |
|
char *cp; |
size_t i; |
size_t i; |
unsigned int slot; |
unsigned int slot; |
|
int mustfree; |
|
|
mlink = mpage->mlinks; |
mlink = mpage->mlinks; |
|
|
if (nodb) { |
if (nodb) { |
for (key = ohash_first(&names, &slot); NULL != key; |
for (key = ohash_first(&names, &slot); NULL != key; |
key = ohash_next(&names, &slot)) { |
key = ohash_next(&names, &slot)) |
if (key->rendered != key->key) |
|
free(key->rendered); |
|
free(key); |
free(key); |
} |
|
for (key = ohash_first(&strings, &slot); NULL != key; |
for (key = ohash_first(&strings, &slot); NULL != key; |
key = ohash_next(&strings, &slot)) { |
key = ohash_next(&strings, &slot)) |
if (key->rendered != key->key) |
|
free(key->rendered); |
|
free(key); |
free(key); |
} |
|
if (0 == debug) |
if (0 == debug) |
return; |
return; |
while (NULL != mlink) { |
while (NULL != mlink) { |
Line 2043 dbadd(struct mpage *mpage, struct mchars *mc) |
|
Line 2097 dbadd(struct mpage *mpage, struct mchars *mc) |
|
if (debug) |
if (debug) |
say(mlink->file, "Adding to database"); |
say(mlink->file, "Adding to database"); |
|
|
i = strlen(mpage->desc) + 1; |
cp = mpage->desc; |
key = mandoc_calloc(1, sizeof(struct str) + i); |
i = strlen(cp); |
memcpy(key->key, mpage->desc, i); |
mustfree = render_string(&cp, &i); |
render_key(mc, key); |
|
|
|
i = 1; |
i = 1; |
SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, key->rendered); |
SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, cp); |
SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, mpage->form); |
SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, mpage->form); |
SQL_STEP(stmts[STMT_INSERT_PAGE]); |
SQL_STEP(stmts[STMT_INSERT_PAGE]); |
mpage->pageid = sqlite3_last_insert_rowid(db); |
mpage->pageid = sqlite3_last_insert_rowid(db); |
sqlite3_reset(stmts[STMT_INSERT_PAGE]); |
sqlite3_reset(stmts[STMT_INSERT_PAGE]); |
|
if (mustfree) |
|
free(cp); |
|
|
if (key->rendered != key->key) |
|
free(key->rendered); |
|
free(key); |
|
|
|
while (NULL != mlink) { |
while (NULL != mlink) { |
dbadd_mlink(mlink); |
dbadd_mlink(mlink); |
mlink = mlink->next; |
mlink = mlink->next; |
Line 2068 dbadd(struct mpage *mpage, struct mchars *mc) |
|
Line 2118 dbadd(struct mpage *mpage, struct mchars *mc) |
|
for (key = ohash_first(&names, &slot); NULL != key; |
for (key = ohash_first(&names, &slot); NULL != key; |
key = ohash_next(&names, &slot)) { |
key = ohash_next(&names, &slot)) { |
assert(key->mpage == mpage); |
assert(key->mpage == mpage); |
if (NULL == key->rendered) |
|
render_key(mc, key); |
|
i = 1; |
i = 1; |
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, key->mask); |
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, key->mask); |
SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, key->rendered); |
SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, key->key); |
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mpage->pageid); |
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mpage->pageid); |
SQL_STEP(stmts[STMT_INSERT_NAME]); |
SQL_STEP(stmts[STMT_INSERT_NAME]); |
sqlite3_reset(stmts[STMT_INSERT_NAME]); |
sqlite3_reset(stmts[STMT_INSERT_NAME]); |
if (key->rendered != key->key) |
|
free(key->rendered); |
|
free(key); |
free(key); |
} |
} |
for (key = ohash_first(&strings, &slot); NULL != key; |
for (key = ohash_first(&strings, &slot); NULL != key; |
key = ohash_next(&strings, &slot)) { |
key = ohash_next(&strings, &slot)) { |
assert(key->mpage == mpage); |
assert(key->mpage == mpage); |
if (NULL == key->rendered) |
|
render_key(mc, key); |
|
i = 1; |
i = 1; |
SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask); |
SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask); |
SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->rendered); |
SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->key); |
SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->pageid); |
SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->pageid); |
SQL_STEP(stmts[STMT_INSERT_KEY]); |
SQL_STEP(stmts[STMT_INSERT_KEY]); |
sqlite3_reset(stmts[STMT_INSERT_KEY]); |
sqlite3_reset(stmts[STMT_INSERT_KEY]); |
if (key->rendered != key->key) |
|
free(key->rendered); |
|
free(key); |
free(key); |
} |
} |
} |
} |
|
|
" \"bits\" INTEGER NOT NULL,\n" |
" \"bits\" INTEGER NOT NULL,\n" |
" \"name\" TEXT NOT NULL,\n" |
" \"name\" TEXT NOT NULL,\n" |
" \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) " |
" \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) " |
"ON DELETE CASCADE\n" |
"ON DELETE CASCADE,\n" |
|
" UNIQUE (\"name\", \"pageid\") ON CONFLICT REPLACE\n" |
");\n" |
");\n" |
"\n" |
"\n" |
"CREATE TABLE \"keys\" (\n" |
"CREATE TABLE \"keys\" (\n" |
Line 2330 prepare_statements: |
|
Line 2373 prepare_statements: |
|
sql = "INSERT INTO mlinks " |
sql = "INSERT INTO mlinks " |
"(sec,arch,name,pageid) VALUES (?,?,?,?)"; |
"(sec,arch,name,pageid) VALUES (?,?,?,?)"; |
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL); |
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL); |
|
sql = "SELECT bits FROM names where pageid = ?"; |
|
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_SELECT_NAME], NULL); |
sql = "INSERT INTO names " |
sql = "INSERT INTO names " |
"(bits,name,pageid) VALUES (?,?,?)"; |
"(bits,name,pageid) VALUES (?,?,?)"; |
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_NAME], NULL); |
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_NAME], NULL); |