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

Diff for /texi2mdoc/util.c between version 1.7 and 1.10

version 1.7, 2015/02/23 11:44:30 version 1.10, 2015/02/23 15:09:09
Line 170  texiputchar(struct texi *p, char c)
Line 170  texiputchar(struct texi *p, char c)
   
         if ('.' == c && 0 == p->outcol)          if ('.' == c && 0 == p->outcol)
                 fputs("\\&", stdout);                  fputs("\\&", stdout);
           if ('\'' == c && 0 == p->outcol)
                   fputs("\\&", stdout);
   
         putchar(c);          putchar(c);
         p->seenvs = 0;          p->seenvs = 0;
Line 193  texiputchars(struct texi *p, const char *s)
Line 195  texiputchars(struct texi *p, const char *s)
 }  }
   
 /*  /*
    * This puts all characters onto the output stream but makes sure to
    * escape mdoc(7) slashes.
    */
   void
   texiputbuf(struct texi *p, const char *buf, size_t start, size_t end)
   {
   
           for ( ; start < end; start++) {
                   texiputchar(p, buf[start]);
                   if ('\\' == buf[start])
                           texiputchar(p, 'e');
           }
   }
   
   /*
  * Close an mdoc(7) macro opened with teximacroopen().   * Close an mdoc(7) macro opened with teximacroopen().
  * If there are no more macros on the line, prints a newline.   * If there are no more macros on the line, prints a newline.
  */   */
Line 409  texiexecmacro(struct texi *p, struct teximacro *m,
Line 426  texiexecmacro(struct texi *p, struct teximacro *m,
         char     *val;          char     *val;
         char    **args;          char    **args;
   
         args = argparse(p, buf, sz, pos, &asz);          args = argparse(p, buf, sz, 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) {
                 parseeof(p, m->value, strlen(m->value));                  parsemembuf(p, m->value, strlen(m->value));
                 return;                  return;
         }          }
   
Line 481  texiexecmacro(struct texi *p, struct teximacro *m,
Line 498  texiexecmacro(struct texi *p, struct teximacro *m,
                 i = end;                  i = end;
         }          }
   
         parseeof(p, val, strlen(val));          parsemembuf(p, val, strlen(val));
   
         for (i = 0; i < asz; i++)          for (i = 0; i < asz; i++)
                 free(args[i]);                  free(args[i]);
Line 527  texiword(struct texi *p, const char *buf, 
Line 544  texiword(struct texi *p, const char *buf, 
                          '\'' == buf[*pos + 1]) {                           '\'' == buf[*pos + 1]) {
                         texiputchars(p, "\\(rq");                          texiputchars(p, "\\(rq");
                         advance(p, buf, pos);                          advance(p, buf, pos);
                   } else if ('\\' == buf[*pos]) {
                           texiputchar(p, buf[*pos]);
                           texiputchar(p, 'e');
                 } else                  } else
                         texiputchar(p, buf[*pos]);                          texiputchar(p, buf[*pos]);
                 advance(p, buf, pos);                  advance(p, buf, pos);
Line 839  parseeof(struct texi *p, const char *buf, size_t sz)
Line 859  parseeof(struct texi *p, const char *buf, size_t sz)
 }  }
   
 /*  /*
    * This is like parseeof() except that it's to be invoked on memory
    * buffers while parsing a larger scope.
    * This is useful for parsing macro sequences.
    * The line, column, and name of the calling file context are saved, the
    * column and line reset, then all of these restored after parse.
    */
   void
   parsemembuf(struct texi *p, const char *buf, size_t sz)
   {
           size_t           svln, svcol;
           const char      *svname;
   
           svln = p->files[p->filepos - 1].line;
           svcol = p->files[p->filepos - 1].col;
           svname = p->files[p->filepos - 1].name;
   
           p->files[p->filepos - 1].line = 0;
           p->files[p->filepos - 1].col = 0;
           p->files[p->filepos - 1].name = "<macro buffer>";
   
           parseeof(p, buf, sz);
   
           p->files[p->filepos - 1].line = svln;
           p->files[p->filepos - 1].col = svcol;
           p->files[p->filepos - 1].name = svname;
   }
   
   /*
  * Parse a block sequence until we have the "@end endtoken" command   * Parse a block sequence until we have the "@end endtoken" command
  * invocation.   * invocation.
  * This will return immediately at EOF.   * This will return immediately at EOF.
Line 1104  valueadd(struct texi *p, char *key, char *val)
Line 1152  valueadd(struct texi *p, char *key, char *val)
  */   */
 char **  char **
 argparse(struct texi *p, const char *buf,  argparse(struct texi *p, const char *buf,
         size_t sz, size_t *pos, size_t *argsz)          size_t sz, size_t *pos, size_t *argsz, size_t hint)
 {  {
         char    **args;          char    **args;
         size_t    start, end, stack;          size_t    start, end, stack;
Line 1115  argparse(struct texi *p, const char *buf, 
Line 1163  argparse(struct texi *p, const char *buf, 
         args = NULL;          args = NULL;
         *argsz = 0;          *argsz = 0;
   
         /* Check for no arguments. */          if ('{' != buf[*pos] && hint) {
         if ('{' != buf[*pos])                  /*
                    * Special case: if we encounter an unbracketed argument
                    * and we're being invoked with non-zero arguments
                    * (versus being set, i.e., hint>0), then parse until
                    * the end of line.
                    */
                   *argsz = 1;
                   args = calloc(1, sizeof(char *));
                   if (NULL == args)
                           texiabort(p, NULL);
                   start = *pos;
                   while (*pos < sz) {
                           if ('\n' == buf[*pos])
                                   break;
                           advance(p, buf, pos);
                   }
                   args[0] = malloc(*pos - start + 1);
                   memcpy(args[0], &buf[start], *pos - start);
                   args[0][*pos - start] = '\0';
                   if (*pos < sz && '\n' == buf[*pos])
                           advance(p, buf, pos);
                 return(args);                  return(args);
           } else if ('{' != buf[*pos])
                   return(args);
   
         /* Parse til the closing '}', putting into the array. */          /* Parse til the closing '}', putting into the array. */
         advance(p, buf, pos);          advance(p, buf, pos);
Line 1133  argparse(struct texi *p, const char *buf, 
Line 1203  argparse(struct texi *p, const char *buf, 
                          * We keep track of embedded-ness in the "stack"                           * We keep track of embedded-ness in the "stack"
                          * state anyway, so this is free.                           * state anyway, so this is free.
                          */                           */
                         if (0 == stack && ',' == buf[*pos])                          if (',' == buf[*pos] && 0 == stack && 1 != hint)
                                 break;                                  break;
                         else if (0 == stack && '}' == buf[*pos])                          else if (0 == stack && '}' == buf[*pos])
                                 break;                                  break;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.10

CVSweb