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