]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/unvis.3
Libc-763.13.tar.gz
[apple/libc.git] / gen / FreeBSD / unvis.3
CommitLineData
5b2abdfb
A
1.\" Copyright (c) 1989, 1991, 1993
2.\" The Regents of the University of California. 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.
5b2abdfb
A
12.\" 4. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" @(#)unvis.3 8.2 (Berkeley) 12/11/93
1f2f436a 29.\" $FreeBSD: src/lib/libc/gen/unvis.3,v 1.18 2007/01/09 00:27:56 imp Exp $
5b2abdfb
A
30.\"
31.Dd December 11, 1993
32.Dt UNVIS 3
33.Os
34.Sh NAME
35.Nm unvis ,
36.Nm strunvis
37.Nd decode a visual representation of characters
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In vis.h
42.Ft int
43.Fn unvis "char *cp" "int c" "int *astate" "int flag"
44.Ft int
45.Fn strunvis "char *dst" "const char *src"
46.Ft int
47.Fn strunvisx "char *dst" "const char *src" "int flag"
48.Sh DESCRIPTION
49The
50.Fn unvis ,
51.Fn strunvis
52and
53.Fn strunvisx
54functions
55are used to decode a visual representation of characters, as produced
56by the
57.Xr vis 3
58function, back into
3d9156a7
A
59the original form.
60Unvis is called with successive characters in
9385eb3d 61.Fa c
5b2abdfb
A
62until a valid
63sequence is recognized, at which time the decoded character is
64available at the character pointed to by
9385eb3d 65.Fa cp .
5b2abdfb
A
66Strunvis decodes the
67characters pointed to by
9385eb3d 68.Fa src
5b2abdfb 69into the buffer pointed to by
9385eb3d 70.Fa dst .
5b2abdfb
A
71.Pp
72The
73.Fn strunvis
74function
75simply copies
9385eb3d 76.Fa src
5b2abdfb 77to
9385eb3d 78.Fa dst ,
5b2abdfb
A
79decoding any escape sequences along the way,
80and returns the number of characters placed into
9385eb3d 81.Fa dst ,
5b2abdfb 82or \-1 if an
3d9156a7
A
83invalid escape sequence was detected.
84The size of
9385eb3d 85.Fa dst
5b2abdfb
A
86should be
87equal to the size of
9385eb3d 88.Fa src
5b2abdfb
A
89(that is, no expansion takes place during
90decoding).
91.Pp
92The
93.Fn strunvisx
94function does the same as the
95.Fn strunvis
96function,
97but it allows you to add a flag that specifies the style the string
9385eb3d 98.Fa src
5b2abdfb
A
99is encoded with.
100Currently, the only supported flag is
101.Dv VIS_HTTPSTYLE .
102.Pp
103The
104.Fn unvis
105function
106implements a state machine that can be used to decode an arbitrary
3d9156a7
A
107stream of bytes.
108All state associated with the bytes being decoded
5b2abdfb
A
109is stored outside the
110.Fn unvis
111function (that is, a pointer to the state is passed in), so
3d9156a7
A
112calls decoding different streams can be freely intermixed.
113To
5b2abdfb 114start decoding a stream of bytes, first initialize an integer
3d9156a7
A
115to zero.
116Call
5b2abdfb
A
117.Fn unvis
118with each successive byte, along with a pointer
119to this integer, and a pointer to a destination character.
120The
121.Fn unvis
122function
3d9156a7
A
123has several return codes that must be handled properly.
124They are:
5b2abdfb
A
125.Bl -tag -width UNVIS_VALIDPUSH
126.It Li \&0 (zero)
127Another character is necessary; nothing has been recognized yet.
3d9156a7 128.It Dv UNVIS_VALID
5b2abdfb
A
129A valid character has been recognized and is available at the location
130pointed to by cp.
3d9156a7 131.It Dv UNVIS_VALIDPUSH
5b2abdfb
A
132A valid character has been recognized and is available at the location
133pointed to by cp; however, the character currently passed in should
134be passed in again.
3d9156a7
A
135.It Dv UNVIS_NOCHAR
136A valid sequence was detected, but no character was produced.
137This
5b2abdfb 138return code is necessary to indicate a logical break between characters.
3d9156a7 139.It Dv UNVIS_SYNBAD
5b2abdfb 140An invalid escape sequence was detected, or the decoder is in an
3d9156a7
A
141unknown state.
142The decoder is placed into the starting state.
5b2abdfb
A
143.El
144.Pp
145When all bytes in the stream have been processed, call
146.Fn unvis
147one more time with
9385eb3d 148.Fa flag
5b2abdfb
A
149set to
150.Dv UNVIS_END
151to extract any remaining character (the character passed in is ignored).
152.Pp
153The
9385eb3d 154.Fa flag
5b2abdfb
A
155argument is also used to specify the encoding style of the source.
156If set to
157.Dv VIS_HTTPSTYLE ,
158.Fn unvis
159will decode URI strings as specified in RFC 1808.
160.Pp
161The following code fragment illustrates a proper use of
162.Fn unvis .
163.Bd -literal -offset indent
164int state = 0;
165char out;
166
167while ((ch = getchar()) != EOF) {
168again:
169 switch(unvis(&out, ch, &state, 0)) {
170 case 0:
171 case UNVIS_NOCHAR:
172 break;
173 case UNVIS_VALID:
174 (void) putchar(out);
175 break;
176 case UNVIS_VALIDPUSH:
177 (void) putchar(out);
178 goto again;
179 case UNVIS_SYNBAD:
180 (void)fprintf(stderr, "bad sequence!\en");
181 exit(1);
182 }
183}
184if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID)
185 (void) putchar(out);
186.Ed
187.Sh SEE ALSO
188.Xr vis 1 ,
189.Xr vis 3
190.Rs
191.%A R. Fielding
192.%T Relative Uniform Resource Locators
193.%O RFC1808
194.Re
195.Sh HISTORY
196The
197.Fn unvis
198function
199first appeared in
200.Bx 4.4 .