uCon HomePageLua
Script Cmd: Lua


Lua {PATH-to-Lua-SCRIPT}

This launches Lua to run the specified Lua script.


For this uCon-specific implementation, there is a builtin 'ucon' library with the following methods (hopefully more to come):
Retreive the content of a uCon shell variable into the Lua environment.
Set the value of a uCon shell variable.
Execute a basic uCon script command (see list here) from Lua.


This basically gives the Lua script full access to uCon scripting commands. So, for example you can build a simple Lua script that has a combination of uCon script commands and Lua language contstructs...

ucon.docmd("CLS")
ucon.docmd("LOG screenoff")
ucon.docmd("RECV blkinit 2048")
ucon.docmd("SEND -p \"U-Boot >\" \"md $loadaddr 16\"")
ucon.docmd("RECV blkcpy")
ucon.docmd("LOG screenon")
resp=ucon.getenv("RECVBLOCK")
io.write(string.format("\nTotal response is %d bytes...\n",string.len(resp)))
print(resp)


The above script generates output that looks like this (you gotta know u-boot a bit to follow this)...

Total response is       420      bytes...
md $loadaddr 16
12000000: cedf5f3d f7e54fdb bbfbf375 de7745cf    =_...O..u....Ew.
12000010: e7e29eea fd5b76d5 ffebfcff 7fddb57f    .....v[.........
12000020: fef373dd 77eb57fd ecde77ae fae45f7d    .s...W.w.w..}_..
12000030: e6bbbaad c9e77156 576edeba d5fadf54    ....Vq....nWT...
12000040: ebeeefbd 3df6e7fd beeedfc3 9ddd77f3    .......=.....w..
12000050: bbbf7afb ff5dddbb                      .z....].
U-Boot > 

The fundamental point to take away from this is that you can use the uCon library embedded in Lua (ucon.getenv(), ucon.setenv() & ucon.docmd()) to invoke basic uCon script commands from within a Lua script.  This gives you the power of Luawithout losing any of the functionality of uCon.

Some good pointers to Lua information...

Main documentation:
http://www.lua.org/docs.html.

Reference manual:
http://www.lua.org/manual/5.2/manual.html

Tutorials/Examples:
http://lua-users.org/wiki/TutorialDirectory
http://lua-users.org/wiki/SampleCode

NOTE1:
You can also invoke a Lua script the same way a regular uCon script would be invoked (through buttons, function keys or the Scripts->Run menuitem) by suffixing the script name with ".lua".

More examples:

Ex1...
Simple example showing the use of functions and formatting printing...

function greet_joe()
  io.write(string.format("Hi Joe, good to see you again.\n"))
end

function greet_ed()
  io.write(string.format("Hi Ed, haven't seen you in a while!\n"))
end

function greet_stranger(name)
  io.write(string.format("Hi %s, my name is uCon.\n",name))
end

ucon.docmd("CLS")
io.write("What is your name?\n")
name = io.read("*l")
if string.lower(name) == "ed" then
  greet_ed()
elseif string.lower(name) == "joe" then
  greet_joe()
else
  greet_stranger(name)
end


Ex2...
Assume you want to grab a portion of your screen buffer for editing.  You could scroll back to it and cut-n-paste it into a file editor manually or you could do it with Lua.  In the case below, I wanted to clip out a section of the screen buffer starting at a line that has STATS and ending with a line that has STOP:.

-- Create temporary file and copy the current screen buffer to that file:
tmpfile = os.mktemp("C:/tmp")
rc = ucon.docmd("FILE copyscreen " .. tmpfile)
if rc ~= 0 then
  print("Screen copy failed: ",rc)
  return
end

-- Open the file and clear the console screen:
printing_stats = false
ucon.docmd("CLS")
file = io.open(tmpfile,"r")

-- Walk through each line of the file looking for "STATS" (to turn on
-- printing to the console) and "STOP:" (to turn off printing to the
-- console).
while true do
  line = file:read()
  if line == nil then break end
  if printing_stats == true then print(line) end
  if string.find(line,"STATS",1,plain) ~= nil then
    printing_stats = true
    print(line)
  elseif string.find(line,"STOP:",1,plain) ~= nil then
    printing_stats = false
  end
 
end

-- Close and remove the temporary file:
file:close()
os.remove(tmpfile)

-- Create a second temp file, and copy the new screen (created above)
-- into it.
tmpfile = os.mktemp("C:/tmp/")
rc = ucon.docmd("FILE copyscreen " .. tmpfile)
if rc ~= 0 then
  print("Screen copy failed: ",rc)
  return
end

-- Open that new file in your local editor:
ucon.docmd("FILE edit " .. tmpfile)


Ex3...
This script is used to automate a connection to  Synaccess NPC-22 box.  It asks the user a few questions using the DIALOG command, and from that, builds a command string to be used to telnet into the NPC-22 unit and have it turn on/off or reboot one of the systems I have connected to it...

-- Ask the user for the IP address of the NPC22 if it isn't
-- already established...
function npc_getipadd()
  npc_ipadd = ucon.getenv("NPC22_IPADD")
  if (npc_ipadd ~= nil) then return end
  ucon.docmd("DIALOG Q_AND_A \"IP Address of NPC-22:\"")
  npc_ipadd = ucon.getenv("DIALOG")
  ucon.setenv("NPC22_IPADD",npc_ipadd)
end

-- Ask for the operation to be performed.
-- This establishes the command (power on/off or reboot) and the port
-- connection on the NPC22 (1 or 2).
function npc_getoperation()
  ucon.docmd("DIALOG DDLIST \"What do you want to do?\" BFIN_ON BFIN_OFF BFIN_REBOOT PPC_ON PPC_OFF PPC_REBOOT")
  op = ucon.getenv("DIALOG")
  if op == "BFIN_ON" then npc_port=1 npc_operation="PowerOn" end
  if op == "BFIN_OFF" then npc_port=1 npc_operation="PowerOff" end
  if op == "BFIN_REBOOT" then npc_port=1 npc_operation="Reboot" end
  if op == "PPC_ON" then npc_port=2 npc_operation="PowerOn" end
  if op == "PPC_OFF" then npc_port=2 npc_operation="PowerOff" end
  if op == "PPC_REBOOT" then npc_port=2 npc_operation="Reboot" end
end

function clearscreen()
  ucon.docmd("CLS")
end

function sleep(msec)
  ucon.docmd(string.format("SLEEP %d",msec))
end

function npc_poweron()
  ucon.docmd(string.format("SOCKET WRITE %s \"/pset %d 1\\n\"",npc_ipadd,npc_port))
end

function npc_poweroff()
  ucon.docmd(string.format("SOCKET WRITE %s \"/pset %d 0\\n\"",npc_ipadd,npc_port))
end

function npc_reboot()
  ucon.docmd(string.format("SOCKET WRITE %s \"/rb %d\\n\"",npc_ipadd,npc_port))
end

-- When npc_control() is called, we know all we need to know to perform
-- the requested operation, so go do it...
function npc_control()
  io.write("\n")
  io.write(" ************************************************\n")
  io.write(string.format(" * CMD: %s, PORT: %d on NPC22: %s\n",npc_operation,npc_port,npc_ipadd))
  io.write(" ************************************************\n")
  ucon.docmd(string.format("SOCKET OPEN %s 23 TCP", npc_ipadd))
  ucon.docmd(string.format("SOCKET -t2 FLUSH %s", npc_ipadd))
  ucon.docmd(string.format("SOCKET WRITE %s \"/login\\n\"",npc_ipadd))
  sleep(300)
  ucon.docmd(string.format("SOCKET WRITE %s \"admin\\n\"",npc_ipadd))
  sleep(300)
  ucon.docmd(string.format("SOCKET WRITE %s \"admin\\n\"",npc_ipadd))
  sleep(300)
  if npc_operation == "PowerOn" then npc_poweron() end
  if npc_operation == "PowerOff" then npc_poweroff() end
  if npc_operation == "Reboot" then npc_reboot() end
  sleep(300)
  ucon.docmd(string.format("SOCKET WRITE %s \"/logout\\n\"",npc_ipadd))
  ucon.docmd(string.format("SOCKET CLOSE %s",npc_ipadd))
end


-- Main...
-- Clear the uCon screen.
-- Get the IP address of the NPC22 unit.
-- Ask the user what they want us to do.
-- Go do it.
clearscreen()
npc_getipadd()
npc_getoperation()
npc_control()