[BACK]Return to mdocterm.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/Attic/mdocterm.c between version 1.44 and 1.49

version 1.44, 2009/03/15 07:18:10 version 1.49, 2009/03/19 11:49:00
Line 1 
Line 1 
 /* $Id$ */  /* $Id$ */
 /*  /*
  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the   * purpose with or without fee is hereby granted, provided that the
Line 17 
Line 17 
  * PERFORMANCE OF THIS SOFTWARE.   * PERFORMANCE OF THIS SOFTWARE.
  */   */
 #include <sys/types.h>  #include <sys/types.h>
   #include <sys/stat.h>
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  
 #include <err.h>  #include <err.h>
   #include <fcntl.h>
 #include <getopt.h>  #include <getopt.h>
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   
 #include "mmain.h"  
 #include "term.h"  #include "term.h"
   
 struct  nroffopt {  #define WARN_WALL         0x03          /* All-warnings mask. */
         int               fl_h;  #define WARN_WCOMPAT     (1 << 0)       /* Compatibility warnings. */
         int               fl_i;  #define WARN_WSYNTAX     (1 << 1)       /* Syntax warnings. */
         char             *arg_m;  #define WARN_WERR        (1 << 2)       /* Warnings->errors. */
         char             *arg_n;  
         char             *arg_o;  
         char             *arg_r;  
         char             *arg_T;  
         struct termp     *termp; /* Ephemeral. */  
 };  
   
 struct  termseq {  enum    termt {
         const char       *enc;          TERMT_ASCII,
         int               sym;          TERMT_LINT,
           TERMT_TREE
 };  };
   
 dead_pre void             punt(struct nroffopt *, char *) dead_post;  extern  char             *__progname;
 static  int               option(void *, int, char *);  
 static  int               optsopt(struct termp *, char *);  __dead  static void       version(void);
 static  void              body(struct termp *,  __dead  static void       usage(void);
                                 struct termpair *,  #if 0
   __dead  static void       punt(struct cmdargs *, char **);
   #endif
   static  int               foptions(int *, char *);
   static  int               toptions(enum termt *, char *);
   static  int               woptions(int *, char *);
   static  int               merr(void *, int, int, const char *);
   static  int               mwarn(void *, int, int,
                                   enum mdoc_warn, const char *);
   static  void              body(struct termp *, struct termpair *,
                                 const struct mdoc_meta *,                                  const struct mdoc_meta *,
                                 const struct mdoc_node *);                                  const struct mdoc_node *);
 static  void              header(struct termp *,  static  void              header(struct termp *,
                                 const struct mdoc_meta *);                                  const struct mdoc_meta *);
 static  void              footer(struct termp *,  static  void              footer(struct termp *,
                                 const struct mdoc_meta *);                                  const struct mdoc_meta *);
   static  int               file(char **, size_t *, char **, size_t *,
 static  void              pword(struct termp *, const char *, size_t);                                  const char *, struct mdoc *);
 static  void              pescape(struct termp *, const char *,  static  int               fdesc(char **, size_t *, char **, size_t *,
                                 size_t *, size_t);                                  const char *, int, struct mdoc *);
   static  void              pword(struct termp *, const char *, int);
   static  void              pescape(struct termp *,
                                   const char *, int *, int);
 static  void              nescape(struct termp *,  static  void              nescape(struct termp *,
                                 const char *, size_t);                                  const char *, size_t);
 static  void              chara(struct termp *, char);  static  void              chara(struct termp *, char);
 static  void              stringa(struct termp *,  static  void              stringa(struct termp *,
                                 const char *, size_t);                                  const char *, size_t);
 static  void              symbola(struct termp *, enum tsym);  
 static  void              sanity(const struct mdoc_node *);  static  void              sanity(const struct mdoc_node *);
   
 #ifdef __linux__  
 extern  size_t            strlcat(char *, const char *, size_t);  
 extern  size_t            strlcpy(char *, const char *, size_t);  
 #endif  
   
 static  struct termseq    termenc1[] = {  int
         { "\\",           TERMSYM_SLASH },  main(int argc, char *argv[])
         { "\'",           TERMSYM_RSQUOTE },  {
         { "`",            TERMSYM_LSQUOTE },          struct termp     termp;
         { "-",            TERMSYM_HYPHEN },          int              c, fflags, wflags;
         { " ",            TERMSYM_SPACE },          struct mdoc_cb   cb;
         { ".",            TERMSYM_PERIOD },          struct mdoc     *mdoc;
         { "&",            TERMSYM_BREAK },          char            *buf, *line;
         { "e",            TERMSYM_SLASH },          size_t           bufsz, linesz;
         { "q",            TERMSYM_DQUOTE },          enum termt       termt;
         { "|",            TERMSYM_BREAK },  
         { NULL,           0 }  
 };  
   
 static  struct termseq    termenc2[] = {          bzero(&termp, sizeof(struct termp));
         { "rC",           TERMSYM_RBRACE },          bzero(&cb, sizeof(struct mdoc_cb));
         { "lC",           TERMSYM_LBRACE },  
         { "rB",           TERMSYM_RBRACK },  
         { "lB",           TERMSYM_LBRACK },  
         { "ra",           TERMSYM_RANGLE },  
         { "la",           TERMSYM_LANGLE },  
         { "Lq",           TERMSYM_LDQUOTE },  
         { "lq",           TERMSYM_LDQUOTE },  
         { "Rq",           TERMSYM_RDQUOTE },  
         { "rq",           TERMSYM_RDQUOTE },  
         { "oq",           TERMSYM_LSQUOTE },  
         { "aq",           TERMSYM_RSQUOTE },  
   
         { "<-",           TERMSYM_LARROW },          termt = TERMT_ASCII;
         { "->",           TERMSYM_RARROW },          fflags = wflags = 0;
         { "ua",           TERMSYM_UARROW },  
         { "da",           TERMSYM_DARROW },  
   
         { "bu",           TERMSYM_BULLET },          /* LINTED */
         { "Ba",           TERMSYM_BAR },          while (-1 != (c = getopt(argc, argv, "f:VW:T:")))
         { "ba",           TERMSYM_BAR },                  switch (c) {
         { "co",           TERMSYM_COPY },                  case ('f'):
         { "Am",           TERMSYM_AMP },                          if ( ! foptions(&fflags, optarg))
                                   return(0);
                           break;
                   case ('T'):
                           if ( ! toptions(&termt, optarg))
                                   return(0);
                           break;
                   case ('W'):
                           if ( ! woptions(&wflags, optarg))
                                   return(0);
                           break;
                   case ('V'):
                           version();
                           /* NOTREACHED */
                   default:
                           usage();
                           /* NOTREACHED */
                   }
   
         { "Le",           TERMSYM_LE },          argc -= optind;
         { "<=",           TERMSYM_LE },          argv += optind;
         { "Ge",           TERMSYM_GE },  
         { ">=",           TERMSYM_GE },  
         { "==",           TERMSYM_EQ },  
         { "Ne",           TERMSYM_NEQ },  
         { "!=",           TERMSYM_NEQ },  
         { "Pm",           TERMSYM_PLUSMINUS },  
         { "+-",           TERMSYM_PLUSMINUS },  
         { "If",           TERMSYM_INF2 },  
         { "if",           TERMSYM_INF },  
         { "Na",           TERMSYM_NAN },  
         { "na",           TERMSYM_NAN },  
         { "**",           TERMSYM_ASTERISK },  
         { "Gt",           TERMSYM_GT },  
         { "Lt",           TERMSYM_LT },  
   
         { "aa",           TERMSYM_ACUTE },          termp.maxrmargin = 78;                  /* FIXME */
         { "ga",           TERMSYM_GRAVE },  
   
         { "en",           TERMSYM_EN },          cb.mdoc_err = merr;
         { "em",           TERMSYM_EM },          cb.mdoc_warn = mwarn;
   
         { "Pi",           TERMSYM_PI },          /* Line and block buffers persist between parses. */
         { NULL,           0 }  
 };  
   
 /* FIXME: abstract to dynamically-compiled table. */          buf = line = NULL;
 static  struct termsym    termsym_ascii[TERMSYM_MAX] = {          bufsz = linesz = 0;
         { "]", 1 },             /* TERMSYM_RBRACK */  
         { "[", 1 },             /* TERMSYM_LBRACK */  
         { "<-", 2 },            /* TERMSYM_LARROW */  
         { "->", 2 },            /* TERMSYM_RARROW */  
         { "^", 1 },             /* TERMSYM_UARROW */  
         { "v", 1 },             /* TERMSYM_DARROW */  
         { "`", 1 },             /* TERMSYM_LSQUOTE */  
         { "\'", 1 },            /* TERMSYM_RSQUOTE */  
         { "\'", 1 },            /* TERMSYM_SQUOTE */  
         { "``", 2 },            /* TERMSYM_LDQUOTE */  
         { "\'\'", 2 },          /* TERMSYM_RDQUOTE */  
         { "\"", 1 },            /* TERMSYM_DQUOTE */  
         { "<", 1 },             /* TERMSYM_LT */  
         { ">", 1 },             /* TERMSYM_GT */  
         { "<=", 2 },            /* TERMSYM_LE */  
         { ">=", 2 },            /* TERMSYM_GE */  
         { "==", 2 },            /* TERMSYM_EQ */  
         { "!=", 2 },            /* TERMSYM_NEQ */  
         { "\'", 1 },            /* TERMSYM_ACUTE */  
         { "`", 1 },             /* TERMSYM_GRAVE */  
         { "pi", 2 },            /* TERMSYM_PI */  
         { "+=", 2 },            /* TERMSYM_PLUSMINUS */  
         { "oo", 2 },            /* TERMSYM_INF */  
         { "infinity", 8 },      /* TERMSYM_INF2 */  
         { "NaN", 3 },           /* TERMSYM_NAN */  
         { "|", 1 },             /* TERMSYM_BAR */  
         { "o", 1 },             /* TERMSYM_BULLET */  
         { "&", 1 },             /* TERMSYM_AMP */  
         { "--", 2 },            /* TERMSYM_EM */  
         { "-", 1 },             /* TERMSYM_EN */  
         { "(C)", 3 },           /* TERMSYM_COPY */  
         { "*", 1 },             /* TERMSYM_ASTERISK */  
         { "\\", 1 },            /* TERMSYM_SLASH */  
         { "-", 1 },             /* TERMSYM_HYPHEN */  
         { " ", 1 },             /* TERMSYM_SPACE */  
         { ".", 1 },             /* TERMSYM_PERIOD */  
         { "", 0 },              /* TERMSYM_BREAK */  
         { "<", 1 },             /* TERMSYM_LANGLE */  
         { ">", 1 },             /* TERMSYM_RANGLE */  
         { "{", 1 },             /* TERMSYM_LBRACE */  
         { "}", 1 },             /* TERMSYM_RBRACE */  
 };  
   
 int          /* Overall mdoc persists between parses. */
 main(int argc, char *argv[])  
           mdoc = mdoc_alloc(&wflags, fflags, &cb);
   
           while (*argv) {
                   if ( ! file(&line, &linesz, &buf, &bufsz, *argv, mdoc))
                           break;
   
                   switch (termt) {
                   case (TERMT_ASCII):
                           if (NULL == termp.symtab)
                                   termp.symtab = ascii2htab();
                           header(&termp, mdoc_meta(mdoc));
                           body(&termp, NULL, mdoc_meta(mdoc),
                                           mdoc_node(mdoc));
                           footer(&termp, mdoc_meta(mdoc));
                           break;
                   default:
                           break;
                   }
   
                   mdoc_reset(mdoc);
                   argv++;
           }
   
           if (buf)
                   free(buf);
           if (line)
                   free(line);
           if (termp.buf)
                   free(termp.buf);
           if (termp.symtab)
                   asciifree(termp.symtab);
   
           mdoc_free(mdoc);
   
           return(0);
   }
   
   
   __dead static void
   version(void)
 {  {
         struct mmain      *p;  
         const struct mdoc *mdoc;  
         struct nroffopt    nroff;  
         struct termp       termp;  
         int                c;  
         char              *in;  
   
         (void)memset(&termp, 0, sizeof(struct termp));          (void)printf("%s %s\n", __progname, VERSION);
         (void)memset(&nroff, 0, sizeof(struct nroffopt));          exit(0);
           /* NOTREACHED */
   }
   
         termp.maxrmargin = termp.rmargin = 78; /* FIXME */  
         termp.maxcols = 1024; /* FIXME */  
         termp.offset = termp.col = 0;  
         termp.flags = TERMP_NOSPACE;  
         termp.symtab = termsym_ascii;  
         termp.enc = TERMENC_NROFF;  
   
         nroff.termp = &termp;  __dead static void
   usage(void)
   {
   
         p = mmain_alloc();          (void)fprintf(stderr, "usage: %s\n", __progname);
           exit(1);
           /* NOTREACHED */
   }
   
         c = mmain_getopt(p, argc, argv, "[-Ooption...]",  
                         "[infile]", "him:n:o:r:T:O:", &nroff, option);  
   
         /* FIXME: this needs to accept multiple outputs. */  static int
         argv += c;  file(char **ln, size_t *lnsz, char **buf, size_t *bufsz,
         if ((argc -= c) > 0)                  const char *file, struct mdoc *mdoc)
                 in = *argv++;  {
         else          int              fd, c;
                 in = "-";  
   
         mmain_prepare(p, in);          if (-1 == (fd = open(file, O_RDONLY, 0))) {
                   warn("%s", file);
                   return(0);
           }
   
         if (NULL == (mdoc = mmain_process(p))) {          c = fdesc(ln, lnsz, buf, bufsz, file, fd, mdoc);
                 if (TERMP_NOPUNT & termp.iflags)  
                         mmain_exit(p, 1);          if (-1 == close(fd))
                 mmain_free(p);                  warn("%s", file);
                 punt(&nroff, in);  
                 /* NOTREACHED */          return(c);
   }
   
   
   static int
   fdesc(char **lnp, size_t *lnsz, char **bufp, size_t *bufsz,
                   const char *f, int fd, struct mdoc *mdoc)
   {
           size_t           sz;
           ssize_t          ssz;
           struct stat      st;
           int              j, i, pos, lnn;
           char            *ln, *buf;
   
           buf = *bufp;
           ln = *lnp;
   
           /*
            * Two buffers: ln and buf.  buf is the input buffer, optimised
            * for each file's block size.  ln is a line buffer.  Both
            * growable, hence passed in by ptr-ptr.
            */
   
           if (-1 == fstat(fd, &st)) {
                   warnx("%s", f);
                   sz = BUFSIZ;
           } else
                   sz = (unsigned)BUFSIZ > st.st_blksize ?
                           (size_t)BUFSIZ : st.st_blksize;
   
           if (sz > *bufsz) {
                   if (NULL == (buf = realloc(buf, sz)))
                           err(1, "realloc");
                   *bufp = buf;
                   *bufsz = sz;
         }          }
   
         if (NULL == (termp.buf = malloc(termp.maxcols)))          /*
                 err(1, "malloc");           * Fill buf with file blocksize and parse newlines into ln.
            */
   
         header(&termp, mdoc_meta(mdoc));          for (lnn = 1, pos = 0; ; ) {
         body(&termp, NULL, mdoc_meta(mdoc), mdoc_node(mdoc));                  if (-1 == (ssz = read(fd, buf, sz))) {
         footer(&termp, mdoc_meta(mdoc));                          warn("%s", f);
                           return(0);
                   } else if (0 == ssz)
                           break;
   
         free(termp.buf);                  for (i = 0; i < (int)ssz; i++) {
                           if (pos >= (int)*lnsz) {
                                   *lnsz += 256; /* Step-size. */
                                   ln = realloc(ln, *lnsz);
                                   if (NULL == ln)
                                           err(1, "realloc");
                                   *lnp = ln;
                           }
   
         mmain_exit(p, 0);                          if ('\n' != buf[i]) {
         /* NOTREACHED */                                  ln[pos++] = buf[i];
                                   continue;
                           }
   
                           /* Check for CPP-escaped newline.  */
   
                           if (pos > 0 && '\\' == ln[pos - 1]) {
                                   for (j = pos - 1; j >= 0; j--)
                                           if ('\\' != ln[j])
                                                   break;
   
                                   if ( ! ((pos - j) % 2)) {
                                           pos--;
                                           lnn++;
                                           continue;
                                   }
                           }
   
                           ln[pos] = 0;
                           if ( ! mdoc_parseln(mdoc, lnn, ln))
                                   return(0);
                           lnn++;
                           pos = 0;
                   }
           }
   
           return(mdoc_endparse(mdoc));
 }  }
   
   
 static int  static int
 optsopt(struct termp *p, char *arg)  toptions(enum termt *tflags, char *arg)
 {  {
   
           if (0 == strcmp(arg, "ascii"))
                   *tflags = TERMT_ASCII;
           else if (0 == strcmp(arg, "lint"))
                   *tflags = TERMT_LINT;
           else if (0 == strcmp(arg, "tree"))
                   *tflags = TERMT_TREE;
           else {
                   warnx("bad argument: -T%s", arg);
                   return(0);
           }
   
           return(1);
   }
   
   
   /*
    * Parse out the options for [-fopt...] setting compiler options.  These
    * can be comma-delimited or called again.
    */
   static int
   foptions(int *fflags, char *arg)
   {
         char            *v;          char            *v;
         char            *toks[] = { "ansi", "nopunt", NULL };          char            *toks[] = { "ign-scope", "ign-escape",
                                       "ign-macro", NULL };
   
         while (*arg)          while (*arg)
                 switch (getsubopt(&arg, toks, &v)) {                  switch (getsubopt(&arg, toks, &v)) {
                 case (0):                  case (0):
                         p->enc = TERMENC_ANSI;                          *fflags |= MDOC_IGN_SCOPE;
                         break;                          break;
                 case (1):                  case (1):
                         p->iflags |= TERMP_NOPUNT;                          *fflags |= MDOC_IGN_ESCAPE;
                         break;                          break;
                   case (2):
                           *fflags |= MDOC_IGN_MACRO;
                           break;
                 default:                  default:
                         warnx("unknown -O argument");                          warnx("bad argument: -f%s", arg);
                         return(0);                          return(0);
                 }                  }
   
Line 266  optsopt(struct termp *p, char *arg)
Line 343  optsopt(struct termp *p, char *arg)
 }  }
   
   
   /*
    * Parse out the options for [-Werr...], which sets warning modes.
    * These can be comma-delimited or called again.  XXX - should this be
    * using -w like troff?
    */
 static int  static int
 option(void *ptr, int c, char *arg)  woptions(int *wflags, char *arg)
 {  {
         struct termp    *termp;          char            *v;
         struct nroffopt *nroff;          char            *toks[] = { "all", "compat",
                           "syntax", "error", NULL };
   
         nroff = (struct nroffopt *)ptr;          while (*arg)
         termp = nroff->termp;                  switch (getsubopt(&arg, toks, &v)) {
                   case (0):
                           *wflags |= WARN_WALL;
                           break;
                   case (1):
                           *wflags |= WARN_WCOMPAT;
                           break;
                   case (2):
                           *wflags |= WARN_WSYNTAX;
                           break;
                   case (3):
                           *wflags |= WARN_WERR;
                           break;
                   default:
                           warnx("bad argument: -W%s", arg);
                           return(0);
                   }
   
         switch (c) {  
         case ('h'):  
                 nroff->fl_h = 1;  
                 break;  
         case ('i'):  
                 nroff->fl_i = 1;  
                 break;  
         case ('m'):  
                 nroff->arg_m = arg;  
                 break;  
         case ('n'):  
                 nroff->arg_n = arg;  
                 break;  
         case ('o'):  
                 nroff->arg_o = arg;  
                 break;  
         case ('r'):  
                 nroff->arg_r = arg;  
                 break;  
         case ('T'):  
                 nroff->arg_T = arg;  
                 break;  
         case ('O'):  
                 return(optsopt(termp, arg));  
         default:  
                 break;  
         }  
   
         return(1);          return(1);
 }  }
   
Line 327  option(void *ptr, int c, char *arg)
Line 398  option(void *ptr, int c, char *arg)
  *    columns.  In short: don't print a newline and instead pad to the   *    columns.  In short: don't print a newline and instead pad to the
  *    right margin.  Used in conjunction with TERMP_NOLPAD.   *    right margin.  Used in conjunction with TERMP_NOLPAD.
  *   *
    *  - TERMP_NONOBREAK: don't newline when TERMP_NOBREAK is specified.
    *
  *  In-line line breaking:   *  In-line line breaking:
  *   *
  *  If TERMP_NOBREAK is specified and the line overruns the right   *  If TERMP_NOBREAK is specified and the line overruns the right
Line 342  option(void *ptr, int c, char *arg)
Line 415  option(void *ptr, int c, char *arg)
 void  void
 flushln(struct termp *p)  flushln(struct termp *p)
 {  {
         size_t           i, j, vsz, vis, maxvis, mmax, bp;          int              i, j;
           size_t           vsz, vis, maxvis, mmax, bp;
   
         /*          /*
          * First, establish the maximum columns of "visible" content.           * First, establish the maximum columns of "visible" content.
Line 365  flushln(struct termp *p)
Line 439  flushln(struct termp *p)
   
         if ( ! (p->flags & TERMP_NOLPAD))          if ( ! (p->flags & TERMP_NOLPAD))
                 /* LINTED */                  /* LINTED */
                 for (j = 0; j < p->offset; j++)                  for (j = 0; j < (int)p->offset; j++)
                         putchar(' ');                          putchar(' ');
   
         for (i = 0; i < p->col; i++) {          for (i = 0; i < (int)p->col; i++) {
                 /*                  /*
                  * Count up visible word characters.  Control sequences                   * Count up visible word characters.  Control sequences
                  * (starting with the CSI) aren't counted.  A space                   * (starting with the CSI) aren't counted.  A space
Line 377  flushln(struct termp *p)
Line 451  flushln(struct termp *p)
                  */                   */
   
                 /* LINTED */                  /* LINTED */
                 for (j = i, vsz = 0; j < p->col; j++) {                  for (j = i, vsz = 0; j < (int)p->col; j++) {
                         if (isspace((u_char)p->buf[j])) {                          if (' ' == p->buf[j])
                                 break;                                  break;
                         } else if (27 == p->buf[j]) {                          else if (8 == p->buf[j])
                                 assert(TERMENC_ANSI == p->enc);  
                                 assert(j + 5 <= p->col);  
                                 j += 4;  
                         } else if (8 == p->buf[j]) {  
                                 assert(TERMENC_NROFF == p->enc);  
                                 assert(j + 2 <= p->col);  
                                 j += 1;                                  j += 1;
                         } else                          else
                                 vsz++;                                  vsz++;
                 }                  }
   
Line 403  flushln(struct termp *p)
Line 471  flushln(struct termp *p)
                 if ( ! (TERMP_NOBREAK & p->flags)) {                  if ( ! (TERMP_NOBREAK & p->flags)) {
                         if (vis && vis + vsz > bp) {                          if (vis && vis + vsz > bp) {
                                 putchar('\n');                                  putchar('\n');
                                 for (j = 0; j < p->offset; j++)                                  for (j = 0; j < (int)p->offset; j++)
                                         putchar(' ');                                          putchar(' ');
                                 vis = 0;                                  vis = 0;
                         } else if (vis + vsz > bp)                          } else if (vis + vsz > bp)
Line 414  flushln(struct termp *p)
Line 482  flushln(struct termp *p)
                 } else {                  } else {
                         if (vis && vis + vsz > bp) {                          if (vis && vis + vsz > bp) {
                                 putchar('\n');                                  putchar('\n');
                                 for (j = 0; j < p->rmargin; j++)                                  for (j = 0; j < (int)p->rmargin; j++)
                                         putchar(' ');                                          putchar(' ');
                                 vis = p->rmargin - p->offset;                                  vis = p->rmargin - p->offset;
                         } else if (vis + vsz > bp)                          } else if (vis + vsz > bp)
Line 429  flushln(struct termp *p)
Line 497  flushln(struct termp *p)
                  * our breakpoint.                   * our breakpoint.
                  */                   */
   
                 for ( ; i < p->col; i++) {                  for ( ; i < (int)p->col; i++) {
                         if (isspace((u_char)p->buf[i]))                          if (' ' == p->buf[i])
                                 break;                                  break;
                         putchar(p->buf[i]);                          putchar(p->buf[i]);
                 }                  }
                 vis += vsz;                  vis += vsz;
                 if (i < p->col && vis <= bp) {                  if (i < (int)p->col && vis <= bp) {
                         putchar(' ');                          putchar(' ');
                         vis++;                          vis++;
                 }                  }
Line 449  flushln(struct termp *p)
Line 517  flushln(struct termp *p)
         if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {          if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {
                 if ( ! (TERMP_NONOBREAK & p->flags)) {                  if ( ! (TERMP_NONOBREAK & p->flags)) {
                         putchar('\n');                          putchar('\n');
                         for (i = 0; i < p->rmargin; i++)                          for (i = 0; i < (int)p->rmargin; i++)
                                 putchar(' ');                                  putchar(' ');
                 }                  }
                 p->col = 0;                  p->col = 0;
Line 515  vspace(struct termp *p)
Line 583  vspace(struct termp *p)
 void  void
 word(struct termp *p, const char *word)  word(struct termp *p, const char *word)
 {  {
         size_t           i, j, len;          int              i, j, len;
   
         if (p->flags & TERMP_LITERAL) {          if (p->flags & TERMP_LITERAL) {
                 pword(p, word, strlen(word));                  pword(p, word, (int)strlen(word));
                 return;                  return;
         }          }
   
         if (0 == (len = strlen(word)))          if (0 == (len = (int)strlen(word)))
                 errx(1, "blank line not in literal context");                  errx(1, "blank line not in literal context");
   
         if (mdoc_isdelim(word)) {          if (mdoc_isdelim(word)) {
Line 533  word(struct termp *p, const char *word)
Line 601  word(struct termp *p, const char *word)
   
         /* LINTED */          /* LINTED */
         for (j = i = 0; i < len; i++) {          for (j = i = 0; i < len; i++) {
                 if ( ! isspace((u_char)word[i])) {                  if (' ' != word[i]) {
                         j++;                          j++;
                         continue;                          continue;
                 }                  }
   
                 /* Escaped spaces don't delimit... */                  /* Escaped spaces don't delimit... */
                 if (i > 0 && isspace((u_char)word[i]) &&                  if (i && ' ' == word[i] && '\\' == word[i - 1]) {
                                 '\\' == word[i - 1]) {  
                         j++;                          j++;
                         continue;                          continue;
                 }                  }
Line 670  footer(struct termp *p, const struct mdoc_meta *meta)
Line 737  footer(struct termp *p, const struct mdoc_meta *meta)
 static void  static void
 header(struct termp *p, const struct mdoc_meta *meta)  header(struct termp *p, const struct mdoc_meta *meta)
 {  {
         char            *buf, *title, *bufp;          char            *buf, *title;
   
         p->rmargin = p->maxrmargin;          p->rmargin = p->maxrmargin;
         p->offset = 0;          p->offset = 0;
Line 705  header(struct termp *p, const struct mdoc_meta *meta)
Line 772  header(struct termp *p, const struct mdoc_meta *meta)
         (void)snprintf(title, p->rmargin, "%s(%d)",          (void)snprintf(title, p->rmargin, "%s(%d)",
                         meta->title, meta->msec);                          meta->title, meta->msec);
   
         for (bufp = title; *bufp; bufp++)  
                 *bufp = toupper((u_char)*bufp);  
   
         p->offset = 0;          p->offset = 0;
         p->rmargin = (p->maxrmargin - strlen(buf)) / 2;          p->rmargin = (p->maxrmargin - strlen(buf)) / 2;
         p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;          p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
Line 747  header(struct termp *p, const struct mdoc_meta *meta)
Line 811  header(struct termp *p, const struct mdoc_meta *meta)
 static void  static void
 nescape(struct termp *p, const char *word, size_t len)  nescape(struct termp *p, const char *word, size_t len)
 {  {
         struct termseq  *enc;          const char      *rhs;
           size_t           sz;
   
         switch (len) {          if (NULL == (rhs = a2ascii(p->symtab, word, len, &sz)))
         case (1):  
                 enc = termenc1;  
                 break;  
         case (2):  
                 enc = termenc2;  
                 break;  
         default:  
                 warnx("unsupported %zu-byte escape sequence", len);  
                 return;                  return;
         }          stringa(p, rhs, sz);
   
         for ( ; enc->enc; enc++)  
                 if (0 == memcmp(enc->enc, word, len)) {  
                         symbola(p, enc->sym);  
                         return;  
                 }  
   
         warnx("unsupported %zu-byte escape sequence", len);  
 }  }
   
   
Line 777  nescape(struct termp *p, const char *word, size_t len)
Line 826  nescape(struct termp *p, const char *word, size_t len)
  * the escape sequence (we assert upon badly-formed escape sequences).   * the escape sequence (we assert upon badly-formed escape sequences).
  */   */
 static void  static void
 pescape(struct termp *p, const char *word, size_t *i, size_t len)  pescape(struct termp *p, const char *word, int *i, int len)
 {  {
         size_t           j;          int              j;
   
         if (++(*i) >= len) {          if (++(*i) >= len)
                 warnx("ignoring bad escape sequence");  
                 return;                  return;
         }  
   
         if ('(' == word[*i]) {          if ('(' == word[*i]) {
                 (*i)++;                  (*i)++;
                 if (*i + 1 >= len) {                  if (*i + 1 >= len)
                         warnx("ignoring bad escape sequence");  
                         return;                          return;
                 }  
                 nescape(p, &word[*i], 2);                  nescape(p, &word[*i], 2);
                 (*i)++;                  (*i)++;
                 return;                  return;
   
         } else if ('*' == word[*i]) {          } else if ('*' == word[*i]) {
                 (*i)++;                  (*i)++;
                 if (*i >= len) {                  if (*i >= len)
                         warnx("ignoring bad escape sequence");  
                         return;                          return;
                 }  
                 switch (word[*i]) {                  switch (word[*i]) {
                 case ('('):                  case ('('):
                         (*i)++;                          (*i)++;
                         if (*i + 1 >= len) {                          if (*i + 1 >= len)
                                 warnx("ignoring bad escape sequence");  
                                 return;                                  return;
                         }  
                         nescape(p, &word[*i], 2);                          nescape(p, &word[*i], 2);
                         (*i)++;                          (*i)++;
                         return;                          return;
Line 828  pescape(struct termp *p, const char *word, size_t *i, 
Line 872  pescape(struct termp *p, const char *word, size_t *i, 
         for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)          for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
                 /* Loop... */ ;                  /* Loop... */ ;
   
         if (0 == word[*i]) {          if (0 == word[*i])
                 warnx("ignoring bad escape sequence");  
                 return;                  return;
         }  
         nescape(p, &word[*i - j], j);          nescape(p, &word[*i - j], (size_t)j);
 }  }
   
   
Line 842  pescape(struct termp *p, const char *word, size_t *i, 
Line 885  pescape(struct termp *p, const char *word, size_t *i, 
  * handles word styling.   * handles word styling.
  */   */
 static void  static void
 pword(struct termp *p, const char *word, size_t len)  pword(struct termp *p, const char *word, int len)
 {  {
         size_t           i;          int              i;
   
         if ( ! (TERMP_NOSPACE & p->flags) &&          if ( ! (TERMP_NOSPACE & p->flags) &&
                         ! (TERMP_LITERAL & p->flags))                          ! (TERMP_LITERAL & p->flags))
Line 858  pword(struct termp *p, const char *word, size_t len)
Line 901  pword(struct termp *p, const char *word, size_t len)
          * before the word.           * before the word.
          */           */
   
         if (TERMENC_ANSI == p->enc && TERMP_STYLE & p->flags) {  
                 if (TERMP_BOLD & p->flags) {  
                         chara(p, 27);  
                         stringa(p, "[01m", 4);  
                 }  
                 if (TERMP_UNDER & p->flags) {  
                         chara(p, 27);  
                         stringa(p, "[04m", 4);  
                 }  
                 if (TERMP_RED & p->flags) {  
                         chara(p, 27);  
                         stringa(p, "[31m", 4);  
                 }  
                 if (TERMP_GREEN & p->flags) {  
                         chara(p, 27);  
                         stringa(p, "[32m", 4);  
                 }  
                 if (TERMP_YELLOW & p->flags) {  
                         chara(p, 27);  
                         stringa(p, "[33m", 4);  
                 }  
                 if (TERMP_BLUE & p->flags) {  
                         chara(p, 27);  
                         stringa(p, "[34m", 4);  
                 }  
                 if (TERMP_MAGENTA & p->flags) {  
                         chara(p, 27);  
                         stringa(p, "[35m", 4);  
                 }  
                 if (TERMP_CYAN & p->flags) {  
                         chara(p, 27);  
                         stringa(p, "[36m", 4);  
                 }  
         }  
   
         for (i = 0; i < len; i++) {          for (i = 0; i < len; i++) {
                 if ('\\' == word[i]) {                  if ('\\' == word[i]) {
                         pescape(p, word, &i, len);                          pescape(p, word, &i, len);
                         continue;                          continue;
                 }                  }
   
                 if (TERMENC_NROFF == p->enc &&                  if (TERMP_STYLE & p->flags) {
                                 TERMP_STYLE & p->flags) {  
                         if (TERMP_BOLD & p->flags) {                          if (TERMP_BOLD & p->flags) {
                                 chara(p, word[i]);                                  chara(p, word[i]);
                                 chara(p, 8);                                  chara(p, 8);
Line 913  pword(struct termp *p, const char *word, size_t len)
Line 920  pword(struct termp *p, const char *word, size_t len)
   
                 chara(p, word[i]);                  chara(p, word[i]);
         }          }
   
         if (TERMENC_ANSI == p->enc && TERMP_STYLE & p->flags) {  
                 chara(p, 27);  
                 stringa(p, "[00m", 4);  
         }  
 }  }
   
   
 /*  /*
  * Add a symbol to the output line buffer.  
  */  
 static void  
 symbola(struct termp *p, enum tsym sym)  
 {  
   
         assert(p->symtab[sym].sym);  
         stringa(p, p->symtab[sym].sym, p->symtab[sym].sz);  
 }  
   
   
 /*  
  * Like chara() but for arbitrary-length buffers.  Resize the buffer by   * Like chara() but for arbitrary-length buffers.  Resize the buffer by
  * a factor of two (if the buffer is less than that) or the buffer's   * a factor of two (if the buffer is less than that) or the buffer's
  * size.   * size.
Line 946  stringa(struct termp *p, const char *c, size_t sz)
Line 936  stringa(struct termp *p, const char *c, size_t sz)
         if (0 == sz)          if (0 == sz)
                 return;                  return;
   
         s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;  
   
         assert(c);          assert(c);
         if (p->col + sz >= p->maxcols) {          if (p->col + sz >= p->maxcols) {
                   if (0 == p->maxcols)
                           p->maxcols = 256;
                   s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;
                 p->buf = realloc(p->buf, s);                  p->buf = realloc(p->buf, s);
                 if (NULL == p->buf)                  if (NULL == p->buf)
                         err(1, "realloc");                          err(1, "realloc");
                 p->maxcols = s;                  p->maxcols = s;
         }          }
   
         (void)memcpy(&p->buf[p->col], c, sz);          (void)memcpy(&p->buf[(int)p->col], c, sz);
         p->col += sz;          p->col += sz;
 }  }
   
Line 969  stringa(struct termp *p, const char *c, size_t sz)
Line 960  stringa(struct termp *p, const char *c, size_t sz)
 static void  static void
 chara(struct termp *p, char c)  chara(struct termp *p, char c)
 {  {
           size_t           s;
   
         if (p->col + 1 >= p->maxcols) {          if (p->col + 1 >= p->maxcols) {
                 p->buf = realloc(p->buf, p->maxcols * 2);                  if (0 == p->maxcols)
                           p->maxcols = 256;
                   s = p->maxcols * 2;
                   p->buf = realloc(p->buf, s);
                 if (NULL == p->buf)                  if (NULL == p->buf)
                         err(1, "malloc");                          err(1, "realloc");
                 p->maxcols *= 2;                  p->maxcols = s;
         }          }
         p->buf[(p->col)++] = c;          p->buf[(int)(p->col)++] = c;
 }  }
   
   
Line 1090  sanity(const struct mdoc_node *n)
Line 1085  sanity(const struct mdoc_node *n)
 }  }
   
   
 dead_pre void  static int
 punt(struct nroffopt *nroff, char *in)  merr(void *arg, int line, int col, const char *msg)
 {  {
         char            *args[32];  
         char             arg0[32], argm[32];  
         int              i;  
   
         warnx("punting to nroff!");          warnx("error: %s (line %d, column %d)", msg, line, col);
           return(0);
   }
   
         i = 0;  
   
         (void)strlcpy(arg0, "nroff", 32);  static int
         args[i++] = arg0;  mwarn(void *arg, int line, int col,
                   enum mdoc_warn type, const char *msg)
   {
           int              flags;
           char            *wtype;
   
         if (nroff->fl_h)          flags = *(int *)arg;
                 args[i++] = "-h";          wtype = NULL;
         if (nroff->fl_i)  
                 args[i++] = "-i";  
   
         if (nroff->arg_m) {          switch (type) {
                 (void)strlcpy(argm, "-m", 32);          case (WARN_COMPAT):
                 (void)strlcat(argm, nroff->arg_m, 32);                  wtype = "compat";
                 args[i++] = argm;                  if (flags & WARN_WCOMPAT)
         } else                          break;
                 args[i++] = "-mandoc";                  return(1);
           case (WARN_SYNTAX):
                   wtype = "syntax";
                   if (flags & WARN_WSYNTAX)
                           break;
                   return(1);
           }
   
         args[i++] = in;          assert(wtype);
         args[i++] = (char *)NULL;          warnx("%s warning: %s (line %d, column %d)",
                           wtype, msg, line, col);
   
         (void)execvp("nroff", args);          if ( ! (flags & WARN_WERR))
         errx(1, "exec");                  return(1);
         /* NOTREACHED */  
           warnx("%s: considering warnings as errors",
                           __progname);
           return(0);
 }  }
   
   

Legend:
Removed from v.1.44  
changed lines
  Added in v.1.49

CVSweb