-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.c
431 lines (343 loc) · 9.87 KB
/
commands.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "headers.h"
#include <ctype.h>
void help_command( args *arguments );
void host_command( args *arguments );
void ls_command( args *arguments );
void cat_command( args *arguments );
void time_command( args *arguments );
void probe_command( args *arguments );
void ps_command( args *arguments );
void kill_command( args *arguments );
void exec_command( args *arguments );
void connect_command( args *arguments );
void get_command( args *arguments );
void put_command( args *arguments );
void exit_command( args *arguments );
void rls_command( args *arguments );
void rm_command( args *arguments );
void ping_command( args *arguments );
commands command_list[ MAX_COMMANDS ] = {
{ "help", "Get help about available system commands.", help_command },
{ "host", "Get info on the current host.", host_command },
{ "ls", "List the files on the current host.", ls_command },
{ "cat", "Cat the contents of a file to the screen.", cat_command },
{ "rm", "Remove a file from the local filesystem.", rm_command },
{ "time", "Get the current system time.", time_command },
{ "probe", "Probe a system to determine its type.", probe_command },
{ "ping", "Ping a system to see if its online.", ping_command },
{ "ps", "List the processes on the current host.", ps_command },
{ "kill", "Kill a process on the current system.", kill_command },
{ "exec", "Create a process from an executable file.", exec_command },
{ "connect", "Connect to a system using its IP address.", connect_command },
{ "rls", "Remote ls (list files on the home system).", rls_command },
{ "get", "Get a file from a remote system.", get_command },
{ "put", "Put a file to a remote system.", put_command },
{ "exit", "Exit the current system.", exit_command },
};
void help_command( args *arguments )
{
char line[MAX_MSG_SIZE];
for ( int i = 0 ; i < MAX_COMMANDS ; i++ )
{
sprintf( line, "%-12s | %s",
command_list[ i ].name, command_list[ i ].help );
add_message( line );
}
}
void host_command( args *arguments )
{
char line[MAX_MSG_SIZE];
sprintf( line, "%s (%s)", systems[ current_system( ) ].hostinfo,
systems[ current_system( ) ].ip_address );
add_message( line );
return;
}
void ls_command( args *arguments )
{
char line[MAX_MSG_SIZE];
for ( int i = 0 ; i < MAX_FILES ; i++ )
{
if ( systems[ current_system( )].filesystem.files[i].active )
{
sprintf( line, "%s %5u %-17s (%d)",
systems[ current_system( ) ].filesystem.files[i].attributes,
(unsigned int)strlen(
systems[ current_system( ) ].filesystem.files[i].contents),
systems[ current_system( ) ].filesystem.files[i].filename,
systems[ current_system( ) ].filesystem.files[i].quantity );
add_message( line );
}
}
}
void cat_file( int file_index )
{
char line[ MAX_MSG_SIZE ] = { 0 };
char *file;
int wrindex = 0, len;
file = systems[ current_system( ) ].
filesystem.files[ file_index ].contents;
len = strlen( file );
for ( int i = 0 ; i < len ; i++ )
{
if ( file[ i ] == '\n' )
{
line[ wrindex ] = 0;
add_message( line );
wrindex = 0;
line[ 0 ] = 0;
}
else line[ wrindex++ ] = file[ i ];
}
return;
}
void cat_command( args *arguments )
{
int file_index;
if ( arguments->num_args < 2 ) return;
file_index = find_file( current_system( ), arguments->args[ 1 ] );
if ( file_index != -1 )
{
cat_file( file_index );
return;
}
add_message( "File not found." );
return;
}
void rm_command( args *arguments )
{
int file_index;
if ( arguments->num_args < 2 ) return;
file_index = find_file( current_system( ), arguments->args[ 1 ] );
if ( file_index != -1 )
{
systems[ current_system( ) ].filesystem.files[ file_index ].active = 0;
add_message( "File removed." );
}
else
{
add_message( "File not found." );
}
return;
}
void time_command( args *arguments )
{
char line[MAX_MSG_SIZE];
sprintf( line, "%7.2f",
(float)(GameTime/100 + systems[ current_system( ) ].flags.timezone ) );
add_message( line );
return;
}
void probe_command( args *arguments )
{
int target_system;
if ( arguments->num_args < 2 ) return;
target_system = find_system( arguments->args[ 1] );
if ( target_system != -1 )
{
if ( systems[ target_system ].flags.discoverable )
{
if ( systems[ target_system ].flags.probeable )
{
char line[MAX_MSG_SIZE];
sprintf( line, "%s (%s)", systems[ target_system ].hostinfo,
systems[ target_system ].ip_address );
add_message( line );
}
else
{
add_message( "Host found but cannot probe." );
}
return;
}
}
add_message( "Host not found." );
return;
}
void ping_command( args *arguments )
{
int target_system;
if ( arguments->num_args < 2 ) return;
target_system = find_system( arguments->args[ 1 ] );
if ( target_system != -1 )
{
if ( systems[ target_system ].flags.discoverable )
{
add_message( "Host is alive." );
return;
}
}
add_message( "Host not found." );
return;
}
void ps_command( args *arguments )
{
char line[ MAX_MSG_SIZE ];
char *state_names[] = { "INVALID", "INSTALLING", "RUNNING",
"SLEEPING", "EXITING", "ZOMBIE" };
processes_t *processes = &systems[ current_system( ) ].processes;
add_message( "PID NAME STATE" );
for ( int i = 0 ; i < MAX_PROCESSES ; i++ )
{
if ( processes->process[i].flags.active )
{
sprintf( line, "%4d %-17s %s", processes->process[i].pid,
processes->process[i].name,
state_names[ processes->process[i].state ] );
add_message( line );
}
}
return;
}
void kill_command( args *arguments )
{
char line[ MAX_MSG_SIZE ];
processes_t *processes = &systems[ current_system( ) ].processes;
if ( arguments->num_args < 2 ) return;
if ( can_execute( ) == 0 )
{
add_message( "Security exception." );
return;
}
for ( int i = 0 ; i < MAX_PROCESSES ; i++ )
{
if ( processes->process[ i ].pid == atoi( arguments->args[ 1 ] ) )
{
if ( processes->process[ i ].flags.active )
{
if ( processes->process[ i ].flags.killable )
{
processes->process[ i ].flags.active = 0;
sprintf( line, "[%4d]+ Killed %s",
processes->process[ i ].pid,
processes->process[ i ].name );
add_message( line );
}
else
{
add_message( "Process not responding to kill request." );
}
}
return;
}
}
add_message( "Process not found." );
return;
}
void exec_command( args *arguments )
{
int file_index, process_index;
unsigned int arg = 0;
if ( arguments->num_args < 2 ) return;
file_index = find_file( current_system( ), arguments->args[ 1 ] );
if ( file_index != -1 )
{
process_index = find_empty_process( );
if ( process_index != -1 )
{
if ( arguments->num_args == 3 )
{
arg = ( unsigned int ) atoi( arguments->args[ 2 ] );
}
create_process_from_file(
process_index, file_index, arg );
}
else
{
add_message( "Process space is full." );
}
}
else
{
add_message( "File not found." );
}
return;
}
void connect_command( args *arguments )
{
char line[ MAX_MSG_SIZE ];
int connection;
if ( arguments->num_args < 2 ) return;
if ( can_execute( ) == 0 )
{
add_message( "Security exception." );
return;
}
connection = find_system( arguments->args[ 1 ] );
if ( connection != -1 )
{
push_system( current_system( ) );
set_current_system( connection );
sprintf( line, "Connected to %s", systems[ connection ].ip_address );
add_message( line );
}
else
{
add_message( "Could not connect to system." );
}
return;
}
void exit_command( args *arguments )
{
if ( system_stack_empty( ) )
{
add_message( "Cannot exit." );
}
else
{
set_current_system( pop_system( ) );
add_message( "Connection terminated." );
}
return;
}
void rls_command( args *arguments )
{
char line[MAX_MSG_SIZE];
for ( int i = 0 ; i < MAX_FILES ; i++ )
{
if ( systems[ 0 ].filesystem.files[ i ].active )
{
sprintf( line, "%s: %s %5u %-17s (%d)",
systems[ 0 ].ip_address,
systems[ 0 ].filesystem.files[i].attributes,
(unsigned int)strlen(
systems[ 0 ].filesystem.files[i].contents),
systems[ 0 ].filesystem.files[i].filename,
systems[ 0 ].filesystem.files[i].quantity );
add_message( line );
}
}
return;
}
void get_command( args *arguments )
{
int file_index;
if ( arguments->num_args < 2 ) return;
if ( can_execute( ) == 0 )
{
add_message( "Security exception." );
return;
}
if ( current_system( ) == 0 )
{
add_message( "This command can only be executed on remote systems." );
}
else
{
move_file( 0, current_system( ), arguments->args[ 1 ] );
}
return;
}
void put_command( args *arguments )
{
int file_index;
if ( arguments->num_args < 2 ) return;
if ( current_system( ) == 0 )
{
add_message( "This command can only be executed on remote systems." );
}
else
{
move_file( current_system( ), 0, arguments->args[ 1 ] );
}
return;
}