๐Ÿ“ฆ MaxwellKnight / shell

๐Ÿ“„ README.md ยท 157 lines
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157# Shell Implementation

A simple Unix-like shell implementation in C with support for command execution, pipelines, file redirection, and built-in commands.

## Features

- **Command Execution**: Execute standard Unix commands
- **Pipelines**: Support for piping commands using the `|` operator
- **Input/Output Redirection**: Redirect input and output using `<` and `>` operators
- **Built-in Commands**:
  - `cd`: Change directory
  - `exit`: Exit the shell
  - `history`: Display command history
  - `tree`: Display file system tree structure

## Project Structure

- `main.c`: Entry point for the shell program
- `shell.h`: Header file containing data structures and function declarations
- `shell.c`: Implementation of shell functionality
- `colors.h`: Color definitions for terminal output

## Data Structures

### Command Structure

The `Command` structure represents a single command in the shell:

```c
typedef struct Command {
  int argc;                // Number of arguments
  char *name;              // Command name
  char *argv[MAX_ARGS];    // Command arguments
  bool is_out_redirect;    // Flag for output redirection
  bool is_in_redirect;     // Flag for input redirection
  char *in_file_name;      // Input file name
  char *out_file_name;     // Output file name
  struct Command *next;    // Pointer to next command in pipeline
} Command;
```

### History Structure

The `History` structure maintains command history:

```c
typedef struct History {
  char *history[HISTORY_LEN];  // Array of history entries
  int start;                   // Start index
  int count;                   // Count of entries
  int index;                   // Current index
} History;
```

## How It Works

### Command Parsing

1. The shell reads input from the user
2. Input is parsed to identify commands, pipes, and redirections
3. A linked list of `Command` structures is created

### Command Execution

1. For each command in the pipeline:
   - Check if it's a built-in command
   - Set up pipes if necessary
   - Set up redirections if necessary
   - Fork a new process
   - Execute the command in the child process
   - Wait for the command to finish in the parent process

### Built-in Commands

- `cd`: Changes the current working directory
- `exit`: Exits the shell
- `history`: Displays command history from history.txt
- `tree`: Displays a tree visualization of the current directory structure

### File Redirection

- Input redirection (`<`): Redirects input from a file
- Output redirection (`>`): Redirects output to a file

### Piping

Pipes are implemented using the `pipe()` system call and connecting the output of one command to the input of the next command.

## Compilation and Execution

Compile the shell with:

```bash
gcc -o myshell main.c shell.c -std=c99
```

Run the shell:

```bash
./myshell
```

## Command Examples

1. Basic command:
   ```
   ls -la
   ```

2. Pipeline:
   ```
   ls -la | grep ".txt" | wc -l
   ```

3. Redirection:
   ```
   ls -la > output.txt
   cat < input.txt
   ```

4. Change directory:
   ```
   cd /path/to/directory
   ```

5. View directory tree:
   ```
   tree
   ```

6. View command history:
   ```
   history
   ```

7. Exit the shell:
   ```
   exit
   ```

## Limitations

- Limited handling of special characters and quotes
- No support for environment variable expansion
- No job control
- No command aliasing
- Limited error handling

## Future Improvements

- Add support for job control
- Implement command aliasing
- Add tab completion
- Add support for environment variable expansion
- Improve error handling
- Add signal handling