~emersion/mrsh

ref: a0b7bd3214fb52230318ac07e85c4a0cd41fa8a4 mrsh/main.c -rw-r--r-- 3.3 KiB
a0b7bd32Simon Ser gitignore: add /build-* 2 years ago
                                                                                
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
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <mrsh/ast.h>
#include <mrsh/buffer.h>
#include <mrsh/builtin.h>
#include <mrsh/entry.h>
#include <mrsh/parser.h>
#include <mrsh/shell.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "frontend.h"

extern char **environ;

int main(int argc, char *argv[]) {
	struct mrsh_state state = {0};
	mrsh_state_init(&state);

	struct mrsh_init_args init_args = {0};
	if (mrsh_process_args(&state, &init_args, argc, argv) != 0) {
		mrsh_state_finish(&state);
		return 1;
	}

	if (!mrsh_populate_env(&state, environ)) {
		return 1;
	}

	if (!(state.options & MRSH_OPT_NOEXEC)) {
		// If argv[0] begins with `-`, it's a login shell
		if (state.args->argv[0][0] == '-') {
			mrsh_source_profile(&state);
		}
		if (state.interactive) {
			mrsh_source_env(&state);
		}
	}

	int fd = -1;
	struct mrsh_buffer parser_buffer = {0};
	struct mrsh_parser *parser;
	if (state.interactive) {
		interactive_init(&state);
		parser = mrsh_parser_with_buffer(&parser_buffer);
	} else {
		if (init_args.command_str) {
			parser = mrsh_parser_with_data(init_args.command_str,
				strlen(init_args.command_str));
		} else {
			if (init_args.command_file) {
				fd = open(init_args.command_file, O_RDONLY | O_CLOEXEC);
				if (fd < 0) {
					fprintf(stderr, "failed to open %s for reading: %s\n",
						init_args.command_file, strerror(errno));
					return 1;
				}
			} else {
				fd = STDIN_FILENO;
			}

			parser = mrsh_parser_with_fd(fd);
		}
	}
	mrsh_state_set_parser_alias_func(&state, parser);

	struct mrsh_buffer read_buffer = {0};
	while (state.exit == -1) {
		if (state.interactive) {
			char *prompt;
			if (read_buffer.len > 0) {
				prompt = mrsh_get_ps2(&state);
			} else {
				// TODO: next_history_id
				prompt = mrsh_get_ps1(&state, 0);
			}
			char *line = NULL;
			size_t n = interactive_next(&state, &line, prompt);
			free(prompt);
			if (!line) {
				state.exit = state.last_status;
				continue;
			}
			mrsh_buffer_append(&read_buffer, line, n);
			free(line);

			parser_buffer.len = 0;
			mrsh_buffer_append(&parser_buffer,
				read_buffer.data, read_buffer.len);

			mrsh_parser_reset(parser);
		}

		struct mrsh_program *prog = mrsh_parse_line(parser);
		if (mrsh_parser_continuation_line(parser)) {
			// Nothing to see here
		} else if (prog == NULL) {
			struct mrsh_position err_pos;
			const char *err_msg = mrsh_parser_error(parser, &err_pos);
			if (err_msg != NULL) {
				mrsh_buffer_finish(&read_buffer);
				fprintf(stderr, "%s:%d:%d: syntax error: %s\n",
					state.args->argv[0], err_pos.line, err_pos.column, err_msg);
				if (state.interactive) {
					continue;
				} else {
					state.exit = 1;
					break;
				}
			} else if (mrsh_parser_eof(parser)) {
				state.exit = state.last_status;
				break;
			} else {
				fprintf(stderr, "unknown error\n");
				state.exit = 1;
				break;
			}
		} else {
			if ((state.options & MRSH_OPT_NOEXEC)) {
				mrsh_program_print(prog);
			} else {
				mrsh_run_program(&state, prog);
			}
			mrsh_buffer_finish(&read_buffer);
		}
		mrsh_program_destroy(prog);
	}

	if (state.interactive) {
		printf("\n");
	}

	mrsh_buffer_finish(&read_buffer);
	mrsh_parser_destroy(parser);
	mrsh_buffer_finish(&parser_buffer);
	mrsh_state_finish(&state);
	if (fd >= 0) {
		close(fd);
	}

	return state.exit;
}