Skip to content

Commit

Permalink
Clean coded! [0.2.1]
Browse files Browse the repository at this point in the history
  • Loading branch information
ashokgelal committed Sep 19, 2011
1 parent c7584ee commit 580befb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 63 deletions.
1 change: 1 addition & 0 deletions include/common.h
Expand Up @@ -14,5 +14,6 @@
#define FALSE 0

typedef int Boolean;
typedef enum { Foreground, Background, Unknown } CommandType;

#endif /* __COMMON_H_ */
160 changes: 98 additions & 62 deletions src/dash.c
Expand Up @@ -24,6 +24,8 @@
#include "JobControl.h"

char *prompt;
CommandType last_command_type;
char *last_command;
pid_t last_child_pid;
int last_child_status;
ListPtr jobList;
Expand Down Expand Up @@ -53,17 +55,6 @@ void setupPrompt(){
prompt = DEFAULT_PROMPT;
}

void clearScreen(){
if((last_child_pid = fork()) < 0)
fprintf(stderr, "fork error");
else if(last_child_pid == 0){
execlp("clear", "clear", (char *)0);
fprintf(stderr, "couldn't clear the screen");
exit(127);
}
if((last_child_pid=waitpid(last_child_pid, &last_child_status, 0)) <0)
fprintf(stderr, "waitpid error");
}

char *trimwhitespace(char *str)
{
Expand All @@ -85,7 +76,7 @@ char *trimwhitespace(char *str)
return str;
}

char* checkIfBackground(const char *line)
char* isBackgroundTask(const char *line)
{
char *delim = "&";
char *temp = strdup(line);
Expand Down Expand Up @@ -119,81 +110,126 @@ void addJob(pid_t pid, char *command) {
free(jobStr);
}

int handleWait(char *bgTask){
if(bgTask == NULL) {
last_child_pid=waitpid(last_child_pid, &last_child_status, 0);
static Boolean isExitCommand(const char * command) {
return (strcmp(command, "exit") == 0 || strcmp(command, "logout") == 0);
}

if(WIFEXITED(last_child_status) || WIFSIGNALED(last_child_status)) {
return EXIT_SUCCESS;
}
}
// background task
else{
addJob(last_child_pid, bgTask);
return EXIT_SUCCESS;
//last_child_pid = waitpid(last_child_pid, &last_child_status, WNOHANG);
//fprintf(stdout, "status of last job: %d\n", last_child_status);
}
static Boolean isEmptyCommand(const char *command) {
return (command==NULL || command=='\0' || strlen(command)==0);
}

if(last_child_pid == -1) {
fprintf(stderr, "waitpid error\n");
}
static Boolean isJobsCommand(const char *command) {
return (strcmp(command, "jobs")==0);
}

static void quit(int status){
free(jobList);
exit(status);
}

static void runInBackground(char *bgCommand){
char *param[2050];
parseParameters(bgCommand, param);
execvp(param[0], param);
fprintf(stderr, "couldn't run background job: %s\n", bgCommand);
// we are quitting; so free memory
free(bgCommand);
quit(127);
}

return EXIT_FAILURE;
static void runInForeground(char *command){
char *param[2050];
parseParameters(command, param);
// at this point we are sure that it is a foreground command
execvp(param[0], param);
fprintf(stderr, "couldn't run foreground job: %s\n", command);
// we are quitting; so free memory
free(command);
quit(127);
}

static void handleNormalCommand(char *command, char *bgCommand){
if(bgCommand != NULL)
runInBackground(bgCommand);
else {
// if it is background we will never be here
free(bgCommand);
runInForeground(command);
}
}

int handleCommand(const char *line) {
static int handleCommand(const char *line) {
// copy line so that we can modify it such as trimming the whitespaces etc
char *command = malloc(strlen(line));
strcpy(command, line);
command = trimwhitespace(command);
int returnStatus = RETURN_FAIL;
// if no bg task, bgCommand is set to null
char *bgCommand = isBackgroundTask(command);

if(command==NULL || command=='\0' || strlen(command)==0){
if(isEmptyCommand(command)){
jobList = reportCompletedJobs(jobList);
free(command);
return RETURN_SUCCESS;
returnStatus = RETURN_SUCCESS;
goto finally;
}

if(strcmp(trimwhitespace(command), "exit") == 0 || strcmp(trimwhitespace(command), "logout") == 0){
free(command);
return EXIT_SHELL;
if(isExitCommand(command)){
returnStatus = EXIT_SHELL;
goto finally;
}

if(strcmp(trimwhitespace(command), "jobs")==0) {
if(isJobsCommand(command)){
jobList = reportAllJobs(jobList);
free(command);
return RETURN_SUCCESS;
returnStatus = RETURN_SUCCESS;
goto finally;
}

char *bgTask = checkIfBackground(command);

if((last_child_pid = fork()) < 0)
if((last_child_pid = fork()) < 0){
fprintf(stderr, "fork error\n");
else if(last_child_pid == 0){
char *param[2050];
if(bgTask !=NULL) {
parseParameters(bgTask, param);
}
else{
parseParameters(command, param);
}
returnStatus = RETURN_FAIL;
goto finally;
}

if(param[0]==NULL || strlen(param[0])==0) {
free(command);
return RETURN_SUCCESS;
}
else if(last_child_pid == 0) {
handleNormalCommand(command, bgCommand);
// we should never be at this point
returnStatus = RETURN_FAIL;
goto finally;
}

execvp(param[0], param);
fprintf(stderr, "couldn't run %s\n", command);
exit(127);
if(bgCommand == NULL) {
last_child_pid=waitpid(last_child_pid, &last_child_status, 0);
if(last_child_pid == -1) {
fprintf(stderr, "waitpid error\n");
returnStatus = RETURN_FAIL;
goto finally;
}
if(WIFEXITED(last_child_status) || WIFSIGNALED(last_child_status)) {
returnStatus = RETURN_SUCCESS;
goto finally;
}
}else {
//fprintf(stderr, "adding job%s\n", command);
addJob(last_child_pid, bgCommand);
returnStatus = RETURN_SUCCESS;
goto finally;
}
free(command);
return handleWait(bgTask);

finally:
free(command);
free(bgCommand);
return returnStatus;
}


void setupJobList(){
jobList = createList(getKey, toString, freeJob);
}

void clearScreen(){
handleCommand("clear");
}

int main(int argc, char *argv[]) {
char *line;
verifyShowVersionCommand(&argc, argv);
Expand All @@ -210,6 +246,6 @@ int main(int argc, char *argv[]) {
free(line);
}
clearScreen();
freeList(jobList);
exit(EXIT_SUCCESS);
quit(EXIT_SUCCESS);
return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion src/version.c
@@ -1,6 +1,6 @@
#include "version.h"

const char* getVersion(){
const char* VERSION="0.2.0";
const char* VERSION="0.2.1";
return VERSION;
}

0 comments on commit 580befb

Please sign in to comment.