luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

init.lua (9925B)


      1 -- CLI Tools - File system utilities
      2 -- Exports: ls, read, write, mv, cp commands
      3 
      4 -- Helper to parse command and arguments
      5 local function parseArgs(argStr)
      6     if not argStr or argStr == "" then
      7         return {}
      8     end
      9 
     10     local args = {}
     11     local inQuote = false
     12     local current = ""
     13 
     14     for i = 1, #argStr do
     15         local c = argStr:sub(i, i)
     16         if c == '"' then
     17             inQuote = not inQuote
     18         elseif c == ' ' and not inQuote then
     19             if #current > 0 then
     20                 table.insert(args, current)
     21                 current = ""
     22             end
     23         else
     24             current = current .. c
     25         end
     26     end
     27 
     28     if #current > 0 then
     29         table.insert(args, current)
     30     end
     31 
     32     return args
     33 end
     34 
     35 -- Export: ls - List directory contents
     36 app:export({
     37     name = "ls",
     38     func = function(path)
     39         path = path or "/"
     40 
     41         if not CRamdiskExists or not CRamdiskList then
     42             return nil, "Ramdisk functions not available"
     43         end
     44 
     45         if not CRamdiskExists(path) then
     46             return nil, "Path not found: " .. path
     47         end
     48 
     49         local entries = CRamdiskList(path)
     50         if not entries then
     51             return nil, "Could not list directory"
     52         end
     53 
     54         local result = {}
     55         for _, entry in ipairs(entries) do
     56             if entry.type == "dir" or entry.type == "directory" then
     57                 table.insert(result, entry.name .. "/")
     58             else
     59                 table.insert(result, entry.name)
     60             end
     61         end
     62 
     63         return table.concat(result, " ")
     64     end,
     65     args = "path=string",
     66     rets = "string",
     67     description = "List directory contents"
     68 })
     69 
     70 -- Export: read - Read file contents
     71 app:export({
     72     name = "read",
     73     func = function(path)
     74         if not path then
     75             return nil, "Usage: read <filepath>"
     76         end
     77 
     78         if not CRamdiskOpen or not CRamdiskRead or not CRamdiskClose then
     79             return nil, "Ramdisk functions not available"
     80         end
     81 
     82         local handle = CRamdiskOpen(path, "r")
     83         if not handle then
     84             return nil, "Could not open file: " .. path
     85         end
     86 
     87         local content = CRamdiskRead(handle)
     88         CRamdiskClose(handle)
     89 
     90         return content or ""
     91     end,
     92     args = "path=string",
     93     rets = "string",
     94     description = "Read file contents"
     95 })
     96 
     97 -- Export: write - Write file contents
     98 app:export({
     99     name = "write",
    100     func = function(path, content)
    101         if not path or not content then
    102             return nil, "Usage: write <filepath> <content>"
    103         end
    104 
    105         if not CRamdiskOpen or not CRamdiskWrite or not CRamdiskClose then
    106             return nil, "Ramdisk functions not available"
    107         end
    108 
    109         local handle = CRamdiskOpen(path, "w")
    110         if not handle then
    111             return nil, "Could not open file for writing: " .. path
    112         end
    113 
    114         local success = CRamdiskWrite(handle, content)
    115         CRamdiskClose(handle)
    116 
    117         if success then
    118             return "Written to " .. path
    119         else
    120             return nil, "Failed to write to " .. path
    121         end
    122     end,
    123     args = "path=string,content=string",
    124     rets = "string",
    125     description = "Write content to file"
    126 })
    127 
    128 -- Export: cp - Copy file
    129 app:export({
    130     name = "cp",
    131     func = function(source, dest)
    132         if not source or not dest then
    133             return nil, "Usage: cp <source> <dest>"
    134         end
    135 
    136         if not CRamdiskOpen or not CRamdiskRead or not CRamdiskWrite or not CRamdiskClose then
    137             return nil, "Ramdisk functions not available"
    138         end
    139 
    140         -- Read source file
    141         local readHandle = CRamdiskOpen(source, "r")
    142         if not readHandle then
    143             return nil, "Could not open source file: " .. source
    144         end
    145 
    146         local content = CRamdiskRead(readHandle)
    147         CRamdiskClose(readHandle)
    148 
    149         if not content then
    150             return nil, "Could not read source file"
    151         end
    152 
    153         -- Write to destination
    154         local writeHandle = CRamdiskOpen(dest, "w")
    155         if not writeHandle then
    156             return nil, "Could not open destination file: " .. dest
    157         end
    158 
    159         local success = CRamdiskWrite(writeHandle, content)
    160         CRamdiskClose(writeHandle)
    161 
    162         if success then
    163             return "Copied " .. source .. " to " .. dest
    164         else
    165             return nil, "Failed to write to destination"
    166         end
    167     end,
    168     args = "source=string,dest=string",
    169     rets = "string",
    170     description = "Copy file from source to destination"
    171 })
    172 
    173 -- Export: mv - Move/rename file
    174 app:export({
    175     name = "mv",
    176     func = function(source, dest)
    177         if not source or not dest then
    178             return nil, "Usage: mv <source> <dest>"
    179         end
    180 
    181         if not CRamdiskOpen or not CRamdiskRead or not CRamdiskWrite or not CRamdiskClose or not CRamdiskRemove then
    182             return nil, "Ramdisk functions not available"
    183         end
    184 
    185         -- Read source file
    186         local readHandle = CRamdiskOpen(source, "r")
    187         if not readHandle then
    188             return nil, "Could not open source file: " .. source
    189         end
    190 
    191         local content = CRamdiskRead(readHandle)
    192         CRamdiskClose(readHandle)
    193 
    194         if not content then
    195             return nil, "Could not read source file"
    196         end
    197 
    198         -- Write to destination
    199         local writeHandle = CRamdiskOpen(dest, "w")
    200         if not writeHandle then
    201             return nil, "Could not open destination file: " .. dest
    202         end
    203 
    204         local success = CRamdiskWrite(writeHandle, content)
    205         CRamdiskClose(writeHandle)
    206 
    207         if not success then
    208             return nil, "Failed to write to destination"
    209         end
    210 
    211         -- Remove source file
    212         local removeSuccess = CRamdiskRemove(source)
    213         if removeSuccess then
    214             return "Moved " .. source .. " to " .. dest
    215         else
    216             return nil, "File copied but could not remove source"
    217         end
    218     end,
    219     args = "source=string,dest=string",
    220     rets = "string",
    221     description = "Move/rename file from source to destination"
    222 })
    223 
    224 -- Export: open - Open file with registered handler
    225 app:export({
    226     name = "open",
    227     func = function(path)
    228         if not path then
    229             return nil, "Usage: open <filepath>"
    230         end
    231 
    232         if not sys or not sys.openFile then
    233             return nil, "sys.openFile not available"
    234         end
    235 
    236         local success, err = sys.openFile(path)
    237         if success then
    238             return "Opened " .. path
    239         else
    240             return nil, err or "Failed to open file"
    241         end
    242     end,
    243     args = "path=string",
    244     rets = "string",
    245     description = "Open file with registered handler application"
    246 })
    247 
    248 -- Export: echo - Print arguments
    249 app:export({
    250     name = "echo",
    251     func = function(...)
    252         local args = {...}
    253         if #args == 0 then
    254             return ""
    255         end
    256         return table.concat(args, " ")
    257     end,
    258     args = "...=string",
    259     rets = "string",
    260     description = "Print arguments to output"
    261 })
    262 
    263 -- Counter for unique timer names
    264 local timeoutCounter = 0
    265 
    266 -- Export: timeout - Execute command after delay
    267 app:export({
    268     name = "timeout",
    269     func = function(seconds, ...)
    270         if not seconds then
    271             return nil, "Usage: timeout <seconds> <command...>"
    272         end
    273 
    274         local delay = tonumber(seconds)
    275         if not delay or delay < 0 then
    276             return nil, "Invalid delay: " .. tostring(seconds)
    277         end
    278 
    279         local cmdArgs = {...}
    280         if #cmdArgs == 0 then
    281             return nil, "Usage: timeout <seconds> <command...>"
    282         end
    283 
    284         if not Timer then
    285             return nil, "Timer not available"
    286         end
    287 
    288         -- Build command string from remaining args
    289         local cmdStr = table.concat(cmdArgs, " ")
    290 
    291         -- Create unique timer name
    292         timeoutCounter = timeoutCounter + 1
    293         local timerName = "timeout_" .. timeoutCounter
    294 
    295         -- Create timer that will execute the command
    296         Timer.simple(timerName, delay, function(timer)
    297             -- Execute the command via shell or just print if it's echo
    298             if cmdArgs[1] == "echo" then
    299                 -- Handle echo directly
    300                 local output = {}
    301                 for i = 2, #cmdArgs do
    302                     table.insert(output, cmdArgs[i])
    303                 end
    304                 print(table.concat(output, " "))
    305             else
    306                 -- Try to execute as exported function
    307                 if apps and apps[cmdArgs[1]] then
    308                     local func = apps[cmdArgs[1]]
    309                     local funcArgs = {}
    310                     for i = 2, #cmdArgs do
    311                         table.insert(funcArgs, cmdArgs[i])
    312                     end
    313                     local result, err = func(unpack(funcArgs))
    314                     if result then
    315                         print(result)
    316                     elseif err then
    317                         print("Error: " .. err)
    318                     end
    319                 else
    320                     print("Unknown command: " .. cmdArgs[1])
    321                 end
    322             end
    323         end)
    324 
    325         return "Timer set for " .. delay .. " seconds: " .. cmdStr
    326     end,
    327     args = "seconds=number,...=string",
    328     rets = "string",
    329     description = "Execute command after specified delay in seconds"
    330 })
    331 
    332 -- Print available commands
    333 if args and args[1] then
    334     print("CLI Tools - Available commands:")
    335     print("  ls [path]           - List directory contents")
    336     print("  read <file>         - Read file contents")
    337     print("  write <file> <txt>  - Write text to file")
    338     print("  cp <src> <dst>      - Copy file")
    339     print("  mv <src> <dst>      - Move/rename file")
    340     print("  open <file>         - Open file with registered app")
    341     print("  echo <args...>      - Print arguments")
    342     print("  timeout <sec> <cmd> - Execute command after delay")
    343 else
    344     print("CLI Tools loaded. Use 'clitools help' for command list.")
    345 end