]>
Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* General utility routines for GDB, the GNU debugger. |
03e2a8c8 | 2 | Copyright 1986, 89, 90, 91, 92, 95, 1996 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
351b221d | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
351b221d JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
351b221d | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
351b221d | 17 | along with this program; if not, write to the Free Software |
dedcc91d | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
bd5635a1 | 19 | |
d747e0af | 20 | #include "defs.h" |
45993f61 | 21 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
22 | #include <stdarg.h> |
23 | #else | |
2bc2e684 | 24 | #include <varargs.h> |
85c613aa | 25 | #endif |
2bc2e684 | 26 | #include <ctype.h> |
2b576293 | 27 | #include "gdb_string.h" |
1a494973 C |
28 | #ifdef HAVE_UNISTD_H |
29 | #include <unistd.h> | |
30 | #endif | |
2bc2e684 | 31 | |
bd5635a1 RP |
32 | #include "signals.h" |
33 | #include "gdbcmd.h" | |
159dd2aa | 34 | #include "serial.h" |
bd5635a1 RP |
35 | #include "bfd.h" |
36 | #include "target.h" | |
bcf2e6ab | 37 | #include "demangle.h" |
bd5d07d9 FF |
38 | #include "expression.h" |
39 | #include "language.h" | |
1c95d7ab | 40 | #include "annotate.h" |
bd5635a1 | 41 | |
d8742f46 JK |
42 | #include "readline.h" |
43 | ||
44 | /* readline defines this. */ | |
45 | #undef savestring | |
46 | ||
7919c3ed JG |
47 | /* Prototypes for local functions */ |
48 | ||
b607efe7 FF |
49 | static void vfprintf_maybe_filtered PARAMS ((FILE *, const char *, va_list, int)); |
50 | ||
51 | static void fputs_maybe_filtered PARAMS ((const char *, FILE *, int)); | |
52 | ||
53 | #if !defined (NO_MMALLOC) && !defined (NO_MMCHECK) | |
54 | static void malloc_botch PARAMS ((void)); | |
55 | #endif | |
56 | ||
7919c3ed | 57 | static void |
85c613aa | 58 | fatal_dump_core PARAMS((char *, ...)); |
7919c3ed JG |
59 | |
60 | static void | |
61 | prompt_for_continue PARAMS ((void)); | |
62 | ||
63 | static void | |
64 | set_width_command PARAMS ((char *, int, struct cmd_list_element *)); | |
65 | ||
bd5635a1 RP |
66 | /* If this definition isn't overridden by the header files, assume |
67 | that isatty and fileno exist on this system. */ | |
68 | #ifndef ISATTY | |
69 | #define ISATTY(FP) (isatty (fileno (FP))) | |
70 | #endif | |
71 | ||
bd5635a1 RP |
72 | /* Chain of cleanup actions established with make_cleanup, |
73 | to be executed if an error happens. */ | |
74 | ||
4ce7ba51 SG |
75 | static struct cleanup *cleanup_chain; /* cleaned up after a failed command */ |
76 | static struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */ | |
bd5635a1 | 77 | |
16d2cc80 SS |
78 | /* Nonzero if we have job control. */ |
79 | ||
80 | int job_control; | |
81 | ||
bd5635a1 RP |
82 | /* Nonzero means a quit has been requested. */ |
83 | ||
84 | int quit_flag; | |
85 | ||
159dd2aa JK |
86 | /* Nonzero means quit immediately if Control-C is typed now, rather |
87 | than waiting until QUIT is executed. Be careful in setting this; | |
88 | code which executes with immediate_quit set has to be very careful | |
89 | about being able to deal with being interrupted at any time. It is | |
90 | almost always better to use QUIT; the only exception I can think of | |
91 | is being able to quit out of a system call (using EINTR loses if | |
92 | the SIGINT happens between the previous QUIT and the system call). | |
93 | To immediately quit in the case in which a SIGINT happens between | |
94 | the previous QUIT and setting immediate_quit (desirable anytime we | |
95 | expect to block), call QUIT after setting immediate_quit. */ | |
bd5635a1 RP |
96 | |
97 | int immediate_quit; | |
98 | ||
99 | /* Nonzero means that encoded C++ names should be printed out in their | |
100 | C++ form rather than raw. */ | |
101 | ||
102 | int demangle = 1; | |
103 | ||
104 | /* Nonzero means that encoded C++ names should be printed out in their | |
105 | C++ form even in assembler language displays. If this is set, but | |
106 | DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ | |
107 | ||
108 | int asm_demangle = 0; | |
109 | ||
110 | /* Nonzero means that strings with character values >0x7F should be printed | |
111 | as octal escapes. Zero means just print the value (e.g. it's an | |
112 | international character, and the terminal or window can cope.) */ | |
113 | ||
114 | int sevenbit_strings = 0; | |
81066208 JG |
115 | |
116 | /* String to be printed before error messages, if any. */ | |
117 | ||
118 | char *error_pre_print; | |
49073be0 SS |
119 | |
120 | /* String to be printed before quit messages, if any. */ | |
121 | ||
122 | char *quit_pre_print; | |
123 | ||
124 | /* String to be printed before warning messages, if any. */ | |
125 | ||
3624c875 | 126 | char *warning_pre_print = "\nwarning: "; |
bd5635a1 RP |
127 | \f |
128 | /* Add a new cleanup to the cleanup_chain, | |
129 | and return the previous chain pointer | |
130 | to be passed later to do_cleanups or discard_cleanups. | |
131 | Args are FUNCTION to clean up with, and ARG to pass to it. */ | |
132 | ||
133 | struct cleanup * | |
134 | make_cleanup (function, arg) | |
7919c3ed JG |
135 | void (*function) PARAMS ((PTR)); |
136 | PTR arg; | |
4ce7ba51 SG |
137 | { |
138 | return make_my_cleanup (&cleanup_chain, function, arg); | |
139 | } | |
140 | ||
141 | struct cleanup * | |
142 | make_final_cleanup (function, arg) | |
143 | void (*function) PARAMS ((PTR)); | |
144 | PTR arg; | |
145 | { | |
146 | return make_my_cleanup (&final_cleanup_chain, function, arg); | |
147 | } | |
148 | struct cleanup * | |
149 | make_my_cleanup (pmy_chain, function, arg) | |
150 | struct cleanup **pmy_chain; | |
151 | void (*function) PARAMS ((PTR)); | |
152 | PTR arg; | |
bd5635a1 RP |
153 | { |
154 | register struct cleanup *new | |
155 | = (struct cleanup *) xmalloc (sizeof (struct cleanup)); | |
4ce7ba51 | 156 | register struct cleanup *old_chain = *pmy_chain; |
bd5635a1 | 157 | |
4ce7ba51 | 158 | new->next = *pmy_chain; |
bd5635a1 RP |
159 | new->function = function; |
160 | new->arg = arg; | |
4ce7ba51 | 161 | *pmy_chain = new; |
bd5635a1 RP |
162 | |
163 | return old_chain; | |
164 | } | |
165 | ||
166 | /* Discard cleanups and do the actions they describe | |
167 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
168 | ||
169 | void | |
170 | do_cleanups (old_chain) | |
171 | register struct cleanup *old_chain; | |
4ce7ba51 SG |
172 | { |
173 | do_my_cleanups (&cleanup_chain, old_chain); | |
174 | } | |
175 | ||
176 | void | |
177 | do_final_cleanups (old_chain) | |
178 | register struct cleanup *old_chain; | |
179 | { | |
180 | do_my_cleanups (&final_cleanup_chain, old_chain); | |
181 | } | |
182 | ||
183 | void | |
184 | do_my_cleanups (pmy_chain, old_chain) | |
185 | register struct cleanup **pmy_chain; | |
186 | register struct cleanup *old_chain; | |
bd5635a1 RP |
187 | { |
188 | register struct cleanup *ptr; | |
4ce7ba51 | 189 | while ((ptr = *pmy_chain) != old_chain) |
bd5635a1 | 190 | { |
4ce7ba51 | 191 | *pmy_chain = ptr->next; /* Do this first incase recursion */ |
bd5635a1 | 192 | (*ptr->function) (ptr->arg); |
bd5635a1 RP |
193 | free (ptr); |
194 | } | |
195 | } | |
196 | ||
197 | /* Discard cleanups, not doing the actions they describe, | |
198 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
199 | ||
200 | void | |
201 | discard_cleanups (old_chain) | |
202 | register struct cleanup *old_chain; | |
4ce7ba51 SG |
203 | { |
204 | discard_my_cleanups (&cleanup_chain, old_chain); | |
205 | } | |
206 | ||
207 | void | |
208 | discard_final_cleanups (old_chain) | |
209 | register struct cleanup *old_chain; | |
210 | { | |
211 | discard_my_cleanups (&final_cleanup_chain, old_chain); | |
212 | } | |
213 | ||
214 | void | |
215 | discard_my_cleanups (pmy_chain, old_chain) | |
216 | register struct cleanup **pmy_chain; | |
217 | register struct cleanup *old_chain; | |
bd5635a1 RP |
218 | { |
219 | register struct cleanup *ptr; | |
4ce7ba51 | 220 | while ((ptr = *pmy_chain) != old_chain) |
bd5635a1 | 221 | { |
4ce7ba51 | 222 | *pmy_chain = ptr->next; |
be772100 | 223 | free ((PTR)ptr); |
bd5635a1 RP |
224 | } |
225 | } | |
226 | ||
227 | /* Set the cleanup_chain to 0, and return the old cleanup chain. */ | |
228 | struct cleanup * | |
229 | save_cleanups () | |
230 | { | |
4ce7ba51 SG |
231 | return save_my_cleanups (&cleanup_chain); |
232 | } | |
233 | ||
234 | struct cleanup * | |
235 | save_final_cleanups () | |
236 | { | |
237 | return save_my_cleanups (&final_cleanup_chain); | |
238 | } | |
239 | ||
240 | struct cleanup * | |
241 | save_my_cleanups (pmy_chain) | |
242 | struct cleanup **pmy_chain; | |
243 | { | |
244 | struct cleanup *old_chain = *pmy_chain; | |
bd5635a1 | 245 | |
4ce7ba51 | 246 | *pmy_chain = 0; |
bd5635a1 RP |
247 | return old_chain; |
248 | } | |
249 | ||
250 | /* Restore the cleanup chain from a previously saved chain. */ | |
251 | void | |
252 | restore_cleanups (chain) | |
253 | struct cleanup *chain; | |
254 | { | |
4ce7ba51 SG |
255 | restore_my_cleanups (&cleanup_chain, chain); |
256 | } | |
257 | ||
258 | void | |
259 | restore_final_cleanups (chain) | |
260 | struct cleanup *chain; | |
261 | { | |
262 | restore_my_cleanups (&final_cleanup_chain, chain); | |
263 | } | |
264 | ||
265 | void | |
266 | restore_my_cleanups (pmy_chain, chain) | |
267 | struct cleanup **pmy_chain; | |
268 | struct cleanup *chain; | |
269 | { | |
270 | *pmy_chain = chain; | |
bd5635a1 RP |
271 | } |
272 | ||
273 | /* This function is useful for cleanups. | |
274 | Do | |
275 | ||
276 | foo = xmalloc (...); | |
277 | old_chain = make_cleanup (free_current_contents, &foo); | |
278 | ||
279 | to arrange to free the object thus allocated. */ | |
280 | ||
281 | void | |
282 | free_current_contents (location) | |
283 | char **location; | |
284 | { | |
285 | free (*location); | |
286 | } | |
088c3a0b JG |
287 | |
288 | /* Provide a known function that does nothing, to use as a base for | |
289 | for a possibly long chain of cleanups. This is useful where we | |
290 | use the cleanup chain for handling normal cleanups as well as dealing | |
291 | with cleanups that need to be done as a result of a call to error(). | |
292 | In such cases, we may not be certain where the first cleanup is, unless | |
293 | we have a do-nothing one to always use as the base. */ | |
294 | ||
295 | /* ARGSUSED */ | |
296 | void | |
297 | null_cleanup (arg) | |
b607efe7 | 298 | PTR arg; |
088c3a0b JG |
299 | { |
300 | } | |
301 | ||
bd5635a1 | 302 | \f |
8989d4fc JK |
303 | /* Print a warning message. Way to use this is to call warning_begin, |
304 | output the warning message (use unfiltered output to gdb_stderr), | |
305 | ending in a newline. There is not currently a warning_end that you | |
306 | call afterwards, but such a thing might be added if it is useful | |
307 | for a GUI to separate warning messages from other output. | |
308 | ||
309 | FIXME: Why do warnings use unfiltered output and errors filtered? | |
310 | Is this anything other than a historical accident? */ | |
2bc2e684 FF |
311 | |
312 | void | |
8989d4fc | 313 | warning_begin () |
2bc2e684 FF |
314 | { |
315 | target_terminal_ours (); | |
316 | wrap_here(""); /* Force out any buffered output */ | |
199b2450 | 317 | gdb_flush (gdb_stdout); |
8989d4fc JK |
318 | if (warning_pre_print) |
319 | fprintf_unfiltered (gdb_stderr, warning_pre_print); | |
2bc2e684 FF |
320 | } |
321 | ||
322 | /* Print a warning message. | |
323 | The first argument STRING is the warning message, used as a fprintf string, | |
324 | and the remaining args are passed as arguments to it. | |
325 | The primary difference between warnings and errors is that a warning | |
8989d4fc | 326 | does not force the return to command level. */ |
2bc2e684 FF |
327 | |
328 | /* VARARGS */ | |
329 | void | |
45993f61 | 330 | #ifdef ANSI_PROTOTYPES |
4ce7ba51 | 331 | warning (const char *string, ...) |
85c613aa | 332 | #else |
2bc2e684 FF |
333 | warning (va_alist) |
334 | va_dcl | |
85c613aa | 335 | #endif |
2bc2e684 FF |
336 | { |
337 | va_list args; | |
45993f61 | 338 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
339 | va_start (args, string); |
340 | #else | |
2bc2e684 FF |
341 | char *string; |
342 | ||
343 | va_start (args); | |
2bc2e684 | 344 | string = va_arg (args, char *); |
85c613aa C |
345 | #endif |
346 | warning_begin (); | |
199b2450 TL |
347 | vfprintf_unfiltered (gdb_stderr, string, args); |
348 | fprintf_unfiltered (gdb_stderr, "\n"); | |
2bc2e684 FF |
349 | va_end (args); |
350 | } | |
351 | ||
a0cf4681 | 352 | /* Start the printing of an error message. Way to use this is to call |
8989d4fc JK |
353 | this, output the error message (use filtered output to gdb_stderr |
354 | (FIXME: Some callers, like memory_error, use gdb_stdout)), ending | |
355 | in a newline, and then call return_to_top_level (RETURN_ERROR). | |
356 | error() provides a convenient way to do this for the special case | |
357 | that the error message can be formatted with a single printf call, | |
358 | but this is more general. */ | |
a0cf4681 JK |
359 | void |
360 | error_begin () | |
361 | { | |
362 | target_terminal_ours (); | |
363 | wrap_here (""); /* Force out any buffered output */ | |
364 | gdb_flush (gdb_stdout); | |
365 | ||
1c95d7ab | 366 | annotate_error_begin (); |
a0cf4681 JK |
367 | |
368 | if (error_pre_print) | |
369 | fprintf_filtered (gdb_stderr, error_pre_print); | |
370 | } | |
371 | ||
bd5635a1 RP |
372 | /* Print an error message and return to command level. |
373 | The first argument STRING is the error message, used as a fprintf string, | |
374 | and the remaining args are passed as arguments to it. */ | |
375 | ||
45993f61 | 376 | #ifdef ANSI_PROTOTYPES |
7919c3ed | 377 | NORETURN void |
4ce7ba51 | 378 | error (const char *string, ...) |
85c613aa | 379 | #else |
1a494973 | 380 | void |
bd5635a1 RP |
381 | error (va_alist) |
382 | va_dcl | |
85c613aa | 383 | #endif |
bd5635a1 RP |
384 | { |
385 | va_list args; | |
1a494973 | 386 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
387 | va_start (args, string); |
388 | #else | |
bd5635a1 | 389 | va_start (args); |
85c613aa | 390 | #endif |
45993f61 | 391 | if (error_hook) |
1a494973 | 392 | (*error_hook) (); |
45993f61 SC |
393 | else |
394 | { | |
45993f61 SC |
395 | error_begin (); |
396 | #ifdef ANSI_PROTOTYPES | |
397 | vfprintf_filtered (gdb_stderr, string, args); | |
398 | #else | |
1a494973 C |
399 | { |
400 | char *string1; | |
401 | ||
402 | string1 = va_arg (args, char *); | |
403 | vfprintf_filtered (gdb_stderr, string1, args); | |
404 | } | |
45993f61 SC |
405 | #endif |
406 | fprintf_filtered (gdb_stderr, "\n"); | |
407 | va_end (args); | |
408 | return_to_top_level (RETURN_ERROR); | |
409 | } | |
bd5635a1 RP |
410 | } |
411 | ||
45993f61 | 412 | |
bd5635a1 RP |
413 | /* Print an error message and exit reporting failure. |
414 | This is for a error that we cannot continue from. | |
7919c3ed JG |
415 | The arguments are printed a la printf. |
416 | ||
417 | This function cannot be declared volatile (NORETURN) in an | |
418 | ANSI environment because exit() is not declared volatile. */ | |
bd5635a1 RP |
419 | |
420 | /* VARARGS */ | |
7919c3ed | 421 | NORETURN void |
45993f61 | 422 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
423 | fatal (char *string, ...) |
424 | #else | |
bd5635a1 RP |
425 | fatal (va_alist) |
426 | va_dcl | |
85c613aa | 427 | #endif |
bd5635a1 RP |
428 | { |
429 | va_list args; | |
45993f61 | 430 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
431 | va_start (args, string); |
432 | #else | |
bd5635a1 | 433 | char *string; |
bd5635a1 RP |
434 | va_start (args); |
435 | string = va_arg (args, char *); | |
85c613aa | 436 | #endif |
199b2450 TL |
437 | fprintf_unfiltered (gdb_stderr, "\ngdb: "); |
438 | vfprintf_unfiltered (gdb_stderr, string, args); | |
439 | fprintf_unfiltered (gdb_stderr, "\n"); | |
bd5635a1 RP |
440 | va_end (args); |
441 | exit (1); | |
442 | } | |
443 | ||
444 | /* Print an error message and exit, dumping core. | |
445 | The arguments are printed a la printf (). */ | |
7919c3ed | 446 | |
bd5635a1 | 447 | /* VARARGS */ |
7919c3ed | 448 | static void |
45993f61 | 449 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
450 | fatal_dump_core (char *string, ...) |
451 | #else | |
bd5635a1 RP |
452 | fatal_dump_core (va_alist) |
453 | va_dcl | |
85c613aa | 454 | #endif |
bd5635a1 RP |
455 | { |
456 | va_list args; | |
45993f61 | 457 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
458 | va_start (args, string); |
459 | #else | |
bd5635a1 RP |
460 | char *string; |
461 | ||
462 | va_start (args); | |
463 | string = va_arg (args, char *); | |
85c613aa | 464 | #endif |
bd5635a1 RP |
465 | /* "internal error" is always correct, since GDB should never dump |
466 | core, no matter what the input. */ | |
199b2450 TL |
467 | fprintf_unfiltered (gdb_stderr, "\ngdb internal error: "); |
468 | vfprintf_unfiltered (gdb_stderr, string, args); | |
469 | fprintf_unfiltered (gdb_stderr, "\n"); | |
bd5635a1 RP |
470 | va_end (args); |
471 | ||
472 | signal (SIGQUIT, SIG_DFL); | |
473 | kill (getpid (), SIGQUIT); | |
474 | /* We should never get here, but just in case... */ | |
475 | exit (1); | |
476 | } | |
7919c3ed | 477 | |
4ace50a5 FF |
478 | /* The strerror() function can return NULL for errno values that are |
479 | out of range. Provide a "safe" version that always returns a | |
480 | printable string. */ | |
481 | ||
482 | char * | |
483 | safe_strerror (errnum) | |
484 | int errnum; | |
485 | { | |
486 | char *msg; | |
487 | static char buf[32]; | |
488 | ||
489 | if ((msg = strerror (errnum)) == NULL) | |
490 | { | |
491 | sprintf (buf, "(undocumented errno %d)", errnum); | |
492 | msg = buf; | |
493 | } | |
494 | return (msg); | |
495 | } | |
496 | ||
497 | /* The strsignal() function can return NULL for signal values that are | |
498 | out of range. Provide a "safe" version that always returns a | |
499 | printable string. */ | |
500 | ||
501 | char * | |
502 | safe_strsignal (signo) | |
503 | int signo; | |
504 | { | |
505 | char *msg; | |
506 | static char buf[32]; | |
507 | ||
508 | if ((msg = strsignal (signo)) == NULL) | |
509 | { | |
510 | sprintf (buf, "(undocumented signal %d)", signo); | |
511 | msg = buf; | |
512 | } | |
513 | return (msg); | |
514 | } | |
515 | ||
516 | ||
bd5635a1 RP |
517 | /* Print the system error message for errno, and also mention STRING |
518 | as the file name for which the error was encountered. | |
519 | Then return to command level. */ | |
520 | ||
521 | void | |
522 | perror_with_name (string) | |
523 | char *string; | |
524 | { | |
bd5635a1 RP |
525 | char *err; |
526 | char *combined; | |
527 | ||
4ace50a5 | 528 | err = safe_strerror (errno); |
bd5635a1 RP |
529 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); |
530 | strcpy (combined, string); | |
531 | strcat (combined, ": "); | |
532 | strcat (combined, err); | |
533 | ||
534 | /* I understand setting these is a matter of taste. Still, some people | |
535 | may clear errno but not know about bfd_error. Doing this here is not | |
536 | unreasonable. */ | |
8eec3310 | 537 | bfd_set_error (bfd_error_no_error); |
bd5635a1 RP |
538 | errno = 0; |
539 | ||
540 | error ("%s.", combined); | |
541 | } | |
542 | ||
543 | /* Print the system error message for ERRCODE, and also mention STRING | |
544 | as the file name for which the error was encountered. */ | |
545 | ||
546 | void | |
547 | print_sys_errmsg (string, errcode) | |
548 | char *string; | |
549 | int errcode; | |
550 | { | |
bd5635a1 RP |
551 | char *err; |
552 | char *combined; | |
553 | ||
4ace50a5 | 554 | err = safe_strerror (errcode); |
bd5635a1 RP |
555 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); |
556 | strcpy (combined, string); | |
557 | strcat (combined, ": "); | |
558 | strcat (combined, err); | |
559 | ||
44a09a68 JK |
560 | /* We want anything which was printed on stdout to come out first, before |
561 | this message. */ | |
562 | gdb_flush (gdb_stdout); | |
199b2450 | 563 | fprintf_unfiltered (gdb_stderr, "%s.\n", combined); |
bd5635a1 RP |
564 | } |
565 | ||
566 | /* Control C eventually causes this to be called, at a convenient time. */ | |
567 | ||
568 | void | |
569 | quit () | |
570 | { | |
199b2450 | 571 | serial_t gdb_stdout_serial = serial_fdopen (1); |
159dd2aa | 572 | |
bd5635a1 | 573 | target_terminal_ours (); |
159dd2aa | 574 | |
44a09a68 JK |
575 | /* We want all output to appear now, before we print "Quit". We |
576 | have 3 levels of buffering we have to flush (it's possible that | |
577 | some of these should be changed to flush the lower-level ones | |
578 | too): */ | |
579 | ||
580 | /* 1. The _filtered buffer. */ | |
581 | wrap_here ((char *)0); | |
582 | ||
583 | /* 2. The stdio buffer. */ | |
584 | gdb_flush (gdb_stdout); | |
585 | gdb_flush (gdb_stderr); | |
159dd2aa | 586 | |
44a09a68 JK |
587 | /* 3. The system-level buffer. */ |
588 | SERIAL_FLUSH_OUTPUT (gdb_stdout_serial); | |
199b2450 | 589 | SERIAL_UN_FDOPEN (gdb_stdout_serial); |
159dd2aa | 590 | |
1c95d7ab | 591 | annotate_error_begin (); |
a0cf4681 | 592 | |
159dd2aa | 593 | /* Don't use *_filtered; we don't want to prompt the user to continue. */ |
49073be0 SS |
594 | if (quit_pre_print) |
595 | fprintf_unfiltered (gdb_stderr, quit_pre_print); | |
159dd2aa JK |
596 | |
597 | if (job_control | |
598 | /* If there is no terminal switching for this target, then we can't | |
599 | possibly get screwed by the lack of job control. */ | |
cad1498f | 600 | || current_target.to_terminal_ours == NULL) |
199b2450 | 601 | fprintf_unfiltered (gdb_stderr, "Quit\n"); |
159dd2aa | 602 | else |
199b2450 | 603 | fprintf_unfiltered (gdb_stderr, |
159dd2aa JK |
604 | "Quit (expect signal SIGINT when the program is resumed)\n"); |
605 | return_to_top_level (RETURN_QUIT); | |
bd5635a1 RP |
606 | } |
607 | ||
bd5d07d9 | 608 | |
4ce7ba51 | 609 | #if defined(__GO32__) || defined (_WIN32) |
bd5d07d9 | 610 | |
4ce7ba51 | 611 | #ifndef _MSC_VER |
bd5d07d9 FF |
612 | /* In the absence of signals, poll keyboard for a quit. |
613 | Called from #define QUIT pollquit() in xm-go32.h. */ | |
614 | ||
615 | void | |
616 | pollquit() | |
617 | { | |
618 | if (kbhit ()) | |
619 | { | |
620 | int k = getkey (); | |
44a09a68 | 621 | if (k == 1) { |
bd5d07d9 | 622 | quit_flag = 1; |
44a09a68 JK |
623 | quit(); |
624 | } | |
625 | else if (k == 2) { | |
bd5d07d9 | 626 | immediate_quit = 1; |
44a09a68 JK |
627 | quit (); |
628 | } | |
629 | else | |
630 | { | |
631 | /* We just ignore it */ | |
4ce7ba51 | 632 | /* FIXME!! Don't think this actually works! */ |
44a09a68 JK |
633 | fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n"); |
634 | } | |
bd5d07d9 FF |
635 | } |
636 | } | |
4ce7ba51 SG |
637 | #else /* !_MSC_VER */ |
638 | ||
639 | /* This above code is not valid for wingdb unless | |
640 | * getkey and kbhit were to be rewritten. | |
641 | * Windows translates all keyboard and mouse events | |
642 | * into a message which is appended to the message | |
643 | * queue for the process. | |
644 | */ | |
645 | void | |
646 | pollquit() | |
647 | { | |
648 | int k = win32pollquit(); | |
649 | if (k == 1) | |
650 | { | |
651 | quit_flag = 1; | |
652 | quit (); | |
653 | } | |
654 | else if (k == 2) | |
655 | { | |
656 | immediate_quit = 1; | |
657 | quit (); | |
658 | } | |
659 | } | |
660 | #endif /* !_MSC_VER */ | |
bd5d07d9 | 661 | |
bd5d07d9 | 662 | |
4ce7ba51 | 663 | #ifndef _MSC_VER |
44a09a68 JK |
664 | void notice_quit() |
665 | { | |
666 | if (kbhit ()) | |
667 | { | |
668 | int k = getkey (); | |
669 | if (k == 1) { | |
670 | quit_flag = 1; | |
671 | } | |
672 | else if (k == 2) | |
673 | { | |
674 | immediate_quit = 1; | |
675 | } | |
676 | else | |
677 | { | |
678 | fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n"); | |
679 | } | |
680 | } | |
681 | } | |
4ce7ba51 SG |
682 | #else /* !_MSC_VER */ |
683 | ||
684 | void notice_quit() | |
685 | { | |
686 | int k = win32pollquit(); | |
687 | if (k == 1) | |
688 | quit_flag = 1; | |
689 | else if (k == 2) | |
690 | immediate_quit = 1; | |
691 | } | |
692 | #endif /* !_MSC_VER */ | |
693 | ||
44a09a68 JK |
694 | #else |
695 | void notice_quit() | |
696 | { | |
697 | /* Done by signals */ | |
698 | } | |
4ce7ba51 SG |
699 | #endif /* defined(__GO32__) || defined(_WIN32) */ |
700 | ||
bd5635a1 RP |
701 | /* Control C comes here */ |
702 | ||
703 | void | |
088c3a0b JG |
704 | request_quit (signo) |
705 | int signo; | |
bd5635a1 RP |
706 | { |
707 | quit_flag = 1; | |
44a09a68 JK |
708 | /* Restore the signal handler. Harmless with BSD-style signals, needed |
709 | for System V-style signals. So just always do it, rather than worrying | |
710 | about USG defines and stuff like that. */ | |
088c3a0b | 711 | signal (signo, request_quit); |
bd5635a1 | 712 | |
cd10c7e3 | 713 | /* start-sanitize-gm */ |
a243a22f | 714 | #ifdef GENERAL_MAGIC |
cd10c7e3 | 715 | target_kill (); |
a243a22f | 716 | #endif /* GENERAL_MAGIC */ |
cd10c7e3 SG |
717 | /* end-sanitize-gm */ |
718 | ||
cad1498f SG |
719 | #ifdef REQUEST_QUIT |
720 | REQUEST_QUIT; | |
721 | #else | |
dedcc91d | 722 | if (immediate_quit) |
bd5635a1 | 723 | quit (); |
cad1498f | 724 | #endif |
bd5635a1 | 725 | } |
3624c875 FF |
726 | |
727 | \f | |
728 | /* Memory management stuff (malloc friends). */ | |
729 | ||
0d172a2e JK |
730 | /* Make a substitute size_t for non-ANSI compilers. */ |
731 | ||
03e2a8c8 | 732 | #ifndef HAVE_STDDEF_H |
0d172a2e JK |
733 | #ifndef size_t |
734 | #define size_t unsigned int | |
735 | #endif | |
736 | #endif | |
03e2a8c8 ILT |
737 | |
738 | #if defined (NO_MMALLOC) | |
0d172a2e | 739 | |
3624c875 FF |
740 | PTR |
741 | mmalloc (md, size) | |
742 | PTR md; | |
0d172a2e | 743 | size_t size; |
3624c875 | 744 | { |
0d172a2e | 745 | return malloc (size); |
3624c875 FF |
746 | } |
747 | ||
748 | PTR | |
749 | mrealloc (md, ptr, size) | |
750 | PTR md; | |
751 | PTR ptr; | |
0d172a2e | 752 | size_t size; |
3624c875 | 753 | { |
4ace50a5 FF |
754 | if (ptr == 0) /* Guard against old realloc's */ |
755 | return malloc (size); | |
756 | else | |
757 | return realloc (ptr, size); | |
3624c875 FF |
758 | } |
759 | ||
760 | void | |
761 | mfree (md, ptr) | |
762 | PTR md; | |
763 | PTR ptr; | |
764 | { | |
765 | free (ptr); | |
766 | } | |
767 | ||
768 | #endif /* NO_MMALLOC */ | |
769 | ||
54109914 | 770 | #if defined (NO_MMALLOC) || defined (NO_MMCHECK) |
3624c875 FF |
771 | |
772 | void | |
773 | init_malloc (md) | |
774 | PTR md; | |
775 | { | |
776 | } | |
777 | ||
54109914 | 778 | #else /* Have mmalloc and want corruption checking */ |
3624c875 FF |
779 | |
780 | static void | |
781 | malloc_botch () | |
782 | { | |
783 | fatal_dump_core ("Memory corruption"); | |
784 | } | |
785 | ||
786 | /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified | |
787 | by MD, to detect memory corruption. Note that MD may be NULL to specify | |
788 | the default heap that grows via sbrk. | |
789 | ||
54109914 | 790 | Note that for freshly created regions, we must call mmcheckf prior to any |
3624c875 FF |
791 | mallocs in the region. Otherwise, any region which was allocated prior to |
792 | installing the checking hooks, which is later reallocated or freed, will | |
793 | fail the checks! The mmcheck function only allows initial hooks to be | |
794 | installed before the first mmalloc. However, anytime after we have called | |
795 | mmcheck the first time to install the checking hooks, we can call it again | |
796 | to update the function pointer to the memory corruption handler. | |
797 | ||
798 | Returns zero on failure, non-zero on success. */ | |
799 | ||
54109914 FF |
800 | #ifndef MMCHECK_FORCE |
801 | #define MMCHECK_FORCE 0 | |
802 | #endif | |
803 | ||
3624c875 FF |
804 | void |
805 | init_malloc (md) | |
806 | PTR md; | |
807 | { | |
54109914 | 808 | if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE)) |
3624c875 | 809 | { |
54109914 FF |
810 | /* Don't use warning(), which relies on current_target being set |
811 | to something other than dummy_target, until after | |
812 | initialize_all_files(). */ | |
813 | ||
814 | fprintf_unfiltered | |
815 | (gdb_stderr, "warning: failed to install memory consistency checks; "); | |
816 | fprintf_unfiltered | |
817 | (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n"); | |
3624c875 FF |
818 | } |
819 | ||
4ed3a9ea | 820 | mmtrace (); |
3624c875 FF |
821 | } |
822 | ||
823 | #endif /* Have mmalloc and want corruption checking */ | |
824 | ||
825 | /* Called when a memory allocation fails, with the number of bytes of | |
826 | memory requested in SIZE. */ | |
827 | ||
828 | NORETURN void | |
829 | nomem (size) | |
830 | long size; | |
831 | { | |
832 | if (size > 0) | |
833 | { | |
834 | fatal ("virtual memory exhausted: can't allocate %ld bytes.", size); | |
835 | } | |
836 | else | |
837 | { | |
838 | fatal ("virtual memory exhausted."); | |
839 | } | |
840 | } | |
841 | ||
842 | /* Like mmalloc but get error if no storage available, and protect against | |
843 | the caller wanting to allocate zero bytes. Whether to return NULL for | |
844 | a zero byte request, or translate the request into a request for one | |
845 | byte of zero'd storage, is a religious issue. */ | |
846 | ||
847 | PTR | |
848 | xmmalloc (md, size) | |
849 | PTR md; | |
850 | long size; | |
851 | { | |
852 | register PTR val; | |
853 | ||
854 | if (size == 0) | |
855 | { | |
856 | val = NULL; | |
857 | } | |
858 | else if ((val = mmalloc (md, size)) == NULL) | |
859 | { | |
860 | nomem (size); | |
861 | } | |
862 | return (val); | |
863 | } | |
864 | ||
865 | /* Like mrealloc but get error if no storage available. */ | |
866 | ||
867 | PTR | |
868 | xmrealloc (md, ptr, size) | |
869 | PTR md; | |
870 | PTR ptr; | |
871 | long size; | |
872 | { | |
873 | register PTR val; | |
874 | ||
875 | if (ptr != NULL) | |
876 | { | |
877 | val = mrealloc (md, ptr, size); | |
878 | } | |
879 | else | |
880 | { | |
881 | val = mmalloc (md, size); | |
882 | } | |
883 | if (val == NULL) | |
884 | { | |
885 | nomem (size); | |
886 | } | |
887 | return (val); | |
888 | } | |
889 | ||
890 | /* Like malloc but get error if no storage available, and protect against | |
891 | the caller wanting to allocate zero bytes. */ | |
892 | ||
893 | PTR | |
894 | xmalloc (size) | |
03e2a8c8 | 895 | size_t size; |
3624c875 | 896 | { |
199b2450 | 897 | return (xmmalloc ((PTR) NULL, size)); |
3624c875 FF |
898 | } |
899 | ||
900 | /* Like mrealloc but get error if no storage available. */ | |
901 | ||
902 | PTR | |
903 | xrealloc (ptr, size) | |
904 | PTR ptr; | |
03e2a8c8 | 905 | size_t size; |
3624c875 | 906 | { |
199b2450 | 907 | return (xmrealloc ((PTR) NULL, ptr, size)); |
3624c875 FF |
908 | } |
909 | ||
bd5635a1 RP |
910 | \f |
911 | /* My replacement for the read system call. | |
912 | Used like `read' but keeps going if `read' returns too soon. */ | |
913 | ||
914 | int | |
915 | myread (desc, addr, len) | |
916 | int desc; | |
917 | char *addr; | |
918 | int len; | |
919 | { | |
920 | register int val; | |
921 | int orglen = len; | |
922 | ||
923 | while (len > 0) | |
924 | { | |
925 | val = read (desc, addr, len); | |
926 | if (val < 0) | |
927 | return val; | |
928 | if (val == 0) | |
929 | return orglen - len; | |
930 | len -= val; | |
931 | addr += val; | |
932 | } | |
933 | return orglen; | |
934 | } | |
935 | \f | |
936 | /* Make a copy of the string at PTR with SIZE characters | |
937 | (and add a null character at the end in the copy). | |
938 | Uses malloc to get the space. Returns the address of the copy. */ | |
939 | ||
940 | char * | |
941 | savestring (ptr, size) | |
088c3a0b | 942 | const char *ptr; |
bd5635a1 RP |
943 | int size; |
944 | { | |
945 | register char *p = (char *) xmalloc (size + 1); | |
4ed3a9ea | 946 | memcpy (p, ptr, size); |
bd5635a1 RP |
947 | p[size] = 0; |
948 | return p; | |
949 | } | |
950 | ||
3624c875 FF |
951 | char * |
952 | msavestring (md, ptr, size) | |
199b2450 | 953 | PTR md; |
3624c875 FF |
954 | const char *ptr; |
955 | int size; | |
956 | { | |
957 | register char *p = (char *) xmmalloc (md, size + 1); | |
4ed3a9ea | 958 | memcpy (p, ptr, size); |
3624c875 FF |
959 | p[size] = 0; |
960 | return p; | |
961 | } | |
962 | ||
8aa13b87 JK |
963 | /* The "const" is so it compiles under DGUX (which prototypes strsave |
964 | in <string.h>. FIXME: This should be named "xstrsave", shouldn't it? | |
965 | Doesn't real strsave return NULL if out of memory? */ | |
bd5635a1 RP |
966 | char * |
967 | strsave (ptr) | |
8aa13b87 | 968 | const char *ptr; |
bd5635a1 RP |
969 | { |
970 | return savestring (ptr, strlen (ptr)); | |
971 | } | |
972 | ||
3624c875 FF |
973 | char * |
974 | mstrsave (md, ptr) | |
199b2450 | 975 | PTR md; |
3624c875 FF |
976 | const char *ptr; |
977 | { | |
978 | return (msavestring (md, ptr, strlen (ptr))); | |
979 | } | |
980 | ||
bd5635a1 RP |
981 | void |
982 | print_spaces (n, file) | |
983 | register int n; | |
984 | register FILE *file; | |
985 | { | |
986 | while (n-- > 0) | |
987 | fputc (' ', file); | |
988 | } | |
989 | ||
8eec3310 SC |
990 | /* Print a host address. */ |
991 | ||
992 | void | |
993 | gdb_print_address (addr, stream) | |
994 | PTR addr; | |
995 | GDB_FILE *stream; | |
996 | { | |
997 | ||
998 | /* We could use the %p conversion specifier to fprintf if we had any | |
999 | way of knowing whether this host supports it. But the following | |
1000 | should work on the Alpha and on 32 bit machines. */ | |
1001 | ||
1002 | fprintf_filtered (stream, "0x%lx", (unsigned long)addr); | |
1003 | } | |
1004 | ||
bd5635a1 RP |
1005 | /* Ask user a y-or-n question and return 1 iff answer is yes. |
1006 | Takes three args which are given to printf to print the question. | |
1007 | The first, a control string, should end in "? ". | |
1008 | It should not say how to answer, because we do that. */ | |
1009 | ||
1010 | /* VARARGS */ | |
1011 | int | |
45993f61 | 1012 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
1013 | query (char *ctlstr, ...) |
1014 | #else | |
bd5635a1 RP |
1015 | query (va_alist) |
1016 | va_dcl | |
85c613aa | 1017 | #endif |
bd5635a1 RP |
1018 | { |
1019 | va_list args; | |
bd5635a1 RP |
1020 | register int answer; |
1021 | register int ans2; | |
d8742f46 | 1022 | int retval; |
bd5635a1 | 1023 | |
45993f61 | 1024 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
1025 | va_start (args, ctlstr); |
1026 | #else | |
1027 | char *ctlstr; | |
1028 | va_start (args); | |
1029 | ctlstr = va_arg (args, char *); | |
1030 | #endif | |
1031 | ||
0d172a2e JK |
1032 | if (query_hook) |
1033 | { | |
85c613aa | 1034 | return query_hook (ctlstr, args); |
0d172a2e JK |
1035 | } |
1036 | ||
bd5635a1 RP |
1037 | /* Automatically answer "yes" if input is not from a terminal. */ |
1038 | if (!input_from_terminal_p ()) | |
1039 | return 1; | |
cad1498f | 1040 | #ifdef MPW |
49073be0 | 1041 | /* FIXME Automatically answer "yes" if called from MacGDB. */ |
cad1498f SG |
1042 | if (mac_app) |
1043 | return 1; | |
1044 | #endif /* MPW */ | |
bd5635a1 RP |
1045 | |
1046 | while (1) | |
1047 | { | |
546014f7 | 1048 | wrap_here (""); /* Flush any buffered output */ |
199b2450 | 1049 | gdb_flush (gdb_stdout); |
d8742f46 JK |
1050 | |
1051 | if (annotation_level > 1) | |
1052 | printf_filtered ("\n\032\032pre-query\n"); | |
1053 | ||
199b2450 | 1054 | vfprintf_filtered (gdb_stdout, ctlstr, args); |
bcf2e6ab | 1055 | printf_filtered ("(y or n) "); |
d8742f46 JK |
1056 | |
1057 | if (annotation_level > 1) | |
1058 | printf_filtered ("\n\032\032query\n"); | |
1059 | ||
cad1498f SG |
1060 | #ifdef MPW |
1061 | /* If not in MacGDB, move to a new line so the entered line doesn't | |
1062 | have a prompt on the front of it. */ | |
1063 | if (!mac_app) | |
1064 | fputs_unfiltered ("\n", gdb_stdout); | |
1065 | #endif /* MPW */ | |
49073be0 | 1066 | |
199b2450 | 1067 | gdb_flush (gdb_stdout); |
b36e3a9b SG |
1068 | answer = fgetc (stdin); |
1069 | clearerr (stdin); /* in case of C-d */ | |
1070 | if (answer == EOF) /* C-d */ | |
d8742f46 JK |
1071 | { |
1072 | retval = 1; | |
1073 | break; | |
1074 | } | |
b36e3a9b SG |
1075 | if (answer != '\n') /* Eat rest of input line, to EOF or newline */ |
1076 | do | |
1077 | { | |
1078 | ans2 = fgetc (stdin); | |
1079 | clearerr (stdin); | |
1080 | } | |
1081 | while (ans2 != EOF && ans2 != '\n'); | |
bd5635a1 RP |
1082 | if (answer >= 'a') |
1083 | answer -= 040; | |
1084 | if (answer == 'Y') | |
d8742f46 JK |
1085 | { |
1086 | retval = 1; | |
1087 | break; | |
1088 | } | |
bd5635a1 | 1089 | if (answer == 'N') |
d8742f46 JK |
1090 | { |
1091 | retval = 0; | |
1092 | break; | |
1093 | } | |
bcf2e6ab | 1094 | printf_filtered ("Please answer y or n.\n"); |
bd5635a1 | 1095 | } |
d8742f46 JK |
1096 | |
1097 | if (annotation_level > 1) | |
1098 | printf_filtered ("\n\032\032post-query\n"); | |
1099 | return retval; | |
bd5635a1 | 1100 | } |
7919c3ed | 1101 | |
bd5635a1 RP |
1102 | \f |
1103 | /* Parse a C escape sequence. STRING_PTR points to a variable | |
1104 | containing a pointer to the string to parse. That pointer | |
1105 | should point to the character after the \. That pointer | |
1106 | is updated past the characters we use. The value of the | |
1107 | escape sequence is returned. | |
1108 | ||
1109 | A negative value means the sequence \ newline was seen, | |
1110 | which is supposed to be equivalent to nothing at all. | |
1111 | ||
1112 | If \ is followed by a null character, we return a negative | |
1113 | value and leave the string pointer pointing at the null character. | |
1114 | ||
1115 | If \ is followed by 000, we return 0 and leave the string pointer | |
1116 | after the zeros. A value of 0 does not mean end of string. */ | |
1117 | ||
1118 | int | |
1119 | parse_escape (string_ptr) | |
1120 | char **string_ptr; | |
1121 | { | |
1122 | register int c = *(*string_ptr)++; | |
1123 | switch (c) | |
1124 | { | |
1125 | case 'a': | |
2bc2e684 | 1126 | return 007; /* Bell (alert) char */ |
bd5635a1 RP |
1127 | case 'b': |
1128 | return '\b'; | |
2bc2e684 | 1129 | case 'e': /* Escape character */ |
bd5635a1 RP |
1130 | return 033; |
1131 | case 'f': | |
1132 | return '\f'; | |
1133 | case 'n': | |
1134 | return '\n'; | |
1135 | case 'r': | |
1136 | return '\r'; | |
1137 | case 't': | |
1138 | return '\t'; | |
1139 | case 'v': | |
1140 | return '\v'; | |
1141 | case '\n': | |
1142 | return -2; | |
1143 | case 0: | |
1144 | (*string_ptr)--; | |
1145 | return 0; | |
1146 | case '^': | |
1147 | c = *(*string_ptr)++; | |
1148 | if (c == '\\') | |
1149 | c = parse_escape (string_ptr); | |
1150 | if (c == '?') | |
1151 | return 0177; | |
1152 | return (c & 0200) | (c & 037); | |
1153 | ||
1154 | case '0': | |
1155 | case '1': | |
1156 | case '2': | |
1157 | case '3': | |
1158 | case '4': | |
1159 | case '5': | |
1160 | case '6': | |
1161 | case '7': | |
1162 | { | |
1163 | register int i = c - '0'; | |
1164 | register int count = 0; | |
1165 | while (++count < 3) | |
1166 | { | |
1167 | if ((c = *(*string_ptr)++) >= '0' && c <= '7') | |
1168 | { | |
1169 | i *= 8; | |
1170 | i += c - '0'; | |
1171 | } | |
1172 | else | |
1173 | { | |
1174 | (*string_ptr)--; | |
1175 | break; | |
1176 | } | |
1177 | } | |
1178 | return i; | |
1179 | } | |
1180 | default: | |
1181 | return c; | |
1182 | } | |
1183 | } | |
1184 | \f | |
51b80b00 FF |
1185 | /* Print the character C on STREAM as part of the contents of a literal |
1186 | string whose delimiter is QUOTER. Note that this routine should only | |
1187 | be call for printing things which are independent of the language | |
1188 | of the program being debugged. */ | |
bd5635a1 RP |
1189 | |
1190 | void | |
51b80b00 | 1191 | gdb_printchar (c, stream, quoter) |
088c3a0b | 1192 | register int c; |
bd5635a1 RP |
1193 | FILE *stream; |
1194 | int quoter; | |
1195 | { | |
bd5635a1 | 1196 | |
7e7e2d40 JG |
1197 | c &= 0xFF; /* Avoid sign bit follies */ |
1198 | ||
fcdb113e JG |
1199 | if ( c < 0x20 || /* Low control chars */ |
1200 | (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ | |
1201 | (sevenbit_strings && c >= 0x80)) { /* high order bit set */ | |
bd5635a1 RP |
1202 | switch (c) |
1203 | { | |
1204 | case '\n': | |
1205 | fputs_filtered ("\\n", stream); | |
1206 | break; | |
1207 | case '\b': | |
1208 | fputs_filtered ("\\b", stream); | |
1209 | break; | |
1210 | case '\t': | |
1211 | fputs_filtered ("\\t", stream); | |
1212 | break; | |
1213 | case '\f': | |
1214 | fputs_filtered ("\\f", stream); | |
1215 | break; | |
1216 | case '\r': | |
1217 | fputs_filtered ("\\r", stream); | |
1218 | break; | |
1219 | case '\033': | |
1220 | fputs_filtered ("\\e", stream); | |
1221 | break; | |
1222 | case '\007': | |
1223 | fputs_filtered ("\\a", stream); | |
1224 | break; | |
1225 | default: | |
1226 | fprintf_filtered (stream, "\\%.3o", (unsigned int) c); | |
1227 | break; | |
1228 | } | |
2bc2e684 FF |
1229 | } else { |
1230 | if (c == '\\' || c == quoter) | |
1231 | fputs_filtered ("\\", stream); | |
1232 | fprintf_filtered (stream, "%c", c); | |
1233 | } | |
bd5635a1 RP |
1234 | } |
1235 | \f | |
1236 | /* Number of lines per page or UINT_MAX if paging is disabled. */ | |
1237 | static unsigned int lines_per_page; | |
1238 | /* Number of chars per line or UNIT_MAX is line folding is disabled. */ | |
1239 | static unsigned int chars_per_line; | |
1240 | /* Current count of lines printed on this page, chars on this line. */ | |
1241 | static unsigned int lines_printed, chars_printed; | |
1242 | ||
1243 | /* Buffer and start column of buffered text, for doing smarter word- | |
1244 | wrapping. When someone calls wrap_here(), we start buffering output | |
1245 | that comes through fputs_filtered(). If we see a newline, we just | |
1246 | spit it out and forget about the wrap_here(). If we see another | |
1247 | wrap_here(), we spit it out and remember the newer one. If we see | |
1248 | the end of the line, we spit out a newline, the indent, and then | |
159dd2aa JK |
1249 | the buffered output. */ |
1250 | ||
1251 | /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which | |
1252 | are waiting to be output (they have already been counted in chars_printed). | |
1253 | When wrap_buffer[0] is null, the buffer is empty. */ | |
1254 | static char *wrap_buffer; | |
bd5635a1 | 1255 | |
159dd2aa JK |
1256 | /* Pointer in wrap_buffer to the next character to fill. */ |
1257 | static char *wrap_pointer; | |
bd5635a1 | 1258 | |
159dd2aa JK |
1259 | /* String to indent by if the wrap occurs. Must not be NULL if wrap_column |
1260 | is non-zero. */ | |
1261 | static char *wrap_indent; | |
1262 | ||
1263 | /* Column number on the screen where wrap_buffer begins, or 0 if wrapping | |
1264 | is not in effect. */ | |
bd5635a1 RP |
1265 | static int wrap_column; |
1266 | ||
e1ce8aa5 | 1267 | /* ARGSUSED */ |
bd5635a1 RP |
1268 | static void |
1269 | set_width_command (args, from_tty, c) | |
1270 | char *args; | |
1271 | int from_tty; | |
1272 | struct cmd_list_element *c; | |
1273 | { | |
1274 | if (!wrap_buffer) | |
1275 | { | |
1276 | wrap_buffer = (char *) xmalloc (chars_per_line + 2); | |
1277 | wrap_buffer[0] = '\0'; | |
1278 | } | |
1279 | else | |
1280 | wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); | |
1281 | wrap_pointer = wrap_buffer; /* Start it at the beginning */ | |
1282 | } | |
1283 | ||
d974236f JG |
1284 | /* Wait, so the user can read what's on the screen. Prompt the user |
1285 | to continue by pressing RETURN. */ | |
1286 | ||
bd5635a1 RP |
1287 | static void |
1288 | prompt_for_continue () | |
1289 | { | |
351b221d | 1290 | char *ignore; |
d8742f46 JK |
1291 | char cont_prompt[120]; |
1292 | ||
4dd876ac JK |
1293 | if (annotation_level > 1) |
1294 | printf_unfiltered ("\n\032\032pre-prompt-for-continue\n"); | |
1295 | ||
d8742f46 JK |
1296 | strcpy (cont_prompt, |
1297 | "---Type <return> to continue, or q <return> to quit---"); | |
1298 | if (annotation_level > 1) | |
1299 | strcat (cont_prompt, "\n\032\032prompt-for-continue\n"); | |
351b221d | 1300 | |
d974236f JG |
1301 | /* We must do this *before* we call gdb_readline, else it will eventually |
1302 | call us -- thinking that we're trying to print beyond the end of the | |
1303 | screen. */ | |
1304 | reinitialize_more_filter (); | |
1305 | ||
bd5635a1 | 1306 | immediate_quit++; |
159dd2aa JK |
1307 | /* On a real operating system, the user can quit with SIGINT. |
1308 | But not on GO32. | |
1309 | ||
1310 | 'q' is provided on all systems so users don't have to change habits | |
1311 | from system to system, and because telling them what to do in | |
1312 | the prompt is more user-friendly than expecting them to think of | |
1313 | SIGINT. */ | |
a94100d1 JK |
1314 | /* Call readline, not gdb_readline, because GO32 readline handles control-C |
1315 | whereas control-C to gdb_readline will cause the user to get dumped | |
1316 | out to DOS. */ | |
d8742f46 | 1317 | ignore = readline (cont_prompt); |
4dd876ac JK |
1318 | |
1319 | if (annotation_level > 1) | |
1320 | printf_unfiltered ("\n\032\032post-prompt-for-continue\n"); | |
1321 | ||
351b221d | 1322 | if (ignore) |
159dd2aa JK |
1323 | { |
1324 | char *p = ignore; | |
1325 | while (*p == ' ' || *p == '\t') | |
1326 | ++p; | |
1327 | if (p[0] == 'q') | |
1328 | request_quit (SIGINT); | |
1329 | free (ignore); | |
1330 | } | |
bd5635a1 | 1331 | immediate_quit--; |
d974236f JG |
1332 | |
1333 | /* Now we have to do this again, so that GDB will know that it doesn't | |
1334 | need to save the ---Type <return>--- line at the top of the screen. */ | |
1335 | reinitialize_more_filter (); | |
1336 | ||
351b221d | 1337 | dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ |
bd5635a1 RP |
1338 | } |
1339 | ||
1340 | /* Reinitialize filter; ie. tell it to reset to original values. */ | |
1341 | ||
1342 | void | |
1343 | reinitialize_more_filter () | |
1344 | { | |
1345 | lines_printed = 0; | |
1346 | chars_printed = 0; | |
1347 | } | |
1348 | ||
1349 | /* Indicate that if the next sequence of characters overflows the line, | |
1350 | a newline should be inserted here rather than when it hits the end. | |
159dd2aa | 1351 | If INDENT is non-null, it is a string to be printed to indent the |
bd5635a1 RP |
1352 | wrapped part on the next line. INDENT must remain accessible until |
1353 | the next call to wrap_here() or until a newline is printed through | |
1354 | fputs_filtered(). | |
1355 | ||
1356 | If the line is already overfull, we immediately print a newline and | |
1357 | the indentation, and disable further wrapping. | |
1358 | ||
2bc2e684 FF |
1359 | If we don't know the width of lines, but we know the page height, |
1360 | we must not wrap words, but should still keep track of newlines | |
1361 | that were explicitly printed. | |
1362 | ||
159dd2aa JK |
1363 | INDENT should not contain tabs, as that will mess up the char count |
1364 | on the next line. FIXME. | |
1365 | ||
1366 | This routine is guaranteed to force out any output which has been | |
1367 | squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be | |
1368 | used to force out output from the wrap_buffer. */ | |
bd5635a1 RP |
1369 | |
1370 | void | |
1371 | wrap_here(indent) | |
159dd2aa | 1372 | char *indent; |
bd5635a1 | 1373 | { |
cad1498f SG |
1374 | /* This should have been allocated, but be paranoid anyway. */ |
1375 | if (!wrap_buffer) | |
1376 | abort (); | |
1377 | ||
bd5635a1 RP |
1378 | if (wrap_buffer[0]) |
1379 | { | |
1380 | *wrap_pointer = '\0'; | |
d8fc8773 | 1381 | fputs_unfiltered (wrap_buffer, gdb_stdout); |
bd5635a1 RP |
1382 | } |
1383 | wrap_pointer = wrap_buffer; | |
1384 | wrap_buffer[0] = '\0'; | |
2bc2e684 FF |
1385 | if (chars_per_line == UINT_MAX) /* No line overflow checking */ |
1386 | { | |
1387 | wrap_column = 0; | |
1388 | } | |
1389 | else if (chars_printed >= chars_per_line) | |
bd5635a1 RP |
1390 | { |
1391 | puts_filtered ("\n"); | |
159dd2aa JK |
1392 | if (indent != NULL) |
1393 | puts_filtered (indent); | |
bd5635a1 RP |
1394 | wrap_column = 0; |
1395 | } | |
1396 | else | |
1397 | { | |
1398 | wrap_column = chars_printed; | |
159dd2aa JK |
1399 | if (indent == NULL) |
1400 | wrap_indent = ""; | |
1401 | else | |
1402 | wrap_indent = indent; | |
bd5635a1 RP |
1403 | } |
1404 | } | |
1405 | ||
51b80b00 FF |
1406 | /* Ensure that whatever gets printed next, using the filtered output |
1407 | commands, starts at the beginning of the line. I.E. if there is | |
1408 | any pending output for the current line, flush it and start a new | |
1409 | line. Otherwise do nothing. */ | |
1410 | ||
1411 | void | |
1412 | begin_line () | |
1413 | { | |
1414 | if (chars_printed > 0) | |
1415 | { | |
1416 | puts_filtered ("\n"); | |
1417 | } | |
1418 | } | |
1419 | ||
199b2450 TL |
1420 | |
1421 | GDB_FILE * | |
1422 | gdb_fopen (name, mode) | |
1423 | char * name; | |
1424 | char * mode; | |
1425 | { | |
1426 | return fopen (name, mode); | |
1427 | } | |
1428 | ||
bd5635a1 | 1429 | void |
199b2450 TL |
1430 | gdb_flush (stream) |
1431 | FILE *stream; | |
1432 | { | |
4ce7ba51 SG |
1433 | if (flush_hook |
1434 | && (stream == gdb_stdout | |
1435 | || stream == gdb_stderr)) | |
0d172a2e JK |
1436 | { |
1437 | flush_hook (stream); | |
1438 | return; | |
1439 | } | |
1440 | ||
199b2450 TL |
1441 | fflush (stream); |
1442 | } | |
1443 | ||
44a09a68 JK |
1444 | /* Like fputs but if FILTER is true, pause after every screenful. |
1445 | ||
1446 | Regardless of FILTER can wrap at points other than the final | |
1447 | character of a line. | |
1448 | ||
1449 | Unlike fputs, fputs_maybe_filtered does not return a value. | |
1450 | It is OK for LINEBUFFER to be NULL, in which case just don't print | |
1451 | anything. | |
1452 | ||
1453 | Note that a longjmp to top level may occur in this routine (only if | |
1454 | FILTER is true) (since prompt_for_continue may do so) so this | |
1455 | routine should not be called when cleanups are not in place. */ | |
1456 | ||
199b2450 TL |
1457 | static void |
1458 | fputs_maybe_filtered (linebuffer, stream, filter) | |
088c3a0b | 1459 | const char *linebuffer; |
bd5635a1 | 1460 | FILE *stream; |
199b2450 | 1461 | int filter; |
bd5635a1 | 1462 | { |
7919c3ed | 1463 | const char *lineptr; |
bd5635a1 RP |
1464 | |
1465 | if (linebuffer == 0) | |
1466 | return; | |
0d172a2e | 1467 | |
bd5635a1 | 1468 | /* Don't do any filtering if it is disabled. */ |
199b2450 | 1469 | if (stream != gdb_stdout |
bd5635a1 RP |
1470 | || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) |
1471 | { | |
d8fc8773 | 1472 | fputs_unfiltered (linebuffer, stream); |
bd5635a1 RP |
1473 | return; |
1474 | } | |
1475 | ||
1476 | /* Go through and output each character. Show line extension | |
1477 | when this is necessary; prompt user for new page when this is | |
1478 | necessary. */ | |
1479 | ||
1480 | lineptr = linebuffer; | |
1481 | while (*lineptr) | |
1482 | { | |
1483 | /* Possible new page. */ | |
199b2450 TL |
1484 | if (filter && |
1485 | (lines_printed >= lines_per_page - 1)) | |
bd5635a1 RP |
1486 | prompt_for_continue (); |
1487 | ||
1488 | while (*lineptr && *lineptr != '\n') | |
1489 | { | |
1490 | /* Print a single line. */ | |
1491 | if (*lineptr == '\t') | |
1492 | { | |
1493 | if (wrap_column) | |
1494 | *wrap_pointer++ = '\t'; | |
1495 | else | |
d8fc8773 | 1496 | fputc_unfiltered ('\t', stream); |
bd5635a1 RP |
1497 | /* Shifting right by 3 produces the number of tab stops |
1498 | we have already passed, and then adding one and | |
1499 | shifting left 3 advances to the next tab stop. */ | |
1500 | chars_printed = ((chars_printed >> 3) + 1) << 3; | |
1501 | lineptr++; | |
1502 | } | |
1503 | else | |
1504 | { | |
1505 | if (wrap_column) | |
1506 | *wrap_pointer++ = *lineptr; | |
1507 | else | |
d8fc8773 | 1508 | fputc_unfiltered (*lineptr, stream); |
bd5635a1 RP |
1509 | chars_printed++; |
1510 | lineptr++; | |
1511 | } | |
1512 | ||
1513 | if (chars_printed >= chars_per_line) | |
1514 | { | |
1515 | unsigned int save_chars = chars_printed; | |
1516 | ||
1517 | chars_printed = 0; | |
1518 | lines_printed++; | |
1519 | /* If we aren't actually wrapping, don't output newline -- | |
1520 | if chars_per_line is right, we probably just overflowed | |
1521 | anyway; if it's wrong, let us keep going. */ | |
1522 | if (wrap_column) | |
d8fc8773 | 1523 | fputc_unfiltered ('\n', stream); |
bd5635a1 RP |
1524 | |
1525 | /* Possible new page. */ | |
1526 | if (lines_printed >= lines_per_page - 1) | |
1527 | prompt_for_continue (); | |
1528 | ||
1529 | /* Now output indentation and wrapped string */ | |
1530 | if (wrap_column) | |
1531 | { | |
d8fc8773 JK |
1532 | fputs_unfiltered (wrap_indent, stream); |
1533 | *wrap_pointer = '\0'; /* Null-terminate saved stuff */ | |
1534 | fputs_unfiltered (wrap_buffer, stream); /* and eject it */ | |
bd5635a1 RP |
1535 | /* FIXME, this strlen is what prevents wrap_indent from |
1536 | containing tabs. However, if we recurse to print it | |
1537 | and count its chars, we risk trouble if wrap_indent is | |
1538 | longer than (the user settable) chars_per_line. | |
1539 | Note also that this can set chars_printed > chars_per_line | |
1540 | if we are printing a long string. */ | |
1541 | chars_printed = strlen (wrap_indent) | |
1542 | + (save_chars - wrap_column); | |
1543 | wrap_pointer = wrap_buffer; /* Reset buffer */ | |
1544 | wrap_buffer[0] = '\0'; | |
1545 | wrap_column = 0; /* And disable fancy wrap */ | |
1546 | } | |
1547 | } | |
1548 | } | |
1549 | ||
1550 | if (*lineptr == '\n') | |
1551 | { | |
1552 | chars_printed = 0; | |
d11c44f1 | 1553 | wrap_here ((char *)0); /* Spit out chars, cancel further wraps */ |
bd5635a1 | 1554 | lines_printed++; |
d8fc8773 | 1555 | fputc_unfiltered ('\n', stream); |
bd5635a1 RP |
1556 | lineptr++; |
1557 | } | |
1558 | } | |
1559 | } | |
1560 | ||
199b2450 TL |
1561 | void |
1562 | fputs_filtered (linebuffer, stream) | |
1563 | const char *linebuffer; | |
1564 | FILE *stream; | |
1565 | { | |
1566 | fputs_maybe_filtered (linebuffer, stream, 1); | |
1567 | } | |
1568 | ||
a7f6f40b JK |
1569 | int |
1570 | putchar_unfiltered (c) | |
199b2450 TL |
1571 | int c; |
1572 | { | |
1573 | char buf[2]; | |
a7f6f40b | 1574 | |
199b2450 TL |
1575 | buf[0] = c; |
1576 | buf[1] = 0; | |
1577 | fputs_unfiltered (buf, gdb_stdout); | |
a7f6f40b | 1578 | return c; |
199b2450 TL |
1579 | } |
1580 | ||
a7f6f40b | 1581 | int |
199b2450 TL |
1582 | fputc_unfiltered (c, stream) |
1583 | int c; | |
1584 | FILE * stream; | |
1585 | { | |
1586 | char buf[2]; | |
a7f6f40b | 1587 | |
199b2450 TL |
1588 | buf[0] = c; |
1589 | buf[1] = 0; | |
1590 | fputs_unfiltered (buf, stream); | |
a7f6f40b | 1591 | return c; |
199b2450 TL |
1592 | } |
1593 | ||
1594 | ||
bd5635a1 RP |
1595 | /* Print a variable number of ARGS using format FORMAT. If this |
1596 | information is going to put the amount written (since the last call | |
d974236f | 1597 | to REINITIALIZE_MORE_FILTER or the last page break) over the page size, |
d8fc8773 | 1598 | call prompt_for_continue to get the users permision to continue. |
bd5635a1 RP |
1599 | |
1600 | Unlike fprintf, this function does not return a value. | |
1601 | ||
1602 | We implement three variants, vfprintf (takes a vararg list and stream), | |
1603 | fprintf (takes a stream to write on), and printf (the usual). | |
1604 | ||
bd5635a1 RP |
1605 | Note also that a longjmp to top level may occur in this routine |
1606 | (since prompt_for_continue may do so) so this routine should not be | |
1607 | called when cleanups are not in place. */ | |
1608 | ||
199b2450 TL |
1609 | static void |
1610 | vfprintf_maybe_filtered (stream, format, args, filter) | |
bd5635a1 | 1611 | FILE *stream; |
b607efe7 | 1612 | const char *format; |
7919c3ed | 1613 | va_list args; |
199b2450 | 1614 | int filter; |
bd5635a1 | 1615 | { |
d8fc8773 JK |
1616 | char *linebuffer; |
1617 | struct cleanup *old_cleanups; | |
bd5635a1 | 1618 | |
d8fc8773 JK |
1619 | vasprintf (&linebuffer, format, args); |
1620 | if (linebuffer == NULL) | |
9c036bd8 JK |
1621 | { |
1622 | fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr); | |
1623 | exit (1); | |
1624 | } | |
d8fc8773 | 1625 | old_cleanups = make_cleanup (free, linebuffer); |
199b2450 | 1626 | fputs_maybe_filtered (linebuffer, stream, filter); |
d8fc8773 | 1627 | do_cleanups (old_cleanups); |
199b2450 TL |
1628 | } |
1629 | ||
1630 | ||
1631 | void | |
1632 | vfprintf_filtered (stream, format, args) | |
1633 | FILE *stream; | |
cd10c7e3 | 1634 | const char *format; |
199b2450 TL |
1635 | va_list args; |
1636 | { | |
1637 | vfprintf_maybe_filtered (stream, format, args, 1); | |
1638 | } | |
1639 | ||
1640 | void | |
1641 | vfprintf_unfiltered (stream, format, args) | |
1642 | FILE *stream; | |
cd10c7e3 | 1643 | const char *format; |
199b2450 TL |
1644 | va_list args; |
1645 | { | |
d8fc8773 JK |
1646 | char *linebuffer; |
1647 | struct cleanup *old_cleanups; | |
1648 | ||
1649 | vasprintf (&linebuffer, format, args); | |
1650 | if (linebuffer == NULL) | |
9c036bd8 JK |
1651 | { |
1652 | fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr); | |
1653 | exit (1); | |
1654 | } | |
d8fc8773 JK |
1655 | old_cleanups = make_cleanup (free, linebuffer); |
1656 | fputs_unfiltered (linebuffer, stream); | |
1657 | do_cleanups (old_cleanups); | |
bd5635a1 RP |
1658 | } |
1659 | ||
51b80b00 FF |
1660 | void |
1661 | vprintf_filtered (format, args) | |
cd10c7e3 | 1662 | const char *format; |
51b80b00 FF |
1663 | va_list args; |
1664 | { | |
199b2450 TL |
1665 | vfprintf_maybe_filtered (gdb_stdout, format, args, 1); |
1666 | } | |
1667 | ||
1668 | void | |
1669 | vprintf_unfiltered (format, args) | |
cd10c7e3 | 1670 | const char *format; |
199b2450 TL |
1671 | va_list args; |
1672 | { | |
d8fc8773 | 1673 | vfprintf_unfiltered (gdb_stdout, format, args); |
51b80b00 FF |
1674 | } |
1675 | ||
bd5635a1 RP |
1676 | /* VARARGS */ |
1677 | void | |
45993f61 | 1678 | #ifdef ANSI_PROTOTYPES |
cd10c7e3 | 1679 | fprintf_filtered (FILE *stream, const char *format, ...) |
85c613aa | 1680 | #else |
bd5635a1 RP |
1681 | fprintf_filtered (va_alist) |
1682 | va_dcl | |
85c613aa | 1683 | #endif |
bd5635a1 | 1684 | { |
546014f7 | 1685 | va_list args; |
45993f61 | 1686 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
1687 | va_start (args, format); |
1688 | #else | |
bd5635a1 RP |
1689 | FILE *stream; |
1690 | char *format; | |
546014f7 PB |
1691 | |
1692 | va_start (args); | |
1693 | stream = va_arg (args, FILE *); | |
1694 | format = va_arg (args, char *); | |
85c613aa | 1695 | #endif |
546014f7 PB |
1696 | vfprintf_filtered (stream, format, args); |
1697 | va_end (args); | |
1698 | } | |
1699 | ||
199b2450 TL |
1700 | /* VARARGS */ |
1701 | void | |
45993f61 | 1702 | #ifdef ANSI_PROTOTYPES |
cd10c7e3 | 1703 | fprintf_unfiltered (FILE *stream, const char *format, ...) |
85c613aa | 1704 | #else |
199b2450 TL |
1705 | fprintf_unfiltered (va_alist) |
1706 | va_dcl | |
85c613aa | 1707 | #endif |
199b2450 TL |
1708 | { |
1709 | va_list args; | |
45993f61 | 1710 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
1711 | va_start (args, format); |
1712 | #else | |
199b2450 TL |
1713 | FILE *stream; |
1714 | char *format; | |
1715 | ||
1716 | va_start (args); | |
1717 | stream = va_arg (args, FILE *); | |
1718 | format = va_arg (args, char *); | |
85c613aa | 1719 | #endif |
199b2450 TL |
1720 | vfprintf_unfiltered (stream, format, args); |
1721 | va_end (args); | |
1722 | } | |
1723 | ||
d8fc8773 | 1724 | /* Like fprintf_filtered, but prints its result indented. |
199b2450 | 1725 | Called as fprintfi_filtered (spaces, stream, format, ...); */ |
546014f7 PB |
1726 | |
1727 | /* VARARGS */ | |
1728 | void | |
45993f61 | 1729 | #ifdef ANSI_PROTOTYPES |
cd10c7e3 | 1730 | fprintfi_filtered (int spaces, FILE *stream, const char *format, ...) |
85c613aa | 1731 | #else |
546014f7 PB |
1732 | fprintfi_filtered (va_alist) |
1733 | va_dcl | |
85c613aa | 1734 | #endif |
546014f7 | 1735 | { |
7919c3ed | 1736 | va_list args; |
45993f61 | 1737 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
1738 | va_start (args, format); |
1739 | #else | |
546014f7 PB |
1740 | int spaces; |
1741 | FILE *stream; | |
1742 | char *format; | |
bd5635a1 RP |
1743 | |
1744 | va_start (args); | |
546014f7 | 1745 | spaces = va_arg (args, int); |
bd5635a1 RP |
1746 | stream = va_arg (args, FILE *); |
1747 | format = va_arg (args, char *); | |
85c613aa | 1748 | #endif |
546014f7 | 1749 | print_spaces_filtered (spaces, stream); |
bd5635a1 | 1750 | |
7919c3ed | 1751 | vfprintf_filtered (stream, format, args); |
bd5635a1 RP |
1752 | va_end (args); |
1753 | } | |
1754 | ||
199b2450 | 1755 | |
bd5635a1 RP |
1756 | /* VARARGS */ |
1757 | void | |
45993f61 | 1758 | #ifdef ANSI_PROTOTYPES |
cd10c7e3 | 1759 | printf_filtered (const char *format, ...) |
85c613aa | 1760 | #else |
bd5635a1 RP |
1761 | printf_filtered (va_alist) |
1762 | va_dcl | |
85c613aa | 1763 | #endif |
bd5635a1 RP |
1764 | { |
1765 | va_list args; | |
45993f61 | 1766 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
1767 | va_start (args, format); |
1768 | #else | |
bd5635a1 RP |
1769 | char *format; |
1770 | ||
1771 | va_start (args); | |
1772 | format = va_arg (args, char *); | |
85c613aa | 1773 | #endif |
199b2450 TL |
1774 | vfprintf_filtered (gdb_stdout, format, args); |
1775 | va_end (args); | |
1776 | } | |
1777 | ||
1778 | ||
1779 | /* VARARGS */ | |
1780 | void | |
45993f61 | 1781 | #ifdef ANSI_PROTOTYPES |
cd10c7e3 | 1782 | printf_unfiltered (const char *format, ...) |
85c613aa | 1783 | #else |
199b2450 TL |
1784 | printf_unfiltered (va_alist) |
1785 | va_dcl | |
85c613aa | 1786 | #endif |
199b2450 TL |
1787 | { |
1788 | va_list args; | |
45993f61 | 1789 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
1790 | va_start (args, format); |
1791 | #else | |
199b2450 TL |
1792 | char *format; |
1793 | ||
1794 | va_start (args); | |
1795 | format = va_arg (args, char *); | |
85c613aa | 1796 | #endif |
199b2450 | 1797 | vfprintf_unfiltered (gdb_stdout, format, args); |
bd5635a1 RP |
1798 | va_end (args); |
1799 | } | |
bd5635a1 | 1800 | |
546014f7 | 1801 | /* Like printf_filtered, but prints it's result indented. |
199b2450 | 1802 | Called as printfi_filtered (spaces, format, ...); */ |
546014f7 PB |
1803 | |
1804 | /* VARARGS */ | |
1805 | void | |
45993f61 | 1806 | #ifdef ANSI_PROTOTYPES |
cd10c7e3 | 1807 | printfi_filtered (int spaces, const char *format, ...) |
85c613aa | 1808 | #else |
546014f7 PB |
1809 | printfi_filtered (va_alist) |
1810 | va_dcl | |
85c613aa | 1811 | #endif |
546014f7 PB |
1812 | { |
1813 | va_list args; | |
45993f61 | 1814 | #ifdef ANSI_PROTOTYPES |
85c613aa C |
1815 | va_start (args, format); |
1816 | #else | |
546014f7 PB |
1817 | int spaces; |
1818 | char *format; | |
1819 | ||
1820 | va_start (args); | |
1821 | spaces = va_arg (args, int); | |
1822 | format = va_arg (args, char *); | |
85c613aa | 1823 | #endif |
199b2450 TL |
1824 | print_spaces_filtered (spaces, gdb_stdout); |
1825 | vfprintf_filtered (gdb_stdout, format, args); | |
546014f7 PB |
1826 | va_end (args); |
1827 | } | |
1828 | ||
51b80b00 FF |
1829 | /* Easy -- but watch out! |
1830 | ||
1831 | This routine is *not* a replacement for puts()! puts() appends a newline. | |
1832 | This one doesn't, and had better not! */ | |
bd5635a1 RP |
1833 | |
1834 | void | |
1835 | puts_filtered (string) | |
cd10c7e3 | 1836 | const char *string; |
bd5635a1 | 1837 | { |
199b2450 TL |
1838 | fputs_filtered (string, gdb_stdout); |
1839 | } | |
1840 | ||
1841 | void | |
1842 | puts_unfiltered (string) | |
cd10c7e3 | 1843 | const char *string; |
199b2450 TL |
1844 | { |
1845 | fputs_unfiltered (string, gdb_stdout); | |
bd5635a1 RP |
1846 | } |
1847 | ||
1848 | /* Return a pointer to N spaces and a null. The pointer is good | |
1849 | until the next call to here. */ | |
1850 | char * | |
1851 | n_spaces (n) | |
1852 | int n; | |
1853 | { | |
1854 | register char *t; | |
1855 | static char *spaces; | |
1856 | static int max_spaces; | |
1857 | ||
1858 | if (n > max_spaces) | |
1859 | { | |
1860 | if (spaces) | |
1861 | free (spaces); | |
3624c875 | 1862 | spaces = (char *) xmalloc (n+1); |
bd5635a1 RP |
1863 | for (t = spaces+n; t != spaces;) |
1864 | *--t = ' '; | |
1865 | spaces[n] = '\0'; | |
1866 | max_spaces = n; | |
1867 | } | |
1868 | ||
1869 | return spaces + max_spaces - n; | |
1870 | } | |
1871 | ||
1872 | /* Print N spaces. */ | |
1873 | void | |
1874 | print_spaces_filtered (n, stream) | |
1875 | int n; | |
1876 | FILE *stream; | |
1877 | { | |
1878 | fputs_filtered (n_spaces (n), stream); | |
1879 | } | |
1880 | \f | |
1881 | /* C++ demangler stuff. */ | |
bd5635a1 | 1882 | |
65ce5df4 JG |
1883 | /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language |
1884 | LANG, using demangling args ARG_MODE, and print it filtered to STREAM. | |
1885 | If the name is not mangled, or the language for the name is unknown, or | |
1886 | demangling is off, the name is printed in its "raw" form. */ | |
1887 | ||
bd5635a1 | 1888 | void |
65ce5df4 | 1889 | fprintf_symbol_filtered (stream, name, lang, arg_mode) |
bd5635a1 RP |
1890 | FILE *stream; |
1891 | char *name; | |
65ce5df4 JG |
1892 | enum language lang; |
1893 | int arg_mode; | |
bd5635a1 | 1894 | { |
65ce5df4 | 1895 | char *demangled; |
bd5d07d9 | 1896 | |
65ce5df4 | 1897 | if (name != NULL) |
bd5d07d9 | 1898 | { |
65ce5df4 JG |
1899 | /* If user wants to see raw output, no problem. */ |
1900 | if (!demangle) | |
bd5d07d9 | 1901 | { |
65ce5df4 JG |
1902 | fputs_filtered (name, stream); |
1903 | } | |
1904 | else | |
1905 | { | |
1906 | switch (lang) | |
1907 | { | |
1908 | case language_cplus: | |
1909 | demangled = cplus_demangle (name, arg_mode); | |
1910 | break; | |
65ce5df4 JG |
1911 | case language_chill: |
1912 | demangled = chill_demangle (name); | |
1913 | break; | |
65ce5df4 JG |
1914 | default: |
1915 | demangled = NULL; | |
1916 | break; | |
1917 | } | |
1918 | fputs_filtered (demangled ? demangled : name, stream); | |
1919 | if (demangled != NULL) | |
1920 | { | |
1921 | free (demangled); | |
1922 | } | |
bd5d07d9 | 1923 | } |
bd5635a1 RP |
1924 | } |
1925 | } | |
51b57ded FF |
1926 | |
1927 | /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any | |
1928 | differences in whitespace. Returns 0 if they match, non-zero if they | |
546014f7 PB |
1929 | don't (slightly different than strcmp()'s range of return values). |
1930 | ||
1931 | As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". | |
2e4964ad FF |
1932 | This "feature" is useful when searching for matching C++ function names |
1933 | (such as if the user types 'break FOO', where FOO is a mangled C++ | |
1934 | function). */ | |
51b57ded | 1935 | |
51b80b00 | 1936 | int |
51b57ded FF |
1937 | strcmp_iw (string1, string2) |
1938 | const char *string1; | |
1939 | const char *string2; | |
1940 | { | |
1941 | while ((*string1 != '\0') && (*string2 != '\0')) | |
1942 | { | |
1943 | while (isspace (*string1)) | |
1944 | { | |
1945 | string1++; | |
1946 | } | |
1947 | while (isspace (*string2)) | |
1948 | { | |
1949 | string2++; | |
1950 | } | |
1951 | if (*string1 != *string2) | |
1952 | { | |
1953 | break; | |
1954 | } | |
1955 | if (*string1 != '\0') | |
1956 | { | |
1957 | string1++; | |
1958 | string2++; | |
1959 | } | |
1960 | } | |
546014f7 | 1961 | return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); |
51b57ded FF |
1962 | } |
1963 | ||
bd5635a1 | 1964 | \f |
bd5635a1 | 1965 | void |
0d172a2e | 1966 | initialize_utils () |
bd5635a1 RP |
1967 | { |
1968 | struct cmd_list_element *c; | |
1969 | ||
1970 | c = add_set_cmd ("width", class_support, var_uinteger, | |
1971 | (char *)&chars_per_line, | |
1972 | "Set number of characters gdb thinks are in a line.", | |
1973 | &setlist); | |
1974 | add_show_from_set (c, &showlist); | |
d747e0af | 1975 | c->function.sfunc = set_width_command; |
bd5635a1 RP |
1976 | |
1977 | add_show_from_set | |
1978 | (add_set_cmd ("height", class_support, | |
1979 | var_uinteger, (char *)&lines_per_page, | |
1980 | "Set number of lines gdb thinks are in a page.", &setlist), | |
1981 | &showlist); | |
1982 | ||
1983 | /* These defaults will be used if we are unable to get the correct | |
1984 | values from termcap. */ | |
03e2a8c8 | 1985 | #if defined(__GO32__) |
51b57ded FF |
1986 | lines_per_page = ScreenRows(); |
1987 | chars_per_line = ScreenCols(); | |
1988 | #else | |
bd5635a1 RP |
1989 | lines_per_page = 24; |
1990 | chars_per_line = 80; | |
49073be0 | 1991 | |
4ce7ba51 | 1992 | #if !defined (MPW) && !defined (_WIN32) |
a6b26c44 SS |
1993 | /* No termcap under MPW, although might be cool to do something |
1994 | by looking at worksheet or console window sizes. */ | |
bd5635a1 RP |
1995 | /* Initialize the screen height and width from termcap. */ |
1996 | { | |
1997 | char *termtype = getenv ("TERM"); | |
1998 | ||
1999 | /* Positive means success, nonpositive means failure. */ | |
2000 | int status; | |
2001 | ||
2002 | /* 2048 is large enough for all known terminals, according to the | |
2003 | GNU termcap manual. */ | |
2004 | char term_buffer[2048]; | |
2005 | ||
2006 | if (termtype) | |
2007 | { | |
2008 | status = tgetent (term_buffer, termtype); | |
2009 | if (status > 0) | |
2010 | { | |
2011 | int val; | |
2012 | ||
2013 | val = tgetnum ("li"); | |
2014 | if (val >= 0) | |
2015 | lines_per_page = val; | |
2016 | else | |
2017 | /* The number of lines per page is not mentioned | |
2018 | in the terminal description. This probably means | |
2019 | that paging is not useful (e.g. emacs shell window), | |
2020 | so disable paging. */ | |
2021 | lines_per_page = UINT_MAX; | |
2022 | ||
2023 | val = tgetnum ("co"); | |
2024 | if (val >= 0) | |
2025 | chars_per_line = val; | |
2026 | } | |
2027 | } | |
2028 | } | |
a6b26c44 | 2029 | #endif /* MPW */ |
bd5635a1 | 2030 | |
1eeba686 PB |
2031 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) |
2032 | ||
4ace50a5 | 2033 | /* If there is a better way to determine the window size, use it. */ |
1eeba686 PB |
2034 | SIGWINCH_HANDLER (); |
2035 | #endif | |
51b57ded | 2036 | #endif |
2bc2e684 | 2037 | /* If the output is not a terminal, don't paginate it. */ |
199b2450 | 2038 | if (!ISATTY (gdb_stdout)) |
2bc2e684 FF |
2039 | lines_per_page = UINT_MAX; |
2040 | ||
bd5635a1 RP |
2041 | set_width_command ((char *)NULL, 0, c); |
2042 | ||
2043 | add_show_from_set | |
2044 | (add_set_cmd ("demangle", class_support, var_boolean, | |
2045 | (char *)&demangle, | |
2046 | "Set demangling of encoded C++ names when displaying symbols.", | |
f266e564 JK |
2047 | &setprintlist), |
2048 | &showprintlist); | |
bd5635a1 RP |
2049 | |
2050 | add_show_from_set | |
2051 | (add_set_cmd ("sevenbit-strings", class_support, var_boolean, | |
2052 | (char *)&sevenbit_strings, | |
2053 | "Set printing of 8-bit characters in strings as \\nnn.", | |
f266e564 JK |
2054 | &setprintlist), |
2055 | &showprintlist); | |
bd5635a1 RP |
2056 | |
2057 | add_show_from_set | |
2058 | (add_set_cmd ("asm-demangle", class_support, var_boolean, | |
2059 | (char *)&asm_demangle, | |
2060 | "Set demangling of C++ names in disassembly listings.", | |
f266e564 JK |
2061 | &setprintlist), |
2062 | &showprintlist); | |
bd5635a1 | 2063 | } |
1eeba686 PB |
2064 | |
2065 | /* Machine specific function to handle SIGWINCH signal. */ | |
2066 | ||
2067 | #ifdef SIGWINCH_HANDLER_BODY | |
2068 | SIGWINCH_HANDLER_BODY | |
2069 | #endif | |
a243a22f | 2070 | \f |
54109914 | 2071 | /* Support for converting target fp numbers into host DOUBLEST format. */ |
a243a22f SG |
2072 | |
2073 | /* XXX - This code should really be in libiberty/floatformat.c, however | |
2074 | configuration issues with libiberty made this very difficult to do in the | |
2075 | available time. */ | |
2076 | ||
2077 | #include "floatformat.h" | |
2078 | #include <math.h> /* ldexp */ | |
2079 | ||
2080 | /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not | |
2081 | going to bother with trying to muck around with whether it is defined in | |
2082 | a system header, what we do if not, etc. */ | |
2083 | #define FLOATFORMAT_CHAR_BIT 8 | |
2084 | ||
2085 | static unsigned long get_field PARAMS ((unsigned char *, | |
2086 | enum floatformat_byteorders, | |
2087 | unsigned int, | |
2088 | unsigned int, | |
2089 | unsigned int)); | |
2090 | ||
2091 | /* Extract a field which starts at START and is LEN bytes long. DATA and | |
2092 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
2093 | static unsigned long | |
2094 | get_field (data, order, total_len, start, len) | |
2095 | unsigned char *data; | |
2096 | enum floatformat_byteorders order; | |
2097 | unsigned int total_len; | |
2098 | unsigned int start; | |
2099 | unsigned int len; | |
2100 | { | |
2101 | unsigned long result; | |
2102 | unsigned int cur_byte; | |
2103 | int cur_bitshift; | |
2104 | ||
2105 | /* Start at the least significant part of the field. */ | |
2106 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
2107 | if (order == floatformat_little) | |
2108 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1; | |
2109 | cur_bitshift = | |
2110 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
2111 | result = *(data + cur_byte) >> (-cur_bitshift); | |
2112 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
2113 | if (order == floatformat_little) | |
2114 | ++cur_byte; | |
2115 | else | |
2116 | --cur_byte; | |
2117 | ||
2118 | /* Move towards the most significant part of the field. */ | |
2119 | while (cur_bitshift < len) | |
2120 | { | |
2121 | if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) | |
2122 | /* This is the last byte; zero out the bits which are not part of | |
2123 | this field. */ | |
2124 | result |= | |
2125 | (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1)) | |
2126 | << cur_bitshift; | |
2127 | else | |
2128 | result |= *(data + cur_byte) << cur_bitshift; | |
2129 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
2130 | if (order == floatformat_little) | |
2131 | ++cur_byte; | |
2132 | else | |
2133 | --cur_byte; | |
2134 | } | |
2135 | return result; | |
2136 | } | |
2137 | ||
54109914 | 2138 | /* Convert from FMT to a DOUBLEST. |
a243a22f | 2139 | FROM is the address of the extended float. |
54109914 | 2140 | Store the DOUBLEST in *TO. */ |
a243a22f SG |
2141 | |
2142 | void | |
54109914 | 2143 | floatformat_to_doublest (fmt, from, to) |
a243a22f SG |
2144 | const struct floatformat *fmt; |
2145 | char *from; | |
54109914 | 2146 | DOUBLEST *to; |
a243a22f SG |
2147 | { |
2148 | unsigned char *ufrom = (unsigned char *)from; | |
54109914 | 2149 | DOUBLEST dto; |
a243a22f SG |
2150 | long exponent; |
2151 | unsigned long mant; | |
2152 | unsigned int mant_bits, mant_off; | |
2153 | int mant_bits_left; | |
449abd89 | 2154 | int special_exponent; /* It's a NaN, denorm or zero */ |
a243a22f SG |
2155 | |
2156 | exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
2157 | fmt->exp_start, fmt->exp_len); | |
2158 | /* Note that if exponent indicates a NaN, we can't really do anything useful | |
2159 | (not knowing if the host has NaN's, or how to build one). So it will | |
2160 | end up as an infinity or something close; that is OK. */ | |
2161 | ||
2162 | mant_bits_left = fmt->man_len; | |
2163 | mant_off = fmt->man_start; | |
2164 | dto = 0.0; | |
449abd89 SG |
2165 | |
2166 | special_exponent = exponent == 0 || exponent == fmt->exp_nan; | |
2167 | ||
2168 | /* Don't bias zero's, denorms or NaNs. */ | |
2169 | if (!special_exponent) | |
2170 | exponent -= fmt->exp_bias; | |
a243a22f SG |
2171 | |
2172 | /* Build the result algebraically. Might go infinite, underflow, etc; | |
2173 | who cares. */ | |
2174 | ||
2175 | /* If this format uses a hidden bit, explicitly add it in now. Otherwise, | |
2176 | increment the exponent by one to account for the integer bit. */ | |
2177 | ||
449abd89 SG |
2178 | if (!special_exponent) |
2179 | if (fmt->intbit == floatformat_intbit_no) | |
2180 | dto = ldexp (1.0, exponent); | |
2181 | else | |
2182 | exponent++; | |
a243a22f SG |
2183 | |
2184 | while (mant_bits_left > 0) | |
2185 | { | |
2186 | mant_bits = min (mant_bits_left, 32); | |
2187 | ||
2188 | mant = get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
2189 | mant_off, mant_bits); | |
2190 | ||
2191 | dto += ldexp ((double)mant, exponent - mant_bits); | |
2192 | exponent -= mant_bits; | |
2193 | mant_off += mant_bits; | |
2194 | mant_bits_left -= mant_bits; | |
2195 | } | |
2196 | ||
2197 | /* Negate it if negative. */ | |
2198 | if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1)) | |
2199 | dto = -dto; | |
449abd89 | 2200 | *to = dto; |
a243a22f SG |
2201 | } |
2202 | \f | |
2203 | static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders, | |
2204 | unsigned int, | |
2205 | unsigned int, | |
2206 | unsigned int, | |
2207 | unsigned long)); | |
2208 | ||
2209 | /* Set a field which starts at START and is LEN bytes long. DATA and | |
2210 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
2211 | static void | |
2212 | put_field (data, order, total_len, start, len, stuff_to_put) | |
2213 | unsigned char *data; | |
2214 | enum floatformat_byteorders order; | |
2215 | unsigned int total_len; | |
2216 | unsigned int start; | |
2217 | unsigned int len; | |
2218 | unsigned long stuff_to_put; | |
2219 | { | |
2220 | unsigned int cur_byte; | |
2221 | int cur_bitshift; | |
2222 | ||
2223 | /* Start at the least significant part of the field. */ | |
2224 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
2225 | if (order == floatformat_little) | |
2226 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1; | |
2227 | cur_bitshift = | |
2228 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
2229 | *(data + cur_byte) &= | |
2230 | ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift)); | |
2231 | *(data + cur_byte) |= | |
2232 | (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); | |
2233 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
2234 | if (order == floatformat_little) | |
2235 | ++cur_byte; | |
2236 | else | |
2237 | --cur_byte; | |
2238 | ||
2239 | /* Move towards the most significant part of the field. */ | |
2240 | while (cur_bitshift < len) | |
2241 | { | |
2242 | if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) | |
2243 | { | |
2244 | /* This is the last byte. */ | |
2245 | *(data + cur_byte) &= | |
2246 | ~((1 << (len - cur_bitshift)) - 1); | |
2247 | *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); | |
2248 | } | |
2249 | else | |
2250 | *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) | |
2251 | & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); | |
2252 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
2253 | if (order == floatformat_little) | |
2254 | ++cur_byte; | |
2255 | else | |
2256 | --cur_byte; | |
2257 | } | |
2258 | } | |
2259 | ||
54109914 | 2260 | #ifdef HAVE_LONG_DOUBLE |
a243a22f SG |
2261 | /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR. |
2262 | The range of the returned value is >= 0.5 and < 1.0. This is equivalent to | |
2263 | frexp, but operates on the long double data type. */ | |
2264 | ||
2265 | static long double ldfrexp PARAMS ((long double value, int *eptr)); | |
2266 | ||
2267 | static long double | |
2268 | ldfrexp (value, eptr) | |
2269 | long double value; | |
2270 | int *eptr; | |
2271 | { | |
2272 | long double tmp; | |
2273 | int exp; | |
2274 | ||
2275 | /* Unfortunately, there are no portable functions for extracting the exponent | |
2276 | of a long double, so we have to do it iteratively by multiplying or dividing | |
2277 | by two until the fraction is between 0.5 and 1.0. */ | |
2278 | ||
2279 | if (value < 0.0l) | |
2280 | value = -value; | |
2281 | ||
2282 | tmp = 1.0l; | |
2283 | exp = 0; | |
2284 | ||
2285 | if (value >= tmp) /* Value >= 1.0 */ | |
2286 | while (value >= tmp) | |
2287 | { | |
2288 | tmp *= 2.0l; | |
2289 | exp++; | |
2290 | } | |
2291 | else if (value != 0.0l) /* Value < 1.0 and > 0.0 */ | |
2292 | { | |
2293 | while (value < tmp) | |
2294 | { | |
2295 | tmp /= 2.0l; | |
2296 | exp--; | |
2297 | } | |
2298 | tmp *= 2.0l; | |
2299 | exp++; | |
2300 | } | |
2301 | ||
2302 | *eptr = exp; | |
2303 | return value/tmp; | |
2304 | } | |
54109914 FF |
2305 | #endif /* HAVE_LONG_DOUBLE */ |
2306 | ||
a243a22f | 2307 | |
54109914 | 2308 | /* The converse: convert the DOUBLEST *FROM to an extended float |
a243a22f SG |
2309 | and store where TO points. Neither FROM nor TO have any alignment |
2310 | restrictions. */ | |
2311 | ||
2312 | void | |
54109914 | 2313 | floatformat_from_doublest (fmt, from, to) |
a243a22f | 2314 | CONST struct floatformat *fmt; |
54109914 | 2315 | DOUBLEST *from; |
a243a22f SG |
2316 | char *to; |
2317 | { | |
54109914 | 2318 | DOUBLEST dfrom; |
a243a22f | 2319 | int exponent; |
54109914 | 2320 | DOUBLEST mant; |
a243a22f SG |
2321 | unsigned int mant_bits, mant_off; |
2322 | int mant_bits_left; | |
2323 | unsigned char *uto = (unsigned char *)to; | |
2324 | ||
2325 | memcpy (&dfrom, from, sizeof (dfrom)); | |
2326 | memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT); | |
2327 | if (dfrom == 0) | |
2328 | return; /* Result is zero */ | |
2329 | if (dfrom != dfrom) | |
2330 | { | |
2331 | /* From is NaN */ | |
2332 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, | |
2333 | fmt->exp_len, fmt->exp_nan); | |
2334 | /* Be sure it's not infinity, but NaN value is irrel */ | |
2335 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start, | |
2336 | 32, 1); | |
2337 | return; | |
2338 | } | |
2339 | ||
2340 | /* If negative, set the sign bit. */ | |
2341 | if (dfrom < 0) | |
2342 | { | |
2343 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1); | |
2344 | dfrom = -dfrom; | |
2345 | } | |
2346 | ||
2347 | /* How to tell an infinity from an ordinary number? FIXME-someday */ | |
2348 | ||
54109914 | 2349 | #ifdef HAVE_LONG_DOUBLE |
a243a22f | 2350 | mant = ldfrexp (dfrom, &exponent); |
54109914 FF |
2351 | #else |
2352 | mant = frexp (dfrom, &exponent); | |
2353 | #endif | |
2354 | ||
a243a22f SG |
2355 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len, |
2356 | exponent + fmt->exp_bias - 1); | |
2357 | ||
2358 | mant_bits_left = fmt->man_len; | |
2359 | mant_off = fmt->man_start; | |
2360 | while (mant_bits_left > 0) | |
2361 | { | |
2362 | unsigned long mant_long; | |
2363 | mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; | |
2364 | ||
2365 | mant *= 4294967296.0; | |
2366 | mant_long = (unsigned long)mant; | |
2367 | mant -= mant_long; | |
2368 | ||
2369 | /* If the integer bit is implicit, then we need to discard it. | |
2370 | If we are discarding a zero, we should be (but are not) creating | |
2371 | a denormalized number which means adjusting the exponent | |
2372 | (I think). */ | |
2373 | if (mant_bits_left == fmt->man_len | |
2374 | && fmt->intbit == floatformat_intbit_no) | |
2375 | { | |
28444bf3 | 2376 | mant_long <<= 1; |
a243a22f SG |
2377 | mant_bits -= 1; |
2378 | } | |
28444bf3 DP |
2379 | |
2380 | if (mant_bits < 32) | |
a243a22f SG |
2381 | { |
2382 | /* The bits we want are in the most significant MANT_BITS bits of | |
2383 | mant_long. Move them to the least significant. */ | |
2384 | mant_long >>= 32 - mant_bits; | |
2385 | } | |
2386 | ||
2387 | put_field (uto, fmt->byteorder, fmt->totalsize, | |
2388 | mant_off, mant_bits, mant_long); | |
2389 | mant_off += mant_bits; | |
2390 | mant_bits_left -= mant_bits; | |
2391 | } | |
2392 | } | |
28444bf3 DP |
2393 | |
2394 | /* temporary storage using circular buffer */ | |
4ce7ba51 | 2395 | #define NUMCELLS 16 |
28444bf3 | 2396 | #define CELLSIZE 32 |
4ce7ba51 | 2397 | static char* |
28444bf3 DP |
2398 | get_cell() |
2399 | { | |
4ce7ba51 | 2400 | static char buf[NUMCELLS][CELLSIZE]; |
28444bf3 | 2401 | static int cell=0; |
4ce7ba51 | 2402 | if (++cell>=NUMCELLS) cell=0; |
28444bf3 DP |
2403 | return buf[cell]; |
2404 | } | |
2405 | ||
2406 | /* print routines to handle variable size regs, etc */ | |
4ce7ba51 SG |
2407 | static int thirty_two = 32; /* eliminate warning from compiler on 32-bit systems */ |
2408 | ||
28444bf3 DP |
2409 | char* |
2410 | paddr(addr) | |
2411 | t_addr addr; | |
2412 | { | |
2413 | char *paddr_str=get_cell(); | |
2414 | switch (sizeof(t_addr)) | |
2415 | { | |
2416 | case 8: | |
2417 | sprintf(paddr_str,"%08x%08x", | |
4ce7ba51 | 2418 | (unsigned long)(addr>>thirty_two),(unsigned long)(addr&0xffffffff)); |
28444bf3 DP |
2419 | break; |
2420 | case 4: | |
2421 | sprintf(paddr_str,"%08x",(unsigned long)addr); | |
2422 | break; | |
2423 | case 2: | |
2424 | sprintf(paddr_str,"%04x",(unsigned short)(addr&0xffff)); | |
2425 | break; | |
2426 | default: | |
2427 | sprintf(paddr_str,"%x",addr); | |
2428 | } | |
2429 | return paddr_str; | |
2430 | } | |
2431 | ||
2432 | char* | |
2433 | preg(reg) | |
2434 | t_reg reg; | |
2435 | { | |
2436 | char *preg_str=get_cell(); | |
2437 | switch (sizeof(t_reg)) | |
2438 | { | |
2439 | case 8: | |
2440 | sprintf(preg_str,"%08x%08x", | |
4ce7ba51 | 2441 | (unsigned long)(reg>>thirty_two),(unsigned long)(reg&0xffffffff)); |
28444bf3 DP |
2442 | break; |
2443 | case 4: | |
2444 | sprintf(preg_str,"%08x",(unsigned long)reg); | |
2445 | break; | |
2446 | case 2: | |
2447 | sprintf(preg_str,"%04x",(unsigned short)(reg&0xffff)); | |
2448 | break; | |
2449 | default: | |
2450 | sprintf(preg_str,"%x",reg); | |
2451 | } | |
2452 | return preg_str; | |
2453 | } | |
2454 | ||
4ce7ba51 SG |
2455 | char* |
2456 | paddr_nz(addr) | |
2457 | t_addr addr; | |
2458 | { | |
2459 | char *paddr_str=get_cell(); | |
2460 | switch (sizeof(t_addr)) | |
2461 | { | |
2462 | case 8: | |
2463 | { | |
2464 | unsigned long high = (unsigned long)(addr>>thirty_two); | |
2465 | if (high == 0) | |
2466 | sprintf(paddr_str,"%x", (unsigned long)(addr&0xffffffff)); | |
2467 | else | |
2468 | sprintf(paddr_str,"%x%08x", | |
2469 | high, (unsigned long)(addr&0xffffffff)); | |
2470 | break; | |
2471 | } | |
2472 | case 4: | |
2473 | sprintf(paddr_str,"%x",(unsigned long)addr); | |
2474 | break; | |
2475 | case 2: | |
2476 | sprintf(paddr_str,"%x",(unsigned short)(addr&0xffff)); | |
2477 | break; | |
2478 | default: | |
2479 | sprintf(paddr_str,"%x",addr); | |
2480 | } | |
2481 | return paddr_str; | |
2482 | } | |
2483 | ||
2484 | char* | |
2485 | preg_nz(reg) | |
2486 | t_reg reg; | |
2487 | { | |
2488 | char *preg_str=get_cell(); | |
2489 | switch (sizeof(t_reg)) | |
2490 | { | |
2491 | case 8: | |
2492 | { | |
2493 | unsigned long high = (unsigned long)(reg>>thirty_two); | |
2494 | if (high == 0) | |
2495 | sprintf(preg_str,"%x", (unsigned long)(reg&0xffffffff)); | |
2496 | else | |
2497 | sprintf(preg_str,"%x%08x", | |
2498 | high, (unsigned long)(reg&0xffffffff)); | |
2499 | break; | |
2500 | } | |
2501 | case 4: | |
2502 | sprintf(preg_str,"%x",(unsigned long)reg); | |
2503 | break; | |
2504 | case 2: | |
2505 | sprintf(preg_str,"%x",(unsigned short)(reg&0xffff)); | |
2506 | break; | |
2507 | default: | |
2508 | sprintf(preg_str,"%x",reg); | |
2509 | } | |
2510 | return preg_str; | |
2511 | } |