-
Notifications
You must be signed in to change notification settings - Fork 181
/
print_cmd.c
1667 lines (1434 loc) · 39.8 KB
/
print_cmd.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
/* print_command -- A way to make readable commands from a command tree. */
/* Copyright (C) 1989-2022 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash 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.
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#if defined (PREFER_STDARG)
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "bashansi.h"
#include "bashintl.h"
#define NEED_XTRACE_SET_DECL
#include "shell.h"
#include "flags.h"
#include <y.tab.h> /* use <...> so we pick it up from the build directory */
#include "input.h"
#include "shmbutil.h"
#include "builtins/common.h"
#if !HAVE_DECL_PRINTF
extern int printf PARAMS((const char *, ...)); /* Yuck. Double yuck. */
#endif
static int indentation;
static int indentation_amount = 4;
#if defined (PREFER_STDARG)
typedef void PFUNC PARAMS((const char *, ...));
static void cprintf PARAMS((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
static void xprintf PARAMS((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
#else
#define PFUNC VFunction
static void cprintf ();
static void xprintf ();
#endif
static void reset_locals PARAMS((void));
static void newline PARAMS((char *));
static void indent PARAMS((int));
static void semicolon PARAMS((void));
static void the_printed_command_resize PARAMS((int));
static void make_command_string_internal PARAMS((COMMAND *));
static void _print_word_list PARAMS((WORD_LIST *, char *, PFUNC *));
static void command_print_word_list PARAMS((WORD_LIST *, char *));
static void print_case_clauses PARAMS((PATTERN_LIST *));
static void print_redirection_list PARAMS((REDIRECT *));
static void print_redirection PARAMS((REDIRECT *));
static void print_heredoc_header PARAMS((REDIRECT *));
static void print_heredoc_body PARAMS((REDIRECT *));
static void print_heredocs PARAMS((REDIRECT *));
static void print_heredoc_bodies PARAMS((REDIRECT *));
static void print_deferred_heredocs PARAMS((const char *));
static void print_for_command PARAMS((FOR_COM *));
#if defined (ARITH_FOR_COMMAND)
static void print_arith_for_command PARAMS((ARITH_FOR_COM *));
#endif
#if defined (SELECT_COMMAND)
static void print_select_command PARAMS((SELECT_COM *));
#endif
static void print_group_command PARAMS((GROUP_COM *));
static void print_case_command PARAMS((CASE_COM *));
static void print_while_command PARAMS((WHILE_COM *));
static void print_until_command PARAMS((WHILE_COM *));
static void print_until_or_while PARAMS((WHILE_COM *, char *));
static void print_if_command PARAMS((IF_COM *));
#if defined (COND_COMMAND)
static void print_cond_node PARAMS((COND_COM *));
#endif
static void print_function_def PARAMS((FUNCTION_DEF *));
#define PRINTED_COMMAND_INITIAL_SIZE 64
#define PRINTED_COMMAND_GROW_SIZE 128
char *the_printed_command = (char *)NULL;
int the_printed_command_size = 0;
int command_string_index = 0;
int xtrace_fd = -1;
FILE *xtrace_fp = 0;
#define CHECK_XTRACE_FP xtrace_fp = (xtrace_fp ? xtrace_fp : stderr)
/* shell expansion characters: used in print_redirection_list */
#define EXPCHAR(c) ((c) == '{' || (c) == '~' || (c) == '$' || (c) == '`')
#define PRINT_DEFERRED_HEREDOCS(x) \
do { \
if (deferred_heredocs) \
print_deferred_heredocs (x); \
} while (0)
/* Non-zero means the stuff being printed is inside of a function def. */
static int inside_function_def;
static int skip_this_indent;
static int was_heredoc;
static int printing_connection;
static int printing_comsub;
static REDIRECT *deferred_heredocs;
/* The depth of the group commands that we are currently printing. This
includes the group command that is a function body. */
static int group_command_nesting;
/* A buffer to indicate the indirection level (PS4) when set -x is enabled. */
static char *indirection_string = 0;
static int indirection_stringsiz = 0;
/* Print COMMAND (a command tree) on standard output. */
void
print_command (command)
COMMAND *command;
{
command_string_index = 0;
printf ("%s", make_command_string (command));
}
/* Make a string which is the printed representation of the command
tree in COMMAND. We return this string. However, the string is
not consed, so you have to do that yourself if you want it to
remain around. */
char *
make_command_string (command)
COMMAND *command;
{
command_string_index = was_heredoc = 0;
deferred_heredocs = 0;
make_command_string_internal (command);
return (the_printed_command);
}
/* Print a command substitution after parsing it in parse_comsub to turn it
back into an external representation without turning newlines into `;'.
Placeholder for other changes, if any are necessary. */
char *
print_comsub (command)
COMMAND *command;
{
char *ret;
printing_comsub++;
ret = make_command_string (command);
printing_comsub--;
return ret;
}
/* The internal function. This is the real workhorse. */
static void
make_command_string_internal (command)
COMMAND *command;
{
char s[3];
if (command == 0)
cprintf ("");
else
{
if (skip_this_indent)
skip_this_indent--;
else
indent (indentation);
if (command->flags & CMD_TIME_PIPELINE)
{
cprintf ("time ");
if (command->flags & CMD_TIME_POSIX)
cprintf ("-p ");
}
if (command->flags & CMD_INVERT_RETURN)
cprintf ("! ");
switch (command->type)
{
case cm_for:
print_for_command (command->value.For);
break;
#if defined (ARITH_FOR_COMMAND)
case cm_arith_for:
print_arith_for_command (command->value.ArithFor);
break;
#endif
#if defined (SELECT_COMMAND)
case cm_select:
print_select_command (command->value.Select);
break;
#endif
case cm_case:
print_case_command (command->value.Case);
break;
case cm_while:
print_while_command (command->value.While);
break;
case cm_until:
print_until_command (command->value.While);
break;
case cm_if:
print_if_command (command->value.If);
break;
#if defined (DPAREN_ARITHMETIC)
case cm_arith:
print_arith_command (command->value.Arith->exp);
break;
#endif
#if defined (COND_COMMAND)
case cm_cond:
print_cond_command (command->value.Cond);
break;
#endif
case cm_simple:
print_simple_command (command->value.Simple);
break;
case cm_connection:
skip_this_indent++;
printing_connection++;
make_command_string_internal (command->value.Connection->first);
switch (command->value.Connection->connector)
{
case '&':
case '|':
{
char c = command->value.Connection->connector;
s[0] = ' ';
s[1] = c;
s[2] = '\0';
print_deferred_heredocs (s);
if (c != '&' || command->value.Connection->second)
{
cprintf (" ");
skip_this_indent++;
}
}
break;
case AND_AND:
print_deferred_heredocs (" && ");
if (command->value.Connection->second)
skip_this_indent++;
break;
case OR_OR:
print_deferred_heredocs (" || ");
if (command->value.Connection->second)
skip_this_indent++;
break;
case ';':
case '\n': /* special case this */
{
char c = command->value.Connection->connector;
int was_newline;
s[0] = printing_comsub ? c : ';';
s[1] = '\0';
was_newline = deferred_heredocs == 0 && was_heredoc == 0 && c == '\n';
if (deferred_heredocs == 0)
{
if (was_heredoc == 0)
cprintf ("%s", s); /* inside_function_def? */
else
was_heredoc = 0;
}
else
/* print_deferred_heredocs special-cases `;' */
print_deferred_heredocs (inside_function_def ? "" : ";");
if (inside_function_def)
cprintf ("\n");
else if (printing_comsub && c == '\n' && was_newline == 0)
cprintf ("\n"); /* preserve newlines in comsubs but don't double them */
else
{
if (c == ';')
cprintf (" ");
if (command->value.Connection->second)
skip_this_indent++;
}
break;
}
default:
cprintf (_("print_command: bad connector `%d'"),
command->value.Connection->connector);
break;
}
make_command_string_internal (command->value.Connection->second);
PRINT_DEFERRED_HEREDOCS ("");
printing_connection--;
break;
case cm_function_def:
print_function_def (command->value.Function_def);
break;
case cm_group:
print_group_command (command->value.Group);
break;
case cm_subshell:
cprintf ("( ");
skip_this_indent++;
make_command_string_internal (command->value.Subshell->command);
PRINT_DEFERRED_HEREDOCS ("");
cprintf (" )");
break;
case cm_coproc:
cprintf ("coproc ");
if (command->value.Coproc->command->type != cm_simple)
cprintf ("%s ", command->value.Coproc->name);
skip_this_indent++;
make_command_string_internal (command->value.Coproc->command);
break;
default:
command_error ("print_command", CMDERR_BADTYPE, command->type, 0);
break;
}
if (command->redirects)
{
cprintf (" ");
print_redirection_list (command->redirects);
}
}
}
static void
_print_word_list (list, separator, pfunc)
WORD_LIST *list;
char *separator;
PFUNC *pfunc;
{
WORD_LIST *w;
for (w = list; w; w = w->next)
(*pfunc) ("%s%s", w->word->word, w->next ? separator : "");
}
void
print_word_list (list, separator)
WORD_LIST *list;
char *separator;
{
_print_word_list (list, separator, xprintf);
}
void
xtrace_set (fd, fp)
int fd;
FILE *fp;
{
if (fd >= 0 && sh_validfd (fd) == 0)
{
internal_error (_("xtrace_set: %d: invalid file descriptor"), fd);
return;
}
if (fp == 0)
{
internal_error (_("xtrace_set: NULL file pointer"));
return;
}
if (fd >= 0 && fileno (fp) != fd)
internal_warning (_("xtrace fd (%d) != fileno xtrace fp (%d)"), fd, fileno (fp));
xtrace_fd = fd;
xtrace_fp = fp;
}
void
xtrace_init ()
{
xtrace_set (-1, stderr);
}
void
xtrace_reset ()
{
if (xtrace_fd >= 0 && xtrace_fp)
{
fflush (xtrace_fp);
fclose (xtrace_fp);
}
else if (xtrace_fd >= 0)
close (xtrace_fd);
xtrace_fd = -1;
xtrace_fp = stderr;
}
void
xtrace_fdchk (fd)
int fd;
{
if (fd == xtrace_fd)
xtrace_reset ();
}
/* Return a string denoting what our indirection level is. */
char *
indirection_level_string ()
{
register int i, j;
char *ps4;
char ps4_firstc[MB_LEN_MAX+1];
int ps4_firstc_len, ps4_len, ineed, old;
ps4 = get_string_value ("PS4");
if (indirection_string == 0)
indirection_string = xmalloc (indirection_stringsiz = 100);
indirection_string[0] = '\0';
if (ps4 == 0 || *ps4 == '\0')
return (indirection_string);
old = change_flag ('x', FLAG_OFF);
ps4 = decode_prompt_string (ps4);
if (old)
change_flag ('x', FLAG_ON);
if (ps4 == 0 || *ps4 == '\0')
{
FREE (ps4);
return (indirection_string);
}
#if defined (HANDLE_MULTIBYTE)
ps4_len = strnlen (ps4, MB_CUR_MAX);
ps4_firstc_len = MBLEN (ps4, ps4_len);
if (ps4_firstc_len == 1 || ps4_firstc_len == 0 || ps4_firstc_len < 0)
{
ps4_firstc[0] = ps4[0];
ps4_firstc[ps4_firstc_len = 1] = '\0';
}
else
memcpy (ps4_firstc, ps4, ps4_firstc_len);
#else
ps4_firstc[0] = ps4[0];
ps4_firstc[ps4_firstc_len = 1] = '\0';
#endif
/* Dynamically resize indirection_string so we have room for everything
and we don't have to truncate ps4 */
ineed = (ps4_firstc_len * indirection_level) + strlen (ps4);
if (ineed > indirection_stringsiz - 1)
{
indirection_stringsiz = ineed + 1;
indirection_string = xrealloc (indirection_string, indirection_stringsiz);
}
for (i = j = 0; ps4_firstc[0] && j < indirection_level && i < indirection_stringsiz - 1; i += ps4_firstc_len, j++)
{
if (ps4_firstc_len == 1)
indirection_string[i] = ps4_firstc[0];
else
memcpy (indirection_string+i, ps4_firstc, ps4_firstc_len);
}
for (j = ps4_firstc_len; *ps4 && ps4[j] && i < indirection_stringsiz - 1; i++, j++)
indirection_string[i] = ps4[j];
indirection_string[i] = '\0';
free (ps4);
return (indirection_string);
}
void
xtrace_print_assignment (name, value, assign_list, xflags)
char *name, *value;
int assign_list, xflags;
{
char *nval;
CHECK_XTRACE_FP;
if (xflags)
fprintf (xtrace_fp, "%s", indirection_level_string ());
/* VALUE should not be NULL when this is called. */
if (*value == '\0' || assign_list)
nval = value;
else if (sh_contains_shell_metas (value))
nval = sh_single_quote (value);
else if (ansic_shouldquote (value))
nval = ansic_quote (value, 0, (int *)0);
else
nval = value;
if (assign_list)
fprintf (xtrace_fp, "%s=(%s)\n", name, nval);
else
fprintf (xtrace_fp, "%s=%s\n", name, nval);
if (nval != value)
FREE (nval);
fflush (xtrace_fp);
}
/* A function to print the words of a simple command when set -x is on. Also used to
print the word list in a for or select command header; in that case, we suppress
quoting the words because they haven't been expanded yet. XTFLAGS&1 means to
print $PS4; XTFLAGS&2 means to suppress quoting the words in LIST. */
void
xtrace_print_word_list (list, xtflags)
WORD_LIST *list;
int xtflags;
{
WORD_LIST *w;
char *t, *x;
CHECK_XTRACE_FP;
if (xtflags&1)
fprintf (xtrace_fp, "%s", indirection_level_string ());
for (w = list; w; w = w->next)
{
t = w->word->word;
if (t == 0 || *t == '\0')
fprintf (xtrace_fp, "''%s", w->next ? " " : "");
else if (xtflags & 2)
fprintf (xtrace_fp, "%s%s", t, w->next ? " " : "");
else if (sh_contains_shell_metas (t))
{
x = sh_single_quote (t);
fprintf (xtrace_fp, "%s%s", x, w->next ? " " : "");
free (x);
}
else if (ansic_shouldquote (t))
{
x = ansic_quote (t, 0, (int *)0);
fprintf (xtrace_fp, "%s%s", x, w->next ? " " : "");
free (x);
}
else
fprintf (xtrace_fp, "%s%s", t, w->next ? " " : "");
}
fprintf (xtrace_fp, "\n");
fflush (xtrace_fp);
}
static void
command_print_word_list (list, separator)
WORD_LIST *list;
char *separator;
{
_print_word_list (list, separator, cprintf);
}
void
print_for_command_head (for_command)
FOR_COM *for_command;
{
cprintf ("for %s in ", for_command->name->word);
command_print_word_list (for_command->map_list, " ");
}
void
xtrace_print_for_command_head (for_command)
FOR_COM *for_command;
{
CHECK_XTRACE_FP;
fprintf (xtrace_fp, "%s", indirection_level_string ());
fprintf (xtrace_fp, "for %s in ", for_command->name->word);
xtrace_print_word_list (for_command->map_list, 2);
}
static void
print_for_command (for_command)
FOR_COM *for_command;
{
print_for_command_head (for_command);
cprintf (";");
newline ("do\n");
indentation += indentation_amount;
make_command_string_internal (for_command->action);
PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
indentation -= indentation_amount;
newline ("done");
}
#if defined (ARITH_FOR_COMMAND)
static void
print_arith_for_command (arith_for_command)
ARITH_FOR_COM *arith_for_command;
{
cprintf ("for ((");
command_print_word_list (arith_for_command->init, " ");
cprintf ("; ");
command_print_word_list (arith_for_command->test, " ");
cprintf ("; ");
command_print_word_list (arith_for_command->step, " ");
cprintf ("))");
newline ("do\n");
indentation += indentation_amount;
make_command_string_internal (arith_for_command->action);
PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
indentation -= indentation_amount;
newline ("done");
}
#endif /* ARITH_FOR_COMMAND */
#if defined (SELECT_COMMAND)
void
print_select_command_head (select_command)
SELECT_COM *select_command;
{
cprintf ("select %s in ", select_command->name->word);
command_print_word_list (select_command->map_list, " ");
}
void
xtrace_print_select_command_head (select_command)
SELECT_COM *select_command;
{
CHECK_XTRACE_FP;
fprintf (xtrace_fp, "%s", indirection_level_string ());
fprintf (xtrace_fp, "select %s in ", select_command->name->word);
xtrace_print_word_list (select_command->map_list, 2);
}
static void
print_select_command (select_command)
SELECT_COM *select_command;
{
print_select_command_head (select_command);
cprintf (";");
newline ("do\n");
indentation += indentation_amount;
make_command_string_internal (select_command->action);
PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
indentation -= indentation_amount;
newline ("done");
}
#endif /* SELECT_COMMAND */
static void
print_group_command (group_command)
GROUP_COM *group_command;
{
group_command_nesting++;
cprintf ("{ ");
if (inside_function_def == 0)
skip_this_indent++;
else
{
/* This is a group command { ... } inside of a function
definition, and should be printed as a multiline group
command, using the current indentation. */
cprintf ("\n");
indentation += indentation_amount;
}
make_command_string_internal (group_command->command);
PRINT_DEFERRED_HEREDOCS ("");
if (inside_function_def)
{
cprintf ("\n");
indentation -= indentation_amount;
indent (indentation);
}
else
{
semicolon ();
cprintf (" ");
}
cprintf ("}");
group_command_nesting--;
}
void
print_case_command_head (case_command)
CASE_COM *case_command;
{
cprintf ("case %s in ", case_command->word->word);
}
void
xtrace_print_case_command_head (case_command)
CASE_COM *case_command;
{
CHECK_XTRACE_FP;
fprintf (xtrace_fp, "%s", indirection_level_string ());
fprintf (xtrace_fp, "case %s in\n", case_command->word->word);
}
static void
print_case_command (case_command)
CASE_COM *case_command;
{
print_case_command_head (case_command);
if (case_command->clauses)
print_case_clauses (case_command->clauses);
newline ("esac");
}
static void
print_case_clauses (clauses)
PATTERN_LIST *clauses;
{
indentation += indentation_amount;
while (clauses)
{
newline ("");
command_print_word_list (clauses->patterns, " | ");
cprintf (")\n");
indentation += indentation_amount;
make_command_string_internal (clauses->action);
indentation -= indentation_amount;
PRINT_DEFERRED_HEREDOCS ("");
if (clauses->flags & CASEPAT_FALLTHROUGH)
newline (";&");
else if (clauses->flags & CASEPAT_TESTNEXT)
newline (";;&");
else
newline (";;");
clauses = clauses->next;
}
indentation -= indentation_amount;
}
static void
print_while_command (while_command)
WHILE_COM *while_command;
{
print_until_or_while (while_command, "while");
}
static void
print_until_command (while_command)
WHILE_COM *while_command;
{
print_until_or_while (while_command, "until");
}
static void
print_until_or_while (while_command, which)
WHILE_COM *while_command;
char *which;
{
cprintf ("%s ", which);
skip_this_indent++;
make_command_string_internal (while_command->test);
PRINT_DEFERRED_HEREDOCS ("");
semicolon ();
cprintf (" do\n"); /* was newline ("do\n"); */
indentation += indentation_amount;
make_command_string_internal (while_command->action);
PRINT_DEFERRED_HEREDOCS ("");
indentation -= indentation_amount;
semicolon ();
newline ("done");
}
static void
print_if_command (if_command)
IF_COM *if_command;
{
cprintf ("if ");
skip_this_indent++;
make_command_string_internal (if_command->test);
semicolon ();
cprintf (" then\n");
indentation += indentation_amount;
make_command_string_internal (if_command->true_case);
PRINT_DEFERRED_HEREDOCS ("");
indentation -= indentation_amount;
if (if_command->false_case)
{
semicolon ();
newline ("else\n");
indentation += indentation_amount;
make_command_string_internal (if_command->false_case);
PRINT_DEFERRED_HEREDOCS ("");
indentation -= indentation_amount;
}
semicolon ();
newline ("fi");
}
#if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
void
print_arith_command (arith_cmd_list)
WORD_LIST *arith_cmd_list;
{
cprintf ("((");
command_print_word_list (arith_cmd_list, " ");
cprintf ("))");
}
#endif
#if defined (COND_COMMAND)
static void
print_cond_node (cond)
COND_COM *cond;
{
if (cond->flags & CMD_INVERT_RETURN)
cprintf ("! ");
if (cond->type == COND_EXPR)
{
cprintf ("( ");
print_cond_node (cond->left);
cprintf (" )");
}
else if (cond->type == COND_AND)
{
print_cond_node (cond->left);
cprintf (" && ");
print_cond_node (cond->right);
}
else if (cond->type == COND_OR)
{
print_cond_node (cond->left);
cprintf (" || ");
print_cond_node (cond->right);
}
else if (cond->type == COND_UNARY)
{
cprintf ("%s", cond->op->word);
cprintf (" ");
print_cond_node (cond->left);
}
else if (cond->type == COND_BINARY)
{
print_cond_node (cond->left);
cprintf (" ");
cprintf ("%s", cond->op->word);
cprintf (" ");
print_cond_node (cond->right);
}
else if (cond->type == COND_TERM)
{
cprintf ("%s", cond->op->word); /* need to add quoting here */
}
}
void
print_cond_command (cond)
COND_COM *cond;
{
cprintf ("[[ ");
print_cond_node (cond);
cprintf (" ]]");
}
#ifdef DEBUG
void
debug_print_word_list (s, list, sep)
char *s;
WORD_LIST *list;
char *sep;
{
WORD_LIST *w;
if (s)
fprintf (stderr, "%s: ", s);
for (w = list; w; w = w->next)
fprintf (stderr, "%s%s", w->word->word, w->next ? sep : "");
fprintf (stderr, "\n");
}
void
debug_print_cond_command (cond)
COND_COM *cond;
{
fprintf (stderr, "DEBUG: ");
command_string_index = 0;
print_cond_command (cond);
fprintf (stderr, "%s\n", the_printed_command);
}
#endif
void
xtrace_print_cond_term (type, invert, op, arg1, arg2)
int type, invert;
WORD_DESC *op;
char *arg1, *arg2;
{
CHECK_XTRACE_FP;
command_string_index = 0;
fprintf (xtrace_fp, "%s", indirection_level_string ());
fprintf (xtrace_fp, "[[ ");
if (invert)
fprintf (xtrace_fp, "! ");
if (type == COND_UNARY)
{
fprintf (xtrace_fp, "%s ", op->word);
fprintf (xtrace_fp, "%s", (arg1 && *arg1) ? arg1 : "''");
}
else if (type == COND_BINARY)
{
fprintf (xtrace_fp, "%s", (arg1 && *arg1) ? arg1 : "''");
fprintf (xtrace_fp, " %s ", op->word);
fprintf (xtrace_fp, "%s", (arg2 && *arg2) ? arg2 : "''");
}
fprintf (xtrace_fp, " ]]\n");
fflush (xtrace_fp);
}
#endif /* COND_COMMAND */
#if defined (DPAREN_ARITHMETIC) || defined (ARITH_FOR_COMMAND)
/* A function to print the words of an arithmetic command when set -x is on. */
void
xtrace_print_arith_cmd (list)
WORD_LIST *list;
{
WORD_LIST *w;
CHECK_XTRACE_FP;
fprintf (xtrace_fp, "%s", indirection_level_string ());
fprintf (xtrace_fp, "(( ");
for (w = list; w; w = w->next)
fprintf (xtrace_fp, "%s%s", w->word->word, w->next ? " " : "");
fprintf (xtrace_fp, " ))\n");
fflush (xtrace_fp);
}
#endif
void
print_simple_command (simple_command)
SIMPLE_COM *simple_command;
{
if (simple_command->words)
command_print_word_list (simple_command->words, " ");
if (simple_command->redirects)
{
if (simple_command->words)
cprintf (" ");