1 /* $RCSfile: sv.h,v $$Revision: 4.1 $$Date: 92/08/07 18:26:57 $
3 * Copyright (c) 1991, Larry Wall
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.
9 * Revision 4.1 92/08/07 18:26:57 lwall
11 * Revision 4.0.1.4 92/06/08 15:41:45 lwall
12 * patch20: fixed confusion between a *var's real name and its effective name
13 * patch20: removed implicit int declarations on functions
15 * Revision 4.0.1.3 91/11/05 18:41:47 lwall
16 * patch11: random cleanup
17 * patch11: solitary subroutine references no longer trigger typo warnings
19 * Revision 4.0.1.2 91/06/07 11:58:33 lwall
20 * patch4: new copyright notice
22 * Revision 4.0.1.1 91/04/12 09:16:12 lwall
23 * patch1: you may now use "die" and "caller" in a signal handler
25 * Revision 4.0 91/03/20 01:40:04 lwall
49 /* Using C's structural equivalence to help emulate C++ inheritance here... */
52 void* sv_any; /* pointer to something */
53 U32 sv_refcnt; /* how many references to us */
54 U32 sv_flags; /* what we are */
58 XPVGV* sv_any; /* pointer to something */
59 U32 sv_refcnt; /* how many references to us */
60 U32 sv_flags; /* what we are */
64 XPVGV* sv_any; /* pointer to something */
65 U32 sv_refcnt; /* how many references to us */
66 U32 sv_flags; /* what we are */
70 XPVAV* sv_any; /* pointer to something */
71 U32 sv_refcnt; /* how many references to us */
72 U32 sv_flags; /* what we are */
76 XPVHV* sv_any; /* pointer to something */
77 U32 sv_refcnt; /* how many references to us */
78 U32 sv_flags; /* what we are */
82 XPVIO* sv_any; /* pointer to something */
83 U32 sv_refcnt; /* how many references to us */
84 U32 sv_flags; /* what we are */
87 #define SvANY(sv) (sv)->sv_any
88 #define SvFLAGS(sv) (sv)->sv_flags
90 #define SvREFCNT(sv) (sv)->sv_refcnt
92 #define SvREFCNT_inc(sv) sv_newref(sv)
93 #define SvREFCNT_dec(sv) sv_free(sv)
95 #define SvREFCNT_inc(sv) ((Sv = (SV*)(sv)), \
96 (Sv && ++SvREFCNT(Sv)), (SV*)Sv)
97 #define SvREFCNT_dec(sv) sv_free(sv)
100 #define SVTYPEMASK 0xff
101 #define SvTYPE(sv) ((sv)->sv_flags & SVTYPEMASK)
103 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
105 #define SVs_PADBUSY 0x00000100 /* reserved for tmp or my already */
106 #define SVs_PADTMP 0x00000200 /* in use as tmp */
107 #define SVs_PADMY 0x00000400 /* in use a "my" variable */
108 #define SVs_TEMP 0x00000800 /* string is stealable? */
109 #define SVs_OBJECT 0x00001000 /* is "blessed" */
110 #define SVs_GMG 0x00002000 /* has magical get method */
111 #define SVs_SMG 0x00004000 /* has magical set method */
112 #define SVs_RMG 0x00008000 /* has random magical methods */
114 #define SVf_IOK 0x00010000 /* has valid public integer value */
115 #define SVf_NOK 0x00020000 /* has valid public numeric value */
116 #define SVf_POK 0x00040000 /* has valid public pointer value */
117 #define SVf_ROK 0x00080000 /* has a valid reference pointer */
118 #define SVf_OK 0x00100000 /* has defined value */
119 #define SVf_OOK 0x00200000 /* has valid offset value */
120 #define SVf_BREAK 0x00400000 /* refcnt is artificially low */
121 #define SVf_READONLY 0x00800000 /* may not be modified */
123 #define SVp_IOK 0x01000000 /* has valid non-public integer value */
124 #define SVp_NOK 0x02000000 /* has valid non-public numeric value */
125 #define SVp_POK 0x04000000 /* has valid non-public pointer value */
126 #define SVp_SCREAM 0x08000000 /* has been studied? */
130 /* Some private flags. */
132 #define SVpfm_COMPILED 0x80000000
134 #define SVpbm_VALID 0x80000000
135 #define SVpbm_CASEFOLD 0x40000000
136 #define SVpbm_TAIL 0x20000000
138 #define SVpgv_MULTI 0x80000000
141 SV * xrv_rv; /* pointer to another SV */
145 char * xpv_pv; /* pointer to malloced string */
146 STRLEN xpv_cur; /* length of xpv_pv as a C string */
147 STRLEN xpv_len; /* allocated size */
151 char * xpv_pv; /* pointer to malloced string */
152 STRLEN xpv_cur; /* length of xpv_pv as a C string */
153 STRLEN xpv_len; /* allocated size */
154 I32 xiv_iv; /* integer value or pv offset */
158 char * xpv_pv; /* pointer to malloced string */
159 STRLEN xpv_cur; /* length of xpv_pv as a C string */
160 STRLEN xpv_len; /* allocated size */
161 I32 xiv_iv; /* integer value or pv offset */
162 double xnv_nv; /* numeric value, if any */
166 char * xpv_pv; /* pointer to malloced string */
167 STRLEN xpv_cur; /* length of xpv_pv as a C string */
168 STRLEN xpv_len; /* allocated size */
169 I32 xiv_iv; /* integer value or pv offset */
170 double xnv_nv; /* numeric value, if any */
171 MAGIC* xmg_magic; /* linked list of magicalness */
172 HV* xmg_stash; /* class package */
176 char * xpv_pv; /* pointer to malloced string */
177 STRLEN xpv_cur; /* length of xpv_pv as a C string */
178 STRLEN xpv_len; /* allocated size */
179 I32 xiv_iv; /* integer value or pv offset */
180 double xnv_nv; /* numeric value, if any */
181 MAGIC* xmg_magic; /* linked list of magicalness */
182 HV* xmg_stash; /* class package */
191 char * xpv_pv; /* pointer to malloced string */
192 STRLEN xpv_cur; /* length of xpv_pv as a C string */
193 STRLEN xpv_len; /* allocated size */
194 I32 xiv_iv; /* integer value or pv offset */
195 double xnv_nv; /* numeric value, if any */
196 MAGIC* xmg_magic; /* linked list of magicalness */
197 HV* xmg_stash; /* class package */
206 char * xpv_pv; /* pointer to malloced string */
207 STRLEN xpv_cur; /* length of xpv_pv as a C string */
208 STRLEN xpv_len; /* allocated size */
209 I32 xiv_iv; /* integer value or pv offset */
210 double xnv_nv; /* numeric value, if any */
211 MAGIC* xmg_magic; /* linked list of magicalness */
212 HV* xmg_stash; /* class package */
214 I32 xbm_useful; /* is this constant pattern being useful? */
215 U16 xbm_previous; /* how many characters in string before rare? */
216 U8 xbm_rare; /* rarest character in string */
220 char * xpv_pv; /* pointer to malloced string */
221 STRLEN xpv_cur; /* length of xpv_pv as a C string */
222 STRLEN xpv_len; /* allocated size */
223 I32 xiv_iv; /* integer value or pv offset */
224 double xnv_nv; /* numeric value, if any */
225 MAGIC* xmg_magic; /* linked list of magicalness */
226 HV* xmg_stash; /* class package */
231 I32 (*xcv_usersub)();
234 long xcv_depth; /* >= 2 indicates recursive call */
241 char * xpv_pv; /* pointer to malloced string */
242 STRLEN xpv_cur; /* length of xpv_pv as a C string */
243 STRLEN xpv_len; /* allocated size */
244 I32 xiv_iv; /* integer value or pv offset */
245 double xnv_nv; /* numeric value, if any */
246 MAGIC* xmg_magic; /* linked list of magicalness */
247 HV* xmg_stash; /* class package */
249 FILE * xio_ifp; /* ifp and ofp are normally the same */
250 FILE * xio_ofp; /* but sockets need separate streams */
251 DIR * xio_dirp; /* for opendir, readdir, etc */
252 long xio_lines; /* $. */
253 long xio_page; /* $% */
254 long xio_page_len; /* $= */
255 long xio_lines_left; /* $- */
256 char * xio_top_name; /* $^ */
257 GV * xio_top_gv; /* $^ */
258 char * xio_fmt_name; /* $~ */
259 GV * xio_fmt_gv; /* $~ */
260 char * xio_bottom_name;/* $^B */
261 GV * xio_bottom_gv; /* $^B */
262 short xio_subprocess; /* -| or |- */
267 #define IOf_ARGV 1 /* this fp iterates over ARGV */
268 #define IOf_START 2 /* check for null ARGV and substitute '-' */
269 #define IOf_FLUSH 4 /* this fp wants a flush after write op */
271 /* The following macros define implementation-independent predicates on SVs. */
273 #define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
275 #define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
276 #define SvOK_on(sv) (SvFLAGS(sv) |= SVf_OK)
277 #define SvOK_off(sv) (SvFLAGS(sv) &= \
278 ~(SVf_IOK|SVf_NOK|SVf_POK|SVf_OK| \
279 SVp_IOK|SVp_NOK|SVp_POK|SVf_ROK),\
282 #define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
283 #define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
284 #define SvIOKp_on(sv) (SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
285 #define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
286 #define SvNOKp_on(sv) (SvFLAGS(sv) |= SVp_NOK)
287 #define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
288 #define SvPOKp_on(sv) (SvFLAGS(sv) |= SVp_POK)
290 #define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
291 #define SvIOK_on(sv) (SvOOK_off(sv), \
292 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK|SVf_OK))
293 #define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK))
294 #define SvIOK_only(sv) (SvOK_off(sv), \
295 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK|SVf_OK))
297 #define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
298 #define SvNOK_on(sv) (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK|SVf_OK))
299 #define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
300 #define SvNOK_only(sv) (SvOK_off(sv), \
301 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK|SVf_OK))
303 #define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
304 #define SvPOK_on(sv) (SvFLAGS(sv) |= (SVf_POK|SVp_POK|SVf_OK))
305 #define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
306 #define SvPOK_only(sv) (SvOK_off(sv), \
307 SvFLAGS(sv) |= (SVf_POK|SVp_POK|SVf_OK))
309 #define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
310 #define SvOOK_on(sv) (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
311 #define SvOOK_off(sv) (SvOOK(sv) && sv_backoff(sv))
313 #define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
314 #define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK|SVf_OK)
315 #define SvROK_off(sv) (SvFLAGS(sv) &= ~SVf_ROK)
317 #define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
318 #define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
319 #define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
321 #define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
322 #define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
323 #define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
325 #define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
326 #define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
327 #define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
329 #define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
330 #define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
331 #define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
333 #define SvTHINKFIRST(sv) (SvFLAGS(sv) & (SVf_ROK|SVf_READONLY))
335 #define SvPADBUSY(sv) (SvFLAGS(sv) & SVs_PADBUSY)
337 #define SvPADTMP(sv) (SvFLAGS(sv) & SVs_PADTMP)
338 #define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
339 #define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
341 #define SvPADMY(sv) (SvFLAGS(sv) & SVs_PADMY)
342 #define SvPADMY_on(sv) (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
344 #define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
345 #define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
346 #define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
348 #define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
349 #define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
350 #define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
352 #define SvREADONLY(sv) (SvFLAGS(sv) & SVf_READONLY)
353 #define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
354 #define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
356 #define SvSCREAM(sv) (SvFLAGS(sv) & SVp_SCREAM)
357 #define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
358 #define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
360 #define SvCOMPILED(sv) (SvFLAGS(sv) & SVpfm_COMPILED)
361 #define SvCOMPILED_on(sv) (SvFLAGS(sv) |= SVpfm_COMPILED)
362 #define SvCOMPILED_off(sv) (SvFLAGS(sv) &= ~SVpfm_COMPILED)
364 #define SvTAIL(sv) (SvFLAGS(sv) & SVpbm_TAIL)
365 #define SvTAIL_on(sv) (SvFLAGS(sv) |= SVpbm_TAIL)
366 #define SvTAIL_off(sv) (SvFLAGS(sv) &= ~SVpbm_TAIL)
368 #define SvCASEFOLD(sv) (SvFLAGS(sv) & SVpbm_CASEFOLD)
369 #define SvCASEFOLD_on(sv) (SvFLAGS(sv) |= SVpbm_CASEFOLD)
370 #define SvCASEFOLD_off(sv) (SvFLAGS(sv) &= ~SVpbm_CASEFOLD)
372 #define SvVALID(sv) (SvFLAGS(sv) & SVpbm_VALID)
373 #define SvVALID_on(sv) (SvFLAGS(sv) |= SVpbm_VALID)
374 #define SvVALID_off(sv) (SvFLAGS(sv) &= ~SVpbm_VALID)
376 #define SvMULTI(sv) (SvFLAGS(sv) & SVpgv_MULTI)
377 #define SvMULTI_on(sv) (SvFLAGS(sv) |= SVpgv_MULTI)
378 #define SvMULTI_off(sv) (SvFLAGS(sv) &= ~SVpgv_MULTI)
380 #define SvRV(sv) ((XRV*) SvANY(sv))->xrv_rv
381 #define SvRVx(sv) SvRV(sv)
383 #define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
384 #define SvIVXx(sv) SvIVX(sv)
385 #define SvNVX(sv) ((XPVNV*)SvANY(sv))->xnv_nv
386 #define SvNVXx(sv) SvNVX(sv)
387 #define SvPVX(sv) ((XPV*) SvANY(sv))->xpv_pv
388 #define SvPVXx(sv) SvPVX(sv)
389 #define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
390 #define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
391 #define SvLENx(sv) SvLEN(sv)
392 #define SvEND(sv)(((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
393 #define SvENDx(sv) ((Sv = (sv)), SvEND(Sv))
394 #define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_magic
395 #define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
397 #define SvIV_set(sv, val) \
398 do { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
399 (((XPVIV*) SvANY(sv))->xiv_iv = val); } while (0)
400 #define SvNV_set(sv, val) \
401 do { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
402 (((XPVNV*) SvANY(sv))->xnv_nv = val); } while (0)
403 #define SvPV_set(sv, val) \
404 do { assert(SvTYPE(sv) >= SVt_PV); \
405 (((XPV*) SvANY(sv))->xpv_pv = val); } while (0)
406 #define SvCUR_set(sv, val) \
407 do { assert(SvTYPE(sv) >= SVt_PV); \
408 (((XPV*) SvANY(sv))->xpv_cur = val); } while (0)
409 #define SvLEN_set(sv, val) \
410 do { assert(SvTYPE(sv) >= SVt_PV); \
411 (((XPV*) SvANY(sv))->xpv_len = val); } while (0)
412 #define SvEND_set(sv, val) \
413 do { assert(SvTYPE(sv) >= SVt_PV); \
414 (((XPV*) SvANY(sv))->xpv_cur = val - SvPVX(sv)); } while (0)
416 #define SvCUROK(sv) (SvPOK(sv) ? SvCUR(sv) : 0)
418 #define BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare
419 #define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful
420 #define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous
422 #define FmLINES(sv) ((XPVFM*) SvANY(sv))->xfm_lines
424 #define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
425 #define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
426 #define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
427 #define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
429 #define IoIFP(sv) ((XPVIO*) SvANY(sv))->xio_ifp
430 #define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
431 #define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
432 #define IoLINES(sv) ((XPVIO*) SvANY(sv))->xio_lines
433 #define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
434 #define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
435 #define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
436 #define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
437 #define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
438 #define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
439 #define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
440 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
441 #define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
442 #define IoSUBPROCESS(sv)((XPVIO*) SvANY(sv))->xio_subprocess
443 #define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
444 #define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
446 #define SvTAINT(sv) if (tainting && tainted) sv_magic(sv, 0, 't', 0, 0)
452 #define SvPV(sv, lp) sv_pvn(sv, &lp)
456 #define SvIVx(sv) SvIV(sv)
457 #define SvNVx(sv) SvNV(sv)
458 #define SvPVx(sv, lp) sv_pvn(sv, &lp)
459 #define SvTRUEx(sv) SvTRUE(sv)
461 #else /* !CRIPPLED_CC */
463 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
465 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
467 #define SvPV(sv, lp) (SvPOK(sv) ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
469 #define SvTRUE(sv) ( \
473 ? ((Xpv = (XPV*)SvANY(sv)) && \
474 (*Xpv->xpv_pv > '0' || \
475 Xpv->xpv_cur > 1 || \
476 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')) \
486 #define SvIVx(sv) ((Sv = (sv)), SvIV(Sv))
487 #define SvNVx(sv) ((Sv = (sv)), SvNV(Sv))
488 #define SvPVx(sv, lp) ((Sv = (sv)), SvPV(Sv, lp))
489 #define SvTRUEx(sv) ((Sv = (sv)), SvTRUE(Sv))
491 #endif /* CRIPPLED_CC */
493 /* the following macro updates any magic values this sv is associated with */
495 #define SvSETMAGIC(x) if (SvSMAGICAL(x)) mg_set(x)
497 #define SvSetSV(dst,src) if (dst != src) sv_setsv(dst,src)
499 #define SvPEEK(sv) sv_peek(sv)
501 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
504 # define SvGROW(sv,len) if (SvLEN(sv) < (len)) sv_grow(sv,len)
505 # define Sv_Grow sv_grow
507 /* extra parentheses intentionally NOT placed around "len"! */
508 # define SvGROW(sv,len) if (SvLEN(sv) < (unsigned long)len) \
509 sv_grow(sv,(unsigned long)len)
510 # define Sv_Grow(sv,len) sv_grow(sv,(unsigned long)(len))