vis.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* $OpenBSD: vis.c,v 1.24 2015/07/20 01:52:28 millert Exp $ */
  2. /*-
  3. * Copyright (c) 1989, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/types.h>
  31. #include <errno.h>
  32. #include <ctype.h>
  33. #include <limits.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include "tmux.h"
  37. #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
  38. #define isvisible(c,flag) \
  39. (((c) == '\\' || (flag & VIS_ALL) == 0) && \
  40. (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \
  41. (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \
  42. (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \
  43. ((flag & VIS_SP) == 0 && (c) == ' ') || \
  44. ((flag & VIS_TAB) == 0 && (c) == '\t') || \
  45. ((flag & VIS_NL) == 0 && (c) == '\n') || \
  46. ((flag & VIS_SAFE) && ((c) == '\b' || \
  47. (c) == '\007' || (c) == '\r' || \
  48. isgraph((u_char)(c))))))
  49. /*
  50. * vis - visually encode characters
  51. */
  52. char *
  53. vis(char *dst, int c, int flag, int nextc)
  54. {
  55. if (isvisible(c, flag)) {
  56. if ((c == '"' && (flag & VIS_DQ) != 0) ||
  57. (c == '\\' && (flag & VIS_NOSLASH) == 0))
  58. *dst++ = '\\';
  59. *dst++ = c;
  60. *dst = '\0';
  61. return (dst);
  62. }
  63. if (flag & VIS_CSTYLE) {
  64. switch(c) {
  65. case '\n':
  66. *dst++ = '\\';
  67. *dst++ = 'n';
  68. goto done;
  69. case '\r':
  70. *dst++ = '\\';
  71. *dst++ = 'r';
  72. goto done;
  73. case '\b':
  74. *dst++ = '\\';
  75. *dst++ = 'b';
  76. goto done;
  77. case '\a':
  78. *dst++ = '\\';
  79. *dst++ = 'a';
  80. goto done;
  81. case '\v':
  82. *dst++ = '\\';
  83. *dst++ = 'v';
  84. goto done;
  85. case '\t':
  86. *dst++ = '\\';
  87. *dst++ = 't';
  88. goto done;
  89. case '\f':
  90. *dst++ = '\\';
  91. *dst++ = 'f';
  92. goto done;
  93. case ' ':
  94. *dst++ = '\\';
  95. *dst++ = 's';
  96. goto done;
  97. case '\0':
  98. *dst++ = '\\';
  99. *dst++ = '0';
  100. if (isoctal(nextc)) {
  101. *dst++ = '0';
  102. *dst++ = '0';
  103. }
  104. goto done;
  105. }
  106. }
  107. if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
  108. ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
  109. *dst++ = '\\';
  110. *dst++ = ((u_char)c >> 6 & 07) + '0';
  111. *dst++ = ((u_char)c >> 3 & 07) + '0';
  112. *dst++ = ((u_char)c & 07) + '0';
  113. goto done;
  114. }
  115. if ((flag & VIS_NOSLASH) == 0)
  116. *dst++ = '\\';
  117. if (c & 0200) {
  118. c &= 0177;
  119. *dst++ = 'M';
  120. }
  121. if (iscntrl((u_char)c)) {
  122. *dst++ = '^';
  123. if (c == 0177)
  124. *dst++ = '?';
  125. else
  126. *dst++ = c + '@';
  127. } else {
  128. *dst++ = '-';
  129. *dst++ = c;
  130. }
  131. done:
  132. *dst = '\0';
  133. return (dst);
  134. }
  135. /*
  136. * strvis, strnvis, strvisx - visually encode characters from src into dst
  137. *
  138. * Dst must be 4 times the size of src to account for possible
  139. * expansion. The length of dst, not including the trailing NULL,
  140. * is returned.
  141. *
  142. * Strnvis will write no more than siz-1 bytes (and will NULL terminate).
  143. * The number of bytes needed to fully encode the string is returned.
  144. *
  145. * Strvisx encodes exactly len bytes from src into dst.
  146. * This is useful for encoding a block of data.
  147. */
  148. int
  149. strvis(char *dst, const char *src, int flag)
  150. {
  151. char c;
  152. char *start;
  153. for (start = dst; (c = *src);)
  154. dst = vis(dst, c, flag, *++src);
  155. *dst = '\0';
  156. return (dst - start);
  157. }
  158. int
  159. strnvis(char *dst, const char *src, size_t siz, int flag)
  160. {
  161. char *start, *end;
  162. char tbuf[5];
  163. int c, i;
  164. i = 0;
  165. for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
  166. if (isvisible(c, flag)) {
  167. if ((c == '"' && (flag & VIS_DQ) != 0) ||
  168. (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
  169. /* need space for the extra '\\' */
  170. if (dst + 1 >= end) {
  171. i = 2;
  172. break;
  173. }
  174. *dst++ = '\\';
  175. }
  176. i = 1;
  177. *dst++ = c;
  178. src++;
  179. } else {
  180. i = vis(tbuf, c, flag, *++src) - tbuf;
  181. if (dst + i <= end) {
  182. memcpy(dst, tbuf, i);
  183. dst += i;
  184. } else {
  185. src--;
  186. break;
  187. }
  188. }
  189. }
  190. if (siz > 0)
  191. *dst = '\0';
  192. if (dst + i > end) {
  193. /* adjust return value for truncation */
  194. while ((c = *src))
  195. dst += vis(tbuf, c, flag, *++src) - tbuf;
  196. }
  197. return (dst - start);
  198. }
  199. int
  200. stravis(char **outp, const char *src, int flag)
  201. {
  202. char *buf;
  203. int len, serrno;
  204. buf = calloc(4, strlen(src) + 1);
  205. if (buf == NULL)
  206. return -1;
  207. len = strvis(buf, src, flag);
  208. serrno = errno;
  209. *outp = realloc(buf, len + 1);
  210. if (*outp == NULL) {
  211. *outp = buf;
  212. errno = serrno;
  213. }
  214. return (len);
  215. }
  216. int
  217. strvisx(char *dst, const char *src, size_t len, int flag)
  218. {
  219. char c;
  220. char *start;
  221. for (start = dst; len > 1; len--) {
  222. c = *src;
  223. dst = vis(dst, c, flag, *++src);
  224. }
  225. if (len)
  226. dst = vis(dst, *src, flag, '\0');
  227. *dst = '\0';
  228. return (dst - start);
  229. }