1.\" $OpenBSD: unvis.3,v 1.12 2000/12/24 00:30:53 aaron 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. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.Dd December 11, 1993 35.Dt UNVIS 3 36.Os 37.Sh NAME 38.Nm unvis , 39.Nm strunvis 40.Nd decode a visual representation of characters 41.Sh SYNOPSIS 42.Fd #include <vis.h> 43.Ft int 44.Fn unvis "char *cp" "char c" "int *astate" "int flag" 45.Ft int 46.Fn strunvis "char *dst" "char *src" 47.Sh DESCRIPTION 48The 49.Fn unvis 50and 51.Fn strunvis 52functions are used to decode a visual representation of characters, 53as produced by the 54.Xr vis 3 55function, back into the original form. 56.Fn unvis 57is called with successive characters in 58.Fa c 59until a valid 60sequence is recognized, at which time the decoded character is 61available at the character pointed to by 62.Fa cp . 63.Pp 64.Fn strunvis 65decodes the characters pointed to by 66.Fa src 67into the buffer pointed to by 68.Fa dst . 69.Pp 70The 71.Fn strunvis 72function simply copies 73.Fa src 74to 75.Fa dst , 76decoding any escape sequences along the way, 77and returns the number of characters placed into 78.Fa dst , 79or \-1 if an 80invalid escape sequence was detected. 81The size of 82.Fa dst 83should be 84equal to the size of 85.Fa src 86(that is, no expansion takes place during decoding). 87.Pp 88The 89.Fn unvis 90function implements a state machine that can be used to decode an arbitrary 91stream of bytes. 92All state associated with the bytes being decoded is stored outside the 93.Fn unvis 94function (that is, a pointer to the state is passed in), so 95calls decoding different streams can be freely intermixed. 96To start decoding a stream of bytes, first initialize an integer 97to zero. 98Call 99.Fn unvis 100with each successive byte, along with a pointer 101to this integer, and a pointer to a destination character. 102The 103.Fn unvis 104function has several return codes that must be handled properly. 105They are: 106.Bl -tag -width UNVIS_VALIDPUSH 107.It Li \&0 (zero) 108Another character is necessary; nothing has been recognized yet. 109.It Dv UNVIS_VALID 110A valid character has been recognized and is available at the location 111pointed to by 112.Fa cp . 113.It Dv UNVIS_VALIDPUSH 114A valid character has been recognized and is available at the location 115pointed to by 116.Fa cp ; 117however, the character currently passed in should be passed in again. 118.It Dv UNVIS_NOCHAR 119A valid sequence was detected, but no character was produced. 120This return code is necessary to indicate a logical break between characters. 121.It Dv UNVIS_SYNBAD 122An invalid escape sequence was detected, or the decoder is in an 123unknown state. 124The decoder is placed into the starting state. 125.El 126.Pp 127When all bytes in the stream have been processed, call 128.Fn unvis 129one more time with flag set to 130.Dv UNVIS_END 131to extract any remaining character (the character passed in is ignored). 132.Sh EXAMPLES 133The following code fragment illustrates a proper use of 134.Fn unvis . 135.Bd -literal -offset indent 136int state = 0; 137char out; 138 139while ((ch = getchar()) != EOF) { 140again: 141 switch(unvis(&out, ch, &state, 0)) { 142 case 0: 143 case UNVIS_NOCHAR: 144 break; 145 case UNVIS_VALID: 146 (void) putchar(out); 147 break; 148 case UNVIS_VALIDPUSH: 149 (void) putchar(out); 150 goto again; 151 case UNVIS_SYNBAD: 152 (void)fprintf(stderr, "bad sequence!\en"); 153 exit(1); 154 } 155} 156if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID) 157 (void) putchar(out); 158.Ed 159.Sh SEE ALSO 160.Xr unvis 1 , 161.Xr vis 1 , 162.Xr vis 3 163.Sh HISTORY 164The 165.Fn unvis 166function first appeared in 167.Bx 4.4 . 168