xref: /csrg-svn/old/sdb/sub.c (revision 7776)
1*7776Srrh static	char sccsid[] = "@(#)sub.c 4.2 08/17/82";
21351Sbill #include "head.h"
31351Sbill #include <a.out.h>
41351Sbill #include <stab.h>
51351Sbill #include "cdefs.h"
61351Sbill #include <stdio.h>
71351Sbill struct user u;
81351Sbill 
91351Sbill char *
readline(f)101351Sbill readline(f)
111351Sbill FILE *f; {
121351Sbill 	static char buff[128];
131351Sbill 
141351Sbill 	register char *p;
151351Sbill 	register int i;
161351Sbill 
171351Sbill 	p = buff;
181351Sbill 	do {
191351Sbill 		if ((i = getc(f)) == EOF) {
201351Sbill 			*p++ = '\004';
211351Sbill 			*p = '\n';
221351Sbill 		}
231351Sbill 		else *p = i;
241351Sbill 	} while (*p++ != '\n');
251351Sbill 
261351Sbill 	return(buff);
271351Sbill }
281351Sbill 
291351Sbill char *
cpname(p,q)301351Sbill cpname(p, q)
311351Sbill char *p, *q; {
321351Sbill 	while(varchar(*q) || number(*q))
331351Sbill 		*p++ = *q++;
341351Sbill 	*p = '\0';
351351Sbill 	return(q);
361351Sbill }
371351Sbill 
381351Sbill char *
cpall(p,q)391351Sbill cpall(p, q)
401351Sbill char *p, *q; {
411351Sbill 	while (*q != '\n')
421351Sbill 		*p++ = *q++;
431351Sbill 	*p = '\0';
441351Sbill 	return(q);
451351Sbill }
461351Sbill 
eqany(c,s)471351Sbill eqany(c, s)
481351Sbill char c, *s; {
491351Sbill 	while(*s != '\0')
501351Sbill 		if (c == *s++) return(1);
511351Sbill 	return(0);
521351Sbill }
531351Sbill 
error(s)541351Sbill error(s)
551351Sbill char *s; {
561351Sbill 	printf("%s\n", s);
571351Sbill }
581351Sbill 
591351Sbill char *
cpstr(p,q)601351Sbill cpstr(p,q)
611351Sbill char *p, *q; {
621351Sbill 	do {
631351Sbill 		*p++ = *q++;
641351Sbill 	} while (*q != '\0');
651351Sbill 	*p = '\0';
661351Sbill }
671351Sbill L_INT
round(a,b)681351Sbill round(a,b)
691351Sbill REG L_INT a, b;
701351Sbill {
711351Sbill 	REG L_INT w;
721351Sbill 	w = (a/b)*b;
731351Sbill 	IF a!=w THEN w += b; FI
741351Sbill 	return(w);
751351Sbill }
761351Sbill 
771351Sbill /* error handling */
781351Sbill 
chkerr()791351Sbill chkerr()
801351Sbill {
811351Sbill 	IF errflg ORF mkfault
821351Sbill 	THEN	error(errflg);
831351Sbill 		longjmp(env, 0);
841351Sbill 	FI
851351Sbill }
861351Sbill 
eqstr(s1,s2)871351Sbill eqstr(s1, s2)
881351Sbill 	REG STRING	s1, s2;
891351Sbill {
901351Sbill #ifndef FLEXNAMES
911351Sbill 	REG STRING	 es1;
921351Sbill #endif
931351Sbill 	if (s2 == (STRING) -1) return(0);
941351Sbill #ifndef FLEXNAMES
951351Sbill 	es1 = s1+8;
961351Sbill #endif
971351Sbill 	WHILE *s1++ == *s2
981351Sbill #ifndef FLEXNAMES
991351Sbill 	DO IF *s2++ == 0 ORF s1>=es1
1001351Sbill #else
1011351Sbill 	DO IF *s2++ == 0
1021351Sbill #endif
1031351Sbill 	   THEN return(1);
1041351Sbill 	   FI
1051351Sbill 	OD
1061351Sbill 	return(0);
1071351Sbill }
1081351Sbill 
longseek(f,a)1091351Sbill longseek(f, a)
1101351Sbill L_INT a;
1111351Sbill {
1121351Sbill 	return(lseek(f,(long) a,0) != -1);
1131351Sbill }
1141351Sbill 
1151351Sbill 
1161351Sbill /* descriptor format to length */
dtol(d)1171351Sbill dtol(d)
1181351Sbill char d; {
1191351Sbill 	switch(d) {
1201351Sbill 
1211351Sbill 	case 'a':
1221351Sbill 	case 's':
1231351Sbill 		return(0);
1241351Sbill 
1251351Sbill 	case 'b':
1261351Sbill 	case 'c':
1271351Sbill 		return(1);
1281351Sbill 
1291351Sbill 	case 'h':
1301351Sbill 		return(2);
1311351Sbill 
1321351Sbill 	case 'l':
1331351Sbill 	case 'f':
1341351Sbill 		return(4);
1351351Sbill 
1361351Sbill 	case 'g':
1371351Sbill 		return(8);
1381351Sbill 
1391351Sbill 	default:
1401351Sbill 		return(WORDSIZE);
1411351Sbill 	}
1421351Sbill }
1431351Sbill 
1441351Sbill /*
1451351Sbill  * checks equality of pattern pat with str,
1461351Sbill  * assuming str is tructaed at length 8
1471351Sbill  */
eqpat(pat,str)1481351Sbill eqpat(pat, str)
1491351Sbill char *pat, *str; {
1501351Sbill #ifndef FLEXNAMES
1511351Sbill 	return(eqpatr(pat, str, 0));
1521351Sbill #else
1531351Sbill 	return(eqpatr(pat, str));
1541351Sbill #endif
1551351Sbill }
1561351Sbill 
1571351Sbill #ifndef FLEXNAMES
eqpatr(pat,str,cnt)1581351Sbill eqpatr(pat, str, cnt)
1591351Sbill #else
1601351Sbill eqpatr(pat, str)
1611351Sbill #endif
1621351Sbill char *pat, *str; {
1631351Sbill 	register int i;
1641351Sbill 	register char p, s;
1651351Sbill 
1661351Sbill 	p = pat[0];
1671351Sbill 	s = str[0];
1681351Sbill #ifndef FLEXNAMES
1691351Sbill 	if (cnt == 8) return(1);
1701351Sbill #endif
1711351Sbill 	if (p == '?') {
1721351Sbill 		if (s == '\0') return(0);
1731351Sbill #ifndef FLEXNAMES
1741351Sbill 		return(eqpatr(pat+1, str+1, cnt+1));
1751351Sbill #else
1761351Sbill 		return(eqpatr(pat+1, str+1));
1771351Sbill #endif
1781351Sbill 	}
1791351Sbill 	if (p == '*') {
1801351Sbill 		if (pat[1] == '\0') return(1);
1811351Sbill #ifndef FLEXNAMES
1821351Sbill 		for(i=1; i<8-cnt; i++) {
1831351Sbill 			if (eqpatr(pat+1, str+i, cnt+i)) return(1);
1841351Sbill #else
1851351Sbill 		for(i=1; ; i++) {
1861351Sbill 			if (eqpatr(pat+1, str+i)) return(1);
1871351Sbill #endif
1881351Sbill 			if (str[i] == '\0') return(0);
1891351Sbill 		}
1901351Sbill #ifndef FLEXNAMES
1911351Sbill 		return(0);
1921351Sbill #else
1931351Sbill 		/*NOTREACHED*/
1941351Sbill #endif
1951351Sbill 	}
1961351Sbill 	if ((eqany(p, ".[->") || p == '\0') && s == '\0') return(1);
1971351Sbill 	if (p != s) return(0);
1981351Sbill #ifndef FLEXNAMES
1991351Sbill 	return(eqpatr(pat+1, str+1, cnt+1));
2001351Sbill #else
2011351Sbill 	return(eqpatr(pat+1, str+1));
2021351Sbill #endif
2031351Sbill }
2041351Sbill 
2051351Sbill /* gets indirect address for pointers and subscripts */
getindir(class,addr,type)2061351Sbill getindir(class, addr, type)
207*7776Srrh u_char class;
2081351Sbill ADDR addr; {
2091351Sbill 	if (ISARY(type)) return(addr);
2101351Sbill 	if (class == N_RSYM)
2111351Sbill 		return(*(ADDR *)(((ADDR) &u) + R0 + (WORDSIZE)*addr));
2121351Sbill 	return(getval(addr, 'd', DSP));
2131351Sbill }
2141351Sbill 
2151351Sbill long
readint(p)2161351Sbill readint(p)
2171351Sbill char **p; {
2181351Sbill 	int sign;
2191351Sbill 
2201351Sbill 	if (**p == '-') {
2211351Sbill 		sign = -1;
2221351Sbill 		(*p)++;
2231351Sbill 	} else {
2241351Sbill 		sign = 1;
2251351Sbill 	}
2261351Sbill 	if (**p == '0') {
2271351Sbill 		(*p)++;
2281351Sbill 		if (**p == 'x' || **p == 'X') {
2291351Sbill 			(*p)++;
2301351Sbill 			return(sign * rint(p, 16, hexdigit, hexconv));
2311351Sbill 		}
2321351Sbill 		else return(sign * rint(p, 8, octdigit, octconv));
2331351Sbill 	}
2341351Sbill 	else return(sign * rint(p, 10, decdigit, decconv));
2351351Sbill }
2361351Sbill 
2371351Sbill long
rint(p,base,digit,conv)2381351Sbill rint(p, base, digit, conv)
2391351Sbill char **p;
2401351Sbill int (*digit)(), (*conv)(); {
2411351Sbill 	long value;
2421351Sbill 
2431351Sbill 	value = 0;
2441351Sbill 	while ((*digit)(**p)) value = base*value + (*conv)(*(*p)++);
2451351Sbill 	return(value);
2461351Sbill }
2471351Sbill 
octdigit(c)2481351Sbill octdigit(c)
2491351Sbill char c; {
2501351Sbill 	return(c >= '0' && c <= '7');
2511351Sbill }
2521351Sbill 
octconv(c)2531351Sbill octconv(c)
2541351Sbill char c; {
2551351Sbill 	return(c - '0');
2561351Sbill }
2571351Sbill 
decdigit(c)2581351Sbill decdigit(c)
2591351Sbill char c; {
2601351Sbill 	return(c >= '0' && c <= '9');
2611351Sbill }
2621351Sbill 
decconv(c)2631351Sbill decconv(c)
2641351Sbill char c; {
2651351Sbill 	return(c - '0');
2661351Sbill }
2671351Sbill 
hexdigit(c)2681351Sbill hexdigit(c)
2691351Sbill char c; {
2701351Sbill 	return((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
2711351Sbill 		(c >= 'A' && c <= 'F'));
2721351Sbill }
2731351Sbill 
hexconv(c)2741351Sbill hexconv(c)
2751351Sbill char c; {
2761351Sbill 	if (c >= '0' && c <= '9') return(c - '0');
2771351Sbill 	if (c >= 'a' && c <= 'f') return(c - 'a' + 10);
2781351Sbill 	if (c >= 'A' && c <= 'F') return(c - 'A' + 10);
2791351Sbill 	error("hex conversion error");
2801351Sbill 	return(0);
2811351Sbill }
2821351Sbill 
2831351Sbill /* decodes number, character or variable */
2841351Sbill long
argvalue(p)2851351Sbill argvalue(p)
2861351Sbill char *p; {
2871351Sbill 	register char ch;
2881351Sbill 	register long value;
2891351Sbill 	register ADDR j;
2901351Sbill 	char var[30];
2911351Sbill 
2921351Sbill 	ch = *p;
2931351Sbill 	if (ch == '\'') {
2941351Sbill 		value = *(p+1);
2951351Sbill 	} else if ((ch >= '0' && ch <= '9') || ch == '-') {
2961351Sbill 		value = readint(&p);
2971351Sbill 	} else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
2981351Sbill 		ch == '_') {
2991351Sbill 		cpname(var, p);
3001351Sbill 		j = varaddr(curproc()->pname, var);
3011351Sbill 		if (j == -1) {
3021351Sbill 			printf("Unknown variable: %s\n", argsp);
3031351Sbill 			return(-1);
3041351Sbill 		}
3051351Sbill 		value = getval(j, typetodesc(sl_type, 0)[0], DSP);
3061351Sbill 		do {
3071351Sbill 			p++;
3081351Sbill 		} while (varchar(*p) || number(*p));
3091351Sbill 	}
3101351Sbill 	return(value);
3111351Sbill }
3121351Sbill 
prhex(v)3131351Sbill prhex(v)
3141351Sbill long v; {
3151351Sbill 	if (v < 0)  {
3161351Sbill 		v = -v;
3171351Sbill 		printf("-");
3181351Sbill 	}
3191351Sbill 	if (v <= 9)
3201351Sbill 		printf("%d", v);
3211351Sbill 	else
3221351Sbill 		printf("0x%x", v);
3231351Sbill }
3241351Sbill 
3251351Sbill /* print hex number in field of length 12 */
prhex12(v)3261351Sbill prhex12(v)
3271351Sbill long v; {
3281351Sbill 	if (v >= -9 && v <= 9)
3291351Sbill 		printf("%-12d", v);
3301351Sbill 	else
3311351Sbill 		printf("0x%-12x", v);
3321351Sbill }
3331351Sbill 
3341351Sbill /* print line number followed by offset */
3351351Sbill prlnoff(procp, v)
3361351Sbill struct proct *procp; ADDR v; {
3371351Sbill 	int lineno, diff;
3381351Sbill 	char *name;
3391351Sbill 	name = procp->pname;
3401351Sbill 	if (name[0] == '_') {
3411351Sbill #ifndef FLEXNAMES
3421351Sbill 		printf("%.7s", name+1);
3431351Sbill #else
3441351Sbill 		printf("%s", name+1);
3451351Sbill #endif
3461351Sbill 		lineno = -1;
3471351Sbill 	} else {
3481351Sbill #ifndef FLEXNAMES
3491351Sbill 		printf("%8s", name);
3501351Sbill #else
3511351Sbill 		printf("%s", name);
3521351Sbill #endif
3531351Sbill 		lineno = adrtolineno((ADDR) v);
3541351Sbill 	}
3551351Sbill 	if (lineno == -1)
3561351Sbill 		diff = v - procp->paddr;
3571351Sbill 	else {
3581351Sbill 		printf(":%d", lineno);
3591351Sbill 		diff = v - lnfaddr;  /* set by adrtolineno() */
3601351Sbill 	}
3611351Sbill 	if (diff) {
3621351Sbill 		printf("+");
3631351Sbill 		prhex(diff);
3641351Sbill 	}
3651351Sbill }
366