This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl5.001 patch.1c
[perl5.git] / handy.h
CommitLineData
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
LW
22#define Nullch Null(char*)
23#define Nullfp Null(FILE*)
79072805 24#define Nullsv Null(SV*)
8d063cd8 25
232e078e
AD
26/* bool is built-in for g++-2.6.3, which might be used for an extension.
27 gcc-2.6.2 under Linux defines _G_HAVE_BOOL to 0, and does not
28 define bool. */
29#if !defined(HAS_BOOL) && !(_G_HAVE_BOOL)
a687059c
LW
30#ifdef UTS
31#define bool int
32#else
8d063cd8 33#define bool char
a687059c 34#endif
232e078e 35#endif /* !defined(HAS_BOOL) && !(_G_HAVE_BOOL) */
0d3e774c
LW
36
37#ifdef TRUE
38#undef TRUE
39#endif
40#ifdef FALSE
41#undef FALSE
42#endif
8d063cd8
LW
43#define TRUE (1)
44#define FALSE (0)
45
79072805
LW
46typedef char I8;
47typedef unsigned char U8;
48
49typedef short I16;
50typedef unsigned short U16;
51
a0d0e21e 52#if BYTEORDER > 0x4321
85e6fe83
LW
53 typedef int I32;
54 typedef unsigned int U32;
79072805 55#else
85e6fe83
LW
56 typedef long I32;
57 typedef unsigned long U32;
79072805
LW
58#endif
59
8d063cd8
LW
60#define Ctl(ch) (ch & 037)
61
62#define strNE(s1,s2) (strcmp(s1,s2))
63#define strEQ(s1,s2) (!strcmp(s1,s2))
64#define strLT(s1,s2) (strcmp(s1,s2) < 0)
65#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
66#define strGT(s1,s2) (strcmp(s1,s2) > 0)
67#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
68#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
69#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
378cc40b 70
2304df62
AD
71#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
72# ifndef CTYPE256
73# define CTYPE256
74# endif
75#endif
76
a0d0e21e
LW
77#ifdef USE_NEXT_CTYPE
78#define isALNUM(c) (NXIsAlpha((unsigned int)c) || NXIsDigit((unsigned int)c) || c == '_')
79#define isIDFIRST(c) (NXIsAlpha((unsigned int)c) || c == '_')
80#define isALPHA(c) NXIsAlpha((unsigned int)c)
81#define isSPACE(c) NXIsSpace((unsigned int)c)
82#define isDIGIT(c) NXIsDigit((unsigned int)c)
83#define isUPPER(c) NXIsUpper((unsigned int)c)
84#define isLOWER(c) NXIsLower((unsigned int)c)
85#define toUPPER(c) NXToUpper((unsigned int)c)
86#define toLOWER(c) NXToLower((unsigned int)c)
87#else /* USE_NEXT_CTYPE */
bee1dbe2 88#if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
85e6fe83
LW
89#define isALNUM(c) (isalpha((unsigned char)(c)) || isdigit((unsigned char)(c)) || c == '_')
90#define isIDFIRST(c) (isalpha((unsigned char)(c)) || (c) == '_')
91#define isALPHA(c) isalpha((unsigned char)(c))
92#define isSPACE(c) isspace((unsigned char)(c))
93#define isDIGIT(c) isdigit((unsigned char)(c))
94#define isUPPER(c) isupper((unsigned char)(c))
95#define isLOWER(c) islower((unsigned char)(c))
a0d0e21e
LW
96#define toUPPER(c) toupper((unsigned char)(c))
97#define toLOWER(c) tolower((unsigned char)(c))
55204971 98#else
79072805
LW
99#define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
100#define isIDFIRST(c) (isascii(c) && (isalpha(c) || (c) == '_'))
101#define isALPHA(c) (isascii(c) && isalpha(c))
102#define isSPACE(c) (isascii(c) && isspace(c))
103#define isDIGIT(c) (isascii(c) && isdigit(c))
104#define isUPPER(c) (isascii(c) && isupper(c))
105#define isLOWER(c) (isascii(c) && islower(c))
a0d0e21e
LW
106#define toUPPER(c) toupper(c)
107#define toLOWER(c) tolower(c)
55204971 108#endif
a0d0e21e 109#endif /* USE_NEXT_CTYPE */
55204971 110
378cc40b 111/* Line numbers are unsigned, 16 bits. */
79072805 112typedef U16 line_t;
378cc40b
LW
113#ifdef lint
114#define NOLINE ((line_t)0)
115#else
116#define NOLINE ((line_t) 65535)
117#endif
118
a687059c
LW
119#ifndef lint
120#ifndef LEAKTEST
55204971 121#ifndef safemalloc
a0d0e21e
LW
122char *safemalloc _((MEM_SIZE));
123char *saferealloc _((char *, MEM_SIZE));
124void safefree _((char *));
55204971 125#endif
154e51a4 126#ifndef MSDOS
a687059c
LW
127#define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t))))
128#define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n) * sizeof(t))))
129#define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n) * sizeof(t)))), \
bee1dbe2 130 memzero((char*)(v), (n) * sizeof(t))
a687059c
LW
131#define Renew(v,n,t) (v = (t*)saferealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
132#define Renewc(v,n,t,c) (v = (c*)saferealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
154e51a4
LW
133#else
134#define New(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t))))
135#define Newc(x,v,n,t,c) (v = (c*)safemalloc(((unsigned long)(n) * sizeof(t))))
136#define Newz(x,v,n,t) (v = (t*)safemalloc(((unsigned long)(n) * sizeof(t)))), \
bee1dbe2 137 memzero((char*)(v), (n) * sizeof(t))
154e51a4
LW
138#define Renew(v,n,t) (v = (t*)saferealloc((char*)(v),((unsigned long)(n)*sizeof(t))))
139#define Renewc(v,n,t,c) (v = (c*)saferealloc((char*)(v),((unsigned long)(n)*sizeof(t))))
140#endif /* MSDOS */
a687059c 141#define Safefree(d) safefree((char*)d)
79072805 142#define NEWSV(x,len) newSV(len)
a687059c
LW
143#else /* LEAKTEST */
144char *safexmalloc();
145char *safexrealloc();
146void safexfree();
147#define New(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t))))
148#define Newc(x,v,n,t,c) (v = (c*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t))))
149#define Newz(x,v,n,t) (v = (t*)safexmalloc(x,(MEM_SIZE)((n) * sizeof(t)))), \
bee1dbe2 150 memzero((char*)(v), (n) * sizeof(t))
a687059c
LW
151#define Renew(v,n,t) (v = (t*)safexrealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
152#define Renewc(v,n,t,c) (v = (c*)safexrealloc((char*)(v),(MEM_SIZE)((n)*sizeof(t))))
153#define Safefree(d) safexfree((char*)d)
79072805 154#define NEWSV(x,len) newSV(x,len)
a687059c
LW
155#define MAXXCOUNT 1200
156long xcount[MAXXCOUNT];
157long lastxcount[MAXXCOUNT];
158#endif /* LEAKTEST */
bee1dbe2
LW
159#define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
160#define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
161#define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
a687059c
LW
162#else /* lint */
163#define New(x,v,n,s) (v = Null(s *))
164#define Newc(x,v,n,s,c) (v = Null(s *))
165#define Newz(x,v,n,s) (v = Null(s *))
166#define Renew(v,n,s) (v = Null(s *))
bee1dbe2 167#define Move(s,d,n,t)
a687059c
LW
168#define Copy(s,d,n,t)
169#define Zero(d,n,t)
170#define Safefree(d) d = d
171#endif /* lint */
bee1dbe2 172
2304df62 173#ifdef USE_STRUCT_COPY
bee1dbe2
LW
174#define StructCopy(s,d,t) *((t*)(d)) = *((t*)(s))
175#else
176#define StructCopy(s,d,t) Copy(s,d,1,t)
177#endif