Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
81 lines (68 loc) · 1.77 KB
/
Copy pathmain.c
File metadata and controls
81 lines (68 loc) · 1.77 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
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <locale.h>
#include <readline/readline.h>
#include "rick.h"
#include "result.h"
#include "error.h"
#include "expr.h"
#include "execute.h"
#include "parser.tab.h"
#include "color.h"
#include "memory.h"
#include "file.h"
#include "log.h"
#include "job.h"
#include "variable.h"
#include "io.h"
#include "history.h"
#define INITIAL_BUFFER_SIZE (1 * 1024) // 1 KB initial size
#define MAX_BUFFER_SIZE (100 * 1024 * 1024) // 100 MB limit
#define MAX_COMMAND_LENGTH 1000 // Maximum length for a single command
#define BUFFER_GROWTH_FACTOR 2
extern int yylex_destroy(void);
extern bool do_not_save_history;
volatile sig_atomic_t keep_running = 1;
volatile int last_status = 0;
static IntResult process_command(const string input) {
NTRY(parse_and_execute(input, (int*)&last_status));
return Ok(NULL);
}
int main(void) {
init_rickshell();
log_info("Shell started");
while (keep_running) {
print_job_status();
string input = get_input();
if (string__is_null_or_empty(input)) {
string__free(input);
if (feof(stdin)) {
println(_SLIT0);
println(_SLIT("exit"));
break;
}
continue;
}
if (input.len == 0) {
string__free(input);
continue;
}
println(_SLIT0);
Result result = process_command(input);
if (!do_not_save_history) save_history(input.str);
do_not_save_history = false;
string__free(input);
if (result.is_err)
report_error(result);
yylex_destroy();
}
cleanup_rickshell();
return last_status;
}