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

Diff for /mandoc/out.c between version 1.15 and 1.46

version 1.15, 2010/04/07 11:29:55 version 1.46, 2013/10/05 20:30:05
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
    * Copyright (c) 2011 Ingo Schwarze <schwarze@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 above   * purpose with or without fee is hereby granted, provided that the above
Line 27 
Line 28 
 #include <string.h>  #include <string.h>
 #include <time.h>  #include <time.h>
   
   #include "mandoc.h"
 #include "out.h"  #include "out.h"
   
 /* See a2roffdeco(). */  static  void    tblcalc_data(struct rofftbl *, struct roffcol *,
 #define C2LIM(c, l) do { \                          const struct tbl_opts *, const struct tbl_dat *);
         (l) = 1; \  static  void    tblcalc_literal(struct rofftbl *, struct roffcol *,
         if ('[' == (c) || '\'' == (c)) \                          const struct tbl_dat *);
                 (l) = 0; \  static  void    tblcalc_number(struct rofftbl *, struct roffcol *,
         else if ('(' == (c)) \                          const struct tbl_opts *, const struct tbl_dat *);
                 (l) = 2; } \  
         while (/* CONSTCOND */ 0)  
   
 /* See a2roffdeco(). */  
 #define C2TERM(c, t) do { \  
         (t) = 0; \  
         if ('\'' == (c)) \  
                 (t) = 1; \  
         else if ('[' == (c)) \  
                 (t) = 2; \  
         else if ('(' == (c)) \  
                 (t) = 3; } \  
         while (/* CONSTCOND */ 0)  
   
 /*  /*
  * Convert a `scaling unit' to a consistent form, or fail.  Scaling   * Convert a `scaling unit' to a consistent form, or fail.  Scaling
  * units are documented in groff.7, mdoc.7, man.7.   * units are documented in groff.7, mdoc.7, man.7.
Line 80  a2roffsu(const char *src, struct roffsu *dst, enum rof
Line 69  a2roffsu(const char *src, struct roffsu *dst, enum rof
                 return(0);                  return(0);
   
         while (i < BUFSIZ) {          while (i < BUFSIZ) {
                 if ( ! isdigit((u_char)*src)) {                  if ( ! isdigit((unsigned char)*src)) {
                         if ('.' != *src)                          if ('.' != *src)
                                 break;                                  break;
                         else if (hasd)                          else if (hasd)
Line 136  a2roffsu(const char *src, struct roffsu *dst, enum rof
Line 125  a2roffsu(const char *src, struct roffsu *dst, enum rof
                 return(0);                  return(0);
         }          }
   
           /* FIXME: do this in the caller. */
         if ((dst->scale = atof(buf)) < 0)          if ((dst->scale = atof(buf)) < 0)
                 dst->scale = 0;                  dst->scale = 0;
         dst->unit = unit;          dst->unit = unit;
         dst->pt = hasd;  
   
         return(1);          return(1);
 }  }
   
   
 /*  /*
  * Correctly writes the time in nroff form, which differs from standard   * Calculate the abstract widths and decimal positions of columns in a
  * form in that a space isn't printed in lieu of the extra %e field for   * table.  This routine allocates the columns structures then runs over
  * single-digit dates.   * all rows and cells in the table.  The function pointers in "tbl" are
    * used for the actual width calculations.
  */   */
 void  void
 time2a(time_t t, char *dst, size_t sz)  tblcalc(struct rofftbl *tbl, const struct tbl_span *sp)
 {  {
         struct tm        tm;          const struct tbl_dat    *dp;
         char             buf[5];          struct roffcol          *col;
         char            *p;          int                      spans;
         size_t           nsz;  
   
         assert(sz > 1);          /*
         localtime_r(&t, &tm);           * Allocate the master column specifiers.  These will hold the
            * widths and decimal positions for all cells in the column.  It
            * must be freed and nullified by the caller.
            */
   
         p = dst;          assert(NULL == tbl->cols);
         nsz = 0;          tbl->cols = mandoc_calloc
                   ((size_t)sp->opts->cols, sizeof(struct roffcol));
   
         dst[0] = '\0';          for ( ; sp; sp = sp->next) {
                   if (TBL_SPAN_DATA != sp->pos)
         if (0 == (nsz = strftime(p, sz, "%B ", &tm)))                          continue;
                 return;                  spans = 1;
                   /*
         p += (int)nsz;                   * Account for the data cells in the layout, matching it
         sz -= nsz;                   * to data cells in the data section.
                    */
         if (0 == strftime(buf, sizeof(buf), "%e, ", &tm))                  for (dp = sp->first; dp; dp = dp->next) {
                 return;                          /* Do not used spanned cells in the calculation. */
                           if (0 < --spans)
         nsz = strlcat(p, buf + (' ' == buf[0] ? 1 : 0), sz);                                  continue;
                           spans = dp->spans;
         if (nsz >= sz)                          if (1 < spans)
                 return;                                  continue;
                           assert(dp->layout);
         p += (int)nsz;                          col = &tbl->cols[dp->layout->head->ident];
         sz -= nsz;                          tblcalc_data(tbl, col, sp->opts, dp);
                   }
         (void)strftime(p, sz, "%Y", &tm);          }
 }  }
   
   static void
 /*  tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
  * Returns length of parsed string (the leading "\" should NOT be                  const struct tbl_opts *opts, const struct tbl_dat *dp)
  * included).  This can be zero if the current character is the nil  
  * terminator.  "d" is set to the type of parsed decorator, which may  
  * have an adjoining "word" of size "sz" (e.g., "(ab" -> "ab", 2).  
  */  
 int  
 a2roffdeco(enum roffdeco *d,  
                 const char **word, size_t *sz)  
 {  {
         int              j, term, lim;          size_t           sz;
         char             set;  
         const char      *wp, *sp;  
   
         *d = DECO_NONE;          /* Branch down into data sub-types. */
         wp = *word;  
   
         switch ((set = *wp)) {          switch (dp->layout->pos) {
         case ('\0'):          case (TBL_CELL_HORIZ):
                 return(0);  
   
         case ('('):  
                 if ('\0' == *(++wp))  
                         return(1);  
                 if ('\0' == *(wp + 1))  
                         return(2);  
   
                 *d = DECO_SPECIAL;  
                 *sz = 2;  
                 *word = wp;  
                 return(3);  
   
         case ('F'):  
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case ('f'):          case (TBL_CELL_DHORIZ):
                 /*                  sz = (*tbl->len)(1, tbl->arg);
                  * FIXME: this needs work and consolidation (it should                  if (col->width < sz)
                  * follow the sequence that special characters do, for                          col->width = sz;
                  * one), but isn't a priority at the moment.  Note, for                  break;
                  * one, that in reality \fB != \FB, although here we let          case (TBL_CELL_LONG):
                  * these slip by.                  /* FALLTHROUGH */
                  */          case (TBL_CELL_CENTRE):
                 switch (*(++wp)) {                  /* FALLTHROUGH */
                 case ('\0'):          case (TBL_CELL_LEFT):
                         return(1);                  /* FALLTHROUGH */
                 case ('3'):          case (TBL_CELL_RIGHT):
                         /* FALLTHROUGH */                  tblcalc_literal(tbl, col, dp);
                 case ('B'):                  break;
                         *d = DECO_BOLD;          case (TBL_CELL_NUMBER):
                         return(2);                  tblcalc_number(tbl, col, opts, dp);
                 case ('2'):                  break;
                         /* FALLTHROUGH */          case (TBL_CELL_DOWN):
                 case ('I'):                  break;
                         *d = DECO_ITALIC;          default:
                         return(2);                  abort();
                 case ('P'):                  /* NOTREACHED */
                         *d = DECO_PREVIOUS;          }
                         return(2);  }
                 case ('1'):  
                         /* FALLTHROUGH */  
                 case ('R'):  
                         *d = DECO_ROMAN;  
                         return(2);  
                 case ('('):  
                         if ('\0' == *(++wp))  
                                 return(2);  
                         if ('\0' == *(wp + 1))  
                                 return(3);  
   
                         *d = 'F' == set ? DECO_FFONT : DECO_FONT;  static void
                         *sz = 2;  tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
                         *word = wp;                  const struct tbl_dat *dp)
                         return(4);  {
                 case ('['):          size_t           sz;
                         *word = ++wp;          const char      *str;
                         for (j = 0; *wp && ']' != *wp; wp++, j++)  
                                 /* Loop... */ ;  
   
                         if ('\0' == *wp)          str = dp->string ? dp->string : "";
                                 return(j + 2);          sz = (*tbl->slen)(str, tbl->arg);
   
                         *d = 'F' == set ? DECO_FFONT : DECO_FONT;          if (col->width < sz)
                         *sz = (size_t)j;                  col->width = sz;
                         return(j + 3);  }
                 default:  
                         break;  
                 }  
   
                 *d = 'F' == set ? DECO_FFONT : DECO_FONT;  static void
                 *sz = 1;  tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
                 *word = wp;                  const struct tbl_opts *opts, const struct tbl_dat *dp)
                 return(2);  {
           int              i;
           size_t           sz, psz, ssz, d;
           const char      *str;
           char            *cp;
           char             buf[2];
   
         case ('*'):          /*
                 switch (*(++wp)) {           * First calculate number width and decimal place (last + 1 for
                 case ('\0'):           * non-decimal numbers).  If the stored decimal is subsequent to
                         return(1);           * ours, make our size longer by that difference
            * (right-"shifting"); similarly, if ours is subsequent the
            * stored, then extend the stored size by the difference.
            * Finally, re-assign the stored values.
            */
   
                 case ('('):          str = dp->string ? dp->string : "";
                         if ('\0' == *(++wp))          sz = (*tbl->slen)(str, tbl->arg);
                                 return(2);  
                         if ('\0' == *(wp + 1))  
                                 return(3);  
   
                         *d = DECO_RESERVED;          /* FIXME: TBL_DATA_HORIZ et al.? */
                         *sz = 2;  
                         *word = wp;  
                         return(4);  
   
                 case ('['):          buf[0] = opts->decimal;
                         *word = ++wp;          buf[1] = '\0';
                         for (j = 0; *wp && ']' != *wp; wp++, j++)  
                                 /* Loop... */ ;  
   
                         if ('\0' == *wp)          psz = (*tbl->slen)(buf, tbl->arg);
                                 return(j + 2);  
   
                         *d = DECO_RESERVED;          if (NULL != (cp = strrchr(str, opts->decimal))) {
                         *sz = (size_t)j;                  buf[1] = '\0';
                         return(j + 3);                  for (ssz = 0, i = 0; cp != &str[i]; i++) {
                           buf[0] = str[i];
                 default:                          ssz += (*tbl->slen)(buf, tbl->arg);
                         break;  
                 }                  }
                   d = ssz + psz;
           } else
                   d = sz + psz;
   
                 *d = DECO_RESERVED;          /* Adjust the settings for this column. */
                 *sz = 1;  
                 *word = wp;  
                 return(2);  
   
         case ('s'):          if (col->decimal > d) {
                 sp = wp;                  sz += col->decimal - d;
                 if ('\0' == *(++wp))                  d = col->decimal;
                         return(1);          } else
                   col->width += d - col->decimal;
   
                 C2LIM(*wp, lim);          if (sz > col->width)
                 C2TERM(*wp, term);                  col->width = sz;
           if (d > col->decimal)
                 if (term)                  col->decimal = d;
                         wp++;  
   
                 *word = wp;  
   
                 if (*wp == '+' || *wp == '-')  
                         ++wp;  
   
                 switch (*wp) {  
                 case ('\''):  
                         /* FALLTHROUGH */  
                 case ('['):  
                         /* FALLTHROUGH */  
                 case ('('):  
                         if (term)  
                                 return((int)(wp - sp));  
   
                         C2LIM(*wp, lim);  
                         C2TERM(*wp, term);  
                         wp++;  
                         break;  
                 default:  
                         break;  
                 }  
   
                 if ( ! isdigit((u_char)*wp))  
                         return((int)(wp - sp));  
   
                 for (j = 0; isdigit((u_char)*wp); j++) {  
                         if (lim && j >= lim)  
                                 break;  
                         ++wp;  
                 }  
   
                 if (term && term < 3) {  
                         if (1 == term && *wp != '\'')  
                                 return((int)(wp - sp));  
                         if (2 == term && *wp != ']')  
                                 return((int)(wp - sp));  
                         ++wp;  
                 }  
   
                 *d = DECO_SIZE;  
                 return((int)(wp - sp));  
   
         case ('['):  
                 *word = ++wp;  
   
                 for (j = 0; *wp && ']' != *wp; wp++, j++)  
                         /* Loop... */ ;  
   
                 if ('\0' == *wp)  
                         return(j + 1);  
   
                 *d = DECO_SPECIAL;  
                 *sz = (size_t)j;  
                 return(j + 2);  
   
         case ('c'):  
                 *d = DECO_NOSPACE;  
                 *sz = 1;  
                 return(1);  
   
         default:  
                 break;  
         }  
   
         *d = DECO_SPECIAL;  
         *word = wp;  
         *sz = 1;  
         return(1);  
 }  }

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.46

CVSweb