English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Im making a unix shell. I ask the user for input and then I have to parse that input and exectute the commands using fork and execvp, etc...

How would I parse an input such as:
ls -l | grep lib | less

so that I can correctly implement the commands in a pipe, etc...

I want to recognize commands like ";" to exectute commands right in a row and ">" and "<" to support file redirection.

Can someone please help me?

Right now, I have a strtok function collecting all of the tokens between spaces. What do I do next?

Thanks!

2006-09-27 16:14:06 · 4 answers · asked by tom_a_hawk12 4 in Computers & Internet Programming & Design

4 answers

As Tomahawk above mentions, tools like lex(flex) and yacc(bison) are the ideal solution.

However if you don't have time to learn the tools and would like to do the same in C/C++ then try the following.
1. Break up the given command line into individual commands by looking for say ';' and '|', or parenthesis if the commands could be grouped.
2. Now iterate through the list of individual commands to look for '<' or '>' file indirections, and suitably connect the input and output filestream of the command appropriately.

For implementing the same, I would suggest that you go bottom up. First implement all the features for a single command and then you could build on this foundation to handle multiple commands.

HTH.

2006-09-27 16:47:17 · answer #1 · answered by swami060 3 · 0 0

There's no need to generate a grammar for this if you are going to keep things simple. However, It's not a bad idea if you wanted to continue working on the shell and improve it.

A good way to handle it is to spit things up, depending upon the
"precedence" of the tokens, kinda like divide and conquer.

for instance , what does a ; b | c mean?

Does that mean execute "a" then pipe output of "b" to "c"? Then have a procedure that splits those tokens into two groups "a" and
"b"."|","c". send those groups to the next parsing level, which may handle piping "|". this is like a recursive decent parser.

Note... things get complicated if you want actually use strings in your shell
as in

"a 'b | c'"

Which could mean run "a" with the parameter string "b | c". The strtok solution no longer works, you need to instead use regular expressions to parse out tokens. Which is why the Lex and Yacc solution is not bad.

2006-09-27 17:44:32 · answer #2 · answered by Jay 3 · 0 0

The usual approach is to use a lexer and parser generator like LEX and YACC, or more commonly Flex and Bison. While you can tokenize and interpret everything yourself, things get quite complex very quickly (as you've already witnessed). Why not use the tools designer for this?

2006-09-27 16:28:26 · answer #3 · answered by toddos1 3 · 0 0

that's an exectuable for Microsoft Window's. Unix can't rune this, devoid of setting up Wine. or perhaps then, it is not liable to furnish needed effects, being an installer. stable luck.

2016-10-01 10:58:48 · answer #4 · answered by vanderbilt 4 · 0 0

fedest.com, questions and answers