summaryrefslogtreecommitdiff
path: root/main.c
blob: 4304a92a78f20f320accb8714fc6dafe11ee4e9e (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>

#define MAX_HISTORY 100
#define MAX_COMMAND_LENGTH 255 // not setting a  macro for the argument because they are included in the command)
#define MAX_PATH_LENGTH 1024

struct command {
    char *com;
    char *arg;
};

struct exec_command {
    const char *cmd;
    void (*func)(const char *);
};


// code prototype functions
struct command split_command(char *input);

void exec_command(char *input);

// code functions
struct command split_command(char *input) {
    struct command NEW_COMMAND = {};

    char *command = strtok(input, " ");
    char *argument = strtok(NULL, " ");

    if (command != NULL) {
        NEW_COMMAND.com = strdup(command);
    } else {
        printf("failed, null pointer detected, no initial command has been inserted");
        free(command);
    }

    if (argument != NULL) {
        NEW_COMMAND.arg = strdup(argument);
    } else {
        free(argument);
    }
    return NEW_COMMAND;
}

void exec_command(char *input) {
    struct command NEW_COMMAND = {};

    // cycle through the command array and find the matching command and execute it
}


// shell commands functions prototypes
void list(const char *path);

void make_dir(const char *path);

void remove_dir(const char *path);

void remove_file(const char *path);

void copy_files(const char *arg);

void move_files(const char *arg);

void print_cdirectory(const char *arg);

void change_directory(const char *path);

void clear(const char *arg);

void echo(const char *arg);

struct exec_command CommandsList[] = {
    {"ls", list},
    {"make_dir", make_dir},
    {"remove_dir", remove_dir},
    {"remove_file", remove_file},
    {"copy_files", copy_files},
    {"move_files", move_files},
    {"print_cdirectory", print_cdirectory},
    {"change_directory", change_directory},
    {"clear", clear},
    {"echo", echo}
};

// code functions
struct command split_command(char *input) {
    struct command NEW_COMMAND = {};

    char *command = strtok(input, " ");
    char *argument = strtok(NULL, " ");

    if (command != NULL) {
        NEW_COMMAND.com = strdup(command);
    } else {
        printf("failed, null pointer detected, no initial command has been inserted");
        free(command);
    }

    if (argument != NULL) {
        NEW_COMMAND.arg = strdup(argument);
    } else {
        free(argument);
    }
    return NEW_COMMAND;
}

void exec_command(char *input) {
    const struct command user_command = split_command(input);
    user_command.com[strcspn(user_command.com, "\n")] = '\0';
    if (user_command.arg != NULL) {
        user_command.arg[strcspn(user_command.arg, "\n")] = '\0';
    }

    for (int i = 0; i < sizeof(*CommandsList) / sizeof(CommandsList->cmd[0]); i++) {
        // ReSharper disable once CppIncompatiblePointerConversion
        if (strcmp(user_command.com, &CommandsList->cmd[i]) == 0) {
            CommandsList[i].func(user_command.arg);
        }
    }
}

// shell command functions
// ls command
void list(const char *path) {
    struct dirent *entry;
    if (path == NULL) {
        printf("No path found\n");
        return;
    }
    DIR *dP = opendir(path);

    // check if the directory got opened successfully
    if (dP == NULL) {
        perror("opendir");
        return;
    }

    // print the folder|directory name
    while ((entry = readdir(dP)) != NULL) {
        printf("%s\n", entry->d_name);
    }
    closedir(dP);
}

void make_dir(const char *path) {
    printf("made a new directory");
}

void remove_dir(const char *path) {
    printf("removed a directory");
}

void remove_file(const char *path) {
    printf("remove_file");
}

void copy_files(const char *arg) {
    printf("copy file");
}

void move_files(const char *arg) {
    printf("move file");
}

void clear(const char *arg) {
    if (arg != NULL) {
        printf("clear doesn't support any arguments, see help() for help");
        return;
    }

    // clear the visible part of the terminal -> see man pages clear command
    printf("\033[2J");
    // clear the input buffer of the terminal -> see man pages clear command
    printf("\033[3J");
}

void echo(const char *arg) {
    printf("%s", arg);
}

void print_cdirectory(const char *arg) {
    if (arg != NULL) {
        printf("print_cdirectory doesn't support any arguments, see help() for help");
        return;
    }
    char *current_working_directory = getcwd(NULL, 0);
    /* getcwd() command already allocates malloc for the path that should be returned,
     to do: should I free it when finished or not?/
      so what I understand from the documentation, you should only free it when actually setting an exact size
      but when keeping the max size to 0 and relying on the malloc there is no need for it
      not sure though
      should rely on further debugging
     */

    return current_working_directory;
}

void change_directory(const char *path) {
    if (path == NULL) {
        printf("No path found\n");
    }
}

int main(void) {
    clear(NULL);
    // ReSharper disable once CppDFAEndlessLoop
    while (1) {
        printf("\n$ ");

        char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH);
        fgets(input, MAX_COMMAND_LENGTH, stdin);

        const struct command new_input = split_command(input);
        new_input.com[strcspn(new_input.com, "\n")] = '\0';

        if (new_input.arg != NULL) {
            new_input.arg[strcspn(new_input.arg, "\n")] = '\0';
        }

        if (strcmp(new_input.com, "exit") == 0) {
            free(input);
            return 0;
        }
        if (strcmp(new_input.com, "ls") == 0) {
            ls(new_input.arg);
            free(new_input.com);
            if (new_input.arg != NULL) {
                free(new_input.arg);
            }
        }
        if (strcmp(new_input.com, "echo") == 0) {
            printf("%s", new_input.arg);
            free(new_input.arg);
        }
        if (strcmp(new_input.com, "clear") == 0) {
            clear();
        }
    }
}