]> andersk Git - openssh.git/blob - openbsd-compat/vis.c
- Sync with OpenBSD glob.c, strlcat.c and vis.c changes
[openssh.git] / openbsd-compat / vis.c
1 /*-
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char rcsid[] = "$OpenBSD: vis.c,v 1.6 2000/11/21 00:47:28 millert Exp $";
36 #endif /* LIBC_SCCS and not lint */
37
38 #include "includes.h"
39
40 #ifndef HAVE_VIS
41
42 #define isoctal(c)      (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
43 #define isvisible(c)    (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \
44                                 isgraph((u_char)(c))) ||                     \
45                                 ((flag & VIS_SP) == 0 && (c) == ' ') ||      \
46                                 ((flag & VIS_TAB) == 0 && (c) == '\t') ||    \
47                                 ((flag & VIS_NL) == 0 && (c) == '\n') ||     \
48                                 ((flag & VIS_SAFE) &&                        \
49                                 ((c) == '\b' || (c) == '\007' || (c) == '\r')))
50
51 /*
52  * vis - visually encode characters
53  */
54 char *vis(char *dst, int c, int flag, int nextc)
55 {
56         if (isvisible(c)) {
57                 *dst++ = c;
58                 if (c == '\\' && (flag & VIS_NOSLASH) == 0)
59                         *dst++ = '\\';
60                 *dst = '\0';
61                 return (dst);
62         }
63
64         if (flag & VIS_CSTYLE) {
65                 switch(c) {
66                 case '\n':
67                         *dst++ = '\\';
68                         *dst++ = 'n';
69                         goto done;
70                 case '\r':
71                         *dst++ = '\\';
72                         *dst++ = 'r';
73                         goto done;
74                 case '\b':
75                         *dst++ = '\\';
76                         *dst++ = 'b';
77                         goto done;
78 #ifdef __STDC__
79                 case '\a':
80 #else
81                 case '\007':
82 #endif
83                         *dst++ = '\\';
84                         *dst++ = 'a';
85                         goto done;
86                 case '\v':
87                         *dst++ = '\\';
88                         *dst++ = 'v';
89                         goto done;
90                 case '\t':
91                         *dst++ = '\\';
92                         *dst++ = 't';
93                         goto done;
94                 case '\f':
95                         *dst++ = '\\';
96                         *dst++ = 'f';
97                         goto done;
98                 case ' ':
99                         *dst++ = '\\';
100                         *dst++ = 's';
101                         goto done;
102                 case '\0':
103                         *dst++ = '\\';
104                         *dst++ = '0';
105                         if (isoctal(nextc)) {
106                                 *dst++ = '0';
107                                 *dst++ = '0';
108                         }
109                         goto done;
110                 }
111         }
112         if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) {        
113                 *dst++ = '\\';
114                 *dst++ = ((u_char)c >> 6 & 07) + '0';
115                 *dst++ = ((u_char)c >> 3 & 07) + '0';
116                 *dst++ = ((u_char)c & 07) + '0';
117                 goto done;
118         }
119         if ((flag & VIS_NOSLASH) == 0)
120                 *dst++ = '\\';
121         if (c & 0200) {
122                 c &= 0177;
123                 *dst++ = 'M';
124         }
125         if (iscntrl(c)) {
126                 *dst++ = '^';
127                 if (c == 0177)
128                         *dst++ = '?';
129                 else
130                         *dst++ = c + '@';
131         } else {
132                 *dst++ = '-';
133                 *dst++ = c;
134         }
135 done:
136         *dst = '\0';
137         return (dst);
138 }
139 #endif /* HAVE_VIS */
This page took 0.048406 seconds and 5 git commands to generate.