xref: /csrg-svn/old/adb/adb.vax/format.c (revision 35101)
114469Ssam #ifndef lint
2*35101Sbostic static	char sccsid[] = "@(#)format.c	4.6 07/18/88";
314469Ssam #endif
43756Sroot /*
53756Sroot  *
63756Sroot  *	UNIX debugger
73756Sroot  *
83756Sroot  */
93756Sroot 
103756Sroot #include "defs.h"
113756Sroot 
123756Sroot MSG		BADMOD;
133756Sroot MSG		NOFORK;
143756Sroot MSG		ADWRAP;
153756Sroot 
163756Sroot INT		mkfault;
173756Sroot CHAR		*lp;
183756Sroot L_INT		maxoff;
193756Sroot ADDR		sigint;
203756Sroot ADDR		sigqit;
213756Sroot STRING		errflg;
223756Sroot CHAR		lastc,peekc;
233756Sroot L_INT		dot;
243756Sroot INT		dotinc;
253756Sroot L_INT		expv;
263756Sroot L_INT		var[];
273756Sroot 
283756Sroot 
293756Sroot STRING		fphack;
rdfp()303756Sroot rdfp()
313756Sroot {
323756Sroot 	return(lastc= *fphack++);
333756Sroot }
343756Sroot 
scanform(icount,ifp,itype,ptype)353756Sroot scanform(icount,ifp,itype,ptype)
363756Sroot L_INT		icount;
373756Sroot STRING		ifp;
383756Sroot {
393756Sroot 	STRING		fp;
403756Sroot 	CHAR		modifier;
413756Sroot 	INT		fcount, init=1;
423756Sroot 	L_INT		savdot;
433756Sroot 	BOOL exact;
443756Sroot 	BOOL doit = 1;
453756Sroot 
463756Sroot 	WHILE icount
473756Sroot 	DO  fp=ifp;
483756Sroot 	    savdot=dot; init=0;
493756Sroot 
503756Sroot 	    IF init==0 ANDF (exact=(findsym(dot,ptype)==0)) ANDF maxoff
513756Sroot 	    THEN printf("\n%s:%16t",cursym->n_un.n_name);
523756Sroot 	    FI
533756Sroot 
543756Sroot 	    /*now loop over format*/
553756Sroot 	    WHILE *fp ANDF errflg==0
563756Sroot 	    DO  IF digit(modifier = *fp)
573756Sroot 		THEN fcount = 0;
583756Sroot 			WHILE digit(modifier = *fp++)
593756Sroot 			DO fcount *= 10;
603756Sroot 			   fcount += modifier-'0';
613756Sroot 			OD
623756Sroot 			fp--;
6326430Smckusick 			IF fcount==0 THEN fcount = 1; FI
643756Sroot 		ELSE fcount = 1;
653756Sroot 		FI
663756Sroot 
673756Sroot 		IF *fp==0 THEN break; FI
6831707Sbostic 		fp=exform(fcount,fp,itype,ptype);
693756Sroot 	    OD
703756Sroot 	    dotinc=dot-savdot;
713756Sroot 	    dot=savdot;
723756Sroot 
733756Sroot 	    IF errflg
743756Sroot 	    THEN IF icount<0
753756Sroot 		 THEN errflg=0; break;
763756Sroot 		 ELSE error(errflg);
773756Sroot 		 FI
783756Sroot 	    FI
793756Sroot 	    IF --icount
803756Sroot 	    THEN dot=inkdot(dotinc);
813756Sroot 	    FI
823756Sroot 	    IF mkfault THEN error(0); FI
833756Sroot 	OD
843756Sroot }
853756Sroot 
863756Sroot STRING
exform(fcount,ifp,itype,ptype)873756Sroot exform(fcount,ifp,itype,ptype)
883756Sroot INT		fcount;
893756Sroot STRING		ifp;
903756Sroot {
913756Sroot 	/* execute single format item `fcount' times
923756Sroot 	 * sets `dotinc' and moves `dot'
933756Sroot 	 * returns address of next format item
943756Sroot 	 */
953756Sroot 	POS		w;
963756Sroot 	L_INT		savdot, wx;
973756Sroot 	STRING		fp;
983756Sroot 	CHAR		c, modifier, longpr;
993756Sroot 	L_REAL		fw;
10034078Sbostic 	struct bad_programming {
1013756Sroot 		L_INT	sa;
1023756Sroot 		INT	sb,sc;
1033756Sroot 	};
1043756Sroot 
1053756Sroot 	WHILE fcount>0
1063756Sroot 	DO	fp = ifp; c = *fp;
1073756Sroot 		longpr=(c>='A')&(c<='Z')|(c=='f')|(c=='4')|(c=='p');
1083756Sroot 		IF itype==NSP ORF *fp=='a'
1093756Sroot 		THEN wx=dot; w=dot;
1103756Sroot 		ELSE w=get(dot,itype);
1113756Sroot 		     IF longpr
1123756Sroot 		     THEN wx=itol(get(inkdot(2),itype),w);
1133756Sroot 		     ELSE wx=w;
1143756Sroot 		     FI
1153756Sroot 		FI
1163756Sroot 		IF c=='F'
11734078Sbostic 		/*
11834078Sbostic 		 * This used to be the following, but real C compilers won't
11934078Sbostic 		 * let you use a double as a struct!
12034078Sbostic 		 *
12134078Sbostic 		 *	THEN fw.sb=get(inkdot(4),itype);
12234078Sbostic 		 *	fw.sc=get(inkdot(6),itype);
12334078Sbostic 		 */
12434078Sbostic 		THEN (*(struct bad_programming *)&fw).sb=get(inkdot(4),itype);
12534078Sbostic 		     (*(struct bad_programming *)&fw).sc=get(inkdot(6),itype);
1263756Sroot 		FI
1273756Sroot 		IF errflg THEN return(fp); FI
1283756Sroot 		IF mkfault THEN error(0); FI
1293756Sroot 		var[0]=wx;
1303756Sroot 		modifier = *fp++;
1313756Sroot 		dotinc=(longpr?4:2);;
1323756Sroot 
1333756Sroot 		IF charpos()==0 ANDF modifier!='a' THEN printf("%16m"); FI
1343756Sroot 
1353756Sroot 		switch(modifier) {
1363756Sroot 
1373756Sroot 		    case SP: case TB:
1383756Sroot 			break;
1393756Sroot 
1403756Sroot 		    case 't': case 'T':
1413756Sroot 			printf("%T",fcount); return(fp);
1423756Sroot 
1433756Sroot 		    case 'r': case 'R':
1443756Sroot 			printf("%M",fcount); return(fp);
1453756Sroot 
1463756Sroot 		    case 'a':
1473756Sroot 			psymoff(dot,ptype,":%16t"); dotinc=0; break;
1483756Sroot 
1493756Sroot 		    case 'p':
1503756Sroot 			psymoff(var[0],ptype,"%16t"); break;
1513756Sroot 
1523756Sroot 		    case 'u':
1533756Sroot 			printf("%-8u",w); break;
1543756Sroot 
1553756Sroot 		    case 'U':
1563756Sroot 			printf("%-16U",wx); break;
1573756Sroot 
1583756Sroot 		    case 'c': case 'C':
1593756Sroot 			IF modifier=='C'
1603756Sroot 			THEN printesc(w&LOBYTE);
1613756Sroot 			ELSE printc(w&LOBYTE);
1623756Sroot 			FI
1633756Sroot 			dotinc=1; break;
1643756Sroot 
1653756Sroot 		    case 'b': case 'B':
1663756Sroot 			printf("%-8o", w&LOBYTE); dotinc=1; break;
1673756Sroot 
1683756Sroot 			case '1':
1693756Sroot 			printf("%-8r", w&LOBYTE); dotinc=1; break;
1703756Sroot 
1713756Sroot 			case '2':
1723756Sroot 		    case 'w':
1733756Sroot 			printf("%-8r", w); break;
1743756Sroot 
1753756Sroot 			case '4':
1763756Sroot 		    case 'W':
1773756Sroot 			printf("%-16R", wx); break;
1783756Sroot 
1793756Sroot 		    case 's': case 'S':
1803756Sroot 			savdot=dot; dotinc=1;
1813756Sroot 			WHILE (c=get(dot,itype)&LOBYTE) ANDF errflg==0
1823756Sroot 			DO dot=inkdot(1);
1833756Sroot 			   IF modifier == 'S'
1843756Sroot 			   THEN printesc(c);
1853756Sroot 			   ELSE printc(c);
1863756Sroot 			   FI
1873756Sroot 			   endline();
1883756Sroot 			OD
1893756Sroot 			dotinc=dot-savdot+1; dot=savdot; break;
1903756Sroot 
1913756Sroot 		    case 'x':
1923756Sroot 			printf("%-8x",w); break;
1933756Sroot 
1943756Sroot 		    case 'X':
1953756Sroot 			printf("%-16X", wx); break;
1963756Sroot 
1973756Sroot 		    case 'Y':
1983756Sroot 			printf("%-24Y", wx); break;
1993756Sroot 
2003756Sroot 		    case 'q':
2013756Sroot 			printf("%-8q", w); break;
2023756Sroot 
2033756Sroot 		    case 'Q':
2043756Sroot 			printf("%-16Q", wx); break;
2053756Sroot 
2063756Sroot 		    case 'o':
2073756Sroot 			printf("%-8o", w); break;
2083756Sroot 
2093756Sroot 		    case 'O':
2103756Sroot 			printf("%-16O", wx); break;
2113756Sroot 
2123756Sroot 		    case 'i':
2133756Sroot 			printins(0,itype,w); printc(EOR); break;
2143756Sroot 
2153756Sroot 		    case 'd':
2163756Sroot 			printf("%-8d", w); break;
2173756Sroot 
2183756Sroot 		    case 'D':
2193756Sroot 			printf("%-16D", wx); break;
2203756Sroot 
2213756Sroot 		    case 'f':
2223756Sroot 			fw = 0;
22334078Sbostic 			/*
22434078Sbostic 			 * This used to be the following, but real compilers
22534078Sbostic 			 * won't let you use a double as a struct!
22634078Sbostic 			 *
22734078Sbostic 			 *	fw.sa = wx;
22834078Sbostic 			 */
22934078Sbostic 			(*(struct bad_programming *)&fw).sa = wx;
230*35101Sbostic 			IF (wx & ~0xFFFF007F) == 0x8000
231*35101Sbostic 			THEN printf("(reserved operand)");
23226430Smckusick 			ELSE printf("%-16.9f", fw);
23326430Smckusick 			FI
2343756Sroot 			dotinc=4; break;
2353756Sroot 
2363756Sroot 		    case 'F':
23734078Sbostic 			(*(struct bad_programming *)&fw).sa = wx;
238*35101Sbostic 			IF (wx & ~0xFFFF007F) == 0x8000
239*35101Sbostic 			THEN printf("%-32s", "(reserved operand)");
24026430Smckusick 			ELSE printf("%-32.18F", fw);
24126430Smckusick 			FI
2423756Sroot 			dotinc=8; break;
2433756Sroot 
2443756Sroot 		    case 'n': case 'N':
2453756Sroot 			printc('\n'); dotinc=0; break;
2463756Sroot 
2473756Sroot 		    case '"':
2483756Sroot 			dotinc=0;
2493756Sroot 			WHILE *fp != '"' ANDF *fp
2503756Sroot 			DO printc(*fp++); OD
2513756Sroot 			IF *fp THEN fp++; FI
2523756Sroot 			break;
2533756Sroot 
2543756Sroot 		    case '^':
2553756Sroot 			dot=inkdot(-dotinc*fcount); return(fp);
2563756Sroot 
2573756Sroot 		    case '+':
2583756Sroot 			dot=inkdot(fcount); return(fp);
2593756Sroot 
2603756Sroot 		    case '-':
2613756Sroot 			dot=inkdot(-fcount); return(fp);
2623756Sroot 
2633756Sroot 		    default: error(BADMOD);
2643756Sroot 		}
2653756Sroot 		IF itype!=NSP
2663756Sroot 		THEN	dot=inkdot(dotinc);
2673756Sroot 		FI
2683756Sroot 		fcount--; endline();
2693756Sroot 	OD
2703756Sroot 
2713756Sroot 	return(fp);
2723756Sroot }
2733756Sroot 
shell()2743756Sroot shell()
2753756Sroot {
2763756Sroot #ifndef EDDT
2773756Sroot 	INT		rc, status, unixpid;
2783756Sroot 	STRING		argp = lp;
2793756Sroot 	STRING		getenv(), shell = getenv("SHELL");
2803756Sroot #ifdef VFORK
2813756Sroot 	char		oldstlp;
2823756Sroot #endif
2833756Sroot 
2843756Sroot 	if (shell == 0)
2853756Sroot 		shell = "/bin/sh";
2863756Sroot 	WHILE lastc!=EOR DO rdc(); OD
2873756Sroot #ifndef VFORK
2883756Sroot 	IF (unixpid=fork())==0
2893756Sroot #else
2903756Sroot 	oldstlp = *lp;
2913756Sroot 	IF (unixpid=vfork())==0
2923756Sroot #endif
2933756Sroot 	THEN	signal(SIGINT,sigint); signal(SIGQUIT,sigqit);
2943756Sroot 		*lp=0; execl(shell, "sh", "-c", argp, 0);
2953756Sroot 		_exit(16);
2963756Sroot #ifndef VFORK
2973756Sroot 	ELIF unixpid == -1
2983756Sroot #else
2993756Sroot 	ELIF *lp = oldstlp, unixpid == -1
3003756Sroot #endif
3013756Sroot 	THEN	error(NOFORK);
3023756Sroot 	ELSE	signal(SIGINT,1);
3033756Sroot 		WHILE (rc = wait(&status)) != unixpid ANDF rc != -1 DONE
3043756Sroot 		signal(SIGINT,sigint);
3053756Sroot 		prints("!"); lp--;
3063756Sroot 	FI
3073756Sroot #endif
3083756Sroot }
3093756Sroot 
3103756Sroot 
printesc(c)3113756Sroot printesc(c)
3123756Sroot {
3133756Sroot 	c &= STRIP;
3143756Sroot 	IF c==0177 THEN printf("^?");
3153756Sroot 	ELIF c<SP
3163756Sroot 	THEN printf("^%c", c + '@');
3173756Sroot 	ELSE printc(c);
3183756Sroot 	FI
3193756Sroot }
3203756Sroot 
inkdot(incr)3213756Sroot L_INT	inkdot(incr)
3223756Sroot {
3233756Sroot 	L_INT		newdot;
3243756Sroot 
3253756Sroot 	newdot=dot+incr;
3263756Sroot 	IF (dot NEQ newdot) >> 24 THEN error(ADWRAP); FI
3273756Sroot 	return(newdot);
3283756Sroot }
3293756Sroot 
digit(c)3303756Sroot digit(c)
3313756Sroot {
3323756Sroot 	return c >= '0' && c <= '9';
3333756Sroot }
334