summaryrefslogtreecommitdiff
path: root/library/dfhack-run.cpp
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-03-14 19:57:29 +0400
committerAlexander Gavrilov2012-03-14 19:57:29 +0400
commit560e977f0589ac1c0feb6ea825d20d351e325826 (patch)
treee32bf2135261fa1e9129fae4c2c7ae068b6ef4cc /library/dfhack-run.cpp
parentc42e2ff053bc3acbded353112cd6412c8211f279 (diff)
downloaddfhack-560e977f0589ac1c0feb6ea825d20d351e325826.tar.gz
dfhack-560e977f0589ac1c0feb6ea825d20d351e325826.tar.bz2
dfhack-560e977f0589ac1c0feb6ea825d20d351e325826.tar.xz
Implement trivial RPC interface for dfhack via TCP & protobufs.
Use it to make an executable capable of calling commands remotely.
Diffstat (limited to 'library/dfhack-run.cpp')
-rw-r--r--library/dfhack-run.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/library/dfhack-run.cpp b/library/dfhack-run.cpp
new file mode 100644
index 00000000..941ce8d0
--- /dev/null
+++ b/library/dfhack-run.cpp
@@ -0,0 +1,90 @@
+/*
+https://github.com/peterix/dfhack
+Copyright (c) 2011 Petr Mrázek <peterix@gmail.com>
+
+A thread-safe logging console with a line editor for windows.
+
+Based on linenoise win32 port,
+copyright 2010, Jon Griffiths <jon_p_griffiths at yahoo dot com>.
+All rights reserved.
+Based on linenoise, copyright 2010, Salvatore Sanfilippo <antirez at gmail dot com>.
+The original linenoise can be found at: http://github.com/antirez/linenoise
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of Redis nor the names of its contributors may be used
+ to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#include <stdarg.h>
+#include <errno.h>
+#include <stdio.h>
+#include <iostream>
+#include <fstream>
+#include <istream>
+#include <string>
+
+#include "RemoteClient.h"
+
+#include <cstdio>
+#include <cstdlib>
+#include <sstream>
+
+#include <memory>
+
+using namespace DFHack;
+using namespace dfproto;
+using std::cout;
+
+int main (int argc, char *argv[])
+{
+ color_ostream_wrapper out(cout);
+
+ if (argc <= 1)
+ {
+ fprintf(stderr, "Usage: dfhack-run <command> [args...]\n");
+ return 2;
+ }
+
+ // Connect to DFHack
+ RemoteClient client;
+ if (!client.connect())
+ return 2;
+
+ // Bind to RunCommand
+ RemoteFunction<CoreRunCommandRequest,CoreVoidReply> command;
+
+ if (!command.bind(out, &client, "RunCommand"))
+ return 2;
+
+ // Execute it
+ command.in()->set_command(argv[1]);
+ for (int i = 2; i < argc; i++)
+ command.in()->add_arguments(argv[i]);
+
+ if (command.execute(out) != CR_OK)
+ return 1;
+
+ out.flush();
+ return 0;
+}