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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include <readline/readline.h>
25 #include <readline/history.h>
28 #include <Resources.h>
29 #include <QuickDraw.h>
37 #include <ToolUtils.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. */
105 {-32000, -32000, 32000, 32000};
110 /* Globals for the console window. */
112 WindowPtr console_window;
114 ControlHandle console_v_scrollbar;
116 Rect console_v_scroll_rect;
118 TEHandle console_text;
120 Rect console_text_rect;
122 /* This will go away eventually. */
123 gdb_has_a_terminal ()
131 int eventloopdone = 0;
141 Handle siow_resource;
145 str = getenv ("DEBUG_GDB");
146 if (str != NULL && str[0] != '\0')
148 if (strcmp (str, "openp") == 0)
152 /* Don't do anything if we`re running under MPW. */
156 /* Don't do anything if we're using SIOW. */
157 /* This test requires that the siow 0 resource, as defined in
158 {RIncludes}siow.r, not be messed with. If it is, then the
159 standard Mac setup below will step on SIOW's Mac setup and
160 most likely crash the machine. */
161 siow_resource = GetResource ('siow', 0);
162 if (siow_resource != nil)
167 /* Do the standard Mac environment setup. */
168 InitGraf (&QD (thePort));
170 FlushEvents (everyEvent, 0);
177 /* Color Quickdraw is different from Classic QD. */
178 SysEnvirons (2, &se);
179 has_color_qd = se.hasColorQD;
180 /* Use it if we got it. */
181 use_color_qd = has_color_qd;
185 sizerect.bottom = 1000;
186 sizerect.right = 1000;
188 sizerect.bottom = screenBits.bounds.bottom - screenBits.bounds.top;
189 sizerect.right = screenBits.bounds.right - screenBits.bounds.left;
192 /* Set up the menus. */
193 menubar = GetNewMBar (mbMain);
194 SetMenuBar (menubar);
195 /* Add the DAs etc as usual. */
196 menu = GetMHandle (mApple);
199 AddResMenu (menu, 'DRVR');
203 new_console_window ();
206 new_console_window ()
208 /* Create the main window we're going to play in. */
210 console_window = GetNewCWindow (wConsole, NULL, (WindowPtr) - 1L);
212 console_window = GetNewWindow (wConsole, NULL, (WindowPtr) - 1L);
214 SetPort (console_window);
215 console_text_rect = console_window->portRect;
216 /* Leave 8 pixels of blank space, for aesthetic reasons and to
217 make it easier to select from the beginning of a line. */
218 console_text_rect.left += 8;
219 console_text_rect.bottom -= sbarwid - 1;
220 console_text_rect.right -= sbarwid - 1;
221 console_text = TENew (&console_text_rect, &console_text_rect);
222 TESetSelect (0, 40000, console_text);
223 TEDelete (console_text);
224 TEAutoView (1, console_text);
226 console_v_scroll_rect = console_window->portRect;
227 console_v_scroll_rect.bottom -= sbarwid - 1;
228 console_v_scroll_rect.left = console_v_scroll_rect.right - sbarwid;
229 console_v_scrollbar =
230 NewControl (console_window, &console_v_scroll_rect,
231 "\p", 1, 0, 0, 0, scrollBarProc, 0L);
233 ShowWindow (console_window);
234 SelectWindow (console_window);
240 int eventloopdone = 0;
250 /* Figure out if the WaitNextEvent Trap is available. */
252 (NGetTrapAddress (0x60, ToolTrap) != NGetTrapAddress (0x9f, ToolTrap));
253 /* Pass WaitNextEvent an empty region the first time through. */
254 cursorRgn = NewRgn ();
255 /* Go into the main event-handling loop. */
256 while (!eventloopdone)
258 /* Use WaitNextEvent if it is available, otherwise GetNextEvent. */
261 get_global_mouse (&mouse);
262 adjust_cursor (mouse, cursorRgn);
263 tm = GetCaretTime ();
264 gotevent = WaitNextEvent (everyEvent, &event, tm, cursorRgn);
269 gotevent = GetNextEvent (everyEvent, &event);
271 /* First decide if the event is for a dialog or is just any old event. */
272 if (FrontWindow () != nil && IsDialogEvent (&event))
277 /* Handle all the modeless dialogs here. */
278 if (DialogSelect (&event, &dialog, &itemhit))
284 /* Make sure we have the right cursor before handling the event. */
285 adjust_cursor (event.where, cursorRgn);
295 /* Collect the global coordinates of the mouse pointer. */
297 get_global_mouse (mouse)
302 OSEventAvail (0, &evt);
306 /* Change the cursor's appearance to be appropriate for the given mouse
309 adjust_cursor (mouse, region)
315 /* Decipher an event, maybe do something with it. */
320 short part, err, rslt = 0;
329 /* See if the click happened in a special part of the screen. */
330 part = FindWindow (evt->where, &win);
335 do_menu_command (MenuSelect (evt->where));
338 SystemClick (evt, win);
341 if (win != FrontWindow ())
343 /* Bring the clicked-on window to the front. */
345 /* Fix the menu to match the new front window. */
347 /* We always want to discard the event now, since clicks in a
348 windows are often irreversible actions. */
351 /* Mouse clicks in the front window do something useful. */
352 do_mouse_down (win, evt);
355 /* Standard drag behavior, no tricks necessary. */
356 DragWindow (win, evt->where, &dragrect);
359 grow_window (win, evt->where);
363 zoom_window (win, evt->where, part);
372 key = evt->message & charCodeMask;
373 /* Check for menukey equivalents. */
374 if (evt->modifiers & cmdKey)
376 if (evt->what == keyDown)
379 do_menu_command (MenuKey (key));
384 if (evt->what == keyDown)
386 /* Random keypress, interpret it. */
387 do_keyboard_command (key);
392 activate_window ((WindowPtr) evt->message, evt->modifiers & activeFlag);
395 update_window ((WindowPtr) evt->message);
398 /* Call DIBadMount in response to a diskEvt, so that the user can format
399 a floppy. (from DTS Sample) */
400 if (HiWord (evt->message) != noErr)
402 SetPt (&pnt, 50, 50);
403 err = DIBadMount (pnt, evt->message);
407 /* Grab only a single byte. */
408 switch ((evt->message >> 24) & 0xFF)
413 inbackground = !(evt->message & 1);
414 activate_window (FrontWindow (), !inbackground);
418 case kHighLevelEvent:
419 AEProcessAppleEvent (evt);
431 /* Do any idle-time activities. */
435 TEIdle (console_text);
438 grow_window (win, where)
446 winsize = GrowWindow (win, where, &sizerect);
447 /* Only do anything if it actually changed size. */
452 if (win == console_window)
454 EraseRect (&win->portRect);
455 h = LoWord (winsize);
456 v = HiWord (winsize);
457 SizeWindow (win, h, v, 1);
458 resize_console_window ();
464 zoom_window (win, where, part)
469 ZoomWindow (win, part, (win == FrontWindow ()));
470 if (win == console_window)
472 resize_console_window ();
476 resize_console_window ()
478 adjust_console_sizes ();
479 adjust_console_scrollbars ();
480 adjust_console_text ();
481 InvalRect (&console_window->portRect);
490 v_scroll_proc (ControlHandle control, short part)
492 int oldval, amount = 0, newval;
493 int pagesize = ((*console_text)->viewRect.bottom - (*console_text)->viewRect.top) / (*console_text)->lineHeight;
496 oldval = GetCtlValue (control);
512 /* (should freak out) */
515 SetCtlValue (control, oldval - amount);
516 newval = GetCtlValue (control);
517 amount = oldval - newval;
519 TEScroll (0, amount * (*console_text)->lineHeight, console_text);
523 do_mouse_down (WindowPtr win, EventRecord * event)
527 ControlHandle control;
529 if (1 /*is_app_window(win) */ )
532 mouse = event->where;
533 GlobalToLocal (&mouse);
534 part = FindControl (mouse, win, &control);
535 if (control == console_v_scrollbar)
540 value = GetCtlValue (control);
541 part = TrackControl (control, mouse, nil);
544 value -= GetCtlValue (control);
546 TEScroll (0, value * (*console_text)->lineHeight,
551 #if 0 /* don't deal with right now */
552 #if 1 /* universal headers */
553 value = TrackControl (control, mouse, (ControlActionUPP) v_scroll_proc);
555 value = TrackControl (control, mouse, (ProcPtr) v_scroll_proc);
563 TEClick (mouse, 0, console_text);
568 scroll_text (hlines, vlines)
573 activate_window (win, activate)
581 /* It's convenient to make the activated window also be the
585 /* Activate the console window's scrollbar. */
586 if (win == console_window)
590 TEActivate (console_text);
591 /* Cause the grow icon to be redrawn at the next update. */
592 grow_rect = console_window->portRect;
593 grow_rect.top = grow_rect.bottom - sbarwid;
594 grow_rect.left = grow_rect.right - sbarwid;
595 InvalRect (&grow_rect);
599 TEDeactivate (console_text);
600 DrawGrowIcon (console_window);
602 HiliteControl (console_v_scrollbar, (activate ? 0 : 255));
609 int controls = 1, growbox = 0;
612 /* Set the updating window to be the current grafport. */
615 /* recalc_depths(); */
617 if (win == console_window)
624 UpdateControls (win, win->visRgn);
635 do_menu_command (which)
638 short menuid, menuitem;
649 menuid = HiWord (which);
650 menuitem = LoWord (which);
661 /* (should pop up help info) */
665 GetItem (GetMHandle (mApple), menuitem, daname);
666 daRefNum = OpenDeskAcc (daname);
673 if (console_window == FrontWindow ())
675 close_window (console_window);
677 new_console_window ();
688 /* handledbyda = SystemEdit(menuitem-1); */
692 TECut (console_text);
695 TECopy (console_text);
698 TEPaste (console_text);
701 TEDelete (console_text);
704 /* All of these operations need the same postprocessing. */
705 adjust_console_sizes ();
706 adjust_console_scrollbars ();
707 adjust_console_text ();
713 sprintf (cmdbuf, "target %s", "remote");
716 sprintf (cmdbuf, "run");
718 case miDebugContinue:
719 sprintf (cmdbuf, "continue");
722 sprintf (cmdbuf, "step");
725 sprintf (cmdbuf, "next");
731 /* Execute a command if one had been given. Do here because a command
732 may longjmp before we get a chance to unhilite the menu. */
733 if (strlen (cmdbuf) > 0)
734 execute_command (cmdbuf, 0);
737 char commandbuf[1000];
739 do_keyboard_command (key)
742 int startpos, endpos, i, len;
744 char buf[10], *text_str, *command, *cmd_start;
747 if (key == '\015' || key == '\003')
749 text = TEGetText (console_text);
750 HLock ((Handle) text);
752 startpos = (*console_text)->selStart;
753 endpos = (*console_text)->selEnd;
754 if (startpos != endpos)
756 len = endpos - startpos;
757 cmd_start = text_str + startpos;
761 for (i = startpos - 1; i >= 0; --i)
762 if (text_str[i] == '\015')
764 last_newline = text_str + i;
765 len = (text_str + startpos) - 1 - last_newline;
766 cmd_start = last_newline + 1;
772 strncpy (commandbuf + 1, cmd_start, len);
773 commandbuf[1 + len] = 0;
774 command = commandbuf + 1;
775 HUnlock ((Handle) text);
776 commandbuf[0] = strlen (command);
778 /* Insert a newline and recalculate before doing any command. */
780 TEKey (key, console_text);
781 TEInsert (buf, 1, console_text);
782 adjust_console_sizes ();
783 adjust_console_scrollbars ();
784 adjust_console_text ();
786 if (strlen (command) > 0)
788 execute_command (command, 0);
789 bpstat_do_actions (&stop_bpstat);
794 /* A self-inserting character. This includes delete. */
795 TEKey (key, console_text);
799 /* Draw all graphical stuff in the console window. */
803 SetPort (console_window);
804 TEUpdate (&(console_window->portRect), console_text);
807 /* Cause an update of a given window's entire contents. */
818 EraseRect (&win->portRect);
819 InvalRect (&win->portRect);
823 adjust_console_sizes ()
827 tmprect = console_window->portRect;
828 /* Move and size the scrollbar. */
829 MoveControl (console_v_scrollbar, tmprect.right - sbarwid, 0);
830 SizeControl (console_v_scrollbar, sbarwid + 1, tmprect.bottom - sbarwid + 1);
831 /* Move and size the text. */
833 tmprect.right -= sbarwid;
834 tmprect.bottom -= sbarwid;
835 InsetRect (&tmprect, 1, 1);
836 (*console_text)->destRect = tmprect;
837 /* Fiddle bottom of viewrect to be even multiple of text lines. */
838 tmprect.bottom = tmprect.top
839 + ((tmprect.bottom - tmprect.top) / (*console_text)->lineHeight)
840 * (*console_text)->lineHeight;
841 (*console_text)->viewRect = tmprect;
844 adjust_console_scrollbars ()
846 int lines, newmax, value;
848 (*console_v_scrollbar)->contrlVis = 0;
849 lines = (*console_text)->nLines;
850 newmax = lines - (((*console_text)->viewRect.bottom
851 - (*console_text)->viewRect.top)
852 / (*console_text)->lineHeight);
855 SetCtlMax (console_v_scrollbar, newmax);
856 value = ((*console_text)->viewRect.top - (*console_text)->destRect.top)
857 / (*console_text)->lineHeight;
858 SetCtlValue (console_v_scrollbar, value);
859 (*console_v_scrollbar)->contrlVis = 0xff;
860 ShowControl (console_v_scrollbar);
863 /* Scroll the TE record so that it is consistent with the scrollbar(s). */
865 adjust_console_text ()
867 TEScroll (((*console_text)->viewRect.left
868 - (*console_text)->destRect.left)
869 - 0 /* get h scroll value */ ,
870 ((((*console_text)->viewRect.top - (*console_text)->destRect.top)
871 / (*console_text)->lineHeight)
872 - GetCtlValue (console_v_scrollbar))
873 * (*console_text)->lineHeight,
877 /* Readline substitute. */
880 readline (char *prrompt)
882 return gdb_readline (prrompt);
885 char *rl_completer_word_break_characters;
887 char *rl_completer_quote_characters;
889 int (*rl_completion_entry_function) ();
893 char *rl_line_buffer;
895 char *rl_readline_name;
897 /* History substitute. */
900 add_history (char *buf)
905 stifle_history (int n)
915 read_history (char *name)
920 write_history (char *name)
925 history_expand (char *x, char **y)
930 history_get (int xxx)
938 filename_completion_function (char *text, char *name)
944 tilde_expand (char *str)
946 return strsave (str);
949 /* Modified versions of standard I/O. */
954 hacked_fprintf (FILE * fp, const char *fmt,...)
960 if (mac_app && (fp == stdout || fp == stderr))
964 ret = vsprintf (buf, fmt, ap);
965 TEInsert (buf, strlen (buf), console_text);
968 ret = vfprintf (fp, fmt, ap);
976 hacked_printf (const char *fmt,...)
982 ret = hacked_vfprintf (stdout, fmt, ap);
990 hacked_vfprintf (FILE * fp, const char *format, va_list args)
992 if (mac_app && (fp == stdout || fp == stderr))
997 ret = vsprintf (buf, format, args);
998 TEInsert (buf, strlen (buf), console_text);
999 if (strchr (buf, '\n'))
1001 adjust_console_sizes ();
1002 adjust_console_scrollbars ();
1003 adjust_console_text ();
1008 return vfprintf (fp, format, args);
1013 hacked_fputs (const char *s, FILE * fp)
1015 if (mac_app && (fp == stdout || fp == stderr))
1017 TEInsert (s, strlen (s), console_text);
1018 if (strchr (s, '\n'))
1020 adjust_console_sizes ();
1021 adjust_console_scrollbars ();
1022 adjust_console_text ();
1027 return fputs (s, fp);
1032 hacked_fputc (const char c, FILE * fp)
1034 if (mac_app && (fp == stdout || fp == stderr))
1039 TEInsert (buf, 1, console_text);
1042 adjust_console_sizes ();
1043 adjust_console_scrollbars ();
1044 adjust_console_text ();
1049 return fputc (c, fp);
1054 hacked_putc (const char c, FILE * fp)
1056 if (mac_app && (fp == stdout || fp == stderr))
1061 TEInsert (buf, 1, console_text);
1064 adjust_console_sizes ();
1065 adjust_console_scrollbars ();
1066 adjust_console_text ();
1071 return fputc (c, fp);
1076 hacked_fflush (FILE * fp)
1078 if (mac_app && (fp == stdout || fp == stderr))
1080 adjust_console_sizes ();
1081 adjust_console_scrollbars ();
1082 adjust_console_text ();
1090 hacked_fgetc (FILE * fp)
1092 if (mac_app && (fp == stdin))
1094 /* Catch any attempts to use this. */
1095 DebugStr ("\pShould not be reading from stdin!");