Advertisement
kd2bwzgen

cclinux installer

Jun 1st, 2017
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.93 KB | None | 0 0
  1. inputTable = {
  2. bin = {
  3. which = "print(shell.resolveProgram(...))",
  4. login = "os.loadAPI(\"/kernel/sha256\")\
  5. term.clear()\
  6. term.setCursorPos(1,1)\
  7. if term.isColor() then term.setTextColor(colors.yellow) end\
  8. print(os.version()..\": Login\")\
  9. if term.isColor() then term.setTextColor(colors.white) end\
  10. write(\"Username: \")\
  11. local user = read()\
  12. write(\"Password: \")\
  13. local pass = read(\"\")\
  14. local h = fs.open(\"/etc/passwd\",\"r\")\
  15. local passwd = textutils.unserialize(h.readAll())\
  16. h.close()\
  17. if next(passwd)==nil then return \"root\",\"\" end\
  18. if passwd[user] == nil then\
  19. printError(\"Invalid user or password\")\
  20. sleep(3)\
  21. os.shutdown()\
  22. else\
  23. if passwd[user].pass ~= sha256.sha256(pass) then\
  24. printError(\"Invalid user or password\")\
  25. sleep(3)\
  26. os.shutdown()\
  27. end\
  28. end\
  29. if bash then\
  30. bash.user = function() return user end\
  31. bash.curHomeDir = function() return passwd[user].homedir end\
  32. else\
  33. return user,passwd[user].homedir\
  34. end",
  35. cat = "local tArgs = {...}\
  36. if #tArgs~=1 then\
  37. print(\"Usage: cat <file>\")\
  38. error(\"\",0)\
  39. end\
  40. local h = fs.open(tArgs[1],\"r\")\
  41. if not h then error(\"File not found\",0) end\
  42. print(h.readAll())\
  43. h.close()",
  44. version = "print(os.version())",
  45. panic = "prevState = {}\
  46. prevState.isCCLinux = os.isCCLinux\
  47. prevState.version = os.version\
  48. prevState.dir = shell.dir()\
  49. os.isCCLinux = nil\
  50. os.version = function() return \"CraftOS 1.7\" end\
  51. term.clear()\
  52. term.setCursorPos(1,1)\
  53. shell.setDir(\"\")\
  54. shell.run(\"shell\")\
  55. term.clear()\
  56. term.setCursorPos(1,1)\
  57. os.isCCLinux = prevState.isCCLinux\
  58. os.version = prevState.version\
  59. shell.setDir(prevState.dir)",
  60. mkuser = "os.loadAPI(\"/kernel/sha256\")\
  61. local h = fs.open(\"/etc/passwd\",\"r\")\
  62. local passwd = textutils.unserialise(h.readAll())\
  63. h.close()\
  64. write(\"Username: \")\
  65. local name = read()\
  66. passwd[name] = {}\
  67. passwd[name].homedir = \"home/\"..name\
  68. if not fs.exists(passwd[name].homedir) then fs.makeDir(passwd[name].homedir) end\
  69. write(\"Password: \")\
  70. passwd[name].pass = sha256.sha256(read(\"\"))\
  71. h = fs.open(\"/etc/passwd\",\"w\")\
  72. h.write(textutils.serialise(passwd))\
  73. h.close()",
  74. less = "local tArgs = {...}\
  75. if #tArgs ~= 1 then\
  76. print(\"Usage: less <path>\")\
  77. error(\"\",0)\
  78. end\
  79. if not fs.exists(tArgs[1]) then error(\"File not found\",0) end\
  80. term.clear()\
  81. term.setCursorPos(1,1)\
  82. local lineNum = 1\
  83. local lines = {}\
  84. local w,h = term.getSize()\
  85. h = h - 2 -- Weird hack to make long file handling work\
  86. local function getFile(n)\
  87. local ret = {}\
  88. local h = fs.open(n,\"r\")\
  89. local nextLine = h.readLine()\
  90. while nextLine do\
  91. table.insert(ret,nextLine)\
  92. nextLine = h.readLine()\
  93. end\
  94. return ret\
  95. end\
  96. \
  97. lines = getFile(tArgs[1])\
  98. \
  99. local function showLines(l,n)\
  100. local atbottom = (n>(#l-h))\
  101. local bottom = #l-h\
  102. for i=(atbottom and bottom or n),(atbottom and #l or n+h) do\
  103. if l[i] then print(l[i]) else print() end\
  104. end\
  105. end\
  106. \
  107. local first = true\
  108. \
  109. while true do\
  110. showLines(lines,lineNum)\
  111. local _,k = os.pullEvent(\"key\")\
  112. if k == keys.down then lineNum = lineNum + 1 end\
  113. if k == keys.up then lineNum = lineNum - 1 end\
  114. if lineNum > #lines then lineNum = #lines end\
  115. if lineNum < 1 then lineNum = 1 end\
  116. if k == keys.q then\
  117. os.pullEvent(\"char\")\
  118. error(\"\",0)\
  119. end\
  120. term.clear()\
  121. term.setCursorPos(1,1)\
  122. end",
  123. whoami = "if bash then print(bash.user()) else print(\"Unknown\") end",
  124. passwd = "os.loadAPI(\"/kernel/sha256\")\
  125. local h = fs.open(\"/etc/passwd\",\"r\")\
  126. local passwd = textutils.unserialise(h.readAll())\
  127. h.close()\
  128. local name = ...\
  129. if name ~= bash.user() and bash.user()~=\"root\" then error(\"Can't set someone else's password!\",0) end\
  130. write(\"Password: \")\
  131. passwd[name].pass = sha256.sha256(read(\"\"))\
  132. h = fs.open(\"/etc/passwd\",\"w\")\
  133. h.write(textutils.serialise(passwd))\
  134. h.close()",
  135. },
  136. [ ".settings" ] = "{\
  137. [ \"bios.use_multishell\" ] = true,\
  138. [ \"shell.autocomplete\" ] = true,\
  139. [ \"shell.allow_disk_startup\" ] = true,\
  140. [ \"shell.allow_startup\" ] = true,\
  141. [ \"lua.autocomplete\" ] = true,\
  142. [ \"list.show_hidden\" ] = false,\
  143. [ \"bash.autocomplete\" ] = true,\
  144. [ \"edit.autocomplete\" ] = true,\
  145. }",
  146. kernel = {
  147. sha256 = "\
  148. -- \
  149. -- Adaptation of the Secure Hashing Algorithm (SHA-244/256)\
  150. -- Found Here: http://lua-users.org/wiki/SecureHashAlgorithm\
  151. -- \
  152. -- Using an adapted version of the bit library\
  153. -- Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua\
  154. -- \
  155. \
  156. local MOD = 2^32\
  157. local MODM = MOD-1\
  158. \
  159. local function memoize(f)\
  160. \009local mt = {}\
  161. \009local t = setmetatable({}, mt)\
  162. \009function mt:__index(k)\
  163. \009\009local v = f(k)\
  164. \009\009t[k] = v\
  165. \009\009return v\
  166. \009end\
  167. \009return t\
  168. end\
  169. \
  170. local function make_bitop_uncached(t, m)\
  171. \009local function bitop(a, b)\
  172. \009\009local res,p = 0,1\
  173. \009\009while a ~= 0 and b ~= 0 do\
  174. \009\009\009local am, bm = a % m, b % m\
  175. \009\009\009res = res + t[am][bm] * p\
  176. \009\009\009a = (a - am) / m\
  177. \009\009\009b = (b - bm) / m\
  178. \009\009\009p = p*m\
  179. \009\009end\
  180. \009\009res = res + (a + b) * p\
  181. \009\009return res\
  182. \009end\
  183. \009return bitop\
  184. end\
  185. \
  186. local function make_bitop(t)\
  187. \009local op1 = make_bitop_uncached(t,2^1)\
  188. \009local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)\
  189. \009return make_bitop_uncached(op2, 2 ^ (t.n or 1))\
  190. end\
  191. \
  192. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})\
  193. \
  194. local function bxor(a, b, c, ...)\
  195. \009local z = nil\
  196. \009if b then\
  197. \009\009a = a % MOD\
  198. \009\009b = b % MOD\
  199. \009\009z = bxor1(a, b)\
  200. \009\009if c then z = bxor(z, c, ...) end\
  201. \009\009return z\
  202. \009elseif a then return a % MOD\
  203. \009else return 0 end\
  204. end\
  205. \
  206. local function band(a, b, c, ...)\
  207. \009local z\
  208. \009if b then\
  209. \009\009a = a % MOD\
  210. \009\009b = b % MOD\
  211. \009\009z = ((a + b) - bxor1(a,b)) / 2\
  212. \009\009if c then z = bit32_band(z, c, ...) end\
  213. \009\009return z\
  214. \009elseif a then return a % MOD\
  215. \009else return MODM end\
  216. end\
  217. \
  218. local function bnot(x) return (-1 - x) % MOD end\
  219. \
  220. local function rshift1(a, disp)\
  221. \009if disp < 0 then return lshift(a,-disp) end\
  222. \009return math.floor(a % 2 ^ 32 / 2 ^ disp)\
  223. end\
  224. \
  225. local function rshift(x, disp)\
  226. \009if disp > 31 or disp < -31 then return 0 end\
  227. \009return rshift1(x % MOD, disp)\
  228. end\
  229. \
  230. local function lshift(a, disp)\
  231. \009if disp < 0 then return rshift(a,-disp) end \
  232. \009return (a * 2 ^ disp) % 2 ^ 32\
  233. end\
  234. \
  235. local function rrotate(x, disp)\
  236. x = x % MOD\
  237. disp = disp % 32\
  238. local low = band(x, 2 ^ disp - 1)\
  239. return rshift(x, disp) + lshift(low, 32 - disp)\
  240. end\
  241. \
  242. local k = {\
  243. \0090x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\
  244. \0090x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
  245. \0090xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\
  246. \0090x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
  247. \0090xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\
  248. \0090x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
  249. \0090x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\
  250. \0090xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
  251. \0090x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\
  252. \0090x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
  253. \0090xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\
  254. \0090xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
  255. \0090x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\
  256. \0090x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
  257. \0090x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\
  258. \0090x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,\
  259. }\
  260. \
  261. local function str2hexa(s)\
  262. \009return (string.gsub(s, \".\", function(c) return string.format(\"%02x\", string.byte(c)) end))\
  263. end\
  264. \
  265. local function num2s(l, n)\
  266. \009local s = \"\"\
  267. \009for i = 1, n do\
  268. \009\009local rem = l % 256\
  269. \009\009s = string.char(rem) .. s\
  270. \009\009l = (l - rem) / 256\
  271. \009end\
  272. \009return s\
  273. end\
  274. \
  275. local function s232num(s, i)\
  276. \009local n = 0\
  277. \009for i = i, i + 3 do n = n*256 + string.byte(s, i) end\
  278. \009return n\
  279. end\
  280. \
  281. local function preproc(msg, len)\
  282. \009local extra = 64 - ((len + 9) % 64)\
  283. \009len = num2s(8 * len, 8)\
  284. \009msg = msg .. \"\\128\" .. string.rep(\"\\0\", extra) .. len\
  285. \009assert(#msg % 64 == 0)\
  286. \009return msg\
  287. end\
  288. \
  289. local function initH256(H)\
  290. \009H[1] = 0x6a09e667\
  291. \009H[2] = 0xbb67ae85\
  292. \009H[3] = 0x3c6ef372\
  293. \009H[4] = 0xa54ff53a\
  294. \009H[5] = 0x510e527f\
  295. \009H[6] = 0x9b05688c\
  296. \009H[7] = 0x1f83d9ab\
  297. \009H[8] = 0x5be0cd19\
  298. \009return H\
  299. end\
  300. \
  301. local function digestblock(msg, i, H)\
  302. \009local w = {}\
  303. \009for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end\
  304. \009for j = 17, 64 do\
  305. \009\009local v = w[j - 15]\
  306. \009\009local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))\
  307. \009\009v = w[j - 2]\
  308. \009\009w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))\
  309. \009end\
  310. \
  311. \009local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]\
  312. \009for i = 1, 64 do\
  313. \009\009local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))\
  314. \009\009local maj = bxor(band(a, b), band(a, c), band(b, c))\
  315. \009\009local t2 = s0 + maj\
  316. \009\009local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))\
  317. \009\009local ch = bxor (band(e, f), band(bnot(e), g))\
  318. \009\009local t1 = h + s1 + ch + k[i] + w[i]\
  319. \009\009h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2\
  320. \009end\
  321. \
  322. \009H[1] = band(H[1] + a)\
  323. \009H[2] = band(H[2] + b)\
  324. \009H[3] = band(H[3] + c)\
  325. \009H[4] = band(H[4] + d)\
  326. \009H[5] = band(H[5] + e)\
  327. \009H[6] = band(H[6] + f)\
  328. \009H[7] = band(H[7] + g)\
  329. \009H[8] = band(H[8] + h)\
  330. end\
  331. \
  332. function sha256(msg)\
  333. \009msg = preproc(msg, #msg)\
  334. \009local H = initH256({})\
  335. \009for i = 1, #msg, 64 do digestblock(msg, i, H) end\
  336. \009return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..\
  337. \009\009num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))\
  338. end",
  339. boot = "--TLCO\
  340. local tlco_lp = {\
  341. function() os.run({},\"/kernel/bash\") end\
  342. }\
  343. local prevState = {}\
  344. prevState.shutdown = function() end\
  345. local function recover()\
  346. --Fix os.shutdown\
  347. os.shutdown = prevState.shutdown\
  348. --Ditch rednet\
  349. _G[\"rednet\"] = nil\
  350. end\
  351. local function _os_shutdown()\
  352. recover()\
  353. term.clear()\
  354. term.setCursorPos(1,1)\
  355. os.isCCLinux = function() return true end\
  356. os.version = function() return \"CCLinux v0.1b\" end\
  357. parallel.waitForAny(unpack(tlco_lp))\
  358. sleep(3)\
  359. os.shutdown()\
  360. end\
  361. local function inject()\
  362. prevState.shutdown = os.shutdown\
  363. os.shutdown = _os_shutdown\
  364. end\
  365. local function exploit()\
  366. inject()\
  367. shell.exit()\
  368. end\
  369. exploit()",
  370. bash = "local multishell = multishell\
  371. local parentShell = bash\
  372. local parentTerm = term.current()\
  373. \
  374. if multishell then\
  375. multishell.setTitle( multishell.getCurrent(), \"bash\" )\
  376. end\
  377. \
  378. local bExit = false\
  379. local sDir = (parentShell and parentShell.dir()) or \"\"\
  380. local sPath = (parentShell and parentShell.path()) or \".:/rom/programs\"\
  381. local tAliases = (parentShell and parentShell.aliases()) or {[\"bash\"]=\"/kernel/bash\"}\
  382. local tCompletionInfo = (parentShell and parentShell.getCompletionInfo()) or {}\
  383. local tProgramStack = {}\
  384. \
  385. local bash = {}\
  386. local tEnv = {\
  387. \009[ \"shell\" ] = bash,\
  388. \009[ \"bash\" ] = bash,\
  389. \009[ \"multishell\" ] = multishell,\
  390. }\
  391. \
  392. -- Colours\
  393. local promptColour, textColour, bgColour\
  394. if term.isColour() then\
  395. \009promptColour = colours.yellow\
  396. \009textColour = colours.white\
  397. \009bgColour = colours.black\
  398. else\
  399. \009promptColour = colours.white\
  400. \009textColour = colours.white\
  401. \009bgColour = colours.black\
  402. end\
  403. \
  404. local function run( _sCommand, ... )\
  405. \009local sPath = bash.resolveProgram( _sCommand )\
  406. \009if sPath ~= nil then\
  407. \009\009tProgramStack[#tProgramStack + 1] = sPath\
  408. \009\009if multishell then\
  409. \009\009 multishell.setTitle( multishell.getCurrent(), fs.getName( sPath ) )\
  410. \009\009end\
  411. \009\009local result = os.run( tEnv, sPath, ... )\
  412. \009\009tProgramStack[#tProgramStack] = nil\
  413. \009\009if multishell then\
  414. \009\009 if #tProgramStack > 0 then\
  415. \009\009 multishell.setTitle( multishell.getCurrent(), fs.getName( tProgramStack[#tProgramStack] ) )\
  416. \009\009else\
  417. \009\009 multishell.setTitle( multishell.getCurrent(), \"shell\" )\
  418. \009\009end\
  419. \009\009end\
  420. \009\009return result\
  421. \009else\
  422. \009printError( \"No such program\" )\
  423. \009return false\
  424. end\
  425. end\
  426. \
  427. local function tokenise( ... )\
  428. local sLine = table.concat( { ... }, \" \" )\
  429. \009local tWords = {}\
  430. local bQuoted = false\
  431. for match in string.gmatch( sLine .. \"\\\"\", \"(.-)\\\"\" ) do\
  432. if bQuoted then\
  433. table.insert( tWords, match )\
  434. else\
  435. for m in string.gmatch( match, \"[^ \\t]+\" ) do\
  436. table.insert( tWords, m )\
  437. end\
  438. end\
  439. bQuoted = not bQuoted\
  440. end\
  441. return tWords\
  442. end\
  443. \
  444. -- Install bash API\
  445. function bash.run( ... )\
  446. \009local tWords = tokenise( ... )\
  447. \009local sCommand = tWords[1]\
  448. \009if sCommand then\
  449. \009\009return run( sCommand, table.unpack( tWords, 2 ) )\
  450. \009end\
  451. \009return false\
  452. end\
  453. \
  454. function bash.exit()\
  455. bExit = true\
  456. end\
  457. \
  458. function bash.dir()\
  459. \009return sDir\
  460. end\
  461. \
  462. function bash.setDir( _sDir )\
  463. \009sDir = _sDir\
  464. end\
  465. \
  466. function bash.path()\
  467. \009return sPath\
  468. end\
  469. \
  470. function bash.setPath( _sPath )\
  471. \009sPath = _sPath\
  472. end\
  473. \
  474. function bash.resolve( _sPath )\
  475. \009local sStartChar = string.sub( _sPath, 1, 1 )\
  476. \009if sStartChar == \"/\" or sStartChar == \"\\\\\" then\
  477. \009\009return fs.combine( \"\", _sPath )\
  478. \009else\
  479. \009\009return fs.combine( sDir, _sPath )\
  480. \009end\
  481. end\
  482. \
  483. function bash.resolveProgram( _sCommand )\
  484. \009-- Substitute aliases firsts\
  485. \009if tAliases[ _sCommand ] ~= nil then\
  486. \009\009_sCommand = tAliases[ _sCommand ]\
  487. \009end\
  488. \
  489. -- If the path is a global path, use it directly\
  490. local sStartChar = string.sub( _sCommand, 1, 1 )\
  491. if sStartChar == \"/\" or sStartChar == \"\\\\\" then\
  492. \009local sPath = fs.combine( \"\", _sCommand )\
  493. \009if fs.exists( sPath ) and not fs.isDir( sPath ) then\
  494. \009\009\009return sPath\
  495. \009end\
  496. \009\009return nil\
  497. end\
  498. \
  499. \009-- Otherwise, look on the path variable\
  500. for sPath in string.gmatch(sPath, \"[^:]+\") do\
  501. \009sPath = fs.combine( bash.resolve( sPath ), _sCommand )\
  502. \009if fs.exists( sPath ) and not fs.isDir( sPath ) then\
  503. \009\009\009return sPath\
  504. \009end\
  505. end\
  506. \009\
  507. \009-- Not found\
  508. \009return nil\
  509. end\
  510. \
  511. function bash.programs( _bIncludeHidden )\
  512. \009local tItems = {}\
  513. \009\
  514. \009-- Add programs from the path\
  515. for sPath in string.gmatch(sPath, \"[^:]+\") do\
  516. \009sPath = bash.resolve( sPath )\
  517. \009\009if fs.isDir( sPath ) then\
  518. \009\009\009local tList = fs.list( sPath )\
  519. for n=1,#tList do\
  520. local sFile = tList[n]\
  521. \009\009\009\009if not fs.isDir( fs.combine( sPath, sFile ) ) and\
  522. \009\009\009\009 (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= \".\") then\
  523. \009\009\009\009\009tItems[ sFile ] = true\
  524. \009\009\009\009end\
  525. \009\009\009end\
  526. \009\009end\
  527. end\009\
  528. \
  529. \009-- Sort and return\
  530. \009local tItemList = {}\
  531. \009for sItem, b in pairs( tItems ) do\
  532. \009\009table.insert( tItemList, sItem )\
  533. \009end\
  534. \009table.sort( tItemList )\
  535. \009return tItemList\
  536. end\
  537. \
  538. local function completeProgram( sLine )\
  539. \009if #sLine > 0 and string.sub( sLine, 1, 1 ) == \"/\" then\
  540. \009 -- Add programs from the root\
  541. \009 return fs.complete( sLine, \"\", true, false )\
  542. \
  543. else\
  544. local tResults = {}\
  545. local tSeen = {}\
  546. \
  547. -- Add aliases\
  548. for sAlias, sCommand in pairs( tAliases ) do\
  549. if #sAlias > #sLine and string.sub( sAlias, 1, #sLine ) == sLine then\
  550. local sResult = string.sub( sAlias, #sLine + 1 )\
  551. if not tSeen[ sResult ] then\
  552. table.insert( tResults, sResult )\
  553. tSeen[ sResult ] = true\
  554. end\
  555. end\
  556. end\
  557. \
  558. -- Add programs from the path\
  559. local tPrograms = bash.programs()\
  560. for n=1,#tPrograms do\
  561. local sProgram = tPrograms[n]\
  562. if #sProgram > #sLine and string.sub( sProgram, 1, #sLine ) == sLine then\
  563. local sResult = string.sub( sProgram, #sLine + 1 )\
  564. if not tSeen[ sResult ] then\
  565. table.insert( tResults, sResult )\
  566. tSeen[ sResult ] = true\
  567. end\
  568. end\
  569. end\
  570. \
  571. -- Sort and return\
  572. table.sort( tResults )\
  573. return tResults\
  574. end\
  575. end\
  576. \
  577. local function completeProgramArgument( sProgram, nArgument, sPart, tPreviousParts )\
  578. local tInfo = tCompletionInfo[ sProgram ]\
  579. if tInfo then\
  580. return tInfo.fnComplete( bash, nArgument, sPart, tPreviousParts )\
  581. end\
  582. return nil\
  583. end\
  584. \
  585. function bash.complete( sLine )\
  586. if #sLine > 0 then\
  587. local tWords = tokenise( sLine )\
  588. local nIndex = #tWords\
  589. if string.sub( sLine, #sLine, #sLine ) == \" \" then\
  590. nIndex = nIndex + 1\
  591. end\
  592. if nIndex == 1 then\
  593. local sBit = tWords[1] or \"\"\
  594. local sPath = bash.resolveProgram( sBit )\
  595. if tCompletionInfo[ sPath ] then\
  596. return { \" \" }\
  597. else\
  598. local tResults = completeProgram( sBit )\
  599. for n=1,#tResults do\
  600. local sResult = tResults[n]\
  601. local sPath = bash.resolveProgram( sBit .. sResult )\
  602. if tCompletionInfo[ sPath ] then\
  603. tResults[n] = sResult .. \" \"\
  604. end\
  605. end\
  606. return tResults\
  607. end\
  608. \
  609. elseif nIndex > 1 then\
  610. local sPath = bash.resolveProgram( tWords[1] )\
  611. local sPart = tWords[nIndex] or \"\"\
  612. local tPreviousParts = tWords\
  613. tPreviousParts[nIndex] = nil\
  614. return completeProgramArgument( sPath , nIndex - 1, sPart, tPreviousParts )\
  615. \
  616. end\
  617. end\
  618. \009return nil\
  619. end\
  620. \
  621. function bash.completeProgram( sProgram )\
  622. return completeProgram( sProgram )\
  623. end\
  624. \
  625. function bash.setCompletionFunction( sProgram, fnComplete )\
  626. tCompletionInfo[ sProgram ] = {\
  627. fnComplete = fnComplete\
  628. }\
  629. end\
  630. \
  631. function bash.getCompletionInfo()\
  632. return tCompletionInfo\
  633. end\
  634. \
  635. function bash.getRunningProgram()\
  636. \009if #tProgramStack > 0 then\
  637. \009\009return tProgramStack[#tProgramStack]\
  638. \009end\
  639. \009return nil\
  640. end\
  641. \
  642. function bash.setAlias( _sCommand, _sProgram )\
  643. \009tAliases[ _sCommand ] = _sProgram\
  644. end\
  645. \
  646. function bash.clearAlias( _sCommand )\
  647. \009tAliases[ _sCommand ] = nil\
  648. end\
  649. \
  650. function bash.aliases()\
  651. \009-- Copy aliases\
  652. \009local tCopy = {}\
  653. \009for sAlias, sCommand in pairs( tAliases ) do\
  654. \009\009tCopy[sAlias] = sCommand\
  655. \009end\
  656. \009return tCopy\
  657. end\
  658. \
  659. if multishell then\
  660. function bash.openTab( ... )\
  661. local tWords = tokenise( ... )\
  662. local sCommand = tWords[1]\
  663. if sCommand then\
  664. \009local sPath = bash.resolveProgram( sCommand )\
  665. \009if sPath == \"kernel/bash\" then\
  666. return multishell.launch( tEnv, sPath, table.unpack( tWords, 2 ) )\
  667. elseif sPath ~= nil then\
  668. return multishell.launch( tEnv, \"kernel/bash\", sCommand, table.unpack( tWords, 2 ) )\
  669. else\
  670. printError( \"No such program\" )\
  671. end\
  672. end\
  673. end\
  674. \
  675. function bash.switchTab( nID )\
  676. multishell.setFocus( nID )\
  677. end\
  678. end\
  679. \
  680. local tArgs = { ... }\
  681. if #tArgs > 0 then\
  682. -- \"bash x y z\"\
  683. -- Run the program specified on the commandline\
  684. bash.run( ... )\
  685. \
  686. else\
  687. -- \"bash\"\
  688. -- Print the header\
  689. term.setBackgroundColor( bgColour )\
  690. term.setTextColour( promptColour )\
  691. print( os.version() )\
  692. term.setTextColour( textColour )\
  693. \
  694. -- Run the startup program\
  695. if parentShell == nil then\
  696. bash.run( \"/kernel/startup\" )\
  697. end\
  698. \
  699. -- Read commands and execute them\
  700. local tCommandHistory = {}\
  701. while not bExit do\
  702. term.redirect( parentTerm )\
  703. term.setBackgroundColor( bgColour )\
  704. term.setTextColour( promptColour )\
  705. if bash.user()~=\"root\" then\
  706. write( string.gsub(\"/\"..bash.dir(),\"/\"..bash.curHomeDir(),\"~\") .. \" $ \" )\
  707. else\
  708. write( \"/\"..bash.dir() .. \" # \" )\
  709. end\
  710. term.setTextColour( textColour )\
  711. \
  712. local sLine\
  713. if settings.get( \"bash.autocomplete\" ) then\
  714. sLine = read( nil, tCommandHistory, bash.complete )\
  715. else\
  716. sLine = read( nil, tCommandHistory )\
  717. end\
  718. table.insert( tCommandHistory, sLine )\
  719. bash.run( sLine )\
  720. end\
  721. end",
  722. startup = "\
  723. -- Setup paths\
  724. local sPath = \".:/bin:/rom/programs\"\
  725. if term.isColor() then\
  726. \009sPath = sPath..\":/rom/programs/advanced\"\
  727. end\
  728. if turtle then\
  729. \009sPath = sPath..\":/rom/programs/turtle\"\
  730. else\
  731. sPath = sPath..\":/rom/programs/rednet:/rom/programs/fun\"\
  732. if term.isColor() then\
  733. \009sPath = sPath..\":/rom/programs/fun/advanced\"\
  734. end\
  735. end\
  736. if pocket then\
  737. sPath = sPath..\":/rom/programs/pocket\"\
  738. end\
  739. if commands then\
  740. sPath = sPath..\":/rom/programs/command\"\
  741. end\
  742. if http then\
  743. \009sPath = sPath..\":/rom/programs/http\"\
  744. end\
  745. shell.setPath( sPath )\
  746. help.setPath( \"/rom/help\" )\
  747. \
  748. -- Setup aliases\
  749. shell.setAlias( \"ls\", \"list\" )\
  750. shell.setAlias( \"dir\", \"list\" )\
  751. shell.setAlias( \"cp\", \"copy\" )\
  752. shell.setAlias( \"mv\", \"move\" )\
  753. shell.setAlias( \"rm\", \"delete\" )\
  754. shell.setAlias( \"clr\", \"clear\" )\
  755. shell.setAlias( \"rs\", \"redstone\" )\
  756. shell.setAlias( \"sh\", \"shell\" )\
  757. if term.isColor() then\
  758. shell.setAlias( \"background\", \"bg\" )\
  759. shell.setAlias( \"foreground\", \"fg\" )\
  760. end\
  761. \
  762. -- Setup completion functions\
  763. local function completeMultipleChoice( sText, tOptions, bAddSpaces )\
  764. local tResults = {}\
  765. for n=1,#tOptions do\
  766. local sOption = tOptions[n]\
  767. if #sOption + (bAddSpaces and 1 or 0) > #sText and string.sub( sOption, 1, #sText ) == sText then\
  768. local sResult = string.sub( sOption, #sText + 1 )\
  769. if bAddSpaces then\
  770. table.insert( tResults, sResult .. \" \" )\
  771. else\
  772. table.insert( tResults, sResult )\
  773. end\
  774. end\
  775. end\
  776. return tResults\
  777. end\
  778. local function completePeripheralName( sText, bAddSpaces )\
  779. return completeMultipleChoice( sText, peripheral.getNames(), bAddSpaces )\
  780. end\
  781. local tRedstoneSides = redstone.getSides()\
  782. local function completeSide( sText, bAddSpaces )\
  783. return completeMultipleChoice( sText, tRedstoneSides, bAddSpaces )\
  784. end\
  785. local function completeFile( shell, nIndex, sText, tPreviousText )\
  786. if nIndex == 1 then\
  787. return fs.complete( sText, shell.dir(), true, false )\
  788. end\
  789. end\
  790. local function completeDir( shell, nIndex, sText, tPreviousText )\
  791. if nIndex == 1 then\
  792. return fs.complete( sText, shell.dir(), false, true )\
  793. end\
  794. end\
  795. local function completeEither( shell, nIndex, sText, tPreviousText )\
  796. if nIndex == 1 then\
  797. return fs.complete( sText, shell.dir(), true, true )\
  798. end\
  799. end\
  800. local function completeEitherEither( shell, nIndex, sText, tPreviousText )\
  801. if nIndex == 1 then\
  802. local tResults = fs.complete( sText, shell.dir(), true, true )\
  803. for n=1,#tResults do\
  804. local sResult = tResults[n]\
  805. if string.sub( sResult, #sResult, #sResult ) ~= \"/\" then\
  806. tResults[n] = sResult .. \" \"\
  807. end\
  808. end\
  809. return tResults\
  810. elseif nIndex == 2 then\
  811. return fs.complete( sText, shell.dir(), true, true )\
  812. end\
  813. end\
  814. local function completeProgram( shell, nIndex, sText, tPreviousText )\
  815. if nIndex == 1 then\
  816. return shell.completeProgram( sText )\
  817. end\
  818. end\
  819. local function completeHelp( shell, nIndex, sText, tPreviousText )\
  820. if nIndex == 1 then\
  821. return help.completeTopic( sText )\
  822. end\
  823. end\
  824. local function completeAlias( shell, nIndex, sText, tPreviousText )\
  825. if nIndex == 2 then\
  826. return shell.completeProgram( sText )\
  827. end\
  828. end\
  829. local function completePeripheral( shell, nIndex, sText, tPreviousText )\
  830. if nIndex == 1 then\
  831. return completePeripheralName( sText )\
  832. end\
  833. end\
  834. local tGPSOptions = { \"host\", \"host \", \"locate\" }\
  835. local function completeGPS( shell, nIndex, sText, tPreviousText )\
  836. if nIndex == 1 then\
  837. return completeMultipleChoice( sText, tGPSOptions )\
  838. end\
  839. end\
  840. local tLabelOptions = { \"get\", \"get \", \"set \", \"clear\", \"clear \" }\
  841. local function completeLabel( shell, nIndex, sText, tPreviousText )\
  842. if nIndex == 1 then\
  843. return completeMultipleChoice( sText, tLabelOptions )\
  844. elseif nIndex == 2 then\
  845. return completePeripheralName( sText )\
  846. end\
  847. end\
  848. local function completeMonitor( shell, nIndex, sText, tPreviousText )\
  849. if nIndex == 1 then\
  850. return completePeripheralName( sText, true )\
  851. elseif nIndex == 2 then\
  852. return shell.completeProgram( sText )\
  853. end\
  854. end\
  855. local tRedstoneOptions = { \"probe\", \"set \", \"pulse \" }\
  856. local function completeRedstone( shell, nIndex, sText, tPreviousText )\
  857. if nIndex == 1 then\
  858. return completeMultipleChoice( sText, tRedstoneOptions )\
  859. elseif nIndex == 2 then\
  860. return completeSide( sText )\
  861. end\
  862. end\
  863. local tDJOptions = { \"play\", \"play \", \"stop \" }\
  864. local function completeDJ( shell, nIndex, sText, tPreviousText )\
  865. if nIndex == 1 then\
  866. return completeMultipleChoice( sText, tDJOptions )\
  867. elseif nIndex == 2 then\
  868. return completePeripheralName( sText )\
  869. end\
  870. end\
  871. local tPastebinOptions = { \"put \", \"get \", \"run \" }\
  872. local function completePastebin( shell, nIndex, sText, tPreviousText )\
  873. if nIndex == 1 then\
  874. return completeMultipleChoice( sText, tPastebinOptions )\
  875. elseif nIndex == 2 then\
  876. if tPreviousText[2] == \"put\" then\
  877. return fs.complete( sText, shell.dir(), true, false )\
  878. end\
  879. end\
  880. end\
  881. local tChatOptions = { \"host \", \"join \" }\
  882. local function completeChat( shell, nIndex, sText, tPreviousText )\
  883. if nIndex == 1 then\
  884. return completeMultipleChoice( sText, tChatOptions )\
  885. end\
  886. end\
  887. local function completeSet( shell, nIndex, sText, tPreviousText )\
  888. if nIndex == 1 then\
  889. return completeMultipleChoice( sText, settings.getNames(), true )\
  890. end\
  891. end\
  892. shell.setCompletionFunction( \"rom/programs/alias\", completeAlias )\
  893. shell.setCompletionFunction( \"rom/programs/cd\", completeDir )\
  894. shell.setCompletionFunction( \"rom/programs/copy\", completeEitherEither )\
  895. shell.setCompletionFunction( \"rom/programs/delete\", completeEither )\
  896. shell.setCompletionFunction( \"rom/programs/drive\", completeDir )\
  897. shell.setCompletionFunction( \"rom/programs/edit\", completeFile )\
  898. shell.setCompletionFunction( \"rom/programs/eject\", completePeripheral )\
  899. shell.setCompletionFunction( \"rom/programs/gps\", completeGPS )\
  900. shell.setCompletionFunction( \"rom/programs/help\", completeHelp )\
  901. shell.setCompletionFunction( \"rom/programs/id\", completePeripheral )\
  902. shell.setCompletionFunction( \"rom/programs/label\", completeLabel )\
  903. shell.setCompletionFunction( \"rom/programs/list\", completeDir )\
  904. shell.setCompletionFunction( \"rom/programs/mkdir\", completeFile )\
  905. shell.setCompletionFunction( \"rom/programs/monitor\", completeMonitor )\
  906. shell.setCompletionFunction( \"rom/programs/move\", completeEitherEither )\
  907. shell.setCompletionFunction( \"rom/programs/redstone\", completeRedstone )\
  908. shell.setCompletionFunction( \"rom/programs/rename\", completeEitherEither )\
  909. shell.setCompletionFunction( \"rom/programs/shell\", completeProgram )\
  910. shell.setCompletionFunction( \"rom/programs/type\", completeEither )\
  911. shell.setCompletionFunction( \"rom/programs/set\", completeSet )\
  912. shell.setCompletionFunction( \"rom/programs/advanced/bg\", completeProgram )\
  913. shell.setCompletionFunction( \"rom/programs/advanced/fg\", completeProgram )\
  914. shell.setCompletionFunction( \"rom/programs/fun/dj\", completeDJ )\
  915. shell.setCompletionFunction( \"rom/programs/fun/advanced/paint\", completeFile )\
  916. shell.setCompletionFunction( \"rom/programs/http/pastebin\", completePastebin )\
  917. shell.setCompletionFunction( \"rom/programs/rednet/chat\", completeChat )\
  918. \
  919. -- Run autorun files\
  920. if fs.exists( \"/rom/autorun\" ) and fs.isDir( \"/rom/autorun\" ) then\
  921. \009local tFiles = fs.list( \"/rom/autorun\" )\
  922. \009table.sort( tFiles )\
  923. \009for n, sFile in ipairs( tFiles ) do\
  924. \009\009if string.sub( sFile, 1, 1 ) ~= \".\" then\
  925. \009\009\009local sPath = \"/rom/autorun/\"..sFile\
  926. \009\009\009if not fs.isDir( sPath ) then\
  927. \009\009\009\009shell.run( sPath )\
  928. \009\009\009end\
  929. \009\009end\
  930. \009end\
  931. end\
  932. \
  933. -- Run the user created startup, either from disk drives or the root\
  934. local user,homedir = loadfile(\"/bin/login\")()\
  935. if settings.get( \"bash.run_bashrc\" ) then\
  936. if fs.exists(homedir..\"/.bashrc\") then bash.run(homedir..\"/.bashrc\") end\
  937. end\
  938. bash.user = function() return user end\
  939. bash.curHomeDir = function() return homedir end\
  940. bash.setDir(homedir)",
  941. },
  942. etc = {
  943. passwd = "{}",
  944. },
  945. }
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954. local function writeFile(path,content)
  955. local file = fs.open(path,"w")
  956. file.write(content)
  957. file.close()
  958. end
  959. function writeDown(input,dir)
  960. for i,v in pairs(input) do
  961. if type(v) == "table" then
  962. writeDown(v,dir.."/"..i)
  963. elseif type(v) == "string" then
  964. writeFile(dir.."/"..i,v)
  965. end
  966. end
  967. end
  968.  
  969. args = {...}
  970. if #args == 0 then
  971. print("Please input a destination folder.")
  972. else
  973. writeDown(inputTable,args[1])
  974. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement