-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.c
216 lines (181 loc) · 4 KB
/
interpreter.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "interpreter.h"
#include "shellmemory.h"
#include "kernel.h"
#include "shell.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
static int run( char* program );
int print( char* argument[] , int numberArgs )
{
int errorCode = 0 ;
if( numberArgs == 1 || strcmp( argument[1] , "\0" ) == 0 )
{
errorCode = 5 ;
return errorCode ;
}
char* var = argument[1] ;
char* value = getVariable( var ) ;
if(strcmp( value , "Variable does not exist" ) == 0 )
{
errorCode = 2;
} else
{
printf( "%s = %s\n" , var , value ) ;
}
return errorCode;
}
int set(char* argument[], int numberArgs)
{
int errorCode = 0;
if( numberArgs == 1 || strcmp( argument[1] , "\0" ) == 0 )
{
errorCode = 5 ;
return errorCode ;
}
if ( numberArgs == 2 || strcmp( argument[2] , "\0" ) == 0 )
{
errorCode = 6 ;
return errorCode ;
}
errorCode = setValue( argument[1] , argument[2] ) ;
return errorCode ;
}
int quit()
{
printf("Ciao!\n");
return -1;
}
int help()
{
printf( "\n\tmyshell man page"
"\n"
"\nCOMMAND\tDESCRIPTION"
"\nhelp\tDisplays all the commands"
"\nquit\tExits / terminates the shell with Bye!"
"\nset VAR STRING\tAssigns a value to shell memory"
"\nprint VAR\tDisplays the STRING assigned to VAR"
"\nrun SCRIPT.TXT\tExecutes the file SCRIPT.TXT"
"\nexec p1 p2 p3\tExecutes concurrent programs\n");
return 0;
}
int exec( char* argument[] , int numberArgs )
{
int errorCode = 0 ;
int sameProgram = 0 ;
char* program ;
if ( numberArgs < 2 || numberArgs > 4 )
{
errorCode = 7 ;
return errorCode ;
}
if ( numberArgs == 3 )
{
if( strcmp( argument[1] , argument[2]) == 0 )
{
sameProgram = 2 ;
program = argument[2] ;
}
}
if ( numberArgs == 4 )
{
if ((strcmp(argument[1], argument[3]) == 0) && (strcmp(argument[2], argument[3]) == 0))
{
sameProgram = -1 ;
program = argument[1] ;
} else if (strcmp(argument[1], argument[3]) == 0)
{
sameProgram = 3;
program = argument[1];
} else if (strcmp(argument[2], argument[3]) == 0 )
{
sameProgram = 3;
program = argument[3];
} else if ( strcmp(argument[1], argument[2]) == 0)
{
sameProgram = 2;
program = argument[1];
}
}
for( int i = 0 ; i < numberArgs - 1 ; i++ )
{
if ( sameProgram != 0 && i == sameProgram - 1 )
{
continue ;
} else
{
errorCode = myinit( argument[ i+1 ] ) ;
if ( errorCode != 0 )
{
return errorCode;
}
}
if( sameProgram == -1 )
{
break;
}
}
errorCode = scheduler() ;
if( sameProgram == -1)
{
printf ( "Error: Script %s already loaded\n" , program ) ;
} else if (sameProgram != 0)
{
printf ( "Error: Script %s already loaded\n" , program ) ;
}
return errorCode;
}
static int run(char* program)
{
int errorCode = 0 ;
char program_line[1000] ;
FILE *p = fopen( program , "r" ) ;
if( p == NULL )
{
errorCode = 3 ;
return errorCode ;
}
fgets( program_line , 999 , p ) ;
while( !feof(p) )
{
if( strcmp ( program_line , "quit\n" ) == 0 ) break ;
errorCode = parse( program_line ) ;
if( errorCode != 0 )
{
fclose(p) ;
return errorCode = interpreter(parse(program_line), 1);
}
//interpreter(parse(program_line), 1);
fgets( program_line , 999 , p ) ;
}
fclose(p) ;
return errorCode ;
}
int interpreter(char* command[], int numberArgs )
{
int errorCode = 0;
if ( strcmp( command[0] , "help" ) == 0)
{
errorCode = help();
} else if( strcmp( command[0] , "quit" ) == 0 )
{
errorCode = quit() ;
} else if ( strcmp( command[0] , "set" ) == 0 )
{
errorCode = set( command , numberArgs ) ;
} else if ( strcmp( command[0] , "print" ) == 0 )
{
errorCode = print( command , numberArgs ) ;
} else if ( strcmp( command[0] , "run" ) == 0 )
{
errorCode = run( command[1] ) ;
} else if ( strcmp( command[0] , "exec" ) == 0 )
{
errorCode = exec( command , numberArgs ) ;
} else
{
errorCode = 1;
}
return errorCode;
}