diff options
author | Emil Renner Berthing <esmil@mailme.dk> | 2013-01-16 11:40:55 +0100 |
---|---|---|
committer | Emil Renner Berthing <esmil@mailme.dk> | 2013-01-16 16:07:18 +0100 |
commit | f86416d607d5a60d944167267e8348a0d4597b0b (patch) | |
tree | 760e01ad33fb4ec5a9c49ac89fc54250e59dc640 | |
parent | 33f8b5586ac93c0f8082d894a612d54bae6b63b4 (diff) | |
download | lem-f86416d607d5a60d944167267e8348a0d4597b0b.tar.gz lem-f86416d607d5a60d944167267e8348a0d4597b0b.tar.xz lem-f86416d607d5a60d944167267e8348a0d4597b0b.zip |
lem: if no file is specified run the repl
-rw-r--r-- | Makefile.in | 3 | ||||
-rwxr-xr-x | bin/lem-repl | 30 | ||||
-rw-r--r-- | bin/lem.c | 15 | ||||
-rw-r--r-- | lem/repl.lua | 110 |
4 files changed, 69 insertions, 89 deletions
diff --git a/Makefile.in b/Makefile.in index 149b2f1..7eaea11 100644 --- a/Makefile.in +++ b/Makefile.in @@ -116,9 +116,8 @@ $(DESTDIR)$(lmoddir)/% $(DESTDIR)$(cmoddir)/% $(DESTDIR)$(pkgconfigdir)/%: % $Q$(INSTALL) -m 644 $< $@ install: \ - $(DESTDIR)$(pkgconfigdir)/lem.pc \ $(DESTDIR)$(bindir)/lem \ - $(DESTDIR)$(bindir)/lem-repl \ + $(DESTDIR)$(pkgconfigdir)/lem.pc \ $(headers:%=$(DESTDIR)$(includedir)/lem/%) \ $(llibs:%=$(DESTDIR)$(lmoddir)/%) \ $(clibs:%=$(DESTDIR)$(cmoddir)/%) diff --git a/bin/lem-repl b/bin/lem-repl deleted file mode 100755 index 13e15b4..0000000 --- a/bin/lem-repl +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env lem --- --- This file is part of LEM, a Lua Event Machine. --- Copyright 2011-2012 Emil Renner Berthing --- --- LEM is free software: you can redistribute it and/or modify it --- under the terms of the GNU Lesser General Public License as --- published by the Free Software Foundation, either version 3 of --- the License, or (at your option) any later version. --- --- LEM is distributed in the hope that it will be useful, but --- WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU Lesser General Public License for more details. --- --- You should have received a copy of the GNU Lesser General Public --- License along with LEM. If not, see <http://www.gnu.org/licenses/>. --- - -local io = require 'lem.io' -local repl = require 'lem.repl' - -io.stdout:write([[ -A Lua Event Machine 0.3 Copyright 2011-2012 Emil Renner Berthing -]]) - -local _, err = repl.repl('stdin', io.stdin, io.stdout) -print(err or '') - --- vim: syntax=lua ts=2 sw=2 noet: @@ -1,6 +1,6 @@ /* * This file is part of LEM, a Lua Event Machine. - * Copyright 2011-2012 Emil Renner Berthing + * Copyright 2011-2013 Emil Renner Berthing * * LEM is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as @@ -268,9 +268,15 @@ static int queue_file(int argc, char *argv[], int fidx) { lua_State *T = lem_newthread(); + const char *filename; int i; - switch (luaL_loadfile(T, argv[fidx])) { + if (fidx < argc) + filename = argv[fidx]; + else + filename = LUA_LDIR "lem/repl.lua"; + + switch (luaL_loadfile(T, filename)) { case LUA_OK: /* success */ break; @@ -296,11 +302,6 @@ queue_file(int argc, char *argv[], int fidx) int main(int argc, char *argv[]) { - if (argc < 2) { - lem_log_error("I need a file.."); - return EXIT_FAILURE; - } - #if EV_MULTIPLICITY lem_loop = ev_default_loop(LEM_LOOPFLAGS); if (lem_loop == NULL) { diff --git a/lem/repl.lua b/lem/repl.lua index ecc2573..84b0b08 100644 --- a/lem/repl.lua +++ b/lem/repl.lua @@ -1,6 +1,6 @@ -- -- This file is part of LEM, a Lua Event Machine. --- Copyright 2011-2012 Emil Renner Berthing +-- Copyright 2011-2013 Emil Renner Berthing -- -- LEM is free software: you can redistribute it and/or modify it -- under the terms of the GNU Lesser General Public License as @@ -16,8 +16,6 @@ -- License along with LEM. If not, see <http://www.gnu.org/licenses/>. -- -local utils = require 'lem.utils' - local load = load if _VERSION == 'Lua 5.1' then load = loadstring @@ -28,70 +26,82 @@ local concat = table.concat local tostring = tostring local select = select -return { - repl = function(name, ins, outs) - name = '=' .. name - - local function onreturn(ok, ...) - if not ok then - local ok, err = outs:write(format("%s\n", select(1, ...))) - if not ok then return nil, err end - return true - end +local function repl(name, ins, outs) + name = '=' .. name - local args = select('#', ...) - if args == 0 then return true end + local function onreturn(ok, ...) + if not ok then + local ok, err = outs:write(format("%s\n", select(1, ...))) + if not ok then return nil, err end + return true + end - local rstr - do - local t = { ... } - for i = 1, args - 1 do - t[i] = tostring(t[i]) - end - t[args] = tostring(t[args])..'\n' + local args = select('#', ...) + if args == 0 then return true end - rstr = concat(t, '\t') + local rstr + do + local t = { ... } + for i = 1, args - 1 do + t[i] = tostring(t[i]) end + t[args] = tostring(t[args])..'\n' - local ok, err = outs:write(rstr) - if not ok then return nil, err end - - return true + rstr = concat(t, '\t') end - while true do - local res, err = outs:write('> ') - if not res then return nil, err end + local ok, err = outs:write(rstr) + if not ok then return nil, err end - res, err = ins:read('*l') - if not res then return nil, err end + return true + end - local line = res:gsub('^=', 'return ') + while true do + local res, err = outs:write('> ') + if not res then return nil, err end - while true do - res, err = load(line, name) - if res then - res, err = onreturn(pcall(res)) - if not res then return nil, err end - break - end + res, err = ins:read('*l') + if not res then return nil, err end - if not err:match("<eof>") then - res, err = outs:write(format("%s\n", err)) - if not res then return nil, err end - break - end + local line = res:gsub('^=', 'return ') - res, err = outs:write('>> ') + while true do + res, err = load(line, name) + if res then + res, err = onreturn(pcall(res)) if not res then return nil, err end + break + end - res, err = ins:read('*l') + if not err:match("<eof>") then + res, err = outs:write(format("%s\n", err)) if not res then return nil, err end - - line = line .. res + break end + + res, err = outs:write('>> ') + if not res then return nil, err end + + res, err = ins:read('*l') + if not res then return nil, err end + + line = line .. res end end -} +end + +-- if not run directly just return the module table +if not arg or arg[0] then + return { repl = repl } +end + +local io = require 'lem.io' + +io.stdout:write([[ +A Lua Event Machine 0.3 Copyright 2011-2013 Emil Renner Berthing +]]) + +local _, err = repl('stdin', io.stdin, io.stdout) +print(err or '') -- vim: ts=2 sw=2 noet: |