From da7b34abc5bd5ab75be7d8fe2946140d523aebba Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Fri, 27 Mar 2015 21:33:20 +0000 Subject: Actually use the new man.conf(5) "output" directive. Additional functionality, yet minus 45 lines of code. --- cgi.c | 13 ++++++------ html.c | 37 ++++++++------------------------ main.c | 17 ++++++++------- main.h | 25 ++++++++++++++-------- man.conf.5 | 20 ++++++++++++++++++ term_ascii.c | 69 ++++++++++++++++++------------------------------------------ term_ps.c | 31 ++++++++------------------- 7 files changed, 91 insertions(+), 121 deletions(-) diff --git a/cgi.c b/cgi.c index e803af15..e29e85e1 100644 --- a/cgi.c +++ b/cgi.c @@ -1,7 +1,7 @@ /* $Id$ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons - * Copyright (c) 2014 Ingo Schwarze + * Copyright (c) 2014, 2015 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -816,12 +816,12 @@ catman(const struct req *req, const char *file) static void format(const struct req *req, const char *file) { + struct manoutput conf; struct mparse *mp; struct mchars *mchars; struct mdoc *mdoc; struct man *man; void *vp; - char *opts; int fd; int usepath; @@ -836,9 +836,10 @@ format(const struct req *req, const char *file) mparse_readfd(mp, fd, file); close(fd); + memset(&conf, 0, sizeof(conf)); + conf.fragment = 1; usepath = strcmp(req->q.manpath, req->p[0]); - mandoc_asprintf(&opts, - "fragment,man=%s?query=%%N&sec=%%S%s%s%s%s", + mandoc_asprintf(&conf.man, "%s?query=%%N&sec=%%S%s%s%s%s", scriptname, req->q.arch ? "&arch=" : "", req->q.arch ? req->q.arch : "", @@ -855,7 +856,7 @@ format(const struct req *req, const char *file) return; } - vp = html_alloc(mchars, opts); + vp = html_alloc(mchars, &conf); if (NULL != mdoc) html_mdoc(vp, mdoc); @@ -865,7 +866,7 @@ format(const struct req *req, const char *file) html_free(vp); mparse_free(mp); mchars_free(mchars); - free(opts); + free(conf.man); } static void diff --git a/html.c b/html.c index c72c3d48..ad587aaf 100644 --- a/html.c +++ b/html.c @@ -7,9 +7,9 @@ * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF @@ -32,6 +32,7 @@ #include "mandoc_aux.h" #include "out.h" #include "html.h" +#include "manconf.h" #include "main.h" struct htmldata { @@ -129,40 +130,20 @@ static void print_attr(struct html *, const char *, const char *); void * -html_alloc(const struct mchars *mchars, char *outopts) +html_alloc(const struct mchars *mchars, const struct manoutput *outopts) { struct html *h; - const char *toks[5]; - char *v; - - toks[0] = "style"; - toks[1] = "man"; - toks[2] = "includes"; - toks[3] = "fragment"; - toks[4] = NULL; h = mandoc_calloc(1, sizeof(struct html)); h->tags.head = NULL; h->symtab = mchars; - while (outopts && *outopts) - switch (getsubopt(&outopts, UNCONST(toks), &v)) { - case 0: - h->style = v; - break; - case 1: - h->base_man = v; - break; - case 2: - h->base_includes = v; - break; - case 3: - h->oflags |= HTML_FRAGMENT; - break; - default: - break; - } + h->style = outopts->style; + h->base_man = outopts->man; + h->base_includes = outopts->includes; + if (outopts->fragment) + h->oflags |= HTML_FRAGMENT; return(h); } diff --git a/main.c b/main.c index 83358dda..a35c88fc 100644 --- a/main.c +++ b/main.c @@ -83,7 +83,7 @@ struct curparse { out_man outman; /* man output ptr */ out_free outfree; /* free output ptr */ void *outdata; /* data for output */ - char outopts[BUFSIZ]; /* buf of output opts */ + struct manoutput *outopts; /* output options */ }; static int fs_lookup(const struct manpaths *, @@ -127,7 +127,7 @@ main(int argc, char *argv[]) struct manpage *res, *resp; char *conf_file, *defpaths; size_t isec, i, sz; - int prio, best_prio, synopsis_only; + int prio, best_prio; char sec; enum mandoclevel rctmp; enum outmode outmode; @@ -174,12 +174,12 @@ main(int argc, char *argv[]) memset(&curp, 0, sizeof(struct curparse)); curp.outtype = OUTT_LOCALE; curp.wlevel = MANDOCLEVEL_BADARG; + curp.outopts = &conf.output; options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1; defos = NULL; pager_pid = 1; show_usage = 0; - synopsis_only = 0; outmode = OUTMODE_DEF; while (-1 != (c = getopt(argc, argv, @@ -198,8 +198,7 @@ main(int argc, char *argv[]) search.argmode = ARG_WORD; break; case 'h': - (void)strlcat(curp.outopts, "synopsis,", BUFSIZ); - synopsis_only = 1; + conf.output.synopsisonly = 1; pager_pid = 0; outmode = OUTMODE_ALL; break; @@ -240,8 +239,9 @@ main(int argc, char *argv[]) break; case 'O': search.outkey = optarg; - (void)strlcat(curp.outopts, optarg, BUFSIZ); - (void)strlcat(curp.outopts, ",", BUFSIZ); + while (optarg != NULL) + manconf_output(&conf.output, + strsep(&optarg, ",")); break; case 'S': search.arch = optarg; @@ -444,7 +444,8 @@ main(int argc, char *argv[]) chdir(conf.manpath.paths[resp->ipath]); parse(&curp, fd, resp->file); } else - passthrough(resp->file, fd, synopsis_only); + passthrough(resp->file, fd, + conf.output.synopsisonly); rctmp = mparse_wait(curp.mp); if (rc < rctmp) diff --git a/main.h b/main.h index 7a6495c3..aae2ee4c 100644 --- a/main.h +++ b/main.h @@ -1,15 +1,15 @@ /* $Id$ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons - * Copyright (c) 2014 Ingo Schwarze + * Copyright (c) 2014, 2015 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF @@ -23,6 +23,7 @@ __BEGIN_DECLS struct mchars; struct mdoc; struct man; +struct manoutput; /* * Definitions for main.c-visible output device functions, e.g., -Thtml @@ -31,7 +32,8 @@ struct man; * terminal output routines with different character settings. */ -void *html_alloc(const struct mchars *, char *); +void *html_alloc(const struct mchars *, + const struct manoutput *); void html_mdoc(void *, const struct mdoc *); void html_man(void *, const struct man *); void html_free(void *); @@ -42,14 +44,19 @@ void tree_man(void *, const struct man *); void man_mdoc(void *, const struct mdoc *); void man_man(void *, const struct man *); -void *locale_alloc(const struct mchars *, char *); -void *utf8_alloc(const struct mchars *, char *); -void *ascii_alloc(const struct mchars *, char *); +void *locale_alloc(const struct mchars *, + const struct manoutput *); +void *utf8_alloc(const struct mchars *, + const struct manoutput *); +void *ascii_alloc(const struct mchars *, + const struct manoutput *); void ascii_free(void *); void ascii_sepline(void *); -void *pdf_alloc(const struct mchars *, char *); -void *ps_alloc(const struct mchars *, char *); +void *pdf_alloc(const struct mchars *, + const struct manoutput *); +void *ps_alloc(const struct mchars *, + const struct manoutput *); void pspdf_free(void *); void terminal_mdoc(void *, const struct mdoc *); diff --git a/man.conf.5 b/man.conf.5 index a726ec11..8f52ed41 100644 --- a/man.conf.5 +++ b/man.conf.5 @@ -76,6 +76,26 @@ is recommended and necessary for .Xr apropos 1 to work, but not strictly required for .Xr man 1 . +.It Ic output Ar option Op Ar value +Configure the default value of an output option. +These directives are overridden by the +.Fl O +command line options of the same names. +For details, see the +.Xr mandoc 1 +manual. +.Pp +.Bl -column fragment integer "ascii, utf8" -compact +.It Ar option Ta Ar value Ta used by Fl T Ta purpose +.It Ta Ta Ta +.It Ic fragment Ta none Ta Cm html Ta print only body +.It Ic includes Ta string Ta Cm html Ta path to header files +.It Ic indent Ta integer Ta Cm ascii , utf8 Ta left margin +.It Ic man Ta string Ta Cm html Ta path for Xr links +.It Ic paper Ta string Ta Cm ps , pdf Ta paper size +.It Ic style Ta string Ta Cm html Ta CSS file +.It Ic width Ta integer Ta Cm ascii , utf8 Ta right margin +.El .It Ic _whatdb Ar path Ns Cm /whatis.db This directive provides the same functionality as .Ic manpath , diff --git a/term_ascii.c b/term_ascii.c index 77a3d591..cbc9c9ea 100644 --- a/term_ascii.c +++ b/term_ascii.c @@ -1,15 +1,15 @@ /* $Id$ */ /* * Copyright (c) 2010, 2011 Kristaps Dzonsons - * Copyright (c) 2014 Ingo Schwarze + * Copyright (c) 2014, 2015 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF @@ -35,10 +35,11 @@ #include "mandoc_aux.h" #include "out.h" #include "term.h" +#include "manconf.h" #include "main.h" -static struct termp *ascii_init(enum termenc, - const struct mchars *, char *); +static struct termp *ascii_init(enum termenc, const struct mchars *, + const struct manoutput *); static double ascii_hspan(const struct termp *, const struct roffsu *); static size_t ascii_width(const struct termp *, int); @@ -58,13 +59,11 @@ static size_t locale_width(const struct termp *, int); static struct termp * -ascii_init(enum termenc enc, const struct mchars *mchars, char *outopts) +ascii_init(enum termenc enc, const struct mchars *mchars, + const struct manoutput *outopts) { - const char *toks[5]; char *v; struct termp *p; - const char *errstr; - int num; p = mandoc_calloc(1, sizeof(struct termp)); @@ -102,62 +101,36 @@ ascii_init(enum termenc enc, const struct mchars *mchars, char *outopts) } #endif - toks[0] = "indent"; - toks[1] = "width"; - toks[2] = "mdoc"; - toks[3] = "synopsis"; - toks[4] = NULL; - - while (outopts && *outopts) - switch (getsubopt(&outopts, UNCONST(toks), &v)) { - case 0: - num = strtonum(v, 0, 1000, &errstr); - if (!errstr) - p->defindent = num; - break; - case 1: - num = strtonum(v, 0, 1000, &errstr); - if (!errstr) - p->defrmargin = num; - break; - case 2: - /* - * Temporary, undocumented mode - * to imitate mdoc(7) output style. - */ - p->mdocstyle = 1; - p->defindent = 5; - break; - case 3: - p->synopsisonly = 1; - break; - default: - break; - } - - /* Enforce a lower boundary. */ - if (p->defrmargin < 58) - p->defrmargin = 58; + if (outopts->mdoc) { + p->mdocstyle = 1; + p->defindent = 5; + } + if (outopts->indent) + p->defindent = outopts->indent; + if (outopts->width) + p->defrmargin = outopts->width; + if (outopts->synopsisonly) + p->synopsisonly = 1; return(p); } void * -ascii_alloc(const struct mchars *mchars, char *outopts) +ascii_alloc(const struct mchars *mchars, const struct manoutput *outopts) { return(ascii_init(TERMENC_ASCII, mchars, outopts)); } void * -utf8_alloc(const struct mchars *mchars, char *outopts) +utf8_alloc(const struct mchars *mchars, const struct manoutput *outopts) { return(ascii_init(TERMENC_UTF8, mchars, outopts)); } void * -locale_alloc(const struct mchars *mchars, char *outopts) +locale_alloc(const struct mchars *mchars, const struct manoutput *outopts) { return(ascii_init(TERMENC_LOCALE, mchars, outopts)); diff --git a/term_ps.c b/term_ps.c index 0d5dda51..c819b55f 100644 --- a/term_ps.c +++ b/term_ps.c @@ -7,9 +7,9 @@ * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF @@ -30,6 +30,7 @@ #include "mandoc_aux.h" #include "out.h" #include "term.h" +#include "manconf.h" #include "main.h" /* These work the buffer used by the header and footer. */ @@ -106,7 +107,8 @@ static void ps_printf(struct termp *, const char *, ...); static void ps_putchar(struct termp *, char); static void ps_setfont(struct termp *, enum termfont); static void ps_setwidth(struct termp *, int, size_t); -static struct termp *pspdf_alloc(const struct mchars *, char *); +static struct termp *pspdf_alloc(const struct mchars *, + const struct manoutput *); static void pdf_obj(struct termp *, size_t); /* @@ -507,7 +509,7 @@ static const struct font fonts[TERMFONT__MAX] = { }; void * -pdf_alloc(const struct mchars *mchars, char *outopts) +pdf_alloc(const struct mchars *mchars, const struct manoutput *outopts) { struct termp *p; @@ -518,7 +520,7 @@ pdf_alloc(const struct mchars *mchars, char *outopts) } void * -ps_alloc(const struct mchars *mchars, char *outopts) +ps_alloc(const struct mchars *mchars, const struct manoutput *outopts) { struct termp *p; @@ -529,14 +531,12 @@ ps_alloc(const struct mchars *mchars, char *outopts) } static struct termp * -pspdf_alloc(const struct mchars *mchars, char *outopts) +pspdf_alloc(const struct mchars *mchars, const struct manoutput *outopts) { struct termp *p; unsigned int pagex, pagey; size_t marginx, marginy, lineheight; - const char *toks[2]; const char *pp; - char *v; p = mandoc_calloc(1, sizeof(struct termp)); p->symtab = mchars; @@ -555,20 +555,6 @@ pspdf_alloc(const struct mchars *mchars, char *outopts) p->setwidth = ps_setwidth; p->width = ps_width; - toks[0] = "paper"; - toks[1] = NULL; - - pp = NULL; - - while (outopts && *outopts) - switch (getsubopt(&outopts, UNCONST(toks), &v)) { - case 0: - pp = v; - break; - default: - break; - } - /* Default to US letter (millimetres). */ pagex = 216; @@ -581,6 +567,7 @@ pspdf_alloc(const struct mchars *mchars, char *outopts) * only happens once, I'm not terribly concerned. */ + pp = outopts->paper; if (pp && strcasecmp(pp, "letter")) { if (0 == strcasecmp(pp, "a3")) { pagex = 297; -- cgit