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

Diff for /mandoc/mdoc_term.c between version 1.89 and 1.101

version 1.89, 2009/10/13 10:57:25 version 1.101, 2009/11/10 11:45:57
Line 18 
Line 18 
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
 #include <err.h>  
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
   #include "out.h"
 #include "term.h"  #include "term.h"
 #include "mdoc.h"  #include "mdoc.h"
 #include "chars.h"  #include "chars.h"
Line 47  struct termact {
Line 47  struct termact {
         void    (*post)(DECL_ARGS);          void    (*post)(DECL_ARGS);
 };  };
   
   static  size_t    a2width(const struct mdoc_argv *, int);
   static  size_t    a2height(const struct mdoc_node *);
   static  size_t    a2offs(const struct mdoc_argv *);
   
   static  int       arg_hasattr(int, const struct mdoc_node *);
   static  int       arg_getattrs(const int *, int *, size_t,
                           const struct mdoc_node *);
   static  int       arg_getattr(int, const struct mdoc_node *);
   static  int       arg_listtype(const struct mdoc_node *);
   static  void      print_bvspace(struct termp *,
                           const struct mdoc_node *,
                           const struct mdoc_node *);
   static  void      print_node(DECL_ARGS);
   static  void      print_head(DECL_ARGS);
   static  void      print_body(DECL_ARGS);
   static  void      print_foot(DECL_ARGS);
   
   #ifdef __linux__
   extern  size_t    strlcpy(char *, const char *, size_t);
   extern  size_t    strlcat(char *, const char *, size_t);
   #endif
   
 static  void      termp____post(DECL_ARGS);  static  void      termp____post(DECL_ARGS);
 static  void      termp_an_post(DECL_ARGS);  static  void      termp_an_post(DECL_ARGS);
 static  void      termp_aq_post(DECL_ARGS);  static  void      termp_aq_post(DECL_ARGS);
Line 235  static const struct termact termacts[MDOC_MAX] = {
Line 257  static const struct termact termacts[MDOC_MAX] = {
         { NULL, termp____post }, /* %Q */          { NULL, termp____post }, /* %Q */
         { termp_sp_pre, NULL }, /* br */          { termp_sp_pre, NULL }, /* br */
         { termp_sp_pre, NULL }, /* sp */          { termp_sp_pre, NULL }, /* sp */
           { termp_under_pre, termp____post }, /* %U */
 };  };
   
 #ifdef __linux__  
 extern  size_t    strlcpy(char *, const char *, size_t);  
 extern  size_t    strlcat(char *, const char *, size_t);  
 #endif  
   
 static  int       arg_hasattr(int, const struct mdoc_node *);  
 static  int       arg_getattrs(const int *, int *, size_t,  
                         const struct mdoc_node *);  
 static  int       arg_getattr(int, const struct mdoc_node *);  
 static  size_t    arg_offset(const struct mdoc_argv *);  
 static  size_t    arg_width(const struct mdoc_argv *, int);  
 static  int       arg_listtype(const struct mdoc_node *);  
 static  void      fmt_block_vspace(struct termp *,  
                         const struct mdoc_node *,  
                         const struct mdoc_node *);  
 static  void      print_node(DECL_ARGS);  
 static  void      print_head(DECL_ARGS);  
 static  void      print_body(DECL_ARGS);  
 static  void      print_foot(DECL_ARGS);  
   
   
 void  void
 terminal_mdoc(void *arg, const struct mdoc *mdoc)  terminal_mdoc(void *arg, const struct mdoc *mdoc)
 {  {
Line 311  print_node(DECL_ARGS)
Line 314  print_node(DECL_ARGS)
         bold = p->bold;          bold = p->bold;
         under = p->under;          under = p->under;
   
         bzero(&npair, sizeof(struct termpair));          memset(&npair, 0, sizeof(struct termpair));
         npair.ppair = pair;          npair.ppair = pair;
   
         if (MDOC_TEXT != n->type) {          if (MDOC_TEXT != n->type) {
Line 344  print_node(DECL_ARGS)
Line 347  print_node(DECL_ARGS)
 static void  static void
 print_foot(DECL_ARGS)  print_foot(DECL_ARGS)
 {  {
         struct tm       *tm;          char            buf[DATESIZ], os[BUFSIZ];
         char            *buf, *os;  
   
           /* Disable meta-fonts. */
           p->metafont = 0;
   
         /*          /*
          * Output the footer in new-groff style, that is, three columns           * Output the footer in new-groff style, that is, three columns
          * with the middle being the manual date and flanking columns           * with the middle being the manual date and flanking columns
Line 355  print_foot(DECL_ARGS)
Line 360  print_foot(DECL_ARGS)
          * SYSTEM                  DATE                    SYSTEM           * SYSTEM                  DATE                    SYSTEM
          */           */
   
         if (NULL == (buf = malloc(p->rmargin)))          time2a(m->date, buf, DATESIZ);
                 err(EXIT_FAILURE, "malloc");          strlcpy(os, m->os, BUFSIZ);
         if (NULL == (os = malloc(p->rmargin)))  
                 err(EXIT_FAILURE, "malloc");  
   
         tm = localtime(&m->date);  
   
         if (0 == strftime(buf, p->rmargin, "%B %e, %Y", tm))  
                 err(EXIT_FAILURE, "strftime");  
   
         (void)strlcpy(os, m->os, p->rmargin);  
   
         term_vspace(p);          term_vspace(p);
   
         p->offset = 0;          p->offset = 0;
Line 394  print_foot(DECL_ARGS)
Line 390  print_foot(DECL_ARGS)
         p->offset = 0;          p->offset = 0;
         p->rmargin = p->maxrmargin;          p->rmargin = p->maxrmargin;
         p->flags = 0;          p->flags = 0;
   
         free(buf);  
         free(os);  
 }  }
   
   
Line 405  print_foot(DECL_ARGS)
Line 398  print_foot(DECL_ARGS)
 static void  static void
 print_head(DECL_ARGS)  print_head(DECL_ARGS)
 {  {
         char            *buf, *title;          char            buf[BUFSIZ], title[BUFSIZ];
   
         p->rmargin = p->maxrmargin;          p->rmargin = p->maxrmargin;
         p->offset = 0;          p->offset = 0;
   
         if (NULL == (buf = malloc(p->rmargin)))  
                 err(EXIT_FAILURE, "malloc");  
         if (NULL == (title = malloc(p->rmargin)))  
                 err(EXIT_FAILURE, "malloc");  
   
         /*          /*
          * The header is strange.  It has three components, which are           * The header is strange.  It has three components, which are
          * really two with the first duplicated.  It goes like this:           * really two with the first duplicated.  It goes like this:
Line 429  print_head(DECL_ARGS)
Line 417  print_head(DECL_ARGS)
          */           */
   
         assert(m->vol);          assert(m->vol);
         (void)strlcpy(buf, m->vol, p->rmargin);          strlcpy(buf, m->vol, BUFSIZ);
   
         if (m->arch) {          if (m->arch) {
                 (void)strlcat(buf, " (", p->rmargin);                  strlcat(buf, " (", BUFSIZ);
                 (void)strlcat(buf, m->arch, p->rmargin);                  strlcat(buf, m->arch, BUFSIZ);
                 (void)strlcat(buf, ")", p->rmargin);                  strlcat(buf, ")", BUFSIZ);
         }          }
   
         snprintf(title, p->rmargin, "%s(%d)", m->title, m->msec);          snprintf(title, BUFSIZ, "%s(%d)", m->title, m->msec);
   
         p->offset = 0;          p->offset = 0;
         p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;          p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
Line 464  print_head(DECL_ARGS)
Line 452  print_head(DECL_ARGS)
         p->offset = 0;          p->offset = 0;
         p->rmargin = p->maxrmargin;          p->rmargin = p->maxrmargin;
         p->flags &= ~TERMP_NOSPACE;          p->flags &= ~TERMP_NOSPACE;
   
         free(title);  
         free(buf);  
 }  }
   
   
 /* FIXME: put in utility file for front-ends. */  
 static size_t  static size_t
 arg_width(const struct mdoc_argv *arg, int pos)  a2height(const struct mdoc_node *n)
 {  {
         int              i, len;          struct roffsu    su;
         const char      *p;  
   
         assert(pos < (int)arg->sz && pos >= 0);          assert(MDOC_TEXT == n->type);
         assert(arg->value[pos]);          assert(n->string);
           if ( ! a2roffsu(n->string, &su, SCALE_VS))
                   SCALE_VS_INIT(&su, strlen(n->string));
   
         p = arg->value[pos];          return(term_vspan(&su));
   }
   
         if (0 == (len = (int)strlen(p)))  
                 return(0);  
   
         for (i = 0; i < len - 1; i++)  static size_t
                 if ( ! isdigit((u_char)p[i]))  a2width(const struct mdoc_argv *arg, int pos)
                         break;  {
           struct roffsu    su;
   
         if (i == len - 1)          assert(arg->value[pos]);
                 if ('n' == p[len - 1] || 'm' == p[len - 1])          if ( ! a2roffsu(arg->value[pos], &su, SCALE_MAX))
                         return((size_t)atoi(p) + 2);                  SCALE_HS_INIT(&su, strlen(arg->value[pos]));
   
         return((size_t)len + 2);          /* XXX: pachemu? */
           return(term_hspan(&su) + 2);
 }  }
   
   
 /* FIXME: put in utility file for front-ends. */  
 static int  static int
 arg_listtype(const struct mdoc_node *n)  arg_listtype(const struct mdoc_node *n)
 {  {
Line 539  arg_listtype(const struct mdoc_node *n)
Line 524  arg_listtype(const struct mdoc_node *n)
 }  }
   
   
 /* FIXME: put in utility file for front-ends. */  
 static size_t  static size_t
 arg_offset(const struct mdoc_argv *arg)  a2offs(const struct mdoc_argv *arg)
 {  {
         int              len, i;          struct roffsu    su;
         const char      *p;  
   
         assert(*arg->value);          if ('\0' == arg->value[0][0])
         p = *arg->value;  
   
         if (0 == strcmp(p, "left"))  
                 return(0);                  return(0);
         if (0 == strcmp(p, "indent"))          else if (0 == strcmp(arg->value[0], "left"))
                   return(0);
           else if (0 == strcmp(arg->value[0], "indent"))
                 return(INDENT + 1);                  return(INDENT + 1);
         if (0 == strcmp(p, "indent-two"))          else if (0 == strcmp(arg->value[0], "indent-two"))
                 return((INDENT + 1) * 2);                  return((INDENT + 1) * 2);
           else if ( ! a2roffsu(arg->value[0], &su, SCALE_MAX))
                   SCALE_HS_INIT(&su, strlen(arg->value[0]));
   
         if (0 == (len = (int)strlen(p)))          return(term_hspan(&su));
                 return(0);  
   
         for (i = 0; i < len - 1; i++)  
                 if ( ! isdigit((u_char)p[i]))  
                         break;  
   
         if (i == len - 1)  
                 if ('n' == p[len - 1] || 'm' == p[len - 1])  
                         return((size_t)atoi(p));  
   
         return((size_t)len);  
 }  }
   
   
Line 607  arg_getattrs(const int *keys, int *vals, 
Line 580  arg_getattrs(const int *keys, int *vals, 
 }  }
   
   
 /* ARGSUSED */  
 static void  static void
 fmt_block_vspace(struct termp *p,  print_bvspace(struct termp *p,
                 const struct mdoc_node *bl,                  const struct mdoc_node *bl,
                 const struct mdoc_node *n)                  const struct mdoc_node *n)
 {  {
Line 689  termp_it_pre(DECL_ARGS)
Line 661  termp_it_pre(DECL_ARGS)
         size_t                  width, offset;          size_t                  width, offset;
   
         if (MDOC_BLOCK == n->type) {          if (MDOC_BLOCK == n->type) {
                 fmt_block_vspace(p, n->parent->parent, n);                  print_bvspace(p, n->parent->parent, n);
                 return(1);                  return(1);
         }          }
   
Line 732  termp_it_pre(DECL_ARGS)
Line 704  termp_it_pre(DECL_ARGS)
                 for (i = 0, nn = n->prev; nn &&                  for (i = 0, nn = n->prev; nn &&
                                 i < (int)bl->args->argv[vals[2]].sz;                                  i < (int)bl->args->argv[vals[2]].sz;
                                 nn = nn->prev, i++)                                  nn = nn->prev, i++)
                         offset += arg_width                          offset += a2width
                                 (&bl->args->argv[vals[2]], i);                                  (&bl->args->argv[vals[2]], i);
   
                 /* Whether exceeds maximum column. */                  /* Whether exceeds maximum column. */
                 if (i < (int)bl->args->argv[vals[2]].sz)                  if (i < (int)bl->args->argv[vals[2]].sz)
                         width = arg_width(&bl->args->argv[vals[2]], i);                          width = a2width(&bl->args->argv[vals[2]], i);
                 else                  else
                         width = 0;                          width = 0;
   
                 if (vals[1] >= 0)                  if (vals[1] >= 0)
                         offset += arg_offset(&bl->args->argv[vals[1]]);                          offset += a2offs(&bl->args->argv[vals[1]]);
                 break;                  break;
         default:          default:
                 if (vals[0] >= 0)                  if (vals[0] >= 0)
                         width = arg_width(&bl->args->argv[vals[0]], 0);                          width = a2width(&bl->args->argv[vals[0]], 0);
                 if (vals[1] >= 0)                  if (vals[1] >= 0)
                         offset += arg_offset(&bl->args->argv[vals[1]]);                          offset += a2offs(&bl->args->argv[vals[1]]);
                 break;                  break;
         }          }
   
Line 1550  termp_bd_pre(DECL_ARGS)
Line 1522  termp_bd_pre(DECL_ARGS)
         const struct mdoc_node  *nn;          const struct mdoc_node  *nn;
   
         if (MDOC_BLOCK == n->type) {          if (MDOC_BLOCK == n->type) {
                 fmt_block_vspace(p, n, n);                  print_bvspace(p, n, n);
                 return(1);                  return(1);
         } else if (MDOC_BODY != n->type)          } else if (MDOC_BODY != n->type)
                 return(1);                  return(1);
Line 1559  termp_bd_pre(DECL_ARGS)
Line 1531  termp_bd_pre(DECL_ARGS)
   
         for (type = -1, i = 0; i < (int)nn->args->argc; i++) {          for (type = -1, i = 0; i < (int)nn->args->argc; i++) {
                 switch (nn->args->argv[i].arg) {                  switch (nn->args->argv[i].arg) {
                   case (MDOC_Centred):
                           /* FALLTHROUGH */
                 case (MDOC_Ragged):                  case (MDOC_Ragged):
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case (MDOC_Filled):                  case (MDOC_Filled):
Line 1569  termp_bd_pre(DECL_ARGS)
Line 1543  termp_bd_pre(DECL_ARGS)
                         type = nn->args->argv[i].arg;                          type = nn->args->argv[i].arg;
                         break;                          break;
                 case (MDOC_Offset):                  case (MDOC_Offset):
                         p->offset += arg_offset(&nn->args->argv[i]);                          p->offset += a2offs(&nn->args->argv[i]);
                         break;                          break;
                 default:                  default:
                         break;                          break;
Line 1663  termp_xx_pre(DECL_ARGS)
Line 1637  termp_xx_pre(DECL_ARGS)
                 pp = "BSDI BSD/OS";                  pp = "BSDI BSD/OS";
                 break;                  break;
         case (MDOC_Dx):          case (MDOC_Dx):
                 pp = "DragonFlyBSD";                  pp = "DragonFly";
                 break;                  break;
         case (MDOC_Fx):          case (MDOC_Fx):
                 pp = "FreeBSD";                  pp = "FreeBSD";
Line 1820  termp_in_post(DECL_ARGS)
Line 1794  termp_in_post(DECL_ARGS)
 static int  static int
 termp_sp_pre(DECL_ARGS)  termp_sp_pre(DECL_ARGS)
 {  {
         int              i, len;          size_t           i, len;
   
         switch (n->tok) {          switch (n->tok) {
         case (MDOC_sp):          case (MDOC_sp):
                 len = n->child ? atoi(n->child->string) : 1;                  len = n->child ? a2height(n->child) : 1;
                 break;                  break;
         case (MDOC_br):          case (MDOC_br):
                 len = 0;                  len = 0;
Line 2020  static void
Line 1994  static void
 termp____post(DECL_ARGS)  termp____post(DECL_ARGS)
 {  {
   
           /* TODO: %U. */
   
         p->flags |= TERMP_NOSPACE;          p->flags |= TERMP_NOSPACE;
         switch (n->tok) {          switch (n->tok) {
         case (MDOC__T):          case (MDOC__T):
Line 2039  termp_lk_pre(DECL_ARGS)
Line 2015  termp_lk_pre(DECL_ARGS)
 {  {
         const struct mdoc_node *nn;          const struct mdoc_node *nn;
   
         if (NULL == (nn = n->child->next)) {          p->under++;
                 p->under++;          nn = n->child;
   
           if (NULL == nn->next)
                 return(1);                  return(1);
         }  
   
         p->under++;  
         term_word(p, nn->string);          term_word(p, nn->string);
           p->under--;
   
         p->flags |= TERMP_NOSPACE;          p->flags |= TERMP_NOSPACE;
         term_word(p, ":");          term_word(p, ":");
         p->under--;  
   
         p->bold++;          p->bold++;
         for (nn = nn->next; nn; nn = nn->next)          for (nn = nn->next; nn; nn = nn->next)

Legend:
Removed from v.1.89  
changed lines
  Added in v.1.101

CVSweb