~ubuntu-branches/ubuntu/oneiric/libbsd/oneiric

« back to all changes in this revision

Viewing changes to src/vis.c

  • Committer: Bazaar Package Importer
  • Author(s): Guillem Jover
  • Date: 2008-07-26 22:57:42 UTC
  • Revision ID: james.westby@ubuntu.com-20080726225742-bqrhxevmabivwctv
Tags: upstream-0.0.1
ImportĀ upstreamĀ versionĀ 0.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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. Neither the name of the University nor the names of its contributors
 
14
 *    may be used to endorse or promote products derived from this software
 
15
 *    without specific prior written permission.
 
16
 *
 
17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
27
 * SUCH DAMAGE.
 
28
 */
 
29
 
 
30
#include <sys/types.h>
 
31
#include <limits.h>
 
32
#include <ctype.h>
 
33
#include <stdio.h>
 
34
#include <vis.h>
 
35
 
 
36
#define isoctal(c)      (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
 
37
 
 
38
/*
 
39
 * vis - visually encode characters
 
40
 */
 
41
char *
 
42
vis(dst, c, flag, nextc)
 
43
        char *dst;
 
44
        int c, nextc;
 
45
        int flag;
 
46
{
 
47
        c = (unsigned char)c;
 
48
 
 
49
        if (flag & VIS_HTTPSTYLE) {
 
50
                /* Described in RFC 1808 */
 
51
                if (!(isalnum(c) /* alpha-numeric */
 
52
                    /* safe */
 
53
                    || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
 
54
                    /* extra */
 
55
                    || c == '!' || c == '*' || c == '\'' || c == '('
 
56
                    || c == ')' || c == ',')) {
 
57
                        *dst++ = '%';
 
58
                        snprintf(dst, 4, (c < 16 ? "0%X" : "%X"), c);
 
59
                        dst += 2;
 
60
                        goto done;
 
61
                }
 
62
        }
 
63
 
 
64
        if ((flag & VIS_GLOB) &&
 
65
            (c == '*' || c == '?' || c == '[' || c == '#'))
 
66
                ;
 
67
        else if (isgraph(c) ||
 
68
           ((flag & VIS_SP) == 0 && c == ' ') ||
 
69
           ((flag & VIS_TAB) == 0 && c == '\t') ||
 
70
           ((flag & VIS_NL) == 0 && c == '\n') ||
 
71
           ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
 
72
                *dst++ = c;
 
73
                if (c == '\\' && (flag & VIS_NOSLASH) == 0)
 
74
                        *dst++ = '\\';
 
75
                *dst = '\0';
 
76
                return (dst);
 
77
        }
 
78
 
 
79
        if (flag & VIS_CSTYLE) {
 
80
                switch(c) {
 
81
                case '\n':
 
82
                        *dst++ = '\\';
 
83
                        *dst++ = 'n';
 
84
                        goto done;
 
85
                case '\r':
 
86
                        *dst++ = '\\';
 
87
                        *dst++ = 'r';
 
88
                        goto done;
 
89
                case '\b':
 
90
                        *dst++ = '\\';
 
91
                        *dst++ = 'b';
 
92
                        goto done;
 
93
                case '\a':
 
94
                        *dst++ = '\\';
 
95
                        *dst++ = 'a';
 
96
                        goto done;
 
97
                case '\v':
 
98
                        *dst++ = '\\';
 
99
                        *dst++ = 'v';
 
100
                        goto done;
 
101
                case '\t':
 
102
                        *dst++ = '\\';
 
103
                        *dst++ = 't';
 
104
                        goto done;
 
105
                case '\f':
 
106
                        *dst++ = '\\';
 
107
                        *dst++ = 'f';
 
108
                        goto done;
 
109
                case ' ':
 
110
                        *dst++ = '\\';
 
111
                        *dst++ = 's';
 
112
                        goto done;
 
113
                case '\0':
 
114
                        *dst++ = '\\';
 
115
                        *dst++ = '0';
 
116
                        if (isoctal(nextc)) {
 
117
                                *dst++ = '0';
 
118
                                *dst++ = '0';
 
119
                        }
 
120
                        goto done;
 
121
                }
 
122
        }
 
123
        if (((c & 0177) == ' ') || isgraph(c) || (flag & VIS_OCTAL)) {
 
124
                *dst++ = '\\';
 
125
                *dst++ = ((u_char)c >> 6 & 07) + '0';
 
126
                *dst++ = ((u_char)c >> 3 & 07) + '0';
 
127
                *dst++ = ((u_char)c & 07) + '0';
 
128
                goto done;
 
129
        }
 
130
        if ((flag & VIS_NOSLASH) == 0)
 
131
                *dst++ = '\\';
 
132
        if (c & 0200) {
 
133
                c &= 0177;
 
134
                *dst++ = 'M';
 
135
        }
 
136
        if (iscntrl(c)) {
 
137
                *dst++ = '^';
 
138
                if (c == 0177)
 
139
                        *dst++ = '?';
 
140
                else
 
141
                        *dst++ = c + '@';
 
142
        } else {
 
143
                *dst++ = '-';
 
144
                *dst++ = c;
 
145
        }
 
146
done:
 
147
        *dst = '\0';
 
148
        return (dst);
 
149
}
 
150
 
 
151
/*
 
152
 * strvis, strvisx - visually encode characters from src into dst
 
153
 *
 
154
 *      Dst must be 4 times the size of src to account for possible
 
155
 *      expansion.  The length of dst, not including the trailing NUL,
 
156
 *      is returned.
 
157
 *
 
158
 *      Strvisx encodes exactly len bytes from src into dst.
 
159
 *      This is useful for encoding a block of data.
 
160
 */
 
161
int
 
162
strvis(dst, src, flag)
 
163
        char *dst;
 
164
        const char *src;
 
165
        int flag;
 
166
{
 
167
        char c;
 
168
        char *start;
 
169
 
 
170
        for (start = dst; (c = *src); )
 
171
                dst = vis(dst, c, flag, *++src);
 
172
        *dst = '\0';
 
173
        return (dst - start);
 
174
}
 
175
 
 
176
int
 
177
strvisx(dst, src, len, flag)
 
178
        char *dst;
 
179
        const char *src;
 
180
        size_t len;
 
181
        int flag;
 
182
{
 
183
        int c;
 
184
        char *start;
 
185
 
 
186
        for (start = dst; len > 1; len--) {
 
187
                c = *src;
 
188
                dst = vis(dst, c, flag, *++src);
 
189
        }
 
190
        if (len)
 
191
                dst = vis(dst, *src, flag, '\0');
 
192
        *dst = '\0';
 
193
 
 
194
        return (dst - start);
 
195
}