perl 3.0 beta kit [13/23]
Larry Wall
lwall at jato.Jpl.Nasa.Gov
Mon Sep 4 05:00:08 AEST 1989
#! /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 <<!GROK!THIS!
X#!$bin/perl
X
X\$bin = '$bin';
X!GROK!THIS!
X
X: In the following dollars and backticks do not need the extra backslash.
X$spitshell >>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 (<body>) {
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 (<body>) {
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 at 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 at 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 <stdio.h>
X#endif
X
X#ifdef debug
X#define ASSERT(p) if (!(p)) botch("p"); else
Xstatic
Xbotch(s)
X char *s;
X{
X
X printf("assertion botched: %s\n", s);
X abort();
X}
X#else
X#define ASSERT(p)
X#endif
X
Xchar *
Xmalloc(nbytes)
X register unsigned nbytes;
X{
X register union overhead *p;
X register int bucket = 0;
X register unsigned shiftr;
X
X /*
X * Convert amount of memory requested into
X * closest block size stored in hash buckets
X * which satisfies request. Account for
X * space used per block for accounting.
X */
X nbytes += sizeof (union overhead) + RSLOP;
X nbytes = (nbytes + 3) &~ 3;
X shiftr = (nbytes - 1) >> 2;
X /* apart from this loop, this is O(1) */
X while (shiftr >>= 1)
X bucket++;
X /*
X * If nothing in hash bucket right now,
X * request more memory from the system.
X */
X if (nextf[bucket] == NULL)
X morecore(bucket);
X if ((p = (union overhead *)nextf[bucket]) == NULL)
X return (NULL);
X /* remove from linked list */
X if (*((int*)p) > 0x10000000)
X#ifndef I286
X fprintf(stderr,"Corrupt malloc ptr 0x%x at 0x%x\n",*((int*)p),p);
X#else
X fprintf(stderr,"Corrupt malloc ptr 0x%lx at 0x%lx\n",*((int*)p),p);
X#endif
X nextf[bucket] = nextf[bucket]->ov_next;
X p->ov_magic = MAGIC;
X p->ov_index= bucket;
X#ifdef MSTATS
X nmalloc[bucket]++;
X#endif
X#ifdef RCHECK
X /*
X * Record allocated size of block and
X * bound space with magic numbers.
X */
X if (nbytes <= 0x10000)
X p->ov_size = nbytes - 1;
X p->ov_rmagic = RMAGIC;
X *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
X#endif
X return ((char *)(p + 1));
X}
X
X/*
X * Allocate more memory to the indicated bucket.
X */
Xstatic
Xmorecore(bucket)
X register int bucket;
X{
X register union overhead *op;
X register int rnu; /* 2^rnu bytes will be requested */
X register int nblks; /* become nblks blocks of the desired size */
X register int siz;
X
X if (nextf[bucket])
X return;
X /*
X * Insure memory is allocated
X * on a page boundary. Should
X * make getpageize call?
X */
X op = (union overhead *)sbrk(0);
X#ifndef I286
X if ((int)op & 0x3ff)
X (void)sbrk(1024 - ((int)op & 0x3ff));
X#else
X /* The sbrk(0) call on the I286 always returns the next segment */
X#endif
X
X#ifndef I286
X /* take 2k unless the block is bigger than that */
X rnu = (bucket <= 8) ? 11 : bucket + 3;
X#else
X /* take 16k unless the block is bigger than that
X (80286s like large segments!) */
X rnu = (bucket <= 11) ? 14 : bucket + 3;
X#endif
X nblks = 1 << (rnu - (bucket + 3)); /* how many blocks to get */
X if (rnu < bucket)
X rnu = bucket;
X op = (union overhead *)sbrk(1 << rnu);
X /* no more room! */
X if ((int)op == -1)
X return;
X /*
X * Round up to minimum allocation size boundary
X * and deduct from block count to reflect.
X */
X#ifndef I286
X if ((int)op & 7) {
X op = (union overhead *)(((int)op + 8) &~ 7);
X nblks--;
X }
X#else
X /* Again, this should always be ok on an 80286 */
X#endif
X /*
X * Add new memory allocated to that on
X * free list for this hash bucket.
X */
X nextf[bucket] = op;
X siz = 1 << (bucket + 3);
X while (--nblks > 0) {
X op->ov_next = (union overhead *)((caddr_t)op + siz);
X op = (union overhead *)((caddr_t)op + siz);
X }
X}
X
Xfree(cp)
X char *cp;
X{
X register int size;
X register union overhead *op;
X
X if (cp == NULL)
X return;
X op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
X#ifdef debug
X ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
X#else
X if (op->ov_magic != MAGIC) {
X fprintf(stderr,"%s free() ignored\n",
X op->ov_magic == OLDMAGIC ? "Duplicate" : "Bad");
X return; /* sanity */
X }
X op->ov_magic = OLDMAGIC;
X#endif
X#ifdef RCHECK
X ASSERT(op->ov_rmagic == RMAGIC);
X if (op->ov_index <= 13)
X ASSERT(*(u_int *)((caddr_t)op + op->ov_size + 1 - RSLOP) == RMAGIC);
X#endif
X ASSERT(op->ov_index < NBUCKETS);
X size = op->ov_index;
X op->ov_next = nextf[size];
X nextf[size] = op;
X#ifdef MSTATS
X nmalloc[size]--;
X#endif
X}
X
X/*
X * When a program attempts "storage compaction" as mentioned in the
X * old malloc man page, it realloc's an already freed block. Usually
X * this is the last block it freed; occasionally it might be farther
X * back. We have to search all the free lists for the block in order
X * to determine its bucket: 1st we make one pass thru the lists
X * checking only the first block in each; if that fails we search
X * ``reall_srchlen'' blocks in each list for a match (the variable
X * is extern so the caller can modify it). If that fails we just copy
X * however many bytes was given to realloc() and hope it's not huge.
X */
Xint reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
X
Xchar *
Xrealloc(cp, nbytes)
X char *cp;
X unsigned nbytes;
X{
X register u_int onb;
X union overhead *op;
X char *res;
X register int i;
X int was_alloced = 0;
X
X if (cp == NULL)
X return (malloc(nbytes));
X op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
X if (op->ov_magic == MAGIC) {
X was_alloced++;
X i = op->ov_index;
X } else {
X /*
X * Already free, doing "compaction".
X *
X * Search for the old block of memory on the
X * free list. First, check the most common
X * case (last element free'd), then (this failing)
X * the last ``reall_srchlen'' items free'd.
X * If all lookups fail, then assume the size of
X * the memory block being realloc'd is the
X * smallest possible.
X */
X if ((i = findbucket(op, 1)) < 0 &&
X (i = findbucket(op, reall_srchlen)) < 0)
X i = 0;
X }
X onb = (1 << (i + 3)) - sizeof (*op) - RSLOP;
X /* avoid the copy if same size block */
X if (was_alloced &&
X nbytes <= onb && nbytes > (onb >> 1) - sizeof(*op) - RSLOP)
X return(cp);
X if ((res = malloc(nbytes)) == NULL)
X return (NULL);
X if (cp != res) /* common optimization */
X (void)bcopy(cp, res, (int)((nbytes < onb) ? nbytes : onb));
X if (was_alloced)
X free(cp);
X return (res);
X}
X
X/*
X * Search ``srchlen'' elements of each free list for a block whose
X * header starts at ``freep''. If srchlen is -1 search the whole list.
X * Return bucket number, or -1 if not found.
X */
Xstatic
Xfindbucket(freep, srchlen)
X union overhead *freep;
X int srchlen;
X{
X register union overhead *p;
X register int i, j;
X
X for (i = 0; i < NBUCKETS; i++) {
X j = 0;
X for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
X if (p == freep)
X return (i);
X j++;
X }
X }
X return (-1);
X}
X
X#ifdef MSTATS
X/*
X * mstats - print out statistics about malloc
X *
X * Prints two lines of numbers, one showing the length of the free list
X * for each size category, the second showing the number of mallocs -
X * frees for each size category.
X */
Xmstats(s)
X char *s;
X{
X register int i, j;
X register union overhead *p;
X int totfree = 0,
X totused = 0;
X
X fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
X for (i = 0; i < NBUCKETS; i++) {
X for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
X ;
X fprintf(stderr, " %d", j);
X totfree += j * (1 << (i + 3));
X }
X fprintf(stderr, "\nused:\t");
X for (i = 0; i < NBUCKETS; i++) {
X fprintf(stderr, " %d", nmalloc[i]);
X totused += nmalloc[i] * (1 << (i + 3));
X }
X fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
X totused, totfree);
X}
X#endif
X#endif /* lint */
!STUFFY!FUNK!
echo ""
echo "End of kit 13 (of 23)"
cat /dev/null >kit13isdone
run=''
config=''
for iskit in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23; do
if test -f kit${iskit}isdone; then
run="$run $iskit"
else
todo="$todo $iskit"
fi
done
case $todo in
'')
echo "You have run all your kits. Please read README and then type Configure."
chmod 755 Configure
;;
*) echo "You have run$run."
echo "You still need to run$todo."
;;
esac
: Someone might mail this, so...
exit
More information about the Alt.sources
mailing list