=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.198 retrieving revision 1.204 diff -u -p -r1.198 -r1.204 --- mandoc/main.c 2014/11/11 02:43:41 1.198 +++ mandoc/main.c 2014/12/09 09:14:33 1.204 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.198 2014/11/11 02:43:41 schwarze Exp $ */ +/* $Id: main.c,v 1.204 2014/12/09 09:14:33 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010, 2011, 2012, 2014 Ingo Schwarze @@ -83,6 +83,9 @@ struct curparse { }; static int koptions(int *, char *); +#if HAVE_SQLITE3 +int mandocdb(int, char**); +#endif static int moptions(int *, char *); static void mmsg(enum mandocerr, enum mandoclevel, const char *, int, int, const char *); @@ -96,6 +99,8 @@ static void version(void) __attribute__((noreturn)) static int woptions(struct curparse *, char *); static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; +static char help_arg[] = "help"; +static char *help_argv[] = {help_arg, NULL}; static const char *progname; @@ -115,7 +120,6 @@ main(int argc, char *argv[]) #endif enum mandoclevel rc; enum outmode outmode; - pid_t child_pid; int fd; int show_usage; int use_pager; @@ -129,6 +133,11 @@ main(int argc, char *argv[]) else ++progname; +#if HAVE_SQLITE3 + if (strcmp(progname, BINM_MAKEWHATIS) == 0) + return(mandocdb(argc, argv)); +#endif + /* Search options. */ memset(&paths, 0, sizeof(struct manpaths)); @@ -137,19 +146,21 @@ main(int argc, char *argv[]) memset(&search, 0, sizeof(struct mansearch)); search.outkey = "Nd"; - if (strcmp(progname, "man") == 0) + if (strcmp(progname, BINM_MAN) == 0) search.argmode = ARG_NAME; - else if (strncmp(progname, "apropos", 7) == 0) + else if (strcmp(progname, BINM_APROPOS) == 0) search.argmode = ARG_EXPR; - else if (strncmp(progname, "whatis", 6) == 0) + else if (strcmp(progname, BINM_WHATIS) == 0) search.argmode = ARG_WORD; + else if (strncmp(progname, "help", 4) == 0) + search.argmode = ARG_NAME; else search.argmode = ARG_FILE; /* Parser and formatter options. */ memset(&curp, 0, sizeof(struct curparse)); - curp.outtype = OUTT_ASCII; + curp.outtype = OUTT_LOCALE; curp.wlevel = MANDOCLEVEL_FATAL; options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1; defos = NULL; @@ -274,15 +285,24 @@ main(int argc, char *argv[]) resp = NULL; #endif - /* Quirk for a man(1) section argument without -s. */ + /* + * Quirks for help(1) + * and for a man(1) section argument without -s. + */ - if (search.argmode == ARG_NAME && - argv[0] != NULL && - isdigit((unsigned char)argv[0][0]) && - (argv[0][1] == '\0' || !strcmp(argv[0], "3p"))) { - search.sec = argv[0]; - argv++; - argc--; + if (search.argmode == ARG_NAME) { + if (*progname == 'h') { + if (argc == 0) { + argv = help_argv; + argc = 1; + } + } else if (argv[0] != NULL && + isdigit((unsigned char)argv[0][0]) && + (argv[0][1] == '\0' || !strcmp(argv[0], "3p"))) { + search.sec = argv[0]; + argv++; + argc--; + } } rc = MANDOCLEVEL_OK; @@ -294,6 +314,10 @@ main(int argc, char *argv[]) if (argc == 0) usage(search.argmode); + if (search.argmode == ARG_NAME && + outmode == OUTMODE_ONE) + search.firstmatch = 1; + /* Access the mandoc database. */ manpath_parse(&paths, conf_file, defpaths, auxpaths); @@ -384,8 +408,7 @@ main(int argc, char *argv[]) while (argc) { #if HAVE_SQLITE3 if (resp != NULL) { - rc = mparse_open(curp.mp, &fd, resp->file, - &child_pid); + rc = mparse_open(curp.mp, &fd, resp->file); if (fd == -1) /* nothing */; else if (resp->form & FORM_SRC) { @@ -399,14 +422,12 @@ main(int argc, char *argv[]) } else #endif { - rc = mparse_open(curp.mp, &fd, *argv++, - &child_pid); + rc = mparse_open(curp.mp, &fd, *argv++); if (fd != -1) parse(&curp, fd, argv[-1], &rc); } - if (child_pid && - mparse_wait(curp.mp, child_pid) != MANDOCLEVEL_OK) + if (mparse_wait(curp.mp) != MANDOCLEVEL_OK) rc = MANDOCLEVEL_SYSERR; if (MANDOCLEVEL_OK != rc && curp.wstop)