source: rtems/cpukit/libmisc/shell/vis.c @ 6cdaa85

5
Last change on this file since 6cdaa85 was 6cdaa85, checked in by Sebastian Huber <sebastian.huber@…>, on 10/04/18 at 18:16:45

shell: Use #include "..." for local header files

Update #3375.

  • Property mode set to 100644
File size: 11.4 KB
Line 
1/*      $NetBSD: vis.c,v 1.33 2005/05/28 13:11:14 lukem Exp $   */
2
3/*-
4 * Copyright (c) 1989, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*-
33 * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 *    must display the following acknowledgement:
46 *        This product includes software developed by the NetBSD
47 *        Foundation, Inc. and its contributors.
48 * 4. Neither the name of The NetBSD Foundation nor the names of its
49 *    contributors may be used to endorse or promote products derived
50 *    from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
63 */
64
65#ifdef HAVE_CONFIG_H
66#include "config.h"
67#endif
68
69#define _DIAGASSERT(a)
70
71#if 0
72#include <sys/cdefs.h>
73#if defined(LIBC_SCCS) && !defined(lint)
74__RCSID("$NetBSD: vis.c,v 1.33 2005/05/28 13:11:14 lukem Exp $");
75#endif /* LIBC_SCCS and not lint */
76#endif
77
78#include <sys/types.h>
79
80#include "vis.h"
81#include <stdlib.h>
82
83#if !HAVE_VIS || !HAVE_SVIS
84#include <ctype.h>
85#include <limits.h>
86#include <stdio.h>
87#include <string.h>
88
89#undef BELL
90#define BELL '\a'
91
92#define isoctal(c)      (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
93#define iswhite(c)      (c == ' ' || c == '\t' || c == '\n')
94#define issafe(c)       (c == '\b' || c == BELL || c == '\r')
95#define xtoa(c)         "0123456789abcdef"[c]
96
97#define MAXEXTRAS       5
98
99
100#define MAKEEXTRALIST(flag, extra, orig)                                      \
101do {                                                                          \
102        const char *o = orig;                                                 \
103        char *e;                                                              \
104        while (*o++)                                                          \
105                continue;                                                     \
106        extra = malloc((size_t)((o - orig) + MAXEXTRAS));                     \
107        if (!extra) break;                                                    \
108        for (o = orig, e = extra; (*e++ = *o++) != '\0';)                     \
109                continue;                                                     \
110        e--;                                                                  \
111        if (flag & VIS_SP) *e++ = ' ';                                        \
112        if (flag & VIS_TAB) *e++ = '\t';                                      \
113        if (flag & VIS_NL) *e++ = '\n';                                       \
114        if ((flag & VIS_NOSLASH) == 0) *e++ = '\\';                           \
115        *e = '\0';                                                            \
116} while (/*CONSTCOND*/0)
117
118
119/*
120 * This is HVIS, the macro of vis used to HTTP style (RFC 1808)
121 */
122#define HVIS(dst, c, flag, nextc, extra)                                      \
123do                                                                            \
124        if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \
125                *dst++ = '%';                                                 \
126                *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);                  \
127                *dst++ = xtoa((unsigned int)c & 0xf);                         \
128        } else {                                                              \
129                SVIS(dst, c, flag, nextc, extra);                             \
130        }                                                                     \
131while (/*CONSTCOND*/0)
132
133/*
134 * This is SVIS, the central macro of vis.
135 * dst:       Pointer to the destination buffer
136 * c:         Character to encode
137 * flag:      Flag word
138 * nextc:     The character following 'c'
139 * extra:     Pointer to the list of extra characters to be
140 *            backslash-protected.
141 */
142#define SVIS(dst, c, flag, nextc, extra)                                      \
143do {                                                                          \
144        int isextra;                                                          \
145        isextra = strchr(extra, c) != NULL;                                   \
146        if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||            \
147            ((flag & VIS_SAFE) && issafe(c)))) {                              \
148                *dst++ = c;                                                   \
149                break;                                                        \
150        }                                                                     \
151        if (flag & VIS_CSTYLE) {                                              \
152                switch (c) {                                                  \
153                case '\n':                                                    \
154                        *dst++ = '\\'; *dst++ = 'n';                          \
155                        continue;                                             \
156                case '\r':                                                    \
157                        *dst++ = '\\'; *dst++ = 'r';                          \
158                        continue;                                             \
159                case '\b':                                                    \
160                        *dst++ = '\\'; *dst++ = 'b';                          \
161                        continue;                                             \
162                case BELL:                                                    \
163                        *dst++ = '\\'; *dst++ = 'a';                          \
164                        continue;                                             \
165                case '\v':                                                    \
166                        *dst++ = '\\'; *dst++ = 'v';                          \
167                        continue;                                             \
168                case '\t':                                                    \
169                        *dst++ = '\\'; *dst++ = 't';                          \
170                        continue;                                             \
171                case '\f':                                                    \
172                        *dst++ = '\\'; *dst++ = 'f';                          \
173                        continue;                                             \
174                case ' ':                                                     \
175                        *dst++ = '\\'; *dst++ = 's';                          \
176                        continue;                                             \
177                case '\0':                                                    \
178                        *dst++ = '\\'; *dst++ = '0';                          \
179                        if (isoctal(nextc)) {                                 \
180                                *dst++ = '0';                                 \
181                                *dst++ = '0';                                 \
182                        }                                                     \
183                        continue;                                             \
184                default:                                                      \
185                        if (isgraph(c)) {                                     \
186                                *dst++ = '\\'; *dst++ = c;                    \
187                                continue;                                     \
188                        }                                                     \
189                }                                                             \
190        }                                                                     \
191        if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {           \
192                *dst++ = '\\';                                                \
193                *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';    \
194                *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';    \
195                *dst++ =                             (c       & 07) + '0';    \
196        } else {                                                              \
197                if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\';                 \
198                if (c & 0200) {                                               \
199                        c &= 0177; *dst++ = 'M';                              \
200                }                                                             \
201                if (iscntrl(c)) {                                             \
202                        *dst++ = '^';                                         \
203                        if (c == 0177)                                        \
204                                *dst++ = '?';                                 \
205                        else                                                  \
206                                *dst++ = c + '@';                             \
207                } else {                                                      \
208                        *dst++ = '-'; *dst++ = c;                             \
209                }                                                             \
210        }                                                                     \
211} while (/*CONSTCOND*/0)
212
213
214/*
215 * svis - visually encode characters, also encoding the characters
216 *        pointed to by `extra'
217 */
218char *
219svis(char *dst, int c, int flag, int nextc, const char *extra)
220{
221        char *nextra = NULL;
222
223        _DIAGASSERT(dst != NULL);
224        _DIAGASSERT(extra != NULL);
225        MAKEEXTRALIST(flag, nextra, extra);
226        if (!nextra) {
227                *dst = '\0';            /* can't create nextra, return "" */
228                return dst;
229        }
230        if (flag & VIS_HTTPSTYLE)
231                HVIS(dst, c, flag, nextc, nextra);
232        else
233                SVIS(dst, c, flag, nextc, nextra);
234        free(nextra);
235        *dst = '\0';
236        return dst;
237}
238
239
240/*
241 * strsvis, strsvisx - visually encode characters from src into dst
242 *
243 *      Extra is a pointer to a \0-terminated list of characters to
244 *      be encoded, too. These functions are useful e. g. to
245 *      encode strings in such a way so that they are not interpreted
246 *      by a shell.
247 *
248 *      Dst must be 4 times the size of src to account for possible
249 *      expansion.  The length of dst, not including the trailing NULL,
250 *      is returned.
251 *
252 *      Strsvisx encodes exactly len bytes from src into dst.
253 *      This is useful for encoding a block of data.
254 */
255int
256strsvis(char *dst, const char *csrc, int flag, const char *extra)
257{
258        int c;
259        char *start;
260        char *nextra = NULL;
261        const unsigned char *src = (const unsigned char *)csrc;
262
263        _DIAGASSERT(dst != NULL);
264        _DIAGASSERT(src != NULL);
265        _DIAGASSERT(extra != NULL);
266        MAKEEXTRALIST(flag, nextra, extra);
267        if (!nextra) {
268                *dst = '\0';            /* can't create nextra, return "" */
269                return 0;
270        }
271        if (flag & VIS_HTTPSTYLE) {
272                for (start = dst; (c = *src++) != '\0'; /* empty */)
273                        HVIS(dst, c, flag, *src, nextra);
274        } else {
275                for (start = dst; (c = *src++) != '\0'; /* empty */)
276                        SVIS(dst, c, flag, *src, nextra);
277        }
278        free(nextra);
279        *dst = '\0';
280        return (dst - start);
281}
282
283
284int
285strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
286{
287        unsigned char c;
288        char *start;
289        char *nextra = NULL;
290        const unsigned char *src = (const unsigned char *)csrc;
291
292        _DIAGASSERT(dst != NULL);
293        _DIAGASSERT(src != NULL);
294        _DIAGASSERT(extra != NULL);
295        MAKEEXTRALIST(flag, nextra, extra);
296        if (! nextra) {
297                *dst = '\0';            /* can't create nextra, return "" */
298                return 0;
299        }
300
301        if (flag & VIS_HTTPSTYLE) {
302                for (start = dst; len > 0; len--) {
303                        c = *src++;
304                        HVIS(dst, c, flag, len ? *src : '\0', nextra);
305                }
306        } else {
307                for (start = dst; len > 0; len--) {
308                        c = *src++;
309                        SVIS(dst, c, flag, len ? *src : '\0', nextra);
310                }
311        }
312        free(nextra);
313        *dst = '\0';
314        return (dst - start);
315}
316#endif
317
318#if !HAVE_VIS
319/*
320 * vis - visually encode characters
321 */
322char *
323vis(char *dst, int c, int flag, int nextc)
324{
325        char *extra = NULL;
326        unsigned char uc = (unsigned char)c;
327
328        _DIAGASSERT(dst != NULL);
329
330        MAKEEXTRALIST(flag, extra, "");
331        if (! extra) {
332                *dst = '\0';            /* can't create extra, return "" */
333                return dst;
334        }
335        if (flag & VIS_HTTPSTYLE)
336                HVIS(dst, uc, flag, nextc, extra);
337        else
338                SVIS(dst, uc, flag, nextc, extra);
339        free(extra);
340        *dst = '\0';
341        return dst;
342}
343
344
345/*
346 * strvis, strvisx - visually encode characters from src into dst
347 *
348 *      Dst must be 4 times the size of src to account for possible
349 *      expansion.  The length of dst, not including the trailing NULL,
350 *      is returned.
351 *
352 *      Strvisx encodes exactly len bytes from src into dst.
353 *      This is useful for encoding a block of data.
354 */
355int
356strvis(char *dst, const char *src, int flag)
357{
358        char *extra = NULL;
359        int rv;
360
361        MAKEEXTRALIST(flag, extra, "");
362        if (!extra) {
363                *dst = '\0';            /* can't create extra, return "" */
364                return 0;
365        }
366        rv = strsvis(dst, src, flag, extra);
367        free(extra);
368        return rv;
369}
370
371
372int
373strvisx(char *dst, const char *src, size_t len, int flag)
374{
375        char *extra = NULL;
376        int rv;
377
378        MAKEEXTRALIST(flag, extra, "");
379        if (!extra) {
380                *dst = '\0';            /* can't create extra, return "" */
381                return 0;
382        }
383        rv = strsvisx(dst, src, len, flag, extra);
384        free(extra);
385        return rv;
386}
387#endif
Note: See TracBrowser for help on using the repository browser.