1VIS(3) BSD Library Functions Manual VIS(3)
2
4 vis, strvis, strnvis, strvisx — visually encode characters
5
7 Utility functions from BSD systems (libbsd, -lbsd)
8
10 #include <stdlib.h>
11 #include <vis.h>
12
13 char *
14 vis(char *dst, int c, int flag, int nextc);
15
16 int
17 strvis(char *dst, const char *src, int flag);
18
19 int
20 strnvis(char *dst, const char *src, size_t size, int flag);
21
22 int
23 strvisx(char *dst, const char *src, size_t len, int flag);
24
26 The vis() function copies into dst a string which represents the charac‐
27 ter c. If c needs no encoding, it is copied in unaltered. The string is
28 NUL terminated and a pointer to the end of the string is returned. The
29 maximum length of any encoding is four characters (not including the
30 trailing NUL); thus, when encoding a set of characters into a buffer, the
31 size of the buffer should be four times the number of characters encoded,
32 plus one for the trailing NUL. The flag parameter is used for altering
33 the default range of characters considered for encoding and for altering
34 the visual representation. The additional character, nextc, is only used
35 when selecting the VIS_CSTYLE encoding format (explained below).
36
37 The strvis(), strnvis() and strvisx() functions copy into dst a visual
38 representation of the string src. The strvis() function encodes charac‐
39 ters from src up to the first NUL. The strnvis() function encodes char‐
40 acters from src up to the first NUL or the end of dst, as indicated by
41 size. The strvisx() function encodes exactly len characters from src
42 (this is useful for encoding a block of data that may contain NULs). All
43 three forms NUL terminate dst, except for strnvis() when size is zero, in
44 which case dst is not touched. For strvis() and strvisx(), the size of
45 dst must be four times the number of characters encoded from src (plus
46 one for the NUL). strvis() and strvisx() return the number of characters
47 in dst (not including the trailing NUL). strnvis() returns the length
48 that dst would become if it were of unlimited size (similar to
49 snprintf(3) or strlcpy(3)). This can be used to detect truncation but it
50 also means that the return value of strnvis() must not be used without
51 checking it against size.
52
53 The encoding is a unique, invertible representation composed entirely of
54 graphic characters; it can be decoded back into the original form using
55 the unvis(3) or strunvis(3) functions.
56
57 There are two parameters that can be controlled: the range of characters
58 that are encoded, and the type of representation used. By default, all
59 non-graphic characters except space, tab, and newline are encoded (see
60 isgraph(3)). The following flags alter this:
61
62 VIS_GLOB Also encode magic characters recognized by glob(3) (‘*’, ‘?’,
63 ‘[’) and ‘#’.
64
65 VIS_SP Also encode space.
66
67 VIS_TAB Also encode tab.
68
69 VIS_NL Also encode newline.
70
71 VIS_WHITE Synonym for VIS_SP | VIS_TAB | VIS_NL.
72
73 VIS_SAFE Only encode “unsafe” characters. These are control charac‐
74 ters which may cause common terminals to perform unexpected
75 functions. Currently this form allows space, tab, newline,
76 backspace, bell, and return -- in addition to all graphic
77 characters -- unencoded.
78
79 There are three forms of encoding. All forms use the backslash ‘\’ char‐
80 acter to introduce a special sequence; two backslashes are used to repre‐
81 sent a real backslash. These are the visual formats:
82
83 (default) Use an ‘M’ to represent meta characters (characters with the
84 8th bit set), and use a caret ‘^’ to represent control char‐
85 acters (see iscntrl(3)). The following formats are used:
86
87 \^C Represents the control character ‘C’. Spans charac‐
88 ters ‘\000’ through ‘\037’, and ‘\177’ (as ‘\^?’).
89
90 \M-C Represents character ‘C’ with the 8th bit set. Spans
91 characters ‘\241’ through ‘\376’.
92
93 \M^C Represents control character ‘C’ with the 8th bit set.
94 Spans characters ‘\200’ through ‘\237’, and ‘\377’ (as
95 ‘\M^?’).
96
97 \040 Represents ASCII space.
98
99 \240 Represents Meta-space.
100
101 VIS_CSTYLE Use C-style backslash sequences to represent standard non-
102 printable characters. The following sequences are used to
103 represent the indicated characters:
104
105 \a - BEL (007)
106 \b - BS (010)
107 \f - NP (014)
108 \n - NL (012)
109 \r - CR (015)
110 \s - SP (040)
111 \t - HT (011)
112 \v - VT (013)
113 \0 - NUL (000)
114
115 When using this format, the nextc parameter is looked at to
116 determine if a NUL character can be encoded as ‘\0’ instead
117 of ‘\000’. If nextc is an octal digit, the latter represen‐
118 tation is used to avoid ambiguity.
119
120 VIS_OCTAL Use a three digit octal sequence. The form is ‘\ddd’ where d
121 represents an octal digit.
122
123 There is one additional flag, VIS_NOSLASH, which inhibits the doubling of
124 backslashes and the backslash before the default format (that is, control
125 characters are represented by ‘^C’ and meta characters as ‘M-C’). With
126 this flag set, the encoding is ambiguous and non-invertible.
127
129 unvis(1), vis(1), snprintf(3), strlcpy(3), unvis(3)
130
132 The vis(), strvis() and strvisx() functions first appeared in 4.4BSD.
133 The strnvis() function first appeared in OpenBSD 2.9.
134
135BSD June 22, 2019 BSD