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