1 /* Top level support for Mac interface to GDB, the GNU debugger.
2 Copyright 1994 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Stan Shebs.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <Resources.h>
29 #include <QuickDraw.h>
37 #include <ToolUtils.h>
48 #include <GestaltEqu.h>
49 #include <PPCToolbox.h>
50 #include <AppleEvents.h>
51 #include <StandardFile.h>
55 #define QD(whatever) (qd.##whatever)
56 #define QDPat(whatever) (&(qd.##whatever))
60 #define QD(whatever) (whatever)
63 #define p2c(pstr,cbuf) \
64 strncpy(cbuf, ((char *) (pstr) + 1), pstr[0]); \
67 #define pascalify(STR) \
68 sprintf(tmpbuf, " %s", STR); \
69 tmpbuf[0] = strlen(STR);
72 #include "call-cmds.h"
77 #include "breakpoint.h"
79 #include "expression.h"
86 /* This is true if we are running as a standalone application. */
90 /* This is true if we are using WaitNextEvent. */
94 /* This is true if we have Color Quickdraw. */
98 /* This is true if we are using Color Quickdraw. */
104 Rect dragrect
= { -32000, -32000, 32000, 32000 };
109 /* Globals for the console window. */
111 WindowPtr console_window
;
113 ControlHandle console_v_scrollbar
;
115 Rect console_v_scroll_rect
;
117 TEHandle console_text
;
119 Rect console_text_rect
;
121 /* This will go away eventually. */
122 gdb_has_a_terminal () { return 1; }
127 int eventloopdone
= 0;
137 Handle siow_resource
;
141 str
= getenv("DEBUG_GDB");
142 if (str
!= NULL
&& str
[0] != '\0')
144 if (strcmp(str
, "openp") == 0)
148 /* Don't do anything if we`re running under MPW. */
152 /* Don't do anything if we're using SIOW. */
153 /* This test requires that the siow 0 resource, as defined in
154 {RIncludes}siow.r, not be messed with. If it is, then the
155 standard Mac setup below will step on SIOW's Mac setup and
156 most likely crash the machine. */
157 siow_resource
= GetResource('siow', 0);
158 if (siow_resource
!= nil
)
163 /* Do the standard Mac environment setup. */
164 InitGraf (&QD (thePort
));
166 FlushEvents (everyEvent
, 0);
173 /* Color Quickdraw is different from Classic QD. */
175 has_color_qd
= se
.hasColorQD
;
176 /* Use it if we got it. */
177 use_color_qd
= has_color_qd
;
181 sizerect
.bottom
= 1000;
182 sizerect
.right
= 1000;
184 sizerect
.bottom
= screenBits
.bounds
.bottom
- screenBits
.bounds
.top
;
185 sizerect
.right
= screenBits
.bounds
.right
- screenBits
.bounds
.left
;
188 /* Set up the menus. */
189 menubar
= GetNewMBar (mbMain
);
190 SetMenuBar (menubar
);
191 /* Add the DAs etc as usual. */
192 menu
= GetMHandle (mApple
);
194 AddResMenu (menu
, 'DRVR');
198 new_console_window ();
201 new_console_window ()
203 /* Create the main window we're going to play in. */
205 console_window
= GetNewCWindow (wConsole
, NULL
, (WindowPtr
) -1L);
207 console_window
= GetNewWindow (wConsole
, NULL
, (WindowPtr
) -1L);
209 SetPort (console_window
);
210 console_text_rect
= console_window
->portRect
;
211 /* Leave 8 pixels of blank space, for aesthetic reasons and to
212 make it easier to select from the beginning of a line. */
213 console_text_rect
.left
+= 8;
214 console_text_rect
.bottom
-= sbarwid
- 1;
215 console_text_rect
.right
-= sbarwid
- 1;
216 console_text
= TENew (&console_text_rect
, &console_text_rect
);
217 TESetSelect (0, 40000, console_text
);
218 TEDelete (console_text
);
219 TEAutoView (1, console_text
);
221 console_v_scroll_rect
= console_window
->portRect
;
222 console_v_scroll_rect
.bottom
-= sbarwid
- 1;
223 console_v_scroll_rect
.left
= console_v_scroll_rect
.right
- sbarwid
;
224 console_v_scrollbar
=
225 NewControl (console_window
, &console_v_scroll_rect
,
226 "\p", 1, 0, 0, 0, scrollBarProc
, 0L);
228 ShowWindow (console_window
);
229 SelectWindow (console_window
);
235 int eventloopdone
= 0;
245 /* Figure out if the WaitNextEvent Trap is available. */
247 (NGetTrapAddress (0x60, ToolTrap
) != NGetTrapAddress (0x9f, ToolTrap
));
248 /* Pass WaitNextEvent an empty region the first time through. */
249 cursorRgn
= NewRgn ();
250 /* Go into the main event-handling loop. */
251 while (!eventloopdone
)
253 /* Use WaitNextEvent if it is available, otherwise GetNextEvent. */
256 get_global_mouse (&mouse
);
257 adjust_cursor (mouse
, cursorRgn
);
259 gotevent
= WaitNextEvent (everyEvent
, &event
, tm
, cursorRgn
);
264 gotevent
= GetNextEvent (everyEvent
, &event
);
266 /* First decide if the event is for a dialog or is just any old event. */
267 if (FrontWindow () != nil
&& IsDialogEvent (&event
))
272 /* Handle all the modeless dialogs here. */
273 if (DialogSelect (&event
, &dialog
, &itemhit
))
279 /* Make sure we have the right cursor before handling the event. */
280 adjust_cursor (event
.where
, cursorRgn
);
290 /* Collect the global coordinates of the mouse pointer. */
292 get_global_mouse (mouse
)
297 OSEventAvail (0, &evt
);
301 /* Change the cursor's appearance to be appropriate for the given mouse
304 adjust_cursor (mouse
, region
)
310 /* Decipher an event, maybe do something with it. */
315 short part
, err
, rslt
= 0;
324 /* See if the click happened in a special part of the screen. */
325 part
= FindWindow (evt
->where
, &win
);
330 do_menu_command (MenuSelect (evt
->where
));
333 SystemClick (evt
, win
);
336 if (win
!= FrontWindow ())
338 /* Bring the clicked-on window to the front. */
340 /* Fix the menu to match the new front window. */
342 /* We always want to discard the event now, since clicks in a
343 windows are often irreversible actions. */
345 /* Mouse clicks in the front window do something useful. */
346 do_mouse_down (win
, evt
);
349 /* Standard drag behavior, no tricks necessary. */
350 DragWindow (win
, evt
->where
, &dragrect
);
353 grow_window (win
, evt
->where
);
357 zoom_window (win
, evt
->where
, part
);
366 key
= evt
->message
& charCodeMask
;
367 /* Check for menukey equivalents. */
368 if (evt
->modifiers
& cmdKey
)
370 if (evt
->what
== keyDown
)
373 do_menu_command (MenuKey (key
));
378 if (evt
->what
== keyDown
)
380 /* Random keypress, interpret it. */
381 do_keyboard_command (key
);
386 activate_window ((WindowPtr
) evt
->message
, evt
->modifiers
& activeFlag
);
389 update_window ((WindowPtr
) evt
->message
);
392 /* Call DIBadMount in response to a diskEvt, so that the user can format
393 a floppy. (from DTS Sample) */
394 if (HiWord (evt
->message
) != noErr
)
396 SetPt (&pnt
, 50, 50);
397 err
= DIBadMount (pnt
, evt
->message
);
401 /* Grab only a single byte. */
402 switch ((evt
->message
>> 24) & 0xFF)
407 inbackground
= !(evt
->message
& 1);
408 activate_window (FrontWindow (), !inbackground
);
412 case kHighLevelEvent
:
413 AEProcessAppleEvent (evt
);
425 /* Do any idle-time activities. */
429 TEIdle (console_text
);
432 grow_window (win
, where
)
440 winsize
= GrowWindow (win
, where
, &sizerect
);
441 /* Only do anything if it actually changed size. */
446 if (win
== console_window
)
448 EraseRect (&win
->portRect
);
449 h
= LoWord (winsize
);
450 v
= HiWord (winsize
);
451 SizeWindow (win
, h
, v
, 1);
452 resize_console_window ();
458 zoom_window (win
, where
, part
)
463 ZoomWindow (win
, part
, (win
== FrontWindow ()));
464 if (win
== console_window
)
466 resize_console_window ();
470 resize_console_window ()
472 adjust_console_sizes ();
473 adjust_console_scrollbars ();
474 adjust_console_text ();
475 InvalRect (&console_window
->portRect
);
484 v_scroll_proc (ControlHandle control
, short part
)
486 int oldval
, amount
= 0, newval
;
487 int pagesize
= ((*console_text
)->viewRect
.bottom
- (*console_text
)->viewRect
.top
) / (*console_text
)->lineHeight
;
490 oldval
= GetCtlValue (control
);
506 /* (should freak out) */
509 SetCtlValue(control
, oldval
- amount
);
510 newval
= GetCtlValue (control
);
511 amount
= oldval
- newval
;
513 TEScroll (0, amount
* (*console_text
)->lineHeight
, console_text
);
517 do_mouse_down (WindowPtr win
, EventRecord
*event
)
521 ControlHandle control
;
523 if (1 /*is_app_window(win)*/)
526 mouse
= event
->where
;
527 GlobalToLocal (&mouse
);
528 part
= FindControl(mouse
, win
, &control
);
529 if (control
== console_v_scrollbar
)
534 value
= GetCtlValue (control
);
535 part
= TrackControl (control
, mouse
, nil
);
538 value
-= GetCtlValue (control
);
540 TEScroll(0, value
* (*console_text
)->lineHeight
,
545 value
= TrackControl (control
, mouse
, (ProcPtr
) v_scroll_proc
);
551 TEClick (mouse
, 0, console_text
);
556 scroll_text (hlines
, vlines
)
561 activate_window (win
, activate
)
567 if (win
== nil
) return;
568 /* It's convenient to make the activated window also be the
572 /* Activate the console window's scrollbar. */
573 if (win
== console_window
)
577 TEActivate (console_text
);
578 /* Cause the grow icon to be redrawn at the next update. */
579 grow_rect
= console_window
->portRect
;
580 grow_rect
.top
= grow_rect
.bottom
- sbarwid
;
581 grow_rect
.left
= grow_rect
.right
- sbarwid
;
582 InvalRect (&grow_rect
);
586 TEDeactivate (console_text
);
587 DrawGrowIcon (console_window
);
589 HiliteControl (console_v_scrollbar
, (activate
? 0 : 255));
596 int controls
= 1, growbox
= 0;
599 /* Set the updating window to be the current grafport. */
602 /* recalc_depths(); */
604 if (win
== console_window
)
611 UpdateControls (win
, win
->visRgn
);
622 do_menu_command (which
)
625 short menuid
, menuitem
;
636 menuid
= HiWord (which
);
637 menuitem
= LoWord (which
);
648 /* (should pop up help info) */
652 GetItem (GetMHandle (mApple
), menuitem
, daname
);
653 daRefNum
= OpenDeskAcc (daname
);
660 if (console_window
== FrontWindow ())
662 close_window (console_window
);
664 new_console_window ();
675 /* handledbyda = SystemEdit(menuitem-1); */
679 TECut (console_text
);
682 TECopy (console_text
);
685 TEPaste (console_text
);
688 TEDelete (console_text
);
691 /* All of these operations need the same postprocessing. */
692 adjust_console_sizes ();
693 adjust_console_scrollbars ();
694 adjust_console_text ();
700 sprintf (cmdbuf
, "target %s", "remote");
703 sprintf (cmdbuf
, "run");
705 case miDebugContinue
:
706 sprintf (cmdbuf
, "continue");
709 sprintf (cmdbuf
, "step");
712 sprintf (cmdbuf
, "next");
718 /* Execute a command if one had been given. Do here because a command
719 may longjmp before we get a chance to unhilite the menu. */
720 if (strlen (cmdbuf
) > 0)
721 execute_command (cmdbuf
, 0);
724 char commandbuf
[1000];
726 do_keyboard_command (key
)
729 int startpos
, endpos
, i
, len
;
731 char buf
[10], *text_str
, *command
, *cmd_start
;
734 if (key
== '\015' || key
== '\003')
736 text
= TEGetText (console_text
);
737 HLock ((Handle
) text
);
739 startpos
= (*console_text
)->selStart
;
740 endpos
= (*console_text
)->selEnd
;
741 if (startpos
!= endpos
)
743 len
= endpos
- startpos
;
744 cmd_start
= text_str
+ startpos
;
748 for (i
= startpos
- 1; i
>= 0; --i
)
749 if (text_str
[i
] == '\015')
751 last_newline
= text_str
+ i
;
752 len
= (text_str
+ startpos
) - 1 - last_newline
;
753 cmd_start
= last_newline
+ 1;
755 if (len
> 1000) len
= 999;
756 if (len
< 0) len
= 0;
757 strncpy (commandbuf
+ 1, cmd_start
, len
);
758 commandbuf
[1 + len
] = 0;
759 command
= commandbuf
+ 1;
760 HUnlock ((Handle
) text
);
761 commandbuf
[0] = strlen(command
);
763 /* Insert a newline and recalculate before doing any command. */
765 TEKey (key
, console_text
);
766 TEInsert (buf
, 1, console_text
);
767 adjust_console_sizes ();
768 adjust_console_scrollbars ();
769 adjust_console_text ();
771 if (strlen (command
) > 0)
773 execute_command (command
, 0);
774 bpstat_do_actions (&stop_bpstat
);
779 /* A self-inserting character. This includes delete. */
780 TEKey (key
, console_text
);
784 /* Draw all graphical stuff in the console window. */
788 SetPort (console_window
);
789 TEUpdate (&(console_window
->portRect
), console_text
);
792 /* Cause an update of a given window's entire contents. */
799 if (win
== nil
) return;
802 EraseRect (&win
->portRect
);
803 InvalRect (&win
->portRect
);
807 adjust_console_sizes ()
811 tmprect
= console_window
->portRect
;
812 /* Move and size the scrollbar. */
813 MoveControl (console_v_scrollbar
, tmprect
.right
- sbarwid
, 0);
814 SizeControl (console_v_scrollbar
, sbarwid
+ 1, tmprect
.bottom
- sbarwid
+ 1);
815 /* Move and size the text. */
817 tmprect
.right
-= sbarwid
;
818 tmprect
.bottom
-= sbarwid
;
819 InsetRect(&tmprect
, 1, 1);
820 (*console_text
)->destRect
= tmprect
;
821 /* Fiddle bottom of viewrect to be even multiple of text lines. */
822 tmprect
.bottom
= tmprect
.top
823 + ((tmprect
.bottom
- tmprect
.top
) / (*console_text
)->lineHeight
)
824 * (*console_text
)->lineHeight
;
825 (*console_text
)->viewRect
= tmprect
;
828 adjust_console_scrollbars ()
830 int lines
, newmax
, value
;
832 (*console_v_scrollbar
)->contrlVis
= 0;
833 lines
= (*console_text
)->nLines
;
834 newmax
= lines
- (((*console_text
)->viewRect
.bottom
835 - (*console_text
)->viewRect
.top
)
836 / (*console_text
)->lineHeight
);
837 if (newmax
< 0) newmax
= 0;
838 SetCtlMax (console_v_scrollbar
, newmax
);
839 value
= ((*console_text
)->viewRect
.top
- (*console_text
)->destRect
.top
)
840 / (*console_text
)->lineHeight
;
841 SetCtlValue (console_v_scrollbar
, value
);
842 (*console_v_scrollbar
)->contrlVis
= 0xff;
843 ShowControl (console_v_scrollbar
);
846 /* Scroll the TE record so that it is consistent with the scrollbar(s). */
848 adjust_console_text ()
850 TEScroll (((*console_text
)->viewRect
.left
851 - (*console_text
)->destRect
.left
)
852 - 0 /* get h scroll value */,
853 ((((*console_text
)->viewRect
.top
- (*console_text
)->destRect
.top
)
854 / (*console_text
)->lineHeight
)
855 - GetCtlValue (console_v_scrollbar
))
856 * (*console_text
)->lineHeight
,
860 /* Readline substitute. */
863 readline (char *prrompt
)
865 return gdb_readline (prrompt
);
868 char *rl_completer_word_break_characters
;
870 char *rl_completer_quote_characters
;
872 int (*rl_completion_entry_function
) ();
876 char *rl_line_buffer
;
878 char *rl_readline_name
;
880 /* History substitute. */
883 add_history (char *buf
)
888 stifle_history (int n
)
898 read_history (char *name
)
903 write_history (char *name
)
908 history_expand (char *x
, char **y
)
913 history_get (int xxx
)
921 filename_completion_function (char *text
, char *name
)
927 tilde_expand (char *str
)
929 return strsave (str
);
932 /* Modified versions of standard I/O. */
939 hacked_fprintf (FILE *fp
, const char *fmt
, ...)
945 if (mac_app
&& (fp
== stdout
|| fp
== stderr
))
949 ret
= vsprintf(buf
, fmt
, ap
);
950 TEInsert (buf
, strlen(buf
), console_text
);
953 ret
= vfprintf (fp
, fmt
, ap
);
961 hacked_printf (const char *fmt
, ...)
967 ret
= hacked_vfprintf(stdout
, fmt
, ap
);
975 hacked_vfprintf (FILE *fp
, const char *format
, va_list args
)
977 if (mac_app
&& (fp
== stdout
|| fp
== stderr
))
982 ret
= vsprintf(buf
, format
, args
);
983 TEInsert (buf
, strlen(buf
), console_text
);
984 if (strchr(buf
, '\n'))
986 adjust_console_sizes ();
987 adjust_console_scrollbars ();
988 adjust_console_text ();
993 return vfprintf (fp
, format
, args
);
998 hacked_fputs (const char *s
, FILE *fp
)
1000 if (mac_app
&& (fp
== stdout
|| fp
== stderr
))
1002 TEInsert (s
, strlen(s
), console_text
);
1003 if (strchr(s
, '\n'))
1005 adjust_console_sizes ();
1006 adjust_console_scrollbars ();
1007 adjust_console_text ();
1012 return fputs (s
, fp
);
1017 hacked_fputc (const char c
, FILE *fp
)
1019 if (mac_app
&& (fp
== stdout
|| fp
== stderr
))
1024 TEInsert (buf
, 1, console_text
);
1027 adjust_console_sizes ();
1028 adjust_console_scrollbars ();
1029 adjust_console_text ();
1034 return fputc (c
, fp
);
1039 hacked_putc (const char c
, FILE *fp
)
1041 if (mac_app
&& (fp
== stdout
|| fp
== stderr
))
1046 TEInsert (buf
, 1, console_text
);
1049 adjust_console_sizes ();
1050 adjust_console_scrollbars ();
1051 adjust_console_text ();
1056 return fputc (c
, fp
);
1061 hacked_fflush (FILE *fp
)
1063 if (mac_app
&& (fp
== stdout
|| fp
== stderr
))
1065 adjust_console_sizes ();
1066 adjust_console_scrollbars ();
1067 adjust_console_text ();
1075 hacked_fgetc (FILE *fp
)
1077 if (mac_app
&& (fp
== stdin
))
1079 /* Catch any attempts to use this. */
1080 DebugStr("\pShould not be reading from stdin!");