-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias.c
More file actions
84 lines (70 loc) · 2.54 KB
/
alias.c
File metadata and controls
84 lines (70 loc) · 2.54 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 "utils.h"
#include "commands.h"
#include "prompt.h"
#include "alias.h"
void load_myshrc() {
// Process aliases from file (if needed)
FILE *file = fopen("shellux.myshrc", "r");
if (file) {
char line[256];
while (fgets(line, sizeof(line), file)) {
// Process aliases
if (strstr(line, "alias") != NULL) {
char *alias_name = strtok(line + 6, "=");
char *command = strtok(NULL, "\n");
alias_name = trim_whitespace_a(alias_name);
command = trim_whitespace_a(command);
if (alias_name && command && alias_count < MAX_ALIASES) {
aliases[alias_count].alias_name = strdup(alias_name);
aliases[alias_count].command = strdup(command);
// printf("Loaded alias: %s -> %s\n", alias_name, command); // Debug
alias_count++;
}
}
}
fclose(file);
}
}
bool execute_func_with_argument(const char *func_name, const char *argument){
char config_path[1024];
snprintf(config_path, sizeof(config_path), "%s/%s", shell_home_directory, "shellux.myshrc");
FILE *config_file = fopen(config_path, "r");
if (!config_file){
perror("Error opening configuration file");
return false;
}
char buffer[1000];
bool inside_function = false;
bool function_found = false;
while (fgets(buffer, sizeof(buffer), config_file)){
buffer[strcspn(buffer, "\n")] = '\0'; // Strip newline
if (!inside_function){
if (strncmp(func_name, buffer, strlen(func_name)) == 0 &&
strstr(buffer, "()") != NULL && buffer[strlen(func_name)] == '('){
inside_function = true;
function_found = true;
continue;
}
}
else{
if (strchr(buffer, '{')){
continue;
}
if (strchr(buffer, '}')){
break;
}
char *arg_position = strstr(buffer, "$1");
if (arg_position){
char modified_command[1000];
size_t prefix_len = arg_position - buffer;
snprintf(modified_command, sizeof(modified_command), "%.*s%s%s", (int)prefix_len, buffer, argument, arg_position + 2);
execute_command(modified_command, 0);
}
else{
execute_command(buffer, 0);
}
}
}
fclose(config_file);
return function_found;
}