From f18d1b7cd0ec4708518ab5e18ea33b6eadca0301 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Fri, 28 Mar 2003 21:08:50 +0000 Subject: Closer to release... --- etc/check-links.lua | 8 ++++++-- etc/dict.lua | 19 +++++++++++++++---- etc/get.lua | 12 ++++++++---- 3 files changed, 29 insertions(+), 10 deletions(-) (limited to 'etc') diff --git a/etc/check-links.lua b/etc/check-links.lua index 0dca27c..705c0ce 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua @@ -1,3 +1,7 @@ +----------------------------------------------------------------------------- +-- Little program that checks links in HTML files +-- LuaSocket 1.5 sample files. +----------------------------------------------------------------------------- socket.http.TIMEOUT = 10 cache = {} @@ -14,7 +18,7 @@ end function getstatus(url) local parsed = socket.url.parse(url, { scheme = "file" }) - if cache[url] then return cache[url].res end + if cache[url] then return cache[url] end local res if parsed.scheme == "http" then local request = { url = url } @@ -34,7 +38,7 @@ function getstatus(url) res = nil else res = error end else res = string.format("unhandled scheme '%s'", parsed.scheme) end - cache[url] = { res = res } + cache[url] = res return res end diff --git a/etc/dict.lua b/etc/dict.lua index 4685ca1..6790cab 100644 --- a/etc/dict.lua +++ b/etc/dict.lua @@ -1,12 +1,16 @@ +----------------------------------------------------------------------------- +-- Little program to download DICT word definitions +-- LuaSocket 1.5 sample files +----------------------------------------------------------------------------- function get_status(sock, valid) local line, err = sock:receive() local code, par if not line then sock:close() return err end - _, _, code = strfind(line, "^(%d%d%d)") + _, _, code = string.find(line, "^(%d%d%d)") code = tonumber(code) if code ~= valid then return code end if code == 150 then - _,_,_, par = strfind(line, "^(%d%d%d) (%d*)") + _,_,_, par = string.find(line, "^(%d%d%d) (%d*)") par = tonumber(par) end return nil, par @@ -24,7 +28,7 @@ function get_def(sock) end function dict_open() - local sock, err = connect("dict.org", 2628) + local sock, err = socket.connect("dict.org", 2628) if not sock then return nil, err end sock:timeout(10) local code, par = get_status(sock, 220) @@ -48,7 +52,7 @@ function dict_define(sock, word, dict) end code, par = get_status(sock, 250) if code then return nil, code end - return gsub(defs, "%s%s$", "") + return string.gsub(defs, "%s%s$", "") end function dict_close(sock) @@ -65,3 +69,10 @@ function dict_get(word, dict) dict_close(sock) return defs, err end + +if arg and arg[1] then + defs, err = dict_get(arg[1], arg[2]) + print(defs or err) +else + io.write("Usage:\n luasocket dict.lua []\n") +end diff --git a/etc/get.lua b/etc/get.lua index 33da653..af46c16 100644 --- a/etc/get.lua +++ b/etc/get.lua @@ -1,3 +1,7 @@ +----------------------------------------------------------------------------- +-- Little program to download files from URLs +-- LuaSocket 1.5 sample files +----------------------------------------------------------------------------- -- formats a number of seconds into human readable form function nicetime(s) local l = "s" @@ -63,15 +67,15 @@ function receive2disk(file, size) size = size } local receive_cb = function(chunk, err) - local dt = socket._time() - %aux.start -- elapsed time since start + local dt = socket._time() - aux.start -- elapsed time since start if not chunk or chunk == "" then io.write("\n") aux.file:close() return end aux.file:write(chunk) - aux.got = aux.got + string.len(chunk) -- total bytes received - if dt < 0.1 then return 1 end -- not enough time for estimate + aux.got = aux.got + string.len(chunk) -- total bytes received + if dt < 0.1 then return 1 end -- not enough time for estimate io.write("\r", gauge(aux.got, dt, aux.size)) return 1 end @@ -122,7 +126,7 @@ function getschemeandname(url, name) return parsed.scheme, name end --- gets a file either by http or url, saving as name +-- gets a file either by http or ftp, saving as function get(url, name) local scheme scheme, name = getschemeandname(url, name) -- cgit v1.2.3-55-g6feb