138eec520a2d90e2610ad5d847393189ce6eea9c
[binutils-gdb.git] / gdb / tui / tuiIO.c
1 /* TUI support I/O functions.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Hewlett-Packard Company.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
21
22 #include <stdio.h>
23 #include "defs.h"
24 #include "terminal.h"
25 #include "tui.h"
26 #include "tuiData.h"
27 #include "tuiIO.h"
28 #include "tuiCommand.h"
29 #include "tuiWin.h"
30
31 #include <stdarg.h>
32
33 /* The Solaris header files seem to provide no declaration for this at
34 all when __STDC__ is defined. This shouldn't conflict with
35 anything. */
36 extern char *tgoto ();
37
38 int insert_mode = 0;
39
40 /********************************************
41 ** LOCAL STATIC FORWARD DECLS **
42 ********************************************/
43 static void _updateCommandInfo (int);
44 static unsigned int _tuiHandleResizeDuringIO (unsigned int);
45
46
47 /*********************************************************************************
48 ** PUBLIC FUNCTIONS **
49 *********************************************************************************/
50
51 /*
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().
59 */
60 void
61 tuiPuts_unfiltered (const char *string, struct ui_file * stream)
62 {
63 int len = strlen (string);
64 int i, linech;
65
66 for (i = 0; i < len; i++)
67 {
68 if (string[i] == '\n' || string[i] == '\r')
69 m_tuiStartNewLine;
70 else
71 {
72 if ((cmdWin->detail.commandInfo.curch + 1) > cmdWin->generic.width)
73 m_tuiStartNewLine;
74
75 if (insert_mode)
76 {
77 mvwinsch (cmdWin->generic.handle,
78 cmdWin->detail.commandInfo.curLine,
79 cmdWin->detail.commandInfo.curch++,
80 string[i]);
81 wmove (cmdWin->generic.handle,
82 cmdWin->detail.commandInfo.curLine,
83 cmdWin->detail.commandInfo.curch);
84 }
85 else
86 mvwaddch (cmdWin->generic.handle,
87 cmdWin->detail.commandInfo.curLine,
88 cmdWin->detail.commandInfo.curch++,
89 string[i]);
90 }
91 }
92 tuiRefreshWin (&cmdWin->generic);
93
94 return;
95 } /* tuiPuts_unfiltered */
96
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.
101 *
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.
106 *
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
113 */
114 void
115 tui_tputs (str, affcnt, putfunc)
116 char *str;
117 int affcnt;
118 int (*putfunc) (int);
119 {
120 extern char *rl_prompt; /* the prompt string */
121
122 /* This set of globals are defined and initialized
123 * by the readline package.
124 *
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
132 *
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
135 */
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;
153
154 /* Sanity check - if not TUI, just call tputs() */
155 if (!tui_version)
156 tputs (str, affcnt, putfunc);
157
158 /* The strings we special-case are handled first */
159
160 if (str == term_backspace)
161 {
162 /* Backspace. */
163
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).
169 */
170
171 _updateCommandInfo (-1);
172 wmove (cmdWin->generic.handle,
173 cmdWin->detail.commandInfo.curLine,
174 cmdWin->detail.commandInfo.curch);
175 wrefresh (cmdWin->generic.handle);
176
177 }
178 else if (str == term_clreol)
179 {
180
181 /* Clear to end of line. */
182 wclrtoeol (cmdWin->generic.handle);
183 wrefresh (cmdWin->generic.handle);
184
185 }
186 else if (str == term_cr)
187 {
188
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);
195
196 }
197 else if (str == term_goto)
198 {
199
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
209 */
210
211 }
212 else if (str == term_dc)
213 {
214
215 /* Delete character at current cursor position */
216 wdelch (cmdWin->generic.handle);
217 wrefresh (cmdWin->generic.handle);
218
219 }
220 else if (str == term_im)
221 {
222
223 /* Turn on insert mode. */
224 insert_mode = 1;
225
226 }
227 else if (str == term_ei)
228 {
229
230 /* Turn off insert mode. */
231 insert_mode = 0;
232
233 /* Strings we know about but don't handle
234 * specially here are just passed along to tputs().
235 *
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.
241 */
242 }
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 */
252 str == visible_bell)
253 { /* flash screen */
254 tputs (str, affcnt, putfunc);
255 }
256 else
257 { /* something else */
258 tputs (str, affcnt, putfunc);
259 }
260 } /* tui_tputs */
261
262
263 /*
264 ** tui_vwgetch()
265 ** Wrapper around wgetch with the window in a va_list
266 */
267 unsigned int
268 tui_vwgetch (va_list args)
269 {
270 unsigned int ch;
271 WINDOW *window;
272
273 window = va_arg (args, WINDOW *);
274
275 return ((unsigned int) wgetch (window));
276 } /* tui_vwgetch */
277
278
279 /*
280 ** tuiGetc().
281 ** Get a character from the command window.
282 ** This is called from the readline package,
283 ** that is, we have:
284 ** tuiGetc() [here], called from
285 ** readline code [in ../readline/], called from
286 ** command_line_input() in top.c
287 */
288 unsigned int
289 tuiGetc (void)
290 {
291 unsigned int ch;
292 extern char *rl_prompt;
293 extern char *rl_line_buffer;
294 extern int rl_point;
295
296 /* Call the curses routine that reads one character */
297 #ifndef COMMENT
298 ch = (unsigned int) vcatch_errors ((OpaqueFuncPtr) tui_vwgetch,
299 cmdWin->generic.handle);
300 #else
301 ch = wgetch (cmdWin->generic.handle);
302 #endif
303 ch = _tuiHandleResizeDuringIO (ch);
304
305 if (m_isCommandChar (ch))
306 { /* Handle prev/next/up/down here */
307 tuiTermSetup (0);
308 ch = tuiDispatchCtrlChar (ch);
309 cmdWin->detail.commandInfo.curch = strlen (rl_prompt) + rl_point;
310 tuiTermUnsetup (0, cmdWin->detail.commandInfo.curch);
311 }
312 if (ch == '\n' || ch == '\r' || ch == '\f')
313 cmdWin->detail.commandInfo.curch = 0;
314 else
315 tuiIncrCommandCharCountBy (1);
316
317 return ch;
318 } /* tuiGetc */
319
320
321 /*
322 ** tuiBufferGetc().
323 */
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
330 utils.c */
331
332 unsigned int
333 tuiBufferGetc (void)
334 {
335 unsigned int ch;
336 static unsigned char _ibuffer[512];
337 static int index_read = -1;
338 static int length_of_answer = -1;
339 int pos = 0;
340
341 if (length_of_answer == -1)
342 {
343 /* this is the first time through, need to read the answer */
344 do
345 {
346 /* Call the curses routine that reads one character */
347 ch = (unsigned int) wgetch (cmdWin->generic.handle);
348 if (ch != '\b')
349 {
350 _ibuffer[pos] = ch;
351 pos++;
352 }
353 else
354 pos--;
355 }
356 while (ch != '\r' && ch != '\n');
357
358 length_of_answer = pos;
359 index_read = 0;
360 }
361
362 ch = _ibuffer[index_read];
363 index_read++;
364
365 if (index_read == length_of_answer)
366 {
367 /*this is the last time through, reset for next query */
368 index_read = -1;
369 length_of_answer = -1;
370 }
371
372 wrefresh (cmdWin->generic.handle);
373
374 return (ch);
375 } /* tuiBufferGetc */
376
377
378 /*
379 ** tuiStartNewLines().
380 */
381 void
382 tuiStartNewLines (int numLines)
383 {
384 if (numLines > 0)
385 {
386 if (cmdWin->generic.viewportHeight > 1 &&
387 cmdWin->detail.commandInfo.curLine < cmdWin->generic.viewportHeight)
388 cmdWin->detail.commandInfo.curLine += numLines;
389 else
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);
396 }
397
398 return;
399 } /* tuiStartNewLines */
400
401
402 /*
403 ** tui_vStartNewLines().
404 ** With numLines in a va_list
405 */
406 void
407 tui_vStartNewLines (va_list args)
408 {
409 int numLines = va_arg (args, int);
410
411 tuiStartNewLines (numLines);
412
413 return;
414 } /* tui_vStartNewLines */
415
416
417 /****************************************************************************
418 ** LOCAL STATIC FUNCTIONS **
419 *****************************************************************************/
420
421
422 /*
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.
427 */
428 static unsigned int
429 _tuiHandleResizeDuringIO (unsigned int originalCh)
430 /* the char just read */
431 {
432 if (tuiWinResized ())
433 {
434 tuiRefreshAll ();
435 dont_repeat ();
436 tuiSetWinResizedTo (FALSE);
437 rl_reset ();
438 return '\n';
439 }
440 else
441 return originalCh;
442 } /* _tuiHandleResizeDuringIO */
443
444
445 /*
446 ** _updateCommandInfo().
447 ** Function to update the command window information.
448 */
449 static void
450 _updateCommandInfo (int sizeOfString)
451 {
452
453 if ((sizeOfString +
454 cmdWin->detail.commandInfo.curch) > cmdWin->generic.width)
455 {
456 int newCurch = sizeOfString + cmdWin->detail.commandInfo.curch;
457
458 tuiStartNewLines (1);
459 cmdWin->detail.commandInfo.curch = newCurch - cmdWin->generic.width;
460 }
461 else
462 cmdWin->detail.commandInfo.curch += sizeOfString;
463
464 return;
465 } /* _updateCommandInfo */
466
467
468 /* Looked at in main.c, fputs_unfiltered(), to decide
469 * if it's safe to do standard output to the command window.
470 */
471 int tui_owns_terminal = 0;
472
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.
477 */
478
479 void
480 tuiTermSetup (int turn_off_echo)
481 {
482 char *buffer;
483 int start;
484 int end;
485 int endcol;
486 extern char *term_scroll_region;
487 extern char *term_cursor_move;
488 extern char *term_memory_lock;
489 extern char *term_memory_unlock;
490
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.
496 */
497 if (turn_off_echo)
498 {
499 #ifdef HAVE_TERMIOS
500 struct termios tio;
501 tcgetattr (0, &tio);
502 tio.c_lflag &= ~(ECHO);
503 tcsetattr (0, TCSANOW, &tio);
504 #endif
505 }
506
507 /* Compute the start and end lines of the command
508 * region. (Actually we only use end here)
509 */
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;
513
514 if (term_memory_unlock)
515 {
516
517 /* Un-do the effect of the memory lock in terminal_inferior() */
518 tputs (term_memory_unlock, 1, (int (*) (int)) putchar);
519 fflush (stdout);
520
521 }
522 else if (term_scroll_region)
523 {
524
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, ...
530 */
531
532 /* Set scroll region to be 0..end */
533 buffer = (char *) tgoto (term_scroll_region, end, 0);
534 tputs (buffer, 1, (int (*) (int)) putchar);
535
536 } /* else we're out of luck */
537
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.
546 */
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
550 * place.
551 * Note I'm relying on refresh()
552 * only writing what changed (the space),
553 * not the whole screen.
554 */
555 standend ();
556 move (end, endcol - 1);
557 addch (' ');
558 refresh ();
559
560 tui_owns_terminal = 1;
561 } /* tuiTermSetup */
562
563
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. */
568 void
569 tuiTermUnsetup (int turn_on_echo, int to_column)
570 {
571 int start;
572 int end;
573 int curline;
574 char *buffer;
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;
581
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.
585 */
586 if (turn_on_echo)
587 {
588 #ifdef HAVE_TERMIOS
589 struct termios tio;
590 tcgetattr (0, &tio);
591 tio.c_lflag |= (ECHO);
592 tcsetattr (0, TCSANOW, &tio);
593 #endif
594 }
595
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)
600 */
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;
604
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).
608 */
609 if (term_cursor_move && term_memory_lock)
610 {
611
612 /* Memory lock means lock region above cursor.
613 * So first position the cursor, then call memory lock.
614 */
615 buffer = tgoto (term_cursor_move, 0, start);
616 tputs (buffer, 1, (int (*) (int)) putchar);
617 tputs (term_memory_lock, 1, (int (*) (int)) putchar);
618
619 }
620 else if (term_scroll_region)
621 {
622
623 /* Set the scroll region to the command window */
624 buffer = tgoto (term_scroll_region, end, start);
625 tputs (buffer, 1, (int (*) (int)) putchar);
626
627 } /* else we can't do anything about target I/O */
628
629 /* Also turn off standout mode, in case it is on */
630 if (term_se != NULL)
631 tputs (term_se, 1, (int (*) (int)) putchar);
632
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);
636 fflush (stdout);
637
638 tui_owns_terminal = 0;
639 } /* tuiTermUnsetup */