This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Let Deparse.t be run from the top-level
[perl5.git] / util.h
1 /*    util.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1999, 2001, 2002, 2003, 2004, 2005,
4  *    2007, by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 #ifndef PERL_UTIL_H_
12 #define PERL_UTIL_H_
13
14
15 #ifdef VMS
16 #  define PERL_FILE_IS_ABSOLUTE(f) \
17         (*(f) == '/'                                                    \
18          || (strchr(f,':')                                              \
19              || ((*(f) == '[' || *(f) == '<')                           \
20                  && (isWORDCHAR((f)[1]) || strchr("$-_]>",(f)[1])))))
21
22 #else           /* !VMS */
23 #  if defined(WIN32) || defined(__CYGWIN__)
24 #    define PERL_FILE_IS_ABSOLUTE(f) \
25         (*(f) == '/' || *(f) == '\\'            /* UNC/rooted path */   \
26          || ((f)[0] && (f)[1] == ':'))          /* drive name */
27 #  else         /* !WIN32 */
28 #  ifdef NETWARE
29 #    define PERL_FILE_IS_ABSOLUTE(f) \
30         (((f)[0] && (f)[1] == ':')              /* drive name */        \
31          || ((f)[0] == '\\' && (f)[1] == '\\')  /* UNC path */  \
32          ||     ((f)[3] == ':'))                                /* volume name, currently only sys */
33 #  else         /* !NETWARE */
34 #    if defined(DOSISH) || defined(__SYMBIAN32__)
35 #      define PERL_FILE_IS_ABSOLUTE(f) \
36         (*(f) == '/'                                                    \
37          || ((f)[0] && (f)[1] == ':'))          /* drive name */
38 #    else       /* NEITHER DOSISH NOR SYMBIANISH */
39 #      define PERL_FILE_IS_ABSOLUTE(f)  (*(f) == '/')
40 #    endif      /* DOSISH */
41 #   endif       /* NETWARE */
42 #  endif        /* WIN32 */
43 #endif          /* VMS */
44
45 /*
46 =head1 Miscellaneous Functions
47
48 =for apidoc ibcmp
49
50 This is a synonym for S<C<(! foldEQ())>>
51
52 =for apidoc ibcmp_locale
53
54 This is a synonym for S<C<(! foldEQ_locale())>>
55
56 =cut
57 */
58 #define ibcmp(s1, s2, len)         cBOOL(! foldEQ(s1, s2, len))
59 #define ibcmp_locale(s1, s2, len)  cBOOL(! foldEQ_locale(s1, s2, len))
60
61 /* outside the core, perl.h undefs HAS_QUAD if IV isn't 64-bit
62    We can't swap this to HAS_QUAD, because the logic here affects the type of
63    perl_drand48_t below, and that is visible outside of the core.  */
64 #if defined(U64TYPE) && !defined(USING_MSVC6)
65 /* use a faster implementation when quads are available,
66  * but not with VC6 on Windows */
67 #    define PERL_DRAND48_QUAD
68 #endif
69
70 #ifdef PERL_DRAND48_QUAD
71
72 /* U64 is only defined under PERL_CORE, but this needs to be visible
73  * elsewhere so the definition of PerlInterpreter is complete.
74  */
75 typedef U64TYPE perl_drand48_t;
76
77 #else
78
79 struct PERL_DRAND48_T {
80     U16 seed[3];
81 };
82
83 typedef struct PERL_DRAND48_T perl_drand48_t;
84
85 #endif
86
87 #define PL_RANDOM_STATE_TYPE perl_drand48_t
88
89 #define Perl_drand48_init(seed) (Perl_drand48_init_r(&PL_random_state, (seed)))
90 #define Perl_drand48() (Perl_drand48_r(&PL_random_state))
91
92 #ifdef PERL_CORE
93 /* uses a different source of randomness to avoid interfering with the results
94  * of rand() */
95 #define Perl_internal_drand48() (Perl_drand48_r(&PL_internal_random_state))
96 #endif
97
98 #ifdef USE_C_BACKTRACE
99
100 typedef struct {
101     /* The number of frames returned. */
102     UV frame_count;
103     /* The total size of the Perl_c_backtrace, including this header,
104      * the frames, and the name strings. */
105     UV total_bytes;
106 } Perl_c_backtrace_header;
107
108 typedef struct {
109     void*  addr;  /* the program counter at this frame */
110
111     /* We could use Dl_info (as used by dladdr()) for many of these but
112      * that would be naughty towards non-dlfcn systems (hi there, Win32). */
113
114     void*  symbol_addr; /* symbol address (hint: try symbol_addr - addr) */
115     void*  object_base_addr;   /* base address of the shared object */
116
117     /* The offsets are from the beginning of the whole backtrace,
118      * which makes the backtrace relocatable. */
119     STRLEN object_name_offset; /* pathname of the shared object */
120     STRLEN object_name_size;   /* length of the pathname */
121     STRLEN symbol_name_offset; /* symbol name */
122     STRLEN symbol_name_size;   /* length of the symbol name */
123     STRLEN source_name_offset; /* source code file name */
124     STRLEN source_name_size;   /* length of the source code file name */
125     STRLEN source_line_number; /* source code line number */
126
127     /* OS X notes: atos(1) (more recently, "xcrun atos"), but the C
128      * API atos() uses is unknown (private "Symbolicator" framework,
129      * might require Objective-C even if the API would be known).
130      * Currently we open read pipe to "xcrun atos" and parse the
131      * output - quite disgusting.  And that won't work if the
132      * Developer Tools isn't installed. */
133
134     /* FreeBSD notes: execinfo.h exists, but probably would need also
135      * the library -lexecinfo.  BFD exists if the pkg devel/binutils
136      * has been installed, but there seems to be a known problem that
137      * the "bfd.h" getting installed refers to "ansidecl.h", which
138      * doesn't get installed. */
139
140     /* Win32 notes: as moral equivalents of backtrace() + dladdr(),
141      * one could possibly first use GetCurrentProcess() +
142      * SymInitialize(), and then CaptureStackBackTrace() +
143      * SymFromAddr(). */
144
145     /* Note that using the compiler optimizer easily leads into much
146      * of this information, like the symbol names (think inlining),
147      * and source code locations getting lost or confused.  In many
148      * cases keeping the debug information (-g) is necessary.
149      *
150      * Note that for example with gcc you can do both -O and -g.
151      *
152      * Note, however, that on some platforms (e.g. OSX + clang (cc))
153      * backtrace() + dladdr() works fine without -g. */
154
155     /* For example: the mere presence of <bfd.h> is no guarantee: e.g.
156      * OS X has that, but BFD does not seem to work on the OSX executables.
157      *
158      * Another niceness would be to able to see something about
159      * the function arguments, however gdb/lldb manage to do that. */
160 } Perl_c_backtrace_frame;
161
162 typedef struct {
163     Perl_c_backtrace_header header;
164     Perl_c_backtrace_frame  frame_info[1];
165     /* After the header come:
166      * (1) header.frame_count frames
167      * (2) frame_count times the \0-terminated strings (object_name
168      * and so forth).  The frames contain the pointers to the starts
169      * of these strings, and the lengths of these strings. */
170 } Perl_c_backtrace;
171
172 #define Perl_free_c_backtrace(bt) Safefree(bt)
173
174 #endif /* USE_C_BACKTRACE */
175
176 /* Use a packed 32 bit constant "key" to start the handshake. The key defines
177    ABI compatibility, and how to process the vararg list.
178
179    Note, some bits may be taken from INTRPSIZE (but then a simple x86 AX register
180    can't be used to read it) and 4 bits from API version len can also be taken,
181    since v00.00.00 is 9 bytes long. XS version length should not have any bits
182    taken since XS_VERSION lengths can get quite long since they are user
183    selectable. These spare bits allow for additional features for the varargs
184    stuff or ABI compat test flags in the future.
185 */
186 #define HSm_APIVERLEN 0x0000001F /* perl version string won't be more than 31 chars */
187 #define HS_APIVERLEN_MAX HSm_APIVERLEN
188 #define HSm_XSVERLEN 0x0000FF00 /* if 0, not present, dont check, die if over 255*/
189 #define HS_XSVERLEN_MAX 0xFF
190 /* uses var file to set default filename for newXS_deffile to use for CvFILE */
191 #define HSf_SETXSUBFN 0x00000020
192 #define HSf_POPMARK 0x00000040 /* popmark mode or you must supply ax and items */
193 #define HSf_IMP_CXT 0x00000080 /* ABI, threaded/PERL_IMPLICIT_CONTEXT, pTHX_ present */
194 #define HSm_INTRPSIZE 0xFFFF0000 /* ABI, interp struct size */
195 /* A mask of bits in the key which must always match between a XS mod and interp.
196    Also if all ABI bits in a key are true, skip all ABI checks, it is very
197    the unlikely interp size will all 1 bits */
198 /* Maybe HSm_APIVERLEN one day if Perl_xs_apiversion_bootcheck is changed to a memcmp */
199 #define HSm_KEY_MATCH (HSm_INTRPSIZE|HSf_IMP_CXT)
200 #define HSf_NOCHK HSm_KEY_MATCH  /* if all ABI bits are 1 in the key, dont chk */
201
202
203 #define HS_GETINTERPSIZE(key) ((key) >> 16)
204 /* if in the future "" and NULL must be separated, XSVERLEN would be 0
205 means arg not present, 1 is empty string/null byte */
206 /* (((key) & 0x0000FF00) >> 8) is less efficient on Visual C */
207 #define HS_GETXSVERLEN(key) ((key) >> 8 & 0xFF)
208 #define HS_GETAPIVERLEN(key) ((key) & HSm_APIVERLEN)
209
210 /* internal to util.h macro to create a packed handshake key, all args must be constants */
211 /* U32 return = (U16 interpsize, bool cxt, bool setxsubfn, bool popmark,
212    U5 (FIVE!) apiverlen, U8 xsverlen) */
213 #define HS_KEYp(interpsize, cxt, setxsubfn, popmark, apiverlen, xsverlen) \
214     (((interpsize)  << 16) \
215     | ((xsverlen) > HS_XSVERLEN_MAX \
216         ? (Perl_croak_nocontext("panic: handshake overflow"), HS_XSVERLEN_MAX) \
217         : (xsverlen) << 8) \
218     | (cBOOL(setxsubfn) ? HSf_SETXSUBFN : 0) \
219     | (cBOOL(cxt) ? HSf_IMP_CXT : 0) \
220     | (cBOOL(popmark) ? HSf_POPMARK : 0) \
221     | ((apiverlen) > HS_APIVERLEN_MAX \
222         ? (Perl_croak_nocontext("panic: handshake overflow"), HS_APIVERLEN_MAX) \
223         : (apiverlen)))
224 /* overflows above will optimize away unless they will execute */
225
226 /* public macro for core usage to create a packed handshake key but this is
227    not public API. This more friendly version already collected all ABI info */
228 /* U32 return = (bool setxsubfn, bool popmark, "litteral_string_api_ver",
229    "litteral_string_xs_ver") */
230 #ifdef PERL_IMPLICIT_CONTEXT
231 #  define HS_KEY(setxsubfn, popmark, apiver, xsver) \
232     HS_KEYp(sizeof(PerlInterpreter), TRUE, setxsubfn, popmark, \
233     sizeof("" apiver "")-1, sizeof("" xsver "")-1)
234 #  define HS_CXT aTHX
235 #else
236 #  define HS_KEY(setxsubfn, popmark, apiver, xsver) \
237     HS_KEYp(sizeof(struct PerlHandShakeInterpreter), FALSE, setxsubfn, popmark, \
238     sizeof("" apiver "")-1, sizeof("" xsver "")-1)
239 #  define HS_CXT cv
240 #endif
241
242 #define instr(haystack, needle) strstr(haystack, needle)
243
244 #ifdef HAS_MEMMEM
245 #   define ninstr(big, bigend, little, lend)                                \
246             ((char *) memmem((big), (bigend) - (big),                       \
247                              (little), (lend) - (little)))
248 #endif
249
250 #if defined(HAS_MKSTEMP) && defined(PERL_CORE)
251 #   define Perl_my_mkstemp(templte) mkstemp(templte)
252 #endif
253
254 #endif /* PERL_UTIL_H_ */
255
256 /*
257  * ex: set ts=8 sts=4 sw=4 et:
258  */