]> git.cameronkatri.com Git - mandoc.git/blobdiff - main.c
implement tagging for .Er
[mandoc.git] / main.c
diff --git a/main.c b/main.c
index d5d61e8123f3f264bf576f7b0248c919864413bd..66bbfea88241a2c05cd184e748c9f4a91b07f887 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,4 +1,4 @@
-/*     $Id: main.c,v 1.229 2015/03/27 17:37:25 schwarze Exp $ */
+/*     $Id: main.c,v 1.243 2015/07/21 03:26:21 schwarze Exp $ */
 /*
  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
 #include <string.h>
 #include <unistd.h>
 
-#include "mandoc.h"
 #include "mandoc_aux.h"
-#include "main.h"
+#include "mandoc.h"
+#include "roff.h"
 #include "mdoc.h"
 #include "man.h"
+#include "tag.h"
+#include "main.h"
 #include "manconf.h"
 #include "mansearch.h"
 
@@ -57,8 +59,8 @@ enum  outmode {
        OUTMODE_ONE
 };
 
-typedef        void            (*out_mdoc)(void *, const struct mdoc *);
-typedef        void            (*out_man)(void *, const struct man *);
+typedef        void            (*out_mdoc)(void *, const struct roff_man *);
+typedef        void            (*out_man)(void *, const struct roff_man *);
 typedef        void            (*out_free)(void *);
 
 enum   outt {
@@ -83,7 +85,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 +129,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 +176,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 +200,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 +241,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;
@@ -351,9 +353,15 @@ main(int argc, char *argv[])
                sz = 0;
 #endif
 
-               if (sz == 0 && search.argmode == ARG_NAME)
-                       fs_search(&search, &conf.manpath,
-                           argc, argv, &res, &sz);
+               if (sz == 0) {
+                       if (search.argmode == ARG_NAME)
+                               fs_search(&search, &conf.manpath,
+                                   argc, argv, &res, &sz);
+                       else
+                               fprintf(stderr,
+                                   "%s: nothing appropriate\n",
+                                   progname);
+               }
 
                if (sz == 0) {
                        rc = MANDOCLEVEL_BADARG;
@@ -411,6 +419,9 @@ main(int argc, char *argv[])
        if (search.argmode == ARG_FILE && ! moptions(&options, auxpaths))
                return((int)MANDOCLEVEL_BADARG);
 
+       if (pager_pid == 1 && isatty(STDOUT_FILENO) == 0)
+               pager_pid = 0;
+
        curp.mchars = mchars_alloc();
        curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
            curp.mchars, defos);
@@ -422,7 +433,7 @@ main(int argc, char *argv[])
                mparse_keep(curp.mp);
 
        if (argc < 1) {
-               if (pager_pid == 1 && isatty(STDOUT_FILENO))
+               if (pager_pid == 1)
                        pager_pid = spawn_pager();
                parse(&curp, STDIN_FILENO, "<stdin>");
        }
@@ -434,7 +445,7 @@ main(int argc, char *argv[])
                        rc = rctmp;
 
                if (fd != -1) {
-                       if (pager_pid == 1 && isatty(STDOUT_FILENO))
+                       if (pager_pid == 1)
                                pager_pid = spawn_pager();
 
                        if (resp == NULL)
@@ -444,11 +455,8 @@ main(int argc, char *argv[])
                                chdir(conf.manpath.paths[resp->ipath]);
                                parse(&curp, fd, resp->file);
                        } else
-                               passthrough(resp->file, fd, synopsis_only);
-
-                       rctmp = mparse_wait(curp.mp);
-                       if (rc < rctmp)
-                               rc = rctmp;
+                               passthrough(resp->file, fd,
+                                   conf.output.synopsisonly);
 
                        if (argc > 1 && curp.outtype <= OUTT_UTF8)
                                ascii_sepline(curp.outdata);
@@ -489,7 +497,9 @@ out:
 
        if (pager_pid != 0 && pager_pid != 1) {
                fclose(stdout);
+               tag_write();
                waitpid(pager_pid, NULL, 0);
+               tag_unlink();
        }
 
        return((int)rc);
@@ -559,7 +569,7 @@ fs_lookup(const struct manpaths *paths, size_t ipath,
                free(file);
        }
 
-       mandoc_asprintf(&file, "%s/man%s/%s.*",
+       mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
            paths->paths[ipath], sec, name);
        globres = glob(file, 0, NULL, &globinfo);
        if (globres != 0 && globres != GLOB_NOMATCH)
@@ -574,11 +584,9 @@ fs_lookup(const struct manpaths *paths, size_t ipath,
 
 found:
 #if HAVE_SQLITE3
-       fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
-           "     consider running  # makewhatis %s\n",
-           progname, name, sec, paths->paths[ipath]);
+       fprintf(stderr, "%s: outdated mandoc.db lacks %s(%s) entry, run "
+           "makewhatis %s\n", progname, name, sec, paths->paths[ipath]);
 #endif
-
        *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
        page = *res + (*ressz - 1);
        page->file = file;
@@ -632,8 +640,7 @@ static void
 parse(struct curparse *curp, int fd, const char *file)
 {
        enum mandoclevel  rctmp;
-       struct mdoc      *mdoc;
-       struct man       *man;
+       struct roff_man  *man;
 
        /* Begin by parsing the file itself. */
 
@@ -720,14 +727,16 @@ parse(struct curparse *curp, int fd, const char *file)
                }
        }
 
-       mparse_result(curp->mp, &mdoc, &man, NULL);
+       mparse_result(curp->mp, &man, NULL);
 
        /* Execute the out device, if it exists. */
 
-       if (man && curp->outman)
+       if (man == NULL)
+               return;
+       if (curp->outmdoc != NULL && man->macroset == MACROSET_MDOC)
+               (*curp->outmdoc)(curp->outdata, man);
+       if (curp->outman != NULL && man->macroset == MACROSET_MAN)
                (*curp->outman)(curp->outdata, man);
-       if (mdoc && curp->outmdoc)
-               (*curp->outmdoc)(curp->outdata, mdoc);
 }
 
 static void
@@ -952,10 +961,53 @@ spawn_pager(void)
        char            *argv[MAX_PAGER_ARGS];
        const char      *pager;
        char            *cp;
+       size_t           cmdlen;
        int              fildes[2];
        int              argc;
        pid_t            pager_pid;
 
+       pager = getenv("MANPAGER");
+       if (pager == NULL || *pager == '\0')
+               pager = getenv("PAGER");
+       if (pager == NULL || *pager == '\0')
+               pager = "more -s";
+       cp = mandoc_strdup(pager);
+
+       /*
+        * Parse the pager command into words.
+        * Intentionally do not do anything fancy here.
+        */
+
+       argc = 0;
+       while (argc + 4 < MAX_PAGER_ARGS) {
+               argv[argc++] = cp;
+               cp = strchr(cp, ' ');
+               if (cp == NULL)
+                       break;
+               *cp++ = '\0';
+               while (*cp == ' ')
+                       cp++;
+               if (*cp == '\0')
+                       break;
+       }
+
+       /* Read all text right away and use the tag file. */
+
+       for (;;) {
+               if ((cmdlen = strlen(argv[0])) < 4)
+                       break;
+               cp = argv[0] + cmdlen - 4;
+               if (strcmp(cp, "less") && strcmp(cp, "more"))
+                       break;
+               if ((cp = tag_init()) == NULL)
+                       break;
+               argv[argc++] = mandoc_strdup("+G1G");
+               argv[argc++] = mandoc_strdup("-T");
+               argv[argc++] = cp;
+               break;
+       }
+       argv[argc] = NULL;
+
        if (pipe(fildes) == -1) {
                fprintf(stderr, "%s: pipe: %s\n",
                    progname, strerror(errno));
@@ -991,36 +1043,10 @@ spawn_pager(void)
        }
        close(fildes[0]);
 
-       pager = getenv("MANPAGER");
-       if (pager == NULL || *pager == '\0')
-               pager = getenv("PAGER");
-       if (pager == NULL || *pager == '\0')
-               pager = "/usr/bin/more -s";
-       cp = mandoc_strdup(pager);
-
-       /*
-        * Parse the pager command into words.
-        * Intentionally do not do anything fancy here.
-        */
-
-       argc = 0;
-       while (argc + 1 < MAX_PAGER_ARGS) {
-               argv[argc++] = cp;
-               cp = strchr(cp, ' ');
-               if (cp == NULL)
-                       break;
-               *cp++ = '\0';
-               while (*cp == ' ')
-                       cp++;
-               if (*cp == '\0')
-                       break;
-       }
-       argv[argc] = NULL;
-
        /* Hand over to the pager. */
 
        execvp(argv[0], argv);
-       fprintf(stderr, "%s: exec: %s\n",
-           progname, strerror(errno));
+       fprintf(stderr, "%s: exec %s: %s\n",
+           progname, argv[0], strerror(errno));
        exit((int)MANDOCLEVEL_SYSERR);
 }