Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!bionet!ames!henry.jpl.nasa.gov!elroy.jpl.nasa.gov!jato!lwall From: lwall@jato.Jpl.Nasa.Gov (Larry Wall) Newsgroups: alt.sources Subject: perl 3.0 beta kit [13/23] Message-ID: <1678@jato.Jpl.Nasa.Gov> Date: 3 Sep 89 19:00:08 GMT Reply-To: lwall@jato.Jpl.Nasa.Gov (Larry Wall) Followup-To: comp.sources.d Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 2147 #! /bin/sh # Make a new directory for the perl sources, cd to it, and run kits 1 # thru 23 through sh. When all 23 kits have been run, read README. echo "This is perl 3.0 kit 13 (of 23). If kit 13 is complete, the line" echo '"'"End of kit 13 (of 23)"'" will echo at the end.' echo "" export PATH || (echo "You didn't use sh, you clunch." ; kill $$) mkdir x2p 2>/dev/null echo Extracting consarg.c sed >consarg.c <<'!STUFFY!FUNK!' -e 's/X//' X/* $Header$ X * X * Copyright (c) 1989, Larry Wall X * X * You may distribute under the terms of the GNU General Public License X * as specified in the README file that comes with the perl 3.0 kit. X * X * $Log$ X */ X X#include "EXTERN.h" X#include "perl.h" Xstatic int nothing_in_common(); Xstatic int arg_common(); Xstatic int spat_common(); X XARG * Xmake_split(stab,arg,limarg) Xregister STAB *stab; Xregister ARG *arg; XARG *limarg; X{ X register SPAT *spat; X X if (arg->arg_type != O_MATCH) { X Newz(201,spat,1,SPAT); X spat->spat_next = curstash->tbl_spatroot; /* link into spat list */ X curstash->tbl_spatroot = spat; X X spat->spat_runtime = arg; X arg = make_match(O_MATCH,stab2arg(A_STAB,defstab),spat); X } X Renew(arg,3,ARG); X arg->arg_len = 3; X if (limarg) { X if (limarg->arg_type == O_ITEM) { X Copy(limarg+1,arg+3,1,ARG); X limarg[1].arg_type = A_NULL; X arg_free(limarg); X } X else { X arg[3].arg_type = A_EXPR; X arg[3].arg_ptr.arg_arg = limarg; X } X } X else X arg[3].arg_type = A_NULL; X arg->arg_type = O_SPLIT; X spat = arg[2].arg_ptr.arg_spat; X spat->spat_repl = stab2arg(A_STAB,aadd(stab)); X if (spat->spat_short) { /* exact match can bypass regexec() */ X if (!((spat->spat_flags & SPAT_SCANFIRST) && X (spat->spat_flags & SPAT_ALL) )) { X str_free(spat->spat_short); X spat->spat_short = Nullstr; X } X } X return arg; X} X XARG * Xmod_match(type,left,pat) Xregister ARG *left; Xregister ARG *pat; X{ X X register SPAT *spat; X register ARG *newarg; X X if ((pat->arg_type == O_MATCH || X pat->arg_type == O_SUBST || X pat->arg_type == O_TRANS || X pat->arg_type == O_SPLIT X ) && X pat[1].arg_ptr.arg_stab == defstab ) { X switch (pat->arg_type) { X case O_MATCH: X newarg = make_op(type == O_MATCH ? O_MATCH : O_NMATCH, X pat->arg_len, X left,Nullarg,Nullarg,0); X break; X case O_SUBST: X newarg = l(make_op(type == O_MATCH ? O_SUBST : O_NSUBST, X pat->arg_len, X left,Nullarg,Nullarg,0)); X break; X case O_TRANS: X newarg = l(make_op(type == O_MATCH ? O_TRANS : O_NTRANS, X pat->arg_len, X left,Nullarg,Nullarg,0)); X break; X case O_SPLIT: X newarg = make_op(type == O_MATCH ? O_SPLIT : O_SPLIT, X pat->arg_len, X left,Nullarg,Nullarg,0); X break; X } X if (pat->arg_len >= 2) { X newarg[2].arg_type = pat[2].arg_type; X newarg[2].arg_ptr = pat[2].arg_ptr; X newarg[2].arg_flags = pat[2].arg_flags; X if (pat->arg_len >= 3) { X newarg[3].arg_type = pat[3].arg_type; X newarg[3].arg_ptr = pat[3].arg_ptr; X newarg[3].arg_flags = pat[3].arg_flags; X } X } X Safefree(pat); X } X else { X Newz(202,spat,1,SPAT); X spat->spat_next = curstash->tbl_spatroot; /* link into spat list */ X curstash->tbl_spatroot = spat; X X spat->spat_runtime = pat; X newarg = make_op(type,2,left,Nullarg,Nullarg,0); X newarg[2].arg_type = A_SPAT | A_DONT; X newarg[2].arg_ptr.arg_spat = spat; X } X X return newarg; X} X XARG * Xmake_op(type,newlen,arg1,arg2,arg3) Xint type; Xint newlen; XARG *arg1; XARG *arg2; XARG *arg3; X{ X register ARG *arg; X register ARG *chld; X register int doarg; X extern ARG *arg4; /* should be normal arguments, really */ X extern ARG *arg5; X X arg = op_new(newlen); X arg->arg_type = type; X doarg = opargs[type]; X if (chld = arg1) { X if (chld->arg_type == O_ITEM && X (hoistable[chld[1].arg_type] || chld[1].arg_type == A_LVAL || X (chld[1].arg_type == A_LEXPR && X (chld[1].arg_ptr.arg_arg->arg_type == O_LIST || X chld[1].arg_ptr.arg_arg->arg_type == O_ARRAY || X chld[1].arg_ptr.arg_arg->arg_type == O_HASH )))) X { X arg[1].arg_type = chld[1].arg_type; X arg[1].arg_ptr = chld[1].arg_ptr; X arg[1].arg_flags |= chld[1].arg_flags; X arg[1].arg_len = chld[1].arg_len; X free_arg(chld); X } X else { X arg[1].arg_type = A_EXPR; X arg[1].arg_ptr.arg_arg = chld; X } X if (!(doarg & 1)) X arg[1].arg_type |= A_DONT; X if (doarg & 2) X arg[1].arg_flags |= AF_ARYOK; X } X doarg >>= 2; X if (chld = arg2) { X if (chld->arg_type == O_ITEM && X (hoistable[chld[1].arg_type] || X (type == O_ASSIGN && X ((chld[1].arg_type == A_READ && !(arg[1].arg_type & A_DONT)) X || X (chld[1].arg_type == A_INDREAD && !(arg[1].arg_type & A_DONT)) X || X (chld[1].arg_type == A_GLOB && !(arg[1].arg_type & A_DONT)) X ) ) ) ) { X arg[2].arg_type = chld[1].arg_type; X arg[2].arg_ptr = chld[1].arg_ptr; X arg[2].arg_len = chld[1].arg_len; X free_arg(chld); X } X else { X arg[2].arg_type = A_EXPR; X arg[2].arg_ptr.arg_arg = chld; X } X if (!(doarg & 1)) X arg[2].arg_type |= A_DONT; X if (doarg & 2) X arg[2].arg_flags |= AF_ARYOK; X } X doarg >>= 2; X if (chld = arg3) { X if (chld->arg_type == O_ITEM && hoistable[chld[1].arg_type]) { X arg[3].arg_type = chld[1].arg_type; X arg[3].arg_ptr = chld[1].arg_ptr; X arg[3].arg_len = chld[1].arg_len; X free_arg(chld); X } X else { X arg[3].arg_type = A_EXPR; X arg[3].arg_ptr.arg_arg = chld; X } X if (!(doarg & 1)) X arg[3].arg_type |= A_DONT; X if (doarg & 2) X arg[3].arg_flags |= AF_ARYOK; X } X if (newlen >= 4 && (chld = arg4)) { X if (chld->arg_type == O_ITEM && hoistable[chld[1].arg_type]) { X arg[4].arg_type = chld[1].arg_type; X arg[4].arg_ptr = chld[1].arg_ptr; X arg[4].arg_len = chld[1].arg_len; X free_arg(chld); X } X else { X arg[4].arg_type = A_EXPR; X arg[4].arg_ptr.arg_arg = chld; X } X } X if (newlen >= 5 && (chld = arg5)) { X if (chld->arg_type == O_ITEM && hoistable[chld[1].arg_type]) { X arg[5].arg_type = chld[1].arg_type; X arg[5].arg_ptr = chld[1].arg_ptr; X arg[5].arg_len = chld[1].arg_len; X free_arg(chld); X } X else { X arg[5].arg_type = A_EXPR; X arg[5].arg_ptr.arg_arg = chld; X } X } X#ifdef DEBUGGING X if (debug & 16) { X fprintf(stderr,"%lx <= make_op(%s",arg,opname[arg->arg_type]); X if (arg1) X fprintf(stderr,",%s=%lx", X argname[arg[1].arg_type&A_MASK],arg[1].arg_ptr.arg_arg); X if (arg2) X fprintf(stderr,",%s=%lx", X argname[arg[2].arg_type&A_MASK],arg[2].arg_ptr.arg_arg); X if (arg3) X fprintf(stderr,",%s=%lx", X argname[arg[3].arg_type&A_MASK],arg[3].arg_ptr.arg_arg); X if (newlen >= 4) X fprintf(stderr,",%s=%lx", X argname[arg[4].arg_type&A_MASK],arg[4].arg_ptr.arg_arg); X if (newlen >= 5) X fprintf(stderr,",%s=%lx", X argname[arg[5].arg_type&A_MASK],arg[5].arg_ptr.arg_arg); X fprintf(stderr,")\n"); X } X#endif X evalstatic(arg); /* see if we can consolidate anything */ X return arg; X} X Xvoid Xevalstatic(arg) Xregister ARG *arg; X{ X register STR *str; X register STR *s1; X register STR *s2; X double value; /* must not be register */ X register char *tmps; X int i; X unsigned long tmplong; X long tmp2; X double exp(), log(), sqrt(), modf(); X char *crypt(); X double sin(), cos(), atan2(), pow(); X X if (!arg || !arg->arg_len) X return; X X if ((arg[1].arg_type == A_SINGLE || arg->arg_type == O_AELEM) && X (arg->arg_len == 1 || arg[2].arg_type == A_SINGLE) ) { X str = str_new(0); X s1 = arg[1].arg_ptr.arg_str; X if (arg->arg_len > 1) X s2 = arg[2].arg_ptr.arg_str; X else X s2 = Nullstr; X switch (arg->arg_type) { X case O_AELEM: X i = (int)str_gnum(s2); X if (i < 32767 && i >= 0) { X arg->arg_type = O_ITEM; X arg->arg_len = 1; X arg[1].arg_type = A_ARYSTAB; /* $abc[123] is hoistable now */ X arg[1].arg_len = i; X arg[1].arg_ptr = arg[1].arg_ptr; /* get stab pointer */ X str_free(s2); X } X /* FALL THROUGH */ X default: X str_free(str); X str = Nullstr; /* can't be evaluated yet */ X break; X case O_CONCAT: X str_sset(str,s1); X str_scat(str,s2); X break; X case O_REPEAT: X i = (int)str_gnum(s2); X while (i-- > 0) X str_scat(str,s1); X break; X case O_MULTIPLY: X value = str_gnum(s1); X str_numset(str,value * str_gnum(s2)); X break; X case O_DIVIDE: X value = str_gnum(s2); X if (value == 0.0) X yyerror("Illegal division by constant zero"); X else X str_numset(str,str_gnum(s1) / value); X break; X case O_MODULO: X tmplong = (long)str_gnum(s2); X if (tmplong == 0L) { X yyerror("Illegal modulus of constant zero"); X break; X } X tmp2 = (long)str_gnum(s1); X#ifndef lint X if (tmp2 >= 0) X str_numset(str,(double)(tmp2 % tmplong)); X else X str_numset(str,(double)(tmplong - (-tmp2 % tmplong))); X#else X tmp2 = tmp2; X#endif X break; X case O_ADD: X value = str_gnum(s1); X str_numset(str,value + str_gnum(s2)); X break; X case O_SUBTRACT: X value = str_gnum(s1); X str_numset(str,value - str_gnum(s2)); X break; X case O_LEFT_SHIFT: X value = str_gnum(s1); X i = (int)str_gnum(s2); X#ifndef lint X str_numset(str,(double)(((long)value) << i)); X#endif X break; X case O_RIGHT_SHIFT: X value = str_gnum(s1); X i = (int)str_gnum(s2); X#ifndef lint X str_numset(str,(double)(((long)value) >> i)); X#endif X break; X case O_LT: X value = str_gnum(s1); X str_numset(str,(value < str_gnum(s2)) ? 1.0 : 0.0); X break; X case O_GT: X value = str_gnum(s1); X str_numset(str,(value > str_gnum(s2)) ? 1.0 : 0.0); X break; X case O_LE: X value = str_gnum(s1); X str_numset(str,(value <= str_gnum(s2)) ? 1.0 : 0.0); X break; X case O_GE: X value = str_gnum(s1); X str_numset(str,(value >= str_gnum(s2)) ? 1.0 : 0.0); X break; X case O_EQ: X if (dowarn) { X if ((!s1->str_nok && !looks_like_number(s1)) || X (!s2->str_nok && !looks_like_number(s2)) ) X warn("Possible use of == on string value"); X } X value = str_gnum(s1); X str_numset(str,(value == str_gnum(s2)) ? 1.0 : 0.0); X break; X case O_NE: X value = str_gnum(s1); X str_numset(str,(value != str_gnum(s2)) ? 1.0 : 0.0); X break; X case O_BIT_AND: X value = str_gnum(s1); X#ifndef lint X str_numset(str,(double)(((long)value) & ((long)str_gnum(s2)))); X#endif X break; X case O_XOR: X value = str_gnum(s1); X#ifndef lint X str_numset(str,(double)(((long)value) ^ ((long)str_gnum(s2)))); X#endif X break; X case O_BIT_OR: X value = str_gnum(s1); X#ifndef lint X str_numset(str,(double)(((long)value) | ((long)str_gnum(s2)))); X#endif X break; X case O_AND: X if (str_true(s1)) X str_sset(str,s2); X else X str_sset(str,s1); X break; X case O_OR: X if (str_true(s1)) X str_sset(str,s1); X else X str_sset(str,s2); X break; X case O_COND_EXPR: X if ((arg[3].arg_type & A_MASK) != A_SINGLE) { X str_free(str); X str = Nullstr; X } X else { X if (str_true(s1)) X str_sset(str,s2); X else X str_sset(str,arg[3].arg_ptr.arg_str); X str_free(arg[3].arg_ptr.arg_str); X } X break; X case O_NEGATE: X str_numset(str,(double)(-str_gnum(s1))); X break; X case O_NOT: X str_numset(str,(double)(!str_true(s1))); X break; X case O_COMPLEMENT: X#ifndef lint X str_numset(str,(double)(~(long)str_gnum(s1))); X#endif X break; X case O_SIN: X str_numset(str,sin(str_gnum(s1))); X break; X case O_COS: X str_numset(str,cos(str_gnum(s1))); X break; X case O_ATAN2: X value = str_gnum(s1); X str_numset(str,atan2(value, str_gnum(s2))); X break; X case O_POW: X value = str_gnum(s1); X str_numset(str,pow(value, str_gnum(s2))); X break; X case O_LENGTH: X str_numset(str, (double)str_len(s1)); X break; X case O_SLT: X str_numset(str,(double)(str_cmp(s1,s2) < 0)); X break; X case O_SGT: X str_numset(str,(double)(str_cmp(s1,s2) > 0)); X break; X case O_SLE: X str_numset(str,(double)(str_cmp(s1,s2) <= 0)); X break; X case O_SGE: X str_numset(str,(double)(str_cmp(s1,s2) >= 0)); X break; X case O_SEQ: X str_numset(str,(double)(str_eq(s1,s2))); X break; X case O_SNE: X str_numset(str,(double)(!str_eq(s1,s2))); X break; X case O_CRYPT: X#ifdef CRYPT X tmps = str_get(s1); X str_set(str,crypt(tmps,str_get(s2))); X#else X yyerror( X "The crypt() function is unimplemented due to excessive paranoia."); X#endif X break; X case O_EXP: X str_numset(str,exp(str_gnum(s1))); X break; X case O_LOG: X str_numset(str,log(str_gnum(s1))); X break; X case O_SQRT: X str_numset(str,sqrt(str_gnum(s1))); X break; X case O_INT: X value = str_gnum(s1); X if (value >= 0.0) X (void)modf(value,&value); X else { X (void)modf(-value,&value); X value = -value; X } X str_numset(str,value); X break; X case O_ORD: X#ifndef I286 X str_numset(str,(double)(*str_get(s1))); X#else X { X int zapc; X char *zaps; X X zaps = str_get(s1); X zapc = (int) *zaps; X str_numset(str,(double)(zapc)); X } X#endif X break; X } X if (str) { X arg->arg_type = O_ITEM; /* note arg1 type is already SINGLE */ X str_free(s1); X str_free(s2); X arg[1].arg_ptr.arg_str = str; X } X } X} X XARG * Xl(arg) Xregister ARG *arg; X{ X register int i; X register ARG *arg1; X register ARG *arg2; X SPAT *spat; X int arghog = 0; X X i = arg[1].arg_type & A_MASK; X X arg->arg_flags |= AF_COMMON; /* assume something in common */ X /* which forces us to copy things */ X X if (i == A_ARYLEN) { X arg[1].arg_type = A_LARYLEN; X return arg; X } X if (i == A_ARYSTAB) { X arg[1].arg_type = A_LARYSTAB; X return arg; X } X X /* see if it's an array reference */ X X if (i == A_EXPR || i == A_LEXPR) { X arg1 = arg[1].arg_ptr.arg_arg; X X if (arg1->arg_type == O_LIST || arg1->arg_type == O_ITEM) { X /* assign to list */ X if (arg->arg_len > 1) { X dehoist(arg,2); X arg2 = arg[2].arg_ptr.arg_arg; X if (nothing_in_common(arg1,arg2)) X arg->arg_flags &= ~AF_COMMON; X if (arg->arg_type == O_ASSIGN) { X if (arg1->arg_flags & AF_LOCAL) X arg->arg_flags |= AF_LOCAL; X arg[1].arg_flags |= AF_ARYOK; X arg[2].arg_flags |= AF_ARYOK; X } X } X else if (arg->arg_type != O_CHOP) X arg->arg_type = O_ASSIGN; /* possible local(); */ X for (i = arg1->arg_len; i >= 1; i--) { X switch (arg1[i].arg_type) { X case A_STAR: case A_LSTAR: X arg1[i].arg_type = A_LSTAR; X break; X case A_STAB: case A_LVAL: X arg1[i].arg_type = A_LVAL; X break; X case A_ARYLEN: case A_LARYLEN: X arg1[i].arg_type = A_LARYLEN; X break; X case A_ARYSTAB: case A_LARYSTAB: X arg1[i].arg_type = A_LARYSTAB; X break; X case A_EXPR: case A_LEXPR: X arg1[i].arg_type = A_LEXPR; X switch(arg1[i].arg_ptr.arg_arg->arg_type) { X case O_ARRAY: case O_LARRAY: X arg1[i].arg_ptr.arg_arg->arg_type = O_LARRAY; X arghog = 1; X break; X case O_AELEM: case O_LAELEM: X arg1[i].arg_ptr.arg_arg->arg_type = O_LAELEM; X break; X case O_HASH: case O_LHASH: X arg1[i].arg_ptr.arg_arg->arg_type = O_LHASH; X arghog = 1; X break; X case O_HELEM: case O_LHELEM: X arg1[i].arg_ptr.arg_arg->arg_type = O_LHELEM; X break; X case O_ASLICE: case O_LASLICE: X arg1[i].arg_ptr.arg_arg->arg_type = O_LASLICE; X break; X case O_HSLICE: case O_LHSLICE: X arg1[i].arg_ptr.arg_arg->arg_type = O_LHSLICE; X break; X default: X goto ill_item; X } X break; X default: X ill_item: X (void)sprintf(tokenbuf, "Illegal item (%s) as lvalue", X argname[arg1[i].arg_type&A_MASK]); X yyerror(tokenbuf); X } X } X if (arg->arg_len > 1) { X if (arg2->arg_type == O_SPLIT && !arg2[3].arg_type && !arghog) { X arg2[3].arg_type = A_SINGLE; X arg2[3].arg_ptr.arg_str = X str_nmake((double)arg1->arg_len + 1); /* limit split len*/ X } X } X } X else if (arg1->arg_type == O_AELEM || arg1->arg_type == O_LAELEM) X arg1->arg_type = O_LAELEM; X else if (arg1->arg_type == O_ARRAY || arg1->arg_type == O_LARRAY) { X arg1->arg_type = O_LARRAY; X if (arg->arg_len > 1) { X dehoist(arg,2); X arg2 = arg[2].arg_ptr.arg_arg; X if (arg2->arg_type == O_SPLIT) { /* use split's builtin =?*/ X spat = arg2[2].arg_ptr.arg_spat; X if (nothing_in_common(arg1,spat->spat_repl)) { X spat->spat_repl[1].arg_ptr.arg_stab = X arg1[1].arg_ptr.arg_stab; X arg_free(arg1); /* recursive */ X free_arg(arg); /* non-recursive */ X return arg2; /* split has builtin assign */ X } X } X else if (nothing_in_common(arg1,arg2)) X arg->arg_flags &= ~AF_COMMON; X if (arg->arg_type == O_ASSIGN) { X arg[1].arg_flags |= AF_ARYOK; X arg[2].arg_flags |= AF_ARYOK; X } X } X } X else if (arg1->arg_type == O_HELEM || arg1->arg_type == O_LHELEM) X arg1->arg_type = O_LHELEM; X else if (arg1->arg_type == O_HASH || arg1->arg_type == O_LHASH) { X arg1->arg_type = O_LHASH; X if (arg->arg_len > 1) { X dehoist(arg,2); X arg2 = arg[2].arg_ptr.arg_arg; X if (nothing_in_common(arg1,arg2)) X arg->arg_flags &= ~AF_COMMON; X if (arg->arg_type == O_ASSIGN) { X arg[1].arg_flags |= AF_ARYOK; X arg[2].arg_flags |= AF_ARYOK; X } X } X } X else if (arg1->arg_type == O_ASLICE) { X arg1->arg_type = O_LASLICE; X if (arg->arg_type == O_ASSIGN) { X arg[1].arg_flags |= AF_ARYOK; X arg[2].arg_flags |= AF_ARYOK; X } X } X else if (arg1->arg_type == O_HSLICE) { X arg1->arg_type = O_LHSLICE; X if (arg->arg_type == O_ASSIGN) { X arg[1].arg_flags |= AF_ARYOK; X arg[2].arg_flags |= AF_ARYOK; X } X } X else if ((arg->arg_type == O_DEFINED || arg->arg_type == O_UNDEF) && X (arg1->arg_type == (perldb ? O_DBSUBR : O_SUBR)) ) { X arg[1].arg_type |= A_DONT; X } X else if (arg1->arg_type == O_SUBSTR || arg1->arg_type == O_VEC) { X (void)l(arg1); X Renewc(arg1->arg_ptr.arg_str, 1, struct lstring, STR); X /* grow string struct to hold an lstring struct */ X } X else if (arg1->arg_type != O_ASSIGN) { X (void)sprintf(tokenbuf, X "Illegal expression (%s) as lvalue",opname[arg1->arg_type]); X yyerror(tokenbuf); X } X arg[1].arg_type = A_LEXPR | (arg[1].arg_type & A_DONT); X if (arg->arg_type == O_ASSIGN && (arg1[1].arg_flags & AF_ARYOK)) { X arg[1].arg_flags |= AF_ARYOK; X if (arg->arg_len > 1) X arg[2].arg_flags |= AF_ARYOK; X } X#ifdef DEBUGGING X if (debug & 16) X fprintf(stderr,"lval LEXPR\n"); X#endif X return arg; X } X if (i == A_STAR || i == A_LSTAR) { X arg[1].arg_type = A_LSTAR | (arg[1].arg_type & A_DONT); X return arg; X } X X /* not an array reference, should be a register name */ X X if (i != A_STAB && i != A_LVAL) { X (void)sprintf(tokenbuf, X "Illegal item (%s) as lvalue",argname[arg[1].arg_type&A_MASK]); X yyerror(tokenbuf); X } X arg[1].arg_type = A_LVAL | (arg[1].arg_type & A_DONT); X#ifdef DEBUGGING X if (debug & 16) X fprintf(stderr,"lval LVAL\n"); X#endif X return arg; X} X XARG * Xfixl(type,arg) Xint type; XARG *arg; X{ X if (type == O_DEFINED || type == O_UNDEF) { X if (arg->arg_type != O_ITEM) X arg = hide_ary(arg); X if (arg->arg_type == O_ITEM) { X type = arg[1].arg_type & A_MASK; X if (type == A_EXPR || type == A_LEXPR) X arg[1].arg_type = A_LEXPR|A_DONT; X } X } X return arg; X} X Xdehoist(arg,i) XARG *arg; X{ X ARG *tmparg; X X if (arg[i].arg_type != A_EXPR) { /* dehoist */ X tmparg = make_op(O_ITEM,1,Nullarg,Nullarg,Nullarg,0); X tmparg[1] = arg[i]; X arg[i].arg_ptr.arg_arg = tmparg; X arg[i].arg_type = A_EXPR; X } X} X XARG * Xaddflags(i,flags,arg) Xregister ARG *arg; X{ X arg[i].arg_flags |= flags; X return arg; X} X XARG * Xhide_ary(arg) XARG *arg; X{ X if (arg->arg_type == O_ARRAY || arg->arg_type == O_HASH) X return make_op(O_ITEM,1,arg,Nullarg,Nullarg,0); X return arg; X} X X/* maybe do a join on multiple array dimensions */ X XARG * Xjmaybe(arg) Xregister ARG *arg; X{ X if (arg && arg->arg_type == O_COMMA) { X arg = listish(arg); X arg = make_op(O_JOIN, 2, X stab2arg(A_STAB,stabent(";",TRUE)), X make_list(arg), X Nullarg, 0); X } X return arg; X} X XARG * Xmake_list(arg) Xregister ARG *arg; X{ X register int i; X register ARG *node; X register ARG *nxtnode; X register int j; X STR *tmpstr; X X if (!arg) { X arg = op_new(0); X arg->arg_type = O_LIST; X } X if (arg->arg_type != O_COMMA) { X if (arg->arg_type != O_ARRAY) X arg->arg_flags |= AF_LISTISH; /* see listish() below */ X return arg; X } X for (i = 2, node = arg; ; i++) { X if (node->arg_len < 2) X break; X if (node[1].arg_type != A_EXPR) X break; X node = node[1].arg_ptr.arg_arg; X if (node->arg_type != O_COMMA) X break; X } X if (i > 2) { X node = arg; X arg = op_new(i); X tmpstr = arg->arg_ptr.arg_str; X#ifdef STRUCTCOPY X *arg = *node; /* copy everything except the STR */ X#else X (void)bcopy((char *)node, (char *)arg, sizeof(ARG)); X#endif X arg->arg_ptr.arg_str = tmpstr; X for (j = i; ; ) { X#ifdef STRUCTCOPY X arg[j] = node[2]; X#else X (void)bcopy((char *)(node+2), (char *)(arg+j), sizeof(ARG)); X#endif X arg[j].arg_flags |= AF_ARYOK; X --j; /* Bug in Xenix compiler */ X if (j < 2) { X#ifdef STRUCTCOPY X arg[1] = node[1]; X#else X (void)bcopy((char *)(node+1), (char *)(arg+1), sizeof(ARG)); X#endif X free_arg(node); X break; X } X nxtnode = node[1].arg_ptr.arg_arg; X free_arg(node); X node = nxtnode; X } X } X arg[1].arg_flags |= AF_ARYOK; X arg[2].arg_flags |= AF_ARYOK; X arg->arg_type = O_LIST; X arg->arg_len = i; X return arg; X} X X/* turn a single item into a list */ X XARG * Xlistish(arg) XARG *arg; X{ X if (arg->arg_flags & AF_LISTISH) X arg = make_op(O_LIST,1,arg,Nullarg,Nullarg,0); X return arg; X} X XARG * Xmaybelistish(optype, arg) Xint optype; XARG *arg; X{ X if (optype == O_PRTF || X (arg->arg_type == O_ASLICE || arg->arg_type == O_HSLICE || X arg->arg_type == O_F_OR_R) ) X arg = listish(arg); X return arg; X} X X/* mark list of local variables */ X XARG * Xlocalize(arg) XARG *arg; X{ X arg->arg_flags |= AF_LOCAL; X return arg; X} X XARG * Xfixeval(arg) XARG *arg; X{ X Renew(arg, 3, ARG); X arg->arg_len = 2; X arg[2].arg_ptr.arg_hash = curstash; X arg[2].arg_type = A_NULL; X return arg; X} X XARG * Xrcatmaybe(arg) XARG *arg; X{ X if (arg->arg_type == O_CONCAT && arg[2].arg_type == A_READ) { X arg->arg_type = O_RCAT; X arg[2].arg_type = arg[2].arg_ptr.arg_arg[1].arg_type; X arg[2].arg_ptr = arg[2].arg_ptr.arg_arg[1].arg_ptr; X free_arg(arg[2].arg_ptr.arg_arg); X } X return arg; X} X XARG * Xstab2arg(atype,stab) Xint atype; Xregister STAB *stab; X{ X register ARG *arg; X X arg = op_new(1); X arg->arg_type = O_ITEM; X arg[1].arg_type = atype; X arg[1].arg_ptr.arg_stab = stab; X return arg; X} X XARG * Xcval_to_arg(cval) Xregister char *cval; X{ X register ARG *arg; X X arg = op_new(1); X arg->arg_type = O_ITEM; X arg[1].arg_type = A_SINGLE; X arg[1].arg_ptr.arg_str = str_make(cval,0); X Safefree(cval); X return arg; X} X XARG * Xop_new(numargs) Xint numargs; X{ X register ARG *arg; X X Newz(203,arg, numargs + 1, ARG); X arg->arg_ptr.arg_str = str_new(0); X arg->arg_len = numargs; X return arg; X} X Xvoid Xfree_arg(arg) XARG *arg; X{ X str_free(arg->arg_ptr.arg_str); X Safefree(arg); X} X XARG * Xmake_match(type,expr,spat) Xint type; XARG *expr; XSPAT *spat; X{ X register ARG *arg; X X arg = make_op(type,2,expr,Nullarg,Nullarg,0); X X arg[2].arg_type = A_SPAT|A_DONT; X arg[2].arg_ptr.arg_spat = spat; X#ifdef DEBUGGING X if (debug & 16) X fprintf(stderr,"make_match SPAT=%lx\n",(long)spat); X#endif X X if (type == O_SUBST || type == O_NSUBST) { X if (arg[1].arg_type != A_STAB) { X yyerror("Illegal lvalue"); X } X arg[1].arg_type = A_LVAL; X } X return arg; X} X XARG * Xcmd_to_arg(cmd) XCMD *cmd; X{ X register ARG *arg; X X arg = op_new(1); X arg->arg_type = O_ITEM; X arg[1].arg_type = A_CMD; X arg[1].arg_ptr.arg_cmd = cmd; X return arg; X} X X/* Check two expressions to see if there is any identifier in common */ X Xstatic int Xnothing_in_common(arg1,arg2) XARG *arg1; XARG *arg2; X{ X static int thisexpr = 0; /* I don't care if this wraps */ X X thisexpr++; X if (arg_common(arg1,thisexpr,1)) X return 0; /* hit eval or do {} */ X if (arg_common(arg2,thisexpr,0)) X return 0; /* hit identifier again */ X return 1; X} X X/* Recursively descend an expression and mark any identifier or check X * it to see if it was marked already. X */ X Xstatic int Xarg_common(arg,exprnum,marking) Xregister ARG *arg; Xint exprnum; Xint marking; X{ X register int i; X X if (!arg) X return 0; X for (i = arg->arg_len; i >= 1; i--) { X switch (arg[i].arg_type & A_MASK) { X case A_NULL: X break; X case A_LEXPR: X case A_EXPR: X if (arg_common(arg[i].arg_ptr.arg_arg,exprnum,marking)) X return 1; X break; X case A_CMD: X return 1; /* assume hanky panky */ X case A_STAR: X case A_LSTAR: X case A_STAB: X case A_LVAL: X case A_ARYLEN: X case A_LARYLEN: X if (marking) X stab_lastexpr(arg[i].arg_ptr.arg_stab) = exprnum; X else if (stab_lastexpr(arg[i].arg_ptr.arg_stab) == exprnum) X return 1; X break; X case A_DOUBLE: X case A_BACKTICK: X { X register char *s = arg[i].arg_ptr.arg_str->str_ptr; X register char *send = s + arg[i].arg_ptr.arg_str->str_cur; X register STAB *stab; X X while (*s) { X if (*s == '$' && s[1]) { X s = scanreg(s,send,tokenbuf); X stab = stabent(tokenbuf,TRUE); X if (marking) X stab_lastexpr(stab) = exprnum; X else if (stab_lastexpr(stab) == exprnum) X return 1; X continue; X } X else if (*s == '\\' && s[1]) X s++; X s++; X } X } X break; X case A_SPAT: X if (spat_common(arg[i].arg_ptr.arg_spat,exprnum,marking)) X return 1; X break; X case A_READ: X case A_INDREAD: X case A_GLOB: X case A_WORD: X case A_SINGLE: X break; X } X } X switch (arg->arg_type) { X case O_ARRAY: X case O_LARRAY: X if ((arg[1].arg_type & A_MASK) == A_STAB) X (void)aadd(arg[1].arg_ptr.arg_stab); X break; X case O_HASH: X case O_LHASH: X if ((arg[1].arg_type & A_MASK) == A_STAB) X (void)hadd(arg[1].arg_ptr.arg_stab); X break; X case O_EVAL: X case O_SUBR: X case O_DBSUBR: X return 1; X } X return 0; X} X Xstatic int Xspat_common(spat,exprnum,marking) Xregister SPAT *spat; Xint exprnum; Xint marking; X{ X if (spat->spat_runtime) X if (arg_common(spat->spat_runtime,exprnum,marking)) X return 1; X if (spat->spat_repl) { X if (arg_common(spat->spat_repl,exprnum,marking)) X return 1; X } X return 0; X} !STUFFY!FUNK! echo Extracting x2p/s2p.SH sed >x2p/s2p.SH <<'!STUFFY!FUNK!' -e 's/X//' Xcase $CONFIG in X'') X if test ! -f config.sh; then X ln ../config.sh . || \ X ln ../../config.sh . || \ X ln ../../../config.sh . || \ X (echo "Can't find config.sh."; exit 1) X fi X . config.sh X ;; Xesac X: This forces SH files to create target in same directory as SH file. X: This is so that make depend always knows where to find SH derivatives. Xcase "$0" in X*/*) cd `expr X$0 : 'X\(.*\)/'` ;; Xesac Xecho "Extracting s2p (with variable substitutions)" X: This section of the file will have variable substitutions done on it. X: Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!. X: Protect any dollar signs and backticks that you do not want interpreted X: by putting a backslash in front. You may delete these comments. X$spitshell >s2p <>s2p <<'!NO!SUBS!' X X# $Header: s2p,v 2.0.1.1 88/07/11 23:26:23 root Exp $ X# X# $Log: s2p,v $ X# Revision 2.0.1.1 88/07/11 23:26:23 root X# patch2: s2p didn't put a proper prologue on output script X# X# Revision 2.0 88/06/05 00:15:55 root X# Baseline version 2.0. X# X# X X$indent = 4; X$shiftwidth = 4; X$l = '{'; $r = '}'; X$tempvar = '1'; X Xwhile ($ARGV[0] =~ '^-') { X $_ = shift; X last if /^--/; X if (/^-D/) { X $debug++; X open(body,'>-'); X next; X } X if (/^-n/) { X $assumen++; X next; X } X if (/^-p/) { X $assumep++; X next; X } X die "I don't recognize this switch: $_\n"; X} X Xunless ($debug) { X open(body,">/tmp/sperl$$") || do Die("Can't open temp file"); X} X Xif (!$assumen && !$assumep) { X print body X'while ($ARGV[0] =~ /^-/) { X $_ = shift; X last if /^--/; X if (/^-n/) { X $nflag++; X next; X } X die "I don\'t recognize this switch: $_\\n"; X} X X'; X} X Xprint body ' X#ifdef PRINTIT X#ifdef ASSUMEP X$printit++; X#else X$printit++ unless $nflag; X#endif X#endif Xline: while (<>) { X'; X Xline: while (<>) { X s/[ \t]*(.*)\n$/$1/; X if (/^:/) { X s/^:[ \t]*//; X $label = do make_label($_); X if ($. == 1) { X $toplabel = $label; X } X $_ = "$label:"; X if ($lastlinewaslabel++) {$_ .= "\t;";} X if ($indent >= 2) { X $indent -= 2; X $indmod = 2; X } X next; X } else { X $lastlinewaslabel = ''; X } X $addr1 = ''; X $addr2 = ''; X if (s/^([0-9]+)//) { X $addr1 = "$1"; X } X elsif (s/^\$//) { X $addr1 = 'eof()'; X } X elsif (s|^/||) { X $addr1 = do fetchpat('/'); X } X if (s/^,//) { X if (s/^([0-9]+)//) { X $addr2 = "$1"; X } elsif (s/^\$//) { X $addr2 = "eof()"; X } elsif (s|^/||) { X $addr2 = do fetchpat('/'); X } else { X do Die("Invalid second address at line $.\n"); X } X $addr1 .= " .. $addr2"; X } X # a { to keep vi happy X s/^[ \t]+//; X if ($_ eq '}') { X $indent -= 4; X next; X } X if (s/^!//) { X $if = 'unless'; X $else = "$r else $l\n"; X } else { X $if = 'if'; X $else = ''; X } X if (s/^{//) { # a } to keep vi happy X $indmod = 4; X $redo = $_; X $_ = ''; X $rmaybe = ''; X } else { X $rmaybe = "\n$r"; X if ($addr2 || $addr1) { X $space = ' ' x $shiftwidth; X } else { X $space = ''; X } X $_ = do transmogrify(); X } X X if ($addr1) { X if ($_ !~ /[\n{}]/ && $rmaybe && !$change && X $_ !~ / if / && $_ !~ / unless /) { X s/;$/ $if $addr1;/; X $_ = substr($_,$shiftwidth,1000); X } else { X $command = $_; X $_ = "$if ($addr1) $l\n$change$command$rmaybe"; X } X $change = ''; X next line; X } X} continue { X @lines = split(/\n/,$_); X while ($#lines >= 0) { X $_ = shift(lines); X unless (s/^ *<<--//) { X print body "\t" x ($indent / 8), ' ' x ($indent % 8); X } X print body $_, "\n"; X } X $indent += $indmod; X $indmod = 0; X if ($redo) { X $_ = $redo; X $redo = ''; X redo line; X } X} X Xprint body "}\n"; Xif ($appendseen || $tseen || !$assumen) { X $printit++ if $dseen || (!$assumen && !$assumep); X print body ' Xcontinue { X#ifdef PRINTIT X#ifdef DSEEN X#ifdef ASSUMEP X print if $printit++; X#else X if ($printit) { print;} else { $printit++ unless $nflag; } X#endif X#else X print if $printit; X#endif X#else X print; X#endif X#ifdef TSEEN X $tflag = \'\'; X#endif X#ifdef APPENDSEEN X if ($atext) { print $atext; $atext = \'\'; } X#endif X} X'; X} X Xclose body; X Xunless ($debug) { X open(head,">/tmp/sperl2$$.c") || do Die("Can't open temp file 2"); X print head "#define PRINTIT\n" if ($printit); X print head "#define APPENDSEEN\n" if ($appendseen); X print head "#define TSEEN\n" if ($tseen); X print head "#define DSEEN\n" if ($dseen); X print head "#define ASSUMEN\n" if ($assumen); X print head "#define ASSUMEP\n" if ($assumep); X if ($opens) {print head "$opens\n";} X open(body,"/tmp/sperl$$") || do Die("Can't reopen temp file"); X while (
) { X print head $_; X } X close head; X X print "#!$bin/perl Xeval \"exec $bin/perl -S \$0 \$*\" X if \$running_under_some_shell; X X"; X open(body,"cc -E /tmp/sperl2$$.c |") || X do Die("Can't reopen temp file"); X while () { X /^# [0-9]/ && next; X /^[ \t]*$/ && next; X s/^<><>//; X print; X } X} X Xunlink "/tmp/sperl$$", "/tmp/sperl2$$"; X Xsub Die { X unlink "/tmp/sperl$$", "/tmp/sperl2$$"; X die $_[0]; X} Xsub make_filehandle { X $fname = $_ = $_[0]; X s/[^a-zA-Z]/_/g; X s/^_*//; X if (/^([a-z])([a-z]*)$/) { X $first = $1; X $rest = $2; X $first =~ y/a-z/A-Z/; X $_ = $first . $rest; X } X if (!$seen{$_}) { X $opens .= "open($_,'>$fname') || die \"Can't create $fname\";\n"; X } X $seen{$_} = $_; X} X Xsub make_label { X $label = $_[0]; X $label =~ s/[^a-zA-Z0-9]/_/g; X if ($label =~ /^[0-9_]/) { $label = 'L' . $label; } X $label = substr($label,0,8); X if ($label =~ /^([a-z])([a-z]*)$/) { # could be reserved word X $first = $1; X $rest = $2; X $first =~ y/a-z/A-Z/; # so capitalize it X $label = $first . $rest; X } X $label; X} X Xsub transmogrify { X { # case X if (/^d/) { X $dseen++; X $_ = ' X<<--#ifdef PRINTIT X$printit = \'\'; X<<--#endif Xnext line;'; X next; X } X X if (/^n/) { X $_ = X'<<--#ifdef PRINTIT X<<--#ifdef DSEEN X<<--#ifdef ASSUMEP Xprint if $printit++; X<<--#else Xif ($printit) { print;} else { $printit++ unless $nflag; } X<<--#endif X<<--#else Xprint if $printit; X<<--#endif X<<--#else Xprint; X<<--#endif X<<--#ifdef APPENDSEEN Xif ($atext) {print $atext; $atext = \'\';} X<<--#endif X$_ = <>; X<<--#ifdef TSEEN X$tflag = \'\'; X<<--#endif'; X next; X } X X if (/^a/) { X $appendseen++; X $command = $space . '$atext .=' . "\n<<--'"; X $lastline = 0; X while (<>) { X s/^[ \t]*//; X s/^[\\]//; X unless (s|\\$||) { $lastline = 1;} X s/'/\\'/g; X s/^([ \t]*\n)/<><>$1/; X $command .= $_; X $command .= '<<--'; X last if $lastline; X } X $_ = $command . "';"; X last; X } X X if (/^[ic]/) { X if (/^c/) { $change = 1; } X $addr1 = '$iter = (' . $addr1 . ')'; X $command = $space . 'if ($iter == 1) { print' . "\n<<--'"; X $lastline = 0; X while (<>) { X s/^[ \t]*//; X s/^[\\]//; X unless (s/\\$//) { $lastline = 1;} X s/'/\\'/g; X s/^([ \t]*\n)/<><>$1/; X $command .= $_; X $command .= '<<--'; X last if $lastline; X } X $_ = $command . "';}"; X if ($change) { X $dseen++; X $change = "$_\n"; X $_ = " X<<--#ifdef PRINTIT X$space\$printit = ''; X<<--#endif X${space}next line;"; X } X last; X } X X if (/^s/) { X $delim = substr($_,1,1); X $len = length($_); X $repl = $end = 0; X $inbracket = 0; X for ($i = 2; $i < $len; $i++) { X $c = substr($_,$i,1); X if ($c eq $delim) { X if ($inbracket) { X $_ = substr($_,0,$i) . '\\' . substr($_,$i,10000); X $i++; X $len++; X } X else { X if ($repl) { X $end = $i; X last; X } else { X $repl = $i; X } X } X } X elsif ($c eq '\\') { X $i++; X if ($i >= $len) { X $_ .= 'n'; X $_ .= <>; X $len = length($_); X $_ = substr($_,0,--$len); X } X elsif (!$repl && substr($_,$i,1) =~ /^[(){}\w]$/) { X $i--; X $len--; X $_ = substr($_,0,$i) . substr($_,$i+1,10000); X } X } X elsif ($c eq '[' && !$repl) { X $i++ if substr($_,$i,1) eq '^'; X $i++ if substr($_,$i,1) eq ']'; X $inbracket = 1; X } X elsif ($c eq ']') { X $inbracket = 0; X } X elsif (!$repl && index("()",$c) >= 0) { X $_ = substr($_,0,$i) . '\\' . substr($_,$i,10000); X $i++; X $len++; X } X } X do Die("Malformed substitution at line $.\n") unless $end; X $pat = substr($_, 0, $repl + 1); X $repl = substr($_, $repl + 1, $end - $repl - 1); X $end = substr($_, $end + 1, 1000); X $dol = '$'; X $repl =~ s/\$/\\$/; X $repl =~ s'&'$&'g; X $repl =~ s/[\\]([0-9])/$dol$1/g; X $subst = "$pat$repl$delim"; X $cmd = ''; X while ($end) { X if ($end =~ s/^g//) { $subst .= 'g'; next; } X if ($end =~ s/^p//) { $cmd .= ' && (print)'; next; } X if ($end =~ s/^w[ \t]*//) { X $fh = do make_filehandle($end); X $cmd .= " && (print $fh \$_)"; X $end = ''; X next; X } X do Die("Unrecognized substitution command ($end) at line $.\n"); X } X $_ = X"<<--#ifdef TSEEN X$subst && \$tflag++$cmd; X<<--#else X$subst$cmd; X<<--#endif"; X next; X } X X if (/^p/) { X $_ = 'print;'; X next; X } X X if (/^w/) { X s/^w[ \t]*//; X $fh = do make_filehandle($_); X $_ = "print $fh \$_;"; X next; X } X X if (/^r/) { X $appendseen++; X s/^r[ \t]*//; X $file = $_; X $_ = "\$atext .= `cat $file 2>/dev/null`;"; X next; X } X X if (/^P/) { X $_ = 'print $1 if /(^.*\n)/;'; X next; X } X X if (/^D/) { X $_ = X's/^.*\n//; Xredo line if $_; Xnext line;'; X next; X } X X if (/^N/) { X $_ = ' X$_ .= <>; X<<--#ifdef TSEEN X$tflag = \'\'; X<<--#endif'; X next; X } X X if (/^h/) { X $_ = '$hold = $_;'; X next; X } X X if (/^H/) { X $_ = '$hold .= $_ ? $_ : "\n";'; X next; X } X X if (/^g/) { X $_ = '$_ = $hold;'; X next; X } X X if (/^G/) { X $_ = '$_ .= $hold ? $hold : "\n";'; X next; X } X X if (/^x/) { X $_ = '($_, $hold) = ($hold, $_);'; X next; X } X X if (/^b$/) { X $_ = 'next line;'; X next; X } X X if (/^b/) { X s/^b[ \t]*//; X $lab = do make_label($_); X if ($lab eq $toplabel) { X $_ = 'redo line;'; X } else { X $_ = "goto $lab;"; X } X next; X } X X if (/^t$/) { X $_ = 'next line if $tflag;'; X $tseen++; X next; X } X X if (/^t/) { X s/^t[ \t]*//; X $lab = do make_label($_); X if ($lab eq $toplabel) { X $_ = 'if ($tflag) {$tflag = \'\'; redo line;}'; X } else { X $_ = "if (\$tflag) {\$tflag = ''; goto $lab;}"; X } X $tseen++; X next; X } X X if (/^=/) { X $_ = 'print "$.\n";'; X next; X } X X if (/^q/) { X $_ = X'close(ARGV); X@ARGV = (); Xnext line;'; X next; X } X } continue { X if ($space) { X s/^/$space/; X s/(\n)(.)/$1$space$2/g; X } X last; X } X $_; X} X Xsub fetchpat { X local($outer) = @_; X local($addr) = $outer; X local($inbracket); X local($prefix,$delim,$ch); X X delim: while (s:^([^\](|)[\\/]*)([](|)[\\/])::) { X $prefix = $1; X $delim = $2; X print "$prefix\t$delim\t$_\n"; X if ($delim eq '\\') { X s/(.)//; X $ch = $1; X $delim = '' if $ch =~ /^[(){}\w]$/; X $delim .= $1; X } X elsif ($delim eq '[') { X $inbracket = 1; X s/^\^// && ($delim .= '^'); X s/^]// && ($delim .= ']'); X print "$prefix\t$delim\t$_\n"; X } X elsif ($delim eq ']') { X $inbracket = 0; X } X elsif ($inbracket || $delim ne $outer) { X print "Adding\n"; X $delim = '\\' . $delim; X } X $addr .= $prefix; X $addr .= $delim; X if ($delim eq $outer && !$inbracket) { X last delim; X } X } X $addr; X} X X!NO!SUBS! Xchmod 755 s2p X$eunicefix s2p !STUFFY!FUNK! echo Extracting malloc.c sed >malloc.c <<'!STUFFY!FUNK!' -e 's/X//' X/* $Header: malloc.c,v 2.0.1.1 88/10/31 16:29:42 lwall Locked $ X * X * $Log: malloc.c,v $ X */ X X#ifndef lint Xstatic char sccsid[] = "@(#)malloc.c 4.3 (Berkeley) 9/16/83"; X X#define RCHECK X/* X * malloc.c (Caltech) 2/21/82 X * Chris Kingsley, kingsley@cit-20. X * X * This is a very fast storage allocator. It allocates blocks of a small X * number of different sizes, and keeps free lists of each size. Blocks that X * don't exactly fit are passed up to the next larger size. In this X * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long. X * This is designed for use in a program that uses vast quantities of memory, X * but bombs when it runs out. X */ X X#include "EXTERN.h" X#include "perl.h" X X/* I don't much care whether these are defined in sys/types.h--LAW */ X X#define u_char unsigned char X#define u_int unsigned int X#define u_short unsigned short X X/* X * The overhead on a block is at least 4 bytes. When free, this space X * contains a pointer to the next free block, and the bottom two bits must X * be zero. When in use, the first byte is set to MAGIC, and the second X * byte is the size index. The remaining bytes are for alignment. X * If range checking is enabled and the size of the block fits X * in two bytes, then the top two bytes hold the size of the requested block X * plus the range checking words, and the header word MINUS ONE. X */ Xunion overhead { X union overhead *ov_next; /* when free */ X struct { X u_char ovu_magic; /* magic number */ X u_char ovu_index; /* bucket # */ X#ifdef RCHECK X u_short ovu_size; /* actual block size */ X u_int ovu_rmagic; /* range magic number */ X#endif X } ovu; X#define ov_magic ovu.ovu_magic X#define ov_index ovu.ovu_index X#define ov_size ovu.ovu_size X#define ov_rmagic ovu.ovu_rmagic X}; X X#define MAGIC 0xff /* magic # on accounting info */ X#define OLDMAGIC 0x7f /* same after a free() */ X#define RMAGIC 0x55555555 /* magic # on range info */ X#ifdef RCHECK X#define RSLOP sizeof (u_int) X#else X#define RSLOP 0 X#endif X X/* X * nextf[i] is the pointer to the next free block of size 2^(i+3). The X * smallest allocatable block is 8 bytes. The overhead information X * precedes the data area returned to the user. X */ X#define NBUCKETS 30 Xstatic union overhead *nextf[NBUCKETS]; Xextern char *sbrk(); X X#ifdef MSTATS X/* X * nmalloc[i] is the difference between the number of mallocs and frees X * for a given block size. X */ Xstatic u_int nmalloc[NBUCKETS]; X#include