Commit | Line | Data |
a0d0e21e |
1 | /* handy.h |
a687059c |
2 | * |
a0d0e21e |
3 | * Copyright (c) 1991-1994, Larry Wall |
a687059c |
4 | * |
6e21c824 |
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 |
8 | */ |
9 | |
85e6fe83 |
10 | #if !defined(__STDC__) |
378cc40b |
11 | #ifdef NULL |
12 | #undef NULL |
13 | #endif |
a687059c |
14 | #ifndef I286 |
15 | # define NULL 0 |
16 | #else |
17 | # define NULL 0L |
18 | #endif |
85e6fe83 |
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 |
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 |
68 | # ifdef UTS |
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 |
84 | */ |
85 | |
79072805 |
86 | typedef char I8; |
87 | typedef unsigned char U8; |
5c9fa16e |
88 | /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type. |
89 | Please search CHAR_MAX in perl.h for further details. */ |
27d4fb96 |
90 | #define U8_MAX PERL_UCHAR_MAX |
91 | #define U8_MIN PERL_UCHAR_MIN |
79072805 |
92 | |
93 | typedef short I16; |
94 | typedef unsigned short U16; |
27d4fb96 |
95 | #define I16_MAX PERL_SHORT_MAX |
96 | #define I16_MIN PERL_SHORT_MIN |
97 | #define U16_MAX PERL_USHORT_MAX |
98 | #define U16_MIN PERL_USHORT_MIN |
79072805 |
99 | |
a0d0e21e |
100 | #if BYTEORDER > 0x4321 |
85e6fe83 |
101 | typedef int I32; |
102 | typedef unsigned int U32; |
27d4fb96 |
103 | # define I32_MAX PERL_INT_MAX |
104 | # define I32_MIN PERL_INT_MIN |
105 | # define U32_MAX PERL_UINT_MAX |
106 | # define U32_MIN PERL_UINT_MIN |
79072805 |
107 | #else |
85e6fe83 |
108 | typedef long I32; |
109 | typedef unsigned long U32; |
27d4fb96 |
110 | # define I32_MAX PERL_LONG_MAX |
111 | # define I32_MIN PERL_LONG_MIN |
112 | # define U32_MAX PERL_ULONG_MAX |
113 | # define U32_MIN PERL_ULONG_MIN |
79072805 |
114 | #endif |
115 | |
8d063cd8 |
116 | #define Ctl(ch) (ch & 037) |
117 | |
118 | #define strNE(s1,s2) (strcmp(s1,s2)) |
119 | #define strEQ(s1,s2) (!strcmp(s1,s2)) |
120 | #define strLT(s1,s2) (strcmp(s1,s2) < 0) |
121 | #define strLE(s1,s2) (strcmp(s1,s2) <= 0) |
122 | #define strGT(s1,s2) (strcmp(s1,s2) > 0) |
123 | #define strGE(s1,s2) (strcmp(s1,s2) >= 0) |
124 | #define strnNE(s1,s2,l) (strncmp(s1,s2,l)) |
125 | #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l)) |
378cc40b |
126 | |
2304df62 |
127 | #ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */ |
128 | # ifndef CTYPE256 |
129 | # define CTYPE256 |
130 | # endif |
131 | #endif |
132 | |
a0d0e21e |
133 | #ifdef USE_NEXT_CTYPE |
134 | #define isALNUM(c) (NXIsAlpha((unsigned int)c) || NXIsDigit((unsigned int)c) || c == '_') |
135 | #define isIDFIRST(c) (NXIsAlpha((unsigned int)c) || c == '_') |
136 | #define isALPHA(c) NXIsAlpha((unsigned int)c) |
137 | #define isSPACE(c) NXIsSpace((unsigned int)c) |
138 | #define isDIGIT(c) NXIsDigit((unsigned int)c) |
139 | #define isUPPER(c) NXIsUpper((unsigned int)c) |
140 | #define isLOWER(c) NXIsLower((unsigned int)c) |
141 | #define toUPPER(c) NXToUpper((unsigned int)c) |
142 | #define toLOWER(c) NXToLower((unsigned int)c) |
143 | #else /* USE_NEXT_CTYPE */ |
bee1dbe2 |
144 | #if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII)) |
85e6fe83 |
145 | #define isALNUM(c) (isalpha((unsigned char)(c)) || isdigit((unsigned char)(c)) || c == '_') |
146 | #define isIDFIRST(c) (isalpha((unsigned char)(c)) || (c) == '_') |
147 | #define isALPHA(c) isalpha((unsigned char)(c)) |
148 | #define isSPACE(c) isspace((unsigned char)(c)) |
149 | #define isDIGIT(c) isdigit((unsigned char)(c)) |
150 | #define isUPPER(c) isupper((unsigned char)(c)) |
151 | #define isLOWER(c) islower((unsigned char)(c)) |
a0d0e21e |
152 | #define toUPPER(c) toupper((unsigned char)(c)) |
153 | #define toLOWER(c) tolower((unsigned char)(c)) |
55204971 |
154 | #else |
79072805 |
155 | #define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_')) |
156 | #define isIDFIRST(c) (isascii(c) && (isalpha(c) || (c) == '_')) |
157 | #define isALPHA(c) (isascii(c) && isalpha(c)) |
158 | #define isSPACE(c) (isascii(c) && isspace(c)) |
159 | #define isDIGIT(c) (isascii(c) && isdigit(c)) |
160 | #define isUPPER(c) (isascii(c) && isupper(c)) |
161 | #define isLOWER(c) (isascii(c) && islower(c)) |
a0d0e21e |
162 | #define toUPPER(c) toupper(c) |
163 | #define toLOWER(c) tolower(c) |
55204971 |
164 | #endif |
a0d0e21e |
165 | #endif /* USE_NEXT_CTYPE */ |
55204971 |
166 | |
378cc40b |
167 | /* Line numbers are unsigned, 16 bits. */ |
79072805 |
168 | typedef U16 line_t; |
378cc40b |
169 | #ifdef lint |
170 | #define NOLINE ((line_t)0) |
171 | #else |
172 | #define NOLINE ((line_t) 65535) |
173 | #endif |
174 | |
27d4fb96 |
175 | /* XXX LEAKTEST doesn't really work in perl5. There are direct calls to |
176 | safemalloc() in the source, so LEAKTEST won't pick them up. |
177 | Further, if you try LEAKTEST, you'll also end up calling |
178 | Safefree, which might call safexfree() on some things that weren't |
179 | malloced with safexmalloc. The correct "fix" to this, if anyone |
180 | is interested, is to ensure that all calls go through the New and |
181 | Renew macros. |
182 | --Andy Dougherty August 1996 |
183 | */ |
a687059c |
184 | #ifndef lint |
185 | #ifndef LEAKTEST |
55204971 |
186 | #ifndef safemalloc |
598a3d64 |
187 | |
188 | # ifdef __cplusplus |
189 | extern "C" { |
190 | # endif |
44ae4f4f |
191 | Malloc_t safemalloc _((MEM_SIZE)); |
192 | Malloc_t saferealloc _((Malloc_t, MEM_SIZE)); |
193 | Free_t safefree _((Malloc_t)); |
194 | Malloc_t safecalloc _((MEM_SIZE, MEM_SIZE)); |
598a3d64 |
195 | # ifdef __cplusplus |
196 | } |
197 | # endif |
55204971 |
198 | #endif |
154e51a4 |
199 | #ifndef MSDOS |
a687059c |
200 | #define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t)))) |
201 | #define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n) * sizeof(t)))) |
202 | #define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t)))), \ |
bee1dbe2 |
203 | memzero((char*)(v), (n) * sizeof(t)) |
44ae4f4f |
204 | #define Renew(v,n,t) (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) |
205 | #define Renewc(v,n,t,c) (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) |
154e51a4 |
206 | #else |
207 | #define New(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t)))) |
208 | #define Newc(x,v,n,t,c) (v = (c*)safemalloc(((unsigned long)(n) * sizeof(t)))) |
209 | #define Newz(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t)))), \ |
bee1dbe2 |
210 | memzero((char*)(v), (n) * sizeof(t)) |
44ae4f4f |
211 | #define Renew(v,n,t) (v = (t*)saferealloc((Malloc_t)(v),((unsigned long)(n)*sizeof(t)))) |
212 | #define Renewc(v,n,t,c) (v = (c*)saferealloc((Malloc_t)(v),((unsigned long)(n)*sizeof(t)))) |
154e51a4 |
213 | #endif /* MSDOS */ |
44ae4f4f |
214 | #define Safefree(d) safefree((Malloc_t)(d)) |
79072805 |
215 | #define NEWSV(x,len) newSV(len) |
a687059c |
216 | #else /* LEAKTEST */ |
44ae4f4f |
217 | Malloc_t safexmalloc(); |
218 | Malloc_t safexrealloc(); |
219 | Free_t safexfree(); |
220 | Malloc_t safexcalloc(); |
a687059c |
221 | #define New(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))) |
222 | #define Newc(x,v,n,t,c) (v = (c*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))) |
223 | #define Newz(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))), \ |
bee1dbe2 |
224 | memzero((char*)(v), (n) * sizeof(t)) |
44ae4f4f |
225 | #define Renew(v,n,t) (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) |
226 | #define Renewc(v,n,t,c) (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) |
227 | #define Safefree(d) safexfree((Malloc_t)d) |
79072805 |
228 | #define NEWSV(x,len) newSV(x,len) |
a687059c |
229 | #define MAXXCOUNT 1200 |
230 | long xcount[MAXXCOUNT]; |
231 | long lastxcount[MAXXCOUNT]; |
232 | #endif /* LEAKTEST */ |
bee1dbe2 |
233 | #define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t)) |
234 | #define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t)) |
235 | #define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t)) |
a687059c |
236 | #else /* lint */ |
237 | #define New(x,v,n,s) (v = Null(s *)) |
238 | #define Newc(x,v,n,s,c) (v = Null(s *)) |
239 | #define Newz(x,v,n,s) (v = Null(s *)) |
240 | #define Renew(v,n,s) (v = Null(s *)) |
bee1dbe2 |
241 | #define Move(s,d,n,t) |
a687059c |
242 | #define Copy(s,d,n,t) |
243 | #define Zero(d,n,t) |
244 | #define Safefree(d) d = d |
245 | #endif /* lint */ |
bee1dbe2 |
246 | |
2304df62 |
247 | #ifdef USE_STRUCT_COPY |
bee1dbe2 |
248 | #define StructCopy(s,d,t) *((t*)(d)) = *((t*)(s)) |
249 | #else |
250 | #define StructCopy(s,d,t) Copy(s,d,1,t) |
251 | #endif |