Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Divide itmmorgue to different modules #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ WORKDIR='bin'
SRC='itmmorgue.c client.c config.c splash.c locale.c menu.c stuff.c'
SRC="$SRC windows.c area.c chat.c keyboard.c server.c protocol.c sysmsg.c"
SRC="$SRC connection.c levels.c tiles.c player.c event.c"
SRC="$SRC error.c time.c logger.c strutils.c ioutils.c"
HDR='itmmorgue.h client.h config.h default_config.h stuff.h windows.h'
HDR="$HDR area.h chat.h keyboard.h server.h protocol.h sysmsg.h"
HDR="$HDR connection.h levels.h tiles.h player.h event.h"
HDR="$HDR error.h time.h logger.h strutils.h ioutils.h"
LIB='trie/trie.o'
DEBUG=1
####################################################################
Expand Down
15 changes: 15 additions & 0 deletions src/error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "error.h"

void warn(const char * const msg) {
if (msg) {
fprintf(stderr, "%s\n", msg);
}
}

void panic(const char * const msg) {
endwin();

warn(msg);

exit(EXIT_FAILURE);
}
31 changes: 31 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef PANIC_H
#define PANIC_H

#include <stdlib.h>
#include <stdio.h>

#if defined(__FreeBSD__) || defined(__linux__)
#include <ncurses.h>
#include <sys/uio.h>
#else
#include <ncurses/ncurses.h>
#endif /* __FreeBSD__ || __linux__ */

void warn(const char * const msg);
void panic(const char * const msg);

#define warnf(fmt, ...) \
do { \
char ___buf[BUFSIZ]; \
snprintf(___buf, BUFSIZ, fmt, __VA_ARGS__); \
warn(___buf); \
} while(0)

#define panicf(fmt, ...) \
do { \
char ___buf[BUFSIZ]; \
snprintf(___buf, BUFSIZ, fmt, __VA_ARGS__); \
panic(___buf); \
} while(0)

#endif /* PANIC_H */
42 changes: 42 additions & 0 deletions src/ioutils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "ioutils.h"


int synchronized_readall(
pthread_mutex_t *mutex,
int fd,
void *buf,
size_t size
) {
if (NULL == mutex) {
panic("synchronized_readall: NULL mutex provided");
}

pthread_mutex_lock(mutex);
int retval = readall(fd, buf, size);
pthread_mutex_unlock(mutex);
return retval;
}

int readall(int fd, void *buf, size_t size) {
int rc = -1;
size_t got = 0;

if (size == 0) {
return 0;
}

while (got < size) {
if ((rc = read(fd, (char *)buf + got, size - got)) > 0) {
got += rc;
} else {
break;
}
}

if (got == size) {
return size;
}

return -1;
}

17 changes: 17 additions & 0 deletions src/ioutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef IO_UTILS_H
#define IO_UTILS_H

#include <unistd.h>
#include <pthread.h>
#include "error.h"

int readall(int fd, void *buf, size_t size);

int synchronized_readall(
pthread_mutex_t *mutex,
int fd,
void *buf,
size_t size
);

#endif /* IO_UTILS_H */
178 changes: 1 addition & 177 deletions src/itmmorgue.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,182 +3,6 @@
#include "server.h"
#include "client.h"

int server_only = 0;

void warn(char *msg) {
if (msg) {
fprintf(stderr, "%s\n", msg);
}
}

void panic(char *msg) {
endwin();

warn(msg);

exit(EXIT_FAILURE);
}

int strtoi(const char *nptr, char **endptr, int base) {
long lval = strtol(nptr, endptr, base);
if (lval < INT_MIN) {
errno = ERANGE;
return INT_MIN;
}
if (lval > INT_MAX) {
errno = ERANGE;
return INT_MAX;
}
return (int) lval;
}

size_t anystrunplen(char *str, size_t maxlen, char ** endp) {
int len = 0;

char *last_sym = str;

while (maxlen-- && *str) {
if ((*str++ & 0xC0) != 0x80) {
len++;
last_sym = str - 1;
} else {
maxlen++;
}
}

if (++maxlen == 0 && endp != NULL && *endp != NULL && *str) {
*endp = last_sym;
}

return len;
}

size_t anystrnplen(char *str, size_t maxlen, char ** endp) {
int len = 0;

while (maxlen-- && *str) {
if ((*str++ & 0xC0) != 0x80) {
len++;
}
}

if (++maxlen == 0 && endp != NULL && *endp != NULL && *str) {
*endp = str;
}

return len;
}

size_t anystrnlen(char *str, size_t maxlen) {
return anystrnplen(str, maxlen, NULL);
}

size_t anystrlen(char *str) {
return anystrnlen(str, UINT_MAX);
}

#ifdef __sun
size_t strnlen(const char *str, size_t maxlen) {
size_t rc = 0;

while (maxlen-- && *str++) {
rc++;
}

return rc;
}
#endif /* __sun */

int synchronized_readall(pthread_mutex_t *mutex, int fd, void *buf,
size_t size) {
if (NULL == mutex) {
panic("synchronized_readall: NULL mutex provided");
}

pthread_mutex_lock(mutex);
int retval = readall(fd, buf, size);
pthread_mutex_unlock(mutex);
return retval;
}

int readall(int fd, void *buf, size_t size) {
int rc = -1;
size_t got = 0;

if (size == 0) {
return 0;
}
while (got < size) {
if ((rc = read(fd, (char *)buf + got, size - got)) > 0) {
got += rc;
} else {
break;
}
}

if (got == size) {
return size;
}

return -1;
}

void log_init() {
char *log_file = CONF_SVAL("file_server_log");
if (! *log_file) {
return;
}

if ((log_fd = open(log_file, O_WRONLY | O_APPEND | O_CREAT, 0666)) < 0) {
panicf("Unable to open %s!", log_file);
}

logger(" ======= GAME STARTED ======= ");
}

void logger(char *str) {
if (log_fd < 0) {
return;
}

char buf[8192];
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) {
panic("Unable to get system time!");
}

int len = snprintf(buf, sizeof(buf), "%lu: %s\n", tv.tv_sec, str);
if (write(log_fd, buf, len) < 0) {
panic("Unable to write log output!");
}

if (server_only == 1) {
if (write(2, buf, len) < 0) {
panic("Unable to write log to stderr!");
}
}
}

unsigned long long systime() {
struct timeval tv;

if (gettimeofday(&tv, NULL) < 0) {
panic("Unable to get system time!");
}

return tv.tv_sec;
}

unsigned long long sysutime() {
struct timeval tv;

if (gettimeofday(&tv, NULL) < 0) {
panic("Unable to get system time!");
}

return tv.tv_sec * 1000000 + tv.tv_usec;
}

int main(int argc, char *argv[]) {
// TODO parse argv and run server / client

Expand All @@ -196,7 +20,7 @@ int main(int argc, char *argv[]) {
}

log_fd = -1;
log_init();
logger_init();

if (server_only == 0) {
client();
Expand Down
51 changes: 7 additions & 44 deletions src/itmmorgue.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,14 @@ enum colors {
#include "stuff.h"
#include "tiles.h"
#include "levels.h"
#include "config.h"
#include "error.h"
#include "ioutils.h"
#include "strutils.h"
#include "logger.h"

int client(void);

int log_fd;
int client(void);
void logger(char *str);
int synchronized_readall(pthread_mutex_t *mutex, int fd, void *buf,
size_t size);
int readall(int fd, void *buf, size_t size);

// Panic related definitions
void warn(char *msg);
void panic(char *msg);
#define warnf(fmt, ...) \
do { \
char ___buf[BUFSIZ]; \
snprintf(___buf, BUFSIZ, fmt, __VA_ARGS__); \
warn(___buf); \
} while(0)
#define panicf(fmt, ...) \
do { \
char ___buf[BUFSIZ]; \
snprintf(___buf, BUFSIZ, fmt, __VA_ARGS__); \
panic(___buf); \
} while(0)
#define loggerf(fmt, ...) \
do { \
char ___buf[BUFSIZ]; \
snprintf(___buf, BUFSIZ, fmt, __VA_ARGS__); \
logger(___buf); \
} while(0)

// Wrapper for strtol(3) for int numbers
int strtoi(const char *nptr, char **endptr, int base);

// strlen() alternatives for UTF-8 strings
size_t anystrlen(char *str);
size_t anystrnlen(char *str, size_t maxlen);
size_t anystrnplen(char *str, size_t maxlen, char ** endp);
size_t anystrunplen(char *str, size_t maxlen, char ** endp);
unsigned long long systime();
unsigned long long sysutime();
int server_only;
void player_move(player_move_t *move);
#ifdef __sun
size_t strnlen(const char *str, size_t maxlen);
#endif /* __sun */


#endif /* ITMMORGUE_H */
Loading