Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

writing a shell

Name: Anonymous 2009-09-19 22:39

I was hoping for some advice from someone more knowledgeable than myself. I'm messing around and trying to learn how to write a shell. So far, I've successfully used flex to parse input and properly put it into a char**. Then I fork() and run execvp() in the child process and wait() in the parent process. Works fine, except that when I run one command it works, but when I run another, it doesn't. What gives?

>echo itworks
itworks
>ls
>echo itworks
>wtf
>^C


Any advice would be appreciated. I don't understand why this would happen. ps doesn't list and child processes, either.

Name: Anonymous 2009-09-19 22:39

Oh and yes, I have put all the code in a while(1){...} loop.

Name: Anonymous 2009-09-19 22:45

some code might help

Name: Anonymous 2009-09-19 23:01

flex

Now you have two problems.

Name: Anonymous 2009-09-19 23:05

Here's the body of the loop. One may assume that all variables and functions used are defined, of course.


  while(1)
    {
      printf(">");
      argv = getline();

      if(strcmp(argv[0],"exit") == 0) exit(0);
      if(argv[argc] == NULL) continue;

      argc=0;
      while(argv[argc] != NULL)
        {
          if(strcmp(argv[argc],"|") == 0)
            {
              split = argc;
              split_type = PIPE;
            }
          else if(strcmp(argv[argc],">") == 0)
            {
              split = argc;
              split_type = REDIRECT;
            }
          argc++;
       }

      if(split == -1)
        {
          pid = fork();
          if(pid == 0)
            {
              if(execvp(argv[0],argv) == -1)
                handle_error_execvp(errno);
            }
          else if(pid > 0)
            {
              if(wait(&status) == -1)
                handle_error_wait(errno);
            }
          else
            {
              handle_error_fork(errno);
            }
        }
      else
        {
          cmda = copy_indices(argv,0,split-1);
         cmdb = copy_indices(argv,split+1,argc-1);
          if(split_type == PIPE)
            {
              pida = fork();
              if(pida == 0)
                {
                  pipe(fd);
                  pidb = fork();
                  if(pidb == 0)
                    {
                      close(fd[0]);
                      dup2(fd[1], 1);
                      close(fd[1]);
                      if(execvp(cmda[0],cmda) == -1)
                        handle_error_execvp(errno);
                    }
                  else if(pidb > 0)
                    {
                      close(fd[1]);
                      dup2(fd[0],0);
                      close(fd[0]);
                      if(execvp(cmdb[0],cmdb) == -1)
                        handle_error_execvp(errno);
                    }
                  else
                    {
                      handle_error_fork(errno);
                    }
                }
              else if(pida > 0)
                {
                  if(wait(&status) == -1)
                    handle_error_wait(errno);
                }
              else
                {
                  handle_error_fork(errno);
                }
            }
          else if(split_type == REDIRECT)
            {
              pid = fork();
              if(pid == 0)
                {
                  if(freopen(cmdb[0],"w",stdout) == NULL)
                    handle_error_freopen(errno);
                  if(execvp(cmda[0],cmda) == -1)
                    handle_error_execvp(errno);
                }
              else if(pid > 0)
                {
                  if(wait(&status) == -1)
                    handle_error_wait(errno);
                }
              else
                {
                  handle_error_fork(errno);
                }
            }
          else
            {
              printf("This section of the code should never execute. Something strange happened.\n");
              exit(-1);
            }
          free(cmda);
          free(cmdb);
        }
    }

Name: sage 2009-09-19 23:08

ARRRRGGHHHH

I was checking to see whether I copy/pasted correctly and I saw my error. I forgot to reset argc to 0 in the beginning of the loop.

I'm going to go shoot myself.

Name: Anonymous 2009-09-19 23:10

>>5
Your GNU indentation is PIG DISGUSTING.

Name: Anonymous 2009-09-19 23:21

>>6
At the risk of sounding like a smarmy asshole, wouldn't this have been picked up right away if you'd used a debugger?

Name: Anonymous 2009-09-19 23:46

>>7
Sir, please refrain from being negative when speaking about the glorious way of indenting that fellow has used.
I, for one, jizzed in my pants upon seeing such a clean and clear indentation style applied to his code.

Name: Anonymous 2009-09-20 2:20

>>5
Your loop isn't long enough and you've defined too many functions.

Name: Anonymous 2009-09-20 4:31

>>7 thank you. I think it is the best type of indentation and I always use it.

>>10
yes, I considated all the error handling functions into one. Though, I don't understand by what you mean by saying that my loop isn't long enough. If you're referring to the fact that I only handle one pipe per line, I do plan on expanding it to general cases once I see that I have the logic down.

Name: Anonymous 2009-09-20 4:36

K&R WAS HERE, GNU STYLE IS LOSER

Name: Anonymous 2009-09-20 4:42

PYTHON WAS HERE, C IS LOSER

Name: Anonymous 2009-09-20 4:46

C WAS HERE, LAUGHED AT PYTHON BABIES

Name: Anonymous 2009-09-20 4:47

SCHEME WAS HERE, CRIED AT THE STATE OF /PROG/

Name: Anonymous 2009-09-20 5:06

so have you added piping and redirection yet?

Name: Anonymous 2009-09-20 5:11

>>12
That was SARCASM and your loop is TOO DAMN LONG and consider writing MORE FUNCTIONS.

Name: Anonymous 2009-09-20 5:13

MILKRIBS WAS HERE, I CAN INSURE YOU I AM NO ANUS!

Name: Anonymous 2009-09-20 9:23

[o]
  while(1)[o]
    {
      printf(">");
      argv = getline();

      if(strcmp(argv[0],"exit") == 0) exit(0);
      if(argv[argc] == NULL) continue;
[o]

      argc=0;
      while(argv[argc] != NULL)
        {
          if(strcmp(argv[argc],"|") == 0)
            {
              split = argc;
              split_type = PIPE;
            }
          else if(strcmp(argv[argc],">") == 0)
            {
              split = argc;
              split_type = REDIRECT;
            }
          argc++;
       }
[o]

      if(split == -1)[o]
        {[o]
          pid = fork();
          if(pid == 0)[o]

            {
              if(execvp(argv[0],argv) == -1)
                handle_error_execvp(errno);            }
          else if(pid > 0)
            {
              if(wait(&status) == -1)
                handle_error_wait(errno);            }
          else
            {
              handle_error_fork(errno);            }
        }
      else[o]

        {
          cmda = copy_indices(argv,0,split-1);         cmdb = copy_indices(argv,split+1,argc-1);[o]

          if(split_type == PIPE)[o]
            {[o]
              pida = fork();
              if(pida == 0)[o]

                {[o]
                  pipe(fd);
                  pidb = fork();
                  if(pidb == 0)[o]

                    {
                      close(fd[0]);
                      dup2(fd[1], 1);
                      close(fd[1]);
                      if(execvp(cmda[0],cmda) == -1)
                        handle_error_execvp(errno);                    }
                  else if(pidb > 0)
                    {
                      close(fd[1]);
                      dup2(fd[0],0);
                      close(fd[0]);
                      if(execvp(cmdb[0],cmdb) == -1)
                        handle_error_execvp(errno);                    }
                  else
                    {
                      handle_error_fork(errno);                    }
                }
              else if(pida > 0)
                {
                  if(wait(&status) == -1)
                    handle_error_wait(errno);                }
              else
                {
                  handle_error_fork(errno);                }
            }
          else if(split_type == REDIRECT)[o]

            {
              pid = fork();
              if(pid == 0)
                {
                  if(freopen(cmdb[0],"w",stdout) == NULL)
                    handle_error_freopen(errno);                  if(execvp(cmda[0],cmda) == -1)
                    handle_error_execvp(errno);                }
              else if(pid > 0)
                {
                  if(wait(&status) == -1)
                    handle_error_wait(errno);                }
              else
                {
                  handle_error_fork(errno);                }
            }
          else
            {
              printf("This section of the code should never execute. Something strange happened.\n");
              exit(-1);
            }
          free(cmda);
          free(cmdb);
        }
    }

Don't change these.
Name: Email:
Entire Thread Thread List