-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
62 lines (54 loc) · 1.28 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "parser.h"
#include "args.h"
#include "internals.h"
#include "dirstack.h"
#include "runner.h"
void parse_error(const char * str, const int where)
{
fprintf(stderr, "Parse error near %d: %s\n", where, str);
}
int main(void)
{
char *input; // holds the input string.
char cwd[128]; // the current working directory.
command_t *root; // the root of the parse tree.
/* initialize directory stack */
init(&ds);
/* initialize the GNU History library */
using_history();
read_history(NULL);
for (;;) {
root = NULL;
/* get current directory */
getcwd(cwd, 128);
/* print shell prompt */
input = readline(strcat(cwd, " #> "));
/* we don't need empty lines in history */
if (input){
add_history(input);
} else {
continue;
}
if (parse_line(input, &root)) {
/* is root valid? */
if (root != NULL)
/* run command */
run_cmd(root);
}
else {
/* error parsing the command */
printf("%s\n", input);
fprintf(stderr, "Error parsing!\n");
}
/* free memory */
free_parse_memory();
free(input);
}
return EXIT_SUCCESS;
}