1 /* TUI support I/O functions.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Hewlett-Packard Company.
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. */
28 #include "tuiCommand.h"
33 /* The Solaris header files seem to provide no declaration for this at
34 all when __STDC__ is defined. This shouldn't conflict with
36 extern char *tgoto ();
40 /********************************************
41 ** LOCAL STATIC FORWARD DECLS **
42 ********************************************/
43 static void _updateCommandInfo (int);
44 static unsigned int _tuiHandleResizeDuringIO (unsigned int);
47 /*********************************************************************************
48 ** PUBLIC FUNCTIONS **
49 *********************************************************************************/
52 ** tuiPuts_unfiltered().
53 ** Function to put a string to the command window
54 ** When running in TUI mode, this is the "hook"
55 ** for fputs_unfiltered(). That is, all debugger
56 ** output eventually makes it's way to the bottom-level
57 ** routine fputs_unfiltered (main.c), which (in TUI
58 ** mode), calls tuiPuts_unfiltered().
61 tuiPuts_unfiltered (const char *string
, struct ui_file
* stream
)
63 int len
= strlen (string
);
66 for (i
= 0; i
< len
; i
++)
68 if (string
[i
] == '\n' || string
[i
] == '\r')
72 if ((cmdWin
->detail
.commandInfo
.curch
+ 1) > cmdWin
->generic
.width
)
77 mvwinsch (cmdWin
->generic
.handle
,
78 cmdWin
->detail
.commandInfo
.curLine
,
79 cmdWin
->detail
.commandInfo
.curch
++,
81 wmove (cmdWin
->generic
.handle
,
82 cmdWin
->detail
.commandInfo
.curLine
,
83 cmdWin
->detail
.commandInfo
.curch
);
86 mvwaddch (cmdWin
->generic
.handle
,
87 cmdWin
->detail
.commandInfo
.curLine
,
88 cmdWin
->detail
.commandInfo
.curch
++,
92 tuiRefreshWin (&cmdWin
->generic
);
95 } /* tuiPuts_unfiltered */
97 /* A cover routine for tputs().
98 * tputs() is called from the readline package to put
99 * out strings representing cursor positioning.
100 * In TUI mode (non-XDB-style), tui_tputs() is called instead.
102 * The reason we need to hook tputs() is:
103 * Since the output is going to curses and not to
104 * a raw terminal, we need to intercept these special
105 * sequences, and handle them them here.
107 * This function seems to be correctly handling all sequences
108 * aimed at hpterm's, but there is additional work to do
109 * for xterm's and dtterm's. I abandoned further work on this
110 * in favor of "XDB style". In "XDB style", the command region
111 * looks like terminal, not a curses window, and this routine
112 * is not called. - RT
115 tui_tputs (str
, affcnt
, putfunc
)
118 int (*putfunc
) (int);
120 extern char *rl_prompt
; /* the prompt string */
122 /* This set of globals are defined and initialized
123 * by the readline package.
125 * Note we're assuming tui_tputs() is being called
126 * by the readline package. That's because we're recognizing
127 * that a given string is being passed by
128 * matching the string address against readline's
129 * term_<whatever> global. To make this more general,
130 * we'd have to actually recognize the termcap sequence
131 * inside the string (more work than I want to do). - RT
133 * We don't see or need to handle every one of these here;
134 * this is just the full list defined in readline/readline.c
136 extern char *term_backspace
;
137 extern char *term_clreol
;
138 extern char *term_clrpag
;
139 extern char *term_cr
;
140 extern char *term_dc
;
141 extern char *term_ei
;
142 extern char *term_goto
;
143 extern char *term_ic
;
144 extern char *term_im
;
145 extern char *term_mm
;
146 extern char *term_mo
;
147 extern char *term_up
;
148 extern char *term_scroll_region
;
149 extern char *term_memory_lock
;
150 extern char *term_memory_unlock
;
151 extern char *term_cursor_move
;
152 extern char *visible_bell
;
154 /* Sanity check - if not TUI, just call tputs() */
156 tputs (str
, affcnt
, putfunc
);
158 /* The strings we special-case are handled first */
160 if (str
== term_backspace
)
164 /* We see this on an emacs control-B.
165 * I.e., it's like the left-arrow key (not like the backspace key).
166 * The effect that readline wants when it transmits this
167 * character to us is simply to back up one character
168 * (but not to write a space over the old character).
171 _updateCommandInfo (-1);
172 wmove (cmdWin
->generic
.handle
,
173 cmdWin
->detail
.commandInfo
.curLine
,
174 cmdWin
->detail
.commandInfo
.curch
);
175 wrefresh (cmdWin
->generic
.handle
);
178 else if (str
== term_clreol
)
181 /* Clear to end of line. */
182 wclrtoeol (cmdWin
->generic
.handle
);
183 wrefresh (cmdWin
->generic
.handle
);
186 else if (str
== term_cr
)
189 /* Carriage return */
190 _updateCommandInfo (-cmdWin
->detail
.commandInfo
.curch
);
191 wmove (cmdWin
->generic
.handle
,
192 cmdWin
->detail
.commandInfo
.curLine
,
193 0 /* readline will rewrite the prompt from 0 */ );
194 wrefresh (cmdWin
->generic
.handle
);
197 else if (str
== term_goto
)
200 /* This is actually a tgoto() specifying a character position,
201 * followed by either a term_IC/term_DC which [I think] means
202 * insert/delete one character at that position.
203 * There are complications with this one - need to either
204 * extract the position from the string, or have a backdoor
205 * means of communicating it from ../readline/display.c.
206 * So this one is not yet implemented.
207 * Not doing it seems to have no ill effects on command-line-editing
208 * that I've noticed so far. - RT
212 else if (str
== term_dc
)
215 /* Delete character at current cursor position */
216 wdelch (cmdWin
->generic
.handle
);
217 wrefresh (cmdWin
->generic
.handle
);
220 else if (str
== term_im
)
223 /* Turn on insert mode. */
227 else if (str
== term_ei
)
230 /* Turn off insert mode. */
233 /* Strings we know about but don't handle
234 * specially here are just passed along to tputs().
236 * These are not handled because (as far as I can tell)
237 * they are not actually emitted by the readline package
238 * in the course of doing command-line editing. Some of them
239 * theoretically could be used in the future, in which case we'd
240 * need to handle them.
243 else if (str
== term_ic
|| /* insert character */
244 str
== term_cursor_move
|| /* cursor move */
245 str
== term_clrpag
|| /* clear page */
246 str
== term_mm
|| /* turn on meta key */
247 str
== term_mo
|| /* turn off meta key */
248 str
== term_up
|| /* up one line (not expected) */
249 str
== term_scroll_region
|| /* set scroll region */
250 str
== term_memory_lock
|| /* lock screen above cursor */
251 str
== term_memory_unlock
|| /* unlock screen above cursor */
254 tputs (str
, affcnt
, putfunc
);
257 { /* something else */
258 tputs (str
, affcnt
, putfunc
);
265 ** Wrapper around wgetch with the window in a va_list
268 tui_vwgetch (va_list args
)
273 window
= va_arg (args
, WINDOW
*);
275 return ((unsigned int) wgetch (window
));
281 ** Get a character from the command window.
282 ** This is called from the readline package,
284 ** tuiGetc() [here], called from
285 ** readline code [in ../readline/], called from
286 ** command_line_input() in top.c
292 extern char *rl_prompt
;
293 extern char *rl_line_buffer
;
296 /* Call the curses routine that reads one character */
298 ch
= (unsigned int) vcatch_errors ((OpaqueFuncPtr
) tui_vwgetch
,
299 cmdWin
->generic
.handle
);
301 ch
= wgetch (cmdWin
->generic
.handle
);
303 ch
= _tuiHandleResizeDuringIO (ch
);
305 if (m_isCommandChar (ch
))
306 { /* Handle prev/next/up/down here */
308 ch
= tuiDispatchCtrlChar (ch
);
309 cmdWin
->detail
.commandInfo
.curch
= strlen (rl_prompt
) + rl_point
;
310 tuiTermUnsetup (0, cmdWin
->detail
.commandInfo
.curch
);
312 if (ch
== '\n' || ch
== '\r' || ch
== '\f')
313 cmdWin
->detail
.commandInfo
.curch
= 0;
315 tuiIncrCommandCharCountBy (1);
324 /*elz: this function reads a line of input from the user and
325 puts it in a static buffer. Subsequent calls to this same function
326 obtain one char at the time, providing the caller with a behavior
327 similar to fgetc. When the input is buffered, the backspaces have
328 the needed effect, i.e. ignore the last char active in the buffer */
329 /* so far this function is called only from the query function in
336 static unsigned char _ibuffer
[512];
337 static int index_read
= -1;
338 static int length_of_answer
= -1;
341 if (length_of_answer
== -1)
343 /* this is the first time through, need to read the answer */
346 /* Call the curses routine that reads one character */
347 ch
= (unsigned int) wgetch (cmdWin
->generic
.handle
);
356 while (ch
!= '\r' && ch
!= '\n');
358 length_of_answer
= pos
;
362 ch
= _ibuffer
[index_read
];
365 if (index_read
== length_of_answer
)
367 /*this is the last time through, reset for next query */
369 length_of_answer
= -1;
372 wrefresh (cmdWin
->generic
.handle
);
375 } /* tuiBufferGetc */
379 ** tuiStartNewLines().
382 tuiStartNewLines (int numLines
)
386 if (cmdWin
->generic
.viewportHeight
> 1 &&
387 cmdWin
->detail
.commandInfo
.curLine
< cmdWin
->generic
.viewportHeight
)
388 cmdWin
->detail
.commandInfo
.curLine
+= numLines
;
390 scroll (cmdWin
->generic
.handle
);
391 cmdWin
->detail
.commandInfo
.curch
= 0;
392 wmove (cmdWin
->generic
.handle
,
393 cmdWin
->detail
.commandInfo
.curLine
,
394 cmdWin
->detail
.commandInfo
.curch
);
395 tuiRefreshWin (&cmdWin
->generic
);
399 } /* tuiStartNewLines */
403 ** tui_vStartNewLines().
404 ** With numLines in a va_list
407 tui_vStartNewLines (va_list args
)
409 int numLines
= va_arg (args
, int);
411 tuiStartNewLines (numLines
);
414 } /* tui_vStartNewLines */
417 /****************************************************************************
418 ** LOCAL STATIC FUNCTIONS **
419 *****************************************************************************/
423 ** _tuiHandleResizeDuringIO
424 ** This function manages the cleanup when a resize has occured
425 ** From within a call to getch() or read. Returns the character
426 ** to return from getc or read.
429 _tuiHandleResizeDuringIO (unsigned int originalCh
)
430 /* the char just read */
432 if (tuiWinResized ())
436 tuiSetWinResizedTo (FALSE
);
442 } /* _tuiHandleResizeDuringIO */
446 ** _updateCommandInfo().
447 ** Function to update the command window information.
450 _updateCommandInfo (int sizeOfString
)
454 cmdWin
->detail
.commandInfo
.curch
) > cmdWin
->generic
.width
)
456 int newCurch
= sizeOfString
+ cmdWin
->detail
.commandInfo
.curch
;
458 tuiStartNewLines (1);
459 cmdWin
->detail
.commandInfo
.curch
= newCurch
- cmdWin
->generic
.width
;
462 cmdWin
->detail
.commandInfo
.curch
+= sizeOfString
;
465 } /* _updateCommandInfo */
468 /* Looked at in main.c, fputs_unfiltered(), to decide
469 * if it's safe to do standard output to the command window.
471 int tui_owns_terminal
= 0;
473 /* Called to set up the terminal for TUI (curses) I/O.
474 * We do this either on our way "in" to GDB after target
475 * program execution, or else within tuiDo just before
476 * going off to TUI routines.
480 tuiTermSetup (int turn_off_echo
)
486 extern char *term_scroll_region
;
487 extern char *term_cursor_move
;
488 extern char *term_memory_lock
;
489 extern char *term_memory_unlock
;
491 /* Turn off echoing, since the TUI does not
492 * expect echoing. Below I only put in the TERMIOS
493 * case, since that is what applies on HP-UX. turn_off_echo
494 * is 1 except for the case where we're being called
495 * on a "quit", in which case we want to leave echo on.
502 tio
.c_lflag
&= ~(ECHO
);
503 tcsetattr (0, TCSANOW
, &tio
);
507 /* Compute the start and end lines of the command
508 * region. (Actually we only use end here)
510 start
= winList
[CMD_WIN
]->generic
.origin
.y
;
511 end
= start
+ winList
[CMD_WIN
]->generic
.height
- 1;
512 endcol
= winList
[CMD_WIN
]->generic
.width
- 1;
514 if (term_memory_unlock
)
517 /* Un-do the effect of the memory lock in terminal_inferior() */
518 tputs (term_memory_unlock
, 1, (int (*) (int)) putchar
);
522 else if (term_scroll_region
)
525 /* Un-do the effect of setting scroll region in terminal_inferior() */
526 /* I'm actually not sure how to do this (we don't know for
527 * sure what the scroll region was *before* we changed it),
528 * but I'll guess that setting it to the whole screen is
529 * the right thing. So, ...
532 /* Set scroll region to be 0..end */
533 buffer
= (char *) tgoto (term_scroll_region
, end
, 0);
534 tputs (buffer
, 1, (int (*) (int)) putchar
);
536 } /* else we're out of luck */
538 /* This is an attempt to keep the logical & physical
539 * cursor in synch, going into curses. Without this,
540 * curses seems to be confused by the fact that
541 * GDB has physically moved the curser on it. One
542 * visible effect of removing this code is that the
543 * locator window fails to get updated and the line
544 * of text that *should* go into the locator window
545 * often goes to the wrong place.
547 /* What's done here is to tell curses to write a ' '
548 * at the bottom right corner of the screen.
549 * The idea is to wind up with the cursor in a known
551 * Note I'm relying on refresh()
552 * only writing what changed (the space),
553 * not the whole screen.
556 move (end
, endcol
- 1);
560 tui_owns_terminal
= 1;
564 /* Called to set up the terminal for target program I/O, meaning I/O
565 * is confined to the command-window area. We also call this on our
566 * way out of tuiDo, thus setting up the terminal this way for
567 * debugger command I/O. */
569 tuiTermUnsetup (int turn_on_echo
, int to_column
)
575 /* The next bunch of things are from readline */
576 extern char *term_scroll_region
;
577 extern char *term_cursor_move
;
578 extern char *term_memory_lock
;
579 extern char *term_memory_unlock
;
580 extern char *term_se
;
582 /* We need to turn on echoing, since the TUI turns it off */
583 /* Below I only put in the TERMIOS case, since that
584 * is what applies on HP-UX.
591 tio
.c_lflag
|= (ECHO
);
592 tcsetattr (0, TCSANOW
, &tio
);
596 /* Compute the start and end lines of the command
597 * region, as well as the last "real" line of
598 * the region (normally same as end, except when
599 * we're first populating the region)
601 start
= winList
[CMD_WIN
]->generic
.origin
.y
;
602 end
= start
+ winList
[CMD_WIN
]->generic
.height
- 1;
603 curline
= start
+ winList
[CMD_WIN
]->detail
.commandInfo
.curLine
;
605 /* We want to confine target I/O to the command region.
606 * In order to do so, we must either have "memory lock"
607 * (hpterm's) or "scroll regions" (xterm's).
609 if (term_cursor_move
&& term_memory_lock
)
612 /* Memory lock means lock region above cursor.
613 * So first position the cursor, then call memory lock.
615 buffer
= tgoto (term_cursor_move
, 0, start
);
616 tputs (buffer
, 1, (int (*) (int)) putchar
);
617 tputs (term_memory_lock
, 1, (int (*) (int)) putchar
);
620 else if (term_scroll_region
)
623 /* Set the scroll region to the command window */
624 buffer
= tgoto (term_scroll_region
, end
, start
);
625 tputs (buffer
, 1, (int (*) (int)) putchar
);
627 } /* else we can't do anything about target I/O */
629 /* Also turn off standout mode, in case it is on */
631 tputs (term_se
, 1, (int (*) (int)) putchar
);
633 /* Now go to the appropriate spot on the end line */
634 buffer
= tgoto (term_cursor_move
, to_column
, end
);
635 tputs (buffer
, 1, (int (*) (int)) putchar
);
638 tui_owns_terminal
= 0;
639 } /* tuiTermUnsetup */