1 /* pred.c -- execute the expression tree.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 2000,
3 2003, 2004 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 #if HAVE_STRING_H || STDC_HEADERS
36 /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth(). */
41 #include "printquoted.h"
45 This comment, IN_CTYPE_DOMAIN and ISPRINT were borrowed from
46 coreutils at Sun Jun 5 21:17:40 2005 UTC.
50 "... Some ctype macros are valid only for character codes that
51 isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
52 using /bin/cc or gcc but without giving an ansi option). So, all
53 ctype uses should be through macros like ISPRINT... If
54 STDC_HEADERS is defined, then autoconf has verified that the ctype
55 macros don't need to be guarded with references to isascii. ...
56 Defining isascii to 1 should let any compiler worth its salt
57 eliminate the && through constant folding."
61 "... Furthermore, isupper(c) etc. have an undefined result if c is
62 outside the range -1 <= c <= 255. One is tempted to write isupper(c)
63 with c being of type `char', but this is wrong if c is an 8-bit
64 character >= 128 which gets sign-extended to a negative value.
65 The macro ISUPPER protects against this as well." */
70 /* ISPRINT is defined in <sys/euc.h> on at least Solaris2.6 systems. */
72 #define ISPRINT(c) (IN_CTYPE_DOMAIN (c) && isprint (c))
74 #if STDC_HEADERS || (!defined (isascii) && !HAVE_ISASCII)
75 # define IN_CTYPE_DOMAIN(c) 1
77 # define IN_CTYPE_DOMAIN(c) isascii(c)
84 /* Convert a possibly-signed character to an unsigned character. This is
85 * a bit safer than casting to unsigned char, since it catches some type
86 * errors that the cast doesn't.
88 * This code taken from coreutils' system.h header at
89 * Sun Jun 5 21:05:21 2005 UTC.
91 static inline unsigned char to_uchar (char ch
)
99 unibyte_qmark_chars(char *buf
, size_t len
)
102 char const *plimit
= buf
+ len
;
106 if (! ISPRINT (to_uchar (*p
)))
116 multibyte_qmark_chars(char *buf
, size_t len
)
120 return unibyte_qmark_chars(buf
, len
);
125 char const *plimit
= buf
+ len
;
131 case ' ': case '!': case '"': case '#': case '%':
132 case '&': case '\'': case '(': case ')': case '*':
133 case '+': case ',': case '-': case '.': case '/':
134 case '0': case '1': case '2': case '3': case '4':
135 case '5': case '6': case '7': case '8': case '9':
136 case ':': case ';': case '<': case '=': case '>':
138 case 'A': case 'B': case 'C': case 'D': case 'E':
139 case 'F': case 'G': case 'H': case 'I': case 'J':
140 case 'K': case 'L': case 'M': case 'N': case 'O':
141 case 'P': case 'Q': case 'R': case 'S': case 'T':
142 case 'U': case 'V': case 'W': case 'X': case 'Y':
144 case '[': case '\\': case ']': case '^': case '_':
145 case 'a': case 'b': case 'c': case 'd': case 'e':
146 case 'f': case 'g': case 'h': case 'i': case 'j':
147 case 'k': case 'l': case 'm': case 'n': case 'o':
148 case 'p': case 'q': case 'r': case 's': case 't':
149 case 'u': case 'v': case 'w': case 'x': case 'y':
150 case 'z': case '{': case '|': case '}': case '~':
151 /* These characters are printable ASCII characters. */
155 /* If we have a multibyte sequence, copy it until we
156 reach its end, replacing each non-printable multibyte
157 character with a single question mark. */
160 memset (&mbstate
, 0, sizeof mbstate
);
167 bytes
= mbrtowc (&wc
, p
, plimit
- p
, &mbstate
);
169 if (bytes
== (size_t) -1)
171 /* An invalid multibyte sequence was
172 encountered. Skip one input byte, and
173 put a question mark. */
179 if (bytes
== (size_t) -2)
181 /* An incomplete multibyte character
182 at the end. Replace it entirely with
190 /* A null wide character was encountered. */
196 /* A printable multibyte character.
198 for (; bytes
> 0; --bytes
)
203 /* An unprintable multibyte character.
204 Replace it entirely with a question
210 while (! mbsinit (&mbstate
));
215 /* The buffer may have shrunk. */
223 /* Scan BUF, replacing any dangerous-looking characters with question
224 * marks. This code is taken from the ls.c file in coreutils as at
225 * Sun Jun 5 20:51:54 2005 UTC.
227 * This function may shrink the buffer. Either way, the new length
231 qmark_chars(char *buf
, size_t len
)
234 return multibyte_qmark_chars(buf
, len
);
236 return unibyte_qmark_chars(buf
, len
);