aboutsummaryrefslogtreecommitdiff
path: root/etc
diff options
context:
space:
mode:
Diffstat (limited to 'etc')
-rw-r--r--etc/lp.lua24
1 files changed, 14 insertions, 10 deletions
diff --git a/etc/lp.lua b/etc/lp.lua
index a60fb86..3757e2d 100644
--- a/etc/lp.lua
+++ b/etc/lp.lua
@@ -11,6 +11,8 @@
11-- make sure LuaSocket is loaded 11-- make sure LuaSocket is loaded
12local io = require("io") 12local io = require("io")
13local base = _G 13local base = _G
14local os = require("os")
15local math = require("math")
14local string = require("string") 16local string = require("string")
15local socket = require("socket") 17local socket = require("socket")
16local ltn12 = require("ltn12") 18local ltn12 = require("ltn12")
@@ -192,6 +194,7 @@ local function send_data(con,fh,size)
192 con.try(size == 0, "file size mismatch") 194 con.try(size == 0, "file size mismatch")
193 end 195 end
194 end 196 end
197 recv_ack(con) -- note the double acknowledgement
195 send_ack(con) 198 send_ack(con)
196 recv_ack(con) 199 recv_ack(con)
197 return size 200 return size
@@ -224,7 +227,7 @@ local control_dflt = {
224local seq = 0 227local seq = 0
225local function newjob(connection) 228local function newjob(connection)
226 seq = seq + 1 229 seq = seq + 1
227 return math.floor(socket.gettime() * 1000 + seq) 230 return math.floor(socket.gettime() * 1000 + seq)%1000
228end 231end
229 232
230 233
@@ -241,22 +244,24 @@ local format_codes = {
241 f = 'f' 244 f = 'f'
242} 245}
243 246
244-- lp.send 247-- lp.send{option}
248-- requires option.file
245 249
246send = socket.protect(function(file, option) 250send = socket.protect(function(option)
247 socket.try(file, "invalid file name")
248 socket.try(option and base.type(option) == "table", "invalid options") 251 socket.try(option and base.type(option) == "table", "invalid options")
252 local file = option.file
253 socket.try(file, "invalid file name")
249 local fh = socket.try(io.open(file,"rb")) 254 local fh = socket.try(io.open(file,"rb"))
250 local datafile_size = fh:seek("end") -- get total size 255 local datafile_size = fh:seek("end") -- get total size
251 fh:seek("set") -- go back to start of file 256 fh:seek("set") -- go back to start of file
252 local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME") 257 local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
253 or "localhost" 258 or "localhost"
254 local con = connect(localhost, option) 259 local con = connect(localhost, option)
255-- format the control file 260-- format the control file
256 local jobno = newjob() 261 local jobno = newjob()
257 local localip = socket.dns.toip(localhost) 262 local localip = socket.dns.toip(localhost)
258 localhost = string.sub(localhost,1,31) 263 localhost = string.sub(localhost,1,31)
259 local user = string.sub(option.user or os.getenv("LPRUSER") or 264 local user = string.sub(option.user or os.getenv("LPRUSER") or
260 os.getenv("USERNAME") or os.getenv("USER") or "anonymous", 1,31) 265 os.getenv("USERNAME") or os.getenv("USER") or "anonymous", 1,31)
261 local lpfile = string.format("dfA%3.3d%-s", jobno, localhost); 266 local lpfile = string.format("dfA%3.3d%-s", jobno, localhost);
262 local fmt = format_codes[option.format] or 'l' 267 local fmt = format_codes[option.format] or 'l'
@@ -306,15 +311,14 @@ end)
306-- 311--
307query = socket.protect(function(p) 312query = socket.protect(function(p)
308 p = p or {} 313 p = p or {}
309 local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME") 314 local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME")
310 or "localhost" 315 or "localhost"
311 local con = connect(localhost,p) 316 local con = connect(localhost,p)
312 local fmt 317 local fmt
313 if string.sub(p.format or 's',1,1) == 's' then fmt = 3 else fmt = 4 end 318 if string.sub(p.format or 's',1,1) == 's' then fmt = 3 else fmt = 4 end
314 con.try(con.skt:send(string.format("%c%s %s\n", fmt, p.queue or "*", 319 con.try(con.skt:send(string.format("%c%s %s\n", fmt, p.queue or "*",
315 p.list or ""))) 320 p.list or "")))
316 local data = con.try(con.skt:receive("*a")) 321 local data = con.try(con.skt:receive("*a"))
317 con.skt:close() 322 con.skt:close()
318 return data 323 return data
319end) 324end)
320