1 .\" $OpenBSD: unvis.3,v 1.18 2014/11/09 04:34:20 guenther Exp $
3 .\" Copyright (c) 1989, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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
30 .Dd $Mdocdate: February 3 2017 $
37 .Nd decode a visual representation of characters
41 .Fn unvis "char *cp" "char c" "int *astate" "int flag"
43 .Fn strunvis "char *dst" "const char *src"
45 .Fn strnunvis "char *dst" "const char *src" "size_t size"
52 functions are used to decode a visual representation of characters,
55 function, back into the original form.
57 is called with successive characters in
60 sequence is recognized, at which time the decoded character is
61 available at the character pointed to by
65 decodes the characters pointed to by
67 into the buffer pointed to by
71 decodes the characters pointed to by
73 into the buffer pointed to by
80 function simply copies
84 decoding any escape sequences along the way,
85 and returns the number of characters placed into
88 invalid escape sequence was detected.
94 (that is, no expansion takes place during decoding).
96 terminates the destination string with a trailing NUL byte;
104 function implements a state machine that can be used to decode an arbitrary
106 All state associated with the bytes being decoded is stored outside the
108 function (that is, a pointer to the state is passed in), so
109 calls decoding different streams can be freely intermixed.
110 To start decoding a stream of bytes, first initialize an integer
114 with each successive byte, along with a pointer
115 to this integer, and a pointer to a destination character.
119 function has several return codes that must be handled properly.
121 .Bl -tag -width UNVIS_VALIDPUSH
123 Another character is necessary; nothing has been recognized yet.
125 A valid character has been recognized and is available at the location
128 .It Dv UNVIS_VALIDPUSH
129 A valid character has been recognized and is available at the location
132 however, the character currently passed in should be passed in again.
134 A valid sequence was detected, but no character was produced.
135 This return code is necessary to indicate a logical break between characters.
137 An invalid escape sequence was detected, or the decoder is in an
139 The decoder is placed into the starting state.
142 When all bytes in the stream have been processed, call
148 to extract any remaining character (the character passed in is ignored).
152 function returns the number of bytes written (not counting
153 the trailing NUL byte) or \-1 if an error occurred.
157 function returns the number of bytes (not counting the trailing NUL byte)
158 that would be needed to fully convert the input string, or \-1 if an
161 The following code fragment illustrates a proper use of
163 .Bd -literal -offset indent
167 while ((ch = getchar()) != EOF) {
169 switch(unvis(&out, ch, &state, 0)) {
176 case UNVIS_VALIDPUSH:
180 (void)fprintf(stderr, "bad sequence!\en");
184 if (unvis(&out, '\0', &state, UNVIS_END) == UNVIS_VALID)
192 function first appeared in