Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/sys.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2015-04-29 20:33:37 +0300
committerSam Gross <colesbury@gmail.com>2015-04-29 20:33:37 +0300
commitb9fc61a0cacce8601c34411731839b963f7f0cc3 (patch)
tree4a50537ec555a18c463b55b6f90c8ed0d7d0ef61
parent7a95486c19c5790bfff87e32e907bfc438d9a9dd (diff)
Lua 5.2 compatibility
-rw-r--r--init.lua185
-rw-r--r--sys.c10
2 files changed, 96 insertions, 99 deletions
diff --git a/init.lua b/init.lua
index aadd26d..17680d0 100644
--- a/init.lua
+++ b/init.lua
@@ -2,110 +2,101 @@
-- sys - a package that provides simple system (unix) tools
----------------------------------------------------------------------
-require 'os'
-require 'io'
-require 'paths'
+local os = require 'os'
+local io = require 'io'
+local paths = require 'paths'
-local _G = _G
-local print = print
-local error = error
-local require = require
-local os = os
-local io = io
-local pairs = pairs
-local ipairs = ipairs
-local paths = paths
-
-module 'sys'
+sys = {}
--------------------------------------------------------------------------------
-- load all functions from lib
--------------------------------------------------------------------------------
-_lib = require 'libsys'
-_G.libsys = nil
+local _lib = require 'libsys'
for k,v in pairs(_lib) do
- _G.sys[k] = v
+ sys[k] = v
end
--------------------------------------------------------------------------------
-- tic/toc (matlab-like) timers
--------------------------------------------------------------------------------
-tic = function()
- __t__ = clock()
- end
-toc = function(verbose)
- __dt__ = clock() - __t__
- if verbose then print(__dt__) end
- return __dt__
- end
+local __t__
+function sys.tic()
+ __t__ = sys.clock()
+end
+function sys.toc(verbose)
+ local __dt__ = sys.clock() - __t__
+ if verbose then print(__dt__) end
+ return __dt__
+end
--------------------------------------------------------------------------------
-- execute an OS command, but retrieves the result in a string
--------------------------------------------------------------------------------
-execute = function(cmd)
- local cmd = cmd .. ' 2>&1'
- local f = io.popen(cmd)
- local s = f:read('*all')
- f:close()
- s = s:gsub('^%s*',''):gsub('%s*$','')
- return s
- end
+local function execute(cmd)
+ local cmd = cmd .. ' 2>&1'
+ local f = io.popen(cmd)
+ local s = f:read('*all')
+ f:close()
+ s = s:gsub('^%s*',''):gsub('%s*$','')
+ return s
+end
+sys.execute = execute
--------------------------------------------------------------------------------
-- execute an OS command, but retrieves the result in a string
-- side effect: file in /tmp
-- this call is typically more robust than the one above (on some systems)
--------------------------------------------------------------------------------
-fexecute = function(cmd, readwhat)
- local tmpfile = os.tmpname()
- local cmd = cmd .. ' 1>'.. tmpfile..' 2>' .. tmpfile
- os.execute(cmd)
- local file = _G.assert(io.open(tmpfile))
- local s = file:read('*all')
- file:close()
- s = s:gsub('^%s*',''):gsub('%s*$','')
- os.execute('rm ' .. tmpfile)
- return s
- end
+function sys.fexecute(cmd, readwhat)
+ local tmpfile = os.tmpname()
+ local cmd = cmd .. ' 1>'.. tmpfile..' 2>' .. tmpfile
+ os.execute(cmd)
+ local file = _G.assert(io.open(tmpfile))
+ local s = file:read('*all')
+ file:close()
+ s = s:gsub('^%s*',''):gsub('%s*$','')
+ os.execute('rm ' .. tmpfile)
+ return s
+end
--------------------------------------------------------------------------------
-- returns the name of the OS in use
-- warning, this method is extremely dumb, and should be replaced by something
-- more reliable
--------------------------------------------------------------------------------
-uname = function()
- if paths.dirp('C:\\') then
- return 'windows'
- else
- local os = execute('uname -a')
- if os:find('Linux') then
- return 'linux'
- elseif os:find('Darwin') then
- return 'macos'
- else
- return '?'
- end
- end
- end
-OS = uname()
+function sys.uname()
+ if paths.dirp('C:\\') then
+ return 'windows'
+ else
+ local os = execute('uname -a')
+ if os:find('Linux') then
+ return 'linux'
+ elseif os:find('Darwin') then
+ return 'macos'
+ else
+ return '?'
+ end
+ end
+end
+sys.OS = sys.uname()
--------------------------------------------------------------------------------
-- ls (list dir)
--------------------------------------------------------------------------------
-ls = function(d) d = d or ' ' return execute('ls ' ..d) end
-ll = function(d) d = d or ' ' return execute('ls -l ' ..d) end
-la = function(d) d = d or ' ' return execute('ls -a ' ..d) end
-lla = function(d) d = d or ' ' return execute('ls -la '..d) end
+sys.ls = function(d) d = d or ' ' return execute('ls ' ..d) end
+sys.ll = function(d) d = d or ' ' return execute('ls -l ' ..d) end
+sys.la = function(d) d = d or ' ' return execute('ls -a ' ..d) end
+sys.lla = function(d) d = d or ' ' return execute('ls -la '..d) end
--------------------------------------------------------------------------------
-- prefix
--------------------------------------------------------------------------------
-prefix = execute('which lua'):gsub('//','/'):gsub('/bin/lua\n','')
+sys.prefix = execute('which lua'):gsub('//','/'):gsub('/bin/lua\n','')
--------------------------------------------------------------------------------
-- always returns the path of the file running
--------------------------------------------------------------------------------
-function fpath()
+function sys.fpath()
local fpath = _G.debug.getinfo(2).source:gsub('@','')
if fpath:find('/') ~= 1 then fpath = paths.concat(paths.cwd(),fpath) end
return paths.dirname(fpath),paths.basename(fpath)
@@ -114,7 +105,7 @@ end
--------------------------------------------------------------------------------
-- split string based on pattern pat
--------------------------------------------------------------------------------
-function split(str, pat)
+function sys.split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
@@ -136,7 +127,7 @@ end
--------------------------------------------------------------------------------
-- check if a number is NaN
--------------------------------------------------------------------------------
-function isNaN(number)
+function sys.isNaN(number)
-- We rely on the property that NaN is the only value that doesn't equal itself.
return (number ~= number)
end
@@ -144,41 +135,43 @@ end
--------------------------------------------------------------------------------
-- sleep
--------------------------------------------------------------------------------
-function sleep(seconds)
- usleep(seconds*1000000)
+function sys.sleep(seconds)
+ sys.usleep(seconds*1000000)
end
--------------------------------------------------------------------------------
-- colors, can be used to print things in color
--------------------------------------------------------------------------------
-COLORS = {none = '\27[0m',
- black = '\27[0;30m',
- red = '\27[0;31m',
- green = '\27[0;32m',
- yellow = '\27[0;33m',
- blue = '\27[0;34m',
- magenta = '\27[0;35m',
- cyan = '\27[0;36m',
- white = '\27[0;37m',
- Black = '\27[1;30m',
- Red = '\27[1;31m',
- Green = '\27[1;32m',
- Yellow = '\27[1;33m',
- Blue = '\27[1;34m',
- Magenta = '\27[1;35m',
- Cyan = '\27[1;36m',
- White = '\27[1;37m',
- _black = '\27[40m',
- _red = '\27[41m',
- _green = '\27[42m',
- _yellow = '\27[43m',
- _blue = '\27[44m',
- _magenta = '\27[45m',
- _cyan = '\27[46m',
- _white = '\27[47m'}
+sys.COLORS = {none = '\27[0m',
+ black = '\27[0;30m',
+ red = '\27[0;31m',
+ green = '\27[0;32m',
+ yellow = '\27[0;33m',
+ blue = '\27[0;34m',
+ magenta = '\27[0;35m',
+ cyan = '\27[0;36m',
+ white = '\27[0;37m',
+ Black = '\27[1;30m',
+ Red = '\27[1;31m',
+ Green = '\27[1;32m',
+ Yellow = '\27[1;33m',
+ Blue = '\27[1;34m',
+ Magenta = '\27[1;35m',
+ Cyan = '\27[1;36m',
+ White = '\27[1;37m',
+ _black = '\27[40m',
+ _red = '\27[41m',
+ _green = '\27[42m',
+ _yellow = '\27[43m',
+ _blue = '\27[44m',
+ _magenta = '\27[45m',
+ _cyan = '\27[46m',
+ _white = '\27[47m'}
--------------------------------------------------------------------------------
-- backward compat
--------------------------------------------------------------------------------
-dirname = paths.dirname
-concat = paths.concat
+sys.dirname = paths.dirname
+sys.concat = paths.concat
+
+return sys \ No newline at end of file
diff --git a/sys.c b/sys.c
index a25a5b2..489fdce 100644
--- a/sys.c
+++ b/sys.c
@@ -1,4 +1,3 @@
-
#include <lua.h>
#include <lauxlib.h>
@@ -45,7 +44,7 @@ static int l_usleep(lua_State *L) {
#endif
-static const struct luaL_reg routines [] = {
+static const struct luaL_Reg routines [] = {
{"clock", l_clock},
{"usleep", l_usleep},
{NULL, NULL}
@@ -53,6 +52,11 @@ static const struct luaL_reg routines [] = {
int luaopen_libsys(lua_State *L)
{
- luaL_openlib(L, "libsys", routines, 0);
+ lua_newtable(L);
+#if LUA_VERSION_NUM == 501
+ luaL_register(L, NULL, routines);
+#else
+ luaL_setfuncs(L, routines, 0);
+#endif
return 1;
}