* Sync comment with code's reality.
[dragonfly.git] / contrib / gdb / gdb / valprint.c
1 /* Print values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1998
3              Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "gdbcmd.h"
28 #include "target.h"
29 #include "obstack.h"
30 #include "language.h"
31 #include "demangle.h"
32 #include "annotate.h"
33 #include "valprint.h"
34
35 #include <errno.h>
36
37 /* Prototypes for local functions */
38
39 static void print_hex_chars PARAMS ((GDB_FILE *, unsigned char *, 
40                                      unsigned int));
41
42 static void show_print PARAMS ((char *, int));
43
44 static void set_print PARAMS ((char *, int));
45
46 static void set_radix PARAMS ((char *, int));
47
48 static void show_radix PARAMS ((char *, int));
49
50 static void set_input_radix PARAMS ((char *, int, struct cmd_list_element *));
51
52 static void set_input_radix_1 PARAMS ((int, unsigned));
53
54 static void set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
55
56 static void set_output_radix_1 PARAMS ((int, unsigned));
57
58 void _initialize_valprint PARAMS ((void));
59
60 /* Maximum number of chars to print for a string pointer value or vector
61    contents, or UINT_MAX for no limit.  Note that "set print elements 0"
62    stores UINT_MAX in print_max, which displays in a show command as
63    "unlimited". */
64
65 unsigned int print_max;
66 #define PRINT_MAX_DEFAULT 200   /* Start print_max off at this value. */
67
68 /* Default input and output radixes, and output format letter.  */
69
70 unsigned input_radix = 10;
71 unsigned output_radix = 10;
72 int output_format = 0;
73
74 /* Print repeat counts if there are more than this many repetitions of an
75    element in an array.  Referenced by the low level language dependent
76    print routines. */
77
78 unsigned int repeat_count_threshold = 10;
79
80 /* If nonzero, stops printing of char arrays at first null. */
81
82 int stop_print_at_null;
83
84 /* Controls pretty printing of structures. */
85
86 int prettyprint_structs;
87
88 /* Controls pretty printing of arrays.  */
89
90 int prettyprint_arrays;
91
92 /* If nonzero, causes unions inside structures or other unions to be
93    printed. */
94
95 int unionprint;                 /* Controls printing of nested unions.  */
96
97 /* If nonzero, causes machine addresses to be printed in certain contexts. */
98
99 int addressprint;               /* Controls printing of machine addresses */
100
101 \f
102 /* Print data of type TYPE located at VALADDR (within GDB), which came from
103    the inferior at address ADDRESS, onto stdio stream STREAM according to
104    FORMAT (a letter, or 0 for natural format using TYPE).
105
106    If DEREF_REF is nonzero, then dereference references, otherwise just print
107    them like pointers.
108
109    The PRETTY parameter controls prettyprinting.
110
111    If the data are a string pointer, returns the number of string characters
112    printed.
113
114    FIXME:  The data at VALADDR is in target byte order.  If gdb is ever
115    enhanced to be able to debug more than the single target it was compiled
116    for (specific CPU type and thus specific target byte ordering), then
117    either the print routines are going to have to take this into account,
118    or the data is going to have to be passed into here already converted
119    to the host byte ordering, whichever is more convenient. */
120
121
122 int
123 val_print (type, valaddr, embedded_offset, address,
124            stream, format, deref_ref, recurse, pretty)
125      struct type *type;
126      char *valaddr;
127      int embedded_offset;
128      CORE_ADDR address;
129      GDB_FILE *stream;
130      int format;
131      int deref_ref;
132      int recurse;
133      enum val_prettyprint pretty;
134 {
135   struct type *real_type = check_typedef (type);
136   if (pretty == Val_pretty_default)
137     {
138       pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
139     }
140   
141   QUIT;
142
143   /* Ensure that the type is complete and not just a stub.  If the type is
144      only a stub and we can't find and substitute its complete type, then
145      print appropriate string and return.  */
146
147   if (TYPE_FLAGS (real_type) & TYPE_FLAG_STUB)
148     {
149       fprintf_filtered (stream, "<incomplete type>");
150       gdb_flush (stream);
151       return (0);
152     }
153   
154   return (LA_VAL_PRINT (type, valaddr, embedded_offset, address,
155                         stream, format, deref_ref, recurse, pretty));
156 }
157
158 /* Print the value VAL in C-ish syntax on stream STREAM.
159    FORMAT is a format-letter, or 0 for print in natural format of data type.
160    If the object printed is a string pointer, returns
161    the number of string bytes printed.  */
162
163 int
164 value_print (val, stream, format, pretty)
165      value_ptr val;
166      GDB_FILE *stream;
167      int format;
168      enum val_prettyprint pretty;
169 {
170   if (val == 0)
171     {
172       printf_filtered ("<address of value unknown>");
173       return 0;
174     }
175   if (VALUE_OPTIMIZED_OUT (val))
176     {
177       printf_filtered ("<value optimized out>");
178       return 0;
179     }
180   return LA_VALUE_PRINT (val, stream, format, pretty);
181 }
182
183 /* Called by various <lang>_val_print routines to print
184    TYPE_CODE_INT's.  TYPE is the type.  VALADDR is the address of the
185    value.  STREAM is where to print the value.  */
186
187 void
188 val_print_type_code_int (type, valaddr, stream)
189      struct type *type;
190      char *valaddr;
191      GDB_FILE *stream;
192 {
193   if (TYPE_LENGTH (type) > sizeof (LONGEST))
194     {
195       LONGEST val;
196
197       if (TYPE_UNSIGNED (type)
198           && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
199                                             &val))
200         {
201           print_longest (stream, 'u', 0, val);
202         }
203       else
204         {
205           /* Signed, or we couldn't turn an unsigned value into a
206              LONGEST.  For signed values, one could assume two's
207              complement (a reasonable assumption, I think) and do
208              better than this.  */
209           print_hex_chars (stream, (unsigned char *) valaddr,
210                            TYPE_LENGTH (type));
211         }
212     }
213   else
214     {
215 #ifdef PRINT_TYPELESS_INTEGER
216       PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
217 #else
218       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
219                      unpack_long (type, valaddr));
220 #endif
221     }
222 }
223
224 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
225    The raison d'etre of this function is to consolidate printing of 
226    LONG_LONG's into this one function.  Some platforms have long longs but
227    don't have a printf() that supports "ll" in the format string.  We handle
228    these by seeing if the number is representable as either a signed or
229    unsigned long, depending upon what format is desired, and if not we just
230    bail out and print the number in hex.
231
232    The format chars b,h,w,g are from print_scalar_formatted().  If USE_LOCAL,
233    format it according to the current language (this should be used for most
234    integers which GDB prints, the exception is things like protocols where
235    the format of the integer is a protocol thing, not a user-visible thing).
236    */
237
238 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
239 static void
240 print_decimal (stream, sign, use_local, val_ulong)
241      GDB_FILE *stream;
242      char *sign;
243      int use_local;
244      ULONGEST val_ulong;
245 {
246   unsigned long temp[3];
247   int i = 0;
248   do
249     {
250       temp[i] = val_ulong % (1000 * 1000 * 1000);
251       val_ulong /= (1000 * 1000 * 1000);
252       i++;
253     }
254   while (val_ulong != 0 && i < (sizeof (temp) / sizeof (temp[0])));
255   switch (i)
256     {
257     case 1:
258       fprintf_filtered (stream, "%s%lu",
259                         sign, temp[0]);
260       break;
261     case 2:
262       fprintf_filtered (stream, "%s%lu%09lu",
263                         sign, temp[1], temp[0]);
264       break;
265     case 3:
266       fprintf_filtered (stream, "%s%lu%09lu%09lu",
267                         sign, temp[2], temp[1], temp[0]);
268       break;
269     default:
270       abort ();
271     }
272   return;
273 }
274 #endif
275
276 void
277 print_longest (stream, format, use_local, val_long)
278      GDB_FILE *stream;
279      int format;
280      int use_local;
281      LONGEST val_long;
282 {
283 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
284   if (sizeof (long) < sizeof (LONGEST))
285     {
286       switch (format)
287         {
288         case 'd':
289           {
290             /* Print a signed value, that doesn't fit in a long */
291             if ((long) val_long != val_long)
292               {
293                 if (val_long < 0)
294                   print_decimal (stream, "-", use_local, -val_long);
295                 else
296                   print_decimal (stream, "", use_local, val_long);
297                 return;
298               }
299             break;
300           }
301         case 'u':
302           {
303             /* Print an unsigned value, that doesn't fit in a long */
304             if ((unsigned long) val_long != (ULONGEST) val_long)
305               {
306                 print_decimal (stream, "", use_local, val_long);
307                 return;
308               }
309             break;
310           }
311         case 'x':
312         case 'o':
313         case 'b':
314         case 'h':
315         case 'w':
316         case 'g':
317           /* Print as unsigned value, must fit completely in unsigned long */
318           {
319             unsigned long temp = val_long;
320             if (temp != val_long)
321               {
322                 /* Urk, can't represent value in long so print in hex.
323                    Do shift in two operations so that if sizeof (long)
324                    == sizeof (LONGEST) we can avoid warnings from
325                    picky compilers about shifts >= the size of the
326                    shiftee in bits */
327                 unsigned long vbot = (unsigned long) val_long;
328                 LONGEST temp = (val_long >> (sizeof (long) * HOST_CHAR_BIT - 1));
329                 unsigned long vtop = temp >> 1;
330                 fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
331                 return;
332               }
333             break;
334           }
335         }
336     }
337 #endif
338
339 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
340   switch (format)
341     {
342     case 'd':
343       fprintf_filtered (stream,
344                         use_local ? local_decimal_format_custom ("ll")
345                                   : "%lld",
346                         val_long);
347       break;
348     case 'u':
349       fprintf_filtered (stream, "%llu", val_long);
350       break;
351     case 'x':
352       fprintf_filtered (stream,
353                         use_local ? local_hex_format_custom ("ll")
354                                   : "%llx",
355                         val_long);
356       break;
357     case 'o':
358       fprintf_filtered (stream,
359                         use_local ? local_octal_format_custom ("ll")
360                                   : "%llo",
361                         val_long);
362       break;
363     case 'b':
364       fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
365       break;
366     case 'h':
367       fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
368       break;
369     case 'w':
370       fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
371       break;
372     case 'g':
373       fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
374       break;
375     default:
376       abort ();
377     }
378 #else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG*/
379   /* In the following it is important to coerce (val_long) to a long. It does
380      nothing if !LONG_LONG, but it will chop off the top half (which we know
381      we can ignore) if the host supports long longs.  */
382
383   switch (format)
384     {
385     case 'd':
386       fprintf_filtered (stream,
387                         use_local ? local_decimal_format_custom ("l")
388                                   : "%ld",
389                         (long) val_long);
390       break;
391     case 'u':
392       fprintf_filtered (stream, "%lu", (unsigned long) val_long);
393       break;
394     case 'x':
395       fprintf_filtered (stream,
396                         use_local ? local_hex_format_custom ("l")
397                                   : "%lx",
398                         (unsigned long) val_long);
399       break;
400     case 'o':
401       fprintf_filtered (stream,
402                         use_local ? local_octal_format_custom ("l")
403                                   : "%lo",
404                         (unsigned long) val_long);
405       break;
406     case 'b':
407       fprintf_filtered (stream, local_hex_format_custom ("02l"),
408                         (unsigned long) val_long);
409       break;
410     case 'h':
411       fprintf_filtered (stream, local_hex_format_custom ("04l"),
412                         (unsigned long) val_long);
413       break;
414     case 'w':
415       fprintf_filtered (stream, local_hex_format_custom ("08l"),
416                         (unsigned long) val_long);
417       break;
418     case 'g':
419       fprintf_filtered (stream, local_hex_format_custom ("016l"),
420                         (unsigned long) val_long);
421       break;
422     default:
423       abort ();
424     }
425 #endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
426 }
427
428 void
429 strcat_longest (format, use_local, val_long, buf, buflen)
430      int format;
431      int use_local;
432      LONGEST val_long;
433      char *buf;
434      int buflen;                /* ignored, for now */
435 {
436 #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
437   long vtop, vbot;
438
439   vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
440   vbot = (long) val_long;
441
442   if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
443       || ((format == 'u' || format == 'x') && (unsigned long long)val_long > UINT_MAX))
444     {
445       sprintf (buf, "0x%lx%08lx", vtop, vbot);
446       return;
447     }
448 #endif
449
450 #ifdef PRINTF_HAS_LONG_LONG
451   switch (format)
452     {
453     case 'd':
454       sprintf (buf,
455                (use_local ? local_decimal_format_custom ("ll") : "%lld"), 
456                val_long);
457       break;
458     case 'u':
459       sprintf (buf, "%llu",  val_long);
460       break;
461     case 'x':
462       sprintf (buf,
463                (use_local ? local_hex_format_custom ("ll") : "%llx"), 
464                         
465                val_long);
466       break;
467     case 'o':
468       sprintf (buf,
469                (use_local ? local_octal_format_custom ("ll") : "%llo"), 
470                val_long);
471       break;
472     case 'b':
473       sprintf (buf, local_hex_format_custom ("02ll"),  val_long);
474       break;
475     case 'h':
476       sprintf (buf, local_hex_format_custom ("04ll"),  val_long);
477       break;
478     case 'w':
479       sprintf (buf, local_hex_format_custom ("08ll"),  val_long);
480       break;
481     case 'g':
482       sprintf (buf, local_hex_format_custom ("016ll"),  val_long);
483       break;
484     default:
485       abort ();
486     }
487 #else /* !PRINTF_HAS_LONG_LONG */
488   /* In the following it is important to coerce (val_long) to a long. It does
489      nothing if !LONG_LONG, but it will chop off the top half (which we know
490      we can ignore) if the host supports long longs.  */
491
492   switch (format)
493     {
494     case 'd':
495       sprintf (buf, (use_local ? local_decimal_format_custom ("l") : "%ld"), 
496                                                                  ((long) val_long));
497       break;
498     case 'u':
499       sprintf (buf, "%lu",  ((unsigned long) val_long));
500       break;
501     case 'x':
502       sprintf (buf, (use_local ? local_hex_format_custom ("l") : "%lx"), 
503                ((long) val_long));
504       break;
505     case 'o':
506       sprintf (buf, (use_local ? local_octal_format_custom ("l") : "%lo"), 
507                ((long) val_long));
508       break;
509     case 'b':
510       sprintf (buf, local_hex_format_custom ("02l"), 
511                ((long) val_long));
512       break;
513     case 'h':
514       sprintf (buf, local_hex_format_custom ("04l"), 
515                ((long) val_long));
516       break;
517     case 'w':
518       sprintf (buf, local_hex_format_custom ("08l"), 
519                ((long) val_long));
520       break;
521     case 'g':
522       sprintf (buf, local_hex_format_custom ("016l"),
523                ((long) val_long));
524       break;
525     default:
526       abort ();
527     }
528     
529 #endif /* !PRINTF_HAS_LONG_LONG */
530 }
531
532 /* This used to be a macro, but I don't think it is called often enough
533    to merit such treatment.  */
534 /* Convert a LONGEST to an int.  This is used in contexts (e.g. number of
535    arguments to a function, number in a value history, register number, etc.)
536    where the value must not be larger than can fit in an int.  */
537
538 int
539 longest_to_int (arg)
540      LONGEST arg;
541 {
542   /* Let the compiler do the work */
543   int rtnval = (int) arg;
544
545   /* Check for overflows or underflows */
546   if (sizeof (LONGEST) > sizeof (int))
547     {
548       if (rtnval != arg)
549         {
550           error ("Value out of range.");
551         }
552     }
553   return (rtnval);
554 }
555
556 /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
557    on STREAM.  */
558
559 void
560 print_floating (valaddr, type, stream)
561      char *valaddr;
562      struct type *type;
563      GDB_FILE *stream;
564 {
565   DOUBLEST doub;
566   int inv;
567   unsigned len = TYPE_LENGTH (type);
568   
569 #if defined (IEEE_FLOAT)
570
571   /* Check for NaN's.  Note that this code does not depend on us being
572      on an IEEE conforming system.  It only depends on the target
573      machine using IEEE representation.  This means (a)
574      cross-debugging works right, and (2) IEEE_FLOAT can (and should)
575      be defined for systems like the 68881, which uses IEEE
576      representation, but is not IEEE conforming.  */
577
578   {
579     unsigned long low, high;
580     /* Is the sign bit 0?  */
581     int nonnegative;
582     /* Is it is a NaN (i.e. the exponent is all ones and
583        the fraction is nonzero)?  */
584     int is_nan;
585
586     /* For lint, initialize these two variables to suppress warning: */
587     low = high = nonnegative = 0;
588     if (len == 4)
589       {
590         /* It's single precision.  */
591         /* Assume that floating point byte order is the same as
592            integer byte order.  */
593         low = extract_unsigned_integer (valaddr, 4);
594         nonnegative = ((low & 0x80000000) == 0);
595         is_nan = ((((low >> 23) & 0xFF) == 0xFF) 
596                   && 0 != (low & 0x7FFFFF));
597         low &= 0x7fffff;
598         high = 0;
599       }
600     else if (len == 8)
601       {
602         /* It's double precision.  Get the high and low words.  */
603
604         /* Assume that floating point byte order is the same as
605            integer byte order.  */
606         if (TARGET_BYTE_ORDER == BIG_ENDIAN)
607           {
608             low = extract_unsigned_integer (valaddr + 4, 4);
609             high = extract_unsigned_integer (valaddr, 4);
610           }
611         else
612           {
613             low = extract_unsigned_integer (valaddr, 4);
614             high = extract_unsigned_integer (valaddr + 4, 4);
615           }
616         nonnegative = ((high & 0x80000000) == 0);
617         is_nan = (((high >> 20) & 0x7ff) == 0x7ff
618                   && ! ((((high & 0xfffff) == 0)) && (low == 0)));
619         high &= 0xfffff;
620       }
621     else
622       /* Extended.  We can't detect NaNs for extendeds yet.  Also note
623          that currently extendeds get nuked to double in
624          REGISTER_CONVERTIBLE.  */
625       is_nan = 0;
626
627     if (is_nan)
628       {
629         /* The meaning of the sign and fraction is not defined by IEEE.
630            But the user might know what they mean.  For example, they
631            (in an implementation-defined manner) distinguish between
632            signaling and quiet NaN's.  */
633         if (high)
634           fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
635                             high, low);
636         else
637           fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
638         return;
639       }
640   }
641 #endif /* IEEE_FLOAT.  */
642
643   doub = unpack_double (type, valaddr, &inv);
644   if (inv)
645     {
646       fprintf_filtered (stream, "<invalid float value>");
647       return;
648     }
649
650   if (len < sizeof (double))
651     fprintf_filtered (stream, "%.9g", (double) doub);
652   else if (len == sizeof (double))
653     fprintf_filtered (stream, "%.17g", (double) doub);
654   else
655 #ifdef PRINTF_HAS_LONG_DOUBLE
656     fprintf_filtered (stream, "%.35Lg", doub);
657 #else
658     /* This at least wins with values that are representable as doubles */
659     fprintf_filtered (stream, "%.17g", (double) doub);
660 #endif
661 }
662
663 void 
664 print_binary_chars (stream, valaddr, len)
665      GDB_FILE *stream;
666      unsigned char *valaddr;
667      unsigned len;
668 {
669
670 #define BITS_IN_BYTES 8
671
672   unsigned char *p;
673   int            i;
674   int            b;
675
676   /* Declared "int" so it will be signed.
677    * This ensures that right shift will shift in zeros.
678    */
679   const int      mask = 0x080;
680
681   /* FIXME: We should be not printing leading zeroes in most cases.  */
682
683   fprintf_filtered (stream, local_binary_format_prefix ());
684   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
685     {
686       for (p = valaddr;
687            p < valaddr + len;
688            p++)
689         {
690           /* Every byte has 8 binary characters; peel off
691            * and print from the MSB end.
692            */
693           for( i = 0; i < (BITS_IN_BYTES * sizeof( *p )); i++ ) {
694               if( *p & ( mask >> i ))
695                   b = 1;
696               else
697                   b = 0;
698
699               fprintf_filtered (stream, "%1d", b);
700           }
701         }
702     }
703   else
704     {
705       for (p = valaddr + len - 1;
706            p >= valaddr;
707            p--)
708         {
709           for( i = 0; i < (BITS_IN_BYTES * sizeof( *p )); i++ ) {
710               if( *p & ( mask >> i ))
711                   b = 1;
712               else
713                   b = 0;
714
715               fprintf_filtered (stream, "%1d", b);
716           }
717         }
718     }
719   fprintf_filtered (stream, local_binary_format_suffix ());
720 }
721
722 /* VALADDR points to an integer of LEN bytes.
723  * Print it in octal on stream or format it in buf.
724  */
725 void
726 print_octal_chars (stream, valaddr, len)
727      GDB_FILE *stream;
728      unsigned char *valaddr;
729      unsigned len;
730 {
731   unsigned char *p;
732   unsigned char octa1, octa2, octa3, carry;
733   int           cycle;
734   
735   /* FIXME: We should be not printing leading zeroes in most cases.  */
736
737
738   /* Octal is 3 bits, which doesn't fit.  Yuk.  So we have to track
739    * the extra bits, which cycle every three bytes:
740    *
741    * Byte side:       0            1             2          3
742    *                         |             |            |            |
743    * bit number   123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
744    *
745    * Octal side:   0   1   carry  3   4  carry ...
746    *
747    * Cycle number:    0             1            2
748    *
749    * But of course we are printing from the high side, so we have to
750    * figure out where in the cycle we are so that we end up with no
751    * left over bits at the end.
752    */
753 #define BITS_IN_OCTAL 3
754 #define HIGH_ZERO     0340
755 #define LOW_ZERO      0016
756 #define CARRY_ZERO    0003
757 #define HIGH_ONE      0200
758 #define MID_ONE       0160
759 #define LOW_ONE       0016
760 #define CARRY_ONE     0001
761 #define HIGH_TWO      0300
762 #define MID_TWO       0070
763 #define LOW_TWO       0007
764
765   /* For 32 we start in cycle 2, with two bits and one bit carry;
766    * for 64 in cycle in cycle 1, with one bit and a two bit carry.
767    */
768   cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
769   carry = 0;
770   
771   fprintf_filtered (stream, local_octal_format_prefix ());
772   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
773     {
774       for (p = valaddr;
775            p < valaddr + len;
776            p++)
777         {
778           switch (cycle) {
779               case 0:
780                  /* No carry in, carry out two bits.
781                   */
782                  octa1 = (HIGH_ZERO  & *p) >> 5;
783                  octa2 = (LOW_ZERO   & *p) >> 2;
784                  carry = (CARRY_ZERO & *p);
785                  fprintf_filtered (stream, "%o", octa1);
786                  fprintf_filtered (stream, "%o", octa2);
787                  break;
788
789               case 1:
790                  /* Carry in two bits, carry out one bit.
791                   */
792                  octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
793                  octa2 = (MID_ONE   & *p) >> 4;
794                  octa3 = (LOW_ONE   & *p) >> 1;
795                  carry = (CARRY_ONE & *p);
796                  fprintf_filtered (stream, "%o", octa1);
797                  fprintf_filtered (stream, "%o", octa2);
798                  fprintf_filtered (stream, "%o", octa3);
799                  break;
800
801               case 2:
802                  /* Carry in one bit, no carry out.
803                   */
804                  octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
805                  octa2 = (MID_TWO & *p) >> 3;
806                  octa3 = (LOW_TWO & *p);
807                  carry = 0;
808                  fprintf_filtered (stream, "%o", octa1);
809                  fprintf_filtered (stream, "%o", octa2);
810                  fprintf_filtered (stream, "%o", octa3);
811                  break;
812                  
813               default:
814                  error( "Internal error in octal conversion;" );
815           }
816
817           cycle++;
818           cycle = cycle % BITS_IN_OCTAL;
819         }
820     }
821   else
822     {
823       for (p = valaddr + len - 1;
824            p >= valaddr;
825            p--)
826         {
827           switch (cycle) {
828               case 0:
829                  /* Carry out, no carry in */
830                  octa1 = (HIGH_ZERO  & *p) >> 5;
831                  octa2 = (LOW_ZERO   & *p) >> 2;
832                  carry = (CARRY_ZERO & *p);
833                  fprintf_filtered (stream, "%o", octa1);
834                  fprintf_filtered (stream, "%o", octa2);
835                  break;
836
837               case 1:
838                  /* Carry in, carry out */
839                  octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
840                  octa2 = (MID_ONE   & *p) >> 4;
841                  octa3 = (LOW_ONE   & *p) >> 1;
842                  carry = (CARRY_ONE & *p);
843                  fprintf_filtered (stream, "%o", octa1);
844                  fprintf_filtered (stream, "%o", octa2);
845                  fprintf_filtered (stream, "%o", octa3);
846                  break;
847
848               case 2:
849                  /* Carry in, no carry out */
850                  octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
851                  octa2 = (MID_TWO & *p) >> 3;
852                  octa3 = (LOW_TWO & *p);
853                  carry = 0;
854                  fprintf_filtered (stream, "%o", octa1);
855                  fprintf_filtered (stream, "%o", octa2);
856                  fprintf_filtered (stream, "%o", octa3);
857                  break;
858                  
859               default:
860                  error( "Internal error in octal conversion;" );
861           }
862
863           cycle++;
864           cycle = cycle % BITS_IN_OCTAL;
865         }
866     }
867
868   fprintf_filtered (stream, local_octal_format_suffix ());
869 }
870
871 /* VALADDR points to an integer of LEN bytes.
872  * Print it in decimal on stream or format it in buf.
873  */
874 void
875 print_decimal_chars (stream, valaddr, len)
876      GDB_FILE *stream;
877      unsigned char *valaddr;
878      unsigned len;
879 {
880 #define TEN             10
881 #define TWO_TO_FOURTH   16
882 #define CARRY_OUT(  x ) ((x) / TEN)  /* extend char to int */
883 #define CARRY_LEFT( x ) ((x) % TEN)
884 #define SHIFT( x )      ((x) << 4)
885 #define START_P \
886         ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? valaddr : valaddr + len - 1)
887 #define NOT_END_P \
888         ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? (p < valaddr + len) : (p >= valaddr))
889 #define NEXT_P \
890         ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? p++ : p-- )
891 #define LOW_NIBBLE(  x ) ( (x) & 0x00F)
892 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
893
894   unsigned char *p;
895   unsigned char *digits;
896   int            carry;
897   int            decimal_len;
898   int            i, j, decimal_digits;
899   int            dummy;
900   int            flip;
901   
902   /* Base-ten number is less than twice as many digits
903    * as the base 16 number, which is 2 digits per byte.
904    */
905   decimal_len = len * 2 * 2;
906   digits = (unsigned char *) malloc( decimal_len );
907   if( digits == NULL )
908       error( "Can't allocate memory for conversion to decimal." );
909
910   for( i = 0; i < decimal_len; i++ ) {
911       digits[i] = 0;
912   }
913
914   fprintf_filtered (stream, local_decimal_format_prefix ());
915
916   /* Ok, we have an unknown number of bytes of data to be printed in
917    * decimal.
918    *
919    * Given a hex number (in nibbles) as XYZ, we start by taking X and
920    * decemalizing it as "x1 x2" in two decimal nibbles.  Then we multiply
921    * the nibbles by 16, add Y and re-decimalize.  Repeat with Z.
922    *
923    * The trick is that "digits" holds a base-10 number, but sometimes
924    * the individual digits are > 10. 
925    *
926    * Outer loop is per nibble (hex digit) of input, from MSD end to
927    * LSD end.
928    */
929   decimal_digits = 0;  /* Number of decimal digits so far */
930   p = START_P;
931   flip = 0;
932   while( NOT_END_P ) {
933       /*
934        * Multiply current base-ten number by 16 in place.
935        * Each digit was between 0 and 9, now is between
936        * 0 and 144.
937        */
938       for( j = 0; j < decimal_digits; j++ ) {
939            digits[j] = SHIFT( digits[j] );
940       }
941     
942       /* Take the next nibble off the input and add it to what
943        * we've got in the LSB position.  Bottom 'digit' is now
944        * between 0 and 159.
945        *
946        * "flip" is used to run this loop twice for each byte.
947        */
948       if( flip == 0 ) {
949           /* Take top nibble.
950            */
951           digits[0] += HIGH_NIBBLE( *p );
952           flip = 1;
953       }
954       else {
955           /* Take low nibble and bump our pointer "p".
956            */
957           digits[0] += LOW_NIBBLE( *p );
958           NEXT_P;
959           flip = 0;
960       }
961
962       /* Re-decimalize.  We have to do this often enough
963        * that we don't overflow, but once per nibble is
964        * overkill.  Easier this way, though.  Note that the
965        * carry is often larger than 10 (e.g. max initial
966        * carry out of lowest nibble is 15, could bubble all
967        * the way up greater than 10).  So we have to do
968        * the carrying beyond the last current digit.
969        */
970       carry = 0;
971       for( j = 0; j < decimal_len - 1; j++ ) {
972           digits[j] += carry;
973
974           /* "/" won't handle an unsigned char with
975            * a value that if signed would be negative.
976            * So extend to longword int via "dummy".
977            */
978           dummy     = digits[j];
979           carry     = CARRY_OUT(  dummy );
980           digits[j] = CARRY_LEFT( dummy );
981
982           if( j >= decimal_digits && carry == 0 ) {
983               /*
984                * All higher digits are 0 and we
985                * no longer have a carry.
986                *
987                * Note: "j" is 0-based, "decimal_digits" is
988                *       1-based.
989                */
990               decimal_digits = j + 1;
991               break;
992           }
993       }
994   }
995
996   /* Ok, now "digits" is the decimal representation, with
997    * the "decimal_digits" actual digits.  Print!
998    */
999   for( i = decimal_digits - 1; i >= 0; i-- ) {
1000       fprintf_filtered( stream, "%1d", digits[i] );
1001   }
1002   free( digits );
1003   
1004   fprintf_filtered (stream, local_decimal_format_suffix ());
1005 }
1006
1007 /* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */
1008
1009 static void
1010 print_hex_chars (stream, valaddr, len)
1011      GDB_FILE *stream;
1012      unsigned char *valaddr;
1013      unsigned len;
1014 {
1015   unsigned char *p;
1016
1017   /* FIXME: We should be not printing leading zeroes in most cases.  */
1018
1019   fprintf_filtered (stream, local_hex_format_prefix ());
1020   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1021     {
1022       for (p = valaddr;
1023            p < valaddr + len;
1024            p++)
1025         {
1026           fprintf_filtered (stream, "%02x", *p);
1027         }
1028     }
1029   else
1030     {
1031       for (p = valaddr + len - 1;
1032            p >= valaddr;
1033            p--)
1034         {
1035           fprintf_filtered (stream, "%02x", *p);
1036         }
1037     }
1038   fprintf_filtered (stream, local_hex_format_suffix ());
1039 }
1040
1041 /*  Called by various <lang>_val_print routines to print elements of an
1042     array in the form "<elem1>, <elem2>, <elem3>, ...".
1043
1044     (FIXME?)  Assumes array element separator is a comma, which is correct
1045     for all languages currently handled.
1046     (FIXME?)  Some languages have a notation for repeated array elements,
1047     perhaps we should try to use that notation when appropriate.
1048     */
1049
1050 void
1051 val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
1052                           recurse, pretty, i)
1053      struct type *type;
1054      char *valaddr;
1055      CORE_ADDR address;
1056      GDB_FILE *stream;
1057      int format;
1058      int deref_ref;
1059      int recurse;
1060      enum val_prettyprint pretty;
1061      unsigned int i;
1062 {
1063   unsigned int things_printed = 0;
1064   unsigned len;
1065   struct type *elttype;
1066   unsigned eltlen;
1067   /* Position of the array element we are examining to see
1068      whether it is repeated.  */
1069   unsigned int rep1;
1070   /* Number of repetitions we have detected so far.  */
1071   unsigned int reps;
1072       
1073   elttype = TYPE_TARGET_TYPE (type);
1074   eltlen = TYPE_LENGTH (check_typedef (elttype));
1075   len = TYPE_LENGTH (type) / eltlen;
1076
1077   annotate_array_section_begin (i, elttype);
1078
1079   for (; i < len && things_printed < print_max; i++)
1080     {
1081       if (i != 0)
1082         {
1083           if (prettyprint_arrays)
1084             {
1085               fprintf_filtered (stream, ",\n");
1086               print_spaces_filtered (2 + 2 * recurse, stream);
1087             }
1088           else
1089             {
1090               fprintf_filtered (stream, ", ");
1091             }
1092         }
1093       wrap_here (n_spaces (2 + 2 * recurse));
1094
1095       rep1 = i + 1;
1096       reps = 1;
1097       while ((rep1 < len) && 
1098              !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1099         {
1100           ++reps;
1101           ++rep1;
1102         }
1103
1104       if (reps > repeat_count_threshold)
1105         {
1106           val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1107                      deref_ref, recurse + 1, pretty);
1108           annotate_elt_rep (reps);
1109           fprintf_filtered (stream, " <repeats %u times>", reps);
1110           annotate_elt_rep_end ();
1111
1112           i = rep1 - 1;
1113           things_printed += repeat_count_threshold;
1114         }
1115       else
1116         {
1117           val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1118                      deref_ref, recurse + 1, pretty);
1119           annotate_elt ();
1120           things_printed++;
1121         }
1122     }
1123   annotate_array_section_end ();
1124   if (i < len)
1125     {
1126       fprintf_filtered (stream, "...");
1127     }
1128 }
1129
1130 /*  Print a string from the inferior, starting at ADDR and printing up to LEN
1131     characters, of WIDTH bytes a piece, to STREAM.  If LEN is -1, printing
1132     stops at the first null byte, otherwise printing proceeds (including null
1133     bytes) until either print_max or LEN characters have been printed,
1134     whichever is smaller. */
1135
1136 /* FIXME: Use target_read_string.  */
1137
1138 int
1139 val_print_string (addr, len, width, stream)
1140     CORE_ADDR addr;
1141     int len;
1142     int width;
1143     GDB_FILE *stream;
1144 {
1145   int force_ellipsis = 0;       /* Force ellipsis to be printed if nonzero. */
1146   int errcode;                  /* Errno returned from bad reads. */
1147   unsigned int fetchlimit;      /* Maximum number of chars to print. */
1148   unsigned int nfetch;          /* Chars to fetch / chars fetched. */
1149   unsigned int chunksize;       /* Size of each fetch, in chars. */
1150   char *buffer = NULL;          /* Dynamically growable fetch buffer. */
1151   char *bufptr;                 /* Pointer to next available byte in buffer. */
1152   char *limit;                  /* First location past end of fetch buffer. */
1153   struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1154   int found_nul;                /* Non-zero if we found the nul char */
1155
1156   /* First we need to figure out the limit on the number of characters we are
1157      going to attempt to fetch and print.  This is actually pretty simple.  If
1158      LEN >= zero, then the limit is the minimum of LEN and print_max.  If
1159      LEN is -1, then the limit is print_max.  This is true regardless of
1160      whether print_max is zero, UINT_MAX (unlimited), or something in between,
1161      because finding the null byte (or available memory) is what actually
1162      limits the fetch. */
1163
1164   fetchlimit = (len == -1 ? print_max : min (len, print_max));
1165
1166   /* Now decide how large of chunks to try to read in one operation.  This
1167      is also pretty simple.  If LEN >= zero, then we want fetchlimit chars,
1168      so we might as well read them all in one operation.  If LEN is -1, we
1169      are looking for a null terminator to end the fetching, so we might as
1170      well read in blocks that are large enough to be efficient, but not so
1171      large as to be slow if fetchlimit happens to be large.  So we choose the
1172      minimum of 8 and fetchlimit.  We used to use 200 instead of 8 but
1173      200 is way too big for remote debugging over a serial line.  */
1174
1175   chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1176
1177   /* Loop until we either have all the characters to print, or we encounter
1178      some error, such as bumping into the end of the address space. */
1179
1180   found_nul = 0;
1181   old_chain = make_cleanup (null_cleanup, 0);
1182
1183   if (len > 0)
1184     {
1185       buffer = (char *) xmalloc (len * width);
1186       bufptr = buffer;
1187       old_chain = make_cleanup (free, buffer);
1188
1189       nfetch = target_read_memory_partial (addr, bufptr, len * width, &errcode)
1190         / width;
1191       addr += nfetch * width;
1192       bufptr += nfetch * width;
1193     }
1194   else if (len == -1)
1195     {
1196       unsigned long bufsize = 0;
1197       do
1198         {
1199           QUIT;
1200           nfetch = min (chunksize, fetchlimit - bufsize);
1201
1202           if (buffer == NULL)
1203             buffer = (char *) xmalloc (nfetch * width);
1204           else
1205             {
1206               discard_cleanups (old_chain);
1207               buffer = (char *) xrealloc (buffer, (nfetch + bufsize) * width);
1208             }
1209
1210           old_chain = make_cleanup (free, buffer);
1211           bufptr = buffer + bufsize * width;
1212           bufsize += nfetch;
1213
1214           /* Read as much as we can. */
1215           nfetch = target_read_memory_partial (addr, bufptr, nfetch * width, &errcode)
1216                    / width;
1217
1218           /* Scan this chunk for the null byte that terminates the string
1219              to print.  If found, we don't need to fetch any more.  Note
1220              that bufptr is explicitly left pointing at the next character
1221              after the null byte, or at the next character after the end of
1222              the buffer. */
1223
1224           limit = bufptr + nfetch * width;
1225           while (bufptr < limit)
1226             {
1227               unsigned long c;
1228
1229               c = extract_unsigned_integer (bufptr, width);
1230               addr += width;
1231               bufptr += width;
1232               if (c == 0)
1233                 {
1234                   /* We don't care about any error which happened after
1235                      the NULL terminator.  */
1236                   errcode = 0;
1237                   found_nul = 1;
1238                   break;
1239                 }
1240             }
1241         }
1242     while (errcode == 0                               /* no error */
1243            && bufptr - buffer < fetchlimit * width   /* no overrun */
1244            && !found_nul);                            /* haven't found nul yet */
1245     }
1246   else
1247     {                           /* length of string is really 0! */
1248       buffer = bufptr = NULL;
1249       errcode = 0;
1250     }
1251
1252   /* bufptr and addr now point immediately beyond the last byte which we
1253      consider part of the string (including a '\0' which ends the string).  */
1254
1255   /* We now have either successfully filled the buffer to fetchlimit, or
1256      terminated early due to an error or finding a null char when LEN is -1. */
1257
1258   if (len == -1 && !found_nul)
1259     {
1260       char *peekbuf;
1261
1262       /* We didn't find a null terminator we were looking for.  Attempt
1263          to peek at the next character.  If not successful, or it is not
1264          a null byte, then force ellipsis to be printed.  */
1265
1266       peekbuf = (char *) alloca (width);
1267
1268       if (target_read_memory (addr, peekbuf, width) == 0
1269           && extract_unsigned_integer (peekbuf, width) != 0)
1270         force_ellipsis = 1;
1271     }
1272   else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer)/width))
1273     {
1274       /* Getting an error when we have a requested length, or fetching less
1275          than the number of characters actually requested, always make us
1276          print ellipsis. */
1277       force_ellipsis = 1;
1278     }
1279
1280   QUIT;
1281
1282   /* If we get an error before fetching anything, don't print a string.
1283      But if we fetch something and then get an error, print the string
1284      and then the error message.  */
1285   if (errcode == 0 || bufptr > buffer)
1286     {
1287       if (addressprint)
1288         {
1289           fputs_filtered (" ", stream);
1290         }
1291       LA_PRINT_STRING (stream, buffer, (bufptr - buffer)/width, width, force_ellipsis);
1292     }
1293
1294   if (errcode != 0)
1295     {
1296       if (errcode == EIO)
1297         {
1298           fprintf_filtered (stream, " <Address ");
1299           print_address_numeric (addr, 1, stream);
1300           fprintf_filtered (stream, " out of bounds>");
1301         }
1302       else
1303         {
1304           fprintf_filtered (stream, " <Error reading address ");
1305           print_address_numeric (addr, 1, stream);
1306           fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1307         }
1308     }
1309   gdb_flush (stream);
1310   do_cleanups (old_chain);
1311   return ((bufptr - buffer)/width);
1312 }
1313
1314 \f
1315 /* Validate an input or output radix setting, and make sure the user
1316    knows what they really did here.  Radix setting is confusing, e.g.
1317    setting the input radix to "10" never changes it!  */
1318
1319 /* ARGSUSED */
1320 static void
1321 set_input_radix (args, from_tty, c)
1322      char *args;
1323      int from_tty;
1324      struct cmd_list_element *c;
1325 {
1326   set_input_radix_1 (from_tty, *(unsigned *)c->var);
1327 }
1328
1329 /* ARGSUSED */
1330 static void
1331 set_input_radix_1 (from_tty, radix)
1332      int from_tty;
1333      unsigned radix;
1334 {
1335   /* We don't currently disallow any input radix except 0 or 1, which don't
1336      make any mathematical sense.  In theory, we can deal with any input
1337      radix greater than 1, even if we don't have unique digits for every
1338      value from 0 to radix-1, but in practice we lose on large radix values.
1339      We should either fix the lossage or restrict the radix range more.
1340      (FIXME). */
1341
1342   if (radix < 2)
1343     {
1344       error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
1345              radix);
1346     }
1347   input_radix = radix;
1348   if (from_tty)
1349     {
1350       printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
1351                        radix, radix, radix);
1352     }
1353 }
1354
1355 /* ARGSUSED */
1356 static void
1357 set_output_radix (args, from_tty, c)
1358      char *args;
1359      int from_tty;
1360      struct cmd_list_element *c;
1361 {
1362   set_output_radix_1 (from_tty, *(unsigned *)c->var);
1363 }
1364
1365 static void
1366 set_output_radix_1 (from_tty, radix)
1367      int from_tty;
1368      unsigned radix;
1369 {
1370   /* Validate the radix and disallow ones that we aren't prepared to
1371      handle correctly, leaving the radix unchanged. */
1372   switch (radix)
1373     {
1374     case 16:
1375       output_format = 'x';              /* hex */
1376       break;
1377     case 10:
1378       output_format = 0;                /* decimal */
1379       break;
1380     case 8:
1381       output_format = 'o';              /* octal */
1382       break;
1383     default:
1384       error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
1385              radix);
1386     }
1387   output_radix = radix;
1388   if (from_tty)
1389     {
1390       printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
1391                        radix, radix, radix);
1392     }
1393 }
1394
1395 /* Set both the input and output radix at once.  Try to set the output radix
1396    first, since it has the most restrictive range.  An radix that is valid as
1397    an output radix is also valid as an input radix.
1398
1399    It may be useful to have an unusual input radix.  If the user wishes to
1400    set an input radix that is not valid as an output radix, he needs to use
1401    the 'set input-radix' command. */
1402
1403 static void
1404 set_radix (arg, from_tty)
1405      char *arg;
1406      int from_tty;
1407 {
1408   unsigned radix;
1409
1410   radix = (arg == NULL) ? 10 : parse_and_eval_address (arg);
1411   set_output_radix_1 (0, radix);
1412   set_input_radix_1 (0, radix);
1413   if (from_tty)
1414     {
1415       printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
1416                        radix, radix, radix);
1417     }
1418 }
1419
1420 /* Show both the input and output radices. */
1421
1422 /*ARGSUSED*/
1423 static void
1424 show_radix (arg, from_tty)
1425      char *arg;
1426      int from_tty;
1427 {
1428   if (from_tty)
1429     {
1430       if (input_radix == output_radix)
1431         {
1432           printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1433                            input_radix, input_radix, input_radix);
1434         }
1435       else
1436         {
1437           printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1438                            input_radix, input_radix, input_radix);
1439           printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1440                            output_radix, output_radix, output_radix);
1441         }
1442     }
1443 }
1444
1445 \f
1446 /*ARGSUSED*/
1447 static void
1448 set_print (arg, from_tty)
1449      char *arg;
1450      int from_tty;
1451 {
1452   printf_unfiltered (
1453 "\"set print\" must be followed by the name of a print subcommand.\n");
1454   help_list (setprintlist, "set print ", -1, gdb_stdout);
1455 }
1456
1457 /*ARGSUSED*/
1458 static void
1459 show_print (args, from_tty)
1460      char *args;
1461      int from_tty;
1462 {
1463   cmd_show_list (showprintlist, from_tty, "");
1464 }
1465 \f
1466 void
1467 _initialize_valprint ()
1468 {
1469   struct cmd_list_element *c;
1470
1471   add_prefix_cmd ("print", no_class, set_print,
1472                   "Generic command for setting how things print.",
1473                   &setprintlist, "set print ", 0, &setlist);
1474   add_alias_cmd ("p", "print", no_class, 1, &setlist); 
1475   /* prefer set print to set prompt */ 
1476   add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1477
1478   add_prefix_cmd ("print", no_class, show_print,
1479                   "Generic command for showing print settings.",
1480                   &showprintlist, "show print ", 0, &showlist);
1481   add_alias_cmd ("p", "print", no_class, 1, &showlist); 
1482   add_alias_cmd ("pr", "print", no_class, 1, &showlist); 
1483
1484   add_show_from_set
1485     (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
1486                   "Set limit on string chars or array elements to print.\n\
1487 \"set print elements 0\" causes there to be no limit.",
1488                   &setprintlist),
1489      &showprintlist);
1490
1491   add_show_from_set
1492     (add_set_cmd ("null-stop", no_class, var_boolean,
1493                   (char *)&stop_print_at_null,
1494                   "Set printing of char arrays to stop at first null char.",
1495                   &setprintlist),
1496      &showprintlist);
1497
1498   add_show_from_set
1499     (add_set_cmd ("repeats", no_class, var_uinteger,
1500                   (char *)&repeat_count_threshold,
1501                   "Set threshold for repeated print elements.\n\
1502 \"set print repeats 0\" causes all elements to be individually printed.",
1503                   &setprintlist),
1504      &showprintlist);
1505
1506   add_show_from_set
1507     (add_set_cmd ("pretty", class_support, var_boolean,
1508                   (char *)&prettyprint_structs,
1509                   "Set prettyprinting of structures.",
1510                   &setprintlist),
1511      &showprintlist);
1512
1513   add_show_from_set
1514     (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
1515                   "Set printing of unions interior to structures.",
1516                   &setprintlist),
1517      &showprintlist);
1518   
1519   add_show_from_set
1520     (add_set_cmd ("array", class_support, var_boolean,
1521                   (char *)&prettyprint_arrays,
1522                   "Set prettyprinting of arrays.",
1523                   &setprintlist),
1524      &showprintlist);
1525
1526   add_show_from_set
1527     (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
1528                   "Set printing of addresses.",
1529                   &setprintlist),
1530      &showprintlist);
1531
1532   c = add_set_cmd ("input-radix", class_support, var_uinteger,
1533                    (char *)&input_radix,
1534                   "Set default input radix for entering numbers.",
1535                   &setlist);
1536   add_show_from_set (c, &showlist);
1537   c->function.sfunc = set_input_radix;
1538
1539   c = add_set_cmd ("output-radix", class_support, var_uinteger,
1540                    (char *)&output_radix,
1541                   "Set default output radix for printing of values.",
1542                   &setlist);
1543   add_show_from_set (c, &showlist);
1544   c->function.sfunc = set_output_radix;
1545
1546   /* The "set radix" and "show radix" commands are special in that they are
1547      like normal set and show commands but allow two normally independent
1548      variables to be either set or shown with a single command.  So the
1549      usual add_set_cmd() and add_show_from_set() commands aren't really
1550      appropriate. */
1551   add_cmd ("radix", class_support, set_radix,
1552            "Set default input and output number radices.\n\
1553 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1554 Without an argument, sets both radices back to the default value of 10.",
1555            &setlist);
1556   add_cmd ("radix", class_support, show_radix,
1557            "Show the default input and output number radices.\n\
1558 Use 'show input-radix' or 'show output-radix' to independently show each.",
1559            &showlist);
1560
1561   /* Give people the defaults which they are used to.  */
1562   prettyprint_structs = 0;
1563   prettyprint_arrays = 0;
1564   unionprint = 1;
1565   addressprint = 1;
1566   print_max = PRINT_MAX_DEFAULT;
1567 }