lpr.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local lp = require("socket.lp")
  2. local function usage()
  3. print('\nUsage: lua lpr.lua [filename] [keyword=val...]\n')
  4. print('Valid keywords are :')
  5. print(
  6. ' host=remote host or IP address (default "localhost")\n' ..
  7. ' queue=remote queue or printer name (default "printer")\n' ..
  8. ' port=remote port number (default 515)\n' ..
  9. ' user=sending user name\n' ..
  10. ' format=["binary" | "text" | "ps" | "pr" | "fortran"] (default "binary")\n' ..
  11. ' banner=true|false\n' ..
  12. ' indent=number of columns to indent\n' ..
  13. ' mail=email of address to notify when print is complete\n' ..
  14. ' title=title to use for "pr" format\n' ..
  15. ' width=width for "text" or "pr" formats\n' ..
  16. ' class=\n' ..
  17. ' job=\n' ..
  18. ' name=\n' ..
  19. ' localbind=true|false\n'
  20. )
  21. return nil
  22. end
  23. if not arg or not arg[1] then
  24. return usage()
  25. end
  26. do
  27. local opt = {}
  28. local pat = "[%s%c%p]*([%w]*)=([\"]?[%w%s_!@#$%%^&*()<>:;]+[\"]?.?)"
  29. for i = 2, #arg, 1 do
  30. string.gsub(arg[i], pat, function(name, value) opt[name] = value end)
  31. end
  32. if not arg[2] then
  33. return usage()
  34. end
  35. if arg[1] ~= "query" then
  36. opt.file = arg[1]
  37. r,e=lp.send(opt)
  38. io.stdout:write(tostring(r or e),'\n')
  39. else
  40. r,e=lp.query(opt)
  41. io.stdout:write(tostring(r or e), '\n')
  42. end
  43. end
  44. -- trivial tests
  45. --lua lp.lua lp.lua queue=default host=localhost
  46. --lua lp.lua lp.lua queue=default host=localhost format=binary localbind=1
  47. --lua lp.lua query queue=default host=localhost