-
Notifications
You must be signed in to change notification settings - Fork 0
/
progflow.c
88 lines (79 loc) · 1.68 KB
/
progflow.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
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
/*
** EPITECH PROJECT, 2019
** PSU_42sh_2018
** File description:
** something small
*/
#include "shell.h"
static void show_ast(ast_t *ast, sh_t *sh)
{
if (!ast || !sh->showtree)
return;
if (ast->type != T_EXPR) {
my_printf("type: %s\n", MYSHTOK_LIST[ast->type]);
} else {
my_printf("expr: ");
for (size_t i = 0 ; ast->cmd->av[i] ; ++i)
my_printf("%s ", ast->cmd->av[i]);
my_putchar('\n');
}
if (ast->left) {
my_printf("left:\t");
show_ast(ast->left, sh);
}
if (ast->right) {
my_printf("right:\t");
show_ast(ast->right, sh);
}
}
int sh_setexc(sh_t *sh, int c)
{
sh->exc = c;
return sh->exc;
}
ast_t *prompt(sh_t *sh)
{
ast_t *ast;
char *in;
if (isatty(fileno(sh->infd)) && isatty(STDOUT_FILENO))
my_putstr(SHELL_PS1);
in = gnugetl(sh->infd);
if (!in) {
cmd_exit(1, 0, sh);
return 0;
}
if (isatty(fileno(sh->infd)) && isatty(STDOUT_FILENO))
my_puts("");
ast = mkast(in, sh);
free(in);
return ast;
}
int noninteractive(int ac, char **av, sh_t *sh)
{
ast_t *ast;
if (ac >= 2 && !my_strcmp(av[1], "-c")) {
if (ac == 2)
return sh->exc;
ast = mkast(av[2], sh);
show_ast(ast, sh);
parse(ast, sh);
rmast(ast);
} else
infile(av[1], sh);
rmsh(sh);
return sh->exc;
}
int loop(sh_t *sh)
{
ast_t *ast;
if (!dict_get(sh->env, "PATH"))
cmd_setenv(3, DEFAULT_PATH, sh);
while (!sh->eof) {
ast = prompt(sh);
show_ast(ast, sh);
parse(ast, sh);
rmast(ast);
}
rmsh(sh);
return sh->exc;
}