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