[BACK]Return to vis.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/lib/libc/gen/vis.c between version 1.41 and 1.42

version 1.41, 2009/11/23 10:08:47 version 1.42, 2011/03/12 03:24:08
Line 82  __weak_alias(vis,_vis)
Line 82  __weak_alias(vis,_vis)
 #include <stdio.h>  #include <stdio.h>
 #include <string.h>  #include <string.h>
   
 static char *do_svis(char *, int, int, int, const char *);  static char *do_svis(char *, size_t *, int, int, int, const char *);
   
 #undef BELL  #undef BELL
 #define BELL '\a'  #define BELL '\a'
Line 118  do {               \
Line 118  do {               \
  * This is do_hvis, for HTTP style (RFC 1808)   * This is do_hvis, for HTTP style (RFC 1808)
  */   */
 static char *  static char *
 do_hvis(char *dst, int c, int flag, int nextc, const char *extra)  do_hvis(char *dst, size_t *len, int c, int flag, int nextc, const char *extra)
 {  {
   
         if ((isascii(c) && isalnum(c))          if ((isascii(c) && isalnum(c))
Line 127  do_hvis(char *dst, int c, int flag, int 
Line 127  do_hvis(char *dst, int c, int flag, int 
             /* extra */              /* extra */
             || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'              || c == '!' || c == '*' || c == '\'' || c == '(' || c == ')'
             || c == ',') {              || c == ',') {
                 dst = do_svis(dst, c, flag, nextc, extra);                  dst = do_svis(dst, len, c, flag, nextc, extra);
         } else {          } else {
                   if (len) {
                           if (*len < 3)
                                   return NULL;
                           *len -= 3;
                   }
                 *dst++ = '%';                  *dst++ = '%';
                 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);                  *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
                 *dst++ = xtoa((unsigned int)c & 0xf);                  *dst++ = xtoa((unsigned int)c & 0xf);
Line 142  do_hvis(char *dst, int c, int flag, int 
Line 147  do_hvis(char *dst, int c, int flag, int 
  * NB: No handling of long lines or CRLF.   * NB: No handling of long lines or CRLF.
  */   */
 static char *  static char *
 do_mvis(char *dst, int c, int flag, int nextc, const char *extra)  do_mvis(char *dst, size_t *len, int c, int flag, int nextc, const char *extra)
 {  {
         if ((c != '\n') &&          if ((c != '\n') &&
             /* Space at the end of the line */              /* Space at the end of the line */
Line 151  do_mvis(char *dst, int c, int flag, int 
Line 156  do_mvis(char *dst, int c, int flag, int 
             (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||              (!isspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
             /* Specific char to be escaped */              /* Specific char to be escaped */
             strchr("#$@[\\]^`{|}~", c) != NULL)) {              strchr("#$@[\\]^`{|}~", c) != NULL)) {
                   if (len) {
                           if (*len < 3)
                                   return NULL;
                           *len -= 3;
                   }
                 *dst++ = '=';                  *dst++ = '=';
                 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);                  *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
                 *dst++ = XTOA((unsigned int)c & 0xf);                  *dst++ = XTOA((unsigned int)c & 0xf);
         } else {          } else {
                 dst = do_svis(dst, c, flag, nextc, extra);                  dst = do_svis(dst, len, c, flag, nextc, extra);
         }          }
         return dst;          return dst;
 }  }
Line 170  do_mvis(char *dst, int c, int flag, int 
Line 180  do_mvis(char *dst, int c, int flag, int 
  *            backslash-protected.   *            backslash-protected.
  */   */
 static char *  static char *
 do_svis(char *dst, int c, int flag, int nextc, const char *extra)  do_svis(char *dst, size_t *len, int c, int flag, int nextc, const char *extra)
 {  {
         int isextra;          int isextra;
         isextra = strchr(extra, c) != NULL;          isextra = strchr(extra, c) != NULL;
           size_t olen = len ? *len : 0;
   #define HAVE(x) \
           do { \
                   if (len) { \
                           if (*len < (x)) \
                                   goto out; \
                           *len -= (x); \
                   } \
           } while (/*CONSTCOND*/0)
         if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||          if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||
             ((flag & VIS_SAFE) && issafe(c)))) {              ((flag & VIS_SAFE) && issafe(c)))) {
                   HAVE(1);
                 *dst++ = c;                  *dst++ = c;
                 return dst;                  return dst;
         }          }
         if (flag & VIS_CSTYLE) {          if (flag & VIS_CSTYLE) {
                   HAVE(2);
                 switch (c) {                  switch (c) {
                 case '\n':                  case '\n':
                         *dst++ = '\\'; *dst++ = 'n';                          *dst++ = '\\'; *dst++ = 'n';
Line 208  do_svis(char *dst, int c, int flag, int 
Line 229  do_svis(char *dst, int c, int flag, int 
                 case '\0':                  case '\0':
                         *dst++ = '\\'; *dst++ = '0';                          *dst++ = '\\'; *dst++ = '0';
                         if (isoctal(nextc)) {                          if (isoctal(nextc)) {
                                   HAVE(2);
                                 *dst++ = '0';                                  *dst++ = '0';
                                 *dst++ = '0';                                  *dst++ = '0';
                         }                          }
Line 217  do_svis(char *dst, int c, int flag, int 
Line 239  do_svis(char *dst, int c, int flag, int 
                                 *dst++ = '\\'; *dst++ = c;                                  *dst++ = '\\'; *dst++ = c;
                                 return dst;                                  return dst;
                         }                          }
                           if (len)
                                   *len = olen;
                 }                  }
         }          }
         if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {          if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
                   HAVE(4);
                 *dst++ = '\\';                  *dst++ = '\\';
                 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';                  *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';
                 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';                  *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';
                 *dst++ =                             (c       & 07) + '0';                  *dst++ =                             (c       & 07) + '0';
         } else {          } else {
                 if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\';                  if ((flag & VIS_NOSLASH) == 0) {
                           HAVE(1);
                           *dst++ = '\\';
                   }
   
                 if (c & 0200) {                  if (c & 0200) {
                           HAVE(1);
                         c &= 0177; *dst++ = 'M';                          c &= 0177; *dst++ = 'M';
                 }                  }
   
                 if (iscntrl(c)) {                  if (iscntrl(c)) {
                           HAVE(2);
                         *dst++ = '^';                          *dst++ = '^';
                         if (c == 0177)                          if (c == 0177)
                                 *dst++ = '?';                                  *dst++ = '?';
                         else                          else
                                 *dst++ = c + '@';                                  *dst++ = c + '@';
                 } else {                  } else {
                           HAVE(2);
                         *dst++ = '-'; *dst++ = c;                          *dst++ = '-'; *dst++ = c;
                 }                  }
         }          }
         return dst;          return dst;
   out:
           *len = olen;
           return NULL;
 }  }
   
 typedef char *(*visfun_t)(char *, int, int, int, const char *);  typedef char *(*visfun_t)(char *, size_t *, int, int, int, const char *);
   
 /*  /*
  * Return the appropriate encoding function depending on the flags given.   * Return the appropriate encoding function depending on the flags given.
Line 275  svis(char *dst, int c, int flag, int nex
Line 311  svis(char *dst, int c, int flag, int nex
                 return dst;                  return dst;
         }          }
         f = getvisfun(flag);          f = getvisfun(flag);
         dst = (*f)(dst, c, flag, nextc, nextra);          dst = (*f)(dst, NULL, c, flag, nextc, nextra);
         free(nextra);          free(nextra);
         *dst = '\0';          *dst = '\0';
         return dst;          return dst;
Line 316  strsvis(char *dst, const char *csrc, int
Line 352  strsvis(char *dst, const char *csrc, int
         }          }
         f = getvisfun(flag);          f = getvisfun(flag);
         for (start = dst; (c = *src++) != '\0'; /* empty */)          for (start = dst; (c = *src++) != '\0'; /* empty */)
                 dst = (*f)(dst, c, flag, *src, nextra);                  dst = (*f)(dst, NULL, c, flag, *src, nextra);
         free(nextra);          free(nextra);
         *dst = '\0';          *dst = '\0';
         return (int)(dst - start);          return (int)(dst - start);
Line 344  strsvisx(char *dst, const char *csrc, si
Line 380  strsvisx(char *dst, const char *csrc, si
         f = getvisfun(flag);          f = getvisfun(flag);
         for (start = dst; len > 0; len--) {          for (start = dst; len > 0; len--) {
                 c = *src++;                  c = *src++;
                 dst = (*f)(dst, c, flag, len > 1 ? *src : '\0', nextra);                  dst = (*f)(dst, NULL, c, flag, len > 1 ? *src : '\0', nextra);
         }          }
         free(nextra);          free(nextra);
         *dst = '\0';          *dst = '\0';
Line 371  vis(char *dst, int c, int flag, int next
Line 407  vis(char *dst, int c, int flag, int next
                 return dst;                  return dst;
         }          }
         f = getvisfun(flag);          f = getvisfun(flag);
         dst = (*f)(dst, uc, flag, nextc, extra);          dst = (*f)(dst, NULL, uc, flag, nextc, extra);
         free(extra);          free(extra);
         *dst = '\0';          *dst = '\0';
         return dst;          return dst;

Legend:
Removed from v.1.41  
changed lines
  Added in v.1.42

CVSweb <webmaster@jp.NetBSD.org>