xXm0dzXx

shell

Jun 9th, 2012
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. local sUserStartup = false
  2. -- FORMAT FOR BIOS.LUA/OS MAKING:
  3. -- #*path of OS startup INSIDE OF DISK (don't add disk/)*
  4. -- *anything here will be ignored*
  5. -- *insert ur code here*
  6.  
  7.  
  8. local parentShell = shell
  9.  
  10. local bExit = false
  11. local sDir = (parentShell and parentShell.dir()) or ""
  12. local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  13. local tAliases = (parentShell and parentShell.aliases()) or {}
  14. local tProgramStack = {}
  15.  
  16. local shell = {}
  17. local tEnv = {
  18. ["shell"] = shell,
  19. }
  20.  
  21. -- Install shell API
  22. function shell.run( _sCommand, ... )
  23. local sPath = shell.resolveProgram( _sCommand )
  24. if sPath ~= nil then
  25. tProgramStack[#tProgramStack + 1] = sPath
  26. local result = os.run( tEnv, sPath, ... )
  27. tProgramStack[#tProgramStack] = nil
  28. return result
  29. else
  30. print( "No such program" )
  31. return false
  32. end
  33. end
  34.  
  35. function shell.exit()
  36. bExit = true
  37. end
  38.  
  39. function shell.dir()
  40. return sDir
  41. end
  42.  
  43. function shell.setDir( _sDir )
  44. sDir = _sDir
  45. end
  46.  
  47. function shell.path()
  48. return sPath
  49. end
  50.  
  51. function shell.setPath( _sPath )
  52. sPath = _sPath
  53. end
  54.  
  55. function shell.resolve( _sPath )
  56. local sStartChar = string.sub( _sPath, 1, 1 )
  57. if sStartChar == "/" or sStartChar == "\\" then
  58. return fs.combine( "", _sPath )
  59. else
  60. return fs.combine( sDir, _sPath )
  61. end
  62. end
  63.  
  64. function shell.resolveProgram( _sCommand )
  65. -- Substitute aliases firsts
  66. if tAliases[ _sCommand ] ~= nil then
  67. _sCommand = tAliases[ _sCommand ]
  68. end
  69.  
  70. -- If the path is a global path, use it directly
  71. local sStartChar = string.sub( _sCommand, 1, 1 )
  72. if sStartChar == "/" or sStartChar == "\\" then
  73. local sPath = fs.combine( "", _sCommand )
  74. if fs.exists( sPath ) and not fs.isDir( sPath ) then
  75. return sPath
  76. end
  77. return nil
  78. end
  79.  
  80. -- Otherwise, look on the path variable
  81. for sPath in string.gmatch(sPath, "[^:]+") do
  82. sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  83. if fs.exists( sPath ) and not fs.isDir( sPath ) then
  84. return sPath
  85. end
  86. end
  87.  
  88. -- Not found
  89. return nil
  90. end
  91.  
  92. function shell.programs( _bIncludeHidden )
  93. local tItems = {}
  94.  
  95. -- Add programs from the path
  96. for sPath in string.gmatch(sPath, "[^:]+") do
  97. sPath = shell.resolve( sPath )
  98. if fs.isDir( sPath ) then
  99. local tList = fs.list( sPath )
  100. for n,sFile in pairs( tList ) do
  101. if not fs.isDir( fs.combine( sPath, sFile ) ) and
  102. (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  103. tItems[ sFile ] = true
  104. end
  105. end
  106. end
  107. end
  108.  
  109. -- Sort and return
  110. local tItemList = {}
  111. for sItem, b in pairs( tItems ) do
  112. table.insert( tItemList, sItem )
  113. end
  114. table.sort( tItemList )
  115. return tItemList
  116. end
  117.  
  118. function shell.getRunningProgram()
  119. if #tProgramStack > 0 then
  120. return tProgramStack[#tProgramStack]
  121. end
  122. return nil
  123. end
  124.  
  125. function shell.setAlias( _sCommand, _sProgram )
  126. tAliases[ _sCommand ] = _sProgram
  127. end
  128.  
  129. function shell.clearAlias( _sCommand )
  130. tAliases[ _sCommand ] = nil
  131. end
  132.  
  133. function shell.aliases()
  134. -- Add aliases
  135. local tCopy = {}
  136. for sAlias, sCommand in pairs( tAliases ) do
  137. tCopy[sAlias] = sCommand
  138. end
  139. return tCopy
  140. end
  141.  
  142. -- Attempt to boot the disks bios.
  143. for n,sSide in pairs( redstone.getSides() ) do
  144. if disk.isPresent( sSide ) and disk.hasData( sSide ) then
  145. sDiskStartup = fs.exists( "disk/bios.lua" )
  146. if sDiskStartup then
  147. sUserStartup = true
  148. break
  149. end
  150. end
  151. end
  152.  
  153. if sUserStartup then
  154. local tLines = {}
  155. local file = io.open("disk/bios.lua", "r")
  156. local line
  157. while true do
  158. line = file:read()
  159. if not line then break end
  160. tLines[#tLines+1] = line
  161. end
  162. file:close()
  163. if string.find( tLines[1], "#" ) then
  164. sUserStartup = string.sub( tLines[1], 2, string.len( tLines[1] ) )
  165. fs.delete("disk/.bios.lua.CNFG")
  166. local file2 = fs.open("disk/.bios.lua.CNFG", "w")
  167. for i = 3, #tLines do
  168. file2.writeLine(tLines[i])
  169. end
  170. file2:close()
  171. shell.run( "disk/.bios.lua.CNFG" )
  172. shell.run( "disk/" ..sUserStartup )
  173. else
  174. print( "unsuported/corrupted bios." )
  175. sleep(2)
  176. os.shutdown()
  177. end
  178. else
  179. print( "bios undetected." )
  180. print( "Please insert a boot disk." )
  181. sleep(2)
  182. os.shutdown()
  183. end
Advertisement
Add Comment
Please, Sign In to add comment