lat.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. --[[
  2. -- Copyright (c) 2013-2016 Marcus Rohrmoser, http://purl.mro.name/recorder
  3. --
  4. -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  5. -- associated documentation files (the "Software"), to deal in the Software without restriction,
  6. -- including without limitation the rights to use, copy, modify, merge, publish, distribute,
  7. -- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  8. -- furnished to do so, subject to the following conditions:
  9. --
  10. -- The above copyright notice and this permission notice shall be included in all copies or
  11. -- substantial portions of the Software.
  12. --
  13. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  14. -- NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. -- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  16. -- OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. --
  19. -- MIT License http://opensource.org/licenses/MIT
  20. ]]
  21. local function escape_cmdline(self)
  22. return table.concat{'"', self:gsub('\\', '\\\\'):gsub('"', '\\"'), '"'}
  23. end
  24. function os.at(t,cmd,queue)
  25. if not queue then queue = 'a' end
  26. local at_cmd = table.concat{'echo ', escape_cmdline(cmd), ' | at -q ', queue, ' ', os.date('%H:%M %d.%m.%Y', t), ' 2>&1'}
  27. io.stderr:write(at_cmd, "\n")
  28. local f = assert(io.popen(at_cmd, 'r'))
  29. local s = assert(f:read('*a'))
  30. f:close()
  31. -- io.stderr:write('at result: \'', s, "'\n")
  32. local _,_,jobnum = s:find('job%s+(%d+)%s+at%s+')
  33. jobnum = tonumber(jobnum)
  34. if jobnum then return jobnum end
  35. return nil,s:gsub('%s+$','')
  36. end
  37. function os.atc(jobnum)
  38. if not jobnum then return nil,'No job number' end
  39. local f = assert(io.popen('at -c ' .. tostring(assert(jobnum)), 'r'))
  40. local s = assert(f:read('*a'))
  41. f:close()
  42. if not s or s:len() == 0 then
  43. return nil,'No at job ' .. jobnum
  44. end
  45. return s
  46. end
  47. function os.atrm(jobnum)
  48. return os.execute('atrm ' .. jobnum)
  49. end
  50. function os.at_cat(jobnum)
  51. if true then
  52. return nil,'not implemented yet'
  53. end
  54. local f = assert(io.popen('at -c ' .. tostring(jobnum), 'r'))
  55. local s = assert(f:read('*a'))
  56. f:close()
  57. -- io.stderr:write('at result: \'', s, "'\n")
  58. local idx0,idx1,_ = s:find('unset OLDPWD')
  59. return s:sub(idx1+2):gsub('%s+$',''),s:sub(1,idx1)
  60. end