~skiqqy/baal

69cb5c1f2440d13973cc3ea124587144979a8e15 — Stephen Cochrane 2 years ago 164ec07
First "Version"

* Setup versioning, using semver semantics
* Setup long opts, --help and --version
* Small improvment to server, instead of dropping con when thread isnt
  available, wait until thread becomes available, TODO. add a timeout
5 files changed, 53 insertions(+), 14 deletions(-)

M Makefile
M README.md
M include/baal.h
M src/baal.c
M src/baalserver.c
M Makefile => Makefile +10 -1
@@ 1,18 1,27 @@
# @Author Stephen Skiqqy Cochrane
# @License GPL

# semvar
VERSION=0.1.0-alpha+build
COPYRIGHT=Copyright (C) 2021 Stephen Cochrane

-include config.mk

CC=clang
FLAGS=-Wall -Wextra -pedantic $(DEBUG) $(LOGGING)
FLAGS=-Wall -Wextra -pedantic $(DEBUG) $(LOGGING) -DVERSION='"$(VERSION)"' -DCOPYRIGHT='"$(COPYRIGHT)"'
PREFIX=/usr/local
BINARY=baal
LDLIBS=-lcrypto -lpthread
SEMVAR_RX=^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$$# Perl regex to check semvar
.PHONY: test

all: baal

semvar:

init:
	# Check semvar
	@echo '$(VERSION)' | grep -P '$(SEMVAR_RX)' || { echo 'ERROR: Invalid semvar $(VERSION)' && exit 1; }
	mkdir -p bin
	-@[ ! -f bin/libcjson.so ] && ln -s ../libs/cJSON/build/libcjson.so bin/libcjson.so \
		|| echo "bin/libcjson.so symlink already exists"

M README.md => README.md +7 -1
@@ 19,7 19,12 @@

> Simply Invoke
```
$ . dev && make # Sets up env vars and builds
$ make
```

> dev'ing
```
$ . dev # Set your shell up to do baal dev
```

### Dependencies


@@ 27,6 32,7 @@ $ . dev && make # Sets up env vars and builds
| lib   | Usage               |
| ----- | ------------------- |
| [openssl](https://www.openssl.org/docs/man1.0.2/man3/SHA256.html) | Handle Encryption |
| [cJSON](https://github.com/DaveGamble/cJSON/tree/master)   | Parse JSON |

### Installing


M include/baal.h => include/baal.h +9 -0
@@ 28,6 28,15 @@ enum commmands {
	COM_LEN, // This must be the final element in the enum.
};

// So that my compiler wont complain
#ifndef VERSION
#define VERSION ""
#endif

#ifndef COPYRIGHT
#define COPYRIGHT ""
#endif

// Debug prints
#ifdef DEBUG
	#define dprintf(str, ...) printf("DEBUG: "); printf(str, __VA_ARGS__)

M src/baal.c => src/baal.c +24 -3
@@ 19,7 19,7 @@ void
usage(int ret)
{
	// Again, this is just for readability, this could be a single printf
	printf("\n  baal ~ Simple jmail server/client\n");
	printf("\n  baal ~ Simple email server/client %s\n", VERSION);
	printf("\n  \tUsage:\n");
	printf("\n  \t   $ baal [options] COMMAND [ARGS...]\n");
	printf("\n  \tOptions:\n");


@@ 36,9 36,14 @@ usage(int ret)
int
main(int argc, char* argv[])
{
	int opt;
	int opt, opt_index = 0;
	char buff[256] = "";
	int (*commands[COM_LEN]) (int argc, char **argv); // Array of function pointers
	struct option long_opts[] = {
		{"help",    no_argument, 0, 'h'},
		{"version", no_argument, 0,  0 },
		{0,         0,           0,  0 }
	};
	commands[COM_SERVER] = server; // See src/baalserver.c
	commands[COM_CLIENT] = client; // See src/baalclient.c



@@ 46,8 51,24 @@ main(int argc, char* argv[])
	serv.port        = -1; // -1 implies not yet set
	serv.max_clients = -1; // likewise

	while ((opt = getopt(argc, argv, ":hp:u:w:")) != -1) {
	// Optarg parsing
	for (;;) {
		opt = getopt_long(argc, argv, ":hp:u:w:", long_opts, &opt_index);
		if (opt == -1) break;

		switch (opt) {
			case 0:
				if (!strcmp("version", long_opts[opt_index].name)) {
					// This can be a single printf, it is just for readability
					printf("baal %s\n", VERSION);
					printf("%s\n", COPYRIGHT); // Copyright
					printf("This is free software; see the source for ");
					printf("copying conditions.  There is NO \nwarranty; not ");
					printf("even for MERCHANTABILITY or FITNESS FOR A ");
					printf("PARTICULAR PURPOSE.\n");
					return EXIT_SUCCESS;
				}
				break;
			case 'h':
				usage(0);
				break;

M src/baalserver.c => src/baalserver.c +3 -9
@@ 465,15 465,9 @@ server(int argc, char **argv)
		arg->client = client;
		arg->id = -1;

		arg->thread = fetch_thread(&arg->id);
		if (arg->id == -1) {
			// TODO. keep trying till we get a thread.
			// TEMPORTARY, STFU CASKD //
			free(arg);
			close(client);
			continue;
			///////////////////////////
		}
		do {
			arg->thread = fetch_thread(&arg->id); // Keep trying until we get a thread
		} while (arg->id == -1);

		binfo("Creating worker thread.", NULL);
		pthread_create(arg->thread, NULL, worker, (void *) arg);