aboutsummaryrefslogtreecommitdiff
path: root/etc
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-11-27 07:58:04 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-11-27 07:58:04 +0000
commit7c97e8e40aaa665226fb54449773dc3134e755b2 (patch)
tree47888d4c924fc24bf3b355bf58120ea3cdc74bc4 /etc
parenteb0fc857ddea6f084d338589e2a33d3e7d4eade6 (diff)
downloadluasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.tar.gz
luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.tar.bz2
luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.zip
Almost ready for beta3
Diffstat (limited to 'etc')
-rw-r--r--etc/dict.lua16
-rw-r--r--etc/lp.lua23
-rw-r--r--etc/tftp.lua17
3 files changed, 37 insertions, 19 deletions
diff --git a/etc/dict.lua b/etc/dict.lua
index 8c197f5..716b8db 100644
--- a/etc/dict.lua
+++ b/etc/dict.lua
@@ -8,11 +8,14 @@
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
9-- Load required modules 9-- Load required modules
10----------------------------------------------------------------------------- 10-----------------------------------------------------------------------------
11local base = require("base")
12local string = require("string")
13local table = require("table")
11local socket = require("socket") 14local socket = require("socket")
12local url = require("socket.url") 15local url = require("socket.url")
13local tp = require("socket.tp") 16local tp = require("socket.tp")
14 17
15module("socket.dict") 18local dict = module("socket.dict")
16 19
17----------------------------------------------------------------------------- 20-----------------------------------------------------------------------------
18-- Globals 21-- Globals
@@ -28,7 +31,7 @@ local metat = { __index = {} }
28 31
29function open(host, port) 32function open(host, port)
30 local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT)) 33 local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT))
31 return setmetatable({tp = tp}, metat) 34 return base.setmetatable({tp = tp}, metat)
32end 35end
33 36
34function metat.__index:greet() 37function metat.__index:greet()
@@ -37,7 +40,8 @@ end
37 40
38function metat.__index:check(ok) 41function metat.__index:check(ok)
39 local code, status = socket.try(self.tp:check(ok)) 42 local code, status = socket.try(self.tp:check(ok))
40 return code, tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)"))) 43 return code,
44 base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)")))
41end 45end
42 46
43function metat.__index:getdef() 47function metat.__index:getdef()
@@ -116,7 +120,7 @@ local function parse(u)
116 if cmd == "m" then 120 if cmd == "m" then
117 arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end) 121 arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end)
118 end 122 end
119 string.gsub(arg, ":([^:]*)$", function(f) t.n = tonumber(f) end) 123 string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end)
120 return t 124 return t
121end 125end
122 126
@@ -143,6 +147,8 @@ local function sget(u)
143end 147end
144 148
145get = socket.protect(function(gett) 149get = socket.protect(function(gett)
146 if type(gett) == "string" then return sget(gett) 150 if base.type(gett) == "string" then return sget(gett)
147 else return tget(gett) end 151 else return tget(gett) end
148end) 152end)
153
154base.setmetatable(dict, nil)
diff --git a/etc/lp.lua b/etc/lp.lua
index b69cc02..a5327d1 100644
--- a/etc/lp.lua
+++ b/etc/lp.lua
@@ -9,9 +9,12 @@
9 if you have any questions: RFC 1179 9 if you have any questions: RFC 1179
10]] 10]]
11-- make sure LuaSocket is loaded 11-- make sure LuaSocket is loaded
12local io = require("io")
13local base = require("base")
14local string = require("string")
12local socket = require("socket") 15local socket = require("socket")
13local ltn12 = require("ltn12") 16local ltn12 = require("ltn12")
14local test = socket.try 17local lp = module("socket.lp")
15 18
16-- default port 19-- default port
17PORT = 515 20PORT = 515
@@ -28,7 +31,7 @@ local function connect(localhost, option)
28 local localport = 721 31 local localport = 721
29 local done, err 32 local done, err
30 repeat 33 repeat
31 skt = test(socket.tcp()) 34 skt = socket.try(socket.tcp())
32 try(skt:settimeout(30)) 35 try(skt:settimeout(30))
33 done, err = skt:bind(localhost, localport) 36 done, err = skt:bind(localhost, localport)
34 if not done then 37 if not done then
@@ -37,8 +40,8 @@ local function connect(localhost, option)
37 skt = nil 40 skt = nil
38 else break end 41 else break end
39 until localport > 731 42 until localport > 731
40 test(skt, err) 43 socket.try(skt, err)
41 else skt = test(socket.tcp()) end 44 else skt = socket.try(socket.tcp()) end
42 try(skt:connect(host, port)) 45 try(skt:connect(host, port))
43 return { skt = skt, try = try } 46 return { skt = skt, try = try }
44end 47end
@@ -241,9 +244,9 @@ local format_codes = {
241-- lp.send 244-- lp.send
242 245
243send = socket.protect(function(file, option) 246send = socket.protect(function(file, option)
244 test(file, "invalid file name") 247 socket.try(file, "invalid file name")
245 test(option and type(option) == "table", "invalid options") 248 socket.try(option and base.type(option) == "table", "invalid options")
246 local fh = test(io.open(file,"rb")) 249 local fh = socket.try(io.open(file,"rb"))
247 local datafile_size = fh:seek("end") -- get total size 250 local datafile_size = fh:seek("end") -- get total size
248 fh:seek("set") -- go back to start of file 251 fh:seek("set") -- go back to start of file
249 local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME") 252 local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
@@ -270,11 +273,11 @@ send = socket.protect(function(file, option)
270 lpfile, 273 lpfile,
271 ctlfn); -- mandatory part of ctl file 274 ctlfn); -- mandatory part of ctl file
272 if (option.banner) then cfile = cfile .. 'L'..user..'\10' end 275 if (option.banner) then cfile = cfile .. 'L'..user..'\10' end
273 if (option.indent) then cfile = cfile .. 'I'..tonumber(option.indent)..'\10' end 276 if (option.indent) then cfile = cfile .. 'I'..base.tonumber(option.indent)..'\10' end
274 if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end 277 if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end
275 if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end 278 if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end
276 if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then 279 if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then
277 cfile = cfile .. 'W'..tonumber(option,width)..'\10' 280 cfile = cfile .. 'W'..base.tonumber(option,width)..'\10'
278 end 281 end
279 282
280 con.skt:settimeout(option.timeout or 65) 283 con.skt:settimeout(option.timeout or 65)
@@ -314,3 +317,5 @@ query = socket.protect(function(p)
314 con.skt:close() 317 con.skt:close()
315 return data 318 return data
316end) 319end)
320
321base.setmetatable(lp, nil)
diff --git a/etc/tftp.lua b/etc/tftp.lua
index f4af8bc..83a08b9 100644
--- a/etc/tftp.lua
+++ b/etc/tftp.lua
@@ -8,11 +8,14 @@
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
9-- Load required files 9-- Load required files
10----------------------------------------------------------------------------- 10-----------------------------------------------------------------------------
11local base = require("base")
12local table = require("table")
13local math = require("math")
14local string = require("string")
11local socket = require("socket") 15local socket = require("socket")
12local ltn12 = require("ltn12") 16local ltn12 = require("ltn12")
13local url = require("socket.url") 17local url = require("socket.url")
14 18local tftp = module("socket.tftp")
15module("socket.tftp")
16 19
17----------------------------------------------------------------------------- 20-----------------------------------------------------------------------------
18-- Program constants 21-- Program constants
@@ -73,16 +76,18 @@ end
73local function tget(gett) 76local function tget(gett)
74 local retries, dgram, sent, datahost, dataport, code 77 local retries, dgram, sent, datahost, dataport, code
75 local last = 0 78 local last = 0
79 socket.try(gett.host, "missing host")
76 local con = socket.try(socket.udp()) 80 local con = socket.try(socket.udp())
77 local try = socket.newtry(function() con:close() end) 81 local try = socket.newtry(function() con:close() end)
78 -- convert from name to ip if needed 82 -- convert from name to ip if needed
79 gett.host = try(socket.dns.toip(gett.host)) 83 gett.host = try(socket.dns.toip(gett.host))
80 con:settimeout(1) 84 con:settimeout(1)
81 -- first packet gives data host/port to be used for data transfers 85 -- first packet gives data host/port to be used for data transfers
86 local path = string.gsub(gett.path or "", "^/", "")
87 path = url.unescape(path)
82 retries = 0 88 retries = 0
83 repeat 89 repeat
84 sent = try(con:sendto(RRQ(gett.path, "octet"), 90 sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port))
85 gett.host, gett.port))
86 dgram, datahost, dataport = con:receivefrom() 91 dgram, datahost, dataport = con:receivefrom()
87 retries = retries + 1 92 retries = retries + 1
88 until dgram or datahost ~= "timeout" or retries > 5 93 until dgram or datahost ~= "timeout" or retries > 5
@@ -144,6 +149,8 @@ local function sget(u)
144end 149end
145 150
146get = socket.protect(function(gett) 151get = socket.protect(function(gett)
147 if type(gett) == "string" then return sget(gett) 152 if base.type(gett) == "string" then return sget(gett)
148 else return tget(gett) end 153 else return tget(gett) end
149end) 154end)
155
156base.setmetatable(tftp, nil)