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

Diff for /mandoc/roff.c between version 1.262 and 1.263

version 1.262, 2015/02/17 18:09:14 version 1.263, 2015/02/21 14:46:58
Line 2653  roff_so(ROFF_ARGS)
Line 2653  roff_so(ROFF_ARGS)
 static enum rofferr  static enum rofferr
 roff_userdef(ROFF_ARGS)  roff_userdef(ROFF_ARGS)
 {  {
         const char       *arg[9];          const char       *arg[9], *ap;
         char             *cp, *n1, *n2;          char             *cp, *n1, *n2;
         int               i;          int               i;
           size_t            asz, rsz;
   
         /*          /*
          * Collect pointers to macro argument strings           * Collect pointers to macro argument strings
          * and NUL-terminate them.           * and NUL-terminate them.
          */           */
   
         cp = buf->buf + pos;          cp = buf->buf + pos;
         for (i = 0; i < 9; i++)          for (i = 0; i < 9; i++)
                 arg[i] = *cp == '\0' ? "" :                  arg[i] = *cp == '\0' ? "" :
Line 2669  roff_userdef(ROFF_ARGS)
Line 2671  roff_userdef(ROFF_ARGS)
         /*          /*
          * Expand macro arguments.           * Expand macro arguments.
          */           */
         buf->sz = 0;  
         n1 = cp = mandoc_strdup(r->current_string);          buf->sz = strlen(r->current_string) + 1;
         while ((cp = strstr(cp, "\\$")) != NULL) {          n1 = cp = mandoc_malloc(buf->sz);
                 i = cp[2] - '1';          memcpy(n1, r->current_string, buf->sz);
                 if (0 > i || 8 < i) {          while (*cp != '\0') {
                         /* Not an argument invocation. */  
                         cp += 2;                  /* Scan ahead for the next argument invocation. */
   
                   if (*cp++ != '\\')
                         continue;                          continue;
                   if (*cp++ != '$')
                           continue;
                   i = *cp - '1';
                   if (0 > i || 8 < i)
                           continue;
                   cp -= 2;
   
                   /*
                    * Determine the size of the expanded argument,
                    * taking escaping of quotes into account.
                    */
   
                   asz = 0;
                   for (ap = arg[i]; *ap != '\0'; ap++) {
                           asz++;
                           if (*ap == '"')
                                   asz += 3;
                 }                  }
                 *cp = '\0';                  if (asz != 3) {
                 buf->sz = mandoc_asprintf(&n2, "%s%s%s",  
                     n1, arg[i], cp + 3) + 1;                          /*
                 cp = n2 + (cp - n1);                           * Determine the size of the rest of the
                 free(n1);                           * unexpanded macro, including the NUL.
                 n1 = n2;                           */
   
                           rsz = buf->sz - (cp - n1) - 3;
   
                           /*
                            * When shrinking, move before
                            * releasing the storage.
                            */
   
                           if (asz < 3)
                                   memmove(cp + asz, cp + 3, rsz);
   
                           /*
                            * Resize the storage for the macro
                            * and readjust the parse pointer.
                            */
   
                           buf->sz += asz - 3;
                           n2 = mandoc_realloc(n1, buf->sz);
                           cp = n2 + (cp - n1);
                           n1 = n2;
   
                           /*
                            * When growing, make room
                            * for the expanded argument.
                            */
   
                           if (asz > 3)
                                   memmove(cp + asz, cp + 3, rsz);
                   }
   
                   /* Copy the expanded argument, escaping quotes. */
   
                   n2 = cp;
                   for (ap = arg[i]; *ap != '\0'; ap++) {
                           if (*ap == '"') {
                                   memcpy(n2, "\\(dq", 4);
                                   n2 += 4;
                           } else
                                   *n2++ = *ap;
                   }
         }          }
   
         /*          /*
          * Replace the macro invocation           * Replace the macro invocation
          * by the expanded macro.           * by the expanded macro.
          */           */
   
         free(buf->buf);          free(buf->buf);
         buf->buf = n1;          buf->buf = n1;
         if (buf->sz == 0)  
                 buf->sz = strlen(buf->buf) + 1;  
         *offs = 0;          *offs = 0;
   
         return(buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?          return(buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?

Legend:
Removed from v.1.262  
changed lines
  Added in v.1.263

CVSweb