Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 147 lines (123 sloc) 3.181 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include "zacsh.h"
#include "eveccommand.h"
#include "history.h"

int main(int argc, char **argv)
{
int length = 0;
int bg; //background process or not
char newCommand[LINEMAX] = "";
char buffer[LINEMAX] = "";
char * newargv[MAXARGV]; //New command to parse and execute

Signal(SIGCHLD, child_reaper); //SIGCHLD handler
initjobs(); //Initialize job list for processing jobs

while(1)
{
strcpy(newCommand, "");
printf("zacsh$ "); //output prompt
fflush(stdout);

fgets(newCommand, LINEMAX, stdin); //Get the new string

if (feof(stdin)) //If input is EOF, then break from loop
{
fflush(stdout);
break;
}

//copy new command to a buffer to be modified
length = strlen(newCommand);
strncpy(buffer, newCommand, length);
buffer[length] = '\0';

bg = parseLine(buffer, newargv); //Parse the new command

if (!newargv[0]) //empty command, so do nothing
;
else if (newargv[0][0] == '!' && newargv[0][1] != '\0') //history call
{
int i;
char hist_num[LINEMAX] = "";

//Remove ! from first token
for(i = 0; newargv[0][i+1] ; ++i)
hist_num[i] = newargv[0][i+1];

history_exec(atoi(hist_num));
}
else //record command to history, and then run it
{
//copy command to history linked list
history_add(newCommand);

if(!builtin_check(newargv))
eveccommand(newargv, bg);
}
}

printf("\n");

//destroy all allocated memory in history LLL
history_free();

return 0;
}

/* builtin_check
* Check to see if the line contains any builtin commands, and if it does then
* run them, and return true. Otherwise, return false.
*/
int builtin_check(char * newargv[])
{
if (!strcmp(newargv[0], "chdir"))
{
if (chdir(newargv[1]) < 0)
fprintf(stderr, "Failed to run command '%s': %s\n",
newargv[0], strerror(errno));
}
else if (!strcmp(newargv[0], "myshell"))
myshell(newargv);
else if (!strcmp(newargv[0], "history"))
history_list();
else
return 0;

return 1;
}

/* parseLine
* Function will parse a character array into an array of character arrays,
* with each string token of the original array stored in successive index
* of the new array of char arrays.
*/
int parseLine(char * buffer, char ** argv)
{
char * delimiter; //space delimiter
int argc; //number of arguments
int bg; //background process or not

buffer[strlen(buffer)-1] = ' '; //remove trailing newline
while(*buffer && (*buffer == ' '))
++buffer;

//Build the argument list
argc = 0;
while ((delimiter = strchr(buffer, ' ')))
{
argv[argc++] = buffer;
*delimiter = '\0';
buffer = delimiter + 1;
while (*buffer && (*buffer == ' '))
buffer++;
}
argv[argc] = NULL;

if (argc == 0)
return 1;

if ((bg = (*argv[argc-1] == '&')) != 0)
argv[--argc] = NULL;

return bg;
}

/* myshell
* Simple function to call the system call to directly run a command.
*/
void myshell(char ** argv)
{
char temp[LINEMAX]; //will hold the modified command
int j;

strcpy(temp,"");
for (j = 1; argv[j] != '\0'; ++j)
{
strcat(temp,argv[j]);
strcat(temp," ");
}
system(temp);
}
Something went wrong with that request. Please try again.