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

Diff for /mandoc/man.c between version 1.101 and 1.109

version 1.101, 2011/02/09 09:18:15 version 1.109, 2011/07/21 10:24:35
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
  *   *
  * 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 26 
Line 26 
 #include <stdio.h>  #include <stdio.h>
 #include <string.h>  #include <string.h>
   
   #include "man.h"
 #include "mandoc.h"  #include "mandoc.h"
 #include "libman.h"  #include "libman.h"
 #include "libmandoc.h"  #include "libmandoc.h"
Line 95  man_free(struct man *man)
Line 96  man_free(struct man *man)
   
   
 struct man *  struct man *
 man_alloc(struct regset *regs, void *data, mandocmsg msg)  man_alloc(struct roff *roff, struct mparse *parse)
 {  {
         struct man      *p;          struct man      *p;
   
         p = mandoc_calloc(1, sizeof(struct man));          p = mandoc_calloc(1, sizeof(struct man));
   
         man_hash_init();          man_hash_init();
         p->data = data;          p->parse = parse;
         p->msg = msg;          p->roff = roff;
         p->regs = regs;  
   
         man_alloc1(p);          man_alloc1(p);
         return(p);          return(p);
Line 130  man_parseln(struct man *m, int ln, char *buf, int offs
Line 130  man_parseln(struct man *m, int ln, char *buf, int offs
         m->flags |= MAN_NEWLINE;          m->flags |= MAN_NEWLINE;
   
         assert( ! (MAN_HALT & m->flags));          assert( ! (MAN_HALT & m->flags));
         return(('.' == buf[offs] || '\'' == buf[offs]) ?  
           return (mandoc_getcontrol(buf, &offs) ?
                         man_pmacro(m, ln, buf, offs) :                          man_pmacro(m, ln, buf, offs) :
                         man_ptext(m, ln, buf, offs));                          man_ptext(m, ln, buf, offs));
 }  }
Line 146  man_free1(struct man *man)
Line 147  man_free1(struct man *man)
                 free(man->meta.title);                  free(man->meta.title);
         if (man->meta.source)          if (man->meta.source)
                 free(man->meta.source);                  free(man->meta.source);
         if (man->meta.rawdate)          if (man->meta.date)
                 free(man->meta.rawdate);                  free(man->meta.date);
         if (man->meta.vol)          if (man->meta.vol)
                 free(man->meta.vol);                  free(man->meta.vol);
         if (man->meta.msec)          if (man->meta.msec)
Line 203  man_node_append(struct man *man, struct man_node *p)
Line 204  man_node_append(struct man *man, struct man_node *p)
                 assert(MAN_BLOCK == p->parent->type);                  assert(MAN_BLOCK == p->parent->type);
                 p->parent->head = p;                  p->parent->head = p;
                 break;                  break;
           case (MAN_TAIL):
                   assert(MAN_BLOCK == p->parent->type);
                   p->parent->tail = p;
                   break;
         case (MAN_BODY):          case (MAN_BODY):
                 assert(MAN_BLOCK == p->parent->type);                  assert(MAN_BLOCK == p->parent->type);
                 p->parent->body = p;                  p->parent->body = p;
Line 261  man_elem_alloc(struct man *m, int line, int pos, enum 
Line 266  man_elem_alloc(struct man *m, int line, int pos, enum 
   
   
 int  int
   man_tail_alloc(struct man *m, int line, int pos, enum mant tok)
   {
           struct man_node *p;
   
           p = man_node_alloc(m, line, pos, MAN_TAIL, tok);
           if ( ! man_node_append(m, p))
                   return(0);
           m->next = MAN_NEXT_CHILD;
           return(1);
   }
   
   
   int
 man_head_alloc(struct man *m, int line, int pos, enum mant tok)  man_head_alloc(struct man *m, int line, int pos, enum mant tok)
 {  {
         struct man_node *p;          struct man_node *p;
Line 353  man_addeqn(struct man *m, const struct eqn *ep)
Line 371  man_addeqn(struct man *m, const struct eqn *ep)
   
         assert( ! (MAN_HALT & m->flags));          assert( ! (MAN_HALT & m->flags));
   
         n = man_node_alloc(m, ep->line, ep->pos, MAN_EQN, MAN_MAX);          n = man_node_alloc(m, ep->ln, ep->pos, MAN_EQN, MAN_MAX);
         n->eqn = ep;          n->eqn = ep;
   
         if ( ! man_node_append(m, n))          if ( ! man_node_append(m, n))
                 return(0);                  return(0);
   
         m->next = MAN_NEXT_SIBLING;          m->next = MAN_NEXT_SIBLING;
         return(man_descope(m, ep->line, ep->pos));          return(man_descope(m, ep->ln, ep->pos));
 }  }
   
 int  int
Line 404  man_descope(struct man *m, int line, int offs)
Line 422  man_descope(struct man *m, int line, int offs)
         return(man_body_alloc(m, line, offs, m->last->tok));          return(man_body_alloc(m, line, offs, m->last->tok));
 }  }
   
   
 static int  static int
 man_ptext(struct man *m, int line, char *buf, int offs)  man_ptext(struct man *m, int line, char *buf, int offs)
 {  {
         int              i;          int              i;
   
         /* Ignore bogus comments. */  
   
         if ('\\' == buf[offs] &&  
                         '.' == buf[offs + 1] &&  
                         '"' == buf[offs + 2]) {  
                 man_pmsg(m, line, offs, MANDOCERR_BADCOMMENT);  
                 return(1);  
         }  
   
         /* Literal free-form text whitespace is preserved. */          /* Literal free-form text whitespace is preserved. */
   
         if (MAN_LITERAL & m->flags) {          if (MAN_LITERAL & m->flags) {
Line 476  man_ptext(struct man *m, int line, char *buf, int offs
Line 484  man_ptext(struct man *m, int line, char *buf, int offs
         return(man_descope(m, line, offs));          return(man_descope(m, line, offs));
 }  }
   
   
 static int  static int
 man_pmacro(struct man *m, int ln, char *buf, int offs)  man_pmacro(struct man *m, int ln, char *buf, int offs)
 {  {
         int              i, j, ppos;          int              i, ppos;
         enum mant        tok;          enum mant        tok;
         char             mac[5];          char             mac[5];
         struct man_node *n;          struct man_node *n;
   
         /* Comments and empties are quickly ignored. */          if ('"' == buf[offs]) {
                   man_pmsg(m, ln, offs, MANDOCERR_BADCOMMENT);
         offs++;  
   
         if ('\0' == buf[offs])  
                 return(1);                  return(1);
           } else if ('\0' == buf[offs])
                   return(1);
   
         i = offs;          ppos = offs;
   
         /*          /*
          * Skip whitespace between the control character and initial  
          * text.  "Whitespace" is both spaces and tabs.  
          */  
   
         if (' ' == buf[i] || '\t' == buf[i]) {  
                 i++;  
                 while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))  
                         i++;  
                 if ('\0' == buf[i])  
                         goto out;  
         }  
   
         ppos = i;  
   
         /*  
          * Copy the first word into a nil-terminated buffer.           * Copy the first word into a nil-terminated buffer.
          * Stop copying when a tab, space, or eoln is encountered.           * Stop copying when a tab, space, or eoln is encountered.
          */           */
   
         j = 0;          i = 0;
         while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])          while (i < 4 && '\0' != buf[offs] &&
                 mac[j++] = buf[i++];                          ' ' != buf[offs] && '\t' != buf[offs])
         mac[j] = '\0';                  mac[i++] = buf[offs++];
   
         tok = (j > 0 && j < 4) ? man_hash_find(mac) : MAN_MAX;          mac[i] = '\0';
   
           tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
   
         if (MAN_MAX == tok) {          if (MAN_MAX == tok) {
                 man_vmsg(m, MANDOCERR_MACRO, ln, ppos, "%s", buf + ppos - 1);                  mandoc_vmsg(MANDOCERR_MACRO, m->parse, ln,
                                   ppos, "%s", buf + ppos - 1);
                 return(1);                  return(1);
         }          }
   
         /* The macro is sane.  Jump to the next word. */          /* The macro is sane.  Jump to the next word. */
   
         while (buf[i] && ' ' == buf[i])          while (buf[offs] && ' ' == buf[offs])
                 i++;                  offs++;
   
         /*          /*
          * Trailing whitespace.  Note that tabs are allowed to be passed           * Trailing whitespace.  Note that tabs are allowed to be passed
          * into the parser as "text", so we only warn about spaces here.           * into the parser as "text", so we only warn about spaces here.
          */           */
   
         if ('\0' == buf[i] && ' ' == buf[i - 1])          if ('\0' == buf[offs] && ' ' == buf[offs - 1])
                 man_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE);                  man_pmsg(m, ln, offs - 1, MANDOCERR_EOLNSPACE);
   
         /*          /*
          * Remove prior ELINE macro, as it's being clobbered by a new           * Remove prior ELINE macro, as it's being clobbered by a new
Line 554  man_pmacro(struct man *m, int ln, char *buf, int offs)
Line 549  man_pmacro(struct man *m, int ln, char *buf, int offs)
                 if (MAN_NSCOPED & man_macros[n->tok].flags)                  if (MAN_NSCOPED & man_macros[n->tok].flags)
                         n = n->parent;                          n = n->parent;
   
                 man_vmsg(m, MANDOCERR_LINESCOPE, n->line, n->pos,                  mandoc_vmsg(MANDOCERR_LINESCOPE, m->parse, n->line,
                                 "%s", man_macronames[n->tok]);                                  n->pos, "%s", man_macronames[n->tok]);
   
                 man_node_delete(m, n);                  man_node_delete(m, n);
                 m->flags &= ~MAN_ELINE;                  m->flags &= ~MAN_ELINE;
Line 573  man_pmacro(struct man *m, int ln, char *buf, int offs)
Line 568  man_pmacro(struct man *m, int ln, char *buf, int offs)
         /* Call to handler... */          /* Call to handler... */
   
         assert(man_macros[tok].fp);          assert(man_macros[tok].fp);
         if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))          if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &offs, buf))
                 goto err;                  goto err;
   
 out:  
         /*          /*
          * We weren't in a block-line scope when entering the           * We weren't in a block-line scope when entering the
          * above-parsed macro, so return.           * above-parsed macro, so return.
Line 613  out:
Line 607  out:
   
         if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))          if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
                 return(0);                  return(0);
         return(man_body_alloc(m, ln, offs, m->last->tok));          return(man_body_alloc(m, ln, ppos, m->last->tok));
   
 err:    /* Error out. */  err:    /* Error out. */
   
         m->flags |= MAN_HALT;          m->flags |= MAN_HALT;
         return(0);          return(0);
 }  }
   
   
 int  
 man_vmsg(struct man *man, enum mandocerr t,  
                 int ln, int pos, const char *fmt, ...)  
 {  
         char             buf[256];  
         va_list          ap;  
   
         va_start(ap, fmt);  
         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);  
         va_end(ap);  
         return((*man->msg)(t, man->data, ln, pos, buf));  
 }  
   
   
 /*  /*
  * Unlink a node from its context.  If "m" is provided, the last parse   * Unlink a node from its context.  If "m" is provided, the last parse

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

CVSweb