Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <signal.h>
- #include "proto.h"
- /* Constants */
- #define LINELEN 1024
- #define EXPANDEDLEN 200000
- /* Globals */
- volatile sig_atomic_t sig_int = 0;
- int last_exit_status = 0;
- int m_argc;
- int m_shifted_argc;
- char **m_argv;
- char **m_shifted_argv;
- pid_t cpid = 1;
- /* Prototypes */
- void processline (char *line, int inFD, int outFD, plFlags FLAGS);
- int findUnquotChar(char *line, char);
- void print_args(char **args);
- /* SIG HANDLERS */
- void sigint_handler(int sig)
- {
- sig_int = 1;
- if(!cpid) kill (0, SIGINT);
- }
- /* Shell main */
- int main (int mainargc, char **mainargv)
- {
- /* PROTOTYPES IN MAIN */
- void sigint_handler(int sig);
- /* Defining Locals */
- struct sigaction sa;
- plFlags pl_flags = {1U, 1U, 1U};
- char *promptStr = "%% ";
- char buffer [LINELEN];
- int len;
- FILE *fp;
- cpid = getpid();
- sa.sa_handler = sigint_handler;
- //sa.sa_flags = 0;
- sa.sa_flags = SA_RESTART;
- sigemptyset(&sa.sa_mask);
- if(sigaction(SIGINT, &sa, NULL) == -1)
- {
- perror("sigaction");
- exit(1);
- }
- m_argc = mainargc;
- m_shifted_argc = mainargc;
- m_argv = mainargv;
- m_shifted_argv = mainargv;
- if(m_argc > 1) fp = fopen(m_argv[1], "r");
- else fp = stdin;
- if(fp == NULL)
- {
- fprintf(stderr, "./msh: Can not open file %s\n", m_argv[1]);
- exit(127);
- }
- while(1)
- {
- if(fp == stdin)
- {
- if(!(promptStr = getenv("P1"))) fprintf(stderr, "%% ");
- else fprintf(stderr, "%s", promptStr);
- }
- if(fgets(buffer, LINELEN, fp) != buffer) break;
- len = strlen(buffer);
- if (buffer[len-1] == '\n') buffer[len-1] = 0;
- processline (buffer, 0, 1, pl_flags); //, mainargc, mainargv, shiftedMArgV);
- }
- if (!feof(fp)) perror ("read");
- if(fclose(fp) != 0) perror("close");
- return 0; /* Also known as exit (0); */
- }
- void processline (char *line, int inFD, int outFD, plFlags FLAGS) //, int mainargc, char **mainargv, char **shiftedMArgV)
- {
- int status;
- char expanded_line[EXPANDEDLEN];
- char **args;
- char *line_itr = line;
- int command_count;
- int pix;
- int fd[4];
- int fdFlipF = 1;
- int line_len = strlen(line);
- plFlags pl_flags = {0U, 0U, 0U};
- if(FLAGS.EXPAND)
- {
- if(expand(line, expanded_line, EXPANDEDLEN)) return; //, mainargc, mainargv, shiftedMArgV)) return;
- line_itr = expanded_line;
- line_len = strlen(expanded_line);
- }
- if((pix = findUnquotChar(line_itr, '|')))
- {
- line_itr[pix++] = 0;
- if(pipe (fd) < 0) perror("pipe");
- processline(line_itr, inFD, fd[1], pl_flags);
- close(fd[1]);
- line_itr = &(line_itr[pix]);
- while((pix = findUnquotChar(line_itr, '|')) && pix < line_len)
- {
- if(pix != 0) line_itr[pix++] = 0;
- if(fdFlipF)
- {
- if(pipe(&(fd[2])) < 0) perror("pipe");
- if(!pix) fd[3] = 1;
- processline(line_itr, fd[0], fd[3], pl_flags);
- close(fd[3]);
- close(fd[0]);
- }
- else
- {
- if(pipe(fd) < 0) perror("pipe");
- if(!pix) fd[1] = 1;
- processline(line_itr, fd[2], fd[1], pl_flags);
- close(fd[1]);
- close(fd[2]);
- }
- line_itr = &(line_itr[pix]);
- fdFlipF = !fdFlipF;
- }
- return;
- }
- command_count = arg_parse(expanded_line, &args);
- /* print_args(args); */
- if(!command_count) return;
- if(!exec_if_built_in(args, outFD))
- {
- /* Start a new process to do the job. */
- cpid = fork();
- if (cpid < 0) {
- perror ("fork");
- free(args);
- return;
- }
- /* Check for who we are! */
- if (cpid == 0) {
- /* We are the child! */
- /* Turning stdout into pipe output if needed */
- if((dup2(outFD, 1)) < 0)
- {
- perror("dup");
- return;
- }
- execvp(args[0], args);
- perror ("exec");
- exit (127);
- last_exit_status = 127;
- }
- /* Have the parent wait for child to complete */
- if(FLAGS.WAIT)
- {
- if(wait(&status) < 0) perror("wait");
- last_exit_status = ((WEXITSTATUS(status) || !WIFEXITED(status)) ? 127 : 0);
- }
- }
- free(args);
- }
- /* Function that returns a pointer to the
- first instance of ch in line */
- int findUnquotChar(char *line, char ch)
- {
- int i = 0;
- int inQuoteFlag = 0;
- while(line[i])
- {
- if(line[i] == ch && !inQuoteFlag) return i;
- else if(line[i] == '"') inQuoteFlag = !inQuoteFlag;
- i++;
- }
- return 0;
- }
- /* Procedure for testing purposes,
- prints the arguments of a command array */
- void print_args(char **args)
- {
- int i = 0;
- char** cmd = args;
- printf("COMMANDS: ");
- while(cmd[i] != 0)
- {
- printf("'%s'\n", cmd[i]);
- i++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement