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