add radixsort.c
[unix-history] / usr / src / lib / libc / gen / unvis.3
CommitLineData
56df4199
MT
1.\" Copyright (c) 1989 The Regents of the University of California.
2.\" All rights reserved.
3.\"
947d15f7 4.\" %sccs.include.redist.man%
56df4199 5.\"
947d15f7 6.\" @(#)unvis.3 1.2 (Berkeley) %G%
56df4199 7.\"
947d15f7 8.TH UNVIS 3 ""
56df4199
MT
9.UC 7
10.SH NAME
947d15f7 11unvis, strunvis - decode a visual representation of characters
56df4199
MT
12.SH SYNOPSIS
13.nf
14.ft B
947d15f7 15#include <vis.h>
56df4199 16
947d15f7
MT
17int unvis(cp, c, astate, flag)
18u_char *cp, c;
19int *astate, flag;
20
21int strunvis(dst, src)
22char *dst, *src;
56df4199 23
56df4199
MT
24.ft R
25.fi
26.SH DESCRIPTION
947d15f7
MT
27.I Unvis
28and
29.I strunvis
30are used to decode a visual representation of characters, as produced
31by the vis(3) function, back into
32its original form. Unvis is called with successive characters in c
33until a valid
34sequence is recognized, at which time the decoded character is
35available at the character pointed to by cp. Strunvis decodes the
36characters pointed to by src into the buffer pointed to by dst.
37.LP
38.I Strunvis
39simply copies src to dst, decoding any escape sequences along the way,
40and returns the number of characters placed into dst, or -1 if an
41invalid escape sequence was detected. The size of dst should be
42equal to the size of src (that is, no expansion takes place during
43decoding).
44.LP
45.I Unvis
46implements a state machine that can be used to decode an arbitrary
47stream of bytes. All state associated with the bytes being decoded
48is stored outside the
49.I unvis
50function (that is, a pointer to the state is passed in), so
51calls decoding different streams can be freely intermixed. To
52start decoding a stream of bytes, first initialize an integer
53to zero. Call unvis with each successive byte, along with a pointer
54to this integer, and a pointer to an destination character.
55.I Vis
56has several return codes that must be handled properly. They are:
57.TP
580 (zero)
59Another character is necessary; nothing has been recognized yet.
56df4199 60.TP
947d15f7
MT
61UNVIS_VALID
62A valid character has been recognized and is available at the location
63pointed to by cp.
56df4199 64.TP
947d15f7
MT
65UNVIS_VALIDPUSH
66A valid character has been recognized and is available at the location
67pointed to by cp; however, the character currently passed in should
68be passed in again.
56df4199 69.TP
947d15f7
MT
70UNVIS_NOCHAR
71A valid sequence was detected, but no character was produced. This
72return code is necessary to indicate a logical break between characters.
56df4199 73.TP
947d15f7
MT
74UNVIS_SYNBAD
75An invalid esacpe sequence was detected, or the decoder is in an
76unknown state. The decoder is placed into the starting state.
77.LP
78When all bytes in the stream have been processed, call
79.I unvis
80one more time with flag set to
81.B UNVIS_END
82to extract any remaining character (the character passed in is ignored).
83.LP
84The following code fragment illustrates a proper use of
85.IR unvis .
56df4199
MT
86.PP
87.nf
947d15f7
MT
88int state = 0;
89char out;
56df4199
MT
90
91while ((ch = getchar()) != EOF) {
92again:
947d15f7
MT
93 switch(unvis(&out, ch, &state, 0)) {
94 case 0:
95 case UNVIS_NOCHAR:
56df4199 96 break;
947d15f7
MT
97 case UNVIS_VALID:
98 (void) putchar(out);
56df4199 99 break;
947d15f7
MT
100 case UNVIS_VALIDPUSH:
101 (void) putchar(out);
56df4199 102 goto again;
947d15f7 103 case UNVIS_SYNBAD:
56df4199
MT
104 (void)fprintf(stderr, "bad sequence!\n");
105 exit(1);
106 }
107}
947d15f7
MT
108if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID)
109 (void) putchar(out);
56df4199
MT
110.fi
111.SH "SEE ALSO"
112vis(1)