1 .\" $OpenBSD: vis.3,v 1.35 2015/07/20 01:52:28 millert Exp $
3 .\" Copyright (c) 1989, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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
30 .Dd $Mdocdate: February 3 2017 $
39 .Nd visually encode characters
44 .Fn vis "char *dst" "int c" "int flag" "int nextc"
46 .Fn strvis "char *dst" "const char *src" "int flag"
48 .Fn strnvis "char *dst" "const char *src" "size_t dstsize" "int flag"
50 .Fn strvisx "char *dst" "const char *src" "size_t srclen" "int flag"
52 .Fn stravis "char **outp" "const char *src" "int flag"
58 a string which represents the character
62 needs no encoding, it is copied in unaltered.
64 will be NUL-terminated and must be at least 5 bytes long
65 (maximum encoding requires 4 bytes plus the NUL).
66 The additional character,
68 is only used when selecting the
70 encoding format (explained below).
79 a visual representation of
85 function encodes characters from
87 up to the first NUL, into a buffer
89 (which must be at least 4 * strlen(src) + 1 long).
93 function encodes characters from
95 up to the first NUL or the end of the buffer
102 function encodes exactly
108 (which must be at least 4 * srclen + 1 long).
110 is useful for encoding a block of data that may contain NULs.
114 function writes a visual representation of the string
116 into a newly allocated string
118 it does not attempt to
124 to release the allocated storage when it is no longer needed.
126 checks for integer overflow when allocating memory.
128 All forms NUL-terminate
134 is zero, in which case
140 parameter is used for altering the default range of
141 characters considered for encoding and for altering the visual
144 The encoding is a unique, invertible representation composed entirely of
145 graphic characters; it can be decoded back into the original form using
152 There are two parameters that can be controlled: the range of
153 characters that are encoded, and the type
154 of representation used.
155 By default, all non-graphic characters
156 except space, tab, and newline are encoded
161 .Bl -tag -width VIS_WHITEX
163 Encode all characters, whether visible or not.
165 Also encode double quote characters
168 Also encode magic characters recognized by
183 .Dv VIS_SP | VIS_TAB | VIS_NL .
188 These are control characters which may cause common terminals to perform
189 unexpected functions.
190 Currently this form allows space,
191 tab, newline, backspace, bell, and return -- in addition
192 to all graphic characters -- unencoded.
195 There are three forms of encoding.
196 All forms use the backslash
198 character to introduce a special
199 sequence; two backslashes are used to represent a real backslash.
200 These are the visual formats:
201 .Bl -tag -width VIS_CSTYLE
205 to represent meta characters (characters with the 8th
206 bit set), and use a caret
208 to represent control characters (see
210 The following formats are used:
211 .Bl -tag -width xxxxx
213 Represents the control character
226 with the 8th bit set.
232 Represents control character
234 with the 8th bit set.
248 Represents Meta-space.
256 Use C-style backslash sequences to represent standard non-printable
258 The following sequences are used to represent the indicated characters:
259 .Bd -unfilled -offset indent
260 .Li \ea Tn - BEL No (007)
261 .Li \eb Tn - BS No (010)
262 .Li \ef Tn - NP No (014)
263 .Li \en Tn - NL No (012)
264 .Li \er Tn - CR No (015)
265 .Li \es Tn - SP No (040)
266 .Li \et Tn - HT No (011)
267 .Li \ev Tn - VT No (013)
268 .Li \e0 Tn - NUL No (000)
271 When using this format, the
273 parameter is looked at to determine
274 if a NUL character can be encoded as
280 is an octal digit, the latter representation is used to
283 Use a three digit octal sequence.
288 represents an octal digit.
291 There is one additional flag,
294 doubling of backslashes and the backslash before the default
295 format (that is, control characters are represented by
300 With this flag set, the encoding is
301 ambiguous and non-invertible.
304 returns a pointer to the terminating NUL character of the string
310 return the number of characters in
312 (not including the trailing NUL).
315 returns the length that
317 would become if it were of unlimited size (similar to
321 This can be used to detect truncation, but it also means that
324 must not be used without checking it against
327 Upon successful completion,
329 returns the number of characters in
331 (not including the trailing NUL).
340 has unusual storage requirements that can lead to stack or heap corruption
341 if the destination is not carefully constructed.
342 A common mistake is to use the same size for the source and destination
343 when the destination actually needs up to 4 * strlen(source) + 1 bytes.
345 If the length of a string to be encoded is not known at compile time, use
347 .Bd -literal -offset indent
351 if (stravis(&dst, src, VIS_OCTAL) == -1)
358 To encode a fixed size buffer,
360 can be used with a fixed size target buffer:
361 .Bd -literal -offset indent
362 char src[MAXPATHLEN];
363 char dst[4 * MAXPATHLEN + 1];
366 if (strnvis(dst, src, sizeof(dst), VIS_OCTAL) >= sizeof(dst))
380 functions first appeared in
392 flag first appeared in