{"id":23092369,"url":"https://github.com/chungquantin/baby-shell","last_synced_at":"2025-04-03T18:28:00.895Z","repository":{"id":194958614,"uuid":"610297762","full_name":"chungquantin/baby-shell","owner":"chungquantin","description":"Baby shell implementation in C supports most OS commands with pipelining and sequencing","archived":false,"fork":false,"pushed_at":"2023-09-15T08:35:34.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-01T17:18:21.170Z","etag":null,"topics":["c","libc","os","shell"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chungquantin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-03-06T13:49:24.000Z","updated_at":"2024-07-02T21:31:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"3f25dc98-5eb5-4b7f-b777-b4a31ae67b16","html_url":"https://github.com/chungquantin/baby-shell","commit_stats":null,"previous_names":["nomadiz/baby-shell","lowlevelers/baby-shell","chungquantin/baby-shell"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chungquantin%2Fbaby-shell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chungquantin%2Fbaby-shell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chungquantin%2Fbaby-shell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chungquantin%2Fbaby-shell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chungquantin","download_url":"https://codeload.github.com/chungquantin/baby-shell/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247055247,"owners_count":20876157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","libc","os","shell"],"created_at":"2024-12-16T21:32:18.951Z","updated_at":"2025-04-03T18:28:00.876Z","avatar_url":"https://github.com/chungquantin.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 👶 Baby Shell\n\n\u003cimg width=\"100%\" alt=\"Screen Shot 2023-03-06 at 10 03 25\" src=\"https://user-images.githubusercontent.com/56880684/223148385-1b600f1f-3848-43b1-ab3d-e9fa38478dba.png\"\u003e\n\n## Description\n\nReimplement the OS Shell in C using libc API. This shell covers most of the features of a shell.\n\n## How does shell works?\n\nShell is simply an interface layer that uses `libc` API to operate a specific command. Terminal and shell is a two different concepts. Terminal is an application that uses a shell as a backend for the user to communicate with the kernel.\n\n### Tokenizer\n\nFirst stage of a shell is to tokenize the command into tokens that will be processed in a later stage for command execution. You can consider a tokenizer is a preprocessing stage before moving it to the main cook. There is a rule for tokenizing:\n\n- Special tokens: `,`, `;`, `|`\n- String: `\"Hello world\"`\n\n### Command execution\n\nTo understand how a shell executes commands, you must have a fundamental knowledge of the process in the operating system. Whenever a process uses a system call like `execve` or `execvp` to execute the binary command, the process exits with the success code `exit(0)` or error code `exit(1)`. Hence, a single process shell is unable to run interactively while waiting for user input.\n\n#### Forking process\n\nThe solution is simple, for every command, we will fork another process which copies the memory of that process to the another. In this way, the context of the old process is the same while the main program is no longer interrupted after the execution.\n\n```\nNAME\n     fork -- create a new process\n\nSYNOPSIS\n     #include \u003cunistd.h\u003e\n\n     pid_t\n     fork(void);\n\nDESCRIPTION\n     fork() causes the creation of a new process...\n```\n\nRead from `man fork`\n\n### Pipe\n\nThe pipelining command is a special thing in every shell, it makes use of OS file descriptors to stream the output from `stdout` to a file descriptor or to read the input from file descriptor instead of `stdin`. Implementing a pipe requires an API called `pipe()`.\n\n```\nNAME\n       pipe - Postfix delivery to external command\n\nSYNOPSIS\n       pipe [generic Postfix daemon options] command_attributes...\n\nDESCRIPTION\n       The pipe(8) daemon processes requests from the Postfix queue ma\nnager to...\n```\n\nRead from `man pipe`\n\n## Features\n\n- [x] Execute single command\n- [x] Sequencing and execute multiple commands (Token: `;`)\n- [x] Input / Output redirection (Token: `\u003e`, `\u003c`)\n- [x] Pipelining command (Token: `|`)\n- [x] Executing scripts (Token: `source`)\n- [ ] Auto code completion\n- [ ] SSH remote connection\n- [ ] Custom config file similar to `.zshrc` or `.bashrc`\n\n## Development Guideline\n\nThe [Makefile](Makefile) contains the following targets:\n\n- `make all` - compile everything\n- `make tokenize` - compile the tokenizer demo\n- `make tokenize-tests` - compile the tokenizer demo\n- `make shell` - compile the shell\n- `make shell-tests` - run a few tests against the shell\n- `make test` - compile and run all the tests\n- `make clean` - perform a minimal clean-up of the source tree\n\nThe [examples](examples/) directory contains an example tokenizer. It might help.\n\n## Contributions\n\nCreated by **Micheal Baraty** and **Tin Chung**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchungquantin%2Fbaby-shell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchungquantin%2Fbaby-shell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchungquantin%2Fbaby-shell/lists"}