Exemple #1
0
void
chsign(Blk *p)
{
	int carry;
	char ct;

	carry=0;
	rewind(p);
	while(sfeof(p) == 0) {
		ct=100-slookc(p)-carry;
		carry=1;
		if(ct>=100) {
			ct -= 100;
			carry=0;
		}
		salterc(p,ct);
	}
	if(carry != 0) {
		sputc(p,-1);
		fsfile(p);
		backc(p);
		ct = sbackc(p);
		if(ct == 99 /*&& !sfbeg(p)*/) {
			truncate(p);
			sputc(p,-1);
		}
	} else{
		fsfile(p);
		ct = sbackc(p);
		if(ct == 0)
			truncate(p);
	}
	return;
}
Exemple #2
0
Blk*
dcsqrt(Blk *p)
{
	Blk *t, *r, *q, *s;
	int c, n, nn;

	n = length(p);
	fsfile(p);
	c = sbackc(p);
	if((n&1) != 1)
		c = c*100+(sfbeg(p)?0:sbackc(p));
	n = (n+1)>>1;
	r = salloc(n);
	zero(r);
	seekc(r,n);
	nn=1;
	while((c -= nn)>=0)
		nn+=2;
	c=(nn+1)>>1;
	fsfile(r);
	backc(r);
	if(c>=100) {
		c -= 100;
		salterc(r,c);
		sputc(r,1);
	} else
		salterc(r,c);
	for(;;){
		q = div(p,r);
		s = add(q,r);
		release(q);
		release(rem);
		q = div(s,sqtemp);
		release(s);
		release(rem);
		s = copy(r,length(r));
		chsign(s);
		t = add(s,q);
		release(s);
		fsfile(t);
		nn = sfbeg(t)?0:sbackc(t);
		if(nn>=0)
			break;
		release(r);
		release(t);
		r = q;
	}
	release(t);
	release(q);
	release(p);
	return(r);
}
void spincube()

{   theta+=0.5+speed;
	 if(theta==360.0)
	theta-=360.0;
	if(theta>=90.0)
	{
		rotationcomplete=1;
	glutIdleFunc(NULL);

if(rotation==1&&inverse==0)
	{
	topc();
}
if(rotation==1&&inverse==1)
	{
	topc();
	topc();
	topc();
}

if(rotation==2&&inverse==0)
	{
   rightc();

   }
if(rotation==2&&inverse==1)
	{
rightc();
rightc();
rightc();

}
if(rotation==3&&inverse==0)
	{
	frontc();
}
if(rotation==3&&inverse==1)
	{
	frontc();
	frontc();
	frontc();
}
if(rotation==4&&inverse==0)
{
	leftc();
}
if(rotation==4&&inverse==1)
{
	leftc();
	leftc();
	leftc();

}
if(rotation==5&&inverse==0)
{
	backc();
}
if(rotation==5&&inverse==1)
{
	backc();
	backc();
	backc();
}
if(rotation==6&&inverse==0)
{
	bottomc();
}
if(rotation==6&&inverse==1)
{
	bottomc();
	bottomc();
	bottomc();

}

rotation=0;
theta=0;
}



glutPostRedisplay();
}
Exemple #4
0
Blk*
div(Blk *ddivd, Blk *ddivr)
{
	int divsign, remsign, offset, divcarry,
		carry, dig, magic, d, dd, under, first;
	long c, td, cc;
	Blk *ps, *px, *p, *divd, *divr;

	dig = 0;
	under = 0;
	divcarry = 0;
	rem = 0;
	p = salloc(0);
	if(length(ddivr) == 0) {
		pushp(ddivr);
		Bprint(&bout,"divide by 0\n");
		return(p);
	}
	divsign = remsign = first = 0;
	divr = ddivr;
	fsfile(divr);
	if(sbackc(divr) == -1) {
		divr = copy(ddivr,length(ddivr));
		chsign(divr);
		divsign = ~divsign;
	}
	divd = copy(ddivd,length(ddivd));
	fsfile(divd);
	if(sfbeg(divd) == 0 && sbackc(divd) == -1) {
		chsign(divd);
		divsign = ~divsign;
		remsign = ~remsign;
	}
	offset = length(divd) - length(divr);
	if(offset < 0)
		goto ddone;
	seekc(p,offset+1);
	sputc(divd,0);
	magic = 0;
	fsfile(divr);
	c = sbackc(divr);
	if(c < 10)
		magic++;
	c = c * 100 + (sfbeg(divr)?0:sbackc(divr));
	if(magic>0){
		c = (c * 100 +(sfbeg(divr)?0:sbackc(divr)))*2;
		c /= 25;
	}
	while(offset >= 0) {
		first++;
		fsfile(divd);
		td = sbackc(divd) * 100;
		dd = sfbeg(divd)?0:sbackc(divd);
		td = (td + dd) * 100;
		dd = sfbeg(divd)?0:sbackc(divd);
		td = td + dd;
		cc = c;
		if(offset == 0)
			td++;
		else
			cc++;
		if(magic != 0)
			td = td<<3;
		dig = td/cc;
		under=0;
		if(td%cc < 8  && dig > 0 && magic) {
			dig--;
			under=1;
		}
		rewind(divr);
		rewind(divxyz);
		carry = 0;
		while(sfeof(divr) == 0) {
			d = sgetc(divr)*dig+carry;
			carry = d / 100;
			salterc(divxyz,d%100);
		}
		salterc(divxyz,carry);
		rewind(divxyz);
		seekc(divd,offset);
		carry = 0;
		while(sfeof(divd) == 0) {
			d = slookc(divd);
			d = d-(sfeof(divxyz)?0:sgetc(divxyz))-carry;
			carry = 0;
			if(d < 0) {
				d += 100;
				carry = 1;
			}
			salterc(divd,d);
		}
		divcarry = carry;
		backc(p);
		salterc(p,dig);
		backc(p);
		fsfile(divd);
		d=sbackc(divd);
		if((d != 0) && /*!divcarry*/ (offset != 0)) {
			d = sbackc(divd) + 100;
			salterc(divd,d);
		}
		if(--offset >= 0)
			divd->wt--;
	}
	if(under) {	/* undershot last - adjust*/
		px = copy(divr,length(divr));	/*11/88 don't corrupt ddivr*/
		chsign(px);
		ps = add(px,divd);
		fsfile(ps);
		if(length(ps) > 0 && sbackc(ps) < 0) {
			release(ps);	/*only adjust in really undershot*/
		} else {
			release(divd);
			salterc(p, dig+1);
			divd=ps;
		}
	}
	if(divcarry != 0) {
		salterc(p,dig-1);
		salterc(divd,-1);
		ps = add(divr,divd);
		release(divd);
		divd = ps;
	}

	rewind(p);
	divcarry = 0;
	while(sfeof(p) == 0){
		d = slookc(p)+divcarry;
		divcarry = 0;
		if(d >= 100){
			d -= 100;
			divcarry = 1;
		}
		salterc(p,d);
	}
	if(divcarry != 0)salterc(p,divcarry);
	fsfile(p);
	while(sfbeg(p) == 0) {
		if(sbackc(p) != 0)
			break;
		truncate(p);
	}
	if(divsign < 0)
		chsign(p);
	fsfile(divd);
	while(sfbeg(divd) == 0) {
		if(sbackc(divd) != 0)
			break;
		truncate(divd);
	}
ddone:
	if(remsign<0)
		chsign(divd);
	if(divr != ddivr)
		release(divr);
	rem = divd;
	return(p);
}
Exemple #5
0
void
commnds(void)
{
	Blk *p, *q, **ptr, *s, *t;
	long l;
	Sym *sp;
	int sk, sk1, sk2, c, sign, n, d;

	while(1) {
		Bflush(&bout);
		if(((c = readc())>='0' && c <= '9') ||
		    (c>='A' && c <='F') || c == '.') {
			unreadc(c);
			p = readin();
			pushp(p);
			continue;
		}
		switch(c) {
		case ' ':
		case '\t':
		case '\n':
		case -1:
			continue;
		case 'Y':
			sdump("stk",*stkptr);
			Bprint(&bout, "all %ld rel %ld headmor %ld\n",all,rel,headmor);
			Bprint(&bout, "nbytes %ld\n",nbytes);
			Bprint(&bout, "longest %ld active %ld maxsize %ld\n", longest,
				active, maxsize);
			Bprint(&bout, "new all %d rel %d copy %d more %d lbytes %d\n",
				lall, lrel, lcopy, lmore, lbytes);
			lall = lrel = lcopy = lmore = lbytes = 0;
			continue;
		case '_':
			p = readin();
			savk = sunputc(p);
			chsign(p);
			sputc(p,savk);
			pushp(p);
			continue;
		case '-':
			subt();
			continue;
		case '+':
			if(eqk() != 0)
				continue;
			binop('+');
			continue;
		case '*':
			arg1 = pop();
			EMPTY;
			arg2 = pop();
			EMPTYR(arg1);
			sk1 = sunputc(arg1);
			sk2 = sunputc(arg2);
			savk = sk1+sk2;
			binop('*');
			p = pop();
			if(savk>k && savk>sk1 && savk>sk2) {
				sclobber(p);
				sk = sk1;
				if(sk<sk2)
					sk = sk2;
				if(sk<k)
					sk = k;
				p = removc(p,savk-sk);
				savk = sk;
				sputc(p,savk);
			}
			pushp(p);
			continue;
		case '/':
		casediv:
			if(dscale() != 0)
				continue;
			binop('/');
			if(irem != 0)
				release(irem);
			release(rem);
			continue;
		case '%':
			if(dscale() != 0)
				continue;
			binop('/');
			p = pop();
			release(p);
			if(irem == 0) {
				sputc(rem,skr+k);
				pushp(rem);
				continue;
			}
			p = add0(rem,skd-(skr+k));
			q = add(p,irem);
			release(p);
			release(irem);
			sputc(q,skd);
			pushp(q);
			continue;
		case 'v':
			p = pop();
			EMPTY;
			savk = sunputc(p);
			if(length(p) == 0) {
				sputc(p,savk);
				pushp(p);
				continue;
			}
			if(sbackc(p)<0) {
				error("sqrt of neg number\n");
			}
			if(k<savk)
				n = savk;
			else {
				n = k*2-savk;
				savk = k;
			}
			arg1 = add0(p,n);
			arg2 = dcsqrt(arg1);
			sputc(arg2,savk);
			pushp(arg2);
			continue;

		case '^':
			neg = 0;
			arg1 = pop();
			EMPTY;
			if(sunputc(arg1) != 0)
				error("exp not an integer\n");
			arg2 = pop();
			EMPTYR(arg1);
			if(sfbeg(arg1) == 0 && sbackc(arg1)<0) {
				neg++;
				chsign(arg1);
			}
			if(length(arg1)>=3) {
				error("exp too big\n");
			}
			savk = sunputc(arg2);
			p = dcexp(arg2,arg1);
			release(arg2);
			rewind(arg1);
			c = sgetc(arg1);
			if(c == -1)
				c = 0;
			else
			if(sfeof(arg1) == 0)
				c = sgetc(arg1)*100 + c;
			d = c*savk;
			release(arg1);
		/*	if(neg == 0) {		removed to fix -exp bug*/
				if(k>=savk)
					n = k;
				else
					n = savk;
				if(n<d) {
					q = removc(p,d-n);
					sputc(q,n);
					pushp(q);
				} else {
					sputc(p,d);
					pushp(p);
				}
		/*	} else { this is disaster for exp <-127 */
		/*		sputc(p,d);		*/
		/*		pushp(p);		*/
		/*	}				*/
			if(neg == 0)
				continue;
			p = pop();
			q = salloc(2);
			sputc(q,1);
			sputc(q,0);
			pushp(q);
			pushp(p);
			goto casediv;
		case 'z':
			p = salloc(2);
			n = stkptr - stkbeg;
			if(n >= 100) {
				sputc(p,n/100);
				n %= 100;
			}
			sputc(p,n);
			sputc(p,0);
			pushp(p);
			continue;
		case 'Z':
			p = pop();
			EMPTY;
			n = (length(p)-1)<<1;
			fsfile(p);
			backc(p);
			if(sfbeg(p) == 0) {
				if((c = sbackc(p))<0) {
					n -= 2;
					if(sfbeg(p) == 1)
						n++;
					else {
						if((c = sbackc(p)) == 0)
							n++;
						else
						if(c > 90)
							n--;
					}
				} else
				if(c < 10)
					n--;
			}
			release(p);
			q = salloc(1);
			if(n >= 100) {
				sputc(q,n%100);
				n /= 100;
			}
			sputc(q,n);
			sputc(q,0);
			pushp(q);
			continue;
		case 'i':
			p = pop();
			EMPTY;
			p = scalint(p);
			release(inbas);
			inbas = p;
			continue;
		case 'I':
			p = copy(inbas,length(inbas)+1);
			sputc(p,0);
			pushp(p);
			continue;
		case 'o':
			p = pop();
			EMPTY;
			p = scalint(p);
			sign = 0;
			n = length(p);
			q = copy(p,n);
			fsfile(q);
			l = c = sbackc(q);
			if(n != 1) {
				if(c<0) {
					sign = 1;
					chsign(q);
					n = length(q);
					fsfile(q);
					l = c = sbackc(q);
				}
				if(n != 1) {
					while(sfbeg(q) == 0)
						l = l*100+sbackc(q);
				}
			}
			logo = log2(l);
			obase = l;
			release(basptr);
			if(sign == 1)
				obase = -l;
			basptr = p;
			outdit = bigot;
			if(n == 1 && sign == 0) {
				if(c <= 16) {
					outdit = hexot;
					fw = 1;
					fw1 = 0;
					ll = 70;
					release(q);
					continue;
				}
			}
			n = 0;
			if(sign == 1)
				n++;
			p = salloc(1);
			sputc(p,-1);
			t = add(p,q);
			n += length(t)*2;
			fsfile(t);
			if(sbackc(t)>9)
				n++;
			release(t);
			release(q);
			release(p);
			fw = n;
			fw1 = n-1;
			ll = 70;
			if(fw>=ll)
				continue;
			ll = (70/fw)*fw;
			continue;
		case 'O':
			p = copy(basptr,length(basptr)+1);
			sputc(p,0);
			pushp(p);
			continue;
		case '[':
			n = 0;
			p = salloc(0);
			for(;;) {
				if((c = readc()) == ']') {
					if(n == 0)
						break;
					n--;
				}
				sputc(p,c);
				if(c == '[')
					n++;
			}
			pushp(p);
			continue;
		case 'k':
			p = pop();
			EMPTY;
			p = scalint(p);
			if(length(p)>1) {
				error("scale too big\n");
			}
			rewind(p);
			k = 0;
			if(!sfeof(p))
				k = sgetc(p);
			release(scalptr);
			scalptr = p;
			continue;
		case 'K':
			p = copy(scalptr,length(scalptr)+1);
			sputc(p,0);
			pushp(p);
			continue;
		case 'X':
			p = pop();
			EMPTY;
			fsfile(p);
			n = sbackc(p);
			release(p);
			p = salloc(2);
			sputc(p,n);
			sputc(p,0);
			pushp(p);
			continue;
		case 'Q':
			p = pop();
			EMPTY;
			if(length(p)>2) {
				error("Q?\n");
			}
			rewind(p);
			if((c =  sgetc(p))<0) {
				error("neg Q\n");
			}
			release(p);
			while(c-- > 0) {
				if(readptr == &readstk[0]) {
					error("readstk?\n");
				}
				if(*readptr != 0)
					release(*readptr);
				readptr--;
			}
			continue;
		case 'q':
			if(readptr <= &readstk[1])
				exits(0);
			if(*readptr != 0)
				release(*readptr);
			readptr--;
			if(*readptr != 0)
				release(*readptr);
			readptr--;
			continue;
		case 'f':
			if(stkptr == &stack[0])
				Bprint(&bout,"empty stack\n");
			else {
				for(ptr = stkptr; ptr > &stack[0];) {
					dcprint(*ptr--);
				}
			}
			continue;
		case 'p':
			if(stkptr == &stack[0])
				Bprint(&bout,"empty stack\n");
			else {
				dcprint(*stkptr);
			}
			continue;
		case 'P':
			p = pop();
			EMPTY;
			sputc(p,0);
			Bprint(&bout,"%s",p->beg);
			release(p);
			continue;
		case 'd':
			if(stkptr == &stack[0]) {
				Bprint(&bout,"empty stack\n");
				continue;
			}
			q = *stkptr;
			n = length(q);
			p = copy(*stkptr,n);
			pushp(p);
			continue;
		case 'c':
			while(stkerr == 0) {
				p = pop();
				if(stkerr == 0)
					release(p);
			}
			continue;
		case 'S':
			if(stkptr == &stack[0]) {
				error("save: args\n");
			}
			c = getstk() & 0377;
			sptr = stable[c];
			sp = stable[c] = sfree;
			sfree = sfree->next;
			if(sfree == 0)
				goto sempty;
			sp->next = sptr;
			p = pop();
			EMPTY;
			if(c >= ARRAYST) {
				q = copy(p,length(p)+PTRSZ);
				for(n = 0;n < PTRSZ;n++) {
					sputc(q,0);
				}
				release(p);
				p = q;
			}
			sp->val = p;
			continue;
		sempty:
			error("symbol table overflow\n");
		case 's':
			if(stkptr == &stack[0]) {
				error("save:args\n");
			}
			c = getstk() & 0377;
			sptr = stable[c];
			if(sptr != 0) {
				p = sptr->val;
				if(c >= ARRAYST) {
					rewind(p);
					while(sfeof(p) == 0)
						release(dcgetwd(p));
				}
				release(p);
			} else {
				sptr = stable[c] = sfree;
				sfree = sfree->next;
				if(sfree == 0)
					goto sempty;
				sptr->next = 0;
			}
			p = pop();
			sptr->val = p;
			continue;
		case 'l':
			load();
			continue;
		case 'L':
			c = getstk() & 0377;
			sptr = stable[c];
			if(sptr == 0) {
				error("L?\n");
			}
			stable[c] = sptr->next;
			sptr->next = sfree;
			sfree = sptr;
			p = sptr->val;
			if(c >= ARRAYST) {
				rewind(p);
				while(sfeof(p) == 0) {
					q = dcgetwd(p);
					if(q != 0)
						release(q);
				}
			}
			pushp(p);
			continue;
		case ':':
			p = pop();
			EMPTY;
			q = scalint(p);
			fsfile(q);
			c = 0;
			if((sfbeg(q) == 0) && ((c = sbackc(q))<0)) {
				error("neg index\n");
			}
			if(length(q)>2) {
				error("index too big\n");
			}
			if(sfbeg(q) == 0)
				c = c*100+sbackc(q);
			if(c >= MAXIND) {
				error("index too big\n");
			}
			release(q);
			n = getstk() & 0377;
			sptr = stable[n];
			if(sptr == 0) {
				sptr = stable[n] = sfree;
				sfree = sfree->next;
				if(sfree == 0)
					goto sempty;
				sptr->next = 0;
				p = salloc((c+PTRSZ)*PTRSZ);
				zero(p);
			} else {
				p = sptr->val;
				if(length(p)-PTRSZ < c*PTRSZ) {
					q = copy(p,(c+PTRSZ)*PTRSZ);
					release(p);
					p = q;
				}
			}
			seekc(p,c*PTRSZ);
			q = lookwd(p);
			if(q!=0)
				release(q);
			s = pop();
			EMPTY;
			salterwd(p, s);
			sptr->val = p;
			continue;
		case ';':
			p = pop();
			EMPTY;
			q = scalint(p);
			fsfile(q);
			c = 0;
			if((sfbeg(q) == 0) && ((c = sbackc(q))<0)) {
				error("neg index\n");
			}
			if(length(q)>2) {
				error("index too big\n");
			}
			if(sfbeg(q) == 0)
				c = c*100+sbackc(q);
			if(c >= MAXIND) {
				error("index too big\n");
			}
			release(q);
			n = getstk() & 0377;
			sptr = stable[n];
			if(sptr != 0){
				p = sptr->val;
				if(length(p)-PTRSZ >= c*PTRSZ) {
					seekc(p,c*PTRSZ);
					s = dcgetwd(p);
					if(s != 0) {
						q = copy(s,length(s));
						pushp(q);
						continue;
					}
				}
			}
			q = salloc(1);	/*so uninitialized array elt prints as 0*/
			sputc(q, 0);
			pushp(q);
			continue;
		case 'x':
		execute:
			p = pop();
			EMPTY;
			if((readptr != &readstk[0]) && (*readptr != 0)) {
				if((*readptr)->rd == (*readptr)->wt)
					release(*readptr);
				else {
					if(readptr++ == &readstk[RDSKSZ]) {
						error("nesting depth\n");
					}
				}
			} else
				readptr++;
			*readptr = p;
			if(p != 0)
				rewind(p);
			else {
				if((c = readc()) != '\n')
					unreadc(c);
			}
			continue;
		case '?':
			if(++readptr == &readstk[RDSKSZ]) {
				error("nesting depth\n");
			}
			*readptr = 0;
			fsave = curfile;
			curfile = &bin;
			while((c = readc()) == '!')
				command();
			p = salloc(0);
			sputc(p,c);
			while((c = readc()) != '\n') {
				sputc(p,c);
				if(c == '\\')
					sputc(p,readc());
			}
			curfile = fsave;
			*readptr = p;
			continue;
		case '!':
			if(command() == 1)
				goto execute;
			continue;
		case '<':
		case '>':
		case '=':
			if(cond(c) == 1)
				goto execute;
			continue;
		default:
			Bprint(&bout,"%o is unimplemented\n",c);
		}
	}
}
Exemple #6
0
/*
 * expanded search comparison
 * returns 1 if match, 0 if not
 */
static int
srch_cmp(void)
{
    int tq;					/* q-reg name for ^EGq */
    struct qp tqp;				/* pointer to read q reg */

    /* what is search character */
    switch (mapch_l[sm.p->ch[sm.c & 0xFF] & 0xFF]) {

    /* match anything but following construct */
    case CTL('N'):
        /* don't read past end of string */
        if (sm.dot >= sm.z) {
            ERROR(E_ISS);
        }

        /* skip the ^N */
        fwdc(&sm);
        return(!srch_cmp());

    /* match any character */
    case CTL('X'):
        return(1);

    /* take next char literally */
    case CTL('Q'):
    case CTL('R'):

        /* don't read past end of string */
        if (sm.dot >= sm.z) {
            ERROR(E_ISS);
        }

        /* skip the ^Q */
        fwdc(&sm);
        return(*(pmap + sb.p->ch[sb.c]) ==
                *(pmap + sm.p->ch[sm.c]));

    /* match any nonalphanumeric */
    case CTL('S'):
        return(!isalnum(sb.p->ch[sb.c]));

    case CTL('E'):
        /* don't read past end of string */
        if (sm.dot >= sm.z)
                ERROR(E_ISS);

        /* skip the ^E */
        fwdc(&sm);

        switch (mapch_l[sm.p->ch[sm.c & 0xFF] & 0xFF]) {

        /* match any alpha */
        case 'a':
            return(isalpha(sb.p->ch[sb.c]));

        /* match any nonalpha */
        case 'b':
            return(!isalnum(sb.p->ch[sb.c]));

        /* rad50 symbol constituent */
        case 'c':
            return(!isalnum(sb.p->ch[sb.c]) ||
                    (sb.p->ch[sb.c] == '$') ||
                    (sb.p->ch[sb.c] == '.'));

        /* digit */
        case 'd':
            return(isdigit(sb.p->ch[sb.c]));

        /* line terminator LF, VT, FF, CR */
        case 'l':
            return((sb.p->ch[sb.c] == LF) ||
                    (sb.p->ch[sb.c] == FF) ||
                    (sb.p->ch[sb.c] == CR) ||
                    (sb.p->ch[sb.c] == VT));

        /* alphanumeric */
        case 'r':
            return(isalnum(sb.p->ch[sb.c]));

        /* lower case */
        case 'v':
            return(islower(sb.p->ch[sb.c]));

        /* upper case */
        case 'w':
            return(isupper(sb.p->ch[sb.c]));

        /* any non-null string of spaces or tabs */
        case 's':
            /* failure */
            if (((sb.p->ch[sb.c]&0177) != ' ') &&
                    ((sb.p->ch[sb.c]&0177) != TAB)) {
                return(0);
            }

            /* skip remaining spaces or tabs */
            for ( fwdc(&sb);
                    ((sb.p->ch[sb.c]&0177) == ' ') ||
                    ((sb.p->ch[sb.c]&0177) == TAB);
                    fwdc(&sb) ) {
               ;
            }

            /*
             * Back up one char (calling routine will
             * skip it)
             */
            backc(&sb);
            return(1);		/* success */

        /* any char in specified q register */
        case 'g':
            /* don't read past end of string */
            if (sm.dot >= sm.z) {
                ERROR(E_ISS);
            }

            /* get to the next char */
            fwdc(&sm);

            /* read q-reg spec */
            tq = getqspec(1, sm.p->ch[sm.c]);

            for (tqp.dot = tqp.c = 0, tqp.p = qreg[tq].f;
                    tqp.dot < qreg[tq].z;
                    fwdc(&tqp)) {

                /* match */
                if (*(pmap + tqp.p->ch[tqp.c]) ==
                        *(pmap + sb.p->ch[sb.c])) {
                    return(1);
                }

            }
            return(0);		/* fail */

        default:
                ERROR(E_ISS);
        }
    default:
        return(*(pmap + sb.p->ch[sb.c]) == *(pmap + sm.p->ch[sm.c]));
    }
}
Exemple #7
0
/*
 * routine to do search operation: called with search count as argument
 * returns -1 (pass) or 0 (fail)
 */
int
do_search(int count)
{
    /* set approp. mapping table */
    pmap = (ctrl_x) ? &mapch[0] : &mapch_l[0];

    /* copy # of chars in search buffer */
    sm.z = sbuf.z;

    if (count > 0) {

        /* loop to count occurrences */
        for (sm.dot = 0; count > 0; count--) {

             /* loop to advance search pointer */
            for (; aa.dot < aa.z; aa.dot++) {
                sb.p = aa.p; sb.c = aa.c; sb.dot = aa.dot;
                sm.p = sbuf.f; sm.dot = sm.c = 0;

                /* for each char in search string */
                for ( ; (sb.dot < z) && (sm.dot < sm.z);
                        sm.dot++, sb.dot++) {

                    /* if search string char is "special" */
                    if (spec_chars[sm.p->ch[sm.c & 0xFF] & 0xFF] & A_A) {

                        /*
                         * Then use expanded comparison
                         * routine
                         */
                        if (!srch_cmp()) {
                            break;
                        }
                    } else if (*(pmap + sb.p->ch[sb.c]) !=
                               *(pmap + sm.p->ch[sm.c])) {
                        break;
                    }

                    /* else just compare */

                    /* advance search-string ptr */
                    if (++sm.c > CELLSIZE-1) {
                        sm.p = sm.p->f;
                        sm.c = 0;
                    }

                    /* advance buffer ptr */
                    if (++sb.c > CELLSIZE-1) {
                        sb.p = sb.p->f;
                        sb.c = 0;
                    }
                }

                /* exit if found */
                if (sm.dot >= sm.z) {
                    break;
                }

                /* else not found: advance buffer pointer */
                if (++aa.c > CELLSIZE-1) {
                    aa.p = aa.p->f;
                    aa.c = 0;
                }
            }

            /* if one search failed, don't do more */
            if (sm.dot < sm.z) {
                    break;
            } else {

                /* otherwise save -length of string found */
                ctrl_s = aa.dot - sb.dot;

                /* if funny "advance by 1" mode */
                if ((ed_val & ED_SMULT) && (count > 1)) {

                    /* advance buffer pointer by one only */
                    ++aa.dot;
                    if (++aa.c > CELLSIZE-1) {
                        aa.p = aa.p->f;
                        aa.c = 0;
                    }
                } else {

                    /* advance search pointer past string */
                    aa.dot = sb.dot;
                    aa.p = sb.p;
                    aa.c = sb.c;
                }
            }
        }

    } else {
        /* search backwards */

        /* loop to count occurrences */
        for (last_z = z, sm.dot = 0; count < 0; count++) {

            /* loop to advance (backwards) search pointer */
            for (; aa.dot >= locb; aa.dot--) {
                sb.p = aa.p; sb.c = aa.c; sb.dot = aa.dot;
                sm.p = sbuf.f; sm.dot = sm.c = 0;

                /* loop to compare string */
                for ( ; (sb.dot < last_z) && (sm.dot < sm.z);
                            sm.dot++, sb.dot++) {

                    /* if search string char is "special" */
                    if (spec_chars[sm.p->ch[sm.c & 0xFF] & 0xFF] & A_A) {

                        /*
                         * Then use expanded comparison
                         * routine
                         */
                        if (!srch_cmp()) {
                            break;
                        }
                    } else if (*(pmap + sb.p->ch[sb.c])
                            != *(pmap + sm.p->ch[sm.c])) {

                        /* else just compare */
                        break;
                    }

                    /* advance search-string ptr */
                    if (++sm.c > CELLSIZE-1) {
                        sm.p = sm.p->f;
                        sm.c = 0;
                    }

                    /* advance buffer ptr */
                    if (++sb.c > CELLSIZE-1) {
                        sb.p = sb.p->f;
                        sb.c = 0;
                    }
                }

                /* search matches: */
                if (sm.dot >= sm.z) {

                    /*
                     * Set last_z to point where this
                     * string was found
                     */
                    if (!(ed_val & ED_SMULT)) {
                        last_z = aa.dot;
                    }
                    break;
                }

                /* or if string is beyond end of buffer */
                if (sb.dot >= last_z) {

                    /*
                     * Make search appear to have
                     * succeeded
                     */
                    sm.dot = sm.z;

                    /*
                     * So as to back up pointer, and
                     * force one more look
                     */
                    --count;
                    break;
                }

                /* else advance buffer pointer (backwards) */
                if (--aa.c < 0) {
                    aa.p = aa.p->b;
                    aa.c = CELLSIZE-1;
                }
            }

            /* if one search failed, don't do more */
            if (sm.dot < sm.z) {
                    break;
            } else {

                /*
                 * If this is not last search, back
                 * pointer up one
                 */
                if (count < -1) {
                    backc(&aa);
                } else {

                    /*
                     * Otherwise save -length of string
                     * found
                     */
                    ctrl_s = aa.dot - sb.dot;

                    /* advance pointer past string */
                    aa.dot = sb.dot;
                    aa.p = sb.p;
                    aa.c = sb.c;
                }
            }
        }
    }

    /* if search succeeded, update pointer	*/
    if (sm.dot >= sm.z) {
        dot = aa.dot;
    }

    /* set "search occurred" (for ES) */
    search_flag = 1;

    /* and return -1 (pass) or 0 (fail) */
    return((sm.dot >= sm.z) ? -1 : 0);
}
Exemple #8
0
static int
do_append(LWCHAR ch, char *rep, off_t pos)
{
	int a;
	LWCHAR prev_ch;

	a = AT_NORMAL;

	if (ch == '\b') {
		if (bs_mode == BS_CONTROL)
			goto do_control_char;

		/*
		 * A better test is needed here so we don't
		 * backspace over part of the printed
		 * representation of a binary character.
		 */
		if (curr <= lmargin ||
		    column <= lmargin ||
		    (attr[curr - 1] & (AT_ANSI|AT_BINARY))) {
			STORE_PRCHAR('\b', pos);
		} else if (bs_mode == BS_NORMAL) {
			STORE_CHAR(ch, AT_NORMAL, NULL, pos);
		} else if (bs_mode == BS_SPECIAL) {
			overstrike = backc();
		}

		return (0);
	}

	if (overstrike > 0) {
		/*
		 * Overstrike the character at the current position
		 * in the line buffer.  This will cause either
		 * underline (if a "_" is overstruck),
		 * bold (if an identical character is overstruck),
		 * or just deletion of the character in the buffer.
		 */
		overstrike = utf_mode ? -1 : 0;
		/* To be correct, this must be a base character.  */
		prev_ch = get_wchar(linebuf + curr);
		a = attr[curr];
		if (ch == prev_ch) {
			/*
			 * Overstriking a char with itself means make it bold.
			 * But overstriking an underscore with itself is
			 * ambiguous.  It could mean make it bold, or
			 * it could mean make it underlined.
			 * Use the previous overstrike to resolve it.
			 */
			if (ch == '_') {
				if ((a & (AT_BOLD|AT_UNDERLINE)) != AT_NORMAL)
					a |= (AT_BOLD|AT_UNDERLINE);
				else if (last_overstrike != AT_NORMAL)
					a |= last_overstrike;
				else
					a |= AT_BOLD;
			} else {
				a |= AT_BOLD;
			}
		} else if (ch == '_') {
			a |= AT_UNDERLINE;
			ch = prev_ch;
			rep = linebuf + curr;
		} else if (prev_ch == '_') {
			a |= AT_UNDERLINE;
		}
		/* Else we replace prev_ch, but we keep its attributes.  */
	} else if (overstrike < 0) {
		if (is_composing_char(ch) ||
		    is_combining_char(get_wchar(linebuf + curr), ch))
			/* Continuation of the same overstrike.  */
			a = last_overstrike;
		else
			overstrike = 0;
	}

	if (ch == '\t') {
		/*
		 * Expand a tab into spaces.
		 */
		switch (bs_mode) {
		case BS_CONTROL:
			goto do_control_char;
		case BS_NORMAL:
		case BS_SPECIAL:
			STORE_TAB(a, pos);
			break;
		}
	} else if ((!utf_mode || is_ascii_char(ch)) && control_char((char)ch)) {
do_control_char:
		if (ctldisp == OPT_ON ||
		    (ctldisp == OPT_ONPLUS && IS_CSI_START(ch))) {
			/*
			 * Output as a normal character.
			 */
			STORE_CHAR(ch, AT_NORMAL, rep, pos);
		} else {
			STORE_PRCHAR((char)ch, pos);
		}
	} else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch)) {
		char *s;

		s = prutfchar(ch);

		if (column + (int)strlen(s) - 1 +
		    pwidth(' ', binattr, 0) + attr_ewidth(binattr) > sc_width)
			return (1);

		for (; *s != 0; s++)
			STORE_CHAR(*s, AT_BINARY, NULL, pos);
	} else {
		STORE_CHAR(ch, a, rep, pos);
	}
	return (0);
}