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

Diff for /texi2mdoc/util.c between version 1.15 and 1.19

version 1.15, 2015/02/25 14:49:14 version 1.19, 2015/02/28 13:16:44
Line 330  advance(struct texi *p, size_t *pos)
Line 330  advance(struct texi *p, size_t *pos)
                         f->col = 0;                          f->col = 0;
                 } else                  } else
                         f->col++;                          f->col++;
         } else          } else {
                 --f->insplice;                  --f->insplice;
                   if (0 == f->insplice)
                           f->depth = 0;
           }
   
         (*pos)++;          (*pos)++;
 }  }
Line 439  advanceto(struct texi *p, size_t *pos, size_t end)
Line 442  advanceto(struct texi *p, size_t *pos, size_t end)
 }  }
   
 static void  static void
 texiexecmacro(struct texi *p, struct teximacro *m, size_t *pos)  texiexecmacro(struct texi *p, struct teximacro *m, size_t sv, size_t *pos)
 {  {
         size_t            valsz, realsz, aasz, asz,          size_t            valsz, realsz, aasz, asz,
                            ssz, i, j, k, start, end;                             ssz, i, j, k, start, end;
Line 447  texiexecmacro(struct texi *p, struct teximacro *m, siz
Line 450  texiexecmacro(struct texi *p, struct teximacro *m, siz
         char            **args;          char            **args;
         const char       *cp;          const char       *cp;
   
           /* Disregard empty macros. */
           if (0 == (valsz = realsz = strlen(m->value)))
                   return;
   
           /*
            * This is important: it protect us from macros that invoke more
            * macros, possibly going on infinitely.
            * We use "sv" instead of the current position because we might
            * be invoked at the end of the macro (i.e., insplice == 0).
            * The "sv" value was initialised at the start of the macro.
            */
           if (sv > 0)
                   if (++p->files[p->filepos].depth > 64)
                           texierr(p, "maximium recursive depth");
   
         args = argparse(p, pos, &asz, m->argsz);          args = argparse(p, pos, &asz, m->argsz);
         if (asz != m->argsz)          if (asz != m->argsz)
                 texiwarn(p, "invalid macro argument length");                  texiwarn(p, "invalid macro argument length");
         aasz = asz < m->argsz ? asz : m->argsz;          aasz = asz < m->argsz ? asz : m->argsz;
   
         if (0 == aasz) {          if (0 == aasz) {
                 texisplice(p, m->value, strlen(m->value), pos);                  texisplice(p, m->value, valsz, pos);
                 return;                  return;
         }          }
   
         valsz = realsz = strlen(m->value);  
         val = strdup(m->value);          val = strdup(m->value);
   
         for (i = j = 0; i < realsz; i++) {          for (i = j = 0; i < realsz; i++) {
Line 574  parseword(struct texi *p, size_t *pos, char extra)
Line 591  parseword(struct texi *p, size_t *pos, char extra)
  * index after the command name.   * index after the command name.
  */   */
 enum texicmd  enum texicmd
 texicmd(struct texi *p, size_t pos, size_t *end, struct teximacro **macro)  texicmd(const struct texi *p, size_t pos, size_t *end, struct teximacro **macro)
 {  {
         size_t   i, len, toksz;          size_t   i, len, toksz;
   
Line 589  texicmd(struct texi *p, size_t pos, size_t *end, struc
Line 606  texicmd(struct texi *p, size_t pos, size_t *end, struc
                 return(TEXICMD__MAX);                  return(TEXICMD__MAX);
   
         /* Alphabetic commands are special. */          /* Alphabetic commands are special. */
         if ( ! isalpha(BUF(p)[pos])) {          if ( ! isalpha((unsigned char)BUF(p)[pos])) {
                 if ((*end = pos + 1) == BUFSZ(p))                  if ((*end = pos + 1) == BUFSZ(p))
                         return(TEXICMD__MAX);                          return(TEXICMD__MAX);
                 for (i = 0; i < TEXICMD__MAX; i++) {                  for (i = 0; i < TEXICMD__MAX; i++) {
Line 653  texicmd(struct texi *p, size_t pos, size_t *end, struc
Line 670  texicmd(struct texi *p, size_t pos, size_t *end, struc
 int  int
 parsearg(struct texi *p, size_t *pos, size_t num)  parsearg(struct texi *p, size_t *pos, size_t num)
 {  {
         size_t            end;          size_t            end, sv;
         enum texicmd      cmd;          enum texicmd      cmd;
         struct teximacro *macro;          struct teximacro *macro;
   
Line 684  parsearg(struct texi *p, size_t *pos, size_t num)
Line 701  parsearg(struct texi *p, size_t *pos, size_t num)
                         continue;                          continue;
                 }                  }
   
                   sv = p->files[p->filepos - 1].insplice;
                 cmd = texicmd(p, *pos, &end, &macro);                  cmd = texicmd(p, *pos, &end, &macro);
                 advanceto(p, pos, end);                  advanceto(p, pos, end);
                 if (NULL != macro)                  if (NULL != macro)
                         texiexecmacro(p, macro, pos);                          texiexecmacro(p, macro, sv, pos);
                 if (TEXICMD__MAX == cmd)                  if (TEXICMD__MAX == cmd)
                         continue;                          continue;
                 if (NULL != texitoks[cmd].fp)                  if (NULL != texitoks[cmd].fp)
Line 701  parsearg(struct texi *p, size_t *pos, size_t num)
Line 719  parsearg(struct texi *p, size_t *pos, size_t num)
  * This will stop in the event of EOF or if we're not at a bracket.   * This will stop in the event of EOF or if we're not at a bracket.
  */   */
 void  void
 parsebracket(struct texi *p, size_t *pos)  parsebracket(struct texi *p, size_t *pos, int dostack)
 {  {
         size_t            end;          size_t            end, sv, stack;
         enum texicmd      cmd;          enum texicmd      cmd;
         struct teximacro *macro;          struct teximacro *macro;
   
Line 714  parsebracket(struct texi *p, size_t *pos)
Line 732  parsebracket(struct texi *p, size_t *pos)
                 return;                  return;
         advance(p, pos);          advance(p, pos);
   
           stack = 0;
         while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {          while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {
                 switch (BUF(p)[*pos]) {                  switch (BUF(p)[*pos]) {
                 case ('}'):                  case ('}'):
                           if (stack > 0) {
                                   stack--;
                                   advance(p, pos);
                                   texiputchar(p, '}');
                                   continue;
                           }
                         advance(p, pos);                          advance(p, pos);
                         return;                          return;
                 case ('{'):                  case ('{'):
                           if (dostack) {
                                   stack++;
                                   advance(p, pos);
                                   texiputchar(p, '{');
                                   continue;
                           }
                         if (0 == p->ign)                          if (0 == p->ign)
                                 texiwarn(p, "unexpected \"{\"");                                  texiwarn(p, "unexpected \"{\"");
                         advance(p, pos);                          advance(p, pos);
Line 731  parsebracket(struct texi *p, size_t *pos)
Line 762  parsebracket(struct texi *p, size_t *pos)
                         continue;                          continue;
                 }                  }
   
                   sv = p->files[p->filepos - 1].insplice;
                 cmd = texicmd(p, *pos, &end, &macro);                  cmd = texicmd(p, *pos, &end, &macro);
                 advanceto(p, pos, end);                  advanceto(p, pos, end);
                 if (NULL != macro)                  if (NULL != macro)
                         texiexecmacro(p, macro, pos);                          texiexecmacro(p, macro, sv, pos);
                 if (TEXICMD__MAX == cmd)                  if (TEXICMD__MAX == cmd)
                         continue;                          continue;
                 if (NULL != texitoks[cmd].fp)                  if (NULL != texitoks[cmd].fp)
Line 750  parsebracket(struct texi *p, size_t *pos)
Line 782  parsebracket(struct texi *p, size_t *pos)
 void  void
 parseeoln(struct texi *p, size_t *pos)  parseeoln(struct texi *p, size_t *pos)
 {  {
         size_t            end;          size_t            end, sv;
         enum texicmd      cmd;          enum texicmd      cmd;
         struct teximacro *macro;          struct teximacro *macro;
   
Line 779  parseeoln(struct texi *p, size_t *pos)
Line 811  parseeoln(struct texi *p, size_t *pos)
                         continue;                          continue;
                 }                  }
   
                   sv = p->files[p->filepos - 1].insplice;
                 cmd = texicmd(p, *pos, &end, &macro);                  cmd = texicmd(p, *pos, &end, &macro);
                 advanceto(p, pos, end);                  advanceto(p, pos, end);
                 if (NULL != macro)                  if (NULL != macro)
                         texiexecmacro(p, macro, pos);                          texiexecmacro(p, macro, sv, pos);
                 if (TEXICMD__MAX == cmd)                  if (TEXICMD__MAX == cmd)
                         continue;                          continue;
                 if (NULL != texitoks[cmd].fp)                  if (NULL != texitoks[cmd].fp)
Line 794  parseeoln(struct texi *p, size_t *pos)
Line 827  parseeoln(struct texi *p, size_t *pos)
 }  }
   
 /*  /*
    * Peek to see if there's a command after subsequent whitespace.
    * If so, return the macro identifier.
    * This DOES NOT work with user-defined macros.
    */
   enum texicmd
   peekcmd(const struct texi *p, size_t pos)
   {
           size_t          end;
   
           while (pos < BUFSZ(p) && ismspace(BUF(p)[pos]))
                   pos++;
           if (pos == BUFSZ(p) || '@' != BUF(p)[pos])
                   return(TEXICMD__MAX);
           return(texicmd(p, pos, &end, NULL));
   }
   
   /*
  * Parse a single word or command.   * Parse a single word or command.
  * This will return immediately at the EOF.   * This will return immediately at the EOF.
  */   */
 static void  static void
 parsesingle(struct texi *p, size_t *pos)  parsesingle(struct texi *p, size_t *pos)
 {  {
         size_t            end;          size_t            end, sv;
         enum texicmd      cmd;          enum texicmd      cmd;
         struct teximacro *macro;          struct teximacro *macro;
   
Line 825  parsesingle(struct texi *p, size_t *pos)
Line 875  parsesingle(struct texi *p, size_t *pos)
                 return;                  return;
         }          }
   
           sv = p->files[p->filepos - 1].insplice;
         cmd = texicmd(p, *pos, &end, &macro);          cmd = texicmd(p, *pos, &end, &macro);
         advanceto(p, pos, end);          advanceto(p, pos, end);
         if (NULL != macro)          if (NULL != macro)
                 texiexecmacro(p, macro, pos);                  texiexecmacro(p, macro, sv, pos);
         if (TEXICMD__MAX == cmd)          if (TEXICMD__MAX == cmd)
                 return;                  return;
         if (NULL != texitoks[cmd].fp)          if (NULL != texitoks[cmd].fp)
Line 853  parselinearg(struct texi *p, size_t *pos)
Line 904  parselinearg(struct texi *p, size_t *pos)
         }          }
   
         if (*pos < BUFSZ(p) && '{' == BUF(p)[*pos])          if (*pos < BUFSZ(p) && '{' == BUF(p)[*pos])
                 parsebracket(p, pos);                  parsebracket(p, pos, 0);
         else if (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos])          else if (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos])
                 parsesingle(p, pos);                  parsesingle(p, pos);
         else          else
Line 905  texisplice(struct texi *p, const char *buf, size_t sz,
Line 956  texisplice(struct texi *p, const char *buf, size_t sz,
 void  void
 parseto(struct texi *p, size_t *pos, const char *endtoken)  parseto(struct texi *p, size_t *pos, const char *endtoken)
 {  {
         size_t            end;          size_t            end, sv;
         enum texicmd      cmd;          enum texicmd      cmd;
         size_t            endtoksz;          size_t            endtoksz;
         struct teximacro *macro;          struct teximacro *macro;
Line 932  parseto(struct texi *p, size_t *pos, const char *endto
Line 983  parseto(struct texi *p, size_t *pos, const char *endto
                         continue;                          continue;
                 }                  }
   
                   sv = p->files[p->filepos - 1].insplice;
                 cmd = texicmd(p, *pos, &end, &macro);                  cmd = texicmd(p, *pos, &end, &macro);
                 advanceto(p, pos, end);                  advanceto(p, pos, end);
                 if (TEXICMD_END == cmd) {                  if (TEXICMD_END == cmd) {
Line 952  parseto(struct texi *p, size_t *pos, const char *endto
Line 1004  parseto(struct texi *p, size_t *pos, const char *endto
                         continue;                          continue;
                 }                  }
                 if (NULL != macro)                  if (NULL != macro)
                         texiexecmacro(p, macro, pos);                          texiexecmacro(p, macro, sv, pos);
                 if (TEXICMD__MAX == cmd)                  if (TEXICMD__MAX == cmd)
                         continue;                          continue;
                 if (NULL != texitoks[cmd].fp)                  if (NULL != texitoks[cmd].fp)
Line 1209  argparse(struct texi *p, size_t *pos, size_t *argsz, s
Line 1261  argparse(struct texi *p, size_t *pos, size_t *argsz, s
         args = NULL;          args = NULL;
         *argsz = 0;          *argsz = 0;
   
           if (*pos == BUFSZ(p))
                   return(args);
   
         if ('{' != BUF(p)[*pos] && hint) {          if ('{' != BUF(p)[*pos] && hint) {
                 /*                  /*
                  * Special case: if we encounter an unbracketed argument                   * Special case: if we encounter an unbracketed argument
Line 1234  argparse(struct texi *p, size_t *pos, size_t *argsz, s
Line 1289  argparse(struct texi *p, size_t *pos, size_t *argsz, s
                 return(args);                  return(args);
         } else if ('{' != BUF(p)[*pos])          } else if ('{' != BUF(p)[*pos])
                 return(args);                  return(args);
   
           assert('{' == BUF(p)[*pos]);
   
         /* Parse til the closing '}', putting into the array. */          /* Parse til the closing '}', putting into the array. */
         advance(p, pos);          advance(p, pos);

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

CVSweb