1UNVIS(3) BSD Library Functions Manual UNVIS(3)
2
4 unvis, strunvis, strnunvis — decode a visual representation of characters
5
7 Utility functions from BSD systems (libbsd, -lbsd)
8
10 #include <vis.h>
11
12 int
13 unvis(char *cp, char c, int *astate, int flag);
14
15 int
16 strunvis(char *dst, char *src);
17
18 ssize_t
19 strnunvis(char *dst, char *src, size_t size);
20
22 The unvis(), strunvis() and strnunvis() functions are used to decode a
23 visual representation of characters, as produced by the vis(3) function,
24 back into the original form. unvis() is called with successive charac‐
25 ters in c until a valid sequence is recognized, at which time the decoded
26 character is available at the character pointed to by cp.
27
28 strunvis() decodes the characters pointed to by src into the buffer
29 pointed to by dst.
30
31 strnunvis() decodes the characters pointed to by src into the buffer
32 pointed to by dst, writing a maximum of size bytes. The strunvis() func‐
33 tion simply copies src to dst, decoding any escape sequences along the
34 way, and returns the number of characters placed into dst, or -1 if an
35 invalid escape sequence was detected. The size of dst should be equal to
36 the size of src (that is, no expansion takes place during decoding).
37 strunvis() terminates the destination string with a trailing NUL byte;
38 strnunvis() does so if size is larger than 0.
39
40 The unvis() function implements a state machine that can be used to
41 decode an arbitrary stream of bytes. All state associated with the bytes
42 being decoded is stored outside the unvis() function (that is, a pointer
43 to the state is passed in), so calls decoding different streams can be
44 freely intermixed. To start decoding a stream of bytes, first initialize
45 an integer to zero. Call unvis() with each successive byte, along with a
46 pointer to this integer, and a pointer to a destination character.
47
49 The unvis() function has several return codes that must be handled prop‐
50 erly. They are:
51
52 0 (zero) Another character is necessary; nothing has been recog‐
53 nized yet.
54
55 UNVIS_VALID A valid character has been recognized and is available
56 at the location pointed to by cp.
57
58 UNVIS_VALIDPUSH A valid character has been recognized and is available
59 at the location pointed to by cp; however, the character
60 currently passed in should be passed in again.
61
62 UNVIS_NOCHAR A valid sequence was detected, but no character was pro‐
63 duced. This return code is necessary to indicate a log‐
64 ical break between characters.
65
66 UNVIS_SYNBAD An invalid escape sequence was detected, or the decoder
67 is in an unknown state. The decoder is placed into the
68 starting state.
69
70 When all bytes in the stream have been processed, call unvis() one more
71 time with flag set to UNVIS_END to extract any remaining character (the
72 character passed in is ignored).
73
74 The strunvis() function returns the number of bytes written (not counting
75 the trailing NUL byte) or -1 if an error occurred.
76
77 The strnunvis() function returns the number of bytes (not counting the
78 trailing NUL byte) that would be needed to fully convert the input
79 string, or -1 if an error occurred.
80
82 The following code fragment illustrates a proper use of unvis().
83
84 int state = 0;
85 char out;
86
87 while ((ch = getchar()) != EOF) {
88 again:
89 switch(unvis(&out, ch, &state, 0)) {
90 case 0:
91 case UNVIS_NOCHAR:
92 break;
93 case UNVIS_VALID:
94 (void) putchar(out);
95 break;
96 case UNVIS_VALIDPUSH:
97 (void) putchar(out);
98 goto again;
99 case UNVIS_SYNBAD:
100 (void)fprintf(stderr, "bad sequence!\n");
101 exit(1);
102 }
103 }
104 if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID)
105 (void) putchar(out);
106
108 unvis(1), vis(1), vis(3)
109
111 The unvis() function first appeared in 4.4BSD.
112
113BSD June 22, 2019 BSD