-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.c
86 lines (70 loc) · 1.82 KB
/
globals.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
/**
* Author: Samuel Marsh
* Date: 30/10/2015
*/
#include "mysh.h"
// THREE INTERNAL VARIABLES (SEE mysh.h FOR EXPLANATION)
char *HOME, *PATH, *CDPATH;
int last_exit_status;
char *argv0 = NULL; // the program's name
bool interactive = false;
// ------------------------------------------------------------------------
void check_allocation0(void *p, char *file, const char *func, int line)
{
if(p == NULL) {
fprintf(stderr, "%s, %s() line %i: unable to allocate memory\n",
file, func, line);
exit(2);
}
}
// REPORT ANY INPUT/OUTPUT REDIRECTION ASSOCIATED WITH A COMMAND NODE
static void print_redirection(CMDTREE *t)
{
if(t->infile != NULL)
printf("< %s ",t->infile);
if(t->outfile != NULL) {
if(t->append == false)
printf(">");
else
printf(">>");
printf(" %s ",t->outfile);
}
}
// PRINT THE COMMANDS AND CONTROL-FLOW SEQUENCES IN THE INDICATED COMMAND NODE
void print_cmdtree0(CMDTREE *t)
{
if(t == NULL) {
printf("<nullcmd> ");
return;
}
switch (t->type) {
case N_COMMAND :
for(int a=0 ; a<t->argc ; a++)
printf("%s ", t->argv[a]);
print_redirection(t);
break;
case N_SUBSHELL :
printf("( "); print_cmdtree0(t->left); printf(") ");
print_redirection(t);
break;
case N_AND :
print_cmdtree0(t->left); printf("&& "); print_cmdtree0(t->right);
break;
case N_OR :
print_cmdtree0(t->left); printf("|| "); print_cmdtree0(t->right);
break;
case N_PIPE :
print_cmdtree0(t->left); printf("| "); print_cmdtree0(t->right);
break;
case N_SEMICOLON :
print_cmdtree0(t->left); printf("; "); print_cmdtree0(t->right);
break;
case N_BACKGROUND :
print_cmdtree0(t->left); printf("& "); print_cmdtree0(t->right);
break;
default :
fprintf(stderr,"%s: invalid NODETYPE in print_cmdtree0()\n",argv0);
exit(1);
break;
}
}