summaryrefslogtreecommitdiff
path: root/shell/hush.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-05-02 21:40:23 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-05-02 21:40:23 +0000
commitd01ff13454ebfe93e3df79eb184818a63f3f5a1c (patch)
treeaafcbe45780fadf52ea1d11ac93f724efa412d98 /shell/hush.c
parentdd999a2241ad243eb1db6bac52e2ee6dc29eea12 (diff)
downloadbusybox-w32-d01ff13454ebfe93e3df79eb184818a63f3f5a1c.tar.gz
busybox-w32-d01ff13454ebfe93e3df79eb184818a63f3f5a1c.tar.bz2
busybox-w32-d01ff13454ebfe93e3df79eb184818a63f3f5a1c.zip
hush: simplify debugging prints a little bit
Diffstat (limited to '')
-rw-r--r--shell/hush.c123
1 files changed, 61 insertions, 62 deletions
diff --git a/shell/hush.c b/shell/hush.c
index f931a06e1..cdb0b74da 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -80,12 +80,37 @@
80#include "busybox.h" 80#include "busybox.h"
81#include <glob.h> /* glob, of course */ 81#include <glob.h> /* glob, of course */
82#include <getopt.h> /* should be pretty obvious */ 82#include <getopt.h> /* should be pretty obvious */
83
84/* #include <dmalloc.h> */ 83/* #include <dmalloc.h> */
85//#define DEBUG_SHELL 84
85
86/* If you comment out one of these below, it will be #defined later
87 * to perform debug printfs to stderr: */
88#define debug_printf(...) do {} while (0)
86/* Finer-grained debug switch */ 89/* Finer-grained debug switch */
87//#define DEBUG_SHELL_JOBS 90#define debug_printf_jobs(...) do {} while (0)
88//#define DEBUG_SHELL_EXECUTION 91#define debug_printf_exec(...) do {} while (0)
92
93
94#ifndef debug_printf
95#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
96/* broken, of course, but OK for testing */
97static const char *indenter(int i)
98{
99 static const char blanks[] = " ";
100 return &blanks[sizeof(blanks) - i - 1];
101}
102#endif
103#define final_printf debug_printf
104
105#ifndef debug_printf_jobs
106#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
107#define DEBUG_SHELL_JOBS 1
108#endif
109
110#ifndef debug_printf_exec
111#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
112#endif
113
89 114
90#if !ENABLE_HUSH_INTERACTIVE 115#if !ENABLE_HUSH_INTERACTIVE
91#undef ENABLE_FEATURE_EDITING 116#undef ENABLE_FEATURE_EDITING
@@ -298,32 +323,6 @@ struct built_in_command {
298 int (*function) (char **argv); /* function ptr */ 323 int (*function) (char **argv); /* function ptr */
299}; 324};
300 325
301#ifdef DEBUG_SHELL
302#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
303/* broken, of course, but OK for testing */
304static char *indenter(int i)
305{
306 static char blanks[] = " ";
307 return &blanks[sizeof(blanks) - i - 1];
308}
309#else
310#define debug_printf(...) do {} while (0)
311#endif
312
313#ifdef DEBUG_SHELL_JOBS
314#define debug_jobs_printf(...) fprintf(stderr, __VA_ARGS__)
315#else
316#define debug_jobs_printf(...) do {} while (0)
317#endif
318
319#ifdef DEBUG_SHELL_EXECUTION
320#define debug_exec_printf(...) fprintf(stderr, __VA_ARGS__)
321#else
322#define debug_exec_printf(...) do {} while (0)
323#endif
324
325#define final_printf debug_printf
326
327static void __syntax(int line) 326static void __syntax(int line)
328{ 327{
329 bb_error_msg("syntax error hush.c:%d", line); 328 bb_error_msg("syntax error hush.c:%d", line);
@@ -514,7 +513,7 @@ static sigjmp_buf nofork_jb;
514 513
515static void handler_ctrl_c(int sig) 514static void handler_ctrl_c(int sig)
516{ 515{
517 debug_jobs_printf("got sig %d\n", sig); 516 debug_printf_jobs("got sig %d\n", sig);
518// as usual we can have all kinds of nasty problems with leaked malloc data here 517// as usual we can have all kinds of nasty problems with leaked malloc data here
519 siglongjmp(nofork_jb, 1); 518 siglongjmp(nofork_jb, 1);
520} 519}
@@ -523,20 +522,20 @@ static void handler_ctrl_z(int sig)
523{ 522{
524 pid_t pid; 523 pid_t pid;
525 524
526 debug_jobs_printf("got tty sig %d\n", sig); 525 debug_printf_jobs("got tty sig %d\n", sig);
527 pid = fork(); 526 pid = fork();
528 if (pid < 0) /* can't fork. Pretend there were no Ctrl-Z */ 527 if (pid < 0) /* can't fork. Pretend there were no Ctrl-Z */
529 return; 528 return;
530 debug_jobs_printf("bg'ing nofork\n"); 529 debug_printf_jobs("bg'ing nofork\n");
531 nofork_save.saved = 0; /* flag the fact that Ctrl-Z was handled */ 530 nofork_save.saved = 0; /* flag the fact that Ctrl-Z was handled */
532 nofork_pipe->running_progs = 1; 531 nofork_pipe->running_progs = 1;
533 nofork_pipe->stopped_progs = 0; 532 nofork_pipe->stopped_progs = 0;
534 if (!pid) { /* child */ 533 if (!pid) { /* child */
535 debug_jobs_printf("setting pgrp for child\n"); 534 debug_printf_jobs("setting pgrp for child\n");
536 setpgrp(); 535 setpgrp();
537 set_every_sighandler(SIG_DFL); 536 set_every_sighandler(SIG_DFL);
538 raise(SIGTSTP); /* resend TSTP so that child will be stopped */ 537 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
539 debug_jobs_printf("returning to child\n"); 538 debug_printf_jobs("returning to child\n");
540 /* return to nofork, it will eventually exit now, 539 /* return to nofork, it will eventually exit now,
541 * not return back to shell */ 540 * not return back to shell */
542 return; 541 return;
@@ -766,9 +765,9 @@ static int builtin_fg_bg(char **argv)
766 } 765 }
767 766
768 /* Restart the processes in the job */ 767 /* Restart the processes in the job */
769 debug_jobs_printf("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp); 768 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
770 for (i = 0; i < pi->num_progs; i++) { 769 for (i = 0; i < pi->num_progs; i++) {
771 debug_jobs_printf("reviving pid %d\n", pi->progs[i].pid); 770 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
772 pi->progs[i].is_stopped = 0; 771 pi->progs[i].is_stopped = 0;
773 } 772 }
774 pi->stopped_progs = 0; 773 pi->stopped_progs = 0;
@@ -1333,7 +1332,7 @@ static void pseudo_exec(struct child_prog *child)
1333#if ENABLE_HUSH_INTERACTIVE 1332#if ENABLE_HUSH_INTERACTIVE
1334 interactive_fd = 0; /* crucial!!!! */ 1333 interactive_fd = 0; /* crucial!!!! */
1335#endif 1334#endif
1336 debug_exec_printf("pseudo_exec: run_list_real\n"); 1335 debug_printf_exec("pseudo_exec: run_list_real\n");
1337 rcode = run_list_real(child->group); 1336 rcode = run_list_real(child->group);
1338 /* OK to leak memory by not calling free_pipe_list, 1337 /* OK to leak memory by not calling free_pipe_list,
1339 * since this process is about to exit */ 1338 * since this process is about to exit */
@@ -1478,20 +1477,20 @@ static int checkjobs(struct pipe* fg_pipe)
1478 1477
1479#ifdef DEBUG_SHELL_JOBS 1478#ifdef DEBUG_SHELL_JOBS
1480 if (WIFSTOPPED(status)) 1479 if (WIFSTOPPED(status))
1481 debug_jobs_printf("pid %d stopped by sig %d (exitcode %d)\n", 1480 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
1482 childpid, WSTOPSIG(status), WEXITSTATUS(status)); 1481 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1483 if (WIFSIGNALED(status)) 1482 if (WIFSIGNALED(status))
1484 debug_jobs_printf("pid %d killed by sig %d (exitcode %d)\n", 1483 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
1485 childpid, WTERMSIG(status), WEXITSTATUS(status)); 1484 childpid, WTERMSIG(status), WEXITSTATUS(status));
1486 if (WIFEXITED(status)) 1485 if (WIFEXITED(status))
1487 debug_jobs_printf("pid %d exited, exitcode %d\n", 1486 debug_printf_jobs("pid %d exited, exitcode %d\n",
1488 childpid, WEXITSTATUS(status)); 1487 childpid, WEXITSTATUS(status));
1489#endif 1488#endif
1490 /* Were we asked to wait for fg pipe? */ 1489 /* Were we asked to wait for fg pipe? */
1491 if (fg_pipe) { 1490 if (fg_pipe) {
1492 int i; 1491 int i;
1493 for (i = 0; i < fg_pipe->num_progs; i++) { 1492 for (i = 0; i < fg_pipe->num_progs; i++) {
1494 debug_jobs_printf("check pid %d\n", fg_pipe->progs[i].pid); 1493 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
1495 if (fg_pipe->progs[i].pid == childpid) { 1494 if (fg_pipe->progs[i].pid == childpid) {
1496 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ 1495 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
1497 if (dead) { 1496 if (dead) {
@@ -1504,7 +1503,7 @@ static int checkjobs(struct pipe* fg_pipe)
1504 fg_pipe->progs[i].is_stopped = 1; 1503 fg_pipe->progs[i].is_stopped = 1;
1505 fg_pipe->stopped_progs++; 1504 fg_pipe->stopped_progs++;
1506 } 1505 }
1507 debug_jobs_printf("fg_pipe: running_progs %d stopped_progs %d\n", 1506 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
1508 fg_pipe->running_progs, fg_pipe->stopped_progs); 1507 fg_pipe->running_progs, fg_pipe->stopped_progs);
1509 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) { 1508 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1510 /* All processes in fg pipe have exited/stopped */ 1509 /* All processes in fg pipe have exited/stopped */
@@ -1606,7 +1605,7 @@ static int run_single_fg_nofork(struct pipe *pi, const struct bb_applet *a,
1606 * Sighandler has longjmped us here */ 1605 * Sighandler has longjmped us here */
1607 signal(SIGINT, SIG_IGN); 1606 signal(SIGINT, SIG_IGN);
1608 signal(SIGTSTP, SIG_IGN); 1607 signal(SIGTSTP, SIG_IGN);
1609 debug_jobs_printf("Exiting nofork early\n"); 1608 debug_printf_jobs("Exiting nofork early\n");
1610 restore_nofork_data(&nofork_save); 1609 restore_nofork_data(&nofork_save);
1611 if (nofork_save.saved == 0) /* Ctrl-Z, not Ctrl-C */ 1610 if (nofork_save.saved == 0) /* Ctrl-Z, not Ctrl-C */
1612 insert_bg_job(pi); 1611 insert_bg_job(pi);
@@ -1648,7 +1647,7 @@ static int run_pipe_real(struct pipe *pi)
1648 int rcode; 1647 int rcode;
1649 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG); 1648 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
1650 1649
1651 debug_exec_printf("run_pipe_real start:\n"); 1650 debug_printf_exec("run_pipe_real start:\n");
1652 1651
1653 nextin = 0; 1652 nextin = 0;
1654#if ENABLE_HUSH_JOB 1653#if ENABLE_HUSH_JOB
@@ -1667,10 +1666,10 @@ static int run_pipe_real(struct pipe *pi)
1667 setup_redirects(child, squirrel); 1666 setup_redirects(child, squirrel);
1668 /* XXX could we merge code with following builtin case, 1667 /* XXX could we merge code with following builtin case,
1669 * by creating a pseudo builtin that calls run_list_real? */ 1668 * by creating a pseudo builtin that calls run_list_real? */
1670 debug_exec_printf(": run_list_real\n"); 1669 debug_printf_exec(": run_list_real\n");
1671 rcode = run_list_real(child->group); 1670 rcode = run_list_real(child->group);
1672 restore_redirects(squirrel); 1671 restore_redirects(squirrel);
1673 debug_exec_printf("run_pipe_real return %d\n", rcode); 1672 debug_printf_exec("run_pipe_real return %d\n", rcode);
1674 return rcode; 1673 return rcode;
1675 } 1674 }
1676 1675
@@ -1718,10 +1717,10 @@ static int run_pipe_real(struct pipe *pi)
1718 char *str; 1717 char *str;
1719 1718
1720 str = make_string(argv + i); 1719 str = make_string(argv + i);
1721 debug_exec_printf(": parse_string_outer '%s'\n", str); 1720 debug_printf_exec(": parse_string_outer '%s'\n", str);
1722 parse_string_outer(str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING); 1721 parse_string_outer(str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING);
1723 free(str); 1722 free(str);
1724 debug_exec_printf("run_pipe_real return %d\n", last_return_code); 1723 debug_printf_exec("run_pipe_real return %d\n", last_return_code);
1725 return last_return_code; 1724 return last_return_code;
1726 } 1725 }
1727 for (x = bltins; x->cmd; x++) { 1726 for (x = bltins; x->cmd; x++) {
@@ -1738,10 +1737,10 @@ static int run_pipe_real(struct pipe *pi)
1738 * things seem to work with glibc. */ 1737 * things seem to work with glibc. */
1739// TODO: fflush(NULL)? 1738// TODO: fflush(NULL)?
1740 setup_redirects(child, squirrel); 1739 setup_redirects(child, squirrel);
1741 debug_exec_printf(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]); 1740 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
1742 rcode = x->function(argv + i); 1741 rcode = x->function(argv + i);
1743 restore_redirects(squirrel); 1742 restore_redirects(squirrel);
1744 debug_exec_printf("run_pipe_real return %d\n", rcode); 1743 debug_printf_exec("run_pipe_real return %d\n", rcode);
1745 return rcode; 1744 return rcode;
1746 } 1745 }
1747 } 1746 }
@@ -1750,10 +1749,10 @@ static int run_pipe_real(struct pipe *pi)
1750 const struct bb_applet *a = find_applet_by_name(argv[i]); 1749 const struct bb_applet *a = find_applet_by_name(argv[i]);
1751 if (a && a->nofork) { 1750 if (a && a->nofork) {
1752 setup_redirects(child, squirrel); 1751 setup_redirects(child, squirrel);
1753 debug_exec_printf(": run_single_fg_nofork '%s' '%s'...\n", argv[i], argv[i+1]); 1752 debug_printf_exec(": run_single_fg_nofork '%s' '%s'...\n", argv[i], argv[i+1]);
1754 rcode = run_single_fg_nofork(pi, a, argv + i); 1753 rcode = run_single_fg_nofork(pi, a, argv + i);
1755 restore_redirects(squirrel); 1754 restore_redirects(squirrel);
1756 debug_exec_printf("run_pipe_real return %d\n", rcode); 1755 debug_printf_exec("run_pipe_real return %d\n", rcode);
1757 return rcode; 1756 return rcode;
1758 } 1757 }
1759 } 1758 }
@@ -1768,7 +1767,7 @@ static int run_pipe_real(struct pipe *pi)
1768 1767
1769 for (i = 0; i < pi->num_progs; i++) { 1768 for (i = 0; i < pi->num_progs; i++) {
1770 child = &(pi->progs[i]); 1769 child = &(pi->progs[i]);
1771 debug_exec_printf(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]); 1770 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
1772 1771
1773 /* pipes are inserted between pairs of commands */ 1772 /* pipes are inserted between pairs of commands */
1774 if ((i + 1) < pi->num_progs) { 1773 if ((i + 1) < pi->num_progs) {
@@ -1849,7 +1848,7 @@ static int run_pipe_real(struct pipe *pi)
1849 but it doesn't matter */ 1848 but it doesn't matter */
1850 nextin = pipefds[0]; 1849 nextin = pipefds[0];
1851 } 1850 }
1852 debug_exec_printf("run_pipe_real return -1\n"); 1851 debug_printf_exec("run_pipe_real return -1\n");
1853 return -1; 1852 return -1;
1854} 1853}
1855 1854
@@ -1866,7 +1865,7 @@ static int run_list_real(struct pipe *pi)
1866 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */ 1865 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
1867 reserved_style rmode, skip_more_in_this_rmode = RES_XXXX; 1866 reserved_style rmode, skip_more_in_this_rmode = RES_XXXX;
1868 1867
1869 debug_exec_printf("run_list_real start:\n"); 1868 debug_printf_exec("run_list_real start:\n");
1870 1869
1871 /* check syntax for "for" */ 1870 /* check syntax for "for" */
1872 for (rpipe = pi; rpipe; rpipe = rpipe->next) { 1871 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
@@ -1874,14 +1873,14 @@ static int run_list_real(struct pipe *pi)
1874 && (rpipe->next == NULL) 1873 && (rpipe->next == NULL)
1875 ) { 1874 ) {
1876 syntax(); 1875 syntax();
1877 debug_exec_printf("run_list_real return 1\n"); 1876 debug_printf_exec("run_list_real return 1\n");
1878 return 1; 1877 return 1;
1879 } 1878 }
1880 if ((rpipe->r_mode == RES_IN && rpipe->next->r_mode == RES_IN && rpipe->next->progs->argv != NULL) 1879 if ((rpipe->r_mode == RES_IN && rpipe->next->r_mode == RES_IN && rpipe->next->progs->argv != NULL)
1881 || (rpipe->r_mode == RES_FOR && rpipe->next->r_mode != RES_IN) 1880 || (rpipe->r_mode == RES_FOR && rpipe->next->r_mode != RES_IN)
1882 ) { 1881 ) {
1883 syntax(); 1882 syntax();
1884 debug_exec_printf("run_list_real return 1\n"); 1883 debug_printf_exec("run_list_real return 1\n");
1885 return 1; 1884 return 1;
1886 } 1885 }
1887 } 1886 }
@@ -1957,7 +1956,7 @@ static int run_list_real(struct pipe *pi)
1957 if (pi->num_progs == 0) 1956 if (pi->num_progs == 0)
1958 continue; 1957 continue;
1959 save_num_progs = pi->num_progs; /* save number of programs */ 1958 save_num_progs = pi->num_progs; /* save number of programs */
1960 debug_exec_printf(": run_pipe_real with %d members\n", pi->num_progs); 1959 debug_printf_exec(": run_pipe_real with %d members\n", pi->num_progs);
1961 rcode = run_pipe_real(pi); 1960 rcode = run_pipe_real(pi);
1962 if (rcode != -1) { 1961 if (rcode != -1) {
1963 /* We only ran a builtin: rcode was set by the return value 1962 /* We only ran a builtin: rcode was set by the return value
@@ -1996,7 +1995,7 @@ static int run_list_real(struct pipe *pi)
1996 } 1995 }
1997 checkjobs(NULL); 1996 checkjobs(NULL);
1998 } 1997 }
1999 debug_exec_printf("run_list_real return %d\n", rcode); 1998 debug_printf_exec("run_list_real return %d\n", rcode);
2000 return rcode; 1999 return rcode;
2001} 2000}
2002 2001
@@ -2072,7 +2071,7 @@ static int run_list(struct pipe *pi)
2072{ 2071{
2073 int rcode = 0; 2072 int rcode = 0;
2074 if (fake_mode == 0) { 2073 if (fake_mode == 0) {
2075 debug_exec_printf("run_list: run_list_real with %d members\n", pi->num_progs); 2074 debug_printf_exec("run_list: run_list_real with %d members\n", pi->num_progs);
2076 rcode = run_list_real(pi); 2075 rcode = run_list_real(pi);
2077 } 2076 }
2078 /* free_pipe_list has the side effect of clearing memory 2077 /* free_pipe_list has the side effect of clearing memory
@@ -3132,7 +3131,7 @@ static int parse_stream_outer(struct in_str *inp, int flag)
3132 if (rcode != 1 && ctx.old_flag == 0) { 3131 if (rcode != 1 && ctx.old_flag == 0) {
3133 done_word(&temp, &ctx); 3132 done_word(&temp, &ctx);
3134 done_pipe(&ctx, PIPE_SEQ); 3133 done_pipe(&ctx, PIPE_SEQ);
3135 debug_exec_printf("parse_stream_outer: run_list\n"); 3134 debug_printf_exec("parse_stream_outer: run_list\n");
3136 run_list(ctx.list_head); 3135 run_list(ctx.list_head);
3137 } else { 3136 } else {
3138 if (ctx.old_flag != 0) { 3137 if (ctx.old_flag != 0) {