From 0548b71a72fc1282a722b78e047f7e186d33ae4e Mon Sep 17 00:00:00 2001 From: Artur Tamborski Date: Sat, 1 Dec 2018 18:10:17 +0100 Subject: [PATCH] Add some sources --- src/common.h | 13 +++++++++++ src/icmd.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/icmd.h | 23 +++++++++++++++++++ src/main.c | 49 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 src/common.h create mode 100644 src/icmd.c create mode 100644 src/icmd.h create mode 100644 src/main.c diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..8f8af8c --- /dev/null +++ b/src/common.h @@ -0,0 +1,13 @@ +#ifndef COMMON_H +#define COMMON_H + + +#define CONCAT(x,y) _CONCAT(x,y) +#define _CONCAT(x,y) (x##y) + + +#define TOSTR(x) _TOSTR(x) +#define _TOSTR(x) (#x) + + +#endif /* COMMON_H */ diff --git a/src/icmd.c b/src/icmd.c new file mode 100644 index 0000000..311dfe9 --- /dev/null +++ b/src/icmd.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include "icmd.h" +#include "common.h" + + +#define ICMDS_SIZE \ + (sizeof(g_icmds) / sizeof(g_icmds[0])) + +#define ICMD(name) \ + { \ + .cmd = TOSTR(name), \ + .len = sizeof(TOSTR(name)), \ + .func = CONCAT(icmd_, name), \ + } + + +struct icmd g_icmds[] = +{ + ICMD(exit), + ICMD(cd), + ICMD(ls), +}; + + +int +call_icmds(char *line) +{ + size_t i; + + for (i = 0; i < ICMDS_SIZE; i++) + { + printf("cmp: %s : %s \n", g_icmds[i].cmd, line); + if (strncmp(g_icmds[i].cmd, line, g_icmds[i].len) == 0) + return g_icmds[i].func(line); + } + + return INT_MAX; +} + + +int +icmd_exit(char *line) +{ + printf("exiting with reason: %s", line); + exit(0); +} + + +int +icmd_cd(char *line) +{ + printf("I'm a cd! line: %s\n", line); + return 0; +} + + +int +icmd_ls(char *line) +{ + printf("I'm a ls! line: %s\n", line); + return 1; +} diff --git a/src/icmd.h b/src/icmd.h new file mode 100644 index 0000000..dc444a8 --- /dev/null +++ b/src/icmd.h @@ -0,0 +1,23 @@ +#ifndef ICMD_H +#define ICMD_H + + +struct icmd +{ + char *cmd; + int len; + int (*func)(char *); +}; + + +/* interface */ +int call_icmds(char *line); + + +/* internal commands as functions */ +int icmd_exit(char *line); +int icmd_cd(char *line); +int icmd_ls(char *line); + + +#endif /* ICMD_H */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..2abb9b0 --- /dev/null +++ b/src/main.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include "icmd.h" + + +/* should be enough */ +#define CMD_LINE_MAX 1024 +#define CMD_PATH_MAX 4096 + + +int +die(char *s) +{ + perror(s); + exit(-1); +} + + +int +main(int argc, char **argv) +{ + char line[CMD_LINE_MAX]; + char cwd[CMD_PATH_MAX]; + char *pline = line; + int ret; + + for (;;) + { + /* todo: does it really fail? */ + if (getcwd(cwd, sizeof(cwd)) == NULL) + die("getcwd()"); + + printf("[%s] ", cwd); + fgets(line, sizeof(line), stdin); + + ret = call_icmds(line); + if (ret == INT_MAX) + puts("no such internal command, going on..."); + else if (ret == 0) + puts("internal command succeded!"); + else + puts("internal command succeded!"); + } + + return 0; +}