OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [top.c] - Blame information for rev 362

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 104 markom
/* Top level stuff for GDB, the GNU debugger.
2
   Copyright 1986-2000 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., 59 Temple Place - Suite 330,
19
   Boston, MA 02111-1307, USA.  */
20
 
21
#include "defs.h"
22
#include "gdbcmd.h"
23
#include "call-cmds.h"
24
#include "symtab.h"
25
#include "inferior.h"
26
#include "signals.h"
27
#include "target.h"
28
#include "breakpoint.h"
29
#include "gdbtypes.h"
30
#include "expression.h"
31
#include "value.h"
32
#include "language.h"
33
#include "terminal.h"           /* For job_control.  */
34
#include "annotate.h"
35
#include "top.h"
36
#include "version.h"
37
 
38
/* readline include files */
39
#include <readline/readline.h>
40
#include <readline/history.h>
41
 
42
/* readline defines this.  */
43
#undef savestring
44
 
45
#include <sys/types.h>
46
 
47
#include <setjmp.h>
48
 
49
#include "event-top.h"
50
#include "gdb_string.h"
51
#include "gdb_stat.h"
52
#include <ctype.h>
53
#ifdef UI_OUT
54
#include "ui-out.h"
55
#include "cli-out.h"
56
#endif
57
 
58
/* Prototypes for local functions */
59
 
60
static void dont_repeat_command PARAMS ((char *, int));
61
 
62
static void source_cleanup_lines PARAMS ((PTR));
63
 
64
static void user_defined_command PARAMS ((char *, int));
65
 
66
static void init_signals PARAMS ((void));
67
 
68
#ifdef STOP_SIGNAL
69
static void stop_sig PARAMS ((int));
70
#endif
71
 
72
static char *line_completion_function PARAMS ((char *, int, char *, int));
73
 
74
static char *readline_line_completion_function PARAMS ((char *, int));
75
 
76
/* NOTE 1999-04-29: this function will be static again, after we make the
77
   event loop be the default command loop for gdb, and we merge
78
   event-top.c into this file, top.c */
79
/* static */ void command_loop_marker PARAMS ((int));
80
 
81
static void while_command PARAMS ((char *, int));
82
 
83
static void if_command PARAMS ((char *, int));
84
 
85
static struct command_line *
86
  build_command_line PARAMS ((enum command_control_type, char *));
87
 
88
static struct command_line *
89
  get_command_line PARAMS ((enum command_control_type, char *));
90
 
91
static void realloc_body_list PARAMS ((struct command_line *, int));
92
 
93
static enum misc_command_type read_next_line PARAMS ((struct command_line **));
94
 
95
static enum command_control_type
96
recurse_read_control_structure PARAMS ((struct command_line *));
97
 
98
static struct cleanup *setup_user_args PARAMS ((char *));
99
 
100
static char *locate_arg PARAMS ((char *));
101
 
102
static char *insert_args PARAMS ((char *));
103
 
104
static void arg_cleanup PARAMS ((void));
105
 
106
static void init_main PARAMS ((void));
107
 
108
static void init_cmd_lists PARAMS ((void));
109
 
110
static void float_handler PARAMS ((int));
111
 
112
static void init_signals PARAMS ((void));
113
 
114
static void set_verbose PARAMS ((char *, int, struct cmd_list_element *));
115
 
116
static void show_history PARAMS ((char *, int));
117
 
118
static void set_history PARAMS ((char *, int));
119
 
120
static void set_history_size_command PARAMS ((char *, int,
121
                                              struct cmd_list_element *));
122
 
123
static void show_commands PARAMS ((char *, int));
124
 
125
static void echo_command PARAMS ((char *, int));
126
 
127
static void pwd_command PARAMS ((char *, int));
128
 
129
static void show_version PARAMS ((char *, int));
130
 
131
static void document_command PARAMS ((char *, int));
132
 
133
static void define_command PARAMS ((char *, int));
134
 
135
static void validate_comname PARAMS ((char *));
136
 
137
static void help_command PARAMS ((char *, int));
138
 
139
static void show_command PARAMS ((char *, int));
140
 
141
static void info_command PARAMS ((char *, int));
142
 
143
static void complete_command PARAMS ((char *, int));
144
 
145
static void do_nothing PARAMS ((int));
146
 
147
static void show_debug PARAMS ((char *, int));
148
 
149
static void set_debug PARAMS ((char *, int));
150
 
151
#ifdef SIGHUP
152
/* NOTE 1999-04-29: This function will be static again, once we modify
153
   gdb to use the event loop as the default command loop and we merge
154
   event-top.c into this file, top.c */
155
/* static */ int quit_cover PARAMS ((PTR));
156
 
157
static void disconnect PARAMS ((int));
158
#endif
159
 
160
static void source_cleanup PARAMS ((FILE *));
161
 
162
/* Default command line prompt.  This is overriden in some configs. */
163
 
164
#ifndef DEFAULT_PROMPT
165
#define DEFAULT_PROMPT  "(gdb) "
166
#endif
167
 
168
/* Initialization file name for gdb.  This is overridden in some configs.  */
169
 
170
#ifndef GDBINIT_FILENAME
171
#define GDBINIT_FILENAME        ".gdbinit"
172
#endif
173
char gdbinit[] = GDBINIT_FILENAME;
174
 
175
int inhibit_gdbinit = 0;
176
 
177
/* If nonzero, and GDB has been configured to be able to use windows,
178
   attempt to open them upon startup.  */
179
 
180
int use_windows = 1;
181
 
182
extern char lang_frame_mismatch_warn[];         /* language.c */
183
 
184
/* Flag for whether we want all the "from_tty" gubbish printed.  */
185
 
186
int caution = 1;                /* Default is yes, sigh. */
187
 
188
/* Define all cmd_list_elements.  */
189
 
190
/* Chain containing all defined commands.  */
191
 
192
struct cmd_list_element *cmdlist;
193
 
194
/* Chain containing all defined info subcommands.  */
195
 
196
struct cmd_list_element *infolist;
197
 
198
/* Chain containing all defined enable subcommands. */
199
 
200
struct cmd_list_element *enablelist;
201
 
202
/* Chain containing all defined disable subcommands. */
203
 
204
struct cmd_list_element *disablelist;
205
 
206
/* Chain containing all defined toggle subcommands. */
207
 
208
struct cmd_list_element *togglelist;
209
 
210
/* Chain containing all defined stop subcommands. */
211
 
212
struct cmd_list_element *stoplist;
213
 
214
/* Chain containing all defined delete subcommands. */
215
 
216
struct cmd_list_element *deletelist;
217
 
218
/* Chain containing all defined "enable breakpoint" subcommands. */
219
 
220
struct cmd_list_element *enablebreaklist;
221
 
222
/* Chain containing all defined set subcommands */
223
 
224
struct cmd_list_element *setlist;
225
 
226
/* Chain containing all defined unset subcommands */
227
 
228
struct cmd_list_element *unsetlist;
229
 
230
/* Chain containing all defined show subcommands.  */
231
 
232
struct cmd_list_element *showlist;
233
 
234
/* Chain containing all defined \"set history\".  */
235
 
236
struct cmd_list_element *sethistlist;
237
 
238
/* Chain containing all defined \"show history\".  */
239
 
240
struct cmd_list_element *showhistlist;
241
 
242
/* Chain containing all defined \"unset history\".  */
243
 
244
struct cmd_list_element *unsethistlist;
245
 
246
/* Chain containing all defined maintenance subcommands. */
247
 
248
struct cmd_list_element *maintenancelist;
249
 
250
/* Chain containing all defined "maintenance info" subcommands. */
251
 
252
struct cmd_list_element *maintenanceinfolist;
253
 
254
/* Chain containing all defined "maintenance print" subcommands. */
255
 
256
struct cmd_list_element *maintenanceprintlist;
257
 
258
struct cmd_list_element *setprintlist;
259
 
260
struct cmd_list_element *showprintlist;
261
 
262
struct cmd_list_element *setdebuglist;
263
 
264
struct cmd_list_element *showdebuglist;
265
 
266
struct cmd_list_element *setchecklist;
267
 
268
struct cmd_list_element *showchecklist;
269
 
270
/* stdio stream that command input is being read from.  Set to stdin normally.
271
   Set by source_command to the file we are sourcing.  Set to NULL if we are
272
   executing a user-defined command or interacting via a GUI.  */
273
 
274
FILE *instream;
275
 
276
/* Current working directory.  */
277
 
278
char *current_directory;
279
 
280
/* The directory name is actually stored here (usually).  */
281
char gdb_dirbuf[1024];
282
 
283
/* Function to call before reading a command, if nonzero.
284
   The function receives two args: an input stream,
285
   and a prompt string.  */
286
 
287
void (*window_hook) PARAMS ((FILE *, char *));
288
 
289
int epoch_interface;
290
int xgdb_verbose;
291
 
292
/* gdb prints this when reading a command interactively */
293
static char *gdb_prompt_string; /* the global prompt string */
294
extern char *get_prompt PARAMS ((void));        /* access function for prompt string */
295
 
296
/* Buffer used for reading command lines, and the size
297
   allocated for it so far.  */
298
 
299
char *line;
300
int linesize = 100;
301
 
302
/* Nonzero if the current command is modified by "server ".  This
303
   affects things like recording into the command history, commands
304
   repeating on RETURN, etc.  This is so a user interface (emacs, GUI,
305
   whatever) can issue its own commands and also send along commands
306
   from the user, and have the user not notice that the user interface
307
   is issuing commands too.  */
308
int server_command;
309
 
310
/* Baud rate specified for talking to serial target systems.  Default
311
   is left as -1, so targets can choose their own defaults.  */
312
/* FIXME: This means that "show remotebaud" and gr_files_info can print -1
313
   or (unsigned int)-1.  This is a Bad User Interface.  */
314
 
315
int baud_rate = -1;
316
 
317
/* Timeout limit for response from target. */
318
 
319
/* The default value has been changed many times over the years.  It
320
   was originally 5 seconds.  But that was thought to be a long time
321
   to sit and wait, so it was changed to 2 seconds.  That was thought
322
   to be plenty unless the connection was going through some terminal
323
   server or multiplexer or other form of hairy serial connection.
324
 
325
   In mid-1996, remote_timeout was moved from remote.c to top.c and
326
   it began being used in other remote-* targets.  It appears that the
327
   default was changed to 20 seconds at that time, perhaps because the
328
   Hitachi E7000 ICE didn't always respond in a timely manner.
329
 
330
   But if 5 seconds is a long time to sit and wait for retransmissions,
331
   20 seconds is far worse.  This demonstrates the difficulty of using
332
   a single variable for all protocol timeouts.
333
 
334
   As remote.c is used much more than remote-e7000.c, it was changed
335
   back to 2 seconds in 1999. */
336
 
337
int remote_timeout = 2;
338
 
339
/* Non-zero tells remote* modules to output debugging info.  */
340
 
341
int remote_debug = 0;
342
 
343
/* Non-zero means the target is running. Note: this is different from
344
   saying that there is an active target and we are stopped at a
345
   breakpoint, for instance. This is a real indicator whether the
346
   target is off and running, which gdb is doing something else. */
347
int target_executing = 0;
348
 
349
/* Level of control structure.  */
350
static int control_level;
351
 
352
/* Structure for arguments to user defined functions.  */
353
#define MAXUSERARGS 10
354
struct user_args
355
  {
356
    struct user_args *next;
357
    struct
358
      {
359
        char *arg;
360
        int len;
361
      }
362
    a[MAXUSERARGS];
363
    int count;
364
  }
365
 *user_args;
366
 
367
/* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT.  */
368
 
369
#ifndef STOP_SIGNAL
370
#ifdef SIGTSTP
371
#define STOP_SIGNAL SIGTSTP
372
static void stop_sig PARAMS ((int));
373
#endif
374
#endif
375
 
376
/* Some System V have job control but not sigsetmask(). */
377
#if !defined (HAVE_SIGSETMASK)
378
#if !defined (USG)
379
#define HAVE_SIGSETMASK 1
380
#else
381
#define HAVE_SIGSETMASK 0
382
#endif
383
#endif
384
 
385
#if 0 == (HAVE_SIGSETMASK)
386
#define sigsetmask(n)
387
#endif
388
 
389
/* Hooks for alternate command interfaces.  */
390
 
391
/* Called after most modules have been initialized, but before taking users
392
   command file.  */
393
 
394
void (*init_ui_hook) PARAMS ((char *argv0));
395
 
396
/* This hook is called from within gdb's many mini-event loops which could
397
   steal control from a real user interface's event loop. It returns
398
   non-zero if the user is requesting a detach, zero otherwise. */
399
 
400
int (*ui_loop_hook) PARAMS ((int));
401
 
402
/* Called instead of command_loop at top level.  Can be invoked via
403
   return_to_top_level.  */
404
 
405
void (*command_loop_hook) PARAMS ((void));
406
 
407
 
408
/* Called from print_frame_info to list the line we stopped in.  */
409
 
410
void (*print_frame_info_listing_hook) PARAMS ((struct symtab * s, int line,
411
                                               int stopline, int noerror));
412
/* Replaces most of query.  */
413
 
414
int (*query_hook) PARAMS ((const char *, va_list));
415
 
416
/* Replaces most of warning.  */
417
 
418
void (*warning_hook) PARAMS ((const char *, va_list));
419
 
420
/* These three functions support getting lines of text from the user.  They
421
   are used in sequence.  First readline_begin_hook is called with a text
422
   string that might be (for example) a message for the user to type in a
423
   sequence of commands to be executed at a breakpoint.  If this function
424
   calls back to a GUI, it might take this opportunity to pop up a text
425
   interaction window with this message.  Next, readline_hook is called
426
   with a prompt that is emitted prior to collecting the user input.
427
   It can be called multiple times.  Finally, readline_end_hook is called
428
   to notify the GUI that we are done with the interaction window and it
429
   can close it. */
430
 
431
void (*readline_begin_hook) PARAMS ((char *,...));
432
char *(*readline_hook) PARAMS ((char *));
433
void (*readline_end_hook) PARAMS ((void));
434
 
435
/* Called as appropriate to notify the interface of the specified breakpoint
436
   conditions.  */
437
 
438
void (*create_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
439
void (*delete_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
440
void (*modify_breakpoint_hook) PARAMS ((struct breakpoint * bpt));
441
 
442
/* Called as appropriate to notify the interface that we have attached
443
   to or detached from an already running process. */
444
 
445
void (*attach_hook) PARAMS ((void));
446
void (*detach_hook) PARAMS ((void));
447
 
448
/* Called during long calculations to allow GUI to repair window damage, and to
449
   check for stop buttons, etc... */
450
 
451
void (*interactive_hook) PARAMS ((void));
452
 
453
/* Called when the registers have changed, as a hint to a GUI
454
   to minimize window update. */
455
 
456
void (*registers_changed_hook) PARAMS ((void));
457
 
458
/* Tell the GUI someone changed the register REGNO. -1 means
459
   that the caller does not know which register changed or
460
   that several registers have changed (see value_assign). */
461
void (*register_changed_hook) PARAMS ((int regno));
462
 
463
/* Tell the GUI someone changed LEN bytes of memory at ADDR */
464
void (*memory_changed_hook) PARAMS ((CORE_ADDR addr, int len));
465
 
466
/* Called when going to wait for the target.  Usually allows the GUI to run
467
   while waiting for target events.  */
468
 
469
int (*target_wait_hook) PARAMS ((int pid, struct target_waitstatus * status));
470
 
471
/* Used by UI as a wrapper around command execution.  May do various things
472
   like enabling/disabling buttons, etc...  */
473
 
474
void (*call_command_hook) PARAMS ((struct cmd_list_element * c, char *cmd,
475
                                   int from_tty));
476
 
477
/* Called after a `set' command has finished.  Is only run if the
478
   `set' command succeeded.  */
479
 
480
void (*set_hook) (struct cmd_list_element * c);
481
 
482
/* Called when the current thread changes.  Argument is thread id.  */
483
 
484
void (*context_hook) PARAMS ((int id));
485
 
486
/* Takes control from error ().  Typically used to prevent longjmps out of the
487
   middle of the GUI.  Usually used in conjunction with a catch routine.  */
488
 
489
NORETURN void (*error_hook) (void) ATTR_NORETURN;
490
 
491
 
492
/* One should use catch_errors rather than manipulating these
493
   directly.  */
494
#if defined(HAVE_SIGSETJMP)
495
#define SIGJMP_BUF              sigjmp_buf
496
#define SIGSETJMP(buf)          sigsetjmp(buf, 1)
497
#define SIGLONGJMP(buf,val)     siglongjmp(buf,val)
498
#else
499
#define SIGJMP_BUF              jmp_buf
500
#define SIGSETJMP(buf)          setjmp(buf)
501
#define SIGLONGJMP(buf,val)     longjmp(buf,val)
502
#endif
503
 
504
/* Where to go for return_to_top_level.  */
505
static SIGJMP_BUF *catch_return;
506
 
507
/* Return for reason REASON to the nearest containing catch_errors().  */
508
 
509
NORETURN void
510
return_to_top_level (reason)
511
     enum return_reason reason;
512
{
513
  quit_flag = 0;
514
  immediate_quit = 0;
515
 
516
  /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
517
     I can think of a reason why that is vital, though).  */
518
  bpstat_clear_actions (stop_bpstat);   /* Clear queued breakpoint commands */
519
 
520
  disable_current_display ();
521
  do_cleanups (ALL_CLEANUPS);
522
  if (event_loop_p && target_can_async_p () && !target_executing)
523
    do_exec_cleanups (ALL_CLEANUPS);
524
  if (event_loop_p && sync_execution)
525
    do_exec_error_cleanups (ALL_CLEANUPS);
526
 
527
  if (annotation_level > 1)
528
    switch (reason)
529
      {
530
      case RETURN_QUIT:
531
        annotate_quit ();
532
        break;
533
      case RETURN_ERROR:
534
        annotate_error ();
535
        break;
536
      }
537
 
538
  /* Jump to the containing catch_errors() call, communicating REASON
539
     to that call via setjmp's return value.  Note that REASON can't
540
     be zero, by definition in defs.h. */
541
 
542
  (NORETURN void) SIGLONGJMP (*catch_return, (int) reason);
543
}
544
 
545
/* Call FUNC with arg ARGS, catching any errors.  If there is no
546
   error, return the value returned by FUNC.  If there is an error,
547
   print ERRSTRING, print the specific error message, then return
548
   zero.
549
 
550
   Must not be called with immediate_quit in effect (bad things might
551
   happen, say we got a signal in the middle of a memcpy to quit_return).
552
   This is an OK restriction; with very few exceptions immediate_quit can
553
   be replaced by judicious use of QUIT.
554
 
555
   MASK specifies what to catch; it is normally set to
556
   RETURN_MASK_ALL, if for no other reason than that the code which
557
   calls catch_errors might not be set up to deal with a quit which
558
   isn't caught.  But if the code can deal with it, it generally
559
   should be RETURN_MASK_ERROR, unless for some reason it is more
560
   useful to abort only the portion of the operation inside the
561
   catch_errors.  Note that quit should return to the command line
562
   fairly quickly, even if some further processing is being done.  */
563
 
564
/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
565
   error() et.al. could maintain a set of flags that indicate the the
566
   current state of each of the longjmp buffers.  This would give the
567
   longjmp code the chance to detect a longjmp botch (before it gets
568
   to longjmperror()).  Prior to 1999-11-05 this wasn't possible as
569
   code also randomly used a SET_TOP_LEVEL macro that directly
570
   initialize the longjmp buffers. */
571
 
572
/* MAYBE: cagney/1999-11-05: Should the catch_erros and cleanups code
573
   be consolidated into a single file instead of being distributed
574
   between utils.c and top.c? */
575
 
576
int
577
catch_errors (func, args, errstring, mask)
578
     catch_errors_ftype *func;
579
     PTR args;
580
     char *errstring;
581
     return_mask mask;
582
{
583
  SIGJMP_BUF *saved_catch;
584
  SIGJMP_BUF catch;
585
  int val;
586
  struct cleanup *saved_cleanup_chain;
587
  char *saved_error_pre_print;
588
  char *saved_quit_pre_print;
589
 
590
  /* Return value from SIGSETJMP(): enum return_reason if error or
591
     quit caught, 0 otherwise. */
592
  int caught;
593
 
594
  /* Override error/quit messages during FUNC. */
595
 
596
  saved_error_pre_print = error_pre_print;
597
  saved_quit_pre_print = quit_pre_print;
598
 
599
  if (mask & RETURN_MASK_ERROR)
600
    error_pre_print = errstring;
601
  if (mask & RETURN_MASK_QUIT)
602
    quit_pre_print = errstring;
603
 
604
  /* Prevent error/quit during FUNC from calling cleanups established
605
     prior to here. */
606
 
607
  saved_cleanup_chain = save_cleanups ();
608
 
609
  /* Call FUNC, catching error/quit events. */
610
 
611
  saved_catch = catch_return;
612
  catch_return = &catch;
613
  caught = SIGSETJMP (catch);
614
  if (!caught)
615
    val = (*func) (args);
616
  catch_return = saved_catch;
617
 
618
  /* FIXME: cagney/1999-11-05: A correct FUNC implementaton will
619
     clean things up (restoring the cleanup chain) to the state they
620
     were just prior to the call.  Unfortunatly, many FUNC's are not
621
     that well behaved.  This could be fixed by adding either a
622
     do_cleanups call (to cover the problem) or an assertion check to
623
     detect bad FUNCs code. */
624
 
625
  /* Restore the cleanup chain and error/quit messages to their
626
     original states. */
627
 
628
  restore_cleanups (saved_cleanup_chain);
629
 
630
  if (mask & RETURN_MASK_QUIT)
631
    quit_pre_print = saved_quit_pre_print;
632
  if (mask & RETURN_MASK_ERROR)
633
    error_pre_print = saved_error_pre_print;
634
 
635
  /* Return normally if no error/quit event occurred. */
636
 
637
  if (!caught)
638
    return val;
639
 
640
  /* If the caller didn't request that the event be caught, relay the
641
     event to the next containing catch_errors(). */
642
 
643
  if (!(mask & RETURN_MASK (caught)))
644
    return_to_top_level (caught);
645
 
646
  /* Tell the caller that an event was caught.
647
 
648
     FIXME: nsd/2000-02-22: When MASK is RETURN_MASK_ALL, the caller
649
     can't tell what type of event occurred.
650
 
651
     A possible fix is to add a new interface, catch_event(), that
652
     returns enum return_reason after catching an error or a quit.
653
 
654
     When returning normally, i.e. without catching an error or a
655
     quit, catch_event() could return RETURN_NORMAL, which would be
656
     added to enum return_reason.  FUNC would return information
657
     exclusively via ARGS.
658
 
659
     Alternatively, normal catch_event() could return FUNC's return
660
     value.  The caller would need to be aware of potential overlap
661
     with enum return_reason, which could be publicly restricted to
662
     negative values to simplify return value processing in FUNC and
663
     in the caller. */
664
 
665
  return 0;
666
}
667
 
668
struct captured_command_args
669
  {
670
    catch_command_errors_ftype *command;
671
    char *arg;
672
    int from_tty;
673
  };
674
 
675
static int
676
do_captured_command (void *data)
677
{
678
  struct captured_command_args *context = data;
679
  context->command (context->arg, context->from_tty);
680
  /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
681
     isn't needed.  Instead an assertion check could be made that
682
     simply confirmed that the called function correctly cleaned up
683
     after its self.  Unfortunatly, old code (prior to 1999-11-04) in
684
     main.c was calling SET_TOP_LEVEL(), calling the command function,
685
     and then *always* calling do_cleanups().  For the moment we
686
     remain ``bug compatible'' with that old code..  */
687
  do_cleanups (ALL_CLEANUPS);
688
  return 1;
689
}
690
 
691
int
692
catch_command_errors (catch_command_errors_ftype * command,
693
                      char *arg, int from_tty, return_mask mask)
694
{
695
  struct captured_command_args args;
696
  args.command = command;
697
  args.arg = arg;
698
  args.from_tty = from_tty;
699
  return catch_errors (do_captured_command, &args, "", mask);
700
}
701
 
702
 
703
/* Handler for SIGHUP.  */
704
 
705
#ifdef SIGHUP
706
static void
707
disconnect (signo)
708
     int signo;
709
{
710
  catch_errors (quit_cover, NULL,
711
              "Could not kill the program being debugged", RETURN_MASK_ALL);
712
  signal (SIGHUP, SIG_DFL);
713
  kill (getpid (), SIGHUP);
714
}
715
 
716
/* Just a little helper function for disconnect().  */
717
 
718
/* NOTE 1999-04-29: This function will be static again, once we modify
719
   gdb to use the event loop as the default command loop and we merge
720
   event-top.c into this file, top.c */
721
/* static */ int
722
quit_cover (s)
723
     PTR s;
724
{
725
  caution = 0;                   /* Throw caution to the wind -- we're exiting.
726
                                   This prevents asking the user dumb questions.  */
727
  quit_command ((char *) 0, 0);
728
  return 0;
729
}
730
#endif /* defined SIGHUP */
731
 
732
/* Line number we are currently in in a file which is being sourced.  */
733
/* NOTE 1999-04-29: This variable will be static again, once we modify
734
   gdb to use the event loop as the default command loop and we merge
735
   event-top.c into this file, top.c */
736
/* static */ int source_line_number;
737
 
738
/* Name of the file we are sourcing.  */
739
/* NOTE 1999-04-29: This variable will be static again, once we modify
740
   gdb to use the event loop as the default command loop and we merge
741
   event-top.c into this file, top.c */
742
/* static */ char *source_file_name;
743
 
744
/* Buffer containing the error_pre_print used by the source stuff.
745
   Malloc'd.  */
746
/* NOTE 1999-04-29: This variable will be static again, once we modify
747
   gdb to use the event loop as the default command loop and we merge
748
   event-top.c into this file, top.c */
749
/* static */ char *source_error;
750
static int source_error_allocated;
751
 
752
/* Something to glom on to the start of error_pre_print if source_file_name
753
   is set.  */
754
/* NOTE 1999-04-29: This variable will be static again, once we modify
755
   gdb to use the event loop as the default command loop and we merge
756
   event-top.c into this file, top.c */
757
/* static */ char *source_pre_error;
758
 
759
/* Clean up on error during a "source" command (or execution of a
760
   user-defined command).  */
761
 
762
static void
763
source_cleanup (stream)
764
     FILE *stream;
765
{
766
  /* Restore the previous input stream.  */
767
  instream = stream;
768
}
769
 
770
/* Read commands from STREAM.  */
771
void
772
read_command_file (stream)
773
     FILE *stream;
774
{
775
  struct cleanup *cleanups;
776
 
777
  cleanups = make_cleanup ((make_cleanup_func) source_cleanup, instream);
778
  instream = stream;
779
  command_loop ();
780
  do_cleanups (cleanups);
781
}
782
 
783
extern void init_proc PARAMS ((void));
784
 
785
void (*pre_init_ui_hook) PARAMS ((void));
786
 
787
void
788
gdb_init (argv0)
789
     char *argv0;
790
{
791
  if (pre_init_ui_hook)
792
    pre_init_ui_hook ();
793
 
794
  /* Run the init function of each source file */
795
 
796
  getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
797
  current_directory = gdb_dirbuf;
798
 
799
#ifdef __MSDOS__
800
  /* Make sure we return to the original directory upon exit, come
801
     what may, since the OS doesn't do that for us.  */
802
  make_final_cleanup ((make_cleanup_func) chdir, strsave (current_directory));
803
#endif
804
 
805
  init_cmd_lists ();            /* This needs to be done first */
806
  initialize_targets ();        /* Setup target_terminal macros for utils.c */
807
  initialize_utils ();          /* Make errors and warnings possible */
808
  initialize_all_files ();
809
  initialize_current_architecture ();
810
  init_main ();                 /* But that omits this file!  Do it now */
811
 
812
  /* The signal handling mechanism is different depending whether or
813
     not the async version is run. NOTE: in the future we plan to make
814
     the event loop be the default engine of gdb, and this difference
815
     will disappear. */
816
  if (event_loop_p)
817
    async_init_signals ();
818
  else
819
    init_signals ();
820
 
821
  /* We need a default language for parsing expressions, so simple things like
822
     "set width 0" won't fail if no language is explicitly set in a config file
823
     or implicitly set by reading an executable during startup. */
824
  set_language (language_c);
825
  expected_language = current_language;         /* don't warn about the change.  */
826
 
827
#ifdef UI_OUT
828
  /* Install the default UI */
829
  uiout = cli_out_new (gdb_stdout);
830
#endif
831
 
832
#ifdef UI_OUT
833
  /* All the interpreters should have had a look at things by now.
834
     Initialize the selected interpreter. */
835
  if (interpreter_p && !init_ui_hook)
836
    {
837
      fprintf_unfiltered (gdb_stderr, "Interpreter `%s' unrecognized.\n",
838
                          interpreter_p);
839
      exit (1);
840
    }
841
#endif
842
 
843
  if (init_ui_hook)
844
    init_ui_hook (argv0);
845
}
846
 
847
/* Allocate, initialize a new command line structure for one of the
848
   control commands (if/while).  */
849
 
850
static struct command_line *
851
build_command_line (type, args)
852
     enum command_control_type type;
853
     char *args;
854
{
855
  struct command_line *cmd;
856
 
857
  if (args == NULL)
858
    error ("if/while commands require arguments.\n");
859
 
860
  cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
861
  cmd->next = NULL;
862
  cmd->control_type = type;
863
 
864
  cmd->body_count = 1;
865
  cmd->body_list
866
    = (struct command_line **) xmalloc (sizeof (struct command_line *)
867
                                        * cmd->body_count);
868
  memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
869
  cmd->line = savestring (args, strlen (args));
870
  return cmd;
871
}
872
 
873
/* Build and return a new command structure for the control commands
874
   such as "if" and "while".  */
875
 
876
static struct command_line *
877
get_command_line (type, arg)
878
     enum command_control_type type;
879
     char *arg;
880
{
881
  struct command_line *cmd;
882
  struct cleanup *old_chain = NULL;
883
 
884
  /* Allocate and build a new command line structure.  */
885
  cmd = build_command_line (type, arg);
886
 
887
  old_chain = make_cleanup ((make_cleanup_func) free_command_lines, &cmd);
888
 
889
  /* Read in the body of this command.  */
890
  if (recurse_read_control_structure (cmd) == invalid_control)
891
    {
892
      warning ("error reading in control structure\n");
893
      do_cleanups (old_chain);
894
      return NULL;
895
    }
896
 
897
  discard_cleanups (old_chain);
898
  return cmd;
899
}
900
 
901
/* Recursively print a command (including full control structures).  */
902
#ifdef UI_OUT
903
void
904
print_command_lines (uiout, cmd, depth)
905
     struct ui_out *uiout;
906
     struct command_line *cmd;
907
     unsigned int depth;
908
{
909
  struct command_line *list;
910
 
911
  list = cmd;
912
  while (list)
913
    {
914
 
915
      if (depth)
916
        ui_out_spaces (uiout, 2 * depth);
917
 
918
      /* A simple command, print it and continue.  */
919
      if (list->control_type == simple_control)
920
        {
921
          ui_out_field_string (uiout, NULL, list->line);
922
          ui_out_text (uiout, "\n");
923
          list = list->next;
924
          continue;
925
        }
926
 
927
      /* loop_continue to jump to the start of a while loop, print it
928
         and continue. */
929
      if (list->control_type == continue_control)
930
        {
931
          ui_out_field_string (uiout, NULL, "loop_continue");
932
          ui_out_text (uiout, "\n");
933
          list = list->next;
934
          continue;
935
        }
936
 
937
      /* loop_break to break out of a while loop, print it and continue.  */
938
      if (list->control_type == break_control)
939
        {
940
          ui_out_field_string (uiout, NULL, "loop_break");
941
          ui_out_text (uiout, "\n");
942
          list = list->next;
943
          continue;
944
        }
945
 
946
      /* A while command.  Recursively print its subcommands and continue.  */
947
      if (list->control_type == while_control)
948
        {
949
          ui_out_text (uiout, "while ");
950
          ui_out_field_fmt (uiout, NULL, "while %s", list->line);
951
          ui_out_text (uiout, "\n");
952
          print_command_lines (uiout, *list->body_list, depth + 1);
953
          ui_out_field_string (uiout, NULL, "end");
954
          if (depth)
955
            ui_out_spaces (uiout, 2 * depth);
956
          ui_out_text (uiout, "end\n");
957
          list = list->next;
958
          continue;
959
        }
960
 
961
      /* An if command.  Recursively print both arms before continueing.  */
962
      if (list->control_type == if_control)
963
        {
964
          ui_out_text (uiout, "if ");
965
          ui_out_field_fmt (uiout, NULL, "if %s", list->line);
966
          ui_out_text (uiout, "\n");
967
          /* The true arm. */
968
          print_command_lines (uiout, list->body_list[0], depth + 1);
969
 
970
          /* Show the false arm if it exists.  */
971
          if (list->body_count == 2)
972
            {
973
              if (depth)
974
                ui_out_spaces (uiout, 2 * depth);
975
              ui_out_field_string (uiout, NULL, "else");
976
              ui_out_text (uiout, "else\n");
977
              print_command_lines (uiout, list->body_list[1], depth + 1);
978
            }
979
 
980
          ui_out_field_string (uiout, NULL, "end");
981
          if (depth)
982
            ui_out_spaces (uiout, 2 * depth);
983
          ui_out_text (uiout, "end\n");
984
          list = list->next;
985
          continue;
986
        }
987
 
988
      /* ignore illegal command type and try next */
989
      list = list->next;
990
    }                           /* while (list) */
991
}
992
#else
993
void
994
print_command_line (cmd, depth, stream)
995
     struct command_line *cmd;
996
     unsigned int depth;
997
     struct ui_file *stream;
998
{
999
  unsigned int i;
1000
 
1001
  if (depth)
1002
    {
1003
      for (i = 0; i < depth; i++)
1004
        fputs_filtered ("  ", stream);
1005
    }
1006
 
1007
  /* A simple command, print it and return.  */
1008
  if (cmd->control_type == simple_control)
1009
    {
1010
      fputs_filtered (cmd->line, stream);
1011
      fputs_filtered ("\n", stream);
1012
      return;
1013
    }
1014
 
1015
  /* loop_continue to jump to the start of a while loop, print it
1016
     and return. */
1017
  if (cmd->control_type == continue_control)
1018
    {
1019
      fputs_filtered ("loop_continue\n", stream);
1020
      return;
1021
    }
1022
 
1023
  /* loop_break to break out of a while loop, print it and return.  */
1024
  if (cmd->control_type == break_control)
1025
    {
1026
      fputs_filtered ("loop_break\n", stream);
1027
      return;
1028
    }
1029
 
1030
  /* A while command.  Recursively print its subcommands before returning.  */
1031
  if (cmd->control_type == while_control)
1032
    {
1033
      struct command_line *list;
1034
      fputs_filtered ("while ", stream);
1035
      fputs_filtered (cmd->line, stream);
1036
      fputs_filtered ("\n", stream);
1037
      list = *cmd->body_list;
1038
      while (list)
1039
        {
1040
          print_command_line (list, depth + 1, stream);
1041
          list = list->next;
1042
        }
1043
    }
1044
 
1045
  /* An if command.  Recursively print both arms before returning.  */
1046
  if (cmd->control_type == if_control)
1047
    {
1048
      fputs_filtered ("if ", stream);
1049
      fputs_filtered (cmd->line, stream);
1050
      fputs_filtered ("\n", stream);
1051
      /* The true arm. */
1052
      print_command_line (cmd->body_list[0], depth + 1, stream);
1053
 
1054
      /* Show the false arm if it exists.  */
1055
      if (cmd->body_count == 2)
1056
        {
1057
          if (depth)
1058
            {
1059
              for (i = 0; i < depth; i++)
1060
                fputs_filtered ("  ", stream);
1061
            }
1062
          fputs_filtered ("else\n", stream);
1063
          print_command_line (cmd->body_list[1], depth + 1, stream);
1064
        }
1065
      if (depth)
1066
        {
1067
          for (i = 0; i < depth; i++)
1068
            fputs_filtered ("  ", stream);
1069
        }
1070
      fputs_filtered ("end\n", stream);
1071
    }
1072
}
1073
#endif
1074
 
1075
/* Execute the command in CMD.  */
1076
 
1077
enum command_control_type
1078
execute_control_command (cmd)
1079
     struct command_line *cmd;
1080
{
1081
  struct expression *expr;
1082
  struct command_line *current;
1083
  struct cleanup *old_chain = 0;
1084
  value_ptr val;
1085
  value_ptr val_mark;
1086
  int loop;
1087
  enum command_control_type ret;
1088
  char *new_line;
1089
 
1090
  switch (cmd->control_type)
1091
    {
1092
    case simple_control:
1093
      /* A simple command, execute it and return.  */
1094
      new_line = insert_args (cmd->line);
1095
      if (!new_line)
1096
        return invalid_control;
1097
      old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
1098
                                &new_line);
1099
      execute_command (new_line, 0);
1100
      ret = cmd->control_type;
1101
      break;
1102
 
1103
    case continue_control:
1104
    case break_control:
1105
      /* Return for "continue", and "break" so we can either
1106
         continue the loop at the top, or break out.  */
1107
      ret = cmd->control_type;
1108
      break;
1109
 
1110
    case while_control:
1111
      {
1112
        /* Parse the loop control expression for the while statement.  */
1113
        new_line = insert_args (cmd->line);
1114
        if (!new_line)
1115
          return invalid_control;
1116
        old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
1117
                                  &new_line);
1118
        expr = parse_expression (new_line);
1119
        make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1120
 
1121
        ret = simple_control;
1122
        loop = 1;
1123
 
1124
        /* Keep iterating so long as the expression is true.  */
1125
        while (loop == 1)
1126
          {
1127
            int cond_result;
1128
 
1129
            QUIT;
1130
 
1131
            /* Evaluate the expression.  */
1132
            val_mark = value_mark ();
1133
            val = evaluate_expression (expr);
1134
            cond_result = value_true (val);
1135
            value_free_to_mark (val_mark);
1136
 
1137
            /* If the value is false, then break out of the loop.  */
1138
            if (!cond_result)
1139
              break;
1140
 
1141
            /* Execute the body of the while statement.  */
1142
            current = *cmd->body_list;
1143
            while (current)
1144
              {
1145
                ret = execute_control_command (current);
1146
 
1147
                /* If we got an error, or a "break" command, then stop
1148
                   looping.  */
1149
                if (ret == invalid_control || ret == break_control)
1150
                  {
1151
                    loop = 0;
1152
                    break;
1153
                  }
1154
 
1155
                /* If we got a "continue" command, then restart the loop
1156
                   at this point.  */
1157
                if (ret == continue_control)
1158
                  break;
1159
 
1160
                /* Get the next statement.  */
1161
                current = current->next;
1162
              }
1163
          }
1164
 
1165
        /* Reset RET so that we don't recurse the break all the way down.  */
1166
        if (ret == break_control)
1167
          ret = simple_control;
1168
 
1169
        break;
1170
      }
1171
 
1172
    case if_control:
1173
      {
1174
        new_line = insert_args (cmd->line);
1175
        if (!new_line)
1176
          return invalid_control;
1177
        old_chain = make_cleanup ((make_cleanup_func) free_current_contents,
1178
                                  &new_line);
1179
        /* Parse the conditional for the if statement.  */
1180
        expr = parse_expression (new_line);
1181
        make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1182
 
1183
        current = NULL;
1184
        ret = simple_control;
1185
 
1186
        /* Evaluate the conditional.  */
1187
        val_mark = value_mark ();
1188
        val = evaluate_expression (expr);
1189
 
1190
        /* Choose which arm to take commands from based on the value of the
1191
           conditional expression.  */
1192
        if (value_true (val))
1193
          current = *cmd->body_list;
1194
        else if (cmd->body_count == 2)
1195
          current = *(cmd->body_list + 1);
1196
        value_free_to_mark (val_mark);
1197
 
1198
        /* Execute commands in the given arm.  */
1199
        while (current)
1200
          {
1201
            ret = execute_control_command (current);
1202
 
1203
            /* If we got an error, get out.  */
1204
            if (ret != simple_control)
1205
              break;
1206
 
1207
            /* Get the next statement in the body.  */
1208
            current = current->next;
1209
          }
1210
 
1211
        break;
1212
      }
1213
 
1214
    default:
1215
      warning ("Invalid control type in command structure.");
1216
      return invalid_control;
1217
    }
1218
 
1219
  if (old_chain)
1220
    do_cleanups (old_chain);
1221
 
1222
  return ret;
1223
}
1224
 
1225
/* "while" command support.  Executes a body of statements while the
1226
   loop condition is nonzero.  */
1227
 
1228
static void
1229
while_command (arg, from_tty)
1230
     char *arg;
1231
     int from_tty;
1232
{
1233
  struct command_line *command = NULL;
1234
 
1235
  control_level = 1;
1236
  command = get_command_line (while_control, arg);
1237
 
1238
  if (command == NULL)
1239
    return;
1240
 
1241
  execute_control_command (command);
1242
  free_command_lines (&command);
1243
}
1244
 
1245
/* "if" command support.  Execute either the true or false arm depending
1246
   on the value of the if conditional.  */
1247
 
1248
static void
1249
if_command (arg, from_tty)
1250
     char *arg;
1251
     int from_tty;
1252
{
1253
  struct command_line *command = NULL;
1254
 
1255
  control_level = 1;
1256
  command = get_command_line (if_control, arg);
1257
 
1258
  if (command == NULL)
1259
    return;
1260
 
1261
  execute_control_command (command);
1262
  free_command_lines (&command);
1263
}
1264
 
1265
/* Cleanup */
1266
static void
1267
arg_cleanup ()
1268
{
1269
  struct user_args *oargs = user_args;
1270
  if (!user_args)
1271
    internal_error ("Internal error, arg_cleanup called with no user args.\n");
1272
 
1273
  user_args = user_args->next;
1274
  free (oargs);
1275
}
1276
 
1277
/* Bind the incomming arguments for a user defined command to
1278
   $arg0, $arg1 ... $argMAXUSERARGS.  */
1279
 
1280
static struct cleanup *
1281
setup_user_args (p)
1282
     char *p;
1283
{
1284
  struct user_args *args;
1285
  struct cleanup *old_chain;
1286
  unsigned int arg_count = 0;
1287
 
1288
  args = (struct user_args *) xmalloc (sizeof (struct user_args));
1289
  memset (args, 0, sizeof (struct user_args));
1290
 
1291
  args->next = user_args;
1292
  user_args = args;
1293
 
1294
  old_chain = make_cleanup ((make_cleanup_func) arg_cleanup, 0);
1295
 
1296
  if (p == NULL)
1297
    return old_chain;
1298
 
1299
  while (*p)
1300
    {
1301
      char *start_arg;
1302
      int squote = 0;
1303
      int dquote = 0;
1304
      int bsquote = 0;
1305
 
1306
      if (arg_count >= MAXUSERARGS)
1307
        {
1308
          error ("user defined function may only have %d arguments.\n",
1309
                 MAXUSERARGS);
1310
          return old_chain;
1311
        }
1312
 
1313
      /* Strip whitespace.  */
1314
      while (*p == ' ' || *p == '\t')
1315
        p++;
1316
 
1317
      /* P now points to an argument.  */
1318
      start_arg = p;
1319
      user_args->a[arg_count].arg = p;
1320
 
1321
      /* Get to the end of this argument.  */
1322
      while (*p)
1323
        {
1324
          if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
1325
            break;
1326
          else
1327
            {
1328
              if (bsquote)
1329
                bsquote = 0;
1330
              else if (*p == '\\')
1331
                bsquote = 1;
1332
              else if (squote)
1333
                {
1334
                  if (*p == '\'')
1335
                    squote = 0;
1336
                }
1337
              else if (dquote)
1338
                {
1339
                  if (*p == '"')
1340
                    dquote = 0;
1341
                }
1342
              else
1343
                {
1344
                  if (*p == '\'')
1345
                    squote = 1;
1346
                  else if (*p == '"')
1347
                    dquote = 1;
1348
                }
1349
              p++;
1350
            }
1351
        }
1352
 
1353
      user_args->a[arg_count].len = p - start_arg;
1354
      arg_count++;
1355
      user_args->count++;
1356
    }
1357
  return old_chain;
1358
}
1359
 
1360
/* Given character string P, return a point to the first argument ($arg),
1361
   or NULL if P contains no arguments.  */
1362
 
1363
static char *
1364
locate_arg (p)
1365
     char *p;
1366
{
1367
  while ((p = strchr (p, '$')))
1368
    {
1369
      if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
1370
        return p;
1371
      p++;
1372
    }
1373
  return NULL;
1374
}
1375
 
1376
/* Insert the user defined arguments stored in user_arg into the $arg
1377
   arguments found in line, with the updated copy being placed into nline.  */
1378
 
1379
static char *
1380
insert_args (line)
1381
     char *line;
1382
{
1383
  char *p, *save_line, *new_line;
1384
  unsigned len, i;
1385
 
1386
  /* First we need to know how much memory to allocate for the new line.  */
1387
  save_line = line;
1388
  len = 0;
1389
  while ((p = locate_arg (line)))
1390
    {
1391
      len += p - line;
1392
      i = p[4] - '0';
1393
 
1394
      if (i >= user_args->count)
1395
        {
1396
          error ("Missing argument %d in user function.\n", i);
1397
          return NULL;
1398
        }
1399
      len += user_args->a[i].len;
1400
      line = p + 5;
1401
    }
1402
 
1403
  /* Don't forget the tail.  */
1404
  len += strlen (line);
1405
 
1406
  /* Allocate space for the new line and fill it in.  */
1407
  new_line = (char *) xmalloc (len + 1);
1408
  if (new_line == NULL)
1409
    return NULL;
1410
 
1411
  /* Restore pointer to beginning of old line.  */
1412
  line = save_line;
1413
 
1414
  /* Save pointer to beginning of new line.  */
1415
  save_line = new_line;
1416
 
1417
  while ((p = locate_arg (line)))
1418
    {
1419
      int i, len;
1420
 
1421
      memcpy (new_line, line, p - line);
1422
      new_line += p - line;
1423
      i = p[4] - '0';
1424
 
1425
      len = user_args->a[i].len;
1426
      if (len)
1427
        {
1428
          memcpy (new_line, user_args->a[i].arg, len);
1429
          new_line += len;
1430
        }
1431
      line = p + 5;
1432
    }
1433
  /* Don't forget the tail.  */
1434
  strcpy (new_line, line);
1435
 
1436
  /* Return a pointer to the beginning of the new line.  */
1437
  return save_line;
1438
}
1439
 
1440
void
1441
execute_user_command (c, args)
1442
     struct cmd_list_element *c;
1443
     char *args;
1444
{
1445
  register struct command_line *cmdlines;
1446
  struct cleanup *old_chain;
1447
  enum command_control_type ret;
1448
 
1449
  old_chain = setup_user_args (args);
1450
 
1451
  cmdlines = c->user_commands;
1452
  if (cmdlines == 0)
1453
    /* Null command */
1454
    return;
1455
 
1456
  /* Set the instream to 0, indicating execution of a
1457
     user-defined function.  */
1458
  old_chain = make_cleanup ((make_cleanup_func) source_cleanup, instream);
1459
  instream = (FILE *) 0;
1460
  while (cmdlines)
1461
    {
1462
      ret = execute_control_command (cmdlines);
1463
      if (ret != simple_control && ret != break_control)
1464
        {
1465
          warning ("Error in control structure.\n");
1466
          break;
1467
        }
1468
      cmdlines = cmdlines->next;
1469
    }
1470
  do_cleanups (old_chain);
1471
}
1472
 
1473
/* Execute the line P as a command.
1474
   Pass FROM_TTY as second argument to the defining function.  */
1475
 
1476
void
1477
execute_command (p, from_tty)
1478
     char *p;
1479
     int from_tty;
1480
{
1481
  register struct cmd_list_element *c;
1482
  register enum language flang;
1483
  static int warned = 0;
1484
  char *line;
1485
  /* FIXME: These should really be in an appropriate header file */
1486
  extern void serial_log_command PARAMS ((const char *));
1487
 
1488
  free_all_values ();
1489
 
1490
  /* Force cleanup of any alloca areas if using C alloca instead of
1491
     a builtin alloca.  */
1492
  alloca (0);
1493
 
1494
  /* This can happen when command_line_input hits end of file.  */
1495
  if (p == NULL)
1496
    return;
1497
 
1498
  serial_log_command (p);
1499
 
1500
  while (*p == ' ' || *p == '\t')
1501
    p++;
1502
  if (*p)
1503
    {
1504
      char *arg;
1505
      line = p;
1506
 
1507
      c = lookup_cmd (&p, cmdlist, "", 0, 1);
1508
 
1509
      /* If the target is running, we allow only a limited set of
1510
         commands. */
1511
      if (event_loop_p && target_can_async_p () && target_executing)
1512
        if (!strcmp (c->name, "help")
1513
            && !strcmp (c->name, "pwd")
1514
            && !strcmp (c->name, "show")
1515
            && !strcmp (c->name, "stop"))
1516
          error ("Cannot execute this command while the target is running.");
1517
 
1518
      /* Pass null arg rather than an empty one.  */
1519
      arg = *p ? p : 0;
1520
 
1521
      /* Clear off trailing whitespace, except for set and complete command.  */
1522
      if (arg && c->type != set_cmd && c->function.cfunc != complete_command)
1523
        {
1524
          p = arg + strlen (arg) - 1;
1525
          while (p >= arg && (*p == ' ' || *p == '\t'))
1526
            p--;
1527
          *(p + 1) = '\0';
1528
        }
1529
 
1530
      /* If this command has been hooked, run the hook first. */
1531
      if (c->hook)
1532
        execute_user_command (c->hook, (char *) 0);
1533
 
1534
      if (c->flags & DEPRECATED_WARN_USER)
1535
        deprecated_cmd_warning (&line);
1536
 
1537
      if (c->class == class_user)
1538
        execute_user_command (c, arg);
1539
      else if (c->type == set_cmd || c->type == show_cmd)
1540
        do_setshow_command (arg, from_tty & caution, c);
1541
      else if (c->function.cfunc == NO_FUNCTION)
1542
        error ("That is not a command, just a help topic.");
1543
      else if (call_command_hook)
1544
        call_command_hook (c, arg, from_tty & caution);
1545
      else
1546
        (*c->function.cfunc) (arg, from_tty & caution);
1547
    }
1548
 
1549
  /* Tell the user if the language has changed (except first time).  */
1550
  if (current_language != expected_language)
1551
    {
1552
      if (language_mode == language_mode_auto)
1553
        {
1554
          language_info (1);    /* Print what changed.  */
1555
        }
1556
      warned = 0;
1557
    }
1558
 
1559
  /* Warn the user if the working language does not match the
1560
     language of the current frame.  Only warn the user if we are
1561
     actually running the program, i.e. there is a stack. */
1562
  /* FIXME:  This should be cacheing the frame and only running when
1563
     the frame changes.  */
1564
 
1565
  if (target_has_stack)
1566
    {
1567
      flang = get_frame_language ();
1568
      if (!warned
1569
          && flang != language_unknown
1570
          && flang != current_language->la_language)
1571
        {
1572
          printf_filtered ("%s\n", lang_frame_mismatch_warn);
1573
          warned = 1;
1574
        }
1575
    }
1576
}
1577
 
1578
/* ARGSUSED */
1579
/* NOTE 1999-04-29: This function will be static again, once we modify
1580
   gdb to use the event loop as the default command loop and we merge
1581
   event-top.c into this file, top.c */
1582
/* static */ void
1583
command_loop_marker (foo)
1584
     int foo;
1585
{
1586
}
1587
 
1588
/* Read commands from `instream' and execute them
1589
   until end of file or error reading instream.  */
1590
 
1591
void
1592
command_loop ()
1593
{
1594
  struct cleanup *old_chain;
1595
  char *command;
1596
  int stdin_is_tty = ISATTY (stdin);
1597
  long time_at_cmd_start;
1598
#ifdef HAVE_SBRK
1599
  long space_at_cmd_start = 0;
1600
#endif
1601
  extern int display_time;
1602
  extern int display_space;
1603
 
1604
  while (instream && !feof (instream))
1605
    {
1606
#if defined(TUI)
1607
      extern int insert_mode;
1608
#endif
1609
      if (window_hook && instream == stdin)
1610
        (*window_hook) (instream, get_prompt ());
1611
 
1612
      quit_flag = 0;
1613
      if (instream == stdin && stdin_is_tty)
1614
        reinitialize_more_filter ();
1615
      old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
1616
 
1617
#if defined(TUI)
1618
      /* A bit of paranoia: I want to make sure the "insert_mode" global
1619
       * is clear except when it is being used for command-line editing
1620
       * (see tuiIO.c, utils.c); otherwise normal output will
1621
       * get messed up in the TUI. So clear it before/after
1622
       * the command-line-input call. - RT
1623
       */
1624
      insert_mode = 0;
1625
#endif
1626
      /* Get a command-line. This calls the readline package. */
1627
      command = command_line_input (instream == stdin ?
1628
                                    get_prompt () : (char *) NULL,
1629
                                    instream == stdin, "prompt");
1630
#if defined(TUI)
1631
      insert_mode = 0;
1632
#endif
1633
      if (command == 0)
1634
        return;
1635
 
1636
      time_at_cmd_start = get_run_time ();
1637
 
1638
      if (display_space)
1639
        {
1640
#ifdef HAVE_SBRK
1641
          extern char **environ;
1642
          char *lim = (char *) sbrk (0);
1643
 
1644
          space_at_cmd_start = (long) (lim - (char *) &environ);
1645
#endif
1646
        }
1647
 
1648
      execute_command (command, instream == stdin);
1649
      /* Do any commands attached to breakpoint we stopped at.  */
1650
      bpstat_do_actions (&stop_bpstat);
1651
      do_cleanups (old_chain);
1652
 
1653
      if (display_time)
1654
        {
1655
          long cmd_time = get_run_time () - time_at_cmd_start;
1656
 
1657
          printf_unfiltered ("Command execution time: %ld.%06ld\n",
1658
                             cmd_time / 1000000, cmd_time % 1000000);
1659
        }
1660
 
1661
      if (display_space)
1662
        {
1663
#ifdef HAVE_SBRK
1664
          extern char **environ;
1665
          char *lim = (char *) sbrk (0);
1666
          long space_now = lim - (char *) &environ;
1667
          long space_diff = space_now - space_at_cmd_start;
1668
 
1669
          printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
1670
                             space_now,
1671
                             (space_diff >= 0 ? '+' : '-'),
1672
                             space_diff);
1673
#endif
1674
        }
1675
    }
1676
}
1677
 
1678
/* Read commands from `instream' and execute them until end of file or
1679
   error reading instream. This command loop doesnt care about any
1680
   such things as displaying time and space usage. If the user asks
1681
   for those, they won't work. */
1682
void
1683
simplified_command_loop (read_input_func, execute_command_func)
1684
     char *(*read_input_func) (char *);
1685
     void (*execute_command_func) (char *, int);
1686
{
1687
  struct cleanup *old_chain;
1688
  char *command;
1689
  int stdin_is_tty = ISATTY (stdin);
1690
 
1691
  while (instream && !feof (instream))
1692
    {
1693
      quit_flag = 0;
1694
      if (instream == stdin && stdin_is_tty)
1695
        reinitialize_more_filter ();
1696
      old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0);
1697
 
1698
      /* Get a command-line. */
1699
      command = (*read_input_func) (instream == stdin ?
1700
                                    get_prompt () : (char *) NULL);
1701
 
1702
      if (command == 0)
1703
        return;
1704
 
1705
      (*execute_command_func) (command, instream == stdin);
1706
 
1707
      /* Do any commands attached to breakpoint we stopped at.  */
1708
      bpstat_do_actions (&stop_bpstat);
1709
 
1710
      do_cleanups (old_chain);
1711
    }
1712
}
1713
 
1714
/* Commands call this if they do not want to be repeated by null lines.  */
1715
 
1716
void
1717
dont_repeat ()
1718
{
1719
  if (server_command)
1720
    return;
1721
 
1722
  /* If we aren't reading from standard input, we are saving the last
1723
     thing read from stdin in line and don't want to delete it.  Null lines
1724
     won't repeat here in any case.  */
1725
  if (instream == stdin)
1726
    *line = 0;
1727
}
1728
 
1729 362 markom
 
1730 104 markom
/* Read a line from the stream "instream" without command line editing.
1731
 
1732
   It prints PROMPT_ARG once at the start.
1733
   Action is compatible with "readline", e.g. space for the result is
1734
   malloc'd and should be freed by the caller.
1735
 
1736
   A NULL return means end of file.  */
1737
char *
1738
gdb_readline (prompt_arg)
1739
     char *prompt_arg;
1740
{
1741
  int c;
1742
  char *result;
1743
  int input_index = 0;
1744
  int result_size = 80;
1745
 
1746
  if (prompt_arg)
1747
    {
1748
      /* Don't use a _filtered function here.  It causes the assumed
1749
         character position to be off, since the newline we read from
1750
         the user is not accounted for.  */
1751
      fputs_unfiltered (prompt_arg, gdb_stdout);
1752
#ifdef MPW
1753
      /* Move to a new line so the entered line doesn't have a prompt
1754
         on the front of it. */
1755
      fputs_unfiltered ("\n", gdb_stdout);
1756
#endif /* MPW */
1757
      gdb_flush (gdb_stdout);
1758
    }
1759
 
1760
  result = (char *) xmalloc (result_size);
1761
 
1762
  while (1)
1763
    {
1764
      /* Read from stdin if we are executing a user defined command.
1765
         This is the right thing for prompt_for_continue, at least.  */
1766
      c = fgetc (instream ? instream : stdin);
1767
 
1768
      if (c == EOF)
1769
        {
1770
          if (input_index > 0)
1771
            /* The last line does not end with a newline.  Return it, and
1772
               if we are called again fgetc will still return EOF and
1773
               we'll return NULL then.  */
1774
            break;
1775
          free (result);
1776
          return NULL;
1777
        }
1778
 
1779
      if (c == '\n')
1780
#ifndef CRLF_SOURCE_FILES
1781
        break;
1782
#else
1783
        {
1784
          if (input_index > 0 && result[input_index - 1] == '\r')
1785
            input_index--;
1786
          break;
1787
        }
1788
#endif
1789
 
1790
      result[input_index++] = c;
1791
      while (input_index >= result_size)
1792
        {
1793
          result_size *= 2;
1794
          result = (char *) xrealloc (result, result_size);
1795
        }
1796
    }
1797
 
1798
  result[input_index++] = '\0';
1799
  return result;
1800
}
1801
 
1802
/* Variables which control command line editing and history
1803
   substitution.  These variables are given default values at the end
1804
   of this file.  */
1805
static int command_editing_p;
1806
/* NOTE 1999-04-29: This variable will be static again, once we modify
1807
   gdb to use the event loop as the default command loop and we merge
1808
   event-top.c into this file, top.c */
1809
/* static */ int history_expansion_p;
1810
static int write_history_p;
1811
static int history_size;
1812
static char *history_filename;
1813
 
1814
/* readline uses the word breaks for two things:
1815
   (1) In figuring out where to point the TEXT parameter to the
1816
   rl_completion_entry_function.  Since we don't use TEXT for much,
1817
   it doesn't matter a lot what the word breaks are for this purpose, but
1818
   it does affect how much stuff M-? lists.
1819
   (2) If one of the matches contains a word break character, readline
1820
   will quote it.  That's why we switch between
1821
   gdb_completer_word_break_characters and
1822
   gdb_completer_command_word_break_characters.  I'm not sure when
1823
   we need this behavior (perhaps for funky characters in C++ symbols?).  */
1824
 
1825
/* Variables which are necessary for fancy command line editing.  */
1826
char *gdb_completer_word_break_characters =
1827
" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
1828
 
1829
/* When completing on command names, we remove '-' from the list of
1830
   word break characters, since we use it in command names.  If the
1831
   readline library sees one in any of the current completion strings,
1832
   it thinks that the string needs to be quoted and automatically supplies
1833
   a leading quote. */
1834
char *gdb_completer_command_word_break_characters =
1835
" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
1836
 
1837
/* When completing on file names, we remove from the list of word
1838
   break characters any characters that are commonly used in file
1839
   names, such as '-', '+', '~', etc.  Otherwise, readline displays
1840
   incorrect completion candidates.  */
1841
char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?/><";
1842
 
1843
/* Characters that can be used to quote completion strings.  Note that we
1844
   can't include '"' because the gdb C parser treats such quoted sequences
1845
   as strings. */
1846
char *gdb_completer_quote_characters =
1847
"'";
1848
 
1849
/* Functions that are used as part of the fancy command line editing.  */
1850
 
1851
/* This can be used for functions which don't want to complete on symbols
1852
   but don't want to complete on anything else either.  */
1853
/* ARGSUSED */
1854
char **
1855
noop_completer (text, prefix)
1856
     char *text;
1857
     char *prefix;
1858
{
1859
  return NULL;
1860
}
1861
 
1862
/* Complete on filenames.  */
1863
char **
1864
filename_completer (text, word)
1865
     char *text;
1866
     char *word;
1867
{
1868
  /* From readline.  */
1869
  extern char *filename_completion_function PARAMS ((char *, int));
1870
  int subsequent_name;
1871
  char **return_val;
1872
  int return_val_used;
1873
  int return_val_alloced;
1874
 
1875
  return_val_used = 0;
1876
  /* Small for testing.  */
1877
  return_val_alloced = 1;
1878
  return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
1879
 
1880
  subsequent_name = 0;
1881
  while (1)
1882
    {
1883
      char *p;
1884
      p = filename_completion_function (text, subsequent_name);
1885
      if (return_val_used >= return_val_alloced)
1886
        {
1887
          return_val_alloced *= 2;
1888
          return_val =
1889
            (char **) xrealloc (return_val,
1890
                                return_val_alloced * sizeof (char *));
1891
        }
1892
      if (p == NULL)
1893
        {
1894
          return_val[return_val_used++] = p;
1895
          break;
1896
        }
1897
      /* We need to set subsequent_name to a non-zero value before the
1898
         continue line below, because otherwise, if the first file seen
1899
         by GDB is a backup file whose name ends in a `~', we will loop
1900
         indefinitely.  */
1901
      subsequent_name = 1;
1902
      /* Like emacs, don't complete on old versions.  Especially useful
1903
         in the "source" command.  */
1904
      if (p[strlen (p) - 1] == '~')
1905
        continue;
1906
 
1907
      {
1908
        char *q;
1909
        if (word == text)
1910
          /* Return exactly p.  */
1911
          return_val[return_val_used++] = p;
1912
        else if (word > text)
1913
          {
1914
            /* Return some portion of p.  */
1915
            q = xmalloc (strlen (p) + 5);
1916
            strcpy (q, p + (word - text));
1917
            return_val[return_val_used++] = q;
1918
            free (p);
1919
          }
1920
        else
1921
          {
1922
            /* Return some of TEXT plus p.  */
1923
            q = xmalloc (strlen (p) + (text - word) + 5);
1924
            strncpy (q, word, text - word);
1925
            q[text - word] = '\0';
1926
            strcat (q, p);
1927
            return_val[return_val_used++] = q;
1928
            free (p);
1929
          }
1930
      }
1931
    }
1932
#if 0
1933
  /* There is no way to do this just long enough to affect quote inserting
1934
     without also affecting the next completion.  This should be fixed in
1935
     readline.  FIXME.  */
1936
  /* Insure that readline does the right thing
1937
     with respect to inserting quotes.  */
1938
  rl_completer_word_break_characters = "";
1939
#endif
1940
  return return_val;
1941
}
1942
 
1943
/* Here are some useful test cases for completion.  FIXME: These should
1944
   be put in the test suite.  They should be tested with both M-? and TAB.
1945
 
1946
   "show output-" "radix"
1947
   "show output" "-radix"
1948
   "p" ambiguous (commands starting with p--path, print, printf, etc.)
1949
   "p "  ambiguous (all symbols)
1950
   "info t foo" no completions
1951
   "info t " no completions
1952
   "info t" ambiguous ("info target", "info terminal", etc.)
1953
   "info ajksdlfk" no completions
1954
   "info ajksdlfk " no completions
1955
   "info" " "
1956
   "info " ambiguous (all info commands)
1957
   "p \"a" no completions (string constant)
1958
   "p 'a" ambiguous (all symbols starting with a)
1959
   "p b-a" ambiguous (all symbols starting with a)
1960
   "p b-" ambiguous (all symbols)
1961
   "file Make" "file" (word break hard to screw up here)
1962
   "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
1963
 */
1964
 
1965
/* Generate completions one by one for the completer.  Each time we are
1966
   called return another potential completion to the caller.
1967
   line_completion just completes on commands or passes the buck to the
1968
   command's completer function, the stuff specific to symbol completion
1969
   is in make_symbol_completion_list.
1970
 
1971
   TEXT is the caller's idea of the "word" we are looking at.
1972
 
1973
   MATCHES is the number of matches that have currently been collected from
1974
   calling this completion function.  When zero, then we need to initialize,
1975
   otherwise the initialization has already taken place and we can just
1976
   return the next potential completion string.
1977
 
1978
   LINE_BUFFER is available to be looked at; it contains the entire text
1979
   of the line.  POINT is the offset in that line of the cursor.  You
1980
   should pretend that the line ends at POINT.
1981
 
1982
   Returns NULL if there are no more completions, else a pointer to a string
1983
   which is a possible completion, it is the caller's responsibility to
1984
   free the string.  */
1985
 
1986
static char *
1987
line_completion_function (text, matches, line_buffer, point)
1988
     char *text;
1989
     int matches;
1990
     char *line_buffer;
1991
     int point;
1992
{
1993
  static char **list = (char **) NULL;  /* Cache of completions */
1994
  static int index;             /* Next cached completion */
1995
  char *output = NULL;
1996
  char *tmp_command, *p;
1997
  /* Pointer within tmp_command which corresponds to text.  */
1998
  char *word;
1999
  struct cmd_list_element *c, *result_list;
2000
 
2001
  if (matches == 0)
2002
    {
2003
      /* The caller is beginning to accumulate a new set of completions, so
2004
         we need to find all of them now, and cache them for returning one at
2005
         a time on future calls. */
2006
 
2007
      if (list)
2008
        {
2009
          /* Free the storage used by LIST, but not by the strings inside.
2010
             This is because rl_complete_internal () frees the strings. */
2011
          free ((PTR) list);
2012
        }
2013
      list = 0;
2014
      index = 0;
2015
 
2016
      /* Choose the default set of word break characters to break completions.
2017
         If we later find out that we are doing completions on command strings
2018
         (as opposed to strings supplied by the individual command completer
2019
         functions, which can be any string) then we will switch to the
2020
         special word break set for command strings, which leaves out the
2021
         '-' character used in some commands.  */
2022
 
2023
      rl_completer_word_break_characters =
2024
        gdb_completer_word_break_characters;
2025
 
2026
      /* Decide whether to complete on a list of gdb commands or on symbols. */
2027
      tmp_command = (char *) alloca (point + 1);
2028
      p = tmp_command;
2029
 
2030
      strncpy (tmp_command, line_buffer, point);
2031
      tmp_command[point] = '\0';
2032
      /* Since text always contains some number of characters leading up
2033
         to point, we can find the equivalent position in tmp_command
2034
         by subtracting that many characters from the end of tmp_command.  */
2035
      word = tmp_command + point - strlen (text);
2036
 
2037
      if (point == 0)
2038
        {
2039
          /* An empty line we want to consider ambiguous; that is, it
2040
             could be any command.  */
2041
          c = (struct cmd_list_element *) -1;
2042
          result_list = 0;
2043
        }
2044
      else
2045
        {
2046
          c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
2047
        }
2048
 
2049
      /* Move p up to the next interesting thing.  */
2050
      while (*p == ' ' || *p == '\t')
2051
        {
2052
          p++;
2053
        }
2054
 
2055
      if (!c)
2056
        {
2057
          /* It is an unrecognized command.  So there are no
2058
             possible completions.  */
2059
          list = NULL;
2060
        }
2061
      else if (c == (struct cmd_list_element *) -1)
2062
        {
2063
          char *q;
2064
 
2065
          /* lookup_cmd_1 advances p up to the first ambiguous thing, but
2066
             doesn't advance over that thing itself.  Do so now.  */
2067
          q = p;
2068
          while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
2069
            ++q;
2070
          if (q != tmp_command + point)
2071
            {
2072
              /* There is something beyond the ambiguous
2073
                 command, so there are no possible completions.  For
2074
                 example, "info t " or "info t foo" does not complete
2075
                 to anything, because "info t" can be "info target" or
2076
                 "info terminal".  */
2077
              list = NULL;
2078
            }
2079
          else
2080
            {
2081
              /* We're trying to complete on the command which was ambiguous.
2082
                 This we can deal with.  */
2083
              if (result_list)
2084
                {
2085
                  list = complete_on_cmdlist (*result_list->prefixlist, p,
2086
                                              word);
2087
                }
2088
              else
2089
                {
2090
                  list = complete_on_cmdlist (cmdlist, p, word);
2091
                }
2092
              /* Insure that readline does the right thing with respect to
2093
                 inserting quotes.  */
2094
              rl_completer_word_break_characters =
2095
                gdb_completer_command_word_break_characters;
2096
            }
2097
        }
2098
      else
2099
        {
2100
          /* We've recognized a full command.  */
2101
 
2102
          if (p == tmp_command + point)
2103
            {
2104
              /* There is no non-whitespace in the line beyond the command.  */
2105
 
2106
              if (p[-1] == ' ' || p[-1] == '\t')
2107
                {
2108
                  /* The command is followed by whitespace; we need to complete
2109
                     on whatever comes after command.  */
2110
                  if (c->prefixlist)
2111
                    {
2112
                      /* It is a prefix command; what comes after it is
2113
                         a subcommand (e.g. "info ").  */
2114
                      list = complete_on_cmdlist (*c->prefixlist, p, word);
2115
 
2116
                      /* Insure that readline does the right thing
2117
                         with respect to inserting quotes.  */
2118
                      rl_completer_word_break_characters =
2119
                        gdb_completer_command_word_break_characters;
2120
                    }
2121
                  else if (c->enums)
2122
                    {
2123
                      list = complete_on_enum (c->enums, p, word);
2124
                      rl_completer_word_break_characters =
2125
                        gdb_completer_command_word_break_characters;
2126
                    }
2127
                  else
2128
                    {
2129
                      /* It is a normal command; what comes after it is
2130
                         completed by the command's completer function.  */
2131
                      list = (*c->completer) (p, word);
2132
                      if (c->completer == filename_completer)
2133
                        rl_completer_word_break_characters =
2134
                          gdb_completer_file_name_break_characters;
2135
                    }
2136
                }
2137
              else
2138
                {
2139
                  /* The command is not followed by whitespace; we need to
2140
                     complete on the command itself.  e.g. "p" which is a
2141
                     command itself but also can complete to "print", "ptype"
2142
                     etc.  */
2143
                  char *q;
2144
 
2145
                  /* Find the command we are completing on.  */
2146
                  q = p;
2147
                  while (q > tmp_command)
2148
                    {
2149
                      if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
2150
                        --q;
2151
                      else
2152
                        break;
2153
                    }
2154
 
2155
                  list = complete_on_cmdlist (result_list, q, word);
2156
 
2157
                  /* Insure that readline does the right thing
2158
                     with respect to inserting quotes.  */
2159
                  rl_completer_word_break_characters =
2160
                    gdb_completer_command_word_break_characters;
2161
                }
2162
            }
2163
          else
2164
            {
2165
              /* There is non-whitespace beyond the command.  */
2166
 
2167
              if (c->prefixlist && !c->allow_unknown)
2168
                {
2169
                  /* It is an unrecognized subcommand of a prefix command,
2170
                     e.g. "info adsfkdj".  */
2171
                  list = NULL;
2172
                }
2173
              else if (c->enums)
2174
                {
2175
                  list = complete_on_enum (c->enums, p, word);
2176
                }
2177
              else
2178
                {
2179
                  /* It is a normal command.  */
2180
                  list = (*c->completer) (p, word);
2181
                  if (c->completer == filename_completer)
2182
                    rl_completer_word_break_characters =
2183
                      gdb_completer_file_name_break_characters;
2184
                }
2185
            }
2186
        }
2187
    }
2188
 
2189
  /* If we found a list of potential completions during initialization then
2190
     dole them out one at a time.  The vector of completions is NULL
2191
     terminated, so after returning the last one, return NULL (and continue
2192
     to do so) each time we are called after that, until a new list is
2193
     available. */
2194
 
2195
  if (list)
2196
    {
2197
      output = list[index];
2198
      if (output)
2199
        {
2200
          index++;
2201
        }
2202
    }
2203
 
2204
#if 0
2205
  /* Can't do this because readline hasn't yet checked the word breaks
2206
     for figuring out whether to insert a quote.  */
2207
  if (output == NULL)
2208
    /* Make sure the word break characters are set back to normal for the
2209
       next time that readline tries to complete something.  */
2210
    rl_completer_word_break_characters =
2211
      gdb_completer_word_break_characters;
2212
#endif
2213
 
2214
  return (output);
2215
}
2216
 
2217
/* Line completion interface function for readline.  */
2218
 
2219
static char *
2220
readline_line_completion_function (text, matches)
2221
     char *text;
2222
     int matches;
2223
{
2224
  return line_completion_function (text, matches, rl_line_buffer, rl_point);
2225
}
2226
 
2227
/* Skip over a possibly quoted word (as defined by the quote characters
2228
   and word break characters the completer uses).  Returns pointer to the
2229
   location after the "word". */
2230
 
2231
char *
2232
skip_quoted (str)
2233
     char *str;
2234
{
2235
  char quote_char = '\0';
2236
  char *scan;
2237
 
2238
  for (scan = str; *scan != '\0'; scan++)
2239
    {
2240
      if (quote_char != '\0')
2241
        {
2242
          /* Ignore everything until the matching close quote char */
2243
          if (*scan == quote_char)
2244
            {
2245
              /* Found matching close quote. */
2246
              scan++;
2247
              break;
2248
            }
2249
        }
2250
      else if (strchr (gdb_completer_quote_characters, *scan))
2251
        {
2252
          /* Found start of a quoted string. */
2253
          quote_char = *scan;
2254
        }
2255
      else if (strchr (gdb_completer_word_break_characters, *scan))
2256
        {
2257
          break;
2258
        }
2259
    }
2260
  return (scan);
2261
}
2262
 
2263
 
2264
#ifdef STOP_SIGNAL
2265
static void
2266
stop_sig (signo)
2267
     int signo;
2268
{
2269
#if STOP_SIGNAL == SIGTSTP
2270
  signal (SIGTSTP, SIG_DFL);
2271
  sigsetmask (0);
2272
  kill (getpid (), SIGTSTP);
2273
  signal (SIGTSTP, stop_sig);
2274
#else
2275
  signal (STOP_SIGNAL, stop_sig);
2276
#endif
2277
  printf_unfiltered ("%s", get_prompt ());
2278
  gdb_flush (gdb_stdout);
2279
 
2280
  /* Forget about any previous command -- null line now will do nothing.  */
2281
  dont_repeat ();
2282
}
2283
#endif /* STOP_SIGNAL */
2284
 
2285
/* Initialize signal handlers. */
2286
static void
2287
do_nothing (signo)
2288
     int signo;
2289
{
2290
  /* Under System V the default disposition of a signal is reinstated after
2291
     the signal is caught and delivered to an application process.  On such
2292
     systems one must restore the replacement signal handler if one wishes
2293
     to continue handling the signal in one's program.  On BSD systems this
2294
     is not needed but it is harmless, and it simplifies the code to just do
2295
     it unconditionally. */
2296
  signal (signo, do_nothing);
2297
}
2298
 
2299
static void
2300
init_signals ()
2301
{
2302
  signal (SIGINT, request_quit);
2303
 
2304
  /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
2305
     to the inferior and breakpoints will be ignored.  */
2306
#ifdef SIGTRAP
2307
  signal (SIGTRAP, SIG_DFL);
2308
#endif
2309
 
2310
  /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
2311
     passed to the inferior, which we don't want.  It would be
2312
     possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
2313
     on BSD4.3 systems using vfork, that can affect the
2314
     GDB process as well as the inferior (the signal handling tables
2315
     might be in memory, shared between the two).  Since we establish
2316
     a handler for SIGQUIT, when we call exec it will set the signal
2317
     to SIG_DFL for us.  */
2318
  signal (SIGQUIT, do_nothing);
2319
#ifdef SIGHUP
2320
  if (signal (SIGHUP, do_nothing) != SIG_IGN)
2321
    signal (SIGHUP, disconnect);
2322
#endif
2323
  signal (SIGFPE, float_handler);
2324
 
2325
#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
2326
  signal (SIGWINCH, SIGWINCH_HANDLER);
2327
#endif
2328
}
2329
 
2330
/* Read one line from the command input stream `instream'
2331
   into the local static buffer `linebuffer' (whose current length
2332
   is `linelength').
2333
   The buffer is made bigger as necessary.
2334
   Returns the address of the start of the line.
2335
 
2336
   NULL is returned for end of file.
2337
 
2338
   *If* the instream == stdin & stdin is a terminal, the line read
2339
   is copied into the file line saver (global var char *line,
2340
   length linesize) so that it can be duplicated.
2341
 
2342
   This routine either uses fancy command line editing or
2343
   simple input as the user has requested.  */
2344
 
2345
char *
2346
command_line_input (prompt_arg, repeat, annotation_suffix)
2347
     char *prompt_arg;
2348
     int repeat;
2349
     char *annotation_suffix;
2350
{
2351
  static char *linebuffer = 0;
2352
  static unsigned linelength = 0;
2353
  register char *p;
2354
  char *p1;
2355
  char *rl;
2356
  char *local_prompt = prompt_arg;
2357
  char *nline;
2358
  char got_eof = 0;
2359
 
2360
  /* The annotation suffix must be non-NULL.  */
2361
  if (annotation_suffix == NULL)
2362
    annotation_suffix = "";
2363
 
2364
  if (annotation_level > 1 && instream == stdin)
2365
    {
2366
      local_prompt = alloca ((prompt_arg == NULL ? 0 : strlen (prompt_arg))
2367
                             + strlen (annotation_suffix) + 40);
2368
      if (prompt_arg == NULL)
2369
        local_prompt[0] = '\0';
2370
      else
2371
        strcpy (local_prompt, prompt_arg);
2372
      strcat (local_prompt, "\n\032\032");
2373
      strcat (local_prompt, annotation_suffix);
2374
      strcat (local_prompt, "\n");
2375
    }
2376
 
2377
  if (linebuffer == 0)
2378
    {
2379
      linelength = 80;
2380
      linebuffer = (char *) xmalloc (linelength);
2381
    }
2382
 
2383
  p = linebuffer;
2384
 
2385
  /* Control-C quits instantly if typed while in this loop
2386
     since it should not wait until the user types a newline.  */
2387
  immediate_quit++;
2388
#ifdef STOP_SIGNAL
2389
  if (job_control)
2390
    {
2391
      if (event_loop_p)
2392
        signal (STOP_SIGNAL, handle_stop_sig);
2393
      else
2394
        signal (STOP_SIGNAL, stop_sig);
2395
    }
2396
#endif
2397
 
2398
  while (1)
2399
    {
2400
      /* Make sure that all output has been output.  Some machines may let
2401
         you get away with leaving out some of the gdb_flush, but not all.  */
2402
      wrap_here ("");
2403
      gdb_flush (gdb_stdout);
2404
      gdb_flush (gdb_stderr);
2405
 
2406
      if (source_file_name != NULL)
2407
        {
2408
          ++source_line_number;
2409
          sprintf (source_error,
2410
                   "%s%s:%d: Error in sourced command file:\n",
2411
                   source_pre_error,
2412
                   source_file_name,
2413
                   source_line_number);
2414
          error_pre_print = source_error;
2415
        }
2416
 
2417
      if (annotation_level > 1 && instream == stdin)
2418
        {
2419
          printf_unfiltered ("\n\032\032pre-");
2420
          printf_unfiltered (annotation_suffix);
2421
          printf_unfiltered ("\n");
2422
        }
2423
 
2424
      /* Don't use fancy stuff if not talking to stdin.  */
2425
      if (readline_hook && instream == NULL)
2426
        {
2427
          rl = (*readline_hook) (local_prompt);
2428
        }
2429
      else if (command_editing_p && instream == stdin && ISATTY (instream))
2430
        {
2431
          rl = readline (local_prompt);
2432
        }
2433
      else
2434
        {
2435
          rl = gdb_readline (local_prompt);
2436
        }
2437
 
2438
      if (annotation_level > 1 && instream == stdin)
2439
        {
2440
          printf_unfiltered ("\n\032\032post-");
2441
          printf_unfiltered (annotation_suffix);
2442
          printf_unfiltered ("\n");
2443
        }
2444
 
2445
      if (!rl || rl == (char *) EOF)
2446
        {
2447
          got_eof = 1;
2448
          break;
2449
        }
2450
      if (strlen (rl) + 1 + (p - linebuffer) > linelength)
2451
        {
2452
          linelength = strlen (rl) + 1 + (p - linebuffer);
2453
          nline = (char *) xrealloc (linebuffer, linelength);
2454
          p += nline - linebuffer;
2455
          linebuffer = nline;
2456
        }
2457
      p1 = rl;
2458
      /* Copy line.  Don't copy null at end.  (Leaves line alone
2459
         if this was just a newline)  */
2460
      while (*p1)
2461
        *p++ = *p1++;
2462
 
2463
      free (rl);                /* Allocated in readline.  */
2464
 
2465
      if (p == linebuffer || *(p - 1) != '\\')
2466
        break;
2467
 
2468
      p--;                      /* Put on top of '\'.  */
2469
      local_prompt = (char *) 0;
2470
    }
2471
 
2472
#ifdef STOP_SIGNAL
2473
  if (job_control)
2474
    signal (STOP_SIGNAL, SIG_DFL);
2475
#endif
2476
  immediate_quit--;
2477
 
2478
  if (got_eof)
2479
    return NULL;
2480
 
2481
#define SERVER_COMMAND_LENGTH 7
2482
  server_command =
2483
    (p - linebuffer > SERVER_COMMAND_LENGTH)
2484
    && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH);
2485
  if (server_command)
2486
    {
2487
      /* Note that we don't set `line'.  Between this and the check in
2488
         dont_repeat, this insures that repeating will still do the
2489
         right thing.  */
2490
      *p = '\0';
2491
      return linebuffer + SERVER_COMMAND_LENGTH;
2492
    }
2493
 
2494
  /* Do history expansion if that is wished.  */
2495
  if (history_expansion_p && instream == stdin
2496
      && ISATTY (instream))
2497
    {
2498
      char *history_value;
2499
      int expanded;
2500
 
2501
      *p = '\0';                /* Insert null now.  */
2502
      expanded = history_expand (linebuffer, &history_value);
2503
      if (expanded)
2504
        {
2505
          /* Print the changes.  */
2506
          printf_unfiltered ("%s\n", history_value);
2507
 
2508
          /* If there was an error, call this function again.  */
2509
          if (expanded < 0)
2510
            {
2511
              free (history_value);
2512
              return command_line_input (prompt_arg, repeat, annotation_suffix);
2513
            }
2514
          if (strlen (history_value) > linelength)
2515
            {
2516
              linelength = strlen (history_value) + 1;
2517
              linebuffer = (char *) xrealloc (linebuffer, linelength);
2518
            }
2519
          strcpy (linebuffer, history_value);
2520
          p = linebuffer + strlen (linebuffer);
2521
          free (history_value);
2522
        }
2523
    }
2524
 
2525
  /* If we just got an empty line, and that is supposed
2526
     to repeat the previous command, return the value in the
2527
     global buffer.  */
2528
  if (repeat && p == linebuffer)
2529
    return line;
2530
  for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
2531
  if (repeat && !*p1)
2532
    return line;
2533
 
2534
  *p = 0;
2535
 
2536
  /* Add line to history if appropriate.  */
2537
  if (instream == stdin
2538
      && ISATTY (stdin) && *linebuffer)
2539
    add_history (linebuffer);
2540
 
2541
  /* Note: lines consisting solely of comments are added to the command
2542
     history.  This is useful when you type a command, and then
2543
     realize you don't want to execute it quite yet.  You can comment
2544
     out the command and then later fetch it from the value history
2545
     and remove the '#'.  The kill ring is probably better, but some
2546
     people are in the habit of commenting things out.  */
2547
  if (*p1 == '#')
2548
    *p1 = '\0';                 /* Found a comment. */
2549
 
2550
  /* Save into global buffer if appropriate.  */
2551
  if (repeat)
2552
    {
2553
      if (linelength > linesize)
2554
        {
2555
          line = xrealloc (line, linelength);
2556
          linesize = linelength;
2557
        }
2558
      strcpy (line, linebuffer);
2559
      return line;
2560
    }
2561
 
2562
  return linebuffer;
2563
}
2564
 
2565
 
2566
/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
2567
   code bodies.  This is typically used when we encounter an "else"
2568
   clause for an "if" command.  */
2569
 
2570
static void
2571
realloc_body_list (command, new_length)
2572
     struct command_line *command;
2573
     int new_length;
2574
{
2575
  int n;
2576
  struct command_line **body_list;
2577
 
2578
  n = command->body_count;
2579
 
2580
  /* Nothing to do?  */
2581
  if (new_length <= n)
2582
    return;
2583
 
2584
  body_list = (struct command_line **)
2585
    xmalloc (sizeof (struct command_line *) * new_length);
2586
 
2587
  memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
2588
 
2589
  free (command->body_list);
2590
  command->body_list = body_list;
2591
  command->body_count = new_length;
2592
}
2593
 
2594
/* Read one line from the input stream.  If the command is an "else" or
2595
   "end", return such an indication to the caller.  */
2596
 
2597
static enum misc_command_type
2598
read_next_line (command)
2599
     struct command_line **command;
2600
{
2601
  char *p, *p1, *prompt_ptr, control_prompt[256];
2602
  int i = 0;
2603
 
2604
  if (control_level >= 254)
2605
    error ("Control nesting too deep!\n");
2606
 
2607
  /* Set a prompt based on the nesting of the control commands.  */
2608
  if (instream == stdin || (instream == 0 && readline_hook != NULL))
2609
    {
2610
      for (i = 0; i < control_level; i++)
2611
        control_prompt[i] = ' ';
2612
      control_prompt[i] = '>';
2613
      control_prompt[i + 1] = '\0';
2614
      prompt_ptr = (char *) &control_prompt[0];
2615
    }
2616
  else
2617
    prompt_ptr = NULL;
2618
 
2619
  p = command_line_input (prompt_ptr, instream == stdin, "commands");
2620
 
2621
  /* Not sure what to do here.  */
2622
  if (p == NULL)
2623
    return end_command;
2624
 
2625
  /* Strip leading and trailing whitespace.  */
2626
  while (*p == ' ' || *p == '\t')
2627
    p++;
2628
 
2629
  p1 = p + strlen (p);
2630
  while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
2631
    p1--;
2632
 
2633
  /* Blanks and comments don't really do anything, but we need to
2634
     distinguish them from else, end and other commands which can be
2635
     executed.  */
2636
  if (p1 == p || p[0] == '#')
2637
    return nop_command;
2638
 
2639
  /* Is this the end of a simple, while, or if control structure?  */
2640
  if (p1 - p == 3 && !strncmp (p, "end", 3))
2641
    return end_command;
2642
 
2643
  /* Is the else clause of an if control structure?  */
2644
  if (p1 - p == 4 && !strncmp (p, "else", 4))
2645
    return else_command;
2646
 
2647
  /* Check for while, if, break, continue, etc and build a new command
2648
     line structure for them.  */
2649
  if (p1 - p > 5 && !strncmp (p, "while", 5))
2650
    *command = build_command_line (while_control, p + 6);
2651
  else if (p1 - p > 2 && !strncmp (p, "if", 2))
2652
    *command = build_command_line (if_control, p + 3);
2653
  else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
2654
    {
2655
      *command = (struct command_line *)
2656
        xmalloc (sizeof (struct command_line));
2657
      (*command)->next = NULL;
2658
      (*command)->line = NULL;
2659
      (*command)->control_type = break_control;
2660
      (*command)->body_count = 0;
2661
      (*command)->body_list = NULL;
2662
    }
2663
  else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
2664
    {
2665
      *command = (struct command_line *)
2666
        xmalloc (sizeof (struct command_line));
2667
      (*command)->next = NULL;
2668
      (*command)->line = NULL;
2669
      (*command)->control_type = continue_control;
2670
      (*command)->body_count = 0;
2671
      (*command)->body_list = NULL;
2672
    }
2673
  else
2674
    {
2675
      /* A normal command.  */
2676
      *command = (struct command_line *)
2677
        xmalloc (sizeof (struct command_line));
2678
      (*command)->next = NULL;
2679
      (*command)->line = savestring (p, p1 - p);
2680
      (*command)->control_type = simple_control;
2681
      (*command)->body_count = 0;
2682
      (*command)->body_list = NULL;
2683
    }
2684
 
2685
  /* Nothing special.  */
2686
  return ok_command;
2687
}
2688
 
2689
/* Recursively read in the control structures and create a command_line
2690
   structure from them.
2691
 
2692
   The parent_control parameter is the control structure in which the
2693
   following commands are nested.  */
2694
 
2695
static enum command_control_type
2696
recurse_read_control_structure (current_cmd)
2697
     struct command_line *current_cmd;
2698
{
2699
  int current_body, i;
2700
  enum misc_command_type val;
2701
  enum command_control_type ret;
2702
  struct command_line **body_ptr, *child_tail, *next;
2703
 
2704
  child_tail = NULL;
2705
  current_body = 1;
2706
 
2707
  /* Sanity checks.  */
2708
  if (current_cmd->control_type == simple_control)
2709
    {
2710
      error ("Recursed on a simple control type\n");
2711
      return invalid_control;
2712
    }
2713
 
2714
  if (current_body > current_cmd->body_count)
2715
    {
2716
      error ("Allocated body is smaller than this command type needs\n");
2717
      return invalid_control;
2718
    }
2719
 
2720
  /* Read lines from the input stream and build control structures.  */
2721
  while (1)
2722
    {
2723
      dont_repeat ();
2724
 
2725
      next = NULL;
2726
      val = read_next_line (&next);
2727
 
2728
      /* Just skip blanks and comments.  */
2729
      if (val == nop_command)
2730
        continue;
2731
 
2732
      if (val == end_command)
2733
        {
2734
          if (current_cmd->control_type == while_control
2735
              || current_cmd->control_type == if_control)
2736
            {
2737
              /* Success reading an entire control structure.  */
2738
              ret = simple_control;
2739
              break;
2740
            }
2741
          else
2742
            {
2743
              ret = invalid_control;
2744
              break;
2745
            }
2746
        }
2747
 
2748
      /* Not the end of a control structure.  */
2749
      if (val == else_command)
2750
        {
2751
          if (current_cmd->control_type == if_control
2752
              && current_body == 1)
2753
            {
2754
              realloc_body_list (current_cmd, 2);
2755
              current_body = 2;
2756
              child_tail = NULL;
2757
              continue;
2758
            }
2759
          else
2760
            {
2761
              ret = invalid_control;
2762
              break;
2763
            }
2764
        }
2765
 
2766
      if (child_tail)
2767
        {
2768
          child_tail->next = next;
2769
        }
2770
      else
2771
        {
2772
          body_ptr = current_cmd->body_list;
2773
          for (i = 1; i < current_body; i++)
2774
            body_ptr++;
2775
 
2776
          *body_ptr = next;
2777
 
2778
        }
2779
 
2780
      child_tail = next;
2781
 
2782
      /* If the latest line is another control structure, then recurse
2783
         on it.  */
2784
      if (next->control_type == while_control
2785
          || next->control_type == if_control)
2786
        {
2787
          control_level++;
2788
          ret = recurse_read_control_structure (next);
2789
          control_level--;
2790
 
2791
          if (ret != simple_control)
2792
            break;
2793
        }
2794
    }
2795
 
2796
  dont_repeat ();
2797
 
2798
  return ret;
2799
}
2800
 
2801
/* Read lines from the input stream and accumulate them in a chain of
2802
   struct command_line's, which is then returned.  For input from a
2803
   terminal, the special command "end" is used to mark the end of the
2804
   input, and is not included in the returned chain of commands. */
2805
 
2806
#define END_MESSAGE "End with a line saying just \"end\"."
2807
 
2808
struct command_line *
2809
read_command_lines (prompt_arg, from_tty)
2810
     char *prompt_arg;
2811
     int from_tty;
2812
{
2813
  struct command_line *head, *tail, *next;
2814
  struct cleanup *old_chain;
2815
  enum command_control_type ret;
2816
  enum misc_command_type val;
2817
 
2818
  control_level = 0;
2819
  if (readline_begin_hook)
2820
    {
2821
      /* Note - intentional to merge messages with no newline */
2822
      (*readline_begin_hook) ("%s  %s\n", prompt_arg, END_MESSAGE);
2823
    }
2824
  else if (from_tty && input_from_terminal_p ())
2825
    {
2826
      printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
2827
      gdb_flush (gdb_stdout);
2828
    }
2829
 
2830
  head = tail = NULL;
2831
  old_chain = NULL;
2832
 
2833
  while (1)
2834
    {
2835
      val = read_next_line (&next);
2836
 
2837
      /* Ignore blank lines or comments.  */
2838
      if (val == nop_command)
2839
        continue;
2840
 
2841
      if (val == end_command)
2842
        {
2843
          ret = simple_control;
2844
          break;
2845
        }
2846
 
2847
      if (val != ok_command)
2848
        {
2849
          ret = invalid_control;
2850
          break;
2851
        }
2852
 
2853
      if (next->control_type == while_control
2854
          || next->control_type == if_control)
2855
        {
2856
          control_level++;
2857
          ret = recurse_read_control_structure (next);
2858
          control_level--;
2859
 
2860
          if (ret == invalid_control)
2861
            break;
2862
        }
2863
 
2864
      if (tail)
2865
        {
2866
          tail->next = next;
2867
        }
2868
      else
2869
        {
2870
          head = next;
2871
          old_chain = make_cleanup ((make_cleanup_func) free_command_lines,
2872
                                    &head);
2873
        }
2874
      tail = next;
2875
    }
2876
 
2877
  dont_repeat ();
2878
 
2879
  if (head)
2880
    {
2881
      if (ret != invalid_control)
2882
        {
2883
          discard_cleanups (old_chain);
2884
        }
2885
      else
2886
        do_cleanups (old_chain);
2887
    }
2888
 
2889
  if (readline_end_hook)
2890
    {
2891
      (*readline_end_hook) ();
2892
    }
2893
  return (head);
2894
}
2895
 
2896
/* Free a chain of struct command_line's.  */
2897
 
2898
void
2899
free_command_lines (lptr)
2900
     struct command_line **lptr;
2901
{
2902
  register struct command_line *l = *lptr;
2903
  register struct command_line *next;
2904
  struct command_line **blist;
2905
  int i;
2906
 
2907
  while (l)
2908
    {
2909
      if (l->body_count > 0)
2910
        {
2911
          blist = l->body_list;
2912
          for (i = 0; i < l->body_count; i++, blist++)
2913
            free_command_lines (blist);
2914
        }
2915
      next = l->next;
2916
      free (l->line);
2917
      free ((PTR) l);
2918
      l = next;
2919
    }
2920
}
2921
 
2922
/* Add an element to the list of info subcommands.  */
2923
 
2924
struct cmd_list_element *
2925
add_info (name, fun, doc)
2926
     char *name;
2927
     void (*fun) PARAMS ((char *, int));
2928
     char *doc;
2929
{
2930
  return add_cmd (name, no_class, fun, doc, &infolist);
2931
}
2932
 
2933
/* Add an alias to the list of info subcommands.  */
2934
 
2935
struct cmd_list_element *
2936
add_info_alias (name, oldname, abbrev_flag)
2937
     char *name;
2938
     char *oldname;
2939
     int abbrev_flag;
2940
{
2941
  return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
2942
}
2943
 
2944
/* The "info" command is defined as a prefix, with allow_unknown = 0.
2945
   Therefore, its own definition is called only for "info" with no args.  */
2946
 
2947
/* ARGSUSED */
2948
static void
2949
info_command (arg, from_tty)
2950
     char *arg;
2951
     int from_tty;
2952
{
2953
  printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
2954
  help_list (infolist, "info ", -1, gdb_stdout);
2955
}
2956
 
2957
/* The "complete" command is used by Emacs to implement completion.  */
2958
 
2959
/* ARGSUSED */
2960
static void
2961
complete_command (arg, from_tty)
2962
     char *arg;
2963
     int from_tty;
2964
{
2965
  int i;
2966
  int argpoint;
2967
  char *completion;
2968
 
2969
  dont_repeat ();
2970
 
2971
  if (arg == NULL)
2972
    arg = "";
2973
  argpoint = strlen (arg);
2974
 
2975
  for (completion = line_completion_function (arg, i = 0, arg, argpoint);
2976
       completion;
2977
       completion = line_completion_function (arg, ++i, arg, argpoint))
2978
    {
2979
      printf_unfiltered ("%s\n", completion);
2980
      free (completion);
2981
    }
2982
}
2983
 
2984
/* The "show" command with no arguments shows all the settings.  */
2985
 
2986
/* ARGSUSED */
2987
static void
2988
show_command (arg, from_tty)
2989
     char *arg;
2990
     int from_tty;
2991
{
2992
  cmd_show_list (showlist, from_tty, "");
2993
}
2994
 
2995
/* Add an element to the list of commands.  */
2996
 
2997
struct cmd_list_element *
2998
add_com (name, class, fun, doc)
2999
     char *name;
3000
     enum command_class class;
3001
     void (*fun) PARAMS ((char *, int));
3002
     char *doc;
3003
{
3004
  return add_cmd (name, class, fun, doc, &cmdlist);
3005
}
3006
 
3007
/* Add an alias or abbreviation command to the list of commands.  */
3008
 
3009
struct cmd_list_element *
3010
add_com_alias (name, oldname, class, abbrev_flag)
3011
     char *name;
3012
     char *oldname;
3013
     enum command_class class;
3014
     int abbrev_flag;
3015
{
3016
  return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
3017
}
3018
 
3019
void
3020
error_no_arg (why)
3021
     char *why;
3022
{
3023
  error ("Argument required (%s).", why);
3024
}
3025
 
3026
/* ARGSUSED */
3027
static void
3028
help_command (command, from_tty)
3029
     char *command;
3030
     int from_tty;              /* Ignored */
3031
{
3032
  help_cmd (command, gdb_stdout);
3033
}
3034
 
3035
static void
3036
validate_comname (comname)
3037
     char *comname;
3038
{
3039
  register char *p;
3040
 
3041
  if (comname == 0)
3042
    error_no_arg ("name of command to define");
3043
 
3044
  p = comname;
3045
  while (*p)
3046
    {
3047
      if (!isalnum (*p) && *p != '-' && *p != '_')
3048
        error ("Junk in argument list: \"%s\"", p);
3049
      p++;
3050
    }
3051
}
3052
 
3053
/* This is just a placeholder in the command data structures.  */
3054
static void
3055
user_defined_command (ignore, from_tty)
3056
     char *ignore;
3057
     int from_tty;
3058
{
3059
}
3060
 
3061
static void
3062
define_command (comname, from_tty)
3063
     char *comname;
3064
     int from_tty;
3065
{
3066
  register struct command_line *cmds;
3067
  register struct cmd_list_element *c, *newc, *hookc = 0;
3068
  char *tem = comname;
3069
  char tmpbuf[128];
3070
#define HOOK_STRING     "hook-"
3071
#define HOOK_LEN 5
3072
 
3073
  validate_comname (comname);
3074
 
3075
  /* Look it up, and verify that we got an exact match.  */
3076
  c = lookup_cmd (&tem, cmdlist, "", -1, 1);
3077
  if (c && !STREQ (comname, c->name))
3078
    c = 0;
3079
 
3080
  if (c)
3081
    {
3082
      if (c->class == class_user || c->class == class_alias)
3083
        tem = "Redefine command \"%s\"? ";
3084
      else
3085
        tem = "Really redefine built-in command \"%s\"? ";
3086
      if (!query (tem, c->name))
3087
        error ("Command \"%s\" not redefined.", c->name);
3088
    }
3089
 
3090
  /* If this new command is a hook, then mark the command which it
3091
     is hooking.  Note that we allow hooking `help' commands, so that
3092
     we can hook the `stop' pseudo-command.  */
3093
 
3094
  if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
3095
    {
3096
      /* Look up cmd it hooks, and verify that we got an exact match.  */
3097
      tem = comname + HOOK_LEN;
3098
      hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
3099
      if (hookc && !STREQ (comname + HOOK_LEN, hookc->name))
3100
        hookc = 0;
3101
      if (!hookc)
3102
        {
3103
          warning ("Your new `%s' command does not hook any existing command.",
3104
                   comname);
3105
          if (!query ("Proceed? "))
3106
            error ("Not confirmed.");
3107
        }
3108
    }
3109
 
3110
  comname = savestring (comname, strlen (comname));
3111
 
3112
  /* If the rest of the commands will be case insensitive, this one
3113
     should behave in the same manner. */
3114
  for (tem = comname; *tem; tem++)
3115
    if (isupper (*tem))
3116
      *tem = tolower (*tem);
3117
 
3118
  sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
3119
  cmds = read_command_lines (tmpbuf, from_tty);
3120
 
3121
  if (c && c->class == class_user)
3122
    free_command_lines (&c->user_commands);
3123
 
3124
  newc = add_cmd (comname, class_user, user_defined_command,
3125
                  (c && c->class == class_user)
3126
                  ? c->doc : savestring ("User-defined.", 13), &cmdlist);
3127
  newc->user_commands = cmds;
3128
 
3129
  /* If this new command is a hook, then mark both commands as being
3130
     tied.  */
3131
  if (hookc)
3132
    {
3133
      hookc->hook = newc;       /* Target gets hooked.  */
3134
      newc->hookee = hookc;     /* We are marked as hooking target cmd.  */
3135
    }
3136
}
3137
 
3138
static void
3139
document_command (comname, from_tty)
3140
     char *comname;
3141
     int from_tty;
3142
{
3143
  struct command_line *doclines;
3144
  register struct cmd_list_element *c;
3145
  char *tem = comname;
3146
  char tmpbuf[128];
3147
 
3148
  validate_comname (comname);
3149
 
3150
  c = lookup_cmd (&tem, cmdlist, "", 0, 1);
3151
 
3152
  if (c->class != class_user)
3153
    error ("Command \"%s\" is built-in.", comname);
3154
 
3155
  sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
3156
  doclines = read_command_lines (tmpbuf, from_tty);
3157
 
3158
  if (c->doc)
3159
    free (c->doc);
3160
 
3161
  {
3162
    register struct command_line *cl1;
3163
    register int len = 0;
3164
 
3165
    for (cl1 = doclines; cl1; cl1 = cl1->next)
3166
      len += strlen (cl1->line) + 1;
3167
 
3168
    c->doc = (char *) xmalloc (len + 1);
3169
    *c->doc = 0;
3170
 
3171
    for (cl1 = doclines; cl1; cl1 = cl1->next)
3172
      {
3173
        strcat (c->doc, cl1->line);
3174
        if (cl1->next)
3175
          strcat (c->doc, "\n");
3176
      }
3177
  }
3178
 
3179
  free_command_lines (&doclines);
3180
}
3181
 
3182
/* Print the GDB banner. */
3183
void
3184
print_gdb_version (stream)
3185
     struct ui_file *stream;
3186
{
3187
  /* From GNU coding standards, first line is meant to be easy for a
3188
     program to parse, and is just canonical program name and version
3189
     number, which starts after last space. */
3190
 
3191
#ifdef UI_OUT
3192
  /* Print it console style until a format is defined */
3193
  fprintf_filtered (stream, "GNU gdb %s (UI_OUT)\n", version);
3194
#else
3195
  fprintf_filtered (stream, "GNU gdb %s\n", version);
3196
#endif
3197
 
3198
  /* Second line is a copyright notice. */
3199
 
3200
  fprintf_filtered (stream, "Copyright 2000 Free Software Foundation, Inc.\n");
3201
 
3202
  /* Following the copyright is a brief statement that the program is
3203
     free software, that users are free to copy and change it on
3204
     certain conditions, that it is covered by the GNU GPL, and that
3205
     there is no warranty. */
3206
 
3207
  fprintf_filtered (stream, "\
3208
GDB is free software, covered by the GNU General Public License, and you are\n\
3209
welcome to change it and/or distribute copies of it under certain conditions.\n\
3210
Type \"show copying\" to see the conditions.\n\
3211
There is absolutely no warranty for GDB.  Type \"show warranty\" for details.\n");
3212
 
3213
  /* After the required info we print the configuration information. */
3214
 
3215
  fprintf_filtered (stream, "This GDB was configured as \"");
3216
  if (!STREQ (host_name, target_name))
3217
    {
3218
      fprintf_filtered (stream, "--host=%s --target=%s", host_name, target_name);
3219
    }
3220
  else
3221
    {
3222
      fprintf_filtered (stream, "%s", host_name);
3223
    }
3224
  fprintf_filtered (stream, "\".");
3225
}
3226
 
3227
/* ARGSUSED */
3228
static void
3229
show_version (args, from_tty)
3230
     char *args;
3231
     int from_tty;
3232
{
3233
  immediate_quit++;
3234
  print_gdb_version (gdb_stdout);
3235
  printf_filtered ("\n");
3236
  immediate_quit--;
3237
}
3238
 
3239
/* get_prompt: access method for the GDB prompt string.  */
3240
 
3241
#define MAX_PROMPT_SIZE 256
3242
 
3243
/*
3244
 * int get_prompt_1 (char * buf);
3245
 *
3246
 * Work-horse for get_prompt (called via catch_errors).
3247
 * Argument is buffer to hold the formatted prompt.
3248
 *
3249
 * Returns: 1 for success (use formatted prompt)
3250
 *          0 for failure (use gdb_prompt_string).
3251
 */
3252
 
3253
static int gdb_prompt_escape;
3254
 
3255
static int
3256
get_prompt_1 (formatted_prompt)
3257
     char *formatted_prompt;
3258
{
3259
  char *local_prompt;
3260
 
3261
  if (event_loop_p)
3262
    local_prompt = PROMPT (0);
3263
  else
3264
    local_prompt = gdb_prompt_string;
3265
 
3266
 
3267
  if (gdb_prompt_escape == 0)
3268
    {
3269
      return 0;                  /* do no formatting */
3270
    }
3271
  else
3272
    /* formatted prompt */
3273
    {
3274
      char fmt[40], *promptp, *outp, *tmp;
3275
      value_ptr arg_val;
3276
      DOUBLEST doubleval;
3277
      LONGEST longval;
3278
      CORE_ADDR addrval;
3279
 
3280
      int i, len;
3281
      struct type *arg_type, *elt_type;
3282
 
3283
      promptp = local_prompt;
3284
      outp = formatted_prompt;
3285
 
3286
      while (*promptp != '\0')
3287
        {
3288
          int available = MAX_PROMPT_SIZE - (outp - formatted_prompt) - 1;
3289
 
3290
          if (*promptp != gdb_prompt_escape)
3291
            {
3292
              if (available >= 1)       /* overflow protect */
3293
                *outp++ = *promptp++;
3294
            }
3295
          else
3296
            {
3297
              /* GDB prompt string contains escape char.  Parse for arg.
3298
                 Two consecutive escape chars followed by arg followed by
3299
                 a comma means to insert the arg using a default format.
3300
                 Otherwise a printf format string may be included between
3301
                 the two escape chars.  eg:
3302
                 %%foo, insert foo using default format
3303
                 %2.2f%foo,     insert foo using "%2.2f" format
3304
                 A mismatch between the format string and the data type
3305
                 of "foo" is an error (which we don't know how to protect
3306
                 against).  */
3307
 
3308
              fmt[0] = '\0';     /* assume null format string */
3309
              if (promptp[1] == gdb_prompt_escape)      /* double esc char */
3310
                {
3311
                  promptp += 2; /* skip past two escape chars. */
3312
                }
3313
              else
3314
                {
3315
                  /* extract format string from between two esc chars */
3316
                  i = 0;
3317
                  do
3318
                    {
3319
                      fmt[i++] = *promptp++;    /* copy format string */
3320
                    }
3321
                  while (i < sizeof (fmt) - 1 &&
3322
                         *promptp != gdb_prompt_escape &&
3323
                         *promptp != '\0');
3324
 
3325
                  if (*promptp != gdb_prompt_escape)
3326
                    error ("Syntax error at prompt position %d",
3327
                           promptp - local_prompt);
3328
                  else
3329
                    {
3330
                      promptp++;        /* skip second escape char */
3331
                      fmt[i++] = '\0';  /* terminate the format string */
3332
                    }
3333
                }
3334
 
3335
              arg_val = parse_to_comma_and_eval (&promptp);
3336
              if (*promptp == ',')
3337
                promptp++;      /* skip past the comma */
3338
              arg_type = check_typedef (VALUE_TYPE (arg_val));
3339
              switch (TYPE_CODE (arg_type))
3340
                {
3341
                case TYPE_CODE_ARRAY:
3342
                  elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
3343
                  if (TYPE_LENGTH (arg_type) > 0 &&
3344
                      TYPE_LENGTH (elt_type) == 1 &&
3345
                      TYPE_CODE (elt_type) == TYPE_CODE_INT)
3346
                    {
3347
                      int len = TYPE_LENGTH (arg_type);
3348
 
3349
                      if (VALUE_LAZY (arg_val))
3350
                        value_fetch_lazy (arg_val);
3351
                      tmp = VALUE_CONTENTS (arg_val);
3352
 
3353
                      if (len > available)
3354
                        len = available;        /* overflow protect */
3355
 
3356
                      /* FIXME: how to protect GDB from crashing
3357
                         from bad user-supplied format string? */
3358
                      if (fmt[0] != 0)
3359
                        sprintf (outp, fmt, tmp);
3360
                      else
3361
                        strncpy (outp, tmp, len);
3362
                      outp[len] = '\0';
3363
                    }
3364
                  break;
3365
                case TYPE_CODE_PTR:
3366
                  elt_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
3367
                  addrval = value_as_pointer (arg_val);
3368
 
3369
                  if (TYPE_LENGTH (elt_type) == 1 &&
3370
                      TYPE_CODE (elt_type) == TYPE_CODE_INT &&
3371
                      addrval != 0)
3372
                    {
3373
                      /* display it as a string */
3374
                      char *default_fmt = "%s";
3375
                      char *tmp;
3376
                      int err = 0;
3377
 
3378
                      /* Limiting the number of bytes that the following call
3379
                         will read protects us from sprintf overflow later. */
3380
                      i = target_read_string (addrval,  /* src */
3381
                                              &tmp,     /* dest */
3382
                                              available,        /* len */
3383
                                              &err);
3384
                      if (err)  /* read failed */
3385
                        error ("%s on target_read", safe_strerror (err));
3386
 
3387
                      tmp[i] = '\0';    /* force-terminate string */
3388
                      /* FIXME: how to protect GDB from crashing
3389
                         from bad user-supplied format string? */
3390
                      sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3391
                               tmp);
3392
                      free (tmp);
3393
                    }
3394
                  else
3395
                    {
3396
                      /* display it as a pointer */
3397
                      char *default_fmt = "0x%x";
3398
 
3399
                      /* FIXME: how to protect GDB from crashing
3400
                         from bad user-supplied format string? */
3401
                      if (available >= 16 /*? */ )      /* overflow protect */
3402
                        sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3403
                                 (long) addrval);
3404
                    }
3405
                  break;
3406
                case TYPE_CODE_FLT:
3407
                  {
3408
                    char *default_fmt = "%g";
3409
 
3410
                    doubleval = value_as_double (arg_val);
3411
                    /* FIXME: how to protect GDB from crashing
3412
                       from bad user-supplied format string? */
3413
                    if (available >= 16 /*? */ )        /* overflow protect */
3414
                      sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3415
                               (double) doubleval);
3416
                    break;
3417
                  }
3418
                case TYPE_CODE_INT:
3419
                  {
3420
                    char *default_fmt = "%d";
3421
 
3422
                    longval = value_as_long (arg_val);
3423
                    /* FIXME: how to protect GDB from crashing
3424
                       from bad user-supplied format string? */
3425
                    if (available >= 16 /*? */ )        /* overflow protect */
3426
                      sprintf (outp, fmt[0] == 0 ? default_fmt : fmt,
3427
                               (long) longval);
3428
                    break;
3429
                  }
3430
                case TYPE_CODE_BOOL:
3431
                  {
3432
                    /* no default format for bool */
3433
                    longval = value_as_long (arg_val);
3434
                    if (available >= 8 /*? */ )         /* overflow protect */
3435
                      {
3436
                        if (longval)
3437
                          strcpy (outp, "<true>");
3438
                        else
3439
                          strcpy (outp, "<false>");
3440
                      }
3441
                    break;
3442
                  }
3443
                case TYPE_CODE_ENUM:
3444
                  {
3445
                    /* no default format for enum */
3446
                    longval = value_as_long (arg_val);
3447
                    len = TYPE_NFIELDS (arg_type);
3448
                    /* find enum name if possible */
3449
                    for (i = 0; i < len; i++)
3450
                      if (TYPE_FIELD_BITPOS (arg_type, i) == longval)
3451
                        break;  /* match -- end loop */
3452
 
3453
                    if (i < len)        /* enum name found */
3454
                      {
3455
                        char *name = TYPE_FIELD_NAME (arg_type, i);
3456
 
3457
                        strncpy (outp, name, available);
3458
                        /* in casel available < strlen (name), */
3459
                        outp[available] = '\0';
3460
                      }
3461
                    else
3462
                      {
3463
                        if (available >= 16 /*? */ )    /* overflow protect */
3464
                          sprintf (outp, "%ld", (long) longval);
3465
                      }
3466
                    break;
3467
                  }
3468
                case TYPE_CODE_VOID:
3469
                  *outp = '\0';
3470
                  break;        /* void type -- no output */
3471
                default:
3472
                  error ("bad data type at prompt position %d",
3473
                         promptp - local_prompt);
3474
                  break;
3475
                }
3476
              outp += strlen (outp);
3477
            }
3478
        }
3479
      *outp++ = '\0';           /* terminate prompt string */
3480
      return 1;
3481
    }
3482
}
3483
 
3484
char *
3485
get_prompt ()
3486
{
3487
  static char buf[MAX_PROMPT_SIZE];
3488
 
3489
  if (catch_errors (get_prompt_1, buf, "bad formatted prompt: ",
3490
                    RETURN_MASK_ALL))
3491
    {
3492
      return &buf[0];            /* successful formatted prompt */
3493
    }
3494
  else
3495
    {
3496
      /* Prompt could not be formatted.  */
3497
      if (event_loop_p)
3498
        return PROMPT (0);
3499
      else
3500
        return gdb_prompt_string;
3501
    }
3502
}
3503
 
3504
void
3505
set_prompt (s)
3506
     char *s;
3507
{
3508
/* ??rehrauer: I don't know why this fails, since it looks as though
3509
   assignments to prompt are wrapped in calls to savestring...
3510
   if (prompt != NULL)
3511
   free (prompt);
3512
 */
3513
  if (event_loop_p)
3514
    PROMPT (0) = savestring (s, strlen (s));
3515
  else
3516
    gdb_prompt_string = savestring (s, strlen (s));
3517
}
3518
 
3519
 
3520
/* If necessary, make the user confirm that we should quit.  Return
3521
   non-zero if we should quit, zero if we shouldn't.  */
3522
 
3523
int
3524
quit_confirm ()
3525
{
3526
  if (inferior_pid != 0 && target_has_execution)
3527
    {
3528
      char *s;
3529
 
3530
      /* This is something of a hack.  But there's no reliable way to
3531
         see if a GUI is running.  The `use_windows' variable doesn't
3532
         cut it.  */
3533
      if (init_ui_hook)
3534
        s = "A debugging session is active.\nDo you still want to close the debugger?";
3535
      else if (attach_flag)
3536
        s = "The program is running.  Quit anyway (and detach it)? ";
3537
      else
3538
        s = "The program is running.  Exit anyway? ";
3539
 
3540
      if (!query (s))
3541
        return 0;
3542
    }
3543
 
3544
  return 1;
3545
}
3546
 
3547
/* Quit without asking for confirmation.  */
3548
 
3549
void
3550
quit_force (args, from_tty)
3551
     char *args;
3552
     int from_tty;
3553
{
3554
  int exit_code = 0;
3555
 
3556
  /* An optional expression may be used to cause gdb to terminate with the
3557
     value of that expression. */
3558
  if (args)
3559
    {
3560
      value_ptr val = parse_and_eval (args);
3561
 
3562
      exit_code = (int) value_as_long (val);
3563
    }
3564
 
3565
  if (inferior_pid != 0 && target_has_execution)
3566
    {
3567
      if (attach_flag)
3568
        target_detach (args, from_tty);
3569
      else
3570
        target_kill ();
3571
    }
3572
 
3573
  /* UDI wants this, to kill the TIP.  */
3574
  target_close (1);
3575
 
3576
  /* Save the history information if it is appropriate to do so.  */
3577
  if (write_history_p && history_filename)
3578
    write_history (history_filename);
3579
 
3580
  do_final_cleanups (ALL_CLEANUPS);     /* Do any final cleanups before exiting */
3581
 
3582
#if defined(TUI)
3583
  /* tuiDo((TuiOpaqueFuncPtr)tuiCleanUp); */
3584
  /* The above does not need to be inside a tuiDo(), since
3585
   * it is not manipulating the curses screen, but rather,
3586
   * it is tearing it down.
3587
   */
3588
  if (tui_version)
3589
    tuiCleanUp ();
3590
#endif
3591
 
3592
  exit (exit_code);
3593
}
3594
 
3595
/* Handle the quit command.  */
3596
 
3597
void
3598
quit_command (args, from_tty)
3599
     char *args;
3600
     int from_tty;
3601
{
3602
  if (!quit_confirm ())
3603
    error ("Not confirmed.");
3604
  quit_force (args, from_tty);
3605
}
3606
 
3607
/* Returns whether GDB is running on a terminal and whether the user
3608
   desires that questions be asked of them on that terminal.  */
3609
 
3610
int
3611
input_from_terminal_p ()
3612
{
3613
  return gdb_has_a_terminal () && (instream == stdin) & caution;
3614
}
3615
 
3616
/* ARGSUSED */
3617
static void
3618
pwd_command (args, from_tty)
3619
     char *args;
3620
     int from_tty;
3621
{
3622
  if (args)
3623
    error ("The \"pwd\" command does not take an argument: %s", args);
3624
  getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3625
 
3626
  if (!STREQ (gdb_dirbuf, current_directory))
3627
    printf_unfiltered ("Working directory %s\n (canonically %s).\n",
3628
                       current_directory, gdb_dirbuf);
3629
  else
3630
    printf_unfiltered ("Working directory %s.\n", current_directory);
3631
}
3632
 
3633
void
3634
cd_command (dir, from_tty)
3635
     char *dir;
3636
     int from_tty;
3637
{
3638
  int len;
3639
  /* Found something other than leading repetitions of "/..".  */
3640
  int found_real_path;
3641
  char *p;
3642
 
3643
  /* If the new directory is absolute, repeat is a no-op; if relative,
3644
     repeat might be useful but is more likely to be a mistake.  */
3645
  dont_repeat ();
3646
 
3647
  if (dir == 0)
3648
    error_no_arg ("new working directory");
3649
 
3650
  dir = tilde_expand (dir);
3651
  make_cleanup (free, dir);
3652
 
3653
  if (chdir (dir) < 0)
3654
    perror_with_name (dir);
3655
 
3656
#if defined(_WIN32) || defined(__MSDOS__)
3657
  /* There's too much mess with DOSish names like "d:", "d:.",
3658
     "d:./foo" etc.  Instead of having lots of special #ifdef'ed code,
3659
     simply get the canonicalized name of the current directory.  */
3660
  dir = getcwd (gdb_dirbuf, sizeof (gdb_dirbuf));
3661
#endif
3662
 
3663
  len = strlen (dir);
3664
  if (SLASH_P (dir[len - 1]))
3665
    {
3666
      /* Remove the trailing slash unless this is a root directory
3667
         (including a drive letter on non-Unix systems).  */
3668
      if (!(len == 1)           /* "/" */
3669
#if defined(_WIN32) || defined(__MSDOS__)
3670
          && !(!SLASH_P (*dir) && ROOTED_P (dir) && len <= 3)   /* "d:/" */
3671
#endif
3672
          )
3673
        len--;
3674
    }
3675
 
3676
  dir = savestring (dir, len);
3677
  if (ROOTED_P (dir))
3678
    current_directory = dir;
3679
  else
3680
    {
3681
      if (SLASH_P (current_directory[strlen (current_directory) - 1]))
3682
        current_directory = concat (current_directory, dir, NULL);
3683
      else
3684
        current_directory = concat (current_directory, SLASH_STRING, dir, NULL);
3685
      free (dir);
3686
    }
3687
 
3688
  /* Now simplify any occurrences of `.' and `..' in the pathname.  */
3689
 
3690
  found_real_path = 0;
3691
  for (p = current_directory; *p;)
3692
    {
3693
      if (SLASH_P (p[0]) && p[1] == '.' && (p[2] == 0 || SLASH_P (p[2])))
3694
        strcpy (p, p + 2);
3695
      else if (SLASH_P (p[0]) && p[1] == '.' && p[2] == '.'
3696
               && (p[3] == 0 || SLASH_P (p[3])))
3697
        {
3698
          if (found_real_path)
3699
            {
3700
              /* Search backwards for the directory just before the "/.."
3701
                 and obliterate it and the "/..".  */
3702
              char *q = p;
3703
              while (q != current_directory && !SLASH_P (q[-1]))
3704
                --q;
3705
 
3706
              if (q == current_directory)
3707
                /* current_directory is
3708
                   a relative pathname ("can't happen"--leave it alone).  */
3709
                ++p;
3710
              else
3711
                {
3712
                  strcpy (q - 1, p + 3);
3713
                  p = q - 1;
3714
                }
3715
            }
3716
          else
3717
            /* We are dealing with leading repetitions of "/..", for example
3718
               "/../..", which is the Mach super-root.  */
3719
            p += 3;
3720
        }
3721
      else
3722
        {
3723
          found_real_path = 1;
3724
          ++p;
3725
        }
3726
    }
3727
 
3728
  forget_cached_source_info ();
3729
 
3730
  if (from_tty)
3731
    pwd_command ((char *) 0, 1);
3732
}
3733
 
3734
struct source_cleanup_lines_args
3735
{
3736
  int old_line;
3737
  char *old_file;
3738
  char *old_pre_error;
3739
  char *old_error_pre_print;
3740
};
3741
 
3742
static void
3743
source_cleanup_lines (args)
3744
     PTR args;
3745
{
3746
  struct source_cleanup_lines_args *p =
3747
  (struct source_cleanup_lines_args *) args;
3748
  source_line_number = p->old_line;
3749
  source_file_name = p->old_file;
3750
  source_pre_error = p->old_pre_error;
3751
  error_pre_print = p->old_error_pre_print;
3752
}
3753
 
3754
/* ARGSUSED */
3755
void
3756
source_command (args, from_tty)
3757
     char *args;
3758
     int from_tty;
3759
{
3760
  FILE *stream;
3761
  struct cleanup *old_cleanups;
3762
  char *file = args;
3763
  struct source_cleanup_lines_args old_lines;
3764
  int needed_length;
3765
 
3766
  if (file == NULL)
3767
    {
3768
      error ("source command requires pathname of file to source.");
3769
    }
3770
 
3771
  file = tilde_expand (file);
3772
  old_cleanups = make_cleanup (free, file);
3773
 
3774
  stream = fopen (file, FOPEN_RT);
3775
  if (!stream)
3776
    {
3777
      if (from_tty)
3778
        perror_with_name (file);
3779
      else
3780
        return;
3781
    }
3782
 
3783
  make_cleanup ((make_cleanup_func) fclose, stream);
3784
 
3785
  old_lines.old_line = source_line_number;
3786
  old_lines.old_file = source_file_name;
3787
  old_lines.old_pre_error = source_pre_error;
3788
  old_lines.old_error_pre_print = error_pre_print;
3789
  make_cleanup (source_cleanup_lines, &old_lines);
3790
  source_line_number = 0;
3791
  source_file_name = file;
3792
  source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
3793
  source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
3794
  make_cleanup (free, source_pre_error);
3795
  /* This will get set every time we read a line.  So it won't stay "" for
3796
     long.  */
3797
  error_pre_print = "";
3798
 
3799
  needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
3800
  if (source_error_allocated < needed_length)
3801
    {
3802
      source_error_allocated *= 2;
3803
      if (source_error_allocated < needed_length)
3804
        source_error_allocated = needed_length;
3805
      if (source_error == NULL)
3806
        source_error = xmalloc (source_error_allocated);
3807
      else
3808
        source_error = xrealloc (source_error, source_error_allocated);
3809
    }
3810
 
3811
  read_command_file (stream);
3812
 
3813
  do_cleanups (old_cleanups);
3814
}
3815
 
3816
/* ARGSUSED */
3817
static void
3818
echo_command (text, from_tty)
3819
     char *text;
3820
     int from_tty;
3821
{
3822
  char *p = text;
3823
  register int c;
3824
 
3825
  if (text)
3826
    while ((c = *p++) != '\0')
3827
      {
3828
        if (c == '\\')
3829
          {
3830
            /* \ at end of argument is used after spaces
3831
               so they won't be lost.  */
3832
            if (*p == 0)
3833
              return;
3834
 
3835
            c = parse_escape (&p);
3836
            if (c >= 0)
3837
              printf_filtered ("%c", c);
3838
          }
3839
        else
3840
          printf_filtered ("%c", c);
3841
      }
3842
 
3843
  /* Force this output to appear now.  */
3844
  wrap_here ("");
3845
  gdb_flush (gdb_stdout);
3846
}
3847
 
3848
/* ARGSUSED */
3849
static void
3850
dont_repeat_command (ignored, from_tty)
3851
     char *ignored;
3852
     int from_tty;
3853
{
3854
  *line = 0;                     /* Can't call dont_repeat here because we're not
3855
                                   necessarily reading from stdin.  */
3856
}
3857
 
3858
/* Functions to manipulate command line editing control variables.  */
3859
 
3860
/* Number of commands to print in each call to show_commands.  */
3861
#define Hist_print 10
3862
static void
3863
show_commands (args, from_tty)
3864
     char *args;
3865
     int from_tty;
3866
{
3867
  /* Index for history commands.  Relative to history_base.  */
3868
  int offset;
3869
 
3870
  /* Number of the history entry which we are planning to display next.
3871
     Relative to history_base.  */
3872
  static int num = 0;
3873
 
3874
  /* The first command in the history which doesn't exist (i.e. one more
3875
     than the number of the last command).  Relative to history_base.  */
3876
  int hist_len;
3877
 
3878
  extern HIST_ENTRY *history_get PARAMS ((int));
3879
 
3880
  /* Print out some of the commands from the command history.  */
3881
  /* First determine the length of the history list.  */
3882
  hist_len = history_size;
3883
  for (offset = 0; offset < history_size; offset++)
3884
    {
3885
      if (!history_get (history_base + offset))
3886
        {
3887
          hist_len = offset;
3888
          break;
3889
        }
3890
    }
3891
 
3892
  if (args)
3893
    {
3894
      if (args[0] == '+' && args[1] == '\0')
3895
        /* "info editing +" should print from the stored position.  */
3896
        ;
3897
      else
3898
        /* "info editing <exp>" should print around command number <exp>.  */
3899
        num = (parse_and_eval_address (args) - history_base) - Hist_print / 2;
3900
    }
3901
  /* "show commands" means print the last Hist_print commands.  */
3902
  else
3903
    {
3904
      num = hist_len - Hist_print;
3905
    }
3906
 
3907
  if (num < 0)
3908
    num = 0;
3909
 
3910
  /* If there are at least Hist_print commands, we want to display the last
3911
     Hist_print rather than, say, the last 6.  */
3912
  if (hist_len - num < Hist_print)
3913
    {
3914
      num = hist_len - Hist_print;
3915
      if (num < 0)
3916
        num = 0;
3917
    }
3918
 
3919
  for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
3920
    {
3921
      printf_filtered ("%5d  %s\n", history_base + offset,
3922
                       (history_get (history_base + offset))->line);
3923
    }
3924
 
3925
  /* The next command we want to display is the next one that we haven't
3926
     displayed yet.  */
3927
  num += Hist_print;
3928
 
3929
  /* If the user repeats this command with return, it should do what
3930
     "show commands +" does.  This is unnecessary if arg is null,
3931
     because "show commands +" is not useful after "show commands".  */
3932
  if (from_tty && args)
3933
    {
3934
      args[0] = '+';
3935
      args[1] = '\0';
3936
    }
3937
}
3938
 
3939
/* Called by do_setshow_command.  */
3940
/* ARGSUSED */
3941
static void
3942
set_history_size_command (args, from_tty, c)
3943
     char *args;
3944
     int from_tty;
3945
     struct cmd_list_element *c;
3946
{
3947
  if (history_size == INT_MAX)
3948
    unstifle_history ();
3949
  else if (history_size >= 0)
3950
    stifle_history (history_size);
3951
  else
3952
    {
3953
      history_size = INT_MAX;
3954
      error ("History size must be non-negative");
3955
    }
3956
}
3957
 
3958
/* ARGSUSED */
3959
static void
3960
set_history (args, from_tty)
3961
     char *args;
3962
     int from_tty;
3963
{
3964
  printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n");
3965
  help_list (sethistlist, "set history ", -1, gdb_stdout);
3966
}
3967
 
3968
/* ARGSUSED */
3969
static void
3970
show_history (args, from_tty)
3971
     char *args;
3972
     int from_tty;
3973
{
3974
  cmd_show_list (showhistlist, from_tty, "");
3975
}
3976
 
3977
int info_verbose = 0;            /* Default verbose msgs off */
3978
 
3979
/* Called by do_setshow_command.  An elaborate joke.  */
3980
/* ARGSUSED */
3981
static void
3982
set_verbose (args, from_tty, c)
3983
     char *args;
3984
     int from_tty;
3985
     struct cmd_list_element *c;
3986
{
3987
  char *cmdname = "verbose";
3988
  struct cmd_list_element *showcmd;
3989
 
3990
  showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1);
3991
 
3992
  if (info_verbose)
3993
    {
3994
      c->doc = "Set verbose printing of informational messages.";
3995
      showcmd->doc = "Show verbose printing of informational messages.";
3996
    }
3997
  else
3998
    {
3999
      c->doc = "Set verbosity.";
4000
      showcmd->doc = "Show verbosity.";
4001
    }
4002
}
4003
 
4004
static void
4005
float_handler (signo)
4006
     int signo;
4007
{
4008
  /* This message is based on ANSI C, section 4.7.  Note that integer
4009
     divide by zero causes this, so "float" is a misnomer.  */
4010
  signal (SIGFPE, float_handler);
4011
  error ("Erroneous arithmetic operation.");
4012
}
4013
 
4014
static void
4015
set_debug (arg, from_tty)
4016
     char *arg;
4017
     int from_tty;
4018
{
4019
  printf_unfiltered ("\"set debug\" must be followed by the name of a print subcommand.\n");
4020
  help_list (setdebuglist, "set debug ", -1, gdb_stdout);
4021
}
4022
 
4023
static void
4024
show_debug (args, from_tty)
4025
     char *args;
4026
     int from_tty;
4027
{
4028
  cmd_show_list (showdebuglist, from_tty, "");
4029
}
4030
 
4031
static void
4032
init_cmd_lists ()
4033
{
4034
  cmdlist = NULL;
4035
  infolist = NULL;
4036
  enablelist = NULL;
4037
  disablelist = NULL;
4038
  togglelist = NULL;
4039
  stoplist = NULL;
4040
  deletelist = NULL;
4041
  enablebreaklist = NULL;
4042
  setlist = NULL;
4043
  unsetlist = NULL;
4044
  showlist = NULL;
4045
  sethistlist = NULL;
4046
  showhistlist = NULL;
4047
  unsethistlist = NULL;
4048
  maintenancelist = NULL;
4049
  maintenanceinfolist = NULL;
4050
  maintenanceprintlist = NULL;
4051
  setprintlist = NULL;
4052
  showprintlist = NULL;
4053
  setchecklist = NULL;
4054
  showchecklist = NULL;
4055
}
4056
 
4057
/* Init the history buffer.  Note that we are called after the init file(s)
4058
 * have been read so that the user can change the history file via his
4059
 * .gdbinit file (for instance).  The GDBHISTFILE environment variable
4060
 * overrides all of this.
4061
 */
4062
 
4063
void
4064
init_history ()
4065
{
4066
  char *tmpenv;
4067
 
4068
  tmpenv = getenv ("HISTSIZE");
4069
  if (tmpenv)
4070
    history_size = atoi (tmpenv);
4071
  else if (!history_size)
4072
    history_size = 256;
4073
 
4074
  stifle_history (history_size);
4075
 
4076
  tmpenv = getenv ("GDBHISTFILE");
4077
  if (tmpenv)
4078
    history_filename = savestring (tmpenv, strlen (tmpenv));
4079
  else if (!history_filename)
4080
    {
4081
      /* We include the current directory so that if the user changes
4082
         directories the file written will be the same as the one
4083
         that was read.  */
4084
#ifdef __MSDOS__
4085
      /* No leading dots in file names are allowed on MSDOS.  */
4086
      history_filename = concat (current_directory, "/_gdb_history", NULL);
4087
#else
4088
      history_filename = concat (current_directory, "/.gdb_history", NULL);
4089
#endif
4090
    }
4091
  read_history (history_filename);
4092
}
4093
 
4094
static void
4095
init_main ()
4096
{
4097
  struct cmd_list_element *c;
4098
 
4099
  /* If we are running the asynchronous version,
4100
     we initialize the prompts differently. */
4101
  if (!event_loop_p)
4102
    {
4103
      gdb_prompt_string = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
4104
    }
4105
  else
4106
    {
4107
      /* initialize the prompt stack to a simple "(gdb) " prompt or to
4108
         whatever the DEFAULT_PROMPT is. */
4109
      the_prompts.top = 0;
4110
      PREFIX (0) = "";
4111
      PROMPT (0) = savestring (DEFAULT_PROMPT, strlen (DEFAULT_PROMPT));
4112
      SUFFIX (0) = "";
4113
      /* Set things up for annotation_level > 1, if the user ever decides
4114
         to use it. */
4115
      async_annotation_suffix = "prompt";
4116
      /* Set the variable associated with the setshow prompt command. */
4117
      new_async_prompt = savestring (PROMPT (0), strlen (PROMPT (0)));
4118
    }
4119
  gdb_prompt_escape = 0; /* default to none.  */
4120
 
4121
  /* Set the important stuff up for command editing.  */
4122
  command_editing_p = 1;
4123
  history_expansion_p = 0;
4124
  write_history_p = 0;
4125
 
4126
  /* Setup important stuff for command line editing.  */
4127
  rl_completion_entry_function = (int (*)()) readline_line_completion_function;
4128
  rl_completer_word_break_characters = gdb_completer_word_break_characters;
4129
  rl_completer_quote_characters = gdb_completer_quote_characters;
4130
  rl_readline_name = "gdb";
4131
 
4132
  /* Define the classes of commands.
4133
     They will appear in the help list in the reverse of this order.  */
4134
 
4135
  add_cmd ("internals", class_maintenance, NO_FUNCTION,
4136
           "Maintenance commands.\n\
4137
Some gdb commands are provided just for use by gdb maintainers.\n\
4138
These commands are subject to frequent change, and may not be as\n\
4139
well documented as user commands.",
4140
           &cmdlist);
4141
  add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist);
4142
  add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist);
4143
  add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\
4144
The commands in this class are those defined by the user.\n\
4145
Use the \"define\" command to define a command.", &cmdlist);
4146
  add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist);
4147
  if (!dbx_commands)
4148
    add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist);
4149
  add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist);
4150
  add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist);
4151
  add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist);
4152
  add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\
4153
The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
4154
counting from zero for the innermost (currently executing) frame.\n\n\
4155
At any time gdb identifies one frame as the \"selected\" frame.\n\
4156
Variable lookups are done with respect to the selected frame.\n\
4157
When the program being debugged stops, gdb selects the innermost frame.\n\
4158
The commands below can be used to select other frames by number or address.",
4159
           &cmdlist);
4160
  add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist);
4161
 
4162
  add_com ("pwd", class_files, pwd_command,
4163
        "Print working directory.  This is used for your program as well.");
4164
  c = add_cmd ("cd", class_files, cd_command,
4165
               "Set working directory to DIR for debugger and program being debugged.\n\
4166
The change does not take effect for the program being debugged\n\
4167
until the next time it is started.", &cmdlist);
4168
  c->completer = filename_completer;
4169
 
4170
  /* The set prompt command is different depending whether or not the
4171
     async version is run. NOTE: this difference is going to
4172
     disappear as we make the event loop be the default engine of
4173
     gdb. */
4174
  if (!event_loop_p)
4175
    {
4176
      add_show_from_set
4177
        (add_set_cmd ("prompt", class_support, var_string,
4178
                      (char *) &gdb_prompt_string, "Set gdb's prompt",
4179
                      &setlist),
4180
         &showlist);
4181
    }
4182
  else
4183
    {
4184
      c = add_set_cmd ("prompt", class_support, var_string,
4185
                       (char *) &new_async_prompt, "Set gdb's prompt",
4186
                       &setlist);
4187
      add_show_from_set (c, &showlist);
4188
      c->function.sfunc = set_async_prompt;
4189
    }
4190
 
4191
  add_show_from_set
4192
    (add_set_cmd ("prompt-escape-char", class_support, var_zinteger,
4193
                  (char *) &gdb_prompt_escape,
4194
                  "Set escape character for formatting of gdb's prompt",
4195
                  &setlist),
4196
     &showlist);
4197
 
4198
  add_com ("echo", class_support, echo_command,
4199
           "Print a constant string.  Give string as argument.\n\
4200
C escape sequences may be used in the argument.\n\
4201
No newline is added at the end of the argument;\n\
4202
use \"\\n\" if you want a newline to be printed.\n\
4203
Since leading and trailing whitespace are ignored in command arguments,\n\
4204
if you want to print some you must use \"\\\" before leading whitespace\n\
4205
to be printed or after trailing whitespace.");
4206
  add_com ("document", class_support, document_command,
4207
           "Document a user-defined command.\n\
4208
Give command name as argument.  Give documentation on following lines.\n\
4209
End with a line of just \"end\".");
4210
  add_com ("define", class_support, define_command,
4211
           "Define a new command name.  Command name is argument.\n\
4212
Definition appears on following lines, one command per line.\n\
4213
End with a line of just \"end\".\n\
4214
Use the \"document\" command to give documentation for the new command.\n\
4215
Commands defined in this way may have up to ten arguments.");
4216
 
4217
#ifdef __STDC__
4218
  c = add_cmd ("source", class_support, source_command,
4219
               "Read commands from a file named FILE.\n\
4220
Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\
4221
when gdb is started.", &cmdlist);
4222
#else
4223
  /* Punt file name, we can't help it easily.  */
4224
  c = add_cmd ("source", class_support, source_command,
4225
               "Read commands from a file named FILE.\n\
4226
Note that the file \".gdbinit\" is read automatically in this way\n\
4227
when gdb is started.", &cmdlist);
4228
#endif
4229
  c->completer = filename_completer;
4230
 
4231
  add_com ("quit", class_support, quit_command, "Exit gdb.");
4232
  add_com ("help", class_support, help_command, "Print list of commands.");
4233
  add_com_alias ("q", "quit", class_support, 1);
4234
  add_com_alias ("h", "help", class_support, 1);
4235
 
4236
  add_com ("dont-repeat", class_support, dont_repeat_command, "Don't repeat this command.\n\
4237
Primarily used inside of user-defined commands that should not be repeated when\n\
4238
hitting return.");
4239
 
4240
  c = add_set_cmd ("verbose", class_support, var_boolean, (char *) &info_verbose,
4241
                   "Set ",
4242
                   &setlist),
4243
    add_show_from_set (c, &showlist);
4244
  c->function.sfunc = set_verbose;
4245
  set_verbose (NULL, 0, c);
4246
 
4247
  /* The set editing command is different depending whether or not the
4248
     async version is run. NOTE: this difference is going to disappear
4249
     as we make the event loop be the default engine of gdb. */
4250
  if (!event_loop_p)
4251
    {
4252
      add_show_from_set
4253
        (add_set_cmd ("editing", class_support, var_boolean, (char *) &command_editing_p,
4254
                      "Set editing of command lines as they are typed.\n\
4255
Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4256
Without an argument, command line editing is enabled.  To edit, use\n\
4257
EMACS-like or VI-like commands like control-P or ESC.", &setlist),
4258
         &showlist);
4259
    }
4260
  else
4261
    {
4262
      c = add_set_cmd ("editing", class_support, var_boolean, (char *) &async_command_editing_p,
4263
                       "Set editing of command lines as they are typed.\n\
4264
Use \"on\" to enable the editing, and \"off\" to disable it.\n\
4265
Without an argument, command line editing is enabled.  To edit, use\n\
4266
EMACS-like or VI-like commands like control-P or ESC.", &setlist);
4267
 
4268
      add_show_from_set (c, &showlist);
4269
      c->function.sfunc = set_async_editing_command;
4270
    }
4271
 
4272
  add_prefix_cmd ("history", class_support, set_history,
4273
                  "Generic command for setting command history parameters.",
4274
                  &sethistlist, "set history ", 0, &setlist);
4275
  add_prefix_cmd ("history", class_support, show_history,
4276
                  "Generic command for showing command history parameters.",
4277
                  &showhistlist, "show history ", 0, &showlist);
4278
 
4279
  add_show_from_set
4280
    (add_set_cmd ("expansion", no_class, var_boolean, (char *) &history_expansion_p,
4281
                  "Set history expansion on command input.\n\
4282
Without an argument, history expansion is enabled.", &sethistlist),
4283
     &showhistlist);
4284
 
4285
  add_show_from_set
4286
    (add_set_cmd ("save", no_class, var_boolean, (char *) &write_history_p,
4287
                  "Set saving of the history record on exit.\n\
4288
Use \"on\" to enable the saving, and \"off\" to disable it.\n\
4289
Without an argument, saving is enabled.", &sethistlist),
4290
     &showhistlist);
4291
 
4292
  c = add_set_cmd ("size", no_class, var_integer, (char *) &history_size,
4293
                   "Set the size of the command history, \n\
4294
ie. the number of previous commands to keep a record of.", &sethistlist);
4295
  add_show_from_set (c, &showhistlist);
4296
  c->function.sfunc = set_history_size_command;
4297
 
4298
  add_show_from_set
4299
    (add_set_cmd ("filename", no_class, var_filename, (char *) &history_filename,
4300
                  "Set the filename in which to record the command history\n\
4301
 (the list of previous commands of which a record is kept).", &sethistlist),
4302
     &showhistlist);
4303
 
4304
  add_show_from_set
4305
    (add_set_cmd ("confirm", class_support, var_boolean,
4306
                  (char *) &caution,
4307
                  "Set whether to confirm potentially dangerous operations.",
4308
                  &setlist),
4309
     &showlist);
4310
 
4311
  add_prefix_cmd ("info", class_info, info_command,
4312
     "Generic command for showing things about the program being debugged.",
4313
                  &infolist, "info ", 0, &cmdlist);
4314
  add_com_alias ("i", "info", class_info, 1);
4315
 
4316
  add_com ("complete", class_obscure, complete_command,
4317
           "List the completions for the rest of the line as a command.");
4318
 
4319
  add_prefix_cmd ("show", class_info, show_command,
4320
                  "Generic command for showing things about the debugger.",
4321
                  &showlist, "show ", 0, &cmdlist);
4322
  /* Another way to get at the same thing.  */
4323
  add_info ("set", show_command, "Show all GDB settings.");
4324
 
4325
  add_cmd ("commands", no_class, show_commands,
4326
           "Show the history of commands you typed.\n\
4327
You can supply a command number to start with, or a `+' to start after\n\
4328
the previous command number shown.",
4329
           &showlist);
4330
 
4331
  add_cmd ("version", no_class, show_version,
4332
           "Show what version of GDB this is.", &showlist);
4333
 
4334
  add_com ("while", class_support, while_command,
4335
           "Execute nested commands WHILE the conditional expression is non zero.\n\
4336
The conditional expression must follow the word `while' and must in turn be\n\
4337
followed by a new line.  The nested commands must be entered one per line,\n\
4338
and should be terminated by the word `end'.");
4339
 
4340
  add_com ("if", class_support, if_command,
4341
           "Execute nested commands once IF the conditional expression is non zero.\n\
4342
The conditional expression must follow the word `if' and must in turn be\n\
4343
followed by a new line.  The nested commands must be entered one per line,\n\
4344
and should be terminated by the word 'else' or `end'.  If an else clause\n\
4345
is used, the same rules apply to its nested commands as to the first ones.");
4346
 
4347
  /* If target is open when baud changes, it doesn't take effect until the
4348
     next open (I think, not sure).  */
4349
  add_show_from_set (add_set_cmd ("remotebaud", no_class,
4350
                                  var_zinteger, (char *) &baud_rate,
4351
                                  "Set baud rate for remote serial I/O.\n\
4352
This value is used to set the speed of the serial port when debugging\n\
4353
using remote targets.", &setlist),
4354
                     &showlist);
4355
 
4356
  c = add_set_cmd ("remotedebug", no_class, var_zinteger,
4357
                   (char *) &remote_debug,
4358
                   "Set debugging of remote protocol.\n\
4359
When enabled, each packet sent or received with the remote target\n\
4360
is displayed.", &setlist);
4361
  deprecate_cmd (c, "set debug remote");
4362
  deprecate_cmd (add_show_from_set (c, &showlist), "show debug remote");
4363
 
4364
  add_show_from_set (add_set_cmd ("remote", no_class, var_zinteger,
4365
                                  (char *) &remote_debug,
4366
                                  "Set debugging of remote protocol.\n\
4367
When enabled, each packet sent or received with the remote target\n\
4368
is displayed.", &setdebuglist),
4369
                     &showdebuglist);
4370
 
4371
  add_show_from_set (
4372
                      add_set_cmd ("remotetimeout", no_class, var_integer, (char *) &remote_timeout,
4373
                                   "Set timeout limit to wait for target to respond.\n\
4374
This value is used to set the time limit for gdb to wait for a response\n\
4375
from the target.", &setlist),
4376
                      &showlist);
4377
 
4378
  /* The set annotate command is different depending whether or not
4379
     the async version is run. NOTE: this difference is going to
4380
     disappear as we make the event loop be the default engine of
4381
     gdb. */
4382
  if (!event_loop_p)
4383
    {
4384
      c = add_set_cmd ("annotate", class_obscure, var_zinteger,
4385
                       (char *) &annotation_level, "Set annotation_level.\n\
4386
 
4387
2 == output annotated suitably for use by programs that control GDB.",
4388
                       &setlist);
4389
      c = add_show_from_set (c, &showlist);
4390
    }
4391
  else
4392
    {
4393
      c = add_set_cmd ("annotate", class_obscure, var_zinteger,
4394
                       (char *) &annotation_level, "Set annotation_level.\n\
4395
 
4396
2 == output annotated suitably for use by programs that control GDB.",
4397
                       &setlist);
4398
      add_show_from_set (c, &showlist);
4399
      c->function.sfunc = set_async_annotation_level;
4400
    }
4401
  if (event_loop_p)
4402
    {
4403
      add_show_from_set
4404
        (add_set_cmd ("exec-done-display", class_support, var_boolean, (char *) &exec_done_display_p,
4405
                      "Set notification of completion for asynchronous execution commands.\n\
4406
Use \"on\" to enable the notification, and \"off\" to disable it.", &setlist),
4407
         &showlist);
4408
    }
4409
  add_prefix_cmd ("debug", no_class, set_debug,
4410
                  "Generic command for setting gdb debugging flags",
4411
                  &setdebuglist, "set debug ", 0, &setlist);
4412
 
4413
  add_prefix_cmd ("debug", no_class, show_debug,
4414
                  "Generic command for showing gdb debugging flags",
4415
                  &showdebuglist, "show debug ", 0, &showlist);
4416
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.