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

Diff for /mandoc/mandoc.c between version 1.94 and 1.105

version 1.94, 2015/10/06 18:32:19 version 1.105, 2018/08/10 22:12:44
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org>   * Copyright (c) 2011-2015, 2017, 2018 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
Line 28 
Line 28 
 #include <string.h>  #include <string.h>
 #include <time.h>  #include <time.h>
   
 #include "mandoc.h"  
 #include "mandoc_aux.h"  #include "mandoc_aux.h"
   #include "mandoc.h"
   #include "roff.h"
 #include "libmandoc.h"  #include "libmandoc.h"
   
 #define DATESIZE 32  
   
 static  int      a2time(time_t *, const char *, const char *);  static  int      a2time(time_t *, const char *, const char *);
 static  char    *time2a(time_t);  static  char    *time2a(time_t);
   
Line 42  enum mandoc_esc
Line 41  enum mandoc_esc
 mandoc_escape(const char **end, const char **start, int *sz)  mandoc_escape(const char **end, const char **start, int *sz)
 {  {
         const char      *local_start;          const char      *local_start;
         int              local_sz;          int              local_sz, c, i;
         char             term;          char             term;
         enum mandoc_esc  gly;          enum mandoc_esc  gly;
   
Line 93  mandoc_escape(const char **end, const char **start, in
Line 92  mandoc_escape(const char **end, const char **start, in
          * Escapes taking no arguments at all.           * Escapes taking no arguments at all.
          */           */
         case 'd':          case 'd':
                 /* FALLTHROUGH */  
         case 'u':          case 'u':
                 /* FALLTHROUGH */  
         case ',':          case ',':
                 /* FALLTHROUGH */  
         case '/':          case '/':
                 return ESCAPE_IGNORE;                  return ESCAPE_IGNORE;
           case 'p':
                   return ESCAPE_BREAK;
   
         /*          /*
          * The \z escape is supposed to output the following           * The \z escape is supposed to output the following
Line 115  mandoc_escape(const char **end, const char **start, in
Line 113  mandoc_escape(const char **end, const char **start, in
          * 'X' is the trigger.  These have opaque sub-strings.           * 'X' is the trigger.  These have opaque sub-strings.
          */           */
         case 'F':          case 'F':
                 /* FALLTHROUGH */  
         case 'g':          case 'g':
                 /* FALLTHROUGH */  
         case 'k':          case 'k':
                 /* FALLTHROUGH */  
         case 'M':          case 'M':
                 /* FALLTHROUGH */  
         case 'm':          case 'm':
                 /* FALLTHROUGH */  
         case 'n':          case 'n':
                 /* FALLTHROUGH */  
         case 'V':          case 'V':
                 /* FALLTHROUGH */  
         case 'Y':          case 'Y':
                 gly = ESCAPE_IGNORE;                  gly = ESCAPE_IGNORE;
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
Line 155  mandoc_escape(const char **end, const char **start, in
Line 146  mandoc_escape(const char **end, const char **start, in
          * The \B and \w escapes are handled in roff.c, roff_res().           * The \B and \w escapes are handled in roff.c, roff_res().
          */           */
         case 'A':          case 'A':
                 /* FALLTHROUGH */  
         case 'b':          case 'b':
                 /* FALLTHROUGH */  
         case 'D':          case 'D':
                 /* FALLTHROUGH */  
         case 'R':          case 'R':
                 /* FALLTHROUGH */  
         case 'X':          case 'X':
                 /* FALLTHROUGH */  
         case 'Z':          case 'Z':
                 gly = ESCAPE_IGNORE;                  gly = ESCAPE_IGNORE;
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
Line 181  mandoc_escape(const char **end, const char **start, in
Line 167  mandoc_escape(const char **end, const char **start, in
          * and 'N' resolves to a numerical expression.           * and 'N' resolves to a numerical expression.
          */           */
         case 'h':          case 'h':
                 /* FALLTHROUGH */  
         case 'H':          case 'H':
                 /* FALLTHROUGH */  
         case 'L':          case 'L':
                 /* FALLTHROUGH */  
         case 'l':          case 'l':
                 /* FALLTHROUGH */  
         case 'S':          case 'S':
                 /* FALLTHROUGH */  
         case 'v':          case 'v':
                 /* FALLTHROUGH */  
         case 'x':          case 'x':
                 if (strchr(" %&()*+-./0123456789:<=>", **start)) {                  if (strchr(" %&()*+-./0123456789:<=>", **start)) {
                         if ('\0' != **start)                          if ('\0' != **start)
                                 ++*end;                                  ++*end;
                         return ESCAPE_ERROR;                          return ESCAPE_ERROR;
                 }                  }
                 gly = ESCAPE_IGNORE;                  switch ((*start)[-1]) {
                   case 'h':
                           gly = ESCAPE_HORIZ;
                           break;
                   case 'l':
                           gly = ESCAPE_HLINE;
                           break;
                   default:
                           gly = ESCAPE_IGNORE;
                           break;
                   }
                 term = **start;                  term = **start;
                 *start = ++*end;                  *start = ++*end;
                 break;                  break;
Line 247  mandoc_escape(const char **end, const char **start, in
Line 237  mandoc_escape(const char **end, const char **start, in
                         term = '\'';                          term = '\'';
                         break;                          break;
                 case '3':                  case '3':
                         /* FALLTHROUGH */  
                 case '2':                  case '2':
                         /* FALLTHROUGH */  
                 case '1':                  case '1':
                         *sz = (*end)[-1] == 's' &&                          *sz = (*end)[-1] == 's' &&
                             isdigit((unsigned char)(*end)[1]) ? 2 : 1;                              isdigit((unsigned char)(*end)[1]) ? 2 : 1;
Line 325  mandoc_escape(const char **end, const char **start, in
Line 313  mandoc_escape(const char **end, const char **start, in
   
                 switch (**start) {                  switch (**start) {
                 case '3':                  case '3':
                         /* FALLTHROUGH */  
                 case 'B':                  case 'B':
                         gly = ESCAPE_FONTBOLD;                          gly = ESCAPE_FONTBOLD;
                         break;                          break;
                 case '2':                  case '2':
                         /* FALLTHROUGH */  
                 case 'I':                  case 'I':
                         gly = ESCAPE_FONTITALIC;                          gly = ESCAPE_FONTITALIC;
                         break;                          break;
Line 338  mandoc_escape(const char **end, const char **start, in
Line 324  mandoc_escape(const char **end, const char **start, in
                         gly = ESCAPE_FONTPREV;                          gly = ESCAPE_FONTPREV;
                         break;                          break;
                 case '1':                  case '1':
                         /* FALLTHROUGH */  
                 case 'R':                  case 'R':
                         gly = ESCAPE_FONTROMAN;                          gly = ESCAPE_FONTROMAN;
                         break;                          break;
                 }                  }
                 break;                  break;
         case ESCAPE_SPECIAL:          case ESCAPE_SPECIAL:
                 if (1 == *sz && 'c' == **start)                  if (**start == 'c') {
                         gly = ESCAPE_NOSPACE;                          if (*sz == 1) {
                                   gly = ESCAPE_NOSPACE;
                                   break;
                           }
                           if (*sz < 6 || *sz > 7 ||
                               strncmp(*start, "char", 4) != 0 ||
                               (int)strspn(*start + 4, "0123456789") + 4 < *sz)
                                   break;
                           c = 0;
                           for (i = 4; i < *sz; i++)
                                   c = 10 * c + ((*start)[i] - '0');
                           if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff)
                                   break;
                           *start += 4;
                           *sz -= 4;
                           gly = ESCAPE_NUMBERED;
                           break;
                   }
   
                 /*                  /*
                  * Unicode escapes are defined in groff as \[u0000]                   * Unicode escapes are defined in groff as \[u0000]
                  * to \[u10FFFF], where the contained value must be                   * to \[u10FFFF], where the contained value must be
Line 359  mandoc_escape(const char **end, const char **start, in
Line 362  mandoc_escape(const char **end, const char **start, in
                         break;                          break;
                 if (*sz == 6 && (*start)[1] == '0')                  if (*sz == 6 && (*start)[1] == '0')
                         break;                          break;
                   if (*sz == 5 && (*start)[1] == 'D' &&
                       strchr("89ABCDEF", (*start)[2]) != NULL)
                           break;
                 if ((int)strspn(*start + 1, "0123456789ABCDEFabcdef")                  if ((int)strspn(*start + 1, "0123456789ABCDEFabcdef")
                     + 1 == *sz)                      + 1 == *sz)
                         gly = ESCAPE_UNICODE;                          gly = ESCAPE_UNICODE;
Line 503  time2a(time_t t)
Line 509  time2a(time_t t)
          * up to 2 characters for the day + comma + blank           * up to 2 characters for the day + comma + blank
          * 4 characters for the year and a terminating '\0'           * 4 characters for the year and a terminating '\0'
          */           */
   
         p = buf = mandoc_malloc(10 + 4 + 4 + 1);          p = buf = mandoc_malloc(10 + 4 + 4 + 1);
   
         if (0 == (ssz = strftime(p, 10 + 1, "%B ", tm)))          if ((ssz = strftime(p, 10 + 1, "%B ", tm)) == 0)
                 goto fail;                  goto fail;
         p += (int)ssz;          p += (int)ssz;
   
         if (-1 == (isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)))          /*
            * The output format is just "%d" here, not "%2d" or "%02d".
            * That's also the reason why we can't just format the
            * date as a whole with "%B %e, %Y" or "%B %d, %Y".
            * Besides, the present approach is less prone to buffer
            * overflows, in case anybody should ever introduce the bug
            * of looking at LC_TIME.
            */
   
           if ((isz = snprintf(p, 4 + 1, "%d, ", tm->tm_mday)) == -1)
                 goto fail;                  goto fail;
         p += isz;          p += isz;
   
         if (0 == strftime(p, 4 + 1, "%Y", tm))          if (strftime(p, 4 + 1, "%Y", tm) == 0)
                 goto fail;                  goto fail;
         return buf;          return buf;
   
Line 523  fail:
Line 539  fail:
 }  }
   
 char *  char *
 mandoc_normdate(struct mparse *parse, char *in, int ln, int pos)  mandoc_normdate(struct roff_man *man, char *in, int ln, int pos)
 {  {
         char            *out;          char            *cp;
         time_t           t;          time_t           t;
   
         if (NULL == in || '\0' == *in ||          /* No date specified: use today's date. */
             0 == strcmp(in, "$" "Mdocdate$")) {  
                 mandoc_msg(MANDOCERR_DATE_MISSING, parse, ln, pos, NULL);          if (in == NULL || *in == '\0' || strcmp(in, "$" "Mdocdate$") == 0) {
                 time(&t);                  mandoc_msg(MANDOCERR_DATE_MISSING, man->parse, ln, pos, NULL);
                   return time2a(time(NULL));
         }          }
         else if (a2time(&t, "%Y-%m-%d", in))  
                 t = 0;          /* Valid mdoc(7) date format. */
         else if (!a2time(&t, "$" "Mdocdate: %b %d %Y $", in) &&  
             !a2time(&t, "%b %d, %Y", in)) {          if (a2time(&t, "$" "Mdocdate: %b %d %Y $", in) ||
                 mandoc_msg(MANDOCERR_DATE_BAD, parse, ln, pos, in);              a2time(&t, "%b %d, %Y", in)) {
                 t = 0;                  cp = time2a(t);
                   if (t > time(NULL) + 86400)
                           mandoc_msg(MANDOCERR_DATE_FUTURE, man->parse,
                               ln, pos, cp);
                   else if (*in != '$' && strcmp(in, cp) != 0)
                           mandoc_msg(MANDOCERR_DATE_NORM, man->parse,
                               ln, pos, cp);
                   return cp;
         }          }
         out = t ? time2a(t) : NULL;  
         return out ? out : mandoc_strdup(in);          /* In man(7), do not warn about the legacy format. */
   
           if (a2time(&t, "%Y-%m-%d", in) == 0)
                   mandoc_msg(MANDOCERR_DATE_BAD, man->parse, ln, pos, in);
           else if (t > time(NULL) + 86400)
                   mandoc_msg(MANDOCERR_DATE_FUTURE, man->parse, ln, pos, in);
           else if (man->macroset == MACROSET_MDOC)
                   mandoc_vmsg(MANDOCERR_DATE_LEGACY, man->parse,
                       ln, pos, "Dd %s", in);
   
           /* Use any non-mdoc(7) date verbatim. */
   
           return mandoc_strdup(in);
 }  }
   
 int  int
Line 563  mandoc_eos(const char *p, size_t sz)
Line 599  mandoc_eos(const char *p, size_t sz)
         for (q = p + (int)sz - 1; q >= p; q--) {          for (q = p + (int)sz - 1; q >= p; q--) {
                 switch (*q) {                  switch (*q) {
                 case '\"':                  case '\"':
                         /* FALLTHROUGH */  
                 case '\'':                  case '\'':
                         /* FALLTHROUGH */  
                 case ']':                  case ']':
                         /* FALLTHROUGH */  
                 case ')':                  case ')':
                         if (0 == found)                          if (0 == found)
                                 enclosed = 1;                                  enclosed = 1;
                         break;                          break;
                 case '.':                  case '.':
                         /* FALLTHROUGH */  
                 case '!':                  case '!':
                         /* FALLTHROUGH */  
                 case '?':                  case '?':
                         found = 1;                          found = 1;
                         break;                          break;

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

CVSweb