Chapter 5: Scripting
Complete example of a Lua script 55
55555
Scripting
resp = resp .. resp_part
if extra_char and not pr then
stream:write(extra_char)
extra_char = nil
end
until extra_char == nil
if resp then
local patt
-- remove trailing prompt
patt = make_pattern(PROMPT) .. "$"
resp = string.gsub(resp, patt, "\r\n", 1)
-- remove the command itself
patt = "^%s*" .. make_pattern(cmd) .. "[\r\n]*"
resp = string.gsub(resp, patt, "", 1)
end
return resp, pr
end
function print_cmd_result(stream, cmd, extra)
local result = send_cmd(stream,cmd,extra)
if result then
print(cmd .. " ---->")
print(result)
end
end
-- main chunk
if os.getenv("TCU") then
1
-- TCU
devname = "/dev/minic"
print("TCU environment")
elseif os.getenv("HOME") then
-- Linux
devname = "/dev/ttyS0"
os.execute("stty -F /dev/ttyS0 raw time 1 min 0 ispeed 38400 ospeed
38400 -echo")
print("Linux environment")
else
-- unsupported system
error("Environment not supported")
end
f = assert(io.open(devname,"r+"))
PROMPT = detect_prompt(f)
assert(PROMPT)
print_cmd_result(f, "st -w")
2
f:close()
1. Platform check
2. Printing mini-C communication