-/* $Id: catman.c,v 1.5 2011/12/12 02:00:49 schwarze Exp $ */
+/* $Id: catman.c,v 1.8 2011/12/18 18:51:01 kristaps Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
*
exit(EXIT_FAILURE); \
} while (/* CONSTCOND */0)
-static int indexhtml(char *, char *);
+static int indexhtml(char *, size_t, char *, size_t);
static int manup(const struct manpaths *, char *);
static int mkpath(char *, mode_t, mode_t);
-static int treecpy(char *, char *, char *);
-static int update(char *, char *, char *);
+static int treecpy(char *, char *);
+static int update(char *, char *);
static void usage(void);
static const char *progname;
main(int argc, char *argv[])
{
int ch;
- char *aux, *base;
+ char *aux, *base, *conf_file;
struct manpaths dirs;
char buf[MAXPATHLEN];
extern char *optarg;
aux = base = NULL;
xstrlcpy(buf, "/var/www/cache/man.cgi", MAXPATHLEN);
- while (-1 != (ch = getopt(argc, argv, "fm:M:o:v")))
+ while (-1 != (ch = getopt(argc, argv, "C:fm:M:o:v")))
switch (ch) {
+ case ('C'):
+ conf_file = optarg;
+ break;
case ('f'):
force = 1;
break;
}
memset(&dirs, 0, sizeof(struct manpaths));
- manpath_parse(&dirs, NULL, base, aux);
+ manpath_parse(&dirs, conf_file, base, aux);
ch = manup(&dirs, buf);
manpath_free(&dirs);
return(ch ? EXIT_SUCCESS : EXIT_FAILURE);
fprintf(stderr, "usage: %s "
"[-fv] "
+ "[-C file] "
"[-o path] "
"[-m manpath] "
"[-M manpath]\n",
* Returns -1 on fatal error, 1 on success.
*/
static int
-indexhtml(char *base, char *dst)
+indexhtml(char *src, size_t ssz, char *dst, size_t dsz)
{
DB *idx;
DBT key, val;
- size_t sz;
int c, rc;
unsigned int fl;
- const char *f, *cp;
+ const char *f;
char *d;
char fname[MAXPATHLEN];
pid_t pid;
- sz = strlen(base);
pid = -1;
xstrlcpy(fname, dst, MAXPATHLEN);
fl = R_FIRST;
while (0 == (c = (*idx->seq)(idx, &key, &val, fl))) {
fl = R_NEXT;
- cp = (const char *)val.data;
+ /*
+ * If the record is zero-length, then it's unassigned.
+ * Skip past these.
+ */
if (0 == val.size)
continue;
- if (NULL == (f = memchr(cp, '\0', val.size)))
- break;
- if (++f - cp >= (int)val.size)
- break;
- if (NULL == memchr(f, '\0', val.size - (f - cp)))
+
+ f = (const char *)val.data + 1;
+ if (NULL == memchr(f, '\0', val.size - 1))
break;
- base[(int)sz] = '\0';
+ src[(int)ssz] = dst[(int)dsz] = '\0';
+
+ xstrlcat(dst, "/", MAXPATHLEN);
+ xstrlcat(dst, f, MAXPATHLEN);
- xstrlcat(base, "/", MAXPATHLEN);
- xstrlcat(base, f, MAXPATHLEN);
+ xstrlcat(src, "/", MAXPATHLEN);
+ xstrlcat(src, f, MAXPATHLEN);
- if (-1 == (rc = isnewer(base, f))) {
+ if (-1 == (rc = isnewer(dst, src))) {
fprintf(stderr, "%s: File missing\n", f);
break;
} else if (0 == rc)
continue;
- d = strrchr(base, '/');
+ d = strrchr(dst, '/');
assert(NULL != d);
*d = '\0';
- if (-1 == mkpath(base, 0755, 0755)) {
- perror(base);
+ if (-1 == mkpath(dst, 0755, 0755)) {
+ perror(dst);
break;
}
*d = '/';
- if ( ! filecpy(base, f))
+ if ( ! filecpy(dst, src))
break;
if (verbose)
- printf("%s\n", base);
+ printf("%s\n", dst);
}
(*idx->close)(idx);
* Return -1 on fatal error and 1 if the update went well.
*/
static int
-update(char *base, char *dst, char *src)
+update(char *dst, char *src)
{
size_t dsz, ssz;
if (verbose)
printf("%s\n", dst);
- dst[(int)dsz] = '\0';
+ dst[(int)dsz] = src[(int)ssz] = '\0';
- return(indexhtml(base, dst));
+ return(indexhtml(src, ssz, dst, dsz));
}
/*
* shouldn't be listed), and 1 if the update went well.
*/
static int
-treecpy(char *base, char *dst, char *src)
+treecpy(char *dst, char *src)
{
size_t dsz, ssz;
int rc;
dst[(int)dsz] = src[(int)ssz] = '\0';
if (1 == rc)
- return(update(base, dst, src));
+ return(update(dst, src));
xstrlcat(src, "/mandoc.db", MAXPATHLEN);
xstrlcat(dst, "/mandoc.db", MAXPATHLEN);
dst[(int)dsz] = src[(int)ssz] = '\0';
- return(update(base, dst, src));
+ return(update(dst, src));
}
/*
for (i = 0; i < dirs->sz; i++) {
path = dirs->paths[i];
- dst[(int)sz] = base[(int)sz] = '\0';
+ dst[(int)sz] = '\0';
xstrlcat(dst, path, MAXPATHLEN);
if (-1 == mkpath(dst, 0755, 0755)) {
perror(dst);
}
xstrlcpy(src, path, MAXPATHLEN);
- if (-1 == (c = treecpy(base, dst, src)))
+ if (-1 == (c = treecpy(dst, src)))
break;
else if (0 == c)
continue;