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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [gdb/] [cli/] [cli-script.c] - Blame information for rev 157

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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