Fix a tar bug: tar removed leading '/'s for symlink targets.
[oweals/busybox.git] / sh.c
diff --git a/sh.c b/sh.c
index e143cfe740393c2ac1a51c127104b3559ef12873..44ffe968f2c6a4096da7abd101c6b296a3fde313 100644 (file)
--- a/sh.c
+++ b/sh.c
@@ -1,6 +1,6 @@
 /* vi: set sw=4 ts=4: */
 /*
- * BusyBox Shell
+ * lash -- the BusyBox Lame-Ass SHell
  *
  * Copyright (C) 2000 by Lineo, inc.
  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <unistd.h>
-
-
 #ifdef BB_FEATURE_SH_COMMAND_EDITING
 #include "cmdedit.h"
 #endif
 
+
 #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
 
 
@@ -98,7 +97,7 @@ static int shell_fg_bg(struct job *cmd, struct jobSet *jobList);
 static int shell_help(struct job *cmd, struct jobSet *junk);
 static int shell_jobs(struct job *dummy, struct jobSet *jobList);
 static int shell_pwd(struct job *dummy, struct jobSet *junk);
-static int shell_set(struct job *cmd, struct jobSet *junk);
+static int shell_export(struct job *cmd, struct jobSet *junk);
 static int shell_source(struct job *cmd, struct jobSet *jobList);
 static int shell_unset(struct job *cmd, struct jobSet *junk);
 
@@ -120,11 +119,10 @@ static struct builtInCommand bltins[] = {
        {"fg", "Bring job into the foreground", "fg [%%job]", shell_fg_bg},
        {"jobs", "Lists the active jobs", "jobs", shell_jobs},
        {"pwd", "Print current directory", "pwd", shell_pwd},
-       {"set", "Set environment variable", "set [VAR=value]", shell_set},
+       {"export", "Set environment variable", "export [VAR=value]", shell_export},
        {"unset", "Unset environment variable", "unset VAR", shell_unset},
        
-               {".", "Source-in and run commands in a file", ". filename",
-        shell_source},
+       {".", "Source-in and run commands in a file", ". filename", shell_source},
        {"help", "List shell built-in commands", "help", shell_help},
        {NULL, NULL, NULL, NULL}
 };
@@ -137,6 +135,16 @@ static const char shell_usage[] =
 static char cwd[1024];
 static char *prompt = "# ";
 
+#ifdef BB_FEATURE_SH_COMMAND_EDITING
+void win_changed(int sig)
+{
+       struct winsize win = { 0, 0 };
+       ioctl(0, TIOCGWINSZ, &win);
+       if (win.ws_col > 0) {
+               cmdedit_setwidth( win.ws_col - 1);
+       }
+}
+#endif
 
 
 /* built-in 'cd <path>' handler */
@@ -182,7 +190,7 @@ static int shell_exit(struct job *cmd, struct jobSet *junk)
 static int shell_fg_bg(struct job *cmd, struct jobSet *jobList)
 {
        int i, jobNum;
-       struct job *job;
+       struct job *job=NULL;
 
        if (!jobList->head) {
                if (!cmd->progs[0].argv[1] || cmd->progs[0].argv[2]) {
@@ -268,8 +276,8 @@ static int shell_pwd(struct job *dummy, struct jobSet *junk)
        return TRUE;
 }
 
-/* built-in 'set VAR=value' handler */
-static int shell_set(struct job *cmd, struct jobSet *junk)
+/* built-in 'export VAR=value' handler */
+static int shell_export(struct job *cmd, struct jobSet *junk)
 {
        int res;
 
@@ -278,7 +286,7 @@ static int shell_set(struct job *cmd, struct jobSet *junk)
        }
        res = putenv(cmd->progs[0].argv[1]);
        if (res)
-               fprintf(stdout, "set: %s\n", strerror(errno));
+               fprintf(stdout, "export: %s\n", strerror(errno));
        return (res);
 }
 
@@ -396,11 +404,19 @@ static void checkJobs(struct jobSet *jobList)
 static int getCommand(FILE * source, char *command)
 {
        if (source == stdin) {
-               fprintf(stdout, "BBSHELL %s %s", cwd, prompt);
-               fflush(stdout);
 #ifdef BB_FEATURE_SH_COMMAND_EDITING
-               cmdedit_read_input(fileno(stdin), fileno(stdout), command);
+               int len;
+               char *promptStr;
+               len=fprintf(stdout, "%s %s", cwd, prompt);
+               fflush(stdout);
+               promptStr=(char*)malloc(sizeof(char)*(len+1));
+               sprintf(promptStr, "%s %s", cwd, prompt);
+               cmdedit_read_input(promptStr, command);
+               free( promptStr);
                return 0;
+#else
+               fprintf(stdout, "%s %s", cwd, prompt);
+               fflush(stdout);
 #endif
        }
 
@@ -693,7 +709,6 @@ static int parseCommand(char **commandPtr, struct job *job, int *isBg)
                strcpy(job->text, *commandPtr);
        } else {
                /* This leaves any trailing spaces, which is a bit sloppy */
-
                count = returnCommand - *commandPtr;
                job->text = malloc(count + 1);
                strncpy(job->text, *commandPtr, count);
@@ -790,14 +805,12 @@ static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
        if (inBg) {
                /* we don't wait for background jobs to return -- append it 
                   to the list of backgrounded jobs and leave it alone */
-
                printf("[%d] %d\n", job->jobId,
                           newJob.progs[newJob.numProgs - 1].pid);
        } else {
                jobList->fg = job;
 
                /* move the new process group into the foreground */
-
                if (tcsetpgrp(0, newJob.pgrp))
                        perror("tcsetpgrp");
        }
@@ -935,29 +948,24 @@ int shell_main(int argc, char **argv)
        /* initialize the cwd */
        getcwd(cwd, sizeof(cwd));
 
+#ifdef BB_FEATURE_SH_COMMAND_EDITING
+       signal(SIGWINCH, win_changed);
+       win_changed(0);
+#endif
 
        //if (argv[0] && argv[0][0] == '-') {
        //      shell_source("/etc/profile");
        //}
 
        if (argc < 2) {
-               fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER,
-                               BB_BT);
-               fprintf(stdout,
-                               "Enter 'help' for a list of built-in commands.\n\n");
+               fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT);
+               fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n");
        } else {
                input = fopen(argv[1], "r");
-               if (!input)
+               if (!input) {
                        fatalError("A: Couldn't open file '%s': %s\n", argv[1],
                                           strerror(errno));
-//              else
-//                      fatalError("Got it.\n");
-               //exit(shell_source(argv[1]));
-
-               /* Set terminal IO to canonical mode, and save old term settings. */
-#ifdef BB_FEATURE_SH_COMMAND_EDITING
-               cmdedit_init();
-#endif
+               }
        }
 
        return (busy_loop(input));