~emersion/mrsh

ref: 2195cd2616b71838ecd10878bb04f70a52cb238c mrsh/main.c -rw-r--r-- 957 bytes
2195cd26emersion Update README with build instructions 3 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
#define _POSIX_C_SOURCE 2
#include <mrsh/ast.h>
#include <mrsh/parser.h>
#include <mrsh/shell.h>
#include <stdlib.h>
#include <unistd.h>

static const char usage[] = "usage: mrsh [-n]\n";

int main(int argc, char *argv[]) {
	bool noexec = false;

	int opt;
	while ((opt = getopt(argc, argv, "n")) != -1) {
		switch (opt) {
		case 'n':
			noexec = true;
			break;
		default:
			fprintf(stderr, usage);
			exit(EXIT_FAILURE);
		}
	}

	if (noexec) {
		struct mrsh_program *prog = mrsh_parse(stdin);
		mrsh_program_print(prog);
		return EXIT_SUCCESS;
	}

	struct mrsh_parser *parser = mrsh_parser_create(stdin);
	struct mrsh_state state = {0};
	mrsh_state_init(&state);

	while (state.exit == -1) {
		struct mrsh_command_list *list = mrsh_parse_command_list(parser);
		if (list == NULL) {
			state.exit = EXIT_SUCCESS;
			break;
		}
		mrsh_command_list_print(list);
		mrsh_run_command_list(&state, list);
	}

	mrsh_parser_destroy(parser);
	return state.exit;
}