forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli-script.c
1724 lines (1426 loc) · 43.5 KB
/
cli-script.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GDB CLI command scripting.
Copyright (C) 1986-2016 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "value.h"
#include "language.h" /* For value_true */
#include <ctype.h>
#include "ui-out.h"
#include "top.h"
#include "breakpoint.h"
#include "cli/cli-cmds.h"
#include "cli/cli-decode.h"
#include "cli/cli-script.h"
#include "extension.h"
#include "interps.h"
#include "compile/compile.h"
#include <vector>
/* Prototypes for local functions. */
static enum command_control_type
recurse_read_control_structure (char * (*read_next_line_func) (void),
struct command_line *current_cmd,
void (*validator)(char *, void *),
void *closure);
static char *read_next_line (void);
/* Level of control structure when reading. */
static int control_level;
/* Level of control structure when executing. */
static int command_nest_depth = 1;
/* This is to prevent certain commands being printed twice. */
static int suppress_next_print_command_trace = 0;
/* A non-owning slice of a string. */
struct string_view
{
string_view (const char *str_, size_t len_)
: str (str_), len (len_)
{}
const char *str;
size_t len;
};
/* Structure for arguments to user defined functions. */
class user_args
{
public:
/* Save the command line and store the locations of arguments passed
to the user defined function. */
explicit user_args (const char *line);
/* Insert the stored user defined arguments into the $arg arguments
found in LINE. */
std::string insert_args (const char *line) const;
private:
/* Disable copy/assignment. (Since the elements of A point inside
COMMAND, copying would need to reconstruct the A vector in the
new copy.) */
user_args (const user_args &) =delete;
user_args &operator= (const user_args &) =delete;
/* It is necessary to store a copy of the command line to ensure
that the arguments are not overwritten before they are used. */
std::string m_command_line;
/* The arguments. Each element points inside M_COMMAND_LINE. */
std::vector<string_view> m_args;
};
/* The stack of arguments passed to user defined functions. We need a
stack because user-defined functions can call other user-defined
functions. */
static std::vector<std::unique_ptr<user_args>> user_args_stack;
/* An RAII-base class used to push/pop args on the user args
stack. */
struct scoped_user_args_level
{
/* Parse the command line and push the arguments in the user args
stack. */
explicit scoped_user_args_level (const char *line)
{
user_args_stack.emplace_back (new user_args (line));
}
/* Pop the current user arguments from the stack. */
~scoped_user_args_level ()
{
user_args_stack.pop_back ();
}
};
/* Return non-zero if TYPE is a multi-line command (i.e., is terminated
by "end"). */
static int
multi_line_command_p (enum command_control_type type)
{
switch (type)
{
case if_control:
case while_control:
case while_stepping_control:
case commands_control:
case compile_control:
case python_control:
case guile_control:
return 1;
default:
return 0;
}
}
/* Allocate, initialize a new command line structure for one of the
control commands (if/while). */
static struct command_line *
build_command_line (enum command_control_type type, char *args)
{
struct command_line *cmd;
if (args == NULL && (type == if_control || type == while_control))
error (_("if/while commands require arguments."));
gdb_assert (args != NULL);
cmd = XNEW (struct command_line);
cmd->next = NULL;
cmd->control_type = type;
cmd->body_count = 1;
cmd->body_list = XCNEWVEC (struct command_line *, cmd->body_count);
cmd->line = xstrdup (args);
return cmd;
}
/* Build and return a new command structure for the control commands
such as "if" and "while". */
struct command_line *
get_command_line (enum command_control_type type, char *arg)
{
struct command_line *cmd;
struct cleanup *old_chain = NULL;
/* Allocate and build a new command line structure. */
cmd = build_command_line (type, arg);
old_chain = make_cleanup_free_command_lines (&cmd);
/* Read in the body of this command. */
if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
== invalid_control)
{
warning (_("Error reading in canned sequence of commands."));
do_cleanups (old_chain);
return NULL;
}
discard_cleanups (old_chain);
return cmd;
}
/* Recursively print a command (including full control structures). */
void
print_command_lines (struct ui_out *uiout, struct command_line *cmd,
unsigned int depth)
{
struct command_line *list;
list = cmd;
while (list)
{
if (depth)
ui_out_spaces (uiout, 2 * depth);
/* A simple command, print it and continue. */
if (list->control_type == simple_control)
{
ui_out_field_string (uiout, NULL, list->line);
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
/* loop_continue to jump to the start of a while loop, print it
and continue. */
if (list->control_type == continue_control)
{
ui_out_field_string (uiout, NULL, "loop_continue");
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
/* loop_break to break out of a while loop, print it and
continue. */
if (list->control_type == break_control)
{
ui_out_field_string (uiout, NULL, "loop_break");
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
/* A while command. Recursively print its subcommands and
continue. */
if (list->control_type == while_control
|| list->control_type == while_stepping_control)
{
/* For while-stepping, the line includes the 'while-stepping'
token. See comment in process_next_line for explanation.
Here, take care not print 'while-stepping' twice. */
if (list->control_type == while_control)
ui_out_field_fmt (uiout, NULL, "while %s", list->line);
else
ui_out_field_string (uiout, NULL, list->line);
ui_out_text (uiout, "\n");
print_command_lines (uiout, *list->body_list, depth + 1);
if (depth)
ui_out_spaces (uiout, 2 * depth);
ui_out_field_string (uiout, NULL, "end");
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
/* An if command. Recursively print both arms before
continueing. */
if (list->control_type == if_control)
{
ui_out_field_fmt (uiout, NULL, "if %s", list->line);
ui_out_text (uiout, "\n");
/* The true arm. */
print_command_lines (uiout, list->body_list[0], depth + 1);
/* Show the false arm if it exists. */
if (list->body_count == 2)
{
if (depth)
ui_out_spaces (uiout, 2 * depth);
ui_out_field_string (uiout, NULL, "else");
ui_out_text (uiout, "\n");
print_command_lines (uiout, list->body_list[1], depth + 1);
}
if (depth)
ui_out_spaces (uiout, 2 * depth);
ui_out_field_string (uiout, NULL, "end");
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
/* A commands command. Print the breakpoint commands and
continue. */
if (list->control_type == commands_control)
{
if (*(list->line))
ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
else
ui_out_field_string (uiout, NULL, "commands");
ui_out_text (uiout, "\n");
print_command_lines (uiout, *list->body_list, depth + 1);
if (depth)
ui_out_spaces (uiout, 2 * depth);
ui_out_field_string (uiout, NULL, "end");
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
if (list->control_type == python_control)
{
ui_out_field_string (uiout, NULL, "python");
ui_out_text (uiout, "\n");
/* Don't indent python code at all. */
print_command_lines (uiout, *list->body_list, 0);
if (depth)
ui_out_spaces (uiout, 2 * depth);
ui_out_field_string (uiout, NULL, "end");
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
if (list->control_type == compile_control)
{
ui_out_field_string (uiout, NULL, "compile expression");
ui_out_text (uiout, "\n");
print_command_lines (uiout, *list->body_list, 0);
if (depth)
ui_out_spaces (uiout, 2 * depth);
ui_out_field_string (uiout, NULL, "end");
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
if (list->control_type == guile_control)
{
ui_out_field_string (uiout, NULL, "guile");
ui_out_text (uiout, "\n");
print_command_lines (uiout, *list->body_list, depth + 1);
if (depth)
ui_out_spaces (uiout, 2 * depth);
ui_out_field_string (uiout, NULL, "end");
ui_out_text (uiout, "\n");
list = list->next;
continue;
}
/* Ignore illegal command type and try next. */
list = list->next;
} /* while (list) */
}
/* Handle pre-post hooks. */
static void
clear_hook_in_cleanup (void *data)
{
struct cmd_list_element *c = (struct cmd_list_element *) data;
c->hook_in = 0; /* Allow hook to work again once it is complete. */
}
void
execute_cmd_pre_hook (struct cmd_list_element *c)
{
if ((c->hook_pre) && (!c->hook_in))
{
struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
c->hook_in = 1; /* Prevent recursive hooking. */
execute_user_command (c->hook_pre, (char *) 0);
do_cleanups (cleanups);
}
}
void
execute_cmd_post_hook (struct cmd_list_element *c)
{
if ((c->hook_post) && (!c->hook_in))
{
struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
c->hook_in = 1; /* Prevent recursive hooking. */
execute_user_command (c->hook_post, (char *) 0);
do_cleanups (cleanups);
}
}
/* Execute the command in CMD. */
static void
do_restore_user_call_depth (void * call_depth)
{
int *depth = (int *) call_depth;
(*depth)--;
if ((*depth) == 0)
in_user_command = 0;
}
void
execute_user_command (struct cmd_list_element *c, char *args)
{
struct ui *ui = current_ui;
struct command_line *cmdlines;
struct cleanup *old_chain;
enum command_control_type ret;
static int user_call_depth = 0;
extern unsigned int max_user_call_depth;
cmdlines = c->user_commands;
if (cmdlines == 0)
/* Null command */
return;
scoped_user_args_level push_user_args (args);
if (++user_call_depth > max_user_call_depth)
error (_("Max user call depth exceeded -- command aborted."));
old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth);
/* Set the instream to 0, indicating execution of a
user-defined function. */
make_cleanup (do_restore_instream_cleanup, ui->instream);
ui->instream = NULL;
/* Also set the global in_user_command, so that NULL instream is
not confused with Insight. */
in_user_command = 1;
scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
command_nest_depth++;
while (cmdlines)
{
ret = execute_control_command (cmdlines);
if (ret != simple_control && ret != break_control)
{
warning (_("Error executing canned sequence of commands."));
break;
}
cmdlines = cmdlines->next;
}
command_nest_depth--;
do_cleanups (old_chain);
}
/* This function is called every time GDB prints a prompt. It ensures
that errors and the like do not confuse the command tracing. */
void
reset_command_nest_depth (void)
{
command_nest_depth = 1;
/* Just in case. */
suppress_next_print_command_trace = 0;
}
/* Print the command, prefixed with '+' to represent the call depth.
This is slightly complicated because this function may be called
from execute_command and execute_control_command. Unfortunately
execute_command also prints the top level control commands.
In these cases execute_command will call execute_control_command
via while_command or if_command. Inner levels of 'if' and 'while'
are dealt with directly. Therefore we can use these functions
to determine whether the command has been printed already or not. */
void
print_command_trace (const char *cmd)
{
int i;
if (suppress_next_print_command_trace)
{
suppress_next_print_command_trace = 0;
return;
}
if (!source_verbose && !trace_commands)
return;
for (i=0; i < command_nest_depth; i++)
printf_filtered ("+");
printf_filtered ("%s\n", cmd);
}
enum command_control_type
execute_control_command (struct command_line *cmd)
{
struct command_line *current;
struct value *val;
struct value *val_mark;
int loop;
enum command_control_type ret;
/* Start by assuming failure, if a problem is detected, the code
below will simply "break" out of the switch. */
ret = invalid_control;
switch (cmd->control_type)
{
case simple_control:
{
/* A simple command, execute it and return. */
std::string new_line = insert_user_defined_cmd_args (cmd->line);
execute_command (&new_line[0], 0);
ret = cmd->control_type;
break;
}
case continue_control:
print_command_trace ("loop_continue");
/* Return for "continue", and "break" so we can either
continue the loop at the top, or break out. */
ret = cmd->control_type;
break;
case break_control:
print_command_trace ("loop_break");
/* Return for "continue", and "break" so we can either
continue the loop at the top, or break out. */
ret = cmd->control_type;
break;
case while_control:
{
int len = strlen (cmd->line) + 7;
char *buffer = (char *) alloca (len);
xsnprintf (buffer, len, "while %s", cmd->line);
print_command_trace (buffer);
/* Parse the loop control expression for the while statement. */
std::string new_line = insert_user_defined_cmd_args (cmd->line);
expression_up expr = parse_expression (new_line.c_str ());
ret = simple_control;
loop = 1;
/* Keep iterating so long as the expression is true. */
while (loop == 1)
{
int cond_result;
QUIT;
/* Evaluate the expression. */
val_mark = value_mark ();
val = evaluate_expression (expr.get ());
cond_result = value_true (val);
value_free_to_mark (val_mark);
/* If the value is false, then break out of the loop. */
if (!cond_result)
break;
/* Execute the body of the while statement. */
current = *cmd->body_list;
while (current)
{
command_nest_depth++;
ret = execute_control_command (current);
command_nest_depth--;
/* If we got an error, or a "break" command, then stop
looping. */
if (ret == invalid_control || ret == break_control)
{
loop = 0;
break;
}
/* If we got a "continue" command, then restart the loop
at this point. */
if (ret == continue_control)
break;
/* Get the next statement. */
current = current->next;
}
}
/* Reset RET so that we don't recurse the break all the way down. */
if (ret == break_control)
ret = simple_control;
break;
}
case if_control:
{
int len = strlen (cmd->line) + 4;
char *buffer = (char *) alloca (len);
xsnprintf (buffer, len, "if %s", cmd->line);
print_command_trace (buffer);
/* Parse the conditional for the if statement. */
std::string new_line = insert_user_defined_cmd_args (cmd->line);
expression_up expr = parse_expression (new_line.c_str ());
current = NULL;
ret = simple_control;
/* Evaluate the conditional. */
val_mark = value_mark ();
val = evaluate_expression (expr.get ());
/* Choose which arm to take commands from based on the value
of the conditional expression. */
if (value_true (val))
current = *cmd->body_list;
else if (cmd->body_count == 2)
current = *(cmd->body_list + 1);
value_free_to_mark (val_mark);
/* Execute commands in the given arm. */
while (current)
{
command_nest_depth++;
ret = execute_control_command (current);
command_nest_depth--;
/* If we got an error, get out. */
if (ret != simple_control)
break;
/* Get the next statement in the body. */
current = current->next;
}
break;
}
case commands_control:
{
/* Breakpoint commands list, record the commands in the
breakpoint's command list and return. */
std::string new_line = insert_user_defined_cmd_args (cmd->line);
ret = commands_from_control_command (new_line.c_str (), cmd);
break;
}
case compile_control:
eval_compile_command (cmd, NULL, cmd->control_u.compile.scope,
cmd->control_u.compile.scope_data);
ret = simple_control;
break;
case python_control:
case guile_control:
{
eval_ext_lang_from_control_command (cmd);
ret = simple_control;
break;
}
default:
warning (_("Invalid control type in canned commands structure."));
break;
}
return ret;
}
/* Like execute_control_command, but first set
suppress_next_print_command_trace. */
enum command_control_type
execute_control_command_untraced (struct command_line *cmd)
{
suppress_next_print_command_trace = 1;
return execute_control_command (cmd);
}
/* "while" command support. Executes a body of statements while the
loop condition is nonzero. */
static void
while_command (char *arg, int from_tty)
{
struct command_line *command = NULL;
control_level = 1;
command = get_command_line (while_control, arg);
if (command == NULL)
return;
scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
execute_control_command_untraced (command);
free_command_lines (&command);
}
/* "if" command support. Execute either the true or false arm depending
on the value of the if conditional. */
static void
if_command (char *arg, int from_tty)
{
struct command_line *command = NULL;
struct cleanup *old_chain;
control_level = 1;
command = get_command_line (if_control, arg);
if (command == NULL)
return;
scoped_restore save_async = make_scoped_restore (¤t_ui->async, 0);
execute_control_command_untraced (command);
free_command_lines (&command);
}
/* Bind the incoming arguments for a user defined command to $arg0,
$arg1 ... $argN. */
user_args::user_args (const char *command_line)
{
const char *p;
if (command_line == NULL)
return;
m_command_line = command_line;
p = m_command_line.c_str ();
while (*p)
{
const char *start_arg;
int squote = 0;
int dquote = 0;
int bsquote = 0;
/* Strip whitespace. */
while (*p == ' ' || *p == '\t')
p++;
/* P now points to an argument. */
start_arg = p;
/* Get to the end of this argument. */
while (*p)
{
if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
break;
else
{
if (bsquote)
bsquote = 0;
else if (*p == '\\')
bsquote = 1;
else if (squote)
{
if (*p == '\'')
squote = 0;
}
else if (dquote)
{
if (*p == '"')
dquote = 0;
}
else
{
if (*p == '\'')
squote = 1;
else if (*p == '"')
dquote = 1;
}
p++;
}
}
m_args.emplace_back (start_arg, p - start_arg);
}
}
/* Given character string P, return a point to the first argument
($arg), or NULL if P contains no arguments. */
static const char *
locate_arg (const char *p)
{
while ((p = strchr (p, '$')))
{
if (startswith (p, "$arg")
&& (isdigit (p[4]) || p[4] == 'c'))
return p;
p++;
}
return NULL;
}
/* See cli-script.h. */
std::string
insert_user_defined_cmd_args (const char *line)
{
/* If we are not in a user-defined command, treat $argc, $arg0, et
cetera as normal convenience variables. */
if (user_args_stack.empty ())
return line;
const std::unique_ptr<user_args> &args = user_args_stack.back ();
return args->insert_args (line);
}
/* Insert the user defined arguments stored in user_args into the $arg
arguments found in line. */
std::string
user_args::insert_args (const char *line) const
{
std::string new_line;
const char *p;
while ((p = locate_arg (line)))
{
new_line.append (line, p - line);
if (p[4] == 'c')
{
new_line += std::to_string (m_args.size ());
line = p + 5;
}
else
{
char *tmp;
unsigned long i;
errno = 0;
i = strtoul (p + 4, &tmp, 10);
if ((i == 0 && tmp == p + 4) || errno != 0)
line = p + 4;
else if (i >= m_args.size ())
error (_("Missing argument %ld in user function."), i);
else
{
new_line.append (m_args[i].str, m_args[i].len);
line = tmp;
}
}
}
/* Don't forget the tail. */
new_line.append (line);
return new_line;
}
/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
code bodies. This is typically used when we encounter an "else"
clause for an "if" command. */
static void
realloc_body_list (struct command_line *command, int new_length)
{
int n;
struct command_line **body_list;
n = command->body_count;
/* Nothing to do? */
if (new_length <= n)
return;
body_list = XCNEWVEC (struct command_line *, new_length);
memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
xfree (command->body_list);
command->body_list = body_list;
command->body_count = new_length;
}
/* Read next line from stdin. Passed to read_command_line_1 and
recurse_read_control_structure whenever we need to read commands
from stdin. */
static char *
read_next_line (void)
{
struct ui *ui = current_ui;
char *prompt_ptr, control_prompt[256];
int i = 0;
int from_tty = ui->instream == ui->stdin_stream;
if (control_level >= 254)
error (_("Control nesting too deep!"));
/* Set a prompt based on the nesting of the control commands. */
if (from_tty
|| (ui->instream == 0 && deprecated_readline_hook != NULL))
{
for (i = 0; i < control_level; i++)
control_prompt[i] = ' ';
control_prompt[i] = '>';
control_prompt[i + 1] = '\0';
prompt_ptr = (char *) &control_prompt[0];
}
else
prompt_ptr = NULL;
return command_line_input (prompt_ptr, from_tty, "commands");
}
/* Process one input line. If the command is an "end", return such an
indication to the caller. If PARSE_COMMANDS is true, strip leading
whitespace (trailing whitespace is always stripped) in the line,
attempt to recognize GDB control commands, and also return an
indication if the command is an "else" or a nop.
Otherwise, only "end" is recognized. */
static enum misc_command_type
process_next_line (char *p, struct command_line **command, int parse_commands,
void (*validator)(char *, void *), void *closure)
{
char *p_end;
char *p_start;
int not_handled = 0;
/* Not sure what to do here. */
if (p == NULL)
return end_command;
/* Strip trailing whitespace. */
p_end = p + strlen (p);
while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
p_end--;
p_start = p;
/* Strip leading whitespace. */
while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
p_start++;
/* 'end' is always recognized, regardless of parse_commands value.
We also permit whitespace before end and after. */
if (p_end - p_start == 3 && startswith (p_start, "end"))
return end_command;
if (parse_commands)
{
/* If commands are parsed, we skip initial spaces. Otherwise,
which is the case for Python commands and documentation
(see the 'document' command), spaces are preserved. */
p = p_start;
/* Blanks and comments don't really do anything, but we need to
distinguish them from else, end and other commands which can
be executed. */
if (p_end == p || p[0] == '#')
return nop_command;
/* Is the else clause of an if control structure? */
if (p_end - p == 4 && startswith (p, "else"))
return else_command;
/* Check for while, if, break, continue, etc and build a new
command line structure for them. */
if ((p_end - p >= 14 && startswith (p, "while-stepping"))
|| (p_end - p >= 8 && startswith (p, "stepping"))
|| (p_end - p >= 2 && startswith (p, "ws")))
{
/* Because validate_actionline and encode_action lookup
command's line as command, we need the line to
include 'while-stepping'.
For 'ws' alias, the command will have 'ws', not expanded
to 'while-stepping'. This is intentional -- we don't
really want frontend to send a command list with 'ws',
and next break-info returning command line with
'while-stepping'. This should work, but might cause the
breakpoint to be marked as changed while it's actually
not. */
*command = build_command_line (while_stepping_control, p);
}
else if (p_end - p > 5 && startswith (p, "while"))
{
char *first_arg;
first_arg = p + 5;
while (first_arg < p_end && isspace (*first_arg))
first_arg++;
*command = build_command_line (while_control, first_arg);
}
else if (p_end - p > 2 && startswith (p, "if"))
{
char *first_arg;
first_arg = p + 2;
while (first_arg < p_end && isspace (*first_arg))
first_arg++;
*command = build_command_line (if_control, first_arg);
}
else if (p_end - p >= 8 && startswith (p, "commands"))
{
char *first_arg;
first_arg = p + 8;