1.\" $NetBSD: unvis.3,v 1.18 2009/02/10 23:06:31 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.\" @(#)unvis.3 8.2 (Berkeley) 12/11/93 31.\" 32.Dd February 10, 2009 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 , 152.Fn unvis 153will decode URI strings as specified in RFC 1808. 154If set to 155.Dv VIS_MIMESTYLE , 156.Fn unvis 157will decode MIME Quoted-Printable strings as specified in RFC 2045. 158.Pp 159The following code fragment illustrates a proper use of 160.Fn unvis . 161.Bd -literal -offset indent 162int state = 0; 163char out; 164 165while ((ch = getchar()) != EOF) { 166again: 167 switch(unvis(\*[Am]out, ch, \*[Am]state, 0)) { 168 case 0: 169 case UNVIS_NOCHAR: 170 break; 171 case UNVIS_VALID: 172 (void)putchar(out); 173 break; 174 case UNVIS_VALIDPUSH: 175 (void)putchar(out); 176 goto again; 177 case UNVIS_SYNBAD: 178 errx(EXIT_FAILURE, "Bad character sequence!"); 179 } 180} 181if (unvis(\*[Am]out, '\e0', \*[Am]state, UNVIS_END) == UNVIS_VALID) 182 (void)putchar(out); 183.Ed 184.Sh SEE ALSO 185.Xr unvis 1 , 186.Xr vis 1 , 187.Xr vis 3 188.Rs 189.%A R. Fielding 190.%T Relative Uniform Resource Locators 191.%O RFC1808 192.Re 193.Sh HISTORY 194The 195.Fn unvis 196function 197first appeared in 198.Bx 4.4 . 199