]>
Commit | Line | Data |
---|---|---|
302d677f | 1 | /* $Id$ |
2 | * vis functions - visually encode characters | |
3 | * Originally from OpenBSD | |
4 | */ | |
5 | ||
6 | /*- | |
7 | * Copyright (c) 1989, 1993 | |
8 | * The Regents of the University of California. All rights reserved. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * 3. All advertising materials mentioning features or use of this software | |
19 | * must display the following acknowledgement: | |
20 | * This product includes software developed by the University of | |
21 | * California, Berkeley and its contributors. | |
22 | * 4. Neither the name of the University nor the names of its contributors | |
23 | * may be used to endorse or promote products derived from this software | |
24 | * without specific prior written permission. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
36 | * SUCH DAMAGE. | |
37 | */ | |
38 | ||
39 | #include <sys/types.h> | |
40 | #include <limits.h> | |
41 | #include <ctype.h> | |
42 | /* | |
43 | * to select alternate encoding format | |
44 | */ | |
45 | #define VIS_OCTAL 0x01 /* use octal \ddd format */ | |
46 | #define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropriate */ | |
47 | /* | |
48 | * to alter set of characters encoded (default is to encode all | |
49 | * non-graphic except space, tab, and newline). | |
50 | */ | |
51 | #define VIS_SP 0x04 /* also encode space */ | |
52 | #define VIS_TAB 0x08 /* also encode tab */ | |
53 | #define VIS_NL 0x10 /* also encode newline */ | |
54 | #define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) | |
55 | #define VIS_SAFE 0x20 /* only encode "unsafe" characters */ | |
56 | ||
57 | #define VIS_NOSLASH 0x40 /* inhibit printing '\' */ | |
58 | ||
59 | /* | |
60 | * unvis return codes | |
61 | */ | |
62 | #define UNVIS_VALID 1 /* character valid */ | |
63 | #define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */ | |
64 | #define UNVIS_NOCHAR 3 /* valid sequence, no character produced */ | |
65 | #define UNVIS_SYNBAD -1 /* unrecognized escape sequence */ | |
66 | #define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */ | |
67 | ||
68 | /* | |
69 | * unvis flags | |
70 | */ | |
71 | #define UNVIS_END 1 /* no more characters */ | |
72 | ||
73 | #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') | |
74 | #define isvisible(c) (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \ | |
75 | isgraph((u_char)(c))) || \ | |
76 | ((flag & VIS_SP) == 0 && (c) == ' ') || \ | |
77 | ((flag & VIS_TAB) == 0 && (c) == '\t') || \ | |
78 | ((flag & VIS_NL) == 0 && (c) == '\n') || \ | |
79 | ((flag & VIS_SAFE) && \ | |
80 | ((c) == '\b' || (c) == '\007' || (c) == '\r'))) | |
81 | ||
82 | /* | |
83 | * decode driven by state machine | |
84 | */ | |
85 | #define S_GROUND 0 /* haven't seen escape char */ | |
86 | #define S_START 1 /* start decoding special sequence */ | |
87 | #define S_META 2 /* metachar started (M) */ | |
88 | #define S_META1 3 /* metachar more, regular char (-) */ | |
89 | #define S_CTRL 4 /* control char started (^) */ | |
90 | #define S_OCTAL2 5 /* octal digit 2 */ | |
91 | #define S_OCTAL3 6 /* octal digit 3 */ | |
92 | ||
93 | char *vis(char *, int, int, int); | |
94 | int strvis(char *, const char *, int); | |
95 | int strnvis(char *, const char *, size_t, int); | |
96 | int strvisx(char *, const char *, size_t, int); | |
97 | int strunvis(char *, const char *); | |
98 | int unvis(char *, char, int *, int); |