cleaned up folder and added pure clam gen

This commit is contained in:
bigsketti 2025-02-09 18:01:47 -05:00
parent f6c7004bdd
commit 3627b76951
7 changed files with 164 additions and 118 deletions

BIN
cshell

Binary file not shown.

View File

@ -1,4 +1,7 @@
// This file exists because I hate myself too much to use cJSON
// I was gonna make a json parser from scratch but ive
// had my fill of writing lexers/parsers lately,
// might finish it eventually but for now itll be
// basic config files
#include <stdio.h>
#include <stdlib.h>

View File

@ -7,100 +7,11 @@
#include <sys/stat.h>
#include <sys/types.h>
#define INPUT_SIZE 1024
#define VERSION 0.1f
#include "cShell.h"
// TODO: research tab completion, very big quality of life feature
char *trimCWD(char *cwd) {
char *token = strtok(cwd, "/");
char *trimmedCWD = NULL;
while (token != NULL) {
trimmedCWD = token;
token = strtok(NULL, "/");
}
return trimmedCWD;
}
void execute(char **args) {
pid_t pid = fork();
if (pid == 0) {
execvp(args[0], args);
perror("execvp failed");
exit(EXIT_FAILURE);
} else if (pid > 0) {
wait(NULL);
} else {
perror("fork failed");
}
}
void parse_input(char *input, char **args) {
char *token = strtok(input, " ");
int i = 0;
while (token != NULL) {
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL;
}
char* getCWD() { // current working directory
char *cwd = malloc(1024);
if (cwd == NULL) {
perror("Malloc failed");
return NULL;
}
if (getcwd(cwd, 1024) != NULL) {
return cwd;
} else {
perror("Could not get current working directory");
free(cwd);
return NULL;
}
}
void listDirectorys(const char *path) {
struct dirent *entry;
DIR *dp = opendir(path);
if (dp == NULL) {
perror("Directory read failed");
return;
}
while ((entry = readdir(dp))) {
// skips hidden entries
if (entry->d_name[0] == '.') {
continue;
}
printf("%s\n", entry->d_name);
}
closedir(dp);
}
void printClamShell() {
printf(" ████ ██████ \n");
printf(" ████░░░░████░░░░░░██ \n");
printf(" ██░░░░░░░░░░░░██░░░░░░██ \n");
printf(" ██░░░░░░▒▒▒▒░░░░░░░░░░▒▒██\n");
printf("██░░░░░░▒▒░░░░░░░░░░░░██▒▒██\n");
printf("██░░░░▒▒░░░░░░░░░░░░░░░░██ \n");
printf("██░░▒▒░░░░░░░░░░░░▒▒░░░░░░██\n");
printf("██░░▒▒░░░░░░░░░░░░▒▒░░░░░░██\n");
printf(" ██░░░░░░░░░░░░▒▒░░░░░░▒▒██\n");
printf(" ██░░░░░░░░░░▒▒░░░░░░░░██ \n");
printf(" ██░░░░▒▒▒▒░░░░░░░░▒▒██ \n");
printf(" ████░░░░░░▒▒▒▒████ \n");
printf(" ██████████ \n");
}
void handle_command(char **args, char **cwd, char **trimmedCWD) {
void handleCommand(char **args, char **cwd, char **trimmedCWD) {
if (args[0] == NULL) {
return;
}
@ -117,6 +28,7 @@ void handle_command(char **args, char **cwd, char **trimmedCWD) {
"\texit\n"
"\thelp\n"
"\tversion\n"
"\tgenerate_clam\n"
"\tcd <dir name>\n"
"\tmkdir <dir name>\n"
"\trmdir <dir name>\n"
@ -213,34 +125,99 @@ void handle_command(char **args, char **cwd, char **trimmedCWD) {
}
}
if (strcmp("generate_clam", args[0]) == 0) {
printClamShell();
return;
}
// execute the external command
execute(args);
}
int main() {
char input[INPUT_SIZE];
char *args[100];
char user[100] = "defaultusr";
char *cwd = getCWD();
char *trimmedCWD = trimCWD(cwd);
while (1) {
if (cwd != NULL) {
printf("[cShell-%s] /%s $> ", user, trimmedCWD);
} else {
printf("[cShell-%s] $> ", user);
}
if (!fgets(input, sizeof(input), stdin)) {
break;
}
input[strcspn(input, "\n")] = '\0';
parse_input(input, args);
handle_command(args, &cwd, &trimmedCWD);
}
free(cwd);
return 0;
void printClamShell() {
printf(" ████ ██████ \n");
printf(" ████░░░░████░░░░░░██ \n");
printf(" ██░░░░░░░░░░░░██░░░░░░██ \n");
printf(" ██░░░░░░▒▒▒▒░░░░░░░░░░▒▒██\n");
printf("██░░░░░░▒▒░░░░░░░░░░░░██▒▒██\n");
printf("██░░░░▒▒░░░░░░░░░░░░░░░░██ \n");
printf("██░░▒▒░░░░░░░░░░░░▒▒░░░░░░██\n");
printf("██░░▒▒░░░░░░░░░░░░▒▒░░░░░░██\n");
printf(" ██░░░░░░░░░░░░▒▒░░░░░░▒▒██\n");
printf(" ██░░░░░░░░░░▒▒░░░░░░░░██ \n");
printf(" ██░░░░▒▒▒▒░░░░░░░░▒▒██ \n");
printf(" ████░░░░░░▒▒▒▒████ \n");
printf(" ██████████ \n");
}
char *trimCWD(char *cwd) {
char *token = strtok(cwd, "/");
char *trimmedCWD = NULL;
while (token != NULL) {
trimmedCWD = token;
token = strtok(NULL, "/");
}
return trimmedCWD;
}
void execute(char **args) {
pid_t pid = fork();
if (pid == 0) {
execvp(args[0], args);
perror("execvp failed");
exit(EXIT_FAILURE);
} else if (pid > 0) {
wait(NULL);
} else {
perror("fork failed");
}
}
void parseInput(char *input, char **args) {
char *token = strtok(input, " ");
int i = 0;
while (token != NULL) {
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL;
}
char* getCWD() { // current working directory
char *cwd = (char *)malloc(INPUT_SIZE);
if (cwd == NULL) {
perror("Malloc failed");
return NULL;
}
if (getcwd(cwd, 1024) != NULL) {
return cwd;
} else {
perror("Could not get current working directory");
free(cwd);
return NULL;
}
}
void listDirectorys(const char *path) {
struct dirent *entry;
DIR *dp = opendir(path);
if (dp == NULL) {
perror("Directory read failed");
return;
}
while ((entry = readdir(dp))) {
// skips hidden entries
if (entry->d_name[0] == '.') {
continue;
}
printf("%s\n", entry->d_name);
}
closedir(dp);
}

23
src/cShell.h Normal file
View File

@ -0,0 +1,23 @@
#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>
#ifndef CSHELL_H
#define CSHELL_H
#define INPUT_SIZE 1024
#define VERSION 0.1f
void handleCommand(char **args, char **cwd, char **trimmedCWD);
void printClamShell(void);
char *trimCWD(char *cwd);
void execute(char **args);
void parseInput(char *input, char **args);
char* getCWD(void);
void listDirectorys(const char *path);
#endif

3
src/history.c Normal file
View File

@ -0,0 +1,3 @@
#include "history.h"
// TODO: define functions in history.h

11
src/history.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef HISTORY_H
#define HISTORY_H
char* history[500];
void createHistoryDir() {};
void initHistory() {};
void writeHistory() {};
void accessHistory() {};
#endif

29
src/main.c Normal file
View File

@ -0,0 +1,29 @@
#include "cShell.h"
int main() {
char input[INPUT_SIZE];
char *args[100];
char user[100] = "defaultusr";
char *cwd = getCWD();
char *trimmedCWD = trimCWD(cwd);
while (1) {
if (cwd != NULL) {
printf("[cShell-%s] /%s $> ", user, trimmedCWD);
} else {
printf("[cShell-%s] $> ", user);
}
if (!fgets(input, sizeof(input), stdin)) {
break;
}
input[strcspn(input, "\n")] = '\0';
parseInput(input, args);
handleCommand(args, &cwd, &trimmedCWD);
}
free(cwd);
return 0;
}