xref: /csrg-svn/lib/libc/gen/unvis.3 (revision 65098)
162871Sbostic.\" Copyright (c) 1989, 1991, 1993
262871Sbostic.\"	The Regents of the University of California.  All rights reserved.
341933Smarc.\"
444332Smarc.\" %sccs.include.redist.man%
541933Smarc.\"
6*65098Smckusick.\"     @(#)unvis.3	8.2 (Berkeley) 12/11/93
741933Smarc.\"
848352Scael.Dd
948352Scael.Dt UNVIS 3
1048352Scael.Os
1148352Scael.Sh NAME
1248352Scael.Nm unvis ,
1348352Scael.Nm strunvis
1448352Scael.Nd decode a visual representation of characters
1548352Scael.Sh SYNOPSIS
1648352Scael.Fd #include <vis.h>
1748352Scael.Ft int
1848352Scael.Fn unvis "u_char *cp" "u_char c" "int *astate" "int flag"
1948352Scael.Ft int
2048352Scael.Fn strunvis "char *dst" "char *src"
2148352Scael.Sh DESCRIPTION
2248352ScaelThe
2348352Scael.Fn unvis
2444332Smarcand
2548352Scael.Fn strunvis
2648352Scaelfunctions
2744332Smarcare used to decode a visual representation of characters, as produced
2848352Scaelby the
2948352Scael.Xr vis 3
3048352Scaelfunction, back into
3148352Scaelthe original form.  Unvis is called with successive characters in
3248352Scael.Ar c
3344332Smarcuntil a valid
3444332Smarcsequence is recognized, at which time the decoded character is
3548352Scaelavailable at the character pointed to by
3648352Scael.Ar cp .
3748352ScaelStrunvis decodes the
3848352Scaelcharacters pointed to by
3948352Scael.Ar src
4048352Scaelinto the buffer pointed to by
4148352Scael.Ar dst .
4248352Scael.Pp
4348352ScaelThe
4448352Scael.Fn strunvis
4548352Scaelfunction
4648352Scaelsimply copies
4748352Scael.Ar src
4848352Scaelto
4948352Scael.Ar dst ,
5048352Scaeldecoding any escape sequences along the way,
5148352Scaeland returns the number of characters placed into
5248352Scael.Ar dst ,
5348352Scaelor \-1 if an
5448352Scaelinvalid escape sequence was detected.  The size of
5548352Scael.Ar dst
5648352Scaelshould be
5748352Scaelequal to the size of
5848352Scael.Ar src
5948352Scael(that is, no expansion takes place during
6044332Smarcdecoding).
6148352Scael.Pp
6248352ScaelThe
6348352Scael.Fn unvis
6448352Scaelfunction
6544332Smarcimplements a state machine that can be used to decode an arbitrary
6644332Smarcstream of bytes.  All state associated with the bytes being decoded
6744332Smarcis stored outside the
6848352Scael.Fn unvis
6944332Smarcfunction (that is, a pointer to the state is passed in), so
7044332Smarccalls decoding different streams can be freely intermixed.  To
7144332Smarcstart decoding a stream of bytes, first initialize an integer
7248352Scaelto zero.  Call
7348352Scael.Fn unvis
7448352Scaelwith each successive byte, along with a pointer
75*65098Smckusickto this integer, and a pointer to a destination character.
7648352ScaelThe
7748352Scael.Xr unvis
7848352Scaelfunction
7944332Smarchas several return codes that must be handled properly.  They are:
8048352Scael.Bl -tag -width UNVIS_VALIDPUSH
8148352Scael.It Li \&0 (zero)
8244332SmarcAnother character is necessary; nothing has been recognized yet.
8348352Scael.It Dv  UNVIS_VALID
8444332SmarcA valid character has been recognized and is available at the location
8544332Smarcpointed to by cp.
8648352Scael.It Dv  UNVIS_VALIDPUSH
8744332SmarcA valid character has been recognized and is available at the location
8844332Smarcpointed to by cp; however, the character currently passed in should
8944332Smarcbe passed in again.
9048352Scael.It Dv  UNVIS_NOCHAR
9144332SmarcA valid sequence was detected, but no character was produced.  This
9244332Smarcreturn code is necessary to indicate a logical break between characters.
9348352Scael.It Dv  UNVIS_SYNBAD
94*65098SmckusickAn invalid escape sequence was detected, or the decoder is in an
9544332Smarcunknown state.  The decoder is placed into the starting state.
9648352Scael.El
9748352Scael.Pp
9844332SmarcWhen all bytes in the stream have been processed, call
9948352Scael.Fn unvis
10044332Smarcone more time with flag set to
10148352Scael.Dv UNVIS_END
10244332Smarcto extract any remaining character (the character passed in is ignored).
10348352Scael.Pp
10444332SmarcThe following code fragment illustrates a proper use of
10548352Scael.Fn unvis .
10648352Scael.Bd -literal -offset indent
10744332Smarcint state = 0;
10844332Smarcchar out;
10941933Smarc
11041933Smarcwhile ((ch = getchar()) != EOF) {
11141933Smarcagain:
11244332Smarc	switch(unvis(&out, ch, &state, 0)) {
11344332Smarc	case 0:
11444332Smarc	case UNVIS_NOCHAR:
11541933Smarc		break;
11644332Smarc	case UNVIS_VALID:
11744332Smarc		(void) putchar(out);
11841933Smarc		break;
11944332Smarc	case UNVIS_VALIDPUSH:
12044332Smarc		(void) putchar(out);
12141933Smarc		goto again;
12244332Smarc	case UNVIS_SYNBAD:
12341933Smarc		(void)fprintf(stderr, "bad sequence!\n");
12448352Scael	exit(1);
12541933Smarc	}
12641933Smarc}
12744332Smarcif (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID)
12844332Smarc	(void) putchar(out);
12948352Scael.Ed
13048352Scael.Sh SEE ALSO
13148352Scael.Xr vis 1
13248352Scael.Sh HISTORY
13348352ScaelThe
13462870Sbostic.Nm unvis
13562870Sbosticfunction
13662870Sbosticfirst appeared in 4.4BSD.
137