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

Diff for /mandoc/roff.c between version 1.45 and 1.51

version 1.45, 2008/12/06 16:50:18 version 1.51, 2008/12/08 12:46:28
Line 57  struct rofftree {
Line 57  struct rofftree {
         char              name[64];             /* `Nm' results. */          char              name[64];             /* `Nm' results. */
         char              os[64];               /* `Os' results. */          char              os[64];               /* `Os' results. */
         char              title[64];            /* `Dt' results. */          char              title[64];            /* `Dt' results. */
         char              section[64];          /* `Dt' results. */          enum roffmsec     section;
         char              volume[64];           /* `Dt' results. */          char              volume[64];           /* `Dt' results. */
         int               state;          int               state;
 #define ROFF_PRELUDE     (1 << 1)               /* In roff prelude. */  #define ROFF_PRELUDE     (1 << 1)               /* In roff prelude. */
Line 80  static int    roffscan(int, const int *);
Line 80  static int    roffscan(int, const int *);
 static  int               rofffindtok(const char *);  static  int               rofffindtok(const char *);
 static  int               rofffindarg(const char *);  static  int               rofffindarg(const char *);
 static  int               rofffindcallable(const char *);  static  int               rofffindcallable(const char *);
   static  int               roffismsec(const char *);
   static  int               roffispunct(const char *);
 static  int               roffargs(const struct rofftree *,  static  int               roffargs(const struct rofftree *,
                                 int, char *, char **);                                  int, char *, char **);
 static  int               roffargok(int, int);  static  int               roffargok(int, int);
Line 88  static int    roffnextopt(const struct rofftree *,
Line 90  static int    roffnextopt(const struct rofftree *,
 static  int               roffparseopts(struct rofftree *, int,  static  int               roffparseopts(struct rofftree *, int,
                                 char ***, int *, char **);                                  char ***, int *, char **);
 static  int               roffcall(struct rofftree *, int, char **);  static  int               roffcall(struct rofftree *, int, char **);
   static  int               roffexit(struct rofftree *, int);
 static  int               roffparse(struct rofftree *, char *);  static  int               roffparse(struct rofftree *, char *);
 static  int               textparse(struct rofftree *, char *);  static  int               textparse(struct rofftree *, char *);
 static  int               roffdata(struct rofftree *, int, char *);  static  int               roffdata(struct rofftree *, int, char *);
 static  int               roffspecial(struct rofftree *, int,  static  int               roffspecial(struct rofftree *, int,
                                 const char *, size_t, char **);                                  const char *, const int *,
                                   const char **, size_t, char **);
 static  int               roffsetname(struct rofftree *, char **);  static  int               roffsetname(struct rofftree *, char **);
   
 #ifdef __linux__  #ifdef __linux__
Line 132  roff_free(struct rofftree *tree, int flush)
Line 136  roff_free(struct rofftree *tree, int flush)
   
         while (tree->last) {          while (tree->last) {
                 t = tree->last->tok;                  t = tree->last->tok;
                 if ( ! (*tokens[t].cb)(t, tree, NULL, ROFF_EXIT))                  if ( ! roffexit(tree, t))
                         goto end;                          goto end;
         }          }
   
Line 165  roff_alloc(const struct roffcb *cb, void *args)
Line 169  roff_alloc(const struct roffcb *cb, void *args)
   
         tree->state = ROFF_PRELUDE;          tree->state = ROFF_PRELUDE;
         tree->arg = args;          tree->arg = args;
           tree->section = ROFF_MSEC_MAX;
   
         (void)memcpy(&tree->cb, cb, sizeof(struct roffcb));          (void)memcpy(&tree->cb, cb, sizeof(struct roffcb));
   
Line 325  roffparse(struct rofftree *tree, char *buf)
Line 330  roffparse(struct rofftree *tree, char *buf)
         if (ROFF_MAX == (tok = rofffindtok(buf + 1))) {          if (ROFF_MAX == (tok = rofffindtok(buf + 1))) {
                 roff_err(tree, buf + 1, "bogus line macro");                  roff_err(tree, buf + 1, "bogus line macro");
                 return(0);                  return(0);
         } else if (NULL == tokens[tok].cb) {          } else if ( ! roffargs(tree, tok, buf, argv))
                 roff_err(tree, buf + 1, "unsupported macro `%s'",  
                                 toknames[tok]);  
                 return(0);                  return(0);
         }  
   
         assert(ROFF___ != tok);  
         if ( ! roffargs(tree, tok, buf, argv))  
                 return(0);  
   
         argvp = (char **)argv;          argvp = (char **)argv;
   
         /*          /*
Line 343  roffparse(struct rofftree *tree, char *buf)
Line 341  roffparse(struct rofftree *tree, char *buf)
   
         if (ROFF_PRELUDE & tree->state) {          if (ROFF_PRELUDE & tree->state) {
                 assert(NULL == tree->last);                  assert(NULL == tree->last);
                 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));                  return(roffcall(tree, tok, argvp));
         }          }
   
         assert(ROFF_BODY & tree->state);          assert(ROFF_BODY & tree->state);
Line 374  roffparse(struct rofftree *tree, char *buf)
Line 372  roffparse(struct rofftree *tree, char *buf)
          */           */
   
         if (ROFF_LAYOUT != tokens[tok].type)          if (ROFF_LAYOUT != tokens[tok].type)
                 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));                  return(roffcall(tree, tok, argvp));
         if (0 == tokens[tok].ctx)          if (0 == tokens[tok].ctx)
                 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));                  return(roffcall(tree, tok, argvp));
   
         /*          /*
          * First consider implicit-end tags, like as follows:           * First consider implicit-end tags, like as follows:
Line 418  roffparse(struct rofftree *tree, char *buf)
Line 416  roffparse(struct rofftree *tree, char *buf)
                  */                   */
   
                 if (NULL == n)                  if (NULL == n)
                         return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));                          return(roffcall(tree, tok, argvp));
   
                 /*                  /*
                  * Close out all intermediary scoped blocks, then hang                   * Close out all intermediary scoped blocks, then hang
Line 427  roffparse(struct rofftree *tree, char *buf)
Line 425  roffparse(struct rofftree *tree, char *buf)
   
                 do {                  do {
                         t = tree->last->tok;                          t = tree->last->tok;
                         if ( ! (*tokens[t].cb)(t, tree, NULL, ROFF_EXIT))                          if ( ! roffexit(tree, t))
                                 return(0);                                  return(0);
                 } while (t != tok);                  } while (t != tok);
   
                 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));                  return(roffcall(tree, tok, argvp));
         }          }
   
         /*          /*
Line 467  roffparse(struct rofftree *tree, char *buf)
Line 465  roffparse(struct rofftree *tree, char *buf)
         /* LINTED */          /* LINTED */
         do {          do {
                 t = tree->last->tok;                  t = tree->last->tok;
                 if ( ! (*tokens[t].cb)(t, tree, NULL, ROFF_EXIT))                  if ( ! roffexit(tree, t))
                         return(0);                          return(0);
         } while (t != tokens[tok].ctx);          } while (t != tokens[tok].ctx);
   
Line 519  rofffindtok(const char *buf)
Line 517  rofffindtok(const char *buf)
   
   
 static int  static int
   roffismsec(const char *p)
   {
   
           if (0 == strcmp(p, "1"))
                   return(ROFF_MSEC_1);
           else if (0 == strcmp(p, "2"))
                   return(ROFF_MSEC_2);
           else if (0 == strcmp(p, "3"))
                   return(ROFF_MSEC_3);
           else if (0 == strcmp(p, "3p"))
                   return(ROFF_MSEC_3p);
           else if (0 == strcmp(p, "4"))
                   return(ROFF_MSEC_4);
           else if (0 == strcmp(p, "5"))
                   return(ROFF_MSEC_5);
           else if (0 == strcmp(p, "6"))
                   return(ROFF_MSEC_6);
           else if (0 == strcmp(p, "7"))
                   return(ROFF_MSEC_7);
           else if (0 == strcmp(p, "8"))
                   return(ROFF_MSEC_8);
           else if (0 == strcmp(p, "9"))
                   return(ROFF_MSEC_9);
           else if (0 == strcmp(p, "unass"))
                   return(ROFF_MSEC_UNASS);
           else if (0 == strcmp(p, "draft"))
                   return(ROFF_MSEC_DRAFT);
           else if (0 == strcmp(p, "paper"))
                   return(ROFF_MSEC_PAPER);
   
           return(ROFF_MSEC_MAX);
   }
   
   
   static int
 roffispunct(const char *p)  roffispunct(const char *p)
 {  {
   
Line 618  roffnode_free(struct rofftree *tree)
Line 651  roffnode_free(struct rofftree *tree)
   
   
 static int  static int
 roffspecial(struct rofftree *tree, int tok,  roffspecial(struct rofftree *tree, int tok, const char *start,
                 const char *start, size_t sz, char **ordp)                  const int *argc, const char **argv,
                   size_t sz, char **ordp)
 {  {
   
         switch (tok) {          switch (tok) {
           case (ROFF_At):
                   if (0 == sz)
                           break;
                   if (0 == strcmp(*ordp, "v6"))
                           break;
                   else if (0 == strcmp(*ordp, "v7"))
                           break;
                   else if (0 == strcmp(*ordp, "32v"))
                           break;
                   else if (0 == strcmp(*ordp, "V.1"))
                           break;
                   else if (0 == strcmp(*ordp, "V.4"))
                           break;
                   roff_err(tree, start, "invalid `At' arg");
                   return(0);
   
           case (ROFF_Xr):
                   if (2 == sz) {
                           assert(ordp[1]);
                           if (ROFF_MSEC_MAX != roffismsec(ordp[1]))
                                   break;
                           roff_warn(tree, start, "invalid `%s' manual "
                                           "section", toknames[tok]);
                   }
                   /* FALLTHROUGH */
   
           case (ROFF_Fn):
                   if (0 != sz)
                           break;
                   roff_err(tree, start, "`%s' expects at least "
                                   "one arg", toknames[tok]);
                   return(0);
   
         case (ROFF_Nm):          case (ROFF_Nm):
                 if (0 == sz) {                  if (0 == sz) {
                         if (0 == tree->name[0]) {                          if (0 == tree->name[0]) {
Line 635  roffspecial(struct rofftree *tree, int tok, 
Line 702  roffspecial(struct rofftree *tree, int tok, 
                         return(0);                          return(0);
                 break;                  break;
   
           case (ROFF_Rv):
                   /* FALLTHROUGH*/
           case (ROFF_Sx):
                   /* FALLTHROUGH*/
         case (ROFF_Ex):          case (ROFF_Ex):
                 if (0 == sz) {                  if (1 == sz)
                         roff_err(tree, start, "`Ex' expects an arg");                          break;
                         return(0);                  roff_err(tree, start, "`%s' expects one arg",
                 } else if (1 != sz) {                                  toknames[tok]);
                         roff_err(tree, start, "`Ex' expects one arg");                  return(0);
                         return(0);  
                 }  
                 break;  
   
         case (ROFF_Sm):          case (ROFF_Sm):
                 if (0 == sz) {                  if (1 != sz) {
                         roff_err(tree, start, "`Sm' expects an arg");  
                         return(0);  
                 } else if (1 != sz) {  
                         roff_err(tree, start, "`Sm' expects one arg");                          roff_err(tree, start, "`Sm' expects one arg");
                         return(0);                          return(0);
                 }                  }
Line 663  roffspecial(struct rofftree *tree, int tok, 
Line 728  roffspecial(struct rofftree *tree, int tok, 
   
         case (ROFF_Ud):          case (ROFF_Ud):
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
           case (ROFF_Ux):
                   /* FALLTHROUGH */
         case (ROFF_Bt):          case (ROFF_Bt):
                 if (0 != sz) {                  if (0 != sz) {
                         roff_err(tree, start, "`%s' expects no args",                          roff_err(tree, start, "`%s' expects no args",
Line 674  roffspecial(struct rofftree *tree, int tok, 
Line 741  roffspecial(struct rofftree *tree, int tok, 
                 break;                  break;
         }          }
   
         return((*tree->cb.roffspecial)          return((*tree->cb.roffspecial)(tree->arg, tok, tree->cur,
                         (tree->arg, tok, tree->cur, ordp));                                  argc, argv, (const char **)ordp));
 }  }
   
   
 static int  static int
   roffexit(struct rofftree *tree, int tok)
   {
   
           assert(tokens[tok].cb);
           return((*tokens[tok].cb)(tok, tree, NULL, ROFF_EXIT));
   }
   
   
   static int
 roffcall(struct rofftree *tree, int tok, char **argv)  roffcall(struct rofftree *tree, int tok, char **argv)
 {  {
           int              i;
           enum roffmsec    c;
   
         if (NULL == tokens[tok].cb) {          if (NULL == tokens[tok].cb) {
                 roff_err(tree, *argv, "unsupported macro `%s'",                  roff_err(tree, *argv, "`%s' is unsupported",
                                 toknames[tok]);                                  toknames[tok]);
                 return(0);                  return(0);
         }          }
         if ( ! (*tokens[tok].cb)(tok, tree, argv, ROFF_ENTER))          if (tokens[tok].sections && ROFF_MSEC_MAX != tree->section) {
                 return(0);                  i = 0;
         return(1);                  while (ROFF_MSEC_MAX !=
                                   (c = tokens[tok].sections[i++]))
                           if (c == tree->section)
                                   break;
                   if (ROFF_MSEC_MAX == c) {
                           roff_warn(tree, *argv, "`%s' is not a valid "
                                           "macro in this manual section",
                                           toknames[tok]);
                   }
           }
   
           return((*tokens[tok].cb)(tok, tree, argv, ROFF_ENTER));
 }  }
   
   
Line 917  roff_Dt(ROFFCALL_ARGS)
Line 1006  roff_Dt(ROFFCALL_ARGS)
         if (NULL == *argv) {          if (NULL == *argv) {
                 roff_err(tree, *argv, "`Dt' needs section");                  roff_err(tree, *argv, "`Dt' needs section");
                 return(0);                  return(0);
         } else if (strlcpy(tree->section, *argv, sizeof(tree->section))          }
                         >= sizeof(tree->section)) {  
                 roff_err(tree, *argv, "`Dt' section too long");          if (ROFF_MSEC_MAX == (tree->section = roffismsec(*argv))) {
                   roff_err(tree, *argv, "bad `Dt' section");
                 return(0);                  return(0);
         }          }
   
Line 963  roffsetname(struct rofftree *tree, char **ordp)
Line 1053  roffsetname(struct rofftree *tree, char **ordp)
   
   
 /* ARGSUSED */  /* ARGSUSED */
 static int  
 roff_Sm(ROFFCALL_ARGS)  
 {  
         char            *ordp[1], *p;  
   
         p = *argv++;  
         *ordp = *argv;  
   
         return(roffspecial(tree, tok, p, *ordp ? 1 : 0, ordp));  
 }  
   
   
 /* ARGSUSED */  
 static  int  static  int
 roff_Ns(ROFFCALL_ARGS)  roff_Ns(ROFFCALL_ARGS)
 {  {
Line 985  roff_Ns(ROFFCALL_ARGS)
Line 1062  roff_Ns(ROFFCALL_ARGS)
         first = (*argv++ == tree->cur);          first = (*argv++ == tree->cur);
         morep[0] = NULL;          morep[0] = NULL;
   
         if ( ! roffspecial(tree, tok, *argv, 0, morep))          if ( ! roffspecial(tree, tok, *argv, NULL, NULL, 0, morep))
                 return(0);                  return(0);
   
         while (*argv) {          while (*argv) {
Line 1063  roff_Os(ROFFCALL_ARGS)
Line 1140  roff_Os(ROFFCALL_ARGS)
         tree->state &= ~ROFF_PRELUDE;          tree->state &= ~ROFF_PRELUDE;
         tree->state |= ROFF_BODY;          tree->state |= ROFF_BODY;
   
           assert(ROFF_MSEC_MAX != tree->section);
           assert(0 != tree->title[0]);
           assert(0 != tree->os[0]);
   
         assert(NULL == tree->last);          assert(NULL == tree->last);
   
         return((*tree->cb.roffhead)(tree->arg, &tree->tm,          return((*tree->cb.roffhead)(tree->arg, &tree->tm,
Line 1104  roff_layout(ROFFCALL_ARGS) 
Line 1185  roff_layout(ROFFCALL_ARGS) 
          * the layout body is everything following until termination.           * the layout body is everything following until termination.
          */           */
   
         if ( ! (*tree->cb.roffblkin)(tree->arg, tok, argcp, argvp))          if ( ! (*tree->cb.roffblkin)(tree->arg, tok, argcp,
                                   (const char **)argvp))
                 return(0);                  return(0);
         if (NULL == *argv)          if (NULL == *argv)
                 return((*tree->cb.roffblkbodyin)                  return((*tree->cb.roffblkbodyin)
                                 (tree->arg, tok, argcp, argvp));                                  (tree->arg, tok, argcp,
                                    (const char **)argvp));
   
         if ( ! (*tree->cb.roffblkheadin)(tree->arg, tok, argcp, argvp))          if ( ! (*tree->cb.roffblkheadin)(tree->arg, tok, argcp,
                                   (const char **)argvp))
                 return(0);                  return(0);
   
         /*          /*
Line 1127  roff_layout(ROFFCALL_ARGS) 
Line 1211  roff_layout(ROFFCALL_ARGS) 
                 if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))                  if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))
                         return(0);                          return(0);
                 return((*tree->cb.roffblkbodyin)                  return((*tree->cb.roffblkbodyin)
                                 (tree->arg, tok, argcp, argvp));                                  (tree->arg, tok, argcp,
                                    (const char **)argvp));
         }          }
   
         /*          /*
Line 1158  roff_layout(ROFFCALL_ARGS) 
Line 1243  roff_layout(ROFFCALL_ARGS) 
                 if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))                  if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))
                         return(0);                          return(0);
                 return((*tree->cb.roffblkbodyin)                  return((*tree->cb.roffblkbodyin)
                                 (tree->arg, tok, argcp, argvp));                                  (tree->arg, tok, argcp,
                                    (const char **)argvp));
         }          }
   
         /*          /*
Line 1172  roff_layout(ROFFCALL_ARGS) 
Line 1258  roff_layout(ROFFCALL_ARGS) 
         if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))          if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))
                 return(0);                  return(0);
         return((*tree->cb.roffblkbodyin)          return((*tree->cb.roffblkbodyin)
                         (tree->arg, tok, argcp, argvp));                          (tree->arg, tok, argcp,
                            (const char **)argvp));
 }  }
   
   
Line 1196  roff_ordered(ROFFCALL_ARGS) 
Line 1283  roff_ordered(ROFFCALL_ARGS) 
         if ( ! roffparseopts(tree, tok, &argv, argcp, argvp))          if ( ! roffparseopts(tree, tok, &argv, argcp, argvp))
                 return(0);                  return(0);
   
         if (NULL == *argv)          if (NULL == *argv) {
                 return(roffspecial(tree, tok, p, 0, ordp));                  ordp[0] = NULL;
                   return(roffspecial(tree, tok, p, argcp,
                                           (const char **)argvp, 0, ordp));
           }
   
         i = 0;          i = 0;
         while (*argv && i < ROFF_MAXLINEARG) {          while (*argv && i < ROFF_MAXLINEARG) {
                 c = rofffindcallable(*argv);                  c = ROFF_PARSED & tokens[tok].flags ?
                           rofffindcallable(*argv) : ROFF_MAX;
   
                 if (ROFF_MAX == c && ! roffispunct(*argv)) {                  if (ROFF_MAX == c && ! roffispunct(*argv)) {
                         ordp[i++] = *argv++;                          ordp[i++] = *argv++;
Line 1212  roff_ordered(ROFFCALL_ARGS) 
Line 1303  roff_ordered(ROFFCALL_ARGS) 
                 if (ROFF_MAX == c)                  if (ROFF_MAX == c)
                         break;                          break;
   
                 if ( ! roffspecial(tree, tok, p, (size_t)i, ordp))                  if ( ! roffspecial(tree, tok, p, argcp,
                                           (const char **)argvp,
                                           (size_t)i, ordp))
                         return(0);                          return(0);
   
                 return(roffcall(tree, c, ordp));                  return(roffcall(tree, c, argv));
         }          }
   
         assert(i != ROFF_MAXLINEARG);          assert(i != ROFF_MAXLINEARG);
         ordp[i] = NULL;          ordp[i] = NULL;
   
         if ( ! roffspecial(tree, tok, p, (size_t)i, ordp))          if ( ! roffspecial(tree, tok, p, argcp,
                                   (const char**)argvp,
                                   (size_t)i, ordp))
                 return(0);                  return(0);
   
         /* FIXME: error if there's stuff after the punctuation. */          /* FIXME: error if there's stuff after the punctuation. */
Line 1251  roff_text(ROFFCALL_ARGS) 
Line 1346  roff_text(ROFFCALL_ARGS) 
   
         if ( ! roffparseopts(tree, tok, &argv, argcp, argvp))          if ( ! roffparseopts(tree, tok, &argv, argcp, argvp))
                 return(0);                  return(0);
         if ( ! (*tree->cb.roffin)(tree->arg, tok, argcp, argvp))          if ( ! (*tree->cb.roffin)(tree->arg, tok, argcp,
                                   (const char **)argvp))
                 return(0);                  return(0);
         if (NULL == *argv)          if (NULL == *argv)
                 return((*tree->cb.roffout)(tree->arg, tok));                  return((*tree->cb.roffout)(tree->arg, tok));
Line 1270  roff_text(ROFFCALL_ARGS) 
Line 1366  roff_text(ROFFCALL_ARGS) 
          * terminating punctuation.  If we encounter it and all           * terminating punctuation.  If we encounter it and all
          * subsequent tokens are punctuation, then stop processing (the           * subsequent tokens are punctuation, then stop processing (the
          * line-dominant macro will print these tokens after closure).           * line-dominant macro will print these tokens after closure).
            * If the punctuation is followed by non-punctuation, then close
            * and re-open our scope, then continue.
          */           */
   
         i = 0;          i = 0;
Line 1301  roff_text(ROFFCALL_ARGS) 
Line 1399  roff_text(ROFFCALL_ARGS) 
                                 break;                                  break;
   
                 if (argv[j]) {                  if (argv[j]) {
                           if (ROFF_LSCOPE & tokens[tok].flags) {
                                   if ( ! roffdata(tree, 0, *argv++))
                                           return(0);
                                   continue;
                           }
                           if ( ! (*tree->cb.roffout)(tree->arg, tok))
                                   return(0);
                         if ( ! roffdata(tree, 0, *argv++))                          if ( ! roffdata(tree, 0, *argv++))
                                 return(0);                                  return(0);
                           if ( ! (*tree->cb.roffin)(tree->arg, tok,
                                                   argcp,
                                                   (const char **)argvp))
                                   return(0);
   
                           i = 0;
                         continue;                          continue;
                 }                  }
   

Legend:
Removed from v.1.45  
changed lines
  Added in v.1.51

CVSweb