aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/db/command.c
blob: 55ed02a8283d830b2939a97bbd2ad20f810fdff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 *
 *	debugger
 *
 */

#include "defs.h"
#include "fns.h"

char	BADEQ[] = "unexpected `='";

BOOL	executing;
extern	char	*lp;

char	eqformat[ARB] = "z";
char	stformat[ARB] = "zMi";

ADDR	ditto;

ADDR	dot;
WORD	dotinc;
WORD	adrval, cntval, loopcnt;
int	adrflg, cntflg;

/* command decoding */

int
command(char *buf, int defcom)
{
	char	*reg;
	char	savc;
	char	*savlp=lp;
	char	savlc = lastc;
	char	savpc = peekc;
	static char lastcom = '=', savecom = '=';

	if (defcom == 0)
		defcom = lastcom;
	if (buf) {
		if (*buf==EOR)
			return(FALSE);
		clrinp();
		lp=buf;
	}
	do {
		adrflg=expr(0);		/* first address */
		if (adrflg){
			dot=expv;
			ditto=expv;
		}
		adrval=dot;

		if (rdc()==',' && expr(0)) {	/* count */
			cntflg=TRUE;
			cntval=expv;
		} else {
			cntflg=FALSE;
			cntval=1;
			reread();
		}

		if (!eol(rdc()))
			lastcom=lastc;		/* command */
		else {
			if (adrflg==0)
				dot=inkdot(dotinc);
			reread();
			lastcom=defcom;
		}
		switch(lastcom) {
		case '/':
		case '=':
		case '?':
			savecom = lastcom;
			acommand(lastcom);
			break;

		case '>':
			lastcom = savecom; 
			savc=rdc();
			if (reg=regname(savc))
				rput(correg, reg, dot);
			else	
				error("bad variable");
			break;

		case '!':
			lastcom=savecom;
			shell(); 
			break;

		case '$':
			lastcom=savecom;
			printdollar(nextchar()); 
			break;

		case ':':
			if (!executing) { 
				executing=TRUE;
				subpcs(nextchar());
				executing=FALSE;
				lastcom=savecom;
			}
			break;

		case 0:
			prints(DBNAME);
			break;

		default: 
			error("bad command");
		}
		flushbuf();
	} while (rdc()==';');
	if (buf == 0)
		reread();
	else {
		clrinp();
		lp=savlp;
		lastc = savlc;
		peekc = savpc;
	}

	if(adrflg)
		return dot;
	return 1;
}

/*
 * [/?][wml]
 */

void
acommand(int pc)
{
	int eqcom;
	Map *map;
	char *fmt;
	char buf[512];

	if (pc == '=') {
		eqcom = 1;
		fmt = eqformat;
		map = dotmap;
	} else {
		eqcom = 0;
		fmt = stformat;
		if (pc == '/')
			map = cormap;
		else
			map = symmap;
	}
	if (!map) {
		sprint(buf, "no map for %c", pc);
		error(buf);
	}

	switch (rdc())
	{
	case 'm':
		if (eqcom)
			error(BADEQ); 
		cmdmap(map);
		break;

	case 'L':
	case 'l':
		if (eqcom)
			error(BADEQ); 
		cmdsrc(lastc, map);
		break;

	case 'W':
	case 'w':
		if (eqcom)
			error(BADEQ); 
		cmdwrite(lastc, map);
		break;

	default:
		reread();
		getformat(fmt);
		scanform(cntval, !eqcom, fmt, map, eqcom);
	}
}

void
cmdsrc(int c, Map *map)
{
	u32int w;
	long locval, locmsk;
	ADDR savdot;
	ushort sh;
	char buf[512];
	int ret;

	if (c == 'L')
		dotinc = 4;
	else
		dotinc = 2;
	savdot=dot;
	expr(1); 
	locval=expv;
	if (expr(0))
		locmsk=expv; 
	else
		locmsk = ~0;
	if (c == 'L')
		while ((ret = get4(map, dot, &w)) > 0 &&  (w&locmsk) != locval)
			dot = inkdot(dotinc);
	else
		while ((ret = get2(map, dot, &sh)) > 0 && (sh&locmsk) != locval)
			dot = inkdot(dotinc);
	if (ret < 0) { 
		dot=savdot; 
		error("%r");
	}
	symoff(buf, 512, dot, CANY);
	dprint(buf);
}

static char badwrite[] = "can't write process memory or text image";

void
cmdwrite(int wcom, Map *map)
{
	ADDR savdot;
	char *format;
	int pass;

	if (wcom == 'w')
		format = "x";
	else
		format = "X";
	expr(1);
	pass = 0;
	do {
		pass++;  
		savdot=dot;
		exform(1, 1, format, map, 0, pass);
		dot=savdot;
		if (wcom == 'W') {
			if (put4(map, dot, expv) <= 0)
				error(badwrite);
		} else {
			if (put2(map, dot, expv) <= 0)
				error(badwrite);
		}
		savdot=dot;
		dprint("=%8t"); 
		exform(1, 0, format, map, 0, pass);
		newline();
	} while (expr(0));
	dot=savdot;
}

/*
 * collect a register name; return register offset
 * this is not what i'd call a good division of labour
 */

char *
regname(int regnam)
{
	static char buf[64];
	char *p;
	int c;

	p = buf;
	*p++ = regnam;
	while (isalnum(c = readchar())) {
		if (p >= buf+sizeof(buf)-1)
			error("register name too long");
		*p++ = c;
	}
	*p = 0;
	reread();
	return (buf);
}

/*
 * shell escape
 */

void
shell(void)
{
	int	rc, unixpid;
	char *argp = lp;

	while (lastc!=EOR)
		rdc();
	if ((unixpid=fork())==0) {
		*lp=0;
		execl("/bin/rc", "rc", "-c", argp, 0);
		exits("execl");				/* botch */
	} else if (unixpid == -1) {
		error("cannot fork");
	} else {
		mkfault = 0;
		while ((rc = waitpid()) != unixpid){
			if(rc == -1 && mkfault){
				mkfault = 0;
				continue;
			}
			break;
		}
		prints("!"); 
		reread();
	}
}