42sh/README.md

60 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

# 42sh - A POSIX shell with a bad name
2026-01-06 16:34:08 +01:00
2026-04-24 21:17:10 +02:00
42sh is a project aiming to implement a POSIX-compliant shell written in C with only the standard library.
Source de is fully documented with the doxygen format so you can easily understand how the project works by exploring it.
2026-01-06 16:34:08 +01:00
2026-04-24 21:17:10 +02:00
> **Note** This is a school project, therefore it probably won't interest you if you are looking for something useful.
2026-01-06 16:34:08 +01:00
2026-04-24 21:17:10 +02:00
## Getting started
2026-01-06 16:34:08 +01:00
### Build
run this command:
`autoreconf --force --verbose --install`
### Test
run this command:
`./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla'`
then:
`make`
2026-04-24 21:17:10 +02:00
#### Build with ASan
run this command:
`./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address'`
2026-04-24 21:17:10 +02:00
or for MacOS (Jean Here):
`./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib'`
2026-04-24 21:17:10 +02:00
then:
`make check`
2026-01-06 16:34:08 +01:00
2026-04-24 21:17:10 +02:00
## Project status
2026-01-06 16:34:08 +01:00
2026-04-24 21:17:10 +02:00
### Implemented features
2026-01-06 16:34:08 +01:00
2026-04-24 21:17:10 +02:00
* **Command Execution:** `$PATH` search and binary execution (via `fork` and `execvp`) with error return code handling.
* **Built-ins:** Native implementation of `echo`, `cd`, `exit`, `export`, `unset`, `set`, `.`, `true`, `false`, as well as loop management with `break` and `continue`.
* **Control Structures:** * Conditions: `if / then / elif / else / fi`.
* Loops: `while`, `until` and `for`.
* **Logical Operators:** Command chaining with `&&`, `||` and negation with `!`.
* **Pipelines and Redirections:** * Full management of pipes `|` to connect the output of one process to the input of another.
* Single and multiple redirections: `>`, `<`, `>>`, `>&`, `<&`, `<>`.
* **Variables Management:** Assignment, variable expansion, and special variables handling like `$?` (return code of the last command).
* **Command Grouping:** Execution blocks `{ ... }` and subshells creation `( ... )`.
* **Quoting:** Support for weak (`"`) and strong (`'`) quoting for special characters escaping.
## Architecture
The shell operates on a classic compilation/interpretation pipeline:
2026-04-24 21:17:10 +02:00
1. **Lexer (Lexical Analysis):** Reads standard input (or script) character by character and generates a stream of "Tokens" (Words, Operators, Redirections).
2. **Parser (Syntax Analysis):** Syntax analyzer that transforms the token stream into a complex Abstract Syntax Tree (AST). This module strictly manages the nesting of control structures and enforces the rigid grammar of the Shell Command Language.
3. **Execution (AST Traversal):** The tree is traversed recursively. Redirections modify file descriptors (`dup2`), child processes are created (`fork`), and commands are executed.
2026-01-07 17:38:54 +01:00
2026-04-24 21:17:10 +02:00
## Authors
- Guillem George
- Matteo Flebus
- Jean Herail
- William Valenduc