summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stef@thewalter.net>2003-09-20 07:12:49 +0000
committerStef Walter <stef@thewalter.net>2003-09-20 07:12:49 +0000
commitb49d8ebefe9b10c53a6a09ad564e22111b7b25c6 (patch)
tree1d5dd4abc38170a7bc106dabbc59b915017222f0
parent1cda9ebbd62916c7c2136722597a86c583e1ecf6 (diff)
Initial Import
-rw-r--r--AUTHORS2
-rw-r--r--BUGS9
-rw-r--r--COPYING14
-rw-r--r--ChangeLog63
-rw-r--r--INSTALL9
-rw-r--r--Makefile.am3
-rw-r--r--NEWS1
-rw-r--r--README3
-rw-r--r--common/Makefile.am2
-rw-r--r--common/binfile.c334
-rw-r--r--common/binfile.h110
-rw-r--r--common/compat.c458
-rw-r--r--common/compat.h177
-rw-r--r--common/repfile.c113
-rw-r--r--common/repfile.h52
-rw-r--r--common/usuals.h61
-rw-r--r--common/xstring.c104
-rw-r--r--common/xstring.h70
-rw-r--r--config.win32.h217
-rw-r--r--configure.in58
-rw-r--r--doc/Makefile.am3
-rw-r--r--doc/language_v2.htm359
-rw-r--r--doc/pcre_man.txt1206
-rw-r--r--lib/Makefile.am9
-rw-r--r--lib/compile.c2337
-rw-r--r--lib/execute.c1715
-rw-r--r--lib/execute.h179
-rw-r--r--lib/ops.h349
-rw-r--r--lib/priv.h95
-rw-r--r--lib/rep.h107
-rw-r--r--lib/rlib.c591
-rw-r--r--lib/rlib.dsp140
-rw-r--r--lib/rlib.h261
-rw-r--r--rep.dsw92
-rw-r--r--src/Makefile.am13
-rw-r--r--src/file.c197
-rw-r--r--src/file.h85
-rw-r--r--src/rep.195
-rw-r--r--src/rep.c703
-rw-r--r--src/rep.dsp114
-rw-r--r--src/repc.173
-rw-r--r--src/repc.c192
-rw-r--r--src/repc.dsp110
-rw-r--r--stamp-h1
-rw-r--r--stamp-h.in1
-rw-r--r--stamp-h11
-rw-r--r--win32/Makefile.am2
-rw-r--r--win32/common/Makefile.am2
-rw-r--r--win32/common/atlprsht.h915
-rw-r--r--win32/common/atlwinhk.h151
-rw-r--r--win32/common/droplet.cpp226
-rw-r--r--win32/common/droplet.h98
-rw-r--r--win32/common/errutil.cpp106
-rw-r--r--win32/common/errutil.h32
-rw-r--r--win32/common/mystring.h536
-rw-r--r--win32/common/rep.icobin0 -> 766 bytes
-rw-r--r--win32/common/rliberr.h136
-rw-r--r--win32/common/rliberr.mc106
-rw-r--r--win32/droplet/Makefile.am2
-rw-r--r--win32/droplet/droplet.dsp223
-rw-r--r--win32/droplet/droplet.rc197
-rw-r--r--win32/droplet/dropletmain.cpp76
-rw-r--r--win32/droplet/progressdlg.cpp211
-rw-r--r--win32/droplet/progressdlg.h92
-rw-r--r--win32/droplet/replace.cpp438
-rw-r--r--win32/droplet/replace.h73
-rw-r--r--win32/droplet/resource.h22
-rw-r--r--win32/droplet/rliberr.h136
-rw-r--r--win32/droplet/rliberr.rc2
-rw-r--r--win32/droplet/stdafx.cpp31
-rw-r--r--win32/droplet/stdafx.h39
-rw-r--r--win32/droplet/temp.cmpbin0 -> 186 bytes
-rw-r--r--win32/droplet/temp.rep24
-rw-r--r--win32/makedrop/MSG00001.binbin0 -> 512 bytes
-rw-r--r--win32/makedrop/Makefile.am2
-rw-r--r--win32/makedrop/dropsheet.cpp301
-rw-r--r--win32/makedrop/dropsheet.h72
-rw-r--r--win32/makedrop/makedrop.cpp74
-rw-r--r--win32/makedrop/makedrop.dsp203
-rw-r--r--win32/makedrop/makedrop.h1
-rw-r--r--win32/makedrop/makedrop.rc275
-rw-r--r--win32/makedrop/processpage.cpp239
-rw-r--r--win32/makedrop/processpage.h75
-rw-r--r--win32/makedrop/resource.h32
-rw-r--r--win32/makedrop/rliberr.h136
-rw-r--r--win32/makedrop/rliberr.rc2
-rw-r--r--win32/makedrop/settingspage.cpp100
-rw-r--r--win32/makedrop/settingspage.h65
-rw-r--r--win32/makedrop/stdafx.cpp27
-rw-r--r--win32/makedrop/stdafx.h40
-rw-r--r--win32/pcre/include/pcre.h113
-rw-r--r--win32/pcre/include/pcreposix.h88
-rw-r--r--win32/pcre/lib/libpcre-bcc.def21
-rw-r--r--win32/pcre/lib/libpcre-bcc.libbin0 -> 4096 bytes
-rw-r--r--win32/pcre/lib/libpcre.def20
-rw-r--r--win32/pcre/lib/libpcre.la32
-rw-r--r--win32/pcre/lib/libpcre.libbin0 -> 5116 bytes
-rw-r--r--win32/pcre/lib/libpcreposix.la32
-rw-r--r--win32/pcre/manifest/pcre-3.9-lib.mft13
-rw-r--r--win32/pcre/manifest/pcre-3.9-lib.ver2
100 files changed, 16729 insertions, 0 deletions
diff --git a/AUTHORS b/AUTHORS
new file mode 100644
index 0000000..e26a5d9
--- /dev/null
+++ b/AUTHORS
@@ -0,0 +1,2 @@
+nielsen@memberwebs.com
+stef0x77@yahoo.com
diff --git a/BUGS b/BUGS
new file mode 100644
index 0000000..7a5a562
--- /dev/null
+++ b/BUGS
@@ -0,0 +1,9 @@
+REP TODOS AND BUGS
+- Code is dependant on a little endian architecture. Wouldn't
+ be too much work to change it around.
+- stdin and stdout piping hang on FreeBSD
+- droplets are limited by command line length on win32
+ (which is shorter than my ...)
+- Tag matching (if you don't know what it is, don't worry)
+ doesn't work reliably anymore.
+- Internal structures use linear lookups. Should use hash or btree \ No newline at end of file
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..cf2037b
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,14 @@
+LICENSE
+This software is in the public domain.
+
+The software is provided "as is", without warranty of any kind,
+express or implied, including but not limited to the warranties
+of merchantability, fitness for a particular purpose, and
+noninfringement. In no event shall the author(s) be liable for any
+claim, damages, or other liability, whether in an action of
+contract, tort, or otherwise, arising from, out of, or in connection
+with the software or the use or other dealings in the software.
+
+SUPPORT
+Send bug reports to: <nielsen@memberwebs.com>
+
diff --git a/ChangeLog b/ChangeLog
new file mode 100644
index 0000000..17fb089
--- /dev/null
+++ b/ChangeLog
@@ -0,0 +1,63 @@
+Version 2.3.1b
+ - Use reallocf instead of realloc
+ - Unbuffered mode works with nonseekable files (stdin)
+ - Added the "message" rep command
+ - Fixed buffer/locking issue
+ - Fixed errors in the "binfile" tagged file format
+ - Added support for Unicode BOM tags on rep scripts:
+ o UTF-8: is discarded and treated as normal text
+ o UCS2 and UCS4: error message that this encoding unsupported
+ - Added high level file functions to rlib
+
+Version 2.3b
+ - Switched to PCRE instead of a hacked up regex
+ - Added droplet support for Win32
+ - Fixed bug that in which rep would only process first file correctly
+ - compile always opens text files in binary mode so \r\n will
+ match \r\n etc...
+ - Fixed variablesSubstitute to handle escaped strings properly
+ - Broke "tag" matching support :) (it's PCRE)
+
+Version 2.2b
+ - An insane amount of syntax changes were made, but since this
+ release isn't public and I should be the only one using it, should
+ be fine.
+ - Syntax Changes:
+ o function names are not in flags (not in parens)
+ o variable names are not in flags (not in parens)
+ o can call a function just by it's name
+ o delimiter for data is the first character of the data
+ o braces around a single statement can be left out
+ - Fixed strlcpy usage bug in compile.c
+ - Changed watermarks to "stack" based memory so matches in functions
+ work properly. Slight chance of endless loop, but we'll see how it
+ holds.
+ - Added a 'text' op for better passing text data to the execution
+ engine. This solves the recursive variable problem.
+ - On Windows always open files as "binary" (ie: fopen(fileName, "rb") )
+ - Language documentation
+ - Renamed statements:
+ o setvar -> set
+ o addvar -> add
+ o clrvar -> clr
+
+Version 2.1.1b
+ - Converted from C++ to C
+ - Added compatibility functions
+ - Autoconfiscated
+
+Version 2.1b
+ - Compiler support
+ - Added 'tag' matching support
+ - Got rid of endless loop problem
+ - Able to match arbitrary data around locks
+
+Version 2.0b
+ - Total redesign for new VM engine.
+ - New language structure.
+ - Support for variables
+ - Support for line matching
+
+Version 1.0
+ - Initial working release of first language (which was crap and some).
+
diff --git a/INSTALL b/INSTALL
new file mode 100644
index 0000000..8c90af1
--- /dev/null
+++ b/INSTALL
@@ -0,0 +1,9 @@
+======================================================================
+ REP 2.3.2b INSTALL
+
+
+QUICK INSTALLATION:
+ # tar -zxvf rep-2.3.1b.tar.gz
+ # ./configure
+ # make && make install
+
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..56968f1
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,3 @@
+EXTRA_DIST = BUGS rep.dsw config.win32.h
+SUBDIRS = lib src common doc win32
+
diff --git a/NEWS b/NEWS
new file mode 100644
index 0000000..c7ab92a
--- /dev/null
+++ b/NEWS
@@ -0,0 +1 @@
+See ChangeLog \ No newline at end of file
diff --git a/README b/README
new file mode 100644
index 0000000..2ee42cc
--- /dev/null
+++ b/README
@@ -0,0 +1,3 @@
+=================================================================
+ REP 2.3.1b README
+
diff --git a/common/Makefile.am b/common/Makefile.am
new file mode 100644
index 0000000..0686d9d
--- /dev/null
+++ b/common/Makefile.am
@@ -0,0 +1,2 @@
+EXTRA_DIST = compat.c compat.h xstring.c xstring.h usuals.h binfile.h binfile.c repfile.h repfile.c
+
diff --git a/common/binfile.c b/common/binfile.c
new file mode 100644
index 0000000..fa0a120
--- /dev/null
+++ b/common/binfile.c
@@ -0,0 +1,334 @@
+ /*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+#include <stdio.h>
+#include <string.h>
+#include <malloc.h>
+#include "binfile.h"
+
+#define BF_FILE 0x00000010
+#define BF_ERROR 0x00000040
+
+/* bfctx: ---------------------------------------------------
+ * This is the structure that becomes a BFILE
+ * to the outside world
+ */
+typedef struct _bfctx
+{
+ union
+ {
+ FILE* file; /* Valid when working with a FILE */
+ void* data; /* Valid when working with memory */
+ } d;
+ size_t cur; /* Current position */
+ size_t len; /* Size of the memory buffer */
+ int flags;
+}
+bfctx;
+
+
+/* bfWriteRaw: -----------------------------------------------
+ * Write data to appropriate buffer/file
+ */
+int bfWriteRaw(BFILE h, const void* data, size_t len)
+{
+ bfctx* ctx = (bfctx*)h;
+
+ /* file */
+ if(ctx->flags & BF_FILE)
+ {
+ len = fwrite(data, len, 1, ctx->d.file);
+ ctx->cur += len;
+ return len;
+ }
+
+ /* memory */
+ else
+ {
+ /* Check if enough space */
+ if((ctx->cur + len) > ctx->len)
+ {
+ /* If we ran out of space try and reallocate */
+ if(ctx->flags & BF_REALLOC)
+ {
+ void* old = ctx->d.data;
+
+ ctx->len = ctx->len + (((len / 1024) + 1) * 1024);
+ ctx->d.data = realloc(ctx->d.data, ctx->len);
+
+ if(!ctx->d.data)
+ free(old);
+ }
+
+ /*
+ * If there's no more space and we can't
+ * reallocate then it's an error
+ */
+ if(!ctx->d.data || !(ctx->flags & BF_REALLOC))
+ {
+ ctx->flags |= BF_ERROR;
+ return 0;
+ }
+
+ }
+
+ /* write the data in */
+ memcpy(((unsigned char*)ctx->d.data) + ctx->cur, data, len);
+ ctx->cur += len;
+ return len;
+ }
+}
+
+
+/* bfReadRaw: ------------------------------------------------------
+ * Read data from either buffer or file
+ */
+int bfReadRaw(BFILE h, void* data, size_t len)
+{
+ bfctx* ctx = (bfctx*)h;
+
+ /* file */
+ if(ctx->flags & BF_FILE)
+ {
+ len = fread(data, 1, len, ctx->d.file);
+ ctx->cur += len;
+ return len;
+ }
+
+ /* memory */
+ else
+ {
+ if((ctx->len - ctx->cur) < len)
+ len = ctx->len - ctx->cur;
+
+ memcpy(data, ((unsigned char*)ctx->d.data) + ctx->cur, len);
+ ctx->cur += len;
+ return len;
+ }
+}
+
+
+/* bfSeek: ---------------------------------------------------------
+ * Seek forward in either buffer or file
+ */
+int bfSeek(bfctx* ctx, long offset)
+{
+ /* file */
+ if(ctx->flags & BF_FILE)
+ {
+ int ret = fseek(ctx->d.file, offset, SEEK_CUR);
+ if(ret == 0)
+ ctx->cur += offset;
+ return ret;
+ }
+
+ /* memory */
+ else
+ {
+ if((long)(ctx->len - ctx->cur) < offset)
+ return 1;
+ ctx->cur += offset;
+
+ return 0;
+ }
+}
+
+
+/* bfError: -------------------------------------------------
+ * Check for errors on output
+ */
+int bfError(BFILE h)
+{
+ bfctx* ctx = (bfctx*)h;
+ if(ctx->flags & BF_FILE)
+ return ferror(ctx->d.file);
+ else
+ return ctx->flags & BF_ERROR;
+}
+
+
+/* bfStartFile: -----------------------------------------------
+ * Get a BFILE based on a FILE output
+ * file should be opened for appropriate input or output
+ */
+BFILE bfStartFile(FILE* file)
+{
+ bfctx* ctx = (bfctx*)malloc(sizeof(bfctx));
+ if(!ctx) return 0;
+ memset(ctx, 0, sizeof(bfctx));
+
+ ctx->d.file = file;
+ ctx->flags = BF_FILE;
+ return ctx;
+}
+
+/* bfStartMem: -------------------------------------------------
+ * Get a BFILE based on memory
+ * If flags have BF_REALLOC set then NULL can be passed
+ * and/or we can reallocate the buffer
+ */
+BFILE bfStartMem(void* mem, size_t len, int flags)
+{
+ bfctx* ctx = (bfctx*)malloc(sizeof(bfctx));
+ if(!ctx) return 0;
+ memset(ctx, 0, sizeof(bfctx));
+
+ ctx->len = len;
+ ctx->flags = flags;
+ ctx->flags &= ~(BF_FILE | BF_ERROR);
+ ctx->d.data = mem;
+
+ return ctx;
+}
+
+/* bfClose: -----------------------------------------------------
+ * Close a BFILE
+ * Returns either the file or memory buffer, and returns size
+ * in the len argument
+ */
+void bfClose(BFILE h)
+{
+ if(h)
+ free(h);
+}
+
+/* bfInternal: --------------------------------------------------
+ * Get the internal file handle or memory pointer for this
+ * binfile
+ */
+void* bfInternal(BFILE h)
+{
+ bfctx* ctx = (bfctx*)h;
+ if(ctx->flags & BF_FILE)
+ return ctx->d.file;
+ else
+ return ctx->d.data;
+}
+
+/* bfCount: ----------------------------------------------------
+ * Returns the number of bytes written or read to this binfile
+ */
+size_t bfCount(BFILE h)
+{
+ bfctx* ctx = (bfctx*)h;
+ return ctx->cur;
+}
+
+/* bfWriteEnd: -------------------------------------------------
+ * Puts down an end of tags marker in the binfile
+ */
+int bfWriteEnd(BFILE h)
+{
+ bfval opt;
+ int c;
+
+ opt.id = BINTYPE_END;
+ opt.len = 0;
+ opt.type = BINTYPE_END;
+ c = bfWriteRaw(h, &opt, sizeof(opt));
+
+ return bfError(h) ? 0 : c;
+}
+
+/* bfWriteValue: -----------------------------------------------
+ * Writes a tagged value to the binfile
+ */
+int bfWriteValue(BFILE h, const bfval* val, const void* data)
+{
+ int c = bfWriteRaw(h, val, sizeof(bfval));
+ c += bfWriteRaw(h, data, val->len);
+ return bfError(h) ? 0 : c;
+}
+
+/* bfReadValueInfo: --------------------------------------------
+ * Gets the next tag (without data) and returns the length
+ * of the memory required to hold it.
+ */
+int bfReadValueInfo(BFILE h, bfval* val)
+{
+ int c = bfReadRaw(h, val, sizeof(bfval));
+ return c == sizeof(bfval) && !bfError(h);
+}
+
+/* bfReadValueData: ---------------------------------------------
+ * Gets the data for a value. val should be same one returned
+ * from bfReadValueInfo
+ */
+int bfReadValueData(BFILE h, const bfval* val, void* data)
+{
+ size_t c;
+ if((c = bfReadRaw(h, data, val->len)) != val->len ||
+ bfError(h))
+ return 0;
+
+ /* Special handling for these guys */
+ switch(val->type)
+ {
+ case BINTYPE_ASCII:
+ {
+ char* str = (char*)data;
+ str[val->len] = 0;
+ }
+ break;
+
+ case BINTYPE_INT32:
+ case BINTYPE_INT16:
+ /* TODO: do endianness here */
+ break;
+ };
+
+ return c;
+}
+
+/* bfSkipValueData: ------------------------------------------------
+ * Skips the data for a tagged value
+ */
+int bfSkipValueData(BFILE h, const bfval* val)
+{
+ bfctx* ctx = (bfctx*)h;
+ return bfSeek(ctx, val->len) == 0;
+}
+
+
+/* bfWriteString: --------------------------------------------------
+ * Convenience function for writing out strings to file
+ */
+int bfWriteString(BFILE h, short id, const char* data)
+{
+ bfval val;
+ val.id = id;
+ val.type = BINTYPE_ASCII;
+ val.len = strlen(data);
+
+ return bfWriteValue(h, &val, (void*)data);
+}
+
+/* bfWriteInt: -----------------------------------------------------
+ * Convenience function for writing out 4 byte integers to file
+ */
+int bfWriteInt(BFILE h, short id, int data)
+{
+ bfval val;
+ val.id = id;
+ val.type = BINTYPE_INT32;
+ val.len = BINSIZE_INT32;
+
+ /* TODO: take care of endianess here */
+ return bfWriteValue(h, &val, &data);
+}
+
diff --git a/common/binfile.h b/common/binfile.h
new file mode 100644
index 0000000..3b86b8f
--- /dev/null
+++ b/common/binfile.h
@@ -0,0 +1,110 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __BINFILE_H__
+#define __BINFILE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef _WIN32
+#pragma pack(push, ops)
+#endif
+
+#pragma pack(1)
+
+/* bfval: ----------------------------------------------------------
+ * A tagged value to be written/read from a binfile
+ */
+typedef struct _bfval
+{
+ short id; /* The tag id */
+ short type; /* The data type */
+ size_t len; /* The length in bytes */
+}
+bfval;
+
+#pragma pack()
+
+#ifdef _WIN32
+#pragma pack(pop, ops)
+#endif
+
+
+/* BFILE: --------------------------------------------------------
+ * The binfile open handle
+ */
+typedef void* BFILE;
+
+
+
+#define BINTYPE_NONE (short)0x0000
+#define BINTYPE_INT16 (short)0x0001
+#define BINTYPE_INT32 (short)0x0002
+#define BINTYPE_ASCII (short)0x0005
+#define BINTYPE_DATA (short)0x0010
+#define BINTYPE_END (short)0xFFFF
+
+#define BINSIZE_INT32 4 /* sizeof(int) */
+#define BINSIZE_INT16 2 /* sizeof(short) */
+
+#define BF_REALLOC 0x00000001
+
+/* Open a binfile based on a disk FILE */
+BFILE bfStartFile(FILE* file);
+/* Open a binfile based on memory */
+BFILE bfStartMem(void* mem, size_t len, int flags);
+
+
+/* Write raw data to a binfile at current position */
+int bfWriteRaw(BFILE h, const void* data, size_t len);
+/* Read raw data from a binfile at the current position */
+int bfReadRaw(BFILE h, void* data, size_t len);
+
+/* Check if an error occured during reading/writing */
+int bfError(BFILE h);
+
+/* Write a bfval to binfile */
+int bfWriteValue(BFILE h, const bfval* opt, const void* data);
+/* Write a string to a binfile */
+int bfWriteString(BFILE h, short id, const char* data);
+/* Write a 4 byte integer to a binfile */
+int bfWriteInt(BFILE h, short id, int data);
+/* Write the end tag marker */
+int bfWriteEnd(BFILE h);
+
+/* Read the tag information for a value */
+int bfReadValueInfo(BFILE h, bfval* opt);
+/* Read the data for a value */
+int bfReadValueData(BFILE h, const bfval* opt, void* data);
+/* Skip the data for the current value */
+int bfSkipValueData(BFILE h, const bfval* opt);
+
+size_t bfCount(BFILE h);
+void* bfInternal(BFILE h);
+
+/* Close a binfile */
+void bfClose(BFILE h);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BINFILE_H__ */
diff --git a/common/compat.c b/common/compat.c
new file mode 100644
index 0000000..ff8d527
--- /dev/null
+++ b/common/compat.c
@@ -0,0 +1,458 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include "compat.h"
+
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#ifndef HAVE_TOLOWER
+int tolower(int c)
+{
+ if('A' <= c && c <= 'Z')
+ c += 'a' - 'A';
+ return c;
+}
+#define HAVE_TOLOWER
+#endif
+
+
+#ifndef HAVE_STRDUP
+
+#ifndef HAVE_MALLOC
+#error Need a working malloc.
+#endif
+
+char* strdup(const char* str)
+{
+ char* dup = (char*)malloc((strlen(str) + 1) * sizeof(char));
+ if(dup)
+ strcpy(dup, str);
+ return dup;
+}
+#define HAVE_STRDUP
+#endif
+
+
+#ifndef HAVE_STRNDUP
+#ifdef HAVE_MEMORY_H
+#include <memory.h>
+#endif
+char* strndup(const char* str, size_t cnt)
+{
+ char* buff = malloc(sizeof(char) * (cnt + 1));
+ if(buff)
+ {
+ memcpy(buff, str, sizeof(char) * cnt);
+ buff[cnt] = 0;
+ }
+ return buff;
+}
+#endif
+
+
+#ifndef HAVE_STRCASESTR
+char* strcasestr(const char* buff, const char* str)
+{
+ const char* ptr = buff;
+
+ while (*ptr != 0x00)
+ {
+ const char* one = ptr;
+ const char* two = str;
+
+ while (tolower(*one) == tolower(*two))
+ {
+ one++;
+ two++;
+ if (*two == 0x00)
+ return (char*) ptr;
+ }
+ ptr++;
+ }
+ return NULL;
+}
+#endif
+
+#ifndef HAVE_STRLCPY
+
+#ifndef HAVE_STRNCPY
+#error neither strncpy or strlcpy found
+#endif
+
+void strlcpy(char* dest, const char* src, size_t count)
+{
+ strncpy(dest, src, count);
+ dest[count - 1] = 0;
+}
+#endif
+
+#ifndef HAVE_STRLCAT
+
+#ifndef HAVE_STRNCAT
+#error neither strncat or strlcat found
+#endif
+
+void strlcat(char* dest, const char* src, size_t count)
+{
+ strncat(dest, src, count);
+ dest[count - 1] = 0;
+}
+#endif
+
+
+#ifndef HAVE_VASPRINTF
+
+#ifndef HAVE_VSNPRINTF
+#error neither vasprintf or vsnprintf found
+#endif
+
+int vasprintf(char** ret, const char* format, va_list vl)
+{
+ size_t size = 32;
+ int c;
+ *ret = NULL;
+
+ do
+ {
+ // Double the buffer size for next time around
+ size += size;
+
+ if(*ret)
+ free(*ret);
+
+ *ret = (char*)malloc(sizeof(char) * size);
+
+ if(!(*ret))
+ {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ c = vsnprintf(*ret, size, format, vl);
+ }
+ while(c < 0);
+
+ return c;
+}
+
+#endif
+
+
+/*
+ * We need to get a better check for this one.
+ * Basically have to make a variable __progname if none
+ * exist
+ */
+
+#ifndef HAVE_UNISTD_H
+
+char* __progname = 0;
+char prognamebuf[256];
+
+void fixprogname()
+{
+ if(__progname == 0)
+ {
+ const char* beg = strrchr(_pgmptr, '\\');
+ const char* temp = strrchr(_pgmptr, '/');
+ beg = (beg > temp) ? beg : temp;
+ beg = (beg) ? beg + 1 : _pgmptr;
+
+ temp = strrchr(_pgmptr, '.');
+ temp = (temp > beg) ? temp : _pgmptr + strlen(_pgmptr);
+
+ if((temp - beg) > 255)
+ temp = beg + 255;
+
+ strncpy(prognamebuf, beg, temp - beg);
+ prognamebuf[temp - beg] = 0;
+ __progname = prognamebuf;
+ }
+}
+#endif
+
+
+
+#ifndef HAVE_GETOPT
+
+int opterr = 1, /* if error message should be printed */
+ optind = 1, /* index into parent argv vector */
+ optopt, /* character checked for validity */
+ optreset; /* reset getopt */
+char* optarg;
+
+#define BADCH (int)'?'
+#define BADARG (int)':'
+#define EMSG ""
+
+/*
+ * getopt --
+ * Parse argc/argv argument vector.
+ */
+int getopt(int nargc, char* const* nargv, const char* ostr)
+{
+ static char* place = EMSG; /* option letter processing */
+ char *oli; /* option letter list index */
+
+ fixprogname();
+
+ if(optreset || !*place) /* update scanning pointer */
+ {
+ optreset = 0;
+ if(optind >= nargc || *(place = nargv[optind]) != '-')
+ {
+ place = EMSG;
+ return (-1);
+ }
+
+ if (place[1] && *++place == '-') /* found "--" */
+ {
+ ++optind;
+ place = EMSG;
+ return (-1);
+ }
+ } /* option letter okay? */
+
+ if ((optopt = (int)*place++) == (int)':' ||
+ !(oli = strchr(ostr, optopt)))
+ {
+ /*
+ * if the user didn't specify '-' as an option,
+ * assume it means -1.
+ */
+ if(optopt == (int)'-')
+ return (-1);
+ if(!*place)
+ ++optind;
+ if(opterr && *ostr != ':' && optopt != BADCH)
+ (void)fprintf(stderr, "%s: illegal option -- %c\n", __progname, optopt);
+ return(BADCH);
+ }
+ if (*++oli != ':') /* don't need argument */
+ {
+ optarg = NULL;
+ if(!*place)
+ ++optind;
+ }
+ else /* need an argument */
+ {
+ if (*place) /* no white space */
+ optarg = place;
+ else if (nargc <= ++optind) /* no arg */
+ {
+ place = EMSG;
+ if (*ostr == ':')
+ return (BADARG);
+ if(opterr)
+ (void)fprintf(stderr, "%s: option requires an argument -- %c\n", __progname, optopt);
+ return(BADCH);
+ }
+ else /* white space */
+ optarg = nargv[optind];
+
+ place = EMSG;
+ ++optind;
+ }
+ return (optopt); /* dump back option letter */
+}
+#endif
+
+
+#ifndef HAVE_ERR_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+static FILE *err_file; /* file to use for error output */
+static void (*err_exit)(int);
+
+/*
+ * This is declared to take a `void *' so that the caller is not required
+ * to include <stdio.h> first. However, it is really a `FILE *', and the
+ * manual page documents it as such.
+ */
+void err_set_file(void *fp)
+{
+ if (fp)
+ err_file = fp;
+ else
+ err_file = stderr;
+}
+
+void err_set_exit(void (*ef)(int))
+{
+ err_exit = ef;
+}
+
+void err(int eval, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verrc(eval, errno, fmt, ap);
+ va_end(ap);
+}
+
+void verr(int eval, const char *fmt, va_list ap)
+{
+ verrc(eval, errno, fmt, ap);
+}
+
+void errc(int eval, int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verrc(eval, code, fmt, ap);
+ va_end(ap);
+}
+
+void verrc(int eval, int code, const char *fmt, va_list ap)
+{
+ fixprogname();
+ if (err_file == 0)
+ err_set_file((FILE *)0);
+ fprintf(err_file, "%s: ", __progname);
+ if (fmt != NULL) {
+ vfprintf(err_file, fmt, ap);
+ fprintf(err_file, ": ");
+ }
+ fprintf(err_file, "%s\n", strerror(code));
+ if (err_exit)
+ err_exit(eval);
+ exit(eval);
+}
+
+void errx(int eval, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ verrx(eval, fmt, ap);
+ va_end(ap);
+}
+
+void verrx(int eval, const char *fmt, va_list ap)
+{
+ fixprogname();
+ if (err_file == 0)
+ err_set_file((FILE *)0);
+ fprintf(err_file, "%s: ", __progname);
+ if (fmt != NULL)
+ vfprintf(err_file, fmt, ap);
+ fprintf(err_file, "\n");
+ if (err_exit)
+ err_exit(eval);
+ exit(eval);
+}
+
+void warn(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnc(errno, fmt, ap);
+ va_end(ap);
+}
+
+void vwarn(const char *fmt, va_list ap)
+{
+ vwarnc(errno, fmt, ap);
+}
+
+void warnc(int code, const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnc(code, fmt, ap);
+ va_end(ap);
+}
+
+void vwarnc(int code, const char *fmt, va_list ap)
+{
+ fixprogname();
+ if (err_file == 0)
+ err_set_file((FILE *)0);
+ fprintf(err_file, "%s: ", __progname);
+ if (fmt != NULL)
+ {
+ vfprintf(err_file, fmt, ap);
+ fprintf(err_file, ": ");
+ }
+ fprintf(err_file, "%s\n", strerror(code));
+}
+
+void warnx(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vwarnx(fmt, ap);
+ va_end(ap);
+}
+
+void vwarnx(const char *fmt, va_list ap)
+{
+ fixprogname();
+ if(err_file == 0)
+ err_set_file((FILE*)0);
+ fprintf(err_file, "%s: ", __progname);
+ if(fmt != NULL)
+ vfprintf(err_file, fmt, ap);
+ fprintf(err_file, "\n");
+}
+
+#endif
+
+
+
+#ifndef HAVE_STRLWR
+
+char* strlwr(char *string)
+{
+ char * cp;
+ for (cp=string; *cp; ++cp)
+ {
+ if('A' <= *cp && *cp <= 'Z')
+ *cp += 'a' - 'A';
+ }
+ return(string);
+}
+
+#endif
+
+
+
+#ifndef HAVE_REALLOCF
+
+void* reallocf(void* ptr, size_t size)
+{
+ void* ret = realloc(ptr, size);
+
+ if(!ret && size)
+ free(ptr);
+
+ return ret;
+}
+
+#endif
+
diff --git a/common/compat.h b/common/compat.h
new file mode 100644
index 0000000..09934db
--- /dev/null
+++ b/common/compat.h
@@ -0,0 +1,177 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef _COMPAT_H_
+#define _COMPAT_H_
+
+/* Force use of win32 configuration if compiling there */
+#ifdef _WIN32
+#include "../config.win32.h"
+#else
+#ifdef HAVE_CONFIG_H
+#include "../config.h"
+#endif
+#endif
+
+#ifndef HAVE_STDARG_H
+#error ERROR: Must have a working <stdarg.h> header
+#else
+#include <stdarg.h>
+#endif
+
+
+#ifndef HAVE_STAT
+#ifdef _WIN32
+#define S_IFDIR _S_IFDIR
+#define HAVE_STAT
+#else
+#error ERROR: Must have a working 'stat' function
+#endif
+#endif
+
+#ifndef HAVE_GETCWD
+#ifdef _WIN32
+#include <direct.h>
+#define getcwd _getcwd
+#define HAVE_GETCWD
+#else
+#error ERROR: Must have a working 'getcwd' function
+#endif
+#endif
+
+#ifndef HAVE_CHDIR
+#ifdef _WIN32
+#include <direct.h>
+#define chdir _chdir
+#define HAVE_CHDIR
+#else
+#error ERROR: Must have a working 'chdir' function
+#endif
+#endif
+
+#ifndef HAVE_TOLOWER
+int tolower(int c);
+#endif
+
+#ifndef HAVE_STRDUP
+char* strdup(const char* str);
+#endif
+
+#ifndef HAVE_STRNDUP
+char* strndup(const char* str, size_t cnt);
+#endif
+
+#ifndef HAVE_STRCASESTR
+char* strcasestr(const char* big, const char* little);
+#endif
+
+#ifndef HAVE_STRCASECMP
+#ifdef HAVE_STRICMP
+#define strcasecmp stricmp
+#else
+#error ERROR: Must have either 'strcasecmp' or 'stricmp'
+#endif
+#endif
+
+#ifndef HAVE_STRCASECMP
+#ifdef HAVE_STRICMP
+#define strncasecmp strnicmp
+#else
+#error ERROR: Must have either 'strncasecmp' or 'strnicmp'
+#endif
+#endif
+
+
+#ifndef NULL
+#define NULL (void*)0
+#endif
+
+#ifndef __cplusplus
+typedef unsigned char bool;
+#define false 0x00
+#define true 0x01
+#endif
+
+#ifndef HAVE_BYTE
+typedef unsigned char byte;
+#define HAVE_BYTE
+#endif
+
+#ifndef HAVE_UINT
+typedef unsigned int uint;
+#define HAVE_UINT
+#endif
+
+#ifndef HAVE_STRLCPY
+void strlcpy(char* dest, const char* src, size_t count);
+#endif
+
+#ifndef HAVE_STRLCAT
+void strlcat(char* dest, const char* src, size_t count);
+#endif
+
+#ifndef HAVE_VSNPRINTF
+
+#ifdef _WIN32
+#define vsnprintf _vsnprintf
+#define HAVE_VSNPRINTF
+#else
+#ifndef HAVE_VASPRINTF
+#error ERROR: Must have a working 'vsnprintf' or 'vasprintf' function
+#endif
+#endif
+#endif
+
+#ifndef HAVE_VASPRINTF
+int vasprintf(char** ret, const char* format, va_list vl);
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifndef HAVE_GETOPT
+extern char* optarg;
+extern int optind, opterr, optopt;
+int getopt(int nargc, char* const* nargv, const char* ostr);
+#endif
+
+#ifndef HAVE_ERR_H
+#include <stdarg.h>
+void err_set_file(void *fp);
+void err_set_exit(void (*ef)(int));
+void err(int eval, const char *fmt, ...);
+void verr(int eval, const char *fmt, va_list ap);
+void errc(int eval, int code, const char *fmt, ...);
+void verrc(int eval, int code, const char *fmt, va_list ap);
+void errx(int eval, const char *fmt, ...);
+void verrx(int eval, const char *fmt, va_list ap);
+void warn(const char *fmt, ...);
+void vwarn(const char *fmt, va_list ap);
+void warnc(int code, const char *fmt, ...);
+void vwarnc(int code, const char *fmt, va_list ap);
+void warnx(const char *fmt, ...);
+void vwarnx(const char *fmt, va_list ap);
+#endif
+
+#ifndef HAVE_REALLOCF
+void* reallocf(void* ptr, size_t size);
+#endif
+
+#endif \ No newline at end of file
diff --git a/common/repfile.c b/common/repfile.c
new file mode 100644
index 0000000..eec6ca2
--- /dev/null
+++ b/common/repfile.c
@@ -0,0 +1,113 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "common/usuals.h"
+#include "common/compat.h"
+#include "common/binfile.h"
+#include "common/repfile.h"
+
+/* errmsg: -----------------------------------------------------------
+ * Display an rlib error on the console
+ */
+int errmsg(int ret, r_script* script)
+{
+ const char* msg;
+
+ switch(ret)
+ {
+ case R_NOMEM:
+ msg = "out of memory";
+ break;
+ case R_SYNTAX:
+ msg = "syntax error:";
+ break;
+ case R_REGEXP:
+ msg = "reg expression error:";
+ break;
+ case R_LOOP:
+ msg = "an endless loop was encountered ";
+ break;
+ case R_USER:
+ msg = "stop: ";
+ break;
+ case R_IOERR:
+ msg = "read or write error";
+ break;
+ case R_INVARG:
+ ASSERT(0 && "This error should be taken care of by programmer");
+ msg = "internal programmer error";
+ break;
+ default:
+ msg = "unknown error";
+ break;
+ }
+
+ if(script && script->error)
+ {
+ if(script->errline > 0)
+ warnx("%s %s (at line %d)", msg, script->error, script->errline);
+ else
+ warnx("%s %s", msg, script->error);
+ }
+ else
+ {
+ warnx(msg);
+ }
+
+ return ret;
+}
+
+/* repscriptheader: -------------------------------------------------
+ * The top of every compiled rep file has this signature
+ */
+typedef struct _repscriptheader
+{
+ uint sig; /* 'rep0' */
+ uint version; /* Note this is the file version */
+}
+repscriptheader;
+
+const uint kReplSig = '0per';
+const uint kReplVer = 0x00023100;
+
+/* repfWriteHeader: -------------------------------------------------
+ * Write out a valid header
+ */
+bool repfWriteHeader(BFILE h)
+{
+ repscriptheader head;
+ head.sig = kReplSig;
+ head.version = kReplVer;
+
+ bfWriteRaw(h, &head, sizeof(head));
+ return !bfError(h);
+}
+
+/* repfReadHeader: --------------------------------------------------
+ * Read and validate rep file header
+ */
+bool repfReadHeader(BFILE h)
+{
+ repscriptheader head;
+ memset(&head, 0, sizeof(head));
+ bfReadRaw(h, &head, sizeof(head));
+ return head.sig == kReplSig && head.version == kReplVer;
+}
diff --git a/common/repfile.h b/common/repfile.h
new file mode 100644
index 0000000..5d0ff95
--- /dev/null
+++ b/common/repfile.h
@@ -0,0 +1,52 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __REPL_H__
+#define __REPL_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+#include "lib/rlib.h"
+#include "common/usuals.h"
+#include "common/compat.h"
+#include "common/binfile.h"
+
+/* Write rlib errors to console */
+int errmsg(int ret, r_script* script);
+
+/* Read and validate a rep compiled script header */
+bool repfReadHeader(BFILE h);
+/* Write a valid rep compiled script header */
+bool repfWriteHeader(BFILE h);
+
+/* Some binfile tags */
+#define REPVAL_BUFSIZE 0x0008
+#define REPVAL_PARSEMODE 0x0009
+#define REPVAL_BINARYMODE 0x000A
+#define REPVAL_SCRIPT 0x0020
+#define REPVAL_VERSION 0x0030
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __REPL_H__ */
diff --git a/common/usuals.h b/common/usuals.h
new file mode 100644
index 0000000..7317f99
--- /dev/null
+++ b/common/usuals.h
@@ -0,0 +1,61 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __USUALS_H__20000613
+#define __USUALS_H__20000613
+
+#include <sys/types.h>
+#include <string.h>
+#include <stdlib.h>
+#include <memory.h>
+#include <stdio.h>
+#include <ctype.h>
+
+#define zero(v) (memset(&(v), 0, sizeof((v))))
+#define countof(a) (sizeof(a) / sizeof(a[0]))
+
+#ifndef max
+#define max(a,b) (((a) > (b)) ? (a) : (b))
+#endif
+
+#ifndef min
+#define min(a,b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#ifndef ASSERT
+#include <assert.h>
+#define ASSERT assert
+#endif
+
+#ifndef ASSERT_PTR
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#define ASSERT_PTR_LEN(p, len) ASSERT(!(IsBadReadPtr((p), len) || IsBadWritePtr((p), len)))
+#else
+#define ASSERT_PTR_LEN(p, len) ASSERT((p) != 0)
+#endif
+
+#define ASSERT_PTR(p) ASSERT_PTR_LEN(p, sizeof(*(p)))
+
+#endif
+
+
+#endif //__USUALS_H__20000613 \ No newline at end of file
diff --git a/common/xstring.c b/common/xstring.c
new file mode 100644
index 0000000..31a363f
--- /dev/null
+++ b/common/xstring.c
@@ -0,0 +1,104 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "compat.h"
+#include "xstring.h"
+
+
+/* memins: -------------------------------------------------------------
+ * Insert bytes at location
+ */
+void memins(void* buff, size_t cbuff, const void* ins, size_t cins)
+{
+ memmove((unsigned char*)buff + cins, buff, cbuff);
+ memcpy(buff, ins, cins);
+}
+
+
+/* memrep: ------------------------------------------------------------
+ * Replace a number of bytes at a certain location with yet
+ * another number of bytes
+ */
+void* memrep(void* buff, size_t len, size_t cold, const void* ins, size_t cnew)
+{
+ memmove((unsigned char*)buff + cnew, (unsigned char*)buff + cold, len - cold);
+ memcpy(buff, ins, cnew);
+ return (unsigned char*)buff + cnew;
+}
+
+
+/* starlen: -----------------------------------------------------------
+ * Returns the length of a null terminated string array
+ */
+size_t starlen(const char* array)
+{
+ size_t cnt = 0;
+ while(array[0] || array[1])
+ {
+ array += strlen(array);
+ cnt++;
+ }
+
+ return cnt;
+}
+
+
+/* starend: -----------------------------------------------------------
+ * Gets the end of a string array
+ */
+char* starend(const char* array)
+{
+ while(array[0])
+ array += strlen(array) + 1;
+
+ return (char*)array;
+}
+
+
+/* starnadd: ----------------------------------------------------------
+ * Add a value to a string array
+ */
+char* starnadd(char** parray, const char* str, size_t len)
+{
+ char* last = starend(*parray);
+ if(!strrsrv(*parray, (last - *parray) + len + 2))
+ return NULL;
+
+ // It's possible we were relocated
+ last = starend(*parray);
+
+ strncpy(last, str, len);
+ last += len;
+ last[0] = last[1] = 0;
+ return last;
+}
+
+
+/* starnext: ----------------------------------------------------------
+ * Get the next value in a string array
+ */
+const char* starnext(const char* prev)
+{
+ prev += strlen(prev) + 1;
+ return prev[0] ? prev : NULL;
+}
+
diff --git a/common/xstring.h b/common/xstring.h
new file mode 100644
index 0000000..3be4116
--- /dev/null
+++ b/common/xstring.h
@@ -0,0 +1,70 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * VERSION
+ * 2.1.2b
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __XSTRING_H__
+#define __XSTRING_H__
+
+#include <stdarg.h>
+
+/* Insert bytes at a memory location */
+void memins(void* buff, size_t cbuff, const void* ins, size_t cins);
+
+/* Insert string into a string */
+#define strins(buff, ins) \
+ memins((byte*)buff, strlen(buff) + 1, (byte*)ins, strlen(ins))
+
+/* Replace a number of bytes with other bytes at a memory location */
+void* memrep(void* buff, size_t len, size_t cold, const void* ins, size_t cnew);
+
+/* Replace a number of chars with a string */
+#define strrep(s, cold, ins) \
+ (char*)memrep(s, sizeof(char) * (strlen(s) + 1), cold, ins, strlen(ins))
+#define strnrep(s, cold, ins, cins) \
+ (char*)memrep(s, sizeof(char) * (strlen(s) + 1), cold, ins, cins)
+
+/* Reserve memory in a string. (NOTE: buff must be allocated with malloc) */
+#define strrsrv(buff, cnt) \
+ (buff = (char*)realloc(buff, sizeof(char) * ((cnt) + 1)))
+
+
+
+/* Get the length of a null terminated string array */
+size_t starlen(const char* array);
+
+/* Get the end of a null terminated string array */
+char* starend(const char* array);
+
+/* Add a string to the end of a null terminated string array */
+char* starnadd(char** parray, const char* str, size_t len);
+#define staradd(parray, str) \
+ starnadd(parray, str, strlen(str))
+
+/* Get next value in a null terminated string array */
+const char* starnext(const char* prev);
+
+/* Clear a null terminated string array */
+#define starclr(arr) \
+ (arr[0] = arr[1] = 0)
+
+
+
+#endif //__XSTRING_H__ \ No newline at end of file
diff --git a/config.win32.h b/config.win32.h
new file mode 100644
index 0000000..13a057a
--- /dev/null
+++ b/config.win32.h
@@ -0,0 +1,217 @@
+/* Configure options for WIN32 */
+
+#define MAX_BUFF 0x0F00000
+
+/* Define to one of `_getb67', `GETB67', `getb67' for Cray-2 and Cray-YMP
+ systems. This function is required for `alloca.c' support on those systems.
+ */
+/* #undef CRAY_STACKSEG_END */
+
+/* Define to 1 if using `alloca.c'. */
+/* #undef C_ALLOCA */
+
+/* Define to 1 if you have `alloca', as a function or macro. */
+#define HAVE_ALLOCA 1
+
+/* Define to 1 if you have <alloca.h> and it should be used (not on Ultrix).
+ */
+/* #undef HAVE_ALLOCA_H */
+
+/* Define to 1 if the system has the type `bool'. */
+/* #undef HAVE_BOOL */
+
+/* Define to 1 if the system has the type `byte'. */
+/* #undef HAVE_BYTE */
+
+/* Define to 1 if you have the `bzero' function. */
+/* #define HAVE_BZERO 1 */
+
+/* Define to 1 if you have the `chdir' function. */
+/* #define HAVE_CHDIR 1 */
+
+/* Define to 1 if you have the <dirent.h> header file, and it defines `DIR'.
+ */
+/* #define HAVE_DIRENT_H 1 */
+
+/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
+/* #undef HAVE_DOPRNT */
+
+/* Define to 1 if you have the <err.h> header file. */
+/* #define HAVE_ERR_H 1 */
+
+/* Define to 1 if your system has a working `fnmatch' function. */
+/* #define HAVE_FNMATCH 1 */
+
+/* Define to 1 if you have the `getcwd' function. */
+/* #define HAVE_GETCWD 1 */
+
+/* Define to 1 if you have the `getopt' function. */
+/* #define HAVE_GETOPT 1 */
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+/* #define HAVE_INTTYPES_H 1 */
+
+/* Define to 1 if you have the `malloc' function. */
+#define HAVE_MALLOC 1
+
+/* Define to 1 if you have the `memmove' function. */
+#define HAVE_MEMMOVE 1
+
+/* Define to 1 if you have the <memory.h> header file. */
+#define HAVE_MEMORY_H 1
+
+/* Define to 1 if you have the `memset' function. */
+#define HAVE_MEMSET 1
+
+/* Define to 1 if you have the <ndir.h> header file, and it defines `DIR'. */
+/* #undef HAVE_NDIR_H */
+
+/* Define to 1 if you have the `stat' function. */
+/* #define HAVE_STAT 1 */
+
+/* Define to 1 if `stat' has the bug that it succeeds when given the
+ zero-length file name argument. */
+/* #undef HAVE_STAT_EMPTY_STRING_BUG */
+
+/* Define to 1 if you have the <stdarg.h> header file. */
+#define HAVE_STDARG_H 1
+
+/* Define to 1 if you have the <stddef.h> header file. */
+#define HAVE_STDDEF_H 1
+
+/* Define to 1 if you have the <stdint.h> header file. */
+/* #undef HAVE_STDINT_H */
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#define HAVE_STDLIB_H 1
+
+/* Define to 1 if you have the `strcasecmp' function. */
+/* #define HAVE_STRCASECMP 1 */
+
+/* Define to 1 if you have the `strcasecmp' function. */
+#define HAVE_STRICMP 1
+
+/* Define to 1 if you have the `strcasestr' function. */
+/* #define HAVE_STRCASESTR 1 */
+
+/* Define to 1 if you have the `strchr' function. */
+#define HAVE_STRCHR 1
+
+/* Define to 1 if you have the `strcpy' function. */
+#define HAVE_STRCPY 1
+
+/* Define to 1 if you have the `strcat' function. */
+#define HAVE_STRCAT 1
+
+/* Define to 1 if you have the `strcspn' function. */
+#define HAVE_STRCSPN 1
+
+/* Define to 1 if you have the `strdup' function. */
+#define HAVE_STRDUP 1
+
+/* Define to 1 if you have the `strerror' function. */
+#define HAVE_STRERROR 1
+
+/* Define to 1 if you have the <strings.h> header file. */
+/* #define HAVE_STRINGS_H 1 */
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the `strlcpy' function. */
+/* #define HAVE_STRLCPY 1 */
+
+/* Define to 1 if you have the `strncpy' function. */
+#define HAVE_STRNCPY 1
+
+/* Define to 1 if you have the `strlcat' function. */
+/* #define HAVE_STRLCAT 1 */
+
+/* Define to 1 if you have the `strncat' function. */
+#define HAVE_STRNCAT 1
+
+/* Define to 1 if you have the `strncasecmp' function. */
+/* #define HAVE_STRNCASECMP 1 */
+
+/* Define to 1 if you have the `strncasecmp' function. */
+#define HAVE_STRNICMP 1
+
+/* Define to 1 if you have the `strndup' function. */
+/* #undef HAVE_STRNDUP */
+
+/* Define to 1 if you have the `strrchr' function. */
+#define HAVE_STRRCHR 1
+
+/* Define to 1 if you have the `strspn' function. */
+#define HAVE_STRSPN 1
+
+/* Define to 1 if you have the <sys/dir.h> header file, and it defines `DIR'.
+ */
+/* #undef HAVE_SYS_DIR_H */
+
+/* Define to 1 if you have the <sys/ndir.h> header file, and it defines `DIR'.
+ */
+/* #undef HAVE_SYS_NDIR_H */
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#define HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if the system has the type `uint'. */
+/* #define HAVE_UINT 1 */
+
+/* Define to 1 if you have the <unistd.h> header file. */
+/* #define HAVE_UNISTD_H 1 */
+
+/* Define to 1 if you have the `vasprintf' function. */
+/* #define HAVE_VASPRINTF 1 */
+
+/* Define to 1 if you have the `vprintf' function. */
+#define HAVE_VPRINTF 1
+
+/* Define to 1 if you have the `vsnprintf' function. */
+/* #define HAVE_VSNPRINTF 1 */
+
+/* Define to 1 if `lstat' dereferences a symlink specified with a trailing
+ slash. */
+/* #undef LSTAT_FOLLOWS_SLASHED_SYMLINK */
+
+/* Name of package */
+#define PACKAGE "rep"
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT "nielsen@memberwebs.com"
+
+/* Define to the full name of this package. */
+#define PACKAGE_NAME "rep"
+
+/* Define to the full name and version of this package. */
+#define PACKAGE_STRING "rep 2.1.2b"
+
+/* Define to the one symbol short name of this package. */
+#define PACKAGE_TARNAME "rep"
+
+/* Define to the version of this package. */
+#define PACKAGE_VERSION "2.1.2b"
+
+/* If using the C implementation of alloca, define if you know the
+ direction of stack growth for your system; otherwise it will be
+ automatically deduced at run-time.
+ STACK_DIRECTION > 0 => grows toward higher addresses
+ STACK_DIRECTION < 0 => grows toward lower addresses
+ STACK_DIRECTION = 0 => direction of growth unknown */
+/* #undef STACK_DIRECTION */
+
+/* Define to 1 if you have the ANSI C header files. */
+#define STDC_HEADERS 1
+
+/* Version number of package */
+#define VERSION "2.1.1b"
+
+/* Define to empty if `const' does not conform to ANSI C. */
+/* #undef const */
+
+/* Define to `unsigned' if <sys/types.h> does not define. */
+/* #undef size_t */
diff --git a/configure.in b/configure.in
new file mode 100644
index 0000000..4411b55
--- /dev/null
+++ b/configure.in
@@ -0,0 +1,58 @@
+# Process this file with autoconf to produce a configure script.
+AC_INIT(rep, 2.3.1b, nielsen@memberwebs.com)
+AM_INIT_AUTOMAKE(rep, 2.3.1b)
+
+LDFLAGS="$LDFLAGS -L/usr/local/lib"
+
+AC_CONFIG_SRCDIR([src/rep.c])
+AM_CONFIG_HEADER([config.h])
+
+# Checks for programs.
+AC_PROG_CC
+AC_PROG_LIBTOOL
+AC_PROG_INSTALL
+AC_PROG_LN_S
+AC_PROG_MAKE_SET
+AC_PROG_AWK
+
+# The maximum buffer size
+AC_ARG_ENABLE(BUF,
+ AC_HELP_STRING([--max-buf=SIZE],
+ [maximum match buffer size (default 15 MB)]),
+ ac_max_buff=$enableval,
+ ac_max_buff=15728640)
+
+AC_DEFINE_UNQUOTED(MAX_BUFF, $ac_max_buff, [The maximum match buffer size])
+
+# Check for libraries
+AC_CHECK_LIB(pcre, pcre_compile, ,
+ [echo "ERROR: Must install PCRE libraries."; exit 1])
+
+# Checks for header files.
+AC_FUNC_ALLOCA
+AC_HEADER_DIRENT
+AC_HEADER_STDC
+AC_CHECK_HEADERS([memory.h stddef.h stdlib.h string.h stdarg.h], ,
+ [echo "ERROR: Required header missing"; exit 1])
+AC_CHECK_HEADERS([unistd.h err.h])
+
+# Checks for typedefs, structures, and compiler characteristics.
+AC_C_CONST
+AC_TYPE_SIZE_T
+AC_CHECK_TYPES([bool, byte, uint])
+
+# Checks for library functions.
+AC_FUNC_FNMATCH
+AC_FUNC_MALLOC
+AC_FUNC_STAT
+AC_FUNC_VPRINTF
+
+# Required Functions
+AC_CHECK_FUNCS([bzero chdir getcwd memmove memset strchr strcspn strerror strrchr strspn malloc realloc stat], ,
+ [echo "ERROR: Required function missing"; exit 1])
+AC_CHECK_FUNCS([strcasestr strlcpy strncpy strcpy strlcat strncat strcat strdup strndup stricmp strnicmp strcasecmp strncasecmp])
+AC_CHECK_FUNCS([getopt vasprintf vsnprintf reallocf])
+
+AC_CONFIG_FILES([Makefile lib/Makefile src/Makefile common/Makefile doc/Makefile
+ win32/Makefile win32/common/Makefile win32/droplet/Makefile win32/makedrop/Makefile])
+AC_OUTPUT
diff --git a/doc/Makefile.am b/doc/Makefile.am
new file mode 100644
index 0000000..04a654c
--- /dev/null
+++ b/doc/Makefile.am
@@ -0,0 +1,3 @@
+EXTRA_DIST = language_v2.htm
+
+
diff --git a/doc/language_v2.htm b/doc/language_v2.htm
new file mode 100644
index 0000000..9e20493
--- /dev/null
+++ b/doc/language_v2.htm
@@ -0,0 +1,359 @@
+<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
+<html>
+<head>
+<title> The rep language </title>
+<style>
+ pre
+ {
+ margin-left: 3em;
+ }
+ h2
+ {
+ margin-top: 3em;
+ }
+ h3
+ {
+ margin-top: 2em;
+ font-weight: normal;
+ border-top: 1px solid black;
+ padding-left: 5px;
+ }
+ p
+ {
+ margin-left: 1em;
+ font-family: tahoma, verdana, arial, helvetica;
+ font-size: 9pt;
+ line-height: 12pt;
+ }
+</style>
+</head>
+
+<body bgcolor="#FFFFFF">
+<h1>The rep Language</h1>
+
+<p><a href="#syntax">Syntax</a><br>
+<a href="#comments">Comments</a><br>
+<a href="#commands">Commands</a><br>
+<a href="#notes">Script Notes</a></p>
+
+<h2><a name="syntax">Syntax</a></h2>
+<p>A rep script is made of various commands. The commands are detailed below, but here's a few basics:</p>
+<p><strong>Options:</strong>Certain command can take various options. These are wrapped in parentheses, and separated by commas when multiple options are present.</p>
+<p><strong>Text:</strong> Certain commands need a bit of text in order to do their thing. This could be a regular expresson, or perhaps replacement text. This text follows any options if present. The first character of the text is the quote character and is used to detect the end of the data block. If the quote character is used inside the text it must be escaped with a backslash. You can use any of these characters as a quote character:</p>
+
+<pre>"~`!@#$%^&*[]|'&gt;&lt;./?+=-;:</pre>
+
+<p>The examples in this document will use a double quote <tt>(")</tt> as the quote character</p>
+
+<p>A general example of a rep command format would go something like this:</p>
+
+<pre>command (option, option) "data"</pre>
+
+<p>A command can extend to multiple lines. The following is valid:</p>
+
+<pre>command (options,
+options)
+"multiline
+data"</pre>
+
+<p>A command can be followed by curly brackets. Generally this means that the result of the command applies to whatever commands are inside the curly brackets. If the command fails (for example a match that doesn't match) then the stuff inside the curly brackets isn't executed:</p>
+
+<pre>command
+{
+ more commands
+}</pre>
+
+<h2><a name="comments">Comments</a></h2>
+
+<p>A comment starts in a # sign and extends to the end of the line. Comments are not valid inside data. For example:</p>
+
+<pre># Is a comment</pre>
+
+<h2><a name="commands">Commands</a></h2>
+
+<h3>match</h3>
+<pre><b>syntax:</b> match (<i>not, once, find, tag, 0-9</i>) "<i>regexp</i>"</pre>
+<p>Matches regular expression. A full study of regular expressions is outside the scope of this document. You can also match simple text, but you'll need to escape (with a backslash) any characters used by regular expressions. Those are: </p>
+
+<pre>.$%^*+?{}[]|()</pre>
+
+<p>Matches are not case sensitive unless specified with the 'case' option (see below).</p>
+
+<p>The statements inside the match are only executed if the match is successful. In addition the statements inside the match can only operate on the text that was matched. The following matches the word 'Hi There!' in a document and then matches the 'Hi' part.</p>
+
+<pre>match "Hi There!"
+{
+ # This only matches the above 'Hi'
+ match "Hi"
+ {
+ # Do something with 'Hi'
+ }
+}</pre>
+
+<p>The match command can have several options:</p>
+
+<p><b>not</b>: Executes the contained statements if it doesn't match.</p>
+
+<pre>match (not) "can't find me"</pre>
+
+<p><b>once</b>: Makes sure this match only matches once in a document.</p>
+
+<pre>match (once) "&lt;title&gt;"</pre>
+
+<p><b>find</b>: Don't restrict statements inside the match to the text that was matched. This is useful for just verifying if something is there.</p>
+
+<pre>match (find) "check"</pre>
+
+<p><b>tag</b>: Makes a tag match. This is explained further below.
+
+<p><b>0-9</b>: Restricts the statements inside the match to the specified group (wrapped with paretheses) in the regular expression. 0 is the entire statement and 1 through 9 are numbered groups.</p>
+
+<pre>match (1) "Johnny (Smith)"
+{
+ # Now we can do something with 'Smith'
+}</pre>
+
+<h3>replace</h3>
+
+<pre><b>syntax:</b> replace "<i>replace text</i>"</pre>
+
+<p>Replaces the matched text with new text. For example the following replaces 'Hello' with 'Yo' anywhere in the document:</p>
+
+<pre>match "Hello"
+{
+ replace "Yo"
+}</pre>
+
+<p>You can include text groups (which were wrapped in parentheses) that were matched in the previous regular expression. These are specified by using a percent and the group number. %0 specifies all the matched text, and %1 - %9 are the numbered groups.</p>
+
+<p>For example the following replaces all &lt;img&gt; tags with &lt;image&gt; tags:</p>
+
+<pre>match "&lt;img(.*?)&gt;"
+{
+ replace "&lt;image%1&gt;"
+}</pre>
+
+<p>After text has been replaced it is locked. It cannot be matched again.</p>
+
+<h3>else</h3>
+
+<pre><b>syntax:</b> else</pre>
+
+<p>Executes the contained statements if the above statements failed. For example the following executes if the match fails</p>
+
+<pre>match "Yo"
+{
+ # Do whatever we do with "Yo"
+}
+else
+{
+ # Didn't match. Do something else
+}</pre>
+
+<h3>lock</h3>
+
+<pre><b>syntax:</b> lock</pre>
+
+<p>Locks text so it cannot be matched again. Useful to exclude portions of the document from replacements later on. The folling would lock all paragraphs in an HTML document.</p>
+
+<pre>match "&lt;p&gt;.*?&lt;/p&gt;"
+{
+ lock
+}</pre>
+
+<h3>loop</h3>
+
+<pre><b>syntax:</b> loop</pre>
+
+<p>Repeats the contained code until no more matches can be found. The following (dumb) example replaces all a's inside 'aardvark' with e's:</p>
+
+<pre>match "aardvark"
+{
+ loop
+ {
+ match "a"
+ {
+ replace "e"
+ }
+ }
+}</pre>
+
+<p>Note that the entire document is actually wrapped in an invisible loop command. The entire document loops until no more matches can be found. However because of locking, the same text will generally not match more than once. In the above example that specific instance of 'aardvark' wouldn't match again after a single 'a' inside had been replaced or locked. Because of this the loop command comes in quite handy.</p>
+
+<h3>function</h3>
+
+<pre><b>syntax:</b> function <i>function_name</i></pre>
+
+<p>Defines a function which can be called with the call command. Function names are case sensitive, can be up to 40 characters long, and can consist of letters and the underscore.</p>
+
+<pre>function test
+{
+ # do whatever here
+}</pre>
+
+<h3>call</h3>
+
+<pre><b>syntax:</b> call <i>function_name</i></pre>
+
+<p>Calls a function. The function must have been defined earlier in the document.</p>
+
+<pre>call test </pre>
+
+<p>The 'call' bit of the statement can be omitted, so the above can be shortened to:</p>
+
+<pre>test</pre>
+
+<p>For example the following function is used for multiple cases:</p>
+
+<pre>function(atoe)
+{
+ loop
+ {
+ match "a"
+ {
+ replace "e"
+ }
+ }
+}
+
+match "aaron"
+{
+ atoe
+}
+
+match "aardvark"
+{
+ atoe
+}</pre>
+
+<h3>return</h3>
+
+<pre><b>syntax:</b>return (<i>number</i>)</pre>
+
+<p>When inside a function returns from that function to the code which called it. A function will normally return when it's end is reached, but <b>return</b> can be used to return earlier.</p>
+
+<p>You can also return a success code. This must be either 0 (for fail) or 1 (for success). In the code that called the function you can use <b>else</b> to take action on failure. </p>
+
+<h3>end</h3>
+
+<pre><b>syntax:</b> end</pre>
+
+<p>Stops the script at the current location. No more matches are done.</p>
+
+<h3>stop</h3>
+
+<pre><b>syntax:</b> stop "optional error message"</pre>
+
+<p>Stops the script as if an error had occurred. You can include an error message.</p>
+
+<h3>set</h3>
+
+<pre><b>syntax:</b> set <i>variable_name</i> "<i>value</i>"</pre>
+
+<p>Assigns the a value to a variable. The value can include backslash references to matched groups just like replace text can. Variables can be used both in later match statements, replaces or any text portion of a command. Variable names can consist of letters and the underscore. The name should alse be less than 40 characters long. Variables are used like so:</p>
+
+<pre>%<i>variable_name</i></pre>
+
+<p>The following example gets a heading from an HTML document and sets the title to it:</p>
+
+<pre>match "&lt;h1&gt;(.*?)&lt;h1&gt;"
+{
+ setv heading "%1"
+}
+
+match "&lt;head&gt;"
+{
+ replace "&lt;head&gt;&lt;title&gt;%heading&lt;/title&gt;"
+}</pre>
+
+<p>An example of using a variable as a match would be the following. The title is matched, and then searched for in the document. If it's found it's made bold:</p>
+
+<pre>match (once) "&lt;title&gt;(.*?)&lt;/title&gt;"
+{
+ set title "%1"
+}
+
+match "%title"
+{
+ replace "&lt;b&gt;%0&lt;/b&gt;
+}</pre>
+
+<p>If a variable is used whose value hasn't been set yet, it's value is blank. You may also use environment variables in your document. This is how you'd pass values to your script from outside.</p>
+
+<h3>add</h3>
+
+<pre><b>syntax:</b> add <i>variable_name</i> "<i>value</i>"</pre>
+
+<p>Similar to <b>set</b> but insteads makes a variable array and adds the value to it. That means multiple values can be set to a variable. This is only useful during matches where any one of the variable values will match. If a multiple variable is used anywhere else an error will result.</p>
+
+<p>The syntax for using a multiple variable is:</p>
+
+<pre>%(<i>variable_name</i>)</pre>
+
+<p>If used without the parentheses the variable will act like a normal variable and the first of it's values will be used.</p>
+
+<p>The following example matches any HTML table element:</p>
+
+<pre>set telement "table"
+add telement "td"
+add telement "tr"
+add telement "tbody"
+add telement "thead"
+
+match "%(telement)"
+{
+ # Do whatever
+}</pre>
+
+<h3>clr</h3>
+
+<pre><b>syntax:</b> clr <i>variable_name</i></pre>
+
+<p>Clears a variable of it's value.</p>
+
+<h3>options</h3>
+
+<pre><b>syntax:</b> options(case, line)</pre>
+
+<p>Sets various options for your script. These options will apply only inside the set of curly braces that they are set. To set global options put the <b>options</b> command at the top of your file. The options are:</p>
+
+<p><b>case:</b> Makes the matches case sensitive.</p>
+
+<p><b>line:</b> Restricts matches to one line.</p>
+
+<p><i>delimiter:</i> Sets the delimiter for the data portions of commands. By default the double-quote is used, but if you need to match this often, then you can change the delimiter to something else.</p>
+
+<p>The following makes matches case sensitive:</p>
+
+<pre>options(case)</pre>
+
+<h2><a name="notes">Script Notes</a></h2>
+
+<p>First of all you'll need to know regular expressions to get anything useful out of rep scripting. The regular expressions used in rep are PCRE (Perl Compatible Regular Expressions). They're not explained here but you can get tons of info online for them.</p>
+
+<p>Once your script gets a little more than just two or three commands, it's important to understand how it gets run:</p>
+
+<p>The entire script is run over and over again in a loop until no more matches are made. Although it's difficult, it is possible to throw your script in an endless loop. If this is the case then execution stops after a million loops.</p>
+
+<p>Portions of the document that have been locked or replaced cannot be matched again. Also if a portion of text to be matched has been locked it cannot be matched. Keep this in mind when making your scripts. If you wish to match and replace multiple items inside another match, you'll need to use a loop command to do so.</p>
+
+<p>The rep processor can be used in a buffered mode where only a portion of the document is operated on at one time. This greatly increases the speed of the processor. But you have to be careful that any matches you make will fit inside that buffer. In many cases (for example matching the entire &lt;body&gt; tag of an HTML document, you won't be able to use buffered mode reliably.)</p>
+
+<h3>Tag Matches</h3>
+<p>Tag matches match a starting and closing tag of your choice. They also take into consideration that there may be other tags inside that could match. The opening and closing tag are separated by an equal sign ('<tt>=</tt>').</p>
+
+<p>For example the following would match a set of <tt>&lt;div&gt;</tt> tags in an HTML document. It would pair up the correct set of <tt>&lt;div&gt;</tt> tags even if there were other contained tags that could match:</p>
+
+<pre>match (tag) "&lt;div&gt;=&lt;/div&gt;"</pre>
+
+<p>The regular expression groups are handled differently when using tag matching:</p>
+
+<p><b>0</b>: This is the entire match as usual.<br>
+<b>1</b>: The text of the opening tag.<br>
+<b>2</b>: The contained text.<br>
+<b>3</b>: The text of the closing tag.</p>
+
+
+
+</body>
+</html>
diff --git a/doc/pcre_man.txt b/doc/pcre_man.txt
new file mode 100644
index 0000000..e3b1c7d
--- /dev/null
+++ b/doc/pcre_man.txt
@@ -0,0 +1,1206 @@
+NAME
+ pcre - Perl-compatible regular expressions.
+
+
+REGULAR EXPRESSION DETAILS
+ The syntax and semantics of the regular expressions sup-
+ ported by PCRE are described below. Regular expressions are
+ also described in the Perl documentation and in a number of
+ other books, some of which have copious examples. Jeffrey
+ Friedl's "Mastering Regular Expressions", published by
+ O'Reilly (ISBN 1-56592-257), covers them in great detail.
+
+ The description here is intended as reference documentation.
+ The basic operation of PCRE is on strings of bytes. However,
+ there is the beginnings of some support for UTF-8 character
+ strings. To use this support you must configure PCRE to
+ include it, and then call pcre_compile() with the PCRE_UTF8
+ option. How this affects the pattern matching is described
+ in the final section of this document.
+
+ A regular expression is a pattern that is matched against a
+ subject string from left to right. Most characters stand for
+ themselves in a pattern, and match the corresponding charac-
+ ters in the subject. As a trivial example, the pattern
+
+ The quick brown fox
+
+ matches a portion of a subject string that is identical to
+ itself. The power of regular expressions comes from the
+ ability to include alternatives and repetitions in the pat-
+ tern. These are encoded in the pattern by the use of meta-
+ characters, which do not stand for themselves but instead
+ are interpreted in some special way.
+
+ There are two different sets of meta-characters: those that
+ are recognized anywhere in the pattern except within square
+ brackets, and those that are recognized in square brackets.
+ Outside square brackets, the meta-characters are as follows:
+
+ \ general escape character with several uses
+ ^ assert start of subject (or line, in multiline
+ mode)
+ $ assert end of subject (or line, in multiline mode)
+ . match any character except newline (by default)
+ [ start character class definition
+ | start of alternative branch
+ ( start subpattern
+ ) end subpattern
+ ? extends the meaning of (
+ also 0 or 1 quantifier
+ also quantifier minimizer
+ * 0 or more quantifier
+ + 1 or more quantifier
+ { start min/max quantifier
+
+ Part of a pattern that is in square brackets is called a
+ "character class". In a character class the only meta-
+ characters are:
+
+ \ general escape character
+ ^ negate the class, but only if the first character
+ - indicates character range
+ ] terminates the character class
+
+ The following sections describe the use of each of the
+ meta-characters.
+
+
+
+BACKSLASH
+ The backslash character has several uses. Firstly, if it is
+ followed by a non-alphameric character, it takes away any
+ special meaning that character may have. This use of
+
+ backslash as an escape character applies both inside and
+ outside character classes.
+
+ For example, if you want to match a "*" character, you write
+ "\*" in the pattern. This applies whether or not the follow-
+ ing character would otherwise be interpreted as a meta-
+ character, so it is always safe to precede a non-alphameric
+ with "\" to specify that it stands for itself. In particu-
+ lar, if you want to match a backslash, you write "\\".
+
+ If a pattern is compiled with the PCRE_EXTENDED option, whi-
+ tespace in the pattern (other than in a character class) and
+ characters between a "#" outside a character class and the
+ next newline character are ignored. An escaping backslash
+ can be used to include a whitespace or "#" character as part
+ of the pattern.
+
+ A second use of backslash provides a way of encoding non-
+ printing characters in patterns in a visible manner. There
+ is no restriction on the appearance of non-printing charac-
+ ters, apart from the binary zero that terminates a pattern,
+ but when a pattern is being prepared by text editing, it is
+ usually easier to use one of the following escape sequences
+ than the binary character it represents:
+
+ \a alarm, that is, the BEL character (hex 07)
+ \cx "control-x", where x is any character
+ \e escape (hex 1B)
+ \f formfeed (hex 0C)
+ \n newline (hex 0A)
+ \r carriage return (hex 0D)
+ \t tab (hex 09)
+ \xhh character with hex code hh
+ \ddd character with octal code ddd, or backreference
+
+ The precise effect of "\cx" is as follows: if "x" is a lower
+ case letter, it is converted to upper case. Then bit 6 of
+ the character (hex 40) is inverted. Thus "\cz" becomes hex
+ 1A, but "\c{" becomes hex 3B, while "\c;" becomes hex 7B.
+
+ After "\x", up to two hexadecimal digits are read (letters
+ can be in upper or lower case).
+
+ After "\0" up to two further octal digits are read. In both
+ cases, if there are fewer than two digits, just those that
+ are present are used. Thus the sequence "\0\x\07" specifies
+ two binary zeros followed by a BEL character. Make sure you
+ supply two digits after the initial zero if the character
+ that follows is itself an octal digit.
+
+ The handling of a backslash followed by a digit other than 0
+ is complicated. Outside a character class, PCRE reads it
+ and any following digits as a decimal number. If the number
+ is less than 10, or if there have been at least that many
+ previous capturing left parentheses in the expression, the
+ entire sequence is taken as a back reference. A description
+ of how this works is given later, following the discussion
+ of parenthesized subpatterns.
+
+ Inside a character class, or if the decimal number is
+ greater than 9 and there have not been that many capturing
+ subpatterns, PCRE re-reads up to three octal digits follow-
+ ing the backslash, and generates a single byte from the
+ least significant 8 bits of the value. Any subsequent digits
+ stand for themselves. For example:
+
+ \040 is another way of writing a space
+ \40 is the same, provided there are fewer than 40
+ previous capturing subpatterns
+ \7 is always a back reference
+ \11 might be a back reference, or another way of
+ writing a tab
+ \011 is always a tab
+ \0113 is a tab followed by the character "3"
+ \113 is the character with octal code 113 (since there
+ can be no more than 99 back references)
+ \377 is a byte consisting entirely of 1 bits
+ \81 is either a back reference, or a binary zero
+ followed by the two characters "8" and "1"
+
+ Note that octal values of 100 or greater must not be intro-
+ duced by a leading zero, because no more than three octal
+ digits are ever read.
+
+ All the sequences that define a single byte value can be
+ used both inside and outside character classes. In addition,
+ inside a character class, the sequence "\b" is interpreted
+ as the backspace character (hex 08). Outside a character
+ class it has a different meaning (see below).
+
+ The third use of backslash is for specifying generic charac-
+ ter types:
+
+ \d any decimal digit
+ \D any character that is not a decimal digit
+ \s any whitespace character
+ \S any character that is not a whitespace character
+ \w any "word" character
+ \W any "non-word" character
+
+ Each pair of escape sequences partitions the complete set of
+ characters into two disjoint sets. Any given character
+ matches one, and only one, of each pair.
+
+ A "word" character is any letter or digit or the underscore
+ character, that is, any character which can be part of a
+ Perl "word". The definition of letters and digits is con-
+ trolled by PCRE's character tables, and may vary if locale-
+ specific matching is taking place (see "Locale support"
+ above). For example, in the "fr" (French) locale, some char-
+ acter codes greater than 128 are used for accented letters,
+ and these are matched by \w.
+
+ These character type sequences can appear both inside and
+ outside character classes. They each match one character of
+ the appropriate type. If the current matching point is at
+ the end of the subject string, all of them fail, since there
+ is no character to match.
+
+ The fourth use of backslash is for certain simple asser-
+ tions. An assertion specifies a condition that has to be met
+ at a particular point in a match, without consuming any
+ characters from the subject string. The use of subpatterns
+ for more complicated assertions is described below. The
+ backslashed assertions are
+
+ \b word boundary
+ \B not a word boundary
+ \A start of subject (independent of multiline mode)
+ \Z end of subject or newline at end (independent of
+ multiline mode)
+ \z end of subject (independent of multiline mode)
+
+ These assertions may not appear in character classes (but
+ note that "\b" has a different meaning, namely the backspace
+ character, inside a character class).
+
+ A word boundary is a position in the subject string where
+ the current character and the previous character do not both
+ match \w or \W (i.e. one matches \w and the other matches
+ \W), or the start or end of the string if the first or last
+ character matches \w, respectively.
+
+ The \A, \Z, and \z assertions differ from the traditional
+ circumflex and dollar (described below) in that they only
+ ever match at the very start and end of the subject string,
+ whatever options are set. They are not affected by the
+ PCRE_NOTBOL or PCRE_NOTEOL options. If the startoffset argu-
+ ment of pcre_exec() is non-zero, \A can never match. The
+ difference between \Z and \z is that \Z matches before a
+ newline that is the last character of the string as well as
+ at the end of the string, whereas \z matches only at the
+ end.
+
+
+
+CIRCUMFLEX AND DOLLAR
+ Outside a character class, in the default matching mode, the
+ circumflex character is an assertion which is true only if
+ the current matching point is at the start of the subject
+ string. If the startoffset argument of pcre_exec() is non-
+ zero, circumflex can never match. Inside a character class,
+ circumflex has an entirely different meaning (see below).
+
+ Circumflex need not be the first character of the pattern if
+ a number of alternatives are involved, but it should be the
+ first thing in each alternative in which it appears if the
+ pattern is ever to match that branch. If all possible alter-
+ natives start with a circumflex, that is, if the pattern is
+ constrained to match only at the start of the subject, it is
+ said to be an "anchored" pattern. (There are also other con-
+ structs that can cause a pattern to be anchored.)
+
+ A dollar character is an assertion which is true only if the
+ current matching point is at the end of the subject string,
+ or immediately before a newline character that is the last
+ character in the string (by default). Dollar need not be the
+ last character of the pattern if a number of alternatives
+ are involved, but it should be the last item in any branch
+ in which it appears. Dollar has no special meaning in a
+ character class.
+
+ The meaning of dollar can be changed so that it matches only
+ at the very end of the string, by setting the
+ PCRE_DOLLAR_ENDONLY option at compile or matching time. This
+ does not affect the \Z assertion.
+
+ The meanings of the circumflex and dollar characters are
+ changed if the PCRE_MULTILINE option is set. When this is
+ the case, they match immediately after and immediately
+ before an internal "\n" character, respectively, in addition
+ to matching at the start and end of the subject string. For
+ example, the pattern /^abc$/ matches the subject string
+ "def\nabc" in multiline mode, but not otherwise. Conse-
+ quently, patterns that are anchored in single line mode
+ because all branches start with "^" are not anchored in mul-
+ tiline mode, and a match for circumflex is possible when the
+ startoffset argument of pcre_exec() is non-zero. The
+ PCRE_DOLLAR_ENDONLY option is ignored if PCRE_MULTILINE is
+ set.
+
+ Note that the sequences \A, \Z, and \z can be used to match
+ the start and end of the subject in both modes, and if all
+ branches of a pattern start with \A it is always anchored,
+ whether PCRE_MULTILINE is set or not.
+
+
+
+FULL STOP (PERIOD, DOT)
+ Outside a character class, a dot in the pattern matches any
+ one character in the subject, including a non-printing char-
+ acter, but not (by default) newline. If the PCRE_DOTALL
+ option is set, dots match newlines as well. The handling of
+ dot is entirely independent of the handling of circumflex
+ and dollar, the only relationship being that they both
+ involve newline characters. Dot has no special meaning in a
+ character class.
+
+
+
+SQUARE BRACKETS
+ An opening square bracket introduces a character class, ter-
+ minated by a closing square bracket. A closing square
+ bracket on its own is not special. If a closing square
+ bracket is required as a member of the class, it should be
+ the first data character in the class (after an initial cir-
+ cumflex, if present) or escaped with a backslash.
+
+ A character class matches a single character in the subject;
+ the character must be in the set of characters defined by
+ the class, unless the first character in the class is a cir-
+ cumflex, in which case the subject character must not be in
+ the set defined by the class. If a circumflex is actually
+ required as a member of the class, ensure it is not the
+ first character, or escape it with a backslash.
+
+ For example, the character class [aeiou] matches any lower
+ case vowel, while [^aeiou] matches any character that is not
+ a lower case vowel. Note that a circumflex is just a con-
+ venient notation for specifying the characters which are in
+ the class by enumerating those that are not. It is not an
+ assertion: it still consumes a character from the subject
+ string, and fails if the current pointer is at the end of
+ the string.
+
+ When caseless matching is set, any letters in a class
+ represent both their upper case and lower case versions, so
+ for example, a caseless [aeiou] matches "A" as well as "a",
+ and a caseless [^aeiou] does not match "A", whereas a case-
+ ful version would.
+
+ The newline character is never treated in any special way in
+ character classes, whatever the setting of the PCRE_DOTALL
+ or PCRE_MULTILINE options is. A class such as [^a] will
+ always match a newline.
+
+ The minus (hyphen) character can be used to specify a range
+ of characters in a character class. For example, [d-m]
+ matches any letter between d and m, inclusive. If a minus
+ character is required in a class, it must be escaped with a
+ backslash or appear in a position where it cannot be inter-
+ preted as indicating a range, typically as the first or last
+ character in the class.
+
+ It is not possible to have the literal character "]" as the
+ end character of a range. A pattern such as [W-]46] is
+ interpreted as a class of two characters ("W" and "-") fol-
+ lowed by a literal string "46]", so it would match "W46]" or
+ "-46]". However, if the "]" is escaped with a backslash it
+ is interpreted as the end of range, so [W-\]46] is inter-
+ preted as a single class containing a range followed by two
+ separate characters. The octal or hexadecimal representation
+ of "]" can also be used to end a range.
+
+ Ranges operate in ASCII collating sequence. They can also be
+ used for characters specified numerically, for example
+ [\000-\037]. If a range that includes letters is used when
+ caseless matching is set, it matches the letters in either
+ case. For example, [W-c] is equivalent to [][\^_`wxyzabc],
+ matched caselessly, and if character tables for the "fr"
+ locale are in use, [\xc8-\xcb] matches accented E characters
+ in both cases.
+
+ The character types \d, \D, \s, \S, \w, and \W may also
+ appear in a character class, and add the characters that
+ they match to the class. For example, [\dABCDEF] matches any
+ hexadecimal digit. A circumflex can conveniently be used
+ with the upper case character types to specify a more res-
+ tricted set of characters than the matching lower case type.
+ For example, the class [^\W_] matches any letter or digit,
+ but not underscore.
+
+ All non-alphameric characters other than \, -, ^ (at the
+ start) and the terminating ] are non-special in character
+ classes, but it does no harm if they are escaped.
+
+
+
+POSIX CHARACTER CLASSES
+ Perl 5.6 (not yet released at the time of writing) is going
+ to support the POSIX notation for character classes, which
+ uses names enclosed by [: and :] within the enclosing
+ square brackets. PCRE supports this notation. For example,
+
+ [01[:alpha:]%]
+
+ matches "0", "1", any alphabetic character, or "%". The sup-
+ ported class names are
+
+ alnum letters and digits
+ alpha letters
+ ascii character codes 0 - 127
+ cntrl control characters
+ digit decimal digits (same as \d)
+ graph printing characters, excluding space
+ lower lower case letters
+ print printing characters, including space
+ punct printing characters, excluding letters and digits
+ space white space (same as \s)
+ upper upper case letters
+ word "word" characters (same as \w)
+ xdigit hexadecimal digits
+
+ The names "ascii" and "word" are Perl extensions. Another
+ Perl extension is negation, which is indicated by a ^ char-
+ acter after the colon. For example,
+
+ [12[:^digit:]]
+
+ matches "1", "2", or any non-digit. PCRE (and Perl) also
+ recognize the POSIX syntax [.ch.] and [=ch=] where "ch" is a
+ "collating element", but these are not supported, and an
+ error is given if they are encountered.
+
+
+
+VERTICAL BAR
+ Vertical bar characters are used to separate alternative
+ patterns. For example, the pattern
+
+ gilbert|sullivan
+
+ matches either "gilbert" or "sullivan". Any number of alter-
+ natives may appear, and an empty alternative is permitted
+ (matching the empty string). The matching process tries
+ each alternative in turn, from left to right, and the first
+ one that succeeds is used. If the alternatives are within a
+ subpattern (defined below), "succeeds" means matching the
+ rest of the main pattern as well as the alternative in the
+ subpattern.
+
+
+
+INTERNAL OPTION SETTING
+ The settings of PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL,
+ and PCRE_EXTENDED can be changed from within the pattern by
+ a sequence of Perl option letters enclosed between "(?" and
+ ")". The option letters are
+
+ i for PCRE_CASELESS
+ m for PCRE_MULTILINE
+ s for PCRE_DOTALL
+ x for PCRE_EXTENDED
+
+ For example, (?im) sets caseless, multiline matching. It is
+ also possible to unset these options by preceding the letter
+ with a hyphen, and a combined setting and unsetting such as
+ (?im-sx), which sets PCRE_CASELESS and PCRE_MULTILINE while
+ unsetting PCRE_DOTALL and PCRE_EXTENDED, is also permitted.
+ If a letter appears both before and after the hyphen, the
+ option is unset.
+
+ The scope of these option changes depends on where in the
+ pattern the setting occurs. For settings that are outside
+ any subpattern (defined below), the effect is the same as if
+ the options were set or unset at the start of matching. The
+ following patterns all behave in exactly the same way:
+
+ (?i)abc
+ a(?i)bc
+ ab(?i)c
+ abc(?i)
+
+ which in turn is the same as compiling the pattern abc with
+ PCRE_CASELESS set. In other words, such "top level" set-
+ tings apply to the whole pattern (unless there are other
+ changes inside subpatterns). If there is more than one set-
+ ting of the same option at top level, the rightmost setting
+ is used.
+
+ If an option change occurs inside a subpattern, the effect
+ is different. This is a change of behaviour in Perl 5.005.
+ An option change inside a subpattern affects only that part
+ of the subpattern that follows it, so
+
+ (a(?i)b)c
+
+ matches abc and aBc and no other strings (assuming
+ PCRE_CASELESS is not used). By this means, options can be
+ made to have different settings in different parts of the
+ pattern. Any changes made in one alternative do carry on
+ into subsequent branches within the same subpattern. For
+ example,
+
+ (a(?i)b|c)
+
+ matches "ab", "aB", "c", and "C", even though when matching
+ "C" the first branch is abandoned before the option setting.
+ This is because the effects of option settings happen at
+ compile time. There would be some very weird behaviour oth-
+ erwise.
+
+ The PCRE-specific options PCRE_UNGREEDY and PCRE_EXTRA can
+ be changed in the same way as the Perl-compatible options by
+ using the characters U and X respectively. The (?X) flag
+ setting is special in that it must always occur earlier in
+ the pattern than any of the additional features it turns on,
+ even when it is at top level. It is best put at the start.
+
+
+
+SUBPATTERNS
+ Subpatterns are delimited by parentheses (round brackets),
+ which can be nested. Marking part of a pattern as a subpat-
+ tern does two things:
+
+ 1. It localizes a set of alternatives. For example, the pat-
+ tern
+
+ cat(aract|erpillar|)
+
+ matches one of the words "cat", "cataract", or "caterpil-
+ lar". Without the parentheses, it would match "cataract",
+ "erpillar" or the empty string.
+
+ 2. It sets up the subpattern as a capturing subpattern (as
+ defined above). When the whole pattern matches, that por-
+ tion of the subject string that matched the subpattern is
+ passed back to the caller via the ovector argument of
+ pcre_exec(). Opening parentheses are counted from left to
+ right (starting from 1) to obtain the numbers of the captur-
+ ing subpatterns.
+
+ For example, if the string "the red king" is matched against
+ the pattern
+
+ the ((red|white) (king|queen))
+
+ the captured substrings are "red king", "red", and "king",
+ and are numbered 1, 2, and 3, respectively.
+
+ The fact that plain parentheses fulfil two functions is not
+ always helpful. There are often times when a grouping sub-
+ pattern is required without a capturing requirement. If an
+ opening parenthesis is followed by "?:", the subpattern does
+ not do any capturing, and is not counted when computing the
+ number of any subsequent capturing subpatterns. For example,
+ if the string "the white queen" is matched against the pat-
+ tern
+
+ the ((?:red|white) (king|queen))
+
+ the captured substrings are "white queen" and "queen", and
+ are numbered 1 and 2. The maximum number of captured sub-
+ strings is 99, and the maximum number of all subpatterns,
+ both capturing and non-capturing, is 200.
+
+ As a convenient shorthand, if any option settings are
+ required at the start of a non-capturing subpattern, the
+ option letters may appear between the "?" and the ":". Thus
+ the two patterns
+
+ (?i:saturday|sunday)
+ (?:(?i)saturday|sunday)
+
+ match exactly the same set of strings. Because alternative
+ branches are tried from left to right, and options are not
+ reset until the end of the subpattern is reached, an option
+ setting in one branch does affect subsequent branches, so
+ the above patterns match "SUNDAY" as well as "Saturday".
+
+
+
+REPETITION
+ Repetition is specified by quantifiers, which can follow any
+ of the following items:
+
+ a single character, possibly escaped
+ the . metacharacter
+ a character class
+ a back reference (see next section)
+ a parenthesized subpattern (unless it is an assertion -
+ see below)
+
+ The general repetition quantifier specifies a minimum and
+ maximum number of permitted matches, by giving the two
+ numbers in curly brackets (braces), separated by a comma.
+ The numbers must be less than 65536, and the first must be
+ less than or equal to the second. For example:
+
+ z{2,4}
+
+ matches "zz", "zzz", or "zzzz". A closing brace on its own
+ is not a special character. If the second number is omitted,
+ but the comma is present, there is no upper limit; if the
+ second number and the comma are both omitted, the quantifier
+ specifies an exact number of required matches. Thus
+
+ [aeiou]{3,}
+
+ matches at least 3 successive vowels, but may match many
+ more, while
+
+ \d{8}
+
+ matches exactly 8 digits. An opening curly bracket that
+ appears in a position where a quantifier is not allowed, or
+ one that does not match the syntax of a quantifier, is taken
+ as a literal character. For example, {,6} is not a quantif-
+ ier, but a literal string of four characters.
+ The quantifier {0} is permitted, causing the expression to
+ behave as if the previous item and the quantifier were not
+ present.
+
+ For convenience (and historical compatibility) the three
+ most common quantifiers have single-character abbreviations:
+
+ * is equivalent to {0,}
+ + is equivalent to {1,}
+ ? is equivalent to {0,1}
+
+ It is possible to construct infinite loops by following a
+ subpattern that can match no characters with a quantifier
+ that has no upper limit, for example:
+
+ (a?)*
+
+ Earlier versions of Perl and PCRE used to give an error at
+ compile time for such patterns. However, because there are
+ cases where this can be useful, such patterns are now
+ accepted, but if any repetition of the subpattern does in
+ fact match no characters, the loop is forcibly broken.
+
+ By default, the quantifiers are "greedy", that is, they
+ match as much as possible (up to the maximum number of per-
+ mitted times), without causing the rest of the pattern to
+ fail. The classic example of where this gives problems is in
+ trying to match comments in C programs. These appear between
+ the sequences /* and */ and within the sequence, individual
+ * and / characters may appear. An attempt to match C com-
+ ments by applying the pattern
+
+ /\*.*\*/
+
+ to the string
+
+ /* first command */ not comment /* second comment */
+
+ fails, because it matches the entire string owing to the
+ greediness of the .* item.
+
+ However, if a quantifier is followed by a question mark, it
+ ceases to be greedy, and instead matches the minimum number
+ of times possible, so the pattern
+
+ /\*.*?\*/
+
+ does the right thing with the C comments. The meaning of the
+ various quantifiers is not otherwise changed, just the pre-
+ ferred number of matches. Do not confuse this use of ques-
+ tion mark with its use as a quantifier in its own right.
+ Because it has two uses, it can sometimes appear doubled, as
+ in
+
+ \d??\d
+
+ which matches one digit by preference, but can match two if
+ that is the only way the rest of the pattern matches.
+
+ If the PCRE_UNGREEDY option is set (an option which is not
+ available in Perl), the quantifiers are not greedy by
+ default, but individual ones can be made greedy by following
+ them with a question mark. In other words, it inverts the
+ default behaviour.
+
+ When a parenthesized subpattern is quantified with a minimum
+ repeat count that is greater than 1 or with a limited max-
+ imum, more store is required for the compiled pattern, in
+ proportion to the size of the minimum or maximum.
+
+ If a pattern starts with .* or .{0,} and the PCRE_DOTALL
+ option (equivalent to Perl's /s) is set, thus allowing the .
+ to match newlines, the pattern is implicitly anchored,
+ because whatever follows will be tried against every charac-
+ ter position in the subject string, so there is no point in
+ retrying the overall match at any position after the first.
+ PCRE treats such a pattern as though it were preceded by \A.
+ In cases where it is known that the subject string contains
+ no newlines, it is worth setting PCRE_DOTALL when the pat-
+ tern begins with .* in order to obtain this optimization, or
+ alternatively using ^ to indicate anchoring explicitly.
+
+ When a capturing subpattern is repeated, the value captured
+ is the substring that matched the final iteration. For exam-
+ ple, after
+
+ (tweedle[dume]{3}\s*)+
+
+ has matched "tweedledum tweedledee" the value of the cap-
+ tured substring is "tweedledee". However, if there are
+ nested capturing subpatterns, the corresponding captured
+ values may have been set in previous iterations. For exam-
+ ple, after
+
+ /(a|(b))+/
+
+ matches "aba" the value of the second captured substring is
+ "b".
+
+
+
+BACK REFERENCES
+ Outside a character class, a backslash followed by a digit
+ greater than 0 (and possibly further digits) is a back
+ reference to a capturing subpattern earlier (i.e. to its
+ left) in the pattern, provided there have been that many
+ previous capturing left parentheses.
+
+ However, if the decimal number following the backslash is
+ less than 10, it is always taken as a back reference, and
+ causes an error only if there are not that many capturing
+ left parentheses in the entire pattern. In other words, the
+ parentheses that are referenced need not be to the left of
+ the reference for numbers less than 10. See the section
+ entitled "Backslash" above for further details of the han-
+ dling of digits following a backslash.
+
+ A back reference matches whatever actually matched the cap-
+ turing subpattern in the current subject string, rather than
+ anything matching the subpattern itself. So the pattern
+
+ (sens|respons)e and \1ibility
+
+ matches "sense and sensibility" and "response and responsi-
+ bility", but not "sense and responsibility". If caseful
+ matching is in force at the time of the back reference, the
+ case of letters is relevant. For example,
+
+ ((?i)rah)\s+\1
+
+ matches "rah rah" and "RAH RAH", but not "RAH rah", even
+ though the original capturing subpattern is matched case-
+ lessly.
+
+ There may be more than one back reference to the same sub-
+ pattern. If a subpattern has not actually been used in a
+ particular match, any back references to it always fail. For
+ example, the pattern
+
+ (a|(bc))\2
+
+ always fails if it starts to match "a" rather than "bc".
+ Because there may be up to 99 back references, all digits
+ following the backslash are taken as part of a potential
+ back reference number. If the pattern continues with a digit
+ character, some delimiter must be used to terminate the back
+ reference. If the PCRE_EXTENDED option is set, this can be
+ whitespace. Otherwise an empty comment can be used.
+
+ A back reference that occurs inside the parentheses to which
+ it refers fails when the subpattern is first used, so, for
+ example, (a\1) never matches. However, such references can
+ be useful inside repeated subpatterns. For example, the pat-
+ tern
+
+ (a|b\1)+
+
+ matches any number of "a"s and also "aba", "ababbaa" etc. At
+ each iteration of the subpattern, the back reference matches
+ the character string corresponding to the previous itera-
+ tion. In order for this to work, the pattern must be such
+ that the first iteration does not need to match the back
+ reference. This can be done using alternation, as in the
+ example above, or by a quantifier with a minimum of zero.
+
+
+
+ASSERTIONS
+ An assertion is a test on the characters following or
+ preceding the current matching point that does not actually
+ consume any characters. The simple assertions coded as \b,
+ \B, \A, \Z, \z, ^ and $ are described above. More compli-
+ cated assertions are coded as subpatterns. There are two
+ kinds: those that look ahead of the current position in the
+ subject string, and those that look behind it.
+
+ An assertion subpattern is matched in the normal way, except
+ that it does not cause the current matching position to be
+ changed. Lookahead assertions start with (?= for positive
+ assertions and (?! for negative assertions. For example,
+
+ \w+(?=;)
+
+ matches a word followed by a semicolon, but does not include
+ the semicolon in the match, and
+
+ foo(?!bar)
+
+ matches any occurrence of "foo" that is not followed by
+ "bar". Note that the apparently similar pattern
+
+ (?!foo)bar
+
+ does not find an occurrence of "bar" that is preceded by
+ something other than "foo"; it finds any occurrence of "bar"
+ whatsoever, because the assertion (?!foo) is always true
+ when the next three characters are "bar". A lookbehind
+ assertion is needed to achieve this effect.
+
+ Lookbehind assertions start with (?<= for positive asser-
+ tions and (?<! for negative assertions. For example,
+
+ (?<!foo)bar
+
+ does find an occurrence of "bar" that is not preceded by
+ "foo". The contents of a lookbehind assertion are restricted
+ such that all the strings it matches must have a fixed
+ length. However, if there are several alternatives, they do
+ not all have to have the same fixed length. Thus
+
+ (?<=bullock|donkey)
+
+ is permitted, but
+
+ (?<!dogs?|cats?)
+
+ causes an error at compile time. Branches that match dif-
+ ferent length strings are permitted only at the top level of
+ a lookbehind assertion. This is an extension compared with
+ Perl 5.005, which requires all branches to match the same
+ length of string. An assertion such as
+
+ (?<=ab(c|de))
+
+ is not permitted, because its single top-level branch can
+ match two different lengths, but it is acceptable if rewrit-
+ ten to use two top-level branches:
+
+ (?<=abc|abde)
+
+ The implementation of lookbehind assertions is, for each
+ alternative, to temporarily move the current position back
+ by the fixed width and then try to match. If there are
+ insufficient characters before the current position, the
+ match is deemed to fail. Lookbehinds in conjunction with
+ once-only subpatterns can be particularly useful for match-
+ ing at the ends of strings; an example is given at the end
+ of the section on once-only subpatterns.
+
+ Several assertions (of any sort) may occur in succession.
+ For example,
+
+ (?<=\d{3})(?<!999)foo
+
+ matches "foo" preceded by three digits that are not "999".
+ Notice that each of the assertions is applied independently
+ at the same point in the subject string. First there is a
+ check that the previous three characters are all digits, and
+ then there is a check that the same three characters are not
+ "999". This pattern does not match "foo" preceded by six
+ characters, the first of which are digits and the last three
+ of which are not "999". For example, it doesn't match
+ "123abcfoo". A pattern to do that is
+
+ (?<=\d{3}...)(?<!999)foo
+
+ This time the first assertion looks at the preceding six
+ characters, checking that the first three are digits, and
+ then the second assertion checks that the preceding three
+ characters are not "999".
+
+ Assertions can be nested in any combination. For example,
+
+ (?<=(?<!foo)bar)baz
+
+ matches an occurrence of "baz" that is preceded by "bar"
+ which in turn is not preceded by "foo", while
+
+ (?<=\d{3}(?!999)...)foo
+
+ is another pattern which matches "foo" preceded by three
+ digits and any three characters that are not "999".
+
+ Assertion subpatterns are not capturing subpatterns, and may
+ not be repeated, because it makes no sense to assert the
+ same thing several times. If any kind of assertion contains
+ capturing subpatterns within it, these are counted for the
+ purposes of numbering the capturing subpatterns in the whole
+ pattern. However, substring capturing is carried out only
+ for positive assertions, because it does not make sense for
+ negative assertions.
+
+ Assertions count towards the maximum of 200 parenthesized
+ subpatterns.
+
+
+
+ONCE-ONLY SUBPATTERNS
+ With both maximizing and minimizing repetition, failure of
+ what follows normally causes the repeated item to be re-
+ evaluated to see if a different number of repeats allows the
+ rest of the pattern to match. Sometimes it is useful to
+ prevent this, either to change the nature of the match, or
+ to cause it fail earlier than it otherwise might, when the
+ author of the pattern knows there is no point in carrying
+ on.
+
+ Consider, for example, the pattern \d+foo when applied to
+ the subject line
+
+ 123456bar
+
+ After matching all 6 digits and then failing to match "foo",
+ the normal action of the matcher is to try again with only 5
+ digits matching the \d+ item, and then with 4, and so on,
+ before ultimately failing. Once-only subpatterns provide the
+ means for specifying that once a portion of the pattern has
+ matched, it is not to be re-evaluated in this way, so the
+ matcher would give up immediately on failing to match "foo"
+ the first time. The notation is another kind of special
+ parenthesis, starting with (?> as in this example:
+
+ (?>\d+)bar
+
+ This kind of parenthesis "locks up" the part of the pattern
+ it contains once it has matched, and a failure further into
+ the pattern is prevented from backtracking into it. Back-
+ tracking past it to previous items, however, works as nor-
+ mal.
+
+ An alternative description is that a subpattern of this type
+ matches the string of characters that an identical stan-
+ dalone pattern would match, if anchored at the current point
+ in the subject string.
+
+ Once-only subpatterns are not capturing subpatterns. Simple
+ cases such as the above example can be thought of as a max-
+ imizing repeat that must swallow everything it can. So,
+ while both \d+ and \d+? are prepared to adjust the number of
+ digits they match in order to make the rest of the pattern
+ match, (?>\d+) can only match an entire sequence of digits.
+
+ This construction can of course contain arbitrarily compli-
+ cated subpatterns, and it can be nested.
+
+ Once-only subpatterns can be used in conjunction with look-
+ behind assertions to specify efficient matching at the end
+ of the subject string. Consider a simple pattern such as
+
+ abcd$
+
+ when applied to a long string which does not match. Because
+ matching proceeds from left to right, PCRE will look for
+ each "a" in the subject and then see if what follows matches
+ the rest of the pattern. If the pattern is specified as
+
+ ^.*abcd$
+
+ the initial .* matches the entire string at first, but when
+ this fails (because there is no following "a"), it back-
+ tracks to match all but the last character, then all but the
+ last two characters, and so on. Once again the search for
+ "a" covers the entire string, from right to left, so we are
+ no better off. However, if the pattern is written as
+
+ ^(?>.*)(?<=abcd)
+
+ there can be no backtracking for the .* item; it can match
+ only the entire string. The subsequent lookbehind assertion
+ does a single test on the last four characters. If it fails,
+ the match fails immediately. For long strings, this approach
+ makes a significant difference to the processing time.
+
+ When a pattern contains an unlimited repeat inside a subpat-
+ tern that can itself be repeated an unlimited number of
+ times, the use of a once-only subpattern is the only way to
+ avoid some failing matches taking a very long time indeed.
+ The pattern
+
+ (\D+|<\d+>)*[!?]
+
+ matches an unlimited number of substrings that either con-
+ sist of non-digits, or digits enclosed in <>, followed by
+ either ! or ?. When it matches, it runs quickly. However, if
+ it is applied to
+
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+
+ it takes a long time before reporting failure. This is
+ because the string can be divided between the two repeats in
+ a large number of ways, and all have to be tried. (The exam-
+ ple used [!?] rather than a single character at the end,
+ because both PCRE and Perl have an optimization that allows
+ for fast failure when a single character is used. They
+ remember the last single character that is required for a
+ match, and fail early if it is not present in the string.)
+ If the pattern is changed to
+
+ ((?>\D+)|<\d+>)*[!?]
+
+ sequences of non-digits cannot be broken, and failure hap-
+ pens quickly.
+
+
+
+CONDITIONAL SUBPATTERNS
+ It is possible to cause the matching process to obey a sub-
+ pattern conditionally or to choose between two alternative
+ subpatterns, depending on the result of an assertion, or
+ whether a previous capturing subpattern matched or not. The
+ two possible forms of conditional subpattern are
+
+ (?(condition)yes-pattern)
+ (?(condition)yes-pattern|no-pattern)
+
+ If the condition is satisfied, the yes-pattern is used; oth-
+ erwise the no-pattern (if present) is used. If there are
+ more than two alternatives in the subpattern, a compile-time
+ error occurs.
+
+ There are two kinds of condition. If the text between the
+ parentheses consists of a sequence of digits, the condition
+ is satisfied if the capturing subpattern of that number has
+ previously matched. The number must be greater than zero.
+ Consider the following pattern, which contains non-
+ significant white space to make it more readable (assume the
+ PCRE_EXTENDED option) and to divide it into three parts for
+ ease of discussion:
+
+ ( \( )? [^()]+ (?(1) \) )
+
+ The first part matches an optional opening parenthesis, and
+ if that character is present, sets it as the first captured
+ substring. The second part matches one or more characters
+ that are not parentheses. The third part is a conditional
+ subpattern that tests whether the first set of parentheses
+ matched or not. If they did, that is, if subject started
+ with an opening parenthesis, the condition is true, and so
+ the yes-pattern is executed and a closing parenthesis is
+ required. Otherwise, since no-pattern is not present, the
+ subpattern matches nothing. In other words, this pattern
+ matches a sequence of non-parentheses, optionally enclosed
+ in parentheses.
+
+ If the condition is not a sequence of digits, it must be an
+ assertion. This may be a positive or negative lookahead or
+ lookbehind assertion. Consider this pattern, again contain-
+ ing non-significant white space, and with the two alterna-
+ tives on the second line:
+
+ (?(?=[^a-z]*[a-z])
+ \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} )
+
+ The condition is a positive lookahead assertion that matches
+ an optional sequence of non-letters followed by a letter. In
+ other words, it tests for the presence of at least one
+ letter in the subject. If a letter is found, the subject is
+ matched against the first alternative; otherwise it is
+ matched against the second. This pattern matches strings in
+ one of the two forms dd-aaa-dd or dd-dd-dd, where aaa are
+ letters and dd are digits.
+
+
+
+COMMENTS
+ The sequence (?# marks the start of a comment which contin-
+ ues up to the next closing parenthesis. Nested parentheses
+ are not permitted. The characters that make up a comment
+ play no part in the pattern matching at all.
+
+ If the PCRE_EXTENDED option is set, an unescaped # character
+ outside a character class introduces a comment that contin-
+ ues up to the next newline character in the pattern.
+
+
+
+RECURSIVE PATTERNS
+ Consider the problem of matching a string in parentheses,
+ allowing for unlimited nested parentheses. Without the use
+ of recursion, the best that can be done is to use a pattern
+ that matches up to some fixed depth of nesting. It is not
+ possible to handle an arbitrary nesting depth. Perl 5.6 has
+ provided an experimental facility that allows regular
+ expressions to recurse (amongst other things). It does this
+ by interpolating Perl code in the expression at run time,
+ and the code can refer to the expression itself. A Perl pat-
+ tern to solve the parentheses problem can be created like
+ this:
+
+ $re = qr{\( (?: (?>[^()]+) | (?p{$re}) )* \)}x;
+
+ The (?p{...}) item interpolates Perl code at run time, and
+ in this case refers recursively to the pattern in which it
+ appears. Obviously, PCRE cannot support the interpolation of
+ Perl code. Instead, the special item (?R) is provided for
+ the specific case of recursion. This PCRE pattern solves the
+ parentheses problem (assume the PCRE_EXTENDED option is set
+ so that white space is ignored):
+
+ \( ( (?>[^()]+) | (?R) )* \)
+
+ First it matches an opening parenthesis. Then it matches any
+ number of substrings which can either be a sequence of non-
+ parentheses, or a recursive match of the pattern itself
+ (i.e. a correctly parenthesized substring). Finally there is
+ a closing parenthesis.
+
+ This particular example pattern contains nested unlimited
+ repeats, and so the use of a once-only subpattern for match-
+ ing strings of non-parentheses is important when applying
+ the pattern to strings that do not match. For example, when
+ it is applied to
+
+ (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
+
+ it yields "no match" quickly. However, if a once-only sub-
+ pattern is not used, the match runs for a very long time
+ indeed because there are so many different ways the + and *
+ repeats can carve up the subject, and all have to be tested
+ before failure can be reported.
+
+ The values set for any capturing subpatterns are those from
+ the outermost level of the recursion at which the subpattern
+ value is set. If the pattern above is matched against
+
+ (ab(cd)ef)
+
+ the value for the capturing parentheses is "ef", which is
+ the last value taken on at the top level. If additional
+ parentheses are added, giving
+
+ \( ( ( (?>[^()]+) | (?R) )* ) \)
+ ^ ^
+ ^ ^ the string they capture is
+ "ab(cd)ef", the contents of the top level parentheses. If
+ there are more than 15 capturing parentheses in a pattern,
+ PCRE has to obtain extra memory to store data during a
+ recursion, which it does by using pcre_malloc, freeing it
+ via pcre_free afterwards. If no memory can be obtained, it
+ saves data for the first 15 capturing parentheses only, as
+ there is no way to give an out-of-memory error from within a
+ recursion.
+
+
+
+PERFORMANCE
+ Certain items that may appear in patterns are more efficient
+ than others. It is more efficient to use a character class
+ like [aeiou] than a set of alternatives such as (a|e|i|o|u).
+ In general, the simplest construction that provides the
+ required behaviour is usually the most efficient. Jeffrey
+ Friedl's book contains a lot of discussion about optimizing
+ regular expressions for efficient performance.
+
+ When a pattern begins with .* and the PCRE_DOTALL option is
+ set, the pattern is implicitly anchored by PCRE, since it
+ can match only at the start of a subject string. However, if
+ PCRE_DOTALL is not set, PCRE cannot make this optimization,
+ because the . metacharacter does not then match a newline,
+ and if the subject string contains newlines, the pattern may
+ match from the character immediately following one of them
+ instead of from the very start. For example, the pattern
+
+ (.*) second
+
+ matches the subject "first\nand second" (where \n stands for
+ a newline character) with the first captured substring being
+ "and". In order to do this, PCRE has to retry the match
+ starting after every newline in the subject.
+
+ If you are using such a pattern with subject strings that do
+ not contain newlines, the best performance is obtained by
+ setting PCRE_DOTALL, or starting the pattern with ^.* to
+ indicate explicit anchoring. That saves PCRE from having to
+ scan along the subject looking for a newline to restart at.
+
+ Beware of patterns that contain nested indefinite repeats.
+ These can take a long time to run when applied to a string
+ that does not match. Consider the pattern fragment
+
+ (a+)*
+
+ This can match "aaaa" in 33 different ways, and this number
+ increases very rapidly as the string gets longer. (The *
+ repeat can match 0, 1, 2, 3, or 4 times, and for each of
+ those cases other than 0, the + repeats can match different
+ numbers of times.) When the remainder of the pattern is such
+ that the entire match is going to fail, PCRE has in princi-
+ ple to try every possible variation, and this can take an
+ extremely long time.
+
+ An optimization catches some of the more simple cases such
+ as
+
+ (a+)*b
+
+ where a literal character follows. Before embarking on the
+ standard matching procedure, PCRE checks that there is a "b"
+ later in the subject string, and if there is not, it fails
+ the match immediately. However, when there is no following
+ literal this optimization cannot be used. You can see the
+ difference by comparing the behaviour of
+
+ (a+)*\d
+
+ with the pattern above. The former gives a failure almost
+ instantly when applied to a whole line of "a" characters,
+ whereas the latter takes an appreciable time with strings
+ longer than about 20 characters.
+
+
+
+
+AUTHOR
+ Philip Hazel <ph10@cam.ac.uk>
+ University Computing Service,
+ New Museums Site,
+ Cambridge CB2 3QG, England.
+ Phone: +44 1223 334714
+
+ Last updated: 15 August 2001
+ Copyright (c) 1997-2001 University of Cambridge.
diff --git a/lib/Makefile.am b/lib/Makefile.am
new file mode 100644
index 0000000..ccc509e
--- /dev/null
+++ b/lib/Makefile.am
@@ -0,0 +1,9 @@
+include_HEADERS = rlib.h rep.h
+lib_LTLIBRARIES = librlib.la
+
+librlib_la_CFLAGS = -I${top_srcdir} -I/usr/local/include
+librlib_la_SOURCES = compile.c execute.c execute.h priv.h ops.h rlib.c \
+../common/xstring.c ../common/compat.c ../common/binfile.c ../common/repfile.c
+librlib_la_LDFLAGS = -version-info 2:3
+
+EXTRA_DIST = rlib.dsp \ No newline at end of file
diff --git a/lib/compile.c b/lib/compile.c
new file mode 100644
index 0000000..e92a2f6
--- /dev/null
+++ b/lib/compile.c
@@ -0,0 +1,2337 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+/* ----------------------------------------------------------------------
+// Recipe Compiler
+// 2000-2002 Copyright, Nate Nielsen
+*/
+
+
+#include <stdarg.h>
+#include "common/usuals.h"
+#include "common/compat.h"
+#include "lib/rlib.h"
+#include "priv.h"
+#include "execute.h"
+#include "ops.h"
+
+const byte kEncUTF8[] = { 0xEF, 0xBB, 0xBF };
+const byte kEncUCS2_L[] = { 0xFF, 0xFE };
+const byte kEncUCS2_R[] = { 0xFE, 0xFF };
+const byte kEncUCS4_L[] = { 0xFF, 0xFE, 0x00, 0x00 };
+const byte kEncUCS4_R[] = { 0x00, 0x00, 0xFE, 0xFF };
+
+/* Flags Syntax: -----------------------------------------------------------------
+ */
+
+/* All flags for available to statements */
+typedef enum _syn_flags
+{
+ f_not = 0x0001, /* match: Reverse effect */
+ f_once = 0x0002, /* match: Only execute once */
+ f_case = 0x0004, /* options: Case sensitive */
+ f_line = 0x0010, /* options: Limit to line matches */
+ f_find = 0x0020, /* match: match but don't move limits */
+ f_tag = 0x0040, /* tag: use special tag matching */
+
+ /* Special cases */
+ f_num = 0x0400 /* represents a number */
+}
+syn_flags;
+
+
+/* Listing of all flags and their text representations */
+typedef struct _flagmap
+{
+ syn_flags flag;
+ const char* text;
+}
+flagmap;
+
+flagmap kAllFlags[] = {
+ { f_not, "not" },
+ { f_once, "once" },
+ { f_case, "case" },
+ { f_line, "line" },
+ { f_find, "find" },
+ { f_tag, "tag" },
+
+ /* Special cases */
+ { f_num, "" }
+};
+
+
+/* Statement Syntax: --------------------------------------------------------------
+ */
+
+typedef enum _syn_statements
+{
+ s_none,
+ s_function,
+ s_loop,
+ s_once,
+ s_options,
+ s_call,
+ s_return,
+ s_end,
+ s_stop,
+ s_match,
+ s_replace,
+ s_lock,
+ s_setvar,
+ s_addvar,
+ s_clrvar,
+ s_message,
+ s_else,
+ s_opbrace,
+ s_clbrace,
+ s_implied
+}
+syn_statements;
+
+typedef struct _syntaxmap
+{
+ syn_statements syntax; /* id */
+ const char* text; /* text representation */
+ uint flags; /* which flags are valid */
+ uint args; /* Does it have arguments (either a name or a data block) */
+ uint context; /* In which context valid */
+}
+syntaxmap;
+
+/* Context values */
+#define SYNTAX_INROOT 0x00000001 /* Only allowed in the root script */
+#define SYNTAX_INBRACE 0x00000004 /* Only allowed inside braces */
+#define SYNTAX_INFUNCTION 0x00000008 /* Only allowed in a function */
+#define SYNTAX_BLOCK 0x00000010 /* The statement is start of a block */
+#define SYNTAX_IMPLIED 0x00000020 /* We're in an implied block */
+#define SYNTAX_SPECIAL 0x00000100 /* Syntax not determined by parser */
+#define SYNTAX_ANYWHERE (SYNTAX_INROOT | SYNTAX_INBRACE | SYNTAX_INFUNCTION)
+
+/* Argument values */
+#define ARGUMENT_NAME 0x00000001
+#define ARGUMENT_DATA 0x00000002
+
+syntaxmap kAllStatements[] = {
+ { s_none, "", 0, 0, SYNTAX_SPECIAL },
+ { s_function, "function", 0, ARGUMENT_NAME, SYNTAX_INROOT },
+ { s_loop, "loop", 0, 0, SYNTAX_ANYWHERE | SYNTAX_BLOCK },
+ { s_once, "once", 0, 0, SYNTAX_ANYWHERE | SYNTAX_BLOCK },
+ { s_options, "options", f_case | f_line, 0, SYNTAX_ANYWHERE },
+ { s_call, "call", 0, ARGUMENT_NAME, SYNTAX_ANYWHERE },
+ { s_return, "return", f_num, 0, SYNTAX_INFUNCTION },
+ { s_end, "end", 0, 0, SYNTAX_ANYWHERE },
+ { s_stop, "stop", 0, ARGUMENT_DATA, SYNTAX_ANYWHERE },
+ { s_match, "match", f_not|f_once|f_num|f_find|f_tag,ARGUMENT_DATA, SYNTAX_ANYWHERE | SYNTAX_BLOCK },
+ { s_replace, "replace", 0, ARGUMENT_DATA, SYNTAX_ANYWHERE },
+ { s_lock, "lock", 0, 0, SYNTAX_ANYWHERE },
+ { s_setvar, "set", 0, ARGUMENT_DATA | ARGUMENT_NAME, SYNTAX_ANYWHERE },
+ { s_addvar, "add", 0, ARGUMENT_DATA | ARGUMENT_NAME, SYNTAX_ANYWHERE },
+ { s_clrvar, "clr", 0, ARGUMENT_NAME, SYNTAX_ANYWHERE },
+ { s_message, "message", 0, ARGUMENT_DATA, SYNTAX_ANYWHERE },
+ { s_else, "else", 0, 0, SYNTAX_ANYWHERE | SYNTAX_BLOCK },
+
+ /* special cases */
+ { s_opbrace, "{", 0, 0, SYNTAX_SPECIAL },
+ { s_clbrace, "}", 0, 0, SYNTAX_SPECIAL },
+ { s_implied, "", 0, 0, SYNTAX_SPECIAL }
+};
+
+
+
+
+/* Syntax Constants: -------------------------------------------------------------
+ */
+
+const char* kValidNum = "0123465798";
+const char* kValidDelim = "\"~`!@#$%^&*[]|'<>./?+=-;:";
+const char* kValidBrace = "{}";
+const char kFlagsStart = '(';
+const char kFlagsEnd = ')';
+const char kFlagDelim = ',';
+const char kTagDelim = '=';
+const char kEscapeChar = '\\';
+
+/* Maximum size of an identifier */
+#define kMaxIdentifier 40
+
+
+#define INVALID_PTR 0xFFFFFFFF
+
+
+
+/* ----------------------------------------------------------------------
+// CODE and COMPILE FUNCTIONALITY
+*/
+
+const size_t OPS_BUFFER_SIZE = 0x1000;
+
+
+/* codestack: We use one of these for each level of braces. Helps
+// us maintain context. */
+
+typedef struct _codestack
+{
+ uint curContext; /* Current parse context */
+ uint curOptions; /* Current options in use */
+ uint numStatements; /* Number of statements in this block */
+
+ size_t insPos; /* The position to insert code */
+ size_t endPos; /* End of code owned by current codestack */
+
+ struct _codestack* pPrev; /* previous stack (if in list) */
+}
+codestack;
+
+/* Add a new post op buffer and stash away current */
+#define PUSH_CODESTACK(c) \
+do { \
+ (c)->code = pushCodeStack((c)->code); \
+ if(!(c)->code) RETURN(R_NOMEM); \
+} while(0)
+
+/* Dump current post op buffer and get previous */
+#define POP_CODESTACK(c) \
+do { \
+ commitCodeStack((c)->code); \
+ (c)->code = popCodeStack((c)->code); \
+} while (0) \
+
+
+/* Initialize a new post ops buffer and hook into previous */
+static codestack* pushCodeStack(codestack* prev)
+{
+ codestack* code = (codestack*)malloc(sizeof(codestack));
+ if(!code)
+ return NULL;
+
+ memset(code, 0, sizeof(codestack));
+
+ if(prev)
+ {
+ /* Things to carry over from previous */
+ code->insPos = code->endPos = prev->insPos;
+ code->curOptions = prev->curOptions;
+ code->curContext = prev->curContext;
+ }
+ else
+ {
+ code->curContext = SYNTAX_INROOT;
+ }
+
+ /* Init the context stuff */
+
+
+ code->pPrev = prev;
+ return code;
+}
+
+/* Free a post op buffer and return previous */
+static codestack* popCodeStack(codestack* code)
+{
+
+ codestack* prev = code->pPrev;
+
+ if(code->pPrev)
+ {
+
+ /* The insertion position needs fixing ... */
+ /* calc offset */
+ size_t offset = code->insPos - prev->insPos;
+ prev->insPos += offset;
+ prev->endPos += offset;
+ }
+
+ free(code);
+
+ return prev;
+}
+
+#define commitCodeStack(code) ((code)->insPos = (code)->endPos)
+
+
+/* compilecontext: The main compile state. Only one used throughout
+// compilation
+*/
+typedef struct _compilecontext
+{
+ syn_statements lastStatement; /* Last statement we had */
+ syn_statements nextStatement; /* Next statement we're expecting */
+
+ syn_statements curStatement; /* Current statement: */
+ const char* curName; /* - The Name */
+ size_t lenName; /* - Length of the name */
+ uint curFlags; /* - Flags */
+ const char* curFlagData; /* - Extra flag (f_num) */
+ size_t lenFlagData; /* - Length of extra flag */
+ const char* curData; /* - Data */
+ size_t lenData; /* - Length of data */
+
+
+ const char* in; /* Next location to compile in script */
+ long curKey; /* a unique key (id) which gets incremented */
+ /* for various uses such as jumps and calls */
+
+ vmop_t* beg; /* start of block of output code */
+ size_t cbops; /* number of bytes of output code */
+ size_t alloc; /* number of bytes allocated for output */
+
+ codestack* code; /* Current codestack */
+ bool failed : 1; /* did the last memory allocation fail? */
+}
+compilecontext;
+
+
+
+/* -------------------------------------------------------------------
+// POS OP
+//
+// the 'pos' op is a temporary place holder while compiling
+// it takes one value parameter and it's value is the same as is
+// temporarily placed in jmp, je, jne or call
+// it's removed and jumps are patched in the optimization stage
+*/
+
+#define o_pos ((vmop_t)(0xFF))
+/* const vmop_t pos = 0xFF; */
+
+
+
+/* -------------------------------------------------------------------
+// OPS CODING FUNCTIONALITY
+*/
+
+/* These macros can only be used within repCompile */
+
+#define PUSH_OP(op) \
+ pushValues(&ctx, true, sizeof(byte), op, 0)
+
+#define PUSH_OP_1(op, arg) \
+ pushValues(&ctx, true, sizeof(byte), op, sizeof(arg), arg, 0)
+
+#define PUSH_OP_2(op, arg, arg2) \
+ pushValues(&ctx, true, sizeof(byte), op, sizeof(arg), arg, sizeof(arg2), arg2, 0)
+
+/* Add ops in reverse */
+#define PUSH_ROP(op) \
+ pushValues(&ctx, false, sizeof(byte), op, 0)
+
+#define PUSH_ROP_1(op, arg) \
+ pushValues(&ctx, false, sizeof(byte), op, sizeof(arg), arg, 0)
+
+#define PUSH_ROP_2(op, arg, arg2) \
+ pushValues(&ctx, false, sizeof(byte), op, sizeof(arg), arg, sizeof(arg2), arg2, 0)
+
+
+/* Get more stack space for ops */
+static void moreOutput(compilecontext* ctx)
+{
+ /* Reallocate */
+ ctx->beg = (byte*)reallocf(ctx->beg, ctx->alloc + OPS_BUFFER_SIZE);
+ ctx->alloc += OPS_BUFFER_SIZE;
+
+ /* Set flag if failed */
+ if(!ctx->beg)
+ ctx->failed = true;
+}
+
+/* Allocate a specific amount from the stack */
+#define allocOutput(ctx, len) pushData(ctx, NULL, len, true)
+
+
+
+
+/* Push any amount of data on ops buffer */
+static void* pushData(compilecontext* ctx, const void* data, size_t len, bool forward)
+{
+ void* pIns;
+
+ if(ctx->cbops + len >= ctx->alloc)
+ moreOutput(ctx);
+
+ if(ctx->failed || !len)
+ return NULL;
+
+ pIns = ctx->beg + ctx->code->insPos;
+
+ /* Make space at insertion point */
+ memmove(ctx->beg + ctx->code->insPos + len, ctx->beg + ctx->code->insPos,
+ ctx->cbops - ctx->code->insPos);
+
+ /* If we have data copy it in */
+ if(data)
+ memcpy(pIns, data, len);
+
+ /* In debug mode clear it */
+#ifdef _DEBUG
+ else
+ memset(pIns, 0xCC, len);
+#endif
+
+ ctx->cbops += len;
+ ctx->code->endPos += len;
+
+ if(forward)
+ ctx->code->insPos += len;
+
+ /* Only return value if no input data */
+ return data ? NULL : pIns;
+}
+
+
+static void pushValues(compilecontext* ctx, bool forward, ...)
+{
+ va_list ap;
+ size_t len;
+
+ #define VAL_BUF 20
+ byte buff[VAL_BUF];
+ size_t cur = 0;
+
+ va_start(ap, forward);
+ while(len = va_arg(ap, size_t))
+ {
+ if(cur + len > VAL_BUF)
+ {
+ pushData(ctx, buff, cur, forward);
+ cur = 0;
+ }
+
+ switch(len)
+ {
+ case 1:
+ buff[cur] = va_arg(ap, byte);
+ break;
+ case 2:
+ *((unsigned short*)(buff + cur)) = va_arg(ap, unsigned short);
+ break;
+ case 4:
+ *((unsigned int*)(buff + cur)) = va_arg(ap, unsigned int);
+ break;
+ default:
+ ASSERT(false);
+ break;
+ }
+
+ cur += len;
+ }
+
+ pushData(ctx, buff, cur, forward);
+}
+
+static int testRegexp(r_script* script, const char* regexp, short options)
+{
+ int erroroffset;
+ const char* error = NULL;
+ pcre* re = pcre_compile(regexp, options, &error, &erroroffset, NULL);
+
+ if(!re)
+ {
+ if(error)
+ scriptSetError(script, error);
+ return R_REGEXP;
+ }
+ else
+ {
+ free(re);
+ return R_OK;
+ }
+}
+
+/* ----------------------------------------------------------------------
+// Functions for pushing specific types of ops on the stack
+*/
+static int pushMatch(r_script* script, compilecontext* ctx, const char* regexp)
+{
+ int ret;
+
+ /* Allocate */
+ size_t len = sizeof(match_op_pcre) + (sizeof(char) * strlen(regexp));
+ match_op_pcre* op = (match_op_pcre*)allocOutput(ctx, len);
+
+ /* Setup op */
+ if(!op)
+ return R_NOMEM;
+
+ memset(op, 0, len);
+ op->header.len = len;
+ op->header.type = kMatchPcre;
+
+ op->options = PCRE_DOLLAR_ENDONLY;
+
+ /* TODO: Do we need to make an option for PCRE_MULTILINE? */
+ if(!(ctx->code->curOptions & f_line))
+ op->options |= PCRE_DOTALL;
+ if(!(ctx->code->curOptions & f_case))
+ op->options |= PCRE_CASELESS;
+
+ ret = testRegexp(script, regexp, op->options);
+ if(ret < 0)
+ return ret;
+
+ /* Copy the uncompiled regular expression onto the ops stack */
+ strcpy(op->pattern, regexp);
+
+ return R_OK;
+}
+
+static void pushText(compilecontext* ctx, const char* string, size_t len)
+{
+ /* Allocate */
+ text_op* op = (text_op*)allocOutput(ctx, sizeof(text_op) + (sizeof(char) * (len)));
+
+ /* Setup op */
+ if(op)
+ {
+ op->len = len;
+
+ /* Copy the replacement string onto the ops stack */
+ /* TODO: Get this ready for binary replacements */
+ strncpy((char*)op->string, string, len);
+ op->string[len] = 0;
+ }
+}
+
+static void pushVar(compilecontext* ctx, const char* name, size_t lenName)
+{
+ /* Allocate */
+ size_t len = lenName + 1;
+ var_op* op = (var_op*)allocOutput(ctx, sizeof(var_op) + (sizeof(char) * len));
+
+ /* Setup op */
+ if(op)
+ {
+ op->len = len;
+
+ /* Copy the variable name onto the ops stack */
+ /* TODO: Get this ready for binary replacements */
+ memcpy(op->name, name, lenName);
+ op->name[lenName] = 0;
+ }
+}
+
+
+/* ----------------------------------------------------------------------
+// FUNCTIONS
+*/
+
+/* Keeps track of current functions seen */
+typedef struct funcdef
+{
+ char name[kMaxIdentifier + 1];
+ uint key;
+}
+funcdef;
+
+typedef struct _funcdefs
+{
+ uint alloc;
+ uint cur;
+ funcdef defs[1];
+}
+funcdefs;
+
+/* Add a function to the stack */
+static bool addFunction(funcdefs** ppdefs, const char* name, size_t len, uint key)
+{
+ /* Do allocation if necessary */
+ if(!*ppdefs || (*ppdefs)->cur >= (*ppdefs)->alloc)
+ {
+ uint alloc = *ppdefs ? (*ppdefs)->alloc : 0;
+ uint cur = *ppdefs ? (*ppdefs)->cur : 0;
+
+ alloc += 0x10;
+
+ *ppdefs = (funcdefs*)reallocf(*ppdefs, sizeof(funcdefs) + (sizeof(funcdef) * alloc));
+ if(!*ppdefs) return false;
+
+ (*ppdefs)->alloc = alloc;
+ (*ppdefs)->cur = cur;
+ }
+
+ /* Push it on the back */
+ strlcpy((*ppdefs)->defs[(*ppdefs)->cur].name, name, min(kMaxIdentifier, len) + 1);
+ (*ppdefs)->defs[(*ppdefs)->cur].key = key;
+ (*ppdefs)->cur++;
+
+ return true;
+}
+
+/* Check if a function exists */
+static uint findFunction(funcdefs* pdefs, const char* name, size_t len)
+{
+ if(pdefs)
+ {
+ char funcname[kMaxIdentifier + 1];
+ size_t i;
+
+ strlcpy(funcname, name, min(len, kMaxIdentifier) + 1);
+
+ /* Just loop through and compare names */
+ for(i = 0; i < pdefs->cur; i++)
+ {
+ if(!strcasecmp(pdefs->defs[i].name, funcname))
+ return pdefs->defs[i].key;
+ }
+ }
+
+ return INVALID_PTR;
+}
+
+/* ----------------------------------------------------------------------
+// SYNTAX FUNCTIONS
+*/
+
+/* Is a character escaped or not? */
+bool isEscaped(const char* str, const char* posi)
+{
+ /*
+ Checks for a backslash before
+ but note that backslash can be escaped to so...
+ */
+ bool bEscaped = false;
+ while(posi > str && posi[-1] == kEscapeChar)
+ {
+ bEscaped = !bEscaped;
+ posi--;
+ }
+
+ return bEscaped;
+}
+
+
+/* Split a tag match into two for later use */
+char* splitTagMatch(r_script* script, char* regexp)
+{
+ char* second = regexp;
+ while(second = strchr(second, kTagDelim))
+ {
+ uint escs = 0;
+ if(!isEscaped(regexp, second))
+ {
+ second[0] = '\0';
+ return second + 1;
+ }
+
+ second++;
+ }
+
+ scriptSetError(script, "Couldn't find tags in match (separate with '%c').", kTagDelim);
+ return NULL;
+}
+
+
+/* Eat spaces and comments */
+static bool compileSpace(compilecontext* ctx)
+{
+ /* Eat white space and comments here */
+ while(isspace(ctx->in[0]) || ctx->in[0] == '#')
+ {
+ /* Comments ... */
+ if(ctx->in[0] == '#')
+ {
+ /* Eat rest off line */
+ while(ctx->in[0] != '\n' && ctx->in[0] != '\0')
+ ctx->in++;
+ }
+
+ ctx->in++;
+ }
+
+ /* Return true if not end of file */
+ return ctx->in[0] != '\0';
+}
+
+int compileEncoding(r_script* script, compilecontext* ctx)
+{
+ if(!memcmp(ctx->in, kEncUTF8, countof(kEncUTF8)))
+ {
+ ctx->in += countof(kEncUTF8);
+ }
+ else if(!memcmp(ctx->in, kEncUCS2_L, countof(kEncUCS2_L)) ||
+ !memcmp(ctx->in, kEncUCS2_R, countof(kEncUCS4_R)) ||
+ !memcmp(ctx->in, kEncUCS4_L, countof(kEncUCS4_L)) ||
+ !memcmp(ctx->in, kEncUCS4_R, countof(kEncUCS4_R)))
+ {
+ scriptSetError(script, "unsupported unicode encoding");
+ return R_SYNTAX;
+ }
+
+ return R_OK;
+}
+
+/* Compile a single statement */
+int compileStatement(r_script* script, compilecontext* ctx)
+{
+ /* Some MACROS */
+ #define SYNTAX_ERROR(s) \
+ do{ \
+ scriptSetError(script, s); \
+ RETURN(R_SYNTAX); \
+ } while(0)
+
+ #define SYNTAX_ERROR_1(s, a1) \
+ do{ \
+ scriptSetError(script, s, a1); \
+ RETURN(R_SYNTAX); \
+ } while(0)
+
+ /* Jump to cleanup label instead of return */
+ #define RETURN(r) \
+ do { \
+ retv = r; \
+ goto cleanup; \
+ } while (0)
+
+ const char* end;
+ int retv = R_OK;
+
+ {
+ int i;
+
+ if(ctx->curStatement != s_opbrace && ctx->curStatement != s_clbrace)
+ ctx->lastStatement = ctx->curStatement;
+
+ ctx->curStatement = s_none;
+ ctx->curName = NULL;
+ ctx->lenName = 0;
+ ctx->curFlags = 0;
+ ctx->curFlagData = NULL;
+ ctx->lenFlagData = 0;
+ ctx->curData = NULL;
+ ctx->lenData = 0;
+
+
+ do
+ {
+ /* Eat all whitespace and comments */
+ compileSpace(ctx);
+
+ /* Check for end of input */
+ if(ctx->in[0] == 0)
+ RETURN(R_OK);
+
+ /* Check for Statement */
+ if(!strcspn(ctx->in, kValidIdentifier))
+ end = ctx->in + strspn(ctx->in, kValidIdentifier);
+
+ /* Check for braces */
+ else if(!strcspn(ctx->in, kValidBrace))
+ end = ctx->in + 1; /* Brace always just one character */
+
+ /* Anything else is bad */
+ else
+ SYNTAX_ERROR_1("Unexpected character \'%c\'.", ctx->in[0]);
+
+ /* Okay now determine which statement we got */
+ for(i = 0; i < countof(kAllStatements); i++)
+ {
+ if(!strncasecmp(kAllStatements[i].text, ctx->in, end - ctx->in))
+ {
+ /* Check the context */
+ if(kAllStatements[i].context != SYNTAX_SPECIAL &&
+ !(ctx->code->curContext & kAllStatements[i].context))
+ SYNTAX_ERROR_1("\'%s\' not allowed here.", kAllStatements[i].text);
+
+ ctx->curStatement = kAllStatements[i].syntax;
+ break;
+ }
+ }
+
+
+ /* Check that we got a statement
+ if not then it should be a function call */
+ if(ctx->curStatement == s_none)
+ {
+ ctx->curStatement = s_call;
+ ctx->curName = ctx->in;
+ ctx->lenName = end - ctx->in;
+ }
+
+
+ /* Make sure it's what we were expecting */
+ if(ctx->nextStatement != s_none &&
+ ctx->nextStatement != ctx->curStatement)
+ {
+ /* If braces were required but not found, then do an impliedBlock */
+ if(ctx->nextStatement == s_opbrace &&
+ kAllStatements[ctx->lastStatement].context & SYNTAX_BLOCK)
+ {
+ ctx->nextStatement = ctx->curStatement;
+ ctx->curStatement = s_implied;
+ end = ctx->in;
+ }
+
+ /* Otherwise it's an error */
+ else
+ {
+ SYNTAX_ERROR_1("Expecting \'%s\' here.", kAllStatements[ctx->nextStatement].text);
+ }
+ }
+
+
+ ctx->in = end;
+ ctx->nextStatement = s_none;
+
+ /* We process braces here */
+ if(ctx->curStatement == s_opbrace ||
+ ctx->curStatement == s_implied)
+ {
+ PUSH_CODESTACK(ctx);
+ ctx->code->curContext |= SYNTAX_INBRACE;
+
+ if(ctx->lastStatement == s_function)
+ ctx->code->curContext |= SYNTAX_INFUNCTION;
+
+ if(ctx->curStatement == s_implied)
+ ctx->code->curContext |= SYNTAX_IMPLIED;
+
+ ctx->curStatement = s_none;
+ continue;
+ }
+
+ /* See if we need to pop any codestacks for implied blocks */
+ while(ctx->code->curContext & SYNTAX_IMPLIED &&
+ ctx->code->numStatements > 0)
+ {
+ /* Only should be one statement on an implied block */
+ ASSERT(ctx->code->numStatements == 1);
+ POP_CODESTACK(ctx);
+ }
+
+ /* Closing braces here */
+ if(ctx->curStatement == s_clbrace)
+ {
+ POP_CODESTACK(ctx);
+ ctx->curStatement = s_none;
+ continue;
+ }
+
+ }
+ while(ctx->curStatement == s_none);
+
+
+ /* Okay now do name if we have one */
+ if(kAllStatements[ctx->curStatement].args & ARGUMENT_NAME && !ctx->curName)
+ {
+ compileSpace(ctx);
+
+ /* Now look for a normal or name flag */
+ if(!strcspn(ctx->in, kValidIdentifier))
+ {
+ end = ctx->in + strspn(ctx->in, kValidIdentifier);
+
+ /* Any flags not found assume it's an identifier */
+ ctx->curName = ctx->in;
+ ctx->lenName = end - ctx->in;
+
+ if(ctx->lenName > kMaxIdentifier)
+ SYNTAX_ERROR("Maximum length for an identifier is 40 characters.");
+
+ ctx->in = end;
+ }
+ }
+
+
+ /* Eat the next little bit of whitespace */
+ compileSpace(ctx);
+
+ /* Okay now look for flags start */
+ if(ctx->in[0] == kFlagsStart)
+ {
+ ctx->in++;
+
+ while(1)
+ {
+ compileSpace(ctx);
+
+
+ /* Check for a number flag */
+ if(!strcspn(ctx->in, kValidNum))
+ {
+ if(ctx->curFlagData)
+ SYNTAX_ERROR("Invalid flags.");
+
+ /* If found then just grab and go */
+ ctx->curFlags |= f_num;
+ ctx->curFlagData = ctx->in;
+ ctx->lenFlagData = strspn(ctx->in, kValidNum);
+
+ ctx->in += ctx->lenFlagData;
+ }
+
+ /* Now look for a normal flag */
+ else if(!strcspn(ctx->in, kValidIdentifier))
+ {
+ bool found = false;
+ end = ctx->in + strspn(ctx->in, kValidIdentifier);
+
+ /* Okay now try and map out that flag */
+ for(i = 0; i < countof(kAllFlags); i++)
+ {
+ if(!strncasecmp(kAllFlags[i].text, ctx->in, end - ctx->in))
+ {
+ found = true;
+ ctx->curFlags |= kAllFlags[i].flag;
+ break;
+ }
+ }
+
+ /* Any flags not found assume it's an identifier */
+ if(!found)
+ {
+ if(ctx->curName != NULL)
+ SYNTAX_ERROR("Invalid flags.");
+
+ ctx->curName = ctx->in;
+ ctx->lenName = end - ctx->in;
+ }
+
+ ctx->in = end;
+ }
+
+ /* End ) of flags */
+ else if(ctx->in[0] == kFlagsEnd)
+ {
+ ctx->in++;
+ break;
+ }
+
+ /* Separator , between flags */
+ else if(ctx->in[0] == kFlagDelim)
+ ctx->in++;
+
+ else
+ SYNTAX_ERROR("Expected a flag.");
+ }
+
+ /* Now check the flags */
+ if((kAllStatements[ctx->curStatement].flags | ctx->curFlags)
+ != kAllStatements[ctx->curStatement].flags)
+ SYNTAX_ERROR("Invalid flags for this statement.");
+
+ }
+
+
+ /* Okay now do data processing */
+ if(kAllStatements[ctx->curStatement].args & ARGUMENT_DATA)
+ {
+ const char* delim;
+ compileSpace(ctx);
+
+ /* Check for a delimiter */
+ if(delim = strchr(kValidDelim, *(ctx->in)))
+ {
+ ctx->in++;
+ end = ctx->in;
+
+ /* Find end of data but checking for escaped delimeters */
+ while(1)
+ {
+ end = strchr(end, *delim);
+
+ if(!end)
+ SYNTAX_ERROR("Couldn't find end of data for this statement");
+
+ if(!isEscaped(ctx->in, end))
+ break;
+
+ end++;
+ }
+
+
+ ctx->curData = ctx->in;
+ ctx->lenData = (end - ctx->in);
+
+ ctx->in = end + 1;
+ }
+ else
+ {
+ SYNTAX_ERROR("Expected data for this statement.");
+ }
+ }
+
+ if(ctx->lenName > kMaxIdentifier)
+ SYNTAX_ERROR("Maximum length for an identifier is 40 characters.");
+
+ ctx->code->numStatements++;
+ }
+
+ /* done! */
+
+cleanup:
+ return retv;
+}
+
+static uint getLine(const char* beg, const char* cur)
+{
+ size_t ret = 1;
+ while(beg <= cur)
+ {
+ if(*beg == '\n')
+ ret++;
+
+ beg++;
+ }
+
+ return ret;
+}
+
+int compilerRun(r_script* script, const char* data)
+{
+
+ int retv = R_OK; /* used by RETURN macro */
+ funcdefs* pFunctions = NULL;
+ char* regexp = NULL;
+
+ /* We allocate the main instruction buffer */
+ compilecontext ctx;
+ memset(&ctx, 0, sizeof(ctx));
+
+ ctx.in = data;
+ ctx.curKey = 0x00000100;
+ ctx.beg = (vmop_t*)malloc(OPS_BUFFER_SIZE);
+ ctx.alloc = OPS_BUFFER_SIZE;
+ ctx.code = pushCodeStack(NULL);
+
+ if(!ctx.beg || !ctx.code)
+ RETURN(R_NOMEM);
+
+#ifdef _DEBUG
+ {
+ int i;
+ /* Do a check here! Index should be equal to */
+ /* command name for each statement */
+ for(i = 0; i < countof(kAllStatements); i++)
+ ASSERT(i == kAllStatements[i].syntax);
+ }
+#endif
+
+
+ /*
+ Push a first empty pops stack
+ We have to have one to pop below
+ */
+ PUSH_CODESTACK(&ctx);
+ ctx.code->curContext = SYNTAX_INROOT;
+
+ /* Push our signature of 4 nop bytes */
+ PUSH_OP(o_nop);
+ PUSH_OP(o_nop);
+ PUSH_OP(o_nop);
+ PUSH_OP(o_nop);
+
+ /* Set up initial stack frame */
+ PUSH_OP_2(o_mov, r_bp, r_sp);
+ /* Push the beginning of the main loop here */
+ PUSH_OP_1(o_pos, ARG_MAKE_VALUE(0L));
+ PUSH_OP_2(o_mov, r_ac, ARG_MAKE_VALUE(0L));
+
+ retv = compileEncoding(script, &ctx);
+ if(retv < 0)
+ RETURN(retv);
+
+ while(ctx.in[0] != '\0')
+ {
+ if(ctx.cbops + 0x200 >= ctx.alloc)
+ moreOutput(&ctx);
+
+ /* Here we check if we have enough memory */
+ if(ctx.failed)
+ RETURN(R_NOMEM);
+
+ /* Get and parse the current statement */
+ retv = compileStatement(script, &ctx);
+ if(retv < 0)
+ RETURN(retv);
+
+ if(ctx.curStatement == s_none)
+ continue;
+
+
+
+
+ /* Now we pop the commit end code from previous statement */
+ commitCodeStack(ctx.code);
+
+ /* Okay now do the rest of the statements */
+ switch(ctx.curStatement)
+ {
+
+
+ /*//////////////////////////////////////////////////////
+ // function
+ */
+ case s_function:
+ {
+ /* SYNTAX ----------------------------------------
+ //
+ // function(name)
+ // {
+ // .....
+ // }
+ */
+
+ uint key, keyJmp;
+
+ /* Check that we got a name */
+ if(!(ctx.curName && ctx.lenName))
+ SYNTAX_ERROR("'function' needs a name");
+
+ /* We need a opening brace next */
+ ctx.nextStatement = s_opbrace;
+
+
+ key = ARG_MAKE_VALUE(ctx.curKey++);
+
+ /* Put the function name at current op pos */
+ if(!addFunction(&pFunctions, ctx.curName,
+ ctx.lenName, key))
+ RETURN(R_NOMEM);
+
+
+
+ /* OPS --------------------------------------------
+ //
+ // jmp <1>
+ // pos <function>
+ // push bp
+ // mov bp, r_sp
+ // etc.
+ // ......
+ // etc.
+ // mov fe, 1
+ // mov sp, bp
+ // pop bp
+ // ret
+ // <1>
+ */
+
+ keyJmp = ARG_MAKE_VALUE(ctx.curKey++);
+
+ PUSH_OP_1(o_jmp, keyJmp);
+ PUSH_OP_1(o_pos, key);
+ PUSH_OP_1(o_push, r_bp);
+ PUSH_OP_2(o_mov, r_bp, r_sp);
+
+
+ PUSH_ROP_1(o_pos, keyJmp);
+ PUSH_ROP(o_ret);
+ PUSH_ROP_1(o_pop, r_bp);
+ PUSH_ROP_2(o_mov, r_sp, r_bp);
+ PUSH_ROP_2(o_mov, r_fe, ARG_MAKE_VALUE(1));
+
+ }
+ break;
+
+
+ /*//////////////////////////////////////////////////////
+ // loop
+ */
+ case s_loop:
+ {
+
+ uint key, key2;
+
+ /* SYNTAX ----------------------------------------
+ //
+ // loop
+ // {
+ // .....
+ // }
+ */
+
+ /* We need a opening brace next */
+ ctx.nextStatement = s_opbrace;
+
+
+
+ /* OPS --------------------------------------------
+ //
+ // push ac
+ // <1>
+ // mov ac, 0
+ // .....
+ // pop x2 pop the previously pushed action value
+ // test ac test current action
+ // jne <2> if action
+ // mov x2, ac then overide previous action value
+ // <2>
+ // push x2 push back changed action value
+ // je <1>
+ // pop ac pop out the
+ */
+
+ key = ARG_MAKE_VALUE(ctx.curKey++);
+ key2 = ARG_MAKE_VALUE(ctx.curKey++);
+
+ PUSH_OP_1(o_push, r_ac);
+ PUSH_OP_1(o_pos, key);
+ PUSH_OP_2(o_mov, r_ac, ARG_MAKE_VALUE(0L));
+
+ PUSH_ROP_1(o_pop, r_ac);
+ PUSH_ROP_1(o_je, key);
+ PUSH_ROP_1(o_push, r_x2);
+ PUSH_ROP_1(o_pos, key2);
+ PUSH_ROP_2(o_mov, r_x2, r_ac);
+ PUSH_ROP_1(o_jne, key2);
+ PUSH_ROP_1(o_test, r_ac);
+ PUSH_ROP_1(o_pop, r_x2);
+ }
+ break;
+
+
+ /*//////////////////////////////////////////////////////
+ // once
+ */
+ case s_once:
+ {
+ uint keyOnce, key1, key5;
+
+ /* SYNTAX ----------------------------------------
+ //
+ // once
+ // {
+ // .....
+ // }
+ */
+
+ /* We need a opening brace next */
+ ctx.nextStatement = s_opbrace;
+
+
+
+ /* OPS --------------------------------------------
+ //
+ // test mem(value) Pull in the flag from memory
+ // jne <1> If not present then jump to containing code
+ // mov fe, 0 Otherwise set fail flag
+ // jmp <5> And skip the match
+ // <1>
+ // .....
+ //
+ // mov mem(value), 1
+ // <5>
+ */
+
+
+ keyOnce = ARG_MAKE_MEMORY(ctx.curKey++);
+ key1 = ARG_MAKE_VALUE(ctx.curKey++);
+ key5 = ARG_MAKE_VALUE(ctx.curKey++);
+
+
+ /* Once code */
+ PUSH_OP_1(o_test, keyOnce);
+ PUSH_OP_1(o_jne, key1);
+ PUSH_OP_2(o_mov, r_fe, ARG_MAKE_VALUE(0));
+ PUSH_OP_1(o_jmp, key5);
+ PUSH_OP_1(o_pos, key1);
+
+
+ PUSH_ROP_1(o_pos, key5);
+ PUSH_ROP_2(o_mov, keyOnce, ARG_MAKE_VALUE(1));
+ }
+ break;
+
+
+
+ /*/////////////////////////////////////////////////////
+ // call
+ */
+ case s_call:
+ {
+ uint key;
+
+ /* SYNTAX ----------------------------------------
+ //
+ // call(name)
+ */
+
+ /* Check that we got a name */
+ if(!(ctx.curName && ctx.lenName))
+ SYNTAX_ERROR("'call' needs a function name");
+
+ /* Find the function */
+ key = findFunction(pFunctions, ctx.curName,
+ ctx.lenName);
+
+ if(key == INVALID_PTR)
+ SYNTAX_ERROR("function does not exist");
+
+
+ /* OPS --------------------------------------------
+ //
+ // call <funcaddr>
+ */
+
+ PUSH_OP_1(o_call, key);
+ }
+ break;
+
+
+ /*///////////////////////////////////////////////////////
+ // return
+ */
+ case s_return:
+ {
+ /* SYNTAX ----------------------------------------
+ //
+ // return(0)
+ */
+
+ uint code = ARG_MAKE_VALUE(1);
+
+ /* Check for the number */
+ if(ctx.curFlags & f_num)
+ {
+ if(ctx.lenFlagData > 1 || (ctx.curFlagData[0] != '0' && ctx.curFlagData[0] != '1'))
+ SYNTAX_ERROR("Return code must be 0 or 1");
+
+ if(ctx.curFlagData[0] == '0')
+ code = ARG_MAKE_VALUE(0);
+ }
+
+ /* OPS --------------------------------------------
+ //
+ // mov fe, 0 (or 1 depending on code)
+ // mov sp, r_bp
+ // pop bp
+ // ret
+ */
+
+ PUSH_OP_2(o_mov, r_fe, code);
+ PUSH_OP_2(o_mov, r_sp, r_bp);
+ PUSH_OP_1(o_pop, r_bp);
+ PUSH_OP(o_ret);
+ }
+ break;
+
+
+
+ /*//////////////////////////////////////////////////////
+ // end
+ */
+ case s_end:
+ {
+
+ /* SYNTAX ----------------------------------------
+ //
+ // end
+ */
+
+
+ /* OPS --------------------------------------------
+ //
+ // stop
+ */
+
+ PUSH_OP(o_text);
+ pushText(&ctx, NULL, 0);
+ PUSH_OP_1(o_stop, ARG_MAKE_VALUE(0));
+ }
+ break;
+
+
+
+ /*//////////////////////////////////////////////////////
+ // end
+ */
+ case s_stop:
+ {
+
+ /* SYNTAX ----------------------------------------
+ //
+ // stop "message"
+ */
+
+ /* OPS --------------------------------------------
+ //
+ // text "message"
+ // stop
+ */
+
+ PUSH_OP(o_text);
+ pushText(&ctx, ctx.curData, ctx.lenData);
+ PUSH_OP_1(o_stop, ARG_MAKE_VALUE(1));
+ }
+ break;
+
+
+ /*/////////////////////////////////////////////////////
+ // match
+ */
+ case s_match:
+ {
+
+ /* SYNTAX ----------------------------------------
+ //
+ // match(not, once) "regexp"
+ */
+
+ uint keyOnce, keyWatermark, keyJmp1, keyJmp2, keyJmp3,
+ keyJmp4, keyJmp5, keyJmp6, keyJmp7, groupNum,
+ key1, key4, key5, key9;
+
+ /* Get the flags */
+ bool bNot = ctx.curFlags & f_not ? true : false;
+ bool bOnce = ctx.curFlags & f_once ? true : false;
+ bool bHas = ctx.curFlags & f_find ? true : false;
+ bool bTag = ctx.curFlags & f_tag ? true : false;
+
+ /* Check that we got data */
+ if(!ctx.curData || !ctx.lenData)
+ SYNTAX_ERROR("'match' needs a regular expression");
+
+ /* We need a opening brace next */
+ ctx.nextStatement = s_opbrace;
+
+ regexp = strndup(ctx.curData, ctx.lenData);
+ if(!regexp)
+ RETURN(R_NOMEM);
+
+ groupNum = 0;
+
+ if(ctx.curFlagData && ctx.lenFlagData)
+ {
+ if(ctx.lenFlagData > 1)
+ SYNTAX_ERROR("Group specifier must be between 0 and 9.");
+
+ if(bHas)
+ SYNTAX_ERROR("Group specifier invalid with 'has' flag.");
+
+
+ /* Get the number to use */
+ groupNum = ctx.curFlagData[0] - '0';
+ }
+
+
+ /* OPS --------------------------------------------
+ //
+ // push x1 Save limits
+ // push y1 " "
+
+ // test mem(value) (once) Pull in the flag from memory
+ // jne <1> (once) If not present then jump to match code
+ // mov fe, 0 (once) Otherwise set fail flag
+ // jmp <5> (once) And skip the match
+ // <1> (once)
+
+ // mov x6, mem(key_value) (watermark) Get the watermark
+ // cmp x1, r_x6 (watermark) If watermark higher than match area
+ // mov fe, r_fg (watermark)
+ // jne <2> (watermark) skip
+ // mov x6, r_x1 (watermark) Otherwise bring up watermark to match area
+ // <2> (watermark)
+
+ // match x6, r_y1 Do match
+ // mov x4, fe
+
+ // <5> (once)
+
+ // jne <3> If match failed skip set below
+
+ // mov ac, 1 Set action flag
+
+ // cmp cg, 2 See if we have enough groups
+ // mov fe, r_fg ""
+ // jne <7>
+ // mov fe, 0 If not then set failed
+ // jmp <3> And skip to failed part
+ // <7>
+ // mov x1, r_b2 Set new limit for inside
+ // mov y1, e2 different registers (depending on number parameter)
+
+ // mov x2, r_b0 (watermark) Get the start of batch
+ // add x2, 1 (watermark) Add one to it
+ // mov mem(value), x2 (watermark) Stash it away in memory
+
+ // <3>
+
+ // cmp x4, 1 (not) Compare success against 0 or 1 depending on not
+ // push fe
+ // jne <4> Skip if no match
+
+ // mov mem(value), 1 (once)
+
+ // .....
+
+ // <4>
+ // pop fe
+
+ // pop y1
+ // pop x1
+ */
+
+ keyOnce = ARG_MAKE_MEMORY(ctx.curKey++);
+ keyWatermark = ARG_MAKE_STACK(ctx.curKey++);
+ keyJmp1 = ARG_MAKE_VALUE(ctx.curKey++);
+ keyJmp2 = ARG_MAKE_VALUE(ctx.curKey++);
+ keyJmp3 = ARG_MAKE_VALUE(ctx.curKey++);
+ keyJmp4 = ARG_MAKE_VALUE(ctx.curKey++);
+ keyJmp5 = ARG_MAKE_VALUE(ctx.curKey++);
+ keyJmp6 = ARG_MAKE_VALUE(ctx.curKey++);
+ keyJmp7 = ARG_MAKE_VALUE(ctx.curKey++);
+
+ /* Save for later */
+ PUSH_OP_1(o_push, r_x1);
+ PUSH_OP_1(o_push, r_y1);
+
+ if(bOnce)
+ {
+ /* Once code */
+ PUSH_OP_1(o_test, keyOnce);
+ PUSH_OP_1(o_jne, keyJmp1);
+ PUSH_OP_2(o_mov, r_fe, ARG_MAKE_VALUE(0));
+ PUSH_OP_1(o_jmp, keyJmp5);
+ PUSH_OP_1(o_pos, keyJmp1);
+ }
+
+ /* Watermark code */
+ PUSH_OP_2(o_mov, r_x6, keyWatermark);
+ PUSH_OP_2(o_cmp, r_x1, r_x6);
+ PUSH_OP_2(o_mov, r_fe, r_fg);
+ PUSH_OP_1(o_jne, keyJmp2);
+ PUSH_OP_2(o_mov, r_x6, r_x1);
+ PUSH_OP_1(o_pos, keyJmp2);
+
+
+ /* Is it a simple match? */
+ if(!bTag)
+ {
+ /* Actual match */
+ PUSH_OP_2(o_match, r_x6, r_y1);
+ retv = pushMatch(script, &ctx, regexp);
+ if(retv < 0) RETURN(retv);
+ }
+
+ /* Or the very complicated tag statement */
+ else
+ {
+ /* Split the regular expression */
+ char* second = splitTagMatch(script, regexp);
+ if(!second) RETURN(R_SYNTAX);
+
+
+ /* (See code docs in tag.txt file) */
+ key1 = ARG_MAKE_VALUE(ctx.curKey++);
+ key4 = ARG_MAKE_VALUE(ctx.curKey++);
+ key5 = ARG_MAKE_VALUE(ctx.curKey++);
+ key9 = ARG_MAKE_VALUE(ctx.curKey++);
+
+
+ /* Setup */
+ PUSH_OP_2(o_mov, r_x2, ARG_MAKE_VALUE(0));
+ PUSH_OP_2(o_mov, r_x0, r_x6);
+ PUSH_OP_2(o_mov, r_y0, r_y1);
+
+ /* Top of loop */
+ PUSH_OP_1(o_pos, key1);
+
+ /* Start code */
+ PUSH_OP_2(o_match, r_x0, r_y1);
+
+ retv = pushMatch(script, &ctx, regexp);
+ if(retv < 0) RETURN(retv);
+
+ PUSH_OP_1(o_jne, key5);
+ PUSH_OP_2(o_cmp, r_b0, r_y0);
+ PUSH_OP_1(o_je, key5);
+ PUSH_OP_2(o_mov, r_fe, r_fg);
+ PUSH_OP_1(o_je, key5);
+
+ PUSH_OP_2(o_cmp, r_x2, ARG_MAKE_VALUE(0));
+ PUSH_OP_1(o_jne, key4);
+ PUSH_OP_2(o_mov, r_x5, r_b0);
+ PUSH_OP_2(o_mov, r_y5, r_e0);
+ PUSH_OP_2(o_mov, r_y0, r_e0);
+ PUSH_OP_1(o_pos, key4);
+
+ PUSH_OP_2(o_mov, r_x2, ARG_MAKE_VALUE(0));
+ PUSH_OP_2(o_mov, r_x0, r_e0);
+
+ /* Endless loop check */
+ PUSH_OP_2(o_cmp, r_y2, ARG_MAKE_VALUE(0x00100000));
+ PUSH_OP_1(o_je, key5);
+
+ /* End code */
+ PUSH_OP_2(o_match, r_y0, r_y1);
+ retv = pushMatch(script, &ctx, second);
+ if(retv < 0) RETURN(retv);
+
+ PUSH_OP_1(o_jne, key5);
+ PUSH_OP_2(o_mov, r_y0, r_e0);
+ PUSH_OP_2(o_mov, r_x7, r_b0);
+ PUSH_OP_2(o_mov, r_y7, r_e0);
+
+ /* Locks check */
+ PUSH_OP_2(o_check, r_y5, r_x7);
+ PUSH_OP_2(o_mov, r_x2, r_fe);
+
+ /* End of loop */
+ PUSH_OP_1(o_jmp, key1);
+ PUSH_OP_1(o_pos, key5);
+
+ /* Wrap up */
+ PUSH_OP_2(o_cmp, r_x2, ARG_MAKE_VALUE(0));
+ PUSH_OP_1(o_je, key9);
+ PUSH_OP_2(o_mov, r_b0, r_x5);
+ PUSH_OP_2(o_mov, r_e0, r_y7);
+ PUSH_OP_2(o_mov, r_b1, r_x5);
+ PUSH_OP_2(o_mov, r_e1, r_y5);
+ PUSH_OP_2(o_mov, r_b2, r_y5);
+ PUSH_OP_2(o_mov, r_e2, r_x7);
+ PUSH_OP_2(o_mov, r_b3, r_x7);
+ PUSH_OP_2(o_mov, r_e3, r_y7);
+ PUSH_OP_2(o_mov, r_cg, ARG_MAKE_VALUE(4));
+ PUSH_OP_1(o_pos, key9);
+ PUSH_OP_2(o_mov, r_fe, r_x2);
+
+ }
+
+
+ PUSH_OP_2(o_mov, r_x4, r_fe);
+
+ if(bOnce)
+ PUSH_OP_1(o_pos, keyJmp5);
+
+ /* Skip all the rest of the setup if failed */
+ PUSH_OP_1(o_jne, keyJmp3);
+
+ /* Set action flag */
+ PUSH_OP_2(o_mov, r_ac, ARG_MAKE_VALUE(1));
+
+ if(!bHas)
+ {
+ /* Group validation code */
+ PUSH_OP_2(o_cmp, ARG_MAKE_VALUE(groupNum + 1), r_cg);
+ PUSH_OP_2(o_mov, r_fe, r_fg);
+ PUSH_OP_1(o_jne, keyJmp7);
+ PUSH_OP_2(o_mov, r_fe, ARG_MAKE_VALUE(0));
+ PUSH_OP_1(o_jmp, keyJmp3);
+ PUSH_OP_1(o_pos, keyJmp7);
+
+ /* Now depending on group number set do this we set a set of registers to check */
+ PUSH_OP_2(o_mov, r_x1, ARG_MAKE_REGISTER(ARG_GET_REGISTER(r_b0) + groupNum));
+ PUSH_OP_2(o_mov, r_y1, ARG_MAKE_REGISTER(ARG_GET_REGISTER(r_e0) + groupNum));
+ }
+
+ /* Watermark */
+ PUSH_OP_2(o_mov, keyWatermark, r_e0);
+
+ /* End of setup code */
+ PUSH_OP_1(o_pos, keyJmp3);
+
+ /* Implement 'not' */
+ PUSH_OP_2(o_cmp, r_x4, ARG_MAKE_VALUE(bNot ? 0 : 1));
+ PUSH_OP_1(o_push, r_fe);
+ PUSH_OP_1(o_jne, keyJmp4);
+
+ /* Now we're inside */
+ if(bOnce)
+ PUSH_OP_2(o_mov, keyOnce, ARG_MAKE_VALUE(1));
+
+
+ /* Wrap up code */
+ PUSH_ROP_1(o_pop, r_x1);
+ PUSH_ROP_1(o_pop, r_y1);
+ PUSH_ROP_1(o_pop, r_fe);
+ PUSH_ROP_1(o_pos, keyJmp4);
+
+
+ free(regexp);
+ regexp = NULL;
+ }
+ break;
+
+
+ /*//////////////////////////////////////////////////////
+ // replace
+ */
+ case s_replace:
+ {
+
+ /* SYNTAX ----------------------------------------
+ //
+ // replace "replacetext"
+ */
+
+ uint key;
+
+ /* Check that we got data */
+ if(!ctx.curData)
+ SYNTAX_ERROR("'replace' needs text to replace");
+
+ /* OPS --------------------------------------------
+ //
+ // check x1, r_y1
+ // jne <1>
+ // repl x1, r_y1
+ // lock x1, r_y1
+ // <1>
+ */
+
+ key = ARG_MAKE_VALUE(ctx.curKey++);
+
+ PUSH_OP_2(o_check, r_x1, r_y1);
+ PUSH_OP_1(o_jne, key);
+
+ PUSH_OP(o_text);
+ pushText(&ctx, ctx.curData, ctx.lenData);
+ PUSH_OP_2(o_repl, r_x1, r_y1);
+
+ PUSH_OP_2(o_lock, r_x1, r_y1);
+ PUSH_OP_1(o_pos, key);
+ }
+ break;
+
+
+ /*//////////////////////////////////////////////////////
+ // lock
+ */
+ case s_lock:
+ {
+
+ /* SYNTAX ----------------------------------------
+ //
+ // lock
+ */
+
+ /* OPS --------------------------------------------
+ //
+ // lock x1, r_y1
+ */
+
+ PUSH_OP_2(o_lock, r_x1, r_y1);
+ }
+ break;
+
+
+
+ /*//////////////////////////////////////////////////////
+ // variable functions
+ */
+ case s_setvar:
+ case s_clrvar:
+ case s_addvar:
+ {
+
+ /* SYNTAX ----------------------------------------
+ //
+ // set name "value"
+ // clr name
+ // add name "value"
+ */
+
+ /* Check that we got a name */
+ if(!(ctx.curName && ctx.lenName))
+ SYNTAX_ERROR("Missing variable name");
+
+ /* Check that we got data */
+ if((ctx.curStatement == s_setvar ||
+ ctx.curStatement == s_addvar) && !ctx.curData)
+ SYNTAX_ERROR("Missing variable value");
+
+
+ /* OPS --------------------------------------------
+ */
+
+ /* If not clearing then push text */
+ if(ctx.curStatement != s_clrvar)
+ {
+ PUSH_OP(o_text);
+ pushText(&ctx, ctx.curData, ctx.lenData);
+ }
+
+ /* If not adding then clear */
+ if(ctx.curStatement != s_addvar)
+ {
+ PUSH_OP(o_clrvar);
+ pushVar(&ctx, ctx.curName, ctx.lenName);
+ }
+
+ /* If not clearing then set */
+ if(ctx.curStatement != s_clrvar)
+ {
+ PUSH_OP(o_setvar);
+ pushVar(&ctx, ctx.curName, ctx.lenName);
+ }
+ }
+ break;
+
+ /*//////////////////////////////////////////////////////
+ // else
+ */
+ case s_else:
+ {
+
+ /* SYNTAX ----------------------------------------
+ //
+ // else
+ // {
+ // .....
+ // }
+ */
+
+ uint key;
+
+ /* REMOVED the following because now else can follow any statement
+ //
+ // Make sure the previous statement was a match
+ // if(ctx.lastStatement != s_match && ctx.lastStatement != s_replace &&
+ // ctx.lastStatement != s_call)
+ // SYNTAX_ERROR("'else' must follow a match, replace or call statement");
+ */
+
+ /* Next statement must be a opening brace */
+ ctx.nextStatement = s_opbrace;
+
+ /* OPS --------------------------------------------
+ //
+ // je <1>
+ // ......
+ // <1>
+ */
+
+ key = ARG_MAKE_VALUE(ctx.curKey++);
+
+ PUSH_OP_1(o_je, key);
+ PUSH_ROP_1(o_pos, key);
+ }
+ break;
+
+ /*/////////////////////////////////////////////////////
+ // options
+ */
+ case s_message:
+ {
+ /* SYNTAX ----------------------------------------
+ //
+ // message "data"
+ */
+
+ /* Check that we got data */
+ if(!ctx.curData)
+ SYNTAX_ERROR("Missing message text.");
+
+ /* OPS --------------------------------------------
+ //
+ // text "message"
+ // stop
+ */
+
+ PUSH_OP(o_text);
+ pushText(&ctx, ctx.curData, ctx.lenData);
+ PUSH_OP(o_msg);
+ }
+ break;
+
+
+ /*/////////////////////////////////////////////////////
+ // options
+ */
+ case s_options:
+ {
+ /* SYNTAX ----------------------------------------
+ //
+ // options(case, line)
+ */
+
+ /* Save the options into the context */
+ ctx.code->curOptions = ctx.curFlags;
+ }
+ break;
+
+ default:
+ ASSERT(false);
+
+ }
+ }
+
+ /* Pop out of any implied blocks */
+ while(ctx.code->curContext & SYNTAX_IMPLIED)
+ POP_CODESTACK(&ctx);
+
+ /* copy any remaining post ops */
+ POP_CODESTACK(&ctx);
+
+ /* This is the bottom of the main loop */
+ PUSH_OP_1(o_test, r_ac);
+ PUSH_OP_1(o_je, ARG_MAKE_VALUE(0));
+
+ /* Put an end marker */
+ PUSH_OP(o_end);
+
+
+ /* Check brace syntax */
+ if(ctx.code->pPrev != NULL)
+ SYNTAX_ERROR("Not all braces matched.");
+
+ /* Put the compiled script in the script */
+ script->ops = ctx.beg;
+ script->len = ctx.cbops;
+
+ /* This fixes all the jmp and removes pos */
+ retv = compilerOptimize(script);
+
+cleanup:
+ if(retv == R_SYNTAX || retv == R_REGEXP)
+ script->errline = getLine(data, ctx.in) - 1;
+
+ /* Unwind codestack */
+ while(ctx.code)
+ ctx.code = popCodeStack(ctx.code);
+
+ if(pFunctions)
+ free(pFunctions);
+ if(regexp)
+ free(regexp);
+
+ if(retv < R_OK)
+ {
+ if(ctx.beg)
+ free(ctx.beg);
+
+ script->ops = NULL;
+ script->len = 0;
+ }
+
+ return retv;
+}
+
+
+
+int compilerOptimize(r_script* scr)
+{
+ vmop_t* op = scr->ops;
+ int retv = R_OK;
+
+ /* First find and remove all pos
+ // NOTE: land and pos are used interchangeably in this code */
+
+ uint* lands = NULL;
+ uint cur = 0;
+ uint alloc = 0;
+
+ while(*op != o_end)
+ {
+ ASSERT(op < scr->ops + scr->len);
+
+ switch(*op)
+ {
+ case o_pos:
+ {
+ if(alloc <= cur)
+ {
+ alloc += 0x40;
+ lands = (uint*)reallocf(lands, sizeof(uint) * 2 * alloc);
+ if(!lands)
+ RETURN(R_NOMEM);
+
+ }
+
+ ASSERT(ARG_TYPE(op[1]) == ARG_VAL_TYPE);
+
+ /* Position in 0 */
+ lands[cur * 2] = op - scr->ops;
+ /* key in 1 */
+ lands[(cur * 2) + 1] = *((uint*)(op + 1));
+
+ cur++;
+
+ /*
+ Okay now eat the rest of the stuff
+ total length of a pos should be 5 bytes
+ */
+ scr->len -= 5;
+ memmove(op, op + 5, scr->len - (op - scr->ops));
+ }
+ break;
+
+ default:
+ opsIterate(&op);
+ break;
+ };
+ }
+
+
+ /* Now fix all jumps and calls */
+ op = scr->ops;
+
+ while(*op != o_end)
+ {
+ ASSERT(op < scr->ops + scr->len);
+
+ switch(*op)
+ {
+ case o_pos:
+ /* Shouldn't meet any pos ops after we removed them above */
+ ASSERT(false);
+ break;
+
+ case o_jmp:
+ case o_jne:
+ case o_je:
+ case o_call:
+ {
+ uint* parg = ((uint*)(op + 1));
+ bool found = false;
+ size_t i;
+
+ /* find the key in our array */
+ for(i = 0; i < cur; i++)
+ {
+ if(lands[(i * 2) + 1] == *parg)
+ {
+ found = true;
+ *parg = ARG_MAKE_VALUE(lands[i * 2]);
+ break;
+ }
+ }
+
+ if(!found)
+ ASSERT(false && "jump without a pos");
+ }
+ /* (Note fall through) */
+
+ default:
+ opsIterate(&op);
+ break;
+ }
+ }
+
+cleanup:
+ if(lands)
+ free(lands);
+
+ return retv;
+}
+
+void opsIterate(vmop_t** ops)
+{
+ vmop_t op = *(*(ops));
+ (*ops)++;
+
+ /* increment *ops to next op point */
+ switch(op)
+ {
+ /* ops without arguments */
+ case o_end:
+ case o_nop:
+ case o_ret:
+ case o_msg:
+ break;
+
+ /* ops with one argument */
+ case o_push:
+ case o_pop:
+ case o_jmp:
+ case o_je:
+ case o_jne:
+ case o_test:
+ case o_call:
+ case o_stop:
+ INC_ARGUMENT(*ops);
+ break;
+
+ /* ops with two arguments */
+ case o_lock:
+ case o_check:
+ case o_cmp:
+ case o_add:
+ case o_sub:
+ case o_mov:
+ case o_repl:
+ INC_ARGUMENT(*ops);
+ INC_ARGUMENT(*ops);
+ break;
+
+ /* Special cases */
+ case o_match:
+ {
+ match_op* op;
+ INC_ARGUMENT(*ops);
+ INC_ARGUMENT(*ops);
+ op = (match_op*)(*ops);
+ (*ops) += match_op_size(*op);
+ }
+ break;
+
+ case o_setvar:
+ case o_clrvar:
+ {
+ var_op* op = (var_op*)(*ops);
+ (*ops) += var_op_size(*op);
+ }
+ break;
+ case o_text:
+ {
+ text_op* op;
+ op = (text_op*)(*ops);
+ (*ops) += text_op_size(*op);
+ }
+ break;
+
+ default:
+ ASSERT(false);
+ };
+
+}
+
+
+/*
+ TODO: individual ops do not need to be freed
+ any longer. Execution no longer changes them.
+*/
+
+/* Frees a set of ops */
+int opsFree(vmop_t* ops, size_t len)
+{
+ byte* cur = ops;
+
+ if(len == 0)
+ len = ~0;
+
+ while(cur < ops + len)
+ {
+ switch(*cur)
+ {
+ case o_end:
+ goto done;
+
+ default:
+ opsIterate(&cur);
+ break;
+ }
+ }
+
+done:
+ free(ops);
+ return R_OK;
+
+}
+
+
+static const char* getOpName(vmop_t op)
+{
+ #define RETOPNAME(r) case o_##r: return #r;
+ switch(op)
+ {
+ RETOPNAME(end);
+ RETOPNAME(stop);
+ RETOPNAME(nop);
+ RETOPNAME(ret);
+ RETOPNAME(push);
+ RETOPNAME(pop);
+ RETOPNAME(jmp);
+ RETOPNAME(je);
+ RETOPNAME(jne);
+ RETOPNAME(test);
+ RETOPNAME(call);
+ RETOPNAME(lock);
+ RETOPNAME(check);
+ RETOPNAME(cmp);
+ RETOPNAME(add);
+ RETOPNAME(sub);
+ RETOPNAME(mov);
+ RETOPNAME(match);
+ RETOPNAME(repl);
+ RETOPNAME(setvar);
+ RETOPNAME(clrvar);
+ RETOPNAME(pos);
+ RETOPNAME(text);
+ RETOPNAME(msg);
+
+ default:
+ return "<INVALID>";
+ }
+}
+
+static const char* getRegisterName(byte reg)
+{
+ #define RETREGNAME(r) case r_##r: return #r;
+ switch(reg)
+ {
+ RETREGNAME(fe);
+ RETREGNAME(fg);
+ RETREGNAME(fl);
+ RETREGNAME(ac);
+ RETREGNAME(sp);
+ RETREGNAME(bp);
+ RETREGNAME(b0);
+ RETREGNAME(b1);
+ RETREGNAME(b2);
+ RETREGNAME(b3);
+ RETREGNAME(b4);
+ RETREGNAME(b5);
+ RETREGNAME(b6);
+ RETREGNAME(b7);
+ RETREGNAME(b8);
+ RETREGNAME(b9);
+ RETREGNAME(e0);
+ RETREGNAME(e1);
+ RETREGNAME(e2);
+ RETREGNAME(e3);
+ RETREGNAME(e4);
+ RETREGNAME(e5);
+ RETREGNAME(e6);
+ RETREGNAME(e7);
+ RETREGNAME(e8);
+ RETREGNAME(e9);
+ RETREGNAME(cg);
+ RETREGNAME(x0);
+ RETREGNAME(x1);
+ RETREGNAME(x2);
+ RETREGNAME(x3);
+ RETREGNAME(x4);
+ RETREGNAME(x5);
+ RETREGNAME(x6);
+ RETREGNAME(x7);
+ RETREGNAME(y0);
+ RETREGNAME(y1);
+ RETREGNAME(y2);
+ RETREGNAME(y3);
+ RETREGNAME(y4);
+ RETREGNAME(y5);
+ RETREGNAME(y6);
+ RETREGNAME(y7);
+
+ default:
+ return "<INVALID>";
+ }
+};
+
+void dumpArgument(FILE* f, vmop_t* ops)
+{
+ switch(ARG_TYPE(*ops))
+ {
+ case ARG_VAL_TYPE:
+ fprintf(f, "0x%06x", ARG_GET_VALUE(*((uint*)ops)));
+ break;
+
+ case ARG_MEM_TYPE:
+ fprintf(f, "<mem:0x%06x>", ARG_GET_VALUE(*((uint*)ops)));
+ break;
+
+ case ARG_STACK_TYPE:
+ fprintf(f, "<stack:0x%06x>", ARG_GET_VALUE(*((uint*)ops)));
+ break;
+
+ case ARG_REG_TYPE:
+ fprintf(f, getRegisterName(*ops));
+ break;
+
+ default:
+ ASSERT(false);
+ }
+}
+
+/* Dump a string of ops to a stream */
+int opsDump(vmop_t* ops, FILE* f)
+{
+ vmop_t* beg = ops;
+
+ while(*ops != o_end)
+ {
+ vmop_t op = *ops;
+
+ fprintf(f, "%06x: %s ", (ops - beg), getOpName(op));
+
+ ops++;
+
+ /* Now the arguments */
+ switch(op)
+ {
+ case o_push:
+ case o_pop:
+ case o_jmp:
+ case o_je:
+ case o_jne:
+ case o_test:
+ case o_call:
+ case o_stop:
+ case o_pos:
+ dumpArgument(f, ops);
+ INC_ARGUMENT(ops);
+ break;
+
+ case o_lock:
+ case o_check:
+ case o_cmp:
+ case o_add:
+ case o_sub:
+ case o_mov:
+ case o_match:
+ case o_repl:
+ dumpArgument(f, ops);
+ fprintf(f, ", ");
+ INC_ARGUMENT(ops);
+ dumpArgument(f, ops);
+ INC_ARGUMENT(ops);
+ break;
+ };
+
+ /* Now any additional data */
+ switch(op)
+ {
+ case o_match:
+ {
+ match_op* op = (match_op*)ops;
+ if(op->type & kMatchPcre)
+ {
+ match_op_pcre* pcre = (match_op_pcre*)op;
+ fprintf(f, " %s", pcre->pattern);
+ }
+ else
+ {
+ fprintf(f, " <regexp>");
+ }
+
+ ops += match_op_size(*op);
+ }
+ break;
+
+ case o_setvar:
+ case o_clrvar:
+ {
+ var_op* vop = (var_op*)ops;
+ fprintf(f, " <%%%s>", vop->name);
+ ops += var_op_size(*vop);
+ }
+ break;
+
+ case o_test:
+ {
+ text_op* op = (text_op*)ops;
+ fprintf(f, " <%s>", op->string);
+ ops += text_op_size(*op);
+ }
+ break;
+ };
+
+
+ fprintf(f, "\n");
+ }
+
+ return R_OK;
+
+}
+
+
+
+
diff --git a/lib/execute.c b/lib/execute.c
new file mode 100644
index 0000000..3bf3c4b
--- /dev/null
+++ b/lib/execute.c
@@ -0,0 +1,1715 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+/* ----------------------------------------------------------------------
+ * Rep Execution Unit
+ * 2000-2001 Copyright, Nate Nielsen
+ */
+
+#include "common/usuals.h"
+#include "common/compat.h"
+#include "common/xstring.h"
+#include "lib/rlib.h"
+#include "priv.h"
+#include "execute.h"
+#include "ops.h"
+
+/* 100 million loops max! */
+#define MAX_PASSES 100000000
+
+/* NOTE: all *end* counts/pointers point to the last char of the actual data */
+
+/* Quick access macro for accessing VM registers */
+#define REGISTER(reg) \
+ (state->vmregs[ARG_GET_REGISTER(reg)])
+#define REGISTER2(reg, off) \
+ (state->vmregs[ARG_GET_REGISTER(reg) + (off)])
+
+/* =========================================================================
+ * MEMORY: Memory blocks are used by the code and are simply a set
+ * of numeric values indexed by key.
+ */
+
+/* _memoryAllocate: ---------------------------------------------------
+ * Helper which double the size of a 'memory' block if needed
+ */
+bool _memoryAllocate(memory* mem)
+{
+ ASSERT_PTR(mem);
+
+ if(!mem->thememory)
+ {
+ /* Allocate new set */
+ mem->thememory = (struct mem*)malloc(sizeof(struct mem) * BLOCK_SIZE);
+ if(!mem->thememory) return false;
+ mem->alloc = BLOCK_SIZE;
+ memoryClearAll(mem);
+ }
+
+ /* If no more space ... */
+ if(mem->alloc <= mem->cur)
+ {
+ /* ...double allocation */
+ size_t sz = sizeof(struct mem) * mem->alloc;
+ mem->thememory = (struct mem*)reallocf(mem->thememory, sz * 2);
+ if(!mem->thememory) return false;
+ mem->alloc *= 2;
+ }
+
+ return true;
+}
+
+
+/* _memoryFind: ----------------------------------------------------
+ * Find memory location for given key
+ */
+struct mem* _memoryFind(memory* mem, uint key)
+{
+ size_t i;
+ ASSERT_PTR(mem);
+
+ for(i = 0; i < mem->cur; i++)
+ {
+ if(key == mem->thememory[i].key)
+ return &(mem->thememory[i]);
+ }
+
+ return 0;
+}
+
+/* memoryInit: -----------------------------------------------------
+ * Initialize and or allocate a memory block
+ */
+bool memoryInit(memory* mem)
+{
+ zero(*mem);
+ return true;;
+}
+
+/* memoryFree: ----------------------------------------------------
+ * Free a memory block
+ */
+void memoryFree(memory* mem)
+{
+ if(mem)
+ {
+ ASSERT_PTR(mem);
+ if(mem->thememory)
+ free(mem->thememory);
+
+ mem->thememory = NULL;
+ }
+}
+
+/* memoryValue: --------------------------------------------------
+ * Get a value for key from memory
+ */
+uint* memoryValue(memory* mem, uint key)
+{
+ struct mem* m;
+ ASSERT_PTR(mem);
+ if(!_memoryAllocate(mem))
+ return NULL;
+
+ m = _memoryFind(mem, key);
+ if(!m)
+ {
+ /* Add new watermark */
+ mem->thememory[mem->cur].key = key;
+ mem->thememory[mem->cur].value = 0;
+ m = &(mem->thememory[mem->cur]);
+ mem->cur++;
+ }
+
+ return &(m->value);
+}
+
+/* memoryClearAll: ---------------------------------------------------
+ * Clear all values from block
+ */
+void memoryClearAll(memory* mem)
+{
+ ASSERT_PTR(mem);
+ mem->cur = 0;
+}
+
+
+/* =========================================================================
+ * DATA:
+ */
+
+/* _dataAllocate: ---------------------------------------------------
+ * Helper which double the size of a 'memory' block if needed
+ */
+bool _dataAllocate(data* dat)
+{
+ ASSERT_PTR(dat);
+
+ if(!dat->thedata)
+ {
+ /* Allocate new set */
+ dat->thedata = (struct dat*)malloc(sizeof(struct dat) * BLOCK_SIZE);
+ if(!dat->thedata) return false;
+ dat->alloc = BLOCK_SIZE;
+ dataClearAll(dat);
+ }
+
+ /* If no more space ... */
+ if(dat->alloc <= dat->cur)
+ {
+ /* ...double allocation */
+ size_t sz = sizeof(struct dat) * dat->alloc;
+ dat->thedata = (struct dat*)reallocf(dat->thedata, sz * 2);
+ if(!dat->thedata) return false;
+ dat->alloc *= 2;
+ }
+
+ return true;
+}
+
+
+/* _dataFind: ----------------------------------------------------
+ * Find memory location for given key
+ */
+struct dat* _dataFind(data* dat, void* key)
+{
+ size_t i;
+ ASSERT_PTR(dat);
+
+ for(i = 0; i < dat->cur; i++)
+ {
+ if(key == dat->thedata[i].key)
+ return &(dat->thedata[i]);
+ }
+
+ return 0;
+}
+
+/* dataInit: -----------------------------------------------------
+ * Initialize and or allocate a data block
+ */
+bool dataInit(data* dat)
+{
+ zero(*dat);
+ return true;
+}
+
+/* dataFree: ----------------------------------------------------
+ * Free a data block
+ */
+void dataFree(data* dat)
+{
+ if(dat)
+ {
+ ASSERT_PTR(dat);
+ if(dat->thedata)
+ {
+ dataClearAll(dat);
+ free(dat->thedata);
+ }
+
+ dat->thedata = NULL;
+ }
+}
+
+/* dataGetValue: --------------------------------------------------
+ * Get a value for key from data
+ */
+void* dataGetValue(data* dat, void* key)
+{
+ struct dat* d;
+ ASSERT_PTR(dat);
+
+ if(d = _dataFind(dat, key))
+ return d->value;
+
+ return NULL;
+}
+
+/* dataSetValue: --------------------------------------------------
+ * Set a value for key
+ */
+bool dataSetValue(data* dat, void* key, void* value)
+{
+ struct dat* d;
+ ASSERT_PTR(dat);
+ if(!_dataAllocate(dat))
+ return false;
+
+ if(d = _dataFind(dat, key))
+ {
+ d->value = value;
+ }
+ else
+ {
+ /* Add new one */
+ dat->thedata[dat->cur].key = key;
+ dat->thedata[dat->cur].value = value;
+ dat->cur++;
+ }
+
+ return true;
+}
+
+/* dataClearAll: ---------------------------------------------------
+ * Clear all values from block
+ */
+void dataClearAll(data* dat)
+{
+ size_t i;
+
+ ASSERT_PTR(dat);
+ if(dat->thedata)
+ {
+ for(i = 0; i < dat->cur; i++)
+ {
+ if(dat->thedata[i].value)
+ free(dat->thedata[i].value);
+ }
+ }
+
+ dat->cur = 0;
+}
+
+
+/* ========================================================================
+ * VARIABLES: Variables are strings indexed by a name.
+ */
+
+/* _variablesAllocate: ----------------------------------------------
+ * Expand a block of variables if necessary
+ */
+bool _variablesAllocate(variables* vars)
+{
+ ASSERT_PTR(vars);
+
+ if(!vars->thevars)
+ {
+ /* Allocate new set */
+ vars->thevars = (struct vari*)malloc(sizeof(struct vari) * BLOCK_SIZE);
+ if(!vars->thevars) return false;
+ vars->alloc = BLOCK_SIZE;
+ vars->cur = 0;
+ }
+
+ /* If no more space ... */
+ if(vars->alloc <= vars->cur)
+ {
+ /* ...double allocation */
+ size_t sz = sizeof(struct vari) * vars->alloc;
+ vars->thevars = (struct vari*)reallocf(vars->thevars, sz * 2);
+ if(!vars->thevars) return false;
+ vars->alloc *= 2;
+ }
+
+ return true;
+}
+
+/* _variablesNew: --------------------------------------------------------
+ * Make a new variable in a given block
+ */
+struct vari* _variablesNew(variables* vars, const char* name,
+ const char* val, size_t cnt)
+{
+ struct vari* v;
+ ASSERT_PTR(vars);
+
+ if(!_variablesAllocate(vars))
+ return NULL;
+
+ v = &(vars->thevars[vars->cur]);
+
+ /* Add new variable */
+ v->name = strdup(name);
+ v->value = (char*)malloc(sizeof(char) * (cnt + 2));
+ starclr(v->value);
+ starnadd(&(v->value), val, cnt);
+
+ /* Oops! */
+ if(!v->name || !v->value)
+ return NULL;
+
+ vars->cur++;
+
+ return v;
+}
+
+/* _variablesFind: ------------------------------------------------------
+ * Find a variable in the block by name
+ */
+struct vari* _variablesFind(variables* vars, const char* name)
+{
+ size_t i;
+ char* val;
+
+ ASSERT_PTR(vars);
+ i = vars->cur;
+
+ while(i--)
+ {
+ if(!strcasecmp(name, vars->thevars[i].name))
+ return &(vars->thevars[i]);
+ }
+
+ /* If not found then look up in the Environment */
+ if(val = getenv(name))
+ return _variablesNew(vars, name, val, strlen(val));
+
+ return NULL;
+}
+
+/* variablesInit: ------------------------------------------------------
+ * Initialize and possibly allocate a variable set
+ */
+bool variablesInit(variables* vars)
+{
+ zero(*vars);
+ return true;
+}
+
+/* variablesFree: ------------------------------------------------------
+ * Free a set of variables
+ */
+void variablesFree(variables* vars)
+{
+ if(vars)
+ {
+ ASSERT_PTR(vars);
+ if(vars->thevars)
+ free(vars->thevars);
+ vars->thevars = NULL;
+ }
+}
+
+/* variablesAddBytes: --------------------------------------------------
+ * Add a counted string to a variable.
+ */
+bool variablesAddBytes(variables* vars, const char* name,
+ const char* val, size_t cnt)
+{
+ struct vari* v;
+ ASSERT_PTR(vars);
+
+ v = _variablesFind(vars, name);
+ if(!v)
+ {
+ return _variablesNew(vars, name, val, cnt) != NULL;
+ }
+ else
+ {
+ /* Already have variable. Just add value */
+ ASSERT(v->value);
+ starnadd(&(v->value), val, cnt);
+ return v->value != NULL;
+ }
+}
+
+/* variablesAdd: -------------------------------------------------------
+ * Add a string to a variable.
+ */
+bool variablesAdd(variables* vars, const char* name, const char* val)
+{
+ ASSERT_PTR(vars);
+ return variablesAddBytes(vars, name, val, strlen(val));
+}
+
+/* variablesClear: ----------------------------------------------------
+ * Clear a variable's value.
+ */
+bool variablesClear(variables* vars, const char* name)
+{
+ struct vari* v;
+ ASSERT_PTR(vars);
+ if(v = _variablesFind(vars, name))
+ starclr(v->value);
+
+ return true;
+}
+
+/* variablesClearAll: -------------------------------------------------
+ * Delete all variables from set
+ */
+bool variablesClearAll(variables* vars)
+{
+ ASSERT_PTR(vars);
+ if(vars->thevars)
+ {
+ while(vars->cur--)
+ {
+ ASSERT(vars->thevars[vars->cur].name);
+ free(vars->thevars[vars->cur].name);
+ ASSERT(vars->thevars[vars->cur].value);
+ free(vars->thevars[vars->cur].value);
+ }
+
+ vars->cur = 0;
+ }
+
+ return true;
+}
+
+/* _escapeString: -----------------------------------------------------
+ * Helper function to make a string pass through the regex compiler
+ */
+static char* _escapeString(const char* string)
+{
+ const char kSpecialChars[] = "$^*(){}[]\\?+.";
+ size_t pos = 0;
+ size_t cnt = 0;
+ char* buff;
+
+ size_t len = strlen(string);
+
+ while((pos += strcspn(string + pos, kSpecialChars)) < len)
+ cnt++, pos++;
+
+ if(buff = (char*)malloc(sizeof(char) * (len + cnt + 1)))
+ {
+ pos = 0;
+ strcpy(buff, string);
+ while((pos += strcspn(buff + pos, kSpecialChars)) < len + cnt)
+ strins(buff + pos, "\\"), pos += 2;
+ }
+
+ return buff;
+}
+
+/* variablesSubstitute: -----------------------------------------------
+ * Perform variable and register substitution on a string
+ */
+int variablesSubstitute(variables* vars, r_stream* stream, r_script* script,
+ char** pstr, bool mode)
+{
+ size_t len = strlen(*pstr);
+ char* next = *pstr;
+ struct internal_state* state = stream->state;
+
+ ASSERT_PTR(vars);
+ ASSERT_PTR(state);
+
+ #define SYNTAX_ERROR(s) \
+ do{ \
+ scriptSetError(script, s); \
+ return R_SYNTAX; \
+ } while(0)
+
+ /*
+ If mode == true then we're substituting inside a regular expression
+ otherwise we're substituting in replaced text
+ */
+
+ /* Find next backslash or percent */
+ while(next += strcspn(next, "\\%"), next && next[0] != 0)
+ {
+ switch(*next)
+ {
+
+ /* It's a variable or a register */
+ case '%':
+
+ /* Is it a register? */
+ if(isdigit(next[1]))
+ {
+ /* Registers only in replace mode replace mode */
+ if(!mode)
+ {
+ uint reg = next[1] - 0x30;
+
+ /* Get register number */
+ if(reg < REGISTER(r_cg))
+ {
+ /* Save offset for reallocations */
+ size_t off = next - *pstr;
+ size_t reglen = REGISTER2(r_e0, reg) - REGISTER2(r_b0, reg);
+
+ /* Reallocate to fit register text */
+ strrsrv(*pstr, strlen(*pstr) + reglen);
+ if(!(*pstr)) return R_NOMEM;
+
+ /* offset next properly */
+ next = *pstr + off;
+
+ /* Replace \N with register text */
+ next = strnrep(next, 2,
+ (stream->nextIn + ABS_TO_REL(REGISTER2(r_b0, reg), stream->state)),
+ reglen);
+ }
+ else
+ /* If invalid number just blow away */
+ next = strnrep(next, 2, "", 0);
+ }
+ }
+
+
+ /* Otherwise it's a variable */
+ else
+ {
+ bool multi = false;
+ size_t len, off;
+ char temp;
+ struct vari* v;
+ const char* value;
+
+ /* Get the name */
+ len = strspn(next + 1, kValidIdentifier);
+ if(len == 0) SYNTAX_ERROR("Invalid variable name.");
+
+ /* Increment length for the % prefix */
+ len++;
+
+ /* Null terminate the variable name */
+ temp = next[len];
+ next[len] = 0;
+
+ /* Do we have this variable? */
+ if(v = _variablesFind(vars, next + 1))
+ value = v->value;
+ else
+ value = "\0\0";
+
+ /* Check if the variable is an array */
+ if(mode)
+ multi = starnext(value) ? true : false;
+
+ /* Unnull-terminate it */
+ next[len] = temp;
+
+ /* Get offset for reallocations */
+ off = next - *pstr;
+
+ /* Reallocate the string to accomodate new replacement */
+ strrsrv(*pstr, strlen(*pstr) + (starend(value) - value) + 4);
+ if(!*pstr) return R_NOMEM;
+
+ /* Offset next back properly */
+ next = *pstr + off;
+
+ /* Eat variable name and add open parentheses if needed
+ The '?:' after the opening paren denotes a non capturing
+ group */
+ next = strrep(next, len, multi ? "(?:" : "");
+
+ do
+ {
+ /* If in regular expression mode ... */
+ char* escval;
+ if(mode)
+ {
+ /* ... escape the value */
+ escval = _escapeString(value);
+ if(!escval) return R_NOMEM;
+ }
+
+ /* Insert the value */
+ next = strrep(next, 0, mode ? escval : value);
+
+ /* If in regular expression mode free escaped. */
+ if(mode)
+ free(escval);
+
+ /* If not multi then only put the first one in */
+ if(!multi)
+ break;
+
+ /* Add a pipe (alternation operator) if we have more values */
+ else
+ next = strrep(next, 0, starnext(value) ? "|" : "");
+
+ }
+ while(value = starnext(value));
+
+ /* Add closing parentheses if needed */
+ next = strrep(next, 0, multi ? ")" : "");
+ }
+ break;
+
+
+ /* It's a variable name or escaped character */
+ case '\\':
+
+ /*
+ Only unescape in replace mode
+ Regular expressions will unescape everything else
+ */
+ if(!mode)
+ next = strnrep(next, 1, "", 0);
+ else
+ next++;
+
+ next++;
+
+ break;
+ }
+ }
+
+ return R_OK;
+}
+
+/* variablesValidName: -------------------------------------------------
+ * Is the given name a valid variable name
+ */
+bool variablesValidName(const char* name)
+{
+ return strspn(name, kValidIdentifier) >= strlen(name);
+}
+
+/* variablesHasVars: ----------------------------------------------------
+ * Does the given string have variables or registers that need
+ * substitution?
+ */
+bool variablesHasVars(const char* string)
+{
+ const char* cur = string;
+
+ if(cur = strchr(cur, '%'))
+ {
+ if(!isEscaped(string, cur))
+ return true;
+
+ cur++;
+ }
+
+ return false;
+}
+
+/* =========================================================================
+ * LOCKS: Locks are used by the engine to implement the replacement locks
+ */
+
+/* _locksAllocate: -------------------------------------------------------
+ * Expand a block of locks if needed
+ */
+bool _locksAllocate(locks* lcks)
+{
+ if(!lcks->thelocks)
+ {
+ /* Allocate the first set */
+ lcks->thelocks = (struct lock*)malloc(sizeof(struct lock) * BLOCK_SIZE);
+ if(!lcks->thelocks) return false;
+ lcks->alloc = BLOCK_SIZE;
+ locksClearAll(lcks);
+ }
+
+ /* If not enough space ... */
+ if(lcks->alloc <= lcks->cur)
+ {
+ /* ... reallocate double */
+ size_t sz = sizeof(struct lock) * lcks->alloc;
+ lcks->thelocks = (struct lock*)reallocf(lcks->thelocks, sz * 2);
+ if(!lcks->thelocks) return false;
+ lcks->alloc *= 2;
+ }
+
+ return true;
+}
+
+/* locksInit: -------------------------------------------------------------
+ * Initialize and or allocate a block of locks
+ */
+bool locksInit(locks* lcks)
+{
+ zero(*lcks);
+ return true;
+}
+
+/* locksFree: -------------------------------------------------------------
+ * Free a set of locks
+ */
+void locksFree(locks* lcks)
+{
+ if(lcks)
+ {
+ ASSERT_PTR(lcks);
+ if(lcks->thelocks)
+ free(lcks->thelocks);
+ lcks->thelocks = NULL;
+ }
+}
+
+#define RANGE_ADD(bd, ed, b1, e1) \
+ (((bd) = (bd) < (b1) ? (bd) : (b1)), ((ed) = (ed) > (e1) ? (ed) : (e1)))
+
+#define RANGE_INTERSECTS(b1, e1, b2, e2) \
+ ((b1) < (e2) && (e1) > (b2))
+
+#define RANGE_BEFORE(b1, e1, b2, e2) \
+ ((b1) <= (b2) && (e1) <= (e2))
+
+
+/* locksAdd: ---------------------------------------------------------------
+ * Add a lock to the set
+ */
+bool locksAdd(locks* lcks, size_t beg, size_t end)
+{
+ size_t i = 0;
+
+ for(; i < lcks->cur; i++)
+ {
+ if(RANGE_INTERSECTS(beg, end, lcks->thelocks[i].beg, lcks->thelocks[i].end))
+ {
+ RANGE_ADD(lcks->thelocks[i].beg, lcks->thelocks[i].end, beg, end);
+
+ /* Clean up any doubles */
+ for(i = 0; i < lcks->cur - 1; i++)
+ {
+ if(RANGE_INTERSECTS(lcks->thelocks[i].beg, lcks->thelocks[i].end,
+ lcks->thelocks[i + 1].beg, lcks->thelocks[i + 1].end))
+ {
+ RANGE_ADD(lcks->thelocks[i].beg, lcks->thelocks[i].end,
+ lcks->thelocks[i + 1].beg, lcks->thelocks[i + 1].end);
+
+ lcks->cur--;
+ memmove(lcks->thelocks + i + 1, lcks->thelocks + i + 2,
+ sizeof(lcks->thelocks[0]) * (lcks->cur - (i + 1)));
+ i--;
+ }
+ }
+
+ return true;
+ }
+
+ if(RANGE_BEFORE(beg, end, lcks->thelocks[i].beg, lcks->thelocks[i].end))
+ break;
+ }
+
+ if(!_locksAllocate(lcks)) return false;
+
+ /* Move the locks one down */
+ memmove(lcks->thelocks + i + 1, lcks->thelocks + i,
+ sizeof(lcks->thelocks[0]) * (lcks->cur - i));
+
+ lcks->thelocks[i].beg = beg;
+ lcks->thelocks[i].end = end;
+
+ lcks->cur++;
+
+ return true;
+}
+
+#ifdef _DEBUG
+static void _locksTestIntersects()
+{
+ ASSERT(!RANGE_INTERSECTS(0x0F, 0x15, 0x15, 0x15));
+ ASSERT(RANGE_INTERSECTS(0x0F, 0x15, 0x0F, 0x15));
+ ASSERT(RANGE_INTERSECTS(0x10, 0x15, 0x0F, 0x15));
+ ASSERT(RANGE_INTERSECTS(0x0F, 0x12, 0x0F, 0x15));
+ ASSERT(RANGE_INTERSECTS(0x10, 0x12, 0x0F, 0x15));
+ ASSERT(RANGE_INTERSECTS(0x0F, 0x15, 0x10, 0x12));
+ ASSERT(RANGE_INTERSECTS(0x10, 0x1A, 0x0F, 0x15));
+ ASSERT(RANGE_INTERSECTS(0x02, 0x12, 0x0F, 0x15));
+ ASSERT(!RANGE_INTERSECTS(0x15, 0x15, 0x15, 0x15));
+ ASSERT(!RANGE_INTERSECTS(0x15, 0x15, 0x15, 0x20));
+}
+#endif
+
+/* locksCheck: -----------------------------------------------------------
+ * Check a range against the locks
+ */
+bool locksCheck(locks* lcks, r_stream* stream, size_t beg, size_t end)
+{
+ size_t cnt;
+
+#ifdef _DEBUG
+ _locksTestIntersects();
+#endif
+
+ for(cnt = 0; cnt < lcks->cur; cnt++)
+ if(RANGE_INTERSECTS(beg, end, lcks->thelocks[cnt].beg, lcks->thelocks[cnt].end))
+ return true;
+
+ return false;
+}
+
+
+/* ============================================================================
+ * REPLACEMENT LIST: Contains the text of any replacements. They're copied
+ * into the output later.
+ */
+
+/* replacementAlloc: ---------------------------------------------------------
+ * Prepare and allocate a replacement
+ */
+int replacementAlloc(r_stream* stream, const char* text, replacement** pprep)
+{
+ int ret;
+
+ size_t len = strlen(text);
+ ret = R_OK;
+
+ /* Allocate the replacement. Note that text hangs off the end */
+ *pprep = (replacement*)malloc(sizeof(replacement) + (sizeof(char) * len));
+ if(!*pprep)
+ ret = R_NOMEM;
+ else
+ {
+ /* Set it up properly */
+ memset(*pprep, 0, sizeof(replacement) + len);
+ (*pprep)->next = NULL;
+
+ /* Copy string on end of buffer */
+ strcpy((*pprep)->text, text);
+ }
+
+ return ret;
+}
+
+
+/* replacementAdd: -----------------------------------------------------------
+ * Insert replacement into queue in the right order (IMPORTANT!)
+ */
+void replacementAdd(replacement* repl, r_stream* stream)
+{
+ replacement top;
+ replacement* first = &top;
+ top.beg = top.end = 0;
+ top.next = stream->state->replaces;
+
+ /* Find appropriate pos */
+ while(first && first->next && (first->next->end < repl->end))
+ first = first->next;
+
+ while(first && first->next && (first->next->beg < repl->beg))
+ first = first->next;
+
+ /* Hook in */
+ repl->next = first->next;
+ first->next = repl;
+
+ stream->state->replaces = top.next;
+}
+
+
+/* replacementPop: -------------------------------------------------------
+ * Remove and return a replacement from the queue
+ */
+replacement* replacementPop(replacement* repl)
+{
+ replacement* ret = repl->next;
+ free(repl);
+ return ret;
+}
+
+/* replacementDump: ------------------------------------------------------
+ * Dump all replacements in the stream to stderr
+ */
+void replacementDump(r_stream* stream)
+{
+ replacement* repl = stream->state->replaces;
+
+ while(repl)
+ {
+ fprintf(stderr, " beg: %x end: %x text: \'%s\'\n", repl->beg, repl->end, repl->text);
+ repl = repl->next;
+ }
+}
+
+
+/* =========================================================================
+ * REGISTER HELPER FUNCTIONS
+ */
+
+/* regsSet: ----------------------------------------------------------
+ * Offset and copy an entire set of regs int the main registers
+ */
+static void regsSet(struct internal_state* state, int pcreregs[], size_t offset)
+{
+ size_t i;
+ for(i = 0; i < REGISTER(r_cg); i++)
+ {
+ REGISTER2(r_b0, i) = pcreregs[i * 2] + offset;
+ REGISTER2(r_e0, i) = pcreregs[(i * 2) + 1] + offset;
+ }
+
+ /* Set rest of registers as invalid */
+ for( ; i < MAX_REGS; i++)
+ {
+ REGISTER2(r_b0, i) = ~0;
+ REGISTER2(r_b0, i) = ~0;
+ }
+}
+
+
+/* ===========================================================================
+ * MAIN EXECUTION FUNCTIONS
+ */
+
+
+/* vmInit: --------------------------------------------------------------
+ * Initialize state for execution
+ */
+bool vmInit(r_stream* stream)
+{
+ struct internal_state* state = stream->state;
+ ASSERT(stream->state);
+
+ if(!variablesInit(&(state->vars)) ||
+ !memoryInit(&(state->mem)) ||
+ !locksInit(&(state->lcks)) ||
+ !dataInit(&(state->working)))
+ return R_NOMEM;
+
+ /* init replaces */
+ state->replaces = NULL;
+
+ return true;
+}
+
+
+/* vmFree: --------------------------------------------------------------
+ * Undo initialization
+ */
+void vmFree(r_stream* stream)
+{
+ struct internal_state* state = stream->state;
+ ASSERT(stream->state);
+
+ while(state->replaces)
+ state->replaces = replacementPop(state->replaces);
+
+ variablesFree(&(state->vars));
+ memoryFree(&(state->mem));
+ locksFree(&(state->lcks));
+ dataFree(&(state->working));
+}
+
+
+/* vmClean: -------------------------------------------------------------
+ * Prepare a VM for a totally new replacement operation
+ */
+void vmClean(r_stream* stream)
+{
+ struct internal_state* state = stream->state;
+ ASSERT(stream->state);
+
+ while(state->replaces)
+ state->replaces = replacementPop(state->replaces);
+
+ variablesClearAll(&(state->vars));
+ memoryClearAll(&(state->mem));
+ locksClearAll(&(state->lcks));
+ dataClearAll(&(state->working));
+ stream->state->offset = 0;
+}
+
+
+
+
+/* rvalue: --------------------------------------------------------------
+ * Return a value to be used on the right (value) side of an expression
+ */
+#ifdef USE_STACK_VARS
+uint rvalue(vmop_t* ops, struct internal_state* state,
+ memory* mem, memory* stackMem)
+#else
+uint rvalue(vmop_t* ops, struct internal_state* state, memory* mem)
+#endif
+{
+ switch(ARG_TYPE(*ops))
+ {
+ case ARG_VAL_TYPE:
+ return ARG_GET_VALUE(*((uint*)ops));
+ case ARG_REG_TYPE:
+ return REGISTER(*ops);
+ case ARG_MEM_TYPE:
+ return *(memoryValue(mem, ARG_GET_MEMORY(*((uint*)ops))));
+ case ARG_STACK_TYPE:
+#ifdef USE_STACK_VARS
+ return *(memoryValue(stackMem, ARG_GET_STACK(*((uint*)ops))));
+#else
+ return *(memoryValue(mem, ARG_GET_STACK(*((uint*)ops))));
+#endif
+ default:
+ ASSERT(false);
+ return 0;
+ }
+}
+
+
+/* lvalue: -----------------------------------------------------------------
+ * Return a value to be used on the left side (assigned) of an expression
+ */
+#ifdef USE_STACK_VARS
+static uint* lvalue(vmop_t* ops, struct internal_state* state,
+ memory* mem, memory* stackMem)
+#else
+static uint* lvalue(vmop_t* ops, struct internal_state* state, memory* mem)
+#endif
+{
+ switch(ARG_TYPE(*ops))
+ {
+ case ARG_VAL_TYPE:
+ ASSERT(false && "Can't put a value on left side.");
+ return 0;
+ case ARG_REG_TYPE:
+ return &(REGISTER(*ops));
+ case ARG_MEM_TYPE:
+ return memoryValue(mem, ARG_GET_MEMORY(*((uint*)ops)));
+ case ARG_STACK_TYPE:
+#ifdef USE_STACK_VARS
+ return memoryValue(stackMem, ARG_GET_STACK(*((uint*)ops)));
+#else
+ return memoryValue(mem, ARG_GET_MEMORY(*((uint*)ops)));
+#endif
+ default:
+ ASSERT(false);
+ return 0;
+ }
+}
+
+
+/* vmExecute: -------------------------------------------------------------
+ * The main VM run loop
+ */
+int vmExecute(r_stream* stream, r_script* script)
+{
+#ifdef USE_STACK_VARS
+ #define RVALUE(ops) rvalue(ops, state, &(state->mem), stackVars)
+ #define LVALUE(ops) lvalue(ops, state, &(state->mem), stackVars)
+#else
+ #define RVALUE(ops) rvalue(ops, state, &(state->mem))
+ #define LVALUE(ops) lvalue(ops, state, &(state->mem))
+#endif
+
+ #define PUSH_STACK(top, v) ((top)++[0] = (v))
+ #define POP_STACK(top) ((--top)[0])
+
+ /* Jump to cleanup label instead of return */
+ #define RETURN(r) \
+ do { \
+ retval = r; \
+ goto cleanup; \
+ } while (0)
+
+ struct internal_state* state;
+ int retval = R_OK;
+ vmop_t* ops;
+ uint passes = 0;
+
+ /* These are the registers passed to PCRE */
+ int pcreregs[MAX_REGS * 3];
+ int num_regs = 0;
+
+ /* And over here we have the stack */
+ uint* vmStack = NULL;
+ size_t allocStack = 0;
+ uint** stack;
+ #define STACK *stack
+
+#ifdef USE_STACK_VARS
+ memory* stackVars;
+#endif
+
+ /* The text buffer */
+ char* text = NULL;
+
+ /* We just setup some vars for easy access to structs */
+ state = stream->state;
+ ops = script->ops;
+ zero(state->vmregs);
+ ASSERT(script->ops);
+
+ stack = (uint**)&(state->vmregs[r_sp >> 2]);
+
+
+ /* Set the initial limits in the x1 and y1 registers */
+ REGISTER(r_x1) = REL_TO_ABS(0, state);
+ REGISTER(r_y1) = REL_TO_ABS(stream->availIn, state);
+
+ /* Stack variables */
+#ifdef USE_STACK_VARS
+ stackVars = (memory*)malloc(sizeof(memory));
+ if(!stackVars || !memoryInit(stackVars))
+ RETURN(R_NOMEM);
+#endif
+
+ /* Preallocate some memory for the text buffer */
+ text = malloc(sizeof(char) * 256);
+ if(!text) RETURN(R_NOMEM);
+ text[0] = 0;
+
+
+ while(1)
+ {
+ vmop_t op = *ops;
+ ops++;
+
+ /* The text buffer should always be pointing to a valid
+ block of memory */
+ ASSERT(text != NULL);
+
+
+ /* Check and see if we have enough stack and allocate if not */
+ if((STACK + 0x010) > (vmStack + allocStack))
+ {
+ size_t off = STACK - vmStack;
+ vmStack = (uint*)reallocf(vmStack, (allocStack + 0x080) * sizeof(uint));
+ if(!vmStack)
+ RETURN(R_NOMEM);
+
+ allocStack += 0x080;
+ STACK = vmStack + off;
+ }
+
+
+ /* Main switch which dispatches the ops */
+ switch(op)
+ {
+
+ /* end: Finished executing script (but can come back again for more) */
+ case o_end:
+ RETURN(R_OK);
+
+
+ /* nop: Do nothing */
+ case o_nop:
+ break;
+
+
+ /* push: Push a value on the stack */
+ case o_push:
+ PUSH_STACK(STACK, RVALUE(ops));
+
+ INC_ARGUMENT(ops);
+ break;
+
+
+ /* pop: Pop a value from the stack */
+ case o_pop:
+ *(LVALUE(ops)) = POP_STACK(STACK);
+ INC_ARGUMENT(ops);
+ break;
+
+
+ /* lock: Lock the area between the selected area */
+ case o_lock:
+ {
+ uint beg = RVALUE(ops);
+ INC_ARGUMENT(ops);
+
+ if(!locksAdd(&(state->lcks), beg, RVALUE(ops)))
+ RETURN(R_NOMEM);
+
+ INC_ARGUMENT(ops);
+
+ /* Locking is an action too */
+ REGISTER(r_ac) = 1;
+ }
+ break;
+
+
+ /* check: Check the selected area against any locks */
+ case o_check:
+ {
+ uint beg = RVALUE(ops);
+ INC_ARGUMENT(ops);
+
+ /* Check the lowest registers against the locks it against the locks */
+ REGISTER(r_fe) = locksCheck(&(state->lcks), stream, beg, RVALUE(ops))
+ ? 0 : 1;
+
+ INC_ARGUMENT(ops);
+ }
+ break;
+
+
+
+ /* match: Match a pattern buffer */
+ case o_match:
+ {
+ uint beg, end, begLimit, endLimit, begMatch, endMatch;
+ match_op* header;
+ pcre* re = NULL;
+ pcre_extra* extra = NULL;
+ bool cache = false;
+ size_t i = 0;
+ int rt;
+
+ beg = ABS_TO_REL(RVALUE(ops), state);
+ INC_ARGUMENT(ops);
+ end = ABS_TO_REL(RVALUE(ops), state);
+ INC_ARGUMENT(ops);
+
+ begLimit = ABS_TO_REL(REGISTER(r_x1), state);
+ endLimit = ABS_TO_REL(REGISTER(r_y1), state);
+
+ /* Determine what kind of a match we're talking about here */
+ header = (match_op*)ops;
+
+ /* This is the only type of regular expression we support
+ at the moment */
+ ASSERT(header->type & kMatchPcre);
+
+
+ /*
+ * Check if we've already cached the compiled
+ * regular expression.
+ * - We use the op header pointer as key to the pcre struct
+ * - And the pcre struct pointer as the key to the pcre_extra struct
+ */
+ if(re = (pcre*)dataGetValue(&(state->working), header))
+ {
+ /* This prevents the freeing of stuff below */
+ cache = true;
+
+ /* Get out the pcre_extra if present */
+ extra = (pcre_extra*)dataGetValue(&(state->working), re);
+ }
+
+ /* Otherwise we compile it */
+ else
+ {
+ match_op_pcre* pcreop = (match_op_pcre*)header;
+ char* pattern = strdup(pcreop->pattern);
+ int erroroffset;
+ const char* error = NULL;
+
+ /* If there's variables then don't cache and ... */
+ if(!(cache = !variablesHasVars(pattern)))
+ {
+ /* ... do variable substitution */
+ rt = variablesSubstitute(&(state->vars), stream, script, &pattern, true);
+ if(rt < 0) RETURN(rt);
+ }
+
+ /* Compile the pattern */
+ re = pcre_compile(pattern, pcreop->options,
+ &error, &erroroffset, NULL);
+
+ if(!re)
+ {
+ if(error)
+ scriptSetError(script, error);
+ RETURN(R_REGEXP);
+ }
+
+ /*
+ * If there's no variables in the regular expression
+ * then we can cache the compiled pcre and study it
+ */
+ if(cache)
+ {
+ if(!dataSetValue(&(state->working), header, re))
+ RETURN(R_NOMEM);
+
+ extra = pcre_study(re, 0, &error);
+ if(error)
+ {
+ scriptSetError(script, error);
+ RETURN(R_REGEXP);
+ }
+
+ if(extra && !dataSetValue(&(state->working), re, extra))
+ RETURN(R_NOMEM);
+ }
+
+ free(pattern);
+ }
+
+
+ begMatch = beg;
+ endMatch = end;
+
+ /* Set failed flag */
+ REGISTER(r_fe) = 0;
+
+ while(i < locksSize(&(state->lcks)) &&
+ locksEnd(&(state->lcks), i) <= REL_TO_ABS(beg, state))
+ i++;
+
+ do
+ {
+ if(i < locksSize(&(state->lcks)))
+ endMatch = ABS_TO_REL(locksBeg(&(state->lcks), i), state);
+ else if(i >= locksSize(&(state->lcks)))
+ endMatch = end;
+
+ /* If that put it too high then bring back */
+ if(endMatch > end)
+ endMatch = end;
+
+ /* If we haven't overstepped the bounds, search */
+ if(begMatch <= endMatch)
+ {
+ int opts = 0;
+
+ /*
+ * We have to do a little trickery here
+ * First we fool pcre_exec into thinking that our range start
+ * is the beginning of the string. We fix up the registers later
+ *
+ * Then we fool'm into thinking that the our search end is the
+ * end of the string, but if it's really not the end of the
+ * range then set not_eol so that '$' doesn't match.
+ */
+
+ if(endMatch != endLimit)
+ opts |= PCRE_NOTEOL;
+
+ /* Do actual search */
+ /* TODO: Get this ready for binary replacements */
+ rt = pcre_exec(re, extra,
+ ((char*)stream->nextIn + begLimit), /* Data */
+ endMatch - begLimit, /* size */
+ begMatch - begLimit, /* position */
+ opts, /* options */
+ pcreregs, /* group registers */
+ MAX_REGS * 3); /* number of registers */
+
+ /* These are programmer errors */
+ ASSERT(rt != PCRE_ERROR_NULL);
+ ASSERT(rt != PCRE_ERROR_BADOPTION);
+ ASSERT(rt != PCRE_ERROR_BADMAGIC);
+ ASSERT(rt != PCRE_ERROR_UNKNOWN_NODE);
+ ASSERT(rt != PCRE_ERROR_NOSUBSTRING);
+
+ if(rt == PCRE_ERROR_NOMEMORY)
+ RETURN(R_NOMEM);
+
+ if(rt >= 0)
+ {
+ /* Found a match! */
+
+ /* Do group register maintainance */
+ REGISTER(r_cg) = rt;
+ regsSet(state, pcreregs, REL_TO_ABS(begLimit, state));
+
+ /* Set succeeded flag */
+ REGISTER(r_fe) = 1;
+
+ /* Get out of here */
+ break;
+ }
+
+ /* Make sure we've got all the errors */
+ else if(rt != -1)
+ ASSERT(0);
+
+ }
+
+ /* The beginning of the next search block should be
+ the end of the current lock */
+ if(i < locksSize(&(state->lcks)))
+ begMatch = ABS_TO_REL(locksEnd(&(state->lcks), i), state);
+
+ i++;
+ }
+ while(endMatch < end);
+
+ if(!cache)
+ {
+ if(re)
+ free(re);
+ if(extra)
+ free(extra);
+ }
+
+ ops += match_op_size(*header);
+ }
+
+ break;
+
+
+
+ /* setvar: Add selected text to a variable */
+ case o_setvar:
+ {
+ var_op* pOp = (var_op*)ops;
+
+ /* TODO: Get this ready for binary replacements */
+ if(!variablesAddBytes(&(state->vars), (char*)pOp->name, text, strlen(text)))
+ RETURN(R_NOMEM);
+
+ ops += var_op_size(*pOp);
+ }
+ break;
+
+
+ /* setvar: clear a variable */
+ case o_clrvar:
+ {
+ var_op* pOp = (var_op*)ops;
+
+ /* TODO: Get this ready for binary variables */
+ variablesClear(&(state->vars), (char*)pOp->name);
+ ops += var_op_size(*pOp);
+ }
+ break;
+
+
+ case o_je:
+ case o_jne:
+ case o_jmp:
+
+ /* Get the conditional jumps out of the way */
+ if((op == o_je && REGISTER(r_fe) == 0) ||
+ (op == o_jne && REGISTER(r_fe) > 0))
+ {
+ INC_ARGUMENT(ops);
+ break;
+ }
+ else
+ {
+ /* Now do the jump */
+ vmop_t* opOld = ops;
+
+ /* Change the op to the indicated pos */
+ ops = script->ops + RVALUE(ops);
+
+ /*
+ * If a backwards jump then count as a "pass" thingy
+ * we use this count to catch endless loop errors
+ */
+
+ /* TODO: I don't think we need this anymore */
+ if(opOld > ops)
+ {
+ passes++;
+ if(passes > MAX_PASSES)
+ return R_LOOP;
+ }
+ }
+ break;
+
+
+ /* repl: Replace last match with given text */
+ case o_repl:
+ {
+ uint beg, end;
+ replacement* rep = NULL;
+ int rt;
+
+ beg = RVALUE(ops);
+ INC_ARGUMENT(ops);
+ end = RVALUE(ops);
+ INC_ARGUMENT(ops);
+
+ /* Get the formatted replacement */
+ rt = replacementAlloc(stream, text, &rep);
+ if(rt < 0)
+ RETURN(rt);
+
+ rep->beg = beg;
+ rep->end = end;
+
+ /* If we have a confirm function then ask about replacement */
+ if(stream->fMatch)
+ {
+ r_replace repl;
+ repl.from = ABS_TO_REL(beg, state);
+ repl.flen = end - beg;
+ repl.to = (byte*)rep->text;
+ repl.tlen = strlen(rep->text);
+ repl.offset = beg;
+
+ if(!(stream->fMatch)(stream, &repl))
+ {
+ free(rep);
+ rep = NULL;
+ }
+ }
+
+ if(rep)
+ {
+ /* Add it! */
+ REGISTER(r_ac) = 1;
+ stream->total++;
+ replacementAdd(rep, stream);
+ }
+ }
+
+ break;
+
+
+ /* stop: Stop execution of script */
+ case o_stop:
+ {
+ uint error = RVALUE(ops);
+ INC_ARGUMENT(ops);
+
+ scriptSetError(script, text);
+
+ if(error == 0)
+ RETURN(R_DONE);
+ else
+ RETURN(R_USER);
+ }
+ break;
+
+
+ /* cmp: compare two values and set registers accordingly */
+ case o_cmp:
+ {
+ uint arg1, arg2;
+
+ arg1 = RVALUE(ops);
+ INC_ARGUMENT(ops);
+ arg2 = RVALUE(ops);
+ INC_ARGUMENT(ops);
+
+ REGISTER(r_fe) = (arg1 == arg2) ? 1 : 0;
+ REGISTER(r_fg) = (arg1 > arg2) ? 1 : 0;
+ REGISTER(r_fl) = (arg1 < arg2) ? 1 : 0;
+ }
+ break;
+
+
+ /* test: Test whether value is 0 or not */
+ case o_test:
+ REGISTER(r_fe) = RVALUE(ops) != 0;
+ INC_ARGUMENT(ops);
+ break;
+
+
+ /* mov: Set one value to another */
+ case o_mov:
+ {
+ uint* dest = LVALUE(ops);
+ INC_ARGUMENT(ops);
+ *dest = RVALUE(ops);
+ INC_ARGUMENT(ops);
+ }
+ break;
+
+
+ /* call: Call a function */
+ case o_call:
+ {
+#ifdef USE_STACK_VARS
+ memory* newStack;
+#endif
+ uint pos = RVALUE(ops);
+ INC_ARGUMENT(ops);
+
+ /* Push current instruction pointer on the stack */
+ PUSH_STACK(STACK, ops - script->ops);
+
+ /* Change instruction pointer to function start */
+ ops = script->ops + pos;
+
+#ifdef USE_STACK_VARS
+ /* Push new set of stack variables */
+ newStack = (memory*)malloc(sizeof(memory));
+ if(!newStack || !memoryInit(newStack))
+ RETURN(R_NOMEM);
+
+ newStack->prev = stackVars;
+ stackVars = newStack;
+#endif
+ }
+ break;
+
+
+ /* ret: Return from a function */
+ case o_ret:
+ {
+#ifdef USE_STACK_VARS
+ memory* prev;
+#endif
+ /* Get location to return to */
+ uint pos = POP_STACK(STACK);
+
+ /* Change instruction pointer to returned location */
+ ops = script->ops + pos;
+
+#ifdef USE_STACK_VARS
+ /* Get previous set of stack vars */
+ ASSERT(stackVars->prev);
+ prev = stackVars->prev;
+ memoryFree(stackVars);
+ free(stackVars);
+ stackVars = prev;
+#endif
+ }
+ break;
+
+
+ /* add: Add 2 values */
+ case o_add:
+ {
+ /* Get first value */
+ uint* val = LVALUE(ops);
+ INC_ARGUMENT(ops);
+ *val += RVALUE(ops);
+ INC_ARGUMENT(ops);
+ }
+ break;
+
+
+ /* sub: Subtract values */
+ case o_sub:
+ {
+ /* Get first value */
+ uint* val = LVALUE(ops);
+ INC_ARGUMENT(ops);
+ *val -= RVALUE(ops);
+ INC_ARGUMENT(ops);
+ }
+ break;
+
+
+ /* text: set the text buffer */
+ case o_text:
+ {
+ text_op* top = (text_op*)ops;
+ int rt;
+
+ text = (char*)reallocf(text, top->len + 1);
+ if(!text) RETURN(R_NOMEM);
+
+ memcpy(text, top->string, top->len * sizeof(char));
+ text[top->len] = 0;
+
+ rt = variablesSubstitute(&(state->vars), stream, script, &text, false);
+ if(rt < 0) RETURN(rt);
+
+ ops += text_op_size(*top);
+ }
+ break;
+
+ /* msg: output text in buffer */
+ case o_msg:
+ {
+ if(stream->fMessage)
+ (stream->fMessage)(stream, text);
+ }
+ break;
+
+ default:
+ /* Invalid Instruction! */
+ ASSERT(false);
+ }
+ }
+
+cleanup:
+ if(vmStack)
+ free(vmStack);
+
+ while(stackVars)
+ {
+ memory* prev = stackVars->prev;
+ memoryFree(stackVars);
+ free(stackVars);
+ stackVars = prev;
+ }
+
+ return retval;
+}
+
+
+
diff --git a/lib/execute.h b/lib/execute.h
new file mode 100644
index 0000000..b3cbb40
--- /dev/null
+++ b/lib/execute.h
@@ -0,0 +1,179 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * VERSION
+ * 2.1.2b
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __EXECUTE_H__20010618
+#define __EXECUTE_H__20010618
+
+#include "ops.h"
+
+/* Allocate Locks, Memory and Variables in blocks this size */
+#define BLOCK_SIZE 0x20
+
+static const size_t kNoMatch = 0xFFFFFFFF;
+static const size_t kInfinity = 0xFFFFFFFE;
+
+/* Maximum amount of registers allowed */
+#define MAX_REGS 10
+
+
+/* =============================================================================
+ * MEMORY:
+ */
+
+typedef struct _memory
+{
+ /* Internal structure */
+ struct mem
+ {
+ uint key;
+ uint value;
+ }
+ *thememory;
+
+ struct _memory* prev;
+ size_t alloc; /* amount allocated of above */
+ size_t cur; /* amount used */
+}
+memory;
+
+bool memoryInit(memory* mem);
+void memoryFree(memory* mem);
+uint* memoryValue(memory* mem, uint key);
+void memoryClearAll(memory* mem);
+
+
+/* =============================================================================
+ * DATA:
+ */
+
+typedef struct _data
+{
+ /* Internal structure */
+ struct dat
+ {
+ void* key;
+ void* value;
+ }
+ *thedata;
+
+ size_t alloc; /* amount allocated of above */
+ size_t cur; /* amount used */
+}
+data;
+
+bool dataInit(data* dat);
+void dataFree(data* dat);
+void* dataGetValue(data* dat, void* key);
+bool dataSetValue(data* dat, void* key, void* value);
+void dataClearAll(data* dat);
+
+
+/* ==========================================================================
+ * VARIABLES: Contains set of variables (and variable arrays) currently
+ * set. This is maintained across blocks.
+ */
+
+typedef struct _variables
+{
+ /* Internal structure */
+ struct vari
+ {
+ char* name;
+ char* value;
+ }
+ *thevars;
+
+ size_t alloc; /* Amount allocated */
+ size_t cur; /* Amount used */
+}
+variables;
+
+bool variablesInit(variables* vars);
+void variablesFree(variables* vars);
+bool variablesAdd(variables* vars, const char* name, const char* val);
+bool variablesAddBytes(variables* vars, const char* name,
+ const char* val, size_t cnt);
+bool variablesClear(variables* vars, const char* name);
+bool variablesClearAll(variables* vars);
+int variablesSubstitute(variables* vars, r_stream* stream, r_script* script,
+ char** pstr, bool mode);
+bool variablesValidName(const char* name);
+bool variablesHasVars(const char* string);
+
+
+
+/* ======================================================================
+ * REPLACEMENT: A list of replacements to be written out.
+ */
+
+typedef struct _replacement
+{
+ size_t beg; /* Beginning of text to be replaced */
+ size_t end; /* end ditto */
+ struct _replacement* next; /* next replacement in list */
+ char text[1]; /* Text to replace with (hangs of end) */
+}
+replacement;
+
+/* Allocate a replacement for the given text. */
+int replacementAlloc(r_stream* stream, const char* text, replacement** pprep);
+/* Place replacement in the replacement list in the appropriate order */
+void replacementAdd(replacement* repl, r_stream* stream);
+/* Remove the first replacement in the replacement list and return */
+replacement* replacementPop(replacement* repl);
+/* Dump all replacements to stderr */
+void replacementDump(r_stream* stream);
+
+
+/* ======================================================================
+ * LOCKS: A list of locks to be checked against
+ */
+
+typedef struct _locks
+{
+ /* Internal structure */
+ struct lock
+ {
+ size_t beg;
+ size_t end;
+ }
+ *thelocks;
+
+ size_t cur;
+ size_t alloc;
+}
+locks;
+
+bool locksInit();
+void locksFree();
+bool locksAdd(locks* lcks, size_t beg, size_t end);
+bool locksCheck(locks* lcks, r_stream* stream, size_t beg, size_t end);
+#define locksClearAll(lcks) ((lcks)->cur = 0)
+#define locksSize(lcks) ((lcks)->cur)
+#define locksBeg(lcks, idx) ((lcks)->thelocks[idx].beg)
+#define locksEnd(lcks, idx) ((lcks)->thelocks[idx].end)
+
+
+
+bool isEscaped(const char* str, const char* posi);
+
+#endif /* __EXECUTE_H__20010618 */
diff --git a/lib/ops.h b/lib/ops.h
new file mode 100644
index 0000000..86bc017
--- /dev/null
+++ b/lib/ops.h
@@ -0,0 +1,349 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * VERSION
+ * 2.1.2b
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __OPS_H__20000616
+#define __OPS_H__20000616
+
+
+/* ----------------------------------------------------------------------
+ Op Arguments
+
+ The first two bits of an op argument are it's type.
+ 00 = value type (4 bytes)
+ 01 = memory type (4 bytes)
+ 10 = register type (1 byte)
+*/
+
+#define ARG_VAL_TYPE 0x00
+#define ARG_MEM_TYPE 0x01
+#define ARG_REG_TYPE 0x02
+#define ARG_STACK_TYPE 0x03
+
+#define ARG_MAKE_STACK(v) ((((v) << 2) | ARG_STACK_TYPE))
+#define ARG_MAKE_MEMORY(v) ((((v) << 2) | ARG_MEM_TYPE))
+#define ARG_MAKE_VALUE(v) ((uint)((v) << 2))
+#define ARG_MAKE_REGISTER(v) ((byte)(((v) << 2) | ARG_REG_TYPE))
+
+#define ARG_GET_MEMORY(v) ((uint)((v) >> 2))
+#define ARG_GET_VALUE(v) ((uint)((v) >> 2))
+#define ARG_GET_REGISTER(v) ((byte)((v) >> 2))
+#define ARG_GET_STACK(v) ((uint)((v) >> 2))
+
+
+#define ARG_TYPE(v) ((v) & 0x03)
+#define ARG_SIZE(v) ((ARG_TYPE(v) == ARG_REG_TYPE) ? 1 : 4)
+
+#define INC_ARGUMENT(op) ((op) += ARG_SIZE(*(op)))
+
+/* ----------------------------------------------------------------------
+ Registers (with 0x10 set)
+*/
+
+ /* General purpose flag
+ gets set by cmp, on equal, match on success, check on success
+ gets read by je and jne for jump conditions
+ */
+ #define r_fe ((byte)ARG_MAKE_REGISTER(0x00))
+ /* const byte fe = ARG_MAKE_REGISTER(0x00); */
+
+ /* Greater and less than flags
+ gets set by cmp
+ */
+ #define r_fg ((byte)ARG_MAKE_REGISTER(0x01))
+ /* const byte fg = ARG_MAKE_REGISTER(0x01); */
+ #define r_fl ((byte)ARG_MAKE_REGISTER(0x02))
+ /* const byte fl = ARG_MAKE_REGISTER(0x02); */
+
+ /* Action flag
+ // gets set by match on success
+ */
+ #define r_ac ((byte)ARG_MAKE_REGISTER(0x03))
+ /* const byte ac = ARG_MAKE_REGISTER(0x03); */
+
+ /* Stack pointer */
+ #define r_sp ((byte)ARG_MAKE_REGISTER(0x0A))
+ /* const byte sp = ARG_MAKE_REGISTER(0x0A); */
+
+ /* Base pointer */
+ #define r_bp ((byte)ARG_MAKE_REGISTER(0x0B))
+ /* const byte bp = ARG_MAKE_REGISTER(0x0B); */
+
+ /* Beginning and end registers
+ // gets set by match
+ */
+ #define r_b0 ((byte)ARG_MAKE_REGISTER(0x10))
+ /* const byte b0 = ARG_MAKE_REGISTER(0x10); */
+ #define r_b1 ((byte)ARG_MAKE_REGISTER(0x11))
+ /* const byte b1 = ARG_MAKE_REGISTER(0x11); */
+ #define r_b2 ((byte)ARG_MAKE_REGISTER(0x12))
+ /* const byte b2 = ARG_MAKE_REGISTER(0x12); */
+ #define r_b3 ((byte)ARG_MAKE_REGISTER(0x13))
+ /* const byte b3 = ARG_MAKE_REGISTER(0x13); */
+ #define r_b4 ((byte)ARG_MAKE_REGISTER(0x14))
+ /* const byte b4 = ARG_MAKE_REGISTER(0x14); */
+ #define r_b5 ((byte)ARG_MAKE_REGISTER(0x15))
+ /* const byte b5 = ARG_MAKE_REGISTER(0x15); */
+ #define r_b6 ((byte)ARG_MAKE_REGISTER(0x16))
+ /* const byte b6 = ARG_MAKE_REGISTER(0x16); */
+ #define r_b7 ((byte)ARG_MAKE_REGISTER(0x17))
+ /* const byte b7 = ARG_MAKE_REGISTER(0x17); */
+ #define r_b8 ((byte)ARG_MAKE_REGISTER(0x18))
+ /* const byte b8 = ARG_MAKE_REGISTER(0x18); */
+ #define r_b9 ((byte)ARG_MAKE_REGISTER(0x19))
+ /* const byte b9 = ARG_MAKE_REGISTER(0x19); */
+ #define r_e0 ((byte)ARG_MAKE_REGISTER(0x1B))
+ /* const byte e0 = ARG_MAKE_REGISTER(0x1B); */
+ #define r_e1 ((byte)ARG_MAKE_REGISTER(0x1C))
+ /* const byte e1 = ARG_MAKE_REGISTER(0x1C); */
+ #define r_e2 ((byte)ARG_MAKE_REGISTER(0x1D))
+ /* const byte e2 = ARG_MAKE_REGISTER(0x1D); */
+ #define r_e3 ((byte)ARG_MAKE_REGISTER(0x1E))
+ /* const byte e3 = ARG_MAKE_REGISTER(0x1E); */
+ #define r_e4 ((byte)ARG_MAKE_REGISTER(0x1F))
+ /* const byte e4 = ARG_MAKE_REGISTER(0x1F); */
+ #define r_e5 ((byte)ARG_MAKE_REGISTER(0x20))
+ /* const byte e5 = ARG_MAKE_REGISTER(0x20); */
+ #define r_e6 ((byte)ARG_MAKE_REGISTER(0x21))
+ /* const byte e6 = ARG_MAKE_REGISTER(0x21); */
+ #define r_e7 ((byte)ARG_MAKE_REGISTER(0x22))
+ /* const byte e7 = ARG_MAKE_REGISTER(0x22); */
+ #define r_e8 ((byte)ARG_MAKE_REGISTER(0x23))
+ /* const byte e8 = ARG_MAKE_REGISTER(0x23); */
+ #define r_e9 ((byte)ARG_MAKE_REGISTER(0x24))
+ /* const byte e9 = ARG_MAKE_REGISTER(0x24); */
+
+ /* The count of groups matched */
+ #define r_cg ((byte)ARG_MAKE_REGISTER(0x25))
+ /* const byte cg = ARG_MAKE_REGISTER(0x25); */
+
+ /* General purpose registers */
+ #define r_x0 ((byte)ARG_MAKE_REGISTER(0x30))
+ /* const byte x0 = ARG_MAKE_REGISTER(0x30); */
+ #define r_x1 ((byte)ARG_MAKE_REGISTER(0x31))
+ /* const byte x1 = ARG_MAKE_REGISTER(0x31); */
+ #define r_x2 ((byte)ARG_MAKE_REGISTER(0x32))
+ /* const byte x2 = ARG_MAKE_REGISTER(0x32); */
+ #define r_x3 ((byte)ARG_MAKE_REGISTER(0x33))
+ /* const byte x3 = ARG_MAKE_REGISTER(0x33); */
+ #define r_x4 ((byte)ARG_MAKE_REGISTER(0x34))
+ /* const byte x4 = ARG_MAKE_REGISTER(0x34); */
+ #define r_x5 ((byte)ARG_MAKE_REGISTER(0x35))
+ /* const byte x5 = ARG_MAKE_REGISTER(0x35); */
+ #define r_x6 ((byte)ARG_MAKE_REGISTER(0x36))
+ /* const byte x6 = ARG_MAKE_REGISTER(0x36); */
+ #define r_x7 ((byte)ARG_MAKE_REGISTER(0x37))
+ /* const byte x7 = ARG_MAKE_REGISTER(0x37); */
+ #define r_y0 ((byte)ARG_MAKE_REGISTER(0x38))
+ /* const byte y0 = ARG_MAKE_REGISTER(0x38); */
+ #define r_y1 ((byte)ARG_MAKE_REGISTER(0x39))
+ /* const byte y1 = ARG_MAKE_REGISTER(0x39); */
+ #define r_y2 ((byte)ARG_MAKE_REGISTER(0x3A))
+ /* const byte y2 = ARG_MAKE_REGISTER(0x3A); */
+ #define r_y3 ((byte)ARG_MAKE_REGISTER(0x3B))
+ /* const byte y3 = ARG_MAKE_REGISTER(0x3B); */
+ #define r_y4 ((byte)ARG_MAKE_REGISTER(0x3C))
+ /* const byte y4 = ARG_MAKE_REGISTER(0x3C); */
+ #define r_y5 ((byte)ARG_MAKE_REGISTER(0x3D))
+ /* const byte y5 = ARG_MAKE_REGISTER(0x3D); */
+ #define r_y6 ((byte)ARG_MAKE_REGISTER(0x3E))
+ /* const byte y6 = ARG_MAKE_REGISTER(0x3E); */
+ #define r_y7 ((byte)ARG_MAKE_REGISTER(0x3F))
+ /* const byte y7 = ARG_MAKE_REGISTER(0x3F); */
+
+ /* Well almost all of the above are general purpose
+ // x1 and y1 are generally used for the limits
+ // x0 and y0 are generally used for selecting areas
+ */
+
+ #define NUM_REGISTERS 0x40
+
+
+typedef unsigned char vmop_t;
+
+/* ----------------------------------------------------------------------
+ OP CODES:
+*/
+ /* END:
+ end of instructions! */
+ #define o_end ((vmop_t)(0x00))
+ /* const vmop_t end = 0x00; */
+
+ /* NOP:
+ Blank / space filler */
+ #define o_nop ((vmop_t)(0xD1))
+ /* const vmop_t nop = 0xD1; */
+
+ /* PUSH: (1 value param)
+ Copy new context and execute */
+ #define o_push ((vmop_t)(0xD2))
+ /* const vmop_t push = 0xD2; */
+
+ /* POP: (1 value param)
+ Remove current context and execute previous */
+ #define o_pop ((vmop_t)(0xD4))
+ /* const vmop_t pop = 0xD4; */
+
+ /* LOCK: (2 value params)
+ Lock data between the parameters */
+ #define o_lock ((vmop_t)(0xE0))
+ /* const vmop_t lock = 0xE0; */
+
+ /* CHECK: (2 value params)
+ Check data between parameters against locks */
+ #define o_check ((vmop_t)(0xE1))
+ /* const vmop_t check = 0xE1; */
+
+ /* MATCH: (2 value params, plus match structure)
+ Match the regexp against string between the limited beg and end */
+ #define o_match ((vmop_t)(0xC0))
+ /* const vmop_t match = 0xC0; */
+
+ /* SETVAR: (2 value params, plus variable name)
+ Add text between selected registers to a variable */
+ #define o_setvar ((vmop_t)(0xC2))
+ /* const vmop_t setvar = 0xC2; */
+
+ /* CLRVAR: (2 value params, plus variable name)
+ Clear a variable */
+ #define o_clrvar ((vmop_t)(0xC3))
+ /* const vmop_t clrvar = 0xC3; */
+
+ /* JMP: (1 address param)
+ Jump to the specified address */
+ #define o_jmp ((vmop_t)(0xD6))
+ /* const vmop_t jmp = 0xD6; */
+
+ /* JE: (1 address param)
+ Jump if fe flag is set */
+ #define o_je ((vmop_t)(0xD7))
+ /* const vmop_t je = 0xD7; */
+
+ /* JNE: (1 address param)
+ Jump if fe flag is not set */
+ #define o_jne ((vmop_t)(0xD8))
+ /* const vmop_t jne = 0xD8; */
+
+ /* REPL: (2 value params, plus repl sizeof(text_op))
+ Perform a replacement operation with current registers and string*/
+ #define o_repl ((vmop_t)(0xC4))
+ /* const vmop_t repl = 0xC4; */
+
+ /* STOP: (1 value param denoting error or not)
+ Plus an optional message */
+ #define o_stop ((vmop_t)(0x02))
+ /* const vmop_t stop = 0x02; */
+
+ /* CMP: (2 value params)
+ Compare the two values */
+ #define o_cmp ((vmop_t)(0xE9))
+ /* const vmop_t cmp = 0xE9; */
+ #define o_test ((vmop_t)(0xEA))
+ /* const vmop_t test = 0xEA; */
+
+ /* MOV: (2 value params)
+ set first value to be equal to second */
+ #define o_mov ((vmop_t)(0xD9))
+ /* const vmop_t mov = 0xD9; */
+
+ /* CALL: (1 address param)
+ Call function at address */
+ #define o_call ((vmop_t)(0xDA))
+ /* const vmop_t call = 0xDA; */
+
+ /* RET:
+ Return control to previous function */
+ #define o_ret ((vmop_t)(0xDB))
+ /* const vmop_t ret = 0xDB; */
+
+ /* ADD: (2 value params)
+ add second value to first */
+ #define o_add ((vmop_t)(0xDC))
+ /* const vmop_t add = 0xDC; */
+
+ /* SUB: (2 value params)
+ subtract second value from first */
+ #define o_sub ((vmop_t)(0xDD))
+ /* const vmop_t sub = 0xDD; */
+
+ /* TEXT: (text block)
+ put text in the data buffer */
+ #define o_text ((vmop_t)(0xDE))
+
+ /* MESSAGE:
+ output data buffer as a message */
+ #define o_msg ((vmop_t)(0xDF))
+
+
+/* ----------------------------------------------------------------------
+ OP STRUCTURES: for large ops structures to ease access
+*/
+
+#ifdef _WIN32
+#pragma pack(push, ops)
+#endif
+
+#pragma pack(1)
+
+typedef struct _match_op
+{
+ short len; /* Length of structure */
+ byte type; /* Match type */
+}
+match_op;
+
+/* Or these two values for type above */
+static const byte kMatchPcre = 0x01;
+
+typedef struct _match_op_pcre
+{
+ match_op header;
+ short options;
+ char pattern[1];
+} match_op_pcre;
+
+#define match_op_size(op) (sizeof(byte) * (op).len)
+
+typedef struct _text_op
+{
+ short len; /* Length of string */
+ byte string[1]; /* Text to put in buffer */
+} text_op;
+
+#define text_op_size(op) (sizeof(text_op) + (sizeof(char) * (op).len))
+
+typedef struct _var_op
+{
+ short len; /* Length of entire structure */
+ byte name[1]; /* Variable name */
+} var_op;
+
+#define var_op_size(op) (sizeof(var_op) + (sizeof(char) * (op).len))
+
+#pragma pack()
+
+#ifdef _WIN32
+#pragma pack(pop, ops)
+#endif
+
+
+
+#endif /* __OPS_H__20000616 */ \ No newline at end of file
diff --git a/lib/priv.h b/lib/priv.h
new file mode 100644
index 0000000..dce25fe
--- /dev/null
+++ b/lib/priv.h
@@ -0,0 +1,95 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __PRIV_H__
+#define __PRIV_H__
+
+#include "pcre.h"
+#include "execute.h"
+
+/*
+ WARNING: There are two different sets of positions around.
+ One set is absolute. This is relative to the beginning of
+ the document. Relative positions are relative to the beginning
+ of the current buffer.
+
+ - Locks use absolute
+ - Registers use absolute
+ - match code uses relative
+ - Replacements use absolute
+*/
+
+#define REL_TO_ABS(v, s) ((v) + (s)->offset)
+#define ABS_TO_REL(v, s) ((v) - (s)->offset)
+
+
+/* Internal state of rlib */
+struct internal_state
+{
+ /* Set of replacements to be written out */
+ replacement* replaces;
+
+ /* Set of variables currently set */
+ variables vars;
+
+ /* Set of watermarks for each regex */
+ memory mem;
+
+ /* Data for compiled expressions fastmaps etc... */
+ data working;
+
+ /* Locks */
+ locks lcks;
+
+ /* Total amount read before this point */
+ size_t offset;
+
+ uint vmregs[NUM_REGISTERS];
+
+ /* Various options for library */
+ long options;
+
+ /* A translate table for mixed case */
+ char caseTranslate[256];
+};
+
+
+/* Scripting functions */
+int compilerRun(r_script* script, const char* data);
+int compilerOptimize(r_script* script);
+void scriptSetError(r_script* script, const char* format, ...);
+
+/* Stream management functions */
+void opsIterate(vmop_t** ops);
+int opsFree(vmop_t* ops, size_t len);
+int opsDump(vmop_t* ops, FILE* f);
+
+
+/* Execution functions */
+int vmExecute(r_stream* stream, r_script* script);
+bool vmInit(r_stream* stream);
+void vmClean(r_stream* stream);
+void vmFree(r_stream* stream);
+
+static const char* kValidIdentifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
+
+
+#define USE_STACK_VARS
+
+#endif /* __PRIV_H__ */
diff --git a/lib/rep.h b/lib/rep.h
new file mode 100644
index 0000000..673b85d
--- /dev/null
+++ b/lib/rep.h
@@ -0,0 +1,107 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __REP_H__
+#define __REP_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "rlib.h"
+
+/*
+ * This file defines a higher level interface into the rlib library.
+ * See "rlib.h" for the low level interface
+ */
+
+
+/*
+ * r_context
+ * A context with all necessary information for replaces etc...
+ */
+typedef struct _r_context
+{
+ r_stream stream; /* The rlib stream */
+ r_script script; /* The rlib script */
+
+ r_uint block; /* The buffer size used for replaces */
+ r_uint options; /* Options to be passed to rlibInit */
+}
+r_context;
+
+
+/*
+ * repLoad
+ * Loads a rep script from a file. Call this before repInit.
+ * This will load various options from the file in addition
+ * to the raw script (see r_context).
+ *
+ * ctx: The rep context (which should be zero'd)
+ * script: A rep script file open for reading.
+ */
+int repLoad(r_context* ctx, FILE* script);
+
+
+/*
+ * repInit
+ * Called to initialize the rlib with your initialization options
+ * etc...
+ *
+ * ctx: The rep context to initialize
+ */
+int repInit(r_context* ctx);
+
+
+/*
+ * repFiles
+ * Runs a rep script on a file, writing output to an output
+ * file.
+ *
+ * ctx: The rep context.
+ * fileIn: The input file opened for reading.
+ * fileOut: The output file opened for writing.
+ */
+int repFiles(r_context* ctx, FILE* fileIn, FILE* fileOut);
+
+
+/*
+ * repFile
+ * Runs a rep script on a file, sending output to the specifed
+ * callback function.
+ *
+ * ctx: The rep context.
+ * fileIn: The input file opened for reading.
+ */
+int repFile(r_context* ctx, FILE* fileIn);
+
+
+/*
+ * repFree
+ * Free a rep context.
+ *
+ * ctx: The rep context.
+ */
+void repFree(r_context* ctx);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __REP_H__ */
diff --git a/lib/rlib.c b/lib/rlib.c
new file mode 100644
index 0000000..db84548
--- /dev/null
+++ b/lib/rlib.c
@@ -0,0 +1,591 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+/* =========================================================================
+ * Main RLIB
+ * 2000-2001 Copyright, Nate Nielsen
+ */
+
+#include <stdarg.h>
+#include "common/usuals.h"
+#include "common/compat.h"
+#include "lib/rlib.h"
+#include "priv.h"
+#include "rep.h"
+#include "common/binfile.h"
+#include "common/repfile.h"
+
+/* rlibInit: --------------------------------------------------------------
+ * Initialize a stream. We already expect stream to be zero'd
+ */
+
+int rlibInit(r_stream* stream, long options)
+{
+ byte* op = NULL;
+ int i, j;
+ struct internal_state* state;
+
+ if(stream->state != NULL)
+ return R_INVARG;
+
+ /* Allocate a state */
+ state = stream->state =
+ (struct internal_state*)malloc(sizeof(struct internal_state));
+
+ if(!state)
+ return R_NOMEM;
+
+ zero(*state);
+
+ /* Init the stream for the current session */
+ if(!vmInit(stream))
+ return R_NOMEM;
+
+ state->options = options;
+
+ /* Init our case table */
+ /* we only do this once for the whole bunch */
+ for (i = 0; i < 256; i++)
+ stream->state->caseTranslate[i] = i;
+ for (j = 'a'; j <= 'z'; j++)
+ stream->state->caseTranslate[j] = j - ('a' - 'A');
+
+ return R_OK;
+}
+
+
+/* rlibCompile: ----------------------------------------------------
+ * Compile a script
+ */
+int rlibCompile(r_script* script, const char* data)
+{
+ return compilerRun(script, data);
+}
+
+
+/* rlibClear: -------------------------------------------------------------
+ * Prepare stream for a new file
+ */
+void rlibClear(r_stream* stream)
+{
+ if(stream)
+ {
+ if(stream->state)
+ vmClean(stream);
+ }
+}
+
+
+
+/* rLibRun: ---------------------------------------------------------------
+ * The replacement coordinator, output etc...
+*/
+int rlibRun(r_stream* stream, r_script* script, int done)
+{
+ #define RETURN(r) do { retv = r; goto finally; } while(0)
+
+ struct internal_state* state = stream->state;
+ size_t backup, curOffset;
+ int retv = R_OK;
+
+ if(!stream->state)
+ return R_INVARG;
+
+ /* Usually have to have an output function, unless just a matcher */
+ if(!stream->fWrite && !(state->options & RLIB_MODE_MATCHER))
+ return R_INVARG;
+ /* And if we are a matcher we have to have a match function */
+ if(state->options & RLIB_MODE_MATCHER && !stream->fMatch)
+ return R_INVARG;
+
+
+ /*
+ * Keep at least 1/2 amount of backup
+ * Backup is only kept if no matches occur in backup area
+ */
+ backup = done ? 0 : stream->availIn / 2;
+
+ /*
+ * Since there are conversions (between absolute and relative offsets)
+ * below, we need to keep inOffset the same for this session. So copy
+ * it and update it again at the end of the function.
+ */
+ curOffset = 0;
+
+ /* Need some data to work with */
+ if(!stream->nextIn || !stream->availIn)
+ RETURN(R_IN);
+
+
+ stream->total = 0;
+
+ /* Execute the script */
+ retv = vmExecute(stream, script);
+
+ /* Error? */
+ if(retv < 0)
+ RETURN(retv);
+
+ /* Fall through to commit code */
+ if(retv == R_DONE)
+ backup = 0;
+
+ if(!(state->options & RLIB_MODE_MATCHER))
+ {
+ /*
+ * Here we write out what we've found so far and advance the
+ * pointers
+ *
+ * On the first round we look something like this
+ *
+ * <- state->searched ->
+ * |---------------------------------------------| <- stream->nextIn
+ * |----------------------------------------| <- stream->nextOut
+ * replacements found ^- leftovers
+ */
+
+ /* toWrite is the size of the current block we need to transfer */
+ size_t toWrite;
+ /* block is the amount we can actually transfer (depending on output buffer) */
+ size_t block = stream->availIn;
+
+#ifdef VERBOSE
+ dumpRelpacements(stream);
+#endif
+
+ while(state->replaces)
+ {
+ /* Get the amount of data till the current replace */
+ toWrite = block = ABS_TO_REL(state->replaces->beg, state) - curOffset;
+
+ if(!(state->options & RLIB_MODE_PARSER))
+ {
+ /* ... write out up to current replacement... */
+ if(!(stream->fWrite)(stream, stream->nextIn, block))
+ RETURN(R_IOERR);
+ }
+
+ stream->nextIn += block;
+ stream->availIn -= block;
+ curOffset += block;
+
+
+ /* ... check space for replacement ... */
+ block = strlen(state->replaces->text);
+
+ /* ...write out replacement... */
+ if(!(stream->fWrite)(stream, (byte*)state->replaces->text, block))
+ RETURN(R_IOERR);
+
+ /* ... and skip (!) the text that we replaced */
+ block = state->replaces->end - state->replaces->beg;
+ stream->nextIn += block;
+ stream->availIn -= block;
+ curOffset += block;
+
+#ifdef _DEBUG
+ /* Check if things are in the right order */
+ if(state->replaces->next)
+ ASSERT(state->replaces->end <= state->replaces->next->end);
+#endif
+ /* Go to the next replacement */
+ state->replaces = replacementPop(state->replaces);
+ }
+
+ /*
+ * Now we check how much data we have left and copy
+ * up to backup if we have more
+ */
+
+ /* Copy out till backup marker */
+ if((stream->availIn) >= backup)
+ {
+ /* Get block size ... */
+ toWrite = block = stream->availIn - backup;
+
+ if(!(state->options & RLIB_MODE_PARSER))
+ {
+ if(!(stream->fWrite)(stream, stream->nextIn, block))
+ RETURN(R_IOERR);
+ }
+
+ stream->nextIn += block;
+ stream->availIn -= block;
+ curOffset += block;
+ }
+ }
+
+ /*
+ * After this the search could start anew
+ * unless in flush mode or done
+ */
+ if(done == true)
+ RETURN(R_DONE);
+ else
+ RETURN(R_IN);
+
+finally:
+ state->offset += curOffset;
+
+ return retv;
+}
+
+
+/* rlibSetVar: -----------------------------------------------------------
+ * Set a variable for the script to use
+ */
+int rlibSetVar(r_stream* stream, const char* var, const char* val)
+{
+ if(!stream || !stream->state || !var || !val)
+ return R_INVARG;
+
+ variablesClear(&(stream->state->vars), var);
+ return variablesAdd(&(stream->state->vars), var, val) ? R_OK : R_NOMEM;
+}
+
+
+/* rlibDump: --------------------------------------------------------------
+ * Dump the rep script opts for debugging
+ */
+void rlibDump(r_script* script, FILE* f)
+{
+ opsDump(script->ops, f);
+}
+
+
+/* rlibFree: --------------------------------------------------------------
+ * Free associated structures data etc...
+ */
+void rlibFree(r_stream* stream, r_script* script)
+{
+ struct internal_state* state;
+
+ if(stream)
+ {
+ if(state = stream->state)
+ {
+ /* Let execution free it's stuff */
+ vmFree(stream);
+
+ /* And free the state */
+ free(state);
+ stream->state = NULL;
+ }
+
+ zero(*stream);
+ }
+
+ if(script)
+ {
+ if(script->ops)
+ free(script->ops);
+
+ if(script->error)
+ free(script->error);
+
+ zero(*script);
+ }
+
+}
+
+
+/* scriptSetError: -------------------------------------------------
+ * Set the stream error text to whatever
+ */
+void scriptSetError(r_script* script, const char* format, ...)
+{
+ char* msg;
+
+ va_list vl;
+ va_start(vl, format);
+
+ if(script->error)
+ free(script->error);
+
+ if(vasprintf(&msg, format, vl) != -1)
+ script->error = msg;
+ else
+ script->error = NULL;
+
+ va_end(vl);
+}
+
+/* compileAlready: ------------------------------------------------------
+ * See if the file has already been compiled and load if so
+ */
+int compileAlready(r_context* ctx, FILE* file)
+{
+ /* Should have already read header */
+ bfval val;
+ BFILE h = NULL;
+ int retv = R_OK;
+ r_uint temp;
+
+ if(!(h = bfStartFile(file)))
+ RETURN(R_INVARG);
+
+ if(!repfReadHeader(h))
+ RETURN(R_IN);
+
+ while(bfReadValueInfo(h, &val))
+ {
+ if(ferror(file))
+ RETURN(R_IOERR);
+
+ if(feof(file))
+ RETURN(R_INVARG);
+
+ switch(val.id)
+ {
+ case REPVAL_BUFSIZE:
+ if(ctx->block == 0)
+ bfReadValueData(h, &val, &(ctx->block));
+ continue;
+
+ case REPVAL_PARSEMODE:
+ if(bfReadValueData(h, &val, &temp))
+ ctx->options |= (temp != 0) ? RLIB_MODE_PARSER : 0;
+ continue;
+
+ case REPVAL_SCRIPT:
+ {
+ ctx->script.ops = (r_byte*)malloc(val.len);
+ if(!ctx->script.ops)
+ RETURN(R_NOMEM);
+
+ bfReadValueData(h, &val, ctx->script.ops);
+ ctx->script.len = val.len;
+ continue;
+ }
+ break;
+ }
+
+ bfSkipValueData(h, &val);
+ }
+
+finally:
+ if(h)
+ bfClose(h);
+
+ return retv;
+}
+
+
+int repLoad(r_context* ctx, FILE* f)
+{
+ int retv = R_OK;
+ char* buff = NULL;
+ size_t len;
+ int r;
+
+ rlibFree(NULL, &(ctx->script));
+
+ /* Okay now try and read header, and possibly process
+ an already executable file. */
+ switch(r = compileAlready(ctx, f))
+ {
+ /* It's already compiled */
+ case R_OK:
+ RETURN(R_OK);
+ break;
+
+ /* It's not compiled so compile */
+ case R_IN:
+ break;
+
+ /* Failed processing */
+ default:
+ if(r < 0)
+ RETURN(r);
+ break;
+ };
+
+ /* Get file size */
+ len = 0;
+
+ if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) ||
+ fseek(f, 0, SEEK_SET))
+ RETURN(R_IOERR);
+
+ buff = (char*)malloc(len + 1);
+ if(!buff)
+ RETURN(R_NOMEM);
+
+ if(fread(buff, 1, len, f) != len)
+ RETURN(R_IOERR);
+
+ /* Needs compiling */
+ buff[len] = '\0';
+
+ /* Init the stream */
+ r = rlibCompile(&(ctx->script), buff);
+ if(r < 0)
+ RETURN(r);
+
+finally:
+ if(buff)
+ free(buff);
+
+ return retv;
+}
+
+int repInit(r_context* ctx)
+{
+ rlibInit(&(ctx->stream), ctx->options);
+ return R_OK;
+}
+
+int repFile(r_context* ctx, FILE* fIn)
+{
+ r_uint batch; /* Current batch size */
+ r_uint block = ctx->block; /* Block size */
+ r_byte* buff = NULL; /* Input buffer */
+ int retv = R_OK;
+ int r = R_IN;
+ int total = 0;
+
+ if(!block)
+ {
+ if(!fseek(fIn, 0, SEEK_END) && (block = ftell(fIn)) != ~0)
+ block++;
+
+ else
+ block = 0;
+
+ fseek(fIn, 0, SEEK_SET);
+ }
+
+ else
+ {
+ block *= 2;
+ }
+
+ /* Okay now if it's a sane value just allocate it */
+ if(block)
+ {
+ if(block > 0x0F00000)
+ RETURN(R_NOMEM);
+
+ /* Allocate buffers */
+ buff = (r_byte*)malloc(block);
+ if(!buff)
+ RETURN(R_NOMEM);
+ }
+
+ /* Hook buffers to the stream */
+ ctx->stream.nextIn = buff;
+ ctx->stream.availIn = 0;
+
+ /* While we either have more data to put in or more output... */
+ while(!feof(fIn) || r == R_DONE)
+ {
+ /* If rlib wants data then give it */
+ if(r == R_IN)
+ {
+ /* This is a normal standard read */
+ if(buff)
+ {
+ /* Move data to front */
+ memmove(buff, ctx->stream.nextIn, ctx->stream.availIn);
+
+ /* Set pointer to data */
+ ctx->stream.nextIn = buff;
+
+ /* Read rest of block */
+ batch = fread(ctx->stream.nextIn + ctx->stream.availIn,
+ sizeof(r_byte), block - ctx->stream.availIn, fIn);
+
+ ctx->stream.availIn += batch;
+
+ if(ferror(fIn))
+ RETURN(R_IOERR);
+ }
+
+ /* Here we read as much as possible in one shot allocating as we go*/
+ else
+ {
+ batch = 0;
+ block = 0;
+
+ while(!feof(fIn))
+ {
+ if(ferror(fIn))
+ RETURN(R_IOERR);
+
+ block += 0x4000;
+
+ if(block > MAX_BUFF ||
+ !(buff = reallocf(buff, block)))
+ RETURN(R_NOMEM);
+
+ batch += fread(buff + batch, sizeof(r_byte),
+ 0x4000, fIn);
+ }
+
+ ctx->stream.nextIn = buff;
+ ctx->stream.availIn += batch;
+ }
+ }
+
+ /* call rlib */
+ r = rlibRun(&(ctx->stream), &(ctx->script), feof(fIn));
+
+ // Oops!
+ if(r < 0)
+ RETURN(r);
+
+ total += ctx->stream.total;
+ }
+
+finally:
+ if(buff)
+ free(buff);
+
+ ctx->stream.total = total;
+
+ return retv;
+}
+
+int fileOutput(struct r_stream* stream, byte* data, size_t len)
+{
+ FILE* f = (FILE*)stream->arg;
+ return (fwrite(data, 1, len, f) == len) && (!ferror(f));
+}
+
+int repFiles(r_context* ctx, FILE* fIn, FILE* fOut)
+{
+ void* arg = ctx->stream.arg;
+ r_write func = ctx->stream.fWrite;
+ int ret;
+
+ ctx->stream.fWrite = fileOutput;
+ ctx->stream.arg = fOut;
+
+ ret = repFile(ctx, fIn);
+
+ ctx->stream.fWrite = func;
+ ctx->stream.arg = arg;
+
+ return ret;
+}
+
+void repFree(r_context* ctx)
+{
+ rlibFree(&(ctx->stream), &(ctx->script));
+}
diff --git a/lib/rlib.dsp b/lib/rlib.dsp
new file mode 100644
index 0000000..b3b149a
--- /dev/null
+++ b/lib/rlib.dsp
@@ -0,0 +1,140 @@
+# Microsoft Developer Studio Project File - Name="rlib" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Static Library" 0x0104
+
+CFG=rlib - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "rlib.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "rlib.mak" CFG="rlib - Win32 Debug"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "rlib - Win32 Release" (based on "Win32 (x86) Static Library")
+!MESSAGE "rlib - Win32 Debug" (based on "Win32 (x86) Static Library")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "rlib - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "release"
+# PROP Intermediate_Dir "release"
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
+# ADD CPP /nologo /W3 /GX /O2 /I ".." /I "..\win32\pcre\include" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LIB32=link.exe -lib
+# ADD BASE LIB32 /nologo
+# ADD LIB32 /nologo /out:"..\win32\release\rlib.lib"
+
+!ELSEIF "$(CFG)" == "rlib - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "debug"
+# PROP Intermediate_Dir "debug"
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /I "..\win32\pcre\include" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LIB32=link.exe -lib
+# ADD BASE LIB32 /nologo
+# ADD LIB32 /nologo /out:"..\win32\debug\rlibd.lib"
+
+!ENDIF
+
+# Begin Target
+
+# Name "rlib - Win32 Release"
+# Name "rlib - Win32 Debug"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=..\common\binfile.c
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\compat.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\compile.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\execute.c
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\repfile.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\rlib.c
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\xstring.c
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=.\execute.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\ops.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\priv.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\rep.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\rlib.h
+# End Source File
+# End Group
+# End Target
+# End Project
diff --git a/lib/rlib.h b/lib/rlib.h
new file mode 100644
index 0000000..b5f8b68
--- /dev/null
+++ b/lib/rlib.h
@@ -0,0 +1,261 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __RLIB_H__
+#define __RLIB_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct internal_state;
+struct r_stream;
+
+typedef unsigned char r_byte;
+typedef unsigned int r_uint;
+
+
+/*
+ * r_replace:
+ * Represents a replacement which was found. This structure is passed
+ * to the r_match matching callback function below.
+ */
+typedef struct _r_replace
+{
+ r_uint offset; /* The offset from the beginning of the file */
+ r_uint from; /* The offset from the beginning of the current buffer */
+ r_uint flen; /* The length of the data to be replaced */
+ r_byte* to; /* The data to replace */
+ r_uint tlen; /* The length of the replacement data */
+}
+r_replace;
+
+
+
+
+/*
+ * r_match
+ * Callback function which receives replacements made by the script.
+ *
+ * stream: rlib stream
+ * replace: The replacement.
+ *
+ * return 1 to accept this match, or 0 to skip.
+ */
+typedef int (*r_match)(struct r_stream* stream, r_replace* replace);
+
+/*
+ * r_write
+ * Callback used by the rlibRun output processed data.
+ *
+ * stream: rlib stream
+ * data: The data
+ * len: The number of bytes to write from data.
+ */
+typedef int (*r_write)(struct r_stream* stream, r_byte* data, r_uint len);
+
+/*
+ * r_message
+ * Callback which is sent messages from the 'message' command
+ * in a rep script.
+ *
+ * stream: rlib stream
+ * message: Null terminated message
+ */
+typedef void (*r_message)(struct r_stream* stream, const char* message);
+
+
+
+
+
+
+/*
+ * r_stream
+ * The basic data interface into rlib.
+ */
+typedef struct r_stream
+{
+ /* ------ Data you supply -------------- */
+ r_byte* nextIn; /* The next byte to be read by rlib */
+ r_uint availIn; /* The number of bytes at nextIn */
+
+ r_write fWrite; /* Callback used for output */
+ r_match fMatch; /* Callback for confirmation or matching */
+ r_message fMessage; /* Callback for messages from script */
+ void* arg; /* Optional data for the above functions */
+
+ /* -------- Data returned -------------- */
+ r_uint total; /* Total replaces */
+
+ /* -------- Internal ------------------- */
+ struct internal_state* state;
+}
+r_stream;
+
+
+
+
+
+/*
+ * r_script
+ * Represents a loaded rep script along with syntax error
+ * information.
+ */
+typedef struct _r_script
+{
+ r_byte* ops; /* Compiled script */
+ r_uint len; /* The length of the script */
+ char* error; /* Syntax error details */
+ r_uint errline; /* Line number of syntax error */
+}
+r_script;
+
+
+
+
+
+/*
+ * rlibCompile
+ * Call this function to compile a script. It will be compiled into the
+ * r_sript structure. Be sure to zero the script structure passed.
+ *
+ * script: rlib script
+ * data: rep script text. Must be null terminated.
+ */
+int rlibCompile(r_script* script, const char* data);
+
+
+
+/*
+ * rlibDump
+ * Write out byte codes for a script
+ *
+ * script: rlib script
+ * f: Output stream to write to
+ */
+void rlibDump(r_script* script, FILE* f);
+
+
+
+/*
+ * rlibFree
+ * Free internal variables associated with this stream and/or
+ * script structure.
+ *
+ * stream: rlib stream
+ * script: rlib script
+ */
+void rlibFree(r_stream* stream, r_script* script);
+
+
+
+/*
+ * rlibInit
+ * Call this function to initialize a rlib stream.
+ * For any of the following functions to work a stream must have been
+ * initialized.
+ *
+ * stream: Pointer to a zero'd stream structure.
+ * options: Can be any combination of the following mode values.
+ */
+int rlibInit(r_stream* stream, long options);
+
+/* Causes rlib to only output replacing text */
+#define RLIB_MODE_PARSER 0x00000100
+
+/* Causes rlib not to output anything */
+#define RLIB_MODE_MATCHER 0x00000200
+
+
+
+/*
+ * rlibSetVar
+ * Set a variable for use in the script.
+ *
+ * stream: rlib stream
+ * var: Variable name
+ * val: Variable value
+ */
+int rlibSetVar(r_stream* stream, const char* var, const char* val);
+
+
+
+/*
+ * rlibRun
+ * Run the script. rlibRun will only "eat" nextIn upto where the
+ * last replace was found or up half whichever is more. Unless
+ * it's the last buffer in which case it finishes up.
+ *
+ * stream: rlib stream
+ * script: rlib script
+ * done: flag whether this is the last buffer
+ */
+int rlibRun(r_stream* stream, r_script* script, int done);
+
+
+
+/* rlibClear: -----------------------------------------------------------
+ * Prepare and cleanup a context/stream after being run for a second
+ * run.
+ *
+ * stream: rlib context/stream
+ */
+void rlibClear(r_stream* stream);
+
+
+
+
+
+/* ERROR CODES */
+
+/* OK */
+#define R_OK 0
+
+/* Need more input */
+#define R_IN 1
+
+/* Finished processing script */
+#define R_DONE 3
+
+/* Not enough memory */
+#define R_NOMEM -1
+
+/* Syntax error in the script */
+#define R_SYNTAX -2
+
+/* Regular expression error in the script */
+#define R_REGEXP -3
+
+/* Enless loop encountered */
+#define R_LOOP -4
+
+/* User defined error from script */
+#define R_USER -5
+
+/* Read or write error */
+#define R_IOERR -6
+
+/* Invalid argument or stream data member */
+#define R_INVARG -10
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __RLIB_H__ */
diff --git a/rep.dsw b/rep.dsw
new file mode 100644
index 0000000..952638d
--- /dev/null
+++ b/rep.dsw
@@ -0,0 +1,92 @@
+Microsoft Developer Studio Workspace File, Format Version 6.00
+# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
+
+###############################################################################
+
+Project: "droplet"=.\win32\droplet\droplet.dsp - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+ Begin Project Dependency
+ Project_Dep_Name rlib
+ End Project Dependency
+}}}
+
+###############################################################################
+
+Project: "makedrop"=.\win32\makedrop\makedrop.dsp - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+ Begin Project Dependency
+ Project_Dep_Name rlib
+ End Project Dependency
+ Begin Project Dependency
+ Project_Dep_Name droplet
+ End Project Dependency
+}}}
+
+###############################################################################
+
+Project: "rep"=.\src\rep.dsp - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+ Begin Project Dependency
+ Project_Dep_Name rlib
+ End Project Dependency
+}}}
+
+###############################################################################
+
+Project: "repc"=.\src\repc.dsp - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+ Begin Project Dependency
+ Project_Dep_Name rlib
+ End Project Dependency
+}}}
+
+###############################################################################
+
+Project: "rlib"=.\lib\rlib.dsp - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+}}}
+
+###############################################################################
+
+Global:
+
+Package=<5>
+{{{
+}}}
+
+Package=<3>
+{{{
+}}}
+
+###############################################################################
+
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..59b1a23
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,13 @@
+bin_PROGRAMS = rep repc
+
+rep_SOURCES = rep.c file.c file.h ../common/compat.c ../common/xstring.c ../common/repfile.c
+rep_LDADD = -lrlib -lpcre
+rep_CFLAGS = -O0 -I${top_srcdir} -I/usr/local/include
+rep_LDFLAGS = -L${top_srcdir}/lib/.libs -L${top_srcdir}/lib -L/usr/local/lib
+
+repc_SOURCES = repc.c ../common/compat.c ../common/xstring.c ../common/binfile.c ../common/repfile.c
+repc_LDADD = -lrlib -lpcre
+repc_CFLAGS = -O0 -I${top_srcdir} -I/usr/local/include
+repc_LDFLAGS = -L${top_srcdir}/lib/.libs -L${top_srcdir}/lib -L/usr/local/lib
+
+EXTRA_DIST = rep.dsp repc.dsp
diff --git a/src/file.c b/src/file.c
new file mode 100644
index 0000000..4f42a5d
--- /dev/null
+++ b/src/file.c
@@ -0,0 +1,197 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "common/compat.h"
+#include "file.h"
+
+
+#if HAVE_UNISTD_H && HAVE_DIRENT_H
+
+int dir_next(DIR* handle, const char* wildcard, struct dirent* entry)
+{
+ struct dirent* ent = 0;
+
+#ifndef HAVE_FNMATCH
+ if(wildcard != NULL)
+ {
+ errno = EOPNOTSUPPORTED;
+ return 0;
+ }
+#endif
+
+ if(!wildcard)
+ wildcard = "*";
+
+ do
+ {
+ ent = readdir(handle);
+ if(ent == NULL)
+ {
+ errno = ENOENT;
+ break;
+ }
+ }
+#ifdef HAVE_FNMATCH
+ while(0);
+#else
+ while(fnmatch(wildcard, ent->d_name, FNM_PATHNAME) == FNM_NOMATCH);
+#endif
+
+ if(ent)
+ memcpy(entry, ent, sizeof(struct dirent));
+
+ return ent != NULL;
+}
+
+DIR* dir_first(const char* folder, const char* wildcard, struct dirent* entry)
+{
+ DIR* handle = opendir((folder && strlen(folder) > 0) ? folder : ".");
+ if(handle == NULL)
+ return INVALID_DIR;
+
+ if(dir_next(handle, wildcard, entry))
+ return handle;
+ else
+ {
+ errno = ENOENT;
+ dir_close(handle);
+ return INVALID_DIR;
+ }
+}
+
+void dir_close(DIR* handle)
+{
+ closedir(handle);
+}
+
+
+#else
+
+#include <direct.h>
+#include <io.h>
+
+static void copyDirStruct(struct _finddata_t* findinfo, struct dirent* entry)
+{
+ /* Copy the structure */
+ memset(entry, 0, sizeof(struct dirent));
+ entry->d_fileno = ~0;
+ entry->d_reclen = ~0;
+ strcpy(entry->d_name, findinfo->name);
+ entry->d_namlen = strlen(findinfo->name);
+
+ if(findinfo->attrib & _A_SUBDIR)
+ entry->d_type |= DT_DIR;
+ else
+ entry->d_type |= DT_REG;
+}
+
+DIR* dir_first(const char* folder, const char* wildcard, struct dirent* entry)
+{
+ DIR* handle;
+ char buff[MAX_PATH * 2];
+ struct _finddata_t findinfo;
+
+ if(!wildcard)
+ wildcard = "*.*";
+
+ /* Check against buffer overflow hacks */
+ if(strlen(folder) + strlen(wildcard) + 1 > MAX_PATH * 2)
+ {
+ errno = ENAMETOOLONG;
+ return INVALID_DIR;
+ }
+
+ if(!(handle = malloc(sizeof(DIR))))
+ {
+ errno = ENOMEM;
+ return INVALID_DIR;
+ }
+
+ strcpy(buff, folder);
+ strcat(buff, strlen(folder) ? "\\" : "");
+ strcat(buff, wildcard);
+
+ /* Okay get the first file */
+ *handle = _findfirst(buff, &findinfo);
+
+ if(*handle == -1)
+ {
+ free(handle);
+ return INVALID_DIR;
+ }
+ else
+ {
+ copyDirStruct(&findinfo, entry);
+ return handle;
+ }
+}
+
+int dir_next(DIR* handle, const char* wildcard, struct dirent* entry)
+{
+ struct _finddata_t findinfo;
+ int ret = _findnext(*handle, &findinfo) == 0;
+ if(ret)
+ copyDirStruct(&findinfo, entry);
+
+ return ret;
+}
+
+void dir_close(DIR* handle)
+{
+ if(handle)
+ {
+ _findclose(*handle);
+ free(handle);
+ }
+}
+
+#endif
+
+const char* getFilename(const char* path)
+{
+ const char* filename = strrchr(path, '\\');
+ const char* filename2 = strrchr(path, '/');
+ if(filename2 > filename)
+ filename = filename2;
+ return filename ? filename : path;
+}
+
+const char* getExtension(const char* path)
+{
+ return strrchr(path, '.');
+}
+
+int isDots(const char* path)
+{
+ const char* filename = getFilename(path);
+ return (filename[0] == '.' && (filename[1] == '.' || filename[1] == '\0'));
+}
+
+int isDirectory(const char* path)
+{
+ struct stat st;
+ if(stat(path, &st) == -1)
+ return 0;
+
+ return (st.st_mode & S_IFDIR) ? 1 : 0;
+}
diff --git a/src/file.h b/src/file.h
new file mode 100644
index 0000000..1b46dd2
--- /dev/null
+++ b/src/file.h
@@ -0,0 +1,85 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+/* -------------------------------------------------------------------------
+ * Directory iterating routines and other handy stuff
+ */
+
+#ifndef __FILE_H__
+#define __FILE_H__
+
+#ifndef MAX_PATH
+#if defined(MAXNAMLEN)
+#define MAX_PATH MAXNAMLEN
+#elif defined(_MAX_FNAME)
+#define MAX_PATH _MAX_FNAME
+#else
+#define MAX_PATH 256
+#endif
+#endif
+
+#if HAVE_UNISTD_H && HAVE_DIRENT_H
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <dirent.h>
+
+
+#else
+
+
+struct dirent {
+ long d_fileno; /* file number of entry */
+ short d_reclen; /* length of this record */
+ unsigned char d_type; /* file type, see below */
+ unsigned char d_namlen; /* length of string in d_name */
+#ifdef _POSIX_SOURCE
+ char d_name[255 + 1]; /* name must be no longer than this */
+#else
+#define MAXNAMLEN 255
+ char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */
+#endif
+};
+
+#define DT_UNKNOWN 0
+#define DT_FIFO 1
+#define DT_CHR 2
+#define DT_DIR 4
+#define DT_BLK 6
+#define DT_REG 8
+#define DT_LNK 10
+#define DT_SOCK 12
+#define DT_WHT 14
+
+typedef int DIR;
+
+#endif
+
+DIR* dir_first(const char* folder, const char* wildcard, struct dirent* entry);
+int dir_next(DIR* handle, const char* wildcard, struct dirent* entry);
+void dir_close(DIR* handle);
+
+#define INVALID_DIR ((DIR*)-1)
+
+const char* getFilename(const char* szPath);
+const char* getExtension(const char* szPath);
+int isDots(const char* szPath);
+int isDirectory(const char* szPath);
+
+#endif \ No newline at end of file
diff --git a/src/rep.1 b/src/rep.1
new file mode 100644
index 0000000..406dcc8
--- /dev/null
+++ b/src/rep.1
@@ -0,0 +1,95 @@
+.Dd September, 2002
+.Dt REP 1
+.Os Rep 2.3
+.Sh NAME
+.Nm rep
+.Nd a regular expression search and replace language
+.Sh SYNOPSIS
+.Nm
+.Op Fl ipq
+.Op Fl z Ar buffsize
+.Ar script
+.Op Ar infile
+.Op Ar outfile
+.Nm
+.Fl f
+.Op Fl bipq
+.Op Fl z Ar buffsize
+.Ar script
+.Ar file
+.Ar
+.Sh DESCRIPTION
+The
+.Nm
+scripting language is a regular expression language used for fine grained,
+buffer based search and replace. It is not limited to lines. A full description
+of what
+.Nm
+is capable of is outside the scope of this document.
+.Pp
+.Ar script
+is a text or compiled
+.Nm
+script. For details see the language documentation that came along with the distribution.
+.Pp
+When used with the
+.Fl f
+argument
+.Nm
+replaces files in place. Otherwise it reads from
+.Ar infile
+and writes to
+.Ar outfile
+\&. If either infile or outfile are missing or are equal to a dash
+.Sq Li -
+, then rep processes
+.Em stdin
+or
+.Em stdout
+respectively.
+.Sh OPTIONS
+The options are as follows:
+.Bl -tag -width Fl
+.It Fl b
+Backup files where replacements have occurred. The backup files have an
+.Sq x_r
+extension appended to their filename.
+.It Fl i
+Prompt for confirmation before each replacement.
+.It Fl p
+Only output replaced text. Can be used as a rudimentary parser.
+.It Fl q
+Supress status messages. Only errors will be sent to stderr.
+.It Fl z
+Set the replacement buffer size to
+.Ar buffsize .
+This speeds up execution as regular expressions only have to act on a small
+portion of the whole file at once. However the largest match will be limited to
+roughly
+.Ar buffsize
+, so use this option with care. The script loops over each buffer until no more
+matches are found within it. Care is taken to overlap the buffers as much as
+possible to ensure that any match smaller than
+.Ar buffsize
+can be matched.
+.Sh NOTE
+The
+.Nm
+command uses
+.Xr getopt 3
+to parse it's arguments, which allows it to accept
+the
+.Sq Li --
+option which will stop processing of flag options at that point. This allows
+the processing of files with names that begin with a dash
+.Pq Sq - .
+.Sh BUGS
+When reading from
+.Em stdin
+you must specify a buffer size.
+.Sh SEE ALSO
+.Xr repc 1 ,
+.Xr rlib 3 ,
+.Xr pcre 3
+.Sh AUTHOR
+.An Nate Nielsen Aq nielsen@memberwebs.com \ No newline at end of file
diff --git a/src/rep.c b/src/rep.c
new file mode 100644
index 0000000..b333ef5
--- /dev/null
+++ b/src/rep.c
@@ -0,0 +1,703 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <errno.h>
+#include "common/usuals.h"
+#include "common/compat.h"
+#include "lib/rlib.h"
+#include "lib/rep.h"
+#include "common/repfile.h"
+#include "file.h"
+
+/* Amount of context that will be shown in a prompt surrounding the text */
+#define CONFIRM_CONTEXT 0x20
+
+/* Extension used for backup files */
+const char* kBackupExt = ".x_r";
+
+
+
+/* ------------------------------------------------------------------------
+ * GLOBALS
+ */
+
+/*
+ * Since WIN32 has a hokey (did I say hokey, I meant crappy) shell
+ * we add a bit more file selection functionality if building there.
+ * We allow a set wildcards to be specified. These apply when recursing
+ * folders.
+ */
+#ifdef _WIN32
+ #define MAX_WILDCARDS 0x20
+ const char* kAllFiles = "*.*";
+
+ /* When recursing through folders this is the set of files to replace */
+ char* g_wildcards[MAX_WILDCARDS];
+#endif
+
+/* Quiet mode or not? */
+bool g_showStatus = true;
+
+/* Backups or not? */
+bool g_keepBackups = false;
+
+/*
+ * For status updates the amount of replacements and the amount of
+ * files seen
+ */
+long g_totalReplaces = 0;
+long g_totalFiles = 0;
+
+
+/* ------------------------------------------------------------------------
+ * FORWARD DECLARATIONS
+ */
+
+/* The two match callbacks */
+int matchConfirm(r_stream* stream, r_replace* repl);
+int matchStatus(r_stream* stream, r_replace* repl);
+
+/* The main replace functions */
+int replaceFolder(r_context* ctx, char* szWildCard);
+int replaceFile(r_context* ctx, FILE* fIn, FILE* fOut);
+int replaceFilename(r_context* ctx, const char* szIn, const char* szOut);
+int replaceSingleFile(r_context* ctx, const char* szIn);
+
+/* Error stuff */
+int usage();
+
+
+
+/* ------------------------------------------------------------------------ */
+
+int main(int argc, char* argv[])
+{
+ /* We use one r_stream throughout for speed */
+ r_context context;
+ bool fileMode = false;
+ int ch = 0;
+
+
+ /*
+ * Function return values throughout program are a bit different
+ *
+ * 0 = normal operation
+ * -1 = nothing happened but successful
+ * >0 = error code
+ */
+ int ret = 0;
+ int r = R_OK;
+
+
+ /* Enough params? */
+ if(argc < 2)
+ return usage();
+
+
+ /* Init the stream/context */
+ memset(&context, 0, sizeof(context));
+
+
+ /* A little preparation */
+ context.stream.fMatch = matchStatus;
+
+
+#ifdef _WIN32
+ *g_wildcards = NULL;
+
+ while((ch = getopt(argc, argv, "bcipqw:z:")) != -1)
+#else
+ while((ch = getopt(argc, argv, "bcipqz:")) != -1)
+#endif
+ {
+ switch(ch)
+ {
+ /* Use backups */
+ case 'b':
+ g_keepBackups = true;
+ break;
+
+ case 'i':
+ fileMode = true;
+ break;
+
+ /* Confirmation mode */
+ case 'c':
+ context.stream.fMatch = matchConfirm;
+ break;
+
+ /* Quiet mode */
+ case 'q':
+ g_showStatus = false;
+ break;
+
+ /* Parse mode */
+ case 'p':
+ context.options |= RLIB_MODE_PARSER;
+ break;
+
+#ifdef _WIN32
+ case 'w':
+ {
+ /* Get all the wildcards out */
+ size_t len = 0;
+ char* arg = optarg;
+ char** wildcards = g_wildcards;
+
+ /*
+ * Enumerate the wild cards seperating them
+ * at semi-colons or colons
+ */
+ while((len = strcspn(arg, ";"))
+ && (wildcards < (g_wildcards + MAX_WILDCARDS)))
+ {
+ bool last = (arg[len] == '\0');
+
+ arg[len] = '\0';
+ *wildcards = arg;
+
+ wildcards++;
+ arg += len;
+ arg++;
+
+ if(last) break;
+ }
+
+ /* Null terminate the array */
+ *wildcards = NULL;
+ }
+ break;
+#endif
+
+ case 'z':
+ context.block = atoi(optarg);
+ if(context.block <= 0x20)
+ errx(2, "invalid argument. specify block size greater than 32.");
+ break;
+
+ case '?':
+ default:
+ return usage();
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+
+#ifdef _WIN32
+ if(*g_wildcards == NULL)
+ {
+ g_wildcards[0] = "*.*";
+ g_wildcards[1] = NULL;
+ }
+#endif
+
+
+ /*
+ * The next param should be the script file
+ * so read it...
+ */
+ if(argc > 0)
+ {
+ FILE* file = fopen(argv[0], "rb");
+ if(!file)
+ err(1, "couldn't open rep script file: %s", argv[0]);
+
+ r = repLoad(&context, file);
+ fclose(file);
+
+ if(r < 0)
+ exit(errmsg(r, &(context.script)));
+ }
+ else
+ return usage();
+
+ /*
+ * Now initialize the rlib library structures
+ */
+ r = repInit(&context);
+ if(r < 0)
+ exit(errmsg(r, NULL));
+
+ argc--;
+ argv++;
+
+
+ /*
+ * Okay now we have two options:
+ *
+ * If -f was specified we read each argument and replace the file in place,
+ * possibly with backups, descend into folders etc...
+ *
+ * Or if not specified we read from the in file and output to the outfile
+ * If out or both in and out are not specified then use stdin/stdout
+ */
+ if(!fileMode)
+ {
+ char* in = "-";
+ char* out = "-";
+ int ret;
+
+ if(argc > 2)
+ usage();
+ if(argc > 0)
+ in = argv[0];
+ if(argc > 1)
+ out = argv[1];
+
+ ret = rlibSetVar(&(context.stream), "FILENAME", in);
+ if(ret < 0)
+ ret = errmsg(ret, &(context.script));
+ else
+ ret = replaceFilename(&context, in, out);
+ }
+ else
+ {
+ while(argc > 0)
+ {
+ if(isDirectory(argv[0]))
+ ret = replaceFolder(&context, argv[0]);
+ else
+ ret = replaceSingleFile(&context, argv[0]);
+
+ if(ret > 0)
+ break;
+
+ argc--;
+ argv++;
+ }
+ }
+
+ /* Release the stream */
+ repFree(&context);
+
+ /* Give some stats */
+ if(ret == 0 && g_showStatus)
+ fprintf(stderr, "\n[%d replacements in %d files]\n", g_totalReplaces, g_totalFiles);
+
+ /* Done! */
+ return ret <= 0 ? 0 : ret;
+}
+
+
+
+/* replaceFolder: --------------------------------------------------------------
+ * Enumerate all files in a folder and do a replace on each one using
+ * backups (Note: this function is called recursively)
+ */
+int replaceFolder(r_context* ctx, char* folder)
+{
+ struct dirent ent;
+ char old[260]; /* Save the current folder (as we'll change it) */
+ int ret = 0;
+ DIR* dir;
+
+ /*
+ * We use a null wildcard as the default and replace below
+ * if running under WIN32
+ */
+ char* wildcard = NULL;
+ char** wildcards = &wildcard;
+
+
+ /* Backup the current directory */
+ if(!getcwd(old, 260))
+ err(1, "couldn't get working folder");
+
+
+#ifdef _WIN32
+ /*
+ * Now loop through all the wildcards and search for them
+ * in the folder
+ */
+ wildcards = g_wildcards;
+ while(*wildcards)
+ {
+#endif
+ dir = dir_first(folder, *wildcards, &ent);
+
+ if(dir == INVALID_DIR && errno != ENOENT)
+ err(1, "couldn't list path: %s", folder);
+
+ /* Any files */
+ if(dir != INVALID_DIR)
+ {
+ /* Change dir to the folder we're searching in */
+ chdir(folder);
+
+ /* And for every file */
+ do
+ {
+ if(ent.d_type & DT_DIR)
+ {
+ int r = 0;
+
+ if(g_showStatus)
+ fprintf(stderr, "%s ", ent.d_name);
+
+ r = replaceSingleFile(ctx, ent.d_name);
+
+ if(g_showStatus)
+ fputc('\n', stderr);
+
+ if(r > 0)
+ return r;
+ if(r > ret)
+ ret = r;
+ }
+
+ }
+ while(dir_next(dir, *wildcards, &ent));
+
+ if(errno != 0 && errno != ENOENT)
+ err(1, "couldn't list path: %s", folder);
+
+ dir_close(dir);
+
+ /*
+ * Change directory back to where we saved it
+ * the reason we do this is that in some cases (sub-folders)
+ */
+ _chdir(old);
+ }
+
+#ifdef _WIN32
+ wildcards++;
+ }
+#endif
+
+ /* Now we enumerate all the folders */
+ dir = dir_first(folder, NULL, &ent);
+
+ if(dir == INVALID_DIR && errno != ENOENT)
+ err(1, "couldn't list path: %s", folder);
+
+ /* Any files? */
+ if(dir != INVALID_DIR)
+ {
+ /* Go into folder */
+ chdir(folder);
+
+ do
+ {
+ /* For each folder... */
+ if(ent.d_type & DT_DIR)
+ {
+ int r = 0;
+
+ /* ... that's not dots ... */
+ if(isDots(ent.d_name))
+ continue;
+
+ /* ... recursively call ourselves */
+ ret = replaceFolder(ctx, ent.d_name);
+
+ if(r > 0)
+ return r;
+ if(r > ret)
+ ret = r;
+ }
+ }
+ while(dir_next(dir, NULL, &ent));
+
+ if(errno != 0 && errno != ENOENT)
+ err(1, "couldn't list path: %s", folder);
+
+ dir_close(dir);
+
+ /* Change directory back */
+ chdir(old);
+ }
+
+ /* Did anything change? */
+ return ret;
+}
+
+
+/* replaceSingleFile: -------------------------------------------------------
+ * Replace a file into itself using temp/backups
+ */
+int replaceSingleFile(r_context* ctx, const char* file)
+{
+ int ret = 2;
+ char temp [260 + 4]; /* Temp buffer for filenames */
+
+ ret = rlibSetVar(&(ctx->stream), "FILENAME", file);
+ if(ret < 0)
+ return errmsg(ret, &(ctx->script));
+
+ /* Make a temp/backup name */
+ strcpy(temp, file);
+ strcat(temp, kBackupExt);
+
+ /* Now we do this 2 ways, if we're keeping backups then... */
+ if(g_keepBackups)
+ {
+ /* Rename the original file to the backup */
+ if(rename(file, temp))
+ err(1, "couldn't make backup file: %s", temp);
+
+ /*
+ * Do a replacement operation back to the original
+ * from the backup
+ */
+ ret = replaceFilename(ctx, temp, file);
+
+ /* If there were no replacements */
+ if(ret == -1)
+ {
+ /* Remove the replacement file */
+ unlink(file);
+
+ /* And rename the backup file back */
+ if(rename(temp, file))
+ err(1, "couldn't rename file: %s", file);
+ }
+ }
+
+ /* No backups, much faster! */
+ else
+ {
+ /* Do a replacement operation to a temp file */
+ ret = replaceFilename(ctx, file, temp);
+
+ /* If there were replacements */
+ if(ret == 0)
+ {
+ /* Remove original file */
+ unlink(file);
+
+ /* Rename temp to original */
+ if(rename(temp, file))
+ err(1, "couldn't rename file: %s", file);
+ }
+
+ /* If no replacements */
+ else
+ {
+ /* Remove temp file */
+ unlink(temp);
+ }
+ }
+
+ return ret;
+}
+
+
+/* replaceFilename: -----------------------------------------------------------
+ * Replace one file into another
+ */
+int replaceFilename(r_context* ctx, const char* in, const char* out)
+{
+ FILE* fIn;
+ FILE* fOut;
+ int ret;
+
+ if(!strcmp(in, "-"))
+ {
+ /*
+ * If someone specified stdin as the input make sure we're
+ * not going to put up confirmations as we need input from
+ * stdin ie: YNA or whatever key
+ */
+ if(ctx->stream.fMatch == matchConfirm)
+ errx(2, "can't confirm replacements while using stdin as input");
+
+ /* Input is stdin */
+ fIn = stdin;
+ }
+
+ /* Input is a file */
+ else
+ {
+#ifdef _WIN32
+ if(!(fIn = fopen(in, "rb")))
+#else
+ if(!(fIn = fopen(in, "r")))
+#endif
+ err(1, "couldn't open input file: %s", in);
+ }
+
+ /* Output is stdout */
+ if(!strcmp(out, "-"))
+ fOut = stdout;
+
+ /* Output is a file */
+#ifdef _WIN32
+ else if(!(fOut = fopen(out, "wb")))
+#else
+ else if(!(fOut = fopen(out, "w")))
+#endif
+ err(1, "couldn't open output file: %s", in);
+
+
+ /* Do replacements! */
+ ret = replaceFile(ctx, fIn, fOut);
+
+ /* Close any files */
+ if(fIn != stdin)
+ fclose(fIn);
+ if(fOut != stdout)
+ fclose(fOut);
+
+ return ret;
+}
+
+
+
+/* matchConfirm: -----------------------------------------------------------------
+ * Confirmation callback for rlib. Used when -i flag is given
+ */
+int matchConfirm(r_stream* stream, r_replace* repl)
+{
+ /* Remember if someone pressed A */
+ static bool all = false;
+ char ch = 0;
+ int ret;
+ size_t context_start, context_end;
+
+ if(all)
+ return 1;
+
+ fprintf(stderr, "\n------------- ORIGINAL TEXT between ~{ and }~ markers ---------------\n");
+
+ /* Figure out if we have some context before replacement */
+ /* and how much */
+ context_start = 0;
+ if(repl->from >= CONFIRM_CONTEXT)
+ context_start = repl->from - CONFIRM_CONTEXT;
+
+
+ /* Figure out if we have context after replacement */
+ context_end = stream->availIn;
+
+ if(stream->availIn >= repl->from + repl->flen + CONFIRM_CONTEXT)
+ context_end = repl->from + repl->flen + CONFIRM_CONTEXT;
+
+ /* Write Context Before */
+ fwrite(stream->nextIn + context_start, sizeof(char),
+ repl->from - context_start, stderr);
+ /* Write our tag */
+ fprintf(stderr, "~{");
+ /* Write from text */
+ fwrite(stream->nextIn + repl->from, sizeof(char), repl->flen, stderr);
+
+ /* Write tag */
+ fprintf(stderr, "}~");
+ /* Write Context After */
+ fwrite(stream->nextIn + repl->from + repl->flen, sizeof(char),
+ context_end - (repl->from + repl->flen), stderr);
+ /* Separator */
+ fprintf(stderr, "\n----------------------------- NEW TEXT -------------------------------\n");
+ // Replace with this text
+ fwrite(repl->to, sizeof(byte), repl->tlen, stderr);
+ /* Prompt */
+ fprintf(stderr, "\n----------------------------------------------------------------------\n");
+
+ fprintf(stderr, "(YES\\No\\All)? ");
+
+ setbuf(stdin, NULL);
+ while(ch != 'y' && ch != 'n' && ch != 'a' && ch != '\r')
+ ch = tolower(getchar());
+
+ if(ch == 'a')
+ stream->fMatch = matchStatus;
+
+ fprintf(stderr, "\r \r");
+
+ ret = (ch == 'y' || ch == 'a' || ch == '\r') ? true : false;
+
+ if(ret)
+ g_totalReplaces++;
+
+ return ret;
+}
+
+
+/* matchStatus: ----------------------------------------------------------
+ * Confirmation function for rlib. Used for status display
+ */
+int matchStatus(r_stream* stream, r_replace* repl)
+{
+ if(g_showStatus)
+ fputc('.', stderr);
+
+ g_totalReplaces++;
+
+ return 1;
+}
+
+
+/* outputMsg: --------------------------------------------------------------
+ * Callback for rlib when a message goes to the console.
+ */
+void outputMsg(struct r_stream* stream, const char* data)
+{
+ fprintf(stderr, g_showStatus ? "\n%s " : "%s\n", data);
+}
+
+/* replaceFile: ------------------------------------------------------------
+ * Replace one file into another using file handles. This is where
+ * the main stuff actually happens.
+ */
+int replaceFile(r_context* ctx, FILE* fIn, FILE* fOut)
+{
+ int ret;
+
+ ctx->stream.fMessage = outputMsg;
+ ctx->stream.total = 0;
+
+ ret = repFiles(ctx, fIn, fOut);
+
+ /* fatal errors */
+ if(ret < 0)
+ return errmsg(ret, &(ctx->script));
+
+ ret = (ctx->stream.total > 0) ? 0 : -1;
+
+ g_totalFiles++;
+ rlibClear(&(ctx->stream));
+
+ return ret;
+}
+
+/* usage: -------------------------------------------------------------
+ * Display usage info
+ */
+int usage()
+{
+#ifdef _WIN32
+ fprintf(stderr, "usage: rep [-cpqx] [ -z buffsize ] script infile outfile\n");
+ fprintf(stderr, " rep -i [-bcpqx] [ -z buffsize ] [ -w wildcard ] script file ...\n");
+#else
+ fprintf(stderr, "usage: rep [-cpq] [ -z buffsize ] script infile outfile\n");
+ fprintf(stderr, " rep -i [-bcpq] [ -z buffsize ] script file ...\n");
+#endif
+ return 2;
+}
+
diff --git a/src/rep.dsp b/src/rep.dsp
new file mode 100644
index 0000000..37a8fd0
--- /dev/null
+++ b/src/rep.dsp
@@ -0,0 +1,114 @@
+# Microsoft Developer Studio Project File - Name="rep" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Console Application" 0x0103
+
+CFG=rep - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "rep.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "rep.mak" CFG="rep - Win32 Debug"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "rep - Win32 Release" (based on "Win32 (x86) Console Application")
+!MESSAGE "rep - Win32 Debug" (based on "Win32 (x86) Console Application")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "rep - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "release"
+# PROP Intermediate_Dir "release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
+# ADD CPP /nologo /W3 /GX /O2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 libpcre.a kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /out:"../win32/release/rep.exe" /libpath:"..\win32\pcre\lib\\"
+
+!ELSEIF "$(CFG)" == "rep - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "debug"
+# PROP Intermediate_Dir "debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 libpcre.a kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /out:"../win32/debug/rep.exe" /pdbtype:sept /libpath:"..\win32\pcre\lib\\"
+
+!ENDIF
+
+# Begin Target
+
+# Name "rep - Win32 Release"
+# Name "rep - Win32 Debug"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=..\common\compat.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\file.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\rep.c
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=.\file.h
+# End Source File
+# End Group
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# End Group
+# End Target
+# End Project
diff --git a/src/repc.1 b/src/repc.1
new file mode 100644
index 0000000..49045ae
--- /dev/null
+++ b/src/repc.1
@@ -0,0 +1,73 @@
+.Dd September, 2002
+.Dt REPC 1
+.Os Rep 2.3
+.Sh NAME
+.Nm repc
+.Nd compiler for the
+.Xr rep 1
+scripting language
+.Sh SYNOPSIS
+.Nm
+.Op Fl rp
+.Op Fl z Ar buffsize
+.Ar script
+.Ar outfile
+.Nm
+.Fl d
+.Ar script
+.Sh DESCRIPTION
+The
+.Nm
+command compiles
+.Xr rep 1
+scripts into an byte code format. This speeds up execution as compilation is not
+required at run time. Also useful when using the
+.Xr rlib 3
+library.
+.Pp
+.Ar script
+is compiled into
+.Ar output .
+The
+.Fl z
+and
+.Fl p
+options are stored with the compiled script, and will be used as defaults when the
+script is executed.
+.Sh OPTIONS
+The options are as follows:
+.Bl -tag -width Fl
+.It Fl d
+Dump dissassembled byte code.
+.It Fl p
+Default to parse mode. When the script is executed only replaments
+will be written out, rather than unreplaced text.
+.It Fl r
+Compile into raw byte code format. For use with the
+.Xr rlib 3
+library. Any options are not stored with the code.
+.Xr rep 3
+cannot read this format.
+.It Fl z
+Set the default buffer size to
+.Ar buffsize
+and store it along with the compiled script. See
+.Xr rep 1
+for a description of the buffer size.
+.Sh NOTE
+The
+.Nm
+command uses
+.Xr getopt 3
+to parse it's arguments, which allows it to accept
+the
+.Sq Li --
+option which will stop processing of flag options at that point. This allows
+the processing of files with names that begin with a dash
+.Pq Sq - .
+.Sh SEE ALSO
+.Xr rep 1 ,
+.Xr rlib 3 ,
+.Xr pcre 3
+.Sh AUTHOR
+.An Nate Nielsen Aq nielsen@memberwebs.com \ No newline at end of file
diff --git a/src/repc.c b/src/repc.c
new file mode 100644
index 0000000..8cd5f9b
--- /dev/null
+++ b/src/repc.c
@@ -0,0 +1,192 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <ctype.h>
+#include <errno.h>
+#include "common/usuals.h"
+#include "common/compat.h"
+#include "lib/rlib.h"
+#include "lib/rep.h"
+#include "common/binfile.h"
+#include "common/repfile.h"
+
+int usage();
+int help();
+
+
+/* --------------------------------------------------------------------------- */
+
+int main(int argc, char* argv[])
+{
+ r_context ctx;
+ int ch = 0;
+ int r = R_OK;
+
+ bool dump = false; /* Do we dump the ops to the display */
+ bool raw = false; /* Output raw script data or rep file */
+
+ memset(&ctx, 0, sizeof(ctx));
+
+ /* Enough params? */
+ if(argc < 2)
+ return usage();
+
+ /* Parse the arguments */
+ while((ch = getopt(argc, argv, "dprz:")) != -1)
+ {
+ switch(ch)
+ {
+ /* Output raw script ops to file instead of rep file*/
+ case 'r':
+ raw = true;
+ break;
+
+ /* Dump rep script ops to stdout */
+ case 'd':
+ dump = true;
+ break;
+
+ /* Set parse mode option in rep file */
+ case 'p':
+ ctx.options |= RLIB_MODE_PARSER;
+ break;
+
+ /* Set the buffer size in rep file */
+ case 'z':
+ ctx.block = atoi(optarg);
+ if(ctx.block <= 0x20)
+ errx(2, "invalid argument. specify block size greater than 32.");
+ break;
+
+ case '?':
+ default:
+ return usage();
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+
+ /* Okay now compile */
+
+ /* Make sure we have a valid amount of file names */
+ if(argc < 1)
+ return usage();
+
+ /*
+ * The next param should be the script file
+ * so read it...
+ */
+ {
+ FILE* file = fopen(argv[0], "rb");
+ if(!file)
+ err(1, "couldn't open rep script file: %s", argv[0]);
+
+ r = repLoad(&ctx, file);
+ fclose(file);
+
+ if(r < 0)
+ exit(errmsg(r, &(ctx.script)));
+ }
+
+ argc--;
+ argv++;
+
+ /* Now if we were asked to dump then output the file */
+ if(dump)
+ {
+ if(argc != 0)
+ return usage();
+
+ rlibDump(&(ctx.script), stdout);
+ }
+
+ /* Otherwise we write it out to a file */
+ else
+ {
+ FILE* file;
+ char* output = argv[0];
+
+ if(argc < 1)
+ return usage();
+
+ /* Opet the output file */
+ if(!(file = fopen(output, "wb")))
+ err(1, "couldn't open output file for compiled script: %s\n", output);
+
+
+ /* Write a full rep file if not in raw mode */
+ if(!raw)
+ {
+ bfval val;
+ BFILE h;
+
+ if(!(h = bfStartFile(file)))
+ errx(1, "couldn't open output file for compiled script: %s\n", output);
+
+ repfWriteHeader(h);
+
+ bfWriteInt(h, REPVAL_PARSEMODE,
+ ctx.options & RLIB_MODE_PARSER ? 1 : 0);
+
+ if(ctx.block != 0)
+ bfWriteInt(h, REPVAL_BUFSIZE, ctx.block);
+
+ val.id = REPVAL_SCRIPT;
+ val.len = ctx.script.len;
+ val.type = BINTYPE_DATA;
+
+ bfWriteValue(h, &val, ctx.script.ops);
+
+ bfWriteEnd(h);
+ bfClose(h);
+ }
+ else
+ {
+ /* Just write out the raw script */
+ fwrite(ctx.script.ops, 1, ctx.script.len, file);
+ }
+
+ if(ferror(file))
+ err(1, "couldn't write to file: %s", output);
+
+ fclose(file);
+ }
+
+ repFree(&ctx);
+
+ return 0;
+}
+
+/* usage: ----------------------------------------------------------------
+ */
+int usage()
+{
+ fprintf(stderr, "usage: repc [-rp] [ -z buffsize ] script output\n");
+ fprintf(stderr, " repc -d script\n");
+ return 2;
+}
diff --git a/src/repc.dsp b/src/repc.dsp
new file mode 100644
index 0000000..47c66d9
--- /dev/null
+++ b/src/repc.dsp
@@ -0,0 +1,110 @@
+# Microsoft Developer Studio Project File - Name="repc" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Console Application" 0x0103
+
+CFG=repc - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "repc.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "repc.mak" CFG="repc - Win32 Debug"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "repc - Win32 Release" (based on "Win32 (x86) Console Application")
+!MESSAGE "repc - Win32 Debug" (based on "Win32 (x86) Console Application")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "repc - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "release"
+# PROP Intermediate_Dir "release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
+# ADD CPP /nologo /W3 /GX /O2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 libpcre.a kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /out:"../win32/release/repc.exe" /libpath:"..\win32\pcre\lib\\"
+
+!ELSEIF "$(CFG)" == "repc - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "debug"
+# PROP Intermediate_Dir "debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 libpcre.a kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /out:"../win32/debug/repc.exe" /pdbtype:sept /libpath:"..\win32\pcre\lib\\"
+
+!ENDIF
+
+# Begin Target
+
+# Name "repc - Win32 Release"
+# Name "repc - Win32 Debug"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=..\common\compat.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\repc.c
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=..\common\compat.h
+# End Source File
+# End Group
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# End Group
+# End Target
+# End Project
diff --git a/stamp-h b/stamp-h
new file mode 100644
index 0000000..9788f70
--- /dev/null
+++ b/stamp-h
@@ -0,0 +1 @@
+timestamp
diff --git a/stamp-h.in b/stamp-h.in
new file mode 100644
index 0000000..9788f70
--- /dev/null
+++ b/stamp-h.in
@@ -0,0 +1 @@
+timestamp
diff --git a/stamp-h1 b/stamp-h1
new file mode 100644
index 0000000..9788f70
--- /dev/null
+++ b/stamp-h1
@@ -0,0 +1 @@
+timestamp
diff --git a/win32/Makefile.am b/win32/Makefile.am
new file mode 100644
index 0000000..144140f
--- /dev/null
+++ b/win32/Makefile.am
@@ -0,0 +1,2 @@
+SUBDIRS = common droplet makedrop
+
diff --git a/win32/common/Makefile.am b/win32/common/Makefile.am
new file mode 100644
index 0000000..e247bdb
--- /dev/null
+++ b/win32/common/Makefile.am
@@ -0,0 +1,2 @@
+EXTRA_DIST = atlprsht.h atlwinhk.h droplet.cpp droplet.h errutil.cpp errutil.h mystring.h rep.ico rliberr.mc
+
diff --git a/win32/common/atlprsht.h b/win32/common/atlprsht.h
new file mode 100644
index 0000000..e5181ea
--- /dev/null
+++ b/win32/common/atlprsht.h
@@ -0,0 +1,915 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+// PropertyPage.h: interface for the CPropertyPage class.
+// Implements Property Pages and Sheets for ATL
+// By Stef 06/10/99
+//
+// This class uses an MFC type Interface (CPropertySheet, CPropertyPage)
+// the only difference being that many of the virtual notify functions
+// are left for you to define in your derived classes using your message map
+//
+// Notes for CPropertySheet:
+// 1. If you define a WM_INITDIALOG handler in your derived class make
+// sure to either call the base handler (OnInitDialog) or set
+// bHandled to false.
+//
+//
+//////////////////////////////////////////////////////////////////////
+
+#if !defined(AFX_PROPERTYPAGE_H__1508DAF4_7AD1_11D3_BF96_0020182B97FC__INCLUDED_)
+#define AFX_PROPERTYPAGE_H__1508DAF4_7AD1_11D3_BF96_0020182B97FC__INCLUDED_
+
+#if _MSC_VER > 1000
+#pragma once
+#endif // _MSC_VER > 1000
+#include <atlwin.h>
+#include <atlwinhk.h>
+#include <commctrl.h>
+
+
+// Property Sheet control id's (determined with Spy++)
+#define ID_APPLY_NOW 0x3021
+#define ID_WIZBACK 0x3023
+#define ID_WIZNEXT 0x3024
+#define ID_WIZFINISH 0x3025
+
+#define AFX_IDC_TAB_CONTROL 0x3020
+
+// determine number of elements in an array (not bytes)
+#define _countof(array) (sizeof(array)/sizeof(array[0]))
+
+template <class TBase> class CPropertySheetT;
+template <class TBase> class CPropertyPageT;
+
+const int _PropSheetIDs[] =
+ { ID_WIZNEXT, ID_WIZFINISH, ID_WIZBACK, IDCANCEL };
+const int _PropSheetButtons[] =
+ { IDOK, IDCANCEL, ID_APPLY_NOW, IDHELP };
+
+////////////////////////////////////////////////////////////////////////
+// Implements one page of a Property Sheet
+template <class TBase = CWindow>
+class CPropertyPageT : public CDialogImplBaseT< TBase >
+{
+// Construction
+public:
+ CPropertyPageT(UINT IDD) : m_szCaption(NULL)
+ { Construct(IDD, _T("")); }
+
+ // Caption from Resource
+ CPropertyPageT(UINT IDD, UINT nIDCaption) : m_szCaption(NULL)
+ { Construct(IDD, nIDCaption); }
+
+ CPropertyPageT(UINT IDD, LPCTSTR szCaption) : m_szCaption(NULL)
+ { Construct(IDD, szCaption); }
+
+ void Construct(UINT IDD, LPCTSTR szCaption)
+ { CommonConstruct(IDD, szCaption); }
+
+ void Construct(UINT IDD, UINT nIDCaption)
+ {
+ ATLASSERT(nIDCaption != 0);
+ USES_CONVERSION;
+ CComBSTR bsCaption;
+ bsCaption.LoadString(nIDCaption);
+ CommonConstruct(IDD, OLE2T(bsCaption));
+ }
+
+ // All Construction ends up here
+ void CommonConstruct(UINT IDD, LPCTSTR szCaption);
+
+ ~CPropertyPageT()
+ { if(m_szCaption) delete[] m_szCaption; }
+
+// Operations
+ void CancelToClose();
+ void SetModified(bool bChanged);
+ void SetHelp(bool bHelp);
+ LRESULT QuerySiblings(WPARAM wParam, LPARAM lParam);
+ bool IsButtonEnabled(int iButton);
+ void EndDialog(int nID);
+
+// Status
+ CPropertySheetT<TBase>* GetParentSheet();
+
+// Implementaition
+protected:
+ CPropertySheetT<TBase>* m_pSheet; // Pointer to Sheet
+ static UINT CALLBACK PropPageCallback(HWND hwnd, UINT uMsg,
+ LPPROPSHEETPAGE ppsp);
+
+// Data
+public:
+ PROPSHEETPAGE m_psp; // API Structure
+ LPTSTR m_szCaption; // Caption
+
+ friend class CPropertySheetT<TBase>;
+};
+
+// Common Construction
+template <class TBase>
+void CPropertyPageT<TBase>::CommonConstruct(UINT IDD, LPCTSTR szCaption)
+{
+ // Clear
+ memset(&m_psp, 0, sizeof(m_psp));
+
+ m_psp.dwSize = sizeof(m_psp);
+ m_psp.hInstance = _Module.m_hInstResource;
+ m_psp.pszTemplate = MAKEINTRESOURCE(IDD);
+ // Set to DialogProc
+ m_psp.pfnDlgProc = (DLGPROC)StartDialogProc;
+ // Use to hook up to ATL WndProc
+ m_psp.pfnCallback = PropPageCallback;
+ m_psp.dwFlags = PSP_USETITLE | PSP_USECALLBACK;
+ // Passed to PropPageCallback
+ m_psp.lParam = (LPARAM)this;
+
+ // Allocate String
+ if(m_szCaption && _tcslen(m_szCaption) < _tcslen(szCaption))
+ {
+ delete[] m_szCaption;
+ m_szCaption = NULL;
+ }
+
+ if(!m_szCaption)
+ m_szCaption = new TCHAR[_tcslen(szCaption) + 1];
+
+ _tcscpy(m_szCaption, szCaption);
+ m_psp.pszTitle = m_szCaption;
+
+ // Set by CPropertySheetT::AddPage
+ m_pSheet = NULL;
+
+}
+
+// Callback used during actual creation of page
+template <class TBase>
+UINT CALLBACK CPropertyPageT<TBase>::PropPageCallback(HWND hwnd, UINT uMsg,
+ LPPROPSHEETPAGE ppsp)
+{
+ switch (uMsg)
+ {
+ case PSPCB_CREATE:
+ {
+ // Get ready for hooking up to an ATL WndProc
+ CPropertyPageT<TBase>* pPage =
+ (CPropertyPageT<TBase>*)ppsp->lParam;
+ _Module.AddCreateWndData(&(pPage->m_thunk.cd), pPage);
+
+ return TRUE;
+ }
+ case PSPCB_RELEASE:
+ break;
+ }
+
+ return 0;
+}
+
+// Thin Wrapper
+template <class TBase>
+void CPropertyPageT<TBase>::CancelToClose()
+{
+ ATLASSERT(::IsWindow(m_hWnd));
+ ATLASSERT(GetParent() != NULL);
+
+ ::SendMessage(GetParent(), PSM_CANCELTOCLOSE, NULL, NULL);
+}
+
+// Thin Wrapper
+template <class TBase>
+void CPropertyPageT<TBase>::SetModified(bool bChanged)
+{
+ ATLASSERT(m_hWnd != NULL);
+ ATLASSERT(::IsWindow(m_hWnd));
+ ATLASSERT(GetParent() != NULL);
+
+ if (bChanged)
+ ::SendMessage(GetParent(), PSM_CHANGED, (WPARAM)m_hWnd, NULL);
+ else
+ ::SendMessage(GetParent(), PSM_UNCHANGED, (WPARAM)m_hWnd, NULL);
+}
+
+// Thin Wrapper
+template <class TBase>
+LRESULT CPropertyPageT<TBase>::QuerySiblings(WPARAM wParam, LPARAM lParam)
+{
+ ATLASSERT(::IsWindow(m_hWnd));
+ ATLASSERT(GetParent() != NULL);
+
+ return ::SendMessage(GetParent(), PSM_QUERYSIBLINGS, wParam, lParam);
+}
+
+template <class TBase>
+void CPropertyPageT<TBase>::SetHelp(bool bHelp)
+{
+ if(bHelp)
+ m_psp.dwFlags |= PSP_HASHELP;
+ else
+ m_psp.dwFlags &= ~(PSP_HASHELP);
+}
+
+// Thin wrapper
+template <class TBase>
+bool CPropertyPageT<TBase>::IsButtonEnabled(int iButton)
+{
+ HWND hWnd = ::GetDlgItem(::GetParent(m_hWnd), iButton);
+ if (hWnd == NULL)
+ return false;
+ return ::IsWindowEnabled(hWnd) ? true : false;
+}
+
+// Routes EndDialog calls to the Sheet
+template <class TBase>
+void CPropertyPageT<TBase>::EndDialog(int nID)
+{
+ // Normally you shouldn't call EndDialog from a page. But in case it does
+ // happen during error situations, call CPropertySheetT::EndDialog instead.
+ CPropertySheetT<TBase>* pParent = GetParentSheet();
+ if (pParent != NULL)
+ pParent->EndDialog(nID);
+}
+
+// Return Sheet Pointer (which is initialized in CPropertySheetT::AddPage)
+template <class TBase>
+CPropertySheetT<TBase>* CPropertyPageT<TBase>::GetParentSheet()
+ { ATLASSERT(m_pSheet != NULL); return m_pSheet; }
+
+
+
+
+
+////////////////////////////////////////////////////////////////////////
+// Implements and hooks into Actual Property Sheet Window
+template <class TBase = CWindow>
+class CPropertySheetT : public CWindowImplBaseT< TBase >
+{
+// Construction
+public:
+ CPropertySheetT() : m_szCaption(NULL)
+ { CommonConstruct(_T(""), 0); }
+
+ // Caption from Resource
+ CPropertySheetT(UINT nIDCaption, HWND hwndParent = NULL, UINT iSelectPage = 0)
+ : m_szCaption(NULL)
+ { Construct(nIDCaption, hwndParent, iSelectPage); }
+
+ CPropertySheetT(LPCTSTR pszCaption, HWND hwndParent = NULL, UINT iSelectPage = 0)
+ : m_szCaption(NULL)
+ { Construct(pszCaption, hwndParent, iSelectPage); }
+
+ // Caption from Resource
+ void Construct(UINT nIDCaption, HWND hwndParent = NULL, UINT iSelectPage = 0)
+ {
+ ATLASSERT(nIDCaption != 0);
+ USES_CONVERSION;
+ CComBSTR bsCaption;
+ bsCaption.LoadString(nIDCaption);
+ CommonConstruct(OLE2T(bsCaption), hwndParent, iSelectPage);
+ }
+
+ void Construct(LPCTSTR pszCaption, HWND hwndParent = NULL, UINT iSelectPage = 0)
+ { CommonConstruct(pszCaption, hwndParent, iSelectPage); }
+
+ // All actual initialization takes place here
+ void CommonConstruct(LPCTSTR sCaption, HWND hwndParent = NULL, UINT iSelectPage = 0);
+
+ virtual ~CPropertySheetT()
+ { if(m_szCaption) delete[] m_szCaption; }
+
+// Creation
+public:
+ // for modeless creation
+ // the default style, expressed by passing -1 as dwStyle, is actually:
+ // WS_SYSMENU | WS_POPUP | WS_CAPTION | DS_MODALFRAME | WS_VISIBLE
+ bool Create(HWND hwndParent = NULL, DWORD dwStyle = (DWORD)-1, DWORD dwExStyle = 0);
+ int DoModal(HWND hwndParent = NULL, DWORD dwStyle = (DWORD)-1, DWORD dwExStyle = 0);
+
+// Operations
+public:
+ void AddPage(CPropertyPageT<TBase>* pPage);
+ void RemovePage(CPropertyPageT<TBase>* pPage);
+ void RemovePage(int nPage);
+ void EndDialog(int nEndID); // used to terminate a modal dialog
+ bool PressButton(int nButton);
+
+// Status and Settings
+public:
+ int GetPageCount() const;
+ int GetActiveIndex() const;
+ CPropertyPageT<TBase>* GetActivePage() const;
+ CPropertyPageT<TBase>* GetPage(int nPage) const;
+ int GetPageIndex(CPropertyPageT<TBase>* pPage);
+ HWND GetTabControl() const;
+ void SetWizardMode();
+ void SetHelp(bool bHelp);
+ void SetFinishText(LPCTSTR lpszText);
+ void SetWizardButtons(DWORD dwFlags);
+ void EnableStackedTabs(BOOL bStacked);
+ bool IsWizard() const;
+ bool SetActivePage(int nPage);
+ bool SetActivePage(CPropertyPageT<TBase>* pPage);
+ void SetTitle(LPCTSTR lpszText, UINT nStyle = 0);
+
+// Message Map
+protected:
+typedef CPropertySheetT<TBase> sheetClass;
+BEGIN_MSG_MAP(sheetClass)
+ MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
+ MESSAGE_HANDLER(WM_SYSCOMMAND, OnSysCommand)
+ MESSAGE_HANDLER(WM_CLOSE, OnClose)
+ COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnEndCode)
+ COMMAND_HANDLER(IDOK, BN_CLICKED, OnEndCode)
+ COMMAND_HANDLER(ID_WIZFINISH, BN_CLICKED, OnEndCode)
+END_MSG_MAP()
+
+// Message Handlers
+ LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT OnEndCode(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+
+
+// Implementation
+protected:
+ void BuildPropPageArray(); // Finalizes the Array of Pages
+
+ // Used during creation to set styles
+ static int CALLBACK PropSheetCallback(HWND, UINT nMsg, LPARAM lParam);
+
+ // For hooking into creation and subclassing the window
+ DECLARE_HOOK_AND_SUBCLASS(sheetClass);
+
+ // Style cache (used in PropSheetCallback)
+ DWORD m_dwCurStyle;
+ DWORD m_dwCurExStyle;
+
+// Data
+protected:
+ CSimpleArray< CPropertyPageT<TBase>* > m_pages; // array of CPropertyPageT pointers
+ LPTSTR m_szCaption; // caption of the pseudo-dialog
+ bool m_bStacked; // EnableStackedTabs sets this
+ bool m_bModeless; // TRUE when Create called instead of DoModal
+ int m_nDlgRet; // Result from EndDialog
+
+public:
+ PROPSHEETHEADER m_psh;
+};
+
+// All Initialization here
+template <class TBase>
+void CPropertySheetT<TBase>::CommonConstruct(LPCTSTR szCaption,
+ HWND hwndParent /*= NULL*/,
+ UINT iSelectPage /*= 0*/)
+{
+ // Clear
+ memset(&m_psh, 0, sizeof(m_psh));
+
+ m_psh.dwSize = sizeof(m_psh);
+ m_psh.dwFlags = PSH_PROPSHEETPAGE;
+ m_psh.nStartPage = iSelectPage;
+
+ // Allocate String
+ if(m_szCaption && _tcslen(m_szCaption) < _tcslen(szCaption))
+ {
+ delete[] m_szCaption;
+ m_szCaption = NULL;
+ }
+
+ if(!m_szCaption)
+ m_szCaption = new TCHAR[_tcslen(szCaption) + 1];
+
+ _tcscpy(m_szCaption, szCaption);
+ m_psh.pszCaption = m_szCaption;
+
+ m_bStacked = true; // Tab Control
+ m_bModeless = false;
+ m_dwCurStyle = 0;
+ m_dwCurExStyle = 0;
+ m_nDlgRet = 0;
+
+ // m_psh.hwndParent set in DoModal/create
+}
+
+// Callback used to set styles of Property Sheet
+template <class TBase>
+int CALLBACK CPropertySheetT<TBase>::PropSheetCallback(HWND hwnd, UINT nMsg,
+ LPARAM lParam)
+{
+ switch (nMsg)
+ {
+ case PSCB_PRECREATE:
+ {
+ // Extract Thunk Create (whatever) data
+ CPropertySheetT<TBase>* pThis = (CPropertySheetT<TBase>*)_Module.ExtractCreateWndData();
+ // We need it later in the proces so add it back again
+ _Module.AddCreateWndData(&(pThis->m_thunk.cd), pThis);
+
+ LPDLGTEMPLATE lpTemplate = (LPDLGTEMPLATE)lParam;
+ if (lpTemplate->style != pThis->m_dwCurStyle ||
+ lpTemplate->dwExtendedStyle != pThis->m_dwCurExStyle)
+ {
+
+ // Mark the dialog template as read-write.
+ DWORD dwOldProtect;
+ VirtualProtect(lpTemplate, sizeof(DLGTEMPLATE), PAGE_READWRITE,
+ &dwOldProtect);
+
+ // Ensure DS_SETFONT is set correctly.
+ lpTemplate->style = lpTemplate->style & DS_SETFONT ?
+ pThis->m_dwCurStyle | DS_SETFONT :
+ pThis->m_dwCurStyle & ~DS_SETFONT;
+
+ lpTemplate->dwExtendedStyle = pThis->m_dwCurExStyle;
+ }
+ return TRUE;
+ }
+ case PSCB_INITIALIZED:
+ return TRUE;
+
+ }
+
+ return 0;
+}
+
+// Creates a Modeless Property Sheet
+template <class TBase>
+bool CPropertySheetT<TBase>::Create(HWND hwndParent /*= NULL*/, DWORD dwStyle /*= (DWORD)-1*/,
+ DWORD dwExStyle /*= 0*/)
+{
+ // Calculate the default window style.
+ if (dwStyle == (DWORD)-1)
+ {
+ dwStyle = DS_MODALFRAME | DS_3DLOOK | DS_SETFONT | WS_POPUP |
+ WS_VISIBLE | WS_CAPTION;
+
+ // Wizards don't have WS_SYSMENU by default
+ if (!IsWizard())
+ dwStyle |= WS_SYSMENU;
+ }
+
+ // Cache the styles (for use in PropSheetCallback)
+ m_dwCurStyle = dwStyle;
+ m_dwCurExStyle = dwExStyle;
+
+ ATLASSERT(m_hWnd == NULL);
+
+ // finish building PROPSHEETHEADER structure
+ BuildPropPageArray();
+
+ // Some more settings
+ m_bModeless = true;
+ m_psh.dwFlags |= (PSH_MODELESS | PSH_USECALLBACK);
+ m_psh.pfnCallback = PropSheetCallback;
+ m_psh.hwndParent = hwndParent;
+
+ // Add Create Thunk whatever Data
+ _Module.AddCreateWndData(&m_thunk.cd, this);
+ // Hook into creation so we can subclass before any messages come in
+ HOOK_AND_SUBCLASS_NEXT();
+
+ // Do it!
+ HWND hWnd = (HWND)::PropertySheet((PROPSHEETHEADER*)&m_psh);
+
+ // Modeless so we return right away
+#ifdef _DEBUG
+ DWORD dwError = ::GetLastError();
+#endif
+
+ if (hWnd == NULL || hWnd == (HWND)-1)
+ {
+#ifdef _DEBUG
+ ATLTRACE("PropertySheet() failed: GetLastError returned %d\n", dwError);
+#endif
+ return false;
+ }
+
+ m_hWnd = hWnd;
+
+ return true;
+}
+
+// Create a Modal Dialog
+template <class TBase>
+int CPropertySheetT<TBase>::DoModal(HWND hwndParent /*= NULL*/, DWORD dwStyle /*= (DWORD)-1*/,
+ DWORD dwExStyle /*= 0*/)
+{
+ ATLASSERT(m_hWnd == NULL);
+
+ // Calculate the default window style.
+ if (dwStyle == (DWORD)-1)
+ {
+ dwStyle = DS_MODALFRAME | DS_3DLOOK | DS_SETFONT | WS_POPUP |
+ WS_VISIBLE | WS_CAPTION;
+
+ // Wizards don't have WS_SYSMENU by default
+ if (!IsWizard())
+ dwStyle |= WS_SYSMENU;
+ }
+
+ // Cache the styles (for use in PropSheetCallback)
+ m_dwCurStyle = dwStyle;
+ m_dwCurExStyle = dwExStyle;
+
+ // finish building PROPSHEETHEADER structure
+ BuildPropPageArray();
+
+ // Set options
+ m_bModeless = false;
+ m_nDlgRet = -1;
+ m_psh.dwFlags |= (PSH_USECALLBACK);
+ m_psh.pfnCallback = PropSheetCallback;
+ m_psh.hwndParent = hwndParent;
+
+ // Add Create Thunk whatever Data
+ _Module.AddCreateWndData(&m_thunk.cd, this);
+ // Hook into creation so we can subclass before any messages come in
+ HOOK_AND_SUBCLASS_NEXT();
+
+ // Do it. Doesn't come back until after the fact
+ int nRet = ::PropertySheet((PROPSHEETHEADER*)&m_psh);
+
+ if(nRet == -1)
+ return nRet;
+
+ return m_nDlgRet;
+}
+
+// Finalize the Array
+template <class TBase>
+void CPropertySheetT<TBase>::BuildPropPageArray()
+{
+ // delete existing prop page array
+ delete[] (PROPSHEETPAGE*)m_psh.ppsp;
+ m_psh.ppsp = NULL;
+
+ // build new prop page array
+ PROPSHEETPAGE* ppsp = new PROPSHEETPAGE[m_pages.GetSize()];
+ m_psh.ppsp = (PROPSHEETPAGE*)ppsp;
+ BOOL bWizard = (m_psh.dwFlags & (PSH_WIZARD));
+ for (int i = 0; i < m_pages.GetSize(); i++)
+ {
+ CPropertyPageT<TBase>* pPage = GetPage(i);
+ memcpy(&ppsp[i], &pPage->m_psp, sizeof(pPage->m_psp));
+// pPage->PreProcessPageTemplate((PROPSHEETPAGE&)ppsp[i], bWizard);
+ }
+ m_psh.nPages = m_pages.GetSize();
+}
+
+// Adds a Page (either before or after we're on the screen)
+template <class TBase>
+void CPropertySheetT<TBase>::AddPage(CPropertyPageT<TBase>* pPage)
+{
+ ATLASSERT(pPage != NULL);
+
+ // Add page to internal list
+ m_pages.Add(pPage);
+ pPage->m_pSheet = this;
+
+ // Add page externally
+ if (m_hWnd != NULL)
+ {
+ // build new prop page array
+ PROPSHEETPAGE* ppsp = new PROPSHEETPAGE[m_pages.GetSize()];
+ memcpy(ppsp, m_psh.ppsp, sizeof(PROPSHEETPAGE) * (m_pages.GetSize() - 1));
+ delete[] (PROPSHEETPAGE*)m_psh.ppsp;
+ m_psh.ppsp = ppsp;
+ ppsp += m_pages.GetSize() - 1;
+
+ // copy processed PROPSHEETPAGE struct to end
+ memcpy(ppsp, &(pPage->m_psp), sizeof(pPage->m_psp));
+// pPage->PreProcessPageTemplate((PROPSHEETPAGE&)*ppsp, IsWizard());
+ HPROPSHEETPAGE hPSP = CreatePropertySheetPage((PROPSHEETPAGE*)ppsp);
+ ATLASSERT(hPSP != NULL);
+
+ // Tray and add it
+ if (!SendMessage(PSM_ADDPAGE, 0, (LPARAM)hPSP))
+ {
+ DestroyPropertySheetPage(hPSP);
+ ATLASSERT(false);
+ }
+ }
+}
+
+// Removes a Page (either before or after we're on the screen)
+template <class TBase>
+void CPropertySheetT<TBase>::RemovePage(CPropertyPageT<TBase>* pPage)
+{
+ ATLASSERT(pPage != NULL);
+
+ // Get Index
+ int nPage = GetPageIndex(pPage);
+ pPage->m_pSheet = NULL;
+ ATLASSERT(nPage >= 0);
+
+ RemovePage(nPage);
+}
+
+// Removes a Page (either before or after we're on the screen)
+template <class TBase>
+void CPropertySheetT<TBase>::RemovePage(int nPage)
+{
+ // remove the page externally
+ if (m_hWnd != NULL)
+ SendMessage(PSM_REMOVEPAGE, nPage);
+
+ // remove the page from internal list
+ m_pages.RemoveAt(nPage);
+}
+
+// Closes the Property Sheet
+template <class TBase>
+void CPropertySheetT<TBase>::EndDialog(int nEndID)
+{
+ m_nDlgRet = nEndID;
+
+ if (m_bModeless)
+ DestroyWindow();
+ else
+ PostMessage(PSM_PRESSBUTTON, PSBTN_CANCEL);
+}
+
+// Need to handle WM_CLOSE for Modeless Property Sheets
+template <class TBase>
+LRESULT CPropertySheetT<TBase>::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ if (m_bModeless)
+ DestroyWindow();
+ else
+ bHandled = false;
+
+ return 0;
+}
+
+// Need to handle SC_CLOSE for Modeless Property Sheets
+template <class TBase>
+LRESULT CPropertySheetT<TBase>::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ switch (wParam & 0xFFF0)
+ {
+ case SC_CLOSE:
+ if (m_bModeless)
+ {
+ SendMessage(WM_CLOSE);
+ return 0;
+ }
+ }
+ bHandled = false;
+ return 0;
+}
+
+// Returns number of Pages
+template <class TBase>
+int CPropertySheetT<TBase>::GetPageCount() const
+{
+ if (m_hWnd == NULL)
+ return m_pages.GetSize();
+
+ return TabCtrl_GetItemCount(GetTabControl());
+}
+
+// Returns Current Page
+template <class TBase>
+int CPropertySheetT<TBase>::GetActiveIndex() const
+{
+ if (m_hWnd == NULL)
+ return m_psh.nStartPage;
+
+ return TabCtrl_GetCurSel(GetTabControl());
+}
+
+// Moves to Page
+template <class TBase>
+bool CPropertySheetT<TBase>::SetActivePage(int nPage)
+{
+ if (m_hWnd == NULL)
+ {
+ m_psh.nStartPage = nPage;
+ return true;
+ }
+ return (bool)SendMessage(PSM_SETCURSEL, nPage);
+}
+
+// Moves to Page
+template <class TBase>
+bool CPropertySheetT<TBase>::SetActivePage(CPropertyPageT<TBase>* pPage)
+{
+ ATLASSERT(pPage != NULL);
+
+ int nPage = GetPageIndex(pPage);
+ ATLASSERT(pPage >= 0);
+
+ return SetActivePage(nPage);
+}
+
+// Gets number of Page
+template <class TBase>
+int CPropertySheetT<TBase>::GetPageIndex(CPropertyPageT<TBase>* pPage)
+{
+ ATLASSERT(pPage != NULL);
+ for (int i = 0; i < GetPageCount(); i++)
+ {
+ if (GetPage(i) == pPage)
+ return i;
+ }
+ return -1; // pPage not found
+}
+
+// Get a Page from Array
+template <class TBase>
+CPropertyPageT<TBase>* CPropertySheetT<TBase>::GetPage(int nPage) const
+ { return m_pages[nPage]; }
+
+// Initdialog: Complete creation and setup for modeless etc...
+template <class TBase>
+LRESULT CPropertySheetT<TBase>::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ // Change tab style if scrolling tabs desired (stacked tabs are default)
+ if (!m_bStacked)
+ ((CWindow)GetTabControl()).ModifyStyle(TCS_MULTILINE, TCS_SINGLELINE, 0);
+
+ // For Property Sheets
+ if (!IsWizard())
+ {
+ // Resize the tab control so the layout is less restrictive
+ HWND hWndTab = ::GetDlgItem(m_hWnd, AFX_IDC_TAB_CONTROL);
+ ATLASSERT(hWndTab != NULL);
+ RECT rcOld;
+ ::GetWindowRect(hWndTab, &rcOld);
+ ScreenToClient(&rcOld);
+ RECT rcNew = {0, 0, 0, 32};
+ ::MapDialogRect(m_hWnd, &rcNew);
+ if (rcNew.bottom < rcNew.bottom)
+ {
+ // Move tab control
+ int cyDiff = (rcOld.bottom - rcOld.top) - rcNew.bottom;
+ ::SetWindowPos(hWndTab, NULL, 0, 0, rcOld.right - rcOld.left, rcNew.bottom,
+ SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
+
+ // Move buttons by similar amount
+ for (int i = 0; i < _countof(_PropSheetButtons); i++)
+ {
+ HWND hWndBtn = ::GetDlgItem(m_hWnd, _PropSheetButtons[i]);
+ if (hWndBtn != NULL)
+ {
+ ::GetWindowRect(hWndBtn, &rcOld);
+ ScreenToClient(&rcOld);
+ ::SetWindowPos(hWndBtn, NULL,
+ rcOld.left, rcOld.top - cyDiff,
+ 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
+ }
+ }
+
+ // Resize property sheet itself similarly
+ GetWindowRect(&rcOld);
+ SetWindowPos(NULL, 0, 0, rcOld.right - rcOld.left, (rcOld.bottom - rcOld.top) - cyDiff,
+ SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
+ }
+ }
+
+ // For Modeless Sheets
+ if (m_bModeless && !IsWizard())
+ {
+ // Layout property sheet so button area is not accounted for
+ RECT rcWnd;
+ GetWindowRect(&rcWnd);
+ RECT rcBtn;
+ HWND hWndBtn = ::GetDlgItem(m_hWnd, IDOK);
+ ATLASSERT(hWndBtn != NULL);
+ ::GetWindowRect(hWndBtn, &rcBtn);
+ SetWindowPos(NULL, 0, 0,
+ rcWnd.right - rcWnd.left,
+ (rcBtn.top - rcWnd.top) + ::GetSystemMetrics(SM_CYBORDER),
+ SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
+
+ // Remove standard buttons for modeless dialogs
+ for (int i = 0; i < _countof(_PropSheetButtons); i++)
+ {
+ hWndBtn = ::GetDlgItem(m_hWnd, _PropSheetButtons[i]);
+ if (hWndBtn != NULL)
+ {
+ ::ShowWindow(hWndBtn, SW_HIDE);
+ ::EnableWindow(hWndBtn, FALSE);
+ }
+ }
+ }
+
+ // Center the property sheet relative to the parent window
+ if (!(GetStyle() & WS_CHILD))
+ CenterWindow();
+
+ // API implementation probably needs this message
+ bHandled = FALSE;
+ return 1;
+}
+
+// Gets the return code for DoModal
+template <class TBase>
+LRESULT CPropertySheetT<TBase>::OnEndCode(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ // API implementation probably needs this message
+ bHandled = FALSE;
+ m_nDlgRet = wID;
+ return 0;
+}
+
+
+// Are we a wizard?
+template <class TBase>
+bool CPropertySheetT<TBase>::IsWizard() const
+ { return ((m_psh.dwFlags & PSH_WIZARD) != 0); }
+
+// Return HWND of Tab Control
+template <class TBase>
+HWND CPropertySheetT<TBase>::GetTabControl() const
+ { ATLASSERT(::IsWindow(m_hWnd)); return (HWND)::SendMessage(m_hWnd, PSM_GETTABCONTROL, 0, 0); }
+
+// Press Button on Property Sheet
+template <class TBase>
+bool CPropertySheetT<TBase>::PressButton(int nButton)
+ { ATLASSERT(::IsWindow(m_hWnd)); return ::SendMessage(m_hWnd, PSM_PRESSBUTTON, nButton, 0) ? true : false; }
+
+// Get the Active Page
+template <class TBase>
+CPropertyPageT<TBase>* CPropertySheetT<TBase>::GetActivePage() const
+ { return GetPage(GetActiveIndex()); }
+
+// Change the Title
+template <class TBase>
+void CPropertySheetT<TBase>::SetTitle(LPCTSTR lpszText, UINT nStyle)
+{
+ ATLASSERT((nStyle & ~PSH_PROPTITLE) == 0); // only PSH_PROPTITLE is valid
+
+ // Set Internal State
+ if(m_szCaption && _tcslen(m_szCaption) < _tcslen(lpszText))
+ {
+ delete[] m_szCaption;
+ m_szCaption = NULL;
+ }
+
+ if(!m_szCaption)
+ m_szCaption = new TCHAR[_tcslen(lpszText) + 1];
+
+ _tcscpy(m_szCaption, lpszText);
+ m_psh.pszCaption = m_szCaption;
+
+ // Set the options
+ m_psh.dwFlags &= ~PSH_PROPTITLE;
+ m_psh.dwFlags |= nStyle;
+
+ if (m_hWnd != NULL)
+ {
+ // set external state
+ SendMessage(PSM_SETTITLE, nStyle, (LPARAM)lpszText);
+ }
+}
+
+// Make us a Wizard
+template <class TBase>
+void CPropertySheetT<TBase>::SetWizardMode()
+ { m_psh.dwFlags |= PSH_WIZARD; }
+
+template <class TBase>
+void CPropertySheetT<TBase>::SetHelp(bool bHelp)
+{
+ if(bHelp)
+ m_psh.dwFlags |= PSH_HASHELP;
+ else
+ m_psh.dwFlags &= ~(PSH_HASHELP);
+}
+
+// Set Text of Finish Button
+template <class TBase>
+void CPropertySheetT<TBase>::SetFinishText(LPCTSTR lpszText)
+ { ATLASSERT(::IsWindow(m_hWnd)); ::SendMessage(m_hWnd, PSM_SETFINISHTEXT, 0, (LPARAM)lpszText); }
+
+// Choose which buttons to have in Wizard
+template <class TBase>
+void CPropertySheetT<TBase>::SetWizardButtons(DWORD dwFlags)
+ { ATLASSERT(::IsWindow(m_hWnd)); ::PostMessage(m_hWnd, PSM_SETWIZBUTTONS, 0, dwFlags); }
+
+// Stacked Tabs
+template <class TBase>
+void CPropertySheetT<TBase>::EnableStackedTabs(BOOL bStacked)
+ { m_bStacked = bStacked; }
+
+typedef CPropertySheetT<CWindow> CPropertySheet;
+typedef CPropertyPageT<CWindow> CPropertyPage;
+
+#endif // !defined(AFX_PROPERTYPAGE_H__1508DAF4_7AD1_11D3_BF96_0020182B97FC__INCLUDED_)
diff --git a/win32/common/atlwinhk.h b/win32/common/atlwinhk.h
new file mode 100644
index 0000000..4dddf82
--- /dev/null
+++ b/win32/common/atlwinhk.h
@@ -0,0 +1,151 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * VERSION
+ * 2.2.0b
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __ATLWINHK_H__
+#define __ATLWINHK_H__
+
+/////////////////////////////////////////////////////////////////////////
+// Hooks an CWindowImpl to windows that the API or other code creates
+// By Stef: 06/10/99
+
+
+// Use this macro in the class definition and pass class name
+// as first param
+#define DECLARE_HOOK_AND_SUBCLASS(Class) \
+ HHOOK m_hHookCur; \
+ static LRESULT CALLBACK HookCreateProc(int nCode, \
+ WPARAM wParam, \
+ LPARAM lParam) \
+ { \
+ Class* pThis = (Class*) _Module.ExtractCreateWndData(); \
+ \
+ if (nCode != HCBT_CREATEWND) \
+ { \
+ /*Add it back again*/ \
+ _Module.AddCreateWndData(&(pThis->m_thunk.cd), pThis); \
+ /*wait for HCBT_CREATEWND just pass others on...*/ \
+ return CallNextHookEx(pThis->m_hHookCur, nCode, wParam, lParam); \
+ } \
+ \
+ pThis->SubclassWindow((HWND)wParam); \
+ \
+ UnhookWindowsHookEx(pThis->m_hHookCur); \
+ HHOOK hHookCur = pThis->m_hHookCur; \
+ pThis->m_hHookCur = NULL; \
+ \
+ return CallNextHookEx(hHookCur, nCode, wParam, lParam); \
+ }
+
+// Use this *immediately* before the code that creates the window
+// It will subclass the first window that's created by that thread
+// so add immediately before.
+#define HOOK_AND_SUBCLASS_NEXT() \
+ ((m_hHookCur = SetWindowsHookEx(WH_CBT, HookCreateProc, NULL, ::GetCurrentThreadId())) == NULL)
+
+const LPCTSTR kDialogClass = (LPCTSTR)0x00008002;
+
+template<class _Class>
+class CHookWindow
+{
+public:
+ CHookWindow()
+ {
+ m_hHookCur = NULL;
+ m_szHookClass = NULL;
+ m_szHookName = NULL;
+ }
+
+ bool SubclassNext(LPCTSTR szClass = NULL, LPCTSTR szName = NULL)
+ {
+ _Class* pThis = (_Class*)this;
+ _Module.AddCreateWndData(&(pThis->m_thunk.cd), pThis);
+
+ m_hHookCur = SetWindowsHookEx(WH_CBT, HookCreateProc, NULL, ::GetCurrentThreadId());
+
+ if(m_hHookCur)
+ {
+ m_szHookClass = szClass;
+ m_szHookName = szName;
+ }
+
+ return m_hHookCur == NULL;
+ }
+
+ static LRESULT CALLBACK HookCreateProc(int nCode, WPARAM wParam, LPARAM lParam)
+ {
+ _Class* pThis = (_Class*) _Module.ExtractCreateWndData();
+ HHOOK hHookCur = pThis->m_hHookCur;
+
+ bool bSkip = false;
+
+ if(nCode != HCBT_CREATEWND)
+ {
+ bSkip = true;
+ }
+ else
+ {
+ CBT_CREATEWND* pCreate = (CBT_CREATEWND*)lParam;
+
+ if(pThis->m_szHookClass)
+ {
+ // If it's not a pointer (for at least two bytes)
+ if(IsBadReadPtr(pCreate->lpcs->lpszClass, 2) || IsBadReadPtr(pThis->m_szHookClass, 2))
+ // Then just compare values
+ bSkip = pCreate->lpcs->lpszClass != pThis->m_szHookClass;
+
+ else
+ // Otherwise compare strings
+ bSkip = _tcsicmp(pCreate->lpcs->lpszClass, pThis->m_szHookClass) != 0;
+ }
+
+ if(pThis->m_szHookName)
+ {
+ if(IsBadReadPtr(pCreate->lpcs->lpszName, _tcslen(pThis->m_szHookName)) ||
+ _tcsicmp(pCreate->lpcs->lpszName, pThis->m_szHookName))
+ bSkip = true;
+ }
+ }
+
+ if(bSkip)
+ {
+ /*Add it back again*/
+ _Module.AddCreateWndData(&(pThis->m_thunk.cd), pThis);
+ }
+ else
+ {
+ pThis->SubclassWindow((HWND)wParam);
+
+ UnhookWindowsHookEx(pThis->m_hHookCur);
+ pThis->m_hHookCur = NULL;
+ }
+
+ return CallNextHookEx(hHookCur, nCode, wParam, lParam);
+ }
+
+protected:
+ HHOOK m_hHookCur;
+ LPCTSTR m_szHookName;
+ LPCTSTR m_szHookClass;
+};
+
+
+#endif //__ATLWINHK_H__
diff --git a/win32/common/droplet.cpp b/win32/common/droplet.cpp
new file mode 100644
index 0000000..e796f49
--- /dev/null
+++ b/win32/common/droplet.cpp
@@ -0,0 +1,226 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include <stdafx.h>
+#include "lib/rep.h"
+#include "common/binfile.h"
+#include "common/repfile.h"
+#include "Droplet.h"
+
+// (Con|De)struction: --------------------------------------------
+
+Droplet::Droplet()
+{
+ m_keepBackups = false;
+ memset(&m_ctx, 0, sizeof(m_ctx));
+}
+
+Droplet::~Droplet()
+{
+ repFree(&m_ctx);
+}
+
+
+// All error handling jumps to 'cleanup' where wrapping up loose
+// ends is done
+#define RETURN(v) { ret = v; goto cleanup; }
+
+
+// save: ---------------------------------------------------
+// Saves a droplet to a file. The file must already exist
+// and must be an executable
+
+bool Droplet::save(LPCTSTR fileName)
+{
+ HANDLE update = NULL;
+ void* data = NULL;
+ BFILE h = NULL;
+ bool ret = true;
+
+ {
+ if(!m_ctx.script.ops)
+ RETURN(false);
+
+ // Make the rep binfile
+ bfval val;
+ BFILE h = bfStartMem(NULL, 0, BF_REALLOC);
+ if(!h) RETURN(false);
+
+ // Write out the headers
+ repfWriteHeader(h);
+
+ // Write to it now
+ if(m_ctx.block != 0)
+ bfWriteInt(h, REPVAL_BUFSIZE, m_ctx.block);
+
+ bfWriteInt(h, DROPVAL_BACKUPS, m_keepBackups ? 1 : 0);
+
+ // Write the script out
+ val.id = REPVAL_SCRIPT;
+ val.len = m_ctx.script.len;
+ val.type = BINTYPE_DATA;
+ bfWriteValue(h, &val, m_ctx.script.ops);
+
+ if(!m_title.empty())
+ bfWriteString(h, DROPVAL_TITLE, m_title.c_str());
+
+ // Done
+ bfWriteEnd(h);
+ if(bfError(h)) RETURN(false);
+
+ size_t len = bfCount(h);
+ data = bfInternal(h);
+ bfClose(h);
+ h = NULL;
+
+ // Now actually modify the EXE
+ update = BeginUpdateResource(fileName, FALSE);
+ if(!update) RETURN(false);
+
+ if(!UpdateResource(update, DROP_RESOURCE_TYPE, DROP_RESOURCE_ID,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), data, len)
+ || !EndUpdateResource(update, FALSE))
+ RETURN(false);
+
+ update = NULL;
+ }
+
+cleanup:
+ while(data)
+ {
+ free(data);
+
+ if(h == NULL)
+ break;
+
+ data = bfInternal(h);
+ bfClose(h);
+ h = NULL;
+ }
+
+ if(update)
+ EndUpdateResource(update, TRUE);
+
+ return true;
+}
+
+
+// load: ---------------------------------------------------------------
+// Load a droplet from a file
+
+bool Droplet::load(LPCTSTR fileName)
+{
+ HMODULE module = LoadLibrary(fileName);
+ if(!module)
+ return false;
+
+ bool ret = load(GetModuleHandle(fileName));
+
+ FreeLibrary(module);
+
+ return ret;
+}
+
+
+// load: ---------------------------------------------------------------
+// Load a droplet from a loaded module
+
+bool Droplet::load(HMODULE module)
+{
+ bool ret = true;
+ BFILE h = NULL;
+
+ {
+ // In case we're called twice
+ repFree(&m_ctx);
+
+ // Load the resource
+ HRSRC hRsrc = FindResourceEx(module, DROP_RESOURCE_TYPE, DROP_RESOURCE_ID,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
+ if(!hRsrc) RETURN(false);
+
+ HGLOBAL hGlobal = LoadResource(module, hRsrc);
+ if(!hGlobal) RETURN(false);
+
+ void* data = LockResource(hGlobal);
+ if(!data) RETURN(false);
+
+ ::SetLastError(0);
+
+ // And now read it
+ h = bfStartMem(data, SizeofResource(module, hRsrc), 0);
+ if(!h) RETURN(false);
+
+ // Check the format
+ if(!repfReadHeader(h))
+ RETURN(false);
+
+ bfval val;
+
+ while(bfReadValueInfo(h, &val))
+ {
+ switch(val.id)
+ {
+ case REPVAL_BUFSIZE:
+ bfReadValueData(h, &val, &(m_ctx.block));
+ continue;
+
+ case DROPVAL_BACKUPS:
+ {
+ long value;
+ bfReadValueData(h, &val, &value);
+ m_keepBackups = value == 0 ? false : true;
+ }
+ continue;
+
+ case REPVAL_SCRIPT:
+ {
+ // Script must have some length
+ if(val.len == 0)
+ RETURN(false);
+
+ m_ctx.script.ops = (byte*)malloc(val.len);
+ if(!m_ctx.script.ops)
+ RETURN(false);
+
+ bfReadValueData(h, &val, m_ctx.script.ops);
+ m_ctx.script.len = val.len;
+ }
+ continue;
+
+ case DROPVAL_TITLE:
+ {
+ bfReadValueData(h, &val, m_title.get_buffer(val.len));
+ m_title.release_buffer();
+ }
+ continue;
+
+ }
+
+ bfSkipValueData(h, &val);
+ }
+ }
+
+cleanup:
+ if(h)
+ bfClose(h);
+
+ return ret;
+}
+
diff --git a/win32/common/droplet.h b/win32/common/droplet.h
new file mode 100644
index 0000000..e98725e
--- /dev/null
+++ b/win32/common/droplet.h
@@ -0,0 +1,98 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+
+#ifndef __DROPLET_H__
+#define __DROPLET_H__
+
+
+// The resources in the droplet
+#define DROP_RESOURCE_TYPE _T("REP")
+#define DROP_RESOURCE_ID _T("SCRIPT")
+#define DROP_RESOURCE_FILE _T("DROPLET")
+
+// Some extra binfile tags
+#define DROPVAL_TITLE 0x0101
+#define DROPVAL_BACKUPS 0x0102
+
+#include "lib/rlib.h"
+#include "lib/rep.h"
+
+
+// Droplet: ---------------------------------------------------------------
+// A droplet provides the saving/loading/loaded functionality of the
+// droplet. Replaces are not done here
+
+class Droplet
+{
+public:
+ Droplet();
+ ~Droplet();
+
+ // Load a droplet from a file
+ bool load(LPCTSTR fileName);
+
+ // Load a droplet from a loaded executable
+ bool load(HMODULE module);
+
+ // Save a droplet out to an existing executable
+ bool save(LPCTSTR fileName);
+
+
+ // Get the internal rlib stream
+ r_context& getContext()
+ { return m_ctx; }
+
+ // Is there a valid script loaded
+ bool hasScript()
+ { return m_ctx.script.ops != NULL; }
+
+ // Get dialog title stored in droplet
+ string getTitle()
+ { return m_title; }
+
+ // Set dialog title stored in droplet
+ void setTitle(astring& title)
+ { m_title = title; }
+
+ // Get the buffer size stored in droplet
+ // 0 = process entire file
+ size_t getBuffSize()
+ { return m_ctx.block; }
+
+ // Set the buffer size to be stored in droplet
+ void setBuffSize(size_t buffSize)
+ { m_ctx.block = buffSize; }
+
+ // Should backups be kept?
+ bool keepBackups()
+ { return m_keepBackups; }
+
+ // Set backup options for droplet
+ void setBackups(bool backups)
+ { m_keepBackups = backups; }
+
+protected:
+ r_context m_ctx;
+
+ astring m_title; // The dialog title
+ bool m_keepBackups; // Should backups be kept?
+};
+
+#endif //__DROPLET_H__ \ No newline at end of file
diff --git a/win32/common/errutil.cpp b/win32/common/errutil.cpp
new file mode 100644
index 0000000..302b4fa
--- /dev/null
+++ b/win32/common/errutil.cpp
@@ -0,0 +1,106 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include "stdafx.h"
+#include "rliberr.h"
+#include "errutil.h"
+
+// rlibError: -------------------------------------------------------------
+// Displays errors from rlib. Assumes we have the appropriate
+// error codes compiled in as resources
+
+HRESULT rlibError(HWND parent, int code, r_script* script)
+{
+ ASSERT(code < 0);
+ USES_CONVERSION;
+
+ LPVOID lpMsgBuf;
+ HRESULT hr = HRESULT_FROM_RLIB(code);
+ string message;
+
+ DWORD dwRet = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
+ FORMAT_MESSAGE_MAX_WIDTH_MASK, GetModuleHandle(NULL), hr,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ (LPTSTR)&lpMsgBuf, 0, NULL);
+ if(dwRet && lpMsgBuf)
+ {
+ message.append((LPTSTR)lpMsgBuf);
+
+ // Free the buffer.
+ ::LocalFree(lpMsgBuf);
+
+ // If there was a specific error message then tack that on
+ if(script->error)
+ {
+ message.append(_T("\n\n"));
+ message.append(A2T(script->error));
+ }
+
+ // If there's an error line then tack that on
+ if(script->errline != 0)
+ {
+ string line;
+ line.format(_T(" (near line %d)"), script->errline);
+ message.append(line);
+ }
+
+ MessageBox(parent, message.c_str(), _T("Rep Droplet"), MB_OK | MB_ICONSTOP);
+ }
+
+ return hr;
+}
+
+
+// errorMessage: -----------------------------------------------------------
+// Displays standard windows errors.
+
+HRESULT errorMessage(HWND parent, HRESULT hr, LPCTSTR format, ...)
+{
+ string message;
+
+ va_list ap;
+ va_start(ap, format);
+
+ message.format_v(format, ap);
+
+ va_end(ap);
+
+ if(FAILED(hr))
+ {
+ LPVOID lpMsgBuf;
+
+ DWORD dwRet = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, hr,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ (LPTSTR) &lpMsgBuf, 0, NULL);
+ if(dwRet && lpMsgBuf)
+ {
+ message.append(_T("\n\n"));
+ message.append((LPTSTR)lpMsgBuf);
+ message.resize(message.size() - 2); // Take off new line
+
+ // Free the buffer.
+ ::LocalFree(lpMsgBuf);
+ }
+ }
+
+ MessageBox(parent, message.c_str(), _T("Rep Droplet"), MB_OK | MB_ICONSTOP);
+
+ return hr == S_OK ? E_FAIL : hr;
+}
diff --git a/win32/common/errutil.h b/win32/common/errutil.h
new file mode 100644
index 0000000..0804149
--- /dev/null
+++ b/win32/common/errutil.h
@@ -0,0 +1,32 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __ERRUTIL_H__
+#define __ERRUTIL_H__
+
+#include "lib/rlib.h"
+
+// Display a standard windows error message
+HRESULT errorMessage(HWND parent, HRESULT hr, LPCTSTR format, ...);
+
+// Display an rlib error message
+HRESULT rlibError(HWND parent, int code, r_script* script);
+
+
+#endif //__ERRUTIL_H__ \ No newline at end of file
diff --git a/win32/common/mystring.h b/win32/common/mystring.h
new file mode 100644
index 0000000..470ac3c
--- /dev/null
+++ b/win32/common/mystring.h
@@ -0,0 +1,536 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#if !defined(_MYSTRING_H_)
+#define _MYSTRING_H_
+
+//////////////////////////////////////////////////////////////////////////////
+
+#include <tchar.h>
+
+#include <stdarg.h>
+
+#include <string>
+#include <locale>
+
+#pragma warning(push)
+#pragma warning(disable:4786) // Disable warning for names > 256
+
+//////////////////////////////////////////////////////////////////////////////
+
+template<typename T>
+class stringT : public std::basic_string<T> // T should be either char or wchar_t
+{
+public:
+ // Constructors
+ stringT()
+ : std::basic_string<T>()
+ { }
+
+ stringT(const stringT<T>& strInput)
+ : std::basic_string<T>(strInput)
+ { }
+
+
+ stringT(const std::basic_string<T>& strInput)
+ : std::basic_string<T>(strInput)
+ { }
+
+ stringT(const std::basic_string<T>& strInput, size_type pos, size_type n)
+ : std::basic_string<T>(strInput, pos, n)
+ { }
+
+ stringT(const std::basic_string<T>::const_iterator first,
+ const std::basic_string<T>::const_iterator last)
+ : std::basic_string<T>(first, last)
+ { }
+
+
+ stringT(const T* strInput)
+ : std::basic_string<T>(strInput)
+ { }
+
+
+ stringT(const T* strInput, size_type n)
+ : std::basic_string<T>(strInput, n)
+ { }
+
+
+#ifdef _INC_COMDEF
+ stringT(_bstr_t bstr)
+ {
+ (*this) = (const T*)bstr;
+ }
+#endif
+
+ stringT(T ch, int nRepeat = 1)
+ : std::basic_string<T>(nRepeat, ch)
+ { }
+
+
+ // Other Conversions
+ stringT<T>& make_upper()
+ {
+ std::ctype<T> c;
+ T* p = get_buffer();
+ c.toupper(p, p + size());
+ release_buffer();
+ return *this;
+ }
+
+ stringT<T> upper() const
+ {
+ stringT<T> sTmp(*this);
+ sTmp.make_upper();
+ return sTmp;
+ }
+
+ stringT<T>& make_lower()
+ {
+ std::ctype<T> c;
+ T* p = get_buffer();
+ c.tolower(p, p + size());
+ release_buffer();
+ return *this;
+ }
+
+ stringT<T> lower() const
+ {
+ stringT<T> sTmp(*this);
+ sTmp.make_lower();
+ return sTmp;
+ }
+
+ void trim_left()
+ {
+ // TODO: Optimize
+ while(!empty() && _istspace(at(0)))
+ erase(0, 1);
+ }
+
+ void trim_right()
+ {
+ // TODO: Optimize
+ while(!empty() && _istspace(at(length()-1)))
+ erase(length() - 1, 1);
+ }
+
+ void trim()
+ {
+ trim_left();
+ trim_right();
+ }
+
+ const stringT<T>& format(const T* pszFormat, ...)
+ {
+ va_list vl;
+ va_start(vl, pszFormat);
+ format_v(pszFormat, vl);
+ va_end(vl);
+
+ return *this;
+ }
+
+#ifdef _WINDOWS_
+
+ const stringT<T>& format(HINSTANCE hInst, UINT nID, ... )
+ {
+ stringT<T> sTemp;
+ sTemp.load_string(hInst, nID);
+
+ va_list vl;
+ va_start(vl, nID);
+ format_v(sTemp, vl);
+ va_end(vl);
+
+ return *this;
+ }
+
+#ifdef __ATLBASE_H__
+ const stringT<T>& format(UINT nID, ... )
+ {
+ stringT<T> sTemp;
+ sTemp.load_string(nID);
+
+ va_list vl;
+ va_start(vl, nID);
+ format_v(sTemp, vl);
+ va_end(vl);
+
+ return *this;
+ }
+#endif // __ATLBASE_H__
+
+#endif
+
+ void format_v(const char* pszFormat, va_list vl)
+ {
+ // Copied Code....
+
+ // Doesn't have all the features of CString::Format()
+ T* pszTemp = NULL;
+ size_t nBufferSize = 32;
+ int nRetVal = -1;
+
+ do
+ {
+ // Double the buffer size for next time around
+ nBufferSize += nBufferSize;
+ delete [] pszTemp;
+ pszTemp = new T [nBufferSize];
+ nRetVal = _vsnprintf(pszTemp, nBufferSize, pszFormat, vl);
+ } while (nRetVal < 0);
+
+ *this = pszTemp;
+ delete [] pszTemp;
+
+ return;
+ }
+
+ void format_v(const wchar_t* pszFormat, va_list vl)
+ {
+ // Copied Code....
+
+ // Doesn't have all the features of CString::Format()
+ T* pszTemp = NULL;
+ size_t nBufferSize = 32;
+ int nRetVal = -1;
+
+ do
+ {
+ // Double the buffer size for next time around
+ nBufferSize += nBufferSize;
+ delete [] pszTemp;
+ pszTemp = new T [nBufferSize];
+ nRetVal = _vsnwprintf(pszTemp, nBufferSize, pszFormat, vl);
+ } while (nRetVal < 0);
+
+ *this = pszTemp;
+ delete [] pszTemp;
+
+ return;
+ }
+
+ int replace(const std::basic_string<T>& sFind, const std::basic_string<T>& sReplace, bool bMultiple = true, size_type nStart = 0)
+ {
+ stringT<T> sTemp = *this;
+ int nReplaced = 0;
+
+ if((nStart = sTemp.find(sFind, nStart)) != npos)
+ {
+ sTemp.erase(nStart, sFind.length());
+ sTemp.insert(nStart, sReplace);
+ nReplaced++;
+ }
+
+ while(bMultiple && (nStart = sTemp.find(sFind, nStart + 1)) != npos)
+ {
+ sTemp.erase(nStart, sFind.length());
+ sTemp.insert(nStart, sReplace);
+ nReplaced++;
+ }
+
+ *this = sTemp;
+ return nReplaced;
+ }
+
+ // Operators
+
+ T operator[](int nIndex) const
+ { return at(nIndex); };
+
+#ifdef _INC_COMDEF
+ operator _bstr_t() const
+ { return _bstr_t(c_str()); };
+#endif
+
+ operator const T*() const
+ { return c_str(); };
+
+
+ // Buffer Operations
+ // Derived code from MFC CString
+ T* get_buffer(size_t sz = npos)
+ {
+ if(sz == npos)
+ sz = size();
+
+ resize(sz + 1);
+ // Make sure we generate a write command
+ // so we have no extra references
+ // TODO: Does basic_string use reference counting?
+ // (much later) A: Yes it does! Reference is in the bytes before data
+ at(sz) = 0;
+ return const_cast<T*>(data());
+ }
+
+ void release_buffer()
+ {
+ if(!empty())
+ {
+ // Make sure we generate a write command
+ // Find the null terminated end
+ for(string::size_type i = 0; i < capacity() && i < size(); i++)
+ if(at(i) == 0)
+ break;
+
+ ASSERT(i != capacity());
+ resize(i);
+ }
+ }
+
+ void clear()
+ { resize(0); }
+
+#ifdef _WINDOWS_
+ bool load_string(HINSTANCE hInst, unsigned int nID)
+ {
+ string::size_type nSize = 0x80;
+ do
+ {
+ release_buffer();
+ nSize *= 2;
+ }
+ while(load_string(hInst, nID, get_buffer(nSize), nSize) == nSize);
+
+ release_buffer();
+ return nSize > 0;
+ }
+#endif // WIN32
+
+#ifdef __ATLBASE_H__
+ bool load_string(unsigned int nID)
+ {
+ string::size_type nSize = 0x80;
+ do
+ {
+ release_buffer();
+ nSize *= 2;
+ }
+ while(load_string(_Module.m_hInstResource, nID, get_buffer(nSize), nSize) == nSize);
+
+ release_buffer();
+ return nSize > 0;
+ }
+#endif // __ATLBASE_H__
+
+
+protected:
+
+#ifdef _WINDOWS_
+
+ static int load_string(HINSTANCE hInst, UINT nID, char* pBuff, size_t nSize)
+ {
+ return ::LoadStringA(hInst, nID, pBuff, nSize);
+ }
+
+ static int load_string(HINSTANCE hInst, UINT nID, wchar_t* pBuff, size_t nSize)
+ {
+#ifdef _UNICODE
+ return ::LoadStringW(hInst, nID, pBuff, nSize);
+#else
+ char* pABuff = new char[nSize];
+ if(!pABuff) return 0;
+
+ int nRet = load_string(hInst, nID, pABuff, nSize);
+ if(nRet)
+ {
+ pBuff[0] = L'\0';
+ MultiByteToWideChar(GetACP(), 0, pABuff, -1, pBuff, nSize);
+ }
+
+ return nRet;
+#endif
+ }
+
+
+
+#endif
+
+
+};
+
+ #ifndef _UNICODE
+ typedef stringT<char> string;
+ #else
+ typedef stringT<wchar_t> string;
+ #endif
+
+ typedef stringT<char> astring;
+ typedef stringT<wchar_t> wstring;
+
+
+template<typename T>
+class cstringT
+{
+public:
+ cstringT()
+ { clear(); };
+ cstringT(const T* ptr, size_t len)
+ { set(ptr, len); };
+
+ operator stringT<T>() const
+ {
+ if(_Ptr)
+ return stringT<T>(_Ptr, _Len);
+ else
+ return stringT<T>();
+ };
+
+ bool is_null() const
+ { return _Ptr ? false : true; }
+ // TODO: Maybe some validity checks
+ void set(const T* ptr, size_t len)
+ { _Ptr = ptr; _Len = len; }
+ void clear()
+ { _Ptr = 0; _Len = 0; }
+
+protected:
+ const T* _Ptr;
+ size_t _Len;
+};
+
+
+ #ifndef _UNICODE
+ typedef cstringT<char> cstring;
+ #else
+ typedef cstringT<wchar_t> cstring;
+ #endif
+
+ typedef cstringT<char> castring;
+ typedef cstringT<wchar_t> cwstring;
+
+
+
+#include <vector>
+#include <algorithm>
+
+template<typename S>
+class string_arrayT : public std::vector<S>
+{
+public:
+ string_arrayT(const std::vector<S> & x)
+ : std::vector<S>(x) {};
+ string_arrayT(string_arrayT & x)
+ : std::vector<S>(x) {};
+ string_arrayT(std::vector<S>::const_iterator first, std::vector<S>::const_iterator last, const std::vector<S>::allocator_type& al = std::vector<S>::allocator_type())
+ : std::vector<S>(first, last, al) {};
+ explicit string_arrayT(const const std::vector<S>::allocator_type& al = std::vector<S>::allocator_type())
+ : std::vector<S>(al) {};
+ explicit string_arrayT(std::vector<S>::size_type n, const std::vector<S>::value_type& v = std::vector<S>::value_type(), const std::vector<S>::allocator_type& al = std::vector<S>::allocator_type())
+ : std::vector<S>(n, v, al) {};
+
+ std::vector<S>::size_type split(S sIn, S sDelim, bool bTrim = false)
+ {
+ S::size_type start = 0;
+ S::size_type ed = 0;
+ std::vector<S>::size_type cnt = 0;
+ S::size_type lenDelim = sDelim.size();
+ S sTemp;
+
+ if(lenDelim == 0)
+ {
+ if(bTrim)
+ sIn.trim();
+ push_back(sIn);
+ return 1;
+ }
+
+ while(ed != string::npos)
+ {
+ ed = sIn.find(sDelim, start);
+ if(ed == string::npos)
+ sTemp = sIn.substr(start);
+ else
+ sTemp = sIn.substr(start, ed - start);
+
+ if(bTrim)
+ sTemp.trim();
+
+ push_back(sTemp);
+
+ cnt++;
+
+ start = ed + lenDelim;
+ }
+
+ return cnt;
+ }
+
+ S join(S sDelim) const
+ {
+ S sTemp;
+ const_iterator it = begin();
+ const_iterator ed = end();
+
+ while(it != ed)
+ {
+ sTemp += *it;
+ it++;
+ }
+
+ return sTemp;
+ }
+
+ class no_case
+ {
+ public:
+ no_case(const S& s) : _str(s)
+ { _str.make_lower(); }
+
+ bool operator()(const S& s)
+ { return s.lower() == _str; }
+ protected:
+ S _str;
+ };
+
+ bool has(S sVal, bool bCase = true) const
+ { return find(sVal, bCase) != end(); }
+
+ const_iterator find(S sVal, bool bCase = true) const
+ {
+ if(bCase)
+ return std::find(begin(), end(), sVal);
+ else
+ {
+ no_case fndr(sVal);
+ return std::find_if(begin(), end(), fndr);
+ }
+ }
+
+ iterator find(S sVal, bool bCase = true)
+ {
+ if(bCase)
+ return std::find(begin(), end(), sVal);
+ else
+ {
+ no_case fndr(sVal);
+ return std::find_if(begin(), end(), fndr);
+ }
+ }
+};
+
+typedef string_arrayT< string > string_array;
+typedef string_arrayT< wstring > wstring_array;
+
+//////////////////////////////////////////////////////////////////////////////
+
+#pragma warning( pop )
+
+#endif // !defined(_MYSTRING_H_)
diff --git a/win32/common/rep.ico b/win32/common/rep.ico
new file mode 100644
index 0000000..51cf5f8
--- /dev/null
+++ b/win32/common/rep.ico
Binary files differ
diff --git a/win32/common/rliberr.h b/win32/common/rliberr.h
new file mode 100644
index 0000000..8fe3a80
--- /dev/null
+++ b/win32/common/rliberr.h
@@ -0,0 +1,136 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * VERSION
+ * 2.2.0b
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef _RLIBERR_H_
+#define _RLIBERR_H_
+
+#ifndef _WINERROR_
+ #error Include winerror.h first.
+#endif
+
+#define HRESULT_FROM_RLIB(code) \
+ MAKE_HRESULT(SEVERITY_ERROR, FACILITY_RLIB, abs(code))
+
+/* ------------------------------------------------------------------------ *\
+ Rlib Errors
+\* ------------------------------------------------------------------------ */
+//
+// Values are 32 bit values layed out as follows:
+//
+// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
+// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+// +---+-+-+-----------------------+-------------------------------+
+// |Sev|C|R| Facility | Code |
+// +---+-+-+-----------------------+-------------------------------+
+//
+// where
+//
+// Sev - is the severity code
+//
+// 00 - Success
+// 01 - Informational
+// 10 - Warning
+// 11 - Error
+//
+// C - is the Customer code flag
+//
+// R - is a reserved bit
+//
+// Facility - is the facility code
+//
+// Code - is the facility's status code
+//
+//
+// Define the facility codes
+//
+#define FACILITY_RLIB 0x196
+
+
+//
+// Define the severity codes
+//
+
+
+//
+// MessageId: RLIB_E_NOMEM
+//
+// MessageText:
+//
+// Out of Memory.
+//
+#define RLIB_E_NOMEM ((HRESULT)0x81960001L)
+
+//
+// MessageId: RLIB_E_SYNTAX
+//
+// MessageText:
+//
+// Rep script syntax error.
+//
+#define RLIB_E_SYNTAX ((HRESULT)0x81960002L)
+
+//
+// MessageId: RLIB_E_REGEXP
+//
+// MessageText:
+//
+// Regular expression syntax error.
+//
+#define RLIB_E_REGEXP ((HRESULT)0x81960003L)
+
+//
+// MessageId: RLIB_E_LOOP
+//
+// MessageText:
+//
+// Rep encountered an endless loop.
+//
+#define RLIB_E_LOOP ((HRESULT)0x81960004L)
+
+//
+// MessageId: RLIB_E_USER
+//
+// MessageText:
+//
+// User defined error.
+//
+#define RLIB_E_USER ((HRESULT)0x81960005L)
+
+//
+// MessageId: RLIB_E_IOERR
+//
+// MessageText:
+//
+// There was an error reading or writing the data.
+//
+#define RLIB_E_IOERR ((HRESULT)0x81960006L)
+
+//
+// MessageId: RLIB_E_INVARG
+//
+// MessageText:
+//
+// Programmer Error: Invalid argument.
+//
+#define RLIB_E_INVARG ((HRESULT)0x8196000AL)
+
+#endif // _RLIBERR_H_ \ No newline at end of file
diff --git a/win32/common/rliberr.mc b/win32/common/rliberr.mc
new file mode 100644
index 0000000..50ac61f
--- /dev/null
+++ b/win32/common/rliberr.mc
@@ -0,0 +1,106 @@
+;/*
+; * AUTHOR
+; * N. Nielsen
+; *
+; * VERSION
+; * 2.2.0b
+; *
+; * LICENSE
+; * This software is in the public domain.
+; *
+; * The software is provided "as is", without warranty of any kind,
+; * express or implied, including but not limited to the warranties
+; * of merchantability, fitness for a particular purpose, and
+; * noninfringement. In no event shall the author(s) be liable for any
+; * claim, damages, or other liability, whether in an action of
+; * contract, tort, or otherwise, arising from, out of, or in connection
+; * with the software or the use or other dealings in the software.
+; *
+; * SUPPORT
+; * Send bug reports to: <nielsen@memberwebs.com>
+; */
+;
+;#ifndef _RLIBERR_H_
+;#define _RLIBERR_H_
+;
+;#ifndef _WINERROR_
+; #error Include winerror.h first.
+;#endif
+;
+;#define HRESULT_FROM_RLIB(code) \
+; MAKE_HRESULT(SEVERITY_ERROR, FACILITY_RLIB, abs(code))
+;
+
+SeverityNames=(
+ Success=0x0
+ Error=0x2
+ )
+
+
+MessageIdTypedef=HRESULT
+FacilityNames=(
+ System=0FF
+ Rlib=406:FACILITY_RLIB
+ )
+
+;/* ------------------------------------------------------------------------ *\
+; Rlib Errors
+;\* ------------------------------------------------------------------------ */
+
+MessageId=1
+Severity=Error
+Facility=Rlib
+SymbolicName=RLIB_E_NOMEM
+Language=English
+Out of Memory.
+.
+
+MessageId=2
+Severity=Error
+Facility=Rlib
+SymbolicName=RLIB_E_SYNTAX
+Language=English
+Rep script syntax error.
+.
+
+MessageId=3
+Severity=Error
+Facility=Rlib
+SymbolicName=RLIB_E_REGEXP
+Language=English
+Regular expression syntax error.
+.
+
+MessageId=4
+Severity=Error
+Facility=Rlib
+SymbolicName=RLIB_E_LOOP
+Language=English
+Rep encountered an endless loop.
+.
+
+MessageId=5
+Severity=Error
+Facility=Rlib
+SymbolicName=RLIB_E_USER
+Language=English
+User defined error.
+.
+
+MessageId=6
+Severity=Error
+Facility=Rlib
+SymbolicName=RLIB_E_IOERR
+Language=English
+There was an error reading or writing the data.
+.
+
+MessageId=10
+Severity=Error
+Facility=Rlib
+SymbolicName=RLIB_E_INVARG
+Language=English
+Programmer Error: Invalid argument.
+.
+
+;#endif // _RLIBERR_H_ \ No newline at end of file
diff --git a/win32/droplet/Makefile.am b/win32/droplet/Makefile.am
new file mode 100644
index 0000000..5a5ca0a
--- /dev/null
+++ b/win32/droplet/Makefile.am
@@ -0,0 +1,2 @@
+EXTRA_DIST = droplet.dsp droplet.rc dropletmain.cpp progressdlg.h progressdlg.cpp replace.cpp replace.h resource.h stdafx.h stdafx.cpp temp.rep
+
diff --git a/win32/droplet/droplet.dsp b/win32/droplet/droplet.dsp
new file mode 100644
index 0000000..579b546
--- /dev/null
+++ b/win32/droplet/droplet.dsp
@@ -0,0 +1,223 @@
+# Microsoft Developer Studio Project File - Name="droplet" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Application" 0x0101
+
+CFG=droplet - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "droplet.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "droplet.mak" CFG="droplet - Win32 Debug"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "droplet - Win32 Debug" (based on "Win32 (x86) Application")
+!MESSAGE "droplet - Win32 Release" (based on "Win32 (x86) Application")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+MTL=midl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "droplet - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "debug"
+# PROP Intermediate_Dir "debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /Gm /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c
+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../.." /I ".." /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 libpcre.a kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /base:"0x500000" /subsystem:windows /debug /machine:I386 /out:"../debug/droplet.exe" /pdbtype:sept /libpath:"..\win32\pcre\lib\\" /libpath:"..\pcre\lib\\"
+
+!ELSEIF "$(CFG)" == "droplet - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "release"
+# PROP Intermediate_Dir "release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /O1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_ATL_DLL" /D "_ATL_MIN_CRT" /Yu"stdafx.h" /FD /c
+# ADD CPP /nologo /W3 /GX /O1 /I "../.." /I ".." /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /c
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /machine:I386
+# ADD LINK32 libpcre.a kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /base:"0x500000" /subsystem:windows /machine:I386 /out:"../release/droplet.exe" /libpath:"..\pcre\lib\\" /opt:nowin98
+# SUBTRACT LINK32 /pdb:none
+
+!ENDIF
+
+# Begin Target
+
+# Name "droplet - Win32 Debug"
+# Name "droplet - Win32 Release"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=..\common\droplet.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\droplet.rc
+# End Source File
+# Begin Source File
+
+SOURCE=.\dropletmain.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\errutil.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\ProgressDlg.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\Replace.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\rliberr.mc
+
+!IF "$(CFG)" == "droplet - Win32 Debug"
+
+# Begin Custom Build
+InputPath=..\common\rliberr.mc
+InputName=rliberr
+
+"$(InputName).rc" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ mc $(InputPath)
+ copy $(InputName).h ..\Common
+
+# End Custom Build
+
+!ELSEIF "$(CFG)" == "droplet - Win32 Release"
+
+# PROP Ignore_Default_Tool 1
+# Begin Custom Build
+InputPath=..\common\rliberr.mc
+InputName=rliberr
+
+"$(InputName).rc" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ mc $(InputPath)
+
+# End Custom Build
+
+!ENDIF
+
+# End Source File
+# Begin Source File
+
+SOURCE=.\StdAfx.cpp
+# ADD CPP /Yc"stdafx.h"
+# End Source File
+# Begin Source File
+
+SOURCE=.\temp.rep
+
+!IF "$(CFG)" == "droplet - Win32 Debug"
+
+# Begin Custom Build
+InputPath=.\temp.rep
+
+"temp.cmp" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ ..\debug\repc.exe temp.rep temp.cmp
+
+# End Custom Build
+
+!ELSEIF "$(CFG)" == "droplet - Win32 Release"
+
+# PROP Ignore_Default_Tool 1
+# Begin Custom Build
+InputPath=.\temp.rep
+
+"temp.cmp" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ ..\release\repc.exe temp.rep temp.cmp
+
+# End Custom Build
+
+!ENDIF
+
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=..\common\droplet.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\ProgressDlg.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\Replace.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\Resource.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\StdAfx.h
+# End Source File
+# End Group
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# Begin Source File
+
+SOURCE=.\MSG00001.bin
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\rep.ico
+# End Source File
+# Begin Source File
+
+SOURCE=.\temp.cmp
+# End Source File
+# End Group
+# End Target
+# End Project
+# Section droplet : {00000000-0000-0000-0000-800000800000}
+# 1:15:IDD_PROGRESSDLG:101
+# End Section
diff --git a/win32/droplet/droplet.rc b/win32/droplet/droplet.rc
new file mode 100644
index 0000000..f0254d7
--- /dev/null
+++ b/win32/droplet/droplet.rc
@@ -0,0 +1,197 @@
+//Microsoft Developer Studio generated resource script.
+//
+#include "resource.h"
+
+#define APSTUDIO_READONLY_SYMBOLS
+/////////////////////////////////////////////////////////////////////////////
+//
+// Generated from the TEXTINCLUDE 2 resource.
+//
+#include "winres.h"
+
+
+/////////////////////////////////////////////////////////////////////////////
+#undef APSTUDIO_READONLY_SYMBOLS
+
+/////////////////////////////////////////////////////////////////////////////
+// Neutral resources
+
+#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_NEU)
+#ifdef _WIN32
+LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
+#pragma code_page(1252)
+#endif //_WIN32
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// REP
+//
+
+SCRIPT REP DISCARDABLE "temp.cmp"
+#endif // Neutral resources
+/////////////////////////////////////////////////////////////////////////////
+
+
+/////////////////////////////////////////////////////////////////////////////
+// English (U.S.) resources
+
+#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
+#ifdef _WIN32
+LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
+#pragma code_page(1252)
+#endif //_WIN32
+
+#ifdef APSTUDIO_INVOKED
+/////////////////////////////////////////////////////////////////////////////
+//
+// TEXTINCLUDE
+//
+
+1 TEXTINCLUDE DISCARDABLE
+BEGIN
+ "resource.h\0"
+END
+
+2 TEXTINCLUDE DISCARDABLE
+BEGIN
+ "#include ""winres.h""\r\n"
+ "\r\n"
+ "\0"
+END
+
+3 TEXTINCLUDE DISCARDABLE
+BEGIN
+ "\r\n"
+ "\0"
+END
+
+#endif // APSTUDIO_INVOKED
+
+
+#ifndef _MAC
+/////////////////////////////////////////////////////////////////////////////
+//
+// Version
+//
+
+VS_VERSION_INFO VERSIONINFO
+ FILEVERSION 2,3,0,1
+ PRODUCTVERSION 2,3,0,1
+ FILEFLAGSMASK 0x3fL
+#ifdef _DEBUG
+ FILEFLAGS 0x1L
+#else
+ FILEFLAGS 0x0L
+#endif
+ FILEOS 0x4L
+ FILETYPE 0x1L
+ FILESUBTYPE 0x0L
+BEGIN
+ BLOCK "StringFileInfo"
+ BEGIN
+ BLOCK "040904b0"
+ BEGIN
+ VALUE "Comments", "\0"
+ VALUE "CompanyName", "Nate Nielsen\0"
+ VALUE "FileDescription", "rep droplet\0"
+ VALUE "FileVersion", "2, 3, 0, 1\0"
+ VALUE "InternalName", "droplet\0"
+ VALUE "LegalCopyright", "Copyright 2002, Nate Nielsen \0"
+ VALUE "LegalTrademarks", "\0"
+ VALUE "OLESelfRegister", "\0"
+ VALUE "OriginalFilename", "droplet.exe\0"
+ VALUE "PrivateBuild", "\0"
+ VALUE "ProductName", "rep\0"
+ VALUE "ProductVersion", "2, 3, 0, 1\0"
+ VALUE "SpecialBuild", "\0"
+ END
+ END
+ BLOCK "VarFileInfo"
+ BEGIN
+ VALUE "Translation", 0x409, 1200
+ END
+END
+
+#endif // !_MAC
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Dialog
+//
+
+IDD_PROGRESSDLG DIALOG DISCARDABLE 0, 0, 219, 95
+STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
+CAPTION "Rep processing files..."
+FONT 8, "Tahoma"
+BEGIN
+ PUSHBUTTON "Cancel",2,162,74,50,14
+ LTEXT "Preparing...",IDC_FILENAME,7,51,205,8
+ LTEXT "",IDC_STATUS,7,62,205,8
+ CONTROL "",IDC_FLIP,"Static",SS_GRAYRECT,7,75,148,10
+ CONTROL "",IDC_STATIC,"Static",SS_GRAYFRAME,7,75,148,10
+ ICON IDI_REP,IDC_STATIC,14,14,20,20
+END
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// DESIGNINFO
+//
+
+#ifdef APSTUDIO_INVOKED
+GUIDELINES DESIGNINFO DISCARDABLE
+BEGIN
+ IDD_PROGRESSDLG, DIALOG
+ BEGIN
+ LEFTMARGIN, 7
+ RIGHTMARGIN, 212
+ TOPMARGIN, 7
+ BOTTOMMARGIN, 88
+ END
+END
+#endif // APSTUDIO_INVOKED
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// 11
+//
+
+1 11 DISCARDABLE "MSG00001.bin"
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Icon
+//
+
+// Icon with lowest ID value placed first to ensure application icon
+// remains consistent on all systems.
+IDI_REP ICON DISCARDABLE "..\\common\\rep.ico"
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// String Table
+//
+
+STRINGTABLE DISCARDABLE
+BEGIN
+ IDS_PROJNAME "droplet"
+END
+
+#endif // English (U.S.) resources
+/////////////////////////////////////////////////////////////////////////////
+
+
+
+#ifndef APSTUDIO_INVOKED
+/////////////////////////////////////////////////////////////////////////////
+//
+// Generated from the TEXTINCLUDE 3 resource.
+//
+
+
+/////////////////////////////////////////////////////////////////////////////
+#endif // not APSTUDIO_INVOKED
+
diff --git a/win32/droplet/dropletmain.cpp b/win32/droplet/dropletmain.cpp
new file mode 100644
index 0000000..9610ce2
--- /dev/null
+++ b/win32/droplet/dropletmain.cpp
@@ -0,0 +1,76 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include "stdafx.h"
+#include "resource.h"
+#include <initguid.h>
+#include "replace.h"
+
+// ATL Support
+CComModule _Module;
+
+BEGIN_OBJECT_MAP(ObjectMap)
+END_OBJECT_MAP()
+
+
+// _tWinMain: -------------------------------------------------------------
+// Our starting point. Silly name
+
+extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
+ HINSTANCE /*hPrevInstance*/, LPTSTR /* lpCmdLine */, int /*nShowCmd*/)
+{
+ // ATL support
+ HRESULT hRes = CoInitialize(NULL);
+ _ASSERTE(SUCCEEDED(hRes));
+ _Module.Init(ObjectMap, hInstance, NULL);
+ HRESULT hr = S_OK;
+
+ // Make sure we have something to process
+ if(__argc <= 1)
+ {
+ MessageBox(NULL, _T("Drag and drop files onto this droplet to process them."), _T("Rep Droplet"), MB_OK | MB_ICONINFORMATION);
+ hr = E_FAIL;
+ }
+
+ else
+ {
+ Replace replace;
+ if(SUCCEEDED(replace.initialize()))
+ {
+ for(int i = 1; i < __argc; i++)
+ {
+ // Process arguments appropriately
+ DWORD attrs = GetFileAttributes(__argv[i]);
+ if(attrs != 0xFFFFFFFF && attrs & FILE_ATTRIBUTE_DIRECTORY)
+ hr = replace.replaceFolder(__argv[i]);
+ else
+ hr = replace.replaceSingleFile(__argv[i]);
+
+ if(FAILED(hr))
+ break;
+ }
+
+ replace.terminate();
+ }
+ }
+
+ _Module.Term();
+ CoUninitialize();
+ return SUCCEEDED(hr) ? 0 : 1;
+}
diff --git a/win32/droplet/progressdlg.cpp b/win32/droplet/progressdlg.cpp
new file mode 100644
index 0000000..53f5e14
--- /dev/null
+++ b/win32/droplet/progressdlg.cpp
@@ -0,0 +1,211 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+// ProgressDlg.cpp : Implementation of CProgressDlg
+#include "stdafx.h"
+#include "ProgressDlg.h"
+
+// (Con|De)struction: -----------------------------------------------
+
+ProgressDlg::ProgressDlg()
+{
+ m_numFiles = 0;
+ m_numReplaces = 0;
+ m_flip = false;
+ m_hThread = NULL;
+ m_hEvent = NULL;
+ InitializeCriticalSection(&m_sec);
+}
+
+ProgressDlg::~ProgressDlg()
+{
+ DeleteCriticalSection(&m_sec);
+ if(m_hEvent)
+ CloseHandle(m_hEvent);
+}
+
+
+// threadProc: ---------------------------------------------------------
+// Runs the dialog in another thread
+static DWORD WINAPI threadProc(void* pv)
+{
+ ProgressDlg* dlg = (ProgressDlg*)pv;
+ return dlg->DoModal(NULL, NULL);
+}
+
+
+// startDialog: --------------------------------------------------------
+// Starts the dialog thread
+HRESULT ProgressDlg::startDialog()
+{
+ ASSERT(m_hThread == NULL);
+ HRESULT ret = S_OK;
+
+ if(m_hEvent)
+ CloseHandle(m_hEvent);
+
+ m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+
+ DWORD threadID;
+ m_hThread = CreateThread(NULL, 0, threadProc, this, CREATE_SUSPENDED, &threadID);
+ if(!m_hThread)
+ ret = HRESULT_FROM_WIN32(::GetLastError());
+ else
+ ResumeThread(m_hThread);
+
+ // Wait for dialog to start up.
+ ::WaitForSingleObject(m_hEvent, 10000);
+ CloseHandle(m_hEvent);
+ m_hEvent = NULL;
+
+ return S_OK;
+}
+
+// stopDialog: ----------------------------------------------------------
+// Stops the dialog thread and closes the dialog
+int ProgressDlg::stopDialog()
+{
+ int ret = 0;
+
+ if(m_hThread && m_hWnd)
+ {
+ ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
+ WaitForSingleObject(m_hThread, INFINITE);
+ }
+ else if(m_hThread)
+ {
+ EnterCriticalSection(&m_sec);
+ TerminateThread(m_hThread, -1);
+ }
+
+ if(m_hThread)
+ {
+ DWORD code = 0;
+ GetExitCodeThread(m_hThread, &code);
+ CloseHandle(m_hThread);
+ m_hThread = NULL;
+ ret = (int)code;
+ }
+
+ return 0;
+}
+
+// isCancelled: -----------------------------------------------------------
+// Checks if user pressed cancel button
+bool ProgressDlg::isCancelled()
+{
+ bool ret = false;
+
+ EnterCriticalSection(&m_sec);
+ ret = m_isCancelled;
+ LeaveCriticalSection(&m_sec);
+
+ return ret;
+}
+
+// updateControls: --------------------------------------------------------
+// Update dialog status
+void ProgressDlg::updateControls()
+{
+ if(IsWindow())
+ {
+ string status;
+ status.format(_T("%d replaces in %d files"), m_numReplaces, m_numFiles);
+ SetDlgItemText(IDC_STATUS, status);
+ }
+}
+
+// onReplaced: -------------------------------------------------------------
+// Called when a new replacement was made
+void ProgressDlg::onReplaced()
+{
+ m_flip = !m_flip;
+ m_numReplaces++;
+
+ // Do flicker thing
+ if(IsWindow())
+ ::ShowWindow(GetDlgItem(IDC_FLIP), m_flip ? SW_SHOW : SW_HIDE);
+
+ updateControls();
+}
+
+// onNewFile: --------------------------------------------------------------
+// Called when a new file starts processing
+void ProgressDlg::onNewFile(LPCTSTR fileName)
+{
+ if(IsWindow())
+ SetDlgItemText(IDC_FILENAME, fileName);
+
+ m_numFiles++;
+
+ updateControls();
+}
+
+// setTitle: ---------------------------------------------------------------
+// Set the title. If no window then just store it
+void ProgressDlg::setTitle(const string& title)
+{
+ m_title = title;
+
+ if(IsWindow())
+ SetWindowText(title);
+}
+
+
+// onInitDialog: ------------------------------------------------------------
+// Dialog initialization. We clear the event that says we've
+// started up.
+LRESULT ProgressDlg::onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ EnterCriticalSection(&m_sec);
+
+ if(!m_title.empty())
+ SetWindowText(m_title.c_str());
+
+ if(m_hEvent)
+ SetEvent(m_hEvent);
+
+ CenterWindow(NULL);
+
+ LeaveCriticalSection(&m_sec);
+
+ return 1;
+}
+
+// onClose: ------------------------------------------------------------------
+// Dialog was closed
+LRESULT ProgressDlg::onClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ // TODO: will this get called by NC area?
+ EndDialog(IDOK);
+ return 0;
+}
+
+// onCancel: ------------------------------------------------------------------
+// User clicked cancel, set flag
+LRESULT ProgressDlg::onCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ EnterCriticalSection(&m_sec);
+ m_isCancelled = true;
+ LeaveCriticalSection(&m_sec);
+
+ EndDialog(IDCANCEL);
+ return 0;
+}
+
diff --git a/win32/droplet/progressdlg.h b/win32/droplet/progressdlg.h
new file mode 100644
index 0000000..1e416a1
--- /dev/null
+++ b/win32/droplet/progressdlg.h
@@ -0,0 +1,92 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __PROGRESSDLG_H_
+#define __PROGRESSDLG_H_
+
+#include "resource.h" // main symbols
+#include <atlhost.h>
+
+// ProgressDlg: --------------------------------------------------------
+// The status dialog which is run in another thread
+
+class ProgressDlg :
+ public CDialogImpl<ProgressDlg>
+{
+public:
+ ProgressDlg();
+ ~ProgressDlg();
+
+ enum { IDD = IDD_PROGRESSDLG };
+
+BEGIN_MSG_MAP(ProgressDlg)
+ MESSAGE_HANDLER(WM_INITDIALOG, onInitDialog)
+ MESSAGE_HANDLER(WM_CLOSE, onClose)
+ COMMAND_ID_HANDLER(IDCANCEL, onCancel)
+END_MSG_MAP()
+
+public:
+ // Start dialog in another thread
+ HRESULT startDialog();
+
+ // Stop the dialog thread
+ int stopDialog();
+
+ // Called when a replacement is noted
+ void onReplaced();
+
+ // Called (by Replace::replaceSingleFile) when starting
+ // to process new file
+ void onNewFile(LPCTSTR fileName);
+
+ // Polled (by Replace::matchStatus) to check if user cancelled
+ bool isCancelled();
+
+ // Set the dialog title
+ void setTitle(const string& title);
+
+protected:
+ // Update status etc...
+ void updateControls();
+
+// The message handlers
+protected:
+ LRESULT onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+
+ // Handler prototypes:
+ // LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ // LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+ // LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
+
+protected:
+ HANDLE m_hThread; // The dialog thread
+ string m_title; // The dialog title
+
+ HANDLE m_hEvent; // dialog initialization event
+ CRITICAL_SECTION m_sec; // Used for syncing access to date below
+
+ bool m_isCancelled; // Set when user clicks cancel
+ uint m_numFiles; // Number of files processed
+ uint m_numReplaces; // Number of replaces seen
+ bool m_flip; // Used for flicker display
+};
+
+#endif //__PROGRESSDLG_H_
diff --git a/win32/droplet/replace.cpp b/win32/droplet/replace.cpp
new file mode 100644
index 0000000..8a7b51b
--- /dev/null
+++ b/win32/droplet/replace.cpp
@@ -0,0 +1,438 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include "stdafx.h"
+#include "Replace.h"
+#include "common/errutil.h"
+#include "common/rliberr.h"
+#include "common/mystring.h"
+
+
+// Error Macros: -------------------------------------------------
+// Display error messages and all that
+// Only valid in this context
+// Each of these jumps to the 'cleanup' label for wrapping
+// up any open objects.
+
+#define RETURN(v) \
+ { ret = (v); goto cleanup; }
+#define WINERR(f) \
+ { HRESULT hr = HRESULT_FROM_WIN32(::GetLastError()); RETURN(errorMessage(m_dlg.m_hWnd, hr, f)); }
+#define WINERR_1(f, a) \
+ { HRESULT hr = HRESULT_FROM_WIN32(::GetLastError()); RETURN(errorMessage(m_dlg.m_hWnd, hr, f, a)); }
+#define WINERR_E(e, f) \
+ { RETURN(errorMessage(m_dlg.m_hWnd, e, f)); }
+#define REPERR(c, s) \
+ { RETURN(rlibError(m_dlg.m_hWnd, c, s)); }
+
+
+// The extension appended to backup or temp files
+LPCTSTR kBackupExt = _T(".x_r");
+
+// (Con|De)struction: -------------------------------------------
+
+Replace::Replace()
+{
+ m_outFile = NULL;
+}
+
+Replace::~Replace()
+{
+ ASSERT(m_outFile == NULL);
+}
+
+
+// initialize: ---------------------------------------------------
+// Loads the script from resources and sets up dialog
+// in another thread
+
+HRESULT Replace::initialize()
+{
+ HRESULT ret = S_OK;
+
+ {
+ if(!m_droplet.load(GetModuleHandle(NULL)))
+ {
+ errorMessage(NULL, 0, _T("Couldn't access droplet rep script. The droplet is probably corrupted."));
+ RETURN(E_FAIL);
+ }
+
+ m_dlg.setTitle(m_droplet.getTitle());
+
+ r_context& ctx = m_droplet.getContext();
+
+ int r = repInit(&ctx);
+ if(r < 0)
+ REPERR(r, &(ctx.script));
+
+ // This starts the dialog in another thread
+ // in order to keep it responsive
+ m_dlg.startDialog();
+ }
+
+cleanup:
+ return ret;
+}
+
+
+// terminate: ----------------------------------------------------
+// Undoes above
+
+HRESULT Replace::terminate()
+{
+ m_dlg.stopDialog();
+ r_context& ctx = m_droplet.getContext();
+ repFree(&ctx);
+ return S_OK;
+}
+
+
+// writeFile: ----------------------------------------------------
+// The callback called from within rlib when data output
+// is required. The argument passed through the r_stream
+// is a pointer to the Replace object.
+
+int Replace::writeFile(r_stream* stream, byte* data, size_t len)
+{
+ Replace* replace = (Replace*)stream->arg;
+
+ // The m_outFile member should be set (and only set)
+ // whenever we're inside rlibRun
+ ASSERT(replace->m_outFile != NULL);
+ DWORD written = 0;
+
+ // Double check that everything was written
+ return WriteFile(replace->m_outFile, data, len, &written, NULL) && len == written;
+}
+
+// matchStatus: --------------------------------------------------
+// Callback whenever something is replaced in rlib
+// Updates the dialog.
+
+int Replace::matchStatus(r_stream* stream, r_replace* repl)
+{
+ Replace* replace = (Replace*)stream->arg;
+ ASSERT_PTR(replace);
+
+ replace->m_dlg.onReplaced();
+ return replace->m_dlg.isCancelled() ? 1 : 0;
+}
+
+
+// replaceFolder: ------------------------------------------------
+// Recursively processes a folder using replaceSingleFile
+
+HRESULT Replace::replaceFolder(LPCTSTR folder)
+{
+ HANDLE hFindFile = NULL;
+ WIN32_FIND_DATA findData;
+ TCHAR old[260]; // Save the current folder (as we'll change it)
+ HRESULT ret = S_FALSE;
+
+ {
+ // Backup the current directory
+ if(!GetCurrentDirectory(260, old))
+ WINERR("Couldn't get the current directory.");
+
+ if(!SetCurrentDirectory(folder))
+ WINERR_1("Couldn't change the current directory to %s", folder);
+
+ if(hFindFile = FindFirstFile(_T("*.*"), &findData))
+ {
+ // And for every file
+ do
+ {
+ HRESULT r = S_FALSE;
+ if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
+ {
+ if(findData.cFileName[0] == '.' &&
+ (findData.cFileName[1] == '.' || findData.cFileName[1] == '\0'))
+ continue;
+
+ r = replaceFolder(findData.cFileName);
+ }
+ else
+ {
+ r = replaceSingleFile(findData.cFileName);
+ }
+
+ if(FAILED(r))
+ RETURN(r);
+ if(r == S_OK)
+ ret = r;
+ }
+ while(FindNextFile(hFindFile, &findData));
+ }
+
+ // Make sure we didn't miss anything
+ if(::GetLastError() != 0 && ::GetLastError() != ERROR_NO_MORE_FILES)
+ WINERR_1("Couldn't list the directory %s", folder);
+
+ // Change directory back
+ SetCurrentDirectory(old);
+ }
+
+cleanup:
+ if(hFindFile)
+ FindClose(hFindFile);
+
+ return ret;
+}
+
+
+// replaceSingleFile: ----------------------------------------------
+// Process a file into a temp file or backup etc...
+// Calls replaceFilename for actual processing
+
+HRESULT Replace::replaceSingleFile(LPCTSTR fileName)
+{
+ HRESULT ret = S_FALSE;
+ string temp;
+ USES_CONVERSION;
+
+ r_context& ctx = m_droplet.getContext();
+
+ {
+ // Update the dialog display
+ m_dlg.onNewFile(fileName);
+
+ // This lets us use the filename from within the script
+ ret = rlibSetVar(&(ctx.stream), "FILENAME", T2CA(fileName));
+ if(ret < 0)
+ REPERR(ret, &(ctx.script));
+
+ // Make a temp/backup name
+ temp.assign(fileName);
+ temp.append(kBackupExt);
+
+ // Now we do this 2 ways, if we're keeping backups then...
+ if(m_droplet.keepBackups())
+ {
+ DeleteFile(temp);
+
+ // Rename the original file to the backup
+ if(!MoveFile(fileName, temp))
+ WINERR_1("There was an error while creating the backup file: %s", temp.c_str());
+
+ // Do a replacement operation back to the original
+ // from the backup
+ ret = replaceFilename(temp, fileName);
+
+ // If there were no replacements
+ if(ret != S_OK)
+ {
+ DeleteFile(fileName);
+
+ // And rename the backup file back
+ if(!MoveFile(temp, fileName))
+ WINERR_1("There was an error while renaming the file: %s", fileName);
+ }
+ }
+
+ // No backups, much faster (when there's no replacements)!
+ else
+ {
+ // Do a replacement operation to a temp file
+ ret = replaceFilename(fileName, temp);
+
+ // If there were replacements
+ if(ret == S_OK)
+ {
+ DeleteFile(fileName);
+
+ // Rename temp to original
+ if(!MoveFile(temp, fileName))
+ WINERR_1("There was an error while renaming the file: %s", fileName);
+ }
+
+ // If no replacements
+ else
+ {
+ // Remove temp file
+ DeleteFile(temp);
+ }
+ }
+ }
+
+cleanup:
+
+ return ret;
+}
+
+// replaceFilename: -------------------------------------------------
+// Process one file into another
+// Open the files and passes them to replaceFile
+
+HRESULT Replace::replaceFilename(LPCTSTR fileIn, LPCTSTR fileOut)
+{
+ HANDLE in = INVALID_HANDLE_VALUE;
+ HANDLE out = INVALID_HANDLE_VALUE;
+ HRESULT ret = S_FALSE;
+
+ {
+ // Basic readonly open
+ in = CreateFile(fileIn, GENERIC_READ, FILE_SHARE_READ, NULL,
+ OPEN_EXISTING, 0, NULL);
+ if(in == INVALID_HANDLE_VALUE)
+ WINERR_1("There was an error opening the file: %s", fileIn);
+
+ // We need to know whether we're NT or not below
+ OSVERSIONINFO os;
+ os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+ GetVersionEx(&os);
+
+ // This creates a new file for writing
+ // If we're running on NT, then use exactly the same
+ // attributes and permissions as original file
+ out = CreateFile(fileOut, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0,
+ os.dwPlatformId & VER_PLATFORM_WIN32_NT ? in : NULL);
+ if(out == INVALID_HANDLE_VALUE)
+ WINERR_1("There was an error opening the file: %s", fileIn);
+
+
+ // Do replacements!
+ ret = replaceFile(in, out);
+
+ }
+
+cleanup:
+
+ if(in != INVALID_HANDLE_VALUE)
+ CloseHandle(in);
+ if(out != INVALID_HANDLE_VALUE)
+ CloseHandle(out);
+
+ return ret;
+}
+
+
+// replaceFile: --------------------------------------------------------
+// Process one open file handle into another
+// This memory maps the input file and passes it to replaceBuffer
+
+HRESULT Replace::replaceFile(HANDLE in, HANDLE out)
+{
+ HANDLE mapping = NULL;
+ void* data = NULL;
+ HRESULT ret = S_FALSE;
+
+ {
+ // Memory map the input file
+ mapping = CreateFileMapping(in, NULL, PAGE_READONLY, 0, 0, NULL);
+ if(!mapping)
+ WINERR("There was an error reading the input file.");
+
+ data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
+ if(!data)
+ WINERR("There was an error reading the input file.");
+
+ size_t size = GetFileSize(in, NULL);
+ if(size == INVALID_FILE_SIZE)
+ WINERR("There was an error reading the input file.");
+
+ // And pass it on
+ ret = replaceBuffer((byte*)data, size, out);
+ }
+
+cleanup:
+
+ if(data)
+ UnmapViewOfFile(data);
+ if(mapping)
+ CloseHandle(mapping);
+
+ return ret;
+}
+
+
+// replaceBuffer: ----------------------------------------------------
+// Process a memory buffer into a file for output
+// Uses the rlib functions directly
+
+HRESULT Replace::replaceBuffer(byte* data, size_t size, HANDLE out)
+{
+ ASSERT(m_outFile == NULL);
+
+ size_t blockSize = m_droplet.getBuffSize() * 2; // Block size
+ size_t batchSize; // Current batch size
+ int r = R_IN;
+ bool dirty = false;
+ HRESULT ret = S_OK;
+
+ {
+ // If there's no blocksize then we process the
+ // entire file at once
+ if(blockSize == 0)
+ blockSize = size;
+
+ batchSize = blockSize;
+
+ // Setup the rlib stream
+ r_context& ctx = m_droplet.getContext();
+
+ ctx.stream.nextIn = data;
+ ctx.stream.availIn = blockSize;
+ ctx.stream.fWrite = writeFile;
+ ctx.stream.arg = this;
+ ctx.stream.fMatch = matchStatus;
+
+ // This is used by writeFile
+ m_outFile = out;
+
+ // While we have more data to put in ...
+ while(size > 0 || r == R_IN)
+ {
+ // If rlib wants data then give it
+ if(r == R_IN)
+ {
+ batchSize = (blockSize > size) ? size : blockSize;
+
+ ctx.stream.nextIn = data;
+ ctx.stream.availIn = batchSize;
+
+ }
+
+ // call rlib
+ r = rlibRun(&(ctx.stream), &(ctx.script), (size - batchSize) <= 0);
+
+ // Oops!
+ if(r < 0)
+ REPERR(r, &(ctx.script));
+
+ if(ctx.stream.total > 0)
+ dirty = true;
+
+ // Increment last batch
+ data += (batchSize - ctx.stream.availIn);
+ size -= (batchSize - ctx.stream.availIn);
+ }
+
+ // Clears and prepares for next file
+ rlibClear(&ctx.stream);
+ }
+
+cleanup:
+
+ m_outFile = NULL;
+
+ if(FAILED(ret))
+ return ret;
+
+ return dirty ? S_OK : S_FALSE;
+}
diff --git a/win32/droplet/replace.h b/win32/droplet/replace.h
new file mode 100644
index 0000000..b3c72c3
--- /dev/null
+++ b/win32/droplet/replace.h
@@ -0,0 +1,73 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __REPLACE_H__
+#define __REPLACE_H__
+
+#include "ProgressDlg.h"
+#include "common/Droplet.h"
+#include "lib/rlib.h"
+
+// Replace: --------------------------------------------------------
+// Main processing class. Calls rlib for replacements
+
+class Replace
+{
+public:
+ Replace();
+ virtual ~Replace();
+
+ // Initialize processing (dialog, droplet etc...)
+ HRESULT initialize();
+
+ // Undo initialization
+ HRESULT terminate();
+
+ // Recursively process files in a folder
+ HRESULT replaceFolder(LPCTSTR folder);
+
+ // Process a single file into itself (maybe using backups)
+ HRESULT replaceSingleFile(LPCTSTR fileName);
+
+ // Process one file into another
+ HRESULT replaceFilename(LPCTSTR fileIn, LPCTSTR fileOut);
+
+ // Process one open file into another
+ HRESULT replaceFile(HANDLE in, HANDLE out);
+
+ // Process a memory buffer into an open file
+ HRESULT replaceBuffer(byte* data, size_t size, HANDLE out);
+
+
+private:
+ // Callback for rlib when data output is needed
+ static int writeFile(r_stream* stream, byte* data, size_t len);
+
+ // Callback for rlib status updates
+ static int matchStatus(r_stream* stream, r_replace* repl);
+
+protected:
+ Droplet m_droplet; // The droplet
+ ProgressDlg m_dlg; // The progress dialog
+
+private:
+ HANDLE m_outFile; // Open output file handle used by writeFile
+};
+
+#endif // __REPLACE_H__
diff --git a/win32/droplet/resource.h b/win32/droplet/resource.h
new file mode 100644
index 0000000..91516ec
--- /dev/null
+++ b/win32/droplet/resource.h
@@ -0,0 +1,22 @@
+//{{NO_DEPENDENCIES}}
+// Microsoft Developer Studio generated include file.
+// Used by droplet.rc
+//
+#define IDS_PROJNAME 100
+#define IDR_Droplet 100
+#define IDD_PROGRESSDLG 101
+#define IDC_FLIP 202
+#define IDC_STATUS 203
+#define IDI_REP 203
+#define IDC_FILENAME 204
+
+// Next default values for new objects
+//
+#ifdef APSTUDIO_INVOKED
+#ifndef APSTUDIO_READONLY_SYMBOLS
+#define _APS_NEXT_RESOURCE_VALUE 204
+#define _APS_NEXT_COMMAND_VALUE 32768
+#define _APS_NEXT_CONTROL_VALUE 205
+#define _APS_NEXT_SYMED_VALUE 102
+#endif
+#endif
diff --git a/win32/droplet/rliberr.h b/win32/droplet/rliberr.h
new file mode 100644
index 0000000..8fe3a80
--- /dev/null
+++ b/win32/droplet/rliberr.h
@@ -0,0 +1,136 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * VERSION
+ * 2.2.0b
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef _RLIBERR_H_
+#define _RLIBERR_H_
+
+#ifndef _WINERROR_
+ #error Include winerror.h first.
+#endif
+
+#define HRESULT_FROM_RLIB(code) \
+ MAKE_HRESULT(SEVERITY_ERROR, FACILITY_RLIB, abs(code))
+
+/* ------------------------------------------------------------------------ *\
+ Rlib Errors
+\* ------------------------------------------------------------------------ */
+//
+// Values are 32 bit values layed out as follows:
+//
+// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
+// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+// +---+-+-+-----------------------+-------------------------------+
+// |Sev|C|R| Facility | Code |
+// +---+-+-+-----------------------+-------------------------------+
+//
+// where
+//
+// Sev - is the severity code
+//
+// 00 - Success
+// 01 - Informational
+// 10 - Warning
+// 11 - Error
+//
+// C - is the Customer code flag
+//
+// R - is a reserved bit
+//
+// Facility - is the facility code
+//
+// Code - is the facility's status code
+//
+//
+// Define the facility codes
+//
+#define FACILITY_RLIB 0x196
+
+
+//
+// Define the severity codes
+//
+
+
+//
+// MessageId: RLIB_E_NOMEM
+//
+// MessageText:
+//
+// Out of Memory.
+//
+#define RLIB_E_NOMEM ((HRESULT)0x81960001L)
+
+//
+// MessageId: RLIB_E_SYNTAX
+//
+// MessageText:
+//
+// Rep script syntax error.
+//
+#define RLIB_E_SYNTAX ((HRESULT)0x81960002L)
+
+//
+// MessageId: RLIB_E_REGEXP
+//
+// MessageText:
+//
+// Regular expression syntax error.
+//
+#define RLIB_E_REGEXP ((HRESULT)0x81960003L)
+
+//
+// MessageId: RLIB_E_LOOP
+//
+// MessageText:
+//
+// Rep encountered an endless loop.
+//
+#define RLIB_E_LOOP ((HRESULT)0x81960004L)
+
+//
+// MessageId: RLIB_E_USER
+//
+// MessageText:
+//
+// User defined error.
+//
+#define RLIB_E_USER ((HRESULT)0x81960005L)
+
+//
+// MessageId: RLIB_E_IOERR
+//
+// MessageText:
+//
+// There was an error reading or writing the data.
+//
+#define RLIB_E_IOERR ((HRESULT)0x81960006L)
+
+//
+// MessageId: RLIB_E_INVARG
+//
+// MessageText:
+//
+// Programmer Error: Invalid argument.
+//
+#define RLIB_E_INVARG ((HRESULT)0x8196000AL)
+
+#endif // _RLIBERR_H_ \ No newline at end of file
diff --git a/win32/droplet/rliberr.rc b/win32/droplet/rliberr.rc
new file mode 100644
index 0000000..0885a89
--- /dev/null
+++ b/win32/droplet/rliberr.rc
@@ -0,0 +1,2 @@
+LANGUAGE 0x9,0x1
+1 11 MSG00001.bin
diff --git a/win32/droplet/stdafx.cpp b/win32/droplet/stdafx.cpp
new file mode 100644
index 0000000..7ab5779
--- /dev/null
+++ b/win32/droplet/stdafx.cpp
@@ -0,0 +1,31 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+// stdafx.cpp : source file that includes just the standard includes
+// stdafx.pch will be the pre-compiled header
+// stdafx.obj will contain the pre-compiled type information
+
+#include "stdafx.h"
+
+#ifdef _ATL_STATIC_REGISTRY
+#include <statreg.h>
+#include <statreg.cpp>
+#endif
+
+#include <atlimpl.cpp>
diff --git a/win32/droplet/stdafx.h b/win32/droplet/stdafx.h
new file mode 100644
index 0000000..16885d5
--- /dev/null
+++ b/win32/droplet/stdafx.h
@@ -0,0 +1,39 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#if !defined(AFX_STDAFX_H__3607160A_967A_4E9D_A154_BC3CF029F2C2__INCLUDED_)
+#define AFX_STDAFX_H__3607160A_967A_4E9D_A154_BC3CF029F2C2__INCLUDED_
+
+#define STRICT
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0400
+#endif
+#define _ATL_APARTMENT_THREADED
+
+#include <atlbase.h>
+extern CComModule _Module;
+#include <atlcom.h>
+#include <atlwin.h>
+
+#include "config.win32.h"
+#include "common/compat.h"
+#include "common/usuals.h"
+#include <mystring.h>
+
+#endif // !defined(AFX_STDAFX_H__3607160A_967A_4E9D_A154_BC3CF029F2C2__INCLUDED)
diff --git a/win32/droplet/temp.cmp b/win32/droplet/temp.cmp
new file mode 100644
index 0000000..137c82e
--- /dev/null
+++ b/win32/droplet/temp.cmp
Binary files differ
diff --git a/win32/droplet/temp.rep b/win32/droplet/temp.rep
new file mode 100644
index 0000000..a2aaee4
--- /dev/null
+++ b/win32/droplet/temp.rep
@@ -0,0 +1,24 @@
+#
+# AUTHOR
+# N. Nielsen
+#
+# LICENSE
+# This software is in the public domain.
+#
+# The software is provided "as is", without warranty of any kind,
+# express or implied, including but not limited to the warranties
+# of merchantability, fitness for a particular purpose, and
+# noninfringement. In no event shall the author(s) be liable for any
+# claim, damages, or other liability, whether in an action of
+# contract, tort, or otherwise, arising from, out of, or in connection
+# with the software or the use or other dealings in the software.
+#
+# SUPPORT
+# Send bug reports to: <nielsen@memberwebs.com>
+#
+
+# This is a placeholder script for the one written
+# into the droplet
+
+match "yada"
+ replace "blah" \ No newline at end of file
diff --git a/win32/makedrop/MSG00001.bin b/win32/makedrop/MSG00001.bin
new file mode 100644
index 0000000..83bcd64
--- /dev/null
+++ b/win32/makedrop/MSG00001.bin
Binary files differ
diff --git a/win32/makedrop/Makefile.am b/win32/makedrop/Makefile.am
new file mode 100644
index 0000000..f79b2b5
--- /dev/null
+++ b/win32/makedrop/Makefile.am
@@ -0,0 +1,2 @@
+EXTRA_DIST = dropsheet.cpp dropsheet.h makedrop.cpp makedrop.dsp makedrop.h makedrop.rc processpage.h processpage.cpp resource.h settingspage.cpp settingspage.h stdafx.h stdafx.cpp
+
diff --git a/win32/makedrop/dropsheet.cpp b/win32/makedrop/dropsheet.cpp
new file mode 100644
index 0000000..04801b4
--- /dev/null
+++ b/win32/makedrop/dropsheet.cpp
@@ -0,0 +1,301 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include "stdafx.h"
+#include "DropSheet.h"
+#include "common/errutil.h"
+
+
+// Error Macros: ----------------------------------------------------------
+// Context specific macros. Each one jumps to 'cleanup' so any wrapping
+// up can be performed
+
+#define RETURN(v) \
+ { ret = (v); goto cleanup; }
+#define WINERR(f) \
+ RETURN(errorMessage(m_hWnd, HRESULT_FROM_WIN32(::GetLastError()), f))
+#define WINERR_1(f, a) \
+ RETURN(errorMessage(m_hWnd, HRESULT_FROM_WIN32(::GetLastError()), f, a))
+#define WINERR_E(e, f) \
+ RETURN(errorMessage(m_hWnd, e, f));
+#define REPERR(c, s) \
+ RETURN(rlibError(m_hWnd, c, s));
+
+
+
+// (Con|De)struction: -------------------------------------------------------
+
+DropSheet::DropSheet() :
+ CPropertySheet(_T("Rep Droplet"))
+{
+ m_dirty = false;
+}
+
+DropSheet::~DropSheet()
+{
+
+}
+
+
+
+// CopyResourceToFile: -----------------------------------------------------
+// Dumps raw data from a resource to a file
+
+bool CopyResourceToFile(LPCTSTR type, LPCTSTR name, LPCTSTR fileName)
+{
+ // Open the Resource
+ HRSRC hRsrc = FindResource(_Module.GetResourceInstance(), name, type);
+ if(!hRsrc) return false;
+
+ HGLOBAL hGlobal = LoadResource(_Module.GetResourceInstance(), hRsrc);
+ if(!hRsrc) return false;
+
+ void* data = LockResource(hGlobal);
+ if(!data) return false;
+
+ // Open the file
+ HANDLE file = CreateFile(fileName, GENERIC_WRITE, 0, NULL,
+ CREATE_ALWAYS, 0, NULL);
+ if(file == INVALID_HANDLE_VALUE) return false;
+
+ DWORD size = SizeofResource(_Module.GetResourceInstance(), hRsrc);
+
+ // Write it
+ DWORD written = 0;
+ bool ret = WriteFile(file, data, size, &written, NULL) ? true : false;
+
+ if(written != size)
+ ret = false;
+
+ CloseHandle(file);
+
+ return ret;
+}
+
+
+
+// saveDroplet: -------------------------------------------------------------
+// Gets property pages to save data, prompts user, saves etc...
+
+bool DropSheet::saveDroplet(bool force)
+{
+ HRESULT ret = S_OK;
+ bool noReturn = false;
+
+ {
+ // The return value is whether or not an exit can
+ // take place
+
+ if(!m_dirty && !force)
+ return true;
+
+ // Prompt the user and see if they want to save
+ if(m_dirty && !force)
+ {
+ switch(MessageBox(_T("You've made changes, would you like to save?"), _T("Rep Droplet"),
+ MB_YESNOCANCEL | MB_ICONQUESTION))
+ {
+ case IDYES:
+ break;
+ case IDNO:
+ return true;
+ case IDCANCEL:
+ return false;
+ }
+ }
+
+ // Clear the dirty flag here
+ m_dirty = false;
+
+ // Now get every page to apply theirs again
+ // if necessary
+ PressButton(PSBTN_APPLYNOW);
+
+ // If dirty is set now, then something failed
+ if(m_dirty)
+ return false;
+
+ // If the user hasn't saved/loaded yet then get a file name
+ if(m_fileName.empty())
+ {
+ OPENFILENAME ofn;
+ memset(&ofn, 0, sizeof(ofn));
+
+ ofn.lStructSize = sizeof(OPENFILENAME);
+ ofn.hwndOwner = m_hWnd;
+ ofn.lpstrFilter = _T("Droplet Files (*.exe)\0*.exe\0All Files (*.*)\0*.*\0\0");
+ ofn.lpstrFile = m_fileName.get_buffer(MAX_PATH);
+ ofn.nMaxFile = MAX_PATH;
+ ofn.lpstrFileTitle = _T("Save Droplet");
+ ofn.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
+ ofn.lpstrDefExt = _T("exe");
+
+ if(GetSaveFileName(&ofn))
+ m_fileName.release_buffer();
+ else
+ return false;
+ }
+
+ // Okay now we copy out the droplet from our resources
+ if(!CopyResourceToFile(DROP_RESOURCE_TYPE, DROP_RESOURCE_FILE,
+ m_fileName))
+ WINERR_1("Couldn't write out droplet: %s", m_fileName.c_str());
+
+ noReturn = true;
+
+ // Now save our droplet data into the stock droplet
+ if(!m_droplet.save(m_fileName))
+ WINERR_1("Couldn't write out droplet: %s", m_fileName.c_str());
+ }
+
+cleanup:
+ // If failed and we already wrote out the stock
+ // droplet, then delete it
+ if(FAILED(ret) && noReturn &&
+ !m_fileName.empty())
+ DeleteFile(m_fileName);
+
+ return SUCCEEDED(ret);
+}
+
+
+// openDroplet: --------------------------------------------------------
+// Prompt user for file, open it and refresh property pages
+
+void DropSheet::openDroplet()
+{
+ HRESULT ret = S_OK;
+
+ {
+ string file;
+ OPENFILENAME ofn;
+ memset(&ofn, 0, sizeof(ofn));
+
+ ofn.lStructSize = sizeof(OPENFILENAME);
+ ofn.hwndOwner = m_hWnd;
+ ofn.lpstrFilter = _T("Droplet Files\0*.exe\0All Files\0*.*\0\0");
+ ofn.lpstrFile = file.get_buffer(MAX_PATH);
+ ofn.nMaxFile = MAX_PATH;
+ ofn.lpstrFileTitle = _T("Choose Rep Script");
+ ofn.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_PATHMUSTEXIST;
+ ofn.lpstrDefExt = _T("rep");;
+
+ if(GetOpenFileName(&ofn))
+ {
+ file.release_buffer();
+
+ if(!m_droplet.load(file))
+ WINERR_1("Couldn't read droplet: %s", file.c_str());
+
+ m_fileName = file;
+
+ // Send around a message to the property pages to reload
+ SendMessage(PSM_QUERYSIBLINGS);
+ }
+ }
+
+cleanup:
+ ;
+}
+
+
+// onInitDialog: ----------------------------------------------------------
+// Initialize basic Property sheet stuff and pass on message
+
+LRESULT DropSheet::onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ // Add the menu
+ HMENU hMenu = LoadMenu(_Module.GetResourceInstance(),
+ MAKEINTRESOURCE(IDR_MENU));
+ SetMenu(hMenu);
+
+ SetIcon(::LoadIcon(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_REP)), TRUE);
+ SetIcon((HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_REP), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR), FALSE);
+
+
+ // Base implementations need this message
+ bHandled = FALSE;
+ return 0;
+}
+
+
+// onClose: -----------------------------------------------------------------
+// Check if save is required, close
+
+LRESULT DropSheet::onClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ // Make sure user saves first
+ if(!m_dirty || saveDroplet(false))
+ {
+ // Modeless dialog, so this is right
+ PostQuitMessage(0);
+ bHandled = FALSE;
+ }
+ else
+ bHandled = TRUE;
+
+ return 0;
+}
+
+
+// onChanged: ---------------------------------------------------------------
+// Catch message sent by property pages, so we can update our dirty flag
+
+LRESULT DropSheet::onChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ m_dirty = true;
+ bHandled = FALSE;
+ return 0;
+}
+
+
+// onOpen: ------------------------------------------------------------------
+// "Open" menu or keyboard accel
+
+LRESULT DropSheet::onOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ // Make sure user saves first
+ if(!m_dirty || saveDroplet(false))
+ {
+ openDroplet();
+ }
+
+ return 0;
+}
+
+
+// onSave: ------------------------------------------------------------------
+// "Save" menu or keyboard accel
+
+LRESULT DropSheet::onSave(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ // Force a save
+ saveDroplet(true);
+ return 0;
+}
+
+
+// onExit: ------------------------------------------------------------------
+// "Exit" menu or keyboard accel
+
+LRESULT DropSheet::onExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ onClose(WM_CLOSE, 0, 0, bHandled);
+ return 0;
+}
diff --git a/win32/makedrop/dropsheet.h b/win32/makedrop/dropsheet.h
new file mode 100644
index 0000000..48ec758
--- /dev/null
+++ b/win32/makedrop/dropsheet.h
@@ -0,0 +1,72 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __DROPSHEET_H__
+#define __DROPSHEET_H__
+
+#include "resource.h"
+#include "common/droplet.h"
+
+// DropSheet: ------------------------------------------------------
+// The main window property sheet
+
+class DropSheet
+ : public CPropertySheet
+{
+public:
+ DropSheet();
+ virtual ~DropSheet();
+
+ BEGIN_MSG_MAP(DropSheet)
+ MESSAGE_HANDLER(WM_INITDIALOG, onInitDialog)
+ COMMAND_ID_HANDLER(ID_FILE_OPEN, onOpen)
+ COMMAND_ID_HANDLER(ID_FILE_SAVE, onSave)
+ COMMAND_ID_HANDLER(ID_FILE_EXIT, onExit)
+ MESSAGE_HANDLER(PSM_CHANGED, onChanged)
+ MESSAGE_HANDLER(WM_CLOSE, onClose)
+ CHAIN_MSG_MAP(CPropertySheet)
+ END_MSG_MAP()
+
+ // Get the current internal droplet
+ Droplet& getDroplet()
+ { return m_droplet; }
+
+// Message Handlers
+protected:
+ LRESULT onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+ LRESULT onSave(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+ LRESULT onExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+
+// Helper Functions
+protected:
+ // Save the droplet, prompt user if not force
+ bool saveDroplet(bool force);
+ // Open the a droplet
+ void openDroplet();
+
+protected:
+ Droplet m_droplet; // The internal loaded droplet
+ bool m_dirty; // Have changes been made?
+ string m_fileName; // The filename to save to
+};
+
+#endif // !defined(AFX_DROPSHEET_H__E3237E90_1FD0_4233_98EE_5991F4FB39F8__INCLUDED_)
diff --git a/win32/makedrop/makedrop.cpp b/win32/makedrop/makedrop.cpp
new file mode 100644
index 0000000..79a5b19
--- /dev/null
+++ b/win32/makedrop/makedrop.cpp
@@ -0,0 +1,74 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include "stdafx.h"
+#include "resource.h"
+#include <initguid.h>
+#include "DropSheet.h"
+#include "ProcessPage.h"
+#include "SettingsPage.h"
+
+// ATL Support stuff
+CComModule _Module;
+BEGIN_OBJECT_MAP(ObjectMap)
+END_OBJECT_MAP()
+
+
+// _tWinMain: ------------------------------------------------------------
+// Our application entry point
+extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
+ HINSTANCE /*hPrevInstance*/, LPTSTR /* lpCmdLine */, int /*nShowCmd*/)
+{
+ // ATL Support Stuff
+ HRESULT hRes = CoInitialize(NULL);
+ _ASSERTE(SUCCEEDED(hRes));
+ _Module.Init(ObjectMap, hInstance, NULL);
+
+ int nRet = 0;
+
+ // Create and start the property sheet
+ DropSheet sheet;
+ ProcessPage page1(sheet.getDroplet());
+ SettingsPage page2(sheet.getDroplet());
+ sheet.AddPage(&page1);
+ sheet.AddPage(&page2);
+ sheet.Create();
+
+ // Load the keyboard shortcuts
+ HACCEL accel = LoadAccelerators(_Module.GetResourceInstance(),
+ MAKEINTRESOURCE(IDR_ACCEL));
+
+ // Process messages
+ MSG msg;
+ while(GetMessage(&msg, NULL, 0, 0))
+ {
+ if(!accel || !TranslateAccelerator(sheet.m_hWnd, accel, &msg))
+ {
+ if(!sheet.IsWindow() || !sheet.IsDialogMessage(&msg))
+ {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+ }
+ }
+
+ _Module.Term();
+ CoUninitialize();
+ return nRet;
+}
diff --git a/win32/makedrop/makedrop.dsp b/win32/makedrop/makedrop.dsp
new file mode 100644
index 0000000..af603d3
--- /dev/null
+++ b/win32/makedrop/makedrop.dsp
@@ -0,0 +1,203 @@
+# Microsoft Developer Studio Project File - Name="makedrop" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Application" 0x0101
+
+CFG=makedrop - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "makedrop.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "makedrop.mak" CFG="makedrop - Win32 Debug"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "makedrop - Win32 Debug" (based on "Win32 (x86) Application")
+!MESSAGE "makedrop - Win32 Release" (based on "Win32 (x86) Application")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+MTL=midl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "makedrop - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "debug"
+# PROP Intermediate_Dir "debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /Gm /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c
+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "..\.." /I ".." /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 libpcre.a comctl32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /base:"0x400000" /subsystem:windows /debug /machine:I386 /out:"../debug/makedrop.exe" /pdbtype:sept /libpath:"..\pcre\lib\\"
+
+!ELSEIF "$(CFG)" == "makedrop - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "release"
+# PROP Intermediate_Dir "release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /O1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_ATL_DLL" /D "_ATL_MIN_CRT" /Yu"stdafx.h" /FD /c
+# ADD CPP /nologo /W3 /GX /O1 /I "..\.." /I ".." /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /Yu"stdafx.h" /FD /c
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /machine:I386
+# ADD LINK32 libpcre.a comctl32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /base:"0x400000" /subsystem:windows /machine:I386 /out:"../release/makedrop.exe" /libpath:"..\pcre\lib\\" /opt:nowin98
+# SUBTRACT LINK32 /pdb:none
+
+!ENDIF
+
+# Begin Target
+
+# Name "makedrop - Win32 Debug"
+# Name "makedrop - Win32 Release"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=..\common\droplet.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\DropSheet.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\errutil.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\makedrop.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\makedrop.rc
+# End Source File
+# Begin Source File
+
+SOURCE=.\ProcessPage.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\rliberr.mc
+
+!IF "$(CFG)" == "makedrop - Win32 Debug"
+
+# PROP Ignore_Default_Tool 1
+# Begin Custom Build
+InputPath=..\common\rliberr.mc
+InputName=rliberr
+
+"$(InputName).rc" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
+ mc $(InputPath)
+
+# End Custom Build
+
+!ELSEIF "$(CFG)" == "makedrop - Win32 Release"
+
+# PROP Ignore_Default_Tool 1
+
+!ENDIF
+
+# End Source File
+# Begin Source File
+
+SOURCE=.\SettingsPage.cpp
+# End Source File
+# Begin Source File
+
+SOURCE=.\StdAfx.cpp
+# ADD CPP /Yc"stdafx.h"
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=..\common\droplet.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\DropSheet.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\ProcessPage.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\Resource.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\SettingsPage.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\StdAfx.h
+# End Source File
+# End Group
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# Begin Source File
+
+SOURCE=..\debug\droplet.exe
+# End Source File
+# Begin Source File
+
+SOURCE=.\droplet.exe
+# End Source File
+# Begin Source File
+
+SOURCE=..\release\droplet.exe
+# End Source File
+# Begin Source File
+
+SOURCE=.\MSG00001.bin
+# End Source File
+# Begin Source File
+
+SOURCE=..\common\rep.ico
+# End Source File
+# End Group
+# End Target
+# End Project
+# Section makedrop : {00000000-0000-0000-0000-800000800000}
+# 1:14:IDD_DROPLETDLG:101
+# End Section
diff --git a/win32/makedrop/makedrop.h b/win32/makedrop/makedrop.h
new file mode 100644
index 0000000..1ef92a3
--- /dev/null
+++ b/win32/makedrop/makedrop.h
@@ -0,0 +1 @@
+/* MIDL: this ALWAYS GENERATED file contains the definitions for the interfaces */
diff --git a/win32/makedrop/makedrop.rc b/win32/makedrop/makedrop.rc
new file mode 100644
index 0000000..0b61072
--- /dev/null
+++ b/win32/makedrop/makedrop.rc
@@ -0,0 +1,275 @@
+//Microsoft Developer Studio generated resource script.
+//
+#include "resource.h"
+
+#define APSTUDIO_READONLY_SYMBOLS
+/////////////////////////////////////////////////////////////////////////////
+//
+// Generated from the TEXTINCLUDE 2 resource.
+//
+#include "winres.h"
+
+/////////////////////////////////////////////////////////////////////////////
+#undef APSTUDIO_READONLY_SYMBOLS
+
+/////////////////////////////////////////////////////////////////////////////
+// Neutral resources
+
+#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_NEU)
+#ifdef _WIN32
+LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
+#pragma code_page(1252)
+#endif //_WIN32
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// REP
+//
+
+#if defined(APSTUDIO_INVOKED) || defined(NDEBUG)
+#if defined(APSTUDIO_INVOKED)
+DROPLET$(NDEBUG) REP DISCARDABLE "..\\release\\droplet.exe"
+#else
+DROPLET REP DISCARDABLE "..\\release\\droplet.exe"
+#endif
+#endif
+#if defined(APSTUDIO_INVOKED) || defined(_DEBUG)
+#if defined(APSTUDIO_INVOKED)
+DROPLET$(_DEBUG) REP DISCARDABLE "..\\debug\\droplet.exe"
+#else
+DROPLET REP DISCARDABLE "..\\debug\\droplet.exe"
+#endif
+#endif
+#endif // Neutral resources
+/////////////////////////////////////////////////////////////////////////////
+
+
+/////////////////////////////////////////////////////////////////////////////
+// English (U.S.) resources
+
+#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
+#ifdef _WIN32
+LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
+#pragma code_page(1252)
+#endif //_WIN32
+
+#ifdef APSTUDIO_INVOKED
+/////////////////////////////////////////////////////////////////////////////
+//
+// TEXTINCLUDE
+//
+
+1 TEXTINCLUDE DISCARDABLE
+BEGIN
+ "resource.h\0"
+END
+
+2 TEXTINCLUDE DISCARDABLE
+BEGIN
+ "#include ""winres.h""\r\n"
+ "\0"
+END
+
+3 TEXTINCLUDE DISCARDABLE
+BEGIN
+ "\r\n"
+ "\0"
+END
+
+#endif // APSTUDIO_INVOKED
+
+
+#ifndef _MAC
+/////////////////////////////////////////////////////////////////////////////
+//
+// Version
+//
+
+VS_VERSION_INFO VERSIONINFO
+ FILEVERSION 2,2,0,1
+ PRODUCTVERSION 2,2,0,1
+ FILEFLAGSMASK 0x3fL
+#ifdef _DEBUG
+ FILEFLAGS 0x1L
+#else
+ FILEFLAGS 0x0L
+#endif
+ FILEOS 0x4L
+ FILETYPE 0x1L
+ FILESUBTYPE 0x0L
+BEGIN
+ BLOCK "StringFileInfo"
+ BEGIN
+ BLOCK "040904b0"
+ BEGIN
+ VALUE "Comments", "\0"
+ VALUE "CompanyName", "Nate Nielsen\0"
+ VALUE "FileDescription", "Rep Droplet Maker\0"
+ VALUE "FileVersion", "2, 2, 0, 1\0"
+ VALUE "InternalName", "makedrop\0"
+ VALUE "LegalCopyright", "Copyright Nate Nielsen 2002\0"
+ VALUE "LegalTrademarks", "\0"
+ VALUE "OLESelfRegister", "\0"
+ VALUE "OriginalFilename", "makedrop.exe\0"
+ VALUE "PrivateBuild", "\0"
+ VALUE "ProductName", "rep\0"
+ VALUE "ProductVersion", "2, 2, 0, 1\0"
+ VALUE "SpecialBuild", "\0"
+ END
+ END
+ BLOCK "VarFileInfo"
+ BEGIN
+ VALUE "Translation", 0x409, 1200
+ END
+END
+
+#endif // !_MAC
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Dialog
+//
+
+IDD_SETTINGS DIALOG DISCARDABLE 0, 0, 227, 210
+STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
+CAPTION "Settings"
+FONT 8, "Tahoma"
+BEGIN
+ LTEXT "This title is shown in the progress dialog for this droplet.",
+ IDC_STATIC,17,19,180,8
+ EDITTEXT IDC_TITLE,17,32,192,14,ES_AUTOHSCROLL
+ CONTROL "Backup replaced files.",IDC_BACKUP,"Button",
+ BS_AUTOCHECKBOX | WS_TABSTOP,17,75,167,10
+ GROUPBOX "Backups",IDC_STATIC,7,61,213,75
+ LTEXT "If this option is checked, then this droplet will backup any files it modifies. It adds a 'x_r' extension to those files.\n\nWhen a backup already exists it is overwritten.",
+ IDC_STATIC,17,91,192,32
+ GROUPBOX "Title",IDC_STATIC,7,7,213,48
+END
+
+IDD_PROCESS DIALOG DISCARDABLE 0, 0, 227, 210
+STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
+CAPTION "Process"
+FONT 8, "Tahoma"
+BEGIN
+ CONTROL "Process entire file at once",IDC_USEFILE,"Button",
+ BS_AUTORADIOBUTTON,18,79,158,10
+ CONTROL "Process file in chunks",IDC_USECHUNK,"Button",
+ BS_AUTORADIOBUTTON,18,126,140,10
+ LTEXT "Largest match size:",IDC_STATIC,35,183,63,8
+ EDITTEXT IDC_CHUNK,104,182,57,14,ES_AUTOHSCROLL | ES_NUMBER
+ GROUPBOX "Chunks",IDC_STATIC,7,65,213,138
+ LTEXT "This runs replaces on all of the file at once. Although the most reliable method, it's slower, sometimes extremely so. ",
+ IDC_STATIC,18,94,191,22
+ LTEXT "This option breaks up the file into smaller blocks for processing. Processing is much faster in this mode. The largest expected match should be entered below; anything larger won't be replaced.",
+ IDC_STATIC,18,140,191,34
+ GROUPBOX "Script",IDC_STATIC,7,7,213,49
+ EDITTEXT IDC_SCRIPT,18,33,170,14,ES_AUTOHSCROLL
+ PUSHBUTTON "...",IDC_BROWSE,190,33,19,14
+ LTEXT "The rep script that will be compiled into the droplet. ",
+ IDC_STATIC,18,19,191,12
+END
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// DESIGNINFO
+//
+
+#ifdef APSTUDIO_INVOKED
+GUIDELINES DESIGNINFO DISCARDABLE
+BEGIN
+ IDD_SETTINGS, DIALOG
+ BEGIN
+ LEFTMARGIN, 7
+ RIGHTMARGIN, 220
+ VERTGUIDE, 17
+ VERTGUIDE, 209
+ TOPMARGIN, 7
+ BOTTOMMARGIN, 203
+ END
+
+ IDD_PROCESS, DIALOG
+ BEGIN
+ LEFTMARGIN, 7
+ RIGHTMARGIN, 220
+ VERTGUIDE, 18
+ VERTGUIDE, 209
+ TOPMARGIN, 7
+ BOTTOMMARGIN, 203
+ END
+END
+#endif // APSTUDIO_INVOKED
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Menu
+//
+
+IDR_MENU MENU DISCARDABLE
+BEGIN
+ POPUP "&File"
+ BEGIN
+ MENUITEM "&Open...\tCtrl+O", ID_FILE_OPEN
+ MENUITEM "&Save...\tCtrl+S", ID_FILE_SAVE
+ MENUITEM SEPARATOR
+ MENUITEM "E&xit\tCtrl+Q", ID_FILE_EXIT
+ END
+END
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Accelerator
+//
+
+IDR_ACCEL ACCELERATORS DISCARDABLE
+BEGIN
+ "O", ID_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT
+ "Q", ID_FILE_EXIT, VIRTKEY, CONTROL, NOINVERT
+ "S", ID_FILE_SAVE, VIRTKEY, CONTROL, NOINVERT
+END
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Icon
+//
+
+// Icon with lowest ID value placed first to ensure application icon
+// remains consistent on all systems.
+IDI_REP ICON DISCARDABLE "..\\common\\rep.ico"
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// 11
+//
+
+1 11 DISCARDABLE "MSG00001.bin"
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// String Table
+//
+
+STRINGTABLE DISCARDABLE
+BEGIN
+ IDS_PROJNAME "makedrop"
+END
+
+#endif // English (U.S.) resources
+/////////////////////////////////////////////////////////////////////////////
+
+
+
+#ifndef APSTUDIO_INVOKED
+/////////////////////////////////////////////////////////////////////////////
+//
+// Generated from the TEXTINCLUDE 3 resource.
+//
+
+
+/////////////////////////////////////////////////////////////////////////////
+#endif // not APSTUDIO_INVOKED
+
diff --git a/win32/makedrop/processpage.cpp b/win32/makedrop/processpage.cpp
new file mode 100644
index 0000000..be3951a
--- /dev/null
+++ b/win32/makedrop/processpage.cpp
@@ -0,0 +1,239 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include "stdafx.h"
+#include "ProcessPage.h"
+#include "common/errutil.h"
+#include "lib/rlib.h"
+#include "lib/rep.h"
+
+
+// Error Macros: ----------------------------------------------------------
+// Context specific macros. Each one jumps to 'cleanup' so any wrapping
+// up can be performed
+
+#define RETURN(v) \
+ { ret = (v); goto cleanup; }
+#define WINERR(f) \
+ RETURN(errorMessage(m_hWnd, HRESULT_FROM_WIN32(::GetLastError()), f))
+#define WINERR_1(f, a) \
+ RETURN(errorMessage(m_hWnd, HRESULT_FROM_WIN32(::GetLastError()), f, a))
+#define WINERR_E(e, f) \
+ RETURN(errorMessage(m_hWnd, e, f));
+#define REPERR(c, s) \
+ RETURN(rlibError(m_hWnd, c, s));
+#define CUSTERR(e, s) \
+ { errorMessage(m_hWnd, 0, s); RETURN(e); }
+#define CUSTERR_1(e, s, a) \
+ { errorMessage(m_hWnd, 0, s, a); RETURN(e); }
+
+
+// (Con|De)struction: ---------------------------------------------------------
+
+ProcessPage::ProcessPage(Droplet& droplet) :
+ CPropertyPage(IDD_PROCESS, _T("Script")),
+ m_droplet(droplet)
+{
+ m_inited = false;
+}
+
+// compileScript: ----------------------------------------------------------
+// Load and possibly compile a script from a file
+
+HRESULT ProcessPage::compileScript(const string& script)
+{
+ FILE* file = NULL;
+ HRESULT ret = S_OK;
+ int r = R_OK;
+
+ {
+ file = fopen(script, "rb");
+ if(!file)
+ CUSTERR_1(E_FAIL, "Couldn't open script file: %s", script.c_str());
+
+ r_context& ctx = m_droplet.getContext();
+
+ r = repLoad(&ctx, file);
+ if(r < 0)
+ REPERR(r, &(ctx.script));
+ }
+
+cleanup:
+ if(file)
+ fclose(file);
+
+ return ret;
+}
+
+
+// loadData: -------------------------------------------------------------
+// Load data from the droplet onto page
+
+void ProcessPage::loadData()
+{
+ m_inited = false;
+ size_t buffSize = m_droplet.getBuffSize();
+ CheckDlgButton(IDC_USEFILE, buffSize == 0);
+ CheckDlgButton(IDC_USECHUNK, buffSize != 0);
+ SetDlgItemInt(IDC_CHUNK, buffSize == 0 ? 2000 : buffSize, FALSE);
+
+ if(m_droplet.hasScript())
+ {
+ // By setting both to the same value we prevent rereading
+ // or compiling
+ m_script = _T("[compiled script]");
+ SetDlgItemText(IDC_SCRIPT, m_script);
+ }
+
+ m_inited = true;
+ updateControls();
+}
+
+
+// onInitDialog: -----------------------------------------------------------
+// Dialog initialization
+
+LRESULT ProcessPage::onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ loadData();
+ return 0;
+}
+
+
+// onInitDialog: -----------------------------------------------------------
+// Broadcast by sheet when required to reread droplet info
+
+LRESULT ProcessPage::onQuerySiblings(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ loadData();
+ return 0;
+}
+
+
+// onInitDialog: -----------------------------------------------------------
+// Save droplet data from page
+
+LRESULT ProcessPage::onApply(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
+{
+ string script;
+ GetDlgItemText(IDC_SCRIPT, script.get_buffer(MAX_PATH), MAX_PATH);
+ script.release_buffer();
+
+ // Make sure we have a script
+ if(script.empty())
+ {
+ errorMessage(m_hWnd, 0, _T("You need to provide the filename of a rep script."));
+ ::SetFocus(GetDlgItem(IDC_SCRIPT));
+ SetModified(TRUE);
+ return PSNRET_INVALID;
+ }
+
+ // If the the script changed
+ if(script != m_script)
+ {
+ // Then recompile it
+ if(SUCCEEDED(compileScript(script)))
+ {
+ m_script = script;
+ }
+ else
+ {
+ // By setting dirty we signal an error to sheet
+ SetModified(TRUE);
+ return PSNRET_INVALID;
+ }
+ }
+
+ // Do buffer thing
+ if(IsDlgButtonChecked(IDC_USEFILE))
+ {
+ // Process entire file at once
+ m_droplet.setBuffSize(0);
+ }
+ else
+ {
+ // Get the actual buffer size
+ BOOL translated;
+ UINT buffSize = GetDlgItemInt(IDC_CHUNK, &translated, FALSE);
+ if(buffSize < 16 || !translated)
+ {
+ errorMessage(m_hWnd, 0, _T("The chunk size must be a valid number greater than 16."));
+ ::SetFocus(GetDlgItem(IDC_CHUNK));
+ SetModified(TRUE);
+ return PSNRET_INVALID;
+ }
+
+ m_droplet.setBuffSize(buffSize);
+ }
+
+ return PSNRET_NOERROR;
+}
+
+
+// onChange: --------------------------------------------------------------
+// Signal dirty to parent sheet
+
+LRESULT ProcessPage::onChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ if(m_inited)
+ {
+ SetModified(TRUE);
+ updateControls();
+ }
+ return 0;
+}
+
+
+// updateControls: --------------------------------------------------------
+// Make sure page remains valid when changes happen
+
+void ProcessPage::updateControls()
+{
+ ::EnableWindow(GetDlgItem(IDC_CHUNK), IsDlgButtonChecked(IDC_USECHUNK));
+}
+
+
+// onBrowse: --------------------------------------------------------------
+// Browse for a script file
+
+LRESULT ProcessPage::onBrowse(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ string file;
+ OPENFILENAME ofn;
+ memset(&ofn, 0, sizeof(ofn));
+
+ ofn.lStructSize = sizeof(OPENFILENAME);
+ ofn.hwndOwner = m_hWnd;
+ ofn.lpstrFilter = _T("Rep Files (*.rep)\0*.rep\0All Files (*.*)\0*.*\0\0");
+ ofn.lpstrFile = file.get_buffer(MAX_PATH);
+ ofn.nMaxFile = MAX_PATH;
+ ofn.lpstrFileTitle = _T("Choose Rep Script");
+ ofn.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_PATHMUSTEXIST;
+ ofn.lpstrDefExt = _T("rep");;
+
+ if(GetOpenFileName(&ofn))
+ {
+ file.release_buffer();
+ SetDlgItemText(IDC_SCRIPT, file);
+ SetModified(TRUE);
+ updateControls();
+ }
+
+ return 0;
+}
diff --git a/win32/makedrop/processpage.h b/win32/makedrop/processpage.h
new file mode 100644
index 0000000..74ad6de
--- /dev/null
+++ b/win32/makedrop/processpage.h
@@ -0,0 +1,75 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __PROCESSPAGE_H__
+#define __PROCESSPAGE_H__
+
+#include "resource.h" // main symbols
+#include <atlhost.h>
+#include "common/droplet.h"
+
+
+// ProcessPage: -------------------------------------------------------
+// Property page for script and buffer size
+
+class ProcessPage :
+ public CPropertyPage
+{
+public:
+ ProcessPage(Droplet& droplet);
+
+ BEGIN_MSG_MAP(ProcessPage)
+ MESSAGE_HANDLER(WM_INITDIALOG, onInitDialog)
+ MESSAGE_HANDLER(PSM_QUERYSIBLINGS, onQuerySiblings)
+ COMMAND_ID_HANDLER(IDC_BROWSE, onBrowse)
+ COMMAND_CODE_HANDLER(BN_CLICKED, onChange)
+ COMMAND_CODE_HANDLER(EN_CHANGE, onChange)
+ NOTIFY_CODE_HANDLER(PSN_APPLY, onApply)
+ END_MSG_MAP()
+
+// Message Handlers
+protected:
+ LRESULT onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onQuerySiblings(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onApply(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
+ LRESULT onChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+ LRESULT onBrowse(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+
+// Helper Functions
+protected:
+ // Updates all status etc...
+ void updateControls();
+
+ // Load data from droplet to page
+ void loadData();
+
+ // Load an already compiled rep script
+ HRESULT compileAlready(FILE* f);
+
+ // Load and possibly compile a rep script
+ HRESULT compileScript(const string& script);
+
+protected:
+ Droplet& m_droplet; // Pointer to sheet's main droplet
+ string m_script; // The script file name last time it was saved
+ // Will not reload if the same
+ bool m_inited; // Have we finished being initialized (used by onChange)
+};
+
+#endif //__PROCESSPAGE_H__
diff --git a/win32/makedrop/resource.h b/win32/makedrop/resource.h
new file mode 100644
index 0000000..4783aae
--- /dev/null
+++ b/win32/makedrop/resource.h
@@ -0,0 +1,32 @@
+//{{NO_DEPENDENCIES}}
+// Microsoft Developer Studio generated include file.
+// Used by makedrop.rc
+//
+#define IDS_PROJNAME 100
+#define IDR_Makedrop 100
+#define IDC_TITLE 201
+#define IDR_MENU 201
+#define IDD_SETTINGS 202
+#define IDC_BACKUP 202
+#define IDR_ACCEL 202
+#define IDC_USEFILE 203
+#define IDD_PROCESS 203
+#define IDC_USECHUNK 204
+#define IDC_CHUNK 205
+#define IDC_SCRIPT 206
+#define IDC_BROWSE 207
+#define IDI_REP 208
+#define ID_FILE_OPEN 32768
+#define ID_FILE_SAVE 32769
+#define ID_FILE_EXIT 32770
+
+// Next default values for new objects
+//
+#ifdef APSTUDIO_INVOKED
+#ifndef APSTUDIO_READONLY_SYMBOLS
+#define _APS_NEXT_RESOURCE_VALUE 210
+#define _APS_NEXT_COMMAND_VALUE 32771
+#define _APS_NEXT_CONTROL_VALUE 208
+#define _APS_NEXT_SYMED_VALUE 102
+#endif
+#endif
diff --git a/win32/makedrop/rliberr.h b/win32/makedrop/rliberr.h
new file mode 100644
index 0000000..8fe3a80
--- /dev/null
+++ b/win32/makedrop/rliberr.h
@@ -0,0 +1,136 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * VERSION
+ * 2.2.0b
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef _RLIBERR_H_
+#define _RLIBERR_H_
+
+#ifndef _WINERROR_
+ #error Include winerror.h first.
+#endif
+
+#define HRESULT_FROM_RLIB(code) \
+ MAKE_HRESULT(SEVERITY_ERROR, FACILITY_RLIB, abs(code))
+
+/* ------------------------------------------------------------------------ *\
+ Rlib Errors
+\* ------------------------------------------------------------------------ */
+//
+// Values are 32 bit values layed out as follows:
+//
+// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
+// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+// +---+-+-+-----------------------+-------------------------------+
+// |Sev|C|R| Facility | Code |
+// +---+-+-+-----------------------+-------------------------------+
+//
+// where
+//
+// Sev - is the severity code
+//
+// 00 - Success
+// 01 - Informational
+// 10 - Warning
+// 11 - Error
+//
+// C - is the Customer code flag
+//
+// R - is a reserved bit
+//
+// Facility - is the facility code
+//
+// Code - is the facility's status code
+//
+//
+// Define the facility codes
+//
+#define FACILITY_RLIB 0x196
+
+
+//
+// Define the severity codes
+//
+
+
+//
+// MessageId: RLIB_E_NOMEM
+//
+// MessageText:
+//
+// Out of Memory.
+//
+#define RLIB_E_NOMEM ((HRESULT)0x81960001L)
+
+//
+// MessageId: RLIB_E_SYNTAX
+//
+// MessageText:
+//
+// Rep script syntax error.
+//
+#define RLIB_E_SYNTAX ((HRESULT)0x81960002L)
+
+//
+// MessageId: RLIB_E_REGEXP
+//
+// MessageText:
+//
+// Regular expression syntax error.
+//
+#define RLIB_E_REGEXP ((HRESULT)0x81960003L)
+
+//
+// MessageId: RLIB_E_LOOP
+//
+// MessageText:
+//
+// Rep encountered an endless loop.
+//
+#define RLIB_E_LOOP ((HRESULT)0x81960004L)
+
+//
+// MessageId: RLIB_E_USER
+//
+// MessageText:
+//
+// User defined error.
+//
+#define RLIB_E_USER ((HRESULT)0x81960005L)
+
+//
+// MessageId: RLIB_E_IOERR
+//
+// MessageText:
+//
+// There was an error reading or writing the data.
+//
+#define RLIB_E_IOERR ((HRESULT)0x81960006L)
+
+//
+// MessageId: RLIB_E_INVARG
+//
+// MessageText:
+//
+// Programmer Error: Invalid argument.
+//
+#define RLIB_E_INVARG ((HRESULT)0x8196000AL)
+
+#endif // _RLIBERR_H_ \ No newline at end of file
diff --git a/win32/makedrop/rliberr.rc b/win32/makedrop/rliberr.rc
new file mode 100644
index 0000000..0885a89
--- /dev/null
+++ b/win32/makedrop/rliberr.rc
@@ -0,0 +1,2 @@
+LANGUAGE 0x9,0x1
+1 11 MSG00001.bin
diff --git a/win32/makedrop/settingspage.cpp b/win32/makedrop/settingspage.cpp
new file mode 100644
index 0000000..953eebd
--- /dev/null
+++ b/win32/makedrop/settingspage.cpp
@@ -0,0 +1,100 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include "stdafx.h"
+#include "SettingsPage.h"
+
+// (Con|De)struction: ----------------------------------------------------
+
+SettingsPage::SettingsPage(Droplet& droplet) :
+ CPropertyPage(IDD_SETTINGS, _T("Settings")),
+ m_droplet(droplet)
+{
+ m_inited = false;
+}
+
+
+// loadData: -------------------------------------------------------------
+// Load info from droplet onto page
+
+void SettingsPage::loadData()
+{
+ m_inited = false;
+ SetDlgItemText(IDC_TITLE, m_droplet.getTitle());
+ CheckDlgButton(IDC_BACKUP, m_droplet.keepBackups());
+ m_inited = true;
+ updateControls();
+}
+
+
+// onQuerySiblings: ------------------------------------------------------
+// Broadcast from parent sheet when we have to reload droplet data
+
+LRESULT SettingsPage::onQuerySiblings(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ loadData();
+ return 0;
+}
+
+
+// onInitDialog: ---------------------------------------------------------
+// Page Initialization
+
+LRESULT SettingsPage::onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
+{
+ loadData();
+ return 0;
+}
+
+
+// onApply: --------------------------------------------------------------
+// Save data to droplet
+
+LRESULT SettingsPage::onApply(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
+{
+ string title;
+ GetDlgItemText(IDC_TITLE, title.get_buffer(MAX_PATH), MAX_PATH);
+ title.release_buffer();
+ m_droplet.setTitle(title);
+
+ m_droplet.setBackups(IsDlgButtonChecked(IDC_BACKUP) ? true : false);
+ return PSNRET_NOERROR;
+}
+
+
+// onChange: -------------------------------------------------------------
+// Signal to parent when changes happen
+
+LRESULT SettingsPage::onChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
+{
+ if(m_inited)
+ {
+ SetModified(TRUE);
+ updateControls();
+ }
+ return 0;
+}
+
+// updateControls: -------------------------------------------------------
+// Keep page status in sync
+
+void SettingsPage::updateControls()
+{
+
+} \ No newline at end of file
diff --git a/win32/makedrop/settingspage.h b/win32/makedrop/settingspage.h
new file mode 100644
index 0000000..f122963
--- /dev/null
+++ b/win32/makedrop/settingspage.h
@@ -0,0 +1,65 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#ifndef __SETTINGSPAGE_H_
+#define __SETTINGSPAGE_H_
+
+#include "resource.h" // main symbols
+#include <atlhost.h>
+#include "common/droplet.h"
+
+// SettingsPage: ----------------------------------------------------------
+// Property page for misc drop settings
+
+class SettingsPage :
+ public CPropertyPage
+{
+public:
+ SettingsPage(Droplet& droplet);
+
+ BEGIN_MSG_MAP(SettingsPage)
+ MESSAGE_HANDLER(WM_INITDIALOG, onInitDialog)
+ MESSAGE_HANDLER(PSM_QUERYSIBLINGS, onQuerySiblings)
+ COMMAND_CODE_HANDLER(BN_CLICKED, onChange)
+ COMMAND_CODE_HANDLER(EN_CHANGE, onChange)
+ NOTIFY_CODE_HANDLER(PSN_APPLY, onApply)
+ END_MSG_MAP()
+
+// Message Handlers
+protected:
+ LRESULT onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onQuerySiblings(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
+ LRESULT onApply(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
+ LRESULT onChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
+
+// Helper Functions
+protected:
+ // Updates all status etc...
+ void updateControls();
+
+ // Load data from droplet to page
+ void loadData();
+
+// Data
+protected:
+ Droplet& m_droplet; // The Property sheet's internal droplet
+ bool m_inited; // Have we been initialized (used by onChange)
+};
+
+#endif //__SETTINGSPAGE_H_
diff --git a/win32/makedrop/stdafx.cpp b/win32/makedrop/stdafx.cpp
new file mode 100644
index 0000000..ba86b3c
--- /dev/null
+++ b/win32/makedrop/stdafx.cpp
@@ -0,0 +1,27 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include "stdafx.h"
+
+#ifdef _ATL_STATIC_REGISTRY
+#include <statreg.h>
+#include <statreg.cpp>
+#endif
+
+#include <atlimpl.cpp>
diff --git a/win32/makedrop/stdafx.h b/win32/makedrop/stdafx.h
new file mode 100644
index 0000000..2de0b77
--- /dev/null
+++ b/win32/makedrop/stdafx.h
@@ -0,0 +1,40 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#if !defined(AFX_STDAFX_H__7FCAFA12_CBDF_41CC_98D1_56D72C99E1C5__INCLUDED_)
+#define AFX_STDAFX_H__7FCAFA12_CBDF_41CC_98D1_56D72C99E1C5__INCLUDED_
+
+#define STRICT
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0400
+#endif
+#define _ATL_APARTMENT_THREADED
+
+#include <atlbase.h>
+extern CComModule _Module;
+#include <atlcom.h>
+#include <atlwin.h>
+
+#include "config.win32.h"
+#include "common/compat.h"
+#include "common/usuals.h"
+#include "common/atlprsht.h"
+#include "common/mystring.h"
+
+#endif // !defined(AFX_STDAFX_H__7FCAFA12_CBDF_41CC_98D1_56D72C99E1C5__INCLUDED)
diff --git a/win32/pcre/include/pcre.h b/win32/pcre/include/pcre.h
new file mode 100644
index 0000000..d6c2393
--- /dev/null
+++ b/win32/pcre/include/pcre.h
@@ -0,0 +1,113 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* Copyright (c) 1997-2001 University of Cambridge */
+
+#ifndef _PCRE_H
+#define _PCRE_H
+
+/* The file pcre.h is build by "configure". Do not edit it; instead
+make changes to pcre.in. */
+
+#define PCRE_MAJOR 3
+#define PCRE_MINOR 9
+#define PCRE_DATE 02-Jan-2002
+
+/* Win32 uses DLL by default */
+
+#ifdef _WIN32
+# ifdef STATIC
+# define PCRE_DL_IMPORT
+# else
+# define PCRE_DL_IMPORT __declspec(dllimport)
+# endif
+#else
+# define PCRE_DL_IMPORT
+#endif
+
+/* Have to include stdlib.h in order to ensure that size_t is defined;
+it is needed here for malloc. */
+
+#include <stdlib.h>
+
+/* Allow for C++ users */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Options */
+
+#define PCRE_CASELESS 0x0001
+#define PCRE_MULTILINE 0x0002
+#define PCRE_DOTALL 0x0004
+#define PCRE_EXTENDED 0x0008
+#define PCRE_ANCHORED 0x0010
+#define PCRE_DOLLAR_ENDONLY 0x0020
+#define PCRE_EXTRA 0x0040
+#define PCRE_NOTBOL 0x0080
+#define PCRE_NOTEOL 0x0100
+#define PCRE_UNGREEDY 0x0200
+#define PCRE_NOTEMPTY 0x0400
+#define PCRE_UTF8 0x0800
+
+/* Exec-time and get-time error codes */
+
+#define PCRE_ERROR_NOMATCH (-1)
+#define PCRE_ERROR_NULL (-2)
+#define PCRE_ERROR_BADOPTION (-3)
+#define PCRE_ERROR_BADMAGIC (-4)
+#define PCRE_ERROR_UNKNOWN_NODE (-5)
+#define PCRE_ERROR_NOMEMORY (-6)
+#define PCRE_ERROR_NOSUBSTRING (-7)
+
+/* Request types for pcre_fullinfo() */
+
+#define PCRE_INFO_OPTIONS 0
+#define PCRE_INFO_SIZE 1
+#define PCRE_INFO_CAPTURECOUNT 2
+#define PCRE_INFO_BACKREFMAX 3
+#define PCRE_INFO_FIRSTCHAR 4
+#define PCRE_INFO_FIRSTTABLE 5
+#define PCRE_INFO_LASTLITERAL 6
+
+/* Types */
+
+struct real_pcre; /* declaration; the definition is private */
+struct real_pcre_extra; /* declaration; the definition is private */
+
+typedef struct real_pcre pcre;
+typedef struct real_pcre_extra pcre_extra;
+
+/* Store get and free functions. These can be set to alternative malloc/free
+functions if required. Some magic is required for Win32 DLL; it is null on
+other OS. */
+
+PCRE_DL_IMPORT extern void *(*pcre_malloc)(size_t);
+PCRE_DL_IMPORT extern void (*pcre_free)(void *);
+
+#undef PCRE_DL_IMPORT
+
+/* Functions */
+
+extern pcre *pcre_compile(const char *, int, const char **, int *,
+ const unsigned char *);
+extern int pcre_copy_substring(const char *, int *, int, int, char *, int);
+extern int pcre_exec(const pcre *, const pcre_extra *, const char *,
+ int, int, int, int *, int);
+extern void pcre_free_substring(const char *);
+extern void pcre_free_substring_list(const char **);
+extern int pcre_get_substring(const char *, int *, int, int, const char **);
+extern int pcre_get_substring_list(const char *, int *, int, const char ***);
+extern int pcre_info(const pcre *, int *, int *);
+extern int pcre_fullinfo(const pcre *, const pcre_extra *, int, void *);
+extern const unsigned char *pcre_maketables(void);
+extern pcre_extra *pcre_study(const pcre *, int, const char **);
+extern const char *pcre_version(void);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* End of pcre.h */
diff --git a/win32/pcre/include/pcreposix.h b/win32/pcre/include/pcreposix.h
new file mode 100644
index 0000000..e70af2d
--- /dev/null
+++ b/win32/pcre/include/pcreposix.h
@@ -0,0 +1,88 @@
+/*************************************************
+* Perl-Compatible Regular Expressions *
+*************************************************/
+
+/* Copyright (c) 1997-2001 University of Cambridge */
+
+#ifndef _PCREPOSIX_H
+#define _PCREPOSIX_H
+
+/* This is the header for the POSIX wrapper interface to the PCRE Perl-
+Compatible Regular Expression library. It defines the things POSIX says should
+be there. I hope. */
+
+/* Have to include stdlib.h in order to ensure that size_t is defined. */
+
+#include <stdlib.h>
+
+/* Allow for C++ users */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Options defined by POSIX. */
+
+#define REG_ICASE 0x01
+#define REG_NEWLINE 0x02
+#define REG_NOTBOL 0x04
+#define REG_NOTEOL 0x08
+
+/* These are not used by PCRE, but by defining them we make it easier
+to slot PCRE into existing programs that make POSIX calls. */
+
+#define REG_EXTENDED 0
+#define REG_NOSUB 0
+
+/* Error values. Not all these are relevant or used by the wrapper. */
+
+enum {
+ REG_ASSERT = 1, /* internal error ? */
+ REG_BADBR, /* invalid repeat counts in {} */
+ REG_BADPAT, /* pattern error */
+ REG_BADRPT, /* ? * + invalid */
+ REG_EBRACE, /* unbalanced {} */
+ REG_EBRACK, /* unbalanced [] */
+ REG_ECOLLATE, /* collation error - not relevant */
+ REG_ECTYPE, /* bad class */
+ REG_EESCAPE, /* bad escape sequence */
+ REG_EMPTY, /* empty expression */
+ REG_EPAREN, /* unbalanced () */
+ REG_ERANGE, /* bad range inside [] */
+ REG_ESIZE, /* expression too big */
+ REG_ESPACE, /* failed to get memory */
+ REG_ESUBREG, /* bad back reference */
+ REG_INVARG, /* bad argument */
+ REG_NOMATCH /* match failed */
+};
+
+
+/* The structure representing a compiled regular expression. */
+
+typedef struct {
+ void *re_pcre;
+ size_t re_nsub;
+ size_t re_erroffset;
+} regex_t;
+
+/* The structure in which a captured offset is returned. */
+
+typedef int regoff_t;
+
+typedef struct {
+ regoff_t rm_so;
+ regoff_t rm_eo;
+} regmatch_t;
+
+/* The functions */
+
+extern int regcomp(regex_t *, const char *, int);
+extern int regexec(regex_t *, const char *, size_t, regmatch_t *, int);
+extern size_t regerror(int, const regex_t *, char *, size_t);
+extern void regfree(regex_t *);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* End of pcreposix.h */
diff --git a/win32/pcre/lib/libpcre-bcc.def b/win32/pcre/lib/libpcre-bcc.def
new file mode 100644
index 0000000..ecdd5c2
--- /dev/null
+++ b/win32/pcre/lib/libpcre-bcc.def
@@ -0,0 +1,21 @@
+LIBRARY PCRE.DLL
+
+EXPORTS
+ _pcre_compile = pcre_compile ; pcre_compile
+ _pcre_copy_substring = pcre_copy_substring ; pcre_copy_substring
+ _pcre_exec = pcre_exec ; pcre_exec
+ _pcre_free = pcre_free ; pcre_free
+ _pcre_free_substring = pcre_free_substring ; pcre_free_substring
+ _pcre_free_substring_list = pcre_free_substring_list ; pcre_free_substring_list
+ _pcre_fullinfo = pcre_fullinfo ; pcre_fullinfo
+ _pcre_get_substring = pcre_get_substring ; pcre_get_substring
+ _pcre_get_substring_list = pcre_get_substring_list ; pcre_get_substring_list
+ _pcre_info = pcre_info ; pcre_info
+ _pcre_maketables = pcre_maketables ; pcre_maketables
+ _pcre_malloc = pcre_malloc ; pcre_malloc
+ _pcre_study = pcre_study ; pcre_study
+ _pcre_version = pcre_version ; pcre_version
+ _regcomp = regcomp ; regcomp
+ _regerror = regerror ; regerror
+ _regexec = regexec ; regexec
+ _regfree = regfree ; regfree
diff --git a/win32/pcre/lib/libpcre-bcc.lib b/win32/pcre/lib/libpcre-bcc.lib
new file mode 100644
index 0000000..e807b0e
--- /dev/null
+++ b/win32/pcre/lib/libpcre-bcc.lib
Binary files differ
diff --git a/win32/pcre/lib/libpcre.def b/win32/pcre/lib/libpcre.def
new file mode 100644
index 0000000..2db32cb
--- /dev/null
+++ b/win32/pcre/lib/libpcre.def
@@ -0,0 +1,20 @@
+; i:\MINGW\BIN\dlltool.exe -Cn -a -z libpcre.def --export-all-symbols DLLTMPDIR.libpcre.a/get.o DLLTMPDIR.libpcre.a/maketables.o DLLTMPDIR.libpcre.a/pcre.o DLLTMPDIR.libpcre.a/study.o DLLTMPDIR.libpcreposix.a/pcreposix.o
+EXPORTS
+ pcre_info @ 1 ;
+ pcre_copy_substring @ 2 ;
+ pcre_exec @ 3 ;
+ pcre_free @ 4 DATA ;
+ pcre_free_substring @ 5 ;
+ pcre_free_substring_list @ 6 ;
+ pcre_fullinfo @ 7 ;
+ pcre_get_substring @ 8 ;
+ pcre_get_substring_list @ 9 ;
+ pcre_compile @ 10 ;
+ pcre_maketables @ 11 ;
+ pcre_malloc @ 12 DATA ;
+ pcre_study @ 13 ;
+ pcre_version @ 14 ;
+ regcomp @ 15 ;
+ regerror @ 16 ;
+ regexec @ 17 ;
+ regfree @ 18 ;
diff --git a/win32/pcre/lib/libpcre.la b/win32/pcre/lib/libpcre.la
new file mode 100644
index 0000000..8072045
--- /dev/null
+++ b/win32/pcre/lib/libpcre.la
@@ -0,0 +1,32 @@
+# libpcre.la - a libtool library file
+# Generated by ltmain.sh - GNU libtool 1.4 (1.920 2001/04/24 23:26:18)
+#
+# Please DO NOT delete this file!
+# It is necessary for linking the library.
+
+# The name that we can dlopen(3).
+dlname=''
+
+# Names of this library.
+library_names=''
+
+# The name of the static archive.
+old_library='libpcre.a'
+
+# Libraries that this one depends upon.
+dependency_libs=''
+
+# Version information for libpcre.
+current=0
+age=0
+revision=1
+
+# Is this an already installed library?
+installed=yes
+
+# Files to dlopen/dlpreopen
+dlopen=''
+dlpreopen=''
+
+# Directory that this library needs to be installed in:
+libdir='c:/progra~1/pcre/lib'
diff --git a/win32/pcre/lib/libpcre.lib b/win32/pcre/lib/libpcre.lib
new file mode 100644
index 0000000..7b8be59
--- /dev/null
+++ b/win32/pcre/lib/libpcre.lib
Binary files differ
diff --git a/win32/pcre/lib/libpcreposix.la b/win32/pcre/lib/libpcreposix.la
new file mode 100644
index 0000000..39ab321
--- /dev/null
+++ b/win32/pcre/lib/libpcreposix.la
@@ -0,0 +1,32 @@
+# libpcreposix.la - a libtool library file
+# Generated by ltmain.sh - GNU libtool 1.4 (1.920 2001/04/24 23:26:18)
+#
+# Please DO NOT delete this file!
+# It is necessary for linking the library.
+
+# The name that we can dlopen(3).
+dlname=''
+
+# Names of this library.
+library_names=''
+
+# The name of the static archive.
+old_library='libpcreposix.a'
+
+# Libraries that this one depends upon.
+dependency_libs=''
+
+# Version information for libpcreposix.
+current=0
+age=0
+revision=0
+
+# Is this an already installed library?
+installed=yes
+
+# Files to dlopen/dlpreopen
+dlopen=''
+dlpreopen=''
+
+# Directory that this library needs to be installed in:
+libdir='c:/progra~1/pcre/lib'
diff --git a/win32/pcre/manifest/pcre-3.9-lib.mft b/win32/pcre/manifest/pcre-3.9-lib.mft
new file mode 100644
index 0000000..024ac72
--- /dev/null
+++ b/win32/pcre/manifest/pcre-3.9-lib.mft
@@ -0,0 +1,13 @@
+include/pcre.h
+include/pcreposix.h
+lib/libpcre-bcc.def
+lib/libpcre-bcc.lib
+lib/libpcre.a
+lib/libpcre.def
+lib/libpcre.dll.a
+lib/libpcre.la
+lib/libpcre.lib
+lib/libpcreposix.a
+lib/libpcreposix.la
+manifest/pcre-3.9-lib.mft
+manifest/pcre-3.9-lib.ver
diff --git a/win32/pcre/manifest/pcre-3.9-lib.ver b/win32/pcre/manifest/pcre-3.9-lib.ver
new file mode 100644
index 0000000..1bf9d6f
--- /dev/null
+++ b/win32/pcre/manifest/pcre-3.9-lib.ver
@@ -0,0 +1,2 @@
+Pcre 3.9: developer files
+Pcre: Perl-compatible regular-expression library