-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
496 lines (431 loc) · 17 KB
/
main.cpp
File metadata and controls
496 lines (431 loc) · 17 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* minishell.cpp
* -----------------------------
* A minimal, educational shell written in C++17.
*
* Author: Ukay Khing Marma Joy
* GitHub: https://github.com/Ukaykhingmarma28
*
* Created: October 2025
* Language: C++17 (POSIX / Linux)
* License: MIT License
*
*/
#include <algorithm>
#include <cerrno>
#include <csignal>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>
#include <termios.h>
#include <limits.h>
#include "minishell_colors.hpp"
#include "minishell_tokenize.hpp"
#include "minishell_expand.hpp"
#include "minishell_jobs.hpp"
#include "minishell_builtins.hpp"
#include "minishell_prompt.hpp"
#ifdef MINISHELL_HAVE_READLINE
# include <readline/readline.h>
# include <readline/history.h>
extern "C" int rl_catch_signals;
#endif
// Version info for --version flag
#define MINISHELL_VERSION "1.0.0"
#define MINISHELL_RELEASE_DATE "October 2025"
struct Redir {
std::string in;
std::string out;
bool append = false;
};
// 🌟 Added: $$ variable expansion
std::string expand_variables(const std::string& input) {
std::string output = input;
size_t pos = 0;
while ((pos = output.find("$$", pos)) != std::string::npos) {
output.replace(pos, 2, std::to_string(getpid()));
pos += std::to_string(getpid()).length();
}
return output;
}
// -----------------------------------------------------------
// Get executable path dynamically
// -----------------------------------------------------------
std::string get_executable_path() {
char path[PATH_MAX];
ssize_t len;
#ifdef __linux__
len = readlink("/proc/self/exe", path, sizeof(path) - 1);
#elif defined(__APPLE__)
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0) {
len = strlen(path);
} else {
len = -1;
}
#elif defined(__FreeBSD__)
len = readlink("/proc/curproc/file", path, sizeof(path) - 1);
#else
// Fallback: try to use argv[0] or default
return "/usr/local/bin/minishell";
#endif
if (len != -1) {
path[len] = '\0';
return std::string(path);
}
// Fallback
return "/usr/local/bin/minishell";
}
// -----------------------------------------------------------
// Print version info
// -----------------------------------------------------------
void print_version() {
std::cout << "MiniShell version " << MINISHELL_VERSION << "\n";
std::cout << "Release Date: " << MINISHELL_RELEASE_DATE << "\n";
std::cout << "Built with C++17 for POSIX systems\n";
std::cout << "Copyright (c) 2025 Ukay Khing Marma Joy\n";
std::cout << "License: MIT\n";
}
// -----------------------------------------------------------
// Print help info
// -----------------------------------------------------------
void print_help(const char* prog_name) {
std::cout << "Usage: " << prog_name << " [OPTIONS]\n\n";
std::cout << "A minimal Shell Like Water.\n\n";
std::cout << "Options:\n";
std::cout << " -c COMMAND Execute COMMAND and exit\n";
std::cout << " --version Display version information\n";
std::cout << " --help Display this help message\n";
std::cout << " -h Display this help message\n\n";
std::cout << "Features:\n";
std::cout << " • Pipelines and I/O redirection (|, <, >, >>)\n";
std::cout << " • Variable expansion ($VAR, ${VAR})\n";
std::cout << " • Command substitution (`cmd` or $(cmd))\n";
std::cout << " • Globbing (*, ?, [...])\n";
std::cout << " • Job control (bg, fg, jobs, &)\n";
std::cout << " • Aliases and built-in commands\n";
std::cout << " • Customizable prompt with Git integration\n\n";
std::cout << "Built-in Commands:\n";
std::cout << " cd, pwd, echo, export, unset, alias, unalias,\n";
std::cout << " source, jobs, fg, bg, exit\n\n";
std::cout << "Config File: ~/.minishellrc\n";
std::cout << "GitHub: https://github.com/Ukaykhingmarma28/minishell\n";
}
// -----------------------------------------------------------
// Global State
// -----------------------------------------------------------
static pid_t g_shell_pgid = -1;
static mshell::JobTable g_jobs;
// -----------------------------------------------------------
// Helpers
// -----------------------------------------------------------
std::vector<std::string> split_pipeline(const std::string& line) {
std::vector<std::string> parts;
std::string cur;
int dq = 0, sq = 0;
for (char c : line) {
if (c == '"' && !sq) dq ^= 1;
else if (c == '\'' && !dq) sq ^= 1;
if (c == '|' && !dq && !sq) {
if (!cur.empty()) { parts.push_back(cur); cur.clear(); }
} else cur.push_back(c);
}
if (!cur.empty()) parts.push_back(cur);
return parts;
}
void parse_redirections(std::vector<std::string>& argv, Redir& r) {
std::vector<std::string> clean;
for (size_t i = 0; i < argv.size(); ++i) {
if (argv[i] == "<" && i + 1 < argv.size()) { r.in = argv[i + 1]; ++i; }
else if (argv[i] == ">" && i + 1 < argv.size()) { r.out = argv[i + 1]; r.append = false; ++i; }
else if (argv[i] == ">>" && i + 1 < argv.size()) { r.out = argv[i + 1]; r.append = true; ++i; }
else clean.push_back(argv[i]);
}
argv.swap(clean);
}
char** vec_to_argv(const std::vector<std::string>& v) {
char** a = new char*[v.size() + 1];
for (size_t i = 0; i < v.size(); ++i)
a[i] = strdup(v[i].c_str());
a[v.size()] = nullptr;
return a;
}
void free_argv(char** a) {
if (!a) return;
for (size_t i = 0; a[i]; ++i) free(a[i]);
delete[] a;
}
// -----------------------------------------------------------
// Execute single command (for -c option)
// -----------------------------------------------------------
int execute_command_string(const std::string& cmdline, mshell::BuiltinEnv& benv) {
std::string line = expand_variables(cmdline);
if (line.empty()) return 0;
bool background = false;
std::string trimmed = line;
while (!trimmed.empty() && isspace((unsigned char)trimmed.back())) trimmed.pop_back();
if (!trimmed.empty() && trimmed.back() == '&') {
background = true;
trimmed.pop_back();
}
auto stages = split_pipeline(trimmed);
std::vector<std::vector<std::string>> commands;
std::vector<Redir> redirs(stages.size());
for (size_t i = 0; i < stages.size(); ++i) {
auto toks = mshell::tokenize(stages[i]);
std::vector<std::string> words; words.reserve(toks.size());
for (auto& t : toks) {
auto scalar = mshell::expand_scalars(t.text);
auto expanded = mshell::glob_expand(scalar);
words.insert(words.end(), expanded.begin(), expanded.end());
}
parse_redirections(words, redirs[i]);
commands.push_back(std::move(words));
}
if (commands.empty()) return 0;
commands.front() = mshell::alias_expand(benv, commands.front());
if (stages.size() == 1 && !background) {
if (mshell::try_autocd(commands[0])) return 0;
int es = 0;
if (mshell::builtin_dispatch(benv, commands[0], es)) return es;
if (!commands[0].empty()) {
const std::string& cmd = commands[0][0];
if (cmd == "exit") exit(0);
}
}
// For -c mode, we don't have full job control, so just run foreground
int n = (int)commands.size();
std::vector<int> fds(std::max(0, (n - 1) * 2));
for (int i = 0; i < n - 1; ++i)
if (pipe(&fds[2 * i]) == -1) { perror("pipe"); return 1; }
pid_t pgid = -1;
for (int i = 0; i < n; ++i) {
pid_t pid = fork();
if (pid < 0) { perror("fork"); return 1; }
if (pid == 0) {
if (i == 0) setpgid(0, 0); else setpgid(0, pgid);
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
if (i > 0) dup2(fds[2 * (i - 1)], STDIN_FILENO);
if (i < n - 1) dup2(fds[2 * i + 1], STDOUT_FILENO);
for (int fd : fds) close(fd);
if (!redirs[i].in.empty()) {
int fd = open(redirs[i].in.c_str(), O_RDONLY);
if (fd == -1 || dup2(fd, STDIN_FILENO) == -1) { perror("redir <"); _exit(1); }
close(fd);
}
if (!redirs[i].out.empty()) {
int fd = open(redirs[i].out.c_str(),
O_WRONLY | O_CREAT | (redirs[i].append ? O_APPEND : O_TRUNC), 0644);
if (fd == -1 || dup2(fd, STDOUT_FILENO) == -1) { perror("redir >"); _exit(1); }
close(fd);
}
if (commands[i].empty()) _exit(0);
char** argv = vec_to_argv(commands[i]);
execvp(argv[0], argv);
std::cerr << "execvp: " << argv[0] << ": " << strerror(errno) << "\n";
free_argv(argv);
_exit(127);
} else {
if (i == 0) { pgid = pid; setpgid(pid, pgid); }
else setpgid(pid, pgid);
}
}
for (int fd : fds) close(fd);
int status = 0;
waitpid(-pgid, &status, 0);
return WIFEXITED(status) ? WEXITSTATUS(status) : 1;
}
// -----------------------------------------------------------
// Run pipeline
// -----------------------------------------------------------
int run_pipeline(std::vector<std::vector<std::string>>& commands,
std::vector<Redir>& redirs,
mshell::JobTable* jt,
bool background)
{
int n = (int)commands.size();
std::vector<int> fds(std::max(0, (n - 1) * 2));
for (int i = 0; i < n - 1; ++i)
if (pipe(&fds[2 * i]) == -1) { perror("pipe"); return 1; }
pid_t pgid = -1;
for (int i = 0; i < n; ++i) {
pid_t pid = fork();
if (pid < 0) { perror("fork"); return 1; }
if (pid == 0) {
// ---- Child ----
if (i == 0) setpgid(0, 0); else setpgid(0, pgid);
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
if (!background) tcsetpgrp(STDIN_FILENO, getpgrp());
if (i > 0) dup2(fds[2 * (i - 1)], STDIN_FILENO);
if (i < n - 1) dup2(fds[2 * i + 1], STDOUT_FILENO);
for (int fd : fds) close(fd);
if (!redirs[i].in.empty()) {
int fd = open(redirs[i].in.c_str(), O_RDONLY);
if (fd == -1 || dup2(fd, STDIN_FILENO) == -1) { perror("redir <"); _exit(1); }
close(fd);
}
if (!redirs[i].out.empty()) {
int fd = open(redirs[i].out.c_str(),
O_WRONLY | O_CREAT | (redirs[i].append ? O_APPEND : O_TRUNC), 0644);
if (fd == -1 || dup2(fd, STDOUT_FILENO) == -1) { perror("redir >"); _exit(1); }
close(fd);
}
if (commands[i].empty()) _exit(0);
char** argv = vec_to_argv(commands[i]);
execvp(argv[0], argv);
std::cerr << "execvp: " << argv[0] << ": " << strerror(errno) << "\n";
free_argv(argv);
_exit(127);
} else {
if (i == 0) { pgid = pid; setpgid(pid, pgid); }
else setpgid(pid, pgid);
}
}
for (int fd : fds) close(fd);
if (background && jt) {
std::ostringstream oss;
for (size_t i = 0; i < commands.size(); ++i) {
for (auto& s : commands[i]) oss << s << ' ';
if (i + 1 < commands.size()) oss << "| ";
}
int id = jt->add(pgid, oss.str());
std::cout << "[" << id << "] " << pgid << "\n";
return 0;
}
// Foreground execution
tcsetpgrp(STDIN_FILENO, pgid);
int status = 0;
waitpid(-pgid, &status, 0);
tcsetpgrp(STDIN_FILENO, g_shell_pgid);
return WIFEXITED(status) ? WEXITSTATUS(status) : 1;
}
static void sigchld_handler(int) { g_jobs.on_sigchld(); }
// -----------------------------------------------------------
// Entry point
// -----------------------------------------------------------
int main(int argc, char* argv[]) {
// Parse command-line arguments
bool exec_mode = false;
std::string exec_command;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--version") {
print_version();
return 0;
} else if (arg == "--help" || arg == "-h") {
print_help(argv[0]);
return 0;
} else if (arg == "-c") {
if (i + 1 < argc) {
exec_mode = true;
exec_command = argv[++i];
} else {
std::cerr << "minishell: -c requires an argument\n";
return 1;
}
} else {
std::cerr << "minishell: unknown option: " << arg << "\n";
std::cerr << "Try 'minishell --help' for more information.\n";
return 1;
}
}
// Set $SHELL to actual executable path (not hardcoded)
std::string shell_path = get_executable_path();
setenv("SHELL", shell_path.c_str(), 1);
// Set MINISHELL_VERSION for detection by tools
setenv("MINISHELL_VERSION", MINISHELL_VERSION, 1);
// -c mode: execute command and exit
if (exec_mode) {
mshell::BuiltinEnv benv;
mshell::load_rc(benv);
return execute_command_string(exec_command, benv);
}
// Interactive mode
setpgid(0, 0);
g_shell_pgid = getpgrp();
tcsetpgrp(STDIN_FILENO, g_shell_pgid);
// 🌟 Ignore background job signals
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGCHLD, sigchld_handler);
#ifdef MINISHELL_HAVE_READLINE
rl_catch_signals = 0;
#endif
mshell::BuiltinEnv benv;
mshell::load_rc(benv);
int last_status = 0;
while (true) {
std::string prompt =
#ifdef MINISHELL_HAVE_READLINE
mshell::build_prompt_readline(last_status);
#else
mshell::build_prompt_plain(last_status);
#endif
std::string line;
#ifdef MINISHELL_HAVE_READLINE
static bool rl_init = false;
if (!rl_init) { using_history(); rl_init = true; }
if (char* buf = readline(prompt.c_str())) {
line = buf;
free(buf);
if (!line.empty()) add_history(line.c_str());
} else { std::cout << "\n"; break; }
#else
std::cout << prompt << std::flush;
if (!std::getline(std::cin, line)) { std::cout << "\n"; break; }
#endif
// 🌟 $$ expansion
line = expand_variables(line);
if (line.empty()) continue;
bool background = false;
std::string trimmed = line;
while (!trimmed.empty() && isspace((unsigned char)trimmed.back())) trimmed.pop_back();
if (!trimmed.empty() && trimmed.back() == '&') {
background = true;
trimmed.pop_back();
}
auto stages = split_pipeline(trimmed);
std::vector<std::vector<std::string>> commands;
std::vector<Redir> redirs(stages.size());
for (size_t i = 0; i < stages.size(); ++i) {
auto toks = mshell::tokenize(stages[i]);
std::vector<std::string> words; words.reserve(toks.size());
for (auto& t : toks) {
auto scalar = mshell::expand_scalars(t.text);
auto expanded = mshell::glob_expand(scalar);
words.insert(words.end(), expanded.begin(), expanded.end());
}
parse_redirections(words, redirs[i]);
commands.push_back(std::move(words));
}
if (commands.empty()) continue;
commands.front() = mshell::alias_expand(benv, commands.front());
if (stages.size() == 1 && !background) {
if (mshell::try_autocd(commands[0])) { last_status = 0; continue; }
int es = 0;
if (mshell::builtin_dispatch(benv, commands[0], es)) { last_status = es; continue; }
if (!commands[0].empty()) {
const std::string& cmd = commands[0][0];
if (cmd == "jobs") { g_jobs.list(); last_status = 0; continue; }
if (cmd == "fg" && commands[0].size() > 1) { last_status = g_jobs.fg(std::stoi(commands[0][1])) ? 0 : 1; continue; }
if (cmd == "bg" && commands[0].size() > 1) { last_status = g_jobs.bg(std::stoi(commands[0][1])) ? 0 : 1; continue; }
if (cmd == "exit") break;
}
}
last_status = run_pipeline(commands, redirs, &g_jobs, background);
}
std::cout << "\n";
return 0;
}