Skip to content

Commit

Permalink
Merge pull request #37 from chrox/chrox-master
Browse files Browse the repository at this point in the history
implement os.execute and log the stdout and stderr
  • Loading branch information
houqp committed Mar 8, 2016
2 parents 08f23b8 + 31fc5dd commit f028484
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion assets/android.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1303,8 +1303,42 @@ local function run(android_app_state)
android.LOGI("run command " .. table.concat(argv, " "))
return JNI:context(android.app.activity.vm, function(JNI)
local process = subprocess(JNI, argv)
local stdout = JNI:callObjectMethod(
process,
"getInputStream",
"()Ljava/io/InputStream;"
)
local stderr = JNI:callObjectMethod(
process,
"getErrorStream",
"()Ljava/io/InputStream;"
)
local out = ""
local err = ""
while true do
local char = JNI:callIntMethod(stdout, "read", "()I")
if char >= 0 then
out = out .. string.char(char)
else
break
end
end
while true do
local char = JNI:callIntMethod(stderr, "read", "()I")
if char >= 0 then
err = err .. string.char(char)
else
break
end
end
local res = JNI:callIntMethod(process, "waitFor", "()I")
android.LOGI("command stdout:" .. out)
android.LOGI("command stderr:" .. err)
android.LOGI("command res:" .. res)
JNI.env[0].DeleteLocalRef(JNI.env, process)
return 0 -- TODO: check if process is an exception
JNI.env[0].DeleteLocalRef(JNI.env, stdout)
JNI.env[0].DeleteLocalRef(JNI.env, stderr)
return res
end)
end
android.stdout = function(...)
Expand All @@ -1326,11 +1360,18 @@ local function run(android_app_state)
break
end
end
android.LOGI("command output:" .. out)
JNI.env[0].DeleteLocalRef(JNI.env, process)
JNI.env[0].DeleteLocalRef(JNI.env, stdout)
return out
end)
end
os.execute = function(command)
if command == nil then return -1 end
local argv = {}
command:gsub("([^ ]+)", function(arg) table.insert(argv, arg) end)
return android.execute(unpack(argv))
end
android.LOGI("Application data directory "..android.dir)
android.LOGI("Application library directory "..android.nativeLibraryDir)
android.LOGI("Screen size "..android.screen.width.."x"..android.screen.height)
Expand Down

0 comments on commit f028484

Please sign in to comment.