refactored handleCommand() to be more modular

This commit is contained in:
bigsketti 2025-02-12 00:44:45 -05:00
parent b320d7e75d
commit e488abb906
5 changed files with 148 additions and 116 deletions

BIN
cshell

Binary file not shown.

View File

@ -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" #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) { void handleCommand(char **args, char **cwd, char **trimmedCWD) {
if (args[0] == NULL) { if (args[0] == NULL) {
@ -17,10 +27,42 @@ void handleCommand(char **args, char **cwd, char **trimmedCWD) {
} }
if (strcmp("exit", args[0]) == 0) { 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) {
if (strcmp("help", 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);
}
}
void cmd_help() {
printf("Welcome to cShell, my shitty terminal written in C\n\n"); printf("Welcome to cShell, my shitty terminal written in C\n\n");
printClamShell(); printClamShell();
printf( printf(
@ -40,15 +82,17 @@ void handleCommand(char **args, char **cwd, char **trimmedCWD) {
"\t\tevil mode: -e\n" "\t\tevil mode: -e\n"
"\t\thacker mode: -h\n\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); 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) { if (args[1] == NULL) {
fprintf(stderr, "Missing argument for command \"cd\"\n"); fprintf(stderr, "Missing argument for command \"cd\"\n");
} else { } else {
@ -60,53 +104,47 @@ void handleCommand(char **args, char **cwd, char **trimmedCWD) {
*trimmedCWD = trimCWD(*cwd); *trimmedCWD = trimCWD(*cwd);
} }
} }
return; }
}
if (strcmp("mkfile", args[0]) == 0) { void cmd_mkfile(char **args) {
FILE *file = fopen(args[1], "w"); FILE *file = fopen(args[1], "w");
if (file == NULL) { if (file == NULL) {
perror("Error making file"); perror("Error making file");
} else { } else {
printf("success\n"); printf("success\n");
} }
return; }
}
if (strcmp("mkdir", args[0]) == 0) { void cmd_mkdir(char **args) {
if (mkdir(args[1], 0777) != 0) { if (mkdir(args[1], 0777) != 0) {
perror("Error making directory"); perror("Error making directory");
} else { } else {
printf("success\n"); printf("success\n");
} }
return; }
}
if (strcmp("rmfile", args[0]) == 0) { void cmd_rmfile(char **args) {
if (remove(args[1]) != 0) { if (remove(args[1]) != 0) {
perror("Error removing file"); perror("Error removing file");
} else { } else {
printf("success\n"); printf("success\n");
} }
return; }
}
if (strcmp("rmdir", args[0]) == 0) { void cmd_rmdir(char **args) {
if (rmdir(args[1]) != 0) { if (rmdir(args[1]) != 0) {
perror("Error removing directory"); perror("Error removing directory");
} else { } else {
printf("success\n"); printf("success\n");
} }
return; }
}
if (strcmp("ls", args[0]) == 0) { void cmd_ls(char **args) {
const char *path = args[1] ? args[1] : "."; const char *path = args[1] ? args[1] : ".";
listDirectorys(path); listDirectorys(path);
return; }
}
if (strcmp("mode", args[0]) == 0) { void cmd_mode(char **args) {
if (strcmp("-h", args[1]) == 0) { if (strcmp("-h", args[1]) == 0) {
printf("\033[32m"); printf("\033[32m");
printf("Hacker mode activated\n"); printf("Hacker mode activated\n");
@ -123,15 +161,6 @@ void handleCommand(char **args, char **cwd, char **trimmedCWD) {
printf("Invalid arguement, type \"help\" for help\n"); printf("Invalid arguement, type \"help\" for help\n");
return; return;
} }
}
if (strcmp("generate_clam", args[0]) == 0) {
printClamShell();
return;
}
// execute the external command
execute(args);
} }
void printClamShell() { void printClamShell() {

View File

@ -1,3 +1,6 @@
#ifndef CSHELL_H
#define CSHELL_H
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -7,8 +10,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#ifndef CSHELL_H
#define CSHELL_H
#define INPUT_SIZE 1024 #define INPUT_SIZE 1024
#define VERSION 0.1f #define VERSION 0.1f

View File

@ -3,9 +3,9 @@
char* history[500]; char* history[500];
void createHistoryDir() {}; void createHistoryDir() {}; // create history in /home/user/cshell_history
void initHistory() {}; void initHistory() {}; // read history from file on start
void writeHistory() {}; void writeHistory() {}; // write history at end of session
void accessHistory() {}; void accessHistory() {}; // handle user up arrow key to history
#endif #endif

View File

@ -22,6 +22,8 @@ int main() {
parseInput(input, args); parseInput(input, args);
// capture input here and add to history
handleCommand(args, &cwd, &trimmedCWD); handleCommand(args, &cwd, &trimmedCWD);
} }
free(cwd); free(cwd);