]> 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 e240719b92b5e5ba1935da01a13cd3422cc556e5..22b825302125624eb733574bb64a3c726d7d053e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,7 +1,7 @@
-/*     $Id: main.c,v 1.275 2016/08/09 15:09:27 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-2016 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2010-2012, 2014-2017 Ingo Schwarze <schwarze@openbsd.org>
  * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
@@ -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 */
@@ -95,11 +96,12 @@ static      int               koptions(int *, char *);
 static int               moptions(int *, char *);
 static void              mmsg(enum mandocerr, enum mandoclevel,
                                const char *, int, int, const char *);
+static void              outdata_alloc(struct curparse *);
 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};
@@ -112,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;
@@ -150,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
 
@@ -167,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;
@@ -245,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;
@@ -293,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))
@@ -300,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
 
@@ -465,8 +478,11 @@ main(int argc, char *argv[])
                                passthrough(resp->file, fd,
                                    conf.output.synopsisonly);
 
-                       if (argc > 1 && curp.outtype <= OUTT_UTF8)
+                       if (argc > 1 && curp.outtype <= OUTT_UTF8) {
+                               if (curp.outdata == NULL)
+                                       outdata_alloc(&curp);
                                terminal_sepline(curp.outdata);
+                       }
                } else if (rc < MANDOCLEVEL_ERROR)
                        rc = MANDOCLEVEL_ERROR;
 
@@ -527,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;
@@ -543,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);
@@ -722,32 +738,8 @@ parse(struct curparse *curp, int fd, const char *file)
        if (rctmp != MANDOCLEVEL_OK && curp->wstop)
                return;
 
-       /* If unset, allocate output dev now (if applicable). */
-
-       if (curp->outdata == NULL) {
-               switch (curp->outtype) {
-               case OUTT_HTML:
-                       curp->outdata = html_alloc(curp->outopts);
-                       break;
-               case OUTT_UTF8:
-                       curp->outdata = utf8_alloc(curp->outopts);
-                       break;
-               case OUTT_LOCALE:
-                       curp->outdata = locale_alloc(curp->outopts);
-                       break;
-               case OUTT_ASCII:
-                       curp->outdata = ascii_alloc(curp->outopts);
-                       break;
-               case OUTT_PDF:
-                       curp->outdata = pdf_alloc(curp->outopts);
-                       break;
-               case OUTT_PS:
-                       curp->outdata = ps_alloc(curp->outopts);
-                       break;
-               default:
-                       break;
-               }
-       }
+       if (curp->outdata == NULL)
+               outdata_alloc(curp);
 
        mparse_result(curp->mp, &man, NULL);
 
@@ -756,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);
@@ -774,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);
@@ -801,6 +798,34 @@ parse(struct curparse *curp, int fd, const char *file)
                        break;
                }
        }
+       mparse_updaterc(curp->mp, &rc);
+}
+
+static void
+outdata_alloc(struct curparse *curp)
+{
+       switch (curp->outtype) {
+       case OUTT_HTML:
+               curp->outdata = html_alloc(curp->outopts);
+               break;
+       case OUTT_UTF8:
+               curp->outdata = utf8_alloc(curp->outopts);
+               break;
+       case OUTT_LOCALE:
+               curp->outdata = locale_alloc(curp->outopts);
+               break;
+       case OUTT_ASCII:
+               curp->outdata = ascii_alloc(curp->outopts);
+               break;
+       case OUTT_PDF:
+               curp->outdata = pdf_alloc(curp->outopts);
+               break;
+       case OUTT_PS:
+               curp->outdata = ps_alloc(curp->outopts);
+               break;
+       default:
+               break;
+       }
 }
 
 static void
@@ -813,11 +838,17 @@ passthrough(const char *file, int fd, int synopsis_only)
        const char      *syscall;
        char            *line, *cp;
        size_t           linesz;
+       ssize_t          len, written;
        int              print;
 
        line = NULL;
        linesz = 0;
 
+       if (fflush(stdout) == EOF) {
+               syscall = "fflush";
+               goto fail;
+       }
+
        if ((stream = fdopen(fd, "r")) == NULL) {
                close(fd);
                syscall = "fdopen";
@@ -825,14 +856,16 @@ passthrough(const char *file, int fd, int synopsis_only)
        }
 
        print = 0;
-       while (getline(&line, &linesz, stream) != -1) {
+       while ((len = getline(&line, &linesz, stream)) != -1) {
                cp = line;
                if (synopsis_only) {
                        if (print) {
                                if ( ! isspace((unsigned char)*cp))
                                        goto done;
-                               while (isspace((unsigned char)*cp))
+                               while (isspace((unsigned char)*cp)) {
                                        cp++;
+                                       len--;
+                               }
                        } else {
                                if (strcmp(cp, synb) == 0 ||
                                    strcmp(cp, synr) == 0)
@@ -840,9 +873,11 @@ passthrough(const char *file, int fd, int synopsis_only)
                                continue;
                        }
                }
-               if (fputs(cp, stdout)) {
+               for (; len > 0; len -= written) {
+                       if ((written = write(STDOUT_FILENO, cp, len)) != -1)
+                               continue;
                        fclose(stream);
-                       syscall = "fputs";
+                       syscall = "write";
                        goto fail;
                }
        }
@@ -919,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"))
@@ -1058,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");
@@ -1076,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);