mirror of
https://github.com/bigsketti/cshell.git
synced 2025-06-06 12:54:46 +00:00
refactored handleCommand() to be more modular
This commit is contained in:
parent
b320d7e75d
commit
e488abb906
249
src/cShell.c
249
src/cShell.c
@ -1,137 +1,166 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "cShell.h"
|
||||
|
||||
// TODO: research tab completion, very big quality of life feature
|
||||
// TODO: research tab completion
|
||||
|
||||
// command prototypes so handleCommand() works
|
||||
// built in commands are prefixed with "cmd_"
|
||||
void cmd_exit();
|
||||
void cmd_help();
|
||||
void cmd_version();
|
||||
void cmd_cd(char **args, char **cwd, char **trimmedCWD);
|
||||
void cmd_mkfile(char **args);
|
||||
void cmd_mkdir(char **args);
|
||||
void cmd_rmfile(char **args);
|
||||
void cmd_rmdir(char **args);
|
||||
void cmd_ls(char **args);
|
||||
void cmd_mode(char **args);
|
||||
void printClamShell();
|
||||
char *trimCWD(char *cwd);
|
||||
void execute(char **args);
|
||||
void parseInput(char *input, char **args);
|
||||
char* getCWD();
|
||||
void listDirectorys(const char *path);
|
||||
|
||||
void handleCommand(char **args, char **cwd, char **trimmedCWD) {
|
||||
if (args[0] == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (strcmp("exit", args[0]) == 0) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (strcmp("help", args[0]) == 0) {
|
||||
printf("Welcome to cShell, my shitty terminal written in C\n\n");
|
||||
cmd_exit();
|
||||
} else if (strcmp("help", args[0]) == 0) {
|
||||
cmd_help();
|
||||
return;
|
||||
} else if (strcmp("version", args[0]) == 0) {
|
||||
cmd_version();
|
||||
return;
|
||||
} else if (strcmp("cd", args[0]) == 0) {
|
||||
cmd_cd(args, cwd, trimmedCWD);
|
||||
return;
|
||||
} else if (strcmp("mkfile", args[0]) == 0) {
|
||||
cmd_mkfile(args);
|
||||
return;
|
||||
} else if (strcmp("mkdir", args[0]) == 0) {
|
||||
cmd_mkdir(args);
|
||||
return;
|
||||
} else if (strcmp("rmfile", args[0]) == 0) {
|
||||
cmd_rmfile(args);
|
||||
return;
|
||||
} else if (strcmp("rmdir", args[0]) == 0) {
|
||||
|
||||
return;
|
||||
} else if (strcmp("ls", args[0]) == 0) {
|
||||
cmd_ls(args);
|
||||
return;
|
||||
} else if (strcmp("mode", args[0]) == 0) {
|
||||
cmd_mode(args);
|
||||
} else if (strcmp("generate_clam", args[0]) == 0) {
|
||||
printClamShell();
|
||||
printf(
|
||||
"\nBuilt-in commands:\n"
|
||||
"\texit\n"
|
||||
"\thelp\n"
|
||||
"\tversion\n"
|
||||
"\tgenerate_clam\n"
|
||||
"\tcd <dir name>\n"
|
||||
"\tmkdir <dir name>\n"
|
||||
"\trmdir <dir name>\n"
|
||||
"\tmkfile <file name>\n"
|
||||
"\trmfile <file name>\n"
|
||||
"\tls <optional: path>\n"
|
||||
"\tmode <-arg>\n"
|
||||
"\t\tnormal mode: -n\n"
|
||||
"\t\tevil mode: -e\n"
|
||||
"\t\thacker mode: -h\n\n"
|
||||
);
|
||||
return;
|
||||
} else {
|
||||
execute(args);
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp("version", args[0]) == 0) {
|
||||
printf("cShell version: %f\n", VERSION);
|
||||
return;
|
||||
}
|
||||
void cmd_help() {
|
||||
printf("Welcome to cShell, my shitty terminal written in C\n\n");
|
||||
printClamShell();
|
||||
printf(
|
||||
"\nBuilt-in commands:\n"
|
||||
"\texit\n"
|
||||
"\thelp\n"
|
||||
"\tversion\n"
|
||||
"\tgenerate_clam\n"
|
||||
"\tcd <dir name>\n"
|
||||
"\tmkdir <dir name>\n"
|
||||
"\trmdir <dir name>\n"
|
||||
"\tmkfile <file name>\n"
|
||||
"\trmfile <file name>\n"
|
||||
"\tls <optional: path>\n"
|
||||
"\tmode <-arg>\n"
|
||||
"\t\tnormal mode: -n\n"
|
||||
"\t\tevil mode: -e\n"
|
||||
"\t\thacker mode: -h\n\n"
|
||||
);
|
||||
}
|
||||
|
||||
if (strcmp("cd", args[0]) == 0) {
|
||||
if (args[1] == NULL) {
|
||||
fprintf(stderr, "Missing argument for command \"cd\"\n");
|
||||
void cmd_exit() {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void cmd_version() {
|
||||
printf("cShell version: %f\n", VERSION);
|
||||
}
|
||||
|
||||
void cmd_cd(char **args, char **cwd, char **trimmedCWD) {
|
||||
if (args[1] == NULL) {
|
||||
fprintf(stderr, "Missing argument for command \"cd\"\n");
|
||||
} else {
|
||||
if (chdir(args[1]) != 0) {
|
||||
perror("chdir failed\n");
|
||||
} else {
|
||||
if (chdir(args[1]) != 0) {
|
||||
perror("chdir failed\n");
|
||||
} else {
|
||||
free(*cwd);
|
||||
*cwd = getCWD();
|
||||
*trimmedCWD = trimCWD(*cwd);
|
||||
}
|
||||
free(*cwd);
|
||||
*cwd = getCWD();
|
||||
*trimmedCWD = trimCWD(*cwd);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp("mkfile", args[0]) == 0) {
|
||||
FILE *file = fopen(args[1], "w");
|
||||
void cmd_mkfile(char **args) {
|
||||
FILE *file = fopen(args[1], "w");
|
||||
if (file == NULL) {
|
||||
perror("Error making file");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_mkdir(char **args) {
|
||||
if (mkdir(args[1], 0777) != 0) {
|
||||
perror("Error making directory");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_rmfile(char **args) {
|
||||
if (remove(args[1]) != 0) {
|
||||
perror("Error removing file");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_rmdir(char **args) {
|
||||
if (rmdir(args[1]) != 0) {
|
||||
perror("Error removing directory");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_ls(char **args) {
|
||||
const char *path = args[1] ? args[1] : ".";
|
||||
listDirectorys(path);
|
||||
}
|
||||
|
||||
void cmd_mode(char **args) {
|
||||
if (strcmp("-h", args[1]) == 0) {
|
||||
printf("\033[32m");
|
||||
printf("Hacker mode activated\n");
|
||||
return;
|
||||
} else if (strcmp("-e", args[1]) == 0) {
|
||||
printf("\033[31m");
|
||||
printf("Evil mode activated\n");
|
||||
return;
|
||||
} else if (strcmp("-n", args[1]) == 0) {
|
||||
printf("\033[0m");
|
||||
printf("Normal mode activated\n");
|
||||
return;
|
||||
} else {
|
||||
printf("Invalid arguement, type \"help\" for help\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("mkdir", args[0]) == 0) {
|
||||
if (mkdir(args[1], 0777) != 0) {
|
||||
perror("Error making directory");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("rmfile", args[0]) == 0) {
|
||||
if (remove(args[1]) != 0) {
|
||||
perror("Error removing file");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("rmdir", args[0]) == 0) {
|
||||
if (rmdir(args[1]) != 0) {
|
||||
perror("Error removing directory");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("ls", args[0]) == 0) {
|
||||
const char *path = args[1] ? args[1] : ".";
|
||||
listDirectorys(path);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("mode", args[0]) == 0) {
|
||||
if (strcmp("-h", args[1]) == 0) {
|
||||
printf("\033[32m");
|
||||
printf("Hacker mode activated\n");
|
||||
return;
|
||||
} else if (strcmp("-e", args[1]) == 0) {
|
||||
printf("\033[31m");
|
||||
printf("Evil mode activated\n");
|
||||
return;
|
||||
} else if (strcmp("-n", args[1]) == 0) {
|
||||
printf("\033[0m");
|
||||
printf("Normal mode activated\n");
|
||||
return;
|
||||
} else {
|
||||
printf("Invalid arguement, type \"help\" for help\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp("generate_clam", args[0]) == 0) {
|
||||
printClamShell();
|
||||
return;
|
||||
}
|
||||
|
||||
// execute the external command
|
||||
execute(args);
|
||||
}
|
||||
|
||||
void printClamShell() {
|
||||
|
@ -1,3 +1,6 @@
|
||||
#ifndef CSHELL_H
|
||||
#define CSHELL_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -7,8 +10,6 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifndef CSHELL_H
|
||||
#define CSHELL_H
|
||||
#define INPUT_SIZE 1024
|
||||
#define VERSION 0.1f
|
||||
|
||||
|
@ -3,9 +3,9 @@
|
||||
|
||||
char* history[500];
|
||||
|
||||
void createHistoryDir() {};
|
||||
void initHistory() {};
|
||||
void writeHistory() {};
|
||||
void accessHistory() {};
|
||||
void createHistoryDir() {}; // create history in /home/user/cshell_history
|
||||
void initHistory() {}; // read history from file on start
|
||||
void writeHistory() {}; // write history at end of session
|
||||
void accessHistory() {}; // handle user up arrow key to history
|
||||
|
||||
#endif
|
@ -22,6 +22,8 @@ int main() {
|
||||
|
||||
parseInput(input, args);
|
||||
|
||||
// capture input here and add to history
|
||||
|
||||
handleCommand(args, &cwd, &trimmedCWD);
|
||||
}
|
||||
free(cwd);
|
||||
|
Loading…
x
Reference in New Issue
Block a user