-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshell.c
More file actions
83 lines (72 loc) · 2.35 KB
/
shell.c
File metadata and controls
83 lines (72 loc) · 2.35 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../include/macros.h"
#include "symbol_table.h"
#include "command.h"
#define MAX_OPTS 20
#define LINE_SIZE 128
void print_banner(){
printf("############################################################################\n");
printf("# #\n");
printf("# ll iii bbb xx xx mm mm ll qqqq uu uu eeeee rrrr yy yy #\n");
printf("# ll i bb b xx xx mmm mmm ll qq qq uu uu ee rr rr yy yy #\n");
printf("# ll i bbb xxx mmmmmmm ll qq qq uu uu eeee rrrr yyy #\n");
printf("# ll i bb b xx xx mm m mm ll qq qq uu uu ee rr rr yyy #\n");
printf("# llll iii bbb xx xx mm mm llll qqqq q uuuuuu eeeee rr rr yyy #\n");
printf("# #\n");
printf("############################################################################\n");
printf("\n");
}
typedef struct parsed_line_s{
int n_args;
char* command;
char* options[MAX_OPTS];
}parsed_line;
parsed_line* parse_command_line(char* line){
parsed_line* pl = alloc(parsed_line, 1);
char *start, *end;
int option = 0;
//find command
start = end = line;
for(; *end != ' ' && *end != '\n'; end++);
pl->command = strndup(start, end - start);
//find options treating double quotes
for(; *end != '\n';){
for(; *end == ' '; end++);
if(*end == '"'){
for(end++, start = end; *end != '"'; end++);
(pl->options)[option++] = strndup(start, end - start);
end++; //consume double quote
start = end;
}else{
for(start = end; *end != ' ' && *end != '\n'; end++);
(pl->options)[option++] = strndup(start, end - start);
start = end;
}
}
pl->n_args = option;
return pl;
}
void destroy_parsed_line(parsed_line* pl){
free(pl->command);
int i;
for(i = 0; i < pl->n_args; i++)
free((pl->options)[i]);
free(pl);
}
int main(){
char line[LINE_SIZE] = {0};
init_command_table();
init_symbol_table();
print_banner();
while(1){
printf(">>> ");
fgets(line, LINE_SIZE, stdin);
parsed_line* pl = parse_command_line(line);
exec_command(pl->command, pl->n_args, pl->options);
destroy_parsed_line(pl);
memset(line, 0, LINE_SIZE);
}
return 0;
}