#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#define MAX_LINE 80
#define BUFFER_SIZE 50
char buffer[BUFFER_SIZE];
char *history[10][10];
int nextPosition=0;
int CommandLenth[10]={0};
void ProcessRCommand(char *args[]);
int success[10]={1,1,1,1,1,1,1,1,1,1};
int flag=1;
FILE *fp;
void setup(char inputBuffer[], char *args[],int *background)
{
int length,i,start, ct;
ct = 0;
length = read(STDIN_FILENO, inputBuffer, MAX_LINE);
start = -1;
if (length == 0)
exit(0);
if (length < 0){
perror("error reading the command");
exit(-1);
}
for (i = 0; i < length; i++) {
switch (inputBuffer[i]){
case ' ':
case '\t' :
if(start != -1){
args[ct] = &inputBuffer[start];
ct++;
}
inputBuffer[i] = '\0';
start = -1;
break;
case '\n':
if (start != -1){
args[ct] = &inputBuffer[start];
ct++;
}
inputBuffer[i] = '\0';
args[ct] = NULL;
break;
case '&':
*background = 1;
inputBuffer[i] = '\0';
break;
default :
if (start == -1)
start = i;
}
}
args[ct] = NULL;
}
void handle_SIGINT()
{
write(STDOUT_FILENO, buffer, strlen(buffer));
printf("COMMAND HISTORY:\n");
int i = nextPosition;
int j;
int counter=10;
i = nextPosition;
while(counter--) {
printf("# ");
for(j=0; j<CommandLenth[i]; ++j){
printf("%s ",history[i][j]);
}
printf("\n");
i = (i + 1) % 10;
}
printf("COMMAND->");
fflush(0);
return;
}
void ProcessRCommand(char *args[]){
int i, j, count=10;
char *newargs[MAX_LINE/2+1];
for(i=0; i<MAX_LINE/2+1; ++i) {
newargs[i] = (char*)malloc((MAX_LINE/2+1)*sizeof(char));
}
if (flag==1) {
nextPosition = (nextPosition-1)%10;
}
history[nextPosition][0] = '\0';
if (args[1]==NULL){
i = (nextPosition + 9) % 10;
for(j=0; j<CommandLenth[i]; ++j){
strcpy(newargs[j], history[i][j]);
}
newargs[j]=NULL;
if (success[i]==1){
execvp(newargs[0], newargs);
success[i]=0;
printf("Wrong Instruction!\n");
}
fp=fopen("/home/ieee/success.txt","r+");
if (fp == NULL) { printf("File open error.\n"); }
int k=0;
for (k=0;k<10;k++){fprintf(fp,"%d\n",success[k]);}
fclose(fp);
exit(0);
}
else{ /*Command "r x"*/
i = nextPosition;
while(count--){
i = (i + 9) % 10;
if (strncmp(args[1], history[i][0], 1) == 0){
for(j=0; j<CommandLenth[i]; ++j){
strcpy(newargs[j], history[i][j]);
}
newargs[j]=NULL;
if (success[i]==1){
execvp(newargs[0], newargs);
success[i]=0;
printf("Wrong Instruction!\n");
}
/*update*/
fp=fopen("/home/ieee/success.txt","r+");
if (fp == NULL) { printf("File open error."); }
int k=0;
for (k=0;k<10;k++){fprintf(fp,"%d\n",success[k]);}
fclose(fp);
exit(0);
}
}
printf("No such instruction!\n");
}
}
int main(void)
{
char inputBuffer[MAX_LINE];
int background;
char *args[MAX_LINE/2+1];
int count;
int i, j;
/*update*/
fp=fopen("/home/ieee/success.txt","r+");
if (fp == NULL) {printf("File open error.");}
int k=0;
for (k=0;k<10;k++){fprintf(fp,"%d\n",success[k]);}
fclose(fp);
for(i=0; i<10; ++i) {
for(j=0; j<10; ++j) {
history[i][j] = (char*)malloc(80*sizeof(char));
}
}
strcpy(buffer, "Caught Control C\n");
if (signal(SIGINT, handle_SIGINT)==SIG_ERR)
printf("ERROR!\n");
while (1){
background = 0;
printf("COMMAND->");
fflush(0);
setup(inputBuffer, args, &background);
i = 0;
if (args[0] != NULL && strcmp(args[0],"r") != 0){
while(args[i] != NULL){
strcpy(history[nextPosition][i], args[i]);
++i;
}
CommandLenth[nextPosition] = i;
nextPosition = (nextPosition + 1) % 10;
}
if (strcmp(args[0],"r") == 0){
if (args[1]==NULL){ /*"r" command only*/
i = (nextPosition + 9) % 10;
if (success[i]==1){
for(j=0; j<CommandLenth[i]; ++j){
strcpy(history[nextPosition][j], history[i][j]);
}
CommandLenth[nextPosition] = j;
nextPosition = (nextPosition + 1) % 10;
flag =1;
}
if (success[i]==0) {
printf("Wrong Instruction!\n");
flag=0;
}
}
else{ /*"r x" command*/
i = nextPosition;
count = 10;
while(count--){
i = (i + 9) % 10;
if (strncmp(args[1], history[i][0], 1) == 0){
if (success[i]==1) {
for(j=0; j<CommandLenth[i]; ++j){
strcpy(history[nextPosition][j], history[i][j]);
}
CommandLenth[nextPosition] = j;
nextPosition = (nextPosition + 1) % 10;
flag=1;
break;
}
if (success[i]==0) {
printf("Wrong Instruction!\n");
flag=0;
break;
}
}
}
}
}
pid_t pid = fork();
if(pid < 0){
printf("Fork failed.\n");
}
else if(pid == 0){ /*Child Process*/
if(strcmp(args[0],"r") == 0){
ProcessRCommand(args);
exit(0);
}
else{
execvp(args[0],args);
int s = (nextPosition -1) % 10;
success[s]=0;
/*update the success.txt*/
fp=fopen("/home/ieee/success.txt","r+");
if (fp == NULL) {printf("File open error.");}
int k=0;
for (k=0;k<10;k++){fprintf(fp,"%d\n",success[k]);}
fclose(fp);
printf("Wrong Instruction!\n");
exit(0);
}
}
else { /*Parent Process*/
if(background == 0){
wait(NULL);
fp=fopen("/home/ieee/success.txt","r+");
fscanf(fp,"%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n",&success[0],&success[1],&success[2],&success[3],&success[4],&success[5],&success[6],&success[7],&success[8],&success[9]);
fclose(fp);
}
else{
setup(inputBuffer, args, &background); /*Parent process will not wait for the child*/
/*read the success.txt and update the success array*/
fp=fopen("/home/ieee/success.txt","r+");
fscanf(fp,"%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n",&success[0],&success[1],&success[2],&success[3],&success[4],&success[5],&success[6],&success[7],&success[8],&success[9]);
fclose(fp);
}
}
}
}
评论0