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

Diff for /mandoc/Attic/mdocterm.c between version 1.5 and 1.15

version 1.5, 2009/02/23 12:45:19 version 1.15, 2009/02/26 17:11:38
Line 1 
Line 1 
         /* $Id$ */  /* $Id$ */
 /*  /*
  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
  *   *
Line 24 
Line 24 
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
 #ifdef __linux__  #ifndef __OpenBSD__
 #include <time.h>  #include <time.h>
 #endif  #endif
   
 #include "mmain.h"  #include "mmain.h"
 #include "term.h"  #include "term.h"
   
   #ifdef __NetBSD__
   #define xisspace(x) isspace((int)(x))
   #else
   #define xisspace(x) isspace((x))
   #endif
   
 enum    termstyle {  enum    termstyle {
         STYLE_CLEAR,          STYLE_CLEAR,
         STYLE_BOLD,          STYLE_BOLD,
Line 38  enum termstyle {
Line 44  enum termstyle {
 };  };
   
 static  void              body(struct termp *,  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 *,
Line 48  static void    footer(struct termp *,
Line 55  static void    footer(struct termp *,
 static  void              pword(struct termp *, const char *, size_t);  static  void              pword(struct termp *, const char *, size_t);
 static  void              pescape(struct termp *,  static  void              pescape(struct termp *,
                                 const char *, size_t *, size_t);                                  const char *, size_t *, size_t);
   static  void              nescape(struct termp *,
                                   const char *, size_t);
 static  void              chara(struct termp *, char);  static  void              chara(struct termp *, char);
 static  void              stringa(struct termp *, const char *);  static  void              stringa(struct termp *, const char *);
 static  void              style(struct termp *, enum termstyle);  static  void              style(struct termp *, enum termstyle);
Line 83  main(int argc, char *argv[])
Line 92  main(int argc, char *argv[])
                 err(1, "malloc");                  err(1, "malloc");
   
         header(&termp, mdoc_meta(mdoc));          header(&termp, mdoc_meta(mdoc));
         body(&termp, mdoc_meta(mdoc), mdoc_node(mdoc));          body(&termp, NULL, mdoc_meta(mdoc), mdoc_node(mdoc));
         footer(&termp, mdoc_meta(mdoc));          footer(&termp, mdoc_meta(mdoc));
   
         free(termp.buf);          free(termp.buf);
Line 137  flushln(struct termp *p)
Line 146  flushln(struct termp *p)
                  * Count up visible word characters.  Control sequences                   * Count up visible word characters.  Control sequences
                  * (starting with the CSI) aren't counted.                   * (starting with the CSI) aren't counted.
                  */                   */
                 assert( ! isspace(p->buf[i]));                  assert( ! xisspace(p->buf[i]));
   
                 /* LINTED */                  /* LINTED */
                 for (j = i, vsz = 0; j < p->col; j++) {                  for (j = i, vsz = 0; j < p->col; j++) {
                         if (isspace(p->buf[j]))                          if (xisspace(p->buf[j]))
                                 break;                                  break;
                         else if (27 == p->buf[j]) {                          else if (27 == p->buf[j]) {
                                 assert(j + 4 <= p->col);                                  assert(j + 4 <= p->col);
Line 159  flushln(struct termp *p)
Line 168  flushln(struct termp *p)
                  * the line with TERMP_NOBREAK).                   * the line with TERMP_NOBREAK).
                  */                   */
   
                 if (vis && vis + vsz >= maxvis) {                  if (vis && vis + vsz > maxvis) {
                         /* FIXME */                          /* FIXME */
                         if (p->flags & TERMP_NOBREAK)                          if (p->flags & TERMP_NOBREAK)
                                 errx(1, "word breaks right margin");                                  errx(1, "word breaks right margin");
Line 167  flushln(struct termp *p)
Line 176  flushln(struct termp *p)
                         for (j = 0; j < p->offset; j++)                          for (j = 0; j < p->offset; j++)
                                 putchar(' ');                                  putchar(' ');
                         vis = 0;                          vis = 0;
                 } else if (vis + vsz >= maxvis) {                  } else if (vis + vsz > maxvis)
                         /* FIXME */                          /* FIXME */
                         errx(1, "word breaks right margin");                          errx(1, "word breaks right margin");
                 }  
   
                 /*                  /*
                  * Write out the word and a trailing space.  Omit the                   * Write out the word and a trailing space.  Omit the
Line 178  flushln(struct termp *p)
Line 186  flushln(struct termp *p)
                  */                   */
   
                 for ( ; i < p->col; i++) {                  for ( ; i < p->col; i++) {
                         if (isspace(p->buf[i]))                          if (xisspace(p->buf[i]))
                                 break;                                  break;
                         putchar(p->buf[i]);                          putchar(p->buf[i]);
                 }                  }
Line 195  flushln(struct termp *p)
Line 203  flushln(struct termp *p)
          */           */
   
         if (p->flags & TERMP_NOBREAK) {          if (p->flags & TERMP_NOBREAK) {
                 for ( ; vis <= maxvis; vis++)                  if ( ! (p->flags & TERMP_NORPAD))
                         putchar(' ');                          for ( ; vis < maxvis; vis++)
                                   putchar(' ');
         } else          } else
                 putchar('\n');                  putchar('\n');
   
Line 213  newln(struct termp *p)
Line 222  newln(struct termp *p)
          * vertical space.           * vertical space.
          */           */
         p->flags |= TERMP_NOSPACE;          p->flags |= TERMP_NOSPACE;
         if (0 == p->col)          if (0 == p->col) {
                   p->flags &= ~TERMP_NOLPAD;
                 return;                  return;
           }
         flushln(p);          flushln(p);
           p->flags &= ~TERMP_NOLPAD;
 }  }
   
   
Line 281  style(struct termp *p, enum termstyle esc)
Line 293  style(struct termp *p, enum termstyle esc)
   
   
 static void  static void
   nescape(struct termp *p, const char *word, size_t len)
   {
   
           switch (len) {
           case (2):
                   if ('r' == word[0] && 'B' == word[1])
                           chara(p, ']');
                   else if ('l' == word[0] && 'B' == word[1])
                           chara(p, '[');
                   else if ('<' == word[0] && '-' == word[1])
                           stringa(p, "<-");
                   else if ('-' == word[0] && '>' == word[1])
                           stringa(p, "->");
                   else if ('l' == word[0] && 'q' == word[1])
                           chara(p, '\"');
                   else if ('r' == word[0] && 'q' == word[1])
                           chara(p, '\"');
                   else if ('b' == word[0] && 'u' == word[1])
                           chara(p, 'o');
                   break;
           default:
                   break;
           }
   }
   
   
   static void
 pescape(struct termp *p, const char *word, size_t *i, size_t len)  pescape(struct termp *p, const char *word, size_t *i, size_t len)
 {  {
           size_t           j;
   
         (*i)++;          (*i)++;
         assert(*i < len);          assert(*i < len);
Line 291  pescape(struct termp *p, const char *word, size_t *i, 
Line 331  pescape(struct termp *p, const char *word, size_t *i, 
                 /* Two-character escapes. */                  /* Two-character escapes. */
                 (*i)++;                  (*i)++;
                 assert(*i + 1 < len);                  assert(*i + 1 < len);
                   nescape(p, &word[*i], 2);
                 if ('r' == word[*i] && 'B' == word[*i + 1])  
                         chara(p, ']');  
                 else if ('l' == word[*i] && 'B' == word[*i + 1])  
                         chara(p, '[');  
                 else if ('<' == word[*i] && '-' == word[*i + 1])  
                         stringa(p, "<-");  
                 else if ('-' == word[*i] && '>' == word[*i + 1])  
                         stringa(p, "->");  
   
                 (*i)++;                  (*i)++;
                 return;                  return;
   
Line 315  pescape(struct termp *p, const char *word, size_t *i, 
Line 346  pescape(struct termp *p, const char *word, size_t *i, 
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case ('-'):                  case ('-'):
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                   case (' '):
                           /* FALLTHROUGH */
                 case ('.'):                  case ('.'):
                         chara(p, word[*i]);                          chara(p, word[*i]);
                 default:                  default:
Line 322  pescape(struct termp *p, const char *word, size_t *i, 
Line 355  pescape(struct termp *p, const char *word, size_t *i, 
                 }                  }
                 return;                  return;
         }          }
         /* n-character escapes. */  
           (*i)++;
           for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
                   /* Loop... */ ;
   
           nescape(p, &word[*i - j], j);
 }  }
   
   
Line 337  pword(struct termp *p, const char *word, size_t len)
Line 375  pword(struct termp *p, const char *word, size_t len)
                         ! (p->flags & TERMP_LITERAL))                          ! (p->flags & TERMP_LITERAL))
                 chara(p, ' ');                  chara(p, ' ');
   
         p->flags &= ~TERMP_NOSPACE;          if ( ! (p->flags & TERMP_NONOSPACE))
                   p->flags &= ~TERMP_NOSPACE;
   
         if (p->flags & TERMP_BOLD)          if (p->flags & TERMP_BOLD)
                 style(p, STYLE_BOLD);                  style(p, STYLE_BOLD);
Line 379  word(struct termp *p, const char *word)
Line 418  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(word[i])) {                  if ( ! xisspace(word[i])) {
                         j++;                          j++;
                         continue;                          continue;
                 }                  }
Line 397  word(struct termp *p, const char *word)
Line 436  word(struct termp *p, const char *word)
   
   
 static void  static void
 body(struct termp *p, const struct mdoc_meta *meta,  body(struct termp *p, struct termpair *ppair,
                   const struct mdoc_meta *meta,
                 const struct mdoc_node *node)                  const struct mdoc_node *node)
 {  {
         int              dochild;          int              dochild;
           struct termpair  pair;
   
         /* Pre-processing. */          /* Pre-processing. */
   
         dochild = 1;          dochild = 1;
           pair.ppair = ppair;
           pair.type = 0;
           pair.offset = pair.rmargin = 0;
           pair.flag = 0;
           pair.count = 0;
   
         if (MDOC_TEXT != node->type) {          if (MDOC_TEXT != node->type) {
                 if (termacts[node->tok].pre)                  if (termacts[node->tok].pre)
                         if ( ! (*termacts[node->tok].pre)(p, meta, node))                          if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node))
                                 dochild = 0;                                  dochild = 0;
         } else /* MDOC_TEXT == node->type */          } else /* MDOC_TEXT == node->type */
                 word(p, node->data.text.string);                  word(p, node->data.text.string);
   
         /* Children. */          /* Children. */
   
           if (TERMPAIR_FLAG & pair.type)
                   p->flags |= pair.flag;
   
         if (dochild && node->child)          if (dochild && node->child)
                 body(p, meta, node->child);                  body(p, &pair, meta, node->child);
   
           if (TERMPAIR_FLAG & pair.type)
                   p->flags &= ~pair.flag;
   
         /* Post-processing. */          /* Post-processing. */
   
         if (MDOC_TEXT != node->type)          if (MDOC_TEXT != node->type)
                 if (termacts[node->tok].post)                  if (termacts[node->tok].post)
                         (*termacts[node->tok].post)(p, meta, node);                          (*termacts[node->tok].post)(p, &pair, meta, node);
   
         /* Siblings. */          /* Siblings. */
   
         if (node->next)          if (node->next)
                 body(p, meta, node->next);                  body(p, ppair, meta, node->next);
 }  }
   
   
Line 436  footer(struct termp *p, const struct mdoc_meta *meta)
Line 488  footer(struct termp *p, const struct mdoc_meta *meta)
 {  {
         struct tm       *tm;          struct tm       *tm;
         char            *buf, *os;          char            *buf, *os;
         size_t           sz, osz, ssz, i;  
   
         if (NULL == (buf = malloc(p->rmargin)))          if (NULL == (buf = malloc(p->rmargin)))
                 err(1, "malloc");                  err(1, "malloc");
Line 445  footer(struct termp *p, const struct mdoc_meta *meta)
Line 496  footer(struct termp *p, const struct mdoc_meta *meta)
   
         tm = localtime(&meta->date);          tm = localtime(&meta->date);
   
 #ifdef __linux__  #ifdef __OpenBSD__
         if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm))  
 #else  
         if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))          if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))
   #else
           if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm))
 #endif  #endif
                 err(1, "strftime");                  err(1, "strftime");
   
         osz = strlcpy(os, meta->os, p->rmargin);          (void)strlcpy(os, meta->os, p->rmargin);
   
         sz = strlen(buf);          vspace(p);
         ssz = sz + osz + 1;  
   
         if (ssz > p->rmargin) {          p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
                 ssz -= p->rmargin;          p->rmargin = p->maxrmargin - strlen(buf);
                 assert(ssz <= osz);          p->offset = 0;
                 os[osz - ssz] = 0;  
                 ssz = 1;  
         } else  
                 ssz = p->rmargin - ssz + 1;  
   
         printf("\n");          word(p, os);
         printf("%s", os);          flushln(p);
         for (i = 0; i < ssz; i++)  
                 printf(" ");  
   
         printf("%s\n", buf);          p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
         fflush(stdout);          p->offset = p->rmargin;
           p->rmargin = p->maxrmargin;
           p->flags &= ~TERMP_NOBREAK;
   
           word(p, buf);
           flushln(p);
   
         free(buf);          free(buf);
         free(os);          free(os);
 }  }
Line 482  static void
Line 531  static void
 header(struct termp *p, const struct mdoc_meta *meta)  header(struct termp *p, const struct mdoc_meta *meta)
 {  {
         char            *buf, *title;          char            *buf, *title;
         const char      *pp, *msec;          const char      *pp;
         size_t           ssz, tsz, ttsz, i;;  
   
         if (NULL == (buf = malloc(p->rmargin)))          if (NULL == (buf = malloc(p->rmargin)))
                 err(1, "malloc");                  err(1, "malloc");
Line 520  header(struct termp *p, const struct mdoc_meta *meta)
Line 568  header(struct termp *p, const struct mdoc_meta *meta)
                                 pp = mdoc_msec2a(MSEC_local);                                  pp = mdoc_msec2a(MSEC_local);
                         break;                          break;
                 }                  }
         assert(pp);  
   
         tsz = strlcpy(buf, pp, p->rmargin);          if (mdoc_arch2a(meta->arch))
         assert(tsz < p->rmargin);                  (void)snprintf(buf, p->rmargin, "%s(%s)",
                                   pp, mdoc_arch2a(meta->arch));
           else
                   (void)strlcpy(buf, pp, p->rmargin);
   
         if ((pp = mdoc_arch2a(meta->arch))) {          pp = mdoc_msec2a(meta->msec);
                 tsz = strlcat(buf, " (", p->rmargin);  
                 assert(tsz < p->rmargin);  
                 tsz = strlcat(buf, pp, p->rmargin);  
                 assert(tsz < p->rmargin);  
                 tsz = strlcat(buf, ")", p->rmargin);  
                 assert(tsz < p->rmargin);  
         }  
   
         ttsz = strlcpy(title, meta->title, p->rmargin);          (void)snprintf(title, p->rmargin, "%s(%s)",
                           meta->title, pp ? pp : "");
   
         if (NULL == (msec = mdoc_msec2a(meta->msec)))          p->offset = 0;
                 msec = "";          p->rmargin = (p->maxrmargin - strlen(buf)) / 2;
           p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
   
         ssz = (2 * (ttsz + 2 + strlen(msec))) + tsz + 2;          word(p, title);
           flushln(p);
   
         if (ssz > p->rmargin) {          p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
                 if ((ssz -= p->rmargin) % 2)          p->offset = p->rmargin;
                         ssz++;          p->rmargin = p->maxrmargin - strlen(title);
                 ssz /= 2;  
   
                 assert(ssz <= ttsz);  
                 title[ttsz - ssz] = 0;  
                 ssz = 1;  
         } else  
                 ssz = ((p->rmargin - ssz) / 2) + 1;  
   
         printf("%s(%s)", title, msec);          word(p, buf);
           flushln(p);
   
         for (i = 0; i < ssz; i++)          p->offset = p->rmargin;
                 printf(" ");          p->rmargin = p->maxrmargin;
           p->flags &= ~TERMP_NOBREAK;
           p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
   
         printf("%s", buf);          word(p, title);
           flushln(p);
   
         for (i = 0; i < ssz; i++)          p->rmargin = p->maxrmargin;
                 printf(" ");          p->offset = 0;
           p->flags &= ~TERMP_NOSPACE;
         printf("%s(%s)\n", title, msec);  
         fflush(stdout);  
   
         free(title);          free(title);
         free(buf);          free(buf);

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

CVSweb