Commit | Line | Data |
---|---|---|
ad787160 C |
1 | .\" Copyright (c) 1989, 1991, 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
56df4199 | 3 | .\" |
ad787160 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 | .\" |
ad787160 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 | .\" |
ad787160 C |
32 | .\" @(#)unvis.3 8.1 (Berkeley) 6/9/93 |
33 | .\" | |
34 | .Dd June 9, 1993 | |
ae59e04c CL |
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 | |
947d15f7 | 50 | and |
ae59e04c CL |
51 | .Fn strunvis |
52 | functions | |
947d15f7 | 53 | are used to decode a visual representation of characters, as produced |
ae59e04c CL |
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 | |
947d15f7 MT |
59 | until a valid |
60 | sequence is recognized, at which time the decoded character is | |
ae59e04c CL |
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 | |
947d15f7 | 86 | decoding). |
ae59e04c CL |
87 | .Pp |
88 | The | |
89 | .Fn unvis | |
90 | function | |
947d15f7 MT |
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 | |
ae59e04c | 94 | .Fn unvis |
947d15f7 MT |
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 | |
ae59e04c CL |
98 | to zero. Call |
99 | .Fn unvis | |
100 | with each successive byte, along with a pointer | |
947d15f7 | 101 | to this integer, and a pointer to an destination character. |
ae59e04c CL |
102 | The |
103 | .Xr unvis | |
104 | function | |
947d15f7 | 105 | has several return codes that must be handled properly. They are: |
ae59e04c CL |
106 | .Bl -tag -width UNVIS_VALIDPUSH |
107 | .It Li \&0 (zero) | |
947d15f7 | 108 | Another character is necessary; nothing has been recognized yet. |
ae59e04c | 109 | .It Dv UNVIS_VALID |
947d15f7 MT |
110 | A valid character has been recognized and is available at the location |
111 | pointed to by cp. | |
ae59e04c | 112 | .It Dv UNVIS_VALIDPUSH |
947d15f7 MT |
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. | |
ae59e04c | 116 | .It Dv UNVIS_NOCHAR |
947d15f7 MT |
117 | A valid sequence was detected, but no character was produced. This |
118 | return code is necessary to indicate a logical break between characters. | |
ae59e04c | 119 | .It Dv UNVIS_SYNBAD |
947d15f7 MT |
120 | An invalid esacpe sequence was detected, or the decoder is in an |
121 | unknown state. The decoder is placed into the starting state. | |
ae59e04c CL |
122 | .El |
123 | .Pp | |
947d15f7 | 124 | When all bytes in the stream have been processed, call |
ae59e04c | 125 | .Fn unvis |
947d15f7 | 126 | one more time with flag set to |
ae59e04c | 127 | .Dv UNVIS_END |
947d15f7 | 128 | to extract any remaining character (the character passed in is ignored). |
ae59e04c | 129 | .Pp |
947d15f7 | 130 | The following code fragment illustrates a proper use of |
ae59e04c CL |
131 | .Fn unvis . |
132 | .Bd -literal -offset indent | |
947d15f7 MT |
133 | int state = 0; |
134 | char out; | |
56df4199 MT |
135 | |
136 | while ((ch = getchar()) != EOF) { | |
137 | again: | |
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"); |
ae59e04c | 150 | exit(1); |
56df4199 MT |
151 | } |
152 | } | |
947d15f7 MT |
153 | if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID) |
154 | (void) putchar(out); | |
ae59e04c CL |
155 | .Ed |
156 | .Sh SEE ALSO | |
157 | .Xr vis 1 | |
158 | .Sh HISTORY | |
159 | The | |
ad787160 C |
160 | .Nm unvis |
161 | function | |
162 | first appeared in 4.4BSD. |