xref: /openbsd/lib/libc/gen/unvis.3 (revision 2b0e239d)
1.\"	$OpenBSD: unvis.3,v 1.7 1999/06/03 10:03:24 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
53are used to decode a visual representation of characters, as produced
54by the
55.Xr vis 3
56function, back into
57the original form.
58.Fn unvis
59is called with successive characters in
60.Fa c
61until a valid
62sequence is recognized, at which time the decoded character is
63available at the character pointed to by
64.Fa cp .
65.Pp
66.Fn strunvis
67decodes the characters pointed to by
68.Fa src
69into the buffer pointed to by
70.Fa dst .
71.Pp
72The
73.Fn strunvis
74function
75simply copies
76.Fa src
77to
78.Fa dst ,
79decoding any escape sequences along the way,
80and returns the number of characters placed into
81.Fa dst ,
82or \-1 if an
83invalid escape sequence was detected.  The size of
84.Fa dst
85should be
86equal to the size of
87.Fa src
88(that is, no expansion takes place during decoding).
89.Pp
90The
91.Fn unvis
92function
93implements a state machine that can be used to decode an arbitrary
94stream of bytes.  All state associated with the bytes being decoded
95is stored outside the
96.Fn unvis
97function (that is, a pointer to the state is passed in), so
98calls decoding different streams can be freely intermixed.  To
99start decoding a stream of bytes, first initialize an integer
100to zero.  Call
101.Fn unvis
102with each successive byte, along with a pointer
103to this integer, and a pointer to a destination character.
104The
105.Fn unvis
106function
107has several return codes that must be handled properly.  They are:
108.Bl -tag -width UNVIS_VALIDPUSH
109.It Li \&0 (zero)
110Another character is necessary; nothing has been recognized yet.
111.It Dv UNVIS_VALID
112A valid character has been recognized and is available at the location
113pointed to by
114.Fa cp .
115.It Dv UNVIS_VALIDPUSH
116A valid character has been recognized and is available at the location
117pointed to by
118.Fa cp ;
119however, the character currently passed in should be passed in again.
120.It Dv UNVIS_NOCHAR
121A valid sequence was detected, but no character was produced.  This
122return code is necessary to indicate a logical break between characters.
123.It Dv UNVIS_SYNBAD
124An invalid escape sequence was detected, or the decoder is in an
125unknown state.  The decoder is placed into the starting state.
126.El
127.Pp
128When all bytes in the stream have been processed, call
129.Fn unvis
130one more time with flag set to
131.Dv UNVIS_END
132to extract any remaining character (the character passed in is ignored).
133.Pp
134.Sh EXAMPLES
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 unvis 1 ,
163.Xr vis 1 ,
164.Xr vis 3
165.Sh HISTORY
166The
167.Fn unvis
168function first appeared in
169.Bx 4.4 .
170