summaryrefslogtreecommitdiff
path: root/xc/unsupported/test/InsPEX/tools/newt/debugger.c
blob: 019d21aad89ae21e426553d12b33e786f261caf2 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810


/* $XConsortium: debugger.c,v 5.3 91/07/01 09:04:44 rws Exp $ */

/*****************************************************************

Copyright (c) 1989,1990, 1991  X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.

Copyright (c) 1989,1990, 1991 by Sun Microsystems, Inc.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Sun Microsystems,
and the X Consortium, not be used in advertising or publicity 
pertaining to distribution of the software without specific, written 
prior permission.  

SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/



/*
 * debugger.c:  the debugger module of NewT
 */

#ifdef SYSV 
#define index strchr
#define rindex strrchr
#include <sys/types.h>
#include <string.h>
#else
#include <strings.h>
#endif

#include "sys/wait.h"
#include "sys/file.h"
#include "signal.h"
#include "looper.h"
#include "debugger.h"

#define MAXSCRATCH 100000	/* max scratch space for storing file images */
#define MAXCHAR 256		/* max char in a line */
#define DEFINC 10		/* default number of lines to list */
#define RUNAGAIN 255		/* to signal a re-run in the middle of a run*/
#define RUNDONE 254		/* to signal end of run */
#define RUNQUIT 253		/* to signal quit */
#define HASHSIZE 97		/* hash table size */

#define eqstr(a,b)  !strcmp((a),(b)) 	/* compare text string */

struct filestrlst {
	FILESTR *filestr;	/* debug structure */
	struct filestrlst *next;/* pointer to next entry */
};

typedef struct filestrlst FILESTRLST;	

FILESTR *N_fstr;		/* current debugging structure */

struct breakstrlst {
	int eventno;		/* event number */
	int lineno;		/* line number */
	FILESTR *filestr;	/* debug structure */
	struct breakstrlst *next;
};

typedef struct breakstrlst BREAKSTRLST;

struct aliasstr {                                               
        char *name;  	   	/* alias name */
        char *actualname;       /* actual name */
        struct aliasstr *next;
};

typedef struct aliasstr ALIASSTR;/* alias structure */
static ALIASSTR *hashtbl[HASHSIZE];/* pointer alias hash table */


static char *	emalloc();
static fatal();
static char *strsave();
char *	calloc();

static FILESTRLST flst = {NULL,NULL}; /* list of debugging structure */
static BREAKSTRLST blst= {0,0,NULL,NULL};  /* list of break structure */

static char    scratch[MAXSCRATCH]; /* scratch space for file images */
static char    *scratchlimit = scratch + MAXSCRATCH - MAXCHAR;
				/* used for boundary check */

int N_resumeat;			/* the line to continue at */
int N_isskip;			/* 1 if user has issued 'cont at ...' command */
int N_isstep;			/* 1 if user has issued 'step' command */
int N_isnext;			/* 1 if user has issued 'next' command */
int N_stepcount;		/* number of 'step' commands */
int N_nextcount;		/* number of 'next' commands */
static int isrunning = 0;	/* 1 if script is running */
static int pfd[2];		/* use to pipe back userbreakpts */
static int pid,cid;		/* parent and child process ids */
static int debugstrnum=0;	/* number of debug structure */
static int breakstrnum=0;	/* number of break structure */
static int curevent=1;		/* current event number of break structure */


/*
 * To initialize debug structure of a file if one doesn't already exist, 
 * or to retrieve the debug structure of this file.
 * The current debug structure is pointed to by "N_fstr".
 */
N_debuguse(filename)
	char *filename;
{
	static char* scratchptr=scratch;
	FILE *fp;
	int inc,i,found;
	FILESTRLST *fl;

	/* see if the debug structure of this file exist */
	for (fl = &flst,found=0;fl->next != NULL && !found; fl= fl->next) {
		if (eqstr(fl->next->filestr->name,filename)) {
			N_fstr = fl->next->filestr;
			found = 1;
		}
	}
	/* if not, create one for this file */
	/* allocate storage for userbreakpts and fileimage */
	/* and read in file image */
	if (!found) {
		fl->next = (FILESTRLST*) emalloc(sizeof(FILESTRLST));
		fl->next->filestr = (FILESTR*) emalloc(sizeof(FILESTR));
		fl->next->filestr->name = filename;
		fl->next->next = NULL;
		N_fstr = fl->next->filestr;
		/* it can be a macro file or the source file */
		if (!(fp=fopen(filename,"r"))) /* it is a macro file */
			if (!(fp=N_fopen_from_path(filename,"r",N_filepath))) 
				fatal("can't open file %s\n",filename);
		for(i=0;;++i) {
			if (scratchptr >= scratchlimit) 
				fatal("run out of scratch space",0);
			N_fstr->fileimage[i] =  fgets(scratchptr,MAXCHAR,fp);
			if (N_fstr->fileimage[i] == NULL)  /* EOF or error */
				break;
			inc = strlen(N_fstr->fileimage[i]) + 1;
			scratchptr += inc;
		}
		N_fstr->maxline = i;
		fclose(fp);
		debugstrnum++;
	}
}

/* 
 * debugger command main loop
 * to dispatch command issue by the user during a debugging session
 */
N_debugger(currentln)
	int currentln;
{
#ifdef SYSV
#define R_OK 4
#endif
	int i;
	char ln[MAXCHAR],*lnptr,*homedir;
	char cmd[MAXCHAR],c;
	ALIASSTR *ap,*Lookup();
	int a1,a2,tmpln;
	int n;
        static int firsttime = 1;
        int receive();
 
        if (firsttime) {
                firsttime = 0;
                signal(SIGUSR1,receive);
                pid = getpid();
		if (access(".NewTdbinit",R_OK) == 0) {
			readalias(".NewTdbinit");
		} else if (homedir = (char*) getenv("HOME")) {
			sprintf(ln,"%s/.NewTdbinit",homedir);
		      	if (access(ln,R_OK) == 0) 
				readalias(ln);
		}
        }
	N_isskip = N_isstep = N_isnext = 0;
	for (;;) {
		printf("Debugger> ");
		if (!fgets(ln,MAXCHAR,stdin))  /* EOF or Error */
			exit(0);
		n = sscanf(ln,"%s",cmd);
		if (n == -1)
			continue;
		if (ap = Lookup(cmd)) {
			aliasing(cmd,ap->actualname,ln);
			n = sscanf(ln,"%s",cmd);
			if (n == -1)
				continue;
		}
		if (eqstr(cmd,"list")) {
		    n = sscanf(ln,"%s %d %1s %d",cmd,&a1,&c,&a2);
		    switch (n) {
		    case 1:  /* default list DEFINC lines */
			if (currentln > N_fstr->maxline)
				lister(currentln,currentln);
			else {
			    if ( (tmpln = currentln+DEFINC-1) > N_fstr->maxline)
				tmpln = N_fstr->maxline;
			    lister(currentln,tmpln);
			    currentln = tmpln+1;
			}
			break;
		    case 2:  /* list the nth lines */
			lister(a1,a1);
			currentln = a1+1;
			break;
		    case 4:  /* list a1 to a2 lines */
			if (c == ',') {
			    lister(a1,a2);
			    currentln = a2+1;
			    break;
			}
		    default:
			printf("Debugger: invalid syntax\n");
		    }
		} else if (eqstr(cmd,"step")) {
		    if (!isrunning) 
		        puts("Debugger: can't continue");
		    else {
                        n = sscanf(ln,"%s %d",cmd,&a1);
                        switch (n) {
                        case 1:
			    N_isstep = 1;
			    N_stepcount = 1;
			    return;
                        case 2:
                            N_isstep = 1;
                            N_stepcount = a1;
			    return;
                        default:
                            puts("Debugger: invalid syntax");
                        }
                    }    
		} else if (eqstr(cmd,"next")) {
                    if (!isrunning)
                        puts("Debugger: can't continue");
                    else {  
                        n = sscanf(ln,"%s %d",cmd,&a1);
                        switch (n) {
                        case 1:
                            N_isnext = 1;
                            N_nextcount = 1;
                            return;
                        case 2:
                            N_isnext = 1;
                            N_nextcount = a1;
                            return;
                        default:
                            puts("Debugger: invalid syntax");
                        }   
                    }    
		} else if (eqstr(cmd,"cont")) {
		    if (!isrunning) 
		        puts("Debugger: can't continue");
		    else {
		        n = sscanf(ln,"%s %s %d",cmd,cmd,&a1);
		        switch (n) {
		        case 1:  
			    return;
		        case 3:  
			    if (eqstr(cmd,"at")) {
				if (N_getflag(N_fstr->breakablept,a1)) {
			            N_isskip = 1;
			            N_resumeat = a1;
		                    return;
				} else {
			    	    printf("Debugger: no executable code at line %d\n",a1);
				    break;
				}
			    }
		        default:
			    puts("Debugger: invalid syntax");
			}
		    }
		} else if (eqstr(cmd,"stop")) {
		    n = sscanf(ln,"%s %s %d",cmd,cmd,&a1);
		    switch (n) {
		    case 3:  
			if (eqstr(cmd,"at")) {
			    breakat(a1);
			    break;
			}
		    default:
			printf("Debugger: invalid syntax\n");
		    }
		} else if (eqstr(cmd,"call")) {
			lnptr = index(ln,'c');
			N_interpret(lnptr+4,NULL); /* skip over "call" */
		} else if (eqstr(cmd,"delete")) {
		    n = sscanf(ln,"%s %d",cmd,&a1);
                    switch (n) {
		    case 1:
                        n = sscanf(ln,"%s %s",cmd,cmd);
			if (n == 2 && eqstr(cmd,"all")) 
			    deleteall();
			else
			    printf("Debugger: invalid syntax\n");
			break;
                    case 2:
		    {
	                int cont = 1;

			delete(a1);
			lnptr = index(ln,'d') + 6; /* skip over "delete" */
			while (cont) {
                            n = sscanf(lnptr,"%d %1s %d",&a1,&c,&a2);
			    switch (n) {
			    case 1:
				cont=0;
				break;
			    case 3:
				if (c == ',') {
				    delete(a2);
				    lnptr = index(lnptr,',') + 1;
				    break;
				}
			    default:
				cont=0;
			        printf("Debugger: invalid syntax\n");
			    }
			}
		    }
			break;
                    default:
                        printf("Debugger: invalid syntax\n");
                    }
		} else if (eqstr(cmd,"status")) {
		    inquire();
		} else if (eqstr(cmd,"run")) {
		    run();
		} else if (eqstr(cmd,"quit")) {
			exit(RUNQUIT);
		} else if (eqstr(cmd,"alias")) {
			lnptr = index(ln,'a') + 5;
                        if (sscanf(lnptr,"%s",cmd) == 1) {
				lnptr = index(lnptr,cmd[0]) + strlen(cmd);
				install (cmd,lnptr);
			} else
                        	printf("Debugger: invalid syntax\n");
		} else {
			puts("Debugger: invalid command");
		}
	}
}

/*
 * Start execution of looper script
 */
static run()
{
	int done=0;
#if defined(SYSV) || defined(__convex__)
	int childstatus;
#else
	union wait childstatus;
#endif

	if (N_2process) {
	    if (isrunning) {
		sendback();
		exit(RUNAGAIN);
	    }
	    while (!done) {
                if (pipe(pfd) == -1)
                        fatal("pipe system call",0);
		if (fork()) { /* parent */
                        if (close(pfd[1]) == -1)
                                fatal("close system call 1",0);
			wait (&childstatus);
                        if (close(pfd[0]) == -1)
                                fatal("close system call 2",0);
#if defined(SYSV) || defined(__convex__)
#ifndef WEXITSTATUS
#define WEXITSTATUS(s) ( ((s)>>8) & 0377 )
#endif
#else
#define WEXITSTATUS(s) ((s).w_retcode) 
#endif
			switch(WEXITSTATUS(childstatus)) {
			case RUNQUIT:
			    	exit(0);
			case RUNAGAIN:
			    	break;
			case RUNDONE:
			    	done = 1;
			    	break;
			default:
				printf("Debugger: program died\n");
				done = 1;
				break;
			}
		 } else { /* child */
                        if (close(pfd[0]) == -1)
                                fatal("close system call 3",0);
			isrunning = 1;
                        cid = getpid();
			N_execute(N_funchead->next,EXECUTE_NORMAL);
			sendback();
			puts("Debugger: execution completed");
			exit(RUNDONE);
		 }
	    }
	} else {
	    	isrunning = 1;
		N_execute(N_funchead->next,EXECUTE_NORMAL);
	    	isrunning = 0;
	}
}

/* 
 * send back userbreakpts array(for all files) and break event list to 
 * the parent process, notify its parent by sending a SIGUSR1 signal
 */
static sendback()
{
        FILESTRLST *fl;
	BREAKSTRLST *bl;
	int i;
 
	/* send back userbreakpt for all files */
        for (fl = flst.next,i=0;i<debugstrnum; ++i,fl= fl->next) {
                if (write(pfd[1],(char*)fl->filestr->userbreakpt,MAXBIT) != MAXBIT)
			fatal("write system call 1",0);
	}

	/* send back number of break structure */
        if (write(pfd[1],(char*)&breakstrnum,sizeof(int)) != sizeof(int))
			fatal("write system call 2",0);

	/* send back current event number of break structure */
        if (write(pfd[1],(char*)&curevent,sizeof(int)) != sizeof(int))
			fatal("write system call 3",0);

	/* send back break structures */
        for (bl = blst.next,i=0;i<breakstrnum; ++i,bl= bl->next) {
                if (write(pfd[1],(char*)bl,sizeof(BREAKSTRLST)) != sizeof(BREAKSTRLST))
			fatal("write system call 4",0);
	}
        if (kill(pid,SIGUSR1) == -1)
		fatal("kill system call 1",0);
}

 
/*
 * parent process receive a userbreakpts array for all files and break
 * event list upon receiving a SIGUSR1 signal from the child
 */
static receive()
{
	FILESTRLST *fl;	/* pointer to debug structure list */
	BREAKSTRLST *bl,*btrace,*bstr;
	int i;

	/* receive userbreakpt for all files */
        for (fl=flst.next,i=0;i<debugstrnum;++i,fl=fl->next) {
		if (read(pfd[0],(char*)fl->filestr->userbreakpt,MAXBIT)!= MAXBIT)
			fatal("read system call 1",0);
	}

	/* delete invalid break structures */
	freebreakstrlst(&blst);

	/* receive back number of break structure */
	if (read(pfd[0],(char*)&breakstrnum,sizeof(int))!= sizeof(int))
		fatal("read system call 2",0);

	/* receive current event number of break structure */
	if (read(pfd[0],(char*)&curevent,sizeof(int))!= sizeof(int))
		fatal("read system call 3",0);

	/* receive and rebuilt break structures */
        for (btrace= &blst,i=0;i<breakstrnum; ++i,btrace= btrace->next) {
		bstr = (BREAKSTRLST*) emalloc(sizeof(BREAKSTRLST));
                if (read(pfd[0],(char*)bstr,sizeof(BREAKSTRLST)) != sizeof(BREAKSTRLST))
			fatal("read system call 4",0);
		btrace->next = bstr;
	}
        signal(SIGUSR1,receive);
}

/*
 * recursively freeing break structure list
 */
static freebreakstrlst(bl)
	BREAKSTRLST *bl;
{
	if (bl->next != NULL) {
		freebreakstrlst(bl->next);
		free(bl->next);
	}
}

/*
 * debug command "list"
 */
static lister(start,end)
	int start,end;
{
	int i;

        if (end > N_fstr->maxline) 
                printf("Debugger: %s has only %d lines\n",N_fstr->name,N_fstr->maxline);
	else if ( start <= 0 || start > end)
                printf("Debugger: invalid argument\n");
        else
		for (i=start-1;N_fstr->fileimage[i] != NULL && i <end ; ++i) 
			printf("	%d %s",i+1,N_fstr->fileimage[i]);
}

/*
 * debug command "stop"
 */
static breakat(line)
	int line;
{
	BREAKSTRLST *bstr;

	if (line > N_fstr->maxline ) 
                printf("Debugger: %s has only %d lines\n",N_fstr->name,N_fstr->maxline);
	else if (!N_getflag(N_fstr->breakablept,line))
		printf("Debugger: no executable code at line %d\n",line);
	else if (N_getflag(N_fstr->userbreakpt,line))
		printf("Debugger: this break point has been registered\n"); 
	else { 
		N_setflag(N_fstr->userbreakpt,line);
		bstr = (BREAKSTRLST*) emalloc(sizeof(BREAKSTRLST));
		bstr->eventno = curevent++;
		bstr->lineno = line;
		bstr->filestr = N_fstr;
		bstr->next = blst.next;
		blst.next = bstr;
		breakstrnum++;
		printf("Debugger: (%d) stop at %s:%d\n",bstr->eventno,N_fstr->name,line);
	}
}

/* 
 * set the flag corresponding to "line" in "str" (userbreakpt or breakablept)
 */
N_setflag(str,line)
	char *str;
	int line;
{
        int offset,idx;
        unsigned char mask;

	idx = (line-1)/8;
        offset = (line-1)%8;
        mask = 1 << offset;
        str[idx] |= mask;
}

/*
 * clear the flag corresponding to "line" in "str" (userbreakpt or breakablept)
 */
static clearflag(str,line)
        char *str; 
        int line; 
{
        int offset,idx; 
        unsigned char mask; 
 
        idx = (line-1)/8; 
        offset = (line-1)%8;
        mask = ~(1 << offset);
        str[idx] &= mask;
}


/*
 * get the flag corresponding to "line" in "str" (userbreakpt or breakablept)
 */
N_getflag(str,line)
        char *str; 
        int line; 
{
        int offset,idx; 
        unsigned char mask; 
 
        idx = (line-1)/8; 
        offset = (line-1)%8;
        mask = 1 << offset;
        return((int)str[idx] & mask);
}

/*
 * debug command "delete"
 */
static delete(event)
        int event;
{
	BREAKSTRLST *bl,*btrace;
	int found = 0;

	for (btrace= &blst,bl=btrace->next;bl!=NULL&&!found;btrace=bl,bl=bl->next) {
		if (bl->eventno == event) {
			btrace->next = bl->next;
			free(bl);
                	clearflag(bl->filestr->userbreakpt,bl->lineno);
			found = 1;
		}
	}
	if (!found)
                printf("Debugger: no such event\n");
}


/*
 * debug command "delete all"
 */
static deleteall()
{
        BREAKSTRLST *bl,*btrace;
        int found = 0;

        for (btrace= &blst,bl=btrace->next;bl!=NULL;btrace=bl,bl=bl->next) {
        	free(bl);
                clearflag(bl->filestr->userbreakpt,bl->lineno);
        }
}                 
 

/*
 * debug command "status" 
 */
static inquire()
{
	BREAKSTRLST *bl;

	for (bl=blst.next;bl!=NULL;bl=bl->next) {
		printf("Debugger: (%d) stop at %s:%d\n",bl->eventno,bl->filestr->name,bl->lineno);
	}
}

/*
 * print fatal error message and exit
 */
static
fatal(s,t)
        char *s,*t;
{
        fprintf(stderr,"Debugger error: ");
        fprintf(stderr,s,t);
        puts("");
	fflush(stdout);
	fflush(stderr);
        exit(1);
}

/*
 * process .NewTdbinit
 */
static readalias(filename)
	char *filename;
{
	FILE *fp;
	char ln[MAXCHAR],cmd[MAXCHAR],*lnptr;
	int n;

	if (!(fp=fopen(filename,"r")))
			fatal("can't open file %s\n",filename);
	while (fgets(ln,MAXCHAR,fp)) {
		n = sscanf(ln,"%s",cmd);
		if (n == -1)
			continue;
		if (eqstr(cmd,"alias")) {
			lnptr = index(ln,'a') + 5;
                        if (sscanf(lnptr,"%s",cmd) == 1) {
				lnptr = index(lnptr,cmd[0]) + strlen(cmd);
				install (cmd,lnptr);
			}
		} else
			printf("Debugger: invalid command %s in file %s\n",cmd,filename);
	}
}

/*
 * remake input line for alias processing
 */
aliasing(name,actualname,ln)
	char *name,*actualname,*ln;
{
	char tmpln[MAXCHAR],*ptr;

	strcpy(tmpln,ln);
	ptr = index(tmpln,name[0]) + strlen(name);
	sprintf(ln,"%s %s",actualname,ptr);
}

/*
 * Retrieve hash value
 */
static
hash(s)
        char *s;
{
        int hashval;

        for (hashval = 0; *s != '\0'; )
                hashval += *s++;
        return(hashval % HASHSIZE);
}

static
install(name,actualname)
        char *name,*actualname;
{
        ALIASSTR *ap, *Lookup();
        int hashval;

        if (ap=Lookup(name))   {
		free(ap->name);
		free(ap->actualname);
                ap->name = strsave(name);
                ap->actualname = strsave(actualname);
	} else {
                ap = (ALIASSTR *) emalloc(sizeof(ALIASSTR));
                ap->name = strsave(name);
                ap->actualname = strsave(actualname);
                hashval = hash(name);
                ap->next = hashtbl[hashval];
                hashtbl[hashval] = ap;
        }
}

/*
 * Retrieve an entry from the alias hash tbl.
 */
static ALIASSTR *
Lookup(name)
        char *name;
{
        ALIASSTR *ap;
         
        for (ap = hashtbl[ hash(name) ]; ap != NULL; ap = ap->next)
                if ( eqstr(name, ap->name) )
                        return(ap); /* found it */
        return((ALIASSTR*)NULL); /* not found */
}


/*
 * Copy and save a string.
 */
static char *
strsave(s)
        char *s;
{
        char *p;

        p = emalloc(strlen(s)+1);
        strcpy(p,s);
        return(p);
}

/*
 * Allocate memory.
 */
static char *
emalloc(size)
        int size;
{
        char *p;

        if (! (p=calloc(size,sizeof(char))))
                fatal("calloc() failed");
        return(p);
}