# HG changeset patch # User Matthew Wild # Date 1262897879 0 # Node ID 6ad3ea055905eb2957130b96d93e005008afc277 # Parent fa9efbef8a0cdfc97848466108850ff417d1d5b9 clix.send: Add --stdin flag to indicate to read data from stdin (but not interactively) diff -r fa9efbef8a0c -r 6ad3ea055905 clix/send.lua --- a/clix/send.lua Thu Jan 07 20:56:18 2010 +0000 +++ b/clix/send.lua Thu Jan 07 20:57:59 2010 +0000 @@ -1,14 +1,15 @@ short_opts.i = "interactive"; +short_opts.e = "echo"; return function (opts, arg) if (#arg == 0 or opts.help) and not opts.interactive then return 0; end local function on_connect(conn) - local function send_message(_, text) + local function send_message(text) conn:send(verse.message({ to = opts.to, type = opts.type or "chat" }):body(text)); end - if opts.interactive then + if opts.interactive or opts.stdin then -- Fake socket object around stdin local stdin = { getfd = function () return 0; end; @@ -18,13 +19,23 @@ close = function () end; receive = function (_, patt) local data = io.stdin:read(patt); - io.write(data, patt == "*l" and "\n" or ""); + if opts.echo then + io.write(data, patt == "*l" and "\n" or ""); + end return data; end }; - stdin = require "net.server".wrapclient(stdin, "stdin", 0, { onincoming = send_message, ondisconnect = function () end }, "*l"); + local function on_incoming(stdin, text) + send_message(text); + 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 - send_message(nil, table.concat(arg, " ")); + send_message(table.concat(arg, " ")); conn:close(); end end