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

Diff for /mandoc/term.c between version 1.1 and 1.5

version 1.1, 2009/02/20 11:04:23 version 1.5, 2009/02/21 19:05:28
Line 1 
Line 1 
 /* $Id$ */  /* $Id$ */
 /*  /*
  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>   * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
  *   *
  * 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 <assert.h>  #include <assert.h>
 #include <curses.h>  #include <ctype.h>
 #include <err.h>  #include <err.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <stdio.h>  #include <stdio.h>
 #include <string.h>  #include <string.h>
 #include <term.h>  
 #include <unistd.h>  #include <unistd.h>
   
 #include "mdoc.h"  #include "term.h"
   #include "private.h" /* XXX */
   
   enum    termstyle {
           STYLE_CLEAR,
           STYLE_BOLD,
           STYLE_UNDERLINE
   };
   
 static  int              termprint_r(size_t, size_t,  static  void              termprint_r(struct termp *,
                                   const struct mdoc_meta *,
                                 const struct mdoc_node *);                                  const struct mdoc_node *);
 static  void             termprint_head(size_t,  static  void              termprint_header(struct termp *,
                                 const struct mdoc_meta *);                                  const struct mdoc_meta *);
 static  void             termprint_tail(size_t,  static  void              termprint_footer(struct termp *,
                                 const struct mdoc_meta *);                                  const struct mdoc_meta *);
   
 static  char            *arch2a(enum mdoc_arch);  static  void              pword(struct termp *, const char *, size_t);
 static  char            *vol2a(enum mdoc_vol);  static  void              chara(struct termp *, char);
 static  char            *msec2a(enum mdoc_msec);  static  void              escape(struct termp *, enum termstyle);
   
 static  size_t           ttitle2a(char *, enum mdoc_vol, enum mdoc_msec,  
                                 enum mdoc_arch, size_t);  
   
   void
 static char *  flushln(struct termp *p)
 arch2a(enum mdoc_arch arch)  
 {  {
           size_t           i, j, vsz, vis, maxvis;
   
         switch (arch) {          /*
         case (ARCH_alpha):           * First, establish the maximum columns of "visible" content.
                 return("Alpha");           * This is usually the difference between the right-margin and
         case (ARCH_amd64):           * an indentation, but can be, for tagged lists or columns, a
                 return("AMD64");           * small set of values.
         case (ARCH_amiga):           */
                 return("Amiga");  
         case (ARCH_arc):          assert(p->offset < p->rmargin);
                 return("ARC");          maxvis = p->rmargin - p->offset;
         case (ARCH_arm):          vis = 0;
                 return("ARM");  
         case (ARCH_armish):          /*
                 return("ARMISH");           * If in the standard case (left-justified), then begin with our
         case (ARCH_aviion):           * indentation, otherwise (columns, etc.) just start spitting
                 return("AViion");           * out text.
         case (ARCH_hp300):           */
                 return("HP300");  
         case (ARCH_hppa):          if ( ! (p->flags & TERMP_NOLPAD))
                 return("HPPA");                  /* LINTED */
         case (ARCH_hppa64):                  for (j = 0; j < p->offset; j++)
                 return("HPPA64");                          putchar(' ');
         case (ARCH_i386):  
                 return("i386");          for (i = 0; i < p->col; i++) {
         case (ARCH_landisk):                  /*
                 return("LANDISK");                   * Count up visible word characters.  Control sequences
         case (ARCH_luna88k):                   * (starting with the CSI) aren't counted.
                 return("Luna88k");                   */
         case (ARCH_mac68k):                  assert( ! isspace(p->buf[i]));
                 return("Mac68k");  
         case (ARCH_macppc):                  /* LINTED */
                 return("MacPPC");                  for (j = i, vsz = 0; j < p->col; j++) {
         case (ARCH_mvme68k):                          if (isspace(p->buf[j]))
                 return("MVME68k");                                  break;
         case (ARCH_mvme88k):                          else if (27 == p->buf[j]) {
                 return("MVME88k");                                  assert(j + 4 <= p->col);
         case (ARCH_mvmeppc):                                  j += 3;
                 return("MVMEPPC");                          } else
         case (ARCH_pmax):                                  vsz++;
                 return("PMAX");                  }
         case (ARCH_sgi):                  assert(vsz > 0);
                 return("SGI");  
         case (ARCH_socppc):                  /*
                 return("SOCPPC");                   * If a word is too long and we're within a line, put it
         case (ARCH_sparc):                   * on the next line.  Puke if we're being asked to write
                 return("SPARC");                   * something that will exceed the right margin (i.e.,
         case (ARCH_sparc64):                   * from a fresh line or when we're not allowed to break
                 return("SPARC64");                   * the line with TERMP_NOBREAK).
         case (ARCH_sun3):                   */
                 return("Sun3");  
         case (ARCH_vax):                  if (vis && vis + vsz >= maxvis) {
                 return("VAX");                          /* FIXME */
         case (ARCH_zaurus):                          if (p->flags & TERMP_NOBREAK)
                 return("Zaurus");                                  errx(1, "word breaks right margin");
         default:                          putchar('\n');
                 break;                          for (j = 0; j < p->offset; j++)
                                   putchar(' ');
                           vis = 0;
                   } else if (vis + vsz >= maxvis) {
                           /* FIXME */
                           errx(1, "word breaks right margin");
                   }
   
                   /*
                    * Write out the word and a trailing space.  Omit the
                    * space if we're the last word in the line.
                    */
   
                   for ( ; i < p->col; i++) {
                           if (isspace(p->buf[i]))
                                   break;
                           putchar(p->buf[i]);
                   }
                   vis += vsz;
                   if (i < p->col) {
                           putchar(' ');
                           vis++;
                   }
         }          }
   
         return(NULL);          /*
            * If we're not to right-marginalise it (newline), then instead
            * pad to the right margin and stay off.
            */
   
           if (p->flags & TERMP_NOBREAK) {
                   for ( ; vis <= maxvis; vis++)
                           putchar(' ');
           } else
                   putchar('\n');
   
           p->col = 0;
 }  }
   
   
 static char *  void
 vol2a(enum mdoc_vol vol)  newln(struct termp *p)
 {  {
   
         switch (vol) {          /*
         case (VOL_AMD):           * A newline only breaks an existing line; it won't assert
                 return("OpenBSD Ancestral Manual Documents");           * vertical space.
         case (VOL_IND):           */
                 return("OpenBSD Manual Master Index");          p->flags |= TERMP_NOSPACE;
         case (VOL_KM):          if (0 == p->col)
                 return("OpenBSD Kernel Manual");                  return;
         case (VOL_LOCAL):          flushln(p);
                 return("OpenBSD Local Manual");  }
         case (VOL_PRM):  
                 return("OpenBSD Programmer's Manual");  
         case (VOL_PS1):  
                 return("OpenBSD Programmer's Supplementary Documents");  
         case (VOL_SMM):  
                 return("OpenBSD System Manager's Manual");  
         case (VOL_URM):  
                 return("OpenBSD Reference Manual");  
         case (VOL_USD):  
                 return("OpenBSD User's Supplementary Documents");  
         default:  
                 break;  
         }  
   
         return(NULL);  
   void
   vspace(struct termp *p)
   {
   
           /*
            * Asserts a vertical space (a full, empty line-break between
            * lines).
            */
           newln(p);
           putchar('\n');
 }  }
   
   
 static char *  static void
 msec2a(enum mdoc_msec msec)  chara(struct termp *p, char c)
 {  {
   
         switch (msec) {          /* TODO: dynamically expand the buffer. */
         case(MSEC_1):          if (p->col + 1 >= p->maxcols)
                 return("1");                  errx(1, "line overrun");
         case(MSEC_2):          p->buf[(p->col)++] = c;
                 return("2");  }
         case(MSEC_3):  
                 return("3");  
         case(MSEC_3f):  static void
                 return("3f");  escape(struct termp *p, enum termstyle esc)
         case(MSEC_3p):  {
                 return("3p");  
         case(MSEC_4):          if (p->col + 4 >= p->maxcols)
                 return("4");                  errx(1, "line overrun");
         case(MSEC_5):  
                 return("5");          p->buf[(p->col)++] = 27;
         case(MSEC_6):          p->buf[(p->col)++] = '[';
                 return("6");          switch (esc) {
         case(MSEC_7):          case (STYLE_CLEAR):
                 return("7");                  p->buf[(p->col)++] = '0';
         case(MSEC_8):  
                 return("8");  
         case(MSEC_9):  
                 return("9");  
         case(MSEC_X11):  
                 return("X11");  
         case(MSEC_X11R6):  
                 return("X11R6");  
         case(MSEC_local):  
                 return("local");  
         case(MSEC_n):  
                 return("n");  
         case(MSEC_unass):  
                 /* FALLTHROUGH */  
         case(MSEC_draft):  
                 return("draft");  
         case(MSEC_paper):  
                 return("paper");  
         default:  
                 break;                  break;
           case (STYLE_BOLD):
                   p->buf[(p->col)++] = '1';
                   break;
           case (STYLE_UNDERLINE):
                   p->buf[(p->col)++] = '4';
                   break;
           default:
                   abort();
                   /* NOTREACHED */
         }          }
         return(NULL);          p->buf[(p->col)++] = 'm';
 }  }
   
   
 static size_t  static void
 ttitle2a(char *dst, enum mdoc_vol vol, enum mdoc_msec msec,  pword(struct termp *p, const char *word, size_t len)
                 enum mdoc_arch arch, size_t sz)  
 {  {
         char            *p;          size_t           i;
         size_t           ssz;  
   
         if (NULL == (p = vol2a(vol)))          assert(len > 0);
                 switch (msec) {  
                 case (MSEC_1):  
                         /* FALLTHROUGH */  
                 case (MSEC_6):  
                         /* FALLTHROUGH */  
                 case (MSEC_7):  
                         p = vol2a(VOL_URM);  
                         break;  
                 case (MSEC_8):  
                         p = vol2a(VOL_SMM);  
                         break;  
                 case (MSEC_2):  
                         /* FALLTHROUGH */  
                 case (MSEC_3):  
                         /* FALLTHROUGH */  
                 case (MSEC_4):  
                         /* FALLTHROUGH */  
                 case (MSEC_5):  
                         p = vol2a(VOL_PRM);  
                         break;  
                 case (MSEC_9):  
                         p = vol2a(VOL_KM);  
                         break;  
                 default:  
                         /* FIXME: capitalise. */  
                         if (NULL == (p = msec2a(msec)))  
                                 p = msec2a(MSEC_local);  
                         break;  
                 }  
         assert(p);  
   
         if ((ssz = strlcpy(dst, p, sz)) >= sz)          if ( ! (p->flags & TERMP_NOSPACE))
                 return(ssz);                  chara(p, ' ');
   
         if ((p = arch2a(arch))) {          p->flags &= ~TERMP_NOSPACE;
                 if ((ssz = strlcat(dst, " (", sz)) >= sz)  
                         return(ssz);  
                 if ((ssz = strlcat(dst, p, sz)) >= sz)  
                         return(ssz);  
                 if ((ssz = strlcat(dst, ")", sz)) >= sz)  
                         return(ssz);  
         }  
   
         return(ssz);          if (p->flags & TERMP_BOLD)
                   escape(p, STYLE_BOLD);
           if (p->flags & TERMP_UNDERLINE)
                   escape(p, STYLE_UNDERLINE);
   
           /* TODO: escape patterns. */
   
           for (i = 0; i < len; i++)
                   chara(p, word[i]);
   
           if (p->flags & TERMP_BOLD ||
                           p->flags & TERMP_UNDERLINE)
                   escape(p, STYLE_CLEAR);
 }  }
   
   
 static int  void
 termprint_r(size_t cols, size_t indent, const struct mdoc_node *node)  word(struct termp *p, const char *word)
 {  {
           size_t           i, j, len;
   
         return(1);          if (mdoc_isdelim(word))
                   p->flags |= TERMP_NOSPACE;
   
           len = strlen(word);
           assert(len > 0);
   
           /* LINTED */
           for (j = i = 0; i < len; i++) {
                   if ( ! isspace(word[i])) {
                           j++;
                           continue;
                   }
                   if (0 == j)
                           continue;
                   assert(i >= j);
                   pword(p, &word[i - j], j);
                   j = 0;
           }
           if (j > 0) {
                   assert(i >= j);
                   pword(p, &word[i - j], j);
           }
 }  }
   
   
 static void  static void
 termprint_tail(size_t cols, const struct mdoc_meta *meta)  termprint_r(struct termp *p, const struct mdoc_meta *meta,
                   const struct mdoc_node *node)
 {  {
           int              dochild;
   
           /* Pre-processing. */
   
           dochild = 1;
   
           if (MDOC_TEXT != node->type) {
                   if (termacts[node->tok].pre)
                           if ( ! (*termacts[node->tok].pre)(p, meta, node))
                                   dochild = 0;
           } else /* MDOC_TEXT == node->type */
                   word(p, node->data.text.string);
   
           /* Children. */
   
           if (dochild && node->child)
                   termprint_r(p, meta, node->child);
   
           /* Post-processing. */
   
           if (MDOC_TEXT != node->type) {
                   if (termacts[node->tok].post)
                           if ( ! (*termacts[node->tok].post)(p, meta, node))
                                   return;
           }
   
           /* Siblings. */
   
           if (node->next)
                   termprint_r(p, meta, node->next);
   }
   
   
   static void
   termprint_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;          size_t           sz, osz, ssz, i;
   
         if (NULL == (buf = malloc(cols)))          if (NULL == (buf = malloc(p->rmargin)))
                 err(1, "malloc");                  err(1, "malloc");
         if (NULL == (os = malloc(cols)))          if (NULL == (os = malloc(p->rmargin)))
                 err(1, "malloc");                  err(1, "malloc");
   
         tm = localtime(&meta->date);          tm = localtime(&meta->date);
         if (NULL == strftime(buf, cols, "%B %d, %Y", tm))          if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))
                 err(1, "strftime");                  err(1, "strftime");
   
         osz = strlcpy(os, meta->os, cols);          osz = strlcpy(os, meta->os, p->rmargin);
   
         sz = strlen(buf);          sz = strlen(buf);
         ssz = sz + osz + 1;          ssz = sz + osz + 1;
   
         if (ssz > cols) {          if (ssz > p->rmargin) {
                 ssz -= cols;                  ssz -= p->rmargin;
                 assert(ssz <= osz);                  assert(ssz <= osz);
                 os[osz - ssz] = 0;                  os[osz - ssz] = 0;
                 ssz = 1;                  ssz = 1;
         } else          } else
                 ssz = cols - ssz + 1;                  ssz = p->rmargin - ssz + 1;
   
           printf("\n");
         printf("%s", os);          printf("%s", os);
         for (i = 0; i < ssz; i++)          for (i = 0; i < ssz; i++)
                 printf(" ");                  printf(" ");
   
         printf("%s\n", buf);          printf("%s\n", buf);
           fflush(stdout);
   
         free(buf);          free(buf);
         free(os);          free(os);
Line 291  termprint_tail(size_t cols, const struct mdoc_meta *me
Line 349  termprint_tail(size_t cols, const struct mdoc_meta *me
   
   
 static void  static void
 termprint_head(size_t cols, const struct mdoc_meta *meta)  termprint_header(struct termp *p, const struct mdoc_meta *meta)
 {  {
         char            *msec, *buf, *title;          char            *msec, *buf, *title, *pp;
         size_t           ssz, tsz, ttsz, i;          size_t           ssz, tsz, ttsz, i;;
   
         if (NULL == (buf = malloc(cols)))          if (NULL == (buf = malloc(p->rmargin)))
                 err(1, "malloc");                  err(1, "malloc");
         if (NULL == (title = malloc(cols)))          if (NULL == (title = malloc(p->rmargin)))
                 err(1, "malloc");                  err(1, "malloc");
   
         /* Format the manual page header. */          if (NULL == (pp = mdoc_vol2a(meta->vol)))
                   switch (meta->msec) {
                   case (MSEC_1):
                           /* FALLTHROUGH */
                   case (MSEC_6):
                           /* FALLTHROUGH */
                   case (MSEC_7):
                           pp = mdoc_vol2a(VOL_URM);
                           break;
                   case (MSEC_8):
                           pp = mdoc_vol2a(VOL_SMM);
                           break;
                   case (MSEC_2):
                           /* FALLTHROUGH */
                   case (MSEC_3):
                           /* FALLTHROUGH */
                   case (MSEC_4):
                           /* FALLTHROUGH */
                   case (MSEC_5):
                           pp = mdoc_vol2a(VOL_PRM);
                           break;
                   case (MSEC_9):
                           pp = mdoc_vol2a(VOL_KM);
                           break;
                   default:
                           /* FIXME: capitalise. */
                           if (NULL == (pp = mdoc_msec2a(meta->msec)))
                                   pp = mdoc_msec2a(MSEC_local);
                           break;
                   }
           assert(pp);
   
         tsz = ttitle2a(buf, meta->vol, meta->msec, meta->arch, cols);          tsz = strlcpy(buf, pp, p->rmargin);
         ttsz = strlcpy(title, meta->title, cols);          assert(tsz < p->rmargin);
   
         if (NULL == (msec = msec2a(meta->msec)))          if ((pp = mdoc_arch2a(meta->arch))) {
                   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);
   
           if (NULL == (msec = mdoc_msec2a(meta->msec)))
                 msec = "";                  msec = "";
   
         ssz = (2 * (ttsz + 2 + strlen(msec))) + tsz + 2;          ssz = (2 * (ttsz + 2 + strlen(msec))) + tsz + 2;
   
         if (ssz > cols) {          if (ssz > p->rmargin) {
                 if ((ssz -= cols) % 2)                  if ((ssz -= p->rmargin) % 2)
                         ssz++;                          ssz++;
                 ssz /= 2;                  ssz /= 2;
   
Line 320  termprint_head(size_t cols, const struct mdoc_meta *me
Line 419  termprint_head(size_t cols, const struct mdoc_meta *me
                 title[ttsz - ssz] = 0;                  title[ttsz - ssz] = 0;
                 ssz = 1;                  ssz = 1;
         } else          } else
                 ssz = ((cols - ssz) / 2) + 1;                  ssz = ((p->rmargin - ssz) / 2) + 1;
   
         printf("%s(%s)", title, msec);          printf("%s(%s)", title, msec);
   
Line 332  termprint_head(size_t cols, const struct mdoc_meta *me
Line 431  termprint_head(size_t cols, const struct mdoc_meta *me
         for (i = 0; i < ssz; i++)          for (i = 0; i < ssz; i++)
                 printf(" ");                  printf(" ");
   
         printf("%s(%s)\n\n", title, msec);          printf("%s(%s)\n", title, msec);
           fflush(stdout);
   
         free(title);          free(title);
         free(buf);          free(buf);
Line 343  int
Line 443  int
 termprint(const struct mdoc_node *node,  termprint(const struct mdoc_node *node,
                 const struct mdoc_meta *meta)                  const struct mdoc_meta *meta)
 {  {
         size_t           cols;          struct termp     p;
   
         if (ERR == setupterm(NULL, STDOUT_FILENO, NULL))          p.maxrmargin = 80; /* XXX */
                 return(0);          p.rmargin = p.maxrmargin;
           p.maxcols = 1024;
           p.offset = p.col = 0;
           p.flags = TERMP_NOSPACE;
   
         cols = columns < 60 ? 60 : (size_t)columns;          if (NULL == (p.buf = malloc(p.maxcols)))
                   err(1, "malloc");
   
         termprint_head(cols, meta);          termprint_header(&p, meta);
         if ( ! termprint_r(cols, 0, node))          termprint_r(&p, meta, node);
                 return(0);          termprint_footer(&p, meta);
         termprint_tail(cols, meta);  
           free(p.buf);
   
         return(1);          return(1);
 }  }
   

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

CVSweb