summaryrefslogtreecommitdiff
path: root/project.md
blob: d8561cf4fd80d46c447f403e42b2e9292e2cd4a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
### **Building a Unix Shell from Scratch**
When building a Unix shell from scratch, you typically create a **command-line interface (CLI)** that can:
1. Read user input (commands).
2. Parse the input (tokenizing, handling arguments, redirections, etc.).
3. Execute the command, either internally (built-in commands) or by forking a new process.
4. Handle job control (foreground/background processes, signals).
5. (Optionally) Support scripting, aliases, and advanced features like pipes (`|`) and I/O redirection (`>`, `<`).

A minimal shell implementation in C follows these steps:

#### **1. Read User Input**
Use `getline()` or `read()` to accept input from the terminal.

#### **2. Tokenize Input*
convert into **tokens** (command and arguments) using `strtok()`.

#### **3. Execute the Command**
- If it is a built-in command (e.g., `cd`, `exit`), handle it directly.
- Otherwise, use `fork()` to create a child process and `execvp()` to execute the command.

#### **4. Handle Process Management**
- Use `waitpid()` to wait for processes.
- Implement background jobs (`&` support).
- Signal handling (`SIGCHLD`, `SIGINT` for `Ctrl+C`).

#### **5. Optional Features**
- Environment variables handling (`export`, `$PATH` resolution).
- File redirections (`>` for output, `<` for input).
- Pipe (`|`) execution with `pipe()`, `dup2()`, and multiple processes.

Here’s a **basic Unix shell** in C:

```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define MAX_CMD_LEN 1024
#define MAX_ARGS 64

void execute_command(char **args) {
    pid_t pid = fork();
    if (pid == 0) { // Child process
        execvp(args[0], args);
        perror("exec failed");
        exit(1);
    } else if (pid > 0) { // Parent process
        wait(NULL);
    } else {
        perror("fork failed");
    }
}

int main() {
    char input[MAX_CMD_LEN];
    char *args[MAX_ARGS];

    while (1) {
        printf("mysh> ");
        if (!fgets(input, MAX_CMD_LEN, stdin)) break;  // Read input

        input[strcspn(input, "\n")] = 0;  // Remove newline

        // Tokenize input
        int i = 0;
        args[i] = strtok(input, " ");
        while (args[i] && i < MAX_ARGS - 1) {
            args[++i] = strtok(NULL, " ");
        }
        args[i] = NULL; // NULL-terminate args

        if (args[0]) {
            if (strcmp(args[0], "exit") == 0) break;  // Built-in exit
            execute_command(args);
        }
    }
    return 0;
}
```

This basic shell:
- Reads input
- Tokenizes it
- Forks a process and executes commands using `execvp()`
- Waits for processes to complete
- Supports built-in `exit` command

A full-featured shell would include:
- **Job control** (`fg`, `bg`, `jobs`)
- **Signal handling** (`SIGTSTP`, `SIGINT`)
- **Redirections** (`>`, `<`)
- **Pipelines** (`|`)
- **Shell scripting** (`if`, `for`, `while` loops)

---

### **Unix Shell vs. POSIX Shell**
A **Unix shell** is a general term for any command interpreter that runs on Unix-like systems (e.g., **bash, zsh, ksh, tcsh, dash**).

A **POSIX shell** is a shell that strictly conforms to the **POSIX standard (IEEE 1003.1-2008)**, ensuring portability across Unix systems. The **POSIX shell specification** is based on **Bourne Shell (sh)** and defines:
- **Syntax** (`if`, `for`, `case`, `while`, etc.)
- **Built-in commands** (`cd`, `echo`, `export`, `set`, etc.)
- **I/O redirection** (`>`, `<`, `2>`, `&>`, etc.)
- **Substitution** (`$()`, `` ` ``, `${VAR}`)

**Key Differences:**
| Feature | Unix Shell (Bash, Zsh, etc.) | POSIX Shell (Dash, sh) |
|---------|-----------------|------------------|
| Scripting Extensions | Yes (arrays, `[[ ]]`, associative arrays, `printf -v`, etc.) | No, follows strict POSIX |
| Performance | Slower due to extra features | Faster (used for system scripts) |
| Compatibility | Linux, BSD, Mac, Solaris | Strictly portable |
| Interactive Features | Yes (command history, autocomplete, themes) | Minimal |

For example:
- **Bash (`bash`)** is a Unix shell but has extra non-POSIX features (`[[` test, associative arrays).
- **Dash (`/bin/dash`)** is a strict POSIX shell, often used for system scripts due to speed.

If you want to write **portable shell scripts**, stick to **POSIX sh** (e.g., `#!/bin/sh` instead of `#!/bin/bash`).

Would you like help adding features like redirections or pipes to the basic shell? 🚀