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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [cli/] [cli-script.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* GDB CLI command scripting.
2
   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3
   1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
 
5
   This file is part of GDB.
6
 
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 2 of the License, or
10
   (at your option) any later version.
11
 
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 59 Temple Place - Suite 330,
20
   Boston, MA 02111-1307, USA.  */
21
 
22
#include "defs.h"
23
#include "value.h"
24
#include "language.h"           /* For value_true */
25
#include <ctype.h>
26
 
27
#ifdef UI_OUT
28
#include "ui-out.h"
29
#endif
30
 
31
#include "top.h"
32
#include "cli/cli-cmds.h"
33
#include "cli/cli-decode.h"
34
#include "cli/cli-script.h"
35
 
36
/* From gdb/top.c */
37
 
38
extern void dont_repeat (void);
39
 
40
extern void do_restore_instream_cleanup (void *stream);
41
 
42
/* Prototypes for local functions */
43
 
44
static struct cleanup *
45
        make_cleanup_free_command_lines (struct command_line **arg);
46
 
47
static enum command_control_type
48
        recurse_read_control_structure (struct command_line *current_cmd);
49
 
50
static char *insert_args (char *line);
51
 
52
static struct cleanup * setup_user_args (char *p);
53
 
54
static void validate_comname (char *);
55
 
56
/* Level of control structure.  */
57
static int control_level;
58
 
59
/* Source command state variable. */
60
static int source_error_allocated;
61
 
62
/* Structure for arguments to user defined functions.  */
63
#define MAXUSERARGS 10
64
struct user_args
65
  {
66
    struct user_args *next;
67
    struct
68
      {
69
        char *arg;
70
        int len;
71
      }
72
    a[MAXUSERARGS];
73
    int count;
74
  }
75
 *user_args;
76
 
77
 
78
/* Allocate, initialize a new command line structure for one of the
79
   control commands (if/while).  */
80
 
81
static struct command_line *
82
build_command_line (enum command_control_type type, char *args)
83
{
84
  struct command_line *cmd;
85
 
86
  if (args == NULL)
87
    error ("if/while commands require arguments.\n");
88
 
89
  cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
90
  cmd->next = NULL;
91
  cmd->control_type = type;
92
 
93
  cmd->body_count = 1;
94
  cmd->body_list
95
    = (struct command_line **) xmalloc (sizeof (struct command_line *)
96
                                        * cmd->body_count);
97
  memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
98
  cmd->line = savestring (args, strlen (args));
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 control structure\n");
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
#ifdef UI_OUT
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_text (uiout, "while ");
175
          ui_out_field_fmt (uiout, NULL, "while %s", list->line);
176
          ui_out_text (uiout, "\n");
177
          print_command_lines (uiout, *list->body_list, depth + 1);
178
          ui_out_field_string (uiout, NULL, "end");
179
          if (depth)
180
            ui_out_spaces (uiout, 2 * depth);
181
          ui_out_text (uiout, "end\n");
182
          list = list->next;
183
          continue;
184
        }
185
 
186
      /* An if command.  Recursively print both arms before continueing.  */
187
      if (list->control_type == if_control)
188
        {
189
          ui_out_text (uiout, "if ");
190
          ui_out_field_fmt (uiout, NULL, "if %s", list->line);
191
          ui_out_text (uiout, "\n");
192
          /* The true arm. */
193
          print_command_lines (uiout, list->body_list[0], depth + 1);
194
 
195
          /* Show the false arm if it exists.  */
196
          if (list->body_count == 2)
197
            {
198
              if (depth)
199
                ui_out_spaces (uiout, 2 * depth);
200
              ui_out_field_string (uiout, NULL, "else");
201
              ui_out_text (uiout, "else\n");
202
              print_command_lines (uiout, list->body_list[1], depth + 1);
203
            }
204
 
205
          ui_out_field_string (uiout, NULL, "end");
206
          if (depth)
207
            ui_out_spaces (uiout, 2 * depth);
208
          ui_out_text (uiout, "end\n");
209
          list = list->next;
210
          continue;
211
        }
212
 
213
      /* ignore illegal command type and try next */
214
      list = list->next;
215
    }                           /* while (list) */
216
}
217
#else
218
void
219
print_command_line (struct command_line *cmd, unsigned int depth,
220
                    struct ui_file *stream)
221
{
222
  unsigned int i;
223
 
224
  if (depth)
225
    {
226
      for (i = 0; i < depth; i++)
227
        fputs_filtered ("  ", stream);
228
    }
229
 
230
  /* A simple command, print it and return.  */
231
  if (cmd->control_type == simple_control)
232
    {
233
      fputs_filtered (cmd->line, stream);
234
      fputs_filtered ("\n", stream);
235
      return;
236
    }
237
 
238
  /* loop_continue to jump to the start of a while loop, print it
239
     and return. */
240
  if (cmd->control_type == continue_control)
241
    {
242
      fputs_filtered ("loop_continue\n", stream);
243
      return;
244
    }
245
 
246
  /* loop_break to break out of a while loop, print it and return.  */
247
  if (cmd->control_type == break_control)
248
    {
249
      fputs_filtered ("loop_break\n", stream);
250
      return;
251
    }
252
 
253
  /* A while command.  Recursively print its subcommands before returning.  */
254
  if (cmd->control_type == while_control)
255
    {
256
      struct command_line *list;
257
      fputs_filtered ("while ", stream);
258
      fputs_filtered (cmd->line, stream);
259
      fputs_filtered ("\n", stream);
260
      list = *cmd->body_list;
261
      while (list)
262
        {
263
          print_command_line (list, depth + 1, stream);
264
          list = list->next;
265
        }
266
    }
267
 
268
  /* An if command.  Recursively print both arms before returning.  */
269
  if (cmd->control_type == if_control)
270
    {
271
      fputs_filtered ("if ", stream);
272
      fputs_filtered (cmd->line, stream);
273
      fputs_filtered ("\n", stream);
274
      /* The true arm. */
275
      print_command_line (cmd->body_list[0], depth + 1, stream);
276
 
277
      /* Show the false arm if it exists.  */
278
      if (cmd->body_count == 2)
279
        {
280
          if (depth)
281
            {
282
              for (i = 0; i < depth; i++)
283
                fputs_filtered ("  ", stream);
284
            }
285
          fputs_filtered ("else\n", stream);
286
          print_command_line (cmd->body_list[1], depth + 1, stream);
287
        }
288
      if (depth)
289
        {
290
          for (i = 0; i < depth; i++)
291
            fputs_filtered ("  ", stream);
292
        }
293
      fputs_filtered ("end\n", stream);
294
    }
295
}
296
#endif
297
 
298
/* Execute the command in CMD.  */
299
 
300
void
301
execute_user_command (struct cmd_list_element *c, char *args)
302
{
303
  register struct command_line *cmdlines;
304
  struct cleanup *old_chain;
305
  enum command_control_type ret;
306
 
307
  old_chain = setup_user_args (args);
308
 
309
  cmdlines = c->user_commands;
310
  if (cmdlines == 0)
311
    /* Null command */
312
    return;
313
 
314
  /* Set the instream to 0, indicating execution of a
315
     user-defined function.  */
316
  old_chain = make_cleanup (do_restore_instream_cleanup, instream);
317
  instream = (FILE *) 0;
318
  while (cmdlines)
319
    {
320
      ret = execute_control_command (cmdlines);
321
      if (ret != simple_control && ret != break_control)
322
        {
323
          warning ("Error in control structure.\n");
324
          break;
325
        }
326
      cmdlines = cmdlines->next;
327
    }
328
  do_cleanups (old_chain);
329
}
330
 
331
enum command_control_type
332
execute_control_command (struct command_line *cmd)
333
{
334
  struct expression *expr;
335
  struct command_line *current;
336
  struct cleanup *old_chain = 0;
337
  value_ptr val;
338
  value_ptr val_mark;
339
  int loop;
340
  enum command_control_type ret;
341
  char *new_line;
342
 
343
  switch (cmd->control_type)
344
    {
345
    case simple_control:
346
      /* A simple command, execute it and return.  */
347
      new_line = insert_args (cmd->line);
348
      if (!new_line)
349
        return invalid_control;
350
      old_chain = make_cleanup (free_current_contents, &new_line);
351
      execute_command (new_line, 0);
352
      ret = cmd->control_type;
353
      break;
354
 
355
    case continue_control:
356
    case break_control:
357
      /* Return for "continue", and "break" so we can either
358
         continue the loop at the top, or break out.  */
359
      ret = cmd->control_type;
360
      break;
361
 
362
    case while_control:
363
      {
364
        /* Parse the loop control expression for the while statement.  */
365
        new_line = insert_args (cmd->line);
366
        if (!new_line)
367
          return invalid_control;
368
        old_chain = make_cleanup (free_current_contents, &new_line);
369
        expr = parse_expression (new_line);
370
        make_cleanup (free_current_contents, &expr);
371
 
372
        ret = simple_control;
373
        loop = 1;
374
 
375
        /* Keep iterating so long as the expression is true.  */
376
        while (loop == 1)
377
          {
378
            int cond_result;
379
 
380
            QUIT;
381
 
382
            /* Evaluate the expression.  */
383
            val_mark = value_mark ();
384
            val = evaluate_expression (expr);
385
            cond_result = value_true (val);
386
            value_free_to_mark (val_mark);
387
 
388
            /* If the value is false, then break out of the loop.  */
389
            if (!cond_result)
390
              break;
391
 
392
            /* Execute the body of the while statement.  */
393
            current = *cmd->body_list;
394
            while (current)
395
              {
396
                ret = execute_control_command (current);
397
 
398
                /* If we got an error, or a "break" command, then stop
399
                   looping.  */
400
                if (ret == invalid_control || ret == break_control)
401
                  {
402
                    loop = 0;
403
                    break;
404
                  }
405
 
406
                /* If we got a "continue" command, then restart the loop
407
                   at this point.  */
408
                if (ret == continue_control)
409
                  break;
410
 
411
                /* Get the next statement.  */
412
                current = current->next;
413
              }
414
          }
415
 
416
        /* Reset RET so that we don't recurse the break all the way down.  */
417
        if (ret == break_control)
418
          ret = simple_control;
419
 
420
        break;
421
      }
422
 
423
    case if_control:
424
      {
425
        new_line = insert_args (cmd->line);
426
        if (!new_line)
427
          return invalid_control;
428
        old_chain = make_cleanup (free_current_contents, &new_line);
429
        /* Parse the conditional for the if statement.  */
430
        expr = parse_expression (new_line);
431
        make_cleanup (free_current_contents, &expr);
432
 
433
        current = NULL;
434
        ret = simple_control;
435
 
436
        /* Evaluate the conditional.  */
437
        val_mark = value_mark ();
438
        val = evaluate_expression (expr);
439
 
440
        /* Choose which arm to take commands from based on the value of the
441
           conditional expression.  */
442
        if (value_true (val))
443
          current = *cmd->body_list;
444
        else if (cmd->body_count == 2)
445
          current = *(cmd->body_list + 1);
446
        value_free_to_mark (val_mark);
447
 
448
        /* Execute commands in the given arm.  */
449
        while (current)
450
          {
451
            ret = execute_control_command (current);
452
 
453
            /* If we got an error, get out.  */
454
            if (ret != simple_control)
455
              break;
456
 
457
            /* Get the next statement in the body.  */
458
            current = current->next;
459
          }
460
 
461
        break;
462
      }
463
 
464
    default:
465
      warning ("Invalid control type in command structure.");
466
      return invalid_control;
467
    }
468
 
469
  if (old_chain)
470
    do_cleanups (old_chain);
471
 
472
  return ret;
473
}
474
 
475
/* "while" command support.  Executes a body of statements while the
476
   loop condition is nonzero.  */
477
 
478
void
479
while_command (char *arg, int from_tty)
480
{
481
  struct command_line *command = NULL;
482
 
483
  control_level = 1;
484
  command = get_command_line (while_control, arg);
485
 
486
  if (command == NULL)
487
    return;
488
 
489
  execute_control_command (command);
490
  free_command_lines (&command);
491
}
492
 
493
/* "if" command support.  Execute either the true or false arm depending
494
   on the value of the if conditional.  */
495
 
496
void
497
if_command (char *arg, int from_tty)
498
{
499
  struct command_line *command = NULL;
500
 
501
  control_level = 1;
502
  command = get_command_line (if_control, arg);
503
 
504
  if (command == NULL)
505
    return;
506
 
507
  execute_control_command (command);
508
  free_command_lines (&command);
509
}
510
 
511
/* Cleanup */
512
static void
513
arg_cleanup (void *ignore)
514
{
515
  struct user_args *oargs = user_args;
516
  if (!user_args)
517
    internal_error (__FILE__, __LINE__,
518
                    "arg_cleanup called with no user args.\n");
519
 
520
  user_args = user_args->next;
521
  xfree (oargs);
522
}
523
 
524
/* Bind the incomming arguments for a user defined command to
525
   $arg0, $arg1 ... $argMAXUSERARGS.  */
526
 
527
static struct cleanup *
528
setup_user_args (char *p)
529
{
530
  struct user_args *args;
531
  struct cleanup *old_chain;
532
  unsigned int arg_count = 0;
533
 
534
  args = (struct user_args *) xmalloc (sizeof (struct user_args));
535
  memset (args, 0, sizeof (struct user_args));
536
 
537
  args->next = user_args;
538
  user_args = args;
539
 
540
  old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
541
 
542
  if (p == NULL)
543
    return old_chain;
544
 
545
  while (*p)
546
    {
547
      char *start_arg;
548
      int squote = 0;
549
      int dquote = 0;
550
      int bsquote = 0;
551
 
552
      if (arg_count >= MAXUSERARGS)
553
        {
554
          error ("user defined function may only have %d arguments.\n",
555
                 MAXUSERARGS);
556
          return old_chain;
557
        }
558
 
559
      /* Strip whitespace.  */
560
      while (*p == ' ' || *p == '\t')
561
        p++;
562
 
563
      /* P now points to an argument.  */
564
      start_arg = p;
565
      user_args->a[arg_count].arg = p;
566
 
567
      /* Get to the end of this argument.  */
568
      while (*p)
569
        {
570
          if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
571
            break;
572
          else
573
            {
574
              if (bsquote)
575
                bsquote = 0;
576
              else if (*p == '\\')
577
                bsquote = 1;
578
              else if (squote)
579
                {
580
                  if (*p == '\'')
581
                    squote = 0;
582
                }
583
              else if (dquote)
584
                {
585
                  if (*p == '"')
586
                    dquote = 0;
587
                }
588
              else
589
                {
590
                  if (*p == '\'')
591
                    squote = 1;
592
                  else if (*p == '"')
593
                    dquote = 1;
594
                }
595
              p++;
596
            }
597
        }
598
 
599
      user_args->a[arg_count].len = p - start_arg;
600
      arg_count++;
601
      user_args->count++;
602
    }
603
  return old_chain;
604
}
605
 
606
/* Given character string P, return a point to the first argument ($arg),
607
   or NULL if P contains no arguments.  */
608
 
609
static char *
610
locate_arg (char *p)
611
{
612
  while ((p = strchr (p, '$')))
613
    {
614
      if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
615
        return p;
616
      p++;
617
    }
618
  return NULL;
619
}
620
 
621
/* Insert the user defined arguments stored in user_arg into the $arg
622
   arguments found in line, with the updated copy being placed into nline.  */
623
 
624
static char *
625
insert_args (char *line)
626
{
627
  char *p, *save_line, *new_line;
628
  unsigned len, i;
629
 
630
  /* First we need to know how much memory to allocate for the new line.  */
631
  save_line = line;
632
  len = 0;
633
  while ((p = locate_arg (line)))
634
    {
635
      len += p - line;
636
      i = p[4] - '0';
637
 
638
      if (i >= user_args->count)
639
        {
640
          error ("Missing argument %d in user function.\n", i);
641
          return NULL;
642
        }
643
      len += user_args->a[i].len;
644
      line = p + 5;
645
    }
646
 
647
  /* Don't forget the tail.  */
648
  len += strlen (line);
649
 
650
  /* Allocate space for the new line and fill it in.  */
651
  new_line = (char *) xmalloc (len + 1);
652
  if (new_line == NULL)
653
    return NULL;
654
 
655
  /* Restore pointer to beginning of old line.  */
656
  line = save_line;
657
 
658
  /* Save pointer to beginning of new line.  */
659
  save_line = new_line;
660
 
661
  while ((p = locate_arg (line)))
662
    {
663
      int i, len;
664
 
665
      memcpy (new_line, line, p - line);
666
      new_line += p - line;
667
      i = p[4] - '0';
668
 
669
      len = user_args->a[i].len;
670
      if (len)
671
        {
672
          memcpy (new_line, user_args->a[i].arg, len);
673
          new_line += len;
674
        }
675
      line = p + 5;
676
    }
677
  /* Don't forget the tail.  */
678
  strcpy (new_line, line);
679
 
680
  /* Return a pointer to the beginning of the new line.  */
681
  return save_line;
682
}
683
 
684
 
685
/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
686
   code bodies.  This is typically used when we encounter an "else"
687
   clause for an "if" command.  */
688
 
689
static void
690
realloc_body_list (struct command_line *command, int new_length)
691
{
692
  int n;
693
  struct command_line **body_list;
694
 
695
  n = command->body_count;
696
 
697
  /* Nothing to do?  */
698
  if (new_length <= n)
699
    return;
700
 
701
  body_list = (struct command_line **)
702
    xmalloc (sizeof (struct command_line *) * new_length);
703
 
704
  memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
705
 
706
  xfree (command->body_list);
707
  command->body_list = body_list;
708
  command->body_count = new_length;
709
}
710
 
711
/* Read one line from the input stream.  If the command is an "else" or
712
   "end", return such an indication to the caller.  */
713
 
714
static enum misc_command_type
715
read_next_line (struct command_line **command)
716
{
717
  char *p, *p1, *prompt_ptr, control_prompt[256];
718
  int i = 0;
719
 
720
  if (control_level >= 254)
721
    error ("Control nesting too deep!\n");
722
 
723
  /* Set a prompt based on the nesting of the control commands.  */
724
  if (instream == stdin || (instream == 0 && readline_hook != NULL))
725
    {
726
      for (i = 0; i < control_level; i++)
727
        control_prompt[i] = ' ';
728
      control_prompt[i] = '>';
729
      control_prompt[i + 1] = '\0';
730
      prompt_ptr = (char *) &control_prompt[0];
731
    }
732
  else
733
    prompt_ptr = NULL;
734
 
735
  p = command_line_input (prompt_ptr, instream == stdin, "commands");
736
 
737
  /* Not sure what to do here.  */
738
  if (p == NULL)
739
    return end_command;
740
 
741
  /* Strip leading and trailing whitespace.  */
742
  while (*p == ' ' || *p == '\t')
743
    p++;
744
 
745
  p1 = p + strlen (p);
746
  while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
747
    p1--;
748
 
749
  /* Blanks and comments don't really do anything, but we need to
750
     distinguish them from else, end and other commands which can be
751
     executed.  */
752
  if (p1 == p || p[0] == '#')
753
    return nop_command;
754
 
755
  /* Is this the end of a simple, while, or if control structure?  */
756
  if (p1 - p == 3 && !strncmp (p, "end", 3))
757
    return end_command;
758
 
759
  /* Is the else clause of an if control structure?  */
760
  if (p1 - p == 4 && !strncmp (p, "else", 4))
761
    return else_command;
762
 
763
  /* Check for while, if, break, continue, etc and build a new command
764
     line structure for them.  */
765
  if (p1 - p > 5 && !strncmp (p, "while", 5))
766
    *command = build_command_line (while_control, p + 6);
767
  else if (p1 - p > 2 && !strncmp (p, "if", 2))
768
    *command = build_command_line (if_control, p + 3);
769
  else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
770
    {
771
      *command = (struct command_line *)
772
        xmalloc (sizeof (struct command_line));
773
      (*command)->next = NULL;
774
      (*command)->line = NULL;
775
      (*command)->control_type = break_control;
776
      (*command)->body_count = 0;
777
      (*command)->body_list = NULL;
778
    }
779
  else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
780
    {
781
      *command = (struct command_line *)
782
        xmalloc (sizeof (struct command_line));
783
      (*command)->next = NULL;
784
      (*command)->line = NULL;
785
      (*command)->control_type = continue_control;
786
      (*command)->body_count = 0;
787
      (*command)->body_list = NULL;
788
    }
789
  else
790
    {
791
      /* A normal command.  */
792
      *command = (struct command_line *)
793
        xmalloc (sizeof (struct command_line));
794
      (*command)->next = NULL;
795
      (*command)->line = savestring (p, p1 - p);
796
      (*command)->control_type = simple_control;
797
      (*command)->body_count = 0;
798
      (*command)->body_list = NULL;
799
    }
800
 
801
  /* Nothing special.  */
802
  return ok_command;
803
}
804
 
805
/* Recursively read in the control structures and create a command_line
806
   structure from them.
807
 
808
   The parent_control parameter is the control structure in which the
809
   following commands are nested.  */
810
 
811
static enum command_control_type
812
recurse_read_control_structure (struct command_line *current_cmd)
813
{
814
  int current_body, i;
815
  enum misc_command_type val;
816
  enum command_control_type ret;
817
  struct command_line **body_ptr, *child_tail, *next;
818
 
819
  child_tail = NULL;
820
  current_body = 1;
821
 
822
  /* Sanity checks.  */
823
  if (current_cmd->control_type == simple_control)
824
    {
825
      error ("Recursed on a simple control type\n");
826
      return invalid_control;
827
    }
828
 
829
  if (current_body > current_cmd->body_count)
830
    {
831
      error ("Allocated body is smaller than this command type needs\n");
832
      return invalid_control;
833
    }
834
 
835
  /* Read lines from the input stream and build control structures.  */
836
  while (1)
837
    {
838
      dont_repeat ();
839
 
840
      next = NULL;
841
      val = read_next_line (&next);
842
 
843
      /* Just skip blanks and comments.  */
844
      if (val == nop_command)
845
        continue;
846
 
847
      if (val == end_command)
848
        {
849
          if (current_cmd->control_type == while_control
850
              || current_cmd->control_type == if_control)
851
            {
852
              /* Success reading an entire control structure.  */
853
              ret = simple_control;
854
              break;
855
            }
856
          else
857
            {
858
              ret = invalid_control;
859
              break;
860
            }
861
        }
862
 
863
      /* Not the end of a control structure.  */
864
      if (val == else_command)
865
        {
866
          if (current_cmd->control_type == if_control
867
              && current_body == 1)
868
            {
869
              realloc_body_list (current_cmd, 2);
870
              current_body = 2;
871
              child_tail = NULL;
872
              continue;
873
            }
874
          else
875
            {
876
              ret = invalid_control;
877
              break;
878
            }
879
        }
880
 
881
      if (child_tail)
882
        {
883
          child_tail->next = next;
884
        }
885
      else
886
        {
887
          body_ptr = current_cmd->body_list;
888
          for (i = 1; i < current_body; i++)
889
            body_ptr++;
890
 
891
          *body_ptr = next;
892
 
893
        }
894
 
895
      child_tail = next;
896
 
897
      /* If the latest line is another control structure, then recurse
898
         on it.  */
899
      if (next->control_type == while_control
900
          || next->control_type == if_control)
901
        {
902
          control_level++;
903
          ret = recurse_read_control_structure (next);
904
          control_level--;
905
 
906
          if (ret != simple_control)
907
            break;
908
        }
909
    }
910
 
911
  dont_repeat ();
912
 
913
  return ret;
914
}
915
 
916
/* Read lines from the input stream and accumulate them in a chain of
917
   struct command_line's, which is then returned.  For input from a
918
   terminal, the special command "end" is used to mark the end of the
919
   input, and is not included in the returned chain of commands. */
920
 
921
#define END_MESSAGE "End with a line saying just \"end\"."
922
 
923
struct command_line *
924
read_command_lines (char *prompt_arg, int from_tty)
925
{
926
  struct command_line *head, *tail, *next;
927
  struct cleanup *old_chain;
928
  enum command_control_type ret;
929
  enum misc_command_type val;
930
 
931
  control_level = 0;
932
  if (readline_begin_hook)
933
    {
934
      /* Note - intentional to merge messages with no newline */
935
      (*readline_begin_hook) ("%s  %s\n", prompt_arg, END_MESSAGE);
936
    }
937
  else if (from_tty && input_from_terminal_p ())
938
    {
939
      printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
940
      gdb_flush (gdb_stdout);
941
    }
942
 
943
  head = tail = NULL;
944
  old_chain = NULL;
945
 
946
  while (1)
947
    {
948
      val = read_next_line (&next);
949
 
950
      /* Ignore blank lines or comments.  */
951
      if (val == nop_command)
952
        continue;
953
 
954
      if (val == end_command)
955
        {
956
          ret = simple_control;
957
          break;
958
        }
959
 
960
      if (val != ok_command)
961
        {
962
          ret = invalid_control;
963
          break;
964
        }
965
 
966
      if (next->control_type == while_control
967
          || next->control_type == if_control)
968
        {
969
          control_level++;
970
          ret = recurse_read_control_structure (next);
971
          control_level--;
972
 
973
          if (ret == invalid_control)
974
            break;
975
        }
976
 
977
      if (tail)
978
        {
979
          tail->next = next;
980
        }
981
      else
982
        {
983
          head = next;
984
          old_chain = make_cleanup_free_command_lines (&head);
985
        }
986
      tail = next;
987
    }
988
 
989
  dont_repeat ();
990
 
991
  if (head)
992
    {
993
      if (ret != invalid_control)
994
        {
995
          discard_cleanups (old_chain);
996
        }
997
      else
998
        do_cleanups (old_chain);
999
    }
1000
 
1001
  if (readline_end_hook)
1002
    {
1003
      (*readline_end_hook) ();
1004
    }
1005
  return (head);
1006
}
1007
 
1008
/* Free a chain of struct command_line's.  */
1009
 
1010
void
1011
free_command_lines (struct command_line **lptr)
1012
{
1013
  register struct command_line *l = *lptr;
1014
  register struct command_line *next;
1015
  struct command_line **blist;
1016
  int i;
1017
 
1018
  while (l)
1019
    {
1020
      if (l->body_count > 0)
1021
        {
1022
          blist = l->body_list;
1023
          for (i = 0; i < l->body_count; i++, blist++)
1024
            free_command_lines (blist);
1025
        }
1026
      next = l->next;
1027
      xfree (l->line);
1028
      xfree (l);
1029
      l = next;
1030
    }
1031
  *lptr = NULL;
1032
}
1033
 
1034
static void
1035
do_free_command_lines_cleanup (void *arg)
1036
{
1037
  free_command_lines (arg);
1038
}
1039
 
1040
static struct cleanup *
1041
make_cleanup_free_command_lines (struct command_line **arg)
1042
{
1043
  return make_cleanup (do_free_command_lines_cleanup, arg);
1044
}
1045
 
1046
static void
1047
validate_comname (char *comname)
1048
{
1049
  register char *p;
1050
 
1051
  if (comname == 0)
1052
    error_no_arg ("name of command to define");
1053
 
1054
  p = comname;
1055
  while (*p)
1056
    {
1057
      if (!isalnum (*p) && *p != '-' && *p != '_')
1058
        error ("Junk in argument list: \"%s\"", p);
1059
      p++;
1060
    }
1061
}
1062
 
1063
/* This is just a placeholder in the command data structures.  */
1064
static void
1065
user_defined_command (char *ignore, int from_tty)
1066
{
1067
}
1068
 
1069
void
1070
define_command (char *comname, int from_tty)
1071
{
1072
#define MAX_TMPBUF 128   
1073
  enum cmd_hook_type
1074
    {
1075
      CMD_NO_HOOK = 0,
1076
      CMD_PRE_HOOK,
1077
      CMD_POST_HOOK
1078
    };
1079
  register struct command_line *cmds;
1080
  register struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1081
  char *tem = comname;
1082
  char *tem2;
1083
  char tmpbuf[MAX_TMPBUF];
1084
  int  hook_type      = CMD_NO_HOOK;
1085
  int  hook_name_size = 0;
1086
 
1087
#define HOOK_STRING     "hook-"
1088
#define HOOK_LEN 5
1089
#define HOOK_POST_STRING "hookpost-"
1090
#define HOOK_POST_LEN    9
1091
 
1092
  validate_comname (comname);
1093
 
1094
  /* Look it up, and verify that we got an exact match.  */
1095
  c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1096
  if (c && !STREQ (comname, c->name))
1097
    c = 0;
1098
 
1099
  if (c)
1100
    {
1101
      if (c->class == class_user || c->class == class_alias)
1102
        tem = "Redefine command \"%s\"? ";
1103
      else
1104
        tem = "Really redefine built-in command \"%s\"? ";
1105
      if (!query (tem, c->name))
1106
        error ("Command \"%s\" not redefined.", c->name);
1107
    }
1108
 
1109
  /* If this new command is a hook, then mark the command which it
1110
     is hooking.  Note that we allow hooking `help' commands, so that
1111
     we can hook the `stop' pseudo-command.  */
1112
 
1113
  if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1114
    {
1115
       hook_type      = CMD_PRE_HOOK;
1116
       hook_name_size = HOOK_LEN;
1117
    }
1118
  else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1119
    {
1120
      hook_type      = CMD_POST_HOOK;
1121
      hook_name_size = HOOK_POST_LEN;
1122
    }
1123
 
1124
  if (hook_type != CMD_NO_HOOK)
1125
    {
1126
      /* Look up cmd it hooks, and verify that we got an exact match.  */
1127
      tem = comname + hook_name_size;
1128
      hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1129
      if (hookc && !STREQ (comname + hook_name_size, hookc->name))
1130
        hookc = 0;
1131
      if (!hookc)
1132
        {
1133
          warning ("Your new `%s' command does not hook any existing command.",
1134
                   comname);
1135
          if (!query ("Proceed? "))
1136
            error ("Not confirmed.");
1137
        }
1138
    }
1139
 
1140
  comname = savestring (comname, strlen (comname));
1141
 
1142
  /* If the rest of the commands will be case insensitive, this one
1143
     should behave in the same manner. */
1144
  for (tem = comname; *tem; tem++)
1145
    if (isupper (*tem))
1146
      *tem = tolower (*tem);
1147
 
1148
  sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1149
  cmds = read_command_lines (tmpbuf, from_tty);
1150
 
1151
  if (c && c->class == class_user)
1152
    free_command_lines (&c->user_commands);
1153
 
1154
  newc = add_cmd (comname, class_user, user_defined_command,
1155
                  (c && c->class == class_user)
1156
                  ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1157
  newc->user_commands = cmds;
1158
 
1159
  /* If this new command is a hook, then mark both commands as being
1160
     tied.  */
1161
  if (hookc)
1162
    {
1163
      switch (hook_type)
1164
        {
1165
        case CMD_PRE_HOOK:
1166
          hookc->hook_pre  = newc;  /* Target gets hooked.  */
1167
          newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1168
          break;
1169
        case CMD_POST_HOOK:
1170
          hookc->hook_post  = newc;  /* Target gets hooked.  */
1171
          newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1172
          break;
1173
        default:
1174
          /* Should never come here as hookc would be 0. */
1175
          internal_error (__FILE__, __LINE__, "bad switch");
1176
        }
1177
    }
1178
}
1179
 
1180
void
1181
document_command (char *comname, int from_tty)
1182
{
1183
  struct command_line *doclines;
1184
  register struct cmd_list_element *c;
1185
  char *tem = comname;
1186
  char tmpbuf[128];
1187
 
1188
  validate_comname (comname);
1189
 
1190
  c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1191
 
1192
  if (c->class != class_user)
1193
    error ("Command \"%s\" is built-in.", comname);
1194
 
1195
  sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1196
  doclines = read_command_lines (tmpbuf, from_tty);
1197
 
1198
  if (c->doc)
1199
    xfree (c->doc);
1200
 
1201
  {
1202
    register struct command_line *cl1;
1203
    register int len = 0;
1204
 
1205
    for (cl1 = doclines; cl1; cl1 = cl1->next)
1206
      len += strlen (cl1->line) + 1;
1207
 
1208
    c->doc = (char *) xmalloc (len + 1);
1209
    *c->doc = 0;
1210
 
1211
    for (cl1 = doclines; cl1; cl1 = cl1->next)
1212
      {
1213
        strcat (c->doc, cl1->line);
1214
        if (cl1->next)
1215
          strcat (c->doc, "\n");
1216
      }
1217
  }
1218
 
1219
  free_command_lines (&doclines);
1220
}
1221
 
1222
struct source_cleanup_lines_args
1223
{
1224
  int old_line;
1225
  char *old_file;
1226
  char *old_pre_error;
1227
  char *old_error_pre_print;
1228
};
1229
 
1230
static void
1231
source_cleanup_lines (PTR args)
1232
{
1233
  struct source_cleanup_lines_args *p =
1234
  (struct source_cleanup_lines_args *) args;
1235
  source_line_number = p->old_line;
1236
  source_file_name = p->old_file;
1237
  source_pre_error = p->old_pre_error;
1238
  error_pre_print = p->old_error_pre_print;
1239
}
1240
 
1241
/* ARGSUSED */
1242
static void
1243
do_fclose_cleanup (void *stream)
1244
{
1245
  fclose (stream);
1246
}
1247
 
1248
/* Used to implement source_command */
1249
 
1250
void
1251
script_from_file (FILE *stream, char *file)
1252
{
1253
  struct cleanup *old_cleanups;
1254
  struct source_cleanup_lines_args old_lines;
1255
  int needed_length;
1256
 
1257
  if (stream == NULL)
1258
    {
1259
      internal_error (__FILE__, __LINE__, "called with NULL file pointer!");
1260
    }
1261
 
1262
  old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1263
 
1264
  old_lines.old_line = source_line_number;
1265
  old_lines.old_file = source_file_name;
1266
  old_lines.old_pre_error = source_pre_error;
1267
  old_lines.old_error_pre_print = error_pre_print;
1268
  make_cleanup (source_cleanup_lines, &old_lines);
1269
  source_line_number = 0;
1270
  source_file_name = file;
1271
  source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
1272
  source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
1273
  make_cleanup (xfree, source_pre_error);
1274
  /* This will get set every time we read a line.  So it won't stay "" for
1275
     long.  */
1276
  error_pre_print = "";
1277
 
1278
  needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
1279
  if (source_error_allocated < needed_length)
1280
    {
1281
      source_error_allocated *= 2;
1282
      if (source_error_allocated < needed_length)
1283
        source_error_allocated = needed_length;
1284
      if (source_error == NULL)
1285
        source_error = xmalloc (source_error_allocated);
1286
      else
1287
        source_error = xrealloc (source_error, source_error_allocated);
1288
    }
1289
 
1290
  read_command_file (stream);
1291
 
1292
  do_cleanups (old_cleanups);
1293
}
1294
 
1295
void
1296
show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1297
{
1298
  register struct command_line *cmdlines;
1299
 
1300
  cmdlines = c->user_commands;
1301
  if (!cmdlines)
1302
    return;
1303
  fputs_filtered ("User command ", stream);
1304
  fputs_filtered (c->name, stream);
1305
  fputs_filtered (":\n", stream);
1306
 
1307
#ifdef UI_OUT
1308
  print_command_lines (uiout, cmdlines, 1);
1309
  fputs_filtered ("\n", stream);
1310
#else
1311
  while (cmdlines)
1312
    {
1313
      print_command_line (cmdlines, 4, stream);
1314
      cmdlines = cmdlines->next;
1315
    }
1316
  fputs_filtered ("\n", stream);
1317
#endif
1318
}
1319
 

powered by: WebSVN 2.1.0

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