diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 53 | ||||
-rw-r--r-- | flake.nix | 5 | ||||
-rw-r--r-- | src/common/array.c (renamed from server/array.c) | 14 | ||||
-rw-r--r-- | src/common/better_string.c (renamed from server/better_string.c) | 4 | ||||
-rw-r--r-- | src/common/hash_table.c (renamed from server/hash_table.c) | 5 | ||||
-rw-r--r-- | src/common/helpers.c (renamed from server/helpers.c) | 4 | ||||
-rw-r--r-- | src/common/opcodes.c (renamed from server/opcodes.c) | 4 | ||||
-rw-r--r-- | src/common/tsv.c (renamed from server/tsv.c) | 6 | ||||
-rw-r--r-- | src/include/array.h (renamed from include/array.h) | 1 | ||||
-rw-r--r-- | src/include/better_string.h (renamed from include/better_string.h) | 1 | ||||
-rw-r--r-- | src/include/hash_table.h (renamed from include/hash_table.h) | 2 | ||||
-rw-r--r-- | src/include/helpers.h (renamed from include/helpers.h) | 2 | ||||
-rw-r--r-- | src/include/opcodes.h (renamed from include/opcodes.h) | 0 | ||||
-rw-r--r-- | src/include/protocol.h (renamed from include/protocol.h) | 6 | ||||
-rw-r--r-- | src/include/tsv.h (renamed from include/tsv.h) | 4 | ||||
-rw-r--r-- | src/msg/main.c (renamed from client/main.c) | 2 | ||||
-rw-r--r-- | src/ramen/main.c (renamed from server/main.c) | 46 |
18 files changed, 88 insertions, 73 deletions
@@ -1,4 +1,6 @@ obj/** +msg_obj/** +ramen_obj/** bin/** .cache/** result/** @@ -1,43 +1,54 @@ -SERVER_DIR := server -CLIENT_DIR := client -OBJ_DIR := server_obj -CLIENT_OBJ_DIR := client_obj +SRC_DIR := src/common +OBJ_DIR := obj +RAMEN_OBJ_DIR := ramen_obj +MSG_OBJ_DIR := msg_obj BIN_DIR := bin -EXE := $(BIN_DIR)/umami -CLIENT_EXE = $(BIN_DIR)/client +RAMEN_DIR := src/ramen +MSG_DIR := src/msg -SRC := $(wildcard $(SERVER_DIR)/*.c) -OBJ := $(SRC:$(SERVER_DIR)/%.c=$(OBJ_DIR)/%.o) +RAMEN_EXE := $(BIN_DIR)/ramen +MSG_EXE := $(BIN_DIR)/msg -CLIENT_SRC := $(wildcard $(CLIENT_DIR/*.c)) -CLIENT_OBJ := $(CLIENT_SRC:$(CLIENT_DIR)/%.c=$(OBJ_DIR)/%.o) - -CFLAGS := -Wall -Iinclude -lcrypto -lssl -LDFLAGS := -Llib -LDLIBS := -lm +SRC := $(wildcard $(SRC_DIR)/*.c) +OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) +CFLAGS := -Wall -lcrypto -lssl CFLAGS += $(NIX_CFLAGS_COMPILE) + +RAMEN_SRC := $(wildcard $(RAMEN_DIR)/*.c) +RAMEN_OBJ := $(RAMEN_SRC:$(RAMEN_DIR)/%.c=$(RAMEN_OBJ_DIR)/%.o) + +MSG_SRC := $(wildcard $(MSG_DIR)/*.c) +MSG_OBJ := $(MSG_SRC:$(MSG_DIR)/%.c=$(MSG_OBJ_DIR)/%.o) + +LDFLAGS := +LDLIBS := +LDFLAGS += $(NIX_LDFLAGS_COMPILE) + .PHONY: all clean -all: $(EXE) $(CLIENT_EXE) +all: $(RAMEN_EXE) $(MSG_EXE) -$(EXE): $(OBJ) | $(BIN_DIR) +$(RAMEN_EXE): $(OBJ) $(RAMEN_OBJ) | $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ -$(CLIENT_EXE): $(CLIENT_OBJ) | $(BIN_DIR) +$(RAMEN_OBJ_DIR)/%.o: $(RAMEN_DIR)/%.c | $(RAMEN_OBJ_DIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(MSG_EXE): $(OBJ) $(MSG_OBJ) | $(BIN_DIR) $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ -$(OBJ_DIR)/%.o: $(SERVER_DIR)/%.c | $(OBJ_DIR) +$(MSG_OBJ_DIR)/%.o: $(MSG_DIR)/%.c | $(MSG_OBJ_DIR) $(CC) $(CFLAGS) -c $< -o $@ -$(CLIENT_OBJ_DIR)/%.o: $(CLIENT_DIR)/%.c | $(CLIENT_OBJ_DIR) +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) $(CC) $(CFLAGS) -c $< -o $@ -$(BIN_DIR) $(OBJ_DIR): +$(BIN_DIR) $(OBJ_DIR) $(MSG_OBJ_DIR) $(RAMEN_OBJ_DIR): mkdir -p $@ clean: - @$(RM) -rv $(BIN_DIR) $(OBJ_DIR) result + @$(RM) -rv $(BIN_DIR) $(OBJ_DIR) $(MSG_OBJ_DIR) $(RAMEN_OBJ_DIR) result -include $(OBJ:.o=.d) @@ -23,9 +23,8 @@ buildPhase = "make"; installPhase = '' - mkdir -p $out/bin - cp ./bin/umami $out/bin/ - cp ./bin/client $out/bin/ + mkdir -p $out + cp -r bin/ $out/ ''; }; }; diff --git a/server/array.c b/src/common/array.c index 84ed938..4ebb67e 100644 --- a/server/array.c +++ b/src/common/array.c @@ -1,5 +1,6 @@ -#include <array.h> -#include <helpers.h> +#include "../include/array.h" +#include "../include/helpers.h" + #include <stdlib.h> array_t *init_array() { @@ -27,6 +28,15 @@ void *array_pop(array_t *a) { return retval; } +array_t *array_reverse(array_t *a) { + array_t *reverse = init_array(); + for (int i = 0; i < a->size; i ++) { + array_push(reverse, array_pop(a)); + } + array_free(a, nothing); + return reverse; +} + void array_free(void *x, void (*freefunc)(void *)) { array_t *a = (array_t *)x; for (int i = 0; i < a->size; i++) { diff --git a/server/better_string.c b/src/common/better_string.c index 4413f77..366dda6 100644 --- a/server/better_string.c +++ b/src/common/better_string.c @@ -1,5 +1,5 @@ -#include <better_string.h> -#include <helpers.h> +#include "../include/better_string.h" +#include "../include/helpers.h" #include <stdlib.h> #include <string.h> diff --git a/server/hash_table.c b/src/common/hash_table.c index cced194..2f66d45 100644 --- a/server/hash_table.c +++ b/src/common/hash_table.c @@ -1,6 +1,7 @@ -#include <hash_table.h> +#include "../include/hash_table.h" +#include "../include/helpers.h" + #include <stdlib.h> -#include <helpers.h> ht_t *init_ht(size_t size) { ht_t *ht = safe_calloc(1, sizeof(size)); diff --git a/server/helpers.c b/src/common/helpers.c index 8485dc6..4bf7301 100644 --- a/server/helpers.c +++ b/src/common/helpers.c @@ -1,4 +1,4 @@ -#include <helpers.h> +#include "../include/helpers.h" #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -32,3 +32,5 @@ void *safe_realloc(void *x, size_t size) { } void alloc_zero(void *ptr, size_t size) { memset(ptr, 0, size); } + +void nothing(void *_) {} diff --git a/server/opcodes.c b/src/common/opcodes.c index 8fc51f2..a489630 100644 --- a/server/opcodes.c +++ b/src/common/opcodes.c @@ -1,6 +1,6 @@ #include <stdlib.h> -#include <opcodes.h> -#include <helpers.h> +#include "../include/opcodes.h" +#include "../include/helpers.h" char *decode_client_opcode(int op) { diff --git a/server/tsv.c b/src/common/tsv.c index 9b1c2be..c06163b 100644 --- a/server/tsv.c +++ b/src/common/tsv.c @@ -1,9 +1,9 @@ #include <stdlib.h> #include <stdbool.h> -#include <tsv.h> -#include <helpers.h> -#include <better_string.h> +#include "../include/tsv.h" +#include "../include/helpers.h" +#include "../include/better_string.h" tsv_t *init_tsv(char *source) { tsv_t *tsv = safe_calloc(1, sizeof(tsv_t *)); diff --git a/include/array.h b/src/include/array.h index 948dad2..d3004e3 100644 --- a/include/array.h +++ b/src/include/array.h @@ -20,4 +20,5 @@ void *array_del(array_t *a, unsigned int ind); void array_free(void *a, void (*freefunc)(void *)); +array_t *array_reverse(array_t *a); #endif diff --git a/include/better_string.h b/src/include/better_string.h index a5f9954..41c0cfd 100644 --- a/include/better_string.h +++ b/src/include/better_string.h @@ -3,6 +3,7 @@ #include <stdlib.h> #define DEFAULT_STR_SIZE 10 + typedef struct { char *buf; size_t len; diff --git a/include/hash_table.h b/src/include/hash_table.h index 357e38a..ea21e37 100644 --- a/include/hash_table.h +++ b/src/include/hash_table.h @@ -1,7 +1,7 @@ #ifndef HASH_TABLE_H #define HASH_TABLE_H -#include <array.h> +#include <stdlib.h> #define DEFAULT_HT_SIZE 500 typedef struct PAIR_STRUCT { diff --git a/include/helpers.h b/src/include/helpers.h index 276b590..5d46909 100644 --- a/include/helpers.h +++ b/src/include/helpers.h @@ -12,4 +12,6 @@ void *safe_calloc(unsigned int i, size_t size); void *safe_realloc(void *x, size_t size); void alloc_zero(void *ptr, size_t size); + +void nothing(void *); #endif diff --git a/include/opcodes.h b/src/include/opcodes.h index ff8400f..ff8400f 100644 --- a/include/opcodes.h +++ b/src/include/opcodes.h diff --git a/include/protocol.h b/src/include/protocol.h index 16ba818..204a507 100644 --- a/include/protocol.h +++ b/src/include/protocol.h @@ -4,9 +4,9 @@ #include <time.h> #include <stdbool.h> -#include <array.h> -#include <better_string.h> -#include <hash_table.h> +#include "array.h" +#include "better_string.h" +#include "hash_table.h" #include <openssl/sha.h> #define MAX_OP_LEN 10 diff --git a/include/tsv.h b/src/include/tsv.h index 5f6a9f6..57bb70a 100644 --- a/include/tsv.h +++ b/src/include/tsv.h @@ -1,7 +1,7 @@ #ifndef TSV_H #define TSV_H -#include <better_string.h> +#include "better_string.h" typedef struct { char *source; @@ -11,6 +11,6 @@ typedef struct { tsv_t *init_tsv(char *source); -string_t *next_tsv(tsv_t *tsv); +string_t *tsv_next(tsv_t *tsv); #endif diff --git a/client/main.c b/src/msg/main.c index 3587a00..c13422d 100644 --- a/client/main.c +++ b/src/msg/main.c @@ -1,6 +1,6 @@ #include <stdio.h> int main(int argc, char **argv) { - printf("HELLO WORLD\n"); + printf("hello world!\n"); return 0; } diff --git a/server/main.c b/src/ramen/main.c index 7a17966..e98b452 100644 --- a/server/main.c +++ b/src/ramen/main.c @@ -12,12 +12,13 @@ #include <sys/time.h> #include <unistd.h> -#include <array.h> -#include <better_string.h> -#include <hash_table.h> -#include <helpers.h> -#include <opcodes.h> -#include <protocol.h> +#include "../include/tsv.h" +#include "../include/array.h" +#include "../include/better_string.h" +#include "../include/hash_table.h" +#include "../include/helpers.h" +#include "../include/opcodes.h" +#include "../include/protocol.h" int PORT = DEFAULT_PORT; int nfds = 1; @@ -36,34 +37,19 @@ void handle_sigint(int sig) { exit(0); } -/* modifying function: modifies `in` to point to new location after opcode */ -int parse_op(char *in) { - char buf[MAX_OP_LEN]; - buf[0] = '\0'; - - char cur; - cur = in[0]; - - bool found = false; - for (int i = 0; i < 11; i++) { - if (cur == '\t') { - found = true; - in++; - break; - } else if (cur == '\0') { - break; - } - - buf[i] = in[0]; - in++; - cur = in[0]; +array_t *parse_args(char *buf) { + tsv_t *tsv = init_tsv(buf); + string_t *s = tsv_next(tsv); + array_t *a = init_array(); + while (s) { + array_push(a, s); + s = tsv_next(tsv); } - return found ? encode_client_opcode(buf) : CO_UNRECOGNIZED; + a = array_reverse(a); + return a; } -array_t *parse_args(char *buf) { return NULL; } - void set_non_blocking(int sock) { int flags = fcntl(sock, F_GETFL, 0); int code = fcntl(sock, F_SETFL, flags | O_NONBLOCK); |