BSD 4_3_Net_2 release
[unix-history] / usr / src / lib / libc / gen / unvis.3
CommitLineData
af359dea 1.\" Copyright (c) 1989, 1991 The Regents of the University of California.
56df4199
MT
2.\" All rights reserved.
3.\"
af359dea
C
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.
56df4199 19.\"
af359dea
C
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.
56df4199 31.\"
af359dea
C
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
947d15f7 50and
af359dea
C
51.Fn strunvis
52functions
947d15f7 53are used to decode a visual representation of characters, as produced
af359dea
C
54by the
55.Xr vis 3
56function, back into
57the original form. Unvis is called with successive characters in
58.Ar c
947d15f7
MT
59until a valid
60sequence is recognized, at which time the decoded character is
af359dea
C
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
947d15f7 86decoding).
af359dea
C
87.Pp
88The
89.Fn unvis
90function
947d15f7
MT
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
af359dea 94.Fn unvis
947d15f7
MT
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
af359dea
C
98to zero. Call
99.Fn unvis
100with each successive byte, along with a pointer
947d15f7 101to this integer, and a pointer to an destination character.
af359dea
C
102The
103.Xr unvis
104function
947d15f7 105has several return codes that must be handled properly. They are:
af359dea
C
106.Bl -tag -width UNVIS_VALIDPUSH
107.It Li \&0 (zero)
947d15f7 108Another character is necessary; nothing has been recognized yet.
af359dea 109.It Dv UNVIS_VALID
947d15f7
MT
110A valid character has been recognized and is available at the location
111pointed to by cp.
af359dea 112.It Dv UNVIS_VALIDPUSH
947d15f7
MT
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.
af359dea 116.It Dv UNVIS_NOCHAR
947d15f7
MT
117A valid sequence was detected, but no character was produced. This
118return code is necessary to indicate a logical break between characters.
af359dea 119.It Dv UNVIS_SYNBAD
947d15f7
MT
120An invalid esacpe sequence was detected, or the decoder is in an
121unknown state. The decoder is placed into the starting state.
af359dea
C
122.El
123.Pp
947d15f7 124When all bytes in the stream have been processed, call
af359dea 125.Fn unvis
947d15f7 126one more time with flag set to
af359dea 127.Dv UNVIS_END
947d15f7 128to extract any remaining character (the character passed in is ignored).
af359dea 129.Pp
947d15f7 130The following code fragment illustrates a proper use of
af359dea
C
131.Fn unvis .
132.Bd -literal -offset indent
947d15f7
MT
133int state = 0;
134char out;
56df4199
MT
135
136while ((ch = getchar()) != EOF) {
137again:
947d15f7
MT
138 switch(unvis(&out, ch, &state, 0)) {
139 case 0:
140 case UNVIS_NOCHAR:
56df4199 141 break;
947d15f7
MT
142 case UNVIS_VALID:
143 (void) putchar(out);
56df4199 144 break;
947d15f7
MT
145 case UNVIS_VALIDPUSH:
146 (void) putchar(out);
56df4199 147 goto again;
947d15f7 148 case UNVIS_SYNBAD:
56df4199 149 (void)fprintf(stderr, "bad sequence!\n");
af359dea 150 exit(1);
56df4199
MT
151 }
152}
947d15f7
MT
153if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID)
154 (void) putchar(out);
af359dea
C
155.Ed
156.Sh SEE ALSO
157.Xr vis 1
158.Sh HISTORY
159The
160.Fn unvis
161function is
162.Ud .