Advertisement
rickydaan

[Lua][CC] Bios.lua unedited

Mar 8th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1.  
  2. -- Install lua parts of the os api
  3. function os.version()
  4. if turtle then
  5. return "TurtleOS 1.3"
  6. end
  7. return "CraftOS 1.3"
  8. end
  9.  
  10. function os.pullEventRaw( _sFilter )
  11. return coroutine.yield( _sFilter )
  12. end
  13.  
  14. function os.pullEvent( _sFilter )
  15. local event, p1, p2, p3, p4, p5 = os.pullEventRaw( _sFilter )
  16. if event == "terminate" then
  17. print( "Terminated" )
  18. error()
  19. end
  20. return event, p1, p2, p3, p4, p5
  21. end
  22.  
  23. -- Install globals
  24. function sleep( _nTime )
  25. local timer = os.startTimer( _nTime )
  26. repeat
  27. local sEvent, param = os.pullEvent( "timer" )
  28. until param == timer
  29. end
  30.  
  31. function write( sText )
  32. local w,h = term.getSize()
  33. local x,y = term.getCursorPos()
  34.  
  35. local nLinesPrinted = 0
  36. local function newLine()
  37. if y + 1 <= h then
  38. term.setCursorPos(1, y + 1)
  39. else
  40. term.scroll(1)
  41. term.setCursorPos(1, h)
  42. end
  43. x, y = term.getCursorPos()
  44. nLinesPrinted = nLinesPrinted + 1
  45. end
  46.  
  47. -- Print the line with proper word wrapping
  48. while string.len(sText) > 0 do
  49. local whitespace = string.match( sText, "^[ \t]+" )
  50. if whitespace then
  51. -- Print whitespace
  52. term.write( whitespace )
  53. x,y = term.getCursorPos()
  54. sText = string.sub( sText, string.len(whitespace) + 1 )
  55. end
  56.  
  57. local newline = string.match( sText, "^\n" )
  58. if newline then
  59. -- Print newlines
  60. newLine()
  61. sText = string.sub( sText, 2 )
  62. end
  63.  
  64. local text = string.match( sText, "^[^ \t\n]+" )
  65. if text then
  66. sText = string.sub( sText, string.len(text) + 1 )
  67. if string.len(text) > w then
  68. -- Print a multiline word
  69. while string.len( text ) > 0 do
  70. if x > w then
  71. newLine()
  72. end
  73. term.write( text )
  74. text = string.sub( text, (w-x) + 2 )
  75. x,y = term.getCursorPos()
  76. end
  77. else
  78. -- Print a word normally
  79. if x + string.len(text) > w then
  80. newLine()
  81. end
  82. term.write( text )
  83. x,y = term.getCursorPos()
  84. end
  85. end
  86. end
  87.  
  88. return nLinesPrinted
  89. end
  90.  
  91. function print( ... )
  92. local nLinesPrinted = 0
  93. for n,v in ipairs( { ... } ) do
  94. nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  95. end
  96. nLinesPrinted = nLinesPrinted + write( "\n" )
  97. return nLinesPrinted
  98. end
  99.  
  100. function read( _sReplaceChar, _tHistory )
  101. term.setCursorBlink( true )
  102.  
  103. local sLine = ""
  104. local nHistoryPos = nil
  105. local nPos = 0
  106. if _sReplaceChar then
  107. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  108. end
  109.  
  110. local w, h = term.getSize()
  111. local sx, sy = term.getCursorPos()
  112. local function redraw()
  113. local nScroll = 0
  114. if sx + nPos >= w then
  115. nScroll = (sx + nPos) - w
  116. end
  117.  
  118. term.setCursorPos( sx, sy )
  119. term.write( string.rep(" ", w - sx + 1) )
  120. term.setCursorPos( sx, sy )
  121. if _sReplaceChar then
  122. term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  123. else
  124. term.write( string.sub( sLine, nScroll + 1 ) )
  125. end
  126. term.setCursorPos( sx + nPos - nScroll, sy )
  127. end
  128.  
  129. while true do
  130. local sEvent, param = os.pullEvent()
  131. if sEvent == "char" then
  132. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  133. nPos = nPos + 1
  134. redraw()
  135.  
  136. elseif sEvent == "key" then
  137. if param == 28 then
  138. -- Enter
  139. break
  140.  
  141. elseif param == 203 then
  142. -- Left
  143. if nPos > 0 then
  144. nPos = nPos - 1
  145. redraw()
  146. end
  147.  
  148. elseif param == 205 then
  149. -- Right
  150. if nPos < string.len(sLine) then
  151. nPos = nPos + 1
  152. redraw()
  153. end
  154.  
  155. elseif param == 200 or param == 208 then
  156. -- Up or down
  157. if _tHistory then
  158. if param == 200 then
  159. -- Up
  160. if nHistoryPos == nil then
  161. if #_tHistory > 0 then
  162. nHistoryPos = #_tHistory
  163. end
  164. elseif nHistoryPos > 1 then
  165. nHistoryPos = nHistoryPos - 1
  166. end
  167. else
  168. -- Down
  169. if nHistoryPos == #_tHistory then
  170. nHistoryPos = nil
  171. elseif nHistoryPos ~= nil then
  172. nHistoryPos = nHistoryPos + 1
  173. end
  174. end
  175.  
  176. if nHistoryPos then
  177. sLine = _tHistory[nHistoryPos]
  178. nPos = string.len( sLine )
  179. else
  180. sLine = ""
  181. nPos = 0
  182. end
  183. redraw()
  184. end
  185. elseif param == 14 then
  186. -- Backspace
  187. if nPos > 0 then
  188. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  189. nPos = nPos - 1
  190. redraw()
  191. end
  192. end
  193. end
  194. end
  195.  
  196. term.setCursorBlink( false )
  197. term.setCursorPos( w + 1, sy )
  198. print()
  199.  
  200. return sLine
  201. end
  202.  
  203. loadfile = function( _sFile )
  204. local file = fs.open( _sFile, "r" )
  205. if file then
  206. local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  207. file.close()
  208. return func, err
  209. end
  210. return nil, "File not found"
  211. end
  212.  
  213. dofile = function( _sFile )
  214. local fnFile, e = loadfile( _sFile )
  215. if fnFile then
  216. setfenv( fnFile, getfenv(2) )
  217. fnFile()
  218. else
  219. error( e )
  220. end
  221. end
  222.  
  223. -- Install the rest of the OS api
  224. function os.run( _tEnv, _sPath, ... )
  225. local tArgs = { ... }
  226. local fnFile, err = loadfile( _sPath )
  227. if fnFile then
  228. local tEnv = _tEnv
  229. setmetatable( tEnv, { __index = _G } )
  230. setfenv( fnFile, tEnv )
  231. local ok, err = pcall( function()
  232. fnFile( unpack( tArgs ) )
  233. end )
  234. if not ok then
  235. if err and err ~= "" then
  236. print( err )
  237. end
  238. return false
  239. end
  240. return true
  241. end
  242. if err and err ~= "" then
  243. print( err )
  244. end
  245. return false
  246. end
  247.  
  248. local nativegetmetatable = getmetatable
  249. function getmetatable( _t )
  250. if type( _t ) == "string" then
  251. error( "Attempt to access string metatable" )
  252. end
  253. return nativegetmetatable( _t )
  254. end
  255.  
  256. local bProtected = true
  257. local function protect( _t )
  258. local meta = getmetatable( _t )
  259. if meta == "Protected" then
  260. -- already protected
  261. return
  262. end
  263.  
  264. setmetatable( _t, {
  265. __newindex = function( t, k, v )
  266. if bProtected then
  267. error( "Attempt to write to global" )
  268. else
  269. rawset( t, k, v )
  270. end
  271. end,
  272. __metatable = "Protected",
  273. } )
  274. end
  275.  
  276. local tAPIsLoading = {}
  277. function os.loadAPI( _sPath )
  278. local sName = fs.getName( _sPath )
  279. if tAPIsLoading[sName] == true then
  280. print( "API "..sName.." is already being loaded" )
  281. return false
  282. end
  283. tAPIsLoading[sName] = true
  284.  
  285. local tEnv = {}
  286. setmetatable( tEnv, { __index = _G } )
  287. local fnAPI, err = loadfile( _sPath )
  288. if fnAPI then
  289. setfenv( fnAPI, tEnv )
  290. fnAPI()
  291. else
  292. print( err )
  293. return false
  294. end
  295.  
  296. local tAPI = {}
  297. for k,v in pairs( tEnv ) do
  298. tAPI[k] = v
  299. end
  300. protect( tAPI )
  301.  
  302. bProtected = false
  303. _G[sName] = tAPI
  304. bProtected = true
  305.  
  306. tAPIsLoading[sName] = nil
  307. return true
  308. end
  309.  
  310. function os.unloadAPI( _sName )
  311. if _sName ~= "_G" and type(_G[_sName] == "table") then
  312. bProtected = false
  313. _G[sName] = nil
  314. bProtected = true
  315. end
  316. end
  317.  
  318. function os.sleep( _nTime )
  319. sleep( _nTime )
  320. end
  321.  
  322. local nativeShutdown = os.shutdown
  323. function os.shutdown()
  324. nativeShutdown()
  325. while true do
  326. coroutine.yield()
  327. end
  328. end
  329.  
  330. -- Install the lua part of the HTTP api (if enabled)
  331. if http then
  332. http.get = function( _url )
  333. local requestID = http.request( _url )
  334. while true do
  335. local event, param1, param2 = os.pullEvent()
  336. if event == "http_success" and param1 == _url then
  337. return param2
  338. elseif event == "http_failure" and param1 == _url then
  339. return nil
  340. end
  341. end
  342. end
  343. end
  344.  
  345. -- Install the lua part of the peripheral api
  346. peripheral.wrap = function( _sSide )
  347. if peripheral.isPresent( _sSide ) then
  348. local tMethods = peripheral.getMethods( _sSide )
  349. local tResult = {}
  350. for n,sMethod in ipairs( tMethods ) do
  351. tResult[sMethod] = function( ... )
  352. return peripheral.call( _sSide, sMethod, ... )
  353. end
  354. end
  355. return tResult
  356. end
  357. return nil
  358. end
  359.  
  360. -- Protect the global table against modifications
  361. protect( _G )
  362. for k,v in pairs( _G ) do
  363. if type(v) == "table" then
  364. protect( v )
  365. end
  366. end
  367.  
  368. -- Load APIs
  369. local tApis = fs.list( "rom/apis" )
  370. for n,sFile in ipairs( tApis ) do
  371. if string.sub( sFile, 1, 1 ) ~= "." then
  372. local sPath = fs.combine( "rom/apis", sFile )
  373. if not fs.isDir( sPath ) then
  374. os.loadAPI( sPath )
  375. end
  376. end
  377. end
  378.  
  379. if turtle then
  380. local tApis = fs.list( "rom/apis/turtle" )
  381. for n,sFile in ipairs( tApis ) do
  382. if string.sub( sFile, 1, 1 ) ~= "." then
  383. local sPath = fs.combine( "rom/apis/turtle", sFile )
  384. if not fs.isDir( sPath ) then
  385. os.loadAPI( sPath )
  386. end
  387. end
  388. end
  389. end
  390.  
  391. -- Run the shell
  392. local ok, err = pcall( function()
  393. parallel.waitForAny(
  394. function()
  395. rednet.run()
  396. end,
  397. function()
  398. os.run( {}, "rom/programs/shell" )
  399. end
  400. )
  401. end )
  402. if not ok then
  403. print( err )
  404. end
  405.  
  406. -- If the shell didn't shutdown the computer,
  407. -- it probably errored, so let the user read it.
  408. pcall( function()
  409. term.setCursorBlink( false )
  410. print( "Press any key to continue" )
  411. os.pullEvent( "key" )
  412. end )
  413. os.shutdown() -- Just in case
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement