-
Notifications
You must be signed in to change notification settings - Fork 0
/
myshell.c
132 lines (103 loc) · 2.61 KB
/
myshell.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
//INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "cmd_utils.h"
#include "sig_handlers.h"
#include "exec.h"
#include "redirectIO.h"
//DEFINES
#define MAX_LIMIT 100
#define CCYN "\x1B[36m"
#define CNRM "\x1B[0m"
#define CGRN "\x1B[32m"
//GLOBAL VARIABLES
sigjmp_buf sigint_buf;
//FUNCTION DECLARATIONS
void print_pwd(void);
void print_user(void);
void print_host(void);
void read_script(char*);
void sigint_handler(int signal);
void set_sigint(void);
int main(int argc, char **argv){
char* command;
command = (char*) malloc(sizeof(char)*1024);
size_t buffer = 0;
set_sigint();
set_sigtstp();
set_sigquit();
clear();
if(argc > 1){
read_script(argv[1]);
}else{
while(1){
print_user();
print_host();
print_pwd();
//Check if getline function is successful. Prevent segmention fault with empty commands
if(getline(&command, &buffer, stdin) == -1 || *command == '\n'){
printf("Your command is empty or erroneous. Try again!\n");
}
else{
command[strcspn(command, "\n")] = '\000'; //Character \n is deleted
if(find_pipe(command)){
mypipe(command);
}
else if(find_redirection(command)){
redirect(command);
}
else{
parse_command(command);
}
}
}
}
free(command);
return 0;
}
void print_pwd(void){
printf( "%s%s", CGRN, getenv("PWD") );
printf(" >%s ", CNRM);
return;
}
void print_user(void){
char* user = getenv("USER");
printf("%s%s", CCYN, user);
return;
}
void print_host(void){
char output[100];
gethostname(output, sizeof(output));
printf("@%s:%s~", output, CNRM);
return;
}
void read_script(char *file){
FILE* fpointer;
fpointer = fopen(file,"r");
char* buffer = NULL;
buffer = malloc(300);
if(fpointer == NULL){
printf("Error: Could not open file %s.\nError number: %d\n",file, errno);
}else{
while(fgets(buffer, 300, fpointer) != NULL){
buffer[strcspn(buffer, "\n")] = '\000';
if(strcmp(buffer, "") != 0){
parse_command(buffer);
}
}
fclose(fpointer);
}
free(buffer);
return;
}
void sigint_handler(int signal){
siglongjmp(sigint_buf, 1);
}
void set_sigint(void){
signal(SIGINT, sigint_handler);
if(!sigsetjmp(sigint_buf, 1));
else;
}