Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* handy.h |
a687059c | 2 | * |
9607fc9c | 3 | * Copyright (c) 1991-1997, Larry Wall |
a687059c | 4 | * |
6e21c824 LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
8d063cd8 | 7 | * |
8d063cd8 LW |
8 | */ |
9 | ||
85e6fe83 | 10 | #if !defined(__STDC__) |
378cc40b LW |
11 | #ifdef NULL |
12 | #undef NULL | |
13 | #endif | |
a687059c LW |
14 | #ifndef I286 |
15 | # define NULL 0 | |
16 | #else | |
17 | # define NULL 0L | |
18 | #endif | |
85e6fe83 LW |
19 | #endif |
20 | ||
378cc40b | 21 | #define Null(type) ((type)NULL) |
8d063cd8 | 22 | #define Nullch Null(char*) |
760ac839 | 23 | #define Nullfp Null(PerlIO*) |
79072805 | 24 | #define Nullsv Null(SV*) |
8d063cd8 | 25 | |
641d3f0b | 26 | #ifdef TRUE |
27 | #undef TRUE | |
28 | #endif | |
29 | #ifdef FALSE | |
30 | #undef FALSE | |
31 | #endif | |
32 | #define TRUE (1) | |
33 | #define FALSE (0) | |
34 | ||
27d4fb96 | 35 | |
36 | /* XXX Configure ought to have a test for a boolean type, if I can | |
37 | just figure out all the headers such a test needs. | |
38 | Andy Dougherty August 1996 | |
39 | */ | |
232e078e | 40 | /* bool is built-in for g++-2.6.3, which might be used for an extension. |
5d94fbed AD |
41 | If the extension includes <_G_config.h> before this file then |
42 | _G_HAVE_BOOL will be properly set. If, however, the extension includes | |
43 | this file first, then you will have to manually set -DHAS_BOOL in | |
44 | your command line to avoid a conflict. | |
45 | */ | |
46 | #ifdef _G_HAVE_BOOL | |
47 | # if _G_HAVE_BOOL | |
48 | # ifndef HAS_BOOL | |
49 | # define HAS_BOOL 1 | |
50 | # endif | |
51 | # endif | |
52 | #endif | |
53 | ||
641d3f0b | 54 | /* The NeXT dynamic loader headers will not build with the bool macro |
55 | So declare them now to clear confusion. | |
56 | */ | |
57 | #ifdef NeXT | |
58 | # undef FALSE | |
59 | # undef TRUE | |
60 | typedef enum bool { FALSE = 0, TRUE = 1 } bool; | |
61 | # define ENUM_BOOL 1 | |
62 | # ifndef HAS_BOOL | |
63 | # define HAS_BOOL 1 | |
64 | # endif /* !HAS_BOOL */ | |
65 | #endif /* NeXT */ | |
66 | ||
5d94fbed | 67 | #ifndef HAS_BOOL |
61bb5906 | 68 | # if defined(UTS) || defined(VMS) |
5d94fbed AD |
69 | # define bool int |
70 | # else | |
71 | # define bool char | |
72 | # endif | |
a687059c | 73 | #endif |
0d3e774c | 74 | |
27d4fb96 | 75 | /* XXX A note on the perl source internal type system. The |
76 | original intent was that I32 be *exactly* 32 bits. | |
77 | ||
78 | Currently, we only guarantee that I32 is *at least* 32 bits. | |
79 | Specifically, if int is 64 bits, then so is I32. (This is the case | |
80 | for the Cray.) This has the advantage of meshing nicely with | |
81 | standard library calls (where we pass an I32 and the library is | |
82 | expecting an int), but the disadvantage that an I32 is not 32 bits. | |
83 | Andy Dougherty August 1996 | |
24fef2a7 AD |
84 | |
85 | In the future, we may perhaps want to think about something like | |
86 | #if INTSIZE == 4 | |
87 | typedef I32 int; | |
88 | #else | |
89 | # if LONGSIZE == 4 | |
90 | typedef I32 long; | |
91 | # else | |
92 | # if SHORTSIZE == 4 | |
93 | typedef I32 short; | |
94 | # else | |
95 | typedef I32 int; | |
96 | # endif | |
97 | # endif | |
98 | #endif | |
693762b4 AD |
99 | |
100 | except that still won't work if a system has no integral type | |
101 | with a size that is 32 bytes. | |
102 | ||
103 | Further, we need to know about PTRSIZE == sizeof(void *) and | |
104 | DOUBLESIZE == sizeof(double) if we really want to try to handle | |
e5c9fcd0 AD |
105 | 32/64-bit combinations. Finally, we might also need to know |
106 | HAS_LONG_DOUBLE and LONG_DOUBLESIZE. | |
693762b4 AD |
107 | |
108 | For the moment, these are only mentioned here so metaconfig will | |
24fef2a7 | 109 | construct Configure to figure out the various sizes. |
27d4fb96 | 110 | */ |
111 | ||
79072805 LW |
112 | typedef char I8; |
113 | typedef unsigned char U8; | |
5c9fa16e KA |
114 | /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type. |
115 | Please search CHAR_MAX in perl.h for further details. */ | |
27d4fb96 | 116 | #define U8_MAX PERL_UCHAR_MAX |
117 | #define U8_MIN PERL_UCHAR_MIN | |
79072805 LW |
118 | |
119 | typedef short I16; | |
120 | typedef unsigned short U16; | |
27d4fb96 | 121 | #define I16_MAX PERL_SHORT_MAX |
122 | #define I16_MIN PERL_SHORT_MIN | |
123 | #define U16_MAX PERL_USHORT_MAX | |
124 | #define U16_MIN PERL_USHORT_MIN | |
79072805 | 125 | |
a0d0e21e | 126 | #if BYTEORDER > 0x4321 |
85e6fe83 LW |
127 | typedef int I32; |
128 | typedef unsigned int U32; | |
27d4fb96 | 129 | # define I32_MAX PERL_INT_MAX |
130 | # define I32_MIN PERL_INT_MIN | |
131 | # define U32_MAX PERL_UINT_MAX | |
132 | # define U32_MIN PERL_UINT_MIN | |
79072805 | 133 | #else |
85e6fe83 LW |
134 | typedef long I32; |
135 | typedef unsigned long U32; | |
27d4fb96 | 136 | # define I32_MAX PERL_LONG_MAX |
137 | # define I32_MIN PERL_LONG_MIN | |
138 | # define U32_MAX PERL_ULONG_MAX | |
139 | # define U32_MIN PERL_ULONG_MIN | |
79072805 LW |
140 | #endif |
141 | ||
fc36a67e | 142 | #define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */ |
143 | #define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8) | |
144 | #define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */ | |
145 | ||
ff68c719 | 146 | #define Ctl(ch) ((ch) & 037) |
8d063cd8 LW |
147 | |
148 | #define strNE(s1,s2) (strcmp(s1,s2)) | |
149 | #define strEQ(s1,s2) (!strcmp(s1,s2)) | |
150 | #define strLT(s1,s2) (strcmp(s1,s2) < 0) | |
151 | #define strLE(s1,s2) (strcmp(s1,s2) <= 0) | |
152 | #define strGT(s1,s2) (strcmp(s1,s2) > 0) | |
153 | #define strGE(s1,s2) (strcmp(s1,s2) >= 0) | |
154 | #define strnNE(s1,s2,l) (strncmp(s1,s2,l)) | |
155 | #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l)) | |
378cc40b | 156 | |
36477c24 | 157 | #ifdef HAS_MEMCMP |
158 | # define memNE(s1,s2,l) (memcmp(s1,s2,l)) | |
159 | # define memEQ(s1,s2,l) (!memcmp(s1,s2,l)) | |
160 | #else | |
161 | # define memNE(s1,s2,l) (bcmp(s1,s2,l)) | |
162 | # define memEQ(s1,s2,l) (!bcmp(s1,s2,l)) | |
163 | #endif | |
164 | ||
bbce6d69 | 165 | /* |
166 | * Character classes. | |
167 | * | |
168 | * Unfortunately, the introduction of locales means that we | |
169 | * can't trust isupper(), etc. to tell the truth. And when | |
170 | * it comes to /\w+/ with tainting enabled, we *must* be able | |
171 | * to trust our character classes. | |
172 | * | |
173 | * Therefore, the default tests in the text of Perl will be | |
174 | * independent of locale. Any code that wants to depend on | |
175 | * the current locale will use the tests that begin with "lc". | |
176 | */ | |
177 | ||
2304df62 AD |
178 | #ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */ |
179 | # ifndef CTYPE256 | |
180 | # define CTYPE256 | |
181 | # endif | |
182 | #endif | |
183 | ||
bbce6d69 | 184 | #define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_') |
185 | #define isIDFIRST(c) (isALPHA(c) || (c) == '_') | |
186 | #define isALPHA(c) (isUPPER(c) || isLOWER(c)) | |
187 | #define isSPACE(c) \ | |
188 | ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f') | |
189 | #define isDIGIT(c) ((c) >= '0' && (c) <= '9') | |
190 | #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z') | |
191 | #define isLOWER(c) ((c) >= 'a' && (c) <= 'z') | |
192 | #define isPRINT(c) (((c) > 32 && (c) < 127) || isSPACE(c)) | |
193 | #define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c)) | |
194 | #define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c)) | |
195 | ||
196 | #ifdef USE_NEXT_CTYPE | |
197 | ||
198 | # define isALNUM_LC(c) \ | |
ff68c719 | 199 | (NXIsAlpha((unsigned int)(c)) || NXIsDigit((unsigned int)(c)) || \ |
200 | (char)(c) == '_') | |
201 | # define isIDFIRST_LC(c) \ | |
202 | (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_') | |
203 | # define isALPHA_LC(c) NXIsAlpha((unsigned int)(c)) | |
204 | # define isSPACE_LC(c) NXIsSpace((unsigned int)(c)) | |
205 | # define isDIGIT_LC(c) NXIsDigit((unsigned int)(c)) | |
206 | # define isUPPER_LC(c) NXIsUpper((unsigned int)(c)) | |
207 | # define isLOWER_LC(c) NXIsLower((unsigned int)(c)) | |
208 | # define isPRINT_LC(c) NXIsPrint((unsigned int)(c)) | |
209 | # define toUPPER_LC(c) NXToUpper((unsigned int)(c)) | |
210 | # define toLOWER_LC(c) NXToLower((unsigned int)(c)) | |
bbce6d69 | 211 | |
212 | #else /* !USE_NEXT_CTYPE */ | |
213 | # if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII)) | |
214 | ||
215 | # define isALNUM_LC(c) \ | |
216 | (isalpha((unsigned char)(c)) || \ | |
ff68c719 | 217 | isdigit((unsigned char)(c)) || (char)(c) == '_') |
218 | # define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_') | |
bbce6d69 | 219 | # define isALPHA_LC(c) isalpha((unsigned char)(c)) |
220 | # define isSPACE_LC(c) isspace((unsigned char)(c)) | |
221 | # define isDIGIT_LC(c) isdigit((unsigned char)(c)) | |
222 | # define isUPPER_LC(c) isupper((unsigned char)(c)) | |
223 | # define isLOWER_LC(c) islower((unsigned char)(c)) | |
224 | # define isPRINT_LC(c) isprint((unsigned char)(c)) | |
225 | # define toUPPER_LC(c) toupper((unsigned char)(c)) | |
226 | # define toLOWER_LC(c) tolower((unsigned char)(c)) | |
227 | ||
228 | # else | |
229 | ||
230 | # define isALNUM_LC(c) \ | |
ff68c719 | 231 | (isascii(c) && (isalpha(c) || isdigit(c) || (c) == '_')) |
bbce6d69 | 232 | # define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_')) |
233 | # define isALPHA_LC(c) (isascii(c) && isalpha(c)) | |
234 | # define isSPACE_LC(c) (isascii(c) && isspace(c)) | |
235 | # define isDIGIT_LC(c) (isascii(c) && isdigit(c)) | |
236 | # define isUPPER_LC(c) (isascii(c) && isupper(c)) | |
237 | # define isLOWER_LC(c) (isascii(c) && islower(c)) | |
238 | # define isPRINT_LC(c) (isascii(c) && isprint(c)) | |
239 | # define toUPPER_LC(c) toupper(c) | |
240 | # define toLOWER_LC(c) tolower(c) | |
241 | ||
242 | # endif | |
a0d0e21e | 243 | #endif /* USE_NEXT_CTYPE */ |
55204971 | 244 | |
bbce6d69 | 245 | /* This conversion works both ways, strangely enough. */ |
246 | #define toCTRL(c) (toUPPER(c) ^ 64) | |
247 | ||
378cc40b | 248 | /* Line numbers are unsigned, 16 bits. */ |
79072805 | 249 | typedef U16 line_t; |
378cc40b LW |
250 | #ifdef lint |
251 | #define NOLINE ((line_t)0) | |
252 | #else | |
253 | #define NOLINE ((line_t) 65535) | |
254 | #endif | |
255 | ||
8c52afec IZ |
256 | |
257 | /* This looks obsolete (IZ): | |
258 | ||
259 | XXX LEAKTEST doesn't really work in perl5. There are direct calls to | |
27d4fb96 | 260 | safemalloc() in the source, so LEAKTEST won't pick them up. |
261 | Further, if you try LEAKTEST, you'll also end up calling | |
262 | Safefree, which might call safexfree() on some things that weren't | |
263 | malloced with safexmalloc. The correct "fix" to this, if anyone | |
264 | is interested, is to ensure that all calls go through the New and | |
265 | Renew macros. | |
266 | --Andy Dougherty August 1996 | |
267 | */ | |
55497cff | 268 | |
a687059c LW |
269 | #ifndef lint |
270 | #ifndef LEAKTEST | |
598a3d64 | 271 | |
ff68c719 | 272 | #define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))) |
273 | #define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t)))) | |
274 | #define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \ | |
275 | memzero((char*)(v), (n)*sizeof(t)) | |
276 | #define Renew(v,n,t) \ | |
277 | (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) | |
278 | #define Renewc(v,n,t,c) \ | |
279 | (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) | |
280 | #define Safefree(d) safefree((Malloc_t)(d)) | |
281 | #define NEWSV(x,len) newSV(len) | |
55497cff | 282 | |
a687059c | 283 | #else /* LEAKTEST */ |
55497cff | 284 | |
ff68c719 | 285 | #define New(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))) |
286 | #define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))) | |
287 | #define Newz(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \ | |
288 | memzero((char*)(v), (n)*sizeof(t)) | |
289 | #define Renew(v,n,t) \ | |
290 | (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) | |
291 | #define Renewc(v,n,t,c) \ | |
292 | (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) | |
8c52afec | 293 | #define Safefree(d) safexfree((Malloc_t)(d)) |
ff68c719 | 294 | #define NEWSV(x,len) newSV(x,len) |
295 | ||
fc36a67e | 296 | #define MAXXCOUNT 1400 |
8c52afec IZ |
297 | #define MAXY_SIZE 80 |
298 | #define MAXYCOUNT 16 /* (MAXY_SIZE/4 + 1) */ | |
299 | extern long xcount[MAXXCOUNT]; | |
300 | extern long lastxcount[MAXXCOUNT]; | |
301 | extern long xycount[MAXXCOUNT][MAXYCOUNT]; | |
302 | extern long lastxycount[MAXXCOUNT][MAXYCOUNT]; | |
55497cff | 303 | |
a687059c | 304 | #endif /* LEAKTEST */ |
55497cff | 305 | |
ff68c719 | 306 | #define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t)) |
307 | #define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t)) | |
308 | #define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t)) | |
55497cff | 309 | |
a687059c | 310 | #else /* lint */ |
55497cff | 311 | |
ff68c719 | 312 | #define New(x,v,n,s) (v = Null(s *)) |
313 | #define Newc(x,v,n,s,c) (v = Null(s *)) | |
314 | #define Newz(x,v,n,s) (v = Null(s *)) | |
315 | #define Renew(v,n,s) (v = Null(s *)) | |
bee1dbe2 | 316 | #define Move(s,d,n,t) |
a687059c LW |
317 | #define Copy(s,d,n,t) |
318 | #define Zero(d,n,t) | |
ff68c719 | 319 | #define Safefree(d) (d) = (d) |
55497cff | 320 | |
a687059c | 321 | #endif /* lint */ |
bee1dbe2 | 322 | |
2304df62 | 323 | #ifdef USE_STRUCT_COPY |
ff68c719 | 324 | #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s))) |
bee1dbe2 LW |
325 | #else |
326 | #define StructCopy(s,d,t) Copy(s,d,1,t) | |
327 | #endif |