오픈소스 minishell 약간 수정해서 컴파일 했지만....
글쓴이: janues / 작성시간: 수, 2013/01/16 - 7:52오후
AIX 5.3 버전에서 cc -o test test.c 로 컴파일 했습니다.
컴파일 후 test를 실행시키고 ls, ls -al, ls -al .. 명령어를 입력했을때
아래처럼 화면에 출력이 전혀 나오지 않습니다.
/test/source ]@ ls
/test/source ]@ ls -al
/test/source ]@ ls -al ..
/test/source ]@ cd ..
/test/source ]@
뭐가 문제일까요? input은 되는것 같은데 output 이 안되는 건지....
#include <stdio.h>
#include <definitions.h>
#include <utilities.h>
#include <sys/wait.h>
#define MAXLINE 4096
void handleUserCommand()
{
/* if( chk_pipe()==1) */
/* { */
if (checkBuiltInCommands() == 0) {
/* printf("%d",commandArgc); */
launchJob(commandArgv, "STANDARD", 0, FOREGROUND);
/* puts(commandArgv[1]); */
}
}
/* int check_pipe() */
/*{ */
void pipelining(int flag)
{
char line[MAXLINE];
FILE *fpin, *fpout;
char arg1[50],arg2[50];
int i;
if(flag==2)
{
strcat(arg1,commandArgv[0]);
strcpy(arg2,commandArgv[2]);
}
else
{
for(i=0;i<flag-1;i++)
{
strcat(arg1,commandArgv[i]);
strcat(arg1," ");
}
strcpy(arg2,commandArgv[commandArgc-1]);
}
puts(commandArgv[0]);
puts(arg1);
puts(arg2);
if ((fpin = popen(arg1, "rw")) == NULL)
printf("can't open %s", arg1);
if ((fpout = popen(arg2, "rw")) == NULL)
printf("popen error");
while (fgets(line, MAXLINE, fpin) != NULL) {
if (fputs(line, fpout) == EOF)
printf("fputs error to pipe");
}
if (ferror(fpin))
printf("fgets error");
if (pclose(fpout) == -1)
printf("pclose error\n");
/* return 1; */
}
int checkBuiltInCommands()
{
if (strcmp("exit", commandArgv[0]) == 0) {
exit(EXIT_SUCCESS);
}
if (strcmp("cd", commandArgv[0]) == 0) {
changeDirectory();
return 1;
}
/* if (strcmp("in", commandArgv[0]) == 0) {
launchJob(commandArgv + 2, *(commandArgv + 1), STDIN, FOREGROUND);
return 1;
}
if (strcmp("out", commandArgv[0]) == 0) {
launchJob(commandArgv + 2, *(commandArgv + 1), STDOUT, FOREGROUND);
return 1;
}*/
if (strcmp("bg", commandArgv[0]) == 0) {
if (commandArgv[1] == NULL)
return 0;
if (strcmp("in", commandArgv[1]) == 0)
launchJob(commandArgv + 3, *(commandArgv + 2), STDIN, BACKGROUND);
else if (strcmp("out", commandArgv[1]) == 0)
launchJob(commandArgv + 3, *(commandArgv + 2), STDOUT, BACKGROUND);
else
launchJob(commandArgv + 1, "STANDARD", 0, BACKGROUND);
return 1;
}
if (strcmp("fg", commandArgv[0]) == 0) {
if (commandArgv[1] == NULL)
return 0;
int jobId = (int) atoi(commandArgv[1]);
t_job* job = getJob(jobId, BY_JOB_ID);
if (job == NULL)
return 0;
if (job->status == SUSPENDED || job->status == WAITING_INPUT)
putJobForeground(job, TRUE);
else /* status = BACKGROUND */
putJobForeground(job, FALSE);
return 1;
}
if (strcmp("jobs", commandArgv[0]) == 0) {
printJobs();
return 1;
}
if (strcmp("kill", commandArgv[0]) == 0)
{
if (commandArgv[1] == NULL)
return 0;
killJob(atoi(commandArgv[1]));
return 1;
}
/* //int i;
////for( i=0;i<=commandArgc;i++)
//{
//char *str;
//strcpy(str,commandArgv); */
if( commandArgc == 3 )
{
if(strcmp("|", commandArgv[1]) == 0 )
{
pipelining(2);
return 1;
}
/* return 1; */
}
if( commandArgc > 3 )
{
if(strcmp("|", commandArgv[commandArgc-2]) == 0 )
{
/* //puts("works");
//printf("%d",commandArgc); */
pipelining(commandArgc-1);
return 1;
}
}
/* //break;
} */
/* return 0; */
}
void executeCommand(char *command[], char *file, int newDescriptor,
int executionMode)
{
int commandDescriptor;
if (newDescriptor == STDIN) {
commandDescriptor = open(file, O_RDONLY, 0600);
dup2(commandDescriptor, STDIN_FILENO);
close(commandDescriptor);
}
if (newDescriptor == STDOUT) {
commandDescriptor = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0600);
dup2(commandDescriptor, STDOUT_FILENO);
close(commandDescriptor);
}
if (execvp(*command, command) == -1)
perror("MSH");
}
void launchJob(char *command[], char *file, int newDescriptor,
int executionMode)
{
pid_t pid;
pid = fork();
switch (pid) {
case -1:
perror("MSH");
exit(EXIT_FAILURE);
break;
case 0:
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGCHLD, &signalHandler_child);
signal(SIGTTIN, SIG_DFL);
usleep(20000);
setpgrp();
if (executionMode == FOREGROUND)
tcsetpgrp(MSH_TERMINAL, getpid());
if (executionMode == BACKGROUND)
printf("[%d] %d\n", ++numActiveJobs, (int) getpid());
executeCommand(command, file, newDescriptor, executionMode);
exit(EXIT_SUCCESS);
break;
default:
setpgid(pid, pid);
jobsList = insertJob(pid, pid, *(command), file, (int) executionMode);
t_job* job = getJob(pid, BY_PROCESS_ID);
if (executionMode == FOREGROUND) {
putJobForeground(job, FALSE);
}
if (executionMode == BACKGROUND)
putJobBackground(job, FALSE);
break;
}
}
void putJobForeground(t_job* job, int continueJob)
{
job->status = FOREGROUND;
tcsetpgrp(MSH_TERMINAL, job->pgid);
if (continueJob) {
if (kill(-job->pgid, SIGCONT) < 0)
perror("kill (SIGCONT)");
}
waitJob(job);
tcsetpgrp(MSH_TERMINAL, MSH_PGID);
}
void putJobBackground(t_job* job, int continueJob)
{
if (job == NULL)
return;
if (continueJob && job->status != WAITING_INPUT)
job->status = WAITING_INPUT;
if (continueJob)
if (kill(-job->pgid, SIGCONT) < 0)
perror("kill (SIGCONT)");
tcsetpgrp(MSH_TERMINAL, MSH_PGID);
}
void waitJob(t_job* job)
{
int terminationStatus;
while (waitpid(job->pid, &terminationStatus, WNOHANG) == 0) {
if (job->status == SUSPENDED)
return;
}
jobsList = delJob(job);
}
void killJob(int jobId)
{
t_job *job = getJob(jobId, BY_JOB_ID);
kill(job->pid, SIGKILL);
}
void changeDirectory()
{
if (commandArgv[1] == NULL) {
chdir(getenv("HOME"));
} else {
if (chdir(commandArgv[1]) == -1) {
printf(" %s: no such directory\n", commandArgv[1]);
}
}
}
void init()
{
MSH_PID = getpid();
MSH_TERMINAL = STDIN_FILENO;
MSH_IS_INTERACTIVE = isatty(MSH_TERMINAL);
if (MSH_IS_INTERACTIVE) {
while (tcgetpgrp(MSH_TERMINAL) != (MSH_PGID = getpgrp()))
kill(MSH_PID, SIGTTIN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGINT, SIG_IGN);
signal(SIGCHLD, &signalHandler_child);
setpgid(MSH_PID, MSH_PID);
MSH_PGID = getpgrp();
if (MSH_PID != MSH_PGID) {
printf("Error, the shell is not process group leader");
exit(EXIT_FAILURE);
}
if (tcsetpgrp(MSH_TERMINAL, MSH_PGID) == -1)
tcgetattr(MSH_TERMINAL, &MSH_TMODES);
currentDirectory = (char*) calloc(1024, sizeof(char));
} else {
printf("Could not make MSH interactive. Exiting..\n");
exit(EXIT_FAILURE);
}
}
int main(int argc, char **argv, char **envp)
{
init();
welcomeScreen();
shellPrompt();
while (TRUE) {
userInput = getchar();
switch (userInput) {
case '\n':
shellPrompt();
break;
default:
getTextLine();
handleUserCommand();
shellPrompt();
break;
}
}
printf("\n");
return 0;
}
void signalHandler_child(int p)
{
pid_t pid;
int terminationStatus;
pid = waitpid (-1, &terminationStatus, WUNTRACED | WNOHANG);
if (pid > 0) {
t_job* job = getJob(pid, BY_PROCESS_ID);
if (job == NULL)
return;
if (WIFEXITED(terminationStatus)) {
if (job->status == BACKGROUND) {
printf("\n[%d]+ Done\t %s\n", job->id, job->name);
jobsList = delJob(job);
}
} else if (WIFSIGNALED(terminationStatus)) {
printf("\n[%d]+ KILLED\t %s\n", job->id, job->name);
jobsList = delJob(job);
} else if (WIFSTOPPED(terminationStatus)) {
if (job->status == BACKGROUND) {
tcsetpgrp(MSH_TERMINAL, MSH_PGID);
changeJobStatus(pid, WAITING_INPUT);
printf("\n[%d]+ suspended [wants input]\t %s\n",
numActiveJobs, job->name);
} else {
tcsetpgrp(MSH_TERMINAL, job->pgid);
changeJobStatus(pid, SUSPENDED);
printf("\n[%d]+ stopped\t %s\n", numActiveJobs, job->name);
}
return;
} else {
if (job->status == BACKGROUND) {
jobsList = delJob(job);
}
}
tcsetpgrp(MSH_TERMINAL, MSH_PGID);
}
}Forums:


댓글 달기