diff options
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/io.c | 55 | ||||
-rw-r--r-- | src/io.h | 9 | ||||
-rw-r--r-- | src/log.c | 37 | ||||
-rw-r--r-- | src/log.h | 8 | ||||
-rw-r--r-- | src/mysqlerl.c | 87 |
6 files changed, 113 insertions, 85 deletions
diff --git a/src/Makefile b/src/Makefile index 4224443..5a9287d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,7 +10,7 @@ PRIVDIR = ../priv BEAMDIR = ../ebin BINS = $(PRIVDIR)/mysqlerl $(BEAMDIR)/mysqlerl.app -MYSQLERLOBJS = mysqlerl.o +MYSQLERLOBJS = io.o log.o mysqlerl.o BEAMS = mysqlerl.beam mysqlerl_app.beam mysqlerl_connection.beam \ mysqlerl_connection_sup.beam LIBS = -lmysqlclient diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..e255f61 --- /dev/null +++ b/src/io.c @@ -0,0 +1,55 @@ +#include "io.h" +#include "log.h" + +#include <errno.h> +#include <sys/types.h> +#include <sys/uio.h> +#include <unistd.h> + +int +restartable_read(unsigned char *buf, size_t buflen) +{ + ssize_t rc, readb; + + rc = 0; + READLOOP: + while (rc < buflen) { + readb = read(STDIN_FILENO, buf + rc, buflen - rc); + if (readb == -1) { + if (errno == EAGAIN || errno == EINTR) + goto READLOOP; + + return -1; + } else if (readb == 0) { + logmsg("ERROR: EOF trying to read additional %d bytes from " + "standard input", buflen - rc); + return -1; + } + + rc += readb; + } + + return rc; +} + +int +restartable_write(const unsigned char *buf, size_t buflen) +{ + ssize_t rc, wroteb; + + rc = 0; + WRITELOOP: + while (rc < buflen) { + wroteb = write(STDOUT_FILENO, buf + rc, buflen - rc); + if (wroteb == -1) { + if (errno == EAGAIN || errno == EINTR) + goto WRITELOOP; + + return -1; + } + + rc += wroteb; + } + + return rc; +} diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..0db889b --- /dev/null +++ b/src/io.h @@ -0,0 +1,9 @@ +#ifndef _IO_H +#define _IO_H + +#include <sys/types.h> + +int restartable_read(unsigned char *buf, size_t buflen); +int restartable_write(const unsigned char *buf, size_t buflen); + +#endif diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..76ba469 --- /dev/null +++ b/src/log.c @@ -0,0 +1,37 @@ +#include "log.h" + +#include <stdio.h> +#include <stdarg.h> + +const char *LOGPATH = "/tmp/mysqlerl.log"; +static FILE *logfile = NULL; + +void +openlog() +{ + logfile = fopen(LOGPATH, "a"); +} + +void +closelog() +{ + fclose(logfile); +} + +void +logmsg(const char *format, ...) +{ + FILE *out = logfile; + va_list args; + + if (logfile == NULL) + out = stderr; + + va_start(args, format); + (void)vfprintf(out, format, args); + (void)fprintf(out, "\n"); + va_end(args); + + fflush(out); +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..d09ca00 --- /dev/null +++ b/src/log.h @@ -0,0 +1,8 @@ +#ifndef _LOG_H +#define _LOG_H + +void openlog(); +void closelog(); +void logmsg(const char *format, ...); + +#endif diff --git a/src/mysqlerl.c b/src/mysqlerl.c index 9266baa..b0bbc87 100644 --- a/src/mysqlerl.c +++ b/src/mysqlerl.c @@ -4,20 +4,16 @@ * Copyright (C) 2008, Brian Cully <bjc@kublai.com> */ +#include "io.h" +#include "log.h" + #include <erl_interface.h> #include <ei.h> #include <mysql.h> #include <errno.h> -#include <stdio.h> -#include <stdarg.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> -#include <unistd.h> - -const char *LOGPATH = "/tmp/mysqlerl.log"; -static FILE *logfile = NULL; const char *QUERY_MSG = "sql_query"; @@ -29,83 +25,6 @@ struct msg { }; typedef struct msg msg_t; -void -openlog() -{ - logfile = fopen(LOGPATH, "a"); -} - -void -closelog() -{ - fclose(logfile); -} - -void -logmsg(const char *format, ...) -{ - FILE *out = logfile; - va_list args; - - if (logfile == NULL) - out = stderr; - - va_start(args, format); - (void)vfprintf(out, format, args); - (void)fprintf(out, "\n"); - va_end(args); - - fflush(out); -} - -int -restartable_read(unsigned char *buf, size_t buflen) -{ - ssize_t rc, readb; - - rc = 0; - READLOOP: - while (rc < buflen) { - readb = read(STDIN_FILENO, buf + rc, buflen - rc); - if (readb == -1) { - if (errno == EAGAIN || errno == EINTR) - goto READLOOP; - - return -1; - } else if (readb == 0) { - logmsg("ERROR: EOF trying to read additional %d bytes from " - "standard input", buflen - rc); - return -1; - } - - rc += readb; - } - - return rc; -} - -int -restartable_write(const unsigned char *buf, size_t buflen) -{ - ssize_t rc, wroteb; - - rc = 0; - WRITELOOP: - while (rc < buflen) { - wroteb = write(STDOUT_FILENO, buf + rc, buflen - rc); - if (wroteb == -1) { - if (errno == EAGAIN || errno == EINTR) - goto WRITELOOP; - - return -1; - } - - rc += wroteb; - } - - return rc; -} - msg_t * read_msg() { |