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