Thread: My shell

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    My shell

    Hello guys!

    I am writing my own shell, and while the basics are complete I came upon a problem.

    At the current moment Im trying to implement multiple Pipes and redirections in my command line. I am unsure of how to handle this, as there could be 2 pipes or 3 pipes, and even redirections along with the pipe. I've been looking on pipe() and etc. and came up with an example test below:

    Code:
    int     main(int argc, char **argv, char **env)
    {
      int   p[2];
      char  *ls[] = {"/bin/ls", "-la", NULL};
      char  *wc[] = {"/usr/bin/wc", NULL};
      char  *less[] = {"/usr/bin/less", NULL};
    
      pipe(p);
      if (fork() == 0)
        {
          close(p[0]);
          dup2(p[1], STDOUT_FILENO);
          execve("/bin/ls", ls, env);
        }
      if (fork() == 0)
        {
          close(p[1]);
          dup2(p[0], STDIN_FILENO);
          dup2(p[1], STDOUT_FILENO);
          close(p[0]);
          execve("/usr/bin/wc", wc, env);
        }
      if (fork() == 0)
        {
          close(p[1]);
          dup2(p[0], STDIN_FILENO);
          execve("/usr/bin/less", less, env);
        }
      close(p[0]);
      close(p[1]);
      wait(NULL);
      wait(NULL);
      wait(NULL);
      return (0);
    }
    This is just an example of 2 pipes and 3 commands. But how could I make it so that it is adaptable at the number of pipes? And what about redirections? All Im looking is for some advice from anyone who has or hasn't experience in this type of thing.

    Thanks guys in advance!

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    When I designed my shell I read in the command and stored the tokens in an array. Then I went through the array to check where the pipes and redirects were. As I went along with the forks() and execs() I could check if a pipe or redirect was following that command. So have an array of all the tokens, and an array to hold the position of the pipes and redirects (maybe an array of a struct with: int position and char type; with type either I(in) O(out) or P(pipe)). I hope some of this made sense and that it helps. Shell algorithms are sort of hard to explain...

    -Dustin
    http://www.theCprogrammer.com

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    This is interesting -- how would I get started on writing my own shell?

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    #define EXIT_SHELL "exit"
    #define CMDBUFSIZ 4096
    char cmdbuf[CMDBUFSIZ];
    extern char *trim(char *);
    int main(int argc, char *argv[]) {
        char *cmd;
        do {
            fgets(cmdbuf, CMDBUFSIZ, stdin);
            cmd = trim(cmdbuf);
            /* Use execve here after parsing the input as your shell implements input */ 
        } while( strcmp(cmd,EXIT_SHELL)!=0 );
    }
    That should get you a bit started...
    Warning: Due to festival activities lasting a week where i come from, my response may be from implementation defined to completely unspecified...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. What shell is more powerful?
    By xddxogm3 in forum Tech Board
    Replies: 10
    Last Post: 07-24-2004, 10:47 PM
  3. System.ini Shell Problems
    By (TNT) in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2001, 01:05 PM