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

Diff for /mandoc/mdoc_argv.c between version 1.95 and 1.105

version 1.95, 2014/07/06 19:09:00 version 1.105, 2015/10/06 18:32:19
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2012 Ingo Schwarze <schwarze@openbsd.org>   * Copyright (c) 2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * 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
  * copyright notice and this permission notice appear in all copies.   * copyright notice and this permission notice appear in all copies.
  *   *
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */   */
 #ifdef HAVE_CONFIG_H  
 #include "config.h"  #include "config.h"
 #endif  
   
 #include <sys/types.h>  #include <sys/types.h>
   
Line 26 
Line 24 
 #include <stdio.h>  #include <stdio.h>
 #include <string.h>  #include <string.h>
   
 #include "mdoc.h"  
 #include "mandoc.h"  
 #include "mandoc_aux.h"  #include "mandoc_aux.h"
 #include "libmdoc.h"  #include "mandoc.h"
   #include "roff.h"
   #include "mdoc.h"
 #include "libmandoc.h"  #include "libmandoc.h"
   #include "libmdoc.h"
   
 #define MULTI_STEP       5 /* pre-allocate argument values */  #define MULTI_STEP       5 /* pre-allocate argument values */
 #define DELIMSZ          6 /* max possible size of a delimiter */  #define DELIMSZ          6 /* max possible size of a delimiter */
Line 53  struct mdocarg {
Line 52  struct mdocarg {
 };  };
   
 static  void             argn_free(struct mdoc_arg *, int);  static  void             argn_free(struct mdoc_arg *, int);
 static  enum margserr    args(struct mdoc *, int, int *,  static  enum margserr    args(struct roff_man *, int, int *,
                                 char *, enum argsflag, char **);                                  char *, enum argsflag, char **);
 static  int              args_checkpunct(const char *, int);  static  int              args_checkpunct(const char *, int);
 static  int              argv_multi(struct mdoc *, int,  static  void             argv_multi(struct roff_man *, int,
                                 struct mdoc_argv *, int *, char *);                                  struct mdoc_argv *, int *, char *);
 static  int              argv_single(struct mdoc *, int,  static  void             argv_single(struct roff_man *, int,
                                 struct mdoc_argv *, int *, char *);                                  struct mdoc_argv *, int *, char *);
   
 static  const enum argvflag argvflags[MDOC_ARG_MAX] = {  static  const enum argvflag argvflags[MDOC_ARG_MAX] = {
Line 272  static const struct mdocarg mdocargs[MDOC_MAX] = {
Line 271  static const struct mdocarg mdocargs[MDOC_MAX] = {
   
   
 /*  /*
  * Parse an argument from line text.  This comes in the form of -key   * Parse flags and their arguments from the input line.
  * [value0...], which may either have a single mandatory value, at least   * These come in the form -flag [argument ...].
  * one mandatory value, an optional single value, or no value.   * Some flags take no argument, some one, some multiple.
  */   */
 enum margverr  void
 mdoc_argv(struct mdoc *mdoc, int line, enum mdoct tok,  mdoc_argv(struct roff_man *mdoc, int line, int tok,
                 struct mdoc_arg **v, int *pos, char *buf)          struct mdoc_arg **reta, int *pos, char *buf)
 {  {
         char             *p, sv;          struct mdoc_argv          tmpv;
         struct mdoc_argv tmp;          struct mdoc_argv        **retv;
         struct mdoc_arg  *arg;          const enum mdocargt      *argtable;
         const enum mdocargt *ap;          char                     *argname;
           int                       ipos, retc;
           char                      savechar;
   
         if ('\0' == buf[*pos])          *reta = NULL;
                 return(ARGV_EOLN);  
         else if (NULL == (ap = mdocargs[tok].argvs))  
                 return(ARGV_WORD);  
         else if ('-' != buf[*pos])  
                 return(ARGV_WORD);  
   
         /* Seek to the first unescaped space. */          /* Which flags does this macro support? */
   
         p = &buf[++(*pos)];          argtable = mdocargs[tok].argvs;
           if (argtable == NULL)
                   return;
   
         assert(*pos > 0);          /* Loop over the flags on the input line. */
   
         for ( ; buf[*pos] ; (*pos)++)          ipos = *pos;
                 if (' ' == buf[*pos] && '\\' != buf[*pos - 1])          while (buf[ipos] == '-') {
                         break;  
   
         /*                  /* Seek to the first unescaped space. */
          * We want to nil-terminate the word to look it up (it's easier  
          * that way).  But we may not have a flag, in which case we need  
          * to restore the line as-is.  So keep around the stray byte,  
          * which we'll reset upon exiting (if necessary).  
          */  
   
         if ('\0' != (sv = buf[*pos]))                  for (argname = buf + ++ipos; buf[ipos] != '\0'; ipos++)
                 buf[(*pos)++] = '\0';                          if (buf[ipos] == ' ' && buf[ipos - 1] != '\\')
                                   break;
   
         /*                  /*
          * Now look up the word as a flag.  Use temporary storage that                   * We want to nil-terminate the word to look it up.
          * we'll copy into the node's flags, if necessary.                   * But we may not have a flag, in which case we need
          */                   * to restore the line as-is.  So keep around the
                    * stray byte, which we'll reset upon exiting.
                    */
   
         memset(&tmp, 0, sizeof(struct mdoc_argv));                  if ((savechar = buf[ipos]) != '\0')
                           buf[ipos++] = '\0';
   
         tmp.line = line;                  /*
         tmp.pos = *pos;                   * Now look up the word as a flag.  Use temporary
         tmp.arg = MDOC_ARG_MAX;                   * storage that we'll copy into the node's flags.
                    */
   
         while (MDOC_ARG_MAX != (tmp.arg = *ap++))                  while ((tmpv.arg = *argtable++) != MDOC_ARG_MAX)
                 if (0 == strcmp(p, mdoc_argnames[tmp.arg]))                          if ( ! strcmp(argname, mdoc_argnames[tmpv.arg]))
                                   break;
   
                   /* If it isn't a flag, restore the saved byte. */
   
                   if (tmpv.arg == MDOC_ARG_MAX) {
                           if (savechar != '\0')
                                   buf[ipos - 1] = savechar;
                         break;                          break;
                   }
   
         if (MDOC_ARG_MAX == tmp.arg) {                  /* Read to the next word (the first argument). */
                 /*  
                  * The flag was not found.  
                  * Restore saved zeroed byte and return as a word.  
                  */  
                 if (sv)  
                         buf[*pos - 1] = sv;  
                 return(ARGV_WORD);  
         }  
   
         /* Read to the next word (the argument). */                  while (buf[ipos] == ' ')
                           ipos++;
   
         while (buf[*pos] && ' ' == buf[*pos])                  /* Parse the arguments of the flag. */
                 (*pos)++;  
   
         switch (argvflags[tmp.arg]) {                  tmpv.line  = line;
         case ARGV_SINGLE:                  tmpv.pos   = *pos;
                 if ( ! argv_single(mdoc, line, &tmp, pos, buf))                  tmpv.sz    = 0;
                         return(ARGV_ERROR);                  tmpv.value = NULL;
                 break;  
         case ARGV_MULTI:  
                 if ( ! argv_multi(mdoc, line, &tmp, pos, buf))  
                         return(ARGV_ERROR);  
                 break;  
         case ARGV_NONE:  
                 break;  
         }  
   
         if (NULL == (arg = *v))                  switch (argvflags[tmpv.arg]) {
                 arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg));                  case ARGV_SINGLE:
                           argv_single(mdoc, line, &tmpv, &ipos, buf);
                           break;
                   case ARGV_MULTI:
                           argv_multi(mdoc, line, &tmpv, &ipos, buf);
                           break;
                   case ARGV_NONE:
                           break;
                   }
   
         arg->argc++;                  /* Append to the return values. */
         arg->argv = mandoc_reallocarray(arg->argv,  
             arg->argc, sizeof(struct mdoc_argv));  
   
         memcpy(&arg->argv[(int)arg->argc - 1], &tmp,                  if (*reta == NULL)
             sizeof(struct mdoc_argv));                          *reta = mandoc_calloc(1, sizeof(**reta));
   
         return(ARGV_ARG);                  retc = ++(*reta)->argc;
                   retv = &(*reta)->argv;
                   *retv = mandoc_reallocarray(*retv, retc, sizeof(**retv));
                   memcpy(*retv + retc - 1, &tmpv, sizeof(**retv));
   
                   /* Prepare for parsing the next flag. */
   
                   *pos = ipos;
                   argtable = mdocargs[tok].argvs;
           }
 }  }
   
 void  void
Line 409  argn_free(struct mdoc_arg *p, int iarg)
Line 413  argn_free(struct mdoc_arg *p, int iarg)
 }  }
   
 enum margserr  enum margserr
 mdoc_zargs(struct mdoc *mdoc, int line, int *pos, char *buf, char **v)  mdoc_args(struct roff_man *mdoc, int line, int *pos,
           char *buf, int tok, char **v)
 {  {
           struct roff_node *n;
         return(args(mdoc, line, pos, buf, ARGSFL_NONE, v));          char             *v_local;
 }  
   
 enum margserr  
 mdoc_args(struct mdoc *mdoc, int line, int *pos,  
                 char *buf, enum mdoct tok, char **v)  
 {  
         enum argsflag     fl;          enum argsflag     fl;
         struct mdoc_node *n;  
   
         fl = mdocargs[tok].flags;          if (v == NULL)
                   v = &v_local;
           fl = tok == TOKEN_NONE ? ARGSFL_NONE : mdocargs[tok].flags;
           if (tok != MDOC_It)
                   return args(mdoc, line, pos, buf, fl, v);
   
         if (MDOC_It != tok)  
                 return(args(mdoc, line, pos, buf, fl, v));  
   
         /*          /*
          * We know that we're in an `It', so it's reasonable to expect           * We know that we're in an `It', so it's reasonable to expect
          * us to be sitting in a `Bl'.  Someday this may not be the case           * us to be sitting in a `Bl'.  Someday this may not be the case
Line 441  mdoc_args(struct mdoc *mdoc, int line, int *pos,
Line 440  mdoc_args(struct mdoc *mdoc, int line, int *pos,
                                 break;                                  break;
                         }                          }
   
         return(args(mdoc, line, pos, buf, fl, v));          return args(mdoc, line, pos, buf, fl, v);
 }  }
   
 static enum margserr  static enum margserr
 args(struct mdoc *mdoc, int line, int *pos,  args(struct roff_man *mdoc, int line, int *pos,
                 char *buf, enum argsflag fl, char **v)                  char *buf, enum argsflag fl, char **v)
 {  {
         char            *p, *pp;          char            *p, *pp;
Line 454  args(struct mdoc *mdoc, int line, int *pos,
Line 453  args(struct mdoc *mdoc, int line, int *pos,
   
         if ('\0' == buf[*pos]) {          if ('\0' == buf[*pos]) {
                 if (MDOC_PPHRASE & mdoc->flags)                  if (MDOC_PPHRASE & mdoc->flags)
                         return(ARGS_EOLN);                          return ARGS_EOLN;
                 /*                  /*
                  * If we're not in a partial phrase and the flag for                   * If we're not in a partial phrase and the flag for
                  * being a phrase literal is still set, the punctuation                   * being a phrase literal is still set, the punctuation
Line 465  args(struct mdoc *mdoc, int line, int *pos,
Line 464  args(struct mdoc *mdoc, int line, int *pos,
                             mdoc->parse, line, *pos, NULL);                              mdoc->parse, line, *pos, NULL);
   
                 mdoc->flags &= ~MDOC_PHRASELIT;                  mdoc->flags &= ~MDOC_PHRASELIT;
                 return(ARGS_EOLN);                  return ARGS_EOLN;
         }          }
   
         *v = &buf[*pos];          *v = &buf[*pos];
   
         if (ARGSFL_DELIM == fl)          if (ARGSFL_DELIM == fl)
                 if (args_checkpunct(buf, *pos))                  if (args_checkpunct(buf, *pos))
                         return(ARGS_PUNCT);                          return ARGS_PUNCT;
   
         /*          /*
          * First handle TABSEP items, restricted to `Bl -column'.  This           * First handle TABSEP items, restricted to `Bl -column'.  This
Line 539  args(struct mdoc *mdoc, int line, int *pos,
Line 538  args(struct mdoc *mdoc, int line, int *pos,
                 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)                  for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
                         /* Skip ahead. */ ;                          /* Skip ahead. */ ;
   
                 return(rc);                  return rc;
         }          }
   
         /*          /*
Line 576  args(struct mdoc *mdoc, int line, int *pos,
Line 575  args(struct mdoc *mdoc, int line, int *pos,
   
                 if ('\0' == buf[*pos]) {                  if ('\0' == buf[*pos]) {
                         if (MDOC_PPHRASE & mdoc->flags)                          if (MDOC_PPHRASE & mdoc->flags)
                                 return(ARGS_QWORD);                                  return ARGS_QWORD;
                         mandoc_msg(MANDOCERR_ARG_QUOTE,                          mandoc_msg(MANDOCERR_ARG_QUOTE,
                             mdoc->parse, line, *pos, NULL);                              mdoc->parse, line, *pos, NULL);
                         return(ARGS_QWORD);                          return ARGS_QWORD;
                 }                  }
   
                 mdoc->flags &= ~MDOC_PHRASELIT;                  mdoc->flags &= ~MDOC_PHRASELIT;
                 buf[(*pos)++] = '\0';                  buf[(*pos)++] = '\0';
   
                 if ('\0' == buf[*pos])                  if ('\0' == buf[*pos])
                         return(ARGS_QWORD);                          return ARGS_QWORD;
   
                 while (' ' == buf[*pos])                  while (' ' == buf[*pos])
                         (*pos)++;                          (*pos)++;
Line 595  args(struct mdoc *mdoc, int line, int *pos,
Line 594  args(struct mdoc *mdoc, int line, int *pos,
                         mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,                          mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
                             line, *pos, NULL);                              line, *pos, NULL);
   
                 return(ARGS_QWORD);                  return ARGS_QWORD;
         }          }
   
         p = &buf[*pos];          p = &buf[*pos];
         *v = mandoc_getarg(mdoc->parse, &p, line, pos);          *v = mandoc_getarg(mdoc->parse, &p, line, pos);
   
         return(ARGS_WORD);          return ARGS_WORD;
 }  }
   
 /*  /*
Line 623  args_checkpunct(const char *buf, int i)
Line 622  args_checkpunct(const char *buf, int i)
                 dbuf[j] = buf[i];                  dbuf[j] = buf[i];
   
         if (DELIMSZ == j)          if (DELIMSZ == j)
                 return(0);                  return 0;
   
         dbuf[j] = '\0';          dbuf[j] = '\0';
         if (DELIM_CLOSE != mdoc_isdelim(dbuf))          if (DELIM_CLOSE != mdoc_isdelim(dbuf))
                 return(0);                  return 0;
   
         while (' ' == buf[i])          while (' ' == buf[i])
                 i++;                  i++;
Line 640  args_checkpunct(const char *buf, int i)
Line 639  args_checkpunct(const char *buf, int i)
                         dbuf[j++] = buf[i++];                          dbuf[j++] = buf[i++];
   
                 if (DELIMSZ == j)                  if (DELIMSZ == j)
                         return(0);                          return 0;
   
                 dbuf[j] = '\0';                  dbuf[j] = '\0';
                 d = mdoc_isdelim(dbuf);                  d = mdoc_isdelim(dbuf);
                 if (DELIM_NONE == d || DELIM_OPEN == d)                  if (DELIM_NONE == d || DELIM_OPEN == d)
                         return(0);                          return 0;
   
                 while (' ' == buf[i])                  while (' ' == buf[i])
                         i++;                          i++;
         }          }
   
         return('\0' == buf[i]);          return '\0' == buf[i];
 }  }
   
 static int  static void
 argv_multi(struct mdoc *mdoc, int line,  argv_multi(struct roff_man *mdoc, int line,
                 struct mdoc_argv *v, int *pos, char *buf)                  struct mdoc_argv *v, int *pos, char *buf)
 {  {
         enum margserr    ac;          enum margserr    ac;
         char            *p;          char            *p;
   
         for (v->sz = 0; ; v->sz++) {          for (v->sz = 0; ; v->sz++) {
                 if ('-' == buf[*pos])                  if (buf[*pos] == '-')
                         break;                          break;
                 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);                  ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
                 if (ARGS_ERROR == ac)                  if (ac == ARGS_EOLN)
                         return(0);  
                 else if (ARGS_EOLN == ac)  
                         break;                          break;
   
                 if (0 == v->sz % MULTI_STEP)                  if (v->sz % MULTI_STEP == 0)
                         v->value = mandoc_reallocarray(v->value,                          v->value = mandoc_reallocarray(v->value,
                             v->sz + MULTI_STEP, sizeof(char *));                              v->sz + MULTI_STEP, sizeof(char *));
   
                 v->value[(int)v->sz] = mandoc_strdup(p);                  v->value[(int)v->sz] = mandoc_strdup(p);
         }          }
   
         return(1);  
 }  }
   
 static int  static void
 argv_single(struct mdoc *mdoc, int line,  argv_single(struct roff_man *mdoc, int line,
                 struct mdoc_argv *v, int *pos, char *buf)                  struct mdoc_argv *v, int *pos, char *buf)
 {  {
         enum margserr    ac;          enum margserr    ac;
         char            *p;          char            *p;
   
         ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);          ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
         if (ARGS_ERROR == ac)          if (ac == ARGS_EOLN)
                 return(0);                  return;
         if (ARGS_EOLN == ac)  
                 return(1);  
   
         v->sz = 1;          v->sz = 1;
         v->value = mandoc_malloc(sizeof(char *));          v->value = mandoc_malloc(sizeof(char *));
         v->value[0] = mandoc_strdup(p);          v->value[0] = mandoc_strdup(p);
   
         return(1);  
 }  }

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

CVSweb