completion, util: Check for io.popen failure.

Signed-off-by: Maarten Maathuis <madman2003@gmail.com>
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Maarten Maathuis 2008-12-11 22:14:07 +01:00 committed by Julien Danjou
parent 6e199bbd76
commit 02e4be93dc
2 changed files with 32 additions and 19 deletions

View File

@ -9,6 +9,7 @@
local io = io local io = io
local table = table local table = table
local math = math local math = math
local print = print
--- Completion module for awful --- Completion module for awful
module("awful.completion") module("awful.completion")
@ -26,16 +27,20 @@ keywords = {}
-- @param src The bash completion source file, /etc/bash_completion by default. -- @param src The bash completion source file, /etc/bash_completion by default.
function bashcomp_load(src) function bashcomp_load(src)
if src then bashcomp_src = src end if src then bashcomp_src = src end
local c = io.popen("/usr/bin/env bash -c 'source " .. bashcomp_src .. "; complete -p'") local c, err = io.popen("/usr/bin/env bash -c 'source " .. bashcomp_src .. "; complete -p'")
while true do if c then
local line = c:read("*line") while true do
if not line then break end local line = c:read("*line")
-- if a bash function is used for completion, register it if not line then break end
if line:match(".* -F .*") then -- if a bash function is used for completion, register it
bashcomp_funcs[line:gsub(".* (%S+)$","%1")] = line:gsub(".*-F +(%S+) .*$", "%1") if line:match(".* -F .*") then
bashcomp_funcs[line:gsub(".* (%S+)$","%1")] = line:gsub(".*-F +(%S+) .*$", "%1")
end
end end
c:close()
else
print(err)
end end
c:close()
end end
--- Use bash completion system to complete command and filename. --- Use bash completion system to complete command and filename.
@ -88,16 +93,20 @@ function bash(command, cur_pos, ncomp)
else else
bash_cmd = "/usr/bin/env bash -c 'compgen -A " .. comptype .. " " .. words[cword_index] .. "'" bash_cmd = "/usr/bin/env bash -c 'compgen -A " .. comptype .. " " .. words[cword_index] .. "'"
end end
local c = io.popen(bash_cmd) local c, err = io.popen(bash_cmd)
local output = {} local output = {}
i = 0 i = 0
while true do if c then
local line = c:read("*line") while true do
if not line then break end local line = c:read("*line")
table.insert(output, line) if not line then break end
end table.insert(output, line)
end
c:close() c:close()
else
print(err)
end
-- no completion, return -- no completion, return
if #output == 0 then if #output == 0 then

View File

@ -73,10 +73,14 @@ end
-- @return A string with the program output. -- @return A string with the program output.
function pread(cmd) function pread(cmd)
if cmd and cmd ~= "" then if cmd and cmd ~= "" then
local f = io.popen(cmd, 'r') local f, err = io.popen(cmd, 'r')
local s = f:read("*all") if f then
f:close() local s = f:read("*all")
return s f:close()
return s
else
print(err)
end
end end
end end