xref: /csrg-svn/old/adb/adb.tahoe/format.c (revision 31706)
126418Ssam #ifndef lint
2*31706Sbostic static	char sccsid[] = "@(#)format.c	1.4 (Berkeley) 4/1/87";
326418Ssam #endif
426418Ssam /*
526418Ssam  *
626418Ssam  *	UNIX debugger
726418Ssam  *
826418Ssam  */
926418Ssam 
1026418Ssam #include "defs.h"
1126418Ssam 
1226418Ssam MSG		BADMOD;
1326418Ssam MSG		NOFORK;
1426418Ssam MSG		ADWRAP;
1526418Ssam 
1626418Ssam INT		mkfault;
1726418Ssam CHAR		*lp;
1826418Ssam ADDR		maxoff;
1926418Ssam SIG		sigint;
2026418Ssam SIG		sigqit;
2126418Ssam STRING		errflg;
2226418Ssam CHAR		lastc,peekc;
2326418Ssam L_INT		dot;
2426418Ssam INT		dotinc;
2526418Ssam L_INT		expv;
2626418Ssam L_INT		var[];
2726418Ssam 
scanform(icount,ifp,itype,ptype)2826418Ssam scanform(icount,ifp,itype,ptype)
2926418Ssam L_INT		icount;
3026418Ssam STRING		ifp;
3126418Ssam {
3226418Ssam 	REG	STRING		fp;
3326418Ssam 	CHAR		modifier;
3426418Ssam 	REG		fcount, init=1;
3526418Ssam 	L_INT		savdot;
3626418Ssam 	BOOL exact;
3726418Ssam 
3826418Ssam 	WHILE icount
3926418Ssam 	DO  fp=ifp;
4026418Ssam 	    savdot=dot; init=0;
4126418Ssam 
4226418Ssam 	    IF init==0 ANDF (exact=(findsym(dot,ptype)==0)) ANDF maxoff
4326418Ssam 	    THEN printf("\n%s:%16t",cursym->n_un.n_name);
4426418Ssam 	    FI
4526418Ssam 
4626418Ssam 	    /*now loop over format*/
4726418Ssam 	    WHILE *fp ANDF errflg==0
4826418Ssam 	    DO  IF isdigit(modifier = *fp)
4926418Ssam 		THEN fcount = 0;
5026418Ssam 			WHILE isdigit(modifier = *fp++)
5126418Ssam 			DO fcount *= 10;
5226418Ssam 			   fcount += modifier-'0';
5326418Ssam 			OD
5426418Ssam 			fp--;
5526418Ssam 		ELSE fcount = 1;
5626418Ssam 		FI
5726418Ssam 
5826418Ssam 		IF *fp==0 THEN break; FI
59*31706Sbostic 		fp=exform(fcount,fp,itype,ptype);
6026418Ssam 	    OD
6126418Ssam 	    dotinc=dot-savdot;
6226418Ssam 	    dot=savdot;
6326418Ssam 
6426418Ssam 	    IF errflg
6526418Ssam 	    THEN IF icount<0
6626418Ssam 		 THEN errflg=0; break;
6726418Ssam 		 ELSE error(errflg);
6826418Ssam 		 FI
6926418Ssam 	    FI
7026418Ssam 	    IF --icount
7126418Ssam 	    THEN dot=inkdot(dotinc);
7226418Ssam 	    FI
7326418Ssam 	    IF mkfault THEN error(0); FI
7426418Ssam 	OD
7526418Ssam }
7626418Ssam 
7726418Ssam STRING
exform(fcount,ifp,itype,ptype)7826418Ssam exform(fcount,ifp,itype,ptype)
7926418Ssam INT		fcount;
8026418Ssam STRING		ifp;
8126418Ssam {
8226418Ssam 	/* execute single format item `fcount' times
8326418Ssam 	 * sets `dotinc' and moves `dot'
8426418Ssam 	 * returns address of next format item
8526418Ssam 	 */
8626418Ssam 	REG	POS	w;
8726418Ssam 	REG	L_INT	savdot, wx;
8826418Ssam 	REG	STRING		fp;
8926418Ssam 	CHAR		c, modifier, longpr;
9026418Ssam 	union{	/* compatible with both VAX and TAHOE */
9126418Ssam 		L_REAL	d;
9226418Ssam 		INT	s[4];
9326418Ssam 	}fw;
9426418Ssam 
9526418Ssam 	WHILE fcount>0
9626418Ssam 	DO	fp = ifp; c = *fp;
9726418Ssam 		longpr=(c>='A')&&(c<='Z')||(c=='f')||(c=='4')||(c=='p');
9826418Ssam 		IF itype==NSP ORF *fp=='a'
9926418Ssam 		THEN wx=dot; w=dot;
10030131Ssam 		     IF c=='b' ORF c=='B' ORF c=='c' ORF c=='C' ORF c=='1'
10130131Ssam 		     THEN w=btol(wx); FI
10226418Ssam 		ELSE wx=get(dot,itype);
10326418Ssam 		     w=shorten(wx);
10426418Ssam 		FI
10526418Ssam 		IF errflg THEN return(fp); FI
10626418Ssam 		IF mkfault THEN error(0); FI
10726418Ssam 		var[0]=wx;
10826418Ssam 		modifier = *fp++;
10926418Ssam 		dotinc=(longpr?4:2);
11026418Ssam 
11126418Ssam 		IF charpos()==0 ANDF modifier!='a' THEN printf("%16m"); FI
11226418Ssam 
11326418Ssam 		switch(modifier) {
11426418Ssam 
11526418Ssam 		    case SP: case TB:
11626418Ssam 			break;
11726418Ssam 
11826418Ssam 		    case 't': case 'T':
11926418Ssam 			printf("%T",fcount); return(fp);
12026418Ssam 
12126418Ssam 		    case 'r': case 'R':
12226418Ssam 			printf("%M",fcount); return(fp);
12326418Ssam 
12426418Ssam 		    case 'a':
12526418Ssam 			psymoff(dot,ptype,":%16t"); dotinc=0; break;
12626418Ssam 
12726418Ssam 		    case 'p':
12826418Ssam 			psymoff(var[0],ptype,"%16t"); break;
12926418Ssam 
13026418Ssam 		    case 'u':
13126418Ssam 			printf("%-8u",w); break;
13226418Ssam 
13326418Ssam 		    case 'U':
13426418Ssam 			printf("%-16U",wx); break;
13526418Ssam 
13626418Ssam 		    case 'c': case 'C':
13726418Ssam 			IF modifier=='C'
13830739Sbostic 			THEN printesc((w>>8)&0xff);
13930739Sbostic 			ELSE printc((w>>8)&0xff);
14026418Ssam 			FI
14126418Ssam 			dotinc=1; break;
14226418Ssam 
14326418Ssam 		    case 'b': case 'B':
14430739Sbostic 			printf("%-8o", (w>>8)&0xff); dotinc=1; break;
14526418Ssam 
14626418Ssam 			case '1':
14726418Ssam 			printf("%-8R", byte(wx)); dotinc=1; break;
14826418Ssam 
14926418Ssam 			case '2':
15026418Ssam 		    case 'w':
15126418Ssam 			printf("%-8R", w); break;
15226418Ssam 
15326418Ssam 			case '4':
15426418Ssam 		    case 'W':
15526418Ssam 			printf("%-16R", wx); break;
15626418Ssam 
15726418Ssam 		    case 's': case 'S':
15826418Ssam 			savdot=dot; dotinc=1;
15926418Ssam 			WHILE (c=byte(get(dot,itype))) ANDF errflg==0
16026418Ssam 			DO dot=inkdot(1);
16126418Ssam 			   IF modifier == 'S'
16226418Ssam 			   THEN printesc(c);
16326418Ssam 			   ELSE printc(c);
16426418Ssam 			   FI
16526418Ssam 			   endline();
16626418Ssam 			OD
16726418Ssam 			dotinc=dot-savdot+1; dot=savdot; break;
16826418Ssam 
16926418Ssam 		    case 'x':
17026418Ssam 			printf("%-8x",w); break;
17126418Ssam 
17226418Ssam 		    case 'X':
17326418Ssam 			printf("%-16X", wx); break;
17426418Ssam 
17526418Ssam 		    case 'z':
17626418Ssam 			printf("%-8z",w); break;
17726418Ssam 
17826418Ssam 		    case 'Z':
17926418Ssam 			printf("%-16Z", wx); break;
18026418Ssam 
18126418Ssam 		    case 'Y':
18226418Ssam 			printf("%-24Y", wx); break;
18326418Ssam 
18426418Ssam 		    case 'q':
18526418Ssam 			printf("%-8q", w); break;
18626418Ssam 
18726418Ssam 		    case 'Q':
18826418Ssam 			printf("%-16Q", wx); break;
18926418Ssam 
19026418Ssam 		    case 'o':
19126418Ssam 			printf("%-8o", w); break;
19226418Ssam 
19326418Ssam 		    case 'O':
19426418Ssam 			printf("%-16O", wx); break;
19526418Ssam 
19626418Ssam 		    case 'i':
19726418Ssam 		    case 'I':
19826418Ssam 			printins(itype,wx); printc(EOR); break;
19926418Ssam 
20026418Ssam 		    case 'd':
20126418Ssam 			printf("%-8d", w); break;
20226418Ssam 
20326418Ssam 		    case 'D':
20426418Ssam 			printf("%-16D", wx); break;
20526418Ssam 
20626418Ssam 		    case 'f':
20730720Sbostic 			if ((w & ~0xFFFF00FF) == 0x8000)
20830720Sbostic 				printf("(reserved oprnd)");
20930720Sbostic 			else {
21030720Sbostic 				fw.d = 0;
21130720Sbostic 				fw.s[0] = w;
21230720Sbostic 				fw.s[1] = wx&0xffff;
21330720Sbostic 				printf("%-16.9f", fw.d);
21430720Sbostic 			}
21530720Sbostic 			dotinc = 4;
21630720Sbostic 			break;
21726418Ssam 
21826418Ssam 		    case 'F':	/* may be done with one get call on TAHOE */
21930720Sbostic 			if ((w & ~0xFFFF00FF) == 0x8000)
22030720Sbostic 				printf("(reserved oprnd)");
22130720Sbostic 			else {
22230720Sbostic 				fw.s[2] = shorten(get(inkdot(4),itype));
22330720Sbostic 				fw.s[3] = shorten(get(inkdot(6),itype));
22430720Sbostic 				if (errflg)
22530720Sbostic 					return(fp);
22630720Sbostic 				fw.s[0] = w;
22730720Sbostic 				fw.s[1] = wx&0xffff;
22830720Sbostic 				printf("%-32.18F", fw.d);
22930720Sbostic 			}
23030720Sbostic 			dotinc = 8;
23130720Sbostic 			break;
23226418Ssam 
23326418Ssam 		    case 'n': case 'N':
23426418Ssam 			printc('\n'); dotinc=0; break;
23526418Ssam 
23626418Ssam 		    case '"':
23726418Ssam 			dotinc=0;
23826418Ssam 			WHILE *fp != '"' ANDF *fp
23926418Ssam 			DO printc(*fp++); OD
24026418Ssam 			IF *fp THEN fp++; FI
24126418Ssam 			break;
24226418Ssam 
24326418Ssam 		    case '^':
24426418Ssam 			dot=inkdot(-dotinc*fcount); return(fp);
24526418Ssam 
24626418Ssam 		    case '+':
24726418Ssam 			dot=inkdot(fcount); return(fp);
24826418Ssam 
24926418Ssam 		    case '-':
25026418Ssam 			dot=inkdot(-fcount); return(fp);
25126418Ssam 
25226418Ssam 		    default: error(BADMOD);
25326418Ssam 		}
25426418Ssam 		IF itype!=NSP
25526418Ssam 		THEN	dot=inkdot(dotinc);
25626418Ssam 		FI
25726418Ssam 		fcount--; endline();
25826418Ssam 	OD
25926418Ssam 
26026418Ssam 	return(fp);
26126418Ssam }
26226418Ssam 
shell()26326418Ssam shell()
26426418Ssam {
26526418Ssam #ifndef EDDT
26626418Ssam 	REG	rc, unixpid;
26726418Ssam 	int	status;
26826418Ssam 	REG STRING	argp = lp;
26926418Ssam 	STRING	getenv(), shell = getenv("SHELL");
27026418Ssam #ifdef VFORK
27126418Ssam 	char		oldstlp;
27226418Ssam #endif
27326418Ssam 
27426418Ssam 	if (shell == 0)
27526418Ssam 		shell = "/bin/sh";
27626418Ssam 	WHILE lastc!=EOR DO rdc(); OD
27726418Ssam #ifndef VFORK
27826418Ssam 	IF (unixpid=fork())==0
27926418Ssam #else
28026418Ssam 	oldstlp = *lp;
28126418Ssam 	IF (unixpid=vfork())==0
28226418Ssam #endif
28326418Ssam 	THEN	signal(SIGINT,sigint); signal(SIGQUIT,sigqit);
28426418Ssam 		*lp=0; execl(shell, "sh", "-c", argp, 0);
28526418Ssam 		_exit(16);
28626418Ssam #ifndef VFORK
28726418Ssam 	ELIF unixpid == -1
28826418Ssam #else
28926418Ssam 	ELIF *lp = oldstlp, unixpid == -1
29026418Ssam #endif
29126418Ssam 	THEN	error(NOFORK);
29226418Ssam 	ELSE	signal(SIGINT,1);
29326418Ssam 		WHILE (rc = wait(&status)) != unixpid ANDF rc != -1 DONE
29426418Ssam 		signal(SIGINT,sigint);
29526418Ssam 		printc('!'); lp--;
29626418Ssam 	FI
29726418Ssam #endif
29826418Ssam }
29926418Ssam 
30026418Ssam 
printesc(c)30126418Ssam printesc(c)
30226418Ssam REG c;
30326418Ssam {
30426418Ssam 	c &= STRIP;
30526418Ssam 	IF c==0177 ORF c<SP
30626418Ssam 	THEN printf("^%c", c ^ 0100);
30726418Ssam 	ELSE printc(c);
30826418Ssam 	FI
30926418Ssam }
31026418Ssam 
inkdot(incr)31126418Ssam L_INT	inkdot(incr)
31226418Ssam {
31326418Ssam 	REG	L_INT	newdot;
31426418Ssam 
31526418Ssam 	newdot=dot+incr;
31626418Ssam 	IF (dot ^ newdot) >> 24 THEN error(ADWRAP); FI
31726418Ssam 	return(newdot);
31826418Ssam }
319