LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   write own linux shell in C (https://www.linuxquestions.org/questions/programming-9/write-own-linux-shell-in-c-19733/)

razza 04-29-2002 06:52 AM

write own linux shell in C
 
Hi, Im trying to find good online examples of how to write my own 'shell' program, while I've written the odd basic shell script using the plentiful info on the net, Im finding very little info on writing my own shell, Id prefer info that is C specific since thats what I plan to write it in.


Thanks for your time


razza

Mik 04-29-2002 07:41 AM

Why don't you look at the source code of some of the existing shells. The source code is all freely available.

tyler_durden 05-07-2002 11:55 AM

It depends on how complicated you want to get. If you want to have it just parse commands run them and pipe stuff, there are two things you need to do.

First, parse the command line input in a loop. Then use some of the string functions to parse the input. Here is some info on string functions

Then you need to pipe data, open close files and run exec system calls. The basic idea is, that if you have a command, you first fork a new process (fork()), then execute that processs (exec() or a like syscall). If necessary you overwrite the stdin and stdout with either a pipe or output file. You may want to look at this fuctions/syscalls:

exec()
execv()
execl() (there are a lot more, type man exec)'
pipe()
open()
dup2() -- copies a file descriptor (this allows you to overwrite stdin or out with a pipe or file)
fork()
wait()

you can type man to look these up or do some searches on google. also, may favorite quick and easy c tutorial is
here.

vfs 05-14-2002 02:02 PM

Try the following shells, for source code and ideas:

- pysh (python shell -- givew it a chance)
- ash
- ae
- rc

They're small and you can find them with http://www.google.com

HTH,

vfs.

alpharomeo95 12-10-2011 08:43 AM

You just have to learn about system calls and signal handling

---------- Post added 12-10-11 at 02:43 PM ----------

Have a look at http://www.gr00ve-hack3r.com/tutorials/shell.html

theNbomr 12-10-2011 12:22 PM

The essential purpose of a shell is to allow a user to start processes. Conventionally, this is done by entering the name of the executable filespec, as well as an ordered list of arguments to pass to the process (a commandline). To launch the executable, the commandline is passed to one of the fork() + exec() family of functions. The readline library provides a ready-made method of managing commandline editing and user interaction. These two ingredients alone will allow you to construct a working shell.

Most modern shells provide some programmability. I would submit that this is an optional extension to a shell's core purpose. The language and syntax making up a shell's programmable aspect can be whatever you want. There is nothing to say (except POSIX, perhaps) that one shell's language is correct and others are wrong. To create a programming language, you can use the tools Lex & Yacc (or more commonly in the GNU world, flex & bison). While this should ease the pain of crafting a usable programming language, it will be a significant effort in its own right.

In Linux, an important idiom is the ability to use the standard IO model to pass information between programs efficiently, and to efficiently write programs that use this model. A very useful aspect of a shell is to support the use of this model. While much of the syntax used to effect this support has become widely standardized across many shells (even MS-DOS), there is nothing inherently 'right' about what has become the standard. You may choose to find some other method to support the use of the standard IO model, or you may choose to mimic the existing standards, or you may choose not to support the idiom at all. If you do choose to support it, the underlying machinery will make use of a small family of system calls: pipe()/popen(), dup(), and a few related functions that can be found by visiting the 'See Also' section of the man pages for the respective functions.

All of this is a long-winded way of saying that the functionality of most shells is defined by the underlying collections of system calls. In some sense, the shell simply provides an interactive way for the user to make use of the system calls present in the kernel and embodied in the standard C library. Start by examining the standard C library, locate those functions that are necessary to expose to the user, and design whatever kind of program you feel does a good job of giving the user access to those system calls and functions. Don't necessarily limit yourself to the style of shell we've all become accustomed to.


--- rod.

EDIT: Grrr.... sucked into a ten year old thread again...


All times are GMT -5. The time now is 11:53 PM.