1.\" $OpenBSD: unvis.3,v 1.13 2003/06/02 20:18:35 millert 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.Dd December 11, 1993 31.Dt UNVIS 3 32.Os 33.Sh NAME 34.Nm unvis , 35.Nm strunvis 36.Nd decode a visual representation of characters 37.Sh SYNOPSIS 38.Fd #include <vis.h> 39.Ft int 40.Fn unvis "char *cp" "char c" "int *astate" "int flag" 41.Ft int 42.Fn strunvis "char *dst" "char *src" 43.Sh DESCRIPTION 44The 45.Fn unvis 46and 47.Fn strunvis 48functions are used to decode a visual representation of characters, 49as produced by the 50.Xr vis 3 51function, back into the original form. 52.Fn unvis 53is called with successive characters in 54.Fa c 55until a valid 56sequence is recognized, at which time the decoded character is 57available at the character pointed to by 58.Fa cp . 59.Pp 60.Fn strunvis 61decodes the characters pointed to by 62.Fa src 63into the buffer pointed to by 64.Fa dst . 65.Pp 66The 67.Fn strunvis 68function simply copies 69.Fa src 70to 71.Fa dst , 72decoding any escape sequences along the way, 73and returns the number of characters placed into 74.Fa dst , 75or \-1 if an 76invalid escape sequence was detected. 77The size of 78.Fa dst 79should be 80equal to the size of 81.Fa src 82(that is, no expansion takes place during decoding). 83.Pp 84The 85.Fn unvis 86function implements a state machine that can be used to decode an arbitrary 87stream of bytes. 88All state associated with the bytes being decoded is stored outside the 89.Fn unvis 90function (that is, a pointer to the state is passed in), so 91calls decoding different streams can be freely intermixed. 92To start decoding a stream of bytes, first initialize an integer 93to zero. 94Call 95.Fn unvis 96with each successive byte, along with a pointer 97to this integer, and a pointer to a destination character. 98The 99.Fn unvis 100function has several return codes that must be handled properly. 101They are: 102.Bl -tag -width UNVIS_VALIDPUSH 103.It Li \&0 (zero) 104Another character is necessary; nothing has been recognized yet. 105.It Dv UNVIS_VALID 106A valid character has been recognized and is available at the location 107pointed to by 108.Fa cp . 109.It Dv UNVIS_VALIDPUSH 110A valid character has been recognized and is available at the location 111pointed to by 112.Fa cp ; 113however, the character currently passed in should be passed in again. 114.It Dv UNVIS_NOCHAR 115A valid sequence was detected, but no character was produced. 116This return code is necessary to indicate a logical break between characters. 117.It Dv UNVIS_SYNBAD 118An invalid escape sequence was detected, or the decoder is in an 119unknown state. 120The decoder is placed into the starting state. 121.El 122.Pp 123When all bytes in the stream have been processed, call 124.Fn unvis 125one more time with flag set to 126.Dv UNVIS_END 127to extract any remaining character (the character passed in is ignored). 128.Sh EXAMPLES 129The following code fragment illustrates a proper use of 130.Fn unvis . 131.Bd -literal -offset indent 132int state = 0; 133char out; 134 135while ((ch = getchar()) != EOF) { 136again: 137 switch(unvis(&out, ch, &state, 0)) { 138 case 0: 139 case UNVIS_NOCHAR: 140 break; 141 case UNVIS_VALID: 142 (void) putchar(out); 143 break; 144 case UNVIS_VALIDPUSH: 145 (void) putchar(out); 146 goto again; 147 case UNVIS_SYNBAD: 148 (void)fprintf(stderr, "bad sequence!\en"); 149 exit(1); 150 } 151} 152if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID) 153 (void) putchar(out); 154.Ed 155.Sh SEE ALSO 156.Xr unvis 1 , 157.Xr vis 1 , 158.Xr vis 3 159.Sh HISTORY 160The 161.Fn unvis 162function first appeared in 163.Bx 4.4 . 164