1.\" $NetBSD: unvis.3,v 1.20 2010/11/28 01:28:21 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.\" @(#)unvis.3 8.2 (Berkeley) 12/11/93 31.\" 32.Dd November 27, 2010 33.Dt UNVIS 3 34.Os 35.Sh NAME 36.Nm unvis , 37.Nm strunvis 38.Nd decode a visual representation of characters 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In vis.h 43.Ft int 44.Fn unvis "char *cp" "int c" "int *astate" "int flag" 45.Ft int 46.Fn strunvis "char *dst" "const char *src" 47.Ft int 48.Fn strunvisx "char *dst" "const char *src" "int flag" 49.Sh DESCRIPTION 50The 51.Fn unvis , 52.Fn strunvis 53and 54.Fn strunvisx 55functions 56are used to decode a visual representation of characters, as produced 57by the 58.Xr vis 3 59function, back into 60the original form. 61.Pp 62The 63.Fn unvis 64function is called with successive characters in 65.Ar c 66until a valid sequence is recognized, at which time the decoded 67character is available at the character pointed to by 68.Ar cp . 69.Pp 70The 71.Fn strunvis 72function decodes the characters pointed to by 73.Ar src 74into the buffer pointed to by 75.Ar dst . 76The 77.Fn strunvis 78function simply copies 79.Ar src 80to 81.Ar dst , 82decoding any escape sequences along the way, 83and returns the number of characters placed into 84.Ar dst , 85or \-1 if an 86invalid escape sequence was detected. 87The size of 88.Ar dst 89should be equal to the size of 90.Ar src 91(that is, no expansion takes place during decoding). 92.Pp 93The 94.Fn strunvisx 95function does the same as the 96.Fn strunvis 97function, 98but it allows you to add a flag that specifies the style the string 99.Ar src 100is encoded with. 101Currently, the supported flags are: 102.Dv VIS_HTTPSTYLE 103and 104.Dv VIS_MIMESTYLE . 105.Pp 106The 107.Fn unvis 108function implements a state machine that can be used to decode an 109arbitrary stream of bytes. 110All state associated with the bytes being decoded is stored outside the 111.Fn unvis 112function (that is, a pointer to the state is passed in), so 113calls decoding different streams can be freely intermixed. 114To start decoding a stream of bytes, first initialize an integer to zero. 115Call 116.Fn unvis 117with each successive byte, along with a pointer 118to this integer, and a pointer to a destination character. 119The 120.Fn unvis 121function has several return codes that must be handled properly. 122They are: 123.Bl -tag -width UNVIS_VALIDPUSH 124.It Li \&0 (zero) 125Another character is necessary; nothing has been recognized yet. 126.It Dv UNVIS_VALID 127A valid character has been recognized and is available at the location 128pointed to by cp. 129.It Dv UNVIS_VALIDPUSH 130A valid character has been recognized and is available at the location 131pointed to by cp; however, the character currently passed in should 132be passed in again. 133.It Dv UNVIS_NOCHAR 134A valid sequence was detected, but no character was produced. 135This return code is necessary to indicate a logical break between characters. 136.It Dv UNVIS_SYNBAD 137An invalid escape sequence was detected, or the decoder is in an unknown state. 138The decoder is placed into the starting state. 139.El 140.Pp 141When all bytes in the stream have been processed, call 142.Fn unvis 143one more time with flag set to 144.Dv UNVIS_END 145to extract any remaining character (the character passed in is ignored). 146.Pp 147The 148.Ar flag 149argument is also used to specify the encoding style of the source. 150If set to 151.Dv VIS_HTTPSTYLE 152or 153.Dv VIS_HTTP1808 , 154.Fn unvis 155will decode URI strings as specified in RFC 1808. 156If set to 157.Dv VIS_HTTP1866 , 158.Fn unvis 159will decode URI strings as specified in RFC 1866. 160If set to 161.Dv VIS_MIMESTYLE , 162.Fn unvis 163will decode MIME Quoted-Printable strings as specified in RFC 2045. 164If set to 165.Dv VIS_NOESCAPE , 166.Fn unvis 167will not decode \e quoted characters. 168.Pp 169The following code fragment illustrates a proper use of 170.Fn unvis . 171.Bd -literal -offset indent 172int state = 0; 173char out; 174 175while ((ch = getchar()) != EOF) { 176again: 177 switch(unvis(\*[Am]out, ch, \*[Am]state, 0)) { 178 case 0: 179 case UNVIS_NOCHAR: 180 break; 181 case UNVIS_VALID: 182 (void)putchar(out); 183 break; 184 case UNVIS_VALIDPUSH: 185 (void)putchar(out); 186 goto again; 187 case UNVIS_SYNBAD: 188 errx(EXIT_FAILURE, "Bad character sequence!"); 189 } 190} 191if (unvis(\*[Am]out, '\e0', \*[Am]state, UNVIS_END) == UNVIS_VALID) 192 (void)putchar(out); 193.Ed 194.Sh SEE ALSO 195.Xr unvis 1 , 196.Xr vis 1 , 197.Xr vis 3 198.Rs 199.%A R. Fielding 200.%T Relative Uniform Resource Locators 201.%O RFC1808 202.Re 203.Sh HISTORY 204The 205.Fn unvis 206function 207first appeared in 208.Bx 4.4 . 209