How do we create a shell?

Alright. I hate to be "that guy" but i seriously need some guidance with creating a UNIX shell. I have it as an assignment for my current sem. We are expected to use C and the shell should be capable of executing around 20 basic commands that we use in UNIX.

I am seriously lost with this. I've read a tutorial about using system calls such as execve, fork, wait, etc. but that was implemented using java. Since i don't really know java, that didn't help with anything other than giving me a vague idea about what i have to do.

Can anyone help? Any kind of C specific tutorial would be highly appreciated. If there's anyone who could point me in the right direction and suggest some reading which might directly or indirectly help me in my efforts, then that would be great too.

Thanks a lot...

Since you haven't asked for a solution, but where to find information I will allow this question.

A good starting point would be the first two C/C++ tutorials here: http://www.unix.com/answers-to-frequently-asked-questions/13774-unix-tutorials-programming-tutorials-shell-scripting-tutorials.html

You need to write a program, in C, which has to operate as a simplified UNIX shell. You're expected to implement "around 20 basic commands" --- which which I suspect you mean there are about twenty syntactic features.

So let's ask the following question: What is the absolute minimum functionality that a UNIX shell can have?

If you answered something like: read lines of input, parse them into commands and arguments, and then execute those commands with those arguments ... you win!

I hope your C programming skills are up to the simplest portion of this task. You better know how safely read one line of input after another ... and detect an EOF (end-of-file) condition. So that sounds like an outer while loop to me. It's can be terminated by EOF and, if you implement an exit built-in command then that can break out of the loop as well.

Parsing these input lines is, of course, the most complex part of the task at hand. In the simplest case you could seek for the first bit of whitespace (perhaps using the strtok() function -- but note the caveats in the man pages for that). From there you could determine if it's a simple command or if it includes a path. In the first case you'd scan a list of your own built-ins then search the PATH for an executable instance of such a command, in the latter you'd just try to build and execve() with that as the "argv[0]." Then you parse the rest of the command to create the rest of your execve() argument vector (list of NUL terminated strings).

That's the very basics. Then you get into a myriad details about handling globbing (expansion of wildcard patterns into lists of matching filenames) and handling the "plumbing" (pipes and redirection). You'd have to ask your instructor about whether things like the popen(), and system(), or even the execl*() library functions are allowed in your program. The problem with all of those is that they implicitly involve the use of a subshell --- which does its own parsing and plumbing for you. You won't learn much about the implementation of a shell in C if you use C wrapper functions that call on some other shell under the hood.

Normally you'd start by defining your own parser (using lex or its GNU version flex. You feed one of these a of regular expressions and rules about what each sort of token can look like) and it spits out a set of C programs which implements a lexical scanner. Then you'd use that to implement a grammar which is a set of rules about which functions are invoked to handle each sort of token.

If your teacher hasn't discussed lexical scanners and parser generators and hasn't assigned reading material that covers them; then I have to assume either that you're supposed to make a very simple program, devising your own parsing; or that he or she is incompetent.

You might find something like:

The LEX & YACC Page

or:

COMP3610: Principles of Programming Languages - Flex & Bison

To be handy overviews and tutorials on using these sorts of tools.

JimD (former Linux Gazette AnswerGuy)

1 Like

Thanks a lot. Looks like i have everything in place to get started....