Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | .\" Copyright (c) 1989, 1991 The Regents of the University of California. |
2 | .\" 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. | |
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. | |
19 | .\" | |
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. | |
31 | .\" | |
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 | |
48 | The | |
49 | .Fn unvis | |
50 | and | |
51 | .Fn strunvis | |
52 | functions | |
53 | are used to decode a visual representation of characters, as produced | |
54 | by the | |
55 | .Xr vis 3 | |
56 | function, back into | |
57 | the original form. Unvis is called with successive characters in | |
58 | .Ar c | |
59 | until a valid | |
60 | sequence is recognized, at which time the decoded character is | |
61 | available at the character pointed to by | |
62 | .Ar cp . | |
63 | Strunvis decodes the | |
64 | characters pointed to by | |
65 | .Ar src | |
66 | into the buffer pointed to by | |
67 | .Ar dst . | |
68 | .Pp | |
69 | The | |
70 | .Fn strunvis | |
71 | function | |
72 | simply copies | |
73 | .Ar src | |
74 | to | |
75 | .Ar dst , | |
76 | decoding any escape sequences along the way, | |
77 | and returns the number of characters placed into | |
78 | .Ar dst , | |
79 | or \-1 if an | |
80 | invalid escape sequence was detected. The size of | |
81 | .Ar dst | |
82 | should be | |
83 | equal to the size of | |
84 | .Ar src | |
85 | (that is, no expansion takes place during | |
86 | decoding). | |
87 | .Pp | |
88 | The | |
89 | .Fn unvis | |
90 | function | |
91 | implements a state machine that can be used to decode an arbitrary | |
92 | stream of bytes. All state associated with the bytes being decoded | |
93 | is stored outside the | |
94 | .Fn unvis | |
95 | function (that is, a pointer to the state is passed in), so | |
96 | calls decoding different streams can be freely intermixed. To | |
97 | start decoding a stream of bytes, first initialize an integer | |
98 | to zero. Call | |
99 | .Fn unvis | |
100 | with each successive byte, along with a pointer | |
101 | to this integer, and a pointer to an destination character. | |
102 | The | |
103 | .Xr unvis | |
104 | function | |
105 | has several return codes that must be handled properly. They are: | |
106 | .Bl -tag -width UNVIS_VALIDPUSH | |
107 | .It Li \&0 (zero) | |
108 | Another character is necessary; nothing has been recognized yet. | |
109 | .It Dv UNVIS_VALID | |
110 | A valid character has been recognized and is available at the location | |
111 | pointed to by cp. | |
112 | .It Dv UNVIS_VALIDPUSH | |
113 | A valid character has been recognized and is available at the location | |
114 | pointed to by cp; however, the character currently passed in should | |
115 | be passed in again. | |
116 | .It Dv UNVIS_NOCHAR | |
117 | A valid sequence was detected, but no character was produced. This | |
118 | return code is necessary to indicate a logical break between characters. | |
119 | .It Dv UNVIS_SYNBAD | |
120 | An invalid esacpe sequence was detected, or the decoder is in an | |
121 | unknown state. The decoder is placed into the starting state. | |
122 | .El | |
123 | .Pp | |
124 | When all bytes in the stream have been processed, call | |
125 | .Fn unvis | |
126 | one more time with flag set to | |
127 | .Dv UNVIS_END | |
128 | to extract any remaining character (the character passed in is ignored). | |
129 | .Pp | |
130 | The following code fragment illustrates a proper use of | |
131 | .Fn unvis . | |
132 | .Bd -literal -offset indent | |
133 | int state = 0; | |
134 | char out; | |
135 | ||
136 | while ((ch = getchar()) != EOF) { | |
137 | again: | |
138 | switch(unvis(&out, ch, &state, 0)) { | |
139 | case 0: | |
140 | case UNVIS_NOCHAR: | |
141 | break; | |
142 | case UNVIS_VALID: | |
143 | (void) putchar(out); | |
144 | break; | |
145 | case UNVIS_VALIDPUSH: | |
146 | (void) putchar(out); | |
147 | goto again; | |
148 | case UNVIS_SYNBAD: | |
149 | (void)fprintf(stderr, "bad sequence!\n"); | |
150 | exit(1); | |
151 | } | |
152 | } | |
153 | if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID) | |
154 | (void) putchar(out); | |
155 | .Ed | |
156 | .Sh SEE ALSO | |
157 | .Xr vis 1 | |
158 | .Sh HISTORY | |
159 | The | |
160 | .Fn unvis | |
161 | function is | |
162 | .Ud . |