]> git.cameronkatri.com Git - mandoc.git/blobdiff - main.c
first batch of -T markdown tests
[mandoc.git] / main.c
diff --git a/main.c b/main.c
index 0aa714bb8731765df07978bb0c5b0fce35959551..22b825302125624eb733574bb64a3c726d7d053e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,4 +1,4 @@
-/*     $Id: main.c,v 1.278 2017/01/09 01:37:03 schwarze Exp $ */
+/*     $Id: main.c,v 1.285 2017/03/03 14:23:23 schwarze Exp $ */
 /*
  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010-2012, 2014-2017 Ingo Schwarze <schwarze@openbsd.org>
@@ -67,6 +67,7 @@ enum  outt {
        OUTT_TREE,      /* -Ttree */
        OUTT_MAN,       /* -Tman */
        OUTT_HTML,      /* -Thtml */
+       OUTT_MARKDOWN,  /* -Tmarkdown */
        OUTT_LINT,      /* -Tlint */
        OUTT_PS,        /* -Tps */
        OUTT_PDF        /* -Tpdf */
@@ -100,7 +101,7 @@ static      void              parse(struct curparse *, int, const char *);
 static void              passthrough(const char *, int, int);
 static pid_t             spawn_pager(struct tag_files *);
 static int               toptions(struct curparse *, char *);
-static void              usage(enum argmode) __attribute__((noreturn));
+static void              usage(enum argmode) __attribute__((__noreturn__));
 static int               woptions(struct curparse *, char *);
 
 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
@@ -113,16 +114,14 @@ int
 main(int argc, char *argv[])
 {
        struct manconf   conf;
-       struct curparse  curp;
        struct mansearch search;
+       struct curparse  curp;
        struct tag_files *tag_files;
-       const char      *progname;
-       char            *auxpaths;
-       char            *defos;
-       unsigned char   *uc;
        struct manpage  *res, *resp;
-       char            *conf_file, *defpaths;
-       const char      *sec;
+       const char      *progname, *sec, *thisarg;
+       char            *conf_file, *defpaths, *auxpaths;
+       char            *defos, *oarg;
+       unsigned char   *uc;
        size_t           i, sz;
        int              prio, best_prio;
        enum outmode     outmode;
@@ -151,7 +150,7 @@ main(int argc, char *argv[])
                return mandocdb(argc, argv);
 
 #if HAVE_PLEDGE
-       if (pledge("stdio rpath tmppath tty proc exec flock", NULL) == -1)
+       if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
                err((int)MANDOCLEVEL_SYSERR, "pledge");
 #endif
 
@@ -168,6 +167,7 @@ main(int argc, char *argv[])
 
        memset(&search, 0, sizeof(struct mansearch));
        search.outkey = "Nd";
+       oarg = NULL;
 
        if (strcmp(progname, BINM_MAN) == 0)
                search.argmode = ARG_NAME;
@@ -246,10 +246,7 @@ main(int argc, char *argv[])
                        auxpaths = optarg;
                        break;
                case 'O':
-                       search.outkey = optarg;
-                       while (optarg != NULL)
-                               manconf_output(&conf.output,
-                                   strsep(&optarg, ","));
+                       oarg = optarg;
                        break;
                case 'S':
                        search.arch = optarg;
@@ -294,6 +291,21 @@ main(int argc, char *argv[])
                }
        }
 
+       if (oarg != NULL) {
+               if (outmode == OUTMODE_LST)
+                       search.outkey = oarg;
+               else {
+                       while (oarg != NULL) {
+                               thisarg = oarg;
+                               if (manconf_output(&conf.output,
+                                   strsep(&oarg, ","), 0) == 0)
+                                       continue;
+                               warnx("-O %s: Bad argument", thisarg);
+                               return (int)MANDOCLEVEL_BADARG;
+                       }
+               }
+       }
+
        if (outmode == OUTMODE_FLN ||
            outmode == OUTMODE_LST ||
            !isatty(STDOUT_FILENO))
@@ -301,7 +313,7 @@ main(int argc, char *argv[])
 
 #if HAVE_PLEDGE
        if (!use_pager)
-               if (pledge("stdio rpath flock", NULL) == -1)
+               if (pledge("stdio rpath", NULL) == -1)
                        err((int)MANDOCLEVEL_SYSERR, "pledge");
 #endif
 
@@ -531,10 +543,10 @@ out:
 
                        /* Stop here until moved to the foreground. */
 
-                       tc_pgid = tcgetpgrp(STDIN_FILENO);
+                       tc_pgid = tcgetpgrp(tag_files->ofd);
                        if (tc_pgid != man_pgid) {
                                if (tc_pgid == pager_pid) {
-                                       (void)tcsetpgrp(STDIN_FILENO,
+                                       (void)tcsetpgrp(tag_files->ofd,
                                            man_pgid);
                                        if (signum == SIGTTIN)
                                                continue;
@@ -547,7 +559,7 @@ out:
                        /* Once in the foreground, activate the pager. */
 
                        if (pager_pid) {
-                               (void)tcsetpgrp(STDIN_FILENO, pager_pid);
+                               (void)tcsetpgrp(tag_files->ofd, pager_pid);
                                kill(pager_pid, SIGCONT);
                        } else
                                pager_pid = spawn_pager(tag_files);
@@ -736,7 +748,8 @@ parse(struct curparse *curp, int fd, const char *file)
        if (man == NULL)
                return;
        if (man->macroset == MACROSET_MDOC) {
-               mdoc_validate(man);
+               if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
+                       mdoc_validate(man);
                switch (curp->outtype) {
                case OUTT_HTML:
                        html_mdoc(curp->outdata, man);
@@ -754,12 +767,16 @@ parse(struct curparse *curp, int fd, const char *file)
                case OUTT_PS:
                        terminal_mdoc(curp->outdata, man);
                        break;
+               case OUTT_MARKDOWN:
+                       markdown_mdoc(curp->outdata, man);
+                       break;
                default:
                        break;
                }
        }
        if (man->macroset == MACROSET_MAN) {
-               man_validate(man);
+               if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
+                       man_validate(man);
                switch (curp->outtype) {
                case OUTT_HTML:
                        html_man(curp->outdata, man);
@@ -937,6 +954,8 @@ toptions(struct curparse *curp, char *arg)
                curp->outtype = OUTT_MAN;
        else if (0 == strcmp(arg, "html"))
                curp->outtype = OUTT_HTML;
+       else if (0 == strcmp(arg, "markdown"))
+               curp->outtype = OUTT_MARKDOWN;
        else if (0 == strcmp(arg, "utf8"))
                curp->outtype = OUTT_UTF8;
        else if (0 == strcmp(arg, "locale"))
@@ -1076,7 +1095,7 @@ spawn_pager(struct tag_files *tag_files)
                break;
        default:
                (void)setpgid(pager_pid, 0);
-               (void)tcsetpgrp(STDIN_FILENO, pager_pid);
+               (void)tcsetpgrp(tag_files->ofd, pager_pid);
 #if HAVE_PLEDGE
                if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
                        err((int)MANDOCLEVEL_SYSERR, "pledge");
@@ -1094,7 +1113,7 @@ spawn_pager(struct tag_files *tag_files)
 
        /* Do not start the pager before controlling the terminal. */
 
-       while (tcgetpgrp(STDIN_FILENO) != getpid())
+       while (tcgetpgrp(STDOUT_FILENO) != getpid())
                nanosleep(&timeout, NULL);
 
        execvp(argv[0], argv);