Fix fg bug

-Erik
This commit is contained in:
Erik Andersen 2000-03-16 08:12:48 +00:00
parent 13456d1fcd
commit 161220c498
3 changed files with 1983 additions and 1965 deletions

22
lash.c
View File

@ -47,7 +47,8 @@
enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE,
REDIRECT_APPEND }; REDIRECT_APPEND
};
struct jobSet { struct jobSet {
struct job *head; /* head of list of running jobs */ struct job *head; /* head of list of running jobs */
@ -121,6 +122,7 @@ static struct builtInCommand bltins[] = {
{"pwd", "Print current directory", "pwd", shell_pwd}, {"pwd", "Print current directory", "pwd", shell_pwd},
{"set", "Set environment variable", "set [VAR=value]", shell_set}, {"set", "Set environment variable", "set [VAR=value]", shell_set},
{"unset", "Unset environment variable", "unset VAR", shell_unset}, {"unset", "Unset environment variable", "unset VAR", shell_unset},
{".", "Source-in and run commands in a file", ". filename", {".", "Source-in and run commands in a file", ". filename",
shell_source}, shell_source},
{"help", "List shell built-in commands", "help", shell_help}, {"help", "List shell built-in commands", "help", shell_help},
@ -128,6 +130,7 @@ static struct builtInCommand bltins[] = {
}; };
static const char shell_usage[] = static const char shell_usage[] =
"sh [FILE]...\n\n" "The BusyBox command interpreter (shell).\n\n"; "sh [FILE]...\n\n" "The BusyBox command interpreter (shell).\n\n";
@ -140,6 +143,7 @@ static char *prompt = "# ";
static int shell_cd(struct job *cmd, struct jobSet *junk) static int shell_cd(struct job *cmd, struct jobSet *junk)
{ {
char *newdir; char *newdir;
if (!cmd->progs[0].argv[1] == 1) if (!cmd->progs[0].argv[1] == 1)
newdir = getenv("HOME"); newdir = getenv("HOME");
else else
@ -169,6 +173,7 @@ static int shell_exit(struct job *cmd, struct jobSet *junk)
{ {
if (!cmd->progs[0].argv[1] == 1) if (!cmd->progs[0].argv[1] == 1)
exit TRUE; exit TRUE;
else else
exit(atoi(cmd->progs[0].argv[1])); exit(atoi(cmd->progs[0].argv[1]));
} }
@ -189,15 +194,16 @@ static int shell_fg_bg(struct job *cmd, struct jobSet *jobList)
fprintf(stderr, "%s: bad argument '%s'\n", fprintf(stderr, "%s: bad argument '%s'\n",
cmd->progs[0].argv[0], cmd->progs[0].argv[1]); cmd->progs[0].argv[0], cmd->progs[0].argv[1]);
return FALSE; return FALSE;
for (job = jobList->head; job; job = job->next) {
if (job->jobId == jobNum) {
break;
}
}
} }
} else { } else {
job = jobList->head; job = jobList->head;
} }
for (job = jobList->head; job; job = job->next)
if (job->jobId == jobNum)
break;
if (!job) { if (!job) {
fprintf(stderr, "%s: unknown job %d\n", fprintf(stderr, "%s: unknown job %d\n",
cmd->progs[0].argv[0], jobNum); cmd->progs[0].argv[0], jobNum);
@ -206,7 +212,6 @@ static int shell_fg_bg(struct job *cmd, struct jobSet *jobList)
if (*cmd->progs[0].argv[0] == 'f') { if (*cmd->progs[0].argv[0] == 'f') {
/* Make this job the foreground job */ /* Make this job the foreground job */
if (tcsetpgrp(0, job->pgrp)) if (tcsetpgrp(0, job->pgrp))
perror("tcsetpgrp"); perror("tcsetpgrp");
jobList->fg = job; jobList->fg = job;
@ -242,6 +247,7 @@ static int shell_jobs(struct job *dummy, struct jobSet *jobList)
{ {
struct job *job; struct job *job;
char *statusString; char *statusString;
for (job = jobList->head; job; job = job->next) { for (job = jobList->head; job; job = job->next) {
if (job->runningProgs == job->stoppedProgs) if (job->runningProgs == job->stoppedProgs)
statusString = "Stopped"; statusString = "Stopped";
@ -848,7 +854,7 @@ static int busy_loop(FILE * input)
int status; int status;
int inBg; int inBg;
command = (char*) calloc(BUFSIZ, sizeof(char)); command = (char *) calloc(BUFSIZ, sizeof(char));
/* don't pay any attention to this signal; it just confuses /* don't pay any attention to this signal; it just confuses
things and isn't really meant for shells anyway */ things and isn't really meant for shells anyway */
@ -913,7 +919,7 @@ static int busy_loop(FILE * input)
} }
} }
} }
free( command); free(command);
return 0; return 0;
} }

22
sh.c
View File

@ -47,7 +47,8 @@
enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE,
REDIRECT_APPEND }; REDIRECT_APPEND
};
struct jobSet { struct jobSet {
struct job *head; /* head of list of running jobs */ struct job *head; /* head of list of running jobs */
@ -121,6 +122,7 @@ static struct builtInCommand bltins[] = {
{"pwd", "Print current directory", "pwd", shell_pwd}, {"pwd", "Print current directory", "pwd", shell_pwd},
{"set", "Set environment variable", "set [VAR=value]", shell_set}, {"set", "Set environment variable", "set [VAR=value]", shell_set},
{"unset", "Unset environment variable", "unset VAR", shell_unset}, {"unset", "Unset environment variable", "unset VAR", shell_unset},
{".", "Source-in and run commands in a file", ". filename", {".", "Source-in and run commands in a file", ". filename",
shell_source}, shell_source},
{"help", "List shell built-in commands", "help", shell_help}, {"help", "List shell built-in commands", "help", shell_help},
@ -128,6 +130,7 @@ static struct builtInCommand bltins[] = {
}; };
static const char shell_usage[] = static const char shell_usage[] =
"sh [FILE]...\n\n" "The BusyBox command interpreter (shell).\n\n"; "sh [FILE]...\n\n" "The BusyBox command interpreter (shell).\n\n";
@ -140,6 +143,7 @@ static char *prompt = "# ";
static int shell_cd(struct job *cmd, struct jobSet *junk) static int shell_cd(struct job *cmd, struct jobSet *junk)
{ {
char *newdir; char *newdir;
if (!cmd->progs[0].argv[1] == 1) if (!cmd->progs[0].argv[1] == 1)
newdir = getenv("HOME"); newdir = getenv("HOME");
else else
@ -169,6 +173,7 @@ static int shell_exit(struct job *cmd, struct jobSet *junk)
{ {
if (!cmd->progs[0].argv[1] == 1) if (!cmd->progs[0].argv[1] == 1)
exit TRUE; exit TRUE;
else else
exit(atoi(cmd->progs[0].argv[1])); exit(atoi(cmd->progs[0].argv[1]));
} }
@ -189,15 +194,16 @@ static int shell_fg_bg(struct job *cmd, struct jobSet *jobList)
fprintf(stderr, "%s: bad argument '%s'\n", fprintf(stderr, "%s: bad argument '%s'\n",
cmd->progs[0].argv[0], cmd->progs[0].argv[1]); cmd->progs[0].argv[0], cmd->progs[0].argv[1]);
return FALSE; return FALSE;
for (job = jobList->head; job; job = job->next) {
if (job->jobId == jobNum) {
break;
}
}
} }
} else { } else {
job = jobList->head; job = jobList->head;
} }
for (job = jobList->head; job; job = job->next)
if (job->jobId == jobNum)
break;
if (!job) { if (!job) {
fprintf(stderr, "%s: unknown job %d\n", fprintf(stderr, "%s: unknown job %d\n",
cmd->progs[0].argv[0], jobNum); cmd->progs[0].argv[0], jobNum);
@ -206,7 +212,6 @@ static int shell_fg_bg(struct job *cmd, struct jobSet *jobList)
if (*cmd->progs[0].argv[0] == 'f') { if (*cmd->progs[0].argv[0] == 'f') {
/* Make this job the foreground job */ /* Make this job the foreground job */
if (tcsetpgrp(0, job->pgrp)) if (tcsetpgrp(0, job->pgrp))
perror("tcsetpgrp"); perror("tcsetpgrp");
jobList->fg = job; jobList->fg = job;
@ -242,6 +247,7 @@ static int shell_jobs(struct job *dummy, struct jobSet *jobList)
{ {
struct job *job; struct job *job;
char *statusString; char *statusString;
for (job = jobList->head; job; job = job->next) { for (job = jobList->head; job; job = job->next) {
if (job->runningProgs == job->stoppedProgs) if (job->runningProgs == job->stoppedProgs)
statusString = "Stopped"; statusString = "Stopped";
@ -848,7 +854,7 @@ static int busy_loop(FILE * input)
int status; int status;
int inBg; int inBg;
command = (char*) calloc(BUFSIZ, sizeof(char)); command = (char *) calloc(BUFSIZ, sizeof(char));
/* don't pay any attention to this signal; it just confuses /* don't pay any attention to this signal; it just confuses
things and isn't really meant for shells anyway */ things and isn't really meant for shells anyway */
@ -913,7 +919,7 @@ static int busy_loop(FILE * input)
} }
} }
} }
free( command); free(command);
return 0; return 0;
} }

View File

@ -47,7 +47,8 @@
enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE,
REDIRECT_APPEND }; REDIRECT_APPEND
};
struct jobSet { struct jobSet {
struct job *head; /* head of list of running jobs */ struct job *head; /* head of list of running jobs */
@ -121,6 +122,7 @@ static struct builtInCommand bltins[] = {
{"pwd", "Print current directory", "pwd", shell_pwd}, {"pwd", "Print current directory", "pwd", shell_pwd},
{"set", "Set environment variable", "set [VAR=value]", shell_set}, {"set", "Set environment variable", "set [VAR=value]", shell_set},
{"unset", "Unset environment variable", "unset VAR", shell_unset}, {"unset", "Unset environment variable", "unset VAR", shell_unset},
{".", "Source-in and run commands in a file", ". filename", {".", "Source-in and run commands in a file", ". filename",
shell_source}, shell_source},
{"help", "List shell built-in commands", "help", shell_help}, {"help", "List shell built-in commands", "help", shell_help},
@ -128,6 +130,7 @@ static struct builtInCommand bltins[] = {
}; };
static const char shell_usage[] = static const char shell_usage[] =
"sh [FILE]...\n\n" "The BusyBox command interpreter (shell).\n\n"; "sh [FILE]...\n\n" "The BusyBox command interpreter (shell).\n\n";
@ -140,6 +143,7 @@ static char *prompt = "# ";
static int shell_cd(struct job *cmd, struct jobSet *junk) static int shell_cd(struct job *cmd, struct jobSet *junk)
{ {
char *newdir; char *newdir;
if (!cmd->progs[0].argv[1] == 1) if (!cmd->progs[0].argv[1] == 1)
newdir = getenv("HOME"); newdir = getenv("HOME");
else else
@ -169,6 +173,7 @@ static int shell_exit(struct job *cmd, struct jobSet *junk)
{ {
if (!cmd->progs[0].argv[1] == 1) if (!cmd->progs[0].argv[1] == 1)
exit TRUE; exit TRUE;
else else
exit(atoi(cmd->progs[0].argv[1])); exit(atoi(cmd->progs[0].argv[1]));
} }
@ -189,15 +194,16 @@ static int shell_fg_bg(struct job *cmd, struct jobSet *jobList)
fprintf(stderr, "%s: bad argument '%s'\n", fprintf(stderr, "%s: bad argument '%s'\n",
cmd->progs[0].argv[0], cmd->progs[0].argv[1]); cmd->progs[0].argv[0], cmd->progs[0].argv[1]);
return FALSE; return FALSE;
for (job = jobList->head; job; job = job->next) {
if (job->jobId == jobNum) {
break;
}
}
} }
} else { } else {
job = jobList->head; job = jobList->head;
} }
for (job = jobList->head; job; job = job->next)
if (job->jobId == jobNum)
break;
if (!job) { if (!job) {
fprintf(stderr, "%s: unknown job %d\n", fprintf(stderr, "%s: unknown job %d\n",
cmd->progs[0].argv[0], jobNum); cmd->progs[0].argv[0], jobNum);
@ -206,7 +212,6 @@ static int shell_fg_bg(struct job *cmd, struct jobSet *jobList)
if (*cmd->progs[0].argv[0] == 'f') { if (*cmd->progs[0].argv[0] == 'f') {
/* Make this job the foreground job */ /* Make this job the foreground job */
if (tcsetpgrp(0, job->pgrp)) if (tcsetpgrp(0, job->pgrp))
perror("tcsetpgrp"); perror("tcsetpgrp");
jobList->fg = job; jobList->fg = job;
@ -242,6 +247,7 @@ static int shell_jobs(struct job *dummy, struct jobSet *jobList)
{ {
struct job *job; struct job *job;
char *statusString; char *statusString;
for (job = jobList->head; job; job = job->next) { for (job = jobList->head; job; job = job->next) {
if (job->runningProgs == job->stoppedProgs) if (job->runningProgs == job->stoppedProgs)
statusString = "Stopped"; statusString = "Stopped";
@ -848,7 +854,7 @@ static int busy_loop(FILE * input)
int status; int status;
int inBg; int inBg;
command = (char*) calloc(BUFSIZ, sizeof(char)); command = (char *) calloc(BUFSIZ, sizeof(char));
/* don't pay any attention to this signal; it just confuses /* don't pay any attention to this signal; it just confuses
things and isn't really meant for shells anyway */ things and isn't really meant for shells anyway */
@ -913,7 +919,7 @@ static int busy_loop(FILE * input)
} }
} }
} }
free( command); free(command);
return 0; return 0;
} }