Improved Windows support.
@@ -1,9 +1,7 @@
import streams, strutils, critbits, os import types, - parser, - ../vendor/linenoise - + parser proc raiseUndefined(msg: string) = raise MinUndefinedError(msg: msg)
@@ -1,9 +1,16 @@
import strutils import ../vendor/sgregex +type + InvalidRegexError = ref SystemError + +proc newRegex(pattern, mods: string): ptr srx_Context = + result = srx_Create(pattern, mods) + if result.isNil: + raise(InvalidRegexError(msg: "Invalid regular expression: \"$1\"" % pattern)) proc match*(str, pattern, mods: string): bool = - let r = srx_Create(pattern, mods) + let r = newRegex(pattern, mods) result = srx_Match(r, str, 0) == 1 discard srx_Destroy(r)@@ -11,7 +18,7 @@ proc match*(str, pattern: string): bool =
return match(str, pattern, "") proc search*(str, pattern, mods: string): seq[string] = - let r = srx_Create(pattern, mods) + let r = newRegex(pattern, mods) discard srx_Match(r, str, 0) == 1 let count = srx_GetCaptureCount(r) result = newSeq[string](count)@@ -26,7 +33,7 @@ proc search*(str, pattern: string): seq[string] =
return search(str, pattern, "") proc replace*(str, pattern, repl, mods: string): string = - var r = srx_Create(pattern, mods) + var r = newRegex(pattern, mods) result = $srx_Replace(r, str, repl) discard srx_Destroy(r)
@@ -91,5 +91,4 @@ MinRuntimeError* = ref object of SystemError
qVal*: seq[MinValue] proc isNotNil*[T](obj: T): bool = - return not obj.isNil - + return not obj.isNil
@@ -3,8 +3,7 @@ import
core/types, core/parser, core/interpreter, - core/utils, - vendor/linenoise + core/utils import lib/min_lang, lib/min_stack,@@ -17,13 +16,15 @@ lib/min_sys,
lib/min_crypto, lib/min_fs +const USE_LINENOISE* = true + +when USE_LINENOISE: + import + vendor/linenoise + const version* = "1.0.0-dev" var REPL = false var DEBUGGING = false - -const - USE_LINENOISE = true - const PRELUDE* = "lib/prelude.min".slurp.strip let usage* = " MiNiM v" & version & " - a tiny concatenative programming language" & """@@ -44,22 +45,26 @@
var CURRSCOPE*: ref MinScope -proc completionCallback*(str: cstring, completions: ptr linenoiseCompletions) {.cdecl.}= - var words = ($str).split(" ") - var w = if words.len > 0: words.pop else: "" - var sep = "" - if words.len > 0: - sep = " " - for s in CURRSCOPE.symbols.keys: - if startsWith(s, w): - linenoiseAddCompletion completions, words.join(" ") & sep & s +when USE_LINENOISE: + proc completionCallback*(str: cstring, completions: ptr linenoiseCompletions) {.cdecl.}= + var words = ($str).split(" ") + var w = if words.len > 0: words.pop else: "" + var sep = "" + if words.len > 0: + sep = " " + for s in CURRSCOPE.symbols.keys: + if startsWith(s, w): + linenoiseAddCompletion completions, words.join(" ") & sep & s proc prompt(s: string): string = - var res = linenoise(s) - if not res.isNil: - discard $linenoiseHistoryAdd(res) - return $res - + when USE_LINENOISE: + var res = linenoise(s) + if not res.isNil: + discard $linenoiseHistoryAdd(res) + return $res + when not(USE_LINENOISE): + stdout.write(s) + return stdin.readLine proc stdLib(i: In) = i.lang_module@@ -126,7 +131,6 @@ finally:
if i.stack.len > 0: let last = i.stack[i.stack.len - 1] let n = $i.stack.len - var output: string if last.isQuotation and last.qVal.len > 1: echo "{$1} -> (" % n for item in last.qVal:
@@ -1,6 +1,3 @@
-define:release -threads:on - # https://gist.github.com/Drakulix/9881160 amd64.windows.gcc.path = "/usr/local/mingw/bin" amd64.windows.gcc.exe = "x86_64-w64-mingw32-gcc"
@@ -1,4 +1,6 @@
+{.compile: "linenoise/libwin32fixes.c".} {.compile: "linenoise/liblinenoise.c".} + type linenoiseCompletions* = object len*: csize
@@ -1,22 +0,0 @@
-STD= -WARN= -Wall -Wno-comment -OPT= -Os - -CFLAGS= $(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) -LDFLAGS= $(LDFLAGS) -DEBUG= -g - -CC := $(CC) $(R_CFLAGS) -LD := $(CC) $(R_LDFLAGS) - - -linenoise.o: linenoise.h linenoise.c - -linenoise_example: linenoise.o example.o - $(LD) -o $@ $^ - -.c.o: - $(CC) -c $< - -clean: - rm -f linenoise_example *.o
@@ -1,47 +0,0 @@
-Extracted from MSOpenTech's [Windows port](https://github.com/MSOpenTech/redis) of redis. - -# Linenoise - -A minimal, zero-config, BSD licensed, readline replacement. - -News: linenoise is now part of [Android](http://android.git.kernel.org/?p=platform/system/core.git;a=tree;f=liblinenoise;h=56450eaed7f783760e5e6a5993ef75cde2e29dea;hb=HEAD Android)! - -## Can a line editing library be 20k lines of code? - -Line editing with some support for history is a really important feature for command line utilities. Instead of retyping almost the same stuff again and again it's just much better to hit the up arrow and edit on syntax errors, or in order to try a slightly different command. But apparently code dealing with terminals is some sort of Black Magic: readline is 30k lines of code, libedit 20k. Is it reasonable to link small utilities to huge libraries just to get a minimal support for line editing? - -So what usually happens is either: - - * Large programs with configure scripts disabling line editing if readline is not present in the system, or not supporting it at all since readline is GPL licensed and libedit (the BSD clone) is not as known and available as readline is (Readl world example of this problem: Tclsh). - * Smaller programs not using a configure script not supporting line editing at all (A problem we had with Redis-cli for instance). - -The result is a pollution of binaries without line editing support. - -So I spent more or less two hours doing a reality check resulting in this little library: is it *really* needed for a line editing library to be 20k lines of code? Apparently not, it is possibe to get a very small, zero configuration, trivial to embed library, that solves the problem. Smaller programs will just include this, supporing line editing out of the box. Larger programs may use this little library or just checking with configure if readline/libedit is available and resorting to linenoise if not. - -## Terminals, in 2010. - -Apparently almost every terminal you can happen to use today has some kind of support for VT100 alike escape sequences. So I tried to write a lib using just very basic VT100 features. The resulting library appears to work everywhere I tried to use it. - -Since it's so young I guess there are a few bugs, or the lib may not compile or work with some operating system, but it's a matter of a few weeks and eventually we'll get it right, and there will be no excuses for not shipping command line tools without built-in line editing support. - -The library is currently less than 400 lines of code. In order to use it in your project just look at the *example.c* file in the source distribution, it is trivial. Linenoise is BSD code, so you can use both in free software and commercial software. - -## Tested with... - - * Linux text only console ($TERM = linux) - * Linux KDE terminal application ($TERM = xterm) - * Linux xterm ($TERM = xterm) - * Mac OS X iTerm ($TERM = xterm) - * Mac OS X default Terminal.app ($TERM = xterm) - * OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen) - * IBM AIX 6.1 - * FreeBSD xterm ($TERM = xterm) - -Please test it everywhere you can and report back! - -## Let's push this forward! - -Please fork it and add something interesting and send me a pull request. What's especially interesting are fixes, new key bindings, completion. - -Send feedbacks to antirez at gmail
@@ -1,27 +0,0 @@
-#include <stdio.h> -#include <stdlib.h> -#include "linenoise.h" - - -void completion(const char *buf, linenoiseCompletions *lc) { - if (buf[0] == 'h') { - linenoiseAddCompletion(lc,"hello"); - linenoiseAddCompletion(lc,"hello there"); - } -} - -int main(void) { - char *line; - - linenoiseSetCompletionCallback(completion); - linenoiseHistoryLoad("history.txt"); /* Load the history at startup */ - while((line = linenoise("hello> ")) != NULL) { - if (line[0] != '\0') { - printf("echo: '%s'\n", line); - linenoiseHistoryAdd(line); - linenoiseHistorySave("history.txt"); /* Save every new entry */ - } - free(line); - } - return 0; -}
@@ -2,8 +2,9 @@ /* linenoise.c -- guerrilla line editing library against the idea that a
* line editing lib needs to be 20,000 lines of C code. * * You can find the latest source code at: - * * http://github.com/antirez/linenoise + * and the win32 port at: + * http://github.com/Choonster/lua-linenoise-windows * * Does a number of crazy assumptions that happen to be true in 99.9999% of * the 2010 UNIX computers around.@@ -47,7 +48,6 @@ *
* Todo list: * - Switch to gets() if $TERM is something we can't support. * - Filter bogus Ctrl+<char> combinations. - * - Win32 support * * Bloat: * - Completion?@@ -84,13 +84,11 @@ * Sequence: ESC [ 2 J
* Effect: clear the whole screen * */ - #ifndef _WIN32 -#include <termios.h> -#include <unistd.h> -#include <sys/ioctl.h> + #include <termios.h> + #include <unistd.h> + #include <sys/ioctl.h> #endif - #include <stdlib.h> #include <stdio.h> #include <errno.h>@@ -98,15 +96,16 @@ #include <string.h>
#include <stdlib.h> #include <sys/types.h> #include "linenoise.h" - +#define NOTUSED(V) ((void) V) #ifdef _WIN32 -#include <windows.h> + #include "win32fixes.h" #endif - #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 4096 +#ifndef _WIN32 static char *unsupported_term[] = {"dumb","cons25",NULL}; +#endif static linenoiseCompletionCallback *completionCallback = NULL; #ifndef _WIN32@@ -125,7 +124,6 @@ #ifdef _WIN32
#ifndef STDIN_FILENO #define STDIN_FILENO (_fileno(stdin)) #endif - HANDLE hOut; HANDLE hIn;@@ -300,6 +298,7 @@ /* put terminal in raw mode after flushing */
if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal; rawmode = 1; #else + NOTUSED(fd); if (!atexit_registered) { /* Init windows console handles only once */@@ -338,6 +337,7 @@ }
static void disableRawMode(int fd) { #ifdef _WIN32 + NOTUSED(fd); rawmode = 0; #else /* Don't even check the return value as it's too late. */@@ -405,6 +405,9 @@ snprintf(seq,64,"\x1b[0G\x1b[%dC", (int)(pos+plen));
if (write(fd,seq,strlen(seq)) == -1) return; #else + NOTUSED(seq); + NOTUSED(fd); + /* Get buffer console info */ if (!GetConsoleScreenBufferInfo(hOut, &b)) return; /* Erase Line */@@ -503,8 +506,6 @@ size_t pos = 0;
size_t len = 0; size_t cols = getColumns(); int history_index = 0; - size_t old_pos; - size_t diff; #ifdef _WIN32 DWORD foo; #endif@@ -708,7 +709,9 @@ linenoiseClearScreen();
refreshLine(fd,prompt,buf,len,pos,cols); break; case 23: /* ctrl+w, delete previous word */ - old_pos = pos; + { + size_t old_pos = pos; + size_t diff; while (pos > 0 && buf[pos-1] == ' ') pos--; while (pos > 0 && buf[pos-1] != ' ')@@ -718,6 +721,7 @@ memmove(&buf[pos], &buf[old_pos], len-old_pos+1);
len -= diff; refreshLine(fd,prompt,buf,len,pos,cols); break; + } } } return (int)len;@@ -806,18 +810,18 @@ return 1;
} int linenoiseHistorySetMaxLen(int len) { - char **new; + char **newC; if (len < 1) return 0; if (history) { int tocopy = history_len; - new = malloc(sizeof(char*)*len); - if (new == NULL) return 0; + newC = malloc(sizeof(char*)*len); + if (newC == NULL) return 0; if (len < tocopy) tocopy = len; - memcpy(new,history+(history_max_len-tocopy), sizeof(char*)*tocopy); + memcpy(newC,history+(history_max_len-tocopy), sizeof(char*)*tocopy); free(history); - history = new; + history = newC; } history_max_len = len; if (history_len > history_max_len)@@ -863,4 +867,4 @@ linenoiseHistoryAdd(buf);
} fclose(fp); return 0; -} +}
@@ -0,0 +1,549 @@
+/* +* Modified by Henry Rawas (henryr@schakra.com) +* - make it compatible with Visual Studio builds +* - added wstrtod to handle INF, NAN +* - added gettimeofday routine +* - modified rename to retry after failure +*/ + +#ifdef _WIN32 + +#include <process.h> +#include <stdlib.h> +#include <errno.h> +#ifndef FD_SETSIZE +# define FD_SETSIZE 16000 +#endif +//#include <winsock2.h> +#include <windows.h> +#include <signal.h> +#include <time.h> +#include <locale.h> +#include <math.h> +#include "win32fixes.h" + + +/* Redefined here to avoid redis.h so it can be used in other projects */ +#define NOTUSED(V) ((void) V) +#define THREAD_STACK_SIZE (1024*1024*4) + +/* Winsock requires library initialization on startup +int w32initWinSock(void) { + + WSADATA t_wsa; + WORD wVers; + int iError; + + wVers = MAKEWORD(2, 2); + iError = WSAStartup(wVers, &t_wsa); + + if(iError != NO_ERROR || LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2 ) { + return 0; + }; + + return 1; +} */ + +/* Behaves as posix, works without ifdefs, makes compiler happy */ +int sigaction(int sig, struct sigaction *in, struct sigaction *out) { + NOTUSED(out); + + /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction + * is used. Otherwise, sa_handler is used */ + if (in->sa_flags & SA_SIGINFO) + signal(sig, in->sa_sigaction); + else + signal(sig, in->sa_handler); + + return 0; +} + +/* Terminates process, implemented only for SIGKILL */ +int kill(pid_t pid, int sig) { + + if (sig == SIGKILL) { + + HANDLE h = OpenProcess(PROCESS_TERMINATE, 0, pid); + + if (!TerminateProcess(h, 127)) { + errno = EINVAL; /* GetLastError() */ + CloseHandle(h); + return -1; + }; + + CloseHandle(h); + return 0; + } else { + errno = EINVAL; + return -1; + }; +} + +/* Forced write to disk */ +int fsync (int fd) { + HANDLE h = (HANDLE) _get_osfhandle(fd); + DWORD err; + + if (h == INVALID_HANDLE_VALUE) { + errno = EBADF; + return -1; + } + + if (!FlushFileBuffers(h)) { + /* Windows error -> Unix */ + err = GetLastError(); + switch (err) { + case ERROR_INVALID_HANDLE: + errno = EINVAL; + break; + + default: + errno = EIO; + } + return -1; + } + + return 0; +} + +/* Missing wait3() implementation */ +pid_t wait3(int *stat_loc, int options, void *rusage) { + NOTUSED(stat_loc); + NOTUSED(options); + NOTUSED(rusage); + return (pid_t) waitpid((intptr_t) -1, 0, WAIT_FLAGS); +} + +/* Replace MS C rtl rand which is 15bit with 32 bit */ +int replace_random() { + unsigned int x=0; + if (RtlGenRandom == NULL) { + // load proc if not loaded + HMODULE lib = LoadLibraryA("advapi32.dll"); + RtlGenRandom = (RtlGenRandomFunc)GetProcAddress(lib, "SystemFunction036"); + if (RtlGenRandom == NULL) return 1; + } + RtlGenRandom(&x, sizeof(UINT_MAX)); + return (int)(x >> 1); +} + +/* BSD sockets compatibile replacement +int replace_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen) { + return (setsockopt)((SOCKET)socket, level, optname, (const char *)optval, optlen); +} +*/ + +/* set size with 64bit support */ +int replace_ftruncate(int fd, off64_t length) { + HANDLE h = (HANDLE) _get_osfhandle (fd); + LARGE_INTEGER l, o; + + if (h == INVALID_HANDLE_VALUE) { + errno = EBADF; + return -1; + } + + l.QuadPart = length; + + if (!SetFilePointerEx(h, l, &o, FILE_BEGIN)) return -1; + if (!SetEndOfFile(h)) return -1; + + return 0; +} + +/* Rename which works on Windows when file exists */ +int replace_rename(const char *src, const char *dst) { + /* anti-virus may lock file - error code 5. Retry until it works or get a different error */ + int retries = 50; + while (1) { + if (MoveFileEx(src, dst, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) { + return 0; + } else { + errno = GetLastError(); + if (errno != 5) break; + retries--; + if (retries == 0) { + retries = 50; + Sleep(10); + } + } + } + /* On error we will return generic error code without GetLastError() */ + return -1; +} + +#if 0 +/* Proxy structure to pass func and arg to thread */ +typedef struct thread_params +{ + void *(*func)(void *); + void * arg; +} thread_params; + +/* Proxy function by windows thread requirements */ +static unsigned __stdcall win32_proxy_threadproc(void *arg) { + + thread_params *p = (thread_params *) arg; + p->func(p->arg); + + /* Dealocate params */ + free(p); + + _endthreadex(0); + return 0; +} + +int pthread_create(pthread_t *thread, const void *unused, + void *(*start_routine)(void*), void *arg) { + HANDLE h; + thread_params *params = (thread_params *)malloc(sizeof(thread_params)); + NOTUSED(unused); + + params->func = start_routine; + params->arg = arg; +#ifndef STACK_SIZE_PARAM_IS_A_RESERVATION +# define STACK_SIZE_PARAM_IS_A_RESERVATION 4096 +#endif + + h =(HANDLE) _beginthreadex(NULL, /* Security not used */ + THREAD_STACK_SIZE, /* Set custom stack size */ + win32_proxy_threadproc, /* calls win32 stdcall proxy */ + params, /* real threadproc is passed as paremeter */ + STACK_SIZE_PARAM_IS_A_RESERVATION, /* reserve stack */ + thread /* returned thread id */ + ); + + if (!h) + return errno; + + CloseHandle(h); + return 0; +} + +/* Noop in windows */ +int pthread_detach (pthread_t thread) { + NOTUSED(thread); + return 0; /* noop */ + } + +pthread_t pthread_self(void) { + return GetCurrentThreadId(); +} + +int win32_pthread_join(pthread_t *thread, void **value_ptr) { + int result; + HANDLE h = OpenThread(SYNCHRONIZE, FALSE, *thread); + NOTUSED(value_ptr); + + switch (WaitForSingleObject(h, INFINITE)) { + case WAIT_OBJECT_0: + result = 0; + case WAIT_ABANDONED: + result = EINVAL; + default: + result = GetLastError(); + } + + CloseHandle(h); + return result; +} + +int pthread_cond_init(pthread_cond_t *cond, const void *unused) { + NOTUSED(unused); + cond->waiters = 0; + cond->was_broadcast = 0; + + InitializeCriticalSection(&cond->waiters_lock); + + cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); + if (!cond->sema) { + errno = GetLastError(); + return -1; + } + + cond->continue_broadcast = CreateEvent(NULL, /* security */ + FALSE, /* auto-reset */ + FALSE, /* not signaled */ + NULL); /* name */ + if (!cond->continue_broadcast) { + errno = GetLastError(); + return -1; + } + + return 0; +} + +int pthread_cond_destroy(pthread_cond_t *cond) { + CloseHandle(cond->sema); + CloseHandle(cond->continue_broadcast); + DeleteCriticalSection(&cond->waiters_lock); + return 0; +} + +int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { + int last_waiter; + + EnterCriticalSection(&cond->waiters_lock); + cond->waiters++; + LeaveCriticalSection(&cond->waiters_lock); + + /* + * Unlock external mutex and wait for signal. + * NOTE: we've held mutex locked long enough to increment + * waiters count above, so there's no problem with + * leaving mutex unlocked before we wait on semaphore. + */ + LeaveCriticalSection(mutex); + + /* let's wait - ignore return value */ + WaitForSingleObject(cond->sema, INFINITE); + + /* + * Decrease waiters count. If we are the last waiter, then we must + * notify the broadcasting thread that it can continue. + * But if we continued due to cond_signal, we do not have to do that + * because the signaling thread knows that only one waiter continued. + */ + EnterCriticalSection(&cond->waiters_lock); + cond->waiters--; + last_waiter = cond->was_broadcast && cond->waiters == 0; + LeaveCriticalSection(&cond->waiters_lock); + + if (last_waiter) { + /* + * cond_broadcast was issued while mutex was held. This means + * that all other waiters have continued, but are contending + * for the mutex at the end of this function because the + * broadcasting thread did not leave cond_broadcast, yet. + * (This is so that it can be sure that each waiter has + * consumed exactly one slice of the semaphor.) + * The last waiter must tell the broadcasting thread that it + * can go on. + */ + SetEvent(cond->continue_broadcast); + /* + * Now we go on to contend with all other waiters for + * the mutex. Auf in den Kampf! + */ + } + /* lock external mutex again */ + EnterCriticalSection(mutex); + + return 0; +} + +/* + * IMPORTANT: This implementation requires that pthread_cond_signal + * is called while the mutex is held that is used in the corresponding + * pthread_cond_wait calls! + */ +int pthread_cond_signal(pthread_cond_t *cond) { + int have_waiters; + + EnterCriticalSection(&cond->waiters_lock); + have_waiters = cond->waiters > 0; + LeaveCriticalSection(&cond->waiters_lock); + + /* + * Signal only when there are waiters + */ + if (have_waiters) + return ReleaseSemaphore(cond->sema, 1, NULL) ? + 0 : GetLastError(); + else + return 0; +} +#endif + +/* Redis forks to perform background writing */ +/* fork() on unix will split process in two */ +/* marking memory pages as Copy-On-Write so */ +/* child process will have data snapshot. */ +/* Windows has no support for fork(). */ +int fork(void) { + return -1; + } + +/* Redis CPU GetProcessTimes -> rusage */ +int getrusage(int who, struct rusage * r) { + + FILETIME starttime, exittime, kerneltime, usertime; + ULARGE_INTEGER li; + + if (r == NULL) { + errno = EFAULT; + return -1; + } + + memset(r, 0, sizeof(struct rusage)); + + if (who == RUSAGE_SELF) { + if (!GetProcessTimes(GetCurrentProcess(), + &starttime, + &exittime, + &kerneltime, + &usertime)) + { + errno = EFAULT; + return -1; + } + } + + if (who == RUSAGE_CHILDREN) { + /* Childless on windows */ + starttime.dwLowDateTime = 0; + starttime.dwHighDateTime = 0; + exittime.dwLowDateTime = 0; + exittime.dwHighDateTime = 0; + kerneltime.dwLowDateTime = 0; + kerneltime.dwHighDateTime = 0; + usertime.dwLowDateTime = 0; + usertime.dwHighDateTime = 0; + } + memcpy(&li, &kerneltime, sizeof(FILETIME)); + li.QuadPart /= 10L; + r->ru_stime.tv_sec = (long)(li.QuadPart / 1000000L); + r->ru_stime.tv_usec = (long)(li.QuadPart % 1000000L); + + memcpy(&li, &usertime, sizeof(FILETIME)); + li.QuadPart /= 10L; + r->ru_utime.tv_sec = (long)(li.QuadPart / 1000000L); + r->ru_utime.tv_usec = (long)(li.QuadPart % 1000000L); + + return 0; +} + +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 + +#if 0 +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tmpres /= 10; /*convert into microseconds*/ + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} +//#endif + +static _locale_t clocale = NULL; +double wstrtod(const char *nptr, char **eptr) { + double d; + char *leptr; +#if 0 + if (clocale == NULL) + clocale = _create_locale(LC_ALL, "C"); +#endif + d = _strtod_l(nptr, &leptr, clocale); + /* if 0, check if input was inf */ + if (d == 0 && nptr == leptr) { + int neg = 0; + while (isspace(*nptr)) + nptr++; + if (*nptr == '+') + nptr++; + else if (*nptr == '-') { + nptr++; + neg = 1; + } + + if (strnicmp("INF", nptr, 3) == 0) { + if (eptr != NULL) { + if (strnicmp("INFINITE", nptr, 8) == 0) + *eptr = (char*)(nptr + 8); + else + *eptr = (char*)(nptr + 3); + } + if (neg == 1) + return -HUGE_VAL; + else + return HUGE_VAL; + } else if (strnicmp("NAN", nptr, 3) == 0) { + if (eptr != NULL) + *eptr = (char*)(nptr + 3); + /* create a NaN : 0 * infinity*/ + d = HUGE_VAL; + return d * 0; + } + } + if (eptr != NULL) + *eptr = leptr; + return d; +} + +int strerror_r(int err, char* buf, size_t buflen) { + int size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + 0, + buf, + (DWORD)buflen, + NULL); + if (size == 0) { + char* strerr = strerror(err); + if (strlen(strerr) >= buflen) { + errno = ERANGE; + return -1; + } + strcpy(buf, strerr); + } + if (size > 2 && buf[size - 2] == '\r') { + /* remove extra CRLF */ + buf[size - 2] = '\0'; + } + return 0; +} + +char wsa_strerror_buf[128]; +char *wsa_strerror(int err) { + int size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + 0, + wsa_strerror_buf, + 128, + NULL); + if (size == 0) return strerror(err); + if (size > 2 && wsa_strerror_buf[size - 2] == '\r') { + /* remove extra CRLF */ + wsa_strerror_buf[size - 2] = '\0'; + } + return wsa_strerror_buf; +} +#endif //0 + +#endif //_WIN32
@@ -3,38 +3,43 @@ * line editing lib needs to be 20,000 lines of C code.
* * See linenoise.c for more information. * + * ------------------------------------------------------------------------ + * * Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com> * Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com> * * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright + * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * * Neither the name of Redis nor the names of its contributors may be used - * to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef __LINENOISE_H #define __LINENOISE_H + +#ifdef _WIN32 +# define off off_t +#endif typedef struct linenoiseCompletions { size_t len;@@ -52,4 +57,4 @@ int linenoiseHistorySave(char *filename);
int linenoiseHistoryLoad(char *filename); void linenoiseClearScreen(void); -#endif /* __LINENOISE_H */ +#endif /* __LINENOISE_H */
@@ -0,0 +1,327 @@
+/* +* Modified by Henry Rawas (henryr@schakra.com) +* - make it compatible with Visual Studio builds +* - added wstrtod to handle INF, NAN +* - added support for using IOCP with sockets +*/ + +#ifndef WIN32FIXES_H +#define WIN32FIXES_H + +#ifdef WIN32 +#ifndef _WIN32 +#define _WIN32 +#endif +#endif + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#define NOGDI +#define __USE_W32_SOCKETS + +#include <stdlib.h> +#include <stdio.h> +#include <io.h> +#include <signal.h> +#include <sys/types.h> +#ifndef FD_SETSIZE +#define FD_SETSIZE 16000 +#endif +#include <winsock2.h> /* setsocketopt */ +#include <ws2tcpip.h> +#include <windows.h> +#include <float.h> +#include <fcntl.h> /* _O_BINARY */ +#include <limits.h> /* INT_MAX */ +#include <process.h> +#include <sys/types.h> + +// mingw32 only +//#if !defined(__MINGW_SCANF_FORMAT) +//#include "fmacros.h" +//#endif + +#define fseeko fseeko64 +#define ftello ftello64 + +#define inline __inline + +#undef ftruncate +#define ftruncate replace_ftruncate +#ifndef off64_t +#define off64_t off_t +#endif + +int replace_ftruncate(int fd, off64_t length); + + +#define snprintf _snprintf +#define ftello64 _ftelli64 +#define fseeko64 _fseeki64 +#define strcasecmp _stricmp +#define strtoll _strtoi64 +#ifndef _MATH_H_ + #define isnan _isnan + #define isfinite _finite + #define isinf(x) (!_finite(x)) +#endif +#define lseek64 lseek +/* following defined to choose little endian byte order */ +#define __i386__ 1 +#if !defined(va_copy) +#define va_copy(d,s) d = (s) +#endif + +#define sleep(x) Sleep((x)*1000) + +#ifndef __RTL_GENRANDOM +#define __RTL_GENRANDOM 1 +typedef BOOLEAN (_stdcall* RtlGenRandomFunc)(void * RandomBuffer, ULONG RandomBufferLength); +#endif +RtlGenRandomFunc RtlGenRandom; + +#define random() (long)replace_random() +#define rand() replace_random() +int replace_random(); + +//#if !defined(ssize_t) +//typedef int ssize_t; +//#endif + +#if !defined(mode_t) +#define mode_t long +#endif + +#if !defined(u_int32_t) +/* sha1 */ +typedef unsigned __int32 u_int32_t; +#endif + +/* Redis calls usleep(1) to give thread some time +* Sleep(0) should do the same on windows +* In other cases, usleep is called with milisec resolution, +* which can be directly translated to winapi Sleep() */ +#undef usleep +#define usleep(x) (x == 1) ? Sleep(0) : Sleep((int)((x)/1000)) + +#define pipe(fds) _pipe(fds, 8192, _O_BINARY|_O_NOINHERIT) + +/* Processes */ +#define waitpid(pid,statusp,options) _cwait (statusp, pid, WAIT_CHILD) + +#define WAIT_T int +#define WTERMSIG(x) ((x) & 0xff) /* or: SIGABRT ?? */ +#define WCOREDUMP(x) 0 +#define WEXITSTATUS(x) (((x) >> 8) & 0xff) /* or: (x) ?? */ +#define WIFSIGNALED(x) (WTERMSIG (x) != 0) /* or: ((x) == 3) ?? */ +#define WIFEXITED(x) (WTERMSIG (x) == 0) /* or: ((x) != 3) ?? */ +#define WIFSTOPPED(x) 0 + +#define WNOHANG 1 + +/* file mapping */ +#define PROT_READ 1 +#define PROT_WRITE 2 + +#define MAP_FAILED (void *) -1 + +#define MAP_SHARED 1 +#define MAP_PRIVATE 2 + +/* rusage */ +#define RUSAGE_SELF 0 +#define RUSAGE_CHILDREN (-1) + +#ifndef _RUSAGE_T_ +#define _RUSAGE_T_ +struct rusage { + struct timeval ru_utime; /* user time used */ + struct timeval ru_stime; /* system time used */ +}; +#endif + +int getrusage(int who, struct rusage * rusage); + +/* Signals */ +#define SIGNULL 0 /* Null Check access to pid*/ +#define SIGHUP 1 /* Hangup Terminate; can be trapped*/ +#define SIGINT 2 /* Interrupt Terminate; can be trapped */ +#define SIGQUIT 3 /* Quit Terminate with core dump; can be trapped */ +#define SIGTRAP 5 +#define SIGBUS 7 +#define SIGKILL 9 /* Kill Forced termination; cannot be trapped */ +#define SIGPIPE 13 +#define SIGALRM 14 +#define SIGTERM 15 /* Terminate Terminate; can be trapped */ +#define SIGSTOP 17 +#define SIGTSTP 18 +#define SIGCONT 19 +#define SIGCHLD 20 +#define SIGTTIN 21 +#define SIGTTOU 22 +#define SIGABRT 22 +/* #define SIGSTOP 24 / * Pause the process; cannot be trapped */ +/* #define SIGTSTP 25 / * Terminal stop Pause the process; can be trapped */ +/* #define SIGCONT 26 */ +#define SIGWINCH 28 +#define SIGUSR1 30 +#define SIGUSR2 31 + +#define ucontext_t void* + +#define SA_NOCLDSTOP 0x00000001u +#define SA_NOCLDWAIT 0x00000002u +#define SA_SIGINFO 0x00000004u +#define SA_ONSTACK 0x08000000u +#define SA_RESTART 0x10000000u +#define SA_NODEFER 0x40000000u +#define SA_RESETHAND 0x80000000u +#define SA_NOMASK SA_NODEFER +#define SA_ONESHOT SA_RESETHAND +#define SA_RESTORER 0x04000000 + + +#define sigemptyset(pset) (*(pset) = 0) +#define sigfillset(pset) (*(pset) = (unsigned int)-1) +#define sigaddset(pset, num) (*(pset) |= (1L<<(num))) +#define sigdelset(pset, num) (*(pset) &= ~(1L<<(num))) +#define sigismember(pset, num) (*(pset) & (1L<<(num))) + +#ifndef SIG_SETMASK +#define SIG_SETMASK (0) +#define SIG_BLOCK (1) +#define SIG_UNBLOCK (2) +#endif /*SIG_SETMASK*/ + +typedef void (*__p_sig_fn_t)(int); + +//typedef int pid_t; + +#ifndef _SIGSET_T_ +#define _SIGSET_T_ +#ifdef _WIN64 +typedef unsigned long long _sigset_t; +#else +typedef unsigned long _sigset_t; +#endif +#endif /* _SIGSET_T_ */ +#ifndef _POSIX +# define sigset_t _sigset_t +#endif + +struct sigaction { + int sa_flags; + sigset_t sa_mask; + __p_sig_fn_t sa_handler; + __p_sig_fn_t sa_sigaction; +}; + +int sigaction(int sig, struct sigaction *in, struct sigaction *out); + +/* Sockets */ + +#ifndef ECONNRESET +#define ECONNRESET WSAECONNRESET +#endif + +#ifndef EINPROGRESS +#define EINPROGRESS WSAEINPROGRESS +#endif + +#ifndef ETIMEDOUT +#define ETIMEDOUT WSAETIMEDOUT +#endif + +#define setsockopt(a,b,c,d,e) replace_setsockopt(a,b,c,d,e) + +int replace_setsockopt(int socket, int level, int optname, + const void *optval, socklen_t optlen); + +#define rename(a,b) replace_rename(a,b) +int replace_rename(const char *src, const char *dest); + +//threads avoiding pthread.h + +#define pthread_mutex_t CRITICAL_SECTION +#define pthread_attr_t ssize_t + +#define pthread_mutex_init(a,b) (InitializeCriticalSectionAndSpinCount((a), 0x80000400),0) +#define pthread_mutex_destroy(a) DeleteCriticalSection((a)) +#define pthread_mutex_lock EnterCriticalSection +#define pthread_mutex_unlock LeaveCriticalSection + +#define pthread_equal(t1, t2) ((t1) == (t2)) + +#define pthread_attr_init(x) (*(x) = 0) +#define pthread_attr_getstacksize(x, y) (*(y) = *(x)) +#define pthread_attr_setstacksize(x, y) (*(x) = y) + +#define pthread_t u_int + +int pthread_create(pthread_t *thread, const void *unused, + void *(*start_routine)(void*), void *arg); + +pthread_t pthread_self(void); + +typedef struct { + CRITICAL_SECTION waiters_lock; + LONG waiters; + int was_broadcast; + HANDLE sema; + HANDLE continue_broadcast; +} pthread_cond_t; + +int pthread_cond_init(pthread_cond_t *cond, const void *unused); +int pthread_cond_destroy(pthread_cond_t *cond); +int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); +int pthread_cond_signal(pthread_cond_t *cond); + +int pthread_detach (pthread_t thread); + +/* Misc Unix -> Win32 */ +int kill(pid_t pid, int sig); +int fsync (int fd); +pid_t wait3(int *stat_loc, int options, void *rusage); + +int w32initWinSock(void); +/* int inet_aton(const char *cp_arg, struct in_addr *addr) */ + +/* redis-check-dump */ +//void *mmap(void *start, size_t length, int prot, int flags, int fd, off offset); +//int munmap(void *start, size_t length); + +int fork(void); +//int gettimeofday(struct timeval *tv, struct timezone *tz); + +/* strtod does not handle Inf and Nan +We need to do the check before calling strtod */ +#undef strtod +#define strtod(nptr, eptr) wstrtod((nptr), (eptr)) + +double wstrtod(const char *nptr, char **eptr); + + +/* structs and functions for using IOCP with windows sockets */ + +/* need callback on write complete. aeWinSendReq is used to pass parameters */ +typedef struct aeWinSendReq { + void *client; + void *data; + char *buf; + int len; +} aeWinSendReq; + + +int aeWinSocketAttach(int fd); +int aeWinSocketDetach(int fd, int shutd); +int aeWinReceiveDone(int fd); +int aeWinSocketSend(int fd, char *buf, int len, int flags, + void *eventLoop, void *client, void *data, void *proc); +int aeWinListen(SOCKET sock, int backlog); +int aeWinAccept(int fd, struct sockaddr *sa, socklen_t *len); + +int strerror_r(int err, char* buf, size_t buflen); +char *wsa_strerror(int err); + +#endif /* WIN32 */ +#endif /* WIN32FIXES_H */
@@ -18,19 +18,12 @@ proc RX_STRLENGTHFUNC*(str: string): int =
return str.len type - srx_Context* = object - -proc srx_DefaultMemFunc*(userdata: pointer, ptr1: pointer, size: csize): pointer = - discard cast[string](userdata) - if size > 0: - return realloc(ptr1, size) - dealloc(ptr1) - return nil + srx_Context* = pointer {.push importc.} proc srx_CreateExt*(str: cstring; strsize: csize; mods: cstring; errnpos: ptr cint; memfn: srx_MemFunc; memctx: pointer): ptr srx_Context template srx_Create*(str, mods: string): ptr srx_Context = - srx_CreateExt(str, RX_STRLENGTHFUNC(str), mods, nil, srx_DefaultMemFunc, nil) + srx_CreateExt(str, RX_STRLENGTHFUNC(str), mods, nil, nil, nil) proc srx_Destroy*(R: ptr srx_Context): cint proc srx_DumpToStdout*(R: ptr srx_Context)
@@ -829,9 +829,9 @@ mods++;
} } - if( !memfn ) + //if( !memfn ) // FC - Always using default memfn (problems on Windows). memfn = srx_DefaultMemFunc; - + R = (srx_Context*) memfn( memctx, NULL, sizeof(srx_Context) ); memset( R, 0, sizeof(*R) ); memset( cel, 0, sizeof(cel) );