9
0
Fork 0

hush: cleanup coding style

General coding style cleanup

- add some blank lines
- add whitespaces on on both sides of operators

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2012-04-29 09:19:47 +02:00
parent b1c5b646b3
commit 3996c942f2
1 changed files with 221 additions and 109 deletions

View File

@ -327,12 +327,15 @@ static int b_check_space(o_string *o, int len)
static int b_addchr(o_string *o, int ch)
{
debug("b_addchr: %c %d %p\n", ch, o->length, o);
debug("%s: %c %d %p\n", __func__, ch, o->length, o);
if (b_check_space(o, 1))
return B_NOSPAC;
o->data[o->length] = ch;
o->length++;
o->data[o->length] = '\0';
return 0;
}
@ -340,7 +343,9 @@ 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)
@ -358,10 +363,12 @@ 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;
}
return b_addchr(o, ch);
}
@ -369,6 +376,7 @@ static int b_adduint(o_string *o, unsigned int i)
{
int r;
char *p = simple_itoa(i);
/* no escape checking necessary */
do {
r = b_addchr(o, *p++);
@ -380,8 +388,10 @@ static int b_adduint(o_string *o, unsigned int i)
static int static_get(struct in_str *i)
{
int ch = *i->p++;
if (ch == '\0')
return EOF;
return ch;
}
@ -413,11 +423,13 @@ static void get_user_input(struct in_str *i)
static char the_command[CONFIG_CBSIZE];
i->__promptme = 1;
if (i->promptmode == 1) {
n = readline(getprompt(), console_buffer, CONFIG_CBSIZE);
} else {
n = readline(CONFIG_PROMPT_HUSH_PS2, console_buffer, CONFIG_CBSIZE);
}
if (n == -1 ) {
i->__promptme = 0;
n = 0;
@ -425,6 +437,7 @@ static void get_user_input(struct in_str *i)
console_buffer[n] = '\n';
console_buffer[n + 1]= '\0';
if (i->promptmode == 1) {
strcpy(the_command,console_buffer);
i->p = the_command;
@ -469,7 +482,7 @@ static int file_get(struct in_str *i)
if (i->p && *i->p) {
ch = *i->p++;
}
debug("b_getch: got a %d\n", ch);
debug("%s: got a %d\n", __func__, ch);
}
return ch;
}
@ -588,14 +601,20 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi)
* pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
*/
if (pi->num_progs == 1)
child = & (pi->progs[0]);
child = &pi->progs[0];
if (pi->num_progs == 1 && child->group) {
int rcode;
debug("non-subshell grouping\n");
rcode = run_list_real(ctx, child->group);
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++) {
@ -608,14 +627,18 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi)
* variable. */
int export_me = 0;
char *name, *value;
name = xstrdup(child->argv[i]);
debug("Local environment set: %s\n", name);
value = strchr(name, '=');
if (value)
*value = 0;
free(name);
p = insert_var_value(child->argv[i]);
set_local_var(p, export_me);
if (p != child->argv[i])
free(p);
}
@ -624,6 +647,7 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi)
for (i = 0; is_assignment(child->argv[i]); i++) {
p = insert_var_value(child->argv[i]);
set_local_var(p, 0);
if (p != child->argv[i]) {
child->sp--;
free(p);
@ -632,10 +656,12 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi)
if (child->sp) {
char * str = NULL;
struct p_context ctx1;
str = make_string((child->argv + i));
parse_string_outer(&ctx1, str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING);
release_context(&ctx1);
free(str);
return last_return_code;
}
#ifdef CONFIG_HUSH_GETOPT
@ -658,6 +684,7 @@ static int run_list_real(struct p_context *ctx, struct pipe *pi)
int flag_restore = 0;
int if_code=0, next_if_code=0; /* need double-buffer to handle elif */
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 ||
@ -688,26 +715,35 @@ static int run_list_real(struct p_context *ctx, struct pipe *pi)
}
}
rmode = pi->r_mode;
debug("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("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_FOR && pi->num_progs) {
if (!list) {
/* if no variable values after "in" we skip "for" */
if (!pi->next->progs->argv)
continue;
/* create list of variable values */
list = make_list_in(pi->next->progs->argv,
pi->progs->argv[0]);
@ -732,32 +768,42 @@ static int run_list_real(struct p_context *ctx, struct pipe *pi)
}
if (rmode == RES_IN)
continue;
if (rmode == RES_DO) {
if (!flag_rep)
continue;
}
if ((rmode == RES_DONE)) {
if (rmode == RES_DONE) {
if (flag_rep) {
flag_restore = 1;
} else {
rpipe = NULL;
}
}
if (pi->num_progs == 0)
continue;
rcode = run_pipe_real(ctx, pi);
debug("run_pipe_real returned %d\n",rcode);
if (rcode < -1) {
last_return_code = -rcode - 2;
return rcode; /* exit */
}
last_return_code = rcode;
if (rmode == RES_IF || rmode == RES_ELIF )
next_if_code = rcode; /* can be overwritten a number of times */
if (rmode == RES_WHILE)
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) )
skip_more_in_this_rmode = rmode;
@ -782,11 +828,14 @@ static int free_pipe(struct pipe *pi, int indent)
int a, i, ret_code = 0;
for (i = 0; i < pi->num_progs; i++) {
child = &pi->progs[i];
final_printf("%s command %d:\n", indenter(indent), i);
if (child->argv) {
for (a = 0,p = child->argv; *p; a++,p++) {
final_printf("%s argv[%d] = %s\n",indenter(indent),a,*p);
final_printf("%s argv[%d] = %s\n",
indenter(indent),a,*p);
}
globfree(&child->glob_result);
child->argv = NULL;
@ -797,8 +846,10 @@ static int free_pipe(struct pipe *pi, int indent)
final_printf("%s (nil)\n",indenter(indent));
}
}
free(pi->progs); /* children are an array, they get freed all at once */
pi->progs = NULL;
return ret_code;
}
@ -806,6 +857,7 @@ static int free_pipe_list(struct pipe *head, int indent)
{
int rcode = 0; /* if list has no members */
struct pipe *pi, *next;
for (pi = head; pi; pi = next) {
final_printf("%s pipe reserved mode %d\n", indenter(indent), pi->r_mode);
rcode = free_pipe(pi, indent);
@ -831,10 +883,13 @@ static int globhack(const char *src, int flags, glob_t *pglob)
char *dest;
for (cnt = 1, s = src; s && *s; s++) {
if (*s == '\\' && strchr("*[?", *(s + 1))) s++;
if (*s == '\\' && strchr("*[?", *(s + 1)))
s++;
cnt++;
}
dest = xmalloc(cnt);
if (!(flags & GLOB_APPEND)) {
globfree(pglob);
pglob->gl_pathv = NULL;
@ -845,11 +900,15 @@ static int globhack(const char *src, int flags, glob_t *pglob)
pglob->gl_pathv = xrealloc(pglob->gl_pathv, (pathc + 1) * sizeof(*pglob->gl_pathv));
pglob->gl_pathv[pathc - 1] = dest;
pglob->gl_pathv[pathc] = NULL;
for (s = src; s && *s; s++, dest++) {
if (*s == '\\' && strchr("*[?", *(s + 1))) s++;
if (*s == '\\' && strchr("*[?", *(s + 1)))
s++;
*dest = *s;
}
*dest = '\0';
return 0;
}
@ -858,7 +917,8 @@ static int glob_needed(const char *s)
{
#ifdef CONFIG_GLOB
for (; *s; s++) {
if (*s == '\\') s++;
if (*s == '\\')
s++;
if (strchr("*[?",*s))
return 1;
}
@ -895,7 +955,9 @@ static int xglob(o_string *dest, int flags, glob_t *pglob)
if (gr != 0) { /* GLOB_ABORTED ? */
error_msg("glob(3) error %d",gr);
}
/* globprint(glob_target); */
return gr;
}
@ -910,6 +972,7 @@ static int run_list(struct p_context *ctx, struct pipe *pi)
* In the long run that function can be merged with run_list_real,
* but doing that now would hobble the debugging effort. */
free_pipe_list(pi, 0);
return rcode;
}
@ -955,15 +1018,19 @@ static int is_assignment(const char *s)
if (!isalpha(*s))
return 0;
++s;
while(isalnum(*s) || *s=='_' || *s=='.')
++s;
return *s=='=';
}
static struct pipe *new_pipe(void) {
return (struct pipe *)xzalloc(sizeof(struct pipe));
static struct pipe *new_pipe(void)
{
return xzalloc(sizeof(struct pipe));
}
static void initialize_context(struct p_context *ctx)
@ -1025,18 +1092,23 @@ static struct reserved_combo reserved_list[] = {
static int reserved_word(o_string *dest, struct p_context *ctx)
{
struct reserved_combo *r;
for (r=reserved_list;
r<reserved_list+NRES; r++) {
for (r = reserved_list; r < reserved_list + NRES; r++) {
if (strcmp(dest->data, r->literal) == 0) {
debug("found reserved word %s, code %d\n",r->literal,r->code);
if (r->flag & FLAG_START) {
struct p_context *new = xmalloc(sizeof(struct p_context));
debug("push stack\n");
if (ctx->w == RES_IN || ctx->w == RES_FOR) {
syntax();
free(new);
ctx->w = RES_SNTX;
b_reset(dest);
return 1;
}
*new = *ctx; /* physical copy */
@ -1048,21 +1120,28 @@ static int reserved_word(o_string *dest, struct p_context *ctx)
b_reset(dest);
return 1;
}
ctx->w = r->code;
ctx->old_flag = r->flag;
if (ctx->old_flag & FLAG_END) {
struct p_context *old;
debug("pop stack\n");
done_pipe(ctx,PIPE_SEQ);
old = ctx->stack;
old->child->group = ctx->list_head;
*ctx = *old; /* physical copy */
free(old);
}
b_reset(dest);
return 1;
}
}
return 0;
}
@ -1074,7 +1153,7 @@ static int done_word(o_string *dest, struct p_context *ctx)
glob_t *glob_target;
int gr, flags = 0;
debug("done_word: %s %p\n", dest->data, child);
debug("%s: %s %p\n", __func__, dest->data, child);
if (dest->length == 0 && !dest->nonnull) {
debug(" true null, ignored\n");
return 0;
@ -1106,6 +1185,7 @@ static int done_word(o_string *dest, struct p_context *ctx)
done_word(dest,ctx);
done_pipe(ctx,PIPE_SEQ);
}
return 0;
}
@ -1121,16 +1201,14 @@ static int done_command(struct p_context *ctx)
struct pipe *pi = ctx->pipe;
struct child_prog *prog = ctx->child;
if (prog && prog->group == NULL
&& prog->argv == NULL
) {
debug("done_command: skipping null command\n");
if (prog && prog->group == NULL && prog->argv == NULL) {
debug("%s: skipping null command\n", __func__);
return 0;
} else if (prog) {
pi->num_progs++;
debug("done_command: num_progs incremented to %d\n",pi->num_progs);
debug("%s: num_progs incremented to %d\n", __func__, pi->num_progs);
} else {
debug("done_command: initializing\n");
debug("%s: initializing\n", __func__);
}
pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs + 1));
@ -1150,15 +1228,26 @@ static int done_command(struct p_context *ctx)
static int done_pipe(struct p_context *ctx, pipe_style type)
{
struct pipe *new_p;
done_command(ctx); /* implicit closure of previous command */
debug("done_pipe, type %d\n", type);
<<<<<<< HEAD
debug("%s: type %d\n", __func__, type);
=======
debug("%s: type %d\n", __func__, type);
>>>>>>> f5c1f0c... hush use func
ctx->pipe->followup = type;
ctx->pipe->r_mode = ctx->w;
new_p = new_pipe();
ctx->pipe->next = new_p;
ctx->pipe = new_p;
ctx->child = NULL;
done_command(ctx); /* set up new pipe to accept commands */
return 0;
}
@ -1179,7 +1268,9 @@ static const char *lookup_param(char *src)
static int parse_string(o_string *dest, struct p_context *ctx, const char *src)
{
struct in_str foo;
setup_string_in_str(&foo, src);
return parse_stream(dest, ctx, &foo, '\0');
}
@ -1188,6 +1279,7 @@ static char *get_dollar_var(char ch)
static char buf[40];
buf[0] = '\0';
switch (ch) {
case '?':
sprintf(buf, "%u", (unsigned int)last_return_code);
@ -1203,15 +1295,20 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
{
int advance = 0, i;
int ch = input->peek(input); /* first character after the $ */
debug("handle_dollar: ch=%c\n",ch);
debug("%s: ch=%c\n", __func__, ch);
if (isalpha(ch)) {
b_addchr(dest, SPECIAL_VAR_SYMBOL);
ctx->child->sp++;
while (ch = b_peek(input),isalnum(ch) || ch == '_' || ch == '.') {
b_getch(input);
b_addchr(dest,ch);
}
b_addchr(dest, SPECIAL_VAR_SYMBOL);
} else if (isdigit(ch)) {
i = ch - '0'; /* XXX is $0 special? */
@ -1237,7 +1334,9 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
b_addchr(dest, SPECIAL_VAR_SYMBOL);
ctx->child->sp++;
b_getch(input);
/* XXX maybe someone will try to escape the '}' */
while(ch = b_getch(input),ch != EOF && ch != '}') {
b_addchr(dest,ch);
}
@ -1258,6 +1357,7 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
*/
if (advance)
b_getch(input);
return 0;
}
@ -1273,14 +1373,16 @@ static int parse_stream(o_string *dest, struct p_context *ctx,
* A single-quote triggers a bypass of the main loop until its mate is
* found. When recursing, quote state is passed in via dest->quote. */
debug("parse_stream, end_trigger=%d\n",end_trigger);
debug("%s: end_trigger=%d\n", __func__, end_trigger);
while ((ch = b_getch(input)) != EOF) {
m = map[ch];
if (input->__promptme == 0)
return 1;
next = (ch == '\n') ? 0 : b_peek(input);
debug("parse_stream: ch=%c (%d) m=%d quote=%d - %c\n",
debug("%s: ch=%c (%d) m=%d quote=%d - %c\n",
__func__,
ch >= ' ' ? ch : '.', ch, m,
dest->quote, ctx->stack == NULL ? '*' : '.');
@ -1300,7 +1402,7 @@ static int parse_stream(o_string *dest, struct p_context *ctx,
}
if (ch == end_trigger && !dest->quote && ctx->w==RES_NONE) {
debug("leaving parse_stream (triggered)\n");
debug("%s: leaving (triggered)\n", __func__);
return 0;
}
@ -1386,15 +1488,19 @@ static int parse_stream(o_string *dest, struct p_context *ctx,
* that is, we were really supposed to get end_trigger, and never got
* one before the EOF. Can't use the standard "syntax error" return code,
* so that parse_stream_outer can distinguish the EOF and exit smoothly. */
debug("leaving parse_stream (EOF)\n");
if (end_trigger != '\0') return -1;
debug("%s: leaving (EOF)\n", __func__);
if (end_trigger != '\0')
return -1;
return 0;
}
static void mapset(const unsigned char *set, int code)
{
const unsigned char *s;
for (s=set; *s; s++) map[*s] = code;
for (s = set; *s; s++)
map[*s] = code;
}
static void update_ifs_map(void)
@ -1402,7 +1508,9 @@ static void update_ifs_map(void)
/* char *ifs and char map[256] are both globals. */
ifs = (uchar *)getenv("IFS");
ifs = NULL;
if (ifs == NULL) ifs=(uchar *)" \t\n";
if (ifs == NULL)
ifs = (uchar *)" \t\n";
/* Precompute a list of 'flow through' behavior so it can be treated
* quickly up front. Computation is necessary because of IFS.
* Special case handling of IFS == " \t\n" is not implemented.
@ -1422,14 +1530,18 @@ static int parse_stream_outer(struct p_context *ctx, struct in_str *inp, int fla
o_string temp = NULL_O_STRING;
int rcode;
int code = 0;
do {
ctx->type = flag;
initialize_context(ctx);
update_ifs_map();
if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING))
mapset((uchar *)";$&|", 0);
inp->promptmode = 1;
rcode = parse_stream(&temp, ctx, inp, '\n');
if (rcode != 1 && ctx->old_flag != 0) {
syntax();
}