]> Git Repo - binutils.git/blob - gdb/utils.c
Make writing to files work properly. (Fixes to BFD are also needed.)
[binutils.git] / gdb / utils.c
1 /* General utility routines for GDB, the GNU debugger.
2    Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <stdio.h>
21 #include <sys/ioctl.h>
22 #include <sys/param.h>
23 #include <pwd.h>
24 #include "defs.h"
25 #include "param.h"
26 #include "signals.h"
27 #include "gdbcmd.h"
28 #include "terminal.h"
29 #include <varargs.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include "bfd.h"
33 #include "target.h"
34
35 extern volatile void return_to_top_level ();
36 extern volatile void exit ();
37 extern char *gdb_readline ();
38 extern char *getenv();
39 extern char *malloc();
40 extern char *realloc();
41
42 /* If this definition isn't overridden by the header files, assume
43    that isatty and fileno exist on this system.  */
44 #ifndef ISATTY
45 #define ISATTY(FP)      (isatty (fileno (FP)))
46 #endif
47
48 #ifdef MISSING_VPRINTF
49 #ifdef __GNU_LIBRARY
50 #undef MISSING_VPRINTF
51 #else  /* !__GNU_LIBRARY */
52
53 #ifndef vfprintf
54 #define vfprintf(file, format, ap) _doprnt (format, ap, file)
55 #endif /* vfprintf */
56
57 #ifndef vprintf
58 /* Can't #define it since printcmd.c needs it */
59 void
60 vprintf (format, ap)
61      char *format;
62      va_list ap;
63 {
64   vfprintf (stdout, format, ap);
65 }
66 #endif /* vprintf */
67
68 #endif /* GNU_LIBRARY */
69 #endif /* MISSING_VPRINTF */
70
71 void error ();
72 void fatal ();
73
74 /* Chain of cleanup actions established with make_cleanup,
75    to be executed if an error happens.  */
76
77 static struct cleanup *cleanup_chain;
78
79 /* Nonzero means a quit has been requested.  */
80
81 int quit_flag;
82
83 /* Nonzero means quit immediately if Control-C is typed now,
84    rather than waiting until QUIT is executed.  */
85
86 int immediate_quit;
87
88 /* Nonzero means that encoded C++ names should be printed out in their
89    C++ form rather than raw.  */
90
91 int demangle = 1;
92
93 /* Nonzero means that encoded C++ names should be printed out in their
94    C++ form even in assembler language displays.  If this is set, but
95    DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls.  */
96
97 int asm_demangle = 0;
98
99 /* Nonzero means that strings with character values >0x7F should be printed
100    as octal escapes.  Zero means just print the value (e.g. it's an
101    international character, and the terminal or window can cope.)  */
102
103 int sevenbit_strings = 0;
104 \f
105 /* Add a new cleanup to the cleanup_chain,
106    and return the previous chain pointer
107    to be passed later to do_cleanups or discard_cleanups.
108    Args are FUNCTION to clean up with, and ARG to pass to it.  */
109
110 struct cleanup *
111 make_cleanup (function, arg)
112      void (*function) ();
113      int arg;
114 {
115   register struct cleanup *new
116     = (struct cleanup *) xmalloc (sizeof (struct cleanup));
117   register struct cleanup *old_chain = cleanup_chain;
118
119   new->next = cleanup_chain;
120   new->function = function;
121   new->arg = arg;
122   cleanup_chain = new;
123
124   return old_chain;
125 }
126
127 /* Discard cleanups and do the actions they describe
128    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
129
130 void
131 do_cleanups (old_chain)
132      register struct cleanup *old_chain;
133 {
134   register struct cleanup *ptr;
135   while ((ptr = cleanup_chain) != old_chain)
136     {
137       cleanup_chain = ptr->next;        /* Do this first incase recursion */
138       (*ptr->function) (ptr->arg);
139       free (ptr);
140     }
141 }
142
143 /* Discard cleanups, not doing the actions they describe,
144    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
145
146 void
147 discard_cleanups (old_chain)
148      register struct cleanup *old_chain;
149 {
150   register struct cleanup *ptr;
151   while ((ptr = cleanup_chain) != old_chain)
152     {
153       cleanup_chain = ptr->next;
154       free (ptr);
155     }
156 }
157
158 /* Set the cleanup_chain to 0, and return the old cleanup chain.  */
159 struct cleanup *
160 save_cleanups ()
161 {
162   struct cleanup *old_chain = cleanup_chain;
163
164   cleanup_chain = 0;
165   return old_chain;
166 }
167
168 /* Restore the cleanup chain from a previously saved chain.  */
169 void
170 restore_cleanups (chain)
171      struct cleanup *chain;
172 {
173   cleanup_chain = chain;
174 }
175
176 /* This function is useful for cleanups.
177    Do
178
179      foo = xmalloc (...);
180      old_chain = make_cleanup (free_current_contents, &foo);
181
182    to arrange to free the object thus allocated.  */
183
184 void
185 free_current_contents (location)
186      char **location;
187 {
188   free (*location);
189 }
190 \f
191 /* Print an error message and return to command level.
192    The first argument STRING is the error message, used as a fprintf string,
193    and the remaining args are passed as arguments to it.  */
194
195 /* VARARGS */
196 void
197 error (va_alist)
198      va_dcl
199 {
200   va_list args;
201   char *string;
202
203   va_start (args);
204   target_terminal_ours ();
205   fflush (stdout);
206   string = va_arg (args, char *);
207   vfprintf (stderr, string, args);
208   fprintf (stderr, "\n");
209   va_end (args);
210   return_to_top_level ();
211 }
212
213 /* Print an error message and exit reporting failure.
214    This is for a error that we cannot continue from.
215    The arguments are printed a la printf.  */
216
217 /* VARARGS */
218 void
219 fatal (va_alist)
220      va_dcl
221 {
222   va_list args;
223   char *string;
224
225   va_start (args);
226   string = va_arg (args, char *);
227   fprintf (stderr, "gdb: ");
228   vfprintf (stderr, string, args);
229   fprintf (stderr, "\n");
230   va_end (args);
231   exit (1);
232 }
233
234 /* Print an error message and exit, dumping core.
235    The arguments are printed a la printf ().  */
236 /* VARARGS */
237 void
238 fatal_dump_core (va_alist)
239      va_dcl
240 {
241   va_list args;
242   char *string;
243
244   va_start (args);
245   string = va_arg (args, char *);
246   /* "internal error" is always correct, since GDB should never dump
247      core, no matter what the input.  */
248   fprintf (stderr, "gdb internal error: ");
249   vfprintf (stderr, string, args);
250   fprintf (stderr, "\n");
251   va_end (args);
252
253   signal (SIGQUIT, SIG_DFL);
254   kill (getpid (), SIGQUIT);
255   /* We should never get here, but just in case...  */
256   exit (1);
257 }
258 \f
259 /* Memory management stuff (malloc friends).  */
260
261 #if defined (NO_MALLOC_CHECK)
262 void
263 init_malloc ()
264 {}
265 #else /* Have mcheck().  */
266 static void
267 malloc_botch ()
268 {
269   fatal_dump_core ("Memory corruption");
270 }
271
272 void
273 init_malloc ()
274 {
275   mcheck (malloc_botch);
276   mtrace ();
277 }
278 #endif /* Have mcheck().  */
279
280 /* Like malloc but get error if no storage available.  */
281
282 #ifdef __STDC__
283 void *
284 #else
285 char *
286 #endif
287 xmalloc (size)
288      long size;
289 {
290   register char *val;
291
292   /* At least one place (dbxread.c:condense_misc_bunches where misc_count == 0)
293      GDB wants to allocate zero bytes.  */
294   if (size == 0)
295     return NULL;
296   
297   val = (char *) malloc (size);
298   if (!val)
299     fatal ("virtual memory exhausted.", 0);
300   return val;
301 }
302
303 /* Like realloc but get error if no storage available.  */
304
305 #ifdef __STDC__
306 void *
307 #else
308 char *
309 #endif
310 xrealloc (ptr, size)
311      char *ptr;
312      long size;
313 {
314   register char *val = (char *) realloc (ptr, size);
315   if (!val)
316     fatal ("virtual memory exhausted.", 0);
317   return val;
318 }
319
320 /* Print the system error message for errno, and also mention STRING
321    as the file name for which the error was encountered.
322    Then return to command level.  */
323
324 void
325 perror_with_name (string)
326      char *string;
327 {
328   extern int sys_nerr;
329   extern char *sys_errlist[];
330   char *err;
331   char *combined;
332
333   if (errno < sys_nerr)
334     err = sys_errlist[errno];
335   else
336     err = "unknown error";
337
338   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
339   strcpy (combined, string);
340   strcat (combined, ": ");
341   strcat (combined, err);
342
343   /* I understand setting these is a matter of taste.  Still, some people
344      may clear errno but not know about bfd_error.  Doing this here is not
345      unreasonable. */
346   bfd_error = no_error;
347   errno = 0;
348
349   error ("%s.", combined);
350 }
351
352 /* Print the system error message for ERRCODE, and also mention STRING
353    as the file name for which the error was encountered.  */
354
355 void
356 print_sys_errmsg (string, errcode)
357      char *string;
358      int errcode;
359 {
360   extern int sys_nerr;
361   extern char *sys_errlist[];
362   char *err;
363   char *combined;
364
365   if (errcode < sys_nerr)
366     err = sys_errlist[errcode];
367   else
368     err = "unknown error";
369
370   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
371   strcpy (combined, string);
372   strcat (combined, ": ");
373   strcat (combined, err);
374
375   printf ("%s.\n", combined);
376 }
377
378 /* Control C eventually causes this to be called, at a convenient time.  */
379
380 void
381 quit ()
382 {
383   target_terminal_ours ();
384   wrap_here ((char *)0);                /* Force out any pending output */
385 #ifdef HAVE_TERMIO
386   ioctl (fileno (stdout), TCFLSH, 1);
387 #else /* not HAVE_TERMIO */
388   ioctl (fileno (stdout), TIOCFLUSH, 0);
389 #endif /* not HAVE_TERMIO */
390 #ifdef TIOCGPGRP
391   error ("Quit");
392 #else
393   error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
394 #endif /* TIOCGPGRP */
395 }
396
397 /* Control C comes here */
398
399 void
400 request_quit ()
401 {
402   quit_flag = 1;
403
404 #ifdef USG
405   /* Restore the signal handler.  */
406   signal (SIGINT, request_quit);
407 #endif
408
409   if (immediate_quit)
410     quit ();
411 }
412 \f
413 /* My replacement for the read system call.
414    Used like `read' but keeps going if `read' returns too soon.  */
415
416 int
417 myread (desc, addr, len)
418      int desc;
419      char *addr;
420      int len;
421 {
422   register int val;
423   int orglen = len;
424
425   while (len > 0)
426     {
427       val = read (desc, addr, len);
428       if (val < 0)
429         return val;
430       if (val == 0)
431         return orglen - len;
432       len -= val;
433       addr += val;
434     }
435   return orglen;
436 }
437 \f
438 /* Make a copy of the string at PTR with SIZE characters
439    (and add a null character at the end in the copy).
440    Uses malloc to get the space.  Returns the address of the copy.  */
441
442 char *
443 savestring (ptr, size)
444      char *ptr;
445      int size;
446 {
447   register char *p = (char *) xmalloc (size + 1);
448   bcopy (ptr, p, size);
449   p[size] = 0;
450   return p;
451 }
452
453 /* The "const" is so it compiles under DGUX (which prototypes strsave
454    in <string.h>.  FIXME: This should be named "xstrsave", shouldn't it?
455    Doesn't real strsave return NULL if out of memory?  */
456 char *
457 strsave (ptr)
458      const char *ptr;
459 {
460   return savestring (ptr, strlen (ptr));
461 }
462
463 char *
464 concat (s1, s2, s3)
465      char *s1, *s2, *s3;
466 {
467   register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
468   register char *val = (char *) xmalloc (len);
469   strcpy (val, s1);
470   strcat (val, s2);
471   strcat (val, s3);
472   return val;
473 }
474
475 void
476 print_spaces (n, file)
477      register int n;
478      register FILE *file;
479 {
480   while (n-- > 0)
481     fputc (' ', file);
482 }
483
484 /* Ask user a y-or-n question and return 1 iff answer is yes.
485    Takes three args which are given to printf to print the question.
486    The first, a control string, should end in "? ".
487    It should not say how to answer, because we do that.  */
488
489 /* VARARGS */
490 int
491 query (va_alist)
492      va_dcl
493 {
494   va_list args;
495   char *ctlstr;
496   register int answer;
497   register int ans2;
498
499   /* Automatically answer "yes" if input is not from a terminal.  */
500   if (!input_from_terminal_p ())
501     return 1;
502
503   while (1)
504     {
505       va_start (args);
506       ctlstr = va_arg (args, char *);
507       vfprintf (stdout, ctlstr, args);
508       va_end (args);
509       printf ("(y or n) ");
510       fflush (stdout);
511       answer = fgetc (stdin);
512       clearerr (stdin);         /* in case of C-d */
513       if (answer == EOF)        /* C-d */
514         return 1;
515       if (answer != '\n')       /* Eat rest of input line, to EOF or newline */
516         do 
517           {
518             ans2 = fgetc (stdin);
519             clearerr (stdin);
520           }
521         while (ans2 != EOF && ans2 != '\n');
522       if (answer >= 'a')
523         answer -= 040;
524       if (answer == 'Y')
525         return 1;
526       if (answer == 'N')
527         return 0;
528       printf ("Please answer y or n.\n");
529     }
530 }
531 \f
532 /* Parse a C escape sequence.  STRING_PTR points to a variable
533    containing a pointer to the string to parse.  That pointer
534    should point to the character after the \.  That pointer
535    is updated past the characters we use.  The value of the
536    escape sequence is returned.
537
538    A negative value means the sequence \ newline was seen,
539    which is supposed to be equivalent to nothing at all.
540
541    If \ is followed by a null character, we return a negative
542    value and leave the string pointer pointing at the null character.
543
544    If \ is followed by 000, we return 0 and leave the string pointer
545    after the zeros.  A value of 0 does not mean end of string.  */
546
547 int
548 parse_escape (string_ptr)
549      char **string_ptr;
550 {
551   register int c = *(*string_ptr)++;
552   switch (c)
553     {
554     case 'a':
555       return '\a';
556     case 'b':
557       return '\b';
558     case 'e':
559       return 033;
560     case 'f':
561       return '\f';
562     case 'n':
563       return '\n';
564     case 'r':
565       return '\r';
566     case 't':
567       return '\t';
568     case 'v':
569       return '\v';
570     case '\n':
571       return -2;
572     case 0:
573       (*string_ptr)--;
574       return 0;
575     case '^':
576       c = *(*string_ptr)++;
577       if (c == '\\')
578         c = parse_escape (string_ptr);
579       if (c == '?')
580         return 0177;
581       return (c & 0200) | (c & 037);
582       
583     case '0':
584     case '1':
585     case '2':
586     case '3':
587     case '4':
588     case '5':
589     case '6':
590     case '7':
591       {
592         register int i = c - '0';
593         register int count = 0;
594         while (++count < 3)
595           {
596             if ((c = *(*string_ptr)++) >= '0' && c <= '7')
597               {
598                 i *= 8;
599                 i += c - '0';
600               }
601             else
602               {
603                 (*string_ptr)--;
604                 break;
605               }
606           }
607         return i;
608       }
609     default:
610       return c;
611     }
612 }
613 \f
614 /* Print the character CH on STREAM as part of the contents
615    of a literal string whose delimiter is QUOTER.  */
616
617 void
618 printchar (ch, stream, quoter)
619      unsigned char ch;
620      FILE *stream;
621      int quoter;
622 {
623   register int c = ch;
624
625   if (c < 040 || (sevenbit_strings && c >= 0177))
626     switch (c)
627       {
628       case '\n':
629         fputs_filtered ("\\n", stream);
630         break;
631       case '\b':
632         fputs_filtered ("\\b", stream);
633         break;
634       case '\t':
635         fputs_filtered ("\\t", stream);
636         break;
637       case '\f':
638         fputs_filtered ("\\f", stream);
639         break;
640       case '\r':
641         fputs_filtered ("\\r", stream);
642         break;
643       case '\033':
644         fputs_filtered ("\\e", stream);
645         break;
646       case '\007':
647         fputs_filtered ("\\a", stream);
648         break;
649       default:
650         fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
651         break;
652       }
653   else
654     {
655       if (c == '\\' || c == quoter)
656         fputs_filtered ("\\", stream);
657       fprintf_filtered (stream, "%c", c);
658     }
659 }
660 \f
661 /* Number of lines per page or UINT_MAX if paging is disabled.  */
662 static unsigned int lines_per_page;
663 /* Number of chars per line or UNIT_MAX is line folding is disabled.  */
664 static unsigned int chars_per_line;
665 /* Current count of lines printed on this page, chars on this line.  */
666 static unsigned int lines_printed, chars_printed;
667
668 /* Buffer and start column of buffered text, for doing smarter word-
669    wrapping.  When someone calls wrap_here(), we start buffering output
670    that comes through fputs_filtered().  If we see a newline, we just
671    spit it out and forget about the wrap_here().  If we see another
672    wrap_here(), we spit it out and remember the newer one.  If we see
673    the end of the line, we spit out a newline, the indent, and then
674    the buffered output.
675
676    wrap_column is the column number on the screen where wrap_buffer begins.
677      When wrap_column is zero, wrapping is not in effect.
678    wrap_buffer is malloc'd with chars_per_line+2 bytes. 
679      When wrap_buffer[0] is null, the buffer is empty.
680    wrap_pointer points into it at the next character to fill.
681    wrap_indent is the string that should be used as indentation if the
682      wrap occurs.  */
683
684 static char *wrap_buffer, *wrap_pointer, *wrap_indent;
685 static int wrap_column;
686
687 /* Get the number of lines to print with commands like "list".
688    This is based on guessing how many long (i.e. more than chars_per_line
689    characters) lines there will be.  To be completely correct, "list"
690    and friends should be rewritten to count characters and see where
691    things are wrapping, but that would be a fair amount of work.  */
692 int
693 lines_to_list ()
694 {
695   /* RMS didn't like the following algorithm.  Let's set it back to
696      10 and see if anyone else complains.  */
697   /* return lines_per_page == UINT_MAX ? 10 : lines_per_page / 2; */
698   return 10;
699 }
700
701 /* ARGSUSED */
702 static void 
703 set_width_command (args, from_tty, c)
704      char *args;
705      int from_tty;
706      struct cmd_list_element *c;
707 {
708   if (!wrap_buffer)
709     {
710       wrap_buffer = (char *) xmalloc (chars_per_line + 2);
711       wrap_buffer[0] = '\0';
712     }
713   else
714     wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
715   wrap_pointer = wrap_buffer;   /* Start it at the beginning */
716 }
717
718 static void
719 prompt_for_continue ()
720 {
721   char *ignore;
722
723   immediate_quit++;
724   ignore = gdb_readline ("---Type <return> to continue---");
725   if (ignore)
726     free (ignore);
727   chars_printed = lines_printed = 0;
728   immediate_quit--;
729   dont_repeat ();               /* Forget prev cmd -- CR won't repeat it. */
730 }
731
732 /* Reinitialize filter; ie. tell it to reset to original values.  */
733
734 void
735 reinitialize_more_filter ()
736 {
737   lines_printed = 0;
738   chars_printed = 0;
739 }
740
741 /* Indicate that if the next sequence of characters overflows the line,
742    a newline should be inserted here rather than when it hits the end. 
743    If INDENT is nonzero, it is a string to be printed to indent the
744    wrapped part on the next line.  INDENT must remain accessible until
745    the next call to wrap_here() or until a newline is printed through
746    fputs_filtered().
747
748    If the line is already overfull, we immediately print a newline and
749    the indentation, and disable further wrapping.
750
751    INDENT should not contain tabs, as that
752    will mess up the char count on the next line.  FIXME.  */
753
754 void
755 wrap_here(indent)
756   char *indent;
757 {
758   if (wrap_buffer[0])
759     {
760       *wrap_pointer = '\0';
761       fputs (wrap_buffer, stdout);
762     }
763   wrap_pointer = wrap_buffer;
764   wrap_buffer[0] = '\0';
765   if (chars_printed >= chars_per_line)
766     {
767       puts_filtered ("\n");
768       puts_filtered (indent);
769       wrap_column = 0;
770     }
771   else
772     {
773       wrap_column = chars_printed;
774       wrap_indent = indent;
775     }
776 }
777
778 /* Like fputs but pause after every screenful, and can wrap at points
779    other than the final character of a line.
780    Unlike fputs, fputs_filtered does not return a value.
781    It is OK for LINEBUFFER to be NULL, in which case just don't print
782    anything.
783
784    Note that a longjmp to top level may occur in this routine
785    (since prompt_for_continue may do so) so this routine should not be
786    called when cleanups are not in place.  */
787
788 void
789 fputs_filtered (linebuffer, stream)
790      char *linebuffer;
791      FILE *stream;
792 {
793   char *lineptr;
794
795   if (linebuffer == 0)
796     return;
797   
798   /* Don't do any filtering if it is disabled.  */
799   if (stream != stdout
800    || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
801     {
802       fputs (linebuffer, stream);
803       return;
804     }
805
806   /* Go through and output each character.  Show line extension
807      when this is necessary; prompt user for new page when this is
808      necessary.  */
809   
810   lineptr = linebuffer;
811   while (*lineptr)
812     {
813       /* Possible new page.  */
814       if (lines_printed >= lines_per_page - 1)
815         prompt_for_continue ();
816
817       while (*lineptr && *lineptr != '\n')
818         {
819           /* Print a single line.  */
820           if (*lineptr == '\t')
821             {
822               if (wrap_column)
823                 *wrap_pointer++ = '\t';
824               else
825                 putc ('\t', stream);
826               /* Shifting right by 3 produces the number of tab stops
827                  we have already passed, and then adding one and
828                  shifting left 3 advances to the next tab stop.  */
829               chars_printed = ((chars_printed >> 3) + 1) << 3;
830               lineptr++;
831             }
832           else
833             {
834               if (wrap_column)
835                 *wrap_pointer++ = *lineptr;
836               else
837                 putc (*lineptr, stream);
838               chars_printed++;
839               lineptr++;
840             }
841       
842           if (chars_printed >= chars_per_line)
843             {
844               unsigned int save_chars = chars_printed;
845
846               chars_printed = 0;
847               lines_printed++;
848               /* If we aren't actually wrapping, don't output newline --
849                  if chars_per_line is right, we probably just overflowed
850                  anyway; if it's wrong, let us keep going.  */
851               if (wrap_column)
852                 putc ('\n', stream);
853
854               /* Possible new page.  */
855               if (lines_printed >= lines_per_page - 1)
856                 prompt_for_continue ();
857
858               /* Now output indentation and wrapped string */
859               if (wrap_column)
860                 {
861                   if (wrap_indent)
862                     fputs (wrap_indent, stream);
863                   *wrap_pointer = '\0';         /* Null-terminate saved stuff */
864                   fputs (wrap_buffer, stream);  /* and eject it */
865                   /* FIXME, this strlen is what prevents wrap_indent from
866                      containing tabs.  However, if we recurse to print it
867                      and count its chars, we risk trouble if wrap_indent is
868                      longer than (the user settable) chars_per_line. 
869                      Note also that this can set chars_printed > chars_per_line
870                      if we are printing a long string.  */
871                   chars_printed = strlen (wrap_indent)
872                                 + (save_chars - wrap_column);
873                   wrap_pointer = wrap_buffer;   /* Reset buffer */
874                   wrap_buffer[0] = '\0';
875                   wrap_column = 0;              /* And disable fancy wrap */
876                 }
877             }
878         }
879
880       if (*lineptr == '\n')
881         {
882           chars_printed = 0;
883           wrap_here ((char *)0);  /* Spit out chars, cancel further wraps */
884           lines_printed++;
885           putc ('\n', stream);
886           lineptr++;
887         }
888     }
889 }
890
891
892 /* fputs_demangled is a variant of fputs_filtered that
893    demangles g++ names.*/
894
895 void
896 fputs_demangled (linebuffer, stream, arg_mode)
897      char *linebuffer;
898      FILE *stream;
899      int arg_mode;
900 {
901 #ifdef __STDC__
902   extern char *cplus_demangle (const char *, int);
903 #else
904   extern char *cplus_demangle ();
905 #endif
906 #define SYMBOL_MAX 1024
907
908 #define SYMBOL_CHAR(c) (isascii(c) \
909   && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
910
911   char buf[SYMBOL_MAX+1];
912 # define SLOP 5         /* How much room to leave in buf */
913   char *p;
914
915   if (linebuffer == NULL)
916     return;
917
918   /* If user wants to see raw output, no problem.  */
919   if (!demangle) {
920     fputs_filtered (linebuffer, stream);
921     return;
922   }
923
924   p = linebuffer;
925
926   while ( *p != (char) 0 ) {
927     int i = 0;
928
929     /* collect non-interesting characters into buf */
930     while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) {
931       buf[i++] = *p;
932       p++;
933     }
934     if (i > 0) {
935       /* output the non-interesting characters without demangling */
936       buf[i] = (char) 0;
937       fputs_filtered(buf, stream);
938       i = 0;  /* reset buf */
939     }
940
941     /* and now the interesting characters */
942     while (i < SYMBOL_MAX
943      && *p != (char) 0
944      && SYMBOL_CHAR(*p)
945      && i < (int)sizeof(buf) - SLOP) {
946       buf[i++] = *p;
947       p++;
948     }
949     buf[i] = (char) 0;
950     if (i > 0) {
951       char * result;
952       
953       if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
954         fputs_filtered(result, stream);
955         free(result);
956       }
957       else {
958         fputs_filtered(buf, stream);
959       }
960     }
961   }
962 }
963
964 /* Print a variable number of ARGS using format FORMAT.  If this
965    information is going to put the amount written (since the last call
966    to INITIALIZE_MORE_FILTER or the last page break) over the page size,
967    print out a pause message and do a gdb_readline to get the users
968    permision to continue.
969
970    Unlike fprintf, this function does not return a value.
971
972    We implement three variants, vfprintf (takes a vararg list and stream),
973    fprintf (takes a stream to write on), and printf (the usual).
974
975    Note that this routine has a restriction that the length of the
976    final output line must be less than 255 characters *or* it must be
977    less than twice the size of the format string.  This is a very
978    arbitrary restriction, but it is an internal restriction, so I'll
979    put it in.  This means that the %s format specifier is almost
980    useless; unless the caller can GUARANTEE that the string is short
981    enough, fputs_filtered should be used instead.
982
983    Note also that a longjmp to top level may occur in this routine
984    (since prompt_for_continue may do so) so this routine should not be
985    called when cleanups are not in place.  */
986
987 #if !defined(MISSING_VPRINTF) || defined (vsprintf)
988 /* VARARGS */
989 void
990 vfprintf_filtered (stream, format, args)
991      va_list args;
992 #else
993 void fprintf_filtered (stream, format, arg1, arg2, arg3, arg4, arg5, arg6)
994 #endif
995      FILE *stream;
996      char *format;
997 {
998   static char *linebuffer = (char *) 0;
999   static int line_size;
1000   int format_length;
1001
1002   format_length = strlen (format);
1003
1004   /* Allocated linebuffer for the first time.  */
1005   if (!linebuffer)
1006     {
1007       linebuffer = (char *) xmalloc (255);
1008       line_size = 255;
1009     }
1010
1011   /* Reallocate buffer to a larger size if this is necessary.  */
1012   if (format_length * 2 > line_size)
1013     {
1014       line_size = format_length * 2;
1015
1016       /* You don't have to copy.  */
1017       free (linebuffer);
1018       linebuffer = (char *) xmalloc (line_size);
1019     }
1020
1021
1022   /* This won't blow up if the restrictions described above are
1023      followed.   */
1024 #if !defined(MISSING_VPRINTF) || defined (vsprintf)
1025   (void) vsprintf (linebuffer, format, args);
1026 #else
1027   (void) sprintf (linebuffer, format, arg1, arg2, arg3, arg4, arg5, arg6);
1028 #endif
1029
1030   fputs_filtered (linebuffer, stream);
1031 }
1032
1033 #if !defined(MISSING_VPRINTF) || defined (vsprintf)
1034 /* VARARGS */
1035 void
1036 fprintf_filtered (va_alist)
1037      va_dcl
1038 {
1039   va_list args;
1040   FILE *stream;
1041   char *format;
1042
1043   va_start (args);
1044   stream = va_arg (args, FILE *);
1045   format = va_arg (args, char *);
1046
1047   /* This won't blow up if the restrictions described above are
1048      followed.   */
1049   (void) vfprintf_filtered (stream, format, args);
1050   va_end (args);
1051 }
1052
1053 /* VARARGS */
1054 void
1055 printf_filtered (va_alist)
1056      va_dcl
1057 {
1058   va_list args;
1059   char *format;
1060
1061   va_start (args);
1062   format = va_arg (args, char *);
1063
1064   (void) vfprintf_filtered (stdout, format, args);
1065   va_end (args);
1066 }
1067 #else
1068 void
1069 printf_filtered (format, arg1, arg2, arg3, arg4, arg5, arg6)
1070      char *format;
1071      int arg1, arg2, arg3, arg4, arg5, arg6;
1072 {
1073   fprintf_filtered (stdout, format, arg1, arg2, arg3, arg4, arg5, arg6);
1074 }
1075 #endif
1076
1077 /* Easy */
1078
1079 void
1080 puts_filtered (string)
1081      char *string;
1082 {
1083   fputs_filtered (string, stdout);
1084 }
1085
1086 /* Return a pointer to N spaces and a null.  The pointer is good
1087    until the next call to here.  */
1088 char *
1089 n_spaces (n)
1090      int n;
1091 {
1092   register char *t;
1093   static char *spaces;
1094   static int max_spaces;
1095
1096   if (n > max_spaces)
1097     {
1098       if (spaces)
1099         free (spaces);
1100       spaces = malloc (n+1);
1101       for (t = spaces+n; t != spaces;)
1102         *--t = ' ';
1103       spaces[n] = '\0';
1104       max_spaces = n;
1105     }
1106
1107   return spaces + max_spaces - n;
1108 }
1109
1110 /* Print N spaces.  */
1111 void
1112 print_spaces_filtered (n, stream)
1113      int n;
1114      FILE *stream;
1115 {
1116   fputs_filtered (n_spaces (n), stream);
1117 }
1118 \f
1119 /* C++ demangler stuff.  */
1120 char *cplus_demangle ();
1121
1122 /* Print NAME on STREAM, demangling if necessary.  */
1123 void
1124 fprint_symbol (stream, name)
1125      FILE *stream;
1126      char *name;
1127 {
1128   char *demangled;
1129   if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1)))
1130     fputs_filtered (name, stream);
1131   else
1132     {
1133       fputs_filtered (demangled, stream);
1134       free (demangled);
1135     }
1136 }
1137 \f
1138 #if !defined (USG_UTILS)
1139 #define USG_UTILS defined (USG)
1140 #endif
1141
1142 #if USG_UTILS
1143 bcopy (from, to, count)
1144 char *from, *to;
1145 {
1146         memcpy (to, from, count);
1147 }
1148
1149 bcmp (from, to, count)
1150 {
1151         return (memcmp (to, from, count));
1152 }
1153
1154 bzero (to, count)
1155 char *to;
1156 {
1157         while (count--)
1158                 *to++ = 0;
1159 }
1160
1161 getwd (buf)
1162 char *buf;
1163 {
1164   getcwd (buf, MAXPATHLEN);
1165 }
1166
1167 char *
1168 index (s, c)
1169      char *s;
1170 {
1171   char *strchr ();
1172   return strchr (s, c);
1173 }
1174
1175 char *
1176 rindex (s, c)
1177      char *s;
1178 {
1179   char *strrchr ();
1180   return strrchr (s, c);
1181 }
1182 #endif /* USG_UTILS.  */
1183
1184 #if !defined (QUEUE_MISSING)
1185 #define QUEUE_MISSING defined (USG)
1186 #endif
1187
1188 #if QUEUE_MISSING
1189 /* Queue routines */
1190
1191 struct queue {
1192         struct queue *forw;
1193         struct queue *back;
1194 };
1195
1196 insque (item, after)
1197 struct queue *item;
1198 struct queue *after;
1199 {
1200         item->forw = after->forw;
1201         after->forw->back = item;
1202
1203         item->back = after;
1204         after->forw = item;
1205 }
1206
1207 remque (item)
1208 struct queue *item;
1209 {
1210         item->forw->back = item->back;
1211         item->back->forw = item->forw;
1212 }
1213 #endif /* QUEUE_MISSING */
1214 \f
1215 #ifndef HAVE_STRSTR
1216 /* Simple implementation of strstr, since some implementations lack it. */
1217 const char *
1218 strstr (in, find)
1219      const char *in, *find;
1220 {
1221   register const char *p = in - 1;
1222
1223   while (0 != (p = strchr (p+1, *find))) {
1224     if (strcmp (p, find))
1225       return p;
1226   }
1227   return 0;
1228 }
1229 #endif /* do not HAVE_STRSTR */
1230 \f
1231 void
1232 _initialize_utils ()
1233 {
1234   struct cmd_list_element *c;
1235
1236   c = add_set_cmd ("width", class_support, var_uinteger, 
1237                   (char *)&chars_per_line,
1238                   "Set number of characters gdb thinks are in a line.",
1239                   &setlist);
1240   add_show_from_set (c, &showlist);
1241   c->function = set_width_command;
1242
1243   add_show_from_set
1244     (add_set_cmd ("height", class_support,
1245                   var_uinteger, (char *)&lines_per_page,
1246                   "Set number of lines gdb thinks are in a page.", &setlist),
1247      &showlist);
1248   
1249   /* These defaults will be used if we are unable to get the correct
1250      values from termcap.  */
1251   lines_per_page = 24;
1252   chars_per_line = 80;
1253   /* Initialize the screen height and width from termcap.  */
1254   {
1255     char *termtype = getenv ("TERM");
1256
1257     /* Positive means success, nonpositive means failure.  */
1258     int status;
1259
1260     /* 2048 is large enough for all known terminals, according to the
1261        GNU termcap manual.  */
1262     char term_buffer[2048];
1263
1264     if (termtype)
1265       {
1266         status = tgetent (term_buffer, termtype);
1267         if (status > 0)
1268           {
1269             int val;
1270             
1271             val = tgetnum ("li");
1272             if (val >= 0)
1273               lines_per_page = val;
1274             else
1275               /* The number of lines per page is not mentioned
1276                  in the terminal description.  This probably means
1277                  that paging is not useful (e.g. emacs shell window),
1278                  so disable paging.  */
1279               lines_per_page = UINT_MAX;
1280             
1281             val = tgetnum ("co");
1282             if (val >= 0)
1283               chars_per_line = val;
1284           }
1285       }
1286   }
1287
1288   set_width_command ((char *)NULL, 0, c);
1289
1290   add_show_from_set
1291     (add_set_cmd ("demangle", class_support, var_boolean, 
1292                   (char *)&demangle,
1293                 "Set demangling of encoded C++ names when displaying symbols.",
1294                   &setprintlist),
1295      &showprintlist);
1296
1297   add_show_from_set
1298     (add_set_cmd ("sevenbit-strings", class_support, var_boolean, 
1299                   (char *)&sevenbit_strings,
1300    "Set printing of 8-bit characters in strings as \\nnn.",
1301                   &setprintlist),
1302      &showprintlist);
1303
1304   add_show_from_set
1305     (add_set_cmd ("asm-demangle", class_support, var_boolean, 
1306                   (char *)&asm_demangle,
1307         "Set demangling of C++ names in disassembly listings.",
1308                   &setprintlist),
1309      &showprintlist);
1310 }
This page took 0.095877 seconds and 4 git commands to generate.