hush: style fixes

This commit is contained in:
Denis Vlasenko 2007-01-30 22:30:09 +00:00
parent 15d78fb724
commit 0c886c65de

View File

@ -79,21 +79,11 @@
*/
#include "busybox.h"
#include <ctype.h> /* isalpha, isdigit */
#include <unistd.h> /* getpid */
#include <stdlib.h> /* getenv, atoi */
#include <string.h> /* strchr */
#include <stdio.h> /* popen etc. */
#include <glob.h> /* glob, of course */
#include <stdarg.h> /* va_list */
#include <errno.h>
#include <fcntl.h>
#include <getopt.h> /* should be pretty obvious */
#include <sys/stat.h> /* ulimit */
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
//#include <sys/wait.h>
//#include <signal.h>
/* #include <dmalloc.h> */
/* #define DEBUG_SHELL */
@ -238,9 +228,9 @@ static int interactive;
static struct close_me *close_me_head;
static const char *cwd;
static struct pipe *job_list;
static unsigned int last_bg_pid;
static unsigned last_bg_pid;
static int last_jobid;
static unsigned int shell_terminal;
static unsigned shell_terminal;
static char *PS1;
static char *PS2;
static struct variables shell_ver = { "HUSH_VERSION", "0.01", 1, 1, 0 };
@ -340,7 +330,7 @@ static int b_check_space(o_string *o, int len);
static int b_addchr(o_string *o, int ch);
static void b_reset(o_string *o);
static int b_addqchr(o_string *o, int ch, int quote);
static int b_adduint(o_string *o, unsigned int i);
static int b_adduint(o_string *o, unsigned i);
/* in_str manipulations: */
static int static_get(struct in_str *i);
static int static_peek(struct in_str *i);
@ -477,8 +467,8 @@ static int builtin_env(struct child_prog *dummy ATTRIBUTE_UNUSED)
{
char **e = environ;
if (e == NULL) return EXIT_FAILURE;
for (; *e; e++) {
puts(*e);
for (*e) {
puts(*e++);
}
return EXIT_SUCCESS;
}
@ -624,7 +614,7 @@ static int builtin_help(struct child_prog *dummy ATTRIBUTE_UNUSED)
static int builtin_jobs(struct child_prog *child ATTRIBUTE_UNUSED)
{
struct pipe *job;
char *status_string;
const char *status_string;
for (job = job_list; job; job = job->next) {
if (job->running_progs == job->stopped_progs)
@ -652,9 +642,9 @@ static int builtin_read(struct child_prog *child)
if (child->argv[1]) {
char string[BUFSIZ];
char *var = 0;
char *var = NULL;
string[0] = 0; /* In case stdin has only EOF */
string[0] = '\0'; /* In case stdin has only EOF */
/* read string */
fgets(string, sizeof(string), stdin);
chomp(string);
@ -668,11 +658,10 @@ static int builtin_read(struct child_prog *child)
bb_perror_msg("read");
free(var); /* So not move up to avoid breaking errno */
return res;
} else {
}
do res = getchar(); while (res != '\n' && res != EOF);
return 0;
}
}
/* built-in 'set VAR=value' handler */
static int builtin_set(struct child_prog *child)
@ -745,7 +734,8 @@ static int builtin_umask(struct child_prog *child)
return EXIT_FAILURE;
}
} else {
printf("%.3o\n", (unsigned int) (new_umask=umask(0)));
new_umask = umask(0);
printf("%.3o\n", (unsigned) new_umask);
}
umask(new_umask);
return EXIT_SUCCESS;
@ -784,7 +774,8 @@ static int b_check_space(o_string *o, int len)
static int b_addchr(o_string *o, int ch)
{
debug_printf("b_addchr: %c %d %p\n", ch, o->length, o);
if (b_check_space(o, 1)) return B_NOSPAC;
if (b_check_space(o, 1))
return B_NOSPAC;
o->data[o->length] = ch;
o->length++;
o->data[o->length] = '\0';
@ -795,7 +786,8 @@ static void b_reset(o_string *o)
{
o->length = 0;
o->nonnull = 0;
if (o->data != NULL) *o->data = '\0';
if (o->data != NULL)
*o->data = '\0';
}
static void b_free(o_string *o)
@ -814,17 +806,17 @@ static int b_addqchr(o_string *o, int ch, int quote)
if (quote && strchr("*?[\\",ch)) {
int rc;
rc = b_addchr(o, '\\');
if (rc) return rc;
if (rc)
return rc;
}
return b_addchr(o, ch);
}
/* belongs in utility.c */
static char *simple_itoa(unsigned int i)
static char *simple_itoa(unsigned i)
{
/* 21 digits plus null terminator, good for 64-bit or smaller ints */
static char local[22];
char *p = &local[21];
static char local[sizeof(int)*3 + 2];
char *p = &local[sizeof(int)*3 + 2 - 1];
*p-- = '\0';
do {
*p-- = '0' + i % 10;
@ -833,7 +825,7 @@ static char *simple_itoa(unsigned int i)
return p + 1;
}
static int b_adduint(o_string *o, unsigned int i)
static int b_adduint(o_string *o, unsigned i)
{
int r;
char *p = simple_itoa(i);
@ -860,7 +852,7 @@ static void cmdedit_set_initial_prompt(void)
PS1 = NULL;
#else
PS1 = getenv("PS1");
if(PS1==0)
if (PS1 == NULL)
PS1 = "\\w \\$ ";
#endif
}
@ -926,7 +918,7 @@ static int file_get(struct in_str *i)
/* need to double check i->file because we might be doing something
* more complicated by now, like sourcing or substituting. */
if (i->__promptme && interactive && i->file == stdin) {
while(! i->p || (interactive && strlen(i->p)==0) ) {
while (!i->p || !(interactive && strlen(i->p))) {
get_user_input(i);
}
i->promptmode = 2;
@ -940,7 +932,8 @@ static int file_get(struct in_str *i)
debug_printf("b_getch: got a %d\n", ch);
}
if (ch == '\n') i->__promptme=1;
if (ch == '\n')
i->__promptme = 1;
return ch;
}
@ -1072,10 +1065,12 @@ static void pseudo_exec(struct child_prog *child)
const struct built_in_command *x;
if (child->argv) {
for (i = 0; is_assignment(child->argv[i]); i++) {
debug_printf("pid %d environment modification: %s\n",getpid(),child->argv[i]);
debug_printf("pid %d environment modification: %s\n",
getpid(), child->argv[i]);
p = insert_var_value(child->argv[i]);
putenv(strdup(p));
if (p != child->argv[i]) free(p);
if (p != child->argv[i])
free(p);
}
child->argv += i; /* XXX this hack isn't so horrible, since we are about
to exit, and therefore don't need to keep data
@ -1118,7 +1113,8 @@ static void pseudo_exec(struct child_prog *child)
char *name = child->argv[0];
/* Count argc for use in a second... */
for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
for (argc_l = 0; *argv_l; argv_l++, argc_l++)
/**/;
optind = 1;
debug_printf("running applet %s\n", name);
run_applet_by_name(name, argc_l, child->argv);
@ -1156,7 +1152,8 @@ static void insert_bg_job(struct pipe *pi)
if (!job_list) {
thejob = job_list = xmalloc(sizeof(*thejob));
} else {
for (thejob = job_list; thejob->next; thejob = thejob->next) /* nothing */;
for (thejob = job_list; thejob->next; thejob = thejob->next)
/* nothing */;
thejob->next = xmalloc(sizeof(*thejob));
thejob = thejob->next;
}
@ -1172,10 +1169,10 @@ static void insert_bg_job(struct pipe *pi)
{
char *bar = thejob->text;
char **foo = pi->progs[0].argv;
while(foo && *foo) {
if (foo)
while (*foo)
bar += sprintf(bar, "%s ", *foo++);
}
}
/* we don't wait for background thejobs to return -- append it
to the list of backgrounded thejobs and leave it alone */
@ -1229,7 +1226,7 @@ static int checkjobs(struct pipe* fg_pipe)
if (fg_pipe->progs[i].pid == childpid) {
if (i == fg_pipe->num_progs-1)
rcode = WEXITSTATUS(status);
(fg_pipe->num_progs)--;
fg_pipe->num_progs--;
return rcode;
}
}
@ -1245,7 +1242,8 @@ static int checkjobs(struct pipe* fg_pipe)
}
if (pi == NULL) {
debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
debug_printf("checkjobs: pid %d was not in our list!\n",
childpid);
continue;
}
@ -1255,7 +1253,8 @@ static int checkjobs(struct pipe* fg_pipe)
pi->progs[prognum].pid = 0;
if (!pi->running_progs) {
printf(JOB_STATUS_FORMAT, pi->jobid, "Done", pi->text);
printf(JOB_STATUS_FORMAT, pi->jobid,
"Done", pi->text);
remove_bg_job(pi);
}
} else {
@ -1318,11 +1317,11 @@ static int run_pipe_real(struct pipe *pi)
restore_redirects(squirrel);
return rcode;
} else if (pi->num_progs == 1 && pi->progs[0].argv != NULL) {
for (i=0; is_assignment(child->argv[i]); i++) { /* nothing */ }
for (i = 0; is_assignment(child->argv[i]); i++)
/* nothing */;
if (i != 0 && child->argv[i] == NULL) {
/* assignments, but no command: set the local environment */
for (i = 0; child->argv[i] != NULL; i++) {
/* Ok, this case is tricky. We have to decide if this is a
* local variable, or an already exported variable. If it is
* already exported, we have to export the new value. If it is
@ -1342,7 +1341,8 @@ static int run_pipe_real(struct pipe *pi)
free(name);
p = insert_var_value(child->argv[i]);
set_local_var(p, export_me);
if (p != child->argv[i]) free(p);
if (p != child->argv[i])
free(p);
}
return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
}
@ -1391,7 +1391,8 @@ static int run_pipe_real(struct pipe *pi)
/* pipes are inserted between pairs of commands */
if ((i + 1) < pi->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 {
nextout = 1;
@ -1400,11 +1401,11 @@ static int run_pipe_real(struct pipe *pi)
/* XXX test for failed fork()? */
#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
if (!(child->pid = fork()))
child->pid = fork();
#else
if (!(child->pid = vfork()))
child->pid = vfork();
#endif
{
if (!child->pid) {
/* Set the handling for job control signals back to the default. */
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
@ -1446,7 +1447,6 @@ static int run_pipe_real(struct pipe *pi)
pseudo_exec(child);
}
/* put our child in the process group whose leader is the
first process in this pipe */
if (pi->pgrp < 0) {
@ -1482,24 +1482,23 @@ static int run_list_real(struct pipe *pi)
reserved_style rmode, skip_more_in_this_rmode = RES_XXXX;
/* check syntax for "for" */
for (rpipe = pi; rpipe; rpipe = rpipe->next) {
if ((rpipe->r_mode == RES_IN ||
rpipe->r_mode == RES_FOR) &&
(rpipe->next == NULL)) {
if ((rpipe->r_mode == RES_IN || rpipe->r_mode == RES_FOR)
&& (rpipe->next == NULL)
) {
syntax();
return 1;
}
if ((rpipe->r_mode == RES_IN &&
(rpipe->next->r_mode == RES_IN &&
rpipe->next->progs->argv != NULL))||
(rpipe->r_mode == RES_FOR &&
rpipe->next->r_mode != RES_IN)) {
if ((rpipe->r_mode == RES_IN && rpipe->next->r_mode == RES_IN && rpipe->next->progs->argv != NULL)
|| (rpipe->r_mode == RES_FOR && rpipe->next->r_mode != RES_IN)
) {
syntax();
return 1;
}
}
for (; pi; pi = (flag_restore != 0) ? rpipe : pi->next) {
if (pi->r_mode == RES_WHILE || pi->r_mode == RES_UNTIL ||
pi->r_mode == RES_FOR) {
if (pi->r_mode == RES_WHILE || pi->r_mode == RES_UNTIL
|| pi->r_mode == RES_FOR
) {
flag_restore = 0;
if (!rpipe) {
flag_rep = 0;
@ -1507,21 +1506,28 @@ static int run_list_real(struct pipe *pi)
}
}
rmode = pi->r_mode;
debug_printf("rmode=%d if_code=%d next_if_code=%d skip_more=%d\n", rmode, if_code, next_if_code, skip_more_in_this_rmode);
debug_printf("rmode=%d if_code=%d next_if_code=%d skip_more=%d\n",
rmode, if_code, next_if_code, skip_more_in_this_rmode);
if (rmode == skip_more_in_this_rmode && flag_skip) {
if (pi->followup == PIPE_SEQ) flag_skip=0;
if (pi->followup == PIPE_SEQ)
flag_skip = 0;
continue;
}
flag_skip = 1;
skip_more_in_this_rmode = RES_XXXX;
if (rmode == RES_THEN || rmode == RES_ELSE) if_code = next_if_code;
if (rmode == RES_THEN && if_code) continue;
if (rmode == RES_ELSE && !if_code) continue;
if (rmode == RES_ELIF && !if_code) break;
if (rmode == RES_THEN || rmode == RES_ELSE)
if_code = next_if_code;
if (rmode == RES_THEN && if_code)
continue;
if (rmode == RES_ELSE && !if_code)
continue;
if (rmode == RES_ELIF && !if_code)
break;
if (rmode == RES_FOR && pi->num_progs) {
if (!list) {
/* if no variable values after "in" we skip "for" */
if (!pi->next->progs->argv) continue;
if (!pi->next->progs->argv)
continue;
/* create list of variable values */
list = make_list_in(pi->next->progs->argv,
pi->progs->argv[0]);
@ -1530,27 +1536,27 @@ static int run_list_real(struct pipe *pi)
pi->progs->argv[0] = NULL;
flag_rep = 1;
}
if (!(*list)) {
if (!*list) {
free(pi->progs->argv[0]);
free(save_list);
list = NULL;
flag_rep = 0;
pi->progs->argv[0] = save_name;
pi->progs->glob_result.gl_pathv[0] =
pi->progs->argv[0];
pi->progs->glob_result.gl_pathv[0] = pi->progs->argv[0];
continue;
} else {
/* insert new value from list for variable */
if (pi->progs->argv[0])
free(pi->progs->argv[0]);
pi->progs->argv[0] = *list++;
pi->progs->glob_result.gl_pathv[0] =
pi->progs->argv[0];
pi->progs->glob_result.gl_pathv[0] = pi->progs->argv[0];
}
}
if (rmode == RES_IN) continue;
if (rmode == RES_IN)
continue;
if (rmode == RES_DO) {
if (!flag_rep) continue;
if (!flag_rep)
continue;
}
if ((rmode == RES_DONE)) {
if (flag_rep) {
@ -1559,7 +1565,8 @@ static int run_list_real(struct pipe *pi)
rpipe = NULL;
}
}
if (pi->num_progs == 0) continue;
if (pi->num_progs == 0)
continue;
save_num_progs = pi->num_progs; /* save number of programs */
rcode = run_pipe_real(pi);
debug_printf("run_pipe_real returned %d\n",rcode);
@ -1594,9 +1601,11 @@ static int run_list_real(struct pipe *pi)
flag_rep = !last_return_code;
if (rmode == RES_UNTIL)
flag_rep = last_return_code;
if ( (rcode==EXIT_SUCCESS && pi->followup==PIPE_OR) ||
(rcode!=EXIT_SUCCESS && pi->followup==PIPE_AND) )
if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
|| (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
) {
skip_more_in_this_rmode = rmode;
}
checkjobs(NULL);
}
return rcode;
@ -1825,7 +1834,7 @@ static int set_local_var(const char *s, int flg_export)
result = -1;
} else {
cur->name = strdup(name);
if(cur->name == 0) {
if (cur->name) {
free(cur);
result = -1;
} else {
@ -1834,7 +1843,8 @@ static int set_local_var(const char *s, int flg_export)
cur->next = 0;
cur->flg_export = flg_export;
cur->flg_read_only = 0;
while(bottom->next) bottom=bottom->next;
while (bottom->next)
bottom = bottom->next;
bottom->next = cur;
}
}
@ -1865,7 +1875,7 @@ static void unset_local_var(const char *name)
if (cur->flg_read_only) {
bb_error_msg("%s: readonly variable", name);
return;
} else {
}
if (cur->flg_export)
unsetenv(cur->name);
free((char*)cur->name);
@ -1873,7 +1883,6 @@ static void unset_local_var(const char *name)
while (next->next != cur)
next = next->next;
next->next = cur->next;
}
free(cur);
}
}
@ -1881,9 +1890,11 @@ static void unset_local_var(const char *name)
static int is_assignment(const char *s)
{
if (s==NULL || !isalpha(*s)) return 0;
++s;
while(isalnum(*s) || *s=='_') ++s;
if (!s || !isalpha(*s))
return 0;
s++;
while (isalnum(*s) || *s == '_')
s++;
return *s == '=';
}
@ -1991,9 +2002,8 @@ static int reserved_word(o_string *dest, struct p_context *ctx)
{ "done", RES_DONE, FLAG_END }
};
struct reserved_combo *r;
for (r=reserved_list;
#define NRES sizeof(reserved_list)/sizeof(struct reserved_combo)
r<reserved_list+NRES; r++) {
for (r = reserved_list; r < reserved_list+NRES; r++) {
if (strcmp(dest->data, r->literal) == 0) {
debug_printf("found reserved word %s, code %d\n",r->literal,r->code);
if (r->flag & FLAG_START) {
@ -2427,7 +2437,12 @@ int parse_stream(o_string *dest, struct p_context *ctx,
if (m!=2) switch (ch) {
case '#':
if (dest->length == 0 && !dest->quote) {
while(ch=b_peek(input),ch!=EOF && ch!='\n') { b_getch(input); }
while (1) {
ch = b_peek(input);
if (ch == EOF || ch == '\n')
break;
b_getch(input);
}
} else {
b_addqchr(dest, ch, dest->quote);
}
@ -2445,7 +2460,10 @@ int parse_stream(o_string *dest, struct p_context *ctx,
break;
case '\'':
dest->nonnull = 1;
while(ch=b_getch(input),ch!=EOF && ch!='\'') {
while (1) {
ch = b_getch(input);
if (ch == EOF || ch == '\'')
break;
b_addchr(dest,ch);
}
if (ch == EOF) {