diff --git a/ANT.cfg b/ANT.cfg new file mode 100755 index 0000000..07cfe85 --- /dev/null +++ b/ANT.cfg @@ -0,0 +1,4 @@ +name = PINgvin +hthn = true +nhost = ANT +welcome = false diff --git a/ANT.cfg~ b/ANT.cfg~ new file mode 100755 index 0000000..c6717bd --- /dev/null +++ b/ANT.cfg~ @@ -0,0 +1,4 @@ +name = PINgvin +hthn = false +nhost = ANT +welcome = true \ No newline at end of file diff --git a/ant_bin/antf.c~ b/ant_bin/antf.c~ new file mode 100755 index 0000000..b0b654e --- /dev/null +++ b/ant_bin/antf.c~ @@ -0,0 +1,33 @@ +#include "moroz.h" +#include "ANTbin.h" + +int ANTbin_antf(char **args) { + const char *TXT_COLOR = TXT_BLUE; + + for (int i = 1; args[i] != NULL; i++) { + if (strcmp(args[i], "-blue") == 0) { + TXT_COLOR = TXT_CYAN; + } + if (strcmp(args[i], "-red") == 0) { + TXT_COLOR = TXT_RED; + } + if (strcmp(args[i], "-blue") == 0) { + TXT_COLOR = TXT_CYAN; + } + } + + (void)args; + prt(stderr, "- ANTFetch 0.6\n"); + prt(stderr, "%s .--. \n", TXT_COLOR); + prt(stderr, "%s |o_o | \n", TXT_COLOR); + prt(stderr, "%s |:_/ | \n", TXT_COLOR); + prt(stderr, "%s // \\\\ \n", TXT_COLOR); + prt(stderr, "%s (| |) \n", TXT_COLOR); + prt(stderr, "%s /'\\_ _/`\\ \n", TXT_COLOR); + prt(stderr, "%s \\___)=(___/ \n", TXT_COLOR); + prt(stderr, TXT_RESET "- ANTarktida Shell "ANT_VER"\n"); + prt(stderr, "- MoRoZ "ANT_BIN_VER"\n"); + prt(stderr, "- GNU/Linux "); + system("uname -r"); + return 0; +} diff --git a/ant_bin/cfg_master.c b/ant_bin/cfg_master.c new file mode 100755 index 0000000..e004f24 --- /dev/null +++ b/ant_bin/cfg_master.c @@ -0,0 +1,55 @@ +#include "moroz.h" +#include "ANTbin.h" + +int ANTbin_cfg(char **args) { + for (int i = 1; args[i] != NULL; i++) { + if (strcmp(args[i], "-r") == 0 || strcmp(args[i], "--read") == 0) { + system("cat ANT.cfg"); + prt(stderr, "\n"); + } + if (strcmp(args[i], "-c") == 0 || strcmp(args[i], "--check") == 0) { + char *name = MRZ_RFILE("ANT.cfg", "name"); + if (name != NULL && name[0] != '\0') { + prt(stdout, "Found name in config: %s\n", name); + } else { + prt(stderr, TXT_RED "No name in config's !\n" TXT_RESET); + prt(stderr, "Default name is \"pin\"\n"); + } + + char *nthn = MRZ_RFILE("ANT.cfg", "hthn"); + if (nthn != NULL && strcmp(nthn, "true") == 0) { + prt(stdout, "hthn (host to host name) this: %s\n", nthn); + } else if (nthn != NULL && strcmp(nthn, "false") == 0) { + char *nhost = MRZ_RFILE("ANT.cfg", "nhost"); + if (nhost != NULL && nhost[0] != '\0') { + prt(stdout, "Found custom name host in config is: %s\n", nhost); + } else { + prt(stderr, TXT_RED "No hthn or nhost in config !\n" TXT_RESET); + return 1; + } + } else if (nthn != NULL && nthn[0] != '\0') { + prt(stdout, TXT_YELLOW "What is \"%s\" ???\n" TXT_RESET, nthn); + + char *nhost = MRZ_RFILE("ANT.cfg", "nhost"); + if (nhost != NULL && nhost[0] != '\0') { + prt(stdout, "Found custom name host in config is: %s\n", nhost); + } else { + prt(stderr, TXT_RED "No hthn or nhost in config !\n" TXT_RESET); + return 1; + } + } + + char *welcome = MRZ_RFILE("ANT.cfg", "welcome"); + if (welcome != NULL && strcmp(welcome, "true") == 0) { + prt(stdout, "Welcome is true: %s\n", welcome); + } else if (welcome != NULL && strcmp(welcome, "false") == 0) { + prt(stdout, "Welcome is false: %s\n", welcome); + } else if (welcome != NULL && welcome[0] != '\0') { + prt(stdout, TXT_YELLOW "What is \"%s\" ???\n" TXT_RESET, welcome); + } else { + prt(stderr, TXT_RED "No welcome (false or true) in config !\n" TXT_RESET); + } + } + } + return 0; +} diff --git a/ant_bin/cfg_master.c~ b/ant_bin/cfg_master.c~ new file mode 100755 index 0000000..c713a16 --- /dev/null +++ b/ant_bin/cfg_master.c~ @@ -0,0 +1,23 @@ +#include "moroz.h" +#include "ANTbin.h" + +int ANTbin_cfg(char **args) { + for (int i = 1; args[i] != NULL; i++) { + if (strcmp(args[i], "-r") == 0 || strcmp(args[i], "--read") == 0) { + system("cat ANT.cfg"); + prt(stderr, "\n"); + } + if (strcmp(args[i], "-c") == 0 || strcmp(args[i], "--check") == 0) { + char *name = MRZ_RFILE("ANT.cfg", "name"); + if (name) { + + } else { + prt(stderr, TXT_RED "No name in config !\n" TXT_RESET); + prt(stderr, "Default name \"pin\"\n"); + } + } + if (strcmp(args[i], "-c") == 0 || strcmp(args[i], "--check") == 0) { + system("cat ANT.cfg"); + } + } +} diff --git a/ant_bin/ptest.c~ b/ant_bin/ptest.c~ new file mode 100755 index 0000000..487139e --- /dev/null +++ b/ant_bin/ptest.c~ @@ -0,0 +1,43 @@ +#include "ANTbin.h" +#include "moroz.h" + +int ANTbin_ptest(char **args) { + (void)args; + prt(stderr, TXT_RED "TEST\n"); + prt(stderr, TXT_BLUE "TEST\n"); + prt(stderr, TXT_YELLOW "TEST\n"); + prt(stderr, TXT_GREEN "TEST\n\n"); + + MRZ_CFILE("test.mrz"); + MRZ_CDIR("test"); + + MRZ_PFILE("test.mrz", "test = ptest.txt") + + char *val = MRZ_RFILE("test.txt", "test"); + + if (val && strcmp(val, "ptest!") == 0) { + prt(stderr, TXT_GREEN "TEST! (Config read success)\n" TXT_RESET); + } else { + prt(stderr, TXT_RED "ERORR: Tag not found or value mismatch\n" TXT_RESET); + return 1; + } + + bool byrn = "true"; + + while (byrn) { + prt(stderr, "rm \"test.mrz\" file ?"); + prt(stderr, "Y/N = "); + char yrn = getchar(); + if (yrn == y || yrn == Y) { + system("rm test.mrz"); + byrn = "false"; + } if else (yrn == n || yrn == N) { + byrn = "false"; + } else { + continue; + } + } + + prt(stderr, "\n"); + return 0; +} diff --git a/include/ANTbin.h~ b/include/ANTbin.h~ new file mode 100755 index 0000000..ecc6d3a --- /dev/null +++ b/include/ANTbin.h~ @@ -0,0 +1,21 @@ +#ifndef ANTBIN_H +#define ANTBIN_H + +typedef int (*ANTbin_fn)(char **args); + +typedef struct { + const char *name; + ANTbin_fn fn; +} ANTbin_t; + +extern const ANTbin_t ANTbin[]; + +ANTbin_fn ANTbin_find(const char *name); + +int ANTbin_ver(char **args); +int ANTbin_help(char **args); +int ANTbin_antf(char **args); +int ANTbin_ptest(char **args); + + +#endif diff --git a/include/config.h~ b/include/config.h~ new file mode 100755 index 0000000..bea1010 --- /dev/null +++ b/include/config.h~ @@ -0,0 +1,15 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define ANT_VER "PRE-Alpha 0.6.0" +#define ANT_BIN_VER "0.4" +#define MAX_ARGS 64 + +extern char ANT_NAME[64]; +extern char ANT_NHOST[64]; + +#include + +bool init(void); + +#endif diff --git a/include/moroz.h~ b/include/moroz.h~ new file mode 100755 index 0000000..5106398 --- /dev/null +++ b/include/moroz.h~ @@ -0,0 +1,56 @@ +#ifndef MOROZ_H +#define MOROZ_H + +#define MOROZ_VER "0.4" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +#define TXT_RESET "\033[0m" /* Сброс всех стилей */ + +#define TXT_BOLD "\033[1m" /* Жирный или же Яркий */ +#define TXT_DIM "\033[2m" /* Тусклый */ +#define TXT_ITALIC "\033[3m" /* Курсив */ +#define TXT_UNDERLINE "\033[4m" /* Подчёркнутый */ +#define TXT_BLINK "\033[5m" /* Мигающий */ +#define TXT_REVERSE "\033[7m" /* Инверсия */ +#define TXT_HIDDEN "\033[8m" /* Скрытый */ +#define TXT_STRIKE "\033[9m" /* Перечёркнутый */ + +#define TXT_RED "\033[0;31m" /* Красный */ +#define TXT_GREEN "\033[0;32m" /* Зеленный */ +#define TXT_YELLOW "\033[0;33m" /* Желтый */ +#define TXT_BLUE "\033[0;34m" /* Синий */ +#define TXT_PURPLE "\033[0;35m" /* Фиолетовый */ +#define TXT_CYAN "\033[0;36m" /* Сине-зеленый */ +#define TXT_WHITE "\033[0;37m" /* Белый */ + +#define TXT_BG_RED "\033[41m" /* Фоновый красный */ +#define TXT_BG_GREEN "\033[42m" /* Фоновый зеленный */ +#define TXT_BG_YELLOW "\033[43m" /* Фоновый желтый */ +#define TXT_BG_BLUE "\033[44m" /* Фоновый синий */ +#define TXT_BG_PURPLE "\033[45m" /* Фоновый фиолетовый */ +#define TXT_BG_CYAN "\033[46m" /* Фоновый сине-зеленый */ +#define TXT_BG_WHITE "\033[47m" /* Фоновый белый */ + +#define prt(stream, ...) fprintf(stream, __VA_ARGS__) + +void MRZ_CFILE(const char *filename); +#define MRZ_CFILE(filename) MRZ_CFILE(filename) + +void MRZ_CDIR(const char *dirname); +#define MRZ_CDIR(dirname) MRZ_CDIR(dirname) + +char *MRZ_RFILE(const char *path, const char *tag); + +#endif diff --git a/src/ANTbin.c~ b/src/ANTbin.c~ new file mode 100755 index 0000000..131d14f --- /dev/null +++ b/src/ANTbin.c~ @@ -0,0 +1,17 @@ +#include +#include "ANTbin.h" + +const ANTbin_t ANTbin[] = { + { "ver", ANTbin_ver }, + { "help", ANTbin_help }, + { "antf", ANTbin_antf }, + { "ptest", ANTbin_ptest }, + { NULL, NULL } +}; + +ANTbin_fn ANTbin_find(const char *name) { + for (int i = 0; ANTbin[i].name != NULL; i++) + if (strcmp(ANTbin[i].name, name) == 0) + return ANTbin[i].fn; + return NULL; +} diff --git a/src/init.c~ b/src/init.c~ new file mode 100755 index 0000000..dd73fc9 --- /dev/null +++ b/src/init.c~ @@ -0,0 +1,40 @@ +#include "config.h" +#include "moroz.h" + +char ANT_NAME[64] = "pin"; +char ANT_NHOST[64] = "ant_sh"; + +bool init(void) { + char *name = MRZ_RFILE("ANT.cfg", "name"); + if (name) { + snprintf(ANT_NAME, sizeof(ANT_NAME), "%s", name); + } else { + prt(stderr, TXT_RED "No name in config !\n" TXT_RESET); + prt(stderr, "Default name \"pin\"\n"); + } + + char *HTHN = MRZ_RFILE("ANT.cfg", "hthn"); + if (HTHN && strcmp(HTHN, "true") == 0) { + char *sys_user = getenv("USER"); + if (sys_user) { + snprintf(ANT_NHOST, sizeof(ANT_NHOST), "%s", sys_user); + } else { + snprintf(ANT_NHOST, sizeof(ANT_NHOST), "%s", "pin"); + } + } else if (HTHN && strcmp(HTHN, "false") == 0) { + char *nhost = MRZ_RFILE("ANT.cfg", "nhost"); + if (nhost) { + snprintf(ANT_NHOST, sizeof(ANT_NHOST), "%s", nhost); + } else { + prt(stderr, TXT_RED "No custom name host in config !\n" TXT_RESET); + prt(stderr, "Default name \"ant_sh\"\n"); + } + } + + char *welcome = MRZ_RFILE("ANT.cfg", "welcome"); + if (welcome && strcmp(welcome, "true") == 0) { + prt(stderr, "Dobro Pozalivat v " TXT_BLUE "ANT Shell !" TXT_RESET " %s\n", ANT_NAME); + } + + return true; +} diff --git a/src/main.c~ b/src/main.c~ new file mode 100755 index 0000000..2c002f8 --- /dev/null +++ b/src/main.c~ @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "ANTbin.h" + +extern char ANT_NAME[64]; +extern char ANT_NHOST[64]; + +char **parse_line(char *line) { + char **args = malloc(MAX_ARGS * sizeof(char *)); + + if (args == NULL) { + perror("ant_sh: malloc failed"); + return NULL; + } + + int i = 0; + char *token = strtok(line, " \t\n"); + while (token != NULL && i < MAX_ARGS - 1) { + args[i++] = token; + token = strtok(NULL, " \t\n"); + } + args[i] = NULL; + return args; +} + +int main(void) { + char prompt[128]; + + init(void); + + snprintf(prompt, sizeof(prompt), "\033[1;36m%s@%s -> \033[0m", ANT_NAME, ANT_NHOST); + + while (1) { + + char *line = readline(prompt); + + if (line == NULL) { + fprintf(stderr, "\033[1;36m\nDosvidanie...\033[0m\n"); + break; + } + + if (*line == '\0') { + free(line); + continue; + } + + add_history(line); + + char **args = parse_line(line); + if (args == NULL || args[0] == NULL) { + if (args) free(args); + continue; + } + + if (strcmp(args[0], "sys") == 0) { + if (args[1]) system(args[1]); + free(args); + free(line); + continue; + } + + ANTbin_fn fn = ANTbin_find(args[0]); + if (fn) { + fn(args); + free(args); + free(line); + continue; + } + + if (strcmp(args[0], "exit") == 0) { + fprintf(stderr, "\033[1;36mDosvidanie...\033[0m\n"); + free(args); + free(line); + break; + } + + pid_t pid = fork(); + if (pid == 0) { + execvp(args[0], args); + perror("ant_sh"); + _exit(127); + } else if (pid > 0) { + int status; + waitpid(pid, &status, 0); + } + + free(args); + free(line); + } + + return 0; +} diff --git a/src/moroz/moroz.c~ b/src/moroz/moroz.c~ new file mode 100755 index 0000000..0faf48c --- /dev/null +++ b/src/moroz/moroz.c~ @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "moroz.h" + +void MRZ_CFILE(const char *filename) { + int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644); + + if (fd != -1) { + close(fd); + } else { + fprintf(stderr, TXT_RED "ERROR !\n" TXT_RESET); + } +} + +void MRZ_CDIR(const char *dirname) { + if (mkdir(dirname, 0755) != 0) { + fprintf(stderr, TXT_RED "ERROR !\n" TXT_RESET); + } +} + +char *MRZ_RFILE(const char *path, const char *tag) { + FILE *file = fopen(path, "r"); + if (!file) return NULL; + + char line[128]; + char key[64], val[64]; + static char result[64]; + result[0] = '\0'; + + while (fgets(line, sizeof(line), file)) { + if (sscanf(line, "%63s = %63s", key, val) == 2) { + if (strcmp(key, tag) == 0) { + strncpy(result, val, sizeof(result) - 1); + result[sizeof(result) - 1] = '\0'; + fclose(file); + return result; + } + } + } + + fclose(file); + return NULL; +} +