1 /*
2  * Copyright (c) 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)kdb_format.c	7.4 (Berkeley) 12/15/86
7  */
8 
9 #include "../kdb/defs.h"
10 
11 char	*BADMOD;
12 char	*ADWRAP;
13 
14 char	*lp;
15 char	lastc,peekc;
16 long	expv;
17 
18 scanform(icount,ifp,itype,ptype)
19 	long icount;
20 	char *ifp;
21 {
22 	register char *fp;
23 	char modifier;
24 	register fcount, init=1;
25 	long savdot;
26 	int exact;
27 
28 	while (icount) {
29 		fp=ifp;
30 		savdot=dot; init=0;
31 		if (!init && (exact=(findsym(dot,ptype)==0)) && maxoff)
32 			printf("\n%s:%16t",cursym->n_un.n_name);
33 		/*now loop over format*/
34 		while (*fp && errflg==0) {
35 			if (isdigit(modifier = *fp)) {
36 				fcount = 0;
37 				while (isdigit(modifier = *fp++)) {
38 				   fcount *= 10;
39 				   fcount += modifier-'0';
40 				}
41 				fp--;
42 			} else
43 				fcount = 1;
44 			if (*fp==0)
45 				break;
46 #ifdef	ENTRYMASK
47 			/* check for entry mask */
48 			if (exact && dot==savdot &&
49 			   (cursym->n_type&N_TYPE)==N_TEXT &&
50 			   cursym->n_un.n_name[0]=='_' && *fp=='i') {
51 				(void) exform(1,"x",itype,ptype);
52 				fp++;
53 				printc(EOR);
54 			} else
55 #endif
56 				fp = exform(fcount,fp,itype,ptype);
57 		}
58 		dotinc=dot-savdot;
59 		dot=savdot;
60 
61 		if (errflg) {
62 			if (icount<0) {
63 				 errflg=0;
64 				 break;
65 			}
66 			error(errflg);
67 		}
68 		if (--icount)
69 			dot=inkdot(dotinc);
70 		if (mkfault)
71 			error((char *)0);
72 	}
73 }
74 
75 /*
76  * Execute single format item `fcount' times
77  * sets `dotinc' and moves `dot'
78  * returns address of next format item
79  */
80 char *
81 exform(fcount,ifp,itype,ptype)
82 	int fcount;
83 	char *ifp;
84 {
85 	register POS w;
86 	register long savdot, wx;
87 	register char *fp;
88 	char c, modifier, longpr;
89 
90 	while (fcount>0) {
91 	  	fp = ifp; c = *fp;
92 		longpr = (isupper(c) || c=='f' || c=='4' || c=='p');
93 		if (itype != NSP && *fp != 'a') {
94 			wx = get(dot, itype);
95 			w = shorten(wx);
96 		} else {
97 			wx = w = dot;
98 			if (itype == NSP &&
99 			    (c == 'b' || c == 'B' ||
100 			     c == 'c' || c == 'C' || c == '1'))
101 				w = btol(wx);
102 		}
103 		if (errflg)
104 			return (fp);
105 		if (mkfault)
106 			error((char *)0);
107 		var[0] = wx;
108 		modifier = *fp++;
109 		dotinc = (longpr ? sizeof (long):sizeof (short));
110 
111 		if (charpos()==0 && modifier!='a')
112 			printf("%16m");
113 		switch (modifier) {
114 
115 		case SP: case TB:
116 			break;
117 
118 		case 't': case 'T':
119 			printf("%T",fcount); return (fp);
120 
121 		case 'r': case 'R':
122 			printf("%M",fcount); return (fp);
123 
124 		case 'a':
125 			psymoff(dot,ptype,":%16t"); dotinc=0; break;
126 
127 		case 'p':
128 			psymoff(var[0],ptype,"%16t"); break;
129 
130 		case 'u':
131 			printf("%-8u",w); break;
132 
133 		case 'U':
134 			printf("%-16U",wx); break;
135 
136 		case 'c': case 'C':
137 			if (modifier == 'C')
138 				printesc((int)byte(w));
139 			else
140 				printc((char)byte(w));
141 			dotinc=1; break;
142 
143 		case 'b': case 'B':
144 			printf("%-8o", byte(w)); dotinc=1; break;
145 
146 		case '1':
147 			printf("%-8R", byte(w)); dotinc=1; break;
148 
149 		case 'w': case '2':
150 			printf("%-8R", w); break;
151 
152 		case 'W': case '4':
153 			printf("%-16R", wx); break;
154 
155 		case 's': case 'S':
156 			savdot=dot; dotinc=1;
157 			while ((c=byte(get(dot,itype))) && errflg==0) {
158 				dot=inkdot(1);
159 				if (modifier == 'S')
160 					printesc((int)c);
161 				else
162 					printc(c);
163 				endline();
164 			}
165 			dotinc=dot-savdot+1; dot=savdot; break;
166 
167 		case 'x':
168 			printf("%-8x",w); break;
169 
170 		case 'X':
171 			printf("%-16X", wx); break;
172 
173 		case 'z':
174 			printf("%-8z",w); break;
175 
176 		case 'Z':
177 			printf("%-16Z", wx); break;
178 
179 		case 'Y':
180 			printf("%-24Y", wx); break;
181 
182 		case 'q':
183 			printf("%-8q", w); break;
184 
185 		case 'Q':
186 			printf("%-16Q", wx); break;
187 
188 		case 'o':
189 			printf("%-8o", w); break;
190 
191 		case 'O':
192 			printf("%-16O", wx); break;
193 
194 		case 'i': case 'I':
195 			printins(itype,wx); printc(EOR); break;
196 
197 		case 'd':
198 			printf("%-8d", w); break;
199 
200 		case 'D':
201 			printf("%-16D", wx); break;
202 
203 		case 'n': case 'N':
204 			printc('\n'); dotinc=0; break;
205 
206 		case '"':
207 			dotinc=0;
208 			while (*fp != '"' && *fp)
209 				printc(*fp++);
210 			if (*fp)
211 				fp++;
212 			break;
213 
214 		case '^':
215 			dot=inkdot(-dotinc*fcount); return (fp);
216 
217 		case '+':
218 			dot=inkdot(fcount); return (fp);
219 
220 		case '-':
221 			dot=inkdot(-fcount); return (fp);
222 
223 		default:
224 			error(BADMOD);
225 		}
226 		if (itype!=NSP)
227 			dot=inkdot(dotinc);
228 		fcount--; endline();
229 	}
230 	return (fp);
231 }
232 
233 static
234 printesc(c)
235 	register int c;
236 {
237 
238 	c &= STRIP;
239 	if (c==0177 || c<SP)
240 		printf("^%c", c ^ 0100);
241 	else
242 		printc(c);
243 }
244 
245 long
246 inkdot(incr)
247 {
248 	register long newdot;
249 
250 	newdot=dot+incr;
251 	if (addrwrap(dot, newdot))
252 		error(ADWRAP);
253 	return (newdot);
254 }
255