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

Diff for /mandoc/term.c between version 1.95 and 1.111

version 1.95, 2009/07/27 12:35:54 version 1.111, 2009/10/26 07:18:23
Line 20 
Line 20 
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
   #include "chars.h"
   #include "out.h"
 #include "term.h"  #include "term.h"
 #include "man.h"  #include "man.h"
 #include "mdoc.h"  #include "mdoc.h"
   #include "main.h"
   
 extern  int               man_run(struct termp *,  /* FIXME: accomodate non-breaking, non-collapsing white-space. */
                                 const struct man *);  /* FIXME: accomodate non-breaking, collapsing white-space. */
 extern  int               mdoc_run(struct termp *,  
                                 const struct mdoc *);  
   
 static  struct termp     *term_alloc(enum termenc);  static  struct termp     *term_alloc(enum termenc);
 static  void              term_free(struct termp *);  static  void              term_free(struct termp *);
Line 39  static void    do_reserved(struct termp *,
Line 40  static void    do_reserved(struct termp *,
                                 const char *, size_t);                                  const char *, size_t);
 static  void              buffer(struct termp *, char);  static  void              buffer(struct termp *, char);
 static  void              encode(struct termp *, char);  static  void              encode(struct termp *, char);
 static  int               isopendelim(const char *);  
 static  int               isclosedelim(const char *);  
   
   
 void *  void *
Line 51  ascii_alloc(void)
Line 50  ascii_alloc(void)
 }  }
   
   
 int  
 terminal_man(void *arg, const struct man *man)  
 {  
         struct termp    *p;  
   
         p = (struct termp *)arg;  
         if (NULL == p->symtab)  
                 p->symtab = term_ascii2htab();  
   
         return(man_run(p, man));  
 }  
   
   
 int  
 terminal_mdoc(void *arg, const struct mdoc *mdoc)  
 {  
         struct termp    *p;  
   
         p = (struct termp *)arg;  
         if (NULL == p->symtab)  
                 p->symtab = term_ascii2htab();  
   
         return(mdoc_run(p, mdoc));  
 }  
   
   
 void  void
 terminal_free(void *arg)  terminal_free(void *arg)
 {  {
Line 91  term_free(struct termp *p)
Line 64  term_free(struct termp *p)
   
         if (p->buf)          if (p->buf)
                 free(p->buf);                  free(p->buf);
         if (TERMENC_ASCII == p->enc && p->symtab)          if (p->symtab)
                 term_asciifree(p->symtab);                  chars_free(p->symtab);
   
         free(p);          free(p);
 }  }
Line 104  term_alloc(enum termenc enc)
Line 77  term_alloc(enum termenc enc)
         struct termp *p;          struct termp *p;
   
         if (NULL == (p = malloc(sizeof(struct termp))))          if (NULL == (p = malloc(sizeof(struct termp))))
                 err(1, "malloc");                  return(NULL);
         bzero(p, sizeof(struct termp));          bzero(p, sizeof(struct termp));
         p->maxrmargin = 78;          p->maxrmargin = 78;
         p->enc = enc;          p->enc = enc;
Line 112  term_alloc(enum termenc enc)
Line 85  term_alloc(enum termenc enc)
 }  }
   
   
 static int  
 isclosedelim(const char *p)  
 {  
   
         if ( ! (*p && 0 == *(p + 1)))  
                 return(0);  
   
         switch (*p) {  
         case('.'):  
                 /* FALLTHROUGH */  
         case(','):  
                 /* FALLTHROUGH */  
         case(';'):  
                 /* FALLTHROUGH */  
         case(':'):  
                 /* FALLTHROUGH */  
         case('?'):  
                 /* FALLTHROUGH */  
         case('!'):  
                 /* FALLTHROUGH */  
         case(')'):  
                 /* FALLTHROUGH */  
         case(']'):  
                 /* FALLTHROUGH */  
         case('}'):  
                 return(1);  
         default:  
                 break;  
         }  
   
         return(0);  
 }  
   
   
 static int  
 isopendelim(const char *p)  
 {  
   
         if ( ! (*p && 0 == *(p + 1)))  
                 return(0);  
   
         switch (*p) {  
         case('('):  
                 /* FALLTHROUGH */  
         case('['):  
                 /* FALLTHROUGH */  
         case('{'):  
                 return(1);  
         default:  
                 break;  
         }  
   
         return(0);  
 }  
   
   
 /*  /*
  * Flush a line of text.  A "line" is loosely defined as being something   * Flush a line of text.  A "line" is loosely defined as being something
  * that should be followed by a newline, regardless of whether it's   * that should be followed by a newline, regardless of whether it's
Line 233  term_flushln(struct termp *p)
Line 150  term_flushln(struct termp *p)
   
         bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;          bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
         vis = 0;          vis = 0;
         overstep = 0;  
   
         /*          /*
          * If in the standard case (left-justified), then begin with our           * If in the standard case (left-justified), then begin with our
Line 287  term_flushln(struct termp *p)
Line 203  term_flushln(struct termp *p)
                                         putchar(' ');                                          putchar(' ');
                                 vis = 0;                                  vis = 0;
                         }                          }
                           /* Remove the overstep width. */
                           bp += overstep;
                           overstep = 0;
                 } else {                  } else {
                         for (j = 0; j < (int)vbl; j++)                          for (j = 0; j < (int)vbl; j++)
                                 putchar(' ');                                  putchar(' ');
Line 303  term_flushln(struct termp *p)
Line 222  term_flushln(struct termp *p)
                 }                  }
                 vis += vsz;                  vis += vsz;
         }          }
   
         p->col = 0;          p->col = 0;
           overstep = 0;
   
         if ( ! (TERMP_NOBREAK & p->flags)) {          if ( ! (TERMP_NOBREAK & p->flags)) {
                 putchar('\n');                  putchar('\n');
Line 389  do_special(struct termp *p, const char *word, size_t l
Line 310  do_special(struct termp *p, const char *word, size_t l
         size_t           sz;          size_t           sz;
         int              i;          int              i;
   
         rhs = term_a2ascii(p->symtab, word, len, &sz);          rhs = chars_a2ascii(p->symtab, word, len, &sz);
   
         if (NULL == rhs)          if (NULL == rhs) {
   #if 0
                   fputs("Unknown special character: ", stderr);
                   for (i = 0; i < (int)len; i++)
                           fputc(word[i], stderr);
                   fputc('\n', stderr);
   #endif
                 return;                  return;
           }
         for (i = 0; i < (int)sz; i++)          for (i = 0; i < (int)sz; i++)
                 encode(p, rhs[i]);                  encode(p, rhs[i]);
 }  }
Line 405  do_reserved(struct termp *p, const char *word, size_t 
Line 333  do_reserved(struct termp *p, const char *word, size_t 
         size_t           sz;          size_t           sz;
         int              i;          int              i;
   
         rhs = term_a2res(p->symtab, word, len, &sz);          rhs = chars_a2res(p->symtab, word, len, &sz);
   
         if (NULL == rhs)          if (NULL == rhs) {
   #if 0
                   fputs("Unknown reserved word: ", stderr);
                   for (i = 0; i < (int)len; i++)
                           fputc(word[i], stderr);
                   fputc('\n', stderr);
   #endif
                 return;                  return;
           }
         for (i = 0; i < (int)sz; i++)          for (i = 0; i < (int)sz; i++)
                 encode(p, rhs[i]);                  encode(p, rhs[i]);
 }  }
Line 422  do_reserved(struct termp *p, const char *word, size_t 
Line 357  do_reserved(struct termp *p, const char *word, size_t 
 static void  static void
 do_escaped(struct termp *p, const char **word)  do_escaped(struct termp *p, const char **word)
 {  {
         int              j;          int              j, type;
         const char      *wp;          const char      *wp;
   
         wp = *word;          wp = *word;
           type = 1;
   
         if (0 == *(++wp)) {          if (0 == *(++wp)) {
                 *word = wp;                  *word = wp;
Line 461  do_escaped(struct termp *p, const char **word)
Line 397  do_escaped(struct termp *p, const char **word)
                         *word = ++wp;                          *word = ++wp;
                         return;                          return;
                 case ('['):                  case ('['):
                           type = 0;
                         break;                          break;
                 default:                  default:
                         do_reserved(p, wp, 1);                          do_reserved(p, wp, 1);
Line 476  do_escaped(struct termp *p, const char **word)
Line 413  do_escaped(struct termp *p, const char **word)
   
                 switch (*wp) {                  switch (*wp) {
                 case ('B'):                  case ('B'):
                         p->flags |= TERMP_BOLD;                          p->bold++;
                         break;                          break;
                 case ('I'):                  case ('I'):
                         p->flags |= TERMP_UNDER;                          p->under++;
                         break;                          break;
                 case ('P'):                  case ('P'):
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case ('R'):                  case ('R'):
                         p->flags &= ~TERMP_STYLE;                          p->bold = p->under = 0;
                         break;                          break;
                 default:                  default:
                         break;                          break;
Line 508  do_escaped(struct termp *p, const char **word)
Line 445  do_escaped(struct termp *p, const char **word)
                 return;                  return;
         }          }
   
         do_special(p, wp - j, (size_t)j);          if (type)
                   do_special(p, wp - j, (size_t)j);
           else
                   do_reserved(p, wp - j, (size_t)j);
         *word = wp;          *word = wp;
 }  }
   
Line 523  term_word(struct termp *p, const char *word)
Line 463  term_word(struct termp *p, const char *word)
 {  {
         const char       *sv;          const char       *sv;
   
         if (isclosedelim(word))          sv = word;
                 if ( ! (TERMP_IGNDELIM & p->flags))  
                         p->flags |= TERMP_NOSPACE;  
   
           if (word[0] && 0 == word[1])
                   switch (word[0]) {
                   case('.'):
                           /* FALLTHROUGH */
                   case(','):
                           /* FALLTHROUGH */
                   case(';'):
                           /* FALLTHROUGH */
                   case(':'):
                           /* FALLTHROUGH */
                   case('?'):
                           /* FALLTHROUGH */
                   case('!'):
                           /* FALLTHROUGH */
                   case(')'):
                           /* FALLTHROUGH */
                   case(']'):
                           /* FALLTHROUGH */
                   case('}'):
                           if ( ! (TERMP_IGNDELIM & p->flags))
                                   p->flags |= TERMP_NOSPACE;
                           break;
                   default:
                           break;
                   }
   
         if ( ! (TERMP_NOSPACE & p->flags))          if ( ! (TERMP_NOSPACE & p->flags))
                 buffer(p, ' ');                  buffer(p, ' ');
   
         if ( ! (p->flags & TERMP_NONOSPACE))          if ( ! (p->flags & TERMP_NONOSPACE))
                 p->flags &= ~TERMP_NOSPACE;                  p->flags &= ~TERMP_NOSPACE;
   
         for (sv = word; *word; word++)          for ( ; *word; word++)
                 if ('\\' != *word)                  if ('\\' != *word)
                         encode(p, *word);                          encode(p, *word);
                 else                  else
                         do_escaped(p, &word);                          do_escaped(p, &word);
   
         if (isopendelim(sv))          if (sv[0] && 0 == sv[1])
                 p->flags |= TERMP_NOSPACE;                  switch (sv[0]) {
                   case('('):
                           /* FALLTHROUGH */
                   case('['):
                           /* FALLTHROUGH */
                   case('{'):
                           p->flags |= TERMP_NOSPACE;
                           break;
                   default:
                           break;
                   }
 }  }
   
   
Line 560  buffer(struct termp *p, char c)
Line 534  buffer(struct termp *p, char c)
                 s = p->maxcols * 2;                  s = 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"); /* FIXME: shouldn't be here! */
                 p->maxcols = s;                  p->maxcols = s;
         }          }
         p->buf[(int)(p->col)++] = c;          p->buf[(int)(p->col)++] = c;
Line 571  static void
Line 545  static void
 encode(struct termp *p, char c)  encode(struct termp *p, char c)
 {  {
   
         if (' ' != c && TERMP_STYLE & p->flags) {          if (' ' != c) {
                 if (TERMP_BOLD & p->flags) {                  if (p->under) {
                         buffer(p, c);                          buffer(p, '_');
                         buffer(p, 8);                          buffer(p, 8);
                 }                  }
                 if (TERMP_UNDER & p->flags) {                  if (p->bold) {
                         buffer(p, '_');                          buffer(p, c);
                         buffer(p, 8);                          buffer(p, 8);
                 }                  }
         }          }
         buffer(p, c);          buffer(p, c);
 }  }
   
   
   size_t
   term_vspan(const struct roffsu *su)
   {
           double           r;
   
           switch (su->unit) {
           case (SCALE_CM):
                   r = su->scale * 2;
                   break;
           case (SCALE_IN):
                   r = su->scale * 6;
                   break;
           case (SCALE_PC):
                   r = su->scale;
                   break;
           case (SCALE_PT):
                   r = su->scale / 8;
                   break;
           case (SCALE_MM):
                   r = su->scale / 1000;
                   break;
           case (SCALE_VS):
                   r = su->scale;
                   break;
           default:
                   r = su->scale - 1;
                   break;
           }
   
           if (r < 0.0)
                   r = 0.0;
           return(/* LINTED */(size_t)
                           r);
   }
   
   
   size_t
   term_hspan(const struct roffsu *su)
   {
           double           r;
   
           /* XXX: CM, IN, and PT are approximations. */
   
           switch (su->unit) {
           case (SCALE_CM):
                   r = 4 * su->scale;
                   break;
           case (SCALE_IN):
                   /* XXX: this is an approximation. */
                   r = 10 * su->scale;
                   break;
           case (SCALE_PC):
                   r = (10 * su->scale) / 6;
                   break;
           case (SCALE_PT):
                   r = (10 * su->scale) / 72;
                   break;
           case (SCALE_MM):
                   r = su->scale / 1000; /* FIXME: double-check. */
                   break;
           case (SCALE_VS):
                   r = su->scale * 2 - 1; /* FIXME: double-check. */
                   break;
           default:
                   r = su->scale;
                   break;
           }
   
           if (r < 0.0)
                   r = 0.0;
           return((size_t)/* LINTED */
                           r);
   }
   
   

Legend:
Removed from v.1.95  
changed lines
  Added in v.1.111

CVSweb