clix/raw.lua

Tue, 22 Feb 2011 20:52:22 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 22 Feb 2011 20:52:22 +0000
changeset 46
b5d6e443e571
parent 27
5b58c002d6ad
child 68
b7d92ed9c268
permissions
-rw-r--r--

raw: If using --stdin and not --interactive then buffer all data before connecting, more reliable in a non-interactive environment

short_opts.i = "interactive";
short_opts.e = "echo";

return function (opts, args)
	if opts.short_help then
		print("Send/receive raw XML to/from the server");
		return;
	end
	
	local send_xml;
	if opts.stdin then
		send_xml = io.read("*a");
	end
	
	local function on_connect(conn)
		conn:hook("incoming-raw", function (data) print(data) end);
		if opts.interactive then
			local stdin = {
				getfd = function () return 0; end;
				dirty = function (self) return false; end;
				settimeout = function () end;
				send = function (_, d) return #d, 0; end;
				close = function () end;
				receive = function (_, patt)
					local data = io.stdin:read(patt);
					if opts.echo then
						io.write(data, patt == "*l" and "\n" or "");
					end
					return data;
				end
			};
			local function on_incoming(stdin, data)
				conn:send(data);
				if not opts.interactive then
					conn:close();
				end
			end
			stdin = require "net.server".wrapclient(stdin, "stdin", 0, {
				onincoming = on_incoming, ondisconnect = function () end
				}, "*l");
		else
			if send_xml then
				conn:send(send_xml);
			else
				conn:send(table.concat(arg, " "));
			end
			conn:close();
		end

	end
	return clix_connect(opts, on_connect);
end

mercurial