xref: /csrg-svn/old/adb/adb.vax/format.c (revision 31707)
114469Ssam #ifndef lint
2*31707Sbostic static	char sccsid[] = "@(#)format.c	4.4 06/30/87";
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;
303756Sroot rdfp()
313756Sroot {
323756Sroot 	return(lastc= *fphack++);
333756Sroot }
343756Sroot 
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
68*31707Sbostic 		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
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;
1003756Sroot 	struct{
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'
1173756Sroot 		THEN fw.sb=get(inkdot(4),itype);
1183756Sroot 		     fw.sc=get(inkdot(6),itype);
1193756Sroot 		FI
1203756Sroot 		IF errflg THEN return(fp); FI
1213756Sroot 		IF mkfault THEN error(0); FI
1223756Sroot 		var[0]=wx;
1233756Sroot 		modifier = *fp++;
1243756Sroot 		dotinc=(longpr?4:2);;
1253756Sroot 
1263756Sroot 		IF charpos()==0 ANDF modifier!='a' THEN printf("%16m"); FI
1273756Sroot 
1283756Sroot 		switch(modifier) {
1293756Sroot 
1303756Sroot 		    case SP: case TB:
1313756Sroot 			break;
1323756Sroot 
1333756Sroot 		    case 't': case 'T':
1343756Sroot 			printf("%T",fcount); return(fp);
1353756Sroot 
1363756Sroot 		    case 'r': case 'R':
1373756Sroot 			printf("%M",fcount); return(fp);
1383756Sroot 
1393756Sroot 		    case 'a':
1403756Sroot 			psymoff(dot,ptype,":%16t"); dotinc=0; break;
1413756Sroot 
1423756Sroot 		    case 'p':
1433756Sroot 			psymoff(var[0],ptype,"%16t"); break;
1443756Sroot 
1453756Sroot 		    case 'u':
1463756Sroot 			printf("%-8u",w); break;
1473756Sroot 
1483756Sroot 		    case 'U':
1493756Sroot 			printf("%-16U",wx); break;
1503756Sroot 
1513756Sroot 		    case 'c': case 'C':
1523756Sroot 			IF modifier=='C'
1533756Sroot 			THEN printesc(w&LOBYTE);
1543756Sroot 			ELSE printc(w&LOBYTE);
1553756Sroot 			FI
1563756Sroot 			dotinc=1; break;
1573756Sroot 
1583756Sroot 		    case 'b': case 'B':
1593756Sroot 			printf("%-8o", w&LOBYTE); dotinc=1; break;
1603756Sroot 
1613756Sroot 			case '1':
1623756Sroot 			printf("%-8r", w&LOBYTE); dotinc=1; break;
1633756Sroot 
1643756Sroot 			case '2':
1653756Sroot 		    case 'w':
1663756Sroot 			printf("%-8r", w); break;
1673756Sroot 
1683756Sroot 			case '4':
1693756Sroot 		    case 'W':
1703756Sroot 			printf("%-16R", wx); break;
1713756Sroot 
1723756Sroot 		    case 's': case 'S':
1733756Sroot 			savdot=dot; dotinc=1;
1743756Sroot 			WHILE (c=get(dot,itype)&LOBYTE) ANDF errflg==0
1753756Sroot 			DO dot=inkdot(1);
1763756Sroot 			   IF modifier == 'S'
1773756Sroot 			   THEN printesc(c);
1783756Sroot 			   ELSE printc(c);
1793756Sroot 			   FI
1803756Sroot 			   endline();
1813756Sroot 			OD
1823756Sroot 			dotinc=dot-savdot+1; dot=savdot; break;
1833756Sroot 
1843756Sroot 		    case 'x':
1853756Sroot 			printf("%-8x",w); break;
1863756Sroot 
1873756Sroot 		    case 'X':
1883756Sroot 			printf("%-16X", wx); break;
1893756Sroot 
1903756Sroot 		    case 'Y':
1913756Sroot 			printf("%-24Y", wx); break;
1923756Sroot 
1933756Sroot 		    case 'q':
1943756Sroot 			printf("%-8q", w); break;
1953756Sroot 
1963756Sroot 		    case 'Q':
1973756Sroot 			printf("%-16Q", wx); break;
1983756Sroot 
1993756Sroot 		    case 'o':
2003756Sroot 			printf("%-8o", w); break;
2013756Sroot 
2023756Sroot 		    case 'O':
2033756Sroot 			printf("%-16O", wx); break;
2043756Sroot 
2053756Sroot 		    case 'i':
2063756Sroot 			printins(0,itype,w); printc(EOR); break;
2073756Sroot 
2083756Sroot 		    case 'd':
2093756Sroot 			printf("%-8d", w); break;
2103756Sroot 
2113756Sroot 		    case 'D':
2123756Sroot 			printf("%-16D", wx); break;
2133756Sroot 
2143756Sroot 		    case 'f':
2153756Sroot 			fw = 0;
2163756Sroot 			fw.sa = wx;
21726430Smckusick 			IF (wx & ~0xFFFF00FF) == 0x8000
21826430Smckusick 			THEN printf("(reserved oprnd)");
21926430Smckusick 			ELSE printf("%-16.9f", fw);
22026430Smckusick 			FI
2213756Sroot 			dotinc=4; break;
2223756Sroot 
2233756Sroot 		    case 'F':
2243756Sroot 			fw.sa = wx;
22526430Smckusick 			IF (wx & ~0xFFFF00FF) == 0x8000
22626430Smckusick 			THEN printf("%-32s", "(reserved oprnd)");
22726430Smckusick 			ELSE printf("%-32.18F", fw);
22826430Smckusick 			FI
2293756Sroot 			dotinc=8; break;
2303756Sroot 
2313756Sroot 		    case 'n': case 'N':
2323756Sroot 			printc('\n'); dotinc=0; break;
2333756Sroot 
2343756Sroot 		    case '"':
2353756Sroot 			dotinc=0;
2363756Sroot 			WHILE *fp != '"' ANDF *fp
2373756Sroot 			DO printc(*fp++); OD
2383756Sroot 			IF *fp THEN fp++; FI
2393756Sroot 			break;
2403756Sroot 
2413756Sroot 		    case '^':
2423756Sroot 			dot=inkdot(-dotinc*fcount); return(fp);
2433756Sroot 
2443756Sroot 		    case '+':
2453756Sroot 			dot=inkdot(fcount); return(fp);
2463756Sroot 
2473756Sroot 		    case '-':
2483756Sroot 			dot=inkdot(-fcount); return(fp);
2493756Sroot 
2503756Sroot 		    default: error(BADMOD);
2513756Sroot 		}
2523756Sroot 		IF itype!=NSP
2533756Sroot 		THEN	dot=inkdot(dotinc);
2543756Sroot 		FI
2553756Sroot 		fcount--; endline();
2563756Sroot 	OD
2573756Sroot 
2583756Sroot 	return(fp);
2593756Sroot }
2603756Sroot 
2613756Sroot shell()
2623756Sroot {
2633756Sroot #ifndef EDDT
2643756Sroot 	INT		rc, status, unixpid;
2653756Sroot 	STRING		argp = lp;
2663756Sroot 	STRING		getenv(), shell = getenv("SHELL");
2673756Sroot #ifdef VFORK
2683756Sroot 	char		oldstlp;
2693756Sroot #endif
2703756Sroot 
2713756Sroot 	if (shell == 0)
2723756Sroot 		shell = "/bin/sh";
2733756Sroot 	WHILE lastc!=EOR DO rdc(); OD
2743756Sroot #ifndef VFORK
2753756Sroot 	IF (unixpid=fork())==0
2763756Sroot #else
2773756Sroot 	oldstlp = *lp;
2783756Sroot 	IF (unixpid=vfork())==0
2793756Sroot #endif
2803756Sroot 	THEN	signal(SIGINT,sigint); signal(SIGQUIT,sigqit);
2813756Sroot 		*lp=0; execl(shell, "sh", "-c", argp, 0);
2823756Sroot 		_exit(16);
2833756Sroot #ifndef VFORK
2843756Sroot 	ELIF unixpid == -1
2853756Sroot #else
2863756Sroot 	ELIF *lp = oldstlp, unixpid == -1
2873756Sroot #endif
2883756Sroot 	THEN	error(NOFORK);
2893756Sroot 	ELSE	signal(SIGINT,1);
2903756Sroot 		WHILE (rc = wait(&status)) != unixpid ANDF rc != -1 DONE
2913756Sroot 		signal(SIGINT,sigint);
2923756Sroot 		prints("!"); lp--;
2933756Sroot 	FI
2943756Sroot #endif
2953756Sroot }
2963756Sroot 
2973756Sroot 
2983756Sroot printesc(c)
2993756Sroot {
3003756Sroot 	c &= STRIP;
3013756Sroot 	IF c==0177 THEN printf("^?");
3023756Sroot 	ELIF c<SP
3033756Sroot 	THEN printf("^%c", c + '@');
3043756Sroot 	ELSE printc(c);
3053756Sroot 	FI
3063756Sroot }
3073756Sroot 
3083756Sroot L_INT	inkdot(incr)
3093756Sroot {
3103756Sroot 	L_INT		newdot;
3113756Sroot 
3123756Sroot 	newdot=dot+incr;
3133756Sroot 	IF (dot NEQ newdot) >> 24 THEN error(ADWRAP); FI
3143756Sroot 	return(newdot);
3153756Sroot }
3163756Sroot 
3173756Sroot digit(c)
3183756Sroot {
3193756Sroot 	return c >= '0' && c <= '9';
3203756Sroot }
321