Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* handy.h |
a687059c | 2 | * |
a0d0e21e | 3 | * Copyright (c) 1991-1994, 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 | ||
232e078e | 35 | /* bool is built-in for g++-2.6.3, which might be used for an extension. |
5d94fbed AD |
36 | If the extension includes <_G_config.h> before this file then |
37 | _G_HAVE_BOOL will be properly set. If, however, the extension includes | |
38 | this file first, then you will have to manually set -DHAS_BOOL in | |
39 | your command line to avoid a conflict. | |
40 | */ | |
41 | #ifdef _G_HAVE_BOOL | |
42 | # if _G_HAVE_BOOL | |
43 | # ifndef HAS_BOOL | |
44 | # define HAS_BOOL 1 | |
45 | # endif | |
46 | # endif | |
47 | #endif | |
48 | ||
641d3f0b | 49 | /* The NeXT dynamic loader headers will not build with the bool macro |
50 | So declare them now to clear confusion. | |
51 | */ | |
52 | #ifdef NeXT | |
53 | # undef FALSE | |
54 | # undef TRUE | |
55 | typedef enum bool { FALSE = 0, TRUE = 1 } bool; | |
56 | # define ENUM_BOOL 1 | |
57 | # ifndef HAS_BOOL | |
58 | # define HAS_BOOL 1 | |
59 | # endif /* !HAS_BOOL */ | |
60 | #endif /* NeXT */ | |
61 | ||
5d94fbed AD |
62 | #ifndef HAS_BOOL |
63 | # ifdef UTS | |
64 | # define bool int | |
65 | # else | |
66 | # define bool char | |
67 | # endif | |
a687059c | 68 | #endif |
0d3e774c | 69 | |
79072805 LW |
70 | typedef char I8; |
71 | typedef unsigned char U8; | |
72 | ||
73 | typedef short I16; | |
74 | typedef unsigned short U16; | |
75 | ||
a0d0e21e | 76 | #if BYTEORDER > 0x4321 |
85e6fe83 LW |
77 | typedef int I32; |
78 | typedef unsigned int U32; | |
79072805 | 79 | #else |
85e6fe83 LW |
80 | typedef long I32; |
81 | typedef unsigned long U32; | |
79072805 LW |
82 | #endif |
83 | ||
8d063cd8 LW |
84 | #define Ctl(ch) (ch & 037) |
85 | ||
86 | #define strNE(s1,s2) (strcmp(s1,s2)) | |
87 | #define strEQ(s1,s2) (!strcmp(s1,s2)) | |
88 | #define strLT(s1,s2) (strcmp(s1,s2) < 0) | |
89 | #define strLE(s1,s2) (strcmp(s1,s2) <= 0) | |
90 | #define strGT(s1,s2) (strcmp(s1,s2) > 0) | |
91 | #define strGE(s1,s2) (strcmp(s1,s2) >= 0) | |
92 | #define strnNE(s1,s2,l) (strncmp(s1,s2,l)) | |
93 | #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l)) | |
378cc40b | 94 | |
2304df62 AD |
95 | #ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */ |
96 | # ifndef CTYPE256 | |
97 | # define CTYPE256 | |
98 | # endif | |
99 | #endif | |
100 | ||
a0d0e21e LW |
101 | #ifdef USE_NEXT_CTYPE |
102 | #define isALNUM(c) (NXIsAlpha((unsigned int)c) || NXIsDigit((unsigned int)c) || c == '_') | |
103 | #define isIDFIRST(c) (NXIsAlpha((unsigned int)c) || c == '_') | |
104 | #define isALPHA(c) NXIsAlpha((unsigned int)c) | |
105 | #define isSPACE(c) NXIsSpace((unsigned int)c) | |
106 | #define isDIGIT(c) NXIsDigit((unsigned int)c) | |
107 | #define isUPPER(c) NXIsUpper((unsigned int)c) | |
108 | #define isLOWER(c) NXIsLower((unsigned int)c) | |
109 | #define toUPPER(c) NXToUpper((unsigned int)c) | |
110 | #define toLOWER(c) NXToLower((unsigned int)c) | |
111 | #else /* USE_NEXT_CTYPE */ | |
bee1dbe2 | 112 | #if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII)) |
85e6fe83 LW |
113 | #define isALNUM(c) (isalpha((unsigned char)(c)) || isdigit((unsigned char)(c)) || c == '_') |
114 | #define isIDFIRST(c) (isalpha((unsigned char)(c)) || (c) == '_') | |
115 | #define isALPHA(c) isalpha((unsigned char)(c)) | |
116 | #define isSPACE(c) isspace((unsigned char)(c)) | |
117 | #define isDIGIT(c) isdigit((unsigned char)(c)) | |
118 | #define isUPPER(c) isupper((unsigned char)(c)) | |
119 | #define isLOWER(c) islower((unsigned char)(c)) | |
a0d0e21e LW |
120 | #define toUPPER(c) toupper((unsigned char)(c)) |
121 | #define toLOWER(c) tolower((unsigned char)(c)) | |
55204971 | 122 | #else |
79072805 LW |
123 | #define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_')) |
124 | #define isIDFIRST(c) (isascii(c) && (isalpha(c) || (c) == '_')) | |
125 | #define isALPHA(c) (isascii(c) && isalpha(c)) | |
126 | #define isSPACE(c) (isascii(c) && isspace(c)) | |
127 | #define isDIGIT(c) (isascii(c) && isdigit(c)) | |
128 | #define isUPPER(c) (isascii(c) && isupper(c)) | |
129 | #define isLOWER(c) (isascii(c) && islower(c)) | |
a0d0e21e LW |
130 | #define toUPPER(c) toupper(c) |
131 | #define toLOWER(c) tolower(c) | |
55204971 | 132 | #endif |
a0d0e21e | 133 | #endif /* USE_NEXT_CTYPE */ |
55204971 | 134 | |
378cc40b | 135 | /* Line numbers are unsigned, 16 bits. */ |
79072805 | 136 | typedef U16 line_t; |
378cc40b LW |
137 | #ifdef lint |
138 | #define NOLINE ((line_t)0) | |
139 | #else | |
140 | #define NOLINE ((line_t) 65535) | |
141 | #endif | |
142 | ||
a687059c LW |
143 | #ifndef lint |
144 | #ifndef LEAKTEST | |
55204971 | 145 | #ifndef safemalloc |
44ae4f4f | 146 | Malloc_t safemalloc _((MEM_SIZE)); |
147 | Malloc_t saferealloc _((Malloc_t, MEM_SIZE)); | |
148 | Free_t safefree _((Malloc_t)); | |
149 | Malloc_t safecalloc _((MEM_SIZE, MEM_SIZE)); | |
55204971 | 150 | #endif |
154e51a4 | 151 | #ifndef MSDOS |
a687059c LW |
152 | #define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t)))) |
153 | #define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n) * sizeof(t)))) | |
154 | #define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t)))), \ | |
bee1dbe2 | 155 | memzero((char*)(v), (n) * sizeof(t)) |
44ae4f4f | 156 | #define Renew(v,n,t) (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) |
157 | #define Renewc(v,n,t,c) (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) | |
154e51a4 LW |
158 | #else |
159 | #define New(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t)))) | |
160 | #define Newc(x,v,n,t,c) (v = (c*)safemalloc(((unsigned long)(n) * sizeof(t)))) | |
161 | #define Newz(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t)))), \ | |
bee1dbe2 | 162 | memzero((char*)(v), (n) * sizeof(t)) |
44ae4f4f | 163 | #define Renew(v,n,t) (v = (t*)saferealloc((Malloc_t)(v),((unsigned long)(n)*sizeof(t)))) |
164 | #define Renewc(v,n,t,c) (v = (c*)saferealloc((Malloc_t)(v),((unsigned long)(n)*sizeof(t)))) | |
154e51a4 | 165 | #endif /* MSDOS */ |
44ae4f4f | 166 | #define Safefree(d) safefree((Malloc_t)(d)) |
79072805 | 167 | #define NEWSV(x,len) newSV(len) |
a687059c | 168 | #else /* LEAKTEST */ |
44ae4f4f | 169 | Malloc_t safexmalloc(); |
170 | Malloc_t safexrealloc(); | |
171 | Free_t safexfree(); | |
172 | Malloc_t safexcalloc(); | |
a687059c LW |
173 | #define New(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))) |
174 | #define Newc(x,v,n,t,c) (v = (c*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))) | |
175 | #define Newz(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))), \ | |
bee1dbe2 | 176 | memzero((char*)(v), (n) * sizeof(t)) |
44ae4f4f | 177 | #define Renew(v,n,t) (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) |
178 | #define Renewc(v,n,t,c) (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t)))) | |
179 | #define Safefree(d) safexfree((Malloc_t)d) | |
79072805 | 180 | #define NEWSV(x,len) newSV(x,len) |
a687059c LW |
181 | #define MAXXCOUNT 1200 |
182 | long xcount[MAXXCOUNT]; | |
183 | long lastxcount[MAXXCOUNT]; | |
184 | #endif /* LEAKTEST */ | |
bee1dbe2 LW |
185 | #define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t)) |
186 | #define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t)) | |
187 | #define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t)) | |
a687059c LW |
188 | #else /* lint */ |
189 | #define New(x,v,n,s) (v = Null(s *)) | |
190 | #define Newc(x,v,n,s,c) (v = Null(s *)) | |
191 | #define Newz(x,v,n,s) (v = Null(s *)) | |
192 | #define Renew(v,n,s) (v = Null(s *)) | |
bee1dbe2 | 193 | #define Move(s,d,n,t) |
a687059c LW |
194 | #define Copy(s,d,n,t) |
195 | #define Zero(d,n,t) | |
196 | #define Safefree(d) d = d | |
197 | #endif /* lint */ | |
bee1dbe2 | 198 | |
2304df62 | 199 | #ifdef USE_STRUCT_COPY |
bee1dbe2 LW |
200 | #define StructCopy(s,d,t) *((t*)(d)) = *((t*)(s)) |
201 | #else | |
202 | #define StructCopy(s,d,t) Copy(s,d,1,t) | |
203 | #endif |