allow for APPLE MACH compilation
[fw/sdcc] / debugger / mcs51 / cmd.c
1 /*-------------------------------------------------------------------------
2     cmd.c - source  file for debugger command execution
3
4               Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19    
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!  
23 -------------------------------------------------------------------------*/
24
25 #include "sdcdb.h"
26 #include "symtab.h"
27 #include "simi.h"
28 #include "break.h"
29 #include "cmd.h"
30
31 int listLines = 10;
32 EXTERN_STACK_DCL(callStack,function *,1024);
33
34 #if defined(__APPLE__) && defined(__MACH__)
35 static char *copying=
36 {"                   GNU GENERAL PUBLIC LICENSE Version 2"};
37 #else
38 static char *copying=
39 "                   GNU GENERAL PUBLIC LICENSE
40                        Version 2, June 1991
41
42  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
43  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
44  Everyone is permitted to copy and distribute verbatim copies
45  of this license document, but changing it is not allowed.
46
47                             Preamble
48
49   The licenses for most software are designed to take away your
50 freedom to share and change it.  By contrast, the GNU General Public
51 License is intended to guarantee your freedom to share and change free
52 software--to make sure the software is free for all its users.  This
53 General Public License applies to most of the Free Software
54 Foundation's software and to any other program whose authors commit to
55 using it.  (Some other Free Software Foundation software is covered by
56 the GNU Library General Public License instead.)  You can apply it to
57 your programs, too.
58
59   When we speak of free software, we are referring to freedom, not
60 price.  Our General Public Licenses are designed to make sure that you
61 have the freedom to distribute copies of free software (and charge for
62 this service if you wish), that you receive source code or can get it
63 if you want it, that you can change the software or use pieces of it
64 in new free programs; and that you know you can do these things.
65
66   To protect your rights, we need to make restrictions that forbid
67 anyone to deny you these rights or to ask you to surrender the rights.
68 These restrictions translate to certain responsibilities for you if you
69 distribute copies of the software, or if you modify it.
70
71   For example, if you distribute copies of such a program, whether
72 gratis or for a fee, you must give the recipients all the rights that
73 you have.  You must make sure that they, too, receive or can get the
74 source code.  And you must show them these terms so they know their
75 rights.
76
77   We protect your rights with two steps: (1) copyright the software, and
78 (2) offer you this license which gives you legal permission to copy,
79 distribute and/or modify the software.
80
81   Also, for each author's protection and ours, we want to make certain
82 that everyone understands that there is no warranty for this free
83 software.  If the software is modified by someone else and passed on, we
84 want its recipients to know that what they have is not the original, so
85 that any problems introduced by others will not reflect on the original
86 authors' reputations.
87
88   Finally, any free program is threatened constantly by software
89 patents.  We wish to avoid the danger that redistributors of a free
90 program will individually obtain patent licenses, in effect making the
91 program proprietary.  To prevent this, we have made it clear that any
92 patent must be licensed for everyone's free use or not licensed at all.
93
94   The precise terms and conditions for copying, distribution and
95 modification follow.
96 ^L
97                     GNU GENERAL PUBLIC LICENSE
98    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
99
100   0. This License applies to any program or other work which contains
101 a notice placed by the copyright holder saying it may be distributed
102 under the terms of this General Public License.  The \"Program\", below,
103 refers to any such program or work, and a \"work based on the Program\"
104 means either the Program or any derivative work under copyright law:
105 that is to say, a work containing the Program or a portion of it,
106 either verbatim or with modifications and/or translated into another
107 language.  (Hereinafter, translation is included without limitation in
108 the term \"modification\".)  Each licensee is addressed as \"you\".
109
110 Activities other than copying, distribution and modification are not
111 covered by this License; they are outside its scope.  The act of
112 running the Program is not restricted, and the output from the Program
113 is covered only if its contents constitute a work based on the
114 Program (independent of having been made by running the Program).
115 Whether that is true depends on what the Program does.
116
117   1. You may copy and distribute verbatim copies of the Program's
118 source code as you receive it, in any medium, provided that you
119 conspicuously and appropriately publish on each copy an appropriate
120 copyright notice and disclaimer of warranty; keep intact all the
121 notices that refer to this License and to the absence of any warranty;
122 and give any other recipients of the Program a copy of this License
123 along with the Program.
124
125 You may charge a fee for the physical act of transferring a copy, and
126 you may at your option offer warranty protection in exchange for a fee.
127
128   2. You may modify your copy or copies of the Program or any portion
129 of it, thus forming a work based on the Program, and copy and
130 distribute such modifications or work under the terms of Section 1
131 above, provided that you also meet all of these conditions:
132
133     a) You must cause the modified files to carry prominent notices
134     stating that you changed the files and the date of any change.
135
136     b) You must cause any work that you distribute or publish, that in
137     whole or in part contains or is derived from the Program or any
138     part thereof, to be licensed as a whole at no charge to all third
139     parties under the terms of this License.
140
141     c) If the modified program normally reads commands interactively
142     when run, you must cause it, when started running for such
143     interactive use in the most ordinary way, to print or display an
144     announcement including an appropriate copyright notice and a
145     notice that there is no warranty (or else, saying that you provide
146     a warranty) and that users may redistribute the program under
147     these conditions, and telling the user how to view a copy of this
148     License.  (Exception: if the Program itself is interactive but
149     does not normally print such an announcement, your work based on
150     the Program is not required to print an announcement.)
151
152 These requirements apply to the modified work as a whole.  If
153 identifiable sections of that work are not derived from the Program,
154 and can be reasonably considered independent and separate works in
155 themselves, then this License, and its terms, do not apply to those
156 sections when you distribute them as separate works.  But when you
157 distribute the same sections as part of a whole which is a work based
158 on the Program, the distribution of the whole must be on the terms of
159 this License, whose permissions for other licensees extend to the
160 entire whole, and thus to each and every part regardless of who wrote it.
161
162 Thus, it is not the intent of this section to claim rights or contest
163 your rights to work written entirely by you; rather, the intent is to
164 exercise the right to control the distribution of derivative or
165 collective works based on the Program.
166
167 In addition, mere aggregation of another work not based on the Program
168 with the Program (or with a work based on the Program) on a volume of
169 a storage or distribution medium does not bring the other work under
170 the scope of this License.
171
172   3. You may copy and distribute the Program (or a work based on it,
173 under Section 2) in object code or executable form under the terms of
174 Sections 1 and 2 above provided that you also do one of the following:
175
176     a) Accompany it with the complete corresponding machine-readable
177     source code, which must be distributed under the terms of Sections
178     1 and 2 above on a medium customarily used for software interchange; or,
179
180     b) Accompany it with a written offer, valid for at least three
181     years, to give any third party, for a charge no more than your
182     cost of physically performing source distribution, a complete
183     machine-readable copy of the corresponding source code, to be
184     distributed under the terms of Sections 1 and 2 above on a medium
185     customarily used for software interchange; or,
186
187     c) Accompany it with the information you received as to the offer
188     to distribute corresponding source code.  (This alternative is
189     allowed only for noncommercial distribution and only if you
190     received the program in object code or executable form with such
191     an offer, in accord with Subsection b above.)
192
193 The source code for a work means the preferred form of the work for
194 making modifications to it.  For an executable work, complete source
195 code means all the source code for all modules it contains, plus any
196 associated interface definition files, plus the scripts used to
197 control compilation and installation of the executable.  However, as a
198 special exception, the source code distributed need not include
199 anything that is normally distributed (in either source or binary
200 form) with the major components (compiler, kernel, and so on) of the
201 operating system on which the executable runs, unless that component
202 itself accompanies the executable.
203
204 If distribution of executable or object code is made by offering
205 access to copy from a designated place, then offering equivalent
206 access to copy the source code from the same place counts as
207 distribution of the source code, even though third parties are not
208 compelled to copy the source along with the object code.
209 ^L
210   4. You may not copy, modify, sublicense, or distribute the Program
211 except as expressly provided under this License.  Any attempt
212 otherwise to copy, modify, sublicense or distribute the Program is
213 void, and will automatically terminate your rights under this License.
214 However, parties who have received copies, or rights, from you under
215 this License will not have their licenses terminated so long as such
216 parties remain in full compliance.
217
218   5. You are not required to accept this License, since you have not
219 signed it.  However, nothing else grants you permission to modify or
220 distribute the Program or its derivative works.  These actions are
221 prohibited by law if you do not accept this License.  Therefore, by
222 modifying or distributing the Program (or any work based on the
223 Program), you indicate your acceptance of this License to do so, and
224 all its terms and conditions for copying, distributing or modifying
225 the Program or works based on it.
226
227   6. Each time you redistribute the Program (or any work based on the
228 Program), the recipient automatically receives a license from the
229 original licensor to copy, distribute or modify the Program subject to
230 these terms and conditions.  You may not impose any further
231 restrictions on the recipients' exercise of the rights granted herein.
232 You are not responsible for enforcing compliance by third parties to
233 this License.
234
235   7. If, as a consequence of a court judgment or allegation of patent
236 infringement or for any other reason (not limited to patent issues),
237 conditions are imposed on you (whether by court order, agreement or
238 otherwise) that contradict the conditions of this License, they do not
239 excuse you from the conditions of this License.  If you cannot
240 distribute so as to satisfy simultaneously your obligations under this
241 License and any other pertinent obligations, then as a consequence you
242 may not distribute the Program at all.  For example, if a patent
243 license would not permit royalty-free redistribution of the Program by
244 all those who receive copies directly or indirectly through you, then
245 the only way you could satisfy both it and this License would be to
246 refrain entirely from distribution of the Program.
247
248 If any portion of this section is held invalid or unenforceable under
249 any particular circumstance, the balance of the section is intended to
250 apply and the section as a whole is intended to apply in other
251 circumstances.
252
253 It is not the purpose of this section to induce you to infringe any
254 patents or other property right claims or to contest validity of any
255 such claims; this section has the sole purpose of protecting the
256 integrity of the free software distribution system, which is
257 implemented by public license practices.  Many people have made
258 generous contributions to the wide range of software distributed
259 through that system in reliance on consistent application of that
260 system; it is up to the author/donor to decide if he or she is willing
261 to distribute software through any other system and a licensee cannot
262 impose that choice.
263
264 This section is intended to make thoroughly clear what is believed to
265 be a consequence of the rest of this License.
266
267   8. If the distribution and/or use of the Program is restricted in
268 certain countries either by patents or by copyrighted interfaces, the
269 original copyright holder who places the Program under this License
270 may add an explicit geographical distribution limitation excluding
271 those countries, so that distribution is permitted only in or among
272 countries not thus excluded.  In such case, this License incorporates
273 the limitation as if written in the body of this License.
274
275   9. The Free Software Foundation may publish revised and/or new versions
276 of the General Public License from time to time.  Such new versions will
277 be similar in spirit to the present version, but may differ in detail to
278 address new problems or concerns.
279
280 Each version is given a distinguishing version number.  If the Program
281 specifies a version number of this License which applies to it and \"any
282 later version\", you have the option of following the terms and conditions
283 either of that version or of any later version published by the Free
284 Software Foundation.  If the Program does not specify a version number of
285 this License, you may choose any version ever published by the Free Software
286 Foundation.
287
288   10. If you wish to incorporate parts of the Program into other free
289 programs whose distribution conditions are different, write to the author
290 to ask for permission.  For software which is copyrighted by the Free
291 Software Foundation, write to the Free Software Foundation; we sometimes
292 make exceptions for this.  Our decision will be guided by the two goals
293 of preserving the free status of all derivatives of our free software and
294 of promoting the sharing and reuse of software generally.
295 ";
296 static char *warranty=
297 "                            NO WARRANTY
298
299   11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
300 FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
301 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
302 PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
303 OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
304 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
305 TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
306 PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
307 REPAIR OR CORRECTION.
308
309   12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
310 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
311 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
312 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
313 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
314 TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
315 YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
316 PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
317 POSSIBILITY OF SUCH DAMAGES.
318 ";
319 #endif
320
321 static void printTypeInfo(link *);
322 static void printValAggregates (symbol *,link *,char,unsigned int);
323
324 int srcMode = SRC_CMODE ;
325
326 /*-----------------------------------------------------------------*/
327 /* funcWithName - returns function with name                       */
328 /*-----------------------------------------------------------------*/
329 DEFSETFUNC(funcWithName)
330 {
331     function *func = item;
332     V_ARG(char *,name);
333     V_ARG(function **,funcp);
334
335     if (*funcp)
336         return 0;
337
338     if (strcmp(func->sym->name,name) == 0) {
339         *funcp = func;
340         return 1;
341     }
342     
343     return 0;
344 }
345
346 /*-----------------------------------------------------------------*/
347 /* setBPatModLine - set break point at the line specified for the  */
348 /*-----------------------------------------------------------------*/
349 static void setBPatModLine (module *mod, int line)
350 {
351     /* look for the first executable line after the line
352        specified & get the break point there */    
353     if (srcMode == SRC_CMODE && line > mod->ncLines) {
354         fprintf(stderr,"No line %d in file \"%s\".\n",
355                 line,mod->c_name);
356         return ;
357     }
358     
359     if (srcMode == SRC_AMODE && line > mod->nasmLines) {
360         fprintf(stderr,"No line %d in file \"%s\".\n",
361                 line,mod->asm_name);
362         return ;
363     }
364
365     for ( ; line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ; 
366           line++ ) {
367         if (srcMode == SRC_CMODE) {
368             if (mod->cLines[line]->addr) {
369                 setBreakPoint (mod->cLines[line]->addr, CODE, USER, 
370                                userBpCB, mod->c_name, line);
371                 break;
372             }
373         }
374         else {
375            if (mod->asmLines[line]->addr) {
376                setBreakPoint (mod->asmLines[line]->addr, CODE, USER, 
377                               userBpCB, mod->asm_name, line);
378                break;
379            } 
380         }
381     }
382
383     return;
384 }
385
386 /*-----------------------------------------------------------------*/
387 /* clearBPatModLine - clr break point at the line specified        */
388 /*-----------------------------------------------------------------*/
389 static void clearBPatModLine (module *mod, int line)
390 {
391     /* look for the first executable line after the line
392        specified & get the break point there */
393     if (srcMode == SRC_CMODE && line > mod->ncLines) {
394         fprintf(stderr,"No line %d in file \"%s\".\n",
395                 line,mod->c_name);
396         return ;
397     }
398     
399     if (srcMode == SRC_AMODE && line > mod->ncLines) {
400         fprintf(stderr,"No line %d in file \"%s\".\n",
401                 line,mod->c_name);
402         return ;
403     }    
404     
405     for ( ; line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ; 
406           line++ ) {
407         if (srcMode == SRC_CMODE) 
408             if (mod->cLines[line]->addr) {
409                 clearUSERbp (mod->cLines[line]->addr);                    
410                 break;
411             }
412         else
413             if (mod->asmLines[line]->addr) {
414                 clearUSERbp (mod->asmLines[line]->addr);                          
415                 break;
416             }
417     }
418
419     return;
420 }
421
422 /*-----------------------------------------------------------------*/
423 /* funcWithNameModule - returns functions with a name module combo */
424 /*-----------------------------------------------------------------*/
425 DEFSETFUNC(funcWithNameModule) 
426 {
427     function *func = item;
428     V_ARG(char *,fname);
429     V_ARG(char *,mname);
430     V_ARG(function **,funcp);
431
432     if (*funcp)
433         return 0;
434
435     if (strcmp(func->sym->name,fname) == 0 &&
436         strcmp(func->mod->c_name,mname) == 0) {
437         *funcp = func;
438         return 1;
439     }
440
441     return 0;
442 }
443
444 /*-----------------------------------------------------------------*/
445 /* funcInAddr - given an address returns the function              */
446 /*-----------------------------------------------------------------*/
447 DEFSETFUNC(funcInAddr)
448 {
449     function *func = item;
450     V_ARG(unsigned int,addr);
451     V_ARG(function **,funcp);
452
453     if (*funcp)
454         return 0;
455
456     /* in the address range */
457     if (func->sym->addr <= addr &&
458         func->sym->eaddr >= addr) {
459         
460         *funcp = func;
461         return 1;
462     }
463
464     return 0;       
465 }
466
467 /*-----------------------------------------------------------------*/
468 /* setStepBp - will set STEP Bp @ function entry points            */
469 /*-----------------------------------------------------------------*/
470 DEFSETFUNC(setStepBp)
471 {
472     function *func = item;
473     
474     if (func->sym && func->sym->addr ) {
475         
476         /* set the entry break point */
477         setBreakPoint (func->sym->addr , CODE , STEP , 
478                        stepBpCB ,func->mod->c_name , func->entryline);
479
480         return 1;
481     }
482
483     return 0;
484 }
485
486 /*-----------------------------------------------------------------*/
487 /* setStepEPBp - sets a given type of bp @ the execution point     */
488 /*-----------------------------------------------------------------*/
489 DEFSETFUNC(setStepEPBp)
490 {
491     exePoint *ep = item;
492     V_ARG(int,bptype);
493     V_ARG(char *,mname);
494    
495     setBreakPoint (ep->addr, CODE, bptype, 
496                    stepBpCB, mname, ep->line);
497     return 1;
498 }
499
500 /*-----------------------------------------------------------------*/
501 /* setNextEPBp - sets a given type of bp @ the execution point     */
502 /*-----------------------------------------------------------------*/
503 DEFSETFUNC(setNextEPBp)
504 {
505     exePoint *ep = item;
506     V_ARG(int,bptype);
507     V_ARG(char *,mname);
508    
509     setBreakPoint (ep->addr, CODE, bptype, 
510                    nextBpCB, mname, ep->line);
511     return 1;
512 }
513
514 /*-----------------------------------------------------------------*/
515 /* lineAtAddr - for execution points returns the one with addr     */
516 /*-----------------------------------------------------------------*/
517 DEFSETFUNC(lineAtAddr)
518 {
519     exePoint *ep = item;
520     V_ARG(unsigned int,addr);
521     V_ARG(int *,line);
522     V_ARG(int *,block);
523     V_ARG(int *,level);
524
525     /* address must be an exact match */
526     if (ep->addr == addr) {
527         *line = ep->line;
528         if (block)
529             *block = ep->block ;
530         if (level)
531             *level = ep->level ;
532         return 1;
533     }
534
535     return 0;
536     
537 }
538
539 /*-----------------------------------------------------------------*/
540 /* discoverContext - find out the current context of the bp        */
541 /*-----------------------------------------------------------------*/
542 context *discoverContext (unsigned addr)
543 {
544     function *func = NULL;
545     int line = 0;
546
547     /* find the function we are in */
548     if (!applyToSet(functions,funcInAddr,addr,&func)) {
549       fprintf(stderr, "Error?:discoverContext: cannot apply to set!\n");
550         return NULL;
551     }
552
553     currCtxt->func = func;
554     currCtxt->addr = func->laddr = addr;
555     currCtxt->modName = func->modName;
556     
557     /* find the c line number */
558     if(applyToSet(func->cfpoints,lineAtAddr,addr,
559                   &line,&currCtxt->block,&currCtxt->level)) 
560         currCtxt->cline = func->lline = line;
561     else
562         currCtxt->cline = func->exitline;
563     
564     /* find the asm line number */
565     line = 0;
566     if (applyToSet(func->afpoints,lineAtAddr,addr,
567                    &line,NULL,NULL))
568         currCtxt->asmline = line;       
569     else
570         currCtxt->asmline = -1;
571         
572     return currCtxt ;
573 }
574
575
576 /*-----------------------------------------------------------------*/
577 /* simGo - send 'go' cmd to simulator and wait till a break occurs */
578 /*-----------------------------------------------------------------*/
579 void simGo (unsigned int gaddr)
580 {   
581     unsigned int addr ;
582     context *ctxt;
583     int rv;
584     static int initial_break_flag = 0;
585
586  top:    
587     addr = simGoTillBp (gaddr);
588
589     /* got the pc for the break point now first
590        discover the program context i.e. module, function 
591        linenumber of the source etc, etc etc */
592     ctxt = discoverContext (addr);
593     
594     /* dispatch all the break point call back functions */
595     rv = dispatchCB (addr,ctxt);    
596
597  ret:    
598
599     /* the dispatch call back function will return
600        non-zero if an user break point has been hit
601        if not then we continue with the execution 
602        of the program */
603     if (!rv) {
604       if (!initial_break_flag) {
605         initial_break_flag = 1;  // kludge to stop only at first run
606         fprintf(stdout, "Stopping at entry.  You can now list and set breakpoints\n");
607       }
608       else {
609         gaddr = -1;
610         goto top ;
611       }
612
613 // notes: kpb
614 // I took this out, after running "run" it would just keep re-running
615 // even after a lot of break points hit.  For some reason above code
616 // not triggering(dispatchCB).  This seems to be by design, startup adds
617 // a bunch of breakpoints-but they are not USER breakpoints.  Perhaps the
618 // debugger changed with its implementation of "go"("run").  It seems we
619 // need to add a "next" or "step" followed by a "run"...
620 // I added a "step" in simi.c when we want a resume function, this seems
621 // to work.
622
623 // still there is question of how do we stop it initially, since
624 // it must be started before it can get a context.  If so, we would
625 // want it to just run up to an initial entry point you'd think...
626 // I don't see why we can't set breakpoints before an initial run,
627 // this does not seem right to me.
628
629 // line #'s are a bit off too.
630
631 #if 0
632         gaddr = -1;
633         goto top ;
634 #endif
635     }
636     
637 }
638
639 /*-----------------------------------------------------------------*/
640 /* cmdSetUserBp - set break point at the user specified location   */
641 /*-----------------------------------------------------------------*/
642 int cmdSetUserBp (char *s, context *cctxt)
643 {
644     char *bp ;
645     function *func = NULL;
646         
647     /* user break point location specification can be of the following
648        forms
649        a) <nothing>        - break point at current location
650        b) lineno           - number of the current module
651        c) filename:lineno  - line number of the given file
652        e) filename:function- function X in file Y (useful for static functions)
653        f) function         - function entry point
654     */
655
656     if (!cctxt) {
657         fprintf(stdout,"No symbol table is loaded.  Use the \"file\" command.\n");
658         return 0;
659     }
660     /* white space skip */
661     while (*s && isspace(*s)) s++;
662     
663     /* null terminate it after stripping trailing blanks*/
664     bp = s + strlen(s);
665     while (bp != s && isspace(*bp)) bp--;
666     *bp = '\0';
667
668     /* case a) nothing */
669     /* if nothing given then current location : we know
670        the current execution location from the currentContext */
671     if (! *s ) {
672
673         /* if current context is known */
674         if (cctxt->func) {
675             if (srcMode == SRC_CMODE)
676                 /* set the break point */
677                 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
678                                 cctxt->func->mod->c_name, cctxt->cline);
679             else
680                 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
681                                 cctxt->func->mod->asm_name, cctxt->asmline);
682                 
683         }
684         else
685             fprintf(stderr,"No default breakpoint address now.\n");
686                         
687         goto ret ;
688     }
689
690     /* case b) lineno */
691     /* check if line number */
692     if (isdigit(*s)) {
693         /* get the lineno */
694         int line = atoi(s);
695
696         /* if current context not present then we must get the module
697            which has main & set the break point @ line number provided
698            of that module : if current context known then set the bp 
699            at the line number given for the current module 
700         */
701         if (cctxt->func) {
702             if (!cctxt->func->mod) {
703                 if (!applyToSet(functions,funcWithName,"main"))
704                     fprintf(stderr,"Function \"main\" not defined.\n");
705                 else 
706                     setBPatModLine(func->mod,line);
707             } else 
708                 setBPatModLine(cctxt->func->mod,line);                  
709         } else {
710                 fprintf(stdout,"No symbol information currently\n");
711         }
712         
713         goto ret;
714     }
715
716     if ((bp = strchr(s,':'))) {
717         
718         module *mod = NULL;
719         *bp = '\0';
720         
721         if (srcMode == SRC_CMODE) {
722             if (!applyToSet(modules,moduleWithCName,s,&mod)) {
723                 fprintf (stderr,"No source file named %s.\n",s);
724                 goto ret;
725             }
726         } else {
727             if (!applyToSet(modules,moduleWithAsmName,s,&mod)) {
728                 fprintf (stderr,"No source file named %s.\n",s);
729                 goto ret;
730             }
731         }
732                 
733         /* case c) filename:lineno */
734         if (isdigit(*(bp +1))) {                    
735          
736             setBPatModLine (mod,atoi(bp+1));        
737             goto ret;
738             
739         }
740         /* case d) filename:function */
741         if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func)) 
742             fprintf(stderr,"Function \"%s\" not defined.\n",bp+1); 
743         else        
744             setBPatModLine (mod,
745                             (srcMode == SRC_CMODE ? 
746                              func->entryline :
747                              func->aentryline));
748         
749         goto ret;
750     }
751             
752     /* case e) function */
753     if (!applyToSet(functions,funcWithName,s,&func))
754         fprintf(stderr,"Function \"%s\" not defined.\n",s); 
755     else
756         setBPatModLine(func->mod,
757                        (srcMode == SRC_CMODE ?
758                         func->entryline :
759                         func->aentryline));
760
761  ret:    
762     return 0;
763 }
764
765 /*-----------------------------------------------------------------*/
766 /* cmdListAsm - list assembler source code                         */
767 /*-----------------------------------------------------------------*/
768 int cmdListAsm (char *s, context *cctxt)
769 {
770     fprintf(stderr,"'listasm' command not yet implemented\n");
771     return 0;
772 }
773
774 /*-----------------------------------------------------------------*/
775 /* cmdSetOption - set debugger options                             */
776 /*-----------------------------------------------------------------*/
777 int cmdSetOption (char *s, context *cctxt)
778 {
779     while (*s && isspace(*s)) s++;
780     if (strncmp(s,"srcmode",7) == 0 ) {
781         if (srcMode == SRC_CMODE)
782             srcMode = SRC_AMODE;
783         else
784             srcMode = SRC_CMODE;
785         fprintf(stderr,"source mode set to '%s'\n", 
786                 (srcMode == SRC_CMODE ? "C" : "asm"));
787         return 0;
788     }
789     
790     fprintf(stderr,"'set %s' command not yet implemented\n",s);
791     return 0;
792 }
793
794 /*-----------------------------------------------------------------*/
795 /* cmdContinue - continue till next break point                    */
796 /*-----------------------------------------------------------------*/
797 int cmdContinue (char *s, context *cctxt)
798 {
799     if (!cctxt || !cctxt->func) {
800         fprintf(stdout,"The program is not being run.\n");
801         return 0;
802     }
803
804     fprintf(stdout,"Continuing.\n");
805     simGo(-1);
806     return 0;
807 }
808
809 /*-----------------------------------------------------------------*/
810 /* cmdDelUserBp - delete user break point                          */
811 /*-----------------------------------------------------------------*/
812 int cmdDelUserBp (char *s, context *cctxt)
813 {
814     int bpnum ;
815     while (isspace(*s)) s++;
816     
817     if (!*s ) {
818         if (userBpPresent) {
819             char buffer[10];
820             fprintf (stdout,"Delete all breakpoints? (y or n) ");
821             fflush(stdout);
822             fgets(buffer,sizeof(buffer),stdin);
823             if (toupper(buffer[0]) == 'Y')
824                 deleteUSERbp(-1);          
825         }
826         return 0;
827     }
828     
829     /* determine the break point number */
830     if (sscanf(s,"%d",&bpnum) == 1)
831         deleteUSERbp(bpnum);
832
833     return 0;
834 }
835
836 /*-----------------------------------------------------------------*/
837 /* cmdStep - single step thru C source file                        */
838 /*-----------------------------------------------------------------*/
839 int cmdStep (char *s, context *cctxt)
840 {
841     function *func = NULL;
842
843     if (!cctxt || !cctxt->func || !cctxt->func->mod) 
844         fprintf(stdout,"The program is not being run.\n");
845     else {
846         /* if we are @ the end of a function then set
847            break points at execution points of the
848            function in the call stack... */
849         if (cctxt->addr == cctxt->func->sym->eaddr) {
850             if ((func = STACK_PEEK(callStack))) {
851                 if (srcMode == SRC_CMODE)
852                     applyToSet (func->cfpoints,setStepEPBp,STEP,
853                                 func->mod->c_name);     
854                 else
855                     applyToSet (func->afpoints,setStepEPBp,STEP,
856                                 func->mod->asm_name);
857             }
858         } else {
859             /* set breakpoints at all function entry points
860                and all exepoints of this functions & for
861                all functions one up in the call stack */
862             
863             /* all function entry points */
864             applyToSet(functions,setStepBp); 
865             
866             if (srcMode == SRC_CMODE) {
867                 /* for all execution points in this function */
868                 applyToSet(cctxt->func->cfpoints,setStepEPBp,STEP,
869                            cctxt->func->mod->c_name);
870                 
871                 /* set a break point @ the current function's
872                    exit */
873                 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP , 
874                                stepBpCB, cctxt->func->mod->c_name, 
875                                cctxt->func->exitline);
876                 
877                 /* now break point @ callers execution points */
878                 if ((func = STACK_PPEEK(callStack))) {
879                     applyToSet (func->cfpoints,setStepEPBp,STEP,
880                                 func->mod->c_name);     
881                     /* set bp @ callers exit point */
882                     setBreakPoint (func->sym->eaddr, CODE, STEP , 
883                                    stepBpCB, func->mod->c_name, 
884                                    func->exitline);
885                 }
886             } else {
887                 /* for all execution points in this function */
888                 applyToSet(cctxt->func->afpoints,setStepEPBp,STEP,
889                            cctxt->func->mod->asm_name);
890                 
891                 /* set a break point @ the current function's
892                    exit */
893                 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP , 
894                                stepBpCB, cctxt->func->mod->asm_name, 
895                                cctxt->func->aexitline);
896                 
897                 /* now break point @ callers execution points */
898                 if ((func = STACK_PPEEK(callStack))) {
899                     
900                     applyToSet (func->afpoints,setStepEPBp,STEP,
901                                 func->mod->asm_name);   
902                     
903                     /* set bp @ callers exit point */
904                     setBreakPoint (func->sym->eaddr, CODE, STEP , 
905                                    stepBpCB, func->mod->asm_name, 
906                                    func->aexitline);
907                 }
908             }
909         }
910
911         simGo(-1);
912     }
913     return 0;
914 }
915
916 /*-----------------------------------------------------------------*/
917 /* cmdNext - next executable C statement file                      */
918 /*-----------------------------------------------------------------*/
919 int cmdNext (char *s, context *cctxt)
920 {
921     function *func = NULL;
922     /* next is almost the same as step except we don't
923        we don't set break point for all function entry
924        points */
925     if (!cctxt || !cctxt->func || !cctxt->func->mod) 
926         fprintf(stdout,"The program is not being run.\n");
927     else {
928         
929         /* if we are @ the end of a function then set
930            break points at execution points of the
931            function in the call stack... */
932         if (cctxt->addr == cctxt->func->sym->eaddr) {
933             if ((func = STACK_PEEK(callStack))) {
934                 if (srcMode == SRC_CMODE)
935                     applyToSet (func->cfpoints,setStepEPBp,STEP,
936                                 func->mod->c_name);     
937                 else
938                     applyToSet (func->afpoints,setStepEPBp,STEP,
939                                func->mod->asm_name);
940             }
941         } else {
942             if (srcMode == SRC_CMODE) {
943                 /* for all execution points in this function */
944                 applyToSet(cctxt->func->cfpoints,setNextEPBp,NEXT,
945                            cctxt->func->mod->c_name);
946                 /* set a break point @ the current function's
947                    exit */
948                 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT , 
949                                nextBpCB, cctxt->func->mod->c_name, 
950                                cctxt->func->exitline);
951                 
952                 /* now break point @ callers execution points */        
953                 if ((func = STACK_PPEEK(callStack))) {
954                     applyToSet (func->cfpoints,setNextEPBp,NEXT ,
955                                 func->mod->c_name);     
956                     /* set bp @ callers exit point */
957                     setBreakPoint (func->sym->eaddr, CODE, NEXT , 
958                                    stepBpCB, func->mod->c_name, 
959                                    func->exitline);
960                 }
961             } else {
962                 /* for all execution points in this function */
963                 applyToSet(cctxt->func->afpoints,setNextEPBp,NEXT,
964                            cctxt->func->mod->asm_name);
965                 /* set a break point @ the current function's
966                    exit */
967                 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT , 
968                                nextBpCB, cctxt->func->mod->asm_name, 
969                                cctxt->func->aexitline);
970                 
971                 /* now break point @ callers execution points */        
972                 if ((func = STACK_PPEEK(callStack))) {
973                     applyToSet (func->cfpoints,setNextEPBp,NEXT ,
974                                 func->mod->asm_name);   
975                     /* set bp @ callers exit point */
976                     setBreakPoint (func->sym->eaddr, CODE, NEXT , 
977                                    stepBpCB, func->mod->asm_name, 
978                                    func->aexitline);
979                 }
980             }
981             simGo(-1);  
982         }
983     }    
984     return 0;
985 }
986
987 /*-----------------------------------------------------------------*/
988 /* cmdRun  - run till next break point                             */
989 /*-----------------------------------------------------------------*/
990 int cmdRun (char *s, context *cctxt)
991 {
992     char buff[10];
993     if (!cctxt || !cctxt->func || !cctxt->func->mod) {
994         fprintf(stdout,"Starting program\n");
995         simGo(0);
996     } else {
997         
998         fprintf(stdout,
999                 "The program being debugged has been started already.\n");
1000         fprintf(stdout,"Start it from the beginning? (y or n) ");
1001         fflush(stdout);
1002
1003         fgets(buff,sizeof(buff),stdin);
1004         if (toupper(buff[0]) == 'Y') {
1005             simReset();
1006             simGo(0);
1007         }
1008     }
1009
1010     return 0;
1011 }
1012
1013 /*-----------------------------------------------------------------*/
1014 /* infoStack - print call stack information                        */
1015 /*-----------------------------------------------------------------*/
1016 static void infoStack(context *ctxt)
1017 {
1018     function *func ;
1019     int i = 0 ;
1020
1021     STACK_STARTWALK(callStack) ;
1022     while ((func = STACK_WALK(callStack))) {
1023
1024         fprintf(stdout,"#%d 0x%04x %s () at %s:%d\n",i++,
1025                 func->laddr,func->sym->name,
1026                 func->mod->c_name,func->lline);
1027     }
1028
1029 }
1030
1031 /*-----------------------------------------------------------------*/
1032 /* cmdInfo - info command                                          */
1033 /*-----------------------------------------------------------------*/
1034 int cmdInfo (char *s, context *cctxt)
1035 {
1036     while (isspace(*s)) s++;
1037
1038     /* list all break points */
1039     if (strcmp(s,"break") == 0) {
1040         listUSERbp();
1041         return 0;
1042     }
1043
1044     /* info frame same as frame */
1045     if (strcmp(s,"frame") == 0) {
1046         cmdFrame (s,cctxt);
1047         return 0;
1048     }
1049
1050     /* info stack display call stack */
1051     if (strcmp(s,"stack") == 0) {
1052         infoStack(cctxt);
1053         return 0;
1054     }
1055
1056     /* info stack display call stack */
1057     if (strcmp(s,"registers") == 0) {
1058         fprintf(stdout,"%s",simRegs());
1059         return 0;
1060     }
1061
1062     fprintf(stdout,"Undefined info command: \"%s\".  Try \"help\n",s);
1063     return 0;
1064
1065 }
1066
1067 /*-----------------------------------------------------------------*/
1068 /* cmdQuit  - quit debugging                                       */
1069 /*-----------------------------------------------------------------*/
1070 int cmdQuit (char *s, context *cctxt)
1071 {   
1072     if (simactive)
1073         closeSimulator();
1074     return 1;
1075 }
1076
1077 /*-----------------------------------------------------------------*/
1078 /* cmdListSrc  - list src                                          */
1079 /*-----------------------------------------------------------------*/
1080 int cmdListSrc (char *s, context *cctxt)
1081 {   
1082     static int currline = 0;
1083     int i =0 ;
1084     int pline = 0;
1085     static module *mod = NULL;
1086     int llines = listLines;
1087
1088     while (*s && isspace(*s)) s++;
1089     
1090     /* if the user has spcified line numer then the line number
1091        can be of the following formats
1092        LINE          - just line number
1093        FILE:LINE     - filename line number
1094        FUNCTION      - list a function
1095        FILE:FUNCTION - function in file */
1096     if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1097         fprintf(stdout,"No symbol table is loaded.  Use the \"file\" command.\n");
1098         return 0;
1099     }
1100     if (*s) {
1101         /* case a) LINE */
1102         if (isdigit(*s)) {
1103             sscanf(s,"%d",&pline);
1104             mod = cctxt->func->mod;
1105         }
1106         else {
1107             char *bp;
1108             function *func = NULL;
1109             
1110             /* if ':' present then FILE:LINE || FILE:FUNCTION */
1111             if ((bp = strchr(s,':'))) {
1112                 *bp = '\0';
1113                 bp ++;
1114                 if (isdigit(*bp)) {
1115                     /* FILE:LINE */
1116                     if (srcMode == SRC_CMODE) {
1117                         if (!applyToSet(modules,moduleWithCName,s,&mod)) {
1118                             fprintf (stderr,"No source file named %s.\n",s);
1119                             return 0;
1120                         }
1121                     } else {
1122                         if (!applyToSet(modules,moduleWithAsmName,s,&mod)) {
1123                             fprintf (stderr,"No source file named %s.\n",s);
1124                             return 0;
1125                         }
1126                     }
1127                     sscanf(bp,"%d",&pline);                         
1128                 } else {
1129                     /* FILE:FUCTION */
1130                     if (!applyToSet(functions,funcWithNameModule,bp,s,&func)) {
1131                         fprintf(stdout,"Function \"%s\" not defined.\n",bp);
1132                         return 0;
1133                     }
1134                     mod = func->mod;
1135                     if (srcMode == SRC_CMODE) {
1136                         pline = func->entryline;
1137                         llines = func->exitline - func->entryline + 1;
1138                     } else {
1139                         pline = func->aentryline;
1140                         llines = func->aexitline - func->aentryline + 1;
1141                     }
1142                 }
1143             }
1144             else {
1145                 /* FUNCTION */
1146                 if (!applyToSet(functions,funcWithName,s,&func)) {
1147                     fprintf(stderr,"Function \"%s\" not defined.\n",s); 
1148                     return 0;
1149                 }
1150                 else {
1151                     mod = func->mod;
1152                     if (srcMode == SRC_CMODE) {
1153                         pline = func->entryline;
1154                         llines = func->exitline - func->entryline + 1; 
1155                     } else {
1156                         pline = func->aentryline;
1157                         llines = func->aexitline - func->aentryline + 1; 
1158                     }
1159                 }
1160             }               
1161         }
1162     } else {
1163         /* if no line specified & we had listed
1164            before then continue from that listing */
1165         if (currline)
1166             pline = currline ;
1167         else {
1168             mod = cctxt->func->mod;
1169             if (srcMode == SRC_CMODE)
1170                 pline = cctxt->cline;
1171             else
1172                 pline = cctxt->asmline;
1173         }
1174     }
1175     
1176     for ( i = 0 ; i < llines ; i++ ) {
1177         if (srcMode == SRC_CMODE) {
1178             if ( (pline + i) >= mod->ncLines )
1179                 break;
1180             fprintf(stdout,"%d\t%s",pline + i,
1181                     mod->cLines[pline +i]->src);
1182         } else {
1183             if ( (pline + i) >= mod->nasmLines )
1184                 break;
1185             fprintf(stdout,"%d\t%s",pline + i,
1186                     mod->asmLines[pline +i]->src);
1187         }
1188     }
1189     currline = pline + i ;
1190     return 0;
1191 }
1192
1193 /*-----------------------------------------------------------------*/
1194 /* printValBasic - print value of basic types                      */
1195 /*-----------------------------------------------------------------*/
1196 static void printValBasic(symbol *sym,unsigned addr,char mem, int size)
1197 {
1198     union {     
1199         float f;     
1200         unsigned long val;
1201         long         sval;
1202         struct {
1203             short    lo;
1204             short    hi;
1205         } i;
1206         unsigned char b[4];
1207     }v;
1208     union {
1209         unsigned char b[4];
1210     }v1;
1211     
1212     v.val = simGetValue(addr,mem,size);
1213     /* if this a floating point number then */
1214     if (IS_FLOAT(sym->type))    
1215         fprintf(stdout,"%f",v.f);    
1216     else
1217         if (IS_PTR(sym->type))
1218             fprintf(stdout,"0x%x",v.val);
1219         else
1220             if (IS_SPEC(sym->type) && IS_INTEGRAL(sym->type)) {
1221                 if (IS_CHAR(sym->etype)) 
1222                     fprintf(stdout,"'%c' %d 0x%x",v.val,v.val,v.val);
1223                 else
1224                     if (IS_INT(sym->etype)) 
1225                         if (IS_LONG(sym->etype))
1226                             if (SPEC_USIGN(sym->etype))
1227                                 fprintf(stdout,"%d 0x%x",v.val,v.val);
1228                             else
1229                                 fprintf(stdout,"%d 0x%x",v.sval,v.sval);
1230                         else
1231                             fprintf(stdout,"%d 0x%x",v.i.lo,v.i.lo);
1232                     else
1233                         fprintf(stdout,"0x%x",v.val);
1234             } else
1235                 fprintf(stdout,"0x%x",v.val);
1236                 
1237     
1238 }
1239
1240 /*-----------------------------------------------------------------*/
1241 /* printValFunc  - prints function values                          */
1242 /*-----------------------------------------------------------------*/
1243 static void printValFunc (symbol *sym)
1244 {
1245     fprintf(stdout,"print function not yet implemented\n");
1246 }
1247
1248 /*-----------------------------------------------------------------*/
1249 /* printArrayValue - will print the values of array elements       */
1250 /*-----------------------------------------------------------------*/
1251 static void printArrayValue (symbol *sym, char space, unsigned int addr)
1252 {
1253         link *elem_type = sym->type->next;
1254         int i;
1255         
1256         fprintf(stdout," { ");
1257         for (i = 0 ; i < DCL_ELEM(sym->type) ; i++) {           
1258                 if (IS_AGGREGATE(elem_type)) {
1259                         printValAggregates(sym,elem_type,space,addr);                  
1260                 } else {
1261                         printValBasic(sym,addr,space,getSize(elem_type));
1262                 }
1263                 addr += getSize(elem_type);
1264                 if (i != DCL_ELEM(sym->type) -1)
1265                         fprintf(stdout,",");
1266         }
1267
1268         fprintf(stdout,"}\n");          
1269 }
1270
1271 /*-----------------------------------------------------------------*/
1272 /* printStructValue - prints structures elements                   */
1273 /*-----------------------------------------------------------------*/
1274 static void printStructValue (symbol *sym,link *type, char space, unsigned int addr) 
1275 {
1276         symbol *fields = SPEC_STRUCT(type)->fields;
1277
1278         fprintf(stdout," { ");
1279         while (fields) {
1280                 fprintf(stdout,"%s = ",fields->name);
1281                 if (IS_AGGREGATE(fields->type)) {
1282                         printValAggregates(fields,fields->type,space, addr);
1283                 } else {
1284                         printValBasic(fields,addr,space,getSize(fields->type));
1285                 }
1286                 addr += getSize(fields->type);
1287                 fields = fields->next;
1288         }
1289         fprintf(stdout,"}\n");
1290 }
1291
1292 /*-----------------------------------------------------------------*/
1293 /* printValAggregates - print value of aggregates                  */
1294 /*-----------------------------------------------------------------*/
1295 static void printValAggregates (symbol *sym, link *type,char space,unsigned int addr)
1296 {
1297
1298         if (IS_ARRAY(type)) {
1299                 printArrayValue(sym, space, addr);
1300                 return ;
1301         }
1302
1303         if (IS_STRUCT(type)) { 
1304                 printStructValue(sym,sym->type,space, addr); 
1305                 return; 
1306         } 
1307 }
1308
1309 /*-----------------------------------------------------------------*/
1310 /* printSymValue - print value of a symbol                         */
1311 /*-----------------------------------------------------------------*/
1312 static void printSymValue (symbol *sym, context *cctxt)
1313 {
1314     static int stack = 1;
1315     unsigned long val;
1316     /* if it is on stack then compute address & fall thru */
1317     if (sym->isonstack) {
1318         symbol *bp = symLookup("bp",cctxt);
1319         if (!bp) {
1320             fprintf(stdout,"cannot determine stack frame\n");
1321             return ;
1322         }
1323
1324         sym->addr = simGetValue(bp->addr,bp->addrspace,bp->size)
1325           + sym->offset ;      
1326     }
1327     
1328     /* get the value from the simulator and
1329        print it */
1330     fprintf(stdout,"$%d = ",stack++);
1331     /* arrays & structures first */
1332     if (IS_AGGREGATE(sym->type))
1333         printValAggregates(sym,sym->type,sym->addrspace,sym->addr);
1334     else
1335         /* functions */
1336         if (IS_FUNC(sym->type))
1337             printValFunc(sym);
1338         else {
1339             printValBasic(sym,sym->addr,sym->addrspace,sym->size);
1340             fprintf(stdout,"\n");
1341         }
1342 }
1343
1344 /*-----------------------------------------------------------------*/
1345 /* printStructInfo - print out structure information               */
1346 /*-----------------------------------------------------------------*/
1347 static void printStructInfo (structdef *sdef)
1348 {
1349     symbol *field = sdef->fields ;
1350     int i = 0 ;
1351     
1352     while (field) {
1353         i += field->offset;
1354         field = field->next;
1355     }
1356
1357     fprintf(stdout,"%s %s {\n",(i ? "struct" : "union" ), sdef->tag);
1358     field = sdef->fields;
1359     while (field) {
1360         printTypeInfo (field->type);
1361         fprintf(stdout," %s ;\n",field->name);
1362         field = field->next ;
1363     }
1364
1365     fprintf(stdout,"}\n");
1366
1367 }
1368
1369 /*-----------------------------------------------------------------*/
1370 /* printTypeInfo - print out the type information                  */
1371 /*-----------------------------------------------------------------*/
1372 static void printTypeInfo(link *p)
1373 {
1374     if (!p)
1375         return ;
1376
1377     if (IS_DECL(p)) {
1378         switch (DCL_TYPE(p))  {
1379         case FUNCTION:
1380             printTypeInfo (p->next);
1381             fprintf(stdout,"()");
1382             break;
1383         case ARRAY:
1384             printTypeInfo (p->next);
1385             fprintf(stdout,"[%d]",DCL_ELEM(p));
1386             break;
1387         
1388         case IPOINTER:
1389         case PPOINTER:
1390         case POINTER:
1391             printTypeInfo (p->next);
1392             fprintf(stdout,"(_near *)");
1393             break;
1394
1395         case FPOINTER:
1396             printTypeInfo (p->next);
1397             fprintf(stdout,"(_xdata *)");
1398             break;
1399
1400         case CPOINTER:
1401             printTypeInfo( p->next);
1402             fprintf(stdout,"(_code *)");
1403             break;
1404             
1405         case GPOINTER:
1406             printTypeInfo( p->next);
1407             fprintf(stdout,"(_generic *)");
1408             break;                   
1409         }
1410     } else {
1411         switch (SPEC_NOUN(p)) { /* depending on the specifier type */
1412         case V_INT:
1413             (IS_LONG(p) ? fputs("long ",stdout) : 
1414              ( IS_SHORT(p) ? fputs("short ",stdout) : 
1415                fputs("int ",stdout))) ;
1416             break;
1417         case V_FLOAT:
1418              fputs("float ",stdout);
1419              break;
1420
1421         case V_CHAR:
1422             fputs ("char ",stdout);
1423             break;
1424
1425         case V_VOID:
1426             fputs("void ",stdout);
1427             break;
1428
1429         case V_STRUCT:
1430             printStructInfo (SPEC_STRUCT(p));
1431             break;
1432
1433         case V_SBIT:
1434             fputs("sbit ",stdout);
1435             break;
1436
1437         case V_BIT:
1438             fprintf(stdout,": %d" ,SPEC_BLEN(p));       
1439             break;
1440         }
1441     }
1442 }
1443
1444 /*-----------------------------------------------------------------*/
1445 /* cmdPrint - print value of variable                              */
1446 /*-----------------------------------------------------------------*/
1447 int cmdPrint (char *s, context *cctxt)
1448 {   
1449     symbol *sym ;
1450     char *bp = s+strlen(s) -1;
1451
1452     while (isspace(*s)) s++;
1453     if (!*s) return 0;
1454     while (isspace(*bp)) bp--;
1455     bp++ ;
1456     *bp = '\0';
1457
1458     if (!cctxt || !cctxt->func) {
1459         fprintf(stdout,"No symbol \"%s\" in current context.\n",
1460                 s);
1461         return 0;
1462     }
1463     if ((sym = symLookup(s,cctxt))) {
1464         printSymValue(sym,cctxt);
1465     } else {
1466         fprintf(stdout,
1467                 "No symbol \"%s\" in current context.\n",              
1468                 s);
1469     }
1470     return 0;
1471 }
1472
1473 /*-----------------------------------------------------------------*/
1474 /* cmdPrintType - print type of a variable                         */
1475 /*-----------------------------------------------------------------*/
1476 int cmdPrintType (char *s, context *cctxt)
1477 {   
1478         symbol *sym ;
1479     char *bp = s+strlen(s) -1;
1480
1481     while (isspace(*s)) s++;
1482     if (!*s) return 0;
1483     while (isspace(*bp)) bp--;
1484     bp++ ;
1485     *bp = '\0';
1486
1487     if (!cctxt || !cctxt->func) {
1488         fprintf(stdout,"No symbol \"%s\" in current context.\n",
1489                 s);
1490         return 0;
1491     }
1492
1493     if ((sym = symLookup(s,cctxt))) {
1494         printTypeInfo(sym->type);
1495         fprintf(stdout,"\n");
1496     } else {
1497         fprintf(stdout,
1498                 "No symbol \"%s\" in current context.\n",              
1499                 s);
1500     }
1501     return 0;   
1502 }
1503
1504 /*-----------------------------------------------------------------*/
1505 /* cmdClrUserBp - clear user break point                           */
1506 /*-----------------------------------------------------------------*/
1507 int cmdClrUserBp (char *s, context *cctxt)
1508 {   
1509     char *bp ;    
1510     function *func = NULL;
1511         
1512     /* clear break point location specification can be of the following
1513        forms
1514        a) <nothing>        - break point at current location
1515        b) lineno           - number of the current module
1516        c) filename:lineno  - line number of the given file
1517        e) filename:function- function X in file Y (useful for static functions)
1518        f) function         - function entry point
1519     */
1520
1521     if (!cctxt) {
1522         fprintf(stdout,"No symbol table is loaded.  Use the \"file\" command.\n");
1523         return 0;
1524     }
1525
1526     /* white space skip */
1527     while (*s && isspace(*s)) s++;
1528     
1529     /* null terminate it after stripping trailing blanks*/
1530     bp = s + strlen(s);
1531     while (bp != s && isspace(*bp)) bp--;
1532     *bp = '\0';
1533
1534     /* case a) nothing */
1535     /* if nothing given then current location : we know
1536        the current execution location from the currentContext */
1537     if (! *s ) {
1538
1539         /* if current context is known */
1540         if (cctxt->func) 
1541             /* clear the break point @ current location */
1542             clearUSERbp (cctxt->addr);
1543         else
1544             fprintf(stderr,"No default breakpoint address now.\n");
1545                         
1546         goto ret ;
1547     }
1548
1549     /* case b) lineno */
1550     /* check if line number */
1551     if (isdigit(*s)) {
1552         /* get the lineno */
1553         int line = atoi(s);
1554
1555         /* if current context not present then we must get the module
1556            which has main & set the break point @ line number provided
1557            of that module : if current context known then set the bp 
1558            at the line number given for the current module 
1559         */
1560         if (cctxt->func) {
1561             if (!cctxt->func->mod) {
1562                 if (!applyToSet(functions,funcWithName,"main"))
1563                     fprintf(stderr,"Function \"main\" not defined.\n");
1564                 else 
1565                     clearBPatModLine(func->mod,line);
1566             } else 
1567                 clearBPatModLine(cctxt->func->mod,line);                        
1568         }
1569         
1570         goto ret;
1571     }
1572
1573     if ((bp = strchr(s,':'))) {
1574         
1575         module *mod = NULL;
1576         *bp = '\0';
1577         
1578         if (!applyToSet(modules,moduleWithCName,s,&mod)) {
1579             fprintf (stderr,"No source file named %s.\n",s);
1580             goto ret;
1581         }
1582
1583         /* case c) filename:lineno */
1584         if (isdigit(*(bp +1))) {                    
1585          
1586             clearBPatModLine (mod,atoi(bp+1));      
1587             goto ret;
1588             
1589         }
1590         /* case d) filename:function */
1591         if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func)) 
1592             fprintf(stderr,"Function \"%s\" not defined.\n",bp+1); 
1593         else
1594             clearBPatModLine (mod,func->entryline);
1595         
1596         goto ret;
1597     }
1598             
1599     /* case e) function */
1600     if (!applyToSet(functions,funcWithName,s,&func))
1601         fprintf(stderr,"Function \"%s\" not defined.\n",s); 
1602     else
1603         clearBPatModLine(func->mod,func->entryline);
1604
1605  ret:    
1606     return 0;        
1607 }
1608
1609
1610 /*-----------------------------------------------------------------*/
1611 /* cmdSimulator - send command to simulator                        */
1612 /*-----------------------------------------------------------------*/
1613 int cmdSimulator (char *s, context *cctxt)
1614 {   
1615   char tmpstr[82];
1616
1617     if (strlen(s) > 80) {
1618       printf("error 3A\n");
1619       exit(1);
1620     }
1621     strcpy(tmpstr, s);
1622     strcat(tmpstr, "\n");
1623     sendSim(tmpstr);
1624     waitForSim();
1625     fprintf(stdout,"%s",simResponse());
1626     return 0;
1627 }
1628
1629 /*-----------------------------------------------------------------*/
1630 /* cmdFrame - Frame command                                        */
1631 /*-----------------------------------------------------------------*/
1632 int cmdFrame (char *s, context *cctxt)
1633 {   
1634     function *func ;
1635
1636     if ((func = STACK_PEEK(callStack))) {
1637         fprintf(stdout,"#0  %s () at %s:%d\n",
1638                 func->sym->name,func->mod->c_name,cctxt->cline);
1639
1640         if (cctxt->cline < func->mod->ncLines)      
1641             fprintf(stdout,"%d\t%s",
1642                     cctxt->cline,
1643                     func->mod->cLines[cctxt->cline]->src);
1644     } else
1645         fprintf(stdout,"No stack.\n");
1646     return 0;
1647 }
1648
1649 /*-----------------------------------------------------------------*/
1650 /* cmdFinish - exec till end of current function                   */
1651 /*-----------------------------------------------------------------*/
1652 int cmdFinish (char *s, context *ctxt)
1653 {
1654     if (!ctxt || ! ctxt->func) {
1655         fprintf(stdout,"The program is not running.\n");
1656         return 0;
1657     }
1658
1659     if (srcMode == SRC_CMODE) {
1660         setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP, 
1661                        stepBpCB, ctxt->func->mod->c_name, 
1662                        ctxt->func->exitline);
1663     } else {
1664         setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP, 
1665                        stepBpCB, ctxt->func->mod->asm_name, 
1666                        ctxt->func->aexitline);
1667     }
1668
1669     simGo(-1);
1670     return 0;
1671     
1672 }
1673
1674
1675 /*-----------------------------------------------------------------*/
1676 /* cmdShow - show command                                          */
1677 /*-----------------------------------------------------------------*/
1678 int cmdShow (char *s, context *cctxt)
1679 {
1680     /* skip white space */
1681     while (*s && isspace(*s)) s++ ;
1682
1683     if (strcmp(s,"copying") == 0) {
1684         fputs(copying,stdout);
1685         return 0;
1686     }
1687     
1688     if (strcmp(s,"warranty") == 0) {
1689         fputs(warranty,stdout);
1690         return 0;
1691     }
1692
1693     return 0;
1694 }
1695