diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-03-28 21:08:50 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-03-28 21:08:50 +0000 |
commit | f18d1b7cd0ec4708518ab5e18ea33b6eadca0301 (patch) | |
tree | e831c6b1957af47db1301675b52c0d2a2e315fa7 /etc | |
parent | 307603b24dde69eac62d2cb52123488137520c9c (diff) | |
download | luasocket-f18d1b7cd0ec4708518ab5e18ea33b6eadca0301.tar.gz luasocket-f18d1b7cd0ec4708518ab5e18ea33b6eadca0301.tar.bz2 luasocket-f18d1b7cd0ec4708518ab5e18ea33b6eadca0301.zip |
Closer to release...
Diffstat (limited to 'etc')
-rw-r--r-- | etc/check-links.lua | 8 | ||||
-rw-r--r-- | etc/dict.lua | 19 | ||||
-rw-r--r-- | etc/get.lua | 12 |
3 files changed, 29 insertions, 10 deletions
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 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- Little program that checks links in HTML files | ||
3 | -- LuaSocket 1.5 sample files. | ||
4 | ----------------------------------------------------------------------------- | ||
1 | socket.http.TIMEOUT = 10 | 5 | socket.http.TIMEOUT = 10 |
2 | 6 | ||
3 | cache = {} | 7 | cache = {} |
@@ -14,7 +18,7 @@ end | |||
14 | 18 | ||
15 | function getstatus(url) | 19 | function getstatus(url) |
16 | local parsed = socket.url.parse(url, { scheme = "file" }) | 20 | local parsed = socket.url.parse(url, { scheme = "file" }) |
17 | if cache[url] then return cache[url].res end | 21 | if cache[url] then return cache[url] end |
18 | local res | 22 | local res |
19 | if parsed.scheme == "http" then | 23 | if parsed.scheme == "http" then |
20 | local request = { url = url } | 24 | local request = { url = url } |
@@ -34,7 +38,7 @@ function getstatus(url) | |||
34 | res = nil | 38 | res = nil |
35 | else res = error end | 39 | else res = error end |
36 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end | 40 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end |
37 | cache[url] = { res = res } | 41 | cache[url] = res |
38 | return res | 42 | return res |
39 | end | 43 | end |
40 | 44 | ||
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 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- Little program to download DICT word definitions | ||
3 | -- LuaSocket 1.5 sample files | ||
4 | ----------------------------------------------------------------------------- | ||
1 | function get_status(sock, valid) | 5 | function get_status(sock, valid) |
2 | local line, err = sock:receive() | 6 | local line, err = sock:receive() |
3 | local code, par | 7 | local code, par |
4 | if not line then sock:close() return err end | 8 | if not line then sock:close() return err end |
5 | _, _, code = strfind(line, "^(%d%d%d)") | 9 | _, _, code = string.find(line, "^(%d%d%d)") |
6 | code = tonumber(code) | 10 | code = tonumber(code) |
7 | if code ~= valid then return code end | 11 | if code ~= valid then return code end |
8 | if code == 150 then | 12 | if code == 150 then |
9 | _,_,_, par = strfind(line, "^(%d%d%d) (%d*)") | 13 | _,_,_, par = string.find(line, "^(%d%d%d) (%d*)") |
10 | par = tonumber(par) | 14 | par = tonumber(par) |
11 | end | 15 | end |
12 | return nil, par | 16 | return nil, par |
@@ -24,7 +28,7 @@ function get_def(sock) | |||
24 | end | 28 | end |
25 | 29 | ||
26 | function dict_open() | 30 | function dict_open() |
27 | local sock, err = connect("dict.org", 2628) | 31 | local sock, err = socket.connect("dict.org", 2628) |
28 | if not sock then return nil, err end | 32 | if not sock then return nil, err end |
29 | sock:timeout(10) | 33 | sock:timeout(10) |
30 | local code, par = get_status(sock, 220) | 34 | local code, par = get_status(sock, 220) |
@@ -48,7 +52,7 @@ function dict_define(sock, word, dict) | |||
48 | end | 52 | end |
49 | code, par = get_status(sock, 250) | 53 | code, par = get_status(sock, 250) |
50 | if code then return nil, code end | 54 | if code then return nil, code end |
51 | return gsub(defs, "%s%s$", "") | 55 | return string.gsub(defs, "%s%s$", "") |
52 | end | 56 | end |
53 | 57 | ||
54 | function dict_close(sock) | 58 | function dict_close(sock) |
@@ -65,3 +69,10 @@ function dict_get(word, dict) | |||
65 | dict_close(sock) | 69 | dict_close(sock) |
66 | return defs, err | 70 | return defs, err |
67 | end | 71 | end |
72 | |||
73 | if arg and arg[1] then | ||
74 | defs, err = dict_get(arg[1], arg[2]) | ||
75 | print(defs or err) | ||
76 | else | ||
77 | io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n") | ||
78 | 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 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- Little program to download files from URLs | ||
3 | -- LuaSocket 1.5 sample files | ||
4 | ----------------------------------------------------------------------------- | ||
1 | -- formats a number of seconds into human readable form | 5 | -- formats a number of seconds into human readable form |
2 | function nicetime(s) | 6 | function nicetime(s) |
3 | local l = "s" | 7 | local l = "s" |
@@ -63,15 +67,15 @@ function receive2disk(file, size) | |||
63 | size = size | 67 | size = size |
64 | } | 68 | } |
65 | local receive_cb = function(chunk, err) | 69 | local receive_cb = function(chunk, err) |
66 | local dt = socket._time() - %aux.start -- elapsed time since start | 70 | local dt = socket._time() - aux.start -- elapsed time since start |
67 | if not chunk or chunk == "" then | 71 | if not chunk or chunk == "" then |
68 | io.write("\n") | 72 | io.write("\n") |
69 | aux.file:close() | 73 | aux.file:close() |
70 | return | 74 | return |
71 | end | 75 | end |
72 | aux.file:write(chunk) | 76 | aux.file:write(chunk) |
73 | aux.got = aux.got + string.len(chunk) -- total bytes received | 77 | aux.got = aux.got + string.len(chunk) -- total bytes received |
74 | if dt < 0.1 then return 1 end -- not enough time for estimate | 78 | if dt < 0.1 then return 1 end -- not enough time for estimate |
75 | io.write("\r", gauge(aux.got, dt, aux.size)) | 79 | io.write("\r", gauge(aux.got, dt, aux.size)) |
76 | return 1 | 80 | return 1 |
77 | end | 81 | end |
@@ -122,7 +126,7 @@ function getschemeandname(url, name) | |||
122 | return parsed.scheme, name | 126 | return parsed.scheme, name |
123 | end | 127 | end |
124 | 128 | ||
125 | -- gets a file either by http or url, saving as name | 129 | -- gets a file either by http or ftp, saving as <name> |
126 | function get(url, name) | 130 | function get(url, name) |
127 | local scheme | 131 | local scheme |
128 | scheme, name = getschemeandname(url, name) | 132 | scheme, name = getschemeandname(url, name) |