Giter Club home page Giter Club logo

c_shell's Introduction

C Shell

This is a simple shell-like command interpreter. It supports basic operations like executing programs (using execvp internally) and changing directories with cd (using chdir). Implementation of additional features is planned.

This shell is not POSIX-compliant and does not have any scripting capabilities. That's mainly because it does not have a complex parser. Instead, single lines are read from stdin and split at delimiters like or tabs. Only a limited set of special characters like | are treated differently.

GNU shell documentation that covers the basic features of bash can be found here. Implementing some of these features might be educational, too.

Roadmap of Future Features

  • implement two-way pipes with | (pipe system call)
  • implement file IO redirection with > (open instead of pipes)
  • implement setenv in child process, i.e. in cmds like VAR=val echo $VAR prints val
  • implement shell variable expansion via getenv (like echo $HOME prints /home/daniel)
  • implement background processes with & (don't wait on child process)

  • implement arbitrary-length pipes
  • implement redirection of fd 2 (stderr like echo 'err' 2> out)
  • implement redirection of fd 2 to fd 1 (like echo 'err' > out 2>&1)
  • implement export builtin which sets a var in the parent
  • implement wildcard expansion (look at glob in glob.h)
  • implement aliasing of commands
  • support a settings file:
    • aliases
    • exported variables
    • $PS1

Some Useful Documentation

Pipes:

int pipe_fds[2];
pipe(pipe_fds);  // puts returns [4, 5] in pipe_fds

To create a pipe ls | wc, we need to:

  1. create a pipe with pipe. Imagine that this creates file descriptors 4 and 5.
  2. fork twice (once for ls and once for wc).
parent: pid 1000, stdin: terminal, stdout, terminal
---- fork twice ----
parent: pid 1000, stdin: terminal, stdout: terminal
child 1: pid 2000, stdin: terminal, stdout: terminal (for `ls`)
child 2: pid 3000, stdin: terminal, stdout: terminal (for `wc`)
  1. In child 1, run dup2(pipe_fds[1], 1) and exec ls
  2. In child 2, run dup2(pipe_fds[0], 0) and exec wc
  3. Close both ends of the pipe in the parent process.
  • Write a fork_and_exec_with_io(cmd* cmd, int stdout_fd, int stdin_fd) that forks and execs the specified command and changes its stdin and stdout as needed

  • Use fork_and_exec_with_io to implement pipes with 2 elements.

License

All code in this repository is MIT-licensed (see license file).

c_shell's People

Contributors

danielschuette avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.