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

Diff for /mandoc/roff.c between version 1.387 and 1.388

version 1.387, 2022/05/01 16:22:06 version 1.388, 2022/05/19 15:37:47
Line 207  static int   roff_evalpar(struct roff *, int,
Line 207  static int   roff_evalpar(struct roff *, int,
 static  int              roff_evalstrcond(const char *, int *);  static  int              roff_evalstrcond(const char *, int *);
 static  int              roff_expand(struct roff *, struct buf *,  static  int              roff_expand(struct roff *, struct buf *,
                                 int, int, char);                                  int, int, char);
   static  void             roff_expand_patch(struct buf *, int,
                                   const char *, int);
 static  void             roff_free1(struct roff *);  static  void             roff_free1(struct roff *);
 static  void             roff_freereg(struct roffreg *);  static  void             roff_freereg(struct roffreg *);
 static  void             roff_freestr(struct roffkv *);  static  void             roff_freestr(struct roffkv *);
Line 1233  deroff(char **dest, const struct roff_node *n)
Line 1235  deroff(char **dest, const struct roff_node *n)
   
 /* --- main functions of the roff parser ---------------------------------- */  /* --- main functions of the roff parser ---------------------------------- */
   
   /*
    * Save comments preceding the title macro, for example in order to
    * preserve Copyright and license headers in HTML output,
    * provide diagnostics about RCS ids and trailing whitespace in comments,
    * then discard comments including preceding whitespace.
    * This function also handles input line continuation.
    */
 static int  static int
 roff_parse_comment(struct roff *r, struct buf *buf, int ln, int pos,  roff_parse_comment(struct roff *r, struct buf *buf, int ln, int pos, char ec)
     char newesc)  
 {  {
         struct roff_node *n;    /* used for header comments */          struct roff_node *n;    /* used for header comments */
         const char      *start; /* start of the string to process */          const char      *start; /* start of the string to process */
Line 1245  roff_parse_comment(struct roff *r, struct buf *buf, in
Line 1253  roff_parse_comment(struct roff *r, struct buf *buf, in
         int              rcsid; /* kind of RCS id seen */          int              rcsid; /* kind of RCS id seen */
   
         for (start = stesc = buf->buf + pos;; stesc++) {          for (start = stesc = buf->buf + pos;; stesc++) {
                   /*
                    * XXX Ugly hack: Remove the newline character that
                    * mparse_buf_r() appended to mark the end of input
                    * if it is not preceded by an escape character.
                    */
                   if (stesc[0] == '\n') {
                           assert(stesc[1] == '\0');
                           stesc[0] = '\0';
                   }
   
                 /* The line ends without continuation or comment. */                  /* The line ends without continuation or comment. */
                 if (stesc[0] == '\0')                  if (stesc[0] == '\0')
                         return ROFF_CONT;                          return ROFF_CONT;
   
                 /* Unescaped byte: skip it. */                  /* Unescaped byte: skip it. */
                 if (stesc[0] != newesc)                  if (stesc[0] != ec)
                         continue;                          continue;
   
                 /* Backslash at end of line requests line continuation. */                  /*
                    * XXX Ugly hack: Do not attempt to append another line
                    * if the function mparse_buf_r() appended a newline
                    * character to indicate the end of input.
                    */
                   if (stesc[1] == '\n') {
                           assert(stesc[2] == '\0');
                           stesc[0] = '\0';
                           return ROFF_CONT;
                   }
   
                   /*
                    * An escape character at the end of an input line
                    * requests line continuation.
                    */
                 if (stesc[1] == '\0') {                  if (stesc[1] == '\0') {
                         stesc[0] = '\0';                          stesc[0] = '\0';
                         return ROFF_IGN | ROFF_APPEND;                          return ROFF_IGN | ROFF_APPEND;
Line 1264  roff_parse_comment(struct roff *r, struct buf *buf, in
Line 1296  roff_parse_comment(struct roff *r, struct buf *buf, in
                         break;                          break;
   
                 /* Escaped escape character: skip them both. */                  /* Escaped escape character: skip them both. */
                 if (stesc[1] == newesc)                  if (stesc[1] == ec)
                         stesc++;                          stesc++;
         }          }
   
Line 1331  roff_parse_comment(struct roff *r, struct buf *buf, in
Line 1363  roff_parse_comment(struct roff *r, struct buf *buf, in
  * which typically produce output glyphs or change formatter state.   * which typically produce output glyphs or change formatter state.
  */   */
 static int  static int
 roff_expand(struct roff *r, struct buf *buf, int ln, int pos, char newesc)  roff_expand(struct roff *r, struct buf *buf, int ln, int pos, char ec)
 {  {
         struct mctx     *ctx;   /* current macro call context */          char             ubuf[24];      /* buffer to print a number */
         char             ubuf[24]; /* buffer to print the number */          struct mctx     *ctx;           /* current macro call context */
         const char      *start; /* start of the string to process */          const char      *res;           /* the string to be pasted */
         char            *stesc; /* start of an escape sequence ('\\') */          const char      *src;           /* source for copying */
         const char      *esct;  /* type of esccape sequence */          char            *dst;           /* destination for copying */
         const char      *stnam; /* start of the name, after "[(*" */          int              iesc;          /* index of leading escape char */
         const char      *cp;    /* end of the name, e.g. before ']' */          int              inam;          /* index of the escape name */
         const char      *res;   /* the string to be substituted */          int              iarg;          /* index beginning the argument */
         char            *nbuf;  /* new buffer to copy buf->buf to */          int              iendarg;       /* index right after the argument */
         size_t           maxl;  /* expected length of the escape name */          int              iend;          /* index right after the sequence */
         size_t           naml;  /* actual length of the escape name */          int              deftype;       /* type of definition to paste */
         size_t           asz;   /* length of the replacement */          int              argi;          /* macro argument index */
         size_t           rsz;   /* length of the rest of the string */          int              quote_args;    /* true for \\$@, false for \\$* */
         int              inaml; /* length returned from mandoc_escape() */          int              asz;           /* length of the replacement */
           int              rsz;           /* length of the rest of the string */
           int              npos;          /* position in numeric expression */
         int              expand_count;  /* to avoid infinite loops */          int              expand_count;  /* to avoid infinite loops */
         int              npos;  /* position in numeric expression */  
         int              arg_complete; /* argument not interrupted by eol */  
         int              quote_args; /* true for \\$@, false for \\$* */  
         int              deftype; /* type of definition to paste */  
         enum mandocerr   err;   /* for escape sequence problems */  
         char             sign;  /* increment number register */  
         char             term;  /* character terminating the escape */  
   
         start = buf->buf + pos;  
         stesc = strchr(start, '\0') - 1;  
         if (stesc >= start && *stesc == '\n')  
                 *stesc-- = '\0';  
   
         expand_count = 0;          expand_count = 0;
         while (stesc >= start) {          while (buf->buf[pos] != '\0') {
                 if (*stesc != newesc) {  
   
                         /*                  /*
                          * If we have a non-standard escape character,                   * Skip plain ASCII characters.
                          * escape literal backslashes because all                   * If we have a non-standard escape character,
                          * processing in subsequent functions uses                   * escape literal backslashes because all processing in
                          * the standard escaping rules.                   * subsequent functions uses the standard escaping rules.
                          */                   */
   
                         if (newesc != ASCII_ESC && *stesc == '\\') {                  if (buf->buf[pos] != ec) {
                                 *stesc = '\0';                          if (ec != ASCII_ESC && buf->buf[pos] == '\\') {
                                 buf->sz = mandoc_asprintf(&nbuf, "%s\\e%s",                                  roff_expand_patch(buf, pos, "\\e", pos + 1);
                                     buf->buf, stesc + 1) + 1;                                  pos++;
                                 start = nbuf + pos;  
                                 stesc = nbuf + (stesc - buf->buf);  
                                 free(buf->buf);  
                                 buf->buf = nbuf;  
                         }                          }
                           pos++;
                         /* Search backwards for the next escape. */  
   
                         stesc--;  
                         continue;                          continue;
                 }                  }
   
                 /* If it is escaped, skip it. */                  /*
                    * Parse escape sequences,
                    * issue diagnostic messages when appropriate,
                    * and skip sequences that do not need expansion.
                    * If we have a non-standard escape character, translate
                    * it to backslashes and translate backslashes to \e.
                    */
   
                 for (cp = stesc - 1; cp >= start; cp--)                  if (roff_escape(buf->buf, ln, pos,
                         if (*cp != r->escape)                      &iesc, &iarg, &iendarg, &iend) != ESCAPE_EXPAND) {
                                 break;                          while (pos < iend) {
                                   if (buf->buf[pos] == ec) {
                 if ((stesc - cp) % 2 == 0) {                                          buf->buf[pos] = '\\';
                         while (stesc > cp)                                          if (pos + 1 < iend)
                                 *stesc-- = '\\';                                                  pos++;
                         continue;                                  } else if (buf->buf[pos] == '\\') {
                 } else if (stesc[1] == '\0') {                                          roff_expand_patch(buf,
                         *stesc-- = '\0';                                              pos, "\\e", pos + 1);
                         continue;                                          pos++;
                 } else                                          iend++;
                         *stesc = '\\';                                  }
                                   pos++;
                 /* Decide whether to expand or to check only. */  
   
                 term = '\0';  
                 cp = stesc + 1;  
                 while (*cp == 'E')  
                         cp++;  
                 esct = cp;  
                 switch (*esct) {  
                 case '*':  
                 case '$':  
                         res = NULL;  
                         break;  
                 case 'B':  
                 case 'w':  
                         term = cp[1];  
                         /* FALLTHROUGH */  
                 case 'n':  
                         sign = cp[1];  
                         if (sign == '+' || sign == '-')  
                                 cp++;  
                         res = ubuf;  
                         break;  
                 default:  
                         err = MANDOCERR_OK;  
                         switch(mandoc_escape(&cp, &stnam, &inaml)) {  
                         case ESCAPE_SPECIAL:  
                                 if (mchars_spec2cp(stnam, inaml) >= 0)  
                                         break;  
                                 /* FALLTHROUGH */  
                         case ESCAPE_ERROR:  
                                 err = MANDOCERR_ESC_BAD;  
                                 break;  
                         case ESCAPE_UNDEF:  
                                 err = MANDOCERR_ESC_UNDEF;  
                                 break;  
                         case ESCAPE_UNSUPP:  
                                 err = MANDOCERR_ESC_UNSUPP;  
                                 break;  
                         default:  
                                 break;  
                         }                          }
                         if (err != MANDOCERR_OK)  
                                 mandoc_msg(err, ln, (int)(stesc - buf->buf),  
                                     "%.*s", (int)(cp - stesc), stesc);  
                         stesc--;  
                         continue;                          continue;
                 }                  }
   
                 if (EXPAND_LIMIT < ++expand_count) {  
                         mandoc_msg(MANDOCERR_ROFFLOOP,  
                             ln, (int)(stesc - buf->buf), NULL);  
                         return ROFF_IGN;  
                 }  
   
                 /*                  /*
                  * The third character decides the length                   * Treat "\E" just like "\";
                  * of the name of the string or register.                   * it only makes a difference in copy mode.
                  * Save a pointer to the name.  
                  */                   */
   
                 if (term == '\0') {                  inam = iesc + 1;
                         switch (*++cp) {                  while (buf->buf[inam] == 'E')
                         case '\0':                          inam++;
                                 maxl = 0;  
                                 break;  
                         case '(':  
                                 cp++;  
                                 maxl = 2;  
                                 break;  
                         case '[':  
                                 cp++;  
                                 term = ']';  
                                 maxl = 0;  
                                 break;  
                         default:  
                                 maxl = 1;  
                                 break;  
                         }  
                 } else {  
                         cp += 2;  
                         maxl = 0;  
                 }  
                 stnam = cp;  
   
                 /* Advance to the end of the name. */                  /* Handle expansion. */
   
                 naml = 0;                  res = NULL;
                 arg_complete = 1;                  switch (buf->buf[inam]) {
                 while (maxl == 0 || naml < maxl) {                  case '*':
                         if (*cp == '\0') {                          if (iendarg == iarg)
                                 mandoc_msg(MANDOCERR_ESC_BAD, ln,  
                                     (int)(stesc - buf->buf), "%s", stesc);  
                                 arg_complete = 0;  
                                 break;                                  break;
                         }                          deftype = ROFFDEF_USER | ROFFDEF_PRE;
                         if (maxl == 0 && *cp == term) {                          if ((res = roff_getstrn(r, buf->buf + iarg,
                                 cp++;                              iendarg - iarg, &deftype)) != NULL)
                                 break;                                  break;
                         }  
                         if (*cp++ != '\\' || *esct != 'w') {  
                                 naml++;  
                                 continue;  
                         }  
                         switch (mandoc_escape(&cp, NULL, NULL)) {  
                         case ESCAPE_SPECIAL:  
                         case ESCAPE_UNICODE:  
                         case ESCAPE_NUMBERED:  
                         case ESCAPE_UNDEF:  
                         case ESCAPE_OVERSTRIKE:  
                                 naml++;  
                                 break;  
                         default:  
                                 break;  
                         }  
                 }  
   
                 /*                          /*
                  * Retrieve the replacement string; if it is                           * If not overriden,
                  * undefined, resume searching for escapes.                           * let \*(.T through to the formatters.
                  */                           */
   
                 switch (*esct) {                          if (iendarg - iarg == 2 &&
                 case '*':                              buf->buf[iarg] == '.' &&
                         if (arg_complete) {                              buf->buf[iarg + 1] == 'T') {
                                 deftype = ROFFDEF_USER | ROFFDEF_PRE;                                  roff_setstrn(&r->strtab, ".T", 2, NULL, 0, 0);
                                 res = roff_getstrn(r, stnam, naml, &deftype);                                  pos = iend;
                                   continue;
                                 /*  
                                  * If not overriden, let \*(.T  
                                  * through to the formatters.  
                                  */  
   
                                 if (res == NULL && naml == 2 &&  
                                     stnam[0] == '.' && stnam[1] == 'T') {  
                                         roff_setstrn(&r->strtab,  
                                             ".T", 2, NULL, 0, 0);  
                                         stesc--;  
                                         continue;  
                                 }  
                         }                          }
   
                           mandoc_msg(MANDOCERR_STR_UNDEF, ln, iesc,
                               "%.*s", iendarg - iarg, buf->buf + iarg);
                         break;                          break;
   
                 case '$':                  case '$':
                         if (r->mstackpos < 0) {                          if (r->mstackpos < 0) {
                                 mandoc_msg(MANDOCERR_ARG_UNDEF, ln,                                  mandoc_msg(MANDOCERR_ARG_UNDEF, ln, iesc,
                                     (int)(stesc - buf->buf), "%.3s", stesc);                                      "%.*s", iend - iesc, buf->buf + iesc);
                                 break;                                  break;
                         }                          }
                         ctx = r->mstack + r->mstackpos;                          ctx = r->mstack + r->mstackpos;
                         npos = esct[1] - '1';                          argi = buf->buf[iarg] - '1';
                         if (npos >= 0 && npos <= 8) {                          if (argi >= 0 && argi <= 8) {
                                 res = npos < ctx->argc ?                                  if (argi < ctx->argc)
                                     ctx->argv[npos] : "";                                          res = ctx->argv[argi];
                                 break;                                  break;
                         }                          }
                         if (esct[1] == '*')                          if (buf->buf[iarg] == '*')
                                 quote_args = 0;                                  quote_args = 0;
                         else if (esct[1] == '@')                          else if (buf->buf[iarg] == '@')
                                 quote_args = 1;                                  quote_args = 1;
                         else {                          else {
                                 mandoc_msg(MANDOCERR_ARG_NONUM, ln,                                  mandoc_msg(MANDOCERR_ARG_NONUM, ln, iesc,
                                     (int)(stesc - buf->buf), "%.3s", stesc);                                      "%.*s", iend - iesc, buf->buf + iesc);
                                 break;                                  break;
                         }                          }
                         asz = 0;                          asz = 0;
                         for (npos = 0; npos < ctx->argc; npos++) {                          for (argi = 0; argi < ctx->argc; argi++) {
                                 if (npos)                                  if (argi)
                                         asz++;  /* blank */                                          asz++;  /* blank */
                                 if (quote_args)                                  if (quote_args)
                                         asz += 2;  /* quotes */                                          asz += 2;  /* quotes */
                                 asz += strlen(ctx->argv[npos]);                                  asz += strlen(ctx->argv[argi]);
                         }                          }
                         if (asz != 3) {                          if (asz != iend - iesc) {
                                 rsz = buf->sz - (stesc - buf->buf) - 3;                                  rsz = buf->sz - iend;
                                 if (asz < 3)                                  if (asz < iend - iesc)
                                         memmove(stesc + asz, stesc + 3, rsz);                                          memmove(buf->buf + iesc + asz,
                                 buf->sz += asz - 3;                                              buf->buf + iend, rsz);
                                 nbuf = mandoc_realloc(buf->buf, buf->sz);                                  buf->sz = iesc + asz + rsz;
                                 start = nbuf + pos;                                  buf->buf = mandoc_realloc(buf->buf, buf->sz);
                                 stesc = nbuf + (stesc - buf->buf);                                  if (asz > iend - iesc)
                                 buf->buf = nbuf;                                          memmove(buf->buf + iesc + asz,
                                 if (asz > 3)                                              buf->buf + iend, rsz);
                                         memmove(stesc + asz, stesc + 3, rsz);  
                         }                          }
                         for (npos = 0; npos < ctx->argc; npos++) {                          dst = buf->buf + iesc;
                                 if (npos)                          for (argi = 0; argi < ctx->argc; argi++) {
                                         *stesc++ = ' ';                                  if (argi)
                                           *dst++ = ' ';
                                 if (quote_args)                                  if (quote_args)
                                         *stesc++ = '"';                                          *dst++ = '"';
                                 cp = ctx->argv[npos];                                  src = ctx->argv[argi];
                                 while (*cp != '\0')                                  while (*src != '\0')
                                         *stesc++ = *cp++;                                          *dst++ = *src++;
                                 if (quote_args)                                  if (quote_args)
                                         *stesc++ = '"';                                          *dst++ = '"';
                         }                          }
                         continue;                          continue;
                 case 'B':                  case 'B':
                         npos = 0;                          npos = 0;
                         ubuf[0] = arg_complete &&                          ubuf[0] = iendarg > iarg && iend > iendarg &&
                             roff_evalnum(r, ln, stnam, &npos,                              roff_evalnum(r, ln, buf->buf + iarg, &npos,
                               NULL, ROFFNUM_SCALE) &&                                           NULL, ROFFNUM_SCALE) &&
                             stnam + npos + 1 == cp ? '1' : '0';                              npos == iendarg - iarg ? '1' : '0';
                         ubuf[1] = '\0';                          ubuf[1] = '\0';
                           res = ubuf;
                         break;                          break;
                 case 'n':                  case 'n':
                         if (arg_complete)                          if (iendarg > iarg)
                                 (void)snprintf(ubuf, sizeof(ubuf), "%d",                                  (void)snprintf(ubuf, sizeof(ubuf), "%d",
                                     roff_getregn(r, stnam, naml, sign));                                      roff_getregn(r, buf->buf + iarg,
                                       iendarg - iarg, buf->buf[inam + 1]));
                         else                          else
                                 ubuf[0] = '\0';                                  ubuf[0] = '\0';
                           res = ubuf;
                         break;                          break;
                 case 'w':                  case 'w':
                         /* use even incomplete args */                          (void)snprintf(ubuf, sizeof(ubuf),
                         (void)snprintf(ubuf, sizeof(ubuf), "%d",                              "%d", (iendarg - iarg) * 24);
                             24 * (int)naml);                          res = ubuf;
                         break;                          break;
                   default:
                           break;
                 }                  }
                   if (res == NULL)
                 if (res == NULL) {  
                         if (*esct == '*')  
                                 mandoc_msg(MANDOCERR_STR_UNDEF,  
                                     ln, (int)(stesc - buf->buf),  
                                     "%.*s", (int)naml, stnam);  
                         res = "";                          res = "";
                 } else if (buf->sz + strlen(res) > SHRT_MAX) {                  if (++expand_count > EXPAND_LIMIT ||
                         mandoc_msg(MANDOCERR_ROFFLOOP,                      buf->sz + strlen(res) > SHRT_MAX) {
                             ln, (int)(stesc - buf->buf), NULL);                          mandoc_msg(MANDOCERR_ROFFLOOP, ln, iesc, NULL);
                         return ROFF_IGN;                          return ROFF_IGN;
                 }                  }
                   roff_expand_patch(buf, iesc, res, iend);
                 /* Replace the escape sequence by the string. */  
   
                 *stesc = '\0';  
                 buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",  
                     buf->buf, res, cp) + 1;  
   
                 /* Prepare for the next replacement. */  
   
                 start = nbuf + pos;  
                 stesc = nbuf + (stesc - buf->buf) + strlen(res);  
                 free(buf->buf);  
                 buf->buf = nbuf;  
         }          }
         return ROFF_CONT;          return ROFF_CONT;
   }
   
   /*
    * Replace the substring from the start position (inclusive)
    * to end position (exclusive) with the repl(acement) string.
    */
   static void
   roff_expand_patch(struct buf *buf, int start, const char *repl, int end)
   {
           char    *nbuf;
   
           buf->buf[start] = '\0';
           buf->sz = mandoc_asprintf(&nbuf, "%s%s%s", buf->buf, repl,
               buf->buf + end) + 1;
           free(buf->buf);
           buf->buf = nbuf;
 }  }
   
 /*  /*

Legend:
Removed from v.1.387  
changed lines
  Added in v.1.388

CVSweb