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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gdb-7.2/] [gdb/] [cli/] [cli-script.c] - Blame information for rev 853

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

Line No. Rev Author Line
1 330 jeremybenn
/* GDB CLI command scripting.
2
 
3
   Copyright (c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4
   1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008,
5
   2009, 2010 Free Software Foundation, Inc.
6
 
7
   This file is part of GDB.
8
 
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3 of the License, or
12
   (at your option) any later version.
13
 
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
 
19
   You should have received a copy of the GNU General Public License
20
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
 
22
#include "defs.h"
23
#include "value.h"
24
#include "language.h"           /* For value_true */
25
#include <ctype.h>
26
 
27
#include "ui-out.h"
28
#include "gdb_string.h"
29
#include "exceptions.h"
30
#include "top.h"
31
#include "breakpoint.h"
32
#include "cli/cli-cmds.h"
33
#include "cli/cli-decode.h"
34
#include "cli/cli-script.h"
35
#include "gdb_assert.h"
36
 
37
#include "python/python.h"
38
 
39
/* Prototypes for local functions */
40
 
41
static enum command_control_type
42
recurse_read_control_structure (char * (*read_next_line_func) (void),
43
                                struct command_line *current_cmd,
44
                                void (*validator)(char *, void *),
45
                                void *closure);
46
 
47
static char *insert_args (char *line);
48
 
49
static struct cleanup * setup_user_args (char *p);
50
 
51
static char *read_next_line (void);
52
 
53
/* Level of control structure when reading.  */
54
static int control_level;
55
 
56
/* Level of control structure when executing.  */
57
static int command_nest_depth = 1;
58
 
59
/* This is to prevent certain commands being printed twice.  */
60
static int suppress_next_print_command_trace = 0;
61
 
62
/* Structure for arguments to user defined functions.  */
63
#define MAXUSERARGS 10
64
struct user_args
65
  {
66
    struct user_args *next;
67
    /* It is necessary to store a malloced copy of the command line to
68
       ensure that the arguments are not overwritten before they are used.  */
69
    char *command;
70
    struct
71
      {
72
        char *arg;
73
        int len;
74
      }
75
    a[MAXUSERARGS];
76
    int count;
77
  }
78
 *user_args;
79
 
80
 
81
/* Allocate, initialize a new command line structure for one of the
82
   control commands (if/while).  */
83
 
84
static struct command_line *
85
build_command_line (enum command_control_type type, char *args)
86
{
87
  struct command_line *cmd;
88
 
89
  if (args == NULL && (type == if_control || type == while_control))
90
    error (_("if/while commands require arguments."));
91
  gdb_assert (args != NULL);
92
 
93
  cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
94
  cmd->next = NULL;
95
  cmd->control_type = type;
96
 
97
  cmd->body_count = 1;
98
  cmd->body_list
99
    = (struct command_line **) xmalloc (sizeof (struct command_line *)
100
                                        * cmd->body_count);
101
  memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
102
  cmd->line = xstrdup (args);
103
 
104
  return cmd;
105
}
106
 
107
/* Build and return a new command structure for the control commands
108
   such as "if" and "while".  */
109
 
110
struct command_line *
111
get_command_line (enum command_control_type type, char *arg)
112
{
113
  struct command_line *cmd;
114
  struct cleanup *old_chain = NULL;
115
 
116
  /* Allocate and build a new command line structure.  */
117
  cmd = build_command_line (type, arg);
118
 
119
  old_chain = make_cleanup_free_command_lines (&cmd);
120
 
121
  /* Read in the body of this command.  */
122
  if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
123
      == invalid_control)
124
    {
125
      warning (_("Error reading in canned sequence of commands."));
126
      do_cleanups (old_chain);
127
      return NULL;
128
    }
129
 
130
  discard_cleanups (old_chain);
131
  return cmd;
132
}
133
 
134
/* Recursively print a command (including full control structures).  */
135
 
136
void
137
print_command_lines (struct ui_out *uiout, struct command_line *cmd,
138
                     unsigned int depth)
139
{
140
  struct command_line *list;
141
 
142
  list = cmd;
143
  while (list)
144
    {
145
      if (depth)
146
        ui_out_spaces (uiout, 2 * depth);
147
 
148
      /* A simple command, print it and continue.  */
149
      if (list->control_type == simple_control)
150
        {
151
          ui_out_field_string (uiout, NULL, list->line);
152
          ui_out_text (uiout, "\n");
153
          list = list->next;
154
          continue;
155
        }
156
 
157
      /* loop_continue to jump to the start of a while loop, print it
158
         and continue. */
159
      if (list->control_type == continue_control)
160
        {
161
          ui_out_field_string (uiout, NULL, "loop_continue");
162
          ui_out_text (uiout, "\n");
163
          list = list->next;
164
          continue;
165
        }
166
 
167
      /* loop_break to break out of a while loop, print it and continue.  */
168
      if (list->control_type == break_control)
169
        {
170
          ui_out_field_string (uiout, NULL, "loop_break");
171
          ui_out_text (uiout, "\n");
172
          list = list->next;
173
          continue;
174
        }
175
 
176
      /* A while command.  Recursively print its subcommands and continue.  */
177
      if (list->control_type == while_control
178
          || list->control_type == while_stepping_control)
179
        {
180
          /* For while-stepping, the line includes the 'while-stepping' token.
181
             See comment in process_next_line for explanation.  Here,
182
             take care not print 'while-stepping' twice.  */
183
          if (list->control_type == while_control)
184
            ui_out_field_fmt (uiout, NULL, "while %s", list->line);
185
          else
186
            ui_out_field_string (uiout, NULL, list->line);
187
          ui_out_text (uiout, "\n");
188
          print_command_lines (uiout, *list->body_list, depth + 1);
189
          if (depth)
190
            ui_out_spaces (uiout, 2 * depth);
191
          ui_out_field_string (uiout, NULL, "end");
192
          ui_out_text (uiout, "\n");
193
          list = list->next;
194
          continue;
195
        }
196
 
197
      /* An if command.  Recursively print both arms before continueing.  */
198
      if (list->control_type == if_control)
199
        {
200
          ui_out_field_fmt (uiout, NULL, "if %s", list->line);
201
          ui_out_text (uiout, "\n");
202
          /* The true arm. */
203
          print_command_lines (uiout, list->body_list[0], depth + 1);
204
 
205
          /* Show the false arm if it exists.  */
206
          if (list->body_count == 2)
207
            {
208
              if (depth)
209
                ui_out_spaces (uiout, 2 * depth);
210
              ui_out_field_string (uiout, NULL, "else");
211
              ui_out_text (uiout, "\n");
212
              print_command_lines (uiout, list->body_list[1], depth + 1);
213
            }
214
 
215
          if (depth)
216
            ui_out_spaces (uiout, 2 * depth);
217
          ui_out_field_string (uiout, NULL, "end");
218
          ui_out_text (uiout, "\n");
219
          list = list->next;
220
          continue;
221
        }
222
 
223
      /* A commands command.  Print the breakpoint commands and continue.  */
224
      if (list->control_type == commands_control)
225
        {
226
          if (*(list->line))
227
            ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
228
          else
229
            ui_out_field_string (uiout, NULL, "commands");
230
          ui_out_text (uiout, "\n");
231
          print_command_lines (uiout, *list->body_list, depth + 1);
232
          if (depth)
233
            ui_out_spaces (uiout, 2 * depth);
234
          ui_out_field_string (uiout, NULL, "end");
235
          ui_out_text (uiout, "\n");
236
          list = list->next;
237
          continue;
238
        }
239
 
240
      if (list->control_type == python_control)
241
        {
242
          ui_out_field_string (uiout, NULL, "python");
243
          ui_out_text (uiout, "\n");
244
          /* Don't indent python code at all.  */
245
          print_command_lines (uiout, *list->body_list, 0);
246
          if (depth)
247
            ui_out_spaces (uiout, 2 * depth);
248
          ui_out_field_string (uiout, NULL, "end");
249
          ui_out_text (uiout, "\n");
250
          list = list->next;
251
          continue;
252
        }
253
 
254
      /* ignore illegal command type and try next */
255
      list = list->next;
256
    }                           /* while (list) */
257
}
258
 
259
/* Handle pre-post hooks.  */
260
 
261
static void
262
clear_hook_in_cleanup (void *data)
263
{
264
  struct cmd_list_element *c = data;
265
 
266
  c->hook_in = 0; /* Allow hook to work again once it is complete */
267
}
268
 
269
void
270
execute_cmd_pre_hook (struct cmd_list_element *c)
271
{
272
  if ((c->hook_pre) && (!c->hook_in))
273
    {
274
      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
275
      c->hook_in = 1; /* Prevent recursive hooking */
276
      execute_user_command (c->hook_pre, (char *) 0);
277
      do_cleanups (cleanups);
278
    }
279
}
280
 
281
void
282
execute_cmd_post_hook (struct cmd_list_element *c)
283
{
284
  if ((c->hook_post) && (!c->hook_in))
285
    {
286
      struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
287
 
288
      c->hook_in = 1; /* Prevent recursive hooking */
289
      execute_user_command (c->hook_post, (char *) 0);
290
      do_cleanups (cleanups);
291
    }
292
}
293
 
294
/* Execute the command in CMD.  */
295
static void
296
do_restore_user_call_depth (void * call_depth)
297
{
298
  int *depth = call_depth;
299
 
300
  (*depth)--;
301
  if ((*depth) == 0)
302
    in_user_command = 0;
303
}
304
 
305
 
306
void
307
execute_user_command (struct cmd_list_element *c, char *args)
308
{
309
  struct command_line *cmdlines;
310
  struct cleanup *old_chain;
311
  enum command_control_type ret;
312
  static int user_call_depth = 0;
313
  extern int max_user_call_depth;
314
 
315
  old_chain = setup_user_args (args);
316
 
317
  cmdlines = c->user_commands;
318
  if (cmdlines == 0)
319
    /* Null command */
320
    return;
321
 
322
  if (++user_call_depth > max_user_call_depth)
323
    error (_("Max user call depth exceeded -- command aborted."));
324
 
325
  make_cleanup (do_restore_user_call_depth, &user_call_depth);
326
 
327
  /* Set the instream to 0, indicating execution of a
328
     user-defined function.  */
329
  make_cleanup (do_restore_instream_cleanup, instream);
330
  instream = (FILE *) 0;
331
 
332
  /* Also set the global in_user_command, so that NULL instream is
333
     not confused with Insight.  */
334
  in_user_command = 1;
335
 
336
  command_nest_depth++;
337
  while (cmdlines)
338
    {
339
      ret = execute_control_command (cmdlines);
340
      if (ret != simple_control && ret != break_control)
341
        {
342
          warning (_("Error executing canned sequence of commands."));
343
          break;
344
        }
345
      cmdlines = cmdlines->next;
346
    }
347
  command_nest_depth--;
348
  do_cleanups (old_chain);
349
}
350
 
351
/* This function is called every time GDB prints a prompt.
352
   It ensures that errors and the like to not confuse the command tracing.  */
353
 
354
void
355
reset_command_nest_depth (void)
356
{
357
  command_nest_depth = 1;
358
 
359
  /* Just in case.  */
360
  suppress_next_print_command_trace = 0;
361
}
362
 
363
/* Print the command, prefixed with '+' to represent the call depth.
364
   This is slightly complicated because this function may be called
365
   from execute_command and execute_control_command.  Unfortunately
366
   execute_command also prints the top level control commands.
367
   In these cases execute_command will call execute_control_command
368
   via while_command or if_command.  Inner levels of 'if' and 'while'
369
   are dealt with directly.  Therefore we can use these functions
370
   to determine whether the command has been printed already or not.  */
371
void
372
print_command_trace (const char *cmd)
373
{
374
  int i;
375
 
376
  if (suppress_next_print_command_trace)
377
    {
378
      suppress_next_print_command_trace = 0;
379
      return;
380
    }
381
 
382
  if (!source_verbose && !trace_commands)
383
    return;
384
 
385
  for (i=0; i < command_nest_depth; i++)
386
    printf_filtered ("+");
387
 
388
  printf_filtered ("%s\n", cmd);
389
}
390
 
391
enum command_control_type
392
execute_control_command (struct command_line *cmd)
393
{
394
  struct expression *expr;
395
  struct command_line *current;
396
  struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
397
  struct value *val;
398
  struct value *val_mark;
399
  int loop;
400
  enum command_control_type ret;
401
  char *new_line;
402
 
403
  /* Start by assuming failure, if a problem is detected, the code
404
     below will simply "break" out of the switch.  */
405
  ret = invalid_control;
406
 
407
  switch (cmd->control_type)
408
    {
409
    case simple_control:
410
      /* A simple command, execute it and return.  */
411
      new_line = insert_args (cmd->line);
412
      if (!new_line)
413
        break;
414
      make_cleanup (free_current_contents, &new_line);
415
      execute_command (new_line, 0);
416
      ret = cmd->control_type;
417
      break;
418
 
419
    case continue_control:
420
      print_command_trace ("loop_continue");
421
 
422
      /* Return for "continue", and "break" so we can either
423
         continue the loop at the top, or break out.  */
424
      ret = cmd->control_type;
425
      break;
426
 
427
    case break_control:
428
      print_command_trace ("loop_break");
429
 
430
      /* Return for "continue", and "break" so we can either
431
         continue the loop at the top, or break out.  */
432
      ret = cmd->control_type;
433
      break;
434
 
435
    case while_control:
436
      {
437
        char *buffer = alloca (strlen (cmd->line) + 7);
438
 
439
        sprintf (buffer, "while %s", cmd->line);
440
        print_command_trace (buffer);
441
 
442
        /* Parse the loop control expression for the while statement.  */
443
        new_line = insert_args (cmd->line);
444
        if (!new_line)
445
          break;
446
        make_cleanup (free_current_contents, &new_line);
447
        expr = parse_expression (new_line);
448
        make_cleanup (free_current_contents, &expr);
449
 
450
        ret = simple_control;
451
        loop = 1;
452
 
453
        /* Keep iterating so long as the expression is true.  */
454
        while (loop == 1)
455
          {
456
            int cond_result;
457
 
458
            QUIT;
459
 
460
            /* Evaluate the expression.  */
461
            val_mark = value_mark ();
462
            val = evaluate_expression (expr);
463
            cond_result = value_true (val);
464
            value_free_to_mark (val_mark);
465
 
466
            /* If the value is false, then break out of the loop.  */
467
            if (!cond_result)
468
              break;
469
 
470
            /* Execute the body of the while statement.  */
471
            current = *cmd->body_list;
472
            while (current)
473
              {
474
                command_nest_depth++;
475
                ret = execute_control_command (current);
476
                command_nest_depth--;
477
 
478
                /* If we got an error, or a "break" command, then stop
479
                   looping.  */
480
                if (ret == invalid_control || ret == break_control)
481
                  {
482
                    loop = 0;
483
                    break;
484
                  }
485
 
486
                /* If we got a "continue" command, then restart the loop
487
                   at this point.  */
488
                if (ret == continue_control)
489
                  break;
490
 
491
                /* Get the next statement.  */
492
                current = current->next;
493
              }
494
          }
495
 
496
        /* Reset RET so that we don't recurse the break all the way down.  */
497
        if (ret == break_control)
498
          ret = simple_control;
499
 
500
        break;
501
      }
502
 
503
    case if_control:
504
      {
505
        char *buffer = alloca (strlen (cmd->line) + 4);
506
 
507
        sprintf (buffer, "if %s", cmd->line);
508
        print_command_trace (buffer);
509
 
510
        new_line = insert_args (cmd->line);
511
        if (!new_line)
512
          break;
513
        make_cleanup (free_current_contents, &new_line);
514
        /* Parse the conditional for the if statement.  */
515
        expr = parse_expression (new_line);
516
        make_cleanup (free_current_contents, &expr);
517
 
518
        current = NULL;
519
        ret = simple_control;
520
 
521
        /* Evaluate the conditional.  */
522
        val_mark = value_mark ();
523
        val = evaluate_expression (expr);
524
 
525
        /* Choose which arm to take commands from based on the value of the
526
           conditional expression.  */
527
        if (value_true (val))
528
          current = *cmd->body_list;
529
        else if (cmd->body_count == 2)
530
          current = *(cmd->body_list + 1);
531
        value_free_to_mark (val_mark);
532
 
533
        /* Execute commands in the given arm.  */
534
        while (current)
535
          {
536
            command_nest_depth++;
537
            ret = execute_control_command (current);
538
            command_nest_depth--;
539
 
540
            /* If we got an error, get out.  */
541
            if (ret != simple_control)
542
              break;
543
 
544
            /* Get the next statement in the body.  */
545
            current = current->next;
546
          }
547
 
548
        break;
549
      }
550
    case commands_control:
551
      {
552
        /* Breakpoint commands list, record the commands in the breakpoint's
553
           command list and return.  */
554
        new_line = insert_args (cmd->line);
555
        if (!new_line)
556
          break;
557
        make_cleanup (free_current_contents, &new_line);
558
        ret = commands_from_control_command (new_line, cmd);
559
        break;
560
      }
561
    case python_control:
562
      {
563
        eval_python_from_control_command (cmd);
564
        ret = simple_control;
565
        break;
566
      }
567
 
568
    default:
569
      warning (_("Invalid control type in canned commands structure."));
570
      break;
571
    }
572
 
573
  do_cleanups (old_chain);
574
 
575
  return ret;
576
}
577
 
578
/* Like execute_control_command, but first set
579
   suppress_next_print_command_trace.   */
580
 
581
enum command_control_type
582
execute_control_command_untraced (struct command_line *cmd)
583
{
584
  suppress_next_print_command_trace = 1;
585
  return execute_control_command (cmd);
586
}
587
 
588
 
589
/* "while" command support.  Executes a body of statements while the
590
   loop condition is nonzero.  */
591
 
592
void
593
while_command (char *arg, int from_tty)
594
{
595
  struct command_line *command = NULL;
596
 
597
  control_level = 1;
598
  command = get_command_line (while_control, arg);
599
 
600
  if (command == NULL)
601
    return;
602
 
603
  execute_control_command_untraced (command);
604
  free_command_lines (&command);
605
}
606
 
607
/* "if" command support.  Execute either the true or false arm depending
608
   on the value of the if conditional.  */
609
 
610
void
611
if_command (char *arg, int from_tty)
612
{
613
  struct command_line *command = NULL;
614
 
615
  control_level = 1;
616
  command = get_command_line (if_control, arg);
617
 
618
  if (command == NULL)
619
    return;
620
 
621
  execute_control_command_untraced (command);
622
  free_command_lines (&command);
623
}
624
 
625
/* Cleanup */
626
static void
627
arg_cleanup (void *ignore)
628
{
629
  struct user_args *oargs = user_args;
630
 
631
  if (!user_args)
632
    internal_error (__FILE__, __LINE__,
633
                    _("arg_cleanup called with no user args.\n"));
634
 
635
  user_args = user_args->next;
636
  xfree (oargs->command);
637
  xfree (oargs);
638
}
639
 
640
/* Bind the incomming arguments for a user defined command to
641
   $arg0, $arg1 ... $argMAXUSERARGS.  */
642
 
643
static struct cleanup *
644
setup_user_args (char *p)
645
{
646
  struct user_args *args;
647
  struct cleanup *old_chain;
648
  unsigned int arg_count = 0;
649
 
650
  args = (struct user_args *) xmalloc (sizeof (struct user_args));
651
  memset (args, 0, sizeof (struct user_args));
652
 
653
  args->next = user_args;
654
  user_args = args;
655
 
656
  old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
657
 
658
  if (p == NULL)
659
    return old_chain;
660
 
661
  user_args->command = p = xstrdup (p);
662
 
663
  while (*p)
664
    {
665
      char *start_arg;
666
      int squote = 0;
667
      int dquote = 0;
668
      int bsquote = 0;
669
 
670
      if (arg_count >= MAXUSERARGS)
671
        {
672
          error (_("user defined function may only have %d arguments."),
673
                 MAXUSERARGS);
674
          return old_chain;
675
        }
676
 
677
      /* Strip whitespace.  */
678
      while (*p == ' ' || *p == '\t')
679
        p++;
680
 
681
      /* P now points to an argument.  */
682
      start_arg = p;
683
      user_args->a[arg_count].arg = p;
684
 
685
      /* Get to the end of this argument.  */
686
      while (*p)
687
        {
688
          if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
689
            break;
690
          else
691
            {
692
              if (bsquote)
693
                bsquote = 0;
694
              else if (*p == '\\')
695
                bsquote = 1;
696
              else if (squote)
697
                {
698
                  if (*p == '\'')
699
                    squote = 0;
700
                }
701
              else if (dquote)
702
                {
703
                  if (*p == '"')
704
                    dquote = 0;
705
                }
706
              else
707
                {
708
                  if (*p == '\'')
709
                    squote = 1;
710
                  else if (*p == '"')
711
                    dquote = 1;
712
                }
713
              p++;
714
            }
715
        }
716
 
717
      user_args->a[arg_count].len = p - start_arg;
718
      arg_count++;
719
      user_args->count++;
720
    }
721
  return old_chain;
722
}
723
 
724
/* Given character string P, return a point to the first argument ($arg),
725
   or NULL if P contains no arguments.  */
726
 
727
static char *
728
locate_arg (char *p)
729
{
730
  while ((p = strchr (p, '$')))
731
    {
732
      if (strncmp (p, "$arg", 4) == 0
733
          && (isdigit (p[4]) || p[4] == 'c'))
734
        return p;
735
      p++;
736
    }
737
  return NULL;
738
}
739
 
740
/* Insert the user defined arguments stored in user_arg into the $arg
741
   arguments found in line, with the updated copy being placed into nline.  */
742
 
743
static char *
744
insert_args (char *line)
745
{
746
  char *p, *save_line, *new_line;
747
  unsigned len, i;
748
 
749
  /* If we are not in a user-defined function, treat $argc, $arg0, et
750
     cetera as normal convenience variables.  */
751
  if (user_args == NULL)
752
    return xstrdup (line);
753
 
754
  /* First we need to know how much memory to allocate for the new line.  */
755
  save_line = line;
756
  len = 0;
757
  while ((p = locate_arg (line)))
758
    {
759
      len += p - line;
760
      i = p[4] - '0';
761
 
762
      if (p[4] == 'c')
763
        {
764
          /* $argc.  Number will be <=10.  */
765
          len += user_args->count == 10 ? 2 : 1;
766
        }
767
      else if (i >= user_args->count)
768
        {
769
          error (_("Missing argument %d in user function."), i);
770
          return NULL;
771
        }
772
      else
773
        {
774
          len += user_args->a[i].len;
775
        }
776
      line = p + 5;
777
    }
778
 
779
  /* Don't forget the tail.  */
780
  len += strlen (line);
781
 
782
  /* Allocate space for the new line and fill it in.  */
783
  new_line = (char *) xmalloc (len + 1);
784
  if (new_line == NULL)
785
    return NULL;
786
 
787
  /* Restore pointer to beginning of old line.  */
788
  line = save_line;
789
 
790
  /* Save pointer to beginning of new line.  */
791
  save_line = new_line;
792
 
793
  while ((p = locate_arg (line)))
794
    {
795
      int i, len;
796
 
797
      memcpy (new_line, line, p - line);
798
      new_line += p - line;
799
 
800
      if (p[4] == 'c')
801
        {
802
          gdb_assert (user_args->count >= 0 && user_args->count <= 10);
803
          if (user_args->count == 10)
804
            {
805
              *(new_line++) = '1';
806
              *(new_line++) = '0';
807
            }
808
          else
809
            *(new_line++) = user_args->count + '0';
810
        }
811
      else
812
        {
813
          i = p[4] - '0';
814
          len = user_args->a[i].len;
815
          if (len)
816
            {
817
              memcpy (new_line, user_args->a[i].arg, len);
818
              new_line += len;
819
            }
820
        }
821
      line = p + 5;
822
    }
823
  /* Don't forget the tail.  */
824
  strcpy (new_line, line);
825
 
826
  /* Return a pointer to the beginning of the new line.  */
827
  return save_line;
828
}
829
 
830
 
831
/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
832
   code bodies.  This is typically used when we encounter an "else"
833
   clause for an "if" command.  */
834
 
835
static void
836
realloc_body_list (struct command_line *command, int new_length)
837
{
838
  int n;
839
  struct command_line **body_list;
840
 
841
  n = command->body_count;
842
 
843
  /* Nothing to do?  */
844
  if (new_length <= n)
845
    return;
846
 
847
  body_list = (struct command_line **)
848
    xmalloc (sizeof (struct command_line *) * new_length);
849
 
850
  memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
851
  memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
852
 
853
  xfree (command->body_list);
854
  command->body_list = body_list;
855
  command->body_count = new_length;
856
}
857
 
858
/* Read next line from stdout.  Passed to read_command_line_1 and
859
   recurse_read_control_structure whenever we need to read commands
860
   from stdout.  */
861
 
862
static char *
863
read_next_line (void)
864
{
865
  char *prompt_ptr, control_prompt[256];
866
  int i = 0;
867
 
868
  if (control_level >= 254)
869
    error (_("Control nesting too deep!"));
870
 
871
  /* Set a prompt based on the nesting of the control commands.  */
872
  if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
873
    {
874
      for (i = 0; i < control_level; i++)
875
        control_prompt[i] = ' ';
876
      control_prompt[i] = '>';
877
      control_prompt[i + 1] = '\0';
878
      prompt_ptr = (char *) &control_prompt[0];
879
    }
880
  else
881
    prompt_ptr = NULL;
882
 
883
  return command_line_input (prompt_ptr, instream == stdin, "commands");
884
}
885
 
886
/* Process one input line.  If the command is an "end",
887
   return such an indication to the caller.  If PARSE_COMMANDS is true,
888
   strip leading whitespace (trailing whitespace is always stripped)
889
   in the line, attempt to recognize GDB control commands, and also
890
   return an indication if the command is an "else" or a nop.
891
   Otherwise, only "end" is recognized.  */
892
 
893
static enum misc_command_type
894
process_next_line (char *p, struct command_line **command, int parse_commands,
895
                   void (*validator)(char *, void *), void *closure)
896
{
897
  char *p_end;
898
  char *p_start;
899
  int not_handled = 0;
900
 
901
  /* Not sure what to do here.  */
902
  if (p == NULL)
903
    return end_command;
904
 
905
  /* Strip trailing whitespace.  */
906
  p_end = p + strlen (p);
907
  while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
908
    p_end--;
909
 
910
  p_start = p;
911
  /* Strip leading whitespace.  */
912
  while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
913
    p_start++;
914
 
915
  /* 'end' is always recognized, regardless of parse_commands value.
916
     We also permit whitespace before end and after.  */
917
  if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
918
    return end_command;
919
 
920
  if (parse_commands)
921
    {
922
      /* If commands are parsed, we skip initial spaces. Otherwise,
923
         which is the case for Python commands and documentation
924
         (see the 'document' command), spaces are preserved.  */
925
      p = p_start;
926
 
927
      /* Blanks and comments don't really do anything, but we need to
928
         distinguish them from else, end and other commands which can be
929
         executed.  */
930
      if (p_end == p || p[0] == '#')
931
        return nop_command;
932
 
933
      /* Is the else clause of an if control structure?  */
934
      if (p_end - p == 4 && !strncmp (p, "else", 4))
935
        return else_command;
936
 
937
      /* Check for while, if, break, continue, etc and build a new command
938
         line structure for them.  */
939
      if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
940
          || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
941
          || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
942
        {
943
          /* Because validate_actionline and encode_action lookup
944
             command's line as command, we need the line to
945
             include 'while-stepping'.
946
 
947
             For 'ws' alias, the command will have 'ws', not expanded
948
             to 'while-stepping'. This is intentional -- we don't
949
             really want frontend to send a command list with 'ws',
950
             and next break-info returning command line with 'while-stepping'.
951
             This should work, but might cause the breakpoint to be marked as
952
             changed while it's actually not.  */
953
          *command = build_command_line (while_stepping_control, p);
954
        }
955
      else if (p_end - p > 5 && !strncmp (p, "while", 5))
956
        {
957
          char *first_arg;
958
 
959
          first_arg = p + 5;
960
          while (first_arg < p_end && isspace (*first_arg))
961
            first_arg++;
962
          *command = build_command_line (while_control, first_arg);
963
        }
964
      else if (p_end - p > 2 && !strncmp (p, "if", 2))
965
        {
966
          char *first_arg;
967
 
968
          first_arg = p + 2;
969
          while (first_arg < p_end && isspace (*first_arg))
970
            first_arg++;
971
          *command = build_command_line (if_control, first_arg);
972
        }
973
      else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
974
        {
975
          char *first_arg;
976
 
977
          first_arg = p + 8;
978
          while (first_arg < p_end && isspace (*first_arg))
979
            first_arg++;
980
          *command = build_command_line (commands_control, first_arg);
981
        }
982
      else if (p_end - p == 6 && !strncmp (p, "python", 6))
983
        {
984
          /* Note that we ignore the inline "python command" form
985
             here.  */
986
          *command = build_command_line (python_control, "");
987
        }
988
      else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
989
        {
990
          *command = (struct command_line *)
991
            xmalloc (sizeof (struct command_line));
992
          (*command)->next = NULL;
993
          (*command)->line = NULL;
994
          (*command)->control_type = break_control;
995
          (*command)->body_count = 0;
996
          (*command)->body_list = NULL;
997
        }
998
      else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
999
        {
1000
          *command = (struct command_line *)
1001
            xmalloc (sizeof (struct command_line));
1002
          (*command)->next = NULL;
1003
          (*command)->line = NULL;
1004
          (*command)->control_type = continue_control;
1005
          (*command)->body_count = 0;
1006
          (*command)->body_list = NULL;
1007
        }
1008
      else
1009
        not_handled = 1;
1010
    }
1011
 
1012
  if (!parse_commands || not_handled)
1013
    {
1014
      /* A normal command.  */
1015
      *command = (struct command_line *)
1016
        xmalloc (sizeof (struct command_line));
1017
      (*command)->next = NULL;
1018
      (*command)->line = savestring (p, p_end - p);
1019
      (*command)->control_type = simple_control;
1020
      (*command)->body_count = 0;
1021
      (*command)->body_list = NULL;
1022
    }
1023
 
1024
  if (validator)
1025
    {
1026
      volatile struct gdb_exception ex;
1027
 
1028
      TRY_CATCH (ex, RETURN_MASK_ALL)
1029
        {
1030
          validator ((*command)->line, closure);
1031
        }
1032
      if (ex.reason < 0)
1033
        {
1034
          xfree (*command);
1035
          throw_exception (ex);
1036
        }
1037
    }
1038
 
1039
  /* Nothing special.  */
1040
  return ok_command;
1041
}
1042
 
1043
/* Recursively read in the control structures and create a command_line
1044
   structure from them.  Use read_next_line_func to obtain lines of
1045
   the command.
1046
 
1047
*/
1048
 
1049
static enum command_control_type
1050
recurse_read_control_structure (char * (*read_next_line_func) (void),
1051
                                struct command_line *current_cmd,
1052
                                void (*validator)(char *, void *),
1053
                                void *closure)
1054
{
1055
  int current_body, i;
1056
  enum misc_command_type val;
1057
  enum command_control_type ret;
1058
  struct command_line **body_ptr, *child_tail, *next;
1059
 
1060
  child_tail = NULL;
1061
  current_body = 1;
1062
 
1063
  /* Sanity checks.  */
1064
  if (current_cmd->control_type == simple_control)
1065
    error (_("Recursed on a simple control type."));
1066
 
1067
  if (current_body > current_cmd->body_count)
1068
    error (_("Allocated body is smaller than this command type needs."));
1069
 
1070
  /* Read lines from the input stream and build control structures.  */
1071
  while (1)
1072
    {
1073
      dont_repeat ();
1074
 
1075
      next = NULL;
1076
      val = process_next_line (read_next_line_func (), &next,
1077
                               current_cmd->control_type != python_control,
1078
                               validator, closure);
1079
 
1080
      /* Just skip blanks and comments.  */
1081
      if (val == nop_command)
1082
        continue;
1083
 
1084
      if (val == end_command)
1085
        {
1086
          if (current_cmd->control_type == while_control
1087
              || current_cmd->control_type == while_stepping_control
1088
              || current_cmd->control_type == if_control
1089
              || current_cmd->control_type == python_control
1090
              || current_cmd->control_type == commands_control)
1091
            {
1092
              /* Success reading an entire canned sequence of commands.  */
1093
              ret = simple_control;
1094
              break;
1095
            }
1096
          else
1097
            {
1098
              ret = invalid_control;
1099
              break;
1100
            }
1101
        }
1102
 
1103
      /* Not the end of a control structure.  */
1104
      if (val == else_command)
1105
        {
1106
          if (current_cmd->control_type == if_control
1107
              && current_body == 1)
1108
            {
1109
              realloc_body_list (current_cmd, 2);
1110
              current_body = 2;
1111
              child_tail = NULL;
1112
              continue;
1113
            }
1114
          else
1115
            {
1116
              ret = invalid_control;
1117
              break;
1118
            }
1119
        }
1120
 
1121
      if (child_tail)
1122
        {
1123
          child_tail->next = next;
1124
        }
1125
      else
1126
        {
1127
          body_ptr = current_cmd->body_list;
1128
          for (i = 1; i < current_body; i++)
1129
            body_ptr++;
1130
 
1131
          *body_ptr = next;
1132
 
1133
        }
1134
 
1135
      child_tail = next;
1136
 
1137
      /* If the latest line is another control structure, then recurse
1138
         on it.  */
1139
      if (next->control_type == while_control
1140
          || next->control_type == while_stepping_control
1141
          || next->control_type == if_control
1142
          || next->control_type == python_control
1143
          || next->control_type == commands_control)
1144
        {
1145
          control_level++;
1146
          ret = recurse_read_control_structure (read_next_line_func, next,
1147
                                                validator, closure);
1148
          control_level--;
1149
 
1150
          if (ret != simple_control)
1151
            break;
1152
        }
1153
    }
1154
 
1155
  dont_repeat ();
1156
 
1157
  return ret;
1158
}
1159
 
1160
/* Read lines from the input stream and accumulate them in a chain of
1161
   struct command_line's, which is then returned.  For input from a
1162
   terminal, the special command "end" is used to mark the end of the
1163
   input, and is not included in the returned chain of commands.
1164
 
1165
   If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1166
   is always stripped) in the line and attempt to recognize GDB control
1167
   commands.  Otherwise, only "end" is recognized.  */
1168
 
1169
#define END_MESSAGE "End with a line saying just \"end\"."
1170
 
1171
struct command_line *
1172
read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1173
                    void (*validator)(char *, void *), void *closure)
1174
{
1175
  struct command_line *head;
1176
 
1177
  if (from_tty && input_from_terminal_p ())
1178
    {
1179
      if (deprecated_readline_begin_hook)
1180
        {
1181
          /* Note - intentional to merge messages with no newline */
1182
          (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg, END_MESSAGE);
1183
        }
1184
      else
1185
        {
1186
          printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1187
          gdb_flush (gdb_stdout);
1188
        }
1189
    }
1190
 
1191
  head = read_command_lines_1 (read_next_line, parse_commands,
1192
                               validator, closure);
1193
 
1194
  if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1195
    {
1196
      (*deprecated_readline_end_hook) ();
1197
    }
1198
  return (head);
1199
}
1200
 
1201
/* Act the same way as read_command_lines, except that each new line is
1202
   obtained using READ_NEXT_LINE_FUNC.  */
1203
 
1204
struct command_line *
1205
read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1206
                      void (*validator)(char *, void *), void *closure)
1207
{
1208
  struct command_line *head, *tail, *next;
1209
  struct cleanup *old_chain;
1210
  enum command_control_type ret;
1211
  enum misc_command_type val;
1212
 
1213
  control_level = 0;
1214
  head = tail = NULL;
1215
  old_chain = NULL;
1216
 
1217
  while (1)
1218
    {
1219
      dont_repeat ();
1220
      val = process_next_line (read_next_line_func (), &next, parse_commands,
1221
                               validator, closure);
1222
 
1223
      /* Ignore blank lines or comments.  */
1224
      if (val == nop_command)
1225
        continue;
1226
 
1227
      if (val == end_command)
1228
        {
1229
          ret = simple_control;
1230
          break;
1231
        }
1232
 
1233
      if (val != ok_command)
1234
        {
1235
          ret = invalid_control;
1236
          break;
1237
        }
1238
 
1239
      if (next->control_type == while_control
1240
          || next->control_type == if_control
1241
          || next->control_type == python_control
1242
          || next->control_type == commands_control
1243
          || next->control_type == while_stepping_control)
1244
        {
1245
          control_level++;
1246
          ret = recurse_read_control_structure (read_next_line_func, next,
1247
                                                validator, closure);
1248
          control_level--;
1249
 
1250
          if (ret == invalid_control)
1251
            break;
1252
        }
1253
 
1254
      if (tail)
1255
        {
1256
          tail->next = next;
1257
        }
1258
      else
1259
        {
1260
          head = next;
1261
          old_chain = make_cleanup_free_command_lines (&head);
1262
        }
1263
      tail = next;
1264
    }
1265
 
1266
  dont_repeat ();
1267
 
1268
  if (head)
1269
    {
1270
      if (ret != invalid_control)
1271
        {
1272
          discard_cleanups (old_chain);
1273
        }
1274
      else
1275
        do_cleanups (old_chain);
1276
    }
1277
 
1278
  return head;
1279
}
1280
 
1281
/* Free a chain of struct command_line's.  */
1282
 
1283
void
1284
free_command_lines (struct command_line **lptr)
1285
{
1286
  struct command_line *l = *lptr;
1287
  struct command_line *next;
1288
  struct command_line **blist;
1289
  int i;
1290
 
1291
  while (l)
1292
    {
1293
      if (l->body_count > 0)
1294
        {
1295
          blist = l->body_list;
1296
          for (i = 0; i < l->body_count; i++, blist++)
1297
            free_command_lines (blist);
1298
        }
1299
      next = l->next;
1300
      xfree (l->line);
1301
      xfree (l);
1302
      l = next;
1303
    }
1304
  *lptr = NULL;
1305
}
1306
 
1307
static void
1308
do_free_command_lines_cleanup (void *arg)
1309
{
1310
  free_command_lines (arg);
1311
}
1312
 
1313
struct cleanup *
1314
make_cleanup_free_command_lines (struct command_line **arg)
1315
{
1316
  return make_cleanup (do_free_command_lines_cleanup, arg);
1317
}
1318
 
1319
struct command_line *
1320
copy_command_lines (struct command_line *cmds)
1321
{
1322
  struct command_line *result = NULL;
1323
 
1324
  if (cmds)
1325
    {
1326
      result = (struct command_line *) xmalloc (sizeof (struct command_line));
1327
 
1328
      result->next = copy_command_lines (cmds->next);
1329
      result->line = xstrdup (cmds->line);
1330
      result->control_type = cmds->control_type;
1331
      result->body_count = cmds->body_count;
1332
      if (cmds->body_count > 0)
1333
        {
1334
          int i;
1335
 
1336
          result->body_list = (struct command_line **)
1337
            xmalloc (sizeof (struct command_line *) * cmds->body_count);
1338
 
1339
          for (i = 0; i < cmds->body_count; i++)
1340
            result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1341
        }
1342
      else
1343
        result->body_list = NULL;
1344
    }
1345
 
1346
  return result;
1347
}
1348
 
1349
/* Validate that *COMNAME is a valid name for a command.  Return the
1350
   containing command list, in case it starts with a prefix command.
1351
   The prefix must already exist.  *COMNAME is advanced to point after
1352
   any prefix, and a NUL character overwrites the space after the
1353
   prefix.  */
1354
 
1355
static struct cmd_list_element **
1356
validate_comname (char **comname)
1357
{
1358
  struct cmd_list_element **list = &cmdlist;
1359
  char *p, *last_word;
1360
 
1361
  if (*comname == 0)
1362
    error_no_arg (_("name of command to define"));
1363
 
1364
  /* Find the last word of the argument.  */
1365
  p = *comname + strlen (*comname);
1366
  while (p > *comname && isspace (p[-1]))
1367
    p--;
1368
  while (p > *comname && !isspace (p[-1]))
1369
    p--;
1370
  last_word = p;
1371
 
1372
  /* Find the corresponding command list.  */
1373
  if (last_word != *comname)
1374
    {
1375
      struct cmd_list_element *c;
1376
      char saved_char, *tem = *comname;
1377
 
1378
      /* Separate the prefix and the command.  */
1379
      saved_char = last_word[-1];
1380
      last_word[-1] = '\0';
1381
 
1382
      c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1383
      if (c->prefixlist == NULL)
1384
        error (_("\"%s\" is not a prefix command."), *comname);
1385
 
1386
      list = c->prefixlist;
1387
      last_word[-1] = saved_char;
1388
      *comname = last_word;
1389
    }
1390
 
1391
  p = *comname;
1392
  while (*p)
1393
    {
1394
      if (!isalnum (*p) && *p != '-' && *p != '_')
1395
        error (_("Junk in argument list: \"%s\""), p);
1396
      p++;
1397
    }
1398
 
1399
  return list;
1400
}
1401
 
1402
/* This is just a placeholder in the command data structures.  */
1403
static void
1404
user_defined_command (char *ignore, int from_tty)
1405
{
1406
}
1407
 
1408
void
1409
define_command (char *comname, int from_tty)
1410
{
1411
#define MAX_TMPBUF 128   
1412
  enum cmd_hook_type
1413
    {
1414
      CMD_NO_HOOK = 0,
1415
      CMD_PRE_HOOK,
1416
      CMD_POST_HOOK
1417
    };
1418
  struct command_line *cmds;
1419
  struct cmd_list_element *c, *newc, *hookc = 0, **list;
1420
  char *tem, *comfull;
1421
  char tmpbuf[MAX_TMPBUF];
1422
  int  hook_type      = CMD_NO_HOOK;
1423
  int  hook_name_size = 0;
1424
 
1425
#define HOOK_STRING     "hook-"
1426
#define HOOK_LEN 5
1427
#define HOOK_POST_STRING "hookpost-"
1428
#define HOOK_POST_LEN    9
1429
 
1430
  comfull = comname;
1431
  list = validate_comname (&comname);
1432
 
1433
  /* Look it up, and verify that we got an exact match.  */
1434
  tem = comname;
1435
  c = lookup_cmd (&tem, *list, "", -1, 1);
1436
  if (c && strcmp (comname, c->name) != 0)
1437
    c = 0;
1438
 
1439
  if (c)
1440
    {
1441
      int q;
1442
 
1443
      if (c->class == class_user || c->class == class_alias)
1444
        q = query (_("Redefine command \"%s\"? "), c->name);
1445
      else
1446
        q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1447
      if (!q)
1448
        error (_("Command \"%s\" not redefined."), c->name);
1449
    }
1450
 
1451
  /* If this new command is a hook, then mark the command which it
1452
     is hooking.  Note that we allow hooking `help' commands, so that
1453
     we can hook the `stop' pseudo-command.  */
1454
 
1455
  if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1456
    {
1457
       hook_type      = CMD_PRE_HOOK;
1458
       hook_name_size = HOOK_LEN;
1459
    }
1460
  else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1461
    {
1462
      hook_type      = CMD_POST_HOOK;
1463
      hook_name_size = HOOK_POST_LEN;
1464
    }
1465
 
1466
  if (hook_type != CMD_NO_HOOK)
1467
    {
1468
      /* Look up cmd it hooks, and verify that we got an exact match.  */
1469
      tem = comname + hook_name_size;
1470
      hookc = lookup_cmd (&tem, *list, "", -1, 0);
1471
      if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1472
        hookc = 0;
1473
      if (!hookc)
1474
        {
1475
          warning (_("Your new `%s' command does not hook any existing command."),
1476
                   comfull);
1477
          if (!query (_("Proceed? ")))
1478
            error (_("Not confirmed."));
1479
        }
1480
    }
1481
 
1482
  comname = xstrdup (comname);
1483
 
1484
  /* If the rest of the commands will be case insensitive, this one
1485
     should behave in the same manner. */
1486
  for (tem = comname; *tem; tem++)
1487
    if (isupper (*tem))
1488
      *tem = tolower (*tem);
1489
 
1490
  sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
1491
  cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
1492
 
1493
  if (c && c->class == class_user)
1494
    free_command_lines (&c->user_commands);
1495
 
1496
  newc = add_cmd (comname, class_user, user_defined_command,
1497
                  (c && c->class == class_user)
1498
                  ? c->doc : xstrdup ("User-defined."), list);
1499
  newc->user_commands = cmds;
1500
 
1501
  /* If this new command is a hook, then mark both commands as being
1502
     tied.  */
1503
  if (hookc)
1504
    {
1505
      switch (hook_type)
1506
        {
1507
        case CMD_PRE_HOOK:
1508
          hookc->hook_pre  = newc;  /* Target gets hooked.  */
1509
          newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1510
          break;
1511
        case CMD_POST_HOOK:
1512
          hookc->hook_post  = newc;  /* Target gets hooked.  */
1513
          newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1514
          break;
1515
        default:
1516
          /* Should never come here as hookc would be 0. */
1517
          internal_error (__FILE__, __LINE__, _("bad switch"));
1518
        }
1519
    }
1520
}
1521
 
1522
void
1523
document_command (char *comname, int from_tty)
1524
{
1525
  struct command_line *doclines;
1526
  struct cmd_list_element *c, **list;
1527
  char *tem, *comfull;
1528
  char tmpbuf[128];
1529
 
1530
  comfull = comname;
1531
  list = validate_comname (&comname);
1532
 
1533
  tem = comname;
1534
  c = lookup_cmd (&tem, *list, "", 0, 1);
1535
 
1536
  if (c->class != class_user)
1537
    error (_("Command \"%s\" is built-in."), comfull);
1538
 
1539
  sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
1540
  doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
1541
 
1542
  if (c->doc)
1543
    xfree (c->doc);
1544
 
1545
  {
1546
    struct command_line *cl1;
1547
    int len = 0;
1548
 
1549
    for (cl1 = doclines; cl1; cl1 = cl1->next)
1550
      len += strlen (cl1->line) + 1;
1551
 
1552
    c->doc = (char *) xmalloc (len + 1);
1553
    *c->doc = 0;
1554
 
1555
    for (cl1 = doclines; cl1; cl1 = cl1->next)
1556
      {
1557
        strcat (c->doc, cl1->line);
1558
        if (cl1->next)
1559
          strcat (c->doc, "\n");
1560
      }
1561
  }
1562
 
1563
  free_command_lines (&doclines);
1564
}
1565
 
1566
struct source_cleanup_lines_args
1567
{
1568
  int old_line;
1569
  const char *old_file;
1570
};
1571
 
1572
static void
1573
source_cleanup_lines (void *args)
1574
{
1575
  struct source_cleanup_lines_args *p =
1576
    (struct source_cleanup_lines_args *) args;
1577
 
1578
  source_line_number = p->old_line;
1579
  source_file_name = p->old_file;
1580
}
1581
 
1582
struct wrapped_read_command_file_args
1583
{
1584
  FILE *stream;
1585
};
1586
 
1587
static void
1588
wrapped_read_command_file (struct ui_out *uiout, void *data)
1589
{
1590
  struct wrapped_read_command_file_args *args = data;
1591
 
1592
  read_command_file (args->stream);
1593
}
1594
 
1595
/* Used to implement source_command */
1596
 
1597
void
1598
script_from_file (FILE *stream, const char *file)
1599
{
1600
  struct cleanup *old_cleanups;
1601
  struct source_cleanup_lines_args old_lines;
1602
 
1603
  if (stream == NULL)
1604
    internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1605
 
1606
  old_cleanups = make_cleanup_fclose (stream);
1607
 
1608
  old_lines.old_line = source_line_number;
1609
  old_lines.old_file = source_file_name;
1610
  make_cleanup (source_cleanup_lines, &old_lines);
1611
  source_line_number = 0;
1612
  source_file_name = file;
1613
  /* This will get set every time we read a line.  So it won't stay "" for
1614
     long.  */
1615
  error_pre_print = "";
1616
 
1617
  {
1618
    struct gdb_exception e;
1619
    struct wrapped_read_command_file_args args;
1620
 
1621
    args.stream = stream;
1622
    e = catch_exception (uiout, wrapped_read_command_file, &args,
1623
                         RETURN_MASK_ERROR);
1624
    switch (e.reason)
1625
      {
1626
      case 0:
1627
        break;
1628
      case RETURN_ERROR:
1629
        /* Re-throw the error, but with the file name information
1630
           prepended.  */
1631
        throw_error (e.error,
1632
                     _("%s:%d: Error in sourced command file:\n%s"),
1633
                     source_file_name, source_line_number, e.message);
1634
      default:
1635
        internal_error (__FILE__, __LINE__, _("bad reason"));
1636
      }
1637
  }
1638
 
1639
  do_cleanups (old_cleanups);
1640
}
1641
 
1642
/* Print the definition of user command C to STREAM.  Or, if C is a
1643
   prefix command, show the definitions of all user commands under C
1644
   (recursively).  PREFIX and NAME combined are the name of the
1645
   current command.  */
1646
void
1647
show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
1648
             struct ui_file *stream)
1649
{
1650
  struct command_line *cmdlines;
1651
 
1652
  if (c->prefixlist != NULL)
1653
    {
1654
      char *prefixname = c->prefixname;
1655
 
1656
      for (c = *c->prefixlist; c != NULL; c = c->next)
1657
        if (c->class == class_user || c->prefixlist != NULL)
1658
          show_user_1 (c, prefixname, c->name, gdb_stdout);
1659
      return;
1660
    }
1661
 
1662
  cmdlines = c->user_commands;
1663
  if (!cmdlines)
1664
    return;
1665
  fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
1666
 
1667
  print_command_lines (uiout, cmdlines, 1);
1668
  fputs_filtered ("\n", stream);
1669
}
1670
 

powered by: WebSVN 2.1.0

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