forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_cmd.cc
1173 lines (1009 loc) · 30 KB
/
print_cmd.cc
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_cmd.cc -- 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"
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#include <cstdarg>
#include "bashintl.hh"
#include "command.hh"
#include "flags.hh"
#include "input.hh"
#include "shell.hh"
#include "shmbutil.hh"
namespace bash
{
#define CHECK_XTRACE_FP xtrace_fp = (xtrace_fp ? xtrace_fp : stderr)
#if 0
/* shell expansion characters: used in print_redirection_list */
#define EXPCHAR(c) ((c) == '{' || (c) == '~' || (c) == '$' || (c) == '`')
#endif
/* The internal function. This is the real workhorse. */
void
Shell::make_command_string_internal (COMMAND *command)
{
if (command == nullptr)
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 ("! ");
// dispatch to the virtual print method
command->print (this);
if (command->redirects)
{
cprintf (" ");
print_redirection_list (command->redirects);
}
}
}
void
Shell::xtrace_set (int fd, FILE *fp)
{
if (fd >= 0 && sh_validfd (fd) == 0)
{
internal_error (_ ("xtrace_set: %d: invalid file descriptor"), fd);
return;
}
if (fp == nullptr)
{
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
Shell::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;
}
/* Return a string denoting what our indirection level is. */
std::string
Shell::indirection_level_string ()
{
const char *ps4;
char ps4_firstc[MB_LEN_MAX + 1];
int ps4_firstc_len;
ps4 = get_string_value ("PS4");
if (ps4 == nullptr || *ps4 == '\0')
return indirection_string;
int old = change_flag ('x', FLAG_OFF);
std::string ps4_str (decode_prompt_string (ps4));
if (old)
change_flag ('x', FLAG_ON);
if (ps4_str.empty ())
return indirection_string;
#if defined(HANDLE_MULTIBYTE)
size_t ps4_len = ps4_str.size ();
ps4_firstc_len = mblen (ps4_str.c_str (), ps4_len);
if (ps4_firstc_len == 1 || ps4_firstc_len == 0 || ps4_firstc_len < 0)
{
ps4_firstc[0] = ps4_str[0];
ps4_firstc[ps4_firstc_len = 1] = '\0';
}
else
memcpy (ps4_firstc, ps4_str.data (),
static_cast<size_t> (ps4_firstc_len + 1)); // include '\0'
#else
ps4_firstc[0] = ps4_str[0];
ps4_firstc[ps4_firstc_len = 1] = '\0';
#endif
for (int i = 0; i < indirection_level; i++)
{
if (ps4_firstc_len == 1)
indirection_string.push_back (ps4_firstc[0]);
else
indirection_string.append (ps4_firstc);
}
indirection_string.append (ps4_str, static_cast<size_t> (ps4_firstc_len));
return indirection_string;
}
void
Shell::xtrace_print_assignment (const char *name, const char *value,
bool assign_list, int xflags)
{
std::string nval;
CHECK_XTRACE_FP;
if (xflags)
fprintf (xtrace_fp, "%s", indirection_level_string ().c_str ());
/* 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);
else
nval = value;
if (assign_list)
fprintf (xtrace_fp, "%s=(%s)\n", name, nval.c_str ());
else
fprintf (xtrace_fp, "%s=%s\n", name, nval.c_str ());
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
Shell::xtrace_print_word_list (WORD_LIST *list, int xtflags)
{
WORD_LIST *w;
CHECK_XTRACE_FP;
if (xtflags & 1)
fprintf (xtrace_fp, "%s", indirection_level_string ().c_str ());
for (w = list; w; w = w->next ())
{
std::string &t = w->word->word;
if (t.empty ())
fprintf (xtrace_fp, "''%s", w->next () ? " " : "");
else if (xtflags & 2)
fprintf (xtrace_fp, "%s%s", t.c_str (), w->next () ? " " : "");
else if (sh_contains_shell_metas (t))
{
std::string x = sh_single_quote (t);
fprintf (xtrace_fp, "%s%s", x.c_str (), w->next () ? " " : "");
}
else if (ansic_shouldquote (t))
{
std::string x = ansic_quote (t);
fprintf (xtrace_fp, "%s%s", x.c_str (), w->next () ? " " : "");
}
else
fprintf (xtrace_fp, "%s%s", t.c_str (), w->next () ? " " : "");
}
fprintf (xtrace_fp, "\n");
fflush (xtrace_fp);
}
void
Shell::xtrace_print_for_command_head (FOR_SELECT_COM *for_command)
{
CHECK_XTRACE_FP;
fprintf (xtrace_fp, "%s", indirection_level_string ().c_str ());
fprintf (xtrace_fp, "for %s in ", for_command->name->word.c_str ());
xtrace_print_word_list (for_command->map_list, 2);
}
// This function is called from execute_for_command ().
void
FOR_SELECT_COM::print_head (Shell *shell)
{
#if defined(SELECT_COMMAND)
if (type == cm_select)
shell->cprintf ("select %s in ", name->word.c_str ());
else
#endif
shell->cprintf ("for %s in ", name->word.c_str ());
shell->command_print_word_list (map_list, " ");
}
void
FOR_SELECT_COM::print (Shell *shell)
{
print_head (shell);
shell->cprintf (";");
shell->newline ("do\n");
shell->add_indentation ();
shell->make_command_string_internal (action);
shell->PRINT_DEFERRED_HEREDOCS ("");
shell->semicolon ();
shell->sub_indentation ();
shell->newline ("done");
}
#if defined(ARITH_FOR_COMMAND)
void
ARITH_FOR_COM::print (Shell *shell)
{
shell->cprintf ("for ((");
shell->command_print_word_list (init, " ");
shell->cprintf ("; ");
shell->command_print_word_list (test, " ");
shell->cprintf ("; ");
shell->command_print_word_list (step, " ");
shell->cprintf ("))");
shell->newline ("do\n");
shell->add_indentation ();
shell->make_command_string_internal (action);
shell->PRINT_DEFERRED_HEREDOCS ("");
shell->semicolon ();
shell->sub_indentation ();
shell->newline ("done");
}
#endif /* ARITH_FOR_COMMAND */
#if defined(SELECT_COMMAND)
void
Shell::xtrace_print_select_command_head (FOR_SELECT_COM *select_command)
{
CHECK_XTRACE_FP;
fprintf (xtrace_fp, "%s", indirection_level_string ().c_str ());
fprintf (xtrace_fp, "select %s in ", select_command->name->word.c_str ());
xtrace_print_word_list (select_command->map_list, 2);
}
#endif /* SELECT_COMMAND */
void
GROUP_COM::print (Shell *shell)
{
shell->group_command_nesting++;
shell->cprintf ("{ ");
if (!shell->inside_function_def)
shell->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. */
shell->cprintf ("\n");
shell->add_indentation ();
}
shell->make_command_string_internal (command);
shell->PRINT_DEFERRED_HEREDOCS ("");
if (shell->inside_function_def)
{
shell->cprintf ("\n");
shell->sub_indentation ();
shell->indent (shell->indentation);
}
else
{
shell->semicolon ();
shell->cprintf (" ");
}
shell->cprintf ("}");
shell->group_command_nesting--;
}
void
Shell::xtrace_print_case_command_head (CASE_COM *case_command)
{
CHECK_XTRACE_FP;
fprintf (xtrace_fp, "%s", indirection_level_string ().c_str ());
fprintf (xtrace_fp, "case %s in\n", case_command->word->word.c_str ());
}
// This function is called from execute_case_command ().
void
CASE_COM::print_head (Shell *shell)
{
shell->cprintf ("case %s in ", word->word.c_str ());
}
void
CASE_COM::print (Shell *shell)
{
print_head (shell);
if (clauses)
print_clauses (shell);
shell->newline ("esac");
}
void
CASE_COM::print_clauses (Shell *shell)
{
shell->add_indentation ();
while (clauses)
{
shell->newline ("");
shell->command_print_word_list (clauses->patterns, " | ");
shell->cprintf (")\n");
shell->add_indentation ();
shell->make_command_string_internal (clauses->action);
shell->sub_indentation ();
shell->PRINT_DEFERRED_HEREDOCS ("");
if (clauses->flags & CASEPAT_FALLTHROUGH)
shell->newline (";&");
else if (clauses->flags & CASEPAT_TESTNEXT)
shell->newline (";;&");
else
shell->newline (";;");
clauses = clauses->next ();
}
shell->sub_indentation ();
}
void
UNTIL_WHILE_COM::print (Shell *shell)
{
if (type == cm_until)
shell->cprintf ("%s ", "until");
else
shell->cprintf ("%s ", "while");
shell->skip_this_indent++;
shell->make_command_string_internal (test);
shell->PRINT_DEFERRED_HEREDOCS ("");
shell->semicolon ();
shell->cprintf (" do\n"); /* was newline ("do\n"); */
shell->add_indentation ();
shell->make_command_string_internal (action);
shell->PRINT_DEFERRED_HEREDOCS ("");
shell->sub_indentation ();
shell->semicolon ();
shell->newline ("done");
}
void
IF_COM::print (Shell *shell)
{
shell->cprintf ("if ");
shell->skip_this_indent++;
shell->make_command_string_internal (test);
shell->semicolon ();
shell->cprintf (" then\n");
shell->add_indentation ();
shell->make_command_string_internal (true_case);
shell->PRINT_DEFERRED_HEREDOCS ("");
shell->sub_indentation ();
if (false_case)
{
shell->semicolon ();
shell->newline ("else\n");
shell->add_indentation ();
shell->make_command_string_internal (false_case);
shell->PRINT_DEFERRED_HEREDOCS ("");
shell->sub_indentation ();
}
shell->semicolon ();
shell->newline ("fi");
}
#if defined(DPAREN_ARITHMETIC) || defined(ARITH_FOR_COMMAND)
void
Shell::print_arith_command (WORD_LIST *arith_cmd_list)
{
cprintf ("((");
command_print_word_list (arith_cmd_list, " ");
cprintf ("))");
}
void
ARITH_COM::print (Shell *shell)
{
shell->print_arith_command (exp);
}
#endif
#if defined(COND_COMMAND)
void
Shell::print_cond_node (COND_COM *cond)
{
if (cond->flags & CMD_INVERT_RETURN)
cprintf ("! ");
if (cond->cond_type == COND_EXPR)
{
cprintf ("( ");
print_cond_node (cond->left);
cprintf (" )");
}
else if (cond->cond_type == COND_AND)
{
print_cond_node (cond->left);
cprintf (" && ");
print_cond_node (cond->right);
}
else if (cond->cond_type == COND_OR)
{
print_cond_node (cond->left);
cprintf (" || ");
print_cond_node (cond->right);
}
else if (cond->cond_type == COND_UNARY)
{
cprintf ("%s", cond->op->word.c_str ());
cprintf (" ");
print_cond_node (cond->left);
}
else if (cond->cond_type == COND_BINARY)
{
print_cond_node (cond->left);
cprintf (" ");
cprintf ("%s", cond->op->word.c_str ());
cprintf (" ");
print_cond_node (cond->right);
}
else if (cond->cond_type == COND_TERM)
{
cprintf ("%s", cond->op->word.c_str ()); // need to add quoting here
}
}
void
COND_COM::print (Shell *shell)
{
shell->cprintf ("[[ ");
shell->print_cond_node (this);
shell->cprintf (" ]]");
}
#ifdef DEBUG
void
Shell::debug_print_cond_command (COND_COM *cond)
{
fprintf (stderr, "DEBUG: ");
the_printed_command.clear ();
cond->print (this);
fprintf (stderr, "%s\n", the_printed_command.c_str ());
}
#endif
void
Shell::xtrace_print_cond_term (cond_com_type type, bool invert, WORD_DESC *op,
const char *arg1, const char *arg2)
{
CHECK_XTRACE_FP;
the_printed_command.clear ();
fprintf (xtrace_fp, "%s", indirection_level_string ().c_str ());
fprintf (xtrace_fp, "[[ ");
if (invert)
fprintf (xtrace_fp, "! ");
if (type == COND_UNARY)
{
fprintf (xtrace_fp, "%s ", op->word.c_str ());
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.c_str ());
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
Shell::xtrace_print_arith_cmd (WORD_LIST *list)
{
WORD_LIST *w;
CHECK_XTRACE_FP;
fprintf (xtrace_fp, "%s", indirection_level_string ().c_str ());
fprintf (xtrace_fp, "(( ");
for (w = list; w; w = w->next ())
fprintf (xtrace_fp, "%s%s", w->word->word.c_str (), w->next () ? " " : "");
fprintf (xtrace_fp, " ))\n");
fflush (xtrace_fp);
}
#endif
void
SIMPLE_COM::print (Shell *shell)
{
if (words)
shell->command_print_word_list (words, " ");
if (redirects)
{
if (words)
shell->cprintf (" ");
shell->print_redirection_list (redirects);
}
}
/* Print heredocs that are attached to the command before the connector
represented by CSTRING. The parsing semantics require us to print the
here-doc delimiters, then the connector (CSTRING), then the here-doc
bodies. We print the here-doc delimiters in print_redirection_list
and print the connector and the bodies here. We don't print the connector
if it's a `;', but we use it to note not to print an extra space after the
last heredoc body and newline. */
void
Shell::print_deferred_heredocs (const char *cstring)
{
/* We now print the heredoc headers in print_redirection_list */
if (cstring && cstring[0] && (cstring[0] != ';' || cstring[1]))
cprintf ("%s", cstring);
if (deferred_heredocs)
{
print_heredoc_bodies (deferred_heredocs);
if (cstring && cstring[0] && (cstring[0] != ';' || cstring[1]))
cprintf (" "); /* make sure there's at least one space */
delete deferred_heredocs;
was_heredoc = true;
}
deferred_heredocs = nullptr;
}
void
Shell::print_redirection_list (REDIRECT *redirects)
{
REDIRECT *heredocs, *hdtail, *newredir;
heredocs = nullptr;
hdtail = heredocs;
was_heredoc = false;
while (redirects)
{
/* Defer printing the here document bodiess until we've printed the rest
of the redirections, but print the headers in the order they're given.
*/
if (redirects->instruction == r_reading_until
|| redirects->instruction == r_deblank_reading_until)
{
newredir = new REDIRECT (*redirects);
print_heredoc_header (newredir);
if (heredocs)
{
hdtail->set_next (newredir);
hdtail = newredir;
}
else
hdtail = heredocs = newredir;
}
#if 0
/* Remove this heuristic now that the command printing code doesn't
unconditionally put in the redirector file descriptor. */
else if (redirects->instruction == r_duplicating_output_word
&& (redirects->flags & REDIR_VARASSIGN) == 0
&& redirects->redirector.r.dest == 1)
{
/* Temporarily translate it as the execution code does. */
const char *rw = redirects->redirectee.r.filename->word.c_str ();
if (*rw && *rw != '-' && c_isdigit (*rw) == 0
&& EXPCHAR (*rw) == 0)
redirects->instruction = r_err_and_out;
print_redirection (redirects);
redirects->instruction = r_duplicating_output_word;
}
#endif
else
print_redirection (redirects);
redirects = redirects->next ();
if (redirects)
cprintf (" ");
}
/* Now that we've printed all the other redirections (on one line),
print the here documents. If we're printing a connection, we wait until
we print the connector symbol, then we print the here document bodies */
if (heredocs && printing_connection)
deferred_heredocs = heredocs;
else if (heredocs)
{
print_heredoc_bodies (heredocs);
delete heredocs;
}
}
void
Shell::print_heredoc_header (REDIRECT *redirect)
{
bool kill_leading = (redirect->instruction == r_deblank_reading_until);
/* Here doc header */
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}", redirect->redirector.r.filename->word.c_str ());
else if (redirect->redirector.r.dest != 0)
cprintf ("%d", redirect->redirector.r.dest);
/* If the here document delimiter is quoted, single-quote it. */
if (redirect->redirectee.r.filename->flags & W_QUOTED)
{
std::string x = sh_single_quote (redirect->here_doc_eof);
cprintf ("<<%s%s", kill_leading ? "-" : "", x.c_str ());
}
else
cprintf ("<<%s%s", kill_leading ? "-" : "",
redirect->here_doc_eof.c_str ());
}
void
Shell::print_redirection (REDIRECT *redirect)
{
int redirector, redir_fd;
WORD_DESC *redirectee, *redir_word;
redirectee = redirect->redirectee.r.filename;
redir_fd = redirect->redirectee.r.dest;
redir_word = redirect->redirector.r.filename;
redirector = redirect->redirector.r.dest;
switch (redirect->instruction)
{
case r_input_direction:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}", redir_word->word.c_str ());
else if (redirector != 0)
cprintf ("%d", redirector);
cprintf ("< %s", redirectee->word.c_str ());
break;
case r_output_direction:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}", redir_word->word.c_str ());
else if (redirector != 1)
cprintf ("%d", redirector);
cprintf ("> %s", redirectee->word.c_str ());
break;
case r_inputa_direction: /* Redirection created by the shell. */
cprintf ("&");
break;
case r_output_force:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}", redir_word->word.c_str ());
else if (redirector != 1)
cprintf ("%d", redirector);
cprintf (">| %s", redirectee->word.c_str ());
break;
case r_appending_to:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}", redir_word->word.c_str ());
else if (redirector != 1)
cprintf ("%d", redirector);
cprintf (">> %s", redirectee->word.c_str ());
break;
case r_input_output:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}", redir_word->word.c_str ());
else if (redirector != 1)
cprintf ("%d", redirector);
cprintf ("<> %s", redirectee->word.c_str ());
break;
case r_deblank_reading_until:
case r_reading_until:
print_heredoc_header (redirect);
cprintf ("\n");
print_heredoc_body (redirect);
break;
case r_reading_string:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}", redir_word->word.c_str ());
else if (redirector != 0)
cprintf ("%d", redirector);
#if 0
/* Don't need to check whether or not to requote, since original quotes
are still intact. The only thing that has happened is that $'...'
has been replaced with 'expanded ...'. */
if (ansic_shouldquote (redirect->redirectee.r.filename->word.c_str ()))
{
char *x;
x = ansic_quote (redirect->redirectee.r.filename->word.c_str ());
cprintf ("<<< %s", x);
delete[] x;
}
else
#endif
cprintf ("<<< %s", redirect->redirectee.r.filename->word.c_str ());
break;
case r_duplicating_input:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}<&%d", redir_word->word.c_str (), redir_fd);
else
cprintf ("%d<&%d", redirector, redir_fd);
break;
case r_duplicating_output:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}>&%d", redir_word->word.c_str (), redir_fd);
else
cprintf ("%d>&%d", redirector, redir_fd);
break;
case r_duplicating_input_word:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}<&%s", redir_word->word.c_str (),
redirectee->word.c_str ());
else if (redirector == 0)
cprintf ("<&%s", redirectee->word.c_str ());
else
cprintf ("%d<&%s", redirector, redirectee->word.c_str ());
break;
case r_duplicating_output_word:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}>&%s", redir_word->word.c_str (),
redirectee->word.c_str ());
else if (redirector == 1)
cprintf (">&%s", redirectee->word.c_str ());
else
cprintf ("%d>&%s", redirector, redirectee->word.c_str ());
break;
case r_move_input:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}<&%d-", redir_word->word.c_str (), redir_fd);
else
cprintf ("%d<&%d-", redirector, redir_fd);
break;
case r_move_output:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}>&%d-", redir_word->word.c_str (), redir_fd);
else
cprintf ("%d>&%d-", redirector, redir_fd);
break;
case r_move_input_word:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}<&%s-", redir_word->word.c_str (),
redirectee->word.c_str ());
else
cprintf ("%d<&%s-", redirector, redirectee->word.c_str ());
break;
case r_move_output_word:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}>&%s-", redir_word->word.c_str (),
redirectee->word.c_str ());
else
cprintf ("%d>&%s-", redirector, redirectee->word.c_str ());
break;
case r_close_this:
if (redirect->rflags & REDIR_VARASSIGN)
cprintf ("{%s}>&-", redir_word->word.c_str ());
else
cprintf ("%d>&-", redirector);
break;
case r_err_and_out:
cprintf ("&> %s", redirectee->word.c_str ());
break;
case r_append_err_and_out:
cprintf ("&>> %s", redirectee->word.c_str ());
break;
}
}
void
FUNCTION_DEF::print (Shell *shell)
{
shell->print_function_def (this);
}
void
Shell::print_function_def (FUNCTION_DEF *func)
{
COMMAND *cmdcopy;
REDIRECT *func_redirects = nullptr;
/* When in posix mode, print functions as posix specifies them. */
if (posixly_correct == 0)
cprintf ("function %s () \n", func->name->word.c_str ());
else
cprintf ("%s () \n", func->name->word.c_str ());
try
{
indent (indentation);
cprintf ("{ \n");
inside_function_def++;
indentation += indentation_amount;
cmdcopy = func->command->clone ();
GROUP_COM *gcomcopy = dynamic_cast<GROUP_COM *> (cmdcopy);
if (gcomcopy)
{
func_redirects = cmdcopy->redirects;
cmdcopy->redirects = nullptr;
}
make_command_string_internal (gcomcopy ? gcomcopy->command : cmdcopy);
PRINT_DEFERRED_HEREDOCS ("");
}
catch (const std::exception &)
{
inside_function_def = 0;
indentation = 0;
printing_connection = 0;
deferred_heredocs = nullptr;
throw;
}
indentation -= indentation_amount;
inside_function_def--;
if (func_redirects)
{
newline ("} ");
print_redirection_list (func_redirects);
cmdcopy->redirects = func_redirects;
}
else
newline ("}");
delete cmdcopy;
}
/* Return the string representation of the named function.
NAME is the name of the function.
COMMAND is the function body. It should be a GROUP_COM.
flags & FUNC_MULTILINE is non-zero to pretty-print, or zero for all on one
line. flags & FUNC_EXTERNAL means convert from internal to external form.
*/
std::string
Shell::named_function_string (const char *name, COMMAND *command,
print_flags flags)
{
int old_indent, old_amount;
COMMAND *cmdcopy;
REDIRECT *func_redirects;
old_indent = indentation;
old_amount = indentation_amount;
the_printed_command.clear ();
was_heredoc = false;
deferred_heredocs = nullptr;
printing_comsub = 0;
if (name && *name)
{
STRING_TOKEN_MAP::iterator it = word_token_map.find (name);
if (it != word_token_map.end ())
cprintf ("function ");
cprintf ("%s ", name);
}
cprintf ("() ");
if ((flags & FUNC_MULTILINE) == 0)
{
indentation = 1;
indentation_amount = 0;
}
else
{
cprintf ("\n");
indentation += indentation_amount;
}
inside_function_def++;
cprintf ((flags & FUNC_MULTILINE) ? "{ \n" : "{ ");
cmdcopy = command->clone ();
/* Take any redirections specified in the function definition (which should
apply to the function as a whole) and save them for printing later. */
func_redirects = nullptr;
GROUP_COM *gcomcopy = dynamic_cast<GROUP_COM *> (cmdcopy);
if (gcomcopy)
{
func_redirects = cmdcopy->redirects;
cmdcopy->redirects = nullptr;
}
make_command_string_internal (gcomcopy ? gcomcopy->command : cmdcopy);
PRINT_DEFERRED_HEREDOCS ("");
indentation = old_indent;
indentation_amount = old_amount;
inside_function_def--;
if (func_redirects)
{
newline ("} ");
print_redirection_list (func_redirects);
cmdcopy->redirects = func_redirects;
}
else
newline ("}");
std::string result (the_printed_command);
if ((flags & FUNC_MULTILINE) == 0)
{
if (result[2] == '\n') /* XXX -- experimental */
result.erase (2, 1);
}