kernel - Implement sbrk(), change low-address mmap hinting
[dragonfly.git] / contrib / libedit / src / vis.c
blobd67f55108baa8379481107815529d86367572ab6
1 /* $NetBSD: vis.c,v 1.66 2014/09/26 15:58:59 roy Exp $ */
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
32 /*-
33 * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
34 * All rights reserved.
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.
45 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 * POSSIBILITY OF SUCH DAMAGE.
58 #include "config.h"
60 #if defined(LIBC_SCCS) && !defined(lint)
61 __RCSID("$NetBSD: vis.c,v 1.66 2014/09/26 15:58:59 roy Exp $");
62 #endif /* LIBC_SCCS and not lint */
63 #ifdef __FBSDID
64 __FBSDID("$FreeBSD$");
65 #endif
67 #include <sys/types.h>
68 #include <sys/param.h>
70 #include <stdint.h>
71 #include <assert.h>
72 #include <vis.h>
73 #include <errno.h>
74 #include <stdlib.h>
75 #include <wchar.h>
76 #include <wctype.h>
78 #ifdef __weak_alias
79 __weak_alias(strvisx,_strvisx)
80 #endif
82 #if !HAVE_VIS || !HAVE_SVIS
83 #include <ctype.h>
84 #include <limits.h>
85 #include <stdio.h>
86 #include <string.h>
89 * The reason for going through the trouble to deal with character encodings
90 * in vis(3), is that we use this to safe encode output of commands. This
91 * safe encoding varies depending on the character set. For example if we
92 * display ps output in French, we don't want to display French characters
93 * as M-foo.
96 static wchar_t *do_svis(wchar_t *, wint_t, int, wint_t, const wchar_t *);
98 #undef BELL
99 #define BELL L'\a'
101 #define iswoctal(c) (((u_char)(c)) >= L'0' && ((u_char)(c)) <= L'7')
102 #define iswwhite(c) (c == L' ' || c == L'\t' || c == L'\n')
103 #define iswsafe(c) (c == L'\b' || c == BELL || c == L'\r')
104 #define xtoa(c) L"0123456789abcdef"[c]
105 #define XTOA(c) L"0123456789ABCDEF"[c]
107 #define MAXEXTRAS 30
109 static const wchar_t char_shell[] = L"'`\";&<>()|{}]\\$!^~";
110 static const wchar_t char_glob[] = L"*?[#";
112 #if !HAVE_NBTOOL_CONFIG_H
113 #ifndef __NetBSD__
115 * On NetBSD MB_LEN_MAX is currently 32 which does not fit on any integer
116 * integral type and it is probably wrong, since currently the maximum
117 * number of bytes and character needs is 6. Until this is fixed, the
118 * loops below are using sizeof(uint64_t) - 1 instead of MB_LEN_MAX, and
119 * the assertion is commented out.
121 #ifdef __FreeBSD__
123 * On FreeBSD including <sys/systm.h> for CTASSERT only works in kernel
124 * mode.
126 #ifndef CTASSERT
127 #define CTASSERT(x) _CTASSERT(x, __LINE__)
128 #define _CTASSERT(x, y) __CTASSERT(x, y)
129 #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
130 #endif
131 #endif /* __FreeBSD__ */
133 CTASSERT(MB_LEN_MAX <= sizeof(uint64_t));
135 #endif /* !__NetBSD__ */
136 #endif
139 * This is do_hvis, for HTTP style (RFC 1808)
141 static wchar_t *
142 do_hvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
144 if (iswalnum(c)
145 /* safe */
146 || c == L'$' || c == L'-' || c == L'_' || c == L'.' || c == L'+'
147 /* extra */
148 || c == L'!' || c == L'*' || c == L'\'' || c == L'(' || c == L')'
149 || c == L',')
150 dst = do_svis(dst, c, flags, nextc, extra);
151 else {
152 *dst++ = L'%';
153 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf);
154 *dst++ = xtoa((unsigned int)c & 0xf);
157 return dst;
161 * This is do_mvis, for Quoted-Printable MIME (RFC 2045)
162 * NB: No handling of long lines or CRLF.
164 static wchar_t *
165 do_mvis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
167 if ((c != L'\n') &&
168 /* Space at the end of the line */
169 ((iswspace(c) && (nextc == L'\r' || nextc == L'\n')) ||
170 /* Out of range */
171 (!iswspace(c) && (c < 33 || (c > 60 && c < 62) || c > 126)) ||
172 /* Specific char to be escaped */
173 wcschr(L"#$@[\\]^`{|}~", c) != NULL)) {
174 *dst++ = L'=';
175 *dst++ = XTOA(((unsigned int)c >> 4) & 0xf);
176 *dst++ = XTOA((unsigned int)c & 0xf);
177 } else
178 dst = do_svis(dst, c, flags, nextc, extra);
179 return dst;
183 * Output single byte of multibyte character.
185 static wchar_t *
186 do_mbyte(wchar_t *dst, wint_t c, int flags, wint_t nextc, int iswextra)
188 if (flags & VIS_CSTYLE) {
189 switch (c) {
190 case L'\n':
191 *dst++ = L'\\'; *dst++ = L'n';
192 return dst;
193 case L'\r':
194 *dst++ = L'\\'; *dst++ = L'r';
195 return dst;
196 case L'\b':
197 *dst++ = L'\\'; *dst++ = L'b';
198 return dst;
199 case BELL:
200 *dst++ = L'\\'; *dst++ = L'a';
201 return dst;
202 case L'\v':
203 *dst++ = L'\\'; *dst++ = L'v';
204 return dst;
205 case L'\t':
206 *dst++ = L'\\'; *dst++ = L't';
207 return dst;
208 case L'\f':
209 *dst++ = L'\\'; *dst++ = L'f';
210 return dst;
211 case L' ':
212 *dst++ = L'\\'; *dst++ = L's';
213 return dst;
214 case L'\0':
215 *dst++ = L'\\'; *dst++ = L'0';
216 if (iswoctal(nextc)) {
217 *dst++ = L'0';
218 *dst++ = L'0';
220 return dst;
221 /* We cannot encode these characters in VIS_CSTYLE
222 * because they special meaning */
223 case L'n':
224 case L'r':
225 case L'b':
226 case L'a':
227 case L'v':
228 case L't':
229 case L'f':
230 case L's':
231 case L'0':
232 case L'M':
233 case L'^':
234 case L'$': /* vis(1) -l */
235 break;
236 default:
237 if (iswgraph(c) && !iswoctal(c)) {
238 *dst++ = L'\\';
239 *dst++ = c;
240 return dst;
244 if (iswextra || ((c & 0177) == L' ') || (flags & VIS_OCTAL)) {
245 *dst++ = L'\\';
246 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + L'0';
247 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + L'0';
248 *dst++ = (c & 07) + L'0';
249 } else {
250 if ((flags & VIS_NOSLASH) == 0)
251 *dst++ = L'\\';
253 if (c & 0200) {
254 c &= 0177;
255 *dst++ = L'M';
258 if (iswcntrl(c)) {
259 *dst++ = L'^';
260 if (c == 0177)
261 *dst++ = L'?';
262 else
263 *dst++ = c + L'@';
264 } else {
265 *dst++ = L'-';
266 *dst++ = c;
270 return dst;
274 * This is do_vis, the central code of vis.
275 * dst: Pointer to the destination buffer
276 * c: Character to encode
277 * flags: Flags word
278 * nextc: The character following 'c'
279 * extra: Pointer to the list of extra characters to be
280 * backslash-protected.
282 static wchar_t *
283 do_svis(wchar_t *dst, wint_t c, int flags, wint_t nextc, const wchar_t *extra)
285 int iswextra, i, shft;
286 uint64_t bmsk, wmsk;
288 iswextra = wcschr(extra, c) != NULL;
289 if (!iswextra && (iswgraph(c) || iswwhite(c) ||
290 ((flags & VIS_SAFE) && iswsafe(c)))) {
291 *dst++ = c;
292 return dst;
295 /* See comment in istrsenvisx() output loop, below. */
296 wmsk = 0;
297 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
298 shft = i * NBBY;
299 bmsk = (uint64_t)0xffLL << shft;
300 wmsk |= bmsk;
301 if ((c & wmsk) || i == 0)
302 dst = do_mbyte(dst, (wint_t)(
303 (uint64_t)(c & bmsk) >> shft),
304 flags, nextc, iswextra);
307 return dst;
310 typedef wchar_t *(*visfun_t)(wchar_t *, wint_t, int, wint_t, const wchar_t *);
313 * Return the appropriate encoding function depending on the flags given.
315 static visfun_t
316 getvisfun(int flags)
318 if (flags & VIS_HTTPSTYLE)
319 return do_hvis;
320 if (flags & VIS_MIMESTYLE)
321 return do_mvis;
322 return do_svis;
326 * Expand list of extra characters to not visually encode.
328 static wchar_t *
329 makeextralist(int flags, const char *src)
331 wchar_t *dst, *d;
332 size_t len;
333 const wchar_t *s;
335 len = strlen(src);
336 if ((dst = calloc(len + MAXEXTRAS, sizeof(*dst))) == NULL)
337 return NULL;
339 if (mbstowcs(dst, src, len) == (size_t)-1) {
340 size_t i;
341 for (i = 0; i < len; i++)
342 dst[i] = (wchar_t)(u_char)src[i];
343 d = dst + len;
344 } else
345 d = dst + wcslen(dst);
347 if (flags & VIS_GLOB)
348 for (s = char_glob; *s; *d++ = *s++)
349 continue;
351 if (flags & VIS_SHELL)
352 for (s = char_shell; *s; *d++ = *s++)
353 continue;
355 if (flags & VIS_SP) *d++ = L' ';
356 if (flags & VIS_TAB) *d++ = L'\t';
357 if (flags & VIS_NL) *d++ = L'\n';
358 if ((flags & VIS_NOSLASH) == 0) *d++ = L'\\';
359 *d = L'\0';
361 return dst;
365 * istrsenvisx()
366 * The main internal function.
367 * All user-visible functions call this one.
369 static int
370 istrsenvisx(char *mbdst, size_t *dlen, const char *mbsrc, size_t mblength,
371 int flags, const char *mbextra, int *cerr_ptr)
373 wchar_t *dst, *src, *pdst, *psrc, *start, *extra;
374 size_t len, olen;
375 uint64_t bmsk, wmsk;
376 wint_t c;
377 visfun_t f;
378 int clen = 0, cerr = 0, error = -1, i, shft;
379 ssize_t mbslength, maxolen;
381 _DIAGASSERT(mbdst != NULL);
382 _DIAGASSERT(mbsrc != NULL || mblength == 0);
383 _DIAGASSERT(mbextra != NULL);
386 * Input (mbsrc) is a char string considered to be multibyte
387 * characters. The input loop will read this string pulling
388 * one character, possibly multiple bytes, from mbsrc and
389 * converting each to wchar_t in src.
391 * The vis conversion will be done using the wide char
392 * wchar_t string.
394 * This will then be converted back to a multibyte string to
395 * return to the caller.
398 /* Allocate space for the wide char strings */
399 psrc = pdst = extra = NULL;
400 if ((psrc = calloc(mblength + 1, sizeof(*psrc))) == NULL)
401 return -1;
402 if ((pdst = calloc((4 * mblength) + 1, sizeof(*pdst))) == NULL)
403 goto out;
404 dst = pdst;
405 src = psrc;
407 /* Use caller's multibyte conversion error flag. */
408 if (cerr_ptr)
409 cerr = *cerr_ptr;
412 * Input loop.
413 * Handle up to mblength characters (not bytes). We do not
414 * stop at NULs because we may be processing a block of data
415 * that includes NULs.
417 mbslength = (ssize_t)mblength;
419 * When inputing a single character, must also read in the
420 * next character for nextc, the look-ahead character.
422 if (mbslength == 1)
423 mbslength++;
424 while (mbslength > 0) {
425 /* Convert one multibyte character to wchar_t. */
426 if (!cerr)
427 clen = mbtowc(src, mbsrc, MB_LEN_MAX);
428 if (cerr || clen < 0) {
429 /* Conversion error, process as a byte instead. */
430 *src = (wint_t)(u_char)*mbsrc;
431 clen = 1;
432 cerr = 1;
434 if (clen == 0)
436 * NUL in input gives 0 return value. process
437 * as single NUL byte and keep going.
439 clen = 1;
440 /* Advance buffer character pointer. */
441 src++;
442 /* Advance input pointer by number of bytes read. */
443 mbsrc += clen;
444 /* Decrement input byte count. */
445 mbslength -= clen;
447 len = src - psrc;
448 src = psrc;
450 * In the single character input case, we will have actually
451 * processed two characters, c and nextc. Reset len back to
452 * just a single character.
454 if (mblength < len)
455 len = mblength;
457 /* Convert extra argument to list of characters for this mode. */
458 extra = makeextralist(flags, mbextra);
459 if (!extra) {
460 if (dlen && *dlen == 0) {
461 errno = ENOSPC;
462 goto out;
464 *mbdst = '\0'; /* can't create extra, return "" */
465 error = 0;
466 goto out;
469 /* Look up which processing function to call. */
470 f = getvisfun(flags);
473 * Main processing loop.
474 * Call do_Xvis processing function one character at a time
475 * with next character available for look-ahead.
477 for (start = dst; len > 0; len--) {
478 c = *src++;
479 dst = (*f)(dst, c, flags, len >= 1 ? *src : L'\0', extra);
480 if (dst == NULL) {
481 errno = ENOSPC;
482 goto out;
486 /* Terminate the string in the buffer. */
487 *dst = L'\0';
490 * Output loop.
491 * Convert wchar_t string back to multibyte output string.
492 * If we have hit a multi-byte conversion error on input,
493 * output byte-by-byte here. Else use wctomb().
495 len = wcslen(start);
496 maxolen = dlen ? *dlen : (wcslen(start) * MB_LEN_MAX + 1);
497 olen = 0;
498 for (dst = start; len > 0; len--) {
499 if (!cerr)
500 clen = wctomb(mbdst, *dst);
501 if (cerr || clen < 0) {
503 * Conversion error, process as a byte(s) instead.
504 * Examine each byte and higher-order bytes for
505 * data. E.g.,
506 * 0x000000000000a264 -> a2 64
507 * 0x000000001f00a264 -> 1f 00 a2 64
509 clen = 0;
510 wmsk = 0;
511 for (i = sizeof(wmsk) - 1; i >= 0; i--) {
512 shft = i * NBBY;
513 bmsk = (uint64_t)0xffLL << shft;
514 wmsk |= bmsk;
515 if ((*dst & wmsk) || i == 0)
516 mbdst[clen++] = (char)(
517 (uint64_t)(*dst & bmsk) >>
518 shft);
520 cerr = 1;
522 /* If this character would exceed our output limit, stop. */
523 if (olen + clen > (size_t)maxolen)
524 break;
525 /* Advance output pointer by number of bytes written. */
526 mbdst += clen;
527 /* Advance buffer character pointer. */
528 dst++;
529 /* Incrment output character count. */
530 olen += clen;
533 /* Terminate the output string. */
534 *mbdst = '\0';
536 /* Pass conversion error flag out. */
537 if (cerr_ptr)
538 *cerr_ptr = cerr;
540 free(extra);
541 free(pdst);
542 free(psrc);
544 return (int)olen;
545 out:
546 free(extra);
547 free(pdst);
548 free(psrc);
549 return error;
552 static int
553 istrsenvisxl(char *mbdst, size_t *dlen, const char *mbsrc,
554 int flags, const char *mbextra, int *cerr_ptr)
556 return istrsenvisx(mbdst, dlen, mbsrc,
557 mbsrc != NULL ? strlen(mbsrc) : 0, flags, mbextra, cerr_ptr);
560 #endif
562 #if !HAVE_SVIS
564 * The "svis" variants all take an "extra" arg that is a pointer
565 * to a NUL-terminated list of characters to be encoded, too.
566 * These functions are useful e. g. to encode strings in such a
567 * way so that they are not interpreted by a shell.
570 char *
571 svis(char *mbdst, int c, int flags, int nextc, const char *mbextra)
573 char cc[2];
574 int ret;
576 cc[0] = c;
577 cc[1] = nextc;
579 ret = istrsenvisx(mbdst, NULL, cc, 1, flags, mbextra, NULL);
580 if (ret < 0)
581 return NULL;
582 return mbdst + ret;
585 char *
586 snvis(char *mbdst, size_t dlen, int c, int flags, int nextc, const char *mbextra)
588 char cc[2];
589 int ret;
591 cc[0] = c;
592 cc[1] = nextc;
594 ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, mbextra, NULL);
595 if (ret < 0)
596 return NULL;
597 return mbdst + ret;
601 strsvis(char *mbdst, const char *mbsrc, int flags, const char *mbextra)
603 return istrsenvisxl(mbdst, NULL, mbsrc, flags, mbextra, NULL);
607 strsnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags, const char *mbextra)
609 return istrsenvisxl(mbdst, &dlen, mbsrc, flags, mbextra, NULL);
613 strsvisx(char *mbdst, const char *mbsrc, size_t len, int flags, const char *mbextra)
615 return istrsenvisx(mbdst, NULL, mbsrc, len, flags, mbextra, NULL);
619 strsnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
620 const char *mbextra)
622 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, NULL);
626 strsenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
627 const char *mbextra, int *cerr_ptr)
629 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, mbextra, cerr_ptr);
631 #endif
633 #if !HAVE_VIS
635 * vis - visually encode characters
637 char *
638 vis(char *mbdst, int c, int flags, int nextc)
640 char cc[2];
641 int ret;
643 cc[0] = c;
644 cc[1] = nextc;
646 ret = istrsenvisx(mbdst, NULL, cc, 1, flags, "", NULL);
647 if (ret < 0)
648 return NULL;
649 return mbdst + ret;
652 char *
653 nvis(char *mbdst, size_t dlen, int c, int flags, int nextc)
655 char cc[2];
656 int ret;
658 cc[0] = c;
659 cc[1] = nextc;
661 ret = istrsenvisx(mbdst, &dlen, cc, 1, flags, "", NULL);
662 if (ret < 0)
663 return NULL;
664 return mbdst + ret;
668 * strvis - visually encode characters from src into dst
670 * Dst must be 4 times the size of src to account for possible
671 * expansion. The length of dst, not including the trailing NULL,
672 * is returned.
676 strvis(char *mbdst, const char *mbsrc, int flags)
678 return istrsenvisxl(mbdst, NULL, mbsrc, flags, "", NULL);
682 strnvis(char *mbdst, size_t dlen, const char *mbsrc, int flags)
684 return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
688 * strvisx - visually encode characters from src into dst
690 * Dst must be 4 times the size of src to account for possible
691 * expansion. The length of dst, not including the trailing NULL,
692 * is returned.
694 * Strvisx encodes exactly len characters from src into dst.
695 * This is useful for encoding a block of data.
699 strvisx(char *mbdst, const char *mbsrc, size_t len, int flags)
701 return istrsenvisx(mbdst, NULL, mbsrc, len, flags, "", NULL);
705 strnvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags)
707 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", NULL);
711 strenvisx(char *mbdst, size_t dlen, const char *mbsrc, size_t len, int flags,
712 int *cerr_ptr)
714 return istrsenvisx(mbdst, &dlen, mbsrc, len, flags, "", cerr_ptr);
716 #endif