-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.h
122 lines (106 loc) · 3.23 KB
/
terminal.h
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
/* STRUCTS */
struct termios orig_termios; /* holds the original terminal settings */
typedef struct Terminal
{
char history[MAX_TOKENS][MAX_TOKEN_LEN];
void (*addHistory)(struct Terminal *terminal, const char input[]); // functie care adauga o comanda in istoric
void (*listDirectory)(const char *path);
void (*clearScreen)();
void (*verifyCharacters)(struct Terminal *terminal, char *input, int *inputIndex, int *historyIndex);
void (*commandRedirection)(char *command, char *file);
int (*doAnInstruction)(struct Terminal *terminal, char* input);
void (*printUser)();
void (*doWithLogicInstruction)(struct Terminal *terminal, char* input);
} Terminal;
/* FUNCTIONS */
/* TERMINAL */
void add_to_history(struct Terminal *terminal, const char input[])
{
if (countHistory < MAX_TOKENS)
{
strncpy(terminal->history[countHistory], input, MAX_TOKEN_LEN - 1); // adaugam comanda in istoric
terminal->history[countHistory][MAX_TOKEN_LEN - 1] = '\0'; // Ensure null-termination
countHistory++;
}
}
// functie care verifica daca inputul este valid
void list_directory(const char *path)
{
struct dirent *entry; // contine informatii despre un fisier din director
DIR *dir = opendir(path);
if (dir == NULL)
{
printf("Error: Unable to open directory.\n");
return;
}
while ((entry = readdir(dir)) != NULL) // citim fiecare fisier din director
{
struct stat statbuf;
stat(entry->d_name, &statbuf); // obtinem informatii despre fisier
// S_ISDIR -> folosit ca sa ne dam seama daca un director
// primeste cv de tip mode_t (vezi lab 2)
if (S_ISDIR(statbuf.st_mode))
{
printf(GREEN_UNDERLINE "%s\n" RESET_BCKGROUND, entry->d_name);
}
else
{
printf(BOLD_BLUE "%s\n" RESET_COLOUR, entry->d_name);
}
}
closedir(dir);
}
void clear_screen()
{
printf("\033[H\033[J");
}
// folosim pipe
int isPackageAccessible(const char *command)
{
char check_command[100];
snprintf(check_command, sizeof(check_command), "command -v %s >/dev/null 2>&1", command); // verificam daca comanda este disponibila in sistem
FILE *fp = popen(check_command, "r"); // deschidem un pipe pentru a citi outputul comenzii
if (fp == NULL)
{
return 0;
}
int result = pclose(fp);
// pclose returns -1 in case of an error
if (result == -1)
{
return 0;
}
// Check if the command is accessible
return (result == 0);
}
void command_redirection(char *command, char *file)
{
pid_t pid = fork();
if (pid == 0) // Child process
{
if (file != NULL)
{
int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
{
perror("open");
exit(EXIT_FAILURE);
}
dup2(fd, STDOUT_FILENO);
close(fd);
}
char *argv[] = {command, NULL};
execvp(command, argv);
perror("execvp");
exit(EXIT_FAILURE);
}
else if (pid > 0) // Parent process
{
wait(NULL); // Waiting for the child...
}
else
{
perror("fork");
exit(EXIT_FAILURE);
}
}