|
@ -0,0 +1,129 @@ |
|
|
|
|
|
#include <stdio.h> |
|
|
|
|
|
#include <stdlib.h> |
|
|
|
|
|
#include <string.h> |
|
|
|
|
|
#include "fun.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void eval(char *cmdline) |
|
|
|
|
|
{ |
|
|
|
|
|
char *argv[MAXLINE]; |
|
|
|
|
|
char buf[MAXLINE]; |
|
|
|
|
|
int isBg; |
|
|
|
|
|
int pid; |
|
|
|
|
|
|
|
|
|
|
|
strcpy(buf,cmdline); |
|
|
|
|
|
isBg = parseline(cmdline,argv); |
|
|
|
|
|
|
|
|
|
|
|
if(argv[0] == NULL) |
|
|
|
|
|
{ |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(!builtin_cmd(argv)) |
|
|
|
|
|
{ |
|
|
|
|
|
/* |
|
|
|
|
|
if((pid=fork())==0) |
|
|
|
|
|
{ |
|
|
|
|
|
setpgid(0,0); |
|
|
|
|
|
if(execvp(argv[0],argv)<0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("%s:Command not found.\n",argv[0]); |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(!isBg) |
|
|
|
|
|
{ |
|
|
|
|
|
waitfg(pid); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
printf("[%d] (%d) %s",pid2jid(pid),pid,cmdline); |
|
|
|
|
|
} |
|
|
|
|
|
*/ |
|
|
|
|
|
} |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int parseline(const char *cmdline, char **argv) |
|
|
|
|
|
{ |
|
|
|
|
|
static char array[MAXLINE]; |
|
|
|
|
|
char *buf = array; |
|
|
|
|
|
char *delim; |
|
|
|
|
|
int argc; |
|
|
|
|
|
int bg; |
|
|
|
|
|
|
|
|
|
|
|
strcpy(buf, cmdline); |
|
|
|
|
|
buf[strlen(buf)-1] = ' '; |
|
|
|
|
|
while (*buf && (*buf == ' ')) |
|
|
|
|
|
buf++; |
|
|
|
|
|
|
|
|
|
|
|
argc = 0; |
|
|
|
|
|
if (*buf == '\'') |
|
|
|
|
|
{ |
|
|
|
|
|
buf++; |
|
|
|
|
|
delim = strchr(buf, '\''); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
delim = strchr(buf, ' '); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (delim) |
|
|
|
|
|
{ |
|
|
|
|
|
argv[argc++] = buf; |
|
|
|
|
|
*delim = '\0'; |
|
|
|
|
|
buf = delim + 1; |
|
|
|
|
|
while (*buf && (*buf == ' ')) |
|
|
|
|
|
{ |
|
|
|
|
|
buf++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (*buf == '\'') |
|
|
|
|
|
{ |
|
|
|
|
|
buf++; |
|
|
|
|
|
delim = strchr(buf, '\''); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
delim = strchr(buf, ' '); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
argv[argc] = NULL; |
|
|
|
|
|
|
|
|
|
|
|
if (argc == 0) |
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
|
|
|
|
if ((bg = (*argv[argc-1] == '&')) != 0) |
|
|
|
|
|
{ |
|
|
|
|
|
argv[--argc] = NULL; |
|
|
|
|
|
} |
|
|
|
|
|
return bg; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int builtin_cmd(char **argv) |
|
|
|
|
|
{ |
|
|
|
|
|
if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit")) |
|
|
|
|
|
{ |
|
|
|
|
|
exit(0); |
|
|
|
|
|
} |
|
|
|
|
|
if(!strcmp(argv[0],"history")) |
|
|
|
|
|
{ |
|
|
|
|
|
print_his(argv[1]); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
if(!strcmp(argv[0],"jobs")) |
|
|
|
|
|
{ |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
if(!strcmp(argv[0],"bg")||!strcmp(argv[0],"fg")) |
|
|
|
|
|
{ |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
if(!strcmp(argv[0], "&")) |
|
|
|
|
|
{ |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
} |