1.\" $NetBSD: vis.3,v 1.37 2013/02/20 17:01:15 christos Exp $ 2.\" 3.\" Copyright (c) 1989, 1991, 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.\" @(#)vis.3 8.1 (Berkeley) 6/9/93 31.\" 32.Dd February 19, 2013 33.Dt VIS 3 34.Os 35.Sh NAME 36.Nm vis , 37.Nm nvis , 38.Nm strvis , 39.Nm strnvis , 40.Nm strvisx , 41.Nm strnvisx , 42.Nm strenvisx , 43.Nm svis , 44.Nm snvis , 45.Nm strsvis , 46.Nm strsnvis , 47.Nm strsvisx , 48.Nm strsnvisx , 49.Nm strsenvisx 50.Nd visually encode characters 51.Sh LIBRARY 52.Lb libc 53.Sh SYNOPSIS 54.In vis.h 55.Ft char * 56.Fn vis "char *dst" "int c" "int flag" "int nextc" 57.Ft char * 58.Fn nvis "char *dst" "size_t dlen" "int c" "int flag" "int nextc" 59.Ft int 60.Fn strvis "char *dst" "const char *src" "int flag" 61.Ft int 62.Fn strnvis "char *dst" "size_t dlen" "const char *src" "int flag" 63.Ft int 64.Fn strvisx "char *dst" "const char *src" "size_t len" "int flag" 65.Ft int 66.Fn strnvisx "char *dst" "size_t dlen" "const char *src" "size_t len" "int flag" 67.Ft int 68.Fn strenvisx "char *dst" "size_t dlen" "const char *src" "size_t len" "int flag" "int *cerr_ptr" 69.Ft char * 70.Fn svis "char *dst" "int c" "int flag" "int nextc" "const char *extra" 71.Ft char * 72.Fn snvis "char *dst" "size_t dlen" "int c" "int flag" "int nextc" "const char *extra" 73.Ft int 74.Fn strsvis "char *dst" "const char *src" "int flag" "const char *extra" 75.Ft int 76.Fn strsnvis "char *dst" "size_t dlen" "const char *src" "int flag" "const char *extra" 77.Ft int 78.Fn strsvisx "char *dst" "const char *src" "size_t len" "int flag" "const char *extra" 79.Ft int 80.Fn strsnvisx "char *dst" "size_t dlen" "const char *src" "size_t len" "int flag" "const char *extra" 81.Ft int 82.Fn strsenvisx "char *dst" "size_t dlen" "const char *src" "size_t len" "int flag" "const char *extra" "int *cerr_ptr" 83.Sh DESCRIPTION 84The 85.Fn vis 86function 87copies into 88.Fa dst 89a string which represents the character 90.Fa c . 91If 92.Fa c 93needs no encoding, it is copied in unaltered. 94The string is null terminated, and a pointer to the end of the string is 95returned. 96The maximum length of any encoding is four 97bytes (not including the trailing 98.Dv NUL ) ; 99thus, when 100encoding a set of characters into a buffer, the size of the buffer should 101be four times the number of bytes encoded, plus one for the trailing 102.Dv NUL . 103The flag parameter is used for altering the default range of 104characters considered for encoding and for altering the visual 105representation. 106The additional character, 107.Fa nextc , 108is only used when selecting the 109.Dv VIS_CSTYLE 110encoding format (explained below). 111.Pp 112The 113.Fn strvis , 114.Fn strnvis , 115.Fn strvisx , 116and 117.Fn strnvisx 118functions copy into 119.Fa dst 120a visual representation of 121the string 122.Fa src . 123The 124.Fn strvis 125and 126.Fn strnvis 127functions encode characters from 128.Fa src 129up to the 130first 131.Dv NUL . 132The 133.Fn strvisx 134and 135.Fn strnvisx 136functions encode exactly 137.Fa len 138characters from 139.Fa src 140(this 141is useful for encoding a block of data that may contain 142.Dv NUL Ns 's ) . 143Both forms 144.Dv NUL 145terminate 146.Fa dst . 147The size of 148.Fa dst 149must be four times the number 150of bytes encoded from 151.Fa src 152(plus one for the 153.Dv NUL ) . 154Both 155forms return the number of characters in 156.Fa dst 157(not including the trailing 158.Dv NUL ) . 159The 160.Dq Nm n 161versions of the functions also take an additional argument 162.Fa dlen 163that indicates the length of the 164.Fa dst 165buffer. 166If 167.Fa dlen 168is not large enough to fit the converted string then the 169.Fn strnvis 170and 171.Fn strnvisx 172functions return \-1 and set 173.Va errno 174to 175.Dv ENOSPC . 176The 177.Fn strenvisx 178function takes an additional argument, 179.Fa cerr_ptr , 180that is used to pass in and out a multibyte conversion error flag. 181This is useful when processing single characters at a time when 182it is possible that the locale may be set to something other 183than the locale of the characters in the input data. 184.Pp 185The functions 186.Fn svis , 187.Fn snvis , 188.Fn strsvis , 189.Fn strsnvis , 190.Fn strsvisx , 191.Fn strsnvisx , 192and 193.Fn strsenvisx 194correspond to 195.Fn vis , 196.Fn nvis , 197.Fn strvis , 198.Fn strnvis , 199.Fn strvisx , 200.Fn strnvisx , 201and 202.Fn strenvisx 203but have an additional argument 204.Fa extra , 205pointing to a 206.Dv NUL 207terminated list of characters. 208These characters will be copied encoded or backslash-escaped into 209.Fa dst . 210These functions are useful e.g. to remove the special meaning 211of certain characters to shells. 212.Pp 213The encoding is a unique, invertible representation composed entirely of 214graphic characters; it can be decoded back into the original form using 215the 216.Xr unvis 3 , 217.Xr strunvis 3 218or 219.Xr strnunvis 3 220functions. 221.Pp 222There are two parameters that can be controlled: the range of 223characters that are encoded (applies only to 224.Fn vis , 225.Fn nvis , 226.Fn strvis , 227.Fn strnvis , 228.Fn strvisx , 229and 230.Fn strnvisx ) , 231and the type of representation used. 232By default, all non-graphic characters, 233except space, tab, and newline are encoded (see 234.Xr isgraph 3 ) . 235The following flags 236alter this: 237.Bl -tag -width VIS_WHITEX 238.It Dv VIS_GLOB 239Also encode the magic characters 240.Ql ( * , 241.Ql \&? , 242.Ql \&[ 243and 244.Ql # ) 245recognized by 246.Xr glob 3 . 247.It Dv VIS_SP 248Also encode space. 249.It Dv VIS_TAB 250Also encode tab. 251.It Dv VIS_NL 252Also encode newline. 253.It Dv VIS_WHITE 254Synonym for 255.Dv VIS_SP 256\&| 257.Dv VIS_TAB 258\&| 259.Dv VIS_NL . 260.It Dv VIS_SAFE 261Only encode 262.Dq unsafe 263characters. 264Unsafe means control characters which may cause common terminals to perform 265unexpected functions. 266Currently this form allows space, tab, newline, backspace, bell, and 267return \(em in addition to all graphic characters \(em unencoded. 268.El 269.Pp 270(The above flags have no effect for 271.Fn svis , 272.Fn snvis , 273.Fn strsvis , 274.Fn strsnvis , 275.Fn strsvisx , 276and 277.Fn strsnvisx . 278When using these functions, place all graphic characters to be 279encoded in an array pointed to by 280.Fa extra . 281In general, the backslash character should be included in this array, see the 282warning on the use of the 283.Dv VIS_NOSLASH 284flag below). 285.Pp 286There are four forms of encoding. 287All forms use the backslash character 288.Ql \e 289to introduce a special 290sequence; two backslashes are used to represent a real backslash, 291except 292.Dv VIS_HTTPSTYLE 293that uses 294.Ql % , 295or 296.Dv VIS_MIMESTYLE 297that uses 298.Ql = . 299These are the visual formats: 300.Bl -tag -width VIS_CSTYLE 301.It (default) 302Use an 303.Ql M 304to represent meta characters (characters with the 8th 305bit set), and use caret 306.Ql ^ 307to represent control characters (see 308.Xr iscntrl 3 ) . 309The following formats are used: 310.Bl -tag -width xxxxx 311.It Dv \e^C 312Represents the control character 313.Ql C . 314Spans characters 315.Ql \e000 316through 317.Ql \e037 , 318and 319.Ql \e177 320(as 321.Ql \e^? ) . 322.It Dv \eM-C 323Represents character 324.Ql C 325with the 8th bit set. 326Spans characters 327.Ql \e241 328through 329.Ql \e376 . 330.It Dv \eM^C 331Represents control character 332.Ql C 333with the 8th bit set. 334Spans characters 335.Ql \e200 336through 337.Ql \e237 , 338and 339.Ql \e377 340(as 341.Ql \eM^? ) . 342.It Dv \e040 343Represents 344.Tn ASCII 345space. 346.It Dv \e240 347Represents Meta-space. 348.El 349.Pp 350.It Dv VIS_CSTYLE 351Use C-style backslash sequences to represent standard non-printable 352characters. 353The following sequences are used to represent the indicated characters: 354.Bd -unfilled -offset indent 355.Li \ea Tn \(em BEL No (007) 356.Li \eb Tn \(em BS No (010) 357.Li \ef Tn \(em NP No (014) 358.Li \en Tn \(em NL No (012) 359.Li \er Tn \(em CR No (015) 360.Li \es Tn \(em SP No (040) 361.Li \et Tn \(em HT No (011) 362.Li \ev Tn \(em VT No (013) 363.Li \e0 Tn \(em NUL No (000) 364.Ed 365.Pp 366When using this format, the 367.Fa nextc 368parameter is looked at to determine if a 369.Dv NUL 370character can be encoded as 371.Ql \e0 372instead of 373.Ql \e000 . 374If 375.Fa nextc 376is an octal digit, the latter representation is used to 377avoid ambiguity. 378.It Dv VIS_OCTAL 379Use a three digit octal sequence. 380The form is 381.Ql \eddd 382where 383.Em d 384represents an octal digit. 385.It Dv VIS_HTTPSTYLE 386Use URI encoding as described in RFC 1738. 387The form is 388.Ql %xx 389where 390.Em x 391represents a lower case hexadecimal digit. 392.It Dv VIS_MIMESTYLE 393Use MIME Quoted-Printable encoding as described in RFC 2045, only don't 394break lines and don't handle CRLF. 395The form is 396.Ql =XX 397where 398.Em X 399represents an upper case hexadecimal digit. 400.El 401.Pp 402There is one additional flag, 403.Dv VIS_NOSLASH , 404which inhibits the 405doubling of backslashes and the backslash before the default 406format (that is, control characters are represented by 407.Ql ^C 408and 409meta characters as 410.Ql M-C ) . 411With this flag set, the encoding is 412ambiguous and non-invertible. 413.Sh MULTIBYTE CHARACTER SUPPORT 414These functions support multibyte character input. 415The encoding conversion is influenced by the setting of the 416.Ev LC_CTYPE 417environment variable which defines the set of characters 418that can be copied without encoding. 419.Pp 420When 8-bit data is present in the input, 421.Ev LC_CTYPE 422must be set to the correct locale or to the C locale. 423If the locales of the data and the conversion are mismatched, 424multibyte character recognition may fail and encoding will be performed 425byte-by-byte instead. 426.Pp 427As noted above, 428.Fa dst 429must be four times the number of bytes processed from 430.Fa src . 431But note that each multibyte character can be up to 432.Dv MB_LEN_MAX 433bytes (see 434.Xr multibyte 3 ) 435so in terms of multibyte characters, 436.Fa dst 437must be four times 438.Dv MB_LEN_MAX 439times the number of characters processed from 440.Fa src . 441.Sh ENVIRONMENT 442.Bl -tag -width ".Ev LC_CTYPE" 443.It Ev LC_CTYPE 444Specify the locale of the input data. 445Set to C if the input data locale is unknown. 446.El 447.Sh ERRORS 448The functions 449.Fn nvis 450and 451.Fn snvis 452will return 453.Dv NULL 454and the functions 455.Fn strnvis , 456.Fn strnvisx , 457.Fn strsnvis , 458and 459.Fn strsnvisx , 460will return \-1 when the 461.Fa dlen 462destination buffer size is not enough to perform the conversion while 463setting 464.Va errno 465to: 466.Bl -tag -width ".Bq Er ENOSPC" 467.It Bq Er ENOSPC 468The destination buffer size is not large enough to perform the conversion. 469.El 470.Sh SEE ALSO 471.Xr unvis 1 , 472.Xr vis 1 , 473.Xr multibyte 3 , 474.Xr glob 3 , 475.Xr unvis 3 476.Rs 477.%A T. Berners-Lee 478.%T Uniform Resource Locators (URL) 479.%O "RFC 1738" 480.Re 481.Rs 482.%T "Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies" 483.%O "RFC 2045" 484.Re 485.Sh HISTORY 486The 487.Fn vis , 488.Fn strvis , 489and 490.Fn strvisx 491functions first appeared in 492.Bx 4.4 . 493The 494.Fn svis , 495.Fn strsvis , 496and 497.Fn strsvisx 498functions appeared in 499.Nx 1.5 . 500The buffer size limited versions of the functions 501.Po Fn nvis , 502.Fn strnvis , 503.Fn strnvisx , 504.Fn snvis , 505.Fn strsnvis , 506and 507.Fn strsnvisx Pc 508appeared in 509.Nx 6.0 510and 511.Fx 9.2 . 512Myltibyte character support was added in 513.Nx 7.0 514and 515.Fx 9.2 . 516