lash: style cleanup

This commit is contained in:
Denis Vlasenko 2007-03-31 11:07:30 +00:00
parent 9eb51ad6b9
commit 6fa8b8e17f

View File

@ -29,12 +29,14 @@
/* Always enable for the moment... */
#define CONFIG_LASH_PIPE_N_REDIRECTS
#define CONFIG_LASH_JOB_CONTROL
#define ENABLE_LASH_PIPE_N_REDIRECTS 1
#define ENABLE_LASH_JOB_CONTROL 1
enum { MAX_READ = 128 }; /* size of input buffer for 'read' builtin */
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
#if ENABLE_LASH_PIPE_N_REDIRECTS
enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
REDIRECT_APPEND
};
@ -51,7 +53,7 @@ enum {
#define LASH_OPT_DONE (1)
#define LASH_OPT_SAW_QUOTE (2)
#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
#if ENABLE_LASH_PIPE_N_REDIRECTS
struct redir_struct {
enum redir_type type; /* type of redirection */
int fd; /* file descriptor being redirected */
@ -65,7 +67,7 @@ struct child_prog {
int num_redirects; /* elements in redirection array */
int is_stopped; /* is the program currently running? */
struct job *family; /* pointer back to the child's parent job */
#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
#if ENABLE_LASH_PIPE_N_REDIRECTS
struct redir_struct *redirects; /* I/O redirects */
#endif
};
@ -229,7 +231,8 @@ static int builtin_exec(struct child_prog *child)
if (child->argv[1] == NULL)
return EXIT_SUCCESS; /* Really? */
child->argv++;
while(close_me_list) close((long)llist_pop(&close_me_list));
while (close_me_list)
close((long)llist_pop(&close_me_list));
pseudo_exec(child);
/* never returns */
}
@ -289,7 +292,8 @@ static int builtin_fg_bg(struct child_prog *child)
job->stopped_progs = 0;
if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
i = kill(- job->pgrp, SIGCONT);
if (i < 0) {
if (i == ESRCH) {
remove_job(&job_list, job);
} else {
@ -365,12 +369,12 @@ static int builtin_export(struct child_prog *child)
res = putenv(v);
if (res)
bb_perror_msg("export");
#ifdef CONFIG_FEATURE_EDITING_FANCY_PROMPT
#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
if (strncmp(v, "PS1=", 4) == 0)
PS1 = getenv("PS1");
#endif
#ifdef CONFIG_LOCALE_SUPPORT
#if ENABLE_LOCALE_SUPPORT
// TODO: why getenv? "" would be just as good...
if (strncmp(v, "LC_ALL=", 7) == 0)
setlocale(LC_ALL, getenv("LC_ALL"));
@ -445,7 +449,7 @@ static int builtin_unset(struct child_prog *child)
return EXIT_SUCCESS;
}
#ifdef CONFIG_LASH_JOB_CONTROL
#if ENABLE_LASH_JOB_CONTROL
/* free up all memory from a job */
static void free_job(struct job *cmd)
{
@ -454,7 +458,7 @@ static void free_job(struct job *cmd)
for (i = 0; i < cmd->num_progs; i++) {
free(cmd->progs[i].argv);
#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
#if ENABLE_LASH_PIPE_N_REDIRECTS
if (cmd->progs[i].redirects)
free(cmd->progs[i].redirects);
#endif
@ -544,7 +548,7 @@ static void remove_job(struct jobset *j_list, struct job *job)
}
#endif
#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
#if ENABLE_LASH_PIPE_N_REDIRECTS
/* squirrel != NULL means we squirrel away copies of stdin, stdout,
* and stderr if they are redirected. */
static int setup_redirects(struct child_prog *prog, int squirrel[])
@ -891,7 +895,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
int argv_alloced;
char quote = '\0';
struct child_prog *prog;
#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
#if ENABLE_LASH_PIPE_N_REDIRECTS
int i;
char *chptr;
#endif
@ -923,7 +927,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
prog->num_redirects = 0;
prog->is_stopped = 0;
prog->family = job;
#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
#if ENABLE_LASH_PIPE_N_REDIRECTS
prog->redirects = NULL;
#endif
@ -961,8 +965,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
if ((argc_l + 1) == argv_alloced) {
argv_alloced += 5;
prog->argv = xrealloc(prog->argv,
sizeof(*prog->argv) *
argv_alloced);
sizeof(*prog->argv) * argv_alloced);
}
prog->argv[argc_l] = buf;
flag ^= LASH_OPT_SAW_QUOTE;
@ -982,13 +985,12 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
flag |= LASH_OPT_DONE;
break;
#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
#if ENABLE_LASH_PIPE_N_REDIRECTS
case '>': /* redirects */
case '<':
i = prog->num_redirects++;
prog->redirects = xrealloc(prog->redirects,
sizeof(*prog->redirects) *
(i + 1));
sizeof(*prog->redirects) * (i + 1));
prog->redirects[i].fd = -1;
if (buf != prog->argv[argc_l]) {
@ -1078,7 +1080,7 @@ empty_command_in_pipe:
break;
#endif
#ifdef CONFIG_LASH_JOB_CONTROL
#if ENABLE_LASH_JOB_CONTROL
case '&': /* background */
*inbg = 1;
/* fallthrough */
@ -1162,7 +1164,6 @@ static int pseudo_exec(struct child_prog *child)
* /bin/foo invocation will fork and exec /bin/foo, even if
* /bin/foo is a symlink to busybox.
*/
if (ENABLE_FEATURE_SH_STANDALONE_SHELL) {
char **argv_l = child->argv;
int argc_l;
@ -1205,7 +1206,7 @@ static void insert_job(struct job *newjob, int inbg)
thejob->running_progs = thejob->num_progs;
thejob->stopped_progs = 0;
#ifdef CONFIG_LASH_JOB_CONTROL
#if ENABLE_LASH_JOB_CONTROL
if (inbg) {
/* we don't wait for background thejobs to return -- append it
to the list of backgrounded thejobs and leave it alone */
@ -1237,7 +1238,8 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
child = & (newjob->progs[i]);
if ((i + 1) < newjob->num_progs) {
if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
if (pipe(pipefds) < 0)
bb_perror_msg_and_die("pipe");
nextout = pipefds[1];
} else {
if (outpipe[1] != -1) {
@ -1288,7 +1290,8 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
signal(SIGCHLD, SIG_DFL);
/* Close all open filehandles. */
while(close_me_list) close((long)llist_pop(&close_me_list));
while (close_me_list)
close((long)llist_pop(&close_me_list));
if (outpipe[1] != -1) {
close(outpipe[0]);
@ -1342,7 +1345,7 @@ static int busy_loop(FILE * input)
int i;
int inbg = 0;
int status;
#ifdef CONFIG_LASH_JOB_CONTROL
#if ENABLE_LASH_JOB_CONTROL
pid_t parent_pgrp;
/* save current owner of TTY so we can restore it on exit */
parent_pgrp = tcgetpgrp(shell_terminal);
@ -1409,7 +1412,7 @@ static int busy_loop(FILE * input)
job_list.fg = NULL;
}
}
#ifdef CONFIG_LASH_JOB_CONTROL
#if ENABLE_LASH_JOB_CONTROL
else {
/* the child was stopped */
job_list.fg->stopped_progs++;
@ -1433,7 +1436,7 @@ static int busy_loop(FILE * input)
}
free(command);
#ifdef CONFIG_LASH_JOB_CONTROL
#if ENABLE_LASH_JOB_CONTROL
/* return controlling TTY back to parent process group before exiting */
if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
bb_perror_msg("tcsetpgrp");
@ -1446,7 +1449,7 @@ static int busy_loop(FILE * input)
return 0;
}
#ifdef CONFIG_FEATURE_CLEAN_UP
#if ENABLE_FEATURE_CLEAN_UP
static void free_memory(void)
{
if (cwd && cwd != bb_msg_unknown) {
@ -1463,7 +1466,7 @@ static void free_memory(void)
void free_memory(void);
#endif
#ifdef CONFIG_LASH_JOB_CONTROL
#if ENABLE_LASH_JOB_CONTROL
/* Make sure we have a controlling tty. If we get started under a job
* aware app (like bash for example), make sure we are now in charge so
* we don't fight over who gets the foreground */
@ -1548,9 +1551,9 @@ int lash_main(int argc_l, char **argv_l)
* standard input is a terminal
* standard output is a terminal
* Refer to Posix.2, the description of the `sh' utility. */
if (argv[optind]==NULL && input==stdin &&
isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))
{
if (argv[optind] == NULL && input == stdin
&& isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
) {
opt |= LASH_OPT_i;
}
setup_job_control();