mirror of
https://github.com/bigsketti/cshell.git
synced 2025-06-06 21:04:46 +00:00
refactored handleCommand() to be more modular
This commit is contained in:
parent
b320d7e75d
commit
e488abb906
103
src/cShell.c
103
src/cShell.c
@ -1,15 +1,25 @@
|
||||
#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) {
|
||||
@ -17,10 +27,42 @@ void handleCommand(char **args, char **cwd, char **trimmedCWD) {
|
||||
}
|
||||
|
||||
if (strcmp("exit", args[0]) == 0) {
|
||||
exit(0);
|
||||
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();
|
||||
return;
|
||||
} else {
|
||||
execute(args);
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp("help", args[0]) == 0) {
|
||||
void cmd_help() {
|
||||
printf("Welcome to cShell, my shitty terminal written in C\n\n");
|
||||
printClamShell();
|
||||
printf(
|
||||
@ -40,15 +82,17 @@ void handleCommand(char **args, char **cwd, char **trimmedCWD) {
|
||||
"\t\tevil mode: -e\n"
|
||||
"\t\thacker mode: -h\n\n"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("version", args[0]) == 0) {
|
||||
void cmd_exit() {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void cmd_version() {
|
||||
printf("cShell version: %f\n", VERSION);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("cd", args[0]) == 0) {
|
||||
void cmd_cd(char **args, char **cwd, char **trimmedCWD) {
|
||||
if (args[1] == NULL) {
|
||||
fprintf(stderr, "Missing argument for command \"cd\"\n");
|
||||
} else {
|
||||
@ -60,53 +104,47 @@ void handleCommand(char **args, char **cwd, char **trimmedCWD) {
|
||||
*trimmedCWD = trimCWD(*cwd);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("mkfile", args[0]) == 0) {
|
||||
void cmd_mkfile(char **args) {
|
||||
FILE *file = fopen(args[1], "w");
|
||||
if (file == NULL) {
|
||||
perror("Error making file");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("mkdir", args[0]) == 0) {
|
||||
void cmd_mkdir(char **args) {
|
||||
if (mkdir(args[1], 0777) != 0) {
|
||||
perror("Error making directory");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("rmfile", args[0]) == 0) {
|
||||
void cmd_rmfile(char **args) {
|
||||
if (remove(args[1]) != 0) {
|
||||
perror("Error removing file");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("rmdir", args[0]) == 0) {
|
||||
void cmd_rmdir(char **args) {
|
||||
if (rmdir(args[1]) != 0) {
|
||||
perror("Error removing directory");
|
||||
} else {
|
||||
printf("success\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("ls", args[0]) == 0) {
|
||||
void cmd_ls(char **args) {
|
||||
const char *path = args[1] ? args[1] : ".";
|
||||
listDirectorys(path);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp("mode", args[0]) == 0) {
|
||||
void cmd_mode(char **args) {
|
||||
if (strcmp("-h", args[1]) == 0) {
|
||||
printf("\033[32m");
|
||||
printf("Hacker mode activated\n");
|
||||
@ -125,15 +163,6 @@ void handleCommand(char **args, char **cwd, char **trimmedCWD) {
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp("generate_clam", args[0]) == 0) {
|
||||
printClamShell();
|
||||
return;
|
||||
}
|
||||
|
||||
// execute the external command
|
||||
execute(args);
|
||||
}
|
||||
|
||||
void printClamShell() {
|
||||
printf(" ████ ██████ \n");
|
||||
printf(" ████░░░░████░░░░░░██ \n");
|
||||
|
@ -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