-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
389 lines (338 loc) · 7.06 KB
/
shell.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shell.h"
#define TRUE 1
#define FALSE 0
#define ALLOCATION_FAILURE 1
#define FAILURE 2
#define SHELL_CWD_BUFFER_SIZE 1024
#define SHELL_HISTORY_SIZE 10
#define SHELL_READLINE_BUFFER_SIZE 1024
#define SHELL_TOKEN_BUFFER_SIZE 64
#define SHELL_TOKEN_DELIMITERS " \t\r\n\a"
#define SUCCESS 0
#define SHELL_NAME "adorno"
#define array_cnt(x) ( sizeof( x ) / sizeof( x[0] ) )
// MEMORY CONSTANTS
typedef const enum
{
CMD_BACK_ID,
CMD_BACK_LIST_ID,
CMD_CLEAR_ID,
CMD_CD_ID,
CMD_CD_LIST_ID,
CMD_EMACS_ID,
CMD_EXIT_ID,
CMD_HELP_ID,
CMD_LIST_ID,
CMD_OPEN_ID,
CMD_SUBLIME_ID,
CMD_VI_ID,
} command_ids;
static const command_type commands[] =
{
{&cmd_back, "b", "change to the parent directory"},
{&cmd_back_list, "bl", "change to the parent directory and list directory contents"},
{&cmd_clear, "c", "clear the screen"},
{&cmd_cd, "cd", "change directory"},
{&cmd_cd_list, "cdl", "change directory and list directory contents"},
{&cmd_emacs, "e", "launch emacs"},
{&cmd_exit, "exit", "exit shell"},
{&cmd_help, "help", "display help"},
{&cmd_list, "l", "list directory contents"},
{&cmd_open, "o", "open file"},
{&cmd_sublime_text_2, "s", "launch sublime text 2"},
{&cmd_vi, "v", "launch vi"},
};
int cmd_back
(
char** args
)
{
args[0] = "cd";
args[1] = "..";
return cmd_cd( args );
} /* cmd_back() */
int cmd_back_list
(
char** args
)
{
cmd_back( args );
args[0] = "ls";
args[1] = NULL;
return shell_launch( args );
} /* cmd_back_list() */
int cmd_cd
(
char** args
)
{
if( NULL == args[1] )
{
fprintf( stderr, "Expected another argument.\n" );
}
else
{
if( chdir( args[1] ) != 0 )
{
perror( "shell" );
}
}
return TRUE;
} /* cmd_cd() */
int cmd_cd_list
(
char** args
)
{
cmd_cd( args );
args[0] = "ls";
args[1] = NULL;
return shell_launch( args );
} /* cmd_cd_list() */
int cmd_clear
(
char** args
)
{
args[0] = "clear";
return shell_launch( args );
} /* cmd_clear() */
int cmd_emacs
(
char** args
)
{
if( NULL == args[1] )
{
fprintf( stderr, "Expected another argument.\n" );
return TRUE;
}
else
{
args[0] = "emacs";
return shell_launch( args );
}
} /* cmd_emacs() */
int cmd_exit
(
char** args
)
{
return SUCCESS;
} /* cmd_exit() */
int cmd_help
(
char** args
)
{
// Local variables
int i;
for( i = 0; i < array_cnt( commands ); i++ )
{
printf( "\t%s\t%s\n", commands[i].cmd_string, commands[i].help_string );
}
return TRUE;
} /* cmd_help() */
int cmd_list
(
char** args
)
{
args[0] = "ls";
return shell_launch( args );
} /* cmd_list() */
int cmd_open
(
char** args
)
{
args[0] = "open";
return shell_launch( args );
}
int cmd_sublime_text_2
(
char** args
)
{
if( NULL == args[1] )
{
fprintf( stderr, "Expected another argument.\n" );
return TRUE;
}
else
{
args[0] = "open";
args[2] = "-a";
args[3] = "sublime text 2";
return shell_launch( args );
}
} /* cmd_sublime_text_2() */
int cmd_vi
(
char** args
)
{
if( NULL == args[1] )
{
fprintf( stderr, "Expected another argument.\n" );
return TRUE;
}
else
{
args[0] = "vi";
return shell_launch( args );
}
} /* cmd_vi() */
char* shell_read_line
(
void
)
{
// Local variables
char* line;
size_t buffer_size;
// Initialize local variables
line = NULL;
buffer_size = 0;
getline( &line, &buffer_size, stdin );
return line;
} /* shell_read_line() */
char** shell_split_line
(
char* line
)
{
// Local variables
int buffer_size;
int position;
char** tokens;
char* token;
// Initialize local variables
buffer_size = SHELL_TOKEN_BUFFER_SIZE;
position = 0;
tokens = malloc( sizeof( char* ) * buffer_size );
if( !tokens )
{
fprintf( stderr, "Allocation error\n" );
exit( ALLOCATION_FAILURE );
}
token = strtok( line, SHELL_TOKEN_DELIMITERS );
while( NULL != token )
{
tokens[position] = token;
position++;
if( position >= buffer_size )
{
buffer_size += SHELL_TOKEN_BUFFER_SIZE;
tokens = realloc( tokens, sizeof( char* ) * buffer_size );
if( !tokens )
{
fprintf( stderr, "Allocation error\n" );
exit( ALLOCATION_FAILURE );
}
}
token = strtok( NULL, SHELL_TOKEN_DELIMITERS );
}
tokens[position] = NULL;
return tokens;
} /* shell_split_line() */
int shell_launch
(
char** args
)
{
// Local variables
pid_t pid;
pid_t wpid;
int status;
pid = fork();
if( 0 == pid )
{
if( -1 == execvp( args[0], args ) )
{
perror( "shell" );
}
exit( FAILURE );
}
else if( pid < 0 )
{
perror( "shell" );
}
else
{
do
{
wpid = waitpid( pid, &status, WUNTRACED );
} while( !WIFEXITED( status ) && !WIFSIGNALED( status ) );
}
return TRUE;
} /* shell_launch() */
int shell_execute
(
char** args
)
{
// Local variables
int i;
if( NULL == args[0] )
{
return TRUE;
}
for( i = 0; i < array_cnt( commands ); i++ )
{
if( 0 == strcmp( args[0], commands[i].cmd_string ) )
{
return (*(commands[i].function_ptr))(args);
}
}
return shell_launch( args );
} /* shell_execute() */
void shell_loop
(
void
)
{
// Local variables
char* line;
char** args;
int status;
char cwd[SHELL_CWD_BUFFER_SIZE];
char* printable_cwd;
do
{
// Display the prompt
getcwd( cwd, sizeof( cwd ) );
printable_cwd = strrchr( cwd, '/' );
if( '/' == printable_cwd[0] )
{
printable_cwd++;
}
printf( "%s::%s> ", SHELL_NAME, printable_cwd );
line = shell_read_line();
args = shell_split_line( line );
status = shell_execute( args );
free( line );
free( args );
} while( status );
} /* shell_loop() */
void shell_intro
(
void
)
{
printf( "Welcome to the %s shell. Type '%s' for a list of commands.\n", SHELL_NAME, commands[CMD_HELP_ID].cmd_string );
} /* shell_intro() */
int main
(
int argc,
char** argv
)
{
shell_intro();
shell_loop();
return SUCCESS;
} /* main() */