For one of my classes, I have to create a shell. There is some small restrictions and modifications that make it a little different than a standard shell, but those aren't an issue for me.
For another class last semester, our first assignment was to create a simple shell. So some of that code was helpful for doing this assignment. Mainly, I used code from it to parse a line of input into a command and its arguments.
So entering:
ls -a -l (or ls -al both work ok)
would store "ls" as the first element in an array, followed by each argument. This is a 2D array (char *input[40]) with a hard limit of 40 arguments with the command. And this is a handy way to parse the line of input and store the command name and arguments since to execute the command, I simply call: execvp(input[0], input)
However, in that previous class, there was no mention of pipes, and I never even thought of handling them, so that aspect was never touched. However, for this new assignment, they have to be handled. I believe I have a decent idea how to do that with the pipe() call and such, but my problem now is parsing the line of input into separate commands and their arguments. So a line of input like:
ls -a -l | head -4 | tail -2
screws up my current parsing. Since I am using a 2D array for a single command and its arguments, the next step would be a 3D array for multiple commands and their arguments. But damn that just makes things a lot more complicated for me. Perhaps it is a simple extension of the 2D (i.e. now becomes char **input[40]), but it's hard to visualize and work with it.
So I'm wondering if using a 3D char array to grab the input, separated by command name and its arguments, when pipes are involved, is the way to go.
People have created tons of different shells... is this the way that they handle pipes? I'm trying to come up with a more simple way of parsing the input that contains multiple pipes. Any ideas?
For another class last semester, our first assignment was to create a simple shell. So some of that code was helpful for doing this assignment. Mainly, I used code from it to parse a line of input into a command and its arguments.
So entering:
ls -a -l (or ls -al both work ok)
would store "ls" as the first element in an array, followed by each argument. This is a 2D array (char *input[40]) with a hard limit of 40 arguments with the command. And this is a handy way to parse the line of input and store the command name and arguments since to execute the command, I simply call: execvp(input[0], input)
However, in that previous class, there was no mention of pipes, and I never even thought of handling them, so that aspect was never touched. However, for this new assignment, they have to be handled. I believe I have a decent idea how to do that with the pipe() call and such, but my problem now is parsing the line of input into separate commands and their arguments. So a line of input like:
ls -a -l | head -4 | tail -2
screws up my current parsing. Since I am using a 2D array for a single command and its arguments, the next step would be a 3D array for multiple commands and their arguments. But damn that just makes things a lot more complicated for me. Perhaps it is a simple extension of the 2D (i.e. now becomes char **input[40]), but it's hard to visualize and work with it.
So I'm wondering if using a 3D char array to grab the input, separated by command name and its arguments, when pipes are involved, is the way to go.
People have created tons of different shells... is this the way that they handle pipes? I'm trying to come up with a more simple way of parsing the input that contains multiple pipes. Any ideas?