Patch from Terje Kvernes adding quite a lot of missing documentation
[oweals/busybox.git] / shell / hush.c
index 08b3b295f671d60be22070ab185a8ccd524b37ed..84818ffa312f973fd6a52ac65af9e1d77937b7aa 100644 (file)
@@ -9,14 +9,16 @@
  *
  * Credits:
  *      The parser routines proper are all original material, first
- *      written Dec 2000 and Jan 2001 by Larry Doolittle.
- *      The execution engine, the builtins, and much of the underlying
- *      support has been adapted from busybox-0.49pre's lash,
- *      which is Copyright (C) 2000 by Lineo, Inc., and
- *      written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>.
- *      That, in turn, is based in part on ladsh.c, by Michael K. Johnson and
- *      Erik W. Troan, which they placed in the public domain.  I don't know
- *      how much of the Johnson/Troan code has survived the repeated rewrites.
+ *      written Dec 2000 and Jan 2001 by Larry Doolittle.  The
+ *      execution engine, the builtins, and much of the underlying
+ *      support has been adapted from busybox-0.49pre's lash, which is
+ *      Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
+ *      written by Erik Andersen <andersen@codepoet.org>.  That, in turn,
+ *      is based in part on ladsh.c, by Michael K. Johnson and Erik W.
+ *      Troan, which they placed in the public domain.  I don't know
+ *      how much of the Johnson/Troan code has survived the repeated
+ *      rewrites.
+ *
  * Other credits:
  *      simple_itoa() was lifted from boa-0.93.15
  *      b_addchr() derived from similar w_addchar function in glibc-2.2
 /* #include <dmalloc.h> */
 /* #define DEBUG_SHELL */
 
-#ifdef BB_VER
+#if 1
 #include "busybox.h"
 #include "cmdedit.h"
 #else
-#define applet_name "hush"
+#define bb_applet_name "hush"
 #include "standalone.h"
 #define hush_main main
 #undef CONFIG_FEATURE_SH_FANCY_PROMPT
@@ -320,7 +322,7 @@ static inline void debug_printf(const char *format, ...) { }
 #define final_printf debug_printf
 
 static void __syntax(char *file, int line) {
-       error_msg("syntax error %s:%d", file, line);
+       bb_error_msg("syntax error %s:%d", file, line);
 }
 #define syntax() __syntax(__FILE__, __LINE__)
 
@@ -441,11 +443,11 @@ static struct built_in_command bltins[] = {
 
 static const char *set_cwd(void)
 {
-       if(cwd==unknown)
+       if(cwd==bb_msg_unknown)
                cwd = NULL;     /* xgetcwd(arg) called free(arg) */
        cwd = xgetcwd((char *)cwd);
        if (!cwd)
-               cwd = unknown;
+               cwd = bb_msg_unknown;
        return cwd;
 }
 
@@ -548,7 +550,7 @@ static int builtin_export(struct child_prog *child)
                }
        }
        if (res<0)
-               perror_msg("export");
+               bb_perror_msg("export");
        else if(res==0)
                res = set_local_var(name, 1);
        else
@@ -573,12 +575,12 @@ static int builtin_fg_bg(struct child_prog *child)
                        }
                }
                if (!pi) {
-                       error_msg("%s: no current job", child->argv[0]);
+                       bb_error_msg("%s: no current job", child->argv[0]);
                        return EXIT_FAILURE;
                }
        } else {
                if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
-                       error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
+                       bb_error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
                        return EXIT_FAILURE;
                }
                for (pi = job_list; pi; pi = pi->next) {
@@ -587,7 +589,7 @@ static int builtin_fg_bg(struct child_prog *child)
                        }
                }
                if (!pi) {
-                       error_msg("%s: %d: no such job", child->argv[0], jobnum);
+                       bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
                        return EXIT_FAILURE;
                }
        }
@@ -605,7 +607,7 @@ static int builtin_fg_bg(struct child_prog *child)
                if (i == ESRCH) {
                        remove_bg_job(pi);
                } else {
-                       perror_msg("kill (SIGCONT)");
+                       bb_perror_msg("kill (SIGCONT)");
                }
        }
 
@@ -728,7 +730,7 @@ static int builtin_source(struct child_prog *child)
        /* XXX search through $PATH is missing */
        input = fopen(child->argv[1], "r");
        if (!input) {
-               error_msg("Couldn't open file '%s'", child->argv[1]);
+               bb_error_msg("Couldn't open file '%s'", child->argv[1]);
                return EXIT_FAILURE;
        }
 
@@ -810,7 +812,7 @@ static void b_reset(o_string *o)
 static void b_free(o_string *o)
 {
        b_reset(o);
-       if (o->data != NULL) free(o->data);
+       free(o->data);
        o->data = NULL;
        o->maxlen = 0;
 }
@@ -880,8 +882,7 @@ static inline void setup_prompt_string(int promptmode, char **prompt_str)
 #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
        /* Set up the prompt */
        if (promptmode == 1) {
-               if (PS1)
-                       free(PS1);
+               free(PS1);
                PS1=xmalloc(strlen(cwd)+4);
                sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ?  "$ ":"# ");
                *prompt_str = PS1;
@@ -997,7 +998,7 @@ static void mark_closed(int fd)
 {
        struct close_me *tmp;
        if (close_me_head == NULL || close_me_head->fd != fd)
-               error_msg_and_die("corrupt close_me");
+               bb_error_msg_and_die("corrupt close_me");
        tmp = close_me_head;
        close_me_head = close_me_head->next;
        free(tmp);
@@ -1030,7 +1031,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
                        if (openfd < 0) {
                        /* this could get lost if stderr has been redirected, but
                           bash and ash both lose it as well (though zsh doesn't!) */
-                               perror_msg("error opening %s", redir->word.gl_pathv[0]);
+                               bb_perror_msg("error opening %s", redir->word.gl_pathv[0]);
                                return 1;
                        }
                } else {
@@ -1126,14 +1127,14 @@ static void pseudo_exec(struct child_prog *child)
 #ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
                        /* Following discussions from November 2000 on the busybox mailing
                         * list, the default configuration, (without
-                        * get_last_path_component()) lets the user force use of an
+                        * bb_get_last_path_component()) lets the user force use of an
                         * external command by specifying the full (with slashes) filename.
                         * If you enable CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN, then applets
                         * _aways_ override external commands, so if you want to run
                         * /bin/cat, it will use BusyBox cat even if /bin/cat exists on the
                         * filesystem and is _not_ busybox.  Some systems may want this,
                         * most do not.  */
-                       name = get_last_path_component(name);
+                       name = bb_get_last_path_component(name);
 #endif
                        /* Count argc for use in a second... */
                        for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
@@ -1144,7 +1145,7 @@ static void pseudo_exec(struct child_prog *child)
 #endif
                debug_printf("exec of %s\n",child->argv[0]);
                execvp(child->argv[0],child->argv);
-               perror_msg("couldn't exec: %s",child->argv[0]);
+               bb_perror_msg("couldn't exec: %s",child->argv[0]);
                _exit(1);
        } else if (child->group) {
                debug_printf("runtime nesting to group\n");
@@ -1293,11 +1294,11 @@ static int checkjobs(struct pipe* fg_pipe)
        }
 
        if (childpid == -1 && errno != ECHILD)
-               perror_msg("waitpid");
+               bb_perror_msg("waitpid");
 
        /* move the shell to the foreground */
        //if (interactive && tcsetpgrp(shell_terminal, getpgid(0)))
-       //      perror_msg("tcsetpgrp-2");
+       //      bb_perror_msg("tcsetpgrp-2");
        return -1;
 }
 
@@ -1382,7 +1383,7 @@ static int run_pipe_real(struct pipe *pi)
                                 * variable. */
                                int export_me=0;
                                char *name, *value;
-                               name = xstrdup(child->argv[i]);
+                               name = bb_xstrdup(child->argv[i]);
                                debug_printf("Local environment set: %s\n", name);
                                value = strchr(name, '=');
                                if (value)
@@ -1442,7 +1443,7 @@ 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) perror_msg_and_die("pipe");
+                       if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
                        nextout = pipefds[1];
                } else {
                        nextout=1;
@@ -1627,11 +1628,11 @@ static int run_list_real(struct pipe *pi)
                        if (interactive) {
                                /* move the new process group into the foreground */
                                if (tcsetpgrp(shell_terminal, pi->pgrp) && errno != ENOTTY)
-                                       perror_msg("tcsetpgrp-3");
+                                       bb_perror_msg("tcsetpgrp-3");
                                rcode = checkjobs(pi);
                                /* move the shell to the foreground */
                                if (tcsetpgrp(shell_terminal, getpgid(0)) && errno != ENOTTY)
-                                       perror_msg("tcsetpgrp-4");
+                                       bb_perror_msg("tcsetpgrp-4");
                        } else {
                                rcode = checkjobs(pi);
                        }
@@ -1826,9 +1827,9 @@ static int xglob(o_string *dest, int flags, glob_t *pglob)
                debug_printf("globhack returned %d\n",gr);
        }
        if (gr == GLOB_NOSPACE)
-               error_msg_and_die("out of memory during glob");
+               bb_error_msg_and_die("out of memory during glob");
        if (gr != 0) { /* GLOB_ABORTED ? */
-               error_msg("glob(3) error %d",gr);
+               bb_error_msg("glob(3) error %d",gr);
        }
        /* globprint(glob_target); */
        return gr;
@@ -1882,7 +1883,7 @@ static int set_local_var(const char *s, int flg_export)
                                result++;
                } else {
                        if(cur->flg_read_only) {
-                               error_msg("%s: readonly variable", name);
+                               bb_error_msg("%s: readonly variable", name);
                                result = -1;
                        } else {
                                if(flg_export>0 || cur->flg_export>1)
@@ -1936,7 +1937,7 @@ static void unset_local_var(const char *name)
                if(cur!=0) {
                        struct variables *next = top_vars;
                        if(cur->flg_read_only) {
-                               error_msg("%s: readonly variable", name);
+                               bb_error_msg("%s: readonly variable", name);
                                return;
                        } else {
                                if(cur->flg_export)
@@ -2140,7 +2141,7 @@ static int done_word(o_string *dest, struct p_context *ctx)
        if (ctx->pending_redirect) {
                ctx->pending_redirect=NULL;
                if (glob_target->gl_pathc != 1) {
-                       error_msg("ambiguous redirect");
+                       bb_error_msg("ambiguous redirect");
                        return 1;
                }
        } else {
@@ -2232,7 +2233,7 @@ static int redirect_dup_num(struct in_str *input)
        }
        if (ok) return d;
 
-       error_msg("ambiguous redirect");
+       bb_error_msg("ambiguous redirect");
        return -2;
 }
 
@@ -2268,14 +2269,14 @@ FILE *generate_stream_from_list(struct pipe *head)
        FILE *pf;
 #if 1
        int pid, channel[2];
-       if (pipe(channel)<0) perror_msg_and_die("pipe");
+       if (pipe(channel)<0) bb_perror_msg_and_die("pipe");
 #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
        pid=fork();
 #else
        pid=vfork();
 #endif
        if (pid<0) {
-               perror_msg_and_die("fork");
+               bb_perror_msg_and_die("fork");
        } else if (pid==0) {
                close(channel[0]);
                if (channel[1] != 1) {
@@ -2451,7 +2452,7 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
                case '-':
                case '_':
                        /* still unhandled, but should be eventually */
-                       error_msg("unhandled syntax: $%c",ch);
+                       bb_error_msg("unhandled syntax: $%c",ch);
                        return 1;
                        break;
                default:
@@ -2812,7 +2813,7 @@ int hush_main(int argc, char **argv)
                                                "   or: sh -c command [args]...\n\n");
                                exit(EXIT_FAILURE);
 #else
-                               show_usage();
+                               bb_show_usage();
 #endif
                }
        }
@@ -2846,12 +2847,12 @@ int hush_main(int argc, char **argv)
        debug_printf("\nrunning script '%s'\n", argv[optind]);
        global_argv = argv+optind;
        global_argc = argc-optind;
-       input = xfopen(argv[optind], "r");
+       input = bb_xfopen(argv[optind], "r");
        opt = parse_file_outer(input);
 
 #ifdef CONFIG_FEATURE_CLEAN_UP
        fclose(input);
-       if (cwd && cwd != unknown)
+       if (cwd && cwd != bb_msg_unknown)
                free((char*)cwd);
        {
                struct variables *cur, *tmp;