aboutsummaryrefslogtreecommitdiff
path: root/etc/dict.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2003-03-28 21:08:50 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2003-03-28 21:08:50 +0000
commitf18d1b7cd0ec4708518ab5e18ea33b6eadca0301 (patch)
treee831c6b1957af47db1301675b52c0d2a2e315fa7 /etc/dict.lua
parent307603b24dde69eac62d2cb52123488137520c9c (diff)
downloadluasocket-f18d1b7cd0ec4708518ab5e18ea33b6eadca0301.tar.gz
luasocket-f18d1b7cd0ec4708518ab5e18ea33b6eadca0301.tar.bz2
luasocket-f18d1b7cd0ec4708518ab5e18ea33b6eadca0301.zip
Closer to release...
Diffstat (limited to 'etc/dict.lua')
-rw-r--r--etc/dict.lua19
1 files changed, 15 insertions, 4 deletions
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-----------------------------------------------------------------------------
1function get_status(sock, valid) 5function 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)
24end 28end
25 29
26function dict_open() 30function 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$", "")
52end 56end
53 57
54function dict_close(sock) 58function 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
67end 71end
72
73if arg and arg[1] then
74 defs, err = dict_get(arg[1], arg[2])
75 print(defs or err)
76else
77 io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n")
78end