Debian Codegraph @

terps/tads/tads2/msdos/dbgu.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/msdos/DBGU.C,v 1.3 1999/07/11 00:46:37 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1992, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 dbgu.c - user interface for debugger
15 Function
16 Implements the user interface for the command-line version of the
17 debugger. This portion of the debugger performs only user-interface
18 functions; the UI-indenpendent section actually performs most of
19 the work of debugging.
20 Notes
21 None
22 Modified
23 04/12/92 MJRoberts - creation
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include "os.h"
31 #include "dbg.h"
32 #include "tio.h"
33 #include "obj.h"
34 #include "prp.h"
35 #include "tok.h"
36 #include "run.h"
37 #include "lin.h"
38 #include "linf.h"
39 #include "osdbg.h"
40 #include "osgen.h"
41
42 #ifdef TURBO
43 #include <dos.h>
44 #endif
45
46 #ifdef MICROSOFT
47 extern char **__argv;
48 #define ARG0 __argv[0]
49 #endif /* MICROSOFT */
50
51 #ifdef __IBMC__
52 extern char **_argv;
53 #endif /* __IBMC__ */
54
55 #ifdef __EMX__
56 # define ARG0 (char *)0
57 #endif /* __EMX__ */
58
59 #ifndef ARG0
60 # define ARG0 _argv[0]
61 #endif /* !ARG0 */
62
63 /* some special characters for PC's */
64 #define DBGUHLIN 196 /* horizontal line */
65 #define DBGARROW 16 /* right-pointing arrow */
66 #define DBGBPCHAR 15 /* breakpoint indicator character */
67
68 /* debug text, status, source, and watch windows */
69 static oswdef dbgtxtwin;
70 static oswdef dbgstawin;
71 static oswdef dbgsrcwin;
72 static oswdef dbgwatwin;
73
74 /* space for lindef containing arbitrary file not part of game source */
75 static char filesrc[sizeof(linfdef) + 128];
76
77 /* location in source file of top current source window and current line */
78 static uchar dbgsrctop[LINLLNMAX]; /* current display seek location */
79 static uchar dbgsrccsr[LINLLNMAX]; /* current cursor line */
80 static uchar dbgsrcloc[LINLLNMAX]; /* current source line in file */
81 static uchar dbgsrchome[LINLLNMAX]; /* seek location of "home" screen */
82
83 /* lindef for current source file */
84 static lindef *dbgsrclin; /* lindef for currently displayed file */
85 static lindef *dbgsrclinhome; /* lindef of "home" file */
86
87 /* color for current line/breakpoint line in source window */
88 static int dbgcurcolor = 30;
89 static int dbgbpcolor = 0x47;
90
91 /* private breakpoint data - maintained with dbgbpeach() */
92 static int dbgbpcnt; /* number of breakpoints set */
93 typedef struct dbgubpdef dbgubpdef;
94 static struct dbgubpdef
95 {
96 int dbgubpid; /* line source id */
97 char dbgubpseek[LINLLNMAX]; /* seek position of breakpoint */
98 }
99 dbgbps[DBGBPMAX]; /* seek locations of breakpoints */
100
101 /* forward declarations */
102 static void dbgugetbp(dbgcxdef *ctx);
103
104
105 /* move back or forward by one or more lines within a file */
106 static char *dbgbuf;
107 static size_t dbgbufsiz;
108 static void dbgmove(lindef *lin, int cur, int *destlinep)
109 {
110 int dest = *destlinep;
111 char *p;
112 size_t max;
113 long backup;
114 long delta;
115 uchar pos[LINLLNMAX];
116 uchar prvpos[LINLLNMAX];
117
118 /* find current location in line source */
119 lintell(lin, pos);
120
121 if (cur < dest)
122 {
123 while (cur != dest)
124 {
125 max = linread(lin, (uchar *)dbgbuf, (int)dbgbufsiz);
126 if (max == 0) break;
127 for (delta = 0, p = dbgbuf ; p < dbgbuf + max && cur != dest ;
128 ++p, ++delta)
129 if (*p == '\n') ++cur;
130 linpadd(lin, pos, delta);
131 }
132 }
133 else
134 {
135 ++cur;
136 while (cur != dest)
137 {
138 backup = linofs(lin);
139 if (backup > (long)dbgbufsiz) backup = dbgbufsiz;
140 memcpy(prvpos, pos, (size_t)sizeof(prvpos));
141 linpadd(lin, prvpos, -(long)dbgbufsiz);
142 linseek(lin, prvpos);
143 max = linread(lin, (uchar *)dbgbuf, backup);
144 if (max == 0) break;
145 p = dbgbuf + max;
146 delta = 0;
147 do
148 {
149 --p;
150 --delta;
151 if (*p == '\n') --cur;
152 } while (p > dbgbuf && cur != dest);
153 linpadd(lin, pos, delta);
154 if (linqtop(lin, prvpos)) break;
155 }
156 if (!linqtop(lin, pos)) linpadd(lin, pos, (long)1);
157 else cur = 1;
158 }
159
160 linseek(lin, pos);
161 *destlinep = cur;
162 }
163
164 /* show current source page */
165 static void dbgsrcshow(lindef *lin, oswdef *win, uchar *top, uchar *loc)
166 {
167 int y;
168 char outbuf[128];
169 int wid = dbgsrcwin.oswx2 - dbgsrcwin.oswx1 + 1;
170 extern int sdesc_color, text_color;
171 char prefix[3];
172 uchar curloc[LINLLNMAX];
173 int i;
174 dbgubpdef *bp;
175 int thisid;
176
177 /* if there's no line source, we can't show anything */
178 if (lin == 0)
179 return;
180
181 thisid = lin->linid;
182 prefix[2] = '\0';
183 osdbgclr(win);
184 for (y = win->oswy1 ; y <= win->oswy2 ; ++y)
185 {
186 win->oswx = win->oswx1;
187 win->oswy = y;
188 linftell(lin, curloc);
189
190 /* special display if this is the currently executing line */
191 if (!memcmp(curloc, loc, lin->linlln) && lin == dbgsrclin)
192 {
193 win->oswcolor = dbgcurcolor;
194 prefix[1] = DBGARROW;
195 }
196 else
197 {
198 win->oswcolor = sdesc_color;
199 prefix[1] = ' ';
200 }
201
202 /* special display if there is a breakpoint at this line */
203 prefix[0] = ' '; /* presume nothing special about this line */
204 for (i = dbgbpcnt, bp = dbgbps ; i ; ++bp, --i)
205 {
206
207 if (!memcmp(bp->dbgubpseek, curloc, lin->linlln)
208 && (bp->dbgubpid == thisid))
209 {
210 prefix[0] = DBGBPCHAR;
211 win->oswcolor = dbgbpcolor;
212 break;
213 }
214 }
215
216 /* display prefix with current execution/breakpoint info */
217 osdbgpt(win, "%s", prefix);
218
219 /* display the line itself */
220 if (!lingets(lin, (uchar *)outbuf, sizeof(outbuf)))
221 break;
222 else
223 {
224 int l;
225
226 l = strlen(outbuf);
227 while (l && outbuf[l-1] == '\n' || outbuf[l-1] == '\r')
228 --l;
229
230 if (l + 2 >= wid)
231 outbuf[wid - 2] = '\0';
232 else
233 {
234 outbuf[l] = '\0';
235 if (prefix[0] == DBGBPCHAR)
236 {
237 /* breakpoint - pad line out with spaces to full width */
238 while (l < wid - 2) outbuf[l++] = ' ';
239 outbuf[l] = '\0';
240 }
241 }
242 osdbgpt(win, "%s", outbuf);
243 if (y != win->oswy2) osdbgpt(win, "\n");
244 }
245 }
246
247 /* restore normal color */
248 win->oswcolor = sdesc_color;
249 }
250
251 /* search for a string in a line source */
252 static void dbgusearch(lindef *lin, uchar *pos, uchar *toppos,
253 oswdef *win, char *str)
254 {
255 int dest;
256
257 /* search is case-insensitive - convert search string to lowercase */
258 os_strlwr(str);
259
260 /* seek to starting location, and read and discard current line */
261 linseek(lin, pos);
262 if (!lingets(lin, (uchar *)dbgbuf, (int)dbgbufsiz)) return;
263
264 for (;;)
265 {
266 lintell(lin, pos);
267 if (!lingets(lin, (uchar *)dbgbuf, (int)dbgbufsiz)) break;
268 os_strlwr(dbgbuf);
269 if (strstr(dbgbuf, str)) break;
270 }
271
272 /* move search expression to middle of screen */
273 linseek(lin, pos);
274 dest = 1;
275 dbgmove(lin, (win->oswy2 - win->oswy1)/2, &dest);
276 lintell(lin, toppos);
277 dbgsrcshow(lin, win, toppos, dbgsrcloc);
278 }
279
280 /* redraw status line */
281 static char dbg_curstat[81];
282 static void dbgustat(dbgcxdef *ctx)
283 {
284 VARUSED(ctx);
285
286 dbgstawin.oswx = dbgstawin.oswx1;
287 dbgstawin.oswy = dbgstawin.oswy1;
288 osdbgpt(&dbgstawin, dbg_curstat);
289 }
290
291 /* show the current line */
292 static void dbgulsho(dbgcxdef *ctx, uchar *buf)
293 {
294 lindef *lin;
295 int cur;
296 int dest;
297 uchar loc[LINLLNMAX];
298 char fnam[182];
299 int i;
300
301 /* search for a line source with this id */
302 for (lin = ctx->dbgcxlin ; lin ; lin = lin->linnxt)
303 if (lin->linid == *buf) break;
304 dbgswitch(&dbgsrclin, lin);
305 dbgsrclinhome = lin;
306
307 if (lin == 0)
308 {
309 osdbgclr(&dbgsrcwin);
310 osdbgpt(&dbgsrcwin, "*** source line not found ***\n");
311 return;
312 }
313
314 /* seek to line in file, then back up by half a screen to center line */
315 memcpy(loc, buf+1, lin->linlln);
316 linseek(lin, loc);
317 cur = (dbgsrcwin.oswy2 - dbgsrcwin.oswy1) / 2;
318 dest = 1;
319 dbgmove(lin, cur, &dest);
320 lintell(lin, dbgsrctop);
321 memcpy(dbgsrchome, dbgsrctop, (size_t)lin->linlln);
322 memcpy(dbgsrcloc, loc, (size_t)lin->linlln);
323 memcpy(dbgsrccsr, loc, (size_t)lin->linlln);
324 dbgsrcshow(lin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
325
326 /* update status line to show current file */
327 for (i = 0 ; i < 5 ; ++i) dbg_curstat[i] = (uchar)DBGUHLIN;
328
329 linnam(lin, fnam);
330 sprintf(&dbg_curstat[5], "%s ", fnam);
331 dbgwhere(ctx, &dbg_curstat[strlen(dbg_curstat)]);
332
333 for (i = strlen(dbg_curstat) ;
334 i <= dbgstawin.oswx2 - dbgstawin.oswx1 ; ++i)
335 dbg_curstat[i] = (uchar)DBGUHLIN;
336 dbg_curstat[i] = '\0';
337
338 /* actually display the status line now */
339 dbgustat(ctx);
340 }
341
342 /* display callback */
343 static void dbgudisp(void *ctx, const char *str, int strl)
344 {
345 oswdef *win = (oswdef *)ctx;
346 int oldpg;
347
348 oldpg = ossvpg(1); /* display values on debugger video page */
349 osdbgpt(win, "%.*s", strl, str);
350 ossvpg((char)oldpg); /* restore previous page */
351 }
352
353 /* display an error message */
354 void dbguerr(dbgcxdef *ctx, int errnum, char *msg)
355 {
356 int oldpg;
357
358 VARUSED(ctx);
359
360 oldpg = ossvpg((char)1); /* figure out what page we're on */
361 ossvpg((char)oldpg); /* restore previous page */
362 if (oldpg == 0) /* if we're on the game page... */
363 {
364 char buf[256];
365 sprintf(buf, "[TADS-%d: %s]\n", errnum, msg);
366 os_printz(buf);
367 }
368 else
369 osdbgpt(&dbgtxtwin, "TADS-%d: %s\n", errnum, msg);
370 }
371
372 /* show user screen, wait for key hit, then change back to dbg screen */
373 static void dbguuser(dbgcxdef *ctx)
374 {
375 VARUSED(ctx);
376
377 ossvpg(0);
378 os_waitc();
379 ossvpg(1);
380 }
381
382 /* execute a special keyboard command key */
383 static int dbgukbc(void *ctx0, char cmd)
384 {
385 dbgcxdef *ctx = (dbgcxdef *)ctx0;
386 int cur = dbgsrcwin.oswy2;
387 int dest = cur;
388 uchar newpos[LINLLNMAX];
389 int scrht = dbgsrcwin.oswy2 - dbgsrcwin.oswy1 - 2;
390
391 switch(cmd)
392 {
393 case CMD_F5: /* show user screen */
394 dbguuser(ctx);
395 return(0);
396
397 case CMD_TAB: /* other screen */
398 case CMD_F7: /* step into */
399 case CMD_F8: /* step over */
400 case CMD_F9: /* go */
401 case CMD_SCR: /* help */
402 return(cmd);
403 }
404
405 /* the remaining commands pertain to the source display only */
406 switch(cmd)
407 {
408 case CMD_CHOME:
409 if (dbgsrclinhome != 0 && dbgsrclin != 0
410 && (dbgsrctop != dbgsrchome || dbgsrclin != dbgsrclinhome))
411 {
412 dbgswitch(&dbgsrclin, dbgsrclinhome);
413
414 linseek(dbgsrclin, dbgsrchome);
415 memcpy(dbgsrctop, dbgsrchome, dbgsrclin->linlln);
416 memcpy(dbgsrccsr, dbgsrcloc, dbgsrclin->linlln);
417 dbgsrcshow(dbgsrclin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
418 }
419 break;
420
421 case CMD_TOP:
422 if (dbgsrctop != 0 && dbgsrclin != 0)
423 {
424 lingoto(dbgsrclin, LINGOTOP);
425 lintell(dbgsrclin, dbgsrccsr);
426 memcpy(dbgsrctop, dbgsrccsr, dbgsrclin->linlln);
427 dbgsrcshow(dbgsrclin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
428 }
429 break;
430
431 case CMD_BOT:
432 if (dbgsrclin != 0)
433 {
434 lingoto(dbgsrclin, LINGOEND);
435 dest -= scrht;
436 }
437 goto move_noseek;
438
439 case CMD_UP:
440 --dest;
441 goto move_source;
442
443 case CMD_DOWN:
444 ++dest;
445 goto move_source;
446
447 case CMD_PGUP:
448 dest -= scrht;
449 goto move_source;
450
451 case CMD_PGDN:
452 dest += scrht;
453
454 move_source:
455 if (dbgsrclin != 0)
456 linseek(dbgsrclin, dbgsrctop);
457
458 move_noseek:
459 if (dbgsrclin != 0)
460 {
461 dbgmove(dbgsrclin, cur, &dest);
462 lintell(dbgsrclin, newpos);
463 if (memcmp(dbgsrctop, newpos, dbgsrclin->linlln))
464 {
465 memcpy(dbgsrccsr, newpos, dbgsrclin->linlln);
466 memcpy(dbgsrctop, newpos, dbgsrclin->linlln);
467 dbgsrcshow(dbgsrclin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
468 }
469 }
470 break;
471 }
472 return(0);
473 }
474
475 /* get commands in source window */
476 static int dbgurunsrc(dbgcxdef *ctx)
477 {
478 int y;
479 char c;
480 oswdef *win = &dbgsrcwin;
481 uchar pos[LINLLNMAX];
482 uchar newpos[LINLLNMAX];
483 int dest;
484 int cur;
485 int scrht = win->oswy2 - win->oswy1 - 2;
486 char *cond;
487 char condbuf[128];
488
489 if (dbgsrclin == 0)
490 return 0;
491
492 /* figure out where we are in the source window */
493 find_srcpos:
494 linseek(dbgsrclin, dbgsrctop);
495 memcpy(pos, dbgsrctop, dbgsrclin->linlln);
496 for (y = win->oswy1 ; y < win->oswy2 ; ++y)
497 {
498 lintell(dbgsrclin, newpos);
499 if (!memcmp(dbgsrccsr, newpos, dbgsrclin->linlln)) break;
500 lingets(dbgsrclin, (uchar *)dbgbuf, (int)dbgbufsiz);
501 }
502
503 /* get and process commands */
504 for (;;)
505 {
506 ossdbgloc((char)y, (char)2); /* locate cursor properly */
507 cond = (char *)0; /* no condition for breakpoints, by default */
508
509 c = os_getc();
510 switch(c)
511 {
512 case '/':
513 return(CMD_UP);
514
515 case '\0':
516 c = os_getc();
517 switch(c)
518 {
519 case CMD_TAB:
520 return(0);
521
522 case CMD_CHOME:
523 if (dbgsrclinhome
524 && (memcmp(dbgsrctop, dbgsrchome, dbgsrclin->linlln)
525 || dbgsrclin != dbgsrclinhome))
526 {
527 dbgswitch(&dbgsrclin, dbgsrclinhome);
528
529 linseek(dbgsrclin, dbgsrchome);
530 memcpy(pos, dbgsrchome, dbgsrclin->linlln);
531 memcpy(dbgsrctop, dbgsrchome, dbgsrclin->linlln);
532 memcpy(dbgsrccsr, dbgsrcloc, dbgsrclin->linlln);
533 dbgsrcshow(dbgsrclin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
534 goto find_srcpos;
535 }
536 break;
537
538 case CMD_UP:
539 if (y > win->oswy1)
540 {
541 --y;
542 goto find_cur_y;
543 }
544 else
545 {
546 dest = 1;
547 cur = 2;
548 goto move_source;
549 }
550
551 case CMD_DOWN:
552 if (y < win->oswy2)
553 {
554 ++y;
555 goto find_cur_y;
556 }
557 else
558 {
559 dest = 2;
560 cur = 1;
561 goto move_source;
562 }
563
564 case CMD_PGUP:
565 cur = scrht;
566 dest = 1;
567 goto move_source;
568
569 case CMD_PGDN:
570 cur = 1;
571 dest = scrht;
572 move_source:
573 linseek(dbgsrclin, pos);
574 move_noseek:
575 dbgmove(dbgsrclin, cur, &dest);
576 lintell(dbgsrclin, newpos);
577 if (memcmp(pos, newpos, dbgsrclin->linlln))
578 {
579 int i;
580
581 memcpy(dbgsrctop, newpos, dbgsrclin->linlln);
582 memcpy(pos, newpos, dbgsrclin->linlln);
583 dbgsrcshow(dbgsrclin, win, pos, dbgsrcloc);
584
585 /* figure out where dbgsrccsr is */
586 find_cur_y:
587 linseek(dbgsrclin, pos);
588 for (i = win->oswy1 ; i < y ; ++i)
589 lingets(dbgsrclin, (uchar *)dbgbuf, (int)dbgbufsiz);
590 lintell(dbgsrclin, dbgsrccsr);
591 }
592 break;
593
594 case CMD_TOP:
595 if (pos != 0)
596 {
597 lingoto(dbgsrclin, LINGOTOP);
598 lintell(dbgsrclin, pos);
599 memcpy(dbgsrccsr, pos, dbgsrclin->linlln);
600 memcpy(dbgsrctop, pos, dbgsrclin->linlln);
601 y = win->oswy1;
602 dbgsrcshow(dbgsrclin, win, pos, dbgsrcloc);
603 }
604 break;
605
606 case CMD_BOT:
607 lingoto(dbgsrclin, LINGOEND);
608 dest = 1;
609 cur = scrht;
610 goto move_noseek;
611
612 case CMD_HOME:
613 y = win->oswy1;
614 goto find_cur_y;
615
616 case CMD_END:
617 y = win->oswy2;
618 goto find_cur_y;
619
620 case CMD_SF2:
621 if (dbgsrclin)
622 {
623 osdbgpt(&dbgtxtwin, "\nCondition: ");
624 while (osdbggts(&dbgtxtwin, condbuf, dbgukbc, ctx));
625 cond = condbuf;
626 /* FALLTHROUGH */
627 }
628 else break;
629
630 case CMD_F2:
631 if (dbgsrclin)
632 {
633 objnum objn;
634 uint ofs;
635 int newbp;
636 char nambuf[128];
637 int len;
638
639 linfind(dbgsrclin, (char *)dbgsrccsr, &objn, &ofs);
640
641 memcpy(nambuf, "<F2> ", (size_t)4);
642 len = dbgnam(ctx, nambuf+4, TOKSTOBJ, objn);
643 if (!memcmp(nambuf+4, "<UNKNOWN>", (size_t)len))
644 len = dbgnam(ctx, nambuf+4, TOKSTFUNC, objn);
645 if (cond)
646 {
647 memcpy(nambuf + 4 + len, " when ", (size_t)6);
648 strcpy(nambuf + 10 + len, cond);
649 }
650 else
651 nambuf[len + 4] = '\0';
652
653 (void)dbgbpat(ctx, objn, MCMONINV, ofs, &newbp, nambuf,
654 TRUE, cond, 0);
655 dbgugetbp(ctx);
656 }
657 break;
658
659 case CMD_F5:
660 dbguuser(ctx);
661 break;
662
663 case CMD_F7:
664 case CMD_F8:
665 case CMD_F9:
666 case CMD_EOF:
667 return(c);
668 }
669 }
670 }
671 }
672
673 /* callback for dbgugetp */
674 static void dbgubp(void *ctx0, int id, uchar *buf, uint bufsiz)
675 {
676 dbgubpdef *bp = &dbgbps[dbgbpcnt++];
677
678 bp->dbgubpid = id;
679 memcpy(bp->dbgubpseek, buf, (size_t)bufsiz);
680 }
681
682 /* reset private breakpoint data - call after setting/clearing bp */
683 static void dbgugetbp(dbgcxdef *ctx)
684 {
685 dbgbpcnt = 0;
686 dbgbpeach(ctx, dbgubp, ctx);
687 if (dbgsrclin != 0)
688 {
689 linseek(dbgsrclin, dbgsrctop);
690 dbgsrcshow(dbgsrclin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
691 }
692 }
693
694 /* output support for hidden output */
695 void trchid(void)
696 {
697 outflushn(1);
698 os_printz("---Hidden output---\n");
699 }
700 void trcsho(void)
701 {
702 outflushn(1);
703 os_printz("---End of hidden output, normal output resumes---\n");
704 }
705
706 /* update display of watchpoints */
707 static void dbguwxupd(dbgcxdef *ctx)
708 {
709 dbgwatwin.oswy = dbgwatwin.oswy1;
710 dbgwatwin.oswx = dbgwatwin.oswx1;
711 if (dbgwatwin.oswy1 <= dbgwatwin.oswy2) osdbgclr(&dbgwatwin);
712 dbgwxupd(ctx, dbgudisp, &dbgwatwin);
713 }
714
715 /* update window sizes to account for more/fewer watches */
716 static void dbguwxadd(dbgcxdef *ctx, int cnt)
717 {
718 /* move status line up by the number of new watch lines */
719 dbgstawin.oswy1 -= cnt;
720 dbgstawin.oswy2 -= cnt;
721
722 /* move bottom of code window up by the number of watch lines */
723 dbgsrcwin.oswy2 -= cnt;
724
725 /* move top of watch window up by the number of new watch lines */
726 dbgwatwin.oswy1 -= cnt;
727
728 /* update the status, source and watch windows */
729 dbgustat(ctx);
730 if (dbgsrclin)
731 {
732 linseek(dbgsrclin, dbgsrctop);
733 dbgsrcshow(dbgsrclin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
734 }
735 dbguwxupd(ctx);
736 }
737
738 /* flags whether a prompt is needed the next time we run command line */
739 static int needprompt = TRUE;
740
741 /* main command loop */
742 static void dbgucmd1(dbgcxdef *ctx)
743 {
744 char cmdbuf[128];
745 int err;
746 int disable;
747 int bpnum;
748 char *buf;
749 int cmd;
750 int i;
751 lindef *lin;
752 static int stay_in_src;
753 static int first_search;
754 static char lastsearch[128];
755 int level = ctx->dbgcxfcn - 1;
756
757 dbglget(ctx, (uchar *)cmdbuf); /* get information on current line */
758 dbgulsho(ctx, (uchar *)cmdbuf); /* display current line */
759 dbguwxupd(ctx); /* udpate watchpoint display */
760
761 for (;;)
762 {
763 if (stay_in_src)
764 goto run_other_window;
765 else
766 {
767 if (needprompt) osdbgpt(&dbgtxtwin, "tdb> ");
768 cmd = osdbggts(&dbgtxtwin, cmdbuf, dbgukbc, ctx);
769 }
770
771 process_cmd:
772 needprompt = (cmd == 0); /* ordinary command ==> prompt next time */
773 switch(cmd)
774 {
775 case 0:
776 for (buf = cmdbuf ; isspace((unsigned char)*buf) ; ++buf);
777 break;
778
779 case CMD_F7:
780 buf = "t";
781 break;
782
783 case CMD_F8:
784 buf = "p";
785 break;
786
787 case CMD_F9:
788 buf = "g";
789 break;
790
791 case CMD_TAB:
792 buf = "\t";
793 break;
794
795 case CMD_SCR:
796 buf = "?";
797 break;
798
799 default:
800 continue;
801 }
802
803 if (isupper((unsigned char)buf[0]))
804 buf[0] = tolower((unsigned char)buf[0]);
805 switch(buf[0])
806 {
807 case 'g':
808 ctx->dbgcxflg &= ~(DBGCXFSS + DBGCXFSO);
809 return;
810
811 case 't':
812 ctx->dbgcxflg |= DBGCXFSS;
813 ctx->dbgcxflg &= ~DBGCXFSO;
814 return;
815
816 case 'p':
817 ctx->dbgcxsof = ctx->dbgcxdep; /* step until back at this depth */
818 ctx->dbgcxflg |= (DBGCXFSS + DBGCXFSO);
819 return;
820
821 case 'e':
822 ossvpg(0); /* eval expression on game video page */
823 err = dbgeval(ctx, buf+1, dbgudisp, &dbgtxtwin, level, TRUE);
824 ossvpg(1); /* return to debugger video page */
825 if (err) errlog(ctx->dbgcxerr, err);
826 dbguwxupd(ctx); /* udpate watchpoint display */
827 break;
828
829 case 'c': /* Call history */
830 if (isupper((unsigned char)buf[1]))
831 buf[1] = tolower((unsigned char)buf[1]);
832 switch(buf[1])
833 {
834 case '+':
835 ctx->dbgcxhstf = 0;
836 ctx->dbgcxflg |= DBGCXFTRC;
837 osdbgpt(&dbgtxtwin, "Call tracing activated.\n");
838 break;
839
840 case '-':
841 ctx->dbgcxflg &= ~DBGCXFTRC;
842 osdbgpt(&dbgtxtwin, "Call tracing disactivated.\n");
843 break;
844
845 case 'c':
846 ctx->dbgcxhstf = 0;
847 osdbgpt(&dbgtxtwin, "Call trace log cleared.\n");
848 break;
849
850 case '\0':
851 case ' ':
852 {
853 char *p;
854 char *endp;
855
856 for (p = ctx->dbgcxhstp, endp = p + ctx->dbgcxhstf
857 ; p < endp ; p += strlen(p) + 1)
858 osdbgpt(&dbgtxtwin, "%s\n", p);
859 }
860 }
861 break;
862
863 case 'b':
864 if (isupper((unsigned char)buf[1]))
865 buf[1] = tolower((unsigned char)buf[1]);
866 switch(buf[1])
867 {
868 case 'p':
869 if (err = dbgbpset(ctx, buf+2, &bpnum))
870 errlog(ctx->dbgcxerr, err);
871 else
872 {
873 osdbgpt(&dbgtxtwin, "breakpoint %d set\n", bpnum);
874 dbgugetbp(ctx);
875 }
876 break;
877
878 case 'c':
879 if (err = dbgbpdel(ctx, atoi(buf + 2)))
880 errlog(ctx->dbgcxerr, err);
881 else
882 {
883 osdbgpt(&dbgtxtwin, "breakpoint cleared\n");
884 dbgugetbp(ctx);
885 }
886 break;
887
888 case 'd':
889 disable = TRUE;
890 goto bp_enable_or_disable;
891 case 'e':
892 disable = FALSE;
893 bp_enable_or_disable:
894 if (err = dbgbpdis(ctx, atoi(buf+2), disable))
895 errlog(ctx->dbgcxerr, err);
896 else
897 osdbgpt(&dbgtxtwin, "breakpoint %sabled\n",
898 disable ? "dis" : "en");
899 break;
900
901 case 'l':
902 dbgbplist(ctx, dbgudisp, &dbgtxtwin);
903 break;
904
905 default:
906 osdbgpt(&dbgtxtwin, "invalid breakpoint command\n");
907 break;
908 }
909 break;
910
911 case 'w':
912 if (isupper((unsigned char)buf[1]))
913 buf[1] = tolower((unsigned char)buf[1]);
914 switch(buf[1])
915 {
916 case 's':
917 if (err = dbgwxset(ctx, buf+2, &bpnum, level))
918 errlog(ctx->dbgcxerr, err);
919 else
920 dbguwxadd(ctx, 1); /* adjust windows for a new wx */
921 break;
922
923 case 'd':
924 if (err = dbgwxdel(ctx, atoi(buf + 2)))
925 errlog(ctx->dbgcxerr, err);
926 else
927 dbguwxadd(ctx, -1); /* adjust windows for one less wx */
928 break;
929
930 default:
931 osdbgpt(&dbgtxtwin, "invalid watch command\n");
932 break;
933 }
934 break;
935
936 case 'f':
937 if (isupper((unsigned char)buf[1]))
938 buf[1] = tolower((unsigned char)buf[1]);
939 switch(buf[1])
940 {
941 case 'l':
942 for (i = 1, lin = ctx->dbgcxlin ; lin ;
943 lin = lin->linnxt, ++i)
944 {
945 osdbgpt(&dbgtxtwin, "#%-2d %s", i,
946 ((linfdef *)lin)->linfnam);
947 osdbgpt(&dbgtxtwin, "\n");
948 }
949 break;
950
951 case 'v':
952 if (atoi(buf + 2) == 0)
953 {
954 osfildef *fp;
955
956 /* file by name - read the filename */
957 for (buf += 2 ; isspace((unsigned char)*buf) ; ++buf);
958 if (!(fp = osfoprb(buf, OSFTTEXT)))
959 osdbgpt(&dbgtxtwin, "file not found\n");
960 else
961 {
962 dbgswitch(&dbgsrclin, (lindef *)0);
963 *(buf-1) = '\0';
964 linfini2(ctx->dbgcxmem, (linfdef *)filesrc, buf-1,
965 (int)strlen(buf) + 2, fp, FALSE);
966 dbgsrclin = (lindef *)filesrc;
967 lintell(dbgsrclin, dbgsrctop);
968 lintell(dbgsrclin, dbgsrccsr);
969 dbgsrcshow(dbgsrclin, &dbgsrcwin, dbgsrctop,
970 dbgsrcloc);
971 }
972 break;
973 }
974
975 /* file by number - look up in line source list */
976 for (i = 1, lin = ctx->dbgcxlin ; lin ;
977 lin = lin->linnxt, ++i)
978 {
979 if (i == atoi(buf + 2)) break;
980 }
981
982 if (lin != 0)
983 {
984 dbgswitch(&dbgsrclin, lin);
985 lingoto(dbgsrclin, LINGOTOP);
986 lintell(dbgsrclin, dbgsrccsr);
987 memcpy(dbgsrctop, dbgsrccsr, dbgsrclin->linlln);
988 dbgsrcshow(lin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
989 }
990 else
991 osdbgpt(&dbgtxtwin, "invalid file number\n");
992 break;
993
994 default:
995 osdbgpt(&dbgtxtwin, "invalid file command\n");
996 break;
997 }
998 break;
999
1000 case '\0':
1001 break;
1002
1003 case 'q':
1004 if (strcmp(buf, "quit"))
1005 osdbgpt(&dbgtxtwin, "invalid debugger command\n");
1006 else
1007 {
1008 ossvpg(0); /* return to game page */
1009 errsig(ctx->dbgcxerr, ERR_RUNQUIT); /* signal QUIT */
1010 }
1011 break;
1012
1013 case 'k':
1014 dbgstktr(ctx, dbgudisp, &dbgtxtwin, level, FALSE, TRUE);
1015 break;
1016
1017 case 'h':
1018 if (!stricmp(buf, "help"))
1019 goto do_help;
1020 else
1021 {
1022 extern int dbghid;
1023
1024 dbghid = !dbghid;
1025 osdbgpt(&dbgtxtwin, "Hidden text will be %s.\n",
1026 dbghid ? "displayed" : "suppressed");
1027 }
1028 break;
1029
1030 case '\\':
1031 dbguuser(ctx);
1032 break;
1033
1034 case '/':
1035 if (dbgsrclin == 0)
1036 break;
1037
1038 ++buf; /* skip the '/' part */
1039 strcpy(lastsearch, buf);
1040 another_search:
1041 dbgusearch(dbgsrclin, dbgsrccsr, dbgsrctop,
1042 &dbgsrcwin, buf);
1043 needprompt = TRUE;
1044 goto run_other_window;
1045
1046 case '\t':
1047 /*
1048 * if there's no current source file, we can't switch to the
1049 * source window
1050 */
1051 if (dbgsrclin == 0)
1052 break;
1053
1054 /* turn off prompting while we're in the source window */
1055 needprompt = FALSE;
1056
1057 /* reset searching */
1058 first_search = TRUE;
1059
1060 /* get a command from the source window */
1061 run_other_window:
1062 cmd = dbgurunsrc(ctx);
1063 if (cmd == CMD_UP) /* special return for "search" */
1064 {
1065 if (first_search)
1066 {
1067 osdbgpt(&dbgtxtwin, "\n");
1068 first_search = FALSE;
1069 }
1070
1071 /* present default if there is one */
1072 if (lastsearch[0])
1073 osdbgpt(&dbgtxtwin, "search [%s]: ", lastsearch);
1074 else
1075 osdbgpt(&dbgtxtwin, "search: ");
1076
1077 /* ignore command keys from search call */
1078 while (osdbggts(&dbgtxtwin, cmdbuf, dbgukbc, ctx)) ;
1079
1080 /* apply default if appropriate */
1081 if (cmdbuf[0])
1082 {
1083 strcpy(lastsearch, (char *)cmdbuf);
1084 buf = cmdbuf;
1085 }
1086 else
1087 buf = lastsearch;
1088
1089 stay_in_src = TRUE;
1090 goto another_search;
1091 }
1092 if (cmd == 0)
1093 {
1094 stay_in_src = FALSE;
1095 continue;
1096 }
1097 stay_in_src = TRUE;
1098 goto process_cmd;
1099
1100 case 'u':
1101 if (level < ctx->dbgcxfcn - 1) ++level;
1102 osdbgpt(&dbgtxtwin, "evaluations are now at level %d\n", level+1);
1103 break;
1104
1105 case 'd':
1106 if (level > 0) --level;
1107 osdbgpt(&dbgtxtwin, "evaluations are now at level %d\n", level+1);
1108 break;
1109
1110 case '?':
1111 {
1112 char buf[128];
1113 osfildef *fp;
1114 do_help:
1115 if ( !os_locate("help.tdb", 8, ARG0, buf, sizeof(buf))
1116 || !(fp = osfoprb(buf, OSFTTEXT)))
1117 osdbgpt(&dbgtxtwin, "HELP.TDB not found\n");
1118 else
1119 {
1120 dbgswitch(&dbgsrclin, (lindef *)0);
1121 linfini2(ctx->dbgcxmem, (linfdef *)filesrc,
1122 buf, (int)strlen(buf), fp, FALSE);
1123 dbgsrclin = (lindef *)filesrc;
1124 lintell(dbgsrclin, dbgsrctop);
1125 lintell(dbgsrclin, dbgsrccsr);
1126 dbgsrcshow(dbgsrclin, &dbgsrcwin, dbgsrctop, dbgsrcloc);
1127 }
1128 }
1129 break;
1130
1131 default:
1132 osdbgpt(&dbgtxtwin, "invalid debugger command\n");
1133 break;
1134 }
1135 }
1136 }
1137
1138 void dbgucmd(dbgcxdef *ctx, int bphit, int err, unsigned int *exec_ofs)
1139 {
1140 VARUSED(exec_ofs);
1141
1142 ossvpg(1); /* change to debugger's video page */
1143
1144 /* display cause of breakpoint if not single-stepping */
1145 if (err || bphit)
1146 {
1147 if (!needprompt) osdbgpt(&dbgtxtwin, "\n");
1148 if (err)
1149 errprelog(ctx->dbgcxerr, err);
1150 else if (bphit)
1151 osdbgpt(&dbgtxtwin, "Stop at breakpoint %d\n", bphit);
1152 needprompt = TRUE;
1153 }
1154
1155 dbgucmd1(ctx); /* run the normal main loop */
1156 ossvpg(0); /* change back to default video page */
1157 }
1158
1159 /*
1160 * initialization phase one (called before the .GAM file is loaded)
1161 */
1162 void dbguini(dbgcxdef *ctx, const char *game_filename)
1163 {
1164 char buf[150];
1165 int i;
1166 extern int sdesc_color;
1167 extern int text_color;
1168 int ldesc_color = 112;
1169 int watch_color = 0x47;
1170 extern int usebios;
1171 int maxline;
1172 int maxcol;
1173 #ifdef MSOS2
1174 int divline;
1175 #endif
1176
1177 ossgmx(&maxline, &maxcol);
1178 osdbgini(maxline+1, maxcol+1);
1179
1180 usebios = 0;
1181
1182 ossvpg(1);
1183 ossclr(0, 0, maxline, maxcol, 7);
1184
1185 if (ossmon())
1186 {
1187 ldesc_color = 7;
1188 dbgcurcolor = 10;
1189 dbgbpcolor = 7;
1190 watch_color = 10;
1191 }
1192
1193 /* initialize windows */
1194 #ifdef MSOS2
1195 divline = maxline * 2 / 3;
1196 osdbgwop(&dbgsrcwin, 0, 0, maxcol, divline-1, sdesc_color);
1197 osdbgwop(&dbgstawin, 0, divline, maxcol, divline, ldesc_color);
1198 osdbgwop(&dbgtxtwin, 0, divline+1, maxcol, maxline, text_color);
1199 osdbgwop(&dbgwatwin, 0, divline+1, maxcol, divline, watch_color);
1200 #else /* !MSOS2 */
1201 osdbgwop(&dbgsrcwin, 0, 0, 79, 15, sdesc_color);
1202 osdbgwop(&dbgstawin, 0, 16, 79, 16, ldesc_color);
1203 osdbgwop(&dbgtxtwin, 0, 17, 79, 24, text_color);
1204 osdbgwop(&dbgwatwin, 0, 17, 79, 16, watch_color);
1205 #endif /* MSOS2 */
1206 dbgsrcwin.oswflg = OSWFCLIP;
1207 dbgstawin.oswflg = OSWFCLIP;
1208 dbgwatwin.oswflg = OSWFCLIP;
1209 dbgtxtwin.oswflg = OSWFMORE;
1210
1211 /* display status line with bar */
1212 for (i = 0 ; i <= maxcol && i+1 < sizeof(buf) ; ++i)
1213 buf[i] = (uchar)DBGUHLIN;
1214 buf[i] = '\0';
1215 osdbgpt(&dbgstawin, buf);
1216
1217 ossvpg(0);
1218
1219 /* allocate buffer */
1220 dbgbuf = (char *)malloc(dbgbufsiz = 4096);
1221 }
1222
1223 /*
1224 * initialization phase two (called after the .GAM file is loaded)
1225 */
1226 void dbguini2(dbgcxdef *ctx)
1227 {
1228 /* we don't need to do any more setup after loading the .GAM file */
1229 }
1230
1231 /* terminate the UI */
1232 void dbguterm(dbgcxdef *ctx)
1233 {
1234 /* there's nothing extra we need to do here */
1235 }
1236
1237 /*
1238 * Determine if we can resume from an error. This UI doesn't allow the
1239 * user to change the instruction pointer, so we can't resume from an
1240 * error.
1241 */
1242 int dbgu_err_resume(dbgcxdef *ctx)
1243 {
1244 VARUSED(ctx);
1245 return FALSE;
1246 }
1247
1248 /*
1249 * Quitting - do nothing; let the system terminate
1250 */
1251 void dbguquitting(dbgcxdef *ctx)
1252 {
1253 VARUSED(ctx);
1254 }
1255
1256 /*
1257 * Locate a source file using UI-dependent mechanisms. We don't have
1258 * anything more we can do beyond what the core debugger does, so we'll
1259 * simply return a failure indication.
1260 */
1261 int dbgu_find_src(const char *origname, int origlen,
1262 char *fullname, size_t full_len, int must_find_file)
1263 {
1264 VARUSED(origname);
1265 VARUSED(origlen);
1266 VARUSED(fullname);
1267 VARUSED(full_len);
1268 VARUSED(must_find_file);
1269
1270 /* indicate failure */
1271 return FALSE;
1272 }