Advertisement
programcreator

OmniOS: 17w36c

Sep 6th, 2017
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 350.55 KB | None | 0 0
  1. inputTable = {
  2.   [ "changes.txt" ] = "Done:\
  3. \009- Added devfs\
  4. \009- Added devbus, which is the device handling library\
  5. \009- Made a vfs library that handles fs that rae hidden in tables\
  6. \009- Worked on IPC\
  7. \009- Worked on piping\
  8. \009- Implemented window switch preview from sidebar\
  9. \009- Moved shall parsing to OmniOS API allowing any program to parse input\
  10. \
  11. To Do:\
  12. \009- FS change callbacks\
  13. \009- Implememnt vdev drivers\
  14. \009- Implememnt deamons",
  15.   lib = {
  16.     [ "AES.lua" ] = "local Delta = ...\
  17. local AES = {}\
  18. \
  19. local function _W(f) local e=setmetatable({}, {__index = getfenv()}) return setfenv(f,e)() or e end\
  20. local bit=_W(function()\
  21. --[[\
  22. \009This bit API is designed to cope with unsigned integers instead of normal integers\
  23. \
  24. \009To do this we\
  25. ]]\
  26. \
  27. local floor = math.floor\
  28. \
  29. local bit_band, bit_bxor = bit.band, bit.bxor\
  30. local function band(a, b)\
  31. \009if a > 2147483647 then a = a - 4294967296 end\
  32. \009if b > 2147483647 then b = b - 4294967296 end\
  33. \009return bit_band(a, b)\
  34. end\
  35. \
  36. local function bxor(a, b)\
  37. \009if a > 2147483647 then a = a - 4294967296 end\
  38. \009if b > 2147483647 then b = b - 4294967296 end\
  39. \009return bit_bxor(a, b)\
  40. end\
  41. \
  42. local lshift, rshift\
  43. \
  44. rshift = function(a,disp)\
  45. \009return floor(a % 4294967296 / 2^disp)\
  46. end\
  47. \
  48. lshift = function(a,disp)\
  49. \009return (a * 2^disp) % 4294967296\
  50. end\
  51. \
  52. return {\
  53. \009-- bit operations\
  54. \009bnot = bit.bnot,\
  55. \009band = band,\
  56. \009bor  = bit.bor,\
  57. \009bxor = bxor,\
  58. \009rshift = rshift,\
  59. \009lshift = lshift,\
  60. }\
  61. end)\
  62. local gf=_W(function()\
  63. -- finite field with base 2 and modulo irreducible polynom x^8+x^4+x^3+x+1 = 0x11d\
  64. local bxor = bit.bxor\
  65. local lshift = bit.lshift\
  66. \
  67. -- private data of gf\
  68. local n = 0x100\
  69. local ord = 0xff\
  70. local irrPolynom = 0x11b\
  71. local exp = {}\
  72. local log = {}\
  73. \
  74. --\
  75. -- add two polynoms (its simply xor)\
  76. --\
  77. local function add(operand1, operand2)\
  78. \009return bxor(operand1,operand2)\
  79. end\
  80. \
  81. --\
  82. -- subtract two polynoms (same as addition)\
  83. --\
  84. local function sub(operand1, operand2)\
  85. \009return bxor(operand1,operand2)\
  86. end\
  87. \
  88. --\
  89. -- inverts element\
  90. -- a^(-1) = g^(order - log(a))\
  91. --\
  92. local function invert(operand)\
  93. \009-- special case for 1\
  94. \009if (operand == 1) then\
  95. \009\009return 1\
  96. \009end\
  97. \009-- normal invert\
  98. \009local exponent = ord - log[operand]\
  99. \009return exp[exponent]\
  100. end\
  101. \
  102. --\
  103. -- multiply two elements using a logarithm table\
  104. -- a*b = g^(log(a)+log(b))\
  105. --\
  106. local function mul(operand1, operand2)\
  107. \009if (operand1 == 0 or operand2 == 0) then\
  108. \009\009return 0\
  109. \009end\
  110. \
  111. \009local exponent = log[operand1] + log[operand2]\
  112. \009if (exponent >= ord) then\
  113. \009\009exponent = exponent - ord\
  114. \009end\
  115. \009return  exp[exponent]\
  116. end\
  117. \
  118. --\
  119. -- divide two elements\
  120. -- a/b = g^(log(a)-log(b))\
  121. --\
  122. local function div(operand1, operand2)\
  123. \009if (operand1 == 0)  then\
  124. \009\009return 0\
  125. \009end\
  126. \009-- TODO: exception if operand2 == 0\
  127. \009local exponent = log[operand1] - log[operand2]\
  128. \009if (exponent < 0) then\
  129. \009\009exponent = exponent + ord\
  130. \009end\
  131. \009return exp[exponent]\
  132. end\
  133. \
  134. --\
  135. -- print logarithmic table\
  136. --\
  137. local function printLog()\
  138. \009for i = 1, n do\
  139. \009\009print(\"log(\", i-1, \")=\", log[i-1])\
  140. \009end\
  141. end\
  142. \
  143. --\
  144. -- print exponentiation table\
  145. --\
  146. local function printExp()\
  147. \009for i = 1, n do\
  148. \009\009print(\"exp(\", i-1, \")=\", exp[i-1])\
  149. \009end\
  150. end\
  151. \
  152. --\
  153. -- calculate logarithmic and exponentiation table\
  154. --\
  155. local function initMulTable()\
  156. \009local a = 1\
  157. \
  158. \009for i = 0,ord-1 do\
  159. \009\009exp[i] = a\
  160. \009\009log[a] = i\
  161. \
  162. \009\009-- multiply with generator x+1 -> left shift + 1\
  163. \009\009a = bxor(lshift(a, 1), a)\
  164. \
  165. \009\009-- if a gets larger than order, reduce modulo irreducible polynom\
  166. \009\009if a > ord then\
  167. \009\009\009a = sub(a, irrPolynom)\
  168. \009\009end\
  169. \009end\
  170. end\
  171. \
  172. initMulTable()\
  173. \
  174. return {\
  175. \009add = add,\
  176. \009sub = sub,\
  177. \009invert = invert,\
  178. \009mul = mul,\
  179. \009div = div,\
  180. \009printLog = printLog,\
  181. \009printExp = printExp,\
  182. }\
  183. end)\
  184. local util=_W(function()\
  185. -- Cache some bit operators\
  186. local bxor = bit.bxor\
  187. local rshift = bit.rshift\
  188. local band = bit.band\
  189. local lshift = bit.lshift\
  190. \
  191. local sleepCheckIn\
  192. --\
  193. -- calculate the parity of one byte\
  194. --\
  195. local function byteParity(byte)\
  196. \009byte = bxor(byte, rshift(byte, 4))\
  197. \009byte = bxor(byte, rshift(byte, 2))\
  198. \009byte = bxor(byte, rshift(byte, 1))\
  199. \009return band(byte, 1)\
  200. end\
  201. \
  202. --\
  203. -- get byte at position index\
  204. --\
  205. local function getByte(number, index)\
  206. \009if (index == 0) then\
  207. \009\009return band(number,0xff)\
  208. \009else\
  209. \009\009return band(rshift(number, index*8),0xff)\
  210. \009end\
  211. end\
  212. \
  213. \
  214. --\
  215. -- put number into int at position index\
  216. --\
  217. local function putByte(number, index)\
  218. \009if (index == 0) then\
  219. \009\009return band(number,0xff)\
  220. \009else\
  221. \009\009return lshift(band(number,0xff),index*8)\
  222. \009end\
  223. end\
  224. \
  225. --\
  226. -- convert byte array to int array\
  227. --\
  228. local function bytesToInts(bytes, start, n)\
  229. \009local ints = {}\
  230. \009for i = 0, n - 1 do\
  231. \009\009ints[i] = putByte(bytes[start + (i*4)    ], 3)\
  232. \009\009\009\009+ putByte(bytes[start + (i*4) + 1], 2)\
  233. \009\009\009\009+ putByte(bytes[start + (i*4) + 2], 1)\
  234. \009\009\009\009+ putByte(bytes[start + (i*4) + 3], 0)\
  235. \
  236. \009\009if n % 10000 == 0 then sleepCheckIn() end\
  237. \009end\
  238. \009return ints\
  239. end\
  240. \
  241. --\
  242. -- convert int array to byte array\
  243. --\
  244. local function intsToBytes(ints, output, outputOffset, n)\
  245. \009n = n or #ints\
  246. \009for i = 0, n do\
  247. \009\009for j = 0,3 do\
  248. \009\009\009output[outputOffset + i*4 + (3 - j)] = getByte(ints[i], j)\
  249. \009\009end\
  250. \
  251. \009\009if n % 10000 == 0 then sleepCheckIn() end\
  252. \009end\
  253. \009return output\
  254. end\
  255. \
  256. --\
  257. -- convert bytes to hexString\
  258. --\
  259. local function bytesToHex(bytes)\
  260. \009local hexBytes = \"\"\
  261. \
  262. \009for i,byte in ipairs(bytes) do\
  263. \009\009hexBytes = hexBytes .. string.format(\"%02x \", byte)\
  264. \009end\
  265. \
  266. \009return hexBytes\
  267. end\
  268. \
  269. --\
  270. -- convert data to hex string\
  271. --\
  272. local function toHexString(data)\
  273. \009local type = type(data)\
  274. \009if (type == \"number\") then\
  275. \009\009return string.format(\"%08x\",data)\
  276. \009elseif (type == \"table\") then\
  277. \009\009return bytesToHex(data)\
  278. \009elseif (type == \"string\") then\
  279. \009\009local bytes = {string.byte(data, 1, #data)}\
  280. \
  281. \009\009return bytesToHex(bytes)\
  282. \009else\
  283. \009\009return data\
  284. \009end\
  285. end\
  286. \
  287. local function padByteString(data)\
  288. \009local dataLength = #data\
  289. \
  290. \009local random1 = math.random(0,255)\
  291. \009local random2 = math.random(0,255)\
  292. \
  293. \009local prefix = string.char(random1,\
  294. \009\009\009\009\009\009\009   random2,\
  295. \009\009\009\009\009\009\009   random1,\
  296. \009\009\009\009\009\009\009   random2,\
  297. \009\009\009\009\009\009\009   getByte(dataLength, 3),\
  298. \009\009\009\009\009\009\009   getByte(dataLength, 2),\
  299. \009\009\009\009\009\009\009   getByte(dataLength, 1),\
  300. \009\009\009\009\009\009\009   getByte(dataLength, 0))\
  301. \
  302. \009data = prefix .. data\
  303. \
  304. \009local paddingLength = math.ceil(#data/16)*16 - #data\
  305. \009local padding = \"\"\
  306. \009for i=1,paddingLength do\
  307. \009\009padding = padding .. string.char(math.random(0,255))\
  308. \009end\
  309. \
  310. \009return data .. padding\
  311. end\
  312. \
  313. local function properlyDecrypted(data)\
  314. \009local random = {string.byte(data,1,4)}\
  315. \
  316. \009if (random[1] == random[3] and random[2] == random[4]) then\
  317. \009\009return true\
  318. \009end\
  319. \
  320. \009return false\
  321. end\
  322. \
  323. local function unpadByteString(data)\
  324. \009if (not properlyDecrypted(data)) then\
  325. \009\009return nil\
  326. \009end\
  327. \
  328. \009local dataLength = putByte(string.byte(data,5), 3)\
  329. \009\009\009\009\009 + putByte(string.byte(data,6), 2)\
  330. \009\009\009\009\009 + putByte(string.byte(data,7), 1)\
  331. \009\009\009\009\009 + putByte(string.byte(data,8), 0)\
  332. \
  333. \009return string.sub(data,9,8+dataLength)\
  334. end\
  335. \
  336. local function xorIV(data, iv)\
  337. \009for i = 1,16 do\
  338. \009\009data[i] = bxor(data[i], iv[i])\
  339. \009end\
  340. end\
  341. \
  342. -- Called every\
  343. local push, pull, time = os.queueEvent, coroutine.yield, os.time\
  344. local oldTime = time()\
  345. local function sleepCheckIn()\
  346.    local newTime = time()\
  347.    if newTime - oldTime >= 0.03 then -- (0.020 * 1.5)\
  348.        oldTime = newTime\
  349.        push(\"sleep\")\
  350.        pull(\"sleep\")\
  351.    end\
  352. end\
  353. \
  354. local function getRandomData(bytes)\
  355. \009local char, random, sleep, insert = string.char, math.random, sleepCheckIn, table.insert\
  356. \009local result = {}\
  357. \
  358. \009for i=1,bytes do\
  359. \009\009insert(result, random(0,255))\
  360. \009\009if i % 10240 == 0 then sleep() end\
  361. \009end\
  362. \
  363. \009return result\
  364. end\
  365. \
  366. local function getRandomString(bytes)\
  367. \009local char, random, sleep, insert = string.char, math.random, sleepCheckIn, table.insert\
  368. \009local result = {}\
  369. \
  370. \009for i=1,bytes do\
  371. \009\009insert(result, char(random(0,255)))\
  372. \009\009if i % 10240 == 0 then sleep() end\
  373. \009end\
  374. \
  375. \009return table.concat(result)\
  376. end\
  377. \
  378. return {\
  379. \009byteParity = byteParity,\
  380. \009getByte = getByte,\
  381. \009putByte = putByte,\
  382. \009bytesToInts = bytesToInts,\
  383. \009intsToBytes = intsToBytes,\
  384. \009bytesToHex = bytesToHex,\
  385. \009toHexString = toHexString,\
  386. \009padByteString = padByteString,\
  387. \009properlyDecrypted = properlyDecrypted,\
  388. \009unpadByteString = unpadByteString,\
  389. \009xorIV = xorIV,\
  390. \
  391. \009sleepCheckIn = sleepCheckIn,\
  392. \
  393. \009getRandomData = getRandomData,\
  394. \009getRandomString = getRandomString,\
  395. }\
  396. end)\
  397. local aes=_W(function()\
  398. -- Implementation of AES with nearly pure lua\
  399. -- AES with lua is slow, really slow :-)\
  400. \
  401. local putByte = util.putByte\
  402. local getByte = util.getByte\
  403. \
  404. -- some constants\
  405. local ROUNDS = 'rounds'\
  406. local KEY_TYPE = \"type\"\
  407. local ENCRYPTION_KEY=1\
  408. local DECRYPTION_KEY=2\
  409. \
  410. -- aes SBOX\
  411. local SBox = {}\
  412. local iSBox = {}\
  413. \
  414. -- aes tables\
  415. local table0 = {}\
  416. local table1 = {}\
  417. local table2 = {}\
  418. local table3 = {}\
  419. \
  420. local tableInv0 = {}\
  421. local tableInv1 = {}\
  422. local tableInv2 = {}\
  423. local tableInv3 = {}\
  424. \
  425. -- round constants\
  426. local rCon = {\
  427. \0090x01000000,\
  428. \0090x02000000,\
  429. \0090x04000000,\
  430. \0090x08000000,\
  431. \0090x10000000,\
  432. \0090x20000000,\
  433. \0090x40000000,\
  434. \0090x80000000,\
  435. \0090x1b000000,\
  436. \0090x36000000,\
  437. \0090x6c000000,\
  438. \0090xd8000000,\
  439. \0090xab000000,\
  440. \0090x4d000000,\
  441. \0090x9a000000,\
  442. \0090x2f000000,\
  443. }\
  444. \
  445. --\
  446. -- affine transformation for calculating the S-Box of AES\
  447. --\
  448. local function affinMap(byte)\
  449. \009mask = 0xf8\
  450. \009result = 0\
  451. \009for i = 1,8 do\
  452. \009\009result = bit.lshift(result,1)\
  453. \
  454. \009\009parity = util.byteParity(bit.band(byte,mask))\
  455. \009\009result = result + parity\
  456. \
  457. \009\009-- simulate roll\
  458. \009\009lastbit = bit.band(mask, 1)\
  459. \009\009mask = bit.band(bit.rshift(mask, 1),0xff)\
  460. \009\009if (lastbit ~= 0) then\
  461. \009\009\009mask = bit.bor(mask, 0x80)\
  462. \009\009else\
  463. \009\009\009mask = bit.band(mask, 0x7f)\
  464. \009\009end\
  465. \009end\
  466. \
  467. \009return bit.bxor(result, 0x63)\
  468. end\
  469. \
  470. --\
  471. -- calculate S-Box and inverse S-Box of AES\
  472. -- apply affine transformation to inverse in finite field 2^8\
  473. --\
  474. local function calcSBox()\
  475. \009for i = 0, 255 do\
  476. \009if (i ~= 0) then\
  477. \009\009inverse = gf.invert(i)\
  478. \009else\
  479. \009\009inverse = i\
  480. \009end\
  481. \009\009mapped = affinMap(inverse)\
  482. \009\009SBox[i] = mapped\
  483. \009\009iSBox[mapped] = i\
  484. \009end\
  485. end\
  486. \
  487. --\
  488. -- Calculate round tables\
  489. -- round tables are used to calculate shiftRow, MixColumn and SubBytes\
  490. -- with 4 table lookups and 4 xor operations.\
  491. --\
  492. local function calcRoundTables()\
  493. \009for x = 0,255 do\
  494. \009\009byte = SBox[x]\
  495. \009\009table0[x] = putByte(gf.mul(0x03, byte), 0)\
  496. \009\009\009\009\009\009  + putByte(             byte , 1)\
  497. \009\009\009\009\009\009  + putByte(             byte , 2)\
  498. \009\009\009\009\009\009  + putByte(gf.mul(0x02, byte), 3)\
  499. \009\009table1[x] = putByte(             byte , 0)\
  500. \009\009\009\009\009\009  + putByte(             byte , 1)\
  501. \009\009\009\009\009\009  + putByte(gf.mul(0x02, byte), 2)\
  502. \009\009\009\009\009\009  + putByte(gf.mul(0x03, byte), 3)\
  503. \009\009table2[x] = putByte(             byte , 0)\
  504. \009\009\009\009\009\009  + putByte(gf.mul(0x02, byte), 1)\
  505. \009\009\009\009\009\009  + putByte(gf.mul(0x03, byte), 2)\
  506. \009\009\009\009\009\009  + putByte(             byte , 3)\
  507. \009\009table3[x] = putByte(gf.mul(0x02, byte), 0)\
  508. \009\009\009\009\009\009  + putByte(gf.mul(0x03, byte), 1)\
  509. \009\009\009\009\009\009  + putByte(             byte , 2)\
  510. \009\009\009\009\009\009  + putByte(             byte , 3)\
  511. \009end\
  512. end\
  513. \
  514. --\
  515. -- Calculate inverse round tables\
  516. -- does the inverse of the normal roundtables for the equivalent\
  517. -- decryption algorithm.\
  518. --\
  519. local function calcInvRoundTables()\
  520. \009for x = 0,255 do\
  521. \009\009byte = iSBox[x]\
  522. \009\009tableInv0[x] = putByte(gf.mul(0x0b, byte), 0)\
  523. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0d, byte), 1)\
  524. \009\009\009\009\009\009\009 + putByte(gf.mul(0x09, byte), 2)\
  525. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0e, byte), 3)\
  526. \009\009tableInv1[x] = putByte(gf.mul(0x0d, byte), 0)\
  527. \009\009\009\009\009\009\009 + putByte(gf.mul(0x09, byte), 1)\
  528. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0e, byte), 2)\
  529. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0b, byte), 3)\
  530. \009\009tableInv2[x] = putByte(gf.mul(0x09, byte), 0)\
  531. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0e, byte), 1)\
  532. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0b, byte), 2)\
  533. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0d, byte), 3)\
  534. \009\009tableInv3[x] = putByte(gf.mul(0x0e, byte), 0)\
  535. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0b, byte), 1)\
  536. \009\009\009\009\009\009\009 + putByte(gf.mul(0x0d, byte), 2)\
  537. \009\009\009\009\009\009\009 + putByte(gf.mul(0x09, byte), 3)\
  538. \009end\
  539. end\
  540. \
  541. \
  542. --\
  543. -- rotate word: 0xaabbccdd gets 0xbbccddaa\
  544. -- used for key schedule\
  545. --\
  546. local function rotWord(word)\
  547. \009local tmp = bit.band(word,0xff000000)\
  548. \009return (bit.lshift(word,8) + bit.rshift(tmp,24))\
  549. end\
  550. \
  551. --\
  552. -- replace all bytes in a word with the SBox.\
  553. -- used for key schedule\
  554. --\
  555. local function subWord(word)\
  556. \009return putByte(SBox[getByte(word,0)],0)\
  557. \009\009+ putByte(SBox[getByte(word,1)],1)\
  558. \009\009+ putByte(SBox[getByte(word,2)],2)\
  559. \009\009+ putByte(SBox[getByte(word,3)],3)\
  560. end\
  561. \
  562. --\
  563. -- generate key schedule for aes encryption\
  564. --\
  565. -- returns table with all round keys and\
  566. -- the necessary number of rounds saved in [ROUNDS]\
  567. --\
  568. local function expandEncryptionKey(key)\
  569. \009local keySchedule = {}\
  570. \009local keyWords = math.floor(#key / 4)\
  571. \
  572. \
  573. \009if ((keyWords ~= 4 and keyWords ~= 6 and keyWords ~= 8) or (keyWords * 4 ~= #key)) then\
  574. \009\009print(\"Invalid key size: \", keyWords)\
  575. \009\009return nil\
  576. \009end\
  577. \
  578. \009keySchedule[ROUNDS] = keyWords + 6\
  579. \009keySchedule[KEY_TYPE] = ENCRYPTION_KEY\
  580. \
  581. \009for i = 0,keyWords - 1 do\
  582. \009\009keySchedule[i] = putByte(key[i*4+1], 3)\
  583. \009\009\009\009\009   + putByte(key[i*4+2], 2)\
  584. \009\009\009\009\009   + putByte(key[i*4+3], 1)\
  585. \009\009\009\009\009   + putByte(key[i*4+4], 0)\
  586. \009end\
  587. \
  588. \009for i = keyWords, (keySchedule[ROUNDS] + 1)*4 - 1 do\
  589. \009\009local tmp = keySchedule[i-1]\
  590. \
  591. \009\009if ( i % keyWords == 0) then\
  592. \009\009\009tmp = rotWord(tmp)\
  593. \009\009\009tmp = subWord(tmp)\
  594. \
  595. \009\009\009local index = math.floor(i/keyWords)\
  596. \009\009\009tmp = bit.bxor(tmp,rCon[index])\
  597. \009\009elseif (keyWords > 6 and i % keyWords == 4) then\
  598. \009\009\009tmp = subWord(tmp)\
  599. \009\009end\
  600. \
  601. \009\009keySchedule[i] = bit.bxor(keySchedule[(i-keyWords)],tmp)\
  602. \009end\
  603. \
  604. \009return keySchedule\
  605. end\
  606. \
  607. --\
  608. -- Inverse mix column\
  609. -- used for key schedule of decryption key\
  610. --\
  611. local function invMixColumnOld(word)\
  612. \009local b0 = getByte(word,3)\
  613. \009local b1 = getByte(word,2)\
  614. \009local b2 = getByte(word,1)\
  615. \009local b3 = getByte(word,0)\
  616. \
  617. \009return putByte(gf.add(gf.add(gf.add(gf.mul(0x0b, b1),\
  618. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x0d, b2)),\
  619. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x09, b3)),\
  620. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x0e, b0)),3)\
  621. \009\009 + putByte(gf.add(gf.add(gf.add(gf.mul(0x0b, b2),\
  622. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x0d, b3)),\
  623. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x09, b0)),\
  624. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x0e, b1)),2)\
  625. \009\009 + putByte(gf.add(gf.add(gf.add(gf.mul(0x0b, b3),\
  626. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x0d, b0)),\
  627. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x09, b1)),\
  628. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x0e, b2)),1)\
  629. \009\009 + putByte(gf.add(gf.add(gf.add(gf.mul(0x0b, b0),\
  630. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x0d, b1)),\
  631. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x09, b2)),\
  632. \009\009\009\009\009\009\009\009\009\009\009 gf.mul(0x0e, b3)),0)\
  633. end\
  634. \
  635. --\
  636. -- Optimized inverse mix column\
  637. -- look at http://fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf\
  638. -- TODO: make it work\
  639. --\
  640. local function invMixColumn(word)\
  641. \009local b0 = getByte(word,3)\
  642. \009local b1 = getByte(word,2)\
  643. \009local b2 = getByte(word,1)\
  644. \009local b3 = getByte(word,0)\
  645. \
  646. \009local t = bit.bxor(b3,b2)\
  647. \009local u = bit.bxor(b1,b0)\
  648. \009local v = bit.bxor(t,u)\
  649. \009v = bit.bxor(v,gf.mul(0x08,v))\
  650. \009w = bit.bxor(v,gf.mul(0x04, bit.bxor(b2,b0)))\
  651. \009v = bit.bxor(v,gf.mul(0x04, bit.bxor(b3,b1)))\
  652. \
  653. \009return putByte( bit.bxor(bit.bxor(b3,v), gf.mul(0x02, bit.bxor(b0,b3))), 0)\
  654. \009\009 + putByte( bit.bxor(bit.bxor(b2,w), gf.mul(0x02, t              )), 1)\
  655. \009\009 + putByte( bit.bxor(bit.bxor(b1,v), gf.mul(0x02, bit.bxor(b0,b3))), 2)\
  656. \009\009 + putByte( bit.bxor(bit.bxor(b0,w), gf.mul(0x02, u              )), 3)\
  657. end\
  658. \
  659. --\
  660. -- generate key schedule for aes decryption\
  661. --\
  662. -- uses key schedule for aes encryption and transforms each\
  663. -- key by inverse mix column.\
  664. --\
  665. local function expandDecryptionKey(key)\
  666. \009local keySchedule = expandEncryptionKey(key)\
  667. \009if (keySchedule == nil) then\
  668. \009\009return nil\
  669. \009end\
  670. \
  671. \009keySchedule[KEY_TYPE] = DECRYPTION_KEY\
  672. \
  673. \009for i = 4, (keySchedule[ROUNDS] + 1)*4 - 5 do\
  674. \009\009keySchedule[i] = invMixColumnOld(keySchedule[i])\
  675. \009end\
  676. \
  677. \009return keySchedule\
  678. end\
  679. \
  680. --\
  681. -- xor round key to state\
  682. --\
  683. local function addRoundKey(state, key, round)\
  684. \009for i = 0, 3 do\
  685. \009\009state[i] = bit.bxor(state[i], key[round*4+i])\
  686. \009end\
  687. end\
  688. \
  689. --\
  690. -- do encryption round (ShiftRow, SubBytes, MixColumn together)\
  691. --\
  692. local function doRound(origState, dstState)\
  693. \009dstState[0] =  bit.bxor(bit.bxor(bit.bxor(\
  694. \009\009\009\009table0[getByte(origState[0],3)],\
  695. \009\009\009\009table1[getByte(origState[1],2)]),\
  696. \009\009\009\009table2[getByte(origState[2],1)]),\
  697. \009\009\009\009table3[getByte(origState[3],0)])\
  698. \
  699. \009dstState[1] =  bit.bxor(bit.bxor(bit.bxor(\
  700. \009\009\009\009table0[getByte(origState[1],3)],\
  701. \009\009\009\009table1[getByte(origState[2],2)]),\
  702. \009\009\009\009table2[getByte(origState[3],1)]),\
  703. \009\009\009\009table3[getByte(origState[0],0)])\
  704. \
  705. \009dstState[2] =  bit.bxor(bit.bxor(bit.bxor(\
  706. \009\009\009\009table0[getByte(origState[2],3)],\
  707. \009\009\009\009table1[getByte(origState[3],2)]),\
  708. \009\009\009\009table2[getByte(origState[0],1)]),\
  709. \009\009\009\009table3[getByte(origState[1],0)])\
  710. \
  711. \009dstState[3] =  bit.bxor(bit.bxor(bit.bxor(\
  712. \009\009\009\009table0[getByte(origState[3],3)],\
  713. \009\009\009\009table1[getByte(origState[0],2)]),\
  714. \009\009\009\009table2[getByte(origState[1],1)]),\
  715. \009\009\009\009table3[getByte(origState[2],0)])\
  716. end\
  717. \
  718. --\
  719. -- do last encryption round (ShiftRow and SubBytes)\
  720. --\
  721. local function doLastRound(origState, dstState)\
  722. \009dstState[0] = putByte(SBox[getByte(origState[0],3)], 3)\
  723. \009\009\009\009+ putByte(SBox[getByte(origState[1],2)], 2)\
  724. \009\009\009\009+ putByte(SBox[getByte(origState[2],1)], 1)\
  725. \009\009\009\009+ putByte(SBox[getByte(origState[3],0)], 0)\
  726. \
  727. \009dstState[1] = putByte(SBox[getByte(origState[1],3)], 3)\
  728. \009\009\009\009+ putByte(SBox[getByte(origState[2],2)], 2)\
  729. \009\009\009\009+ putByte(SBox[getByte(origState[3],1)], 1)\
  730. \009\009\009\009+ putByte(SBox[getByte(origState[0],0)], 0)\
  731. \
  732. \009dstState[2] = putByte(SBox[getByte(origState[2],3)], 3)\
  733. \009\009\009\009+ putByte(SBox[getByte(origState[3],2)], 2)\
  734. \009\009\009\009+ putByte(SBox[getByte(origState[0],1)], 1)\
  735. \009\009\009\009+ putByte(SBox[getByte(origState[1],0)], 0)\
  736. \
  737. \009dstState[3] = putByte(SBox[getByte(origState[3],3)], 3)\
  738. \009\009\009\009+ putByte(SBox[getByte(origState[0],2)], 2)\
  739. \009\009\009\009+ putByte(SBox[getByte(origState[1],1)], 1)\
  740. \009\009\009\009+ putByte(SBox[getByte(origState[2],0)], 0)\
  741. end\
  742. \
  743. --\
  744. -- do decryption round\
  745. --\
  746. local function doInvRound(origState, dstState)\
  747. \009dstState[0] =  bit.bxor(bit.bxor(bit.bxor(\
  748. \009\009\009\009tableInv0[getByte(origState[0],3)],\
  749. \009\009\009\009tableInv1[getByte(origState[3],2)]),\
  750. \009\009\009\009tableInv2[getByte(origState[2],1)]),\
  751. \009\009\009\009tableInv3[getByte(origState[1],0)])\
  752. \
  753. \009dstState[1] =  bit.bxor(bit.bxor(bit.bxor(\
  754. \009\009\009\009tableInv0[getByte(origState[1],3)],\
  755. \009\009\009\009tableInv1[getByte(origState[0],2)]),\
  756. \009\009\009\009tableInv2[getByte(origState[3],1)]),\
  757. \009\009\009\009tableInv3[getByte(origState[2],0)])\
  758. \
  759. \009dstState[2] =  bit.bxor(bit.bxor(bit.bxor(\
  760. \009\009\009\009tableInv0[getByte(origState[2],3)],\
  761. \009\009\009\009tableInv1[getByte(origState[1],2)]),\
  762. \009\009\009\009tableInv2[getByte(origState[0],1)]),\
  763. \009\009\009\009tableInv3[getByte(origState[3],0)])\
  764. \
  765. \009dstState[3] =  bit.bxor(bit.bxor(bit.bxor(\
  766. \009\009\009\009tableInv0[getByte(origState[3],3)],\
  767. \009\009\009\009tableInv1[getByte(origState[2],2)]),\
  768. \009\009\009\009tableInv2[getByte(origState[1],1)]),\
  769. \009\009\009\009tableInv3[getByte(origState[0],0)])\
  770. end\
  771. \
  772. --\
  773. -- do last decryption round\
  774. --\
  775. local function doInvLastRound(origState, dstState)\
  776. \009dstState[0] = putByte(iSBox[getByte(origState[0],3)], 3)\
  777. \009\009\009\009+ putByte(iSBox[getByte(origState[3],2)], 2)\
  778. \009\009\009\009+ putByte(iSBox[getByte(origState[2],1)], 1)\
  779. \009\009\009\009+ putByte(iSBox[getByte(origState[1],0)], 0)\
  780. \
  781. \009dstState[1] = putByte(iSBox[getByte(origState[1],3)], 3)\
  782. \009\009\009\009+ putByte(iSBox[getByte(origState[0],2)], 2)\
  783. \009\009\009\009+ putByte(iSBox[getByte(origState[3],1)], 1)\
  784. \009\009\009\009+ putByte(iSBox[getByte(origState[2],0)], 0)\
  785. \
  786. \009dstState[2] = putByte(iSBox[getByte(origState[2],3)], 3)\
  787. \009\009\009\009+ putByte(iSBox[getByte(origState[1],2)], 2)\
  788. \009\009\009\009+ putByte(iSBox[getByte(origState[0],1)], 1)\
  789. \009\009\009\009+ putByte(iSBox[getByte(origState[3],0)], 0)\
  790. \
  791. \009dstState[3] = putByte(iSBox[getByte(origState[3],3)], 3)\
  792. \009\009\009\009+ putByte(iSBox[getByte(origState[2],2)], 2)\
  793. \009\009\009\009+ putByte(iSBox[getByte(origState[1],1)], 1)\
  794. \009\009\009\009+ putByte(iSBox[getByte(origState[0],0)], 0)\
  795. end\
  796. \
  797. --\
  798. -- encrypts 16 Bytes\
  799. -- key           encryption key schedule\
  800. -- input         array with input data\
  801. -- inputOffset   start index for input\
  802. -- output        array for encrypted data\
  803. -- outputOffset  start index for output\
  804. --\
  805. local function encrypt(key, input, inputOffset, output, outputOffset)\
  806. \009--default parameters\
  807. \009inputOffset = inputOffset or 1\
  808. \009output = output or {}\
  809. \009outputOffset = outputOffset or 1\
  810. \
  811. \009local state = {}\
  812. \009local tmpState = {}\
  813. \
  814. \009if (key[KEY_TYPE] ~= ENCRYPTION_KEY) then\
  815. \009\009print(\"No encryption key: \", key[KEY_TYPE])\
  816. \009\009return\
  817. \009end\
  818. \
  819. \009state = util.bytesToInts(input, inputOffset, 4)\
  820. \009addRoundKey(state, key, 0)\
  821. \
  822. \009local checkIn = util.sleepCheckIn\
  823. \
  824. \009local round = 1\
  825. \009while (round < key[ROUNDS] - 1) do\
  826. \009\009-- do a double round to save temporary assignments\
  827. \009\009doRound(state, tmpState)\
  828. \009\009addRoundKey(tmpState, key, round)\
  829. \009\009round = round + 1\
  830. \
  831. \009\009doRound(tmpState, state)\
  832. \009\009addRoundKey(state, key, round)\
  833. \009\009round = round + 1\
  834. \009end\
  835. \
  836. \009checkIn()\
  837. \
  838. \009doRound(state, tmpState)\
  839. \009addRoundKey(tmpState, key, round)\
  840. \009round = round +1\
  841. \
  842. \009doLastRound(tmpState, state)\
  843. \009addRoundKey(state, key, round)\
  844. \
  845. \009return util.intsToBytes(state, output, outputOffset)\
  846. end\
  847. \
  848. --\
  849. -- decrypt 16 bytes\
  850. -- key           decryption key schedule\
  851. -- input         array with input data\
  852. -- inputOffset   start index for input\
  853. -- output        array for decrypted data\
  854. -- outputOffset  start index for output\
  855. ---\
  856. local function decrypt(key, input, inputOffset, output, outputOffset)\
  857. \009-- default arguments\
  858. \009inputOffset = inputOffset or 1\
  859. \009output = output or {}\
  860. \009outputOffset = outputOffset or 1\
  861. \
  862. \009local state = {}\
  863. \009local tmpState = {}\
  864. \
  865. \009if (key[KEY_TYPE] ~= DECRYPTION_KEY) then\
  866. \009\009print(\"No decryption key: \", key[KEY_TYPE])\
  867. \009\009return\
  868. \009end\
  869. \
  870. \009state = util.bytesToInts(input, inputOffset, 4)\
  871. \009addRoundKey(state, key, key[ROUNDS])\
  872. \
  873. \009local checkIn = util.sleepCheckIn\
  874. \
  875. \009local round = key[ROUNDS] - 1\
  876. \009while (round > 2) do\
  877. \009\009-- do a double round to save temporary assignments\
  878. \009\009doInvRound(state, tmpState)\
  879. \009\009addRoundKey(tmpState, key, round)\
  880. \009\009round = round - 1\
  881. \
  882. \009\009doInvRound(tmpState, state)\
  883. \009\009addRoundKey(state, key, round)\
  884. \009\009round = round - 1\
  885. \
  886. \009\009if round % 32 == 0 then\
  887. \009\009\009checkIn()\
  888. \009\009end\
  889. \009end\
  890. \
  891. \009checkIn()\
  892. \
  893. \009doInvRound(state, tmpState)\
  894. \009addRoundKey(tmpState, key, round)\
  895. \009round = round - 1\
  896. \
  897. \009doInvLastRound(tmpState, state)\
  898. \009addRoundKey(state, key, round)\
  899. \
  900. \009return util.intsToBytes(state, output, outputOffset)\
  901. end\
  902. \
  903. -- calculate all tables when loading this file\
  904. calcSBox()\
  905. calcRoundTables()\
  906. calcInvRoundTables()\
  907. \
  908. return {\
  909. \009ROUNDS = ROUNDS,\
  910. \009KEY_TYPE = KEY_TYPE,\
  911. \009ENCRYPTION_KEY = ENCRYPTION_KEY,\
  912. \009DECRYPTION_KEY = DECRYPTION_KEY,\
  913. \
  914. \009expandEncryptionKey = expandEncryptionKey,\
  915. \009expandDecryptionKey = expandDecryptionKey,\
  916. \009encrypt = encrypt,\
  917. \009decrypt = decrypt,\
  918. }\
  919. end)\
  920. local buffer=_W(function()\
  921. local function new ()\
  922. \009return {}\
  923. end\
  924. \
  925. local function addString (stack, s)\
  926. \009table.insert(stack, s)\
  927. \009for i = #stack - 1, 1, -1 do\
  928. \009\009if #stack[i] > #stack[i+1] then\
  929. \009\009\009\009break\
  930. \009\009end\
  931. \009\009stack[i] = stack[i] .. table.remove(stack)\
  932. \009end\
  933. end\
  934. \
  935. local function toString (stack)\
  936. \009for i = #stack - 1, 1, -1 do\
  937. \009\009stack[i] = stack[i] .. table.remove(stack)\
  938. \009end\
  939. \009return stack[1]\
  940. end\
  941. \
  942. return {\
  943. \009new = new,\
  944. \009addString = addString,\
  945. \009toString = toString,\
  946. }\
  947. end)\
  948. local ciphermode=_W(function()\
  949. local public = {}\
  950. \
  951. --\
  952. -- Encrypt strings\
  953. -- key - byte array with key\
  954. -- string - string to encrypt\
  955. -- modefunction - function for cipher mode to use\
  956. --\
  957. function public.encryptString(key, data, modeFunction)\
  958. \009local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}\
  959. \009local keySched = aes.expandEncryptionKey(key)\
  960. \009local encryptedData = buffer.new()\
  961. \
  962. \009for i = 1, #data/16 do\
  963. \009\009local offset = (i-1)*16 + 1\
  964. \009\009local byteData = {string.byte(data,offset,offset +15)}\
  965. \
  966. \009\009modeFunction(keySched, byteData, iv)\
  967. \
  968. \009\009buffer.addString(encryptedData, string.char(unpack(byteData)))\
  969. \009end\
  970. \
  971. \009return buffer.toString(encryptedData)\
  972. end\
  973. \
  974. --\
  975. -- the following 4 functions can be used as\
  976. -- modefunction for encryptString\
  977. --\
  978. \
  979. -- Electronic code book mode encrypt function\
  980. function public.encryptECB(keySched, byteData, iv)\
  981. \009aes.encrypt(keySched, byteData, 1, byteData, 1)\
  982. end\
  983. \
  984. -- Cipher block chaining mode encrypt function\
  985. function public.encryptCBC(keySched, byteData, iv)\
  986. \009util.xorIV(byteData, iv)\
  987. \
  988. \009aes.encrypt(keySched, byteData, 1, byteData, 1)\
  989. \
  990. \009for j = 1,16 do\
  991. \009\009iv[j] = byteData[j]\
  992. \009end\
  993. end\
  994. \
  995. -- Output feedback mode encrypt function\
  996. function public.encryptOFB(keySched, byteData, iv)\
  997. \009aes.encrypt(keySched, iv, 1, iv, 1)\
  998. \009util.xorIV(byteData, iv)\
  999. end\
  1000. \
  1001. -- Cipher feedback mode encrypt function\
  1002. function public.encryptCFB(keySched, byteData, iv)\
  1003. \009aes.encrypt(keySched, iv, 1, iv, 1)\
  1004. \009util.xorIV(byteData, iv)\
  1005. \
  1006. \009for j = 1,16 do\
  1007. \009\009iv[j] = byteData[j]\
  1008. \009end\
  1009. end\
  1010. \
  1011. --\
  1012. -- Decrypt strings\
  1013. -- key - byte array with key\
  1014. -- string - string to decrypt\
  1015. -- modefunction - function for cipher mode to use\
  1016. --\
  1017. function public.decryptString(key, data, modeFunction)\
  1018. \009local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}\
  1019. \
  1020. \009local keySched\
  1021. \009if (modeFunction == public.decryptOFB or modeFunction == public.decryptCFB) then\
  1022. \009\009keySched = aes.expandEncryptionKey(key)\
  1023. \009else\
  1024. \009\009keySched = aes.expandDecryptionKey(key)\
  1025. \009end\
  1026. \
  1027. \009local decryptedData = buffer.new()\
  1028. \
  1029. \009for i = 1, #data/16 do\
  1030. \009\009local offset = (i-1)*16 + 1\
  1031. \009\009local byteData = {string.byte(data,offset,offset +15)}\
  1032. \
  1033. \009\009iv = modeFunction(keySched, byteData, iv)\
  1034. \
  1035. \009\009buffer.addString(decryptedData, string.char(unpack(byteData)))\
  1036. \009end\
  1037. \
  1038. \009return buffer.toString(decryptedData)\
  1039. end\
  1040. \
  1041. --\
  1042. -- the following 4 functions can be used as\
  1043. -- modefunction for decryptString\
  1044. --\
  1045. \
  1046. -- Electronic code book mode decrypt function\
  1047. function public.decryptECB(keySched, byteData, iv)\
  1048. \
  1049. \009aes.decrypt(keySched, byteData, 1, byteData, 1)\
  1050. \
  1051. \009return iv\
  1052. end\
  1053. \
  1054. -- Cipher block chaining mode decrypt function\
  1055. function public.decryptCBC(keySched, byteData, iv)\
  1056. \009local nextIV = {}\
  1057. \009for j = 1,16 do\
  1058. \009\009nextIV[j] = byteData[j]\
  1059. \009end\
  1060. \
  1061. \009aes.decrypt(keySched, byteData, 1, byteData, 1)\
  1062. \009util.xorIV(byteData, iv)\
  1063. \
  1064. \009return nextIV\
  1065. end\
  1066. \
  1067. -- Output feedback mode decrypt function\
  1068. function public.decryptOFB(keySched, byteData, iv)\
  1069. \009aes.encrypt(keySched, iv, 1, iv, 1)\
  1070. \009util.xorIV(byteData, iv)\
  1071. \
  1072. \009return iv\
  1073. end\
  1074. \
  1075. -- Cipher feedback mode decrypt function\
  1076. function public.decryptCFB(keySched, byteData, iv)\
  1077. \009local nextIV = {}\
  1078. \009for j = 1,16 do\
  1079. \009\009nextIV[j] = byteData[j]\
  1080. \009end\
  1081. \
  1082. \009aes.encrypt(keySched, iv, 1, iv, 1)\
  1083. \
  1084. \009util.xorIV(byteData, iv)\
  1085. \
  1086. \009return nextIV\
  1087. end\
  1088. \
  1089. return public\
  1090. end)\
  1091. --@require lib/ciphermode.lua\
  1092. --@require lib/util.lua\
  1093. --\
  1094. -- Simple API for encrypting strings.\
  1095. --\
  1096. AES.AES128 = 16\
  1097. AES.AES192 = 24\
  1098. AES.AES256 = 32\
  1099. \
  1100. AES.ECBMODE = 1\
  1101. AES.CBCMODE = 2\
  1102. AES.OFBMODE = 3\
  1103. AES.CFBMODE = 4\
  1104. \
  1105. local function pwToKey(password, keyLength)\
  1106. \009local padLength = keyLength\
  1107. \009if (keyLength == AES.AES192) then\
  1108. \009\009padLength = 32\
  1109. \009end\
  1110. \009\
  1111. \009if (padLength > #password) then\
  1112. \009\009local postfix = \"\"\
  1113. \009\009for i = 1,padLength - #password do\
  1114. \009\009\009postfix = postfix .. string.char(0)\
  1115. \009\009end\
  1116. \009\009password = password .. postfix\
  1117. \009else\
  1118. \009\009password = string.sub(password, 1, padLength)\
  1119. \009end\
  1120. \009\
  1121. \009local pwBytes = {string.byte(password,1,#password)}\
  1122. \009password = ciphermode.encryptString(pwBytes, password, ciphermode.encryptCBC)\
  1123. \009\
  1124. \009password = string.sub(password, 1, keyLength)\
  1125.   \
  1126. \009return {string.byte(password,1,#password)}\
  1127. end\
  1128. \
  1129. --\
  1130. -- Encrypts string data with password password.\
  1131. -- password  - the encryption key is generated from this string\
  1132. -- data      - string to encrypt (must not be too large)\
  1133. -- keyLength - length of aes key: 128(default), 192 or 256 Bit\
  1134. -- mode      - mode of encryption: ecb, cbc(default), ofb, cfb \
  1135. --\
  1136. -- mode and keyLength must be the same for encryption and decryption.\
  1137. --\
  1138. function AES.encrypt(password, data, keyLength, mode)\
  1139. \009assert(password ~= nil, \"Empty password.\")\
  1140. \009assert(password ~= nil, \"Empty data.\")\
  1141. \009 \
  1142. \009local mode = mode or AES.CBCMODE\
  1143. \009local keyLength = keyLength or AES.AES128\
  1144. \
  1145. \009local key = pwToKey(password, keyLength)\
  1146. \
  1147. \009local paddedData = util.padByteString(data)\
  1148. \009\
  1149. \009if (mode == AES.ECBMODE) then\
  1150. \009\009return ciphermode.encryptString(key, paddedData, ciphermode.encryptECB)\
  1151. \009elseif (mode == AES.CBCMODE) then\
  1152. \009\009return ciphermode.encryptString(key, paddedData, ciphermode.encryptCBC)\
  1153. \009elseif (mode == AES.OFBMODE) then\
  1154. \009\009return ciphermode.encryptString(key, paddedData, ciphermode.encryptOFB)\
  1155. \009elseif (mode == AES.CFBMODE) then\
  1156. \009\009return ciphermode.encryptString(key, paddedData, ciphermode.encryptCFB)\
  1157. \009else\
  1158. \009\009return nil\
  1159. \009end\
  1160. end\
  1161. \
  1162. \
  1163. \
  1164. \
  1165. --\
  1166. -- Decrypts string data with password password.\
  1167. -- password  - the decryption key is generated from this string\
  1168. -- data      - string to encrypt\
  1169. -- keyLength - length of aes key: 128(default), 192 or 256 Bit\
  1170. -- mode      - mode of decryption: ecb, cbc(default), ofb, cfb \
  1171. --\
  1172. -- mode and keyLength must be the same for encryption and decryption.\
  1173. --\
  1174. function AES.decrypt(password, data, keyLength, mode)\
  1175. \009local mode = mode or AES.CBCMODE\
  1176. \009local keyLength = keyLength or AES.AES128\
  1177. \
  1178. \009local key = pwToKey(password, keyLength)\
  1179. \009\
  1180. \009local plain\
  1181. \009if (mode == AES.ECBMODE) then\
  1182. \009\009plain = ciphermode.decryptString(key, data, ciphermode.decryptECB)\
  1183. \009elseif (mode == AES.CBCMODE) then\
  1184. \009\009plain = ciphermode.decryptString(key, data, ciphermode.decryptCBC)\
  1185. \009elseif (mode == AES.OFBMODE) then\
  1186. \009\009plain = ciphermode.decryptString(key, data, ciphermode.decryptOFB)\
  1187. \009elseif (mode == AES.CFBMODE) then\
  1188. \009\009plain = ciphermode.decryptString(key, data, ciphermode.decryptCFB)\
  1189. \009end\
  1190. \009\
  1191. \009result = util.unpadByteString(plain)\
  1192. \009\
  1193. \009if (result == nil) then\
  1194. \009\009return nil\
  1195. \009end\
  1196. \009\
  1197. \009return result\
  1198. end\
  1199. \
  1200. --I (Creator )added this\
  1201. \
  1202. function AES.encryptBytes(...)\
  1203. \009return {AES.encrypt(...):byte(1,-1)}\
  1204. end\
  1205. \
  1206. function AES.decryptBytes(a,i,o,n)\
  1207. \009return AES.decrypt(a,string.char(unpack(i)),o,n)\
  1208. end\
  1209. \
  1210. return AES",
  1211.     [ "SHA.lua" ] = "-- SHA-256 implementation in CC-Lua\
  1212. -- By Anavrins\
  1213. \
  1214. local mod = 2^32\
  1215. \
  1216. local band    = bit32 and bit32.band or bit.band\
  1217. local bnot    = bit32 and bit32.bnot or bit.bnot\
  1218. local bxor    = bit32 and bit32.bxor or bit.bxor\
  1219. local blshift = bit32 and bit32.lshift or bit.blshift\
  1220. local upack   = unpack\
  1221. \
  1222. local function rrotate(n, b)\
  1223. \009local s = n/(2^b)\
  1224. \009local f = s%1\
  1225. \009return (s-f) + f*mod\
  1226. end\
  1227. local function brshift(int, by) -- Thanks bit32 for bad rshift\
  1228. \009local s = int / (2^by)\
  1229. \009return s - s%1\
  1230. end\
  1231. \
  1232. local H = {\
  1233. \0090x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,\
  1234. \0090x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,\
  1235. }\
  1236. \
  1237. local K = {\
  1238. \0090x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
  1239. \0090xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
  1240. \0090xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
  1241. \0090x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
  1242. \0090x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
  1243. \0090xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
  1244. \0090x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
  1245. \0090x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,\
  1246. }\
  1247. \
  1248. local function incr(t, incr)\
  1249. \009if 0xFFFFFFFF - t[1] < incr then\
  1250. \009\009t[2] = t[2] + 1\
  1251. \009\009t[1] = incr - (0xFFFFFFFF - t[1]) - 1\009\009\
  1252. \009else t[1] = t[1] + incr\
  1253. \009end\
  1254. \009return t\
  1255. end\
  1256. \
  1257. local function preprocess(data)\
  1258. \009local len = #data\
  1259. \
  1260. \009data[#data+1] = 0x80\
  1261. \009while #data%64~=56 do data[#data+1] = 0 end\
  1262. \009local l = incr({0,0}, len*8)\
  1263. \009for i = 2, 1, -1 do\
  1264. \009\009data[#data+1] = band(brshift(band(l[i], 0xFF000000), 24), 0xFF)\
  1265. \009\009data[#data+1] = band(brshift(band(l[i], 0xFF0000), 16), 0xFF)\
  1266. \009\009data[#data+1] = band(brshift(band(l[i], 0xFF00), 8), 0xFF)\
  1267. \009\009data[#data+1] = band(l[i], 0xFF)\
  1268. \009end\
  1269. \009return data\
  1270. end\
  1271. \
  1272. local function BE_toInt(bs, i)\
  1273. \009return blshift((bs[i] or 0), 24) + blshift((bs[i+1] or 0), 16) + blshift((bs[i+2] or 0), 8) + (bs[i+3] or 0)\
  1274. end\
  1275. \
  1276. local function digestblock(data, i, C)\
  1277. \009local w = {}\
  1278. \009for j = 1, 16 do w[j] = BE_toInt(data, i+(j-1)*4) end\
  1279. \009for j = 17, 64 do\
  1280. \009\009local v = w[j-15]\
  1281. \009\009local s0 = bxor(bxor(rrotate(w[j-15], 7), rrotate(w[j-15], 18)), brshift(w[j-15], 3))\
  1282. \009\009local s1 = bxor(bxor(rrotate(w[j-2], 17), rrotate(w[j-2], 19)), brshift(w[j-2], 10))\
  1283. \009\009w[j] = (w[j-16] + s0 + w[j-7] + s1)%mod\
  1284. \009end\
  1285. \009local a, b, c, d, e, f, g, h = upack(C)\
  1286. \009for j = 1, 64 do\
  1287. \009\009local S1 = bxor(bxor(rrotate(e, 6), rrotate(e, 11)), rrotate(e, 25))\
  1288. \009\009local ch = bxor(band(e, f), band(bnot(e), g))\
  1289. \009\009local temp1 = (h + S1 + ch + K[j] + w[j])%mod\
  1290. \009\009local S0 = bxor(bxor(rrotate(a, 2), rrotate(a, 13)), rrotate(a, 22))\
  1291. \009\009local maj = bxor(bxor(band(a, b), band(a, c)), band(b, c))\
  1292. \009\009local temp2 = (S0 + maj)%mod\
  1293. \009\009h, g, f, e, d, c, b, a = g, f, e, (d+temp1)%mod, c, b, a, (temp1+temp2)%mod\
  1294. \009end\
  1295. \009C[1] = (C[1] + a)%mod\
  1296. \009C[2] = (C[2] + b)%mod\
  1297. \009C[3] = (C[3] + c)%mod\
  1298. \009C[4] = (C[4] + d)%mod\
  1299. \009C[5] = (C[5] + e)%mod\
  1300. \009C[6] = (C[6] + f)%mod\
  1301. \009C[7] = (C[7] + g)%mod\
  1302. \009C[8] = (C[8] + h)%mod\
  1303. \009return C\
  1304. end\
  1305. \
  1306. return function(data)\
  1307. \009data = data or \"\"\
  1308. \009data = type(data) == \"string\" and {data:byte(1,-1)} or data\
  1309. \
  1310. \009data = preprocess(data)\
  1311. \009local C = {upack(H)}\
  1312. \009for i = 1, #data, 64 do C = digestblock(data, i, C) end\
  1313. \009return ((\"%08x\"):rep(8)):format(upack(C))\
  1314. end",
  1315.     [ "textutils.lua" ] = "\
  1316. local textutils = {}\
  1317. \
  1318. function textutils.slowWrite( sText, nRate )\
  1319.    nRate = nRate or 20\
  1320.    if nRate < 0 then\
  1321.        error( \"Rate must be positive\", 2 )\
  1322.    end\
  1323.    local nSleep = 1 / nRate\
  1324.        \
  1325.    sText = tostring( sText )\
  1326.    local x,y = term.getCursorPos(x,y)\
  1327.    local len = string.len( sText )\
  1328.    \
  1329.    for n=1,len do\
  1330.        term.setCursorPos( x, y )\
  1331.        sleep( nSleep )\
  1332.        local nLines = write( string.sub( sText, 1, n ) )\
  1333.        local newX, newY = term.getCursorPos()\
  1334.        y = newY - nLines\
  1335.    end\
  1336. end\
  1337. \
  1338. function textutils.slowPrint( sText, nRate )\
  1339.    slowWrite( sText, nRate)\
  1340.    print()\
  1341. end\
  1342. \
  1343. function textutils.formatTime( nTime, bTwentyFourHour )\
  1344.    local sTOD = nil\
  1345.    if not bTwentyFourHour then\
  1346.        if nTime >= 12 then\
  1347.            sTOD = \"PM\"\
  1348.        else\
  1349.            sTOD = \"AM\"\
  1350.        end\
  1351.        if nTime >= 13 then\
  1352.            nTime = nTime - 12\
  1353.        end\
  1354.    end\
  1355. \
  1356.    local nHour = math.floor(nTime)\
  1357.    local nMinute = math.floor((nTime - nHour)*60)\
  1358.    if sTOD then\
  1359.        return string.format( \"%d:%02d %s\", nHour, nMinute, sTOD )\
  1360.    else\
  1361.        return string.format( \"%d:%02d\", nHour, nMinute )\
  1362.    end\
  1363. end\
  1364. \
  1365. local function makePagedScroll( _term, _nFreeLines )\
  1366.    local nativeScroll = _term.scroll\
  1367.    local nFreeLines = _nFreeLines or 0\
  1368.    return function( _n )\
  1369.        for n=1,_n do\
  1370.            nativeScroll( 1 )\
  1371.            \
  1372.            if nFreeLines <= 0 then\
  1373.                local w,h = _term.getSize()\
  1374.                _term.setCursorPos( 1, h )\
  1375.                _term.write( \"Press any key to continue\" )\
  1376.                os.pullEvent( \"key\" )\
  1377.                _term.clearLine()\
  1378.                _term.setCursorPos( 1, h )\
  1379.            else\
  1380.                nFreeLines = nFreeLines - 1\
  1381.            end\
  1382.        end\
  1383.    end\
  1384. end\
  1385. \
  1386. function textutils.pagedPrint( _sText, _nFreeLines )\
  1387.    -- Setup a redirector\
  1388.    local oldTerm = term.current()\
  1389.    local newTerm = {}\
  1390.    for k,v in pairs( oldTerm ) do\
  1391.        newTerm[k] = v\
  1392.    end\
  1393.    newTerm.scroll = makePagedScroll( oldTerm, _nFreeLines )\
  1394.    term.redirect( newTerm )\
  1395. \
  1396.    -- Print the text\
  1397.    local result\
  1398.    local ok, err = pcall( function()\
  1399.        if _sText ~= nil then\
  1400.            result = print( _sText )\
  1401.        else\
  1402.            result = print()\
  1403.        end\
  1404.    end )\
  1405. \
  1406.    -- Removed the redirector\
  1407.    term.redirect( oldTerm )\
  1408. \
  1409.    -- Propogate errors\
  1410.    if not ok then\
  1411.        error( err, 0 )\
  1412.    end\
  1413.    return result\
  1414. end\
  1415. \
  1416. local function tabulateCommon( bPaged, ... )\
  1417.    local tAll = { ... }\
  1418.    \
  1419.    local w,h = term.getSize()\
  1420.    local nMaxLen = w / 8\
  1421.    for n, t in ipairs( tAll ) do\
  1422.        if type(t) == \"table\" then\
  1423.            for n, sItem in pairs(t) do\
  1424.                nMaxLen = math.max( string.len( sItem ) + 1, nMaxLen )\
  1425.            end\
  1426.        end\
  1427.    end\
  1428.    local nCols = math.floor( w / nMaxLen )\
  1429.    local nLines = 0\
  1430.    local function newLine()\
  1431.        if bPaged and nLines >= (h-3) then\
  1432.            pagedPrint()\
  1433.        else\
  1434.            print()\
  1435.        end\
  1436.        nLines = nLines + 1\
  1437.    end\
  1438.    \
  1439.    local function drawCols( _t )\
  1440.        local nCol = 1\
  1441.        for n, s in ipairs( _t ) do\
  1442.            if nCol > nCols then\
  1443.                nCol = 1\
  1444.                newLine()\
  1445.            end\
  1446. \
  1447.            local cx, cy = term.getCursorPos()\
  1448.            cx = 1 + ((nCol - 1) * nMaxLen)\
  1449.            term.setCursorPos( cx, cy )\
  1450.            term.write( s )\
  1451. \
  1452.            nCol = nCol + 1      \
  1453.        end\
  1454.        print()\
  1455.    end\
  1456.    for n, t in ipairs( tAll ) do\
  1457.        if type(t) == \"table\" then\
  1458.            if #t > 0 then\
  1459.                drawCols( t )\
  1460.            end\
  1461.        elseif type(t) == \"number\" then\
  1462.            term.setTextColor( t )\
  1463.        end\
  1464.    end    \
  1465. end\
  1466. \
  1467. function textutils.tabulate( ... )\
  1468.    tabulateCommon( false, ... )\
  1469. end\
  1470. \
  1471. function textutils.pagedTabulate( ... )\
  1472.    tabulateCommon( true, ... )\
  1473. end\
  1474. \
  1475. local g_tLuaKeywords = {\
  1476.    [ \"and\" ] = true,\
  1477.    [ \"break\" ] = true,\
  1478.    [ \"do\" ] = true,\
  1479.    [ \"else\" ] = true,\
  1480.    [ \"elseif\" ] = true,\
  1481.    [ \"end\" ] = true,\
  1482.    [ \"false\" ] = true,\
  1483.    [ \"for\" ] = true,\
  1484.    [ \"function\" ] = true,\
  1485.    [ \"if\" ] = true,\
  1486.    [ \"in\" ] = true,\
  1487.    [ \"local\" ] = true,\
  1488.    [ \"nil\" ] = true,\
  1489.    [ \"not\" ] = true,\
  1490.    [ \"or\" ] = true,\
  1491.    [ \"repeat\" ] = true,\
  1492.    [ \"return\" ] = true,\
  1493.    [ \"then\" ] = true,\
  1494.    [ \"true\" ] = true,\
  1495.    [ \"until\" ] = true,\
  1496.    [ \"while\" ] = true,\
  1497. }\
  1498. \
  1499. local function serializeImpl( t, tTracking, sIndent )\
  1500.    local sType = type(t)\
  1501.    if sType == \"table\" then\
  1502.        if tTracking[t] ~= nil then\
  1503.            return tostring(t)\
  1504.        end\
  1505.        tTracking[t] = true\
  1506. \
  1507.        if next(t) == nil then\
  1508.            -- Empty tables are simple\
  1509.            return \"{}\"\
  1510.        else\
  1511.            -- Other tables take more work\
  1512.            local sResult = \"{\\n\"\
  1513.            local sSubIndent = sIndent .. \"  \"\
  1514.            local tSeen = {}\
  1515.            for k,v in ipairs(t) do\
  1516.                tSeen[k] = true\
  1517.                sResult = sResult .. sSubIndent .. serializeImpl( v, tTracking, sSubIndent ) .. \",\\n\"\
  1518.            end\
  1519.            for k,v in pairs(t) do\
  1520.                if not tSeen[k] then\
  1521.                    local sEntry\
  1522.                    if type(k) == \"string\" and not g_tLuaKeywords[k] and string.match( k, \"^[%a_][%a%d_]*$\" ) then\
  1523.                        sEntry = k .. \" = \" .. serializeImpl( v, tTracking, sSubIndent ) .. \",\\n\"\
  1524.                    else\
  1525.                        sEntry = \"[ \" .. serializeImpl( k, tTracking, sSubIndent ) .. \" ] = \" .. serializeImpl( v, tTracking, sSubIndent ) .. \",\\n\"\
  1526.                    end\
  1527.                    sResult = sResult .. sSubIndent .. sEntry\
  1528.                end\
  1529.            end\
  1530.            sResult = sResult .. sIndent .. \"}\"\
  1531.            return sResult\
  1532.        end\
  1533.        \
  1534.    elseif sType == \"string\" then\
  1535.        return string.format( \"%q\", t )\
  1536.    \
  1537.    else\
  1538.        return tostring(t) \
  1539.    end\
  1540. end\
  1541. \
  1542. empty_json_array = {}\
  1543. \
  1544. local function serializeJSONImpl( t, tTracking, bNBTStyle )\
  1545.    local sType = type(t)\
  1546.    if t == empty_json_array then\
  1547.        return \"[]\"\
  1548. \
  1549.    elseif sType == \"table\" then\
  1550.        if tTracking[t] ~= nil then\
  1551.            error( \"Cannot serialize table with recursive entries\", 0 )\
  1552.        end\
  1553.        tTracking[t] = true\
  1554. \
  1555.        if next(t) == nil then\
  1556.            -- Empty tables are simple\
  1557.            return \"{}\"\
  1558.        else\
  1559.            -- Other tables take more work\
  1560.            local sObjectResult = \"{\"\
  1561.            local sArrayResult = \"[\"\
  1562.            local nObjectSize = 0\
  1563.            local nArraySize = 0\
  1564.            for k,v in pairs(t) do\
  1565.                if type(k) == \"string\" then\
  1566.                    local sEntry\
  1567.                    if bNBTStyle then\
  1568.                        sEntry = tostring(k) .. \":\" .. serializeJSONImpl( v, tTracking, bNBTStyle )\
  1569.                    else\
  1570.                        sEntry = string.format( \"%q\", k ) .. \":\" .. serializeJSONImpl( v, tTracking, bNBTStyle )\
  1571.                    end\
  1572.                    if nObjectSize == 0 then\
  1573.                        sObjectResult = sObjectResult .. sEntry\
  1574.                    else\
  1575.                        sObjectResult = sObjectResult .. \",\" .. sEntry\
  1576.                    end\
  1577.                    nObjectSize = nObjectSize + 1\
  1578.                end\
  1579.            end\
  1580.            for n,v in ipairs(t) do\
  1581.                local sEntry = serializeJSONImpl( v, tTracking, bNBTStyle )\
  1582.                if nArraySize == 0 then\
  1583.                    sArrayResult = sArrayResult .. sEntry\
  1584.                else\
  1585.                    sArrayResult = sArrayResult .. \",\" .. sEntry\
  1586.                end\
  1587.                nArraySize = nArraySize + 1\
  1588.            end\
  1589.            sObjectResult = sObjectResult .. \"}\"\
  1590.            sArrayResult = sArrayResult .. \"]\"\
  1591.            if nObjectSize > 0 or nArraySize == 0 then\
  1592.                return sObjectResult\
  1593.            else\
  1594.                return sArrayResult\
  1595.            end\
  1596.        end\
  1597. \
  1598.    elseif sType == \"string\" then\
  1599.        return string.format( \"%q\", t )\
  1600. \
  1601.    elseif sType == \"number\" or sType == \"boolean\" then\
  1602.        return tostring(t)\
  1603. \
  1604.    else\
  1605.        error( \"Cannot serialize type \"..sType, 0 )\
  1606. \
  1607.    end\
  1608. end\
  1609. \
  1610. function textutils.serialize( t )\
  1611.    local tTracking = {}\
  1612.    return serializeImpl( t, tTracking, \"\" )\
  1613. end\
  1614. \
  1615. function textutils.unserialize( s )\
  1616.    local func = load( \"return \"..s, \"unserialize\", \"t\", {} )\
  1617.    if func then\
  1618.        local ok, result = pcall( func )\
  1619.        if ok then\
  1620.            return result\
  1621.        end\
  1622.    end\
  1623.    return nil\
  1624. end\
  1625. \
  1626. function textutils.serializeJSON( t, bNBTStyle )\
  1627.    local tTracking = {}\
  1628.    return serializeJSONImpl( t, tTracking, bNBTStyle or false )\
  1629. end\
  1630. \
  1631. function textutils.urlEncode( str )\
  1632.    if str then\
  1633.        str = string.gsub(str, \"\\n\", \"\\r\\n\")\
  1634.        str = string.gsub(str, \"([^A-Za-z0-9 %-%_%.])\", function(c)\
  1635.            local n = string.byte(c)\
  1636.            if n < 128 then\
  1637.                -- ASCII\
  1638.                return string.format(\"%%%02X\", n)\
  1639.            else\
  1640.                -- Non-ASCII (encode as UTF-8)\
  1641.                return\
  1642.                    string.format(\"%%%02X\", 192 + bit32.band( bit32.arshift(n,6), 31 ) ) ..\
  1643.                    string.format(\"%%%02X\", 128 + bit32.band( n, 63 ) )\
  1644.            end\
  1645.        end )\
  1646.        str = string.gsub(str, \" \", \"+\")\
  1647.    end\
  1648.    return str    \
  1649. end\
  1650. \
  1651. local tEmpty = {}\
  1652. function textutils.complete( sSearchText, tSearchTable )\
  1653.    local nStart = 1\
  1654.    local nDot = string.find( sSearchText, \".\", nStart, true )\
  1655.    local tTable = tSearchTable or _ENV\
  1656.    while nDot do\
  1657.        local sPart = string.sub( sSearchText, nStart, nDot - 1 )\
  1658.        local value = tTable[ sPart ]\
  1659.        if type( value ) == \"table\" then\
  1660.            tTable = value\
  1661.            nStart = nDot + 1\
  1662.            nDot = string.find( sSearchText, \".\", nStart, true )\
  1663.        else\
  1664.            return tEmpty\
  1665.        end\
  1666.    end\
  1667. \
  1668.    local sPart = string.sub( sSearchText, nStart, nDot )\
  1669.    local nPartLength = string.len( sPart )\
  1670. \
  1671.    local tResults = {}\
  1672.    local tSeen = {}\
  1673.    while tTable do\
  1674.        for k,v in pairs( tTable ) do\
  1675.            if not tSeen[k] and type(k) == \"string\" then\
  1676.                if string.find( k, sPart, 1, true ) == 1 then\
  1677.                    if not g_tLuaKeywords[k] and string.match( k, \"^[%a_][%a%d_]*$\" ) then\
  1678.                        local sResult = string.sub( k, nPartLength + 1 )\
  1679.                        if type(v) == \"function\" then\
  1680.                            sResult = sResult .. \"(\"\
  1681.                        elseif type(v) == \"table\" and next(v) ~= nil then\
  1682.                            sResult = sResult .. \".\"\
  1683.                        end\
  1684.                        table.insert( tResults, sResult )\
  1685.                    end\
  1686.                end\
  1687.            end\
  1688.            tSeen[k] = true\
  1689.        end\
  1690.        local tMetatable = getmetatable( tTable )\
  1691.        if tMetatable and type( tMetatable.__index ) == \"table\" then\
  1692.            tTable = tMetatable.__index\
  1693.        else\
  1694.            tTable = nil\
  1695.        end\
  1696.    end\
  1697. \
  1698.    table.sort( tResults )\
  1699.    return tResults\
  1700. end\
  1701. \
  1702. -- GB versions\
  1703. textutils.serialise = textutils.serialize\
  1704. textutils.unserialise = textutils.unserialize\
  1705. textutils.serialiseJSON = textutils.serializeJSON\
  1706. \
  1707. return textutils",
  1708.     [ "crc.lua" ] = "--[[\
  1709. \009CRC lib for OmniOS\
  1710. ]]\
  1711. \
  1712. local bnot   = bit32.bnot\
  1713. local rshift = bit32.rshift\
  1714. local bxor   = bit32.bxor\
  1715. local band   = bit32.band\
  1716. local crc_table = {}\
  1717. local POLY = 0xEDB88320\
  1718. \
  1719. local function crc_core(crc)\
  1720. \009for _=1, 8 do\
  1721. \009\009local b = band(crc, 1)\
  1722. \009\009crc = rshift(crc, 1)\
  1723. \009\009if b == 1 then crc = bxor(crc, POLY) end\
  1724. \009end\
  1725. \009return crc\
  1726. end\
  1727. \
  1728. local function crc_byte(byte, crc)\
  1729. \009crc = bnot(crc or 0)\
  1730. \009local a = rshift(crc, 8)\
  1731. \009local c = bxor(crc % 256, byte)\
  1732. \009local b = crc_table[c]\
  1733. \009if not b then\
  1734. \009\009b = crc_core(c)\
  1735. \009\009crc_table[c] = b\
  1736. \009end\
  1737. \009return bnot(bxor(a, b))\
  1738. end\
  1739. \
  1740. local function crc_string(str, crc)\
  1741. \009crc = crc or 0\
  1742. \009for i=1, #str do\
  1743. \009\009crc = crc_byte(str:byte(i,i), crc)\
  1744. \009end\
  1745. \009return crc\
  1746. end\
  1747. \
  1748. local function crc32(str, crc)\
  1749. \009if type(str) == \"string\" then\
  1750. \009\009return crc_string(str, crc)\
  1751. \009else\
  1752. \009\009return crc_byte(str, crc)\
  1753. \009end\
  1754. end\
  1755. \
  1756. return crc32",
  1757.   },
  1758.   Programs = {
  1759.     Edit = {
  1760.       [ "main.lua" ] = "dofile(\"OmniOS/bin/edit.lua\", ...)",
  1761.     },
  1762.     NeuralNetwork = {
  1763.       [ "NotWorking.lua" ] = "--Neural Network by Creator\
  1764. local eta = 0.15\
  1765. local alpha = 0.5\
  1766. --Neuron\
  1767. local Neuron = {\
  1768.  new = function(numInputs, first)\
  1769.    local self = {\
  1770.      output = 0,\
  1771.      weights = {},\
  1772.      gradient = 0\
  1773.    }\
  1774.    for i=(first and 1 or 0),numInputs do\
  1775.      self.weights[i] = 1/(numInputs+(first and 0 or 1))\
  1776.    end\
  1777.    setmetatable(self,{__index = Neuron})\
  1778.    print(\"Made neuron with \"..tostring(numInputs)..\" inputs!\")\
  1779.    return self\
  1780.  end,\
  1781. \
  1782.  run = function(self,input)\
  1783.    local sum = 0\
  1784.    for i=0,#input do\
  1785.      sum = sum + self.weights[i]*input[i]\
  1786.    end\
  1787.    self.output = 1 / (1 + math.exp((sum * -1) / 0.5))\
  1788.    print(self.output)\
  1789.    return self.output\
  1790.  end,\
  1791. \
  1792.  derivativeTransferFunction = function(x)\
  1793.    return 1.0 - x * x\
  1794.  end,\
  1795. \
  1796.  calculateOutputGradient = function(self,target)\
  1797.    local delta = target - self.output\
  1798.    self.gradient = delta * (1.0 - self.output*self.output)\
  1799.  end,\
  1800. \
  1801.  SumDOW  = function(nextLayer,index)\
  1802.    local sum = 0\
  1803.    for i=1,#nextLayer do\
  1804.      sum = sum + nextLayer[i].weights[index] * nextLayer[i].gradient\
  1805.    end\
  1806.    return sum\
  1807.  end,\
  1808. \
  1809.  calcHiddenGradient = function(self,nextLayer,index)\
  1810.    local dow = (function(nextLayer,index)\
  1811.      local sum = 0\
  1812.      for i=1,#nextLayer do\
  1813.        sum = sum + nextLayer[i].weights[index] * nextLayer[i].gradient\
  1814.      end\
  1815.      return sum\
  1816.    end)(nextLayer,index)\
  1817.    self.gradient = dow * (1.0 - self.output*self.output)\
  1818.  end,\
  1819. \
  1820.  updateInputWeights = function(self,prevLayer)\
  1821.    for i=0,#prevLayer do\
  1822.      local n = prevLayer[i]\
  1823.      local oldDeltaWeight = self.weights[i]\
  1824.      local newDeltaWeight = eta*\
  1825.      n.output*\
  1826.      self.gradient\
  1827.      +alpha*oldDeltaWeight\
  1828.      self.weights[i] = self.weights[i] + newDeltaWeight\
  1829.    end\
  1830.  end\
  1831. }\
  1832. \
  1833. --Layer\
  1834. local Layer = {\
  1835.  new = function(numNeurons,numInputs,first)\
  1836.    local self = {}\
  1837.    for i=1,numNeurons do\
  1838.      self[i] = Neuron.new(numInputs,first)\
  1839.    end\
  1840.    setmetatable(self,{__index = Layer})\
  1841.    return self\
  1842.  end,\
  1843. \
  1844.  run = function(self,input,isInput)\
  1845.    print(textutils.serialize(input))\
  1846.    if isInput then\
  1847.      local output = {[0] = 1}\
  1848.      for i=1,#input do\
  1849.        output[i] = input[i]\
  1850.      end\
  1851.      return output\
  1852.    end\
  1853.    local output = {[0] = 1}\
  1854.    for i=1,#self do\
  1855.      print(\"Running neuron \"..tostring(i))\
  1856.      output[i] = Neuron.run(self[i],input)\
  1857.    end\
  1858.    return output\
  1859.  end\
  1860. }\
  1861. \
  1862. --Network\
  1863. local Network = {\
  1864.  new = function(topology)\
  1865.    local self = {topology = {},m_error = 0}\
  1866.    self.topology[1] = Layer.new(topology[1],1,true)\
  1867.    for i=2, #topology do\
  1868.      self.topology[i] = Layer.new(topology[i],topology[i-1])\
  1869.    end\
  1870.    setmetatable(self,{__index = Network})\
  1871.    return self\
  1872.  end,\
  1873. \
  1874.  feedForward = function(self,input)\
  1875.    if #input ~= #self.topology[1] then return end\
  1876.    print(\"Running layer 1\")\
  1877.    local output = Layer.run(self.topology[1],input,true)\
  1878.    for i=2,#self.topology do\
  1879.      print(\"Running layer \"..tostring(i))\
  1880.      output = Layer.run(self.topology[i],output,false)\
  1881.    end\
  1882.    output[0] = nil\
  1883.    return output\
  1884.  end,\
  1885. \
  1886.  backPropagation = function(self,output)\
  1887.    local m_error = 0\
  1888.    for i=1,#self.topology[#self.topology] do\
  1889.    --  print(type(self.topology[#self.topology][i].output))\
  1890.      local delta = output[i] - self.topology[#self.topology][i].output\
  1891.      m_error = m_error + delta * delta\
  1892.    end\
  1893.    m_error = m_error/#self.topology[#self.topology]\
  1894.    m_error = math.sqrt(m_error)\
  1895.    self.m_error = m_error\
  1896. \
  1897.    for i=1,#self.topology[#self.topology] do\
  1898.      Neuron.calculateOutputGradient(self.topology[#self.topology][i],output[i])\
  1899.    end\
  1900. \
  1901.    for i=#self.topology-1,2,-1 do\
  1902.      local currentLayer = self.topology[i]\
  1903.      local nextLayer = self.topology[i+1]\
  1904.      for m=1,#currentLayer do\
  1905.        Neuron.calcHiddenGradient(self.topology[i][m],nextLayer,m)\
  1906.      end\
  1907.    end\
  1908. \
  1909.    for i=#self.topology,2,-1 do\
  1910.      local currentLayer = self.topology[i]\
  1911.      local prevlayer = self.topology[i-1]\
  1912.      for m=1,#currentLayer do\
  1913.        Neuron.updateInputWeights(self.topology[i][m],prevlayer)\
  1914.      end\
  1915.    end\
  1916. \
  1917.  end,\
  1918. \
  1919.  getResults = function()\
  1920. \
  1921.  end\
  1922. }\
  1923. \
  1924. net = Network.new({2,4,1})\
  1925. --[[file = fs.open(\"OmniOS/nnn\",\"w\")\
  1926. file.write(textutils.serialize(net)..\"\\n\"..textutils.serialize(getmetatable(net)))\
  1927. file.close()]]--\
  1928. l = 1\
  1929. while true do\
  1930.  l = l + 1\
  1931.  if l%50==0 then sleep(0) end\
  1932.  if l==500 then break end\
  1933.  Network.feedForward(net,{1,0})\
  1934.  Network.backPropagation(net,{1})\
  1935.  Network.feedForward(net,{1,1})\
  1936.  Network.backPropagation(net,{0})\
  1937.  Network.feedForward(net,{0,1})\
  1938.  Network.backPropagation(net,{1})\
  1939.  Network.feedForward(net,{0,0})\
  1940.  Network.backPropagation(net,0)\
  1941. end\
  1942. output = Network.feedForward(net,{1,0})\
  1943. print(\"Yay \"..textutils.serialize(output))",
  1944.       [ "Main.lua" ] = "local myNet = AI.Net({2,4,1})\
  1945. local cycle =1\
  1946. while true do\
  1947.  myNet.feedForward({1,0})\
  1948.  myNet.backProp({1})\
  1949.  print(\"1,0\")\
  1950.  print(textutils.serialize(myNet.getResults()))\
  1951.  myNet.feedForward({0,1})\
  1952.  myNet.backProp({1})\
  1953.  print(\"0,1\")\
  1954.  print(textutils.serialize(myNet.getResults()))\
  1955.  myNet.feedForward({0,0})\
  1956.  myNet.backProp({0})\
  1957.  print(\"0,0\")\
  1958.  print(textutils.serialize(myNet.getResults()))\
  1959.  myNet.feedForward({1,1})\
  1960.  myNet.backProp({0})\
  1961.  print(\"1,1\")\
  1962.  print(textutils.serialize(myNet.getResults()))\
  1963.  print(os.clock())\
  1964.  print(cycle)\
  1965.  cycle = cycle + 1\
  1966.  os.queueEvent(\"Lol\")\
  1967.  event = os.pullEvent()\
  1968.  if event == \"key\" then break end\
  1969. end\
  1970. \
  1971. ser = myNet.serialize()\
  1972. \
  1973. myNet = nil\
  1974. \
  1975. myNet = AI.unserialize(ser)\
  1976. \
  1977. while true do\
  1978.  local one = tonumber(read())\
  1979.  local two = tonumber(read())\
  1980.  myNet.feedForward({one,two})\
  1981.  print(textutils.serialize(myNet.getResults()))\
  1982. end",
  1983.       [ "Main.cpp" ] = "// neural-net-tutorial.cpp\
  1984. // David Miller, http://millermattson.com/dave\
  1985. // See the associated video for instructions: http://vimeo.com/19569529\
  1986. \
  1987. \
  1988. #include <vector>\
  1989. #include <iostream>\
  1990. #include <cstdlib>\
  1991. #include <cassert>\
  1992. #include <cmath>\
  1993. #include <fstream>\
  1994. #include <sstream>\
  1995. \
  1996. using namespace std;\
  1997. \
  1998. // Silly class to read training data from a text file -- Replace This.\
  1999. // Replace class TrainingData with whatever you need to get input data into the\
  2000. // program, e.g., connect to a database, or take a stream of data from stdin, or\
  2001. // from a file specified by a command line argument, etc.\
  2002. \
  2003. class TrainingData\
  2004. {\
  2005. public:\
  2006.    TrainingData(const string filename);\
  2007.    bool isEof(void) { return m_trainingDataFile.eof(); }\
  2008.    void getTopology(vector<unsigned> &topology);\
  2009. \
  2010.    // Returns the number of input values read from the file:\
  2011.    unsigned getNextInputs(vector<double> &inputVals);\
  2012.    unsigned getTargetOutputs(vector<double> &targetOutputVals);\
  2013. \
  2014. private:\
  2015.    ifstream m_trainingDataFile;\
  2016. };\
  2017. \
  2018. void TrainingData::getTopology(vector<unsigned> &topology)\
  2019. {\
  2020.    string line;\
  2021.    string label;\
  2022. \
  2023.    getline(m_trainingDataFile, line);\
  2024.    stringstream ss(line);\
  2025.    ss >> label;\
  2026.    if (this->isEof() || label.compare(\"topology:\") != 0) {\
  2027.        abort();\
  2028.    }\
  2029. \
  2030.    while (!ss.eof()) {\
  2031.        unsigned n;\
  2032.        ss >> n;\
  2033.        topology.push_back(n);\
  2034.    }\
  2035. \
  2036.    return;\
  2037. }\
  2038. \
  2039. TrainingData::TrainingData(const string filename)\
  2040. {\
  2041.    m_trainingDataFile.open(filename.c_str());\
  2042. }\
  2043. \
  2044. unsigned TrainingData::getNextInputs(vector<double> &inputVals)\
  2045. {\
  2046.    inputVals.clear();\
  2047. \
  2048.    string line;\
  2049.    getline(m_trainingDataFile, line);\
  2050.    stringstream ss(line);\
  2051. \
  2052.    string label;\
  2053.    ss>> label;\
  2054.    if (label.compare(\"in:\") == 0) {\
  2055.        double oneValue;\
  2056.        while (ss >> oneValue) {\
  2057.            inputVals.push_back(oneValue);\
  2058.        }\
  2059.    }\
  2060. \
  2061.    return inputVals.size();\
  2062. }\
  2063. \
  2064. unsigned TrainingData::getTargetOutputs(vector<double> &targetOutputVals)\
  2065. {\
  2066.    targetOutputVals.clear();\
  2067. \
  2068.    string line;\
  2069.    getline(m_trainingDataFile, line);\
  2070.    stringstream ss(line);\
  2071. \
  2072.    string label;\
  2073.    ss>> label;\
  2074.    if (label.compare(\"out:\") == 0) {\
  2075.        double oneValue;\
  2076.        while (ss >> oneValue) {\
  2077.            targetOutputVals.push_back(oneValue);\
  2078.        }\
  2079.    }\
  2080. \
  2081.    return targetOutputVals.size();\
  2082. }\
  2083. \
  2084. \
  2085. struct Connection\
  2086. {\
  2087.    double weight;\
  2088.    double deltaWeight;\
  2089. };\
  2090. \
  2091. \
  2092. class Neuron;\
  2093. \
  2094. typedef vector<Neuron> Layer;\
  2095. \
  2096. // ****************** class Neuron ******************\
  2097. class Neuron\
  2098. {\
  2099. public:\
  2100.    Neuron(unsigned numOutputs, unsigned myIndex);\
  2101.    void setOutputVal(double val) { m_outputVal = val; }\
  2102.    double getOutputVal(void) const { return m_outputVal; }\
  2103.    void feedForward(const Layer &prevLayer);\
  2104.    void calcOutputGradients(double targetVal);\
  2105.    void calcHiddenGradients(const Layer &nextLayer);\
  2106.    void updateInputWeights(Layer &prevLayer);\
  2107. \
  2108. private:\
  2109.    static double eta;   // [0.0..1.0] overall net training rate\
  2110.    static double alpha; // [0.0..n] multiplier of last weight change (momentum)\
  2111.    static double transferFunction(double x);\
  2112.    static double transferFunctionDerivative(double x);\
  2113.    static double randomWeight(void) { return rand() / double(RAND_MAX); }\
  2114.    double sumDOW(const Layer &nextLayer) const;\
  2115.    double m_outputVal;\
  2116.    vector<Connection> m_outputWeights;\
  2117.    unsigned m_myIndex;\
  2118.    double m_gradient;\
  2119. };\
  2120. \
  2121. double Neuron::eta = 0.15;    // overall net learning rate, [0.0..1.0]\
  2122. double Neuron::alpha = 0.5;   // momentum, multiplier of last deltaWeight, [0.0..1.0]\
  2123. \
  2124. \
  2125. void Neuron::updateInputWeights(Layer &prevLayer)\
  2126. {\
  2127.    // The weights to be updated are in the Connection container\
  2128.    // in the neurons in the preceding layer\
  2129. \
  2130.    for (unsigned n = 0; n < prevLayer.size(); ++n) {\
  2131.        Neuron &neuron = prevLayer[n];\
  2132.        double oldDeltaWeight = neuron.m_outputWeights[m_myIndex].deltaWeight;\
  2133. \
  2134.        double newDeltaWeight =\
  2135.                // Individual input, magnified by the gradient and train rate:\
  2136.                eta\
  2137.                * neuron.getOutputVal()\
  2138.                * m_gradient\
  2139.                // Also add momentum = a fraction of the previous delta weight;\
  2140.                + alpha\
  2141.                * oldDeltaWeight;\
  2142. \
  2143.        neuron.m_outputWeights[m_myIndex].deltaWeight = newDeltaWeight;\
  2144.        neuron.m_outputWeights[m_myIndex].weight += newDeltaWeight;\
  2145.    }\
  2146. }\
  2147. \
  2148. double Neuron::sumDOW(const Layer &nextLayer) const\
  2149. {\
  2150.    double sum = 0.0;\
  2151. \
  2152.    // Sum our contributions of the errors at the nodes we feed.\
  2153. \
  2154.    for (unsigned n = 0; n < nextLayer.size() - 1; ++n) {\
  2155.        sum += m_outputWeights[n].weight * nextLayer[n].m_gradient;\
  2156.    }\
  2157. \
  2158.    return sum;\
  2159. }\
  2160. \
  2161. void Neuron::calcHiddenGradients(const Layer &nextLayer)\
  2162. {\
  2163.    double dow = sumDOW(nextLayer);\
  2164.    m_gradient = dow * Neuron::transferFunctionDerivative(m_outputVal);\
  2165. }\
  2166. \
  2167. void Neuron::calcOutputGradients(double targetVal)\
  2168. {\
  2169.    double delta = targetVal - m_outputVal;\
  2170.    m_gradient = delta * Neuron::transferFunctionDerivative(m_outputVal);\
  2171. }\
  2172. \
  2173. double Neuron::transferFunction(double x)\
  2174. {\
  2175.    // tanh - output range [-1.0..1.0]\
  2176. \
  2177.    return tanh(x);\
  2178. }\
  2179. \
  2180. double Neuron::transferFunctionDerivative(double x)\
  2181. {\
  2182.    // tanh derivative\
  2183.    return 1.0 - x * x;\
  2184. }\
  2185. \
  2186. void Neuron::feedForward(const Layer &prevLayer)\
  2187. {\
  2188.    double sum = 0.0;\
  2189. \
  2190.    // Sum the previous layer's outputs (which are our inputs)\
  2191.    // Include the bias node from the previous layer.\
  2192. \
  2193.    for (unsigned n = 0; n < prevLayer.size(); ++n) {\
  2194.        sum += prevLayer[n].getOutputVal() *\
  2195.                prevLayer[n].m_outputWeights[m_myIndex].weight;\
  2196.    }\
  2197. \
  2198.    m_outputVal = Neuron::transferFunction(sum);\
  2199. }\
  2200. \
  2201. Neuron::Neuron(unsigned numOutputs, unsigned myIndex)\
  2202. {\
  2203.    for (unsigned c = 0; c < numOutputs; ++c) {\
  2204.        m_outputWeights.push_back(Connection());\
  2205.        m_outputWeights.back().weight = randomWeight();\
  2206.    }\
  2207. \
  2208.    m_myIndex = myIndex;\
  2209. }\
  2210. \
  2211. \
  2212. // ****************** class Net ******************\
  2213. class Net\
  2214. {\
  2215. public:\
  2216.    Net(const vector<unsigned> &topology);\
  2217.    void feedForward(const vector<double> &inputVals);\
  2218.    void backProp(const vector<double> &targetVals);\
  2219.    void getResults(vector<double> &resultVals) const;\
  2220.    double getRecentAverageError(void) const { return m_recentAverageError; }\
  2221. \
  2222. private:\
  2223.    vector<Layer> m_layers; // m_layers[layerNum][neuronNum]\
  2224.    double m_error;\
  2225.    double m_recentAverageError;\
  2226.    static double m_recentAverageSmoothingFactor;\
  2227. };\
  2228. \
  2229. \
  2230. double Net::m_recentAverageSmoothingFactor = 100.0; // Number of training samples to average over\
  2231. \
  2232. \
  2233. void Net::getResults(vector<double> &resultVals) const\
  2234. {\
  2235.    resultVals.clear();\
  2236. \
  2237.    for (unsigned n = 0; n < m_layers.back().size() - 1; ++n) {\
  2238.        resultVals.push_back(m_layers.back()[n].getOutputVal());\
  2239.    }\
  2240. }\
  2241. \
  2242. void Net::backProp(const vector<double> &targetVals)\
  2243. {\
  2244.    // Calculate overall net error (RMS of output neuron errors)\
  2245. \
  2246.    Layer &outputLayer = m_layers.back();\
  2247.    m_error = 0.0;\
  2248. \
  2249.    for (unsigned n = 0; n < outputLayer.size() - 1; ++n) {\
  2250.        double delta = targetVals[n] - outputLayer[n].getOutputVal();\
  2251.        m_error += delta * delta;\
  2252.    }\
  2253.    m_error /= outputLayer.size() - 1; // get average error squared\
  2254.    m_error = sqrt(m_error); // RMS\
  2255. \
  2256.    // Implement a recent average measurement\
  2257. \
  2258.    m_recentAverageError =\
  2259.            (m_recentAverageError * m_recentAverageSmoothingFactor + m_error)\
  2260.            / (m_recentAverageSmoothingFactor + 1.0);\
  2261. \
  2262.    // Calculate output layer gradients\
  2263. \
  2264.    for (unsigned n = 0; n < outputLayer.size() - 1; ++n) {\
  2265.        outputLayer[n].calcOutputGradients(targetVals[n]);\
  2266.    }\
  2267. \
  2268.    // Calculate hidden layer gradients\
  2269. \
  2270.    for (unsigned layerNum = m_layers.size() - 2; layerNum > 0; --layerNum) {\
  2271.        Layer &hiddenLayer = m_layers[layerNum];\
  2272.        Layer &nextLayer = m_layers[layerNum + 1];\
  2273. \
  2274.        for (unsigned n = 0; n < hiddenLayer.size(); ++n) {\
  2275.            hiddenLayer[n].calcHiddenGradients(nextLayer);\
  2276.        }\
  2277.    }\
  2278. \
  2279.    // For all layers from outputs to first hidden layer,\
  2280.    // update connection weights\
  2281. \
  2282.    for (unsigned layerNum = m_layers.size() - 1; layerNum > 0; --layerNum) {\
  2283.        Layer &layer = m_layers[layerNum];\
  2284.        Layer &prevLayer = m_layers[layerNum - 1];\
  2285. \
  2286.        for (unsigned n = 0; n < layer.size() - 1; ++n) {\
  2287.            layer[n].updateInputWeights(prevLayer);\
  2288.        }\
  2289.    }\
  2290. }\
  2291. \
  2292. void Net::feedForward(const vector<double> &inputVals)\
  2293. {\
  2294.    assert(inputVals.size() == m_layers[0].size() - 1);\
  2295. \
  2296.    // Assign (latch) the input values into the input neurons\
  2297.    for (unsigned i = 0; i < inputVals.size(); ++i) {\
  2298.        m_layers[0][i].setOutputVal(inputVals[i]);\
  2299.    }\
  2300. \
  2301.    // forward propagate\
  2302.    for (unsigned layerNum = 1; layerNum < m_layers.size(); ++layerNum) {\
  2303.        Layer &prevLayer = m_layers[layerNum - 1];\
  2304.        for (unsigned n = 0; n < m_layers[layerNum].size() - 1; ++n) {\
  2305.            m_layers[layerNum][n].feedForward(prevLayer);\
  2306.        }\
  2307.    }\
  2308. }\
  2309. \
  2310. Net::Net(const vector<unsigned> &topology)\
  2311. {\
  2312.    unsigned numLayers = topology.size();\
  2313.    for (unsigned layerNum = 0; layerNum < numLayers; ++layerNum) {\
  2314.        m_layers.push_back(Layer());\
  2315.        unsigned numOutputs = layerNum == topology.size() - 1 ? 0 : topology[layerNum + 1];\
  2316. \
  2317.        // We have a new layer, now fill it with neurons, and\
  2318.        // add a bias neuron in each layer.\
  2319.        for (unsigned neuronNum = 0; neuronNum <= topology[layerNum]; ++neuronNum) {\
  2320.            m_layers.back().push_back(Neuron(numOutputs, neuronNum));\
  2321.            cout << \"Made a Neuron!\" << endl;\
  2322.        }\
  2323. \
  2324.        // Force the bias node's output to 1.0 (it was the last neuron pushed in this layer):\
  2325.        m_layers.back().back().setOutputVal(1.0);\
  2326.    }\
  2327. }\
  2328. \
  2329. \
  2330. void showVectorVals(string label, vector<double> &v)\
  2331. {\
  2332.    cout << label << \" \";\
  2333.    for (unsigned i = 0; i < v.size(); ++i) {\
  2334.        cout << v[i] << \" \";\
  2335.    }\
  2336. \
  2337.    cout << endl;\
  2338. }\
  2339. \
  2340. \
  2341. int main()\
  2342. {\
  2343.    TrainingData trainData(\"/tmp/trainingData.txt\");\
  2344. \
  2345.    // e.g., { 3, 2, 1 }\
  2346.    vector<unsigned> topology;\
  2347.    trainData.getTopology(topology);\
  2348. \
  2349.    Net myNet(topology);\
  2350. \
  2351.    vector<double> inputVals, targetVals, resultVals;\
  2352.    int trainingPass = 0;\
  2353. \
  2354.    while (!trainData.isEof()) {\
  2355.        ++trainingPass;\
  2356.        cout << endl << \"Pass \" << trainingPass;\
  2357. \
  2358.        // Get new input data and feed it forward:\
  2359.        if (trainData.getNextInputs(inputVals) != topology[0]) {\
  2360.            break;\
  2361.        }\
  2362.        showVectorVals(\": Inputs:\", inputVals);\
  2363.        myNet.feedForward(inputVals);\
  2364. \
  2365.        // Collect the net's actual output results:\
  2366.        myNet.getResults(resultVals);\
  2367.        showVectorVals(\"Outputs:\", resultVals);\
  2368. \
  2369.        // Train the net what the outputs should have been:\
  2370.        trainData.getTargetOutputs(targetVals);\
  2371.        showVectorVals(\"Targets:\", targetVals);\
  2372.        assert(targetVals.size() == topology.back());\
  2373. \
  2374.        myNet.backProp(targetVals);\
  2375. \
  2376.        // Report how well the training is working, average over recent samples:\
  2377.        cout << \"Net recent average error: \"\
  2378.                << myNet.getRecentAverageError() << endl;\
  2379.    }\
  2380. \
  2381.    cout << endl << \"Done\" << endl;\
  2382. }",
  2383.     },
  2384.     Calculator = {
  2385.       [ "Main.lua" ] = "--[[\
  2386. \009Calculator App\
  2387. \009for OmniOS by\
  2388. \009Creator\
  2389. ]]--\
  2390. \
  2391. --Varialbles--\
  2392. local gui = {}\
  2393. local path = OmniOS and \"OmniOS/Programs/Calculator.app/Data/\" or \"Calculator/\"\
  2394. local MainLayout = {}\
  2395. local env = {\
  2396. \009deg = math.deg,\
  2397. \009fmod = math.fmod,\
  2398. \009random = math.random,\
  2399. \009asin = math.asin,\
  2400. \009max = math.max,\
  2401. \009modf = math.modf,\
  2402. \009log10 = math.log10,\
  2403. \009floor = math.floor,\
  2404. \009cosh = math.cosh,\
  2405. \009ldexp = math.ldexp,\
  2406. \009log = math.log,\
  2407. \009pow = math.pow,\
  2408. \009rndsd = math.randomseed,\
  2409. \009frexp = math.frexp,\
  2410. \009abs = math.abs,\
  2411. \009tanh = math.tanh,\
  2412. \009acos = math.acos,\
  2413. \009atan2 = math.atan2,\
  2414. \009tan = math.tan,\
  2415. \009min = math.min,\
  2416. \009ceil = math.ceil,\
  2417. \009sinh = math.sinh,\
  2418. \009sqrt = math.sqrt,\
  2419. \009huge = math.huge,\
  2420. \009rad = math.rad,\
  2421. \009sin = math.sin,\
  2422. \009exp = math.exp,\
  2423. \009cos = math.cos,\
  2424. \009atan = math.atan,\
  2425. \009pi = math.pi,\
  2426. }\
  2427. \
  2428. --Functions--\
  2429. local function readLayoutFile(sLayout)\
  2430. \009local func, err = loadfile(path..\"Layouts/\"..sLayout..\".layout\")\
  2431. \009if err then\
  2432. \009\009log.log(\"Calculator\",err)\
  2433. \009end\
  2434. \009setfenv(func,_G)\
  2435. \009return func()\
  2436. end\
  2437. \
  2438. \
  2439. --Code--\
  2440. if not OmniOS or not Interact then\
  2441. \009shell.run(\"pastebin --- Interact\")\
  2442. \009os.loadAPI(\"Intearct\")\
  2443. end\
  2444. gui = NewInteract:Initialize()\
  2445. MainLayout = gui.Layout.new(readLayoutFile(\"Main\"))\
  2446. local input = gui.Text.new({name = \"input\",text = \"\",xPos = 1,yPos = 2,bgColor = colors.orange,fgColor = colors.white})\
  2447. \
  2448. MainLayout:draw()\
  2449. while true do\
  2450. \009MainLayout.ColorField.Top:draw()\
  2451. \009MainLayout.Button.Exit:draw()\
  2452. \009MainLayout.Button.Clear:draw()\
  2453. \009MainLayout.Button.Delete:draw()\
  2454. \009MainLayout.Button.xxx:draw()\
  2455. \009input:draw()\
  2456. \009local MainLayoutEvent = gui.eventHandler(MainLayout)\
  2457. \009if MainLayoutEvent[2] == \"=\" then\
  2458. \009\009local fOutput = setfenv(loadstring(\"return \"..input.text),env)\
  2459. \009\009local ok, output = pcall(fOutput)\
  2460. \009\009if not ok then \
  2461. \009\009\009log.log(\"Calculator\",output,\"Calculator\")\
  2462. \009\009end\
  2463. \009\009input.text = output\
  2464. \009\009input:draw()\
  2465. \009elseif MainLayoutEvent[2] == \"Exit\" then\
  2466. \009\009break\
  2467. \009elseif MainLayoutEvent[2] == \"Clear\" then\
  2468. \009\009input.text = \"\"\
  2469. \009elseif MainLayoutEvent[2] == \"Delete\" then\
  2470. \009\009input.text = input.text:sub(1,-2)\
  2471. \009else\
  2472. \009\009input.text = input.text..MainLayoutEvent[2]\
  2473. \009\009log.log(\"Calculator\",MainLayoutEvent[2])\
  2474. \009end\
  2475. end",
  2476.       Data = {
  2477.         Layouts = {
  2478.           [ "Main.layout" ] = "local w,h = term.getSize()\
  2479. local tTable = {\
  2480. \009BackgroundColor = colors.gray,\
  2481. \009xPos = 1,\
  2482. \009yPos = 1,\
  2483. \009xLength = w,\
  2484. \009yLength = h,\
  2485. \009Button = {\
  2486. \009\009Exit = {\
  2487. \009\009\009name = \"Exit\",\
  2488. \009\009\009label = \"x\",\
  2489. \009\009\009xPos = w-1,\
  2490. \009\009\009yPos = 2,\
  2491. \009\009\009fgColor = colors.white,\
  2492. \009\009\009bgColor = colors.orange,\
  2493. \009\009\009xLength = 1,\
  2494. \009\009\009yLength = 1,\
  2495. \009\009\009returnValue = \"Exit\",\
  2496. \009\009\009xTextPos = 1,\
  2497. \009\009\009yTextPos = 1,\
  2498. \009\009},\
  2499. \009\009Clear = {\
  2500. \009\009\009name = \"Clear\",\
  2501. \009\009\009label = \"Clear\",\
  2502. \009\009\009xPos = w-7,\
  2503. \009\009\009yPos = 2,\
  2504. \009\009\009fgColor = colors.white,\
  2505. \009\009\009bgColor = colors.orange,\
  2506. \009\009\009xLength = 5,\
  2507. \009\009\009yLength = 1,\
  2508. \009\009\009returnValue = \"Clear\",\
  2509. \009\009\009xTextPos = 1,\
  2510. \009\009\009yTextPos = 1,\
  2511. \009\009},\
  2512. \009\009Delete = {\
  2513. \009\009\009name = \"Delete\",\
  2514. \009\009\009label = \"<--\",\
  2515. \009\009\009xPos = w-7,\
  2516. \009\009\009yPos = 1,\
  2517. \009\009\009fgColor = colors.white,\
  2518. \009\009\009bgColor = colors.orange,\
  2519. \009\009\009xLength = 5,\
  2520. \009\009\009yLength = 1,\
  2521. \009\009\009returnValue = \"Delete\",\
  2522. \009\009\009xTextPos = 1,\
  2523. \009\009\009yTextPos = 1,\
  2524. \009\009},\
  2525. \009\009xxx = {\
  2526. \009\009\009name = \"xxx\",\
  2527. \009\009\009label = \"xxx\",\
  2528. \009\009\009xPos = w-11,\
  2529. \009\009\009yPos = 1,\
  2530. \009\009\009fgColor = colors.white,\
  2531. \009\009\009bgColor = colors.orange,\
  2532. \009\009\009xLength = 3,\
  2533. \009\009\009yLength = 1,\
  2534. \009\009\009returnValue = \"xxx\",\
  2535. \009\009\009xTextPos = 1,\
  2536. \009\009\009yTextPos = 1,\
  2537. \009\009},\
  2538. \009},\
  2539. \009ColorField = {\
  2540. \009\009Top = {\
  2541. \009\009\009name = \"Top\",\
  2542. \009\009\009xPos = 1,\
  2543. \009\009\009yPos = 1,\
  2544. \009\009\009xLength = 51, \
  2545. \009\009\009yLength = 2,\
  2546. \009\009\009color = colors.orange,\
  2547. \009\009},\
  2548. \009},\
  2549. \009Layout  = {\
  2550. \009\009Input = {\
  2551. \009\009\009name = \"Input\",\
  2552. \009\009\009xPos = 1,\
  2553. \009\009\009yPos = 1,\
  2554. \009\009\009xLength = w,\
  2555. \009\009\009yLength = h,\
  2556. \009\009\009BackgroundColor = colors.gray,\
  2557. \009\009\009Button = {\
  2558. \009\009\009\009One = {\
  2559. \009\009\009\009\009name = \"One\",\
  2560. \009\009\009\009\009label = \"1\",\
  2561. \009\009\009\009\009xPos = 35,\
  2562. \009\009\009\009\009yPos = 4,\
  2563. \009\009\009\009\009fgColor = colors.white,\
  2564. \009\009\009\009\009bgColor = colors.orange,\
  2565. \009\009\009\009\009xLength = 3,\
  2566. \009\009\009\009\009yLength = 3,\
  2567. \009\009\009\009\009returnValue = \"1\",\
  2568. \009\009\009\009\009xTextPos = 2,\
  2569. \009\009\009\009\009yTextPos = 2,\
  2570. \009\009\009\009},\
  2571. \009\009\009\009Two = {\
  2572. \009\009\009\009\009name = \"Two\",\
  2573. \009\009\009\009\009label = \"2\",\
  2574. \009\009\009\009\009xPos = 39,\
  2575. \009\009\009\009\009yPos = 4,\
  2576. \009\009\009\009\009fgColor = colors.white,\
  2577. \009\009\009\009\009bgColor = colors.orange,\
  2578. \009\009\009\009\009xLength = 3,\
  2579. \009\009\009\009\009yLength = 3,\
  2580. \009\009\009\009\009returnValue = \"2\",\
  2581. \009\009\009\009\009xTextPos = 2,\
  2582. \009\009\009\009\009yTextPos = 2,\
  2583. \009\009\009\009},\
  2584. \009\009\009\009Three = {\
  2585. \009\009\009\009\009name = \"Three\",\
  2586. \009\009\009\009\009label = \"3\",\
  2587. \009\009\009\009\009xPos = 43,\
  2588. \009\009\009\009\009yPos = 4,\
  2589. \009\009\009\009\009fgColor = colors.white,\
  2590. \009\009\009\009\009bgColor = colors.orange,\
  2591. \009\009\009\009\009xLength = 3,\
  2592. \009\009\009\009\009yLength = 3,\
  2593. \009\009\009\009\009returnValue = \"3\",\
  2594. \009\009\009\009\009xTextPos = 2,\
  2595. \009\009\009\009\009yTextPos = 2,\
  2596. \009\009\009\009},\
  2597. \009\009\009\009Four = {\
  2598. \009\009\009\009\009name = \"Four\",\
  2599. \009\009\009\009\009label = \"4\",\
  2600. \009\009\009\009\009xPos = 35,\
  2601. \009\009\009\009\009yPos = 8,\
  2602. \009\009\009\009\009fgColor = colors.white,\
  2603. \009\009\009\009\009bgColor = colors.orange,\
  2604. \009\009\009\009\009xLength = 3,\
  2605. \009\009\009\009\009yLength = 3,\
  2606. \009\009\009\009\009returnValue = \"4\",\
  2607. \009\009\009\009\009xTextPos = 2,\
  2608. \009\009\009\009\009yTextPos = 2,\
  2609. \009\009\009\009},\
  2610. \009\009\009\009Five = {\
  2611. \009\009\009\009\009name = \"Five\",\
  2612. \009\009\009\009\009label = \"5\",\
  2613. \009\009\009\009\009xPos = 39,\
  2614. \009\009\009\009\009yPos = 8,\
  2615. \009\009\009\009\009fgColor = colors.white,\
  2616. \009\009\009\009\009bgColor = colors.orange,\
  2617. \009\009\009\009\009xLength = 3,\
  2618. \009\009\009\009\009yLength = 3,\
  2619. \009\009\009\009\009returnValue = \"5\",\
  2620. \009\009\009\009\009xTextPos = 2,\
  2621. \009\009\009\009\009yTextPos = 2,\
  2622. \009\009\009\009},\
  2623. \009\009\009\009Six = {\
  2624. \009\009\009\009\009name = \"Six\",\
  2625. \009\009\009\009\009label = \"6\",\
  2626. \009\009\009\009\009xPos = 43,\
  2627. \009\009\009\009\009yPos = 8,\
  2628. \009\009\009\009\009fgColor = colors.white,\
  2629. \009\009\009\009\009bgColor = colors.orange,\
  2630. \009\009\009\009\009xLength = 3,\
  2631. \009\009\009\009\009yLength = 3,\
  2632. \009\009\009\009\009returnValue = \"6\",\
  2633. \009\009\009\009\009xTextPos = 2,\
  2634. \009\009\009\009\009yTextPos = 2,\
  2635. \009\009\009\009},\
  2636. \009\009\009\009Seven = {\
  2637. \009\009\009\009\009name = \"Seven\",\
  2638. \009\009\009\009\009label = \"7\",\
  2639. \009\009\009\009\009xPos = 35,\
  2640. \009\009\009\009\009yPos = 12,\
  2641. \009\009\009\009\009fgColor = colors.white,\
  2642. \009\009\009\009\009bgColor = colors.orange,\
  2643. \009\009\009\009\009xLength = 3,\
  2644. \009\009\009\009\009yLength = 3,\
  2645. \009\009\009\009\009returnValue = \"7\",\
  2646. \009\009\009\009\009xTextPos = 2,\
  2647. \009\009\009\009\009yTextPos = 2,\
  2648. \009\009\009\009},\
  2649. \009\009\009\009Eight = {\
  2650. \009\009\009\009\009name = \"Eight\",\
  2651. \009\009\009\009\009label = \"8\",\
  2652. \009\009\009\009\009xPos = 39,\
  2653. \009\009\009\009\009yPos = 12,\
  2654. \009\009\009\009\009fgColor = colors.white,\
  2655. \009\009\009\009\009bgColor = colors.orange,\
  2656. \009\009\009\009\009xLength = 3,\
  2657. \009\009\009\009\009yLength = 3,\
  2658. \009\009\009\009\009returnValue = \"8\",\
  2659. \009\009\009\009\009xTextPos = 2,\
  2660. \009\009\009\009\009yTextPos = 2,\
  2661. \009\009\009\009},\
  2662. \009\009\009\009Nine = {\
  2663. \009\009\009\009\009name = \"Nine\",\
  2664. \009\009\009\009\009label = \"9\",\
  2665. \009\009\009\009\009xPos = 43,\
  2666. \009\009\009\009\009yPos = 12,\
  2667. \009\009\009\009\009fgColor = colors.white,\
  2668. \009\009\009\009\009bgColor = colors.orange,\
  2669. \009\009\009\009\009xLength = 3,\
  2670. \009\009\009\009\009yLength = 3,\
  2671. \009\009\009\009\009returnValue = \"9\",\
  2672. \009\009\009\009\009xTextPos = 2,\
  2673. \009\009\009\009\009yTextPos = 2,\
  2674. \009\009\009\009},\
  2675. \009\009\009\009Zero = {\
  2676. \009\009\009\009\009name = \"Zero\",\
  2677. \009\009\009\009\009label = \"0\",\
  2678. \009\009\009\009\009xPos = 35,\
  2679. \009\009\009\009\009yPos = 16,\
  2680. \009\009\009\009\009fgColor = colors.white,\
  2681. \009\009\009\009\009bgColor = colors.orange,\
  2682. \009\009\009\009\009xLength = 3,\
  2683. \009\009\009\009\009yLength = 3,\
  2684. \009\009\009\009\009returnValue = \"0\",\
  2685. \009\009\009\009\009xTextPos = 2,\
  2686. \009\009\009\009\009yTextPos = 2,\
  2687. \009\009\009\009},\
  2688. \009\009\009\009Equals = {\
  2689. \009\009\009\009\009name = \"Equals\",\
  2690. \009\009\009\009\009label = \"=\",\
  2691. \009\009\009\009\009xPos = 39,\
  2692. \009\009\009\009\009yPos = 16,\
  2693. \009\009\009\009\009fgColor = colors.white,\
  2694. \009\009\009\009\009bgColor = colors.lightGray,\
  2695. \009\009\009\009\009xLength = 7,\
  2696. \009\009\009\009\009yLength = 3,\
  2697. \009\009\009\009\009returnValue = \"=\",\
  2698. \009\009\009\009\009xTextPos = 4,\
  2699. \009\009\009\009\009yTextPos = 2,\
  2700. \009\009\009\009},\
  2701. \009\009\009\009Add = {\
  2702. \009\009\009\009\009name = \"Add\",\
  2703. \009\009\009\009\009label = \"+\",\
  2704. \009\009\009\009\009xPos = 47,\
  2705. \009\009\009\009\009yPos = 4,\
  2706. \009\009\009\009\009fgColor = colors.white,\
  2707. \009\009\009\009\009bgColor = colors.green,\
  2708. \009\009\009\009\009xLength = 3,\
  2709. \009\009\009\009\009yLength = 3,\
  2710. \009\009\009\009\009returnValue = \"+\",\
  2711. \009\009\009\009\009xTextPos = 2,\
  2712. \009\009\009\009\009yTextPos = 2,\
  2713. \009\009\009\009},\
  2714. \009\009\009\009Sub = {\
  2715. \009\009\009\009\009name = \"Sub\",\
  2716. \009\009\009\009\009label = \"-\",\
  2717. \009\009\009\009\009xPos = 47,\
  2718. \009\009\009\009\009yPos = 8,\
  2719. \009\009\009\009\009fgColor = colors.white,\
  2720. \009\009\009\009\009bgColor = colors.green,\
  2721. \009\009\009\009\009xLength = 3,\
  2722. \009\009\009\009\009yLength = 3,\
  2723. \009\009\009\009\009returnValue = \"-\",\
  2724. \009\009\009\009\009xTextPos = 2,\
  2725. \009\009\009\009\009yTextPos = 2,\
  2726. \009\009\009\009},\
  2727. \009\009\009\009Mul = {\
  2728. \009\009\009\009\009name = \"Mul\",\
  2729. \009\009\009\009\009label = \"*\",\
  2730. \009\009\009\009\009xPos = 47,\
  2731. \009\009\009\009\009yPos = 12,\
  2732. \009\009\009\009\009fgColor = colors.white,\
  2733. \009\009\009\009\009bgColor = colors.green,\
  2734. \009\009\009\009\009xLength = 3,\
  2735. \009\009\009\009\009yLength = 3,\
  2736. \009\009\009\009\009returnValue = \"*\",\
  2737. \009\009\009\009\009xTextPos = 2,\
  2738. \009\009\009\009\009yTextPos = 2,\
  2739. \009\009\009\009},\
  2740. \009\009\009\009Div = {\
  2741. \009\009\009\009\009name = \"Div\",\
  2742. \009\009\009\009\009label = \"/\",\
  2743. \009\009\009\009\009xPos = 47,\
  2744. \009\009\009\009\009yPos = 16,\
  2745. \009\009\009\009\009fgColor = colors.white,\
  2746. \009\009\009\009\009bgColor = colors.green,\
  2747. \009\009\009\009\009xLength = 3,\
  2748. \009\009\009\009\009yLength = 3,\
  2749. \009\009\009\009\009returnValue = \"/\",\
  2750. \009\009\009\009\009xTextPos = 2,\
  2751. \009\009\009\009\009yTextPos = 2,\
  2752. \009\009\009\009},\
  2753. \009\009\009\009LPar = {\
  2754. \009\009\009\009\009name = \"LPar\",\
  2755. \009\009\009\009\009label = \"(\",\
  2756. \009\009\009\009\009xPos = 31,\
  2757. \009\009\009\009\009yPos = 12,\
  2758. \009\009\009\009\009fgColor = colors.white,\
  2759. \009\009\009\009\009bgColor = colors.green,\
  2760. \009\009\009\009\009xLength = 3,\
  2761. \009\009\009\009\009yLength = 3,\
  2762. \009\009\009\009\009returnValue = \"(\",\
  2763. \009\009\009\009\009xTextPos = 2,\
  2764. \009\009\009\009\009yTextPos = 2,\
  2765. \009\009\009\009},\
  2766. \009\009\009\009RPar = {\
  2767. \009\009\009\009\009name = \"RPar\",\
  2768. \009\009\009\009\009label = \")\",\
  2769. \009\009\009\009\009xPos = 31,\
  2770. \009\009\009\009\009yPos = 16,\
  2771. \009\009\009\009\009fgColor = colors.white,\
  2772. \009\009\009\009\009bgColor = colors.green,\
  2773. \009\009\009\009\009xLength = 3,\
  2774. \009\009\009\009\009yLength = 3,\
  2775. \009\009\009\009\009returnValue = \")\",\
  2776. \009\009\009\009\009xTextPos = 2,\
  2777. \009\009\009\009\009yTextPos = 2,\
  2778. \009\009\009\009},\
  2779. \009\009\009\009Mod = {\
  2780. \009\009\009\009\009name = \"Mod\",\
  2781. \009\009\009\009\009label = \"%\",\
  2782. \009\009\009\009\009xPos = 31,\
  2783. \009\009\009\009\009yPos = 8,\
  2784. \009\009\009\009\009fgColor = colors.white,\
  2785. \009\009\009\009\009bgColor = colors.green,\
  2786. \009\009\009\009\009xLength = 3,\
  2787. \009\009\009\009\009yLength = 3,\
  2788. \009\009\009\009\009returnValue = \"%\",\
  2789. \009\009\009\009\009xTextPos = 2,\
  2790. \009\009\009\009\009yTextPos = 2,\
  2791. \009\009\009\009},\
  2792. \009\009\009\009Comma = {\
  2793. \009\009\009\009\009name = \"Comma\",\
  2794. \009\009\009\009\009label = \",\",\
  2795. \009\009\009\009\009xPos = 31,\
  2796. \009\009\009\009\009yPos = 4,\
  2797. \009\009\009\009\009fgColor = colors.white,\
  2798. \009\009\009\009\009bgColor = colors.green,\
  2799. \009\009\009\009\009xLength = 3,\
  2800. \009\009\009\009\009yLength = 3,\
  2801. \009\009\009\009\009returnValue = \",\",\
  2802. \009\009\009\009\009xTextPos = 2,\
  2803. \009\009\009\009\009yTextPos = 2,\
  2804. \009\009\009\009},\
  2805. \009\009\009\009Abs = {\
  2806. \009\009\009\009\009name = \"Abs\",\
  2807. \009\009\009\009\009label = \"abs\",\
  2808. \009\009\009\009\009xPos = 25,\
  2809. \009\009\009\009\009yPos = 4,\
  2810. \009\009\009\009\009fgColor = colors.white,\
  2811. \009\009\009\009\009bgColor = colors.lightGray,\
  2812. \009\009\009\009\009xLength = 5,\
  2813. \009\009\009\009\009yLength = 3,\
  2814. \009\009\009\009\009returnValue = \"abs(\",\
  2815. \009\009\009\009\009xTextPos = 2,\
  2816. \009\009\009\009\009yTextPos = 2,\
  2817. \009\009\009\009},\
  2818. \009\009\009\009Deg = {\
  2819. \009\009\009\009\009name = \"Deg\",\
  2820. \009\009\009\009\009label = \"deg\",\
  2821. \009\009\009\009\009xPos = 25,\
  2822. \009\009\009\009\009yPos = 8,\
  2823. \009\009\009\009\009fgColor = colors.white,\
  2824. \009\009\009\009\009bgColor = colors.lightGray,\
  2825. \009\009\009\009\009xLength = 5,\
  2826. \009\009\009\009\009yLength = 3,\
  2827. \009\009\009\009\009returnValue = \"deg(\",\
  2828. \009\009\009\009\009xTextPos = 2,\
  2829. \009\009\009\009\009yTextPos = 2,\
  2830. \009\009\009\009},\
  2831. \009\009\009\009Max = {\
  2832. \009\009\009\009\009name = \"Max\",\
  2833. \009\009\009\009\009label = \"max\",\
  2834. \009\009\009\009\009xPos = 25,\
  2835. \009\009\009\009\009yPos = 12,\
  2836. \009\009\009\009\009fgColor = colors.white,\
  2837. \009\009\009\009\009bgColor = colors.lightGray,\
  2838. \009\009\009\009\009xLength = 5,\
  2839. \009\009\009\009\009yLength = 3,\
  2840. \009\009\009\009\009returnValue = \"max(\",\
  2841. \009\009\009\009\009xTextPos = 2,\
  2842. \009\009\009\009\009yTextPos = 2,\
  2843. \009\009\009\009},\
  2844. \009\009\009\009Log = {\
  2845. \009\009\009\009\009name = \"Log\",\
  2846. \009\009\009\009\009label = \"log\",\
  2847. \009\009\009\009\009xPos = 25,\
  2848. \009\009\009\009\009yPos = 16,\
  2849. \009\009\009\009\009fgColor = colors.white,\
  2850. \009\009\009\009\009bgColor = colors.lightGray,\
  2851. \009\009\009\009\009xLength = 5,\
  2852. \009\009\009\009\009yLength = 3,\
  2853. \009\009\009\009\009returnValue = \"log(\",\
  2854. \009\009\009\009\009xTextPos = 2,\
  2855. \009\009\009\009\009yTextPos = 2,\
  2856. \009\009\009\009},\
  2857. \009\009\009\009Pow = {\
  2858. \009\009\009\009\009name = \"Pow\",\
  2859. \009\009\009\009\009label = \"pow\",\
  2860. \009\009\009\009\009xPos = 19,\
  2861. \009\009\009\009\009yPos = 4,\
  2862. \009\009\009\009\009fgColor = colors.white,\
  2863. \009\009\009\009\009bgColor = colors.lightGray,\
  2864. \009\009\009\009\009xLength = 5,\
  2865. \009\009\009\009\009yLength = 3,\
  2866. \009\009\009\009\009returnValue = \"pow(\",\
  2867. \009\009\009\009\009xTextPos = 2,\
  2868. \009\009\009\009\009yTextPos = 2,\
  2869. \009\009\009\009},\
  2870. \009\009\009\009Tan = {\
  2871. \009\009\009\009\009name = \"Tan\",\
  2872. \009\009\009\009\009label = \"tan\",\
  2873. \009\009\009\009\009xPos = 19,\
  2874. \009\009\009\009\009yPos = 8,\
  2875. \009\009\009\009\009fgColor = colors.white,\
  2876. \009\009\009\009\009bgColor = colors.lightGray,\
  2877. \009\009\009\009\009xLength = 5,\
  2878. \009\009\009\009\009yLength = 3,\
  2879. \009\009\009\009\009returnValue = \"tan(\",\
  2880. \009\009\009\009\009xTextPos = 2,\
  2881. \009\009\009\009\009yTextPos = 2,\
  2882. \009\009\009\009},\
  2883. \009\009\009\009Min = {\
  2884. \009\009\009\009\009name = \"Min\",\
  2885. \009\009\009\009\009label = \"min\",\
  2886. \009\009\009\009\009xPos = 19,\
  2887. \009\009\009\009\009yPos = 12,\
  2888. \009\009\009\009\009fgColor = colors.white,\
  2889. \009\009\009\009\009bgColor = colors.lightGray,\
  2890. \009\009\009\009\009xLength = 5,\
  2891. \009\009\009\009\009yLength = 3,\
  2892. \009\009\009\009\009returnValue = \"min(\",\
  2893. \009\009\009\009\009xTextPos = 2,\
  2894. \009\009\009\009\009yTextPos = 2,\
  2895. \009\009\009\009},\
  2896. \009\009\009\009Rad = {\
  2897. \009\009\009\009\009name = \"Rad\",\
  2898. \009\009\009\009\009label = \"rad\",\
  2899. \009\009\009\009\009xPos = 19,\
  2900. \009\009\009\009\009yPos = 16,\
  2901. \009\009\009\009\009fgColor = colors.white,\
  2902. \009\009\009\009\009bgColor = colors.lightGray,\
  2903. \009\009\009\009\009xLength = 5,\
  2904. \009\009\009\009\009yLength = 3,\
  2905. \009\009\009\009\009returnValue = \"rad(\",\
  2906. \009\009\009\009\009xTextPos = 2,\
  2907. \009\009\009\009\009yTextPos = 2,\
  2908. \009\009\009\009},\
  2909. \009\009\009\009Sin = {\
  2910. \009\009\009\009\009name = \"Sin\",\
  2911. \009\009\009\009\009label = \"sin\",\
  2912. \009\009\009\009\009xPos = 13,\
  2913. \009\009\009\009\009yPos = 4,\
  2914. \009\009\009\009\009fgColor = colors.white,\
  2915. \009\009\009\009\009bgColor = colors.lightGray,\
  2916. \009\009\009\009\009xLength = 5,\
  2917. \009\009\009\009\009yLength = 3,\
  2918. \009\009\009\009\009returnValue = \"sin(\",\
  2919. \009\009\009\009\009xTextPos = 2,\
  2920. \009\009\009\009\009yTextPos = 2,\
  2921. \009\009\009\009},\
  2922. \009\009\009\009Exp = {\
  2923. \009\009\009\009\009name = \"Exp\",\
  2924. \009\009\009\009\009label = \"exp\",\
  2925. \009\009\009\009\009xPos = 13,\
  2926. \009\009\009\009\009yPos = 8,\
  2927. \009\009\009\009\009fgColor = colors.white,\
  2928. \009\009\009\009\009bgColor = colors.lightGray,\
  2929. \009\009\009\009\009xLength = 5,\
  2930. \009\009\009\009\009yLength = 3,\
  2931. \009\009\009\009\009returnValue = \"exp(\",\
  2932. \009\009\009\009\009xTextPos = 2,\
  2933. \009\009\009\009\009yTextPos = 2,\
  2934. \009\009\009\009},\
  2935. \009\009\009\009Cos = {\
  2936. \009\009\009\009\009name = \"Cos\",\
  2937. \009\009\009\009\009label = \"cos\",\
  2938. \009\009\009\009\009xPos = 13,\
  2939. \009\009\009\009\009yPos = 12,\
  2940. \009\009\009\009\009fgColor = colors.white,\
  2941. \009\009\009\009\009bgColor = colors.lightGray,\
  2942. \009\009\009\009\009xLength = 5,\
  2943. \009\009\009\009\009yLength = 3,\
  2944. \009\009\009\009\009returnValue = \"cos(\",\
  2945. \009\009\009\009\009xTextPos = 2,\
  2946. \009\009\009\009\009yTextPos = 2,\
  2947. \009\009\009\009},\
  2948. \009\009\009\009Pi = {\
  2949. \009\009\009\009\009name = \"Pi\",\
  2950. \009\009\009\009\009label = \"pi\",\
  2951. \009\009\009\009\009xPos = 14,\
  2952. \009\009\009\009\009yPos = 16,\
  2953. \009\009\009\009\009fgColor = colors.white,\
  2954. \009\009\009\009\009bgColor = colors.lightGray,\
  2955. \009\009\009\009\009xLength = 4,\
  2956. \009\009\009\009\009yLength = 3,\
  2957. \009\009\009\009\009returnValue = \"pi\",\
  2958. \009\009\009\009\009xTextPos = 2,\
  2959. \009\009\009\009\009yTextPos = 2,\
  2960. \009\009\009\009},\
  2961. \009\009\009\009Atan = {\
  2962. \009\009\009\009\009name = \"Atan\",\
  2963. \009\009\009\009\009label = \"atan\",\
  2964. \009\009\009\009\009xPos = 7,\
  2965. \009\009\009\009\009yPos = 16,\
  2966. \009\009\009\009\009fgColor = colors.white,\
  2967. \009\009\009\009\009bgColor = colors.lightGray,\
  2968. \009\009\009\009\009xLength = 6,\
  2969. \009\009\009\009\009yLength = 3,\
  2970. \009\009\009\009\009returnValue = \"atan(\",\
  2971. \009\009\009\009\009xTextPos = 2,\
  2972. \009\009\009\009\009yTextPos = 2,\
  2973. \009\009\009\009},\
  2974. \009\009\009},\
  2975. \009\009}\
  2976. \009}\
  2977. }\
  2978. \
  2979. return tTable",
  2980.         },
  2981.       },
  2982.       [ "Calculator.ico" ] = "error('This is an image, not a program!')\
  2983. {\
  2984.  {\
  2985.    {\
  2986.      8192,\
  2987.      \"a\",\
  2988.      1,\
  2989.    },\
  2990.    {\
  2991.      8192,\
  2992.      \"+\",\
  2993.      1,\
  2994.    },\
  2995.    {\
  2996.      8192,\
  2997.      \"-\",\
  2998.      1,\
  2999.    },\
  3000.  },\
  3001.  {\
  3002.    {\
  3003.      8192,\
  3004.      \"l\",\
  3005.      1,\
  3006.    },\
  3007.    {\
  3008.      8192,\
  3009.      \"*\",\
  3010.      1,\
  3011.    },\
  3012.    {\
  3013.      8192,\
  3014.      \"/\",\
  3015.      1,\
  3016.    },\
  3017.  },\
  3018.  {\
  3019.    {\
  3020.      8192,\
  3021.      \"c\",\
  3022.      1,\
  3023.    },\
  3024.    {\
  3025.      8192,\
  3026.      \" \",\
  3027.      1,\
  3028.    },\
  3029.    {\
  3030.      8192,\
  3031.      \" \",\
  3032.      1,\
  3033.    },\
  3034.  },\
  3035.  [ 0 ] = {\
  3036.    {\
  3037.      8192,\
  3038.      \"C\",\
  3039.      1,\
  3040.    },\
  3041.    {\
  3042.      8192,\
  3043.      \" \",\
  3044.      1,\
  3045.    },\
  3046.    {\
  3047.      8192,\
  3048.      \" \",\
  3049.      1,\
  3050.    },\
  3051.  },\
  3052. }",
  3053.     },
  3054.     Compress = {
  3055.       [ "Main.lua" ] = "--[[\
  3056. \009Filesystem\
  3057. \009explorer\
  3058. \009by Creator\
  3059. ]]--\
  3060. local args = {...}\
  3061. \
  3062. local filesystem = {}\
  3063. \
  3064. local function readFile(path)\
  3065. \009local file = fs.open(path,\"r\")\
  3066. \009local variable = file.readAll()\
  3067. \009file.close()\
  3068. \009return variable\
  3069. end\
  3070. \
  3071. local function explore(dir)\
  3072. \009local buffer = {}\
  3073. \009local sBuffer = fs.list(dir)\
  3074. \009for i,v in pairs(sBuffer) do\
  3075. \009\009sleep(0.05)\
  3076. \009\009if fs.isDir(dir..\"/\"..v) then\
  3077. \009\009\009if v ~= \".git\" then\
  3078. \009\009\009\009print(\"Compressing directory: \"..dir..\"/\"..v)\
  3079. \009\009\009\009buffer[v] = explore(dir..\"/\"..v)\
  3080. \009\009\009end\
  3081. \009\009else\
  3082. \009\009\009print(\"Compressing file: \"..dir..\"/\"..v)\
  3083. \009\009\009buffer[v] = readFile(dir..\"/\"..v)\
  3084. \009\009end\
  3085. \009end\
  3086. \009return buffer\
  3087. end\
  3088. \
  3089. append = [[\
  3090. local function writeFile(path,content)\
  3091. \009local file = fs.open(path,\"w\")\
  3092. \009file.write(content)\
  3093. \009file.close()\
  3094. end\
  3095. function writeDown(input,dir)\
  3096. \009\009for i,v in pairs(input) do\
  3097. \009\009if type(v) == \"table\" then\
  3098. \009\009\009writeDown(v,dir..\"/\"..i)\
  3099. \009\009elseif type(v) == \"string\" then\
  3100. \009\009\009writeFile(dir..\"/\"..i,v)\
  3101. \009\009end\
  3102. \009end\
  3103. end\
  3104. \
  3105. args = {...}\
  3106. if #args == 0 then\
  3107. \009print(\"Please input a destination folder.\")\
  3108. else\
  3109. \009writeDown(inputTable,args[1])\
  3110. end\
  3111. \
  3112. ]]\
  3113. \
  3114. local filesystem = explore(args[1])\
  3115. local file = fs.open(args[2],\"w\")\
  3116. file.write(\"inputTable = \"..textutils.serialize(filesystem)..\"\\n\\n\\n\\n\\n\\n\\n\\n\\n\"..append)\
  3117. file.close()",
  3118.     },
  3119.     FileX = {
  3120.       [ "FileX.ico" ] = "error('This is an image, not a program!')\
  3121. {\
  3122.  {\
  3123.    {\
  3124.      16,\
  3125.      \" \",\
  3126.      1,\
  3127.    },\
  3128.    {\
  3129.      16,\
  3130.      \"F\",\
  3131.      32768,\
  3132.    },\
  3133.    {\
  3134.      16,\
  3135.      \" \",\
  3136.      1,\
  3137.    },\
  3138.  },\
  3139.  {\
  3140.    {\
  3141.      2,\
  3142.      \" \",\
  3143.      2,\
  3144.    },\
  3145.    {\
  3146.      16,\
  3147.      \"X\",\
  3148.      32768,\
  3149.    },\
  3150.    {\
  3151.      16,\
  3152.      \" \",\
  3153.      1,\
  3154.    },\
  3155.  },\
  3156.  {\
  3157.    {\
  3158.      2,\
  3159.      \" \",\
  3160.      2,\
  3161.    },\
  3162.    {\
  3163.      16,\
  3164.      \" \",\
  3165.      1,\
  3166.    },\
  3167.    {\
  3168.      16,\
  3169.      \" \",\
  3170.      1,\
  3171.    },\
  3172.  },\
  3173.  {},\
  3174.  [ 0 ] = {\
  3175.    {\
  3176.      16,\
  3177.      \" \",\
  3178.      1,\
  3179.    },\
  3180.    {\
  3181.      16,\
  3182.      \" \",\
  3183.      1,\
  3184.    },\
  3185.    {\
  3186.      16,\
  3187.      \" \",\
  3188.      1,\
  3189.    },\
  3190.  },\
  3191.  [ 14 ] = {},\
  3192.  [ 12 ] = {},\
  3193.  [ 24 ] = {},\
  3194.  [ 26 ] = {},\
  3195.  [ 13 ] = {},\
  3196. }",
  3197.       [ "Main.lua" ] = "--------------FileX v1.2-------------\
  3198. ---------------Program---------------\
  3199. --------------by Creator-------------\
  3200. \
  3201. --Variables--\
  3202. --if OmniOS then OmniOS.setSidebarColor(colors.orange) OmniOS.setSidebarTextColor(colors.black) end\
  3203. \
  3204. term.current(term.native())\
  3205. local copyPath = \"\"\
  3206. local copyHelp = \"\"\
  3207. local textutilsserialize = textutils.serialize\
  3208. local textutilsunserialize = textutils.unserialize\
  3209. local w, h = term.getSize()\
  3210. local folderMenu = {\
  3211. \009term.current(),\
  3212. \00916,\
  3213. \009\"Folder Options\",\
  3214. \009colors.white,\
  3215. \009colors.blue,\
  3216. \009colors.black,\
  3217. \009colors.lightGray,\
  3218. \009{\
  3219. \009\009{\
  3220. \009\009\009\"Open\",\
  3221. \009\009\009\"open\",\
  3222. \009\009},\
  3223. \009\009{\
  3224. \009\009\009\"Delete\",\
  3225. \009\009\009\"delete\",\
  3226. \009\009},\
  3227. \009\009{\
  3228. \009\009\009\"Rename\",\
  3229. \009\009\009\"rename\",\
  3230. \009\009},\
  3231. \009\009{\
  3232. \009\009\009\"Move\",\
  3233. \009\009\009\"move\",\
  3234. \009\009},\
  3235. \009\009{\
  3236. \009\009\009\"Copy\",\
  3237. \009\009\009\"copy\",\
  3238. \009\009},\
  3239. \009\009{\
  3240. \009\009\009\"Cancel\",\
  3241. \009\009\009\"cancel\",\
  3242. \009\009},\
  3243. \009},\
  3244. }\
  3245. local fileMenu = {\
  3246. \009term.current(),\
  3247. \00916,\
  3248. \009\"File Options\",\
  3249. \009colors.white,\
  3250. \009colors.blue,\
  3251. \009colors.black,\
  3252. \009colors.lightGray,\
  3253. \009{\
  3254. \009\009{\
  3255. \009\009\009\"Open\",\
  3256. \009\009\009\"open\",\
  3257. \009\009},\
  3258. \009\009{\
  3259. \009\009\009\"Delete\",\
  3260. \009\009\009\"delete\",\
  3261. \009\009},\
  3262. \009\009{\
  3263. \009\009\009\"Rename\",\
  3264. \009\009\009\"rename\",\
  3265. \009\009},\
  3266. \009\009{\
  3267. \009\009\009\"Move\",\
  3268. \009\009\009\"move\",\
  3269. \009\009},\
  3270. \009\009{\
  3271. \009\009\009\"Cancel\",\
  3272. \009\009\009\"cancel\",\
  3273. \009\009},\
  3274. \009\009{\
  3275. \009\009\009\"Run\",\
  3276. \009\009\009\"run\",\
  3277. \009\009},\
  3278. \009\009{\
  3279. \009\009\009\"Copy\",\
  3280. \009\009\009\"copy\",\
  3281. \009\009},\
  3282. \009\009{\
  3283. \009\009\009\"Open with\",\
  3284. \009\009\009\"openWith\",\
  3285. \009\009},\
  3286. \009},\
  3287. }\
  3288. local nilMenu = {\
  3289. \009term.current(),\
  3290. \00916,\
  3291. \009\"Options\",\
  3292. \009colors.white,\
  3293. \009colors.blue,\
  3294. \009colors.black,\
  3295. \009colors.lightGray,\
  3296. \009{\
  3297. \009\009{\
  3298. \009\009\009\"Make a folder\",\
  3299. \009\009\009\"makeFolder\",\
  3300. \009\009},\
  3301. \009\009{\
  3302. \009\009\009\"Make a file\",\
  3303. \009\009\009\"makeFile\",\
  3304. \009\009},\
  3305. \009\009{\
  3306. \009\009\009\"Cancel\",\
  3307. \009\009\009\"cancel\",\
  3308. \009\009},\
  3309. \009},\
  3310. }\
  3311. local newFolderWindow = {\
  3312. \009term.current(),\
  3313. \009math.floor((w-32)/2),\
  3314. \009math.floor((h-8)/2),\
  3315. \00932,\
  3316. \0098,\
  3317. \009true,\
  3318. \009\"New folder name\",\
  3319. \009colors.white,\
  3320. \009colors.blue,\
  3321. \009\"Write the name of%the new folder.\",\
  3322. \009colors.black,\
  3323. \009colors.lightGray,\
  3324. \009colors.white,\
  3325. \009colors.black,\
  3326. }\
  3327. local newFileWindow = {\
  3328. \009term.current(),\
  3329. \009math.floor((w-32)/2),\
  3330. \009math.floor((h-8)/2),\
  3331. \00932,\
  3332. \0098,\
  3333. \009true,\
  3334. \009\"New file name\",\
  3335. \009colors.white,\
  3336. \009colors.blue,\
  3337. \009\"Write the name of%the new file.\",\
  3338. \009colors.black,\
  3339. \009colors.lightGray,\
  3340. \009colors.white,\
  3341. \009colors.black,\
  3342. }\
  3343. local upFileWindow = {\
  3344. \009term.current(),\
  3345. \009math.floor((w-32)/2),\
  3346. \009math.floor((h-8)/2),\
  3347. \00932,\
  3348. \0098,\
  3349. \009true,\
  3350. \009\"File path\",\
  3351. \009colors.white,\
  3352. \009colors.blue,\
  3353. \009\"Write the path of%the file.\",\
  3354. \009colors.black,\
  3355. \009colors.lightGray,\
  3356. \009colors.white,\
  3357. \009colors.black,\
  3358. }\
  3359. local moveFolderWindow = {\
  3360. \009term.current(),\
  3361. \009math.floor((w-32)/2),\
  3362. \009math.floor((h-8)/2),\
  3363. \00932,\
  3364. \0098,\
  3365. \009true,\
  3366. \009\"New folder path\",\
  3367. \009colors.white,\
  3368. \009colors.blue,\
  3369. \009\"Write the name of%the new folder path.\",\
  3370. \009colors.black,\
  3371. \009colors.lightGray,\
  3372. \009colors.white,\
  3373. \009colors.black,\
  3374. }\
  3375. local bgColor = colors.black\
  3376. local txtColor = colors.white\
  3377. local domain = {}\
  3378. local currDir = \"\"\
  3379. local files = {}\
  3380. local Buttons = {\
  3381. \009{\
  3382. \009\0092,\
  3383. \009\0092,\
  3384. \009\0099,\
  3385. \009\0091,\
  3386. \009\009\" Refresh \",\
  3387. \009\009colors.white,\
  3388. \009\009colors.green,\
  3389. \009\009\"refresh\"\
  3390. \009},\
  3391. \009{\
  3392. \009\00913,\
  3393. \009\0092,\
  3394. \009\0094,\
  3395. \009\0091,\
  3396. \009\009\" Up \",\
  3397. \009\009colors.white,\
  3398. \009\009colors.green,\
  3399. \009\009\"up\"\
  3400. \009},\
  3401. \009{\
  3402. \009\00919,\
  3403. \009\0092,\
  3404. \009\0097,\
  3405. \009\0091,\
  3406. \009\009\" Paste \",\
  3407. \009\009colors.white,\
  3408. \009\009colors.green,\
  3409. \009\009\"paste\"\
  3410. \009},\
  3411. }\
  3412. local folderIcon = {{2,2,2,2,16,16,16,16,},{2,2,2,2,2,2,2,2,},{2,2,2,2,2,2,2,2,},{2,2,2,2,2,2,2,2,},}\
  3413. local fileIcon = {{8192,8192,8192,8192,8192,8192,8192,8192,},{8192,8192,8192,8192,8192,8192,8192,8192,},{8192,8192,8192,8192,8192,8192,8192,8192,},{8192,8192,8192,8192,8192,8192,8192,8192,},}\
  3414. local scroll = 0\
  3415. local globalButtonList = {}\
  3416. local notEnded = true\
  3417. local fileExtensions = {\
  3418. nfp = \"nPaintPro\",\
  3419. nfa = \"nPaintPro\",\
  3420. txt = \"Edit\",\
  3421. exe = \"Shell\",\
  3422. lua = \"Shell\",\
  3423. ico = \"BetterPaint\",\
  3424. }\
  3425. \
  3426. --Functions--\
  3427. \
  3428. local function detectButtonHit(buttonsToTest)\
  3429. \009local event, button, x, y\
  3430. \009repeat\
  3431. \009\009event, button, x, y = os.pullEvent()\
  3432. \009until event == \"mouse_click\" or event == \"key\"\
  3433. \009if event == \"mouse_click\" then\
  3434. \009\009for i,v in pairs(buttonsToTest) do\
  3435. \009\009\009if v[1] <= x and x <= v[3] and v[2] <= y and y <= v[4] then\
  3436. \009\009\009\009return {v[5], button, x, y}\
  3437. \009\009\009end\
  3438. \009\009end\
  3439. \009elseif event == \"key\" then\
  3440. \009\009return {\"key:\"..tostring(button)}\
  3441. \009end\
  3442. \009return {\"pong\"}\
  3443. end\
  3444. \
  3445. local function mkStrShort(str,lenght,n)\
  3446. \009local bufferTable = {}\
  3447. \009local toReturnTable = {}\
  3448. \009local lenghtN = tonumber(lenght)\
  3449. \009if lenghtN == nil then return false end\
  3450. \009for i = 0,n-1 do\
  3451. \009\009bufferTable[i+1] = string.sub(tostring(str),(i*lenghtN)+1,(i*lenghtN)+lenghtN)\
  3452. \009end\
  3453. \009for i,v in pairs(bufferTable) do\
  3454. \009\009if v ~= nil then\
  3455. \009\009\009toReturnTable[#toReturnTable+1] = v\
  3456. \009\009end\
  3457. \009end\
  3458. \009return toReturnTable\
  3459. end\
  3460. \
  3461. local function clear(bgColorArg)\
  3462. \009term.setBackgroundColor(bgColorArg or bgColor)\
  3463. \009term.setTextColor(txtColor)\
  3464. \009term.setCursorPos(1,1)\
  3465. \009term.clear()\
  3466. \009globalButtonList = {}\
  3467. end\
  3468. \
  3469. local function dropDownMenu(tableInput,clearBg,x,y)\
  3470. \009term.setCursorPos(1,1)\
  3471. \009if clearBg ~= false then\
  3472. \009\009clear(colors.cyan)\
  3473. \009end\
  3474. \009globalButtonList = {}\
  3475. \009local nTable = {}\
  3476. \009local dropDownMenuWindow = window.create(tableInput[1],x,y,tableInput[2],#tableInput[8]+1,true)\
  3477. \009dropDownMenuWindow.setTextColor(tableInput[4])\
  3478. \009dropDownMenuWindow.setBackgroundColor(tableInput[5])\
  3479. \009dropDownMenuWindow.setCursorPos(1,1)\
  3480. \009dropDownMenuWindow.clearLine()\
  3481. \009dropDownMenuWindow.write(tableInput[3])\
  3482. \009dropDownMenuWindow.setTextColor(tableInput[6])\
  3483. \009dropDownMenuWindow.setBackgroundColor(tableInput[7])\
  3484. \009for i = 1 , #tableInput[8] do\
  3485. \009\009dropDownMenuWindow.setCursorPos(1,i+1)\
  3486. \009\009dropDownMenuWindow.clearLine()\
  3487. \009\009dropDownMenuWindow.write(tableInput[8][i][1])\
  3488. \009\009globalButtonList[#globalButtonList+1] = {x+1,y+i,x+tableInput[2],y+i,tableInput[8][i][2]}\
  3489. \009end\
  3490. \009local result\
  3491. \009repeat\
  3492. \009\009result =  detectButtonHit(globalButtonList)\
  3493. \009until result[1] ~= \"pong\"\
  3494. \009return result[1]\
  3495. end\
  3496. \
  3497. local function dialogBox(tableInput,clearBg)\
  3498. \009if clearBg ~= false then\
  3499. \009\009clear(colors.cyan)\
  3500. \009end\
  3501. \009local nTable = {}\
  3502. \009dialogBoxWindow = window.create(\
  3503. \009tableInput[1],\
  3504. \009tableInput[2],\
  3505. \009tableInput[3],\
  3506. \009tableInput[4],\
  3507. \009tableInput[5])\
  3508. \009dialogBoxWindow.setBackgroundColor(tableInput[9])\
  3509. \009dialogBoxWindow.setCursorPos(2,1)\
  3510. \009dialogBoxWindow.clearLine()\
  3511. \009dialogBoxWindow.setTextColor(tableInput[8])\
  3512. \009dialogBoxWindow.write(tableInput[7])\
  3513. \009dialogBoxWindow.setCursorPos(1,2)\
  3514. \009dialogBoxWindow.setBackgroundColor(tableInput[12])\
  3515. \009for i = 2 , tableInput[5] do\
  3516. \009\009dialogBoxWindow.setCursorPos(1,i)\
  3517. \009\009dialogBoxWindow.clearLine()\
  3518. \009end\
  3519. \009dialogBoxWindow.setCursorPos(1,2)\
  3520. \009dialogBoxWindow.setTextColor(tableInput[11])\
  3521. \009for token in string.gmatch(tableInput[10],\"[^%%]+\") do\
  3522. \009\009nTable[#nTable+1] = token\
  3523. \009end\
  3524. \009for i,v in pairs(nTable) do\
  3525. \009\009dialogBoxWindow.setCursorPos(2,1+i)\
  3526. \009\009dialogBoxWindow.write(v)\
  3527. \009end\
  3528. \009local totalLenght = 0\
  3529. \009globalButtonList = {}\
  3530. \009for i,v in pairs(tableInput[13]) do\
  3531. \009\009dialogBoxWindow.setCursorPos(2+totalLenght,tableInput[5]-1)\
  3532. \009\009dialogBoxWindow.setTextColor(v[2])\
  3533. \009\009dialogBoxWindow.setBackgroundColor(v[3])\
  3534. \009\009local toWrite = \" \"..v[1]..\" \"\
  3535. \009\009dialogBoxWindow.write(toWrite)\
  3536. \009\009if globalButtonList == nil then\
  3537. \009\009\009globalButtonList = {{tableInput[2]+1+totalLenght,tableInput[3] + tableInput[5]-2,tableInput[2]+totalLenght + #toWrite,tableInput[3] + tableInput[5]-1,v[4]}}\
  3538. \009\009else\
  3539. \009\009\009globalButtonList[#globalButtonList+1] = {tableInput[2]+1+totalLenght,tableInput[3] + tableInput[5]-2,tableInput[2]+totalLenght + #toWrite,tableInput[3] + tableInput[5]-1,v[4]}\
  3540. \009\009end\
  3541. \009\009totalLenght = #toWrite + totalLenght + 2\
  3542. \009end\
  3543. \009local repeatIt = true\
  3544. \009while repeatIt == true do\
  3545. \009\009local unparsedResult = detectButtonHit(globalButtonList)\
  3546. \009\009result = unparsedResult[1]\
  3547. \009\009if result ~= \"pong\" then\
  3548. \009\009\009repeatIt = false\
  3549. \009\009end\
  3550. \009end\
  3551. \009return result\
  3552. end\
  3553. \
  3554. local function textBox(tableInput,secret,clearBg)\
  3555. \009if clearBg ~= false then\
  3556. \009\009clear(colors.cyan)\
  3557. \009end\
  3558. \009local nTable = {}\
  3559. \009textBoxWindow = window.create(tableInput[1],tableInput[2],tableInput[3],tableInput[4],tableInput[5])\
  3560. \009textBoxWindow.setCursorPos(2,1)\
  3561. \009textBoxWindow.setBackgroundColor(tableInput[9])\
  3562. \009textBoxWindow.clearLine()\
  3563. \009textBoxWindow.setTextColor(tableInput[8])\
  3564. \009textBoxWindow.write(tableInput[7])\
  3565. \009textBoxWindow.setCursorPos(1,2)\
  3566. \009textBoxWindow.setBackgroundColor(tableInput[12])\
  3567. \009for i = 2 , tableInput[5] do\
  3568. \009\009textBoxWindow.setCursorPos(1,i)\
  3569. \009\009textBoxWindow.clearLine()\
  3570. \009end\
  3571. \009textBoxWindow.setTextColor(tableInput[11])\
  3572. \009for token in string.gmatch(tableInput[10],\"[^%%]+\") do\
  3573. \009\009nTable[#nTable+1] = token\
  3574. \009end\
  3575. \009for i,v in pairs(nTable) do\
  3576. \009\009textBoxWindow.setCursorPos(2,1+i)\
  3577. \009\009textBoxWindow.write(v)\
  3578. \009end\
  3579. \009textBoxWindow.setTextColor(tableInput[13])\
  3580. \009textBoxWindow.setBackgroundColor(tableInput[14])\
  3581. \009textBoxWindow.setCursorPos(2,tableInput[5]-1)\
  3582. \009textBoxWindow.clearLine()\
  3583. \009textBoxWindow.setCursorPos(2,tableInput[5]-1)\
  3584. \009textBoxWindow.setTextColor(tableInput[13])\
  3585. \009textBoxWindow.setBackgroundColor(tableInput[14])\
  3586. \009if secret then\
  3587. \009\009return read(\"*\")\
  3588. \009else\
  3589. \009\009return read()\
  3590. \009end\
  3591. end\
  3592. \
  3593. local function refresh(dir)\
  3594. \009local bufferFiles = {}\
  3595. \009for i,v in pairs(fs.list(dir)) do\
  3596. \009\009if fs.isDir(currDir..\"/\"..v) then\
  3597. \009\009\009bufferFiles[#bufferFiles+1] = {v,\"folder\"}\
  3598. \009\009else\
  3599. \009\009\009bufferFiles[#bufferFiles+1] = {v,\"file\"}\
  3600. \009\009end\
  3601. \009end\
  3602. \009return bufferFiles\
  3603. end\
  3604. \
  3605. local function drawOptions(tableInputDrawOptions)\
  3606. \009for i,v in pairs(tableInputDrawOptions) do\
  3607. \009\009term.setCursorPos(v[1],v[2])\
  3608. \009\009paintutils.drawFilledBox(v[1],v[2],v[3]+v[1]-1,v[4]+v[2]-1,v[7])\
  3609. \009\009term.setCursorPos(v[1],v[2])\
  3610. \009\009term.setTextColor(v[6])\
  3611. \009\009term.write(v[5])\
  3612. \009\009if globalButtonList == nil then\
  3613. \009\009\009globalButtonList = {{v[1],v[2],v[3]+v[1]-1,v[4]+v[2]-1,\"button:\"..v[8]}}\
  3614. \009\009else\
  3615. \009\009\009globalButtonList[#globalButtonList+1] = {v[1],v[2],v[3]+v[1]-1,v[4]+v[2]-1,\"button:\"..v[8]}\
  3616. \009\009end\
  3617. \009end\
  3618. end\
  3619. \
  3620. local function drawFiles(filesToDisplay)\
  3621. \009local numItemsX = math.floor((w-1)/10)\
  3622. \009numItemsY = math.ceil(#filesToDisplay/numItemsX)\
  3623. \009local currFile = 1\
  3624. \009for i = 0 , numItemsY-1 do\
  3625. \009\009for k = 0 , numItemsX - 1 do\
  3626. \009\009\009if currFile > #filesToDisplay then\
  3627. \009\009\009\009break\
  3628. \009\009\009else\
  3629. \009\009\009\009term.setTextColor(colors.black)\
  3630. \009\009\009\009if filesToDisplay[currFile][2] == \"file\" then\
  3631. \009\009\009\009\009paintutils.drawImage(fileIcon,(k*10)+2,(i*5)+4-scroll)\
  3632. \009\009\009\009\009for l,m in pairs(mkStrShort(filesToDisplay[currFile][1],6,3)) do\
  3633. \009\009\009\009\009\009term.setCursorPos((k*10)+3,(i*5)+4+l-scroll)\
  3634. \009\009\009\009\009\009term.write(m)\
  3635. \009\009\009\009\009end\
  3636. \009\009\009\009\009if ((i*5)+4-scroll) < 4 then\
  3637. \009\009\009\009\009\009if ((i*5)+7-scroll) >= 4 then\
  3638. \009\009\009\009\009\009\009if globalButtonList == nil then\
  3639. \009\009\009\009\009\009\009\009globalButtonList = {{((k*10)+2),4,((k*10)+9),((i*5)+7-scroll),\"file:\"..filesToDisplay[currFile][1]}}\
  3640. \009\009\009\009\009\009\009else\
  3641. \009\009\009\009\009\009\009\009globalButtonList[#globalButtonList+1] = {((k*10)+2),4,((k*10)+9),((i*5)+7-scroll),\"file:\"..filesToDisplay[currFile][1]}\
  3642. \009\009\009\009\009\009\009end\
  3643. \009\009\009\009\009\009end\
  3644. \009\009\009\009\009else\
  3645. \009\009\009\009\009\009if globalButtonList == nil then\
  3646. \009\009\009\009\009\009\009globalButtonList = {{((k*10)+2),((i*5)+4-scroll),((k*10)+9),((i*5)+7-scroll),\"file:\"..filesToDisplay[currFile][1]}}\
  3647. \009\009\009\009\009\009else\
  3648. \009\009\009\009\009\009\009globalButtonList[#globalButtonList+1] = {((k*10)+2),((i*5)+4-scroll),((k*10)+9),((i*5)+7-scroll),\"file:\"..filesToDisplay[currFile][1]}\
  3649. \009\009\009\009\009\009end\
  3650. \009\009\009\009\009end\
  3651. \009\009\009\009elseif filesToDisplay[currFile][2] == \"folder\" then\
  3652. \009\009\009\009\009paintutils.drawImage(folderIcon,(k*10)+2,(i*5)+4-scroll)\
  3653. \009\009\009\009\009for l,m in pairs(mkStrShort(filesToDisplay[currFile][1],6,3)) do\
  3654. \009\009\009\009\009\009term.setCursorPos((k*10)+3,(i*5)+4+l-scroll)\
  3655. \009\009\009\009\009\009term.write(m)\
  3656. \009\009\009\009\009end\
  3657. \009\009\009\009\009if ((i*5)+4-scroll) < 4 then\
  3658. \009\009\009\009\009\009if ((i*5)+7-scroll) >= 4 then\
  3659. \009\009\009\009\009\009\009if globalButtonList == nil then\
  3660. \009\009\009\009\009\009\009\009globalButtonList = {{((k*10)+2),4,((k*10)+9),((i*5)+7-scroll),\"folder:\"..filesToDisplay[currFile][1]}}\
  3661. \009\009\009\009\009\009\009else\
  3662. \009\009\009\009\009\009\009\009globalButtonList[#globalButtonList+1] = {((k*10)+2),4,((k*10)+9),((i*5)+7-scroll),\"folder:\"..filesToDisplay[currFile][1]}\
  3663. \009\009\009\009\009\009\009end\
  3664. \009\009\009\009\009\009end\
  3665. \009\009\009\009\009else\
  3666. \009\009\009\009\009\009if globalButtonList == nil then\
  3667. \009\009\009\009\009\009\009globalButtonList = {{((k*10)+2),((i*5)+4-scroll),((k*10)+9),((i*5)+7-scroll),\"folder:\"..filesToDisplay[currFile][1]}}\
  3668. \009\009\009\009\009\009else\
  3669. \009\009\009\009\009\009\009globalButtonList[#globalButtonList+1] = {((k*10)+2),((i*5)+4-scroll),((k*10)+9),((i*5)+7-scroll),\"folder:\"..filesToDisplay[currFile][1]}\
  3670. \009\009\009\009\009\009end\
  3671. \009\009\009\009\009end\
  3672. \009\009\009\009end\
  3673. \009\009\009\009currFile = currFile + 1\
  3674. \009\009\009end\
  3675. \009\009end\
  3676. \009end\
  3677. end\
  3678. \
  3679. local function drawSideBar()\
  3680. \009local lenghtSideBar = h-4\
  3681. \009if numItemsY ~= 0 then\
  3682. \009\009 lenghtSideBar = math.ceil(((h-5)*(h-5))/(numItemsY*5))\
  3683. \009end\
  3684. \009paintutils.drawLine(w,3,w,3+lenghtSideBar,colors.green)\
  3685. \009term.setBackgroundColor(colors.blue)\
  3686. \009term.setCursorPos(w,3)\
  3687. \009term.write(\"^\")\
  3688. \009term.setCursorPos(w,h-1)\
  3689. \009term.write(\"v\")\
  3690. \009if globalButtonList == nil then\
  3691. \009\009globalButtonList = {{w,3,w,3,\"button:scrollUp\"}}\
  3692. \009else\
  3693. \009\009globalButtonList[#globalButtonList+1] = {w,3,w,3,\"button:scrollUp\"}\
  3694. \009end\
  3695. \009if globalButtonList == nil then\
  3696. \009\009globalButtonList = {{w,h-1,w,h-1,\"button:scrollDown\"}}\
  3697. \009else\
  3698. \009\009globalButtonList[#globalButtonList+1] = {w,h-1,w,h-1,\"button:scrollDown\"}\
  3699. \009end\
  3700. end\
  3701. \
  3702. local function program(extension)\
  3703. \009if fileExtensions[extension] ~= nil then\
  3704. \009\009return fileExtensions[extension]\
  3705. \009else\
  3706. \009\009return \"edit\"\
  3707. \009end\
  3708. end\
  3709. \
  3710. local function main(filesToDisplay,buttonsToDisplay)\
  3711. \009clear(colors.white)\
  3712. \009drawFiles(filesToDisplay)\
  3713. \009drawSideBar()\
  3714. \009term.setBackgroundColor(colors.orange)\
  3715. \009for i = 1,2 do\
  3716. \009\009term.setCursorPos(1,i)\
  3717. \009\009term.clearLine()\
  3718. \009end\
  3719. \009term.setCursorPos(1,1)\
  3720. \009term.setTextColor(colors.black)\
  3721. \009term.write(\"Creator\\'s FileX v1.0\")\
  3722. \009term.setCursorPos(w,1)\
  3723. \009term.setBackgroundColor(colors.magenta)\
  3724. \009term.write(\"X\")\
  3725. \009if globalButtonList == nil then\
  3726. \009\009globalButtonList = {{w,1,w,1,\"button:x\"}}\
  3727. \009else\
  3728. \009\009globalButtonList[#globalButtonList+1] = {w,1,w,1,\"button:x\"}\
  3729. \009end\
  3730. \009if globalButtonList == nil then\
  3731. \009\009globalButtonList = {{1,3,w,h,\"nil:nil\"}}\
  3732. \009else\
  3733. \009\009globalButtonList[#globalButtonList+1] = {1,4,w,h,\"nil:nil\"}\
  3734. \009end\
  3735. \009drawOptions(buttonsToDisplay)\
  3736. \009if globalButtonList == nil then\
  3737. \009\009globalButtonList = {{1,1,w,3,\"nil:nilnil\"}}\
  3738. \009else\
  3739. \009\009globalButtonList[#globalButtonList+1] = {1,1,w,3,\"nil:nilnil\"}\
  3740. \009end\
  3741. \009term.setCursorPos(1,h)\
  3742. \009term.setBackgroundColor(colors.orange)\
  3743. \009term.clearLine()\
  3744. \009term.setTextColor(colors.black)\
  3745. \009term.write(currDir)\
  3746. \009term.setCursorPos(1,1)\
  3747. \009local detectedButtonUnparsedTable = detectButtonHit(globalButtonList)\
  3748. \009local detectedButtonUnparsed = detectedButtonUnparsedTable[1]\
  3749. \009local button = detectedButtonUnparsedTable[2]\
  3750. \009local detectedButtonParsedTable = {}\
  3751. \009for token in string.gmatch(detectedButtonUnparsed,\"[^:]+\") do\
  3752. \009\009detectedButtonParsedTable[#detectedButtonParsedTable + 1] = token\
  3753. \009end\
  3754. \009local action = detectedButtonParsedTable[2]\
  3755. \009if detectedButtonParsedTable[1] == \"button\" then\
  3756. \009\009if action == \"x\" then\
  3757. \009\009\009term.setBackgroundColor(colors.black)\
  3758. \009\009\009clear()\
  3759. \009\009\009print(\"Thank you for using Creator\\'s FTPclient. More coming soon...\\nSpecial thanks to:\\nNitrogenFingers for nPaintPro\")\
  3760. \009\009\009notEnded = false\
  3761. \009\009elseif action == \"up\" then\
  3762. \009\009\009scroll = 0\
  3763. \009\009\009if currDir == \"/\" then\
  3764. \009\009\009else\
  3765. \009\009\009\009local currDirBuffer = {}\
  3766. \009\009\009\009for token in string.gmatch(currDir,\"(/[^/]+)\") do\
  3767. \009\009\009\009\009currDirBuffer[#currDirBuffer + 1] = token\
  3768. \009\009\009\009end\
  3769. \009\009\009\009currDir = \"\"\
  3770. \009\009\009\009if #currDirBuffer == 1 then\
  3771. \009\009\009\009\009currDir = \"\"\
  3772. \009\009\009\009else\
  3773. \009\009\009\009\009for i = 1, #currDirBuffer-1 do\
  3774. \009\009\009\009\009\009if i == 1 then\
  3775. \009\009\009\009\009\009\009currDir = currDirBuffer[1]\
  3776. \009\009\009\009\009\009else\
  3777. \009\009\009\009\009\009\009currDir = currDir..currDirBuffer[i]\
  3778. \009\009\009\009\009\009end\
  3779. \009\009\009\009\009end\
  3780. \009\009\009\009end\
  3781. \009\009\009\009files = refresh(currDir)\
  3782. \009\009\009end\
  3783. \009\009elseif action == \"refresh\" then\
  3784. \009\009\009files = refresh(currDir)\
  3785. \009\009elseif action == \"scrollUp\" then\
  3786. \009\009\009scroll = scroll - 1\
  3787. \009\009\009if scroll < 0 then\
  3788. \009\009\009\009scroll = 0\
  3789. \009\009\009end\
  3790. \009\009elseif action == \"scrollDown\" then\
  3791. \009\009\009scroll = scroll + 1\
  3792. \009\009\009if scroll > numItemsY*6 - h then\
  3793. \009\009\009\009scroll = scroll - 1\
  3794. \009\009\009end\
  3795. \009\009elseif action == \"paste\" then\
  3796. \009\009\009term.setCursorPos(1,1)\
  3797. \009\009\009sleep(3)\
  3798. \009\009\009fs.copy(copyPath,currDir..\"/\"..copyHelp)\
  3799. \009\009\009files = refresh(currDir)\
  3800. \
  3801. \009\009end\
  3802. \009elseif detectedButtonParsedTable[1] == \"key\" then\
  3803. \009\009if tonumber(action) == keys.up then\
  3804. \009\009\009scroll = scroll - 1\
  3805. \009\009\009if scroll < 0 then\
  3806. \009\009\009\009scroll = 0\
  3807. \009\009\009end\
  3808. \009\009elseif tonumber(action) == keys.down then\
  3809. \009\009\009scroll = scroll + 1\
  3810. \009\009\009if scroll > numItemsY*6 - h then\
  3811. \009\009\009\009scroll = scroll - 1\
  3812. \009\009\009end\
  3813. \009\009end\
  3814. \009elseif detectedButtonParsedTable[1] == \"folder\" then\
  3815. \009\009if button == 1 then\
  3816. \009\009\009currDir = currDir..\"/\"..action\
  3817. \009\009\009files = refresh(currDir)\
  3818. \009\009\009scroll = 0\
  3819. \009\009elseif button == 2 then\
  3820. \009\009\009local result = dropDownMenu(folderMenu,false,1,1)\
  3821. \009\009\009if result == \"open\" then\
  3822. \009\009\009\009currDir = currDir..\"/\"..action\
  3823. \009\009\009\009files = refresh(currDir)\
  3824. \009\009\009\009scroll = 0\
  3825. \009\009\009elseif result == \"copy\" then\
  3826. \009\009\009\009pathToCopy = currDir..action\
  3827. \009\009\009elseif result == \"delete\" then\
  3828. \009\009\009\009fs.delete(currDir..\"/\"..action)\
  3829. \009\009\009\009files = refresh(currDir)\
  3830. \009\009\009elseif result == \"rename\" then\
  3831. \009\009\009\009local answ = textBox(newFolderWindow,false,false)\
  3832. \009\009\009\009fs.move(currDir..\"/\"..action,currDir..\"/\"..answ)\
  3833. \009\009\009\009files = refresh(currDir)\
  3834. \009\009\009elseif result == \"move\" then\
  3835. \009\009\009\009local answ = textBox(moveFolderWindow,false,false)\
  3836. \009\009\009\009if string.sub(answ,1,1) ~= \"/\" then\
  3837. \009\009\009\009\009answ = \"/\"..answ\
  3838. \009\009\009\009end\
  3839. \009\009\009\009fs.move(currDir..\"/\"..action,answ)\
  3840. \009\009\009\009files = refresh(currDir)\
  3841. \009\009\009elseif result == \"copy\" then\
  3842. \009\009\009\009copyPath = currDir..\"/\"..action\
  3843. \009\009\009\009copyHelp = action\
  3844. \009\009\009end\
  3845. \009\009end\
  3846. \009elseif detectedButtonParsedTable[1] == \"file\" then\
  3847. \009\009if button == 1 then\
  3848. \009\009\009local fileExtension\
  3849. \009\009\009for token in string.gmatch(action,\"[^%.]+\") do\
  3850. \009\009\009\009fileExtension = token\
  3851. \009\009\009end\
  3852. \009\009\009if fileExtension == action then\
  3853. \009\009\009\009fileExtension = \"txt\"\
  3854. \009\009\009end\
  3855. \009\009\009programT = program(fileExtension)\
  3856. \009\009\009--shell.run(progsDir..\"/\"..programT..\" \"..currDir..\"/\"..action)\
  3857. \009\009\009OmniOS.execute(currDir..\"/\"..action)\
  3858. \009\009elseif button == 2 then\
  3859. \009\009\009local result = dropDownMenu(fileMenu,false,1,1)\
  3860. \009\009\009if result == \"open\" then\
  3861. \009\009\009\009local fileExtension\
  3862. \009\009\009\009for token in string.gmatch(action,\"[^%.]+\") do\
  3863. \009\009\009\009\009fileExtension = token\
  3864. \009\009\009\009end\
  3865. \009\009\009\009if fileExtension == action then\
  3866. \009\009\009\009\009fileExtension = \"txt\"\
  3867. \009\009\009\009end\
  3868. \009\009\009\009programT = program(fileExtension)\
  3869. \009\009\009\009OmniOS.launch(programT,\"term\",currDir..\"/\"..action)\
  3870. \009\009\009elseif result == \"delete\" then\
  3871. \009\009\009\009fs.delete(currDir..\"/\"..action)\
  3872. \009\009\009\009files = refresh(currDir)\
  3873. \009\009\009elseif result == \"rename\" then\
  3874. \009\009\009\009local answ = textBox(newFolderWindow,false,false)\
  3875. \009\009\009\009fs.move(currDir..\"/\"..action,currDir..\"/\"..answ)\
  3876. \009\009\009\009files = refresh(currDir)\
  3877. \009\009\009elseif result == \"move\" then\
  3878. \009\009\009\009local answ = textBox(moveFolderWindow,false,false)\
  3879. \009\009\009\009if string.sub(answ,1,1) ~= \"/\" then\
  3880. \009\009\009\009\009answ = \"/\"..answ\
  3881. \009\009\009\009end\
  3882. \009\009\009\009fs.move(currDir..\"/\"..action,answ)\
  3883. \009\009\009\009files = refresh(currDir)\
  3884. \009\009\009elseif result == \"run\" then\
  3885. \009\009\009\009shell.run(progsDir..\"/shell \"..currDir..\"/\"..action)\
  3886. \009\009\009elseif result == \"openWith\" then\
  3887. \009\009\009\009local possibleExtensions = {}\
  3888. \009\009\009\009for i,v in pairs(fileExtensions) do\
  3889. \009\009\009\009\009local alreadyRegistered = false\
  3890. \009\009\009\009\009for m,k in pairs(possibleExtensions) do\
  3891. \009\009\009\009\009\009if v == k then\
  3892. \009\009\009\009\009\009\009alreadyRegistered = true\
  3893. \009\009\009\009\009\009end\
  3894. \009\009\009\009\009end\
  3895. \009\009\009\009\009if not alreadyRegistered then\
  3896. \009\009\009\009\009\009possibleExtensions[#possibleExtensions+1] = v\
  3897. \009\009\009\009\009end\
  3898. \009\009\009\009end\
  3899. \009\009\009\009local openWith = {\
  3900. \009\009\009\009\009term.current(),\
  3901. \009\009\009\009\00916,\
  3902. \009\009\009\009\009\"Open with:\",\
  3903. \009\009\009\009\009colors.white,\
  3904. \009\009\009\009\009colors.blue,\
  3905. \009\009\009\009\009colors.black,\
  3906. \009\009\009\009\009colors.lightGray,\
  3907. \009\009\009\009\009{}\
  3908. \009\009\009\009}\
  3909. \009\009\009\009for i,v in pairs(possibleExtensions) do\
  3910. \009\009\009\009\009openWith[8][#openWith[8]+1] = {v,v}\
  3911. \009\009\009\009end\
  3912. \009\009\009\009openWith[8][#openWith[8]+1] = {\"Cancel\",\"cancel\"}\
  3913. \009\009\009\009globalButtonList = {}\
  3914. \009\009\009\009result = dropDownMenu(openWith,false,1,7)\
  3915. \009\009\009\009OmniOS.launch(result,\"term\",currDir..\"/\"..action)\
  3916. \009\009\009elseif result == \"copy\" then\
  3917. \009\009\009\009copyPath = currDir..\"/\"..action\
  3918. \009\009\009\009copyHelp = action\
  3919. \009\009\009end\
  3920. \009\009end\
  3921. \009elseif detectedButtonParsedTable[1] == \"nil\" then\
  3922. \009\009if button == 2 then\
  3923. \009\009\009if action == \"nil\" then\
  3924. \009\009\009\009local result = dropDownMenu(nilMenu,false,1,1)\
  3925. \009\009\009\009if result == \"makeFolder\" then\
  3926. \009\009\009\009\009local answ = textBox(newFolderWindow,false,false)\
  3927. \009\009\009\009\009fs.makeDir(currDir..\"/\"..answ)\
  3928. \009\009\009\009\009files = refresh(currDir)\
  3929. \009\009\009\009\009scroll = 0\
  3930. \009\009\009\009elseif result == \"makeFile\" then\
  3931. \009\009\009\009\009local answ = textBox(newFileWindow,false,false)\
  3932. \009\009\009\009\009f = fs.open(currDir..\"/\"..answ,\"w\")\
  3933. \009\009\009\009\009f.close()\
  3934. \009\009\009\009\009files = refresh(currDir)\
  3935. \009\009\009\009\009scroll = 0\
  3936. \009\009\009\009end\
  3937. \009\009\009end\
  3938. \009\009end\
  3939. \009end\
  3940. end\
  3941. \
  3942. --Code--\
  3943. \
  3944. clear()\
  3945. files = refresh(currDir)\
  3946. while notEnded do\
  3947. \009main(files,Buttons)\
  3948. end\
  3949. shell.setDir(currDir)",
  3950.     },
  3951.     Desktop = {
  3952.       [ "Main.lua" ] = "--[[\
  3953. \009Desktop in ComputerCraft\
  3954. \009by Creator\
  3955. \009for OmniOS\
  3956. ]]--\
  3957. --Variables--\
  3958. local textutilsserialize = textutils.serialize\
  3959. local textutilsunserialize = textutils.unserialize\
  3960. local w,h = term.getSize()\
  3961. local Settings = {}\
  3962. local result = {}\
  3963. local Main = {}\
  3964. local QuickSettings = {}\
  3965. local MatchingColors = {}\
  3966. local gui = {}\
  3967. local mainLayout = {}\
  3968. local quickSettingsLayout = {}\
  3969. local mainLayoutTable = {}\
  3970. local quickSettingsLayoutTable = {}\
  3971. local paths = {}\
  3972. local scroll = 0\
  3973. local programPath = \"Programs/Desktop/Data/\"\
  3974. \
  3975. --Functions--\
  3976. local function readFile(_path)\
  3977. \009local file = fs.open(_path,\"r\")\
  3978. \009local data = file.readAll()\
  3979. \009file.close()\
  3980. \009return data\
  3981. end\
  3982. \
  3983. local function split(str,sep)\
  3984. \009local buffer = {}\
  3985. \009for token in str:gmatch(sep) do\
  3986. \009\009buffer[#buffer+1] = token\
  3987. \009end\
  3988. \009return buffer\
  3989. end\
  3990. \
  3991. local function loadShortcuts()\
  3992. \009local buffer = readFile(programPath..\"Shortcuts\")\
  3993. \009local sBuffer = textutilsunserialize(buffer)\
  3994. \009local nBuffer = {}\
  3995. \009paths = {}\
  3996. \009for i,v in pairs(sBuffer) do\
  3997. \009\009nBuffer[v.name] = {\
  3998. \009\009\009xPos = 2+(6*(v.xPos-1)),\
  3999. \009\009\009yPos = 1+(5*(v.yPos-1)),\
  4000. \009\009\009name = v.name,\
  4001. \009\009\009path = v.path..\".ico\",\
  4002. \009\009\009yLength = 4,\
  4003. \009\009\009xLength = 3,\
  4004. \009\009\009returnValue = v.name,\
  4005. \009\009\009label = string.sub(v.name,1,5),\
  4006. \009\009\009labelFg = MatchingColors[Settings.bgColor][\"mainButtons\"],\
  4007. \009\009\009labelBg = Settings.bgColor,\
  4008. \009\009\009moveY = true,\
  4009. \009\009}\
  4010. \009\009paths[v.name] = v.path\
  4011. \009end\
  4012. \009Main.BetterPaint = nBuffer\
  4013. end\
  4014. \
  4015. local function loadObjects()\
  4016. --Object table--\
  4017. \009Main.Button = {\
  4018. \009\009quickSettings = {\
  4019. \009\009\009name = \"quickSettings\",\
  4020. \009\009\009label = \"^\",\
  4021. \009\009\009xPos = 1,\
  4022. \009\009\009yPos = h,\
  4023. \009\009\009xLength = 1,\
  4024. \009\009\009yLength = 1,\
  4025. \009\009\009xTextPos = 1,\
  4026. \009\009\009yTextPos = 1,\
  4027. \009\009\009fgColor = MatchingColors[Settings.bgColor][\"mainButtons\"],\
  4028. \009\009\009bgColor = Settings.bgColor,\
  4029. \009\009\009returnValue = \"quickSettings\",\
  4030. \009\009}\
  4031. \009}\
  4032. \
  4033. \009QuickSettings.Button = {\
  4034. \009\009Right = {\
  4035. \009\009\009name = \"Right\",\
  4036. \009\009\009label = \">\",\
  4037. \009\009\009xPos = w-1,\
  4038. \009\009\009yPos = 5,\
  4039. \009\009\009xLength = 1,\
  4040. \009\009\009yLength = 1,\
  4041. \009\009\009xTextPos = 1,\
  4042. \009\009\009yTextPos = 1,\
  4043. \009\009\009fgColor = MatchingColors[MatchingColors[Settings.bgColor][\"quickSettings\"]][\"mainButtons\"],\
  4044. \009\009\009bgColor = MatchingColors[Settings.bgColor][\"quickSettings\"],\
  4045. \009\009\009returnValue = \"Right\",\
  4046. \009\009},\
  4047. \009\009Left = {\
  4048. \009\009\009name = \"Left\",\
  4049. \009\009\009label = \"<\",\
  4050. \009\009\009xPos = 2,\
  4051. \009\009\009yPos = 5,\
  4052. \009\009\009xLength = 1,\
  4053. \009\009\009yLength = 1,\
  4054. \009\009\009xTextPos = 1,\
  4055. \009\009\009yTextPos = 1,\
  4056. \009\009\009fgColor = MatchingColors[MatchingColors[Settings.bgColor][\"quickSettings\"]][\"mainButtons\"],\
  4057. \009\009\009bgColor = MatchingColors[Settings.bgColor][\"quickSettings\"],\
  4058. \009\009\009returnValue = \"Left\",\
  4059. \009\009},\
  4060. \009\009Green = {\
  4061. \009\009\009name = \"Green\",\
  4062. \009\009\009label = \" \",\
  4063. \009\009\009xPos = 31,\
  4064. \009\009\009yPos = 6,\
  4065. \009\009\009xLength = 1,\
  4066. \009\009\009yLength = 1,\
  4067. \009\009\009xTextPos = 1,\
  4068. \009\009\009yTextPos = 1,\
  4069. \009\009\009fgColor = 1,\
  4070. \009\009\009bgColor = colors.green,\
  4071. \009\009\009returnValue = \"color:green\",\
  4072. \009\009},\
  4073. \009\009Lime = {\
  4074. \009\009\009name = \"Lime\",\
  4075. \009\009\009label = \" \",\
  4076. \009\009\009xPos = 31,\
  4077. \009\009\009yPos = 7,\
  4078. \009\009\009xLength = 1,\
  4079. \009\009\009yLength = 1,\
  4080. \009\009\009xTextPos = 1,\
  4081. \009\009\009yTextPos = 1,\
  4082. \009\009\009fgColor = 1,\
  4083. \009\009\009bgColor = colors.lime,\
  4084. \009\009\009returnValue = \"color:lime\",\
  4085. \009\009},\
  4086. \009\009Brown = {\
  4087. \009\009\009name = \"Brown\",\
  4088. \009\009\009label = \" \",\
  4089. \009\009\009xPos = 32,\
  4090. \009\009\009yPos = 6,\
  4091. \009\009\009xLength = 1,\
  4092. \009\009\009yLength = 1,\
  4093. \009\009\009xTextPos = 1,\
  4094. \009\009\009yTextPos = 1,\
  4095. \009\009\009fgColor = 1,\
  4096. \009\009\009bgColor = colors.brown,\
  4097. \009\009\009returnValue = \"color:brown\",\
  4098. \009\009},\
  4099. \009\009Purple = {\
  4100. \009\009\009name = \"Purple\",\
  4101. \009\009\009label = \" \",\
  4102. \009\009\009xPos = 32,\
  4103. \009\009\009yPos = 7,\
  4104. \009\009\009xLength = 1,\
  4105. \009\009\009yLength = 1,\
  4106. \009\009\009xTextPos = 1,\
  4107. \009\009\009yTextPos = 1,\
  4108. \009\009\009fgColor = 1,\
  4109. \009\009\009bgColor = colors.purple,\
  4110. \009\009\009returnValue = \"color:purple\",\
  4111. \009\009},\
  4112. \009\009Blue = {\
  4113. \009\009\009name = \"Blue\",\
  4114. \009\009\009label = \" \",\
  4115. \009\009\009xPos = 33,\
  4116. \009\009\009yPos = 6,\
  4117. \009\009\009xLength = 1,\
  4118. \009\009\009yLength = 1,\
  4119. \009\009\009xTextPos = 1,\
  4120. \009\009\009yTextPos = 1,\
  4121. \009\009\009fgColor = 1,\
  4122. \009\009\009bgColor = colors.blue,\
  4123. \009\009\009returnValue = \"color:blue\",\
  4124. \009\009},\
  4125. \009\009lightBlue = {\
  4126. \009\009\009name = \"lightBlue\",\
  4127. \009\009\009label = \" \",\
  4128. \009\009\009xPos = 33,\
  4129. \009\009\009yPos = 7,\
  4130. \009\009\009xLength = 1,\
  4131. \009\009\009yLength = 1,\
  4132. \009\009\009xTextPos = 1,\
  4133. \009\009\009yTextPos = 1,\
  4134. \009\009\009fgColor = 1,\
  4135. \009\009\009bgColor = colors.lightBlue,\
  4136. \009\009\009returnValue = \"color:lightBlue\",\
  4137. \009\009},\
  4138. \009\009Yellow = {\
  4139. \009\009\009name = \"Yellow\",\
  4140. \009\009\009label = \" \",\
  4141. \009\009\009xPos = 34,\
  4142. \009\009\009yPos = 6,\
  4143. \009\009\009xLength = 1,\
  4144. \009\009\009yLength = 1,\
  4145. \009\009\009xTextPos = 1,\
  4146. \009\009\009yTextPos = 1,\
  4147. \009\009\009fgColor = 1,\
  4148. \009\009\009bgColor = colors.yellow,\
  4149. \009\009\009returnValue = \"color:yellow\",\
  4150. \009\009},\
  4151. \009\009Cyan = {\
  4152. \009\009\009name = \"Cyan\",\
  4153. \009\009\009label = \" \",\
  4154. \009\009\009xPos = 34,\
  4155. \009\009\009yPos = 7,\
  4156. \009\009\009xLength = 1,\
  4157. \009\009\009yLength = 1,\
  4158. \009\009\009xTextPos = 1,\
  4159. \009\009\009yTextPos = 1,\
  4160. \009\009\009fgColor = 1,\
  4161. \009\009\009bgColor = colors.cyan,\
  4162. \009\009\009returnValue = \"color:cyan\",\
  4163. \009\009},\
  4164. \009\009Orange = {\
  4165. \009\009\009name = \"Orange\",\
  4166. \009\009\009label = \" \",\
  4167. \009\009\009xPos = 35,\
  4168. \009\009\009yPos = 6,\
  4169. \009\009\009xLength = 1,\
  4170. \009\009\009yLength = 1,\
  4171. \009\009\009xTextPos = 1,\
  4172. \009\009\009yTextPos = 1,\
  4173. \009\009\009fgColor = 1,\
  4174. \009\009\009bgColor = colors.orange,\
  4175. \009\009\009returnValue = \"color:orange\",\
  4176. \009\009},\
  4177. \009\009Pink = {\
  4178. \009\009\009name = \"Pink\",\
  4179. \009\009\009label = \" \",\
  4180. \009\009\009xPos = 35,\
  4181. \009\009\009yPos = 7,\
  4182. \009\009\009xLength = 1,\
  4183. \009\009\009yLength = 1,\
  4184. \009\009\009xTextPos = 1,\
  4185. \009\009\009yTextPos = 1,\
  4186. \009\009\009fgColor = 1,\
  4187. \009\009\009bgColor = colors.pink,\
  4188. \009\009\009returnValue = \"color:pink\",\
  4189. \009\009},\
  4190. \009\009Red = {\
  4191. \009\009\009name = \"Red\",\
  4192. \009\009\009label = \" \",\
  4193. \009\009\009xPos = 36,\
  4194. \009\009\009yPos = 6,\
  4195. \009\009\009xLength = 1,\
  4196. \009\009\009yLength = 1,\
  4197. \009\009\009xTextPos = 1,\
  4198. \009\009\009yTextPos = 1,\
  4199. \009\009\009fgColor = 1,\
  4200. \009\009\009bgColor = colors.red,\
  4201. \009\009\009returnValue = \"color:red\",\
  4202. \009\009},\
  4203. \009\009Magenta = {\
  4204. \009\009\009name = \"Magenta\",\
  4205. \009\009\009label = \" \",\
  4206. \009\009\009xPos = 36,\
  4207. \009\009\009yPos = 7,\
  4208. \009\009\009xLength = 1,\
  4209. \009\009\009yLength = 1,\
  4210. \009\009\009xTextPos = 1,\
  4211. \009\009\009yTextPos = 1,\
  4212. \009\009\009fgColor = 1,\
  4213. \009\009\009bgColor = colors.magenta,\
  4214. \009\009\009returnValue = \"color:magenta\",\
  4215. \009\009},\
  4216. \009\009White = {\
  4217. \009\009\009name = \"White\",\
  4218. \009\009\009label = \" \",\
  4219. \009\009\009xPos = 37,\
  4220. \009\009\009yPos = 6,\
  4221. \009\009\009xLength = 1,\
  4222. \009\009\009yLength = 1,\
  4223. \009\009\009xTextPos = 1,\
  4224. \009\009\009yTextPos = 1,\
  4225. \009\009\009fgColor = 1,\
  4226. \009\009\009bgColor = colors.white,\
  4227. \009\009\009returnValue = \"color:white\",\
  4228. \009\009},\
  4229. \009\009lightGray = {\
  4230. \009\009\009name = \"lightGray\",\
  4231. \009\009\009label = \" \",\
  4232. \009\009\009xPos = 37,\
  4233. \009\009\009yPos = 7,\
  4234. \009\009\009xLength = 1,\
  4235. \009\009\009yLength = 1,\
  4236. \009\009\009xTextPos = 1,\
  4237. \009\009\009yTextPos = 1,\
  4238. \009\009\009fgColor = 1,\
  4239. \009\009\009bgColor = colors.lightGray,\
  4240. \009\009\009returnValue = \"color:lightGray\",\
  4241. \009\009},\
  4242. \009\009Black = {\
  4243. \009\009\009name = \"Black\",\
  4244. \009\009\009label = \" \",\
  4245. \009\009\009xPos = 38,\
  4246. \009\009\009yPos = 6,\
  4247. \009\009\009xLength = 1,\
  4248. \009\009\009yLength = 1,\
  4249. \009\009\009xTextPos = 1,\
  4250. \009\009\009yTextPos = 1,\
  4251. \009\009\009fgColor = 1,\
  4252. \009\009\009bgColor = colors.black,\
  4253. \009\009\009returnValue = \"color:black\",\
  4254. \009\009},\
  4255. \009\009Gray = {\
  4256. \009\009\009name = \"Gray\",\
  4257. \009\009\009label = \" \",\
  4258. \009\009\009xPos = 38,\
  4259. \009\009\009yPos = 7,\
  4260. \009\009\009xLength = 1,\
  4261. \009\009\009yLength = 1,\
  4262. \009\009\009xTextPos = 1,\
  4263. \009\009\009yTextPos = 1,\
  4264. \009\009\009fgColor = 1,\
  4265. \009\009\009bgColor = colors.gray,\
  4266. \009\009\009returnValue = \"color:gray\",\
  4267. \009\009},\
  4268. \009\009RefreshColor = {\
  4269. \009\009\009name = \"RefreshColor\",\
  4270. \009\009\009label = \"Refresh\",\
  4271. \009\009\009xPos = 40,\
  4272. \009\009\009yPos = 3,\
  4273. \009\009\009xLength = 9,\
  4274. \009\009\009yLength = 5,\
  4275. \009\009\009xTextPos = 2,\
  4276. \009\009\009yTextPos = 3,\
  4277. \009\009\009fgColor = 1,\
  4278. \009\009\009bgColor = colors.gray,\
  4279. \009\009\009returnValue = \"Refresh\",\
  4280. \009\009},\
  4281. \009}\
  4282. \
  4283. \009QuickSettings.BetterPaint = {\
  4284. \009\009Restart = {\
  4285. \009\009\009xPos = 4,\
  4286. \009\009\009yPos = 2,\
  4287. \009\009\009name = \"Restart\",\
  4288. \009\009\009path = programPath..\"QuickSettings/restart.ico\",\
  4289. \009\009\009yLength = 6,\
  4290. \009\009\009xLength = 7,\
  4291. \009\009\009returnValue = \"reboot\",\
  4292. \009\009\009label = \"Restart\",\
  4293. \009\009\009labelFg = MatchingColors[MatchingColors[Settings.bgColor][\"quickSettings\"]][\"mainButtons\"],\
  4294. \009\009\009labelBg = MatchingColors[Settings.bgColor][\"quickSettings\"],\
  4295. \009\009},\
  4296. \009\009Shutdown = {\
  4297. \009\009\009xPos = 22,\
  4298. \009\009\009yPos = 2,\
  4299. \009\009\009name = \"Shutdown\",\
  4300. \009\009\009path = programPath..\"QuickSettings/shutdown.ico\",\
  4301. \009\009\009yLength = 6,\
  4302. \009\009\009xLength = 7,\
  4303. \009\009\009returnValue = \"shutdown\",\
  4304. \009\009\009label = \"Shutdown\",\
  4305. \009\009\009labelFg = MatchingColors[MatchingColors[Settings.bgColor][\"quickSettings\"]][\"mainButtons\"],\
  4306. \009\009\009labelBg = MatchingColors[Settings.bgColor][\"quickSettings\"],\
  4307. \009\009},\
  4308. \009\009Settings = {\
  4309. \009\009\009xPos = 13,\
  4310. \009\009\009yPos = 2,\
  4311. \009\009\009name = \"Settings\",\
  4312. \009\009\009path = programPath..\"QuickSettings/settings.ico\",\
  4313. \009\009\009yLength = 6,\
  4314. \009\009\009xLength = 7,\
  4315. \009\009\009returnValue = \"settings\",\
  4316. \009\009\009label = \"Settings\",\
  4317. \009\009\009labelFg = MatchingColors[MatchingColors[Settings.bgColor][\"quickSettings\"]][\"mainButtons\"],\
  4318. \009\009\009labelBg = MatchingColors[Settings.bgColor][\"quickSettings\"],\
  4319. \009\009},\
  4320. \009}\
  4321. \
  4322. \009QuickSettings.Text = {\
  4323. \009\009Label = {\
  4324. \009\009\009name = \"Label\",\
  4325. \009\009\009text = \"QuickSettings\",\
  4326. \009\009\009xPos = w/2-6,\
  4327. \009\009\009yPos = 1,\
  4328. \009\009\009bgColor = MatchingColors[Settings.bgColor][\"quickSettings\"],\
  4329. \009\009\009fgColor = Settings.bgColor,\
  4330. \009\009},\
  4331. \009}\
  4332. \009QuickSettings.Key = {\
  4333. \009\009R = {\
  4334. \009\009\009name = \"R\",\
  4335. \009\009\009key = \"r\",\
  4336. \009\009\009onPress = function() os.reboot() end,\
  4337. \009\009},\
  4338. \009\009S = {\
  4339. \009\009\009name = \"S\",\
  4340. \009\009\009key = \"s\",\
  4341. \009\009\009onPress = function() os.shutdown() end,\
  4342. \009\009},\
  4343. \009\009LeftAlt = {\
  4344. \009\009\009name = \"LeftAlt\",\
  4345. \009\009\009key = \"LeftAlt\",\
  4346. \009\009\009onPress = function() OmniOS.switch(OmniOS.launch(\"Settings\",\"Settings\")) end,\
  4347. \009\009},\
  4348. \009}\
  4349. \
  4350. \009loadShortcuts()\
  4351. \
  4352. end\
  4353. \
  4354. local function loadGUI()\
  4355. \009--Initializing GUI components\
  4356. \009gui = Interact.Initialize()\
  4357. \009mainLayout = gui.Layout.new({xPos = 1,yPos = 1,xLength = w,yLength = h})\
  4358. \009quickSettingsLayout = gui.Layout.new({xPos = 1,yPos = h-7,xLength = w,yLength = h, nilClick = true})\
  4359. \009mainLayoutTable = {}\
  4360. \009quickSettingsLayoutTable = {}\
  4361. end\
  4362. \
  4363. local function InitializeGUI()\
  4364. \009loadObjects()\
  4365. \009--Main--\
  4366. \009mainLayoutTable = {}\
  4367. \009mainLayoutTable = gui.loadObjects(Main)\
  4368. \009mainLayoutTable.mainBgColor = gui.BackgroundColor.new({color = Settings.bgColor})\
  4369. \009\
  4370. \009--QuickSettings--\
  4371. \009quickSettingsLayoutTable = {}\
  4372. \009quickSettingsLayoutTable.Text = {}\
  4373. \009quickSettingsLayoutTable = gui.loadObjects(QuickSettings)\
  4374. \009quickSettingsLayoutTable.quickSettingsBgColor = gui.BackgroundColor.new({color = MatchingColors[Settings.bgColor][\"quickSettings\"]})\
  4375. \009\
  4376. \009--Initializing structures--\
  4377. \009--Main--\
  4378. \009for i,v in pairs(mainLayoutTable.Button) do\
  4379. \009\009mainLayout:addButton(mainLayoutTable.Button[i]:returnData())\
  4380. \009end\
  4381. \009for i,v in pairs(mainLayoutTable.BetterPaint) do\
  4382. \009\009mainLayout:addBetterPaint(mainLayoutTable.BetterPaint[i]:returnData())\
  4383. \009end\
  4384. \009mainLayout:addBackgroundColor(mainLayoutTable.mainBgColor:returnData())\
  4385. \009\
  4386. \009--QuickSettings--\
  4387. \009for i,v in pairs(quickSettingsLayoutTable.Button) do\
  4388. \009\009quickSettingsLayout:addButton(quickSettingsLayoutTable.Button[i]:returnData())\
  4389. \009end\
  4390. \009quickSettingsLayout:addBackgroundColor(quickSettingsLayoutTable.quickSettingsBgColor:returnData())\
  4391. \009quickSettingsLayout:addBetterPaint(quickSettingsLayoutTable.BetterPaint.Restart:returnData())\
  4392. \009quickSettingsLayout:addBetterPaint(quickSettingsLayoutTable.BetterPaint.Shutdown:returnData())\
  4393. \009quickSettingsLayout:addBetterPaint(quickSettingsLayoutTable.BetterPaint.Settings:returnData())\
  4394. \009quickSettingsLayout:addKey(quickSettingsLayoutTable.Key.R:returnData())\
  4395. \009quickSettingsLayout:addKey(quickSettingsLayoutTable.Key.S:returnData())\
  4396. end\
  4397. \
  4398. local function changeColor(color)\
  4399. \009Settings.bgColor = colors[color] or colors.green\
  4400. \009local f = fs.open(programPath..\"Settings\",\"w\")\
  4401. \009f.write(textutilsserialize(Settings))\
  4402. \009f.close()\
  4403. \009local buffer = readFile(programPath..\"Settings\")\
  4404. \009Settings = textutils.unserialize( buffer )\
  4405. \009local buffer = readFile(programPath..\"MatchingColors\")\
  4406. \009MatchingColors = textutilsunserialize(buffer)\
  4407. \009loadObjects()\
  4408. \009InitializeGUI()\
  4409. end\
  4410. \
  4411. local function writeTable()\
  4412. \009file = fs.open(programPath..\"MatchingColorsLKK\",\"w\")\
  4413. \009file.write(textutilsserialize({\
  4414. \009\009[8192] = {\
  4415. \009\009\009name = \"green\",\
  4416. \009\009\009quickSettings = colors.lime,\
  4417. \009\009},\
  4418. \009}))\
  4419. \009file.close()\
  4420. end\
  4421. \
  4422. \
  4423. --Loading settings--\
  4424. local buffer = readFile(programPath..\"Settings\")\
  4425. Settings = textutils.unserialize( buffer )\
  4426. \
  4427. local buffer = readFile(programPath..\"MatchingColors\")\
  4428. MatchingColors = textutilsunserialize(buffer)\
  4429. \
  4430. \
  4431. \
  4432. loadObjects()\
  4433. loadGUI()\
  4434. InitializeGUI()\
  4435. \
  4436. \
  4437. --Code--\
  4438. while true do\
  4439. \009mainLayout:draw(0,scroll)\
  4440. \009local result = gui.eventHandler(mainLayout)\
  4441. \009if result[1] == \"Button\" then\
  4442. \009\009if result[2] == \"quickSettings\" then\
  4443. \009\009\009quickSettingsLayout:draw()\
  4444. \009\009\009local notClose = true\
  4445. \009\009\009while notClose do\
  4446. \009\009\009\009local answer = gui.eventHandler(quickSettingsLayout)\
  4447. \009\009\009\009if answer[1] == \"Button\" then\
  4448. \009\009\009\009\009if answer[2] == \"reboot\" then\
  4449. \009\009\009\009\009\009os.reboot()\
  4450. \009\009\009\009\009elseif answer[2] == \"shutdown\" then\
  4451. \009\009\009\009\009\009os.shutdown()\
  4452. \009\009\009\009\009elseif answer[2] == \"Close\" then\
  4453. \009\009\009\009\009\009notClose = false\
  4454. \009\009\009\009\009elseif answer[2] == \"settings\" then\
  4455. \009\009\009\009\009\009OmniOS.switch(OmniOS.launch(\"Settings\",\"term\"))\
  4456. \009\009\009\009\009\009notClose = false\
  4457. \009\009\009\009\009elseif answer[2] == \"Refresh\" then\
  4458. \009\009\009\009\009\009local buffer = readFile(programPath..\"Settings\")\
  4459. \009\009\009\009\009\009Settings = textutils.unserialize( buffer )\
  4460. \009\009\009\009\009\009local buffer = readFile(programPath..\"MatchingColors\")\
  4461. \009\009\009\009\009\009MatchingColors = textutilsunserialize(buffer)\
  4462. \009\009\009\009\009\009loadObjects()\
  4463. \009\009\009\009\009\009InitializeGUI()\
  4464. \009\009\009\009\009\009notClose = false\
  4465. \009\009\009\009\009else\
  4466. \009\009\009\009\009\009local buffer = split(answer[2],\"[^:]+\")\
  4467. \009\009\009\009\009\009changeColor(buffer[2])\
  4468. \009\009\009\009\009\009notClose = false\
  4469. \009\009\009\009\009end\
  4470. \009\009\009\009elseif answer[1] == \"Nil\" then\
  4471. \009\009\009\009\009if answer[2] == \"Nil\" then\
  4472. \009\009\009\009\009\009notClose = false\
  4473. \009\009\009\009\009end\
  4474. \009\009\009\009end\
  4475. \009\009\009end\
  4476. \009\009else\
  4477. \009\009\009OmniOS.switch(OmniOS.launch(result[2],\"term\"))\
  4478. \009\009end\
  4479. \009end\
  4480. end",
  4481.       Data = {
  4482.         Settings = "{\
  4483.  bgColor = 32,\
  4484. }",
  4485.         QuickSettings = {
  4486.           [ "restart.ico" ] = "error('This is an image, not a program!')\
  4487. {\
  4488.  {\
  4489.    {\
  4490.      16384,\
  4491.      \"O\",\
  4492.      1,\
  4493.    },\
  4494.    {\
  4495.      16384,\
  4496.      \"O\",\
  4497.      1,\
  4498.    },\
  4499.    {\
  4500.      16384,\
  4501.      \"O\",\
  4502.      1,\
  4503.    },\
  4504.    {\
  4505.      16384,\
  4506.      \"O\",\
  4507.      1,\
  4508.    },\
  4509.    {\
  4510.      16384,\
  4511.      \"O\",\
  4512.      1,\
  4513.    },\
  4514.  },\
  4515.  {\
  4516.    {\
  4517.      16384,\
  4518.      \"O\",\
  4519.      1,\
  4520.    },\
  4521.    {\
  4522.      16384,\
  4523.      \" \",\
  4524.      32,\
  4525.    },\
  4526.    {\
  4527.      16384,\
  4528.      \"O\",\
  4529.      1,\
  4530.    },\
  4531.    {\
  4532.      16384,\
  4533.      \" \",\
  4534.      32,\
  4535.    },\
  4536.    {\
  4537.      16384,\
  4538.      \" \",\
  4539.      32,\
  4540.    },\
  4541.  },\
  4542.  {\
  4543.    {\
  4544.      16384,\
  4545.      \"O\",\
  4546.      1,\
  4547.    },\
  4548.    {\
  4549.      16384,\
  4550.      \" \",\
  4551.      32,\
  4552.    },\
  4553.    {\
  4554.      16384,\
  4555.      \"O\",\
  4556.      1,\
  4557.    },\
  4558.    {\
  4559.      16384,\
  4560.      \"O\",\
  4561.      1,\
  4562.    },\
  4563.    {\
  4564.      16384,\
  4565.      \" \",\
  4566.      32,\
  4567.    },\
  4568.  },\
  4569.  {\
  4570.    {\
  4571.      16384,\
  4572.      \"O\",\
  4573.      1,\
  4574.    },\
  4575.    {\
  4576.      16384,\
  4577.      \"O\",\
  4578.      1,\
  4579.    },\
  4580.    {\
  4581.      16384,\
  4582.      \"O\",\
  4583.      1,\
  4584.    },\
  4585.    {\
  4586.      16384,\
  4587.      \" \",\
  4588.      32,\
  4589.    },\
  4590.    {\
  4591.      16384,\
  4592.      \"O\",\
  4593.      1,\
  4594.    },\
  4595.  },\
  4596.  {\
  4597.    {\
  4598.      16384,\
  4599.      \" \",\
  4600.      1,\
  4601.    },\
  4602.    {\
  4603.      16384,\
  4604.      \" \",\
  4605.      1,\
  4606.    },\
  4607.    {\
  4608.      16384,\
  4609.      \" \",\
  4610.      1,\
  4611.    },\
  4612.    {\
  4613.      16384,\
  4614.      \" \",\
  4615.      1,\
  4616.    },\
  4617.    {\
  4618.      16384,\
  4619.      \" \",\
  4620.      32,\
  4621.    },\
  4622.  },\
  4623.  {\
  4624.    {\
  4625.      16384,\
  4626.      \" \",\
  4627.      1,\
  4628.    },\
  4629.    {\
  4630.      16384,\
  4631.      \" \",\
  4632.      1,\
  4633.    },\
  4634.    {\
  4635.      16384,\
  4636.      \" \",\
  4637.      1,\
  4638.    },\
  4639.    {\
  4640.      16384,\
  4641.      \" \",\
  4642.      1,\
  4643.    },\
  4644.    {\
  4645.      16384,\
  4646.      \" \",\
  4647.      1,\
  4648.    },\
  4649.  },\
  4650.  [ 0 ] = {\
  4651.    {\
  4652.      16384,\
  4653.      \" \",\
  4654.      32,\
  4655.    },\
  4656.    {\
  4657.      16384,\
  4658.      \" \",\
  4659.      32,\
  4660.    },\
  4661.    {\
  4662.      16384,\
  4663.      \" \",\
  4664.      32,\
  4665.    },\
  4666.    {\
  4667.      16384,\
  4668.      \" \",\
  4669.      32,\
  4670.    },\
  4671.    {\
  4672.      16384,\
  4673.      \" \",\
  4674.      32,\
  4675.    },\
  4676.  },\
  4677.  [ 10 ] = {},\
  4678. }",
  4679.           [ "settings.ico" ] = "error('This is an image, not a program!')\
  4680. {\
  4681.  {\
  4682.    {\
  4683.      2,\
  4684.      \"-\",\
  4685.      1,\
  4686.    },\
  4687.    {\
  4688.      2,\
  4689.      \"-\",\
  4690.      1,\
  4691.    },\
  4692.    {\
  4693.      2,\
  4694.      \"-\",\
  4695.      1,\
  4696.    },\
  4697.    {\
  4698.      2,\
  4699.      \"O\",\
  4700.      1,\
  4701.    },\
  4702.    {\
  4703.      2,\
  4704.      \"-\",\
  4705.      1,\
  4706.    },\
  4707.  },\
  4708.  {\
  4709.    {\
  4710.      2,\
  4711.      \"-\",\
  4712.      1,\
  4713.    },\
  4714.    {\
  4715.      2,\
  4716.      \"-\",\
  4717.      1,\
  4718.    },\
  4719.    {\
  4720.      2,\
  4721.      \"O\",\
  4722.      1,\
  4723.    },\
  4724.    {\
  4725.      2,\
  4726.      \"-\",\
  4727.      1,\
  4728.    },\
  4729.    {\
  4730.      2,\
  4731.      \"-\",\
  4732.      1,\
  4733.    },\
  4734.  },\
  4735.  {\
  4736.    {\
  4737.      2,\
  4738.      \"O\",\
  4739.      1,\
  4740.    },\
  4741.    {\
  4742.      2,\
  4743.      \"-\",\
  4744.      1,\
  4745.    },\
  4746.    {\
  4747.      2,\
  4748.      \"-\",\
  4749.      1,\
  4750.    },\
  4751.    {\
  4752.      2,\
  4753.      \"-\",\
  4754.      1,\
  4755.    },\
  4756.    {\
  4757.      2,\
  4758.      \"-\",\
  4759.      1,\
  4760.    },\
  4761.  },\
  4762.  {\
  4763.    {\
  4764.      2,\
  4765.      \"-\",\
  4766.      1,\
  4767.    },\
  4768.    {\
  4769.      2,\
  4770.      \"-\",\
  4771.      1,\
  4772.    },\
  4773.    {\
  4774.      2,\
  4775.      \"-\",\
  4776.      1,\
  4777.    },\
  4778.    {\
  4779.      2,\
  4780.      \"-\",\
  4781.      1,\
  4782.    },\
  4783.    {\
  4784.      2,\
  4785.      \"O\",\
  4786.      1,\
  4787.    },\
  4788.  },\
  4789.  {\
  4790.    {\
  4791.      2,\
  4792.      \"-\",\
  4793.      1,\
  4794.    },\
  4795.    {\
  4796.      2,\
  4797.      \"O\",\
  4798.      1,\
  4799.    },\
  4800.    {\
  4801.      2,\
  4802.      \"-\",\
  4803.      1,\
  4804.    },\
  4805.    {\
  4806.      2,\
  4807.      \"-\",\
  4808.      1,\
  4809.    },\
  4810.    {\
  4811.      2,\
  4812.      \"-\",\
  4813.      1,\
  4814.    },\
  4815.  },\
  4816.  {\
  4817.    {\
  4818.      2,\
  4819.      \" \",\
  4820.      2,\
  4821.    },\
  4822.    {\
  4823.      2,\
  4824.      \" \",\
  4825.      2,\
  4826.    },\
  4827.    {\
  4828.      2,\
  4829.      \" \",\
  4830.      2,\
  4831.    },\
  4832.    {\
  4833.      2,\
  4834.      \" \",\
  4835.      2,\
  4836.    },\
  4837.    {\
  4838.      2,\
  4839.      \" \",\
  4840.      1,\
  4841.    },\
  4842.  },\
  4843.  [ 0 ] = {\
  4844.    {\
  4845.      2,\
  4846.      \" \",\
  4847.      2,\
  4848.    },\
  4849.    {\
  4850.      2,\
  4851.      \" \",\
  4852.      2,\
  4853.    },\
  4854.    {\
  4855.      2,\
  4856.      \" \",\
  4857.      2,\
  4858.    },\
  4859.    {\
  4860.      2,\
  4861.      \" \",\
  4862.      2,\
  4863.    },\
  4864.    {\
  4865.      2,\
  4866.      \" \",\
  4867.      2,\
  4868.    },\
  4869.  },\
  4870. }",
  4871.           [ "shutdown.ico" ] = "error('This is an image, not a program!')\
  4872. {\
  4873.  {\
  4874.    {\
  4875.      8192,\
  4876.      \"O\",\
  4877.      1,\
  4878.    },\
  4879.    {\
  4880.      8192,\
  4881.      \"O\",\
  4882.      1,\
  4883.    },\
  4884.    {\
  4885.      8192,\
  4886.      \"O\",\
  4887.      1,\
  4888.    },\
  4889.    {\
  4890.      8192,\
  4891.      \" \",\
  4892.      8192,\
  4893.    },\
  4894.    {\
  4895.      8192,\
  4896.      \"O\",\
  4897.      1,\
  4898.    },\
  4899.  },\
  4900.  {\
  4901.    {\
  4902.      8192,\
  4903.      \"O\",\
  4904.      1,\
  4905.    },\
  4906.    {\
  4907.      8192,\
  4908.      \" \",\
  4909.      1,\
  4910.    },\
  4911.    {\
  4912.      8192,\
  4913.      \"O\",\
  4914.      1,\
  4915.    },\
  4916.    {\
  4917.      8192,\
  4918.      \" \",\
  4919.      8192,\
  4920.    },\
  4921.    {\
  4922.      8192,\
  4923.      \"O\",\
  4924.      1,\
  4925.    },\
  4926.  },\
  4927.  {\
  4928.    {\
  4929.      8192,\
  4930.      \"O\",\
  4931.      1,\
  4932.    },\
  4933.    {\
  4934.      8192,\
  4935.      \" \",\
  4936.      1,\
  4937.    },\
  4938.    {\
  4939.      8192,\
  4940.      \"O\",\
  4941.      1,\
  4942.    },\
  4943.    {\
  4944.      8192,\
  4945.      \" \",\
  4946.      8192,\
  4947.    },\
  4948.    {\
  4949.      8192,\
  4950.      \"O\",\
  4951.      1,\
  4952.    },\
  4953.  },\
  4954.  {\
  4955.    {\
  4956.      8192,\
  4957.      \"O\",\
  4958.      1,\
  4959.    },\
  4960.    {\
  4961.      8192,\
  4962.      \" \",\
  4963.      1,\
  4964.    },\
  4965.    {\
  4966.      8192,\
  4967.      \"O\",\
  4968.      1,\
  4969.    },\
  4970.    {\
  4971.      8192,\
  4972.      \"O\",\
  4973.      1,\
  4974.    },\
  4975.    {\
  4976.      8192,\
  4977.      \"O\",\
  4978.      1,\
  4979.    },\
  4980.  },\
  4981.  {\
  4982.    {\
  4983.      8192,\
  4984.      \" \",\
  4985.      8192,\
  4986.    },\
  4987.    {\
  4988.      8192,\
  4989.      \" \",\
  4990.      8192,\
  4991.    },\
  4992.    {\
  4993.      8192,\
  4994.      \" \",\
  4995.      8192,\
  4996.    },\
  4997.    {\
  4998.      8192,\
  4999.      \" \",\
  5000.      8192,\
  5001.    },\
  5002.    {\
  5003.      8192,\
  5004.      \" \",\
  5005.      8192,\
  5006.    },\
  5007.  },\
  5008.  {\
  5009.    {\
  5010.      8192,\
  5011.      \" \",\
  5012.      8192,\
  5013.    },\
  5014.    {\
  5015.      8192,\
  5016.      \" \",\
  5017.      8192,\
  5018.    },\
  5019.    {\
  5020.      8192,\
  5021.      \" \",\
  5022.      8192,\
  5023.    },\
  5024.    {\
  5025.      8192,\
  5026.      \" \",\
  5027.      8192,\
  5028.    },\
  5029.    {\
  5030.      8192,\
  5031.      \" \",\
  5032.      8192,\
  5033.    },\
  5034.  },\
  5035.  [ 0 ] = {\
  5036.    {\
  5037.      8192,\
  5038.      \" \",\
  5039.      8192,\
  5040.    },\
  5041.    {\
  5042.      8192,\
  5043.      \" \",\
  5044.      8192,\
  5045.    },\
  5046.    {\
  5047.      8192,\
  5048.      \" \",\
  5049.      8192,\
  5050.    },\
  5051.    {\
  5052.      8192,\
  5053.      \" \",\
  5054.      8192,\
  5055.    },\
  5056.    {\
  5057.      8192,\
  5058.      \" \",\
  5059.      8192,\
  5060.    },\
  5061.  },\
  5062. }",
  5063.         },
  5064.         MatchingColors = "{\
  5065.  [ 8192 ] = {\
  5066.    name = \"green\",\
  5067.    quickSettings = 32,\
  5068. \009mainButtons  = 1,\
  5069.  },\
  5070.  [ 4096 ] = {\
  5071.    name = \"brown\",\
  5072.    quickSettings = 2,\
  5073. \009mainButtons  = 1,\
  5074.  },\
  5075.  [ 32768 ] = {\
  5076.    name = \"black\",\
  5077.    quickSettings = 128,\
  5078. \009mainButtons  = 1,\
  5079.  },\
  5080.  [ 64 ] = {\
  5081.    name = \"pink\",\
  5082.    quickSettings = 16384,\
  5083. \009mainButtons  = 1,\
  5084.  },\
  5085.  [ 16 ] = {\
  5086.    name = \"yellow\",\
  5087.    quickSettings = 1,\
  5088. \009mainButtons  = 32768,\
  5089.  },\
  5090.  [ 2 ] = {\
  5091.    name = \"orange\",\
  5092.    quickSettings = 4096,\
  5093. \009mainButtons  = 1,\
  5094.  },\
  5095.  [ 4 ] = {\
  5096.    name = \"magenta\",\
  5097.    quickSettings = 1024,\
  5098. \009mainButtons  = 1,\
  5099.  },\
  5100.  [ 1024 ] = {\
  5101.    name = \"purple\",\
  5102.    quickSettings = 4,\
  5103. \009mainButtons  = 1,\
  5104.  },\
  5105.  [ 512 ] = {\
  5106.    name = \"cyan\",\
  5107.    quickSettings = 8,\
  5108. \009mainButtons  = 1,\
  5109.  },\
  5110.  [ 16384 ] = {\
  5111.    name = \"red\",\
  5112.    quickSettings = 64,\
  5113. \009mainButtons  = 1,\
  5114.  },\
  5115.  [ 1 ] = {\
  5116.    name = \"white\",\
  5117.    quickSettings = 128,\
  5118. \009mainButtons  = 32768,\
  5119.  },\
  5120.  [ 8 ] = {\
  5121.    name = \"lightBlue\",\
  5122.    quickSettings = 2048,\
  5123. \009mainButtons  = 32768,\
  5124.  },\
  5125.  [ 256 ] = {\
  5126.    name = \"lightGray\",\
  5127.    quickSettings = 1,\
  5128. \009mainButtons  = 1,\
  5129.  },\
  5130.  [ 128 ] = {\
  5131.    name = \"gray\",\
  5132.    quickSettings = 256,\
  5133. \009mainButtons  = 1,\
  5134.  },\
  5135.  [ 32 ] = {\
  5136.    name = \"lime\",\
  5137.    quickSettings = 8192,\
  5138. \009mainButtons  = 1,\
  5139.  },\
  5140.  [ 2048 ] = {\
  5141.    name = \"blue\",\
  5142.    quickSettings = 8,\
  5143. \009mainButtons  = 1,\
  5144.  },\
  5145. }",
  5146.         MatchingColorsT = "green 8192\
  5147. brown 4096\
  5148. black 32768\
  5149. pink 64\
  5150. yellow 16\
  5151. orange 2\
  5152. magenta 4\
  5153. purple 1024\
  5154. cyan 512\
  5155. red 16384\
  5156. white 1\
  5157. lightBlue 8\
  5158. lightGray 256\
  5159. gray 128\
  5160. lime 32\
  5161. blue 2048",
  5162.         Shortcuts = "{\
  5163. \009{\
  5164. \009\009xPos = 7,\
  5165. \009\009yPos = 3,\
  5166. \009\009path = \"OmniOS/Programs/FileX/FileX\",\
  5167. \009\009name = \"FileX\",\
  5168. \009},\
  5169. \009{\
  5170. \009\009xPos = 2,\
  5171. \009\009yPos = 1,\
  5172. \009\009path = \"OmniOS/Programs/nPaintPro/nPaintPro\",\
  5173. \009\009name = \"nPaintPro\"\
  5174. \009},\
  5175. \009{\
  5176. \009\009xPos = 3,\
  5177. \009\009yPos = 1,\
  5178. \009\009path = \"OmniOS/Programs/BetterPaint/BetterPaint\",\
  5179. \009\009name = \"BetterPaint\"\
  5180. \009},\
  5181. \009{\
  5182. \009\009xPos = 2,\
  5183. \009\009yPos = 2,\
  5184. \009\009path = \"OmniOS/Programs/Edit/Edit\",\
  5185. \009\009name = \"Edit\"\
  5186. \009},\
  5187. \009{\
  5188. \009\009xPos = 3,\
  5189. \009\009yPos = 2,\
  5190. \009\009path = \"OmniOS/Programs/Shell/Shell\",\
  5191. \009\009name = \"Shell\"\
  5192. \009},\
  5193. }",
  5194.       },
  5195.     },
  5196.     Settings = {
  5197.       [ "Settings.ico" ] = "error('This is an image, not a program!')\
  5198. {\
  5199.  {\
  5200.    {\
  5201.      2,\
  5202.      \"-\",\
  5203.      1,\
  5204.    },\
  5205.    {\
  5206.      2,\
  5207.      \"-\",\
  5208.      1,\
  5209.    },\
  5210.    {\
  5211.      2,\
  5212.      \"O\",\
  5213.      1,\
  5214.    },\
  5215.  },\
  5216.  {\
  5217.    {\
  5218.      2,\
  5219.      \"-\",\
  5220.      1,\
  5221.    },\
  5222.    {\
  5223.      2,\
  5224.      \"-\",\
  5225.      1,\
  5226.    },\
  5227.    {\
  5228.      2,\
  5229.      \"-\",\
  5230.      1,\
  5231.    },\
  5232.  },\
  5233.  {\
  5234.    {\
  5235.      2,\
  5236.      \"-\",\
  5237.      1,\
  5238.    },\
  5239.    {\
  5240.      2,\
  5241.      \"O\",\
  5242.      1,\
  5243.    },\
  5244.    {\
  5245.      2,\
  5246.      \"-\",\
  5247.      1,\
  5248.    },\
  5249.  },\
  5250.  [ 0 ] = {\
  5251.    {\
  5252.      2,\
  5253.      \"O\",\
  5254.      1,\
  5255.    },\
  5256.    {\
  5257.      2,\
  5258.      \"-\",\
  5259.      1,\
  5260.    },\
  5261.    {\
  5262.      2,\
  5263.      \"-\",\
  5264.      1,\
  5265.    },\
  5266.  },\
  5267. }",
  5268.       [ "Main.lua" ] = "--[[\
  5269. \009Settings\
  5270. \009by Creator\
  5271. \009for OmniOS\
  5272. ]]--\
  5273. \
  5274. --variables--\
  5275. local w,h = term.getSize()\
  5276. local MainLayoutTable = {}\
  5277. local path = \"OmniOS/Programs/Settings/Data/\"\
  5278. \
  5279. --functions--\
  5280. local function loadLayout(sPath,...)\
  5281. \009local func, err = loadfile(sPath,...)\
  5282. \009if not func then\
  5283. \009\009print(err)\
  5284. \009\009print(sPath)\
  5285. \009end\
  5286. \009return func()\
  5287. end\
  5288. \
  5289. local function changeColor(color)\
  5290. \009local f = fs.open(\"OmniOS/Programs/Desktop/Data/Settings\",\"r\")\
  5291. \009local Settings = textutils.unserialize(f.readAll())\
  5292. \009f.close()\
  5293. \009Settings.bgColor = colors[color] or colors.green\
  5294. \009local f = fs.open(\"OmniOS/Programs/Desktop/Data/Settings\",\"w\")\
  5295. \009f.write(textutils.serialize(Settings))\
  5296. \009f.close()\
  5297. end\
  5298. \
  5299. local function split(str,sep)\
  5300. \009local buffer = {}\
  5301. \009for token in str:gmatch(sep) do\
  5302. \009\009buffer[#buffer+1] = token\
  5303. \009end\
  5304. \009return buffer\
  5305. end\
  5306. \
  5307. local function drawPrograms()\
  5308. \009local tRawPrograms = fs.list(\"OmniOS/Programs\")\
  5309. \009local tPrograms = {}\
  5310. \009for i,v in pairs(tRawPrograms) do\
  5311. \009\009tPrograms[#tPrograms+1] = string.match(v,\"[^.]+\")\
  5312. \009end\
  5313. \009local SelectLayout = gui.Layout.new({xPos = 1,\
  5314. \009\009yPos = h-#tPrograms,\
  5315. \009\009xLength = 15,\
  5316. \009\009yLength = #tPrograms})\
  5317. \009SelectLayoutTable = gui.loadObjects(loadLayout(path..\"Layouts/Select.layout\",tPrograms))\
  5318. \009gui.loadLayout(SelectLayoutTable,SelectLayout)\
  5319. \009SelectLayout:addBackgroundColor({color = colors.white})\
  5320. \009SelectLayout:draw()\
  5321. \009SelectLayoutEvent = gui.eventHandler(SelectLayout)\
  5322. end\
  5323. \
  5324. --Code--\
  5325. --os.loadAPI(\"OmniOS/API/Interact\")\
  5326. local gui = Interact.Initialize()\
  5327. \
  5328. --Layouts--\
  5329. local MainLayout = gui.Layout.new({xPos = 1,yPos = 1,xLength = w,yLength = h})\
  5330. local DesktopLayout = gui.Layout.new({xPos = 1,yPos = 1,xLength = w,yLength = h})\
  5331. local SecurityLayout = gui.Layout.new({xPos = 1,yPos = 1,xLength = w,yLength = h})\
  5332. local ShortcutsLayout = gui.Layout.new({xPos = 1,yPos = 1,xLength = w,yLength = h})\
  5333. \
  5334. --MainLayout--\
  5335. print(loadLayout)\
  5336. local MainLayoutTable = gui.loadObjects(\
  5337. \009loadLayout(path..\"Layouts/Main.layout\"))\
  5338. gui.loadLayout(MainLayoutTable,MainLayout)\
  5339. MainLayout:addBackgroundColor({color = colors.white})\
  5340. \
  5341. --Desktop Layout--\
  5342. local DesktopLayoutTable = gui.loadObjects(loadLayout(path..\"Layouts/Desktop.layout\"))\
  5343. gui.loadLayout(DesktopLayoutTable,DesktopLayout)\
  5344. DesktopLayout:addBackgroundColor({color = colors.white})\
  5345. \
  5346. --SecurityLayout--\
  5347. local SecurityLayoutTable = gui.loadObjects(loadLayout(path..\"Layouts/Security.layout\"))\
  5348. gui.loadLayout(SecurityLayoutTable,SecurityLayout)\
  5349. SecurityLayout:addBackgroundColor({color = colors.white})\
  5350. \
  5351. --Desktop Shortcuts Layout\
  5352. local ShortcutsLayoutTable = gui.loadObjects(loadLayout(path..\"Layouts/Shortcuts.layout\"))\
  5353. gui.loadLayout(ShortcutsLayoutTable,ShortcutsLayout)\
  5354. ShortcutsLayout:addBackgroundColor({color = colors.white})\
  5355. \
  5356. --Select Layout\
  5357. local SelectLayoutTable = {}\
  5358. \
  5359. \
  5360. while true do\
  5361. \009MainLayout:draw()\
  5362. \009local MainLayoutEvent = gui.eventHandler(MainLayout)\
  5363. \009if MainLayoutEvent[1] == \"Button\" then\
  5364. \009\009if MainLayoutEvent[2] == \"Desktop\" then\
  5365. \009\009\009local notClose = true\
  5366. \009\009\009while notClose do\
  5367. \009\009\009\009DesktopLayout:draw()\
  5368. \009\009\009\009DesktopLayoutEvent = gui.eventHandler(DesktopLayout)\
  5369. \009\009\009\009if DesktopLayoutEvent[1] == \"Button\" then\
  5370. \009\009\009\009\009if DesktopLayoutEvent[2] == \"Done\" then\
  5371. \009\009\009\009\009\009notClose = false\
  5372. \009\009\009\009\009elseif DesktopLayoutEvent[2] == \"Shortcuts\" then\
  5373. \009\009\009\009\009\009while true do\
  5374. \009\009\009\009\009\009\009ShortcutsLayout:draw()\
  5375. \009\009\009\009\009\009\009ShortcutsLayoutEvent = gui.eventHandler(ShortcutsLayout)\
  5376. \009\009\009\009\009\009\009if ShortcutsLayoutEvent[1] == \"Button\" then\
  5377. \009\009\009\009\009\009\009\009if ShortcutsLayoutEvent[2] == \"Add\" then\
  5378. \009\009\009\009\009\009\009\009\009drawPrograms()\
  5379. \009\009\009\009\009\009\009\009elseif ShortcutsLayoutEvent[2] == \"Done\" then\
  5380. \009\009\009\009\009\009\009\009\009break\
  5381. \009\009\009\009\009\009\009\009end\
  5382. \009\009\009\009\009\009\009elseif ShortcutsLayoutEvent[1] == \"BeterPaint\" then\
  5383. \
  5384. \009\009\009\009\009\009\009end\
  5385. \009\009\009\009\009\009end\
  5386. \009\009\009\009\009\009notClose = false\
  5387. \009\009\009\009\009else\
  5388. \009\009\009\009\009\009local buffer = split(DesktopLayoutEvent[2],\"[^:]+\")\
  5389. \009\009\009\009\009\009print(DesktopLayoutEvent[2])\
  5390. \009\009\009\009\009\009changeColor(buffer[2])\
  5391. \009\009\009\009\009end\
  5392. \009\009\009\009end\
  5393. \009\009\009end\
  5394. \009\009elseif MainLayoutEvent[2] == \"Security\" then\
  5395. \009\009\009local notClose = true\
  5396. \009\009\009while notClose do\
  5397. \009\009\009\009SecurityLayout:draw()\
  5398. \009\009\009\009SecurityLayoutEvent = gui.eventHandler(SecurityLayout)\
  5399. \009\009\009\009if SecurityLayoutEvent[1] == \"TextBox\" then\
  5400. \009\009\009\009\009if SecurityLayoutEvent[2] == \"Password\" then\
  5401. \009\009\009\009\009\009local newPass = SecurityLayout.TextBox.Password:read()\
  5402. \009\009\009\009\009\009local file = fs.open(\"OmniOS/Settings/Users/Admin\",\"w\")\
  5403. \009\009\009\009\009\009file.write(Sha.sha256(newPass))\
  5404. \009\009\009\009\009\009file.close()\
  5405. \009\009\009\009\009end\
  5406. \009\009\009\009elseif SecurityLayoutEvent[1] == \"Button\" then\
  5407. \009\009\009\009\009if SecurityLayoutEvent[2] == \"Done\" then\
  5408. \009\009\009\009\009\009notClose = false\
  5409. \009\009\009\009\009end\
  5410. \009\009\009\009end\
  5411. \009\009\009end\
  5412. \009\009elseif MainLayoutEvent[2] == \"Close\" then\
  5413. \009\009\009break\
  5414. \009\009end\
  5415. \009end\
  5416. end",
  5417.       Data = {
  5418.         Layouts = {
  5419.           [ "Shortcuts.layout" ] = "local w,h = term.getSize()\
  5420. local programPath = \"OmniOS/Programs/Desktop/Data/\"\
  5421. \
  5422. MainTable = {\
  5423. \009Button = {\
  5424. \009\009Done = {\
  5425. \009\009\009name = \"Done\",\
  5426. \009\009\009label = \"Done\",\
  5427. \009\009\009xPos = w-3,\
  5428. \009\009\009yPos = h,\
  5429. \009\009\009fgColor = colors.white,\
  5430. \009\009\009bgColor = colors.green,\
  5431. \009\009\009xLength = 4,\
  5432. \009\009\009yLength = 1,\
  5433. \009\009\009returnValue = \"Done\",\
  5434. \009\009\009xTextPos = 1,\
  5435. \009\009\009yTextPos = 1,\
  5436. \009\009},\
  5437. \009\009Add = {\
  5438. \009\009\009name = \"Add\",\
  5439. \009\009\009label = \"+\",\
  5440. \009\009\009xPos = 1,\
  5441. \009\009\009yPos = h,\
  5442. \009\009\009fgColor = colors.black,\
  5443. \009\009\009bgColor = colors.white,\
  5444. \009\009\009xLength = 11,\
  5445. \009\009\009yLength = 3,\
  5446. \009\009\009returnValue = \"Add\",\
  5447. \009\009\009xTextPos = 1,\
  5448. \009\009\009yTextPos = 1,\
  5449. \009\009},\
  5450. \009},\
  5451. }\
  5452. \
  5453. local function readFile(_path)\
  5454. \009local file = fs.open(_path,\"r\")\
  5455. \009local data = file.readAll()\
  5456. \009file.close()\
  5457. \009return data\
  5458. end\
  5459. \
  5460. local function loadShortcuts()\
  5461. \009local buffer = readFile(programPath..\"Shortcuts\")\
  5462. \009local sBuffer = textutils.unserialize(buffer)\
  5463. \009local nBuffer = {}\
  5464. \009paths = {}\
  5465. \009for i,v in pairs(sBuffer) do\
  5466. \009\009nBuffer[v.name] = {\
  5467. \009\009\009xPos = 2+(6*(v.xPos-1)),\
  5468. \009\009\009yPos = 1+(5*(v.yPos-1)),\
  5469. \009\009\009name = v.name,\
  5470. \009\009\009path = v.path..\".ico\",\
  5471. \009\009\009yLength = 4,\
  5472. \009\009\009xLength = 3,\
  5473. \009\009\009returnValue = v.name,\
  5474. \009\009\009label = string.sub(v.name,1,5),\
  5475. \009\009\009labelFg = colors.black,\
  5476. \009\009\009labelBg = colors.white,\
  5477. \009\009\009moveY = true,\
  5478. \009\009}\
  5479. \009\009paths[v.name] = v.path\
  5480. \009end\
  5481. \009MainTable.BetterPaint = nBuffer\
  5482. end\
  5483. \
  5484. loadShortcuts()\
  5485. \
  5486. return MainTable",
  5487.           [ "Select.layout" ] = "local tArgs = {...}\
  5488. local w,h = term.getSize()\
  5489. local tReturnTable = {\
  5490. \009Button = {},\
  5491. \009Text = {\
  5492. \009\009Title = {\
  5493. \009\009\009name = \"Title\",\
  5494. \009\009\009text = \"Select\",\
  5495. \009\009\009xPos = 1,\
  5496. \009\009\009yPos = 1,\
  5497. \009\009\009bgColor = colors.gray,\
  5498. \009\009\009fgColor = colors.white,\
  5499. \009\009},\
  5500. \009}\
  5501. }",
  5502.           [ "Desktop.layout" ] = "local w,h = term.getSize()\
  5503. MainTable = {\
  5504. \009ColorField = {\
  5505. \009\009Top = {\
  5506. \009\009\009name = \"Top\",\
  5507. \009\009\009xPos = 1,\
  5508. \009\009\009yPos = 1,\
  5509. \009\009\009xLength = w,\
  5510. \009\009\009yLength = 3,\
  5511. \009\009\009color = colors.orange,\
  5512. \009\009\009\
  5513. \009\009},\
  5514. \009},\
  5515. \009Button = {\
  5516. \009\009Done = {\
  5517. \009\009\009name = \"Done\",\
  5518. \009\009\009label = \"Done\",\
  5519. \009\009\009xPos = w-6,\
  5520. \009\009\009yPos = h-3,\
  5521. \009\009\009fgColor = colors.white,\
  5522. \009\009\009bgColor = colors.green,\
  5523. \009\009\009xLength = 6,\
  5524. \009\009\009yLength = 3,\
  5525. \009\009\009returnValue = \"Done\",\
  5526. \009\009\009xTextPos = 2,\
  5527. \009\009\009yTextPos = 2,\
  5528. \009\009},\
  5529. \009\009Shortcuts = {\
  5530. \009\009\009name = \"Shortcuts\",\
  5531. \009\009\009label = \"Shortcuts\",\
  5532. \009\009\009xPos = 3,\
  5533. \009\009\009yPos = h-3,\
  5534. \009\009\009fgColor = colors.white,\
  5535. \009\009\009bgColor = colors.cyan,\
  5536. \009\009\009xLength = 11,\
  5537. \009\009\009yLength = 3,\
  5538. \009\009\009returnValue = \"Shortcuts\",\
  5539. \009\009\009xTextPos = 2,\
  5540. \009\009\009yTextPos = 2,\
  5541. \009\009},\
  5542. \009\009\009Green = {\
  5543. \009\009\009name = \"Green\",\
  5544. \009\009\009label = \" \",\
  5545. \009\009\009xPos = w-3,\
  5546. \009\009\009yPos = 5,\
  5547. \009\009\009xLength = 1,\
  5548. \009\009\009yLength = 1,\
  5549. \009\009\009xTextPos = 1,\
  5550. \009\009\009yTextPos = 1,\
  5551. \009\009\009fgColor = 1,\
  5552. \009\009\009bgColor = colors.green,\
  5553. \009\009\009returnValue = \"color:green\",\
  5554. \009\009},\
  5555. \009\009Lime = {\
  5556. \009\009\009name = \"Lime\",\
  5557. \009\009\009label = \" \",\
  5558. \009\009\009xPos = w-4,\
  5559. \009\009\009yPos = 5,\
  5560. \009\009\009xLength = 1,\
  5561. \009\009\009yLength = 1,\
  5562. \009\009\009xTextPos = 1,\
  5563. \009\009\009yTextPos = 1,\
  5564. \009\009\009fgColor = 1,\
  5565. \009\009\009bgColor = colors.lime,\
  5566. \009\009\009returnValue = \"color:lime\",\
  5567. \009\009},\
  5568. \009\009Brown = {\
  5569. \009\009\009name = \"Brown\",\
  5570. \009\009\009label = \" \",\
  5571. \009\009\009xPos = w-5,\
  5572. \009\009\009yPos = 5,\
  5573. \009\009\009xLength = 1,\
  5574. \009\009\009yLength = 1,\
  5575. \009\009\009xTextPos = 1,\
  5576. \009\009\009yTextPos = 1,\
  5577. \009\009\009fgColor = 1,\
  5578. \009\009\009bgColor = colors.brown,\
  5579. \009\009\009returnValue = \"color:brown\",\
  5580. \009\009},\
  5581. \009\009Purple = {\
  5582. \009\009\009name = \"Purple\",\
  5583. \009\009\009label = \" \",\
  5584. \009\009\009xPos = w-6,\
  5585. \009\009\009yPos = 5,\
  5586. \009\009\009xLength = 1,\
  5587. \009\009\009yLength = 1,\
  5588. \009\009\009xTextPos = 1,\
  5589. \009\009\009yTextPos = 1,\
  5590. \009\009\009fgColor = 1,\
  5591. \009\009\009bgColor = colors.purple,\
  5592. \009\009\009returnValue = \"color:purple\",\
  5593. \009\009},\
  5594. \009\009Blue = {\
  5595. \009\009\009name = \"Blue\",\
  5596. \009\009\009label = \" \",\
  5597. \009\009\009xPos = w-7,\
  5598. \009\009\009yPos = 5,\
  5599. \009\009\009xLength = 1,\
  5600. \009\009\009yLength = 1,\
  5601. \009\009\009xTextPos = 1,\
  5602. \009\009\009yTextPos = 1,\
  5603. \009\009\009fgColor = 1,\
  5604. \009\009\009bgColor = colors.blue,\
  5605. \009\009\009returnValue = \"color:blue\",\
  5606. \009\009},\
  5607. \009\009lightBlue = {\
  5608. \009\009\009name = \"lightBlue\",\
  5609. \009\009\009label = \" \",\
  5610. \009\009\009xPos = w-8,\
  5611. \009\009\009yPos = 5,\
  5612. \009\009\009xLength = 1,\
  5613. \009\009\009yLength = 1,\
  5614. \009\009\009xTextPos = 1,\
  5615. \009\009\009yTextPos = 1,\
  5616. \009\009\009fgColor = 1,\
  5617. \009\009\009bgColor = colors.lightBlue,\
  5618. \009\009\009returnValue = \"color:lightBlue\",\
  5619. \009\009},\
  5620. \009\009Yellow = {\
  5621. \009\009\009name = \"Yellow\",\
  5622. \009\009\009label = \" \",\
  5623. \009\009\009xPos = w-9,\
  5624. \009\009\009yPos = 5,\
  5625. \009\009\009xLength = 1,\
  5626. \009\009\009yLength = 1,\
  5627. \009\009\009xTextPos = 1,\
  5628. \009\009\009yTextPos = 1,\
  5629. \009\009\009fgColor = 1,\
  5630. \009\009\009bgColor = colors.yellow,\
  5631. \009\009\009returnValue = \"color:yellow\",\
  5632. \009\009},\
  5633. \009\009Cyan = {\
  5634. \009\009\009name = \"Cyan\",\
  5635. \009\009\009label = \" \",\
  5636. \009\009\009xPos = w-10,\
  5637. \009\009\009yPos = 5,\
  5638. \009\009\009xLength = 1,\
  5639. \009\009\009yLength = 1,\
  5640. \009\009\009xTextPos = 1,\
  5641. \009\009\009yTextPos = 1,\
  5642. \009\009\009fgColor = 1,\
  5643. \009\009\009bgColor = colors.cyan,\
  5644. \009\009\009returnValue = \"color:cyan\",\
  5645. \009\009},\
  5646. \009\009Orange = {\
  5647. \009\009\009name = \"Orange\",\
  5648. \009\009\009label = \" \",\
  5649. \009\009\009xPos = w-11,\
  5650. \009\009\009yPos = 5,\
  5651. \009\009\009xLength = 1,\
  5652. \009\009\009yLength = 1,\
  5653. \009\009\009xTextPos = 1,\
  5654. \009\009\009yTextPos = 1,\
  5655. \009\009\009fgColor = 1,\
  5656. \009\009\009bgColor = colors.orange,\
  5657. \009\009\009returnValue = \"color:orange\",\
  5658. \009\009},\
  5659. \009\009Pink = {\
  5660. \009\009\009name = \"Pink\",\
  5661. \009\009\009label = \" \",\
  5662. \009\009\009xPos = w-12,\
  5663. \009\009\009yPos = 5,\
  5664. \009\009\009xLength = 1,\
  5665. \009\009\009yLength = 1,\
  5666. \009\009\009xTextPos = 1,\
  5667. \009\009\009yTextPos = 1,\
  5668. \009\009\009fgColor = 1,\
  5669. \009\009\009bgColor = colors.pink,\
  5670. \009\009\009returnValue = \"color:pink\",\
  5671. \009\009},\
  5672. \009\009Red = {\
  5673. \009\009\009name = \"Red\",\
  5674. \009\009\009label = \" \",\
  5675. \009\009\009xPos = w-13,\
  5676. \009\009\009yPos = 5,\
  5677. \009\009\009xLength = 1,\
  5678. \009\009\009yLength = 1,\
  5679. \009\009\009xTextPos = 1,\
  5680. \009\009\009yTextPos = 1,\
  5681. \009\009\009fgColor = 1,\
  5682. \009\009\009bgColor = colors.red,\
  5683. \009\009\009returnValue = \"color:red\",\
  5684. \009\009},\
  5685. \009\009Magenta = {\
  5686. \009\009\009name = \"Magenta\",\
  5687. \009\009\009label = \" \",\
  5688. \009\009\009xPos = w-14,\
  5689. \009\009\009yPos = 5,\
  5690. \009\009\009xLength = 1,\
  5691. \009\009\009yLength = 1,\
  5692. \009\009\009xTextPos = 1,\
  5693. \009\009\009yTextPos = 1,\
  5694. \009\009\009fgColor = 1,\
  5695. \009\009\009bgColor = colors.magenta,\
  5696. \009\009\009returnValue = \"color:magenta\",\
  5697. \009\009},\
  5698. \009\009White = {\
  5699. \009\009\009name = \"White\",\
  5700. \009\009\009label = \" \",\
  5701. \009\009\009xPos = w-15,\
  5702. \009\009\009yPos = 5,\
  5703. \009\009\009xLength = 1,\
  5704. \009\009\009yLength = 1,\
  5705. \009\009\009xTextPos = 1,\
  5706. \009\009\009yTextPos = 1,\
  5707. \009\009\009fgColor = 1,\
  5708. \009\009\009bgColor = colors.white,\
  5709. \009\009\009returnValue = \"color:white\",\
  5710. \009\009},\
  5711. \009\009lightGray = {\
  5712. \009\009\009name = \"lightGray\",\
  5713. \009\009\009label = \" \",\
  5714. \009\009\009xPos = w-16,\
  5715. \009\009\009yPos = 5,\
  5716. \009\009\009xLength = 1,\
  5717. \009\009\009yLength = 1,\
  5718. \009\009\009xTextPos = 1,\
  5719. \009\009\009yTextPos = 1,\
  5720. \009\009\009fgColor = 1,\
  5721. \009\009\009bgColor = colors.lightGray,\
  5722. \009\009\009returnValue = \"color:lightGray\",\
  5723. \009\009},\
  5724. \009\009Black = {\
  5725. \009\009\009name = \"Black\",\
  5726. \009\009\009label = \" \",\
  5727. \009\009\009xPos = w-17,\
  5728. \009\009\009yPos = 5,\
  5729. \009\009\009xLength = 1,\
  5730. \009\009\009yLength = 1,\
  5731. \009\009\009xTextPos = 1,\
  5732. \009\009\009yTextPos = 1,\
  5733. \009\009\009fgColor = 1,\
  5734. \009\009\009bgColor = colors.black,\
  5735. \009\009\009returnValue = \"color:black\",\
  5736. \009\009},\
  5737. \009\009Gray = {\
  5738. \009\009\009name = \"Gray\",\
  5739. \009\009\009label = \" \",\
  5740. \009\009\009xPos = w-18,\
  5741. \009\009\009yPos = 5,\
  5742. \009\009\009xLength = 1,\
  5743. \009\009\009yLength = 1,\
  5744. \009\009\009xTextPos = 1,\
  5745. \009\009\009yTextPos = 1,\
  5746. \009\009\009fgColor = 1,\
  5747. \009\009\009bgColor = colors.gray,\
  5748. \009\009\009returnValue = \"color:gray\",\
  5749. \009\009},\
  5750. \009},\
  5751. \009Text = {\
  5752. \009\009Color = {\
  5753. \009\009\009name = \"Color\",\
  5754. \009\009\009text = \"Desktop Color\",\
  5755. \009\009\009xPos = 3,\
  5756. \009\009\009yPos = 5,\
  5757. \009\009\009bgColor = colors.white,\
  5758. \009\009\009fgColor = colors.black,\
  5759. \009\009},\
  5760. \009\009Title = {\
  5761. \009\009\009name = \"Title\",\
  5762. \009\009\009text = \"Settings/Desktop\",\
  5763. \009\009\009xPos = 2,\
  5764. \009\009\009yPos = 2,\
  5765. \009\009\009fgColor = colors.white,\
  5766. \009\009\009bgColor = colors.orange,\
  5767. \009\009},\
  5768. \009},\
  5769. }\
  5770. return MainTable",
  5771.           [ "Security.layout" ] = "local w,h = term.getSize()\
  5772. MainTable = {\
  5773. \009ColorField = {\
  5774. \009\009Top = {\
  5775. \009\009\009name = \"Top\",\
  5776. \009\009\009xPos = 1,\
  5777. \009\009\009yPos = 1,\
  5778. \009\009\009xLength = w,\
  5779. \009\009\009yLength = 3,\
  5780. \009\009\009color = colors.orange,\
  5781. \009\009\009\
  5782. \009\009},\
  5783. \009},\
  5784. \009Text = {\
  5785. \009\009Title = {\
  5786. \009\009\009name = \"Title\",\
  5787. \009\009\009text = \"Settings/Security\",\
  5788. \009\009\009xPos = 2,\
  5789. \009\009\009yPos = 2,\
  5790. \009\009\009fgColor = colors.white,\
  5791. \009\009\009bgColor = colors.orange,\
  5792. \009\009},\
  5793. \009\009Password = {\
  5794. \009\009\009name = \"Password\",\
  5795. \009\009\009text = \"Password\",\
  5796. \009\009\009xPos = 2,\
  5797. \009\009\009yPos = 5,\
  5798. \009\009\009fgColor = colors.black,\
  5799. \009\009\009bgColor = colors.white,\
  5800. \009\009},\
  5801. \009},\
  5802. \009TextBox = {\
  5803. \009\009Password = {\
  5804. \009\009\009name = \"Password\",\
  5805. \009\009\009helpText = \"Change password...\",\
  5806. \009\009\009xPos = 20,\
  5807. \009\009\009yPos = 5,\
  5808. \009\009\009xLength = 30,\
  5809. \009\009\009yLength = 1,\
  5810. \009\009\009bgColor = colors.lightGray,\
  5811. \009\009\009fgColor = colors.black,\
  5812. \009\009\009helpFgColor = colors.gray,\
  5813. \009\009\009charLimit = 4,\
  5814. \009\009\009confidential = true,\
  5815. \009\009\009returnValue = \"Password\",\
  5816. \009\009}\
  5817. \009},\
  5818. \009Button = {\
  5819. \009\009Done = {\
  5820. \009\009\009name = \"Done\",\
  5821. \009\009\009label = \"Done\",\
  5822. \009\009\009xPos = w-6,\
  5823. \009\009\009yPos = h-3,\
  5824. \009\009\009fgColor = colors.white,\
  5825. \009\009\009bgColor = colors.green,\
  5826. \009\009\009xLength = 6,\
  5827. \009\009\009yLength = 3,\
  5828. \009\009\009returnValue = \"Done\",\
  5829. \009\009\009xTextPos = 2,\
  5830. \009\009\009yTextPos = 2,\
  5831. \009\009},\
  5832. \009},\
  5833. }\
  5834. return MainTable",
  5835.           [ "Main.layout" ] = "local w,h = term.getSize()\
  5836. MainTable = {\
  5837. \009ColorField = {\
  5838. \009\009Top = {\
  5839. \009\009\009name = \"Top\",\
  5840. \009\009\009xPos = 1,\
  5841. \009\009\009yPos = 1,\
  5842. \009\009\009xLength = w,\
  5843. \009\009\009yLength = 3,\
  5844. \009\009\009color = colors.orange,\
  5845. \009\009},\
  5846. \009},\
  5847. \009Button = {\
  5848. \009\009Desktop = {\
  5849. \009\009\009name = \"Desktop\",\
  5850. \009\009\009label = \"Desktop\",\
  5851. \009\009\009xPos = 2,\
  5852. \009\009\009yPos = 5,\
  5853. \009\009\009fgColor = colors.white,\
  5854. \009\009\009bgColor = colors.blue,\
  5855. \009\009\009xLength = 11,\
  5856. \009\009\009yLength = 3,\
  5857. \009\009\009returnValue = \"Desktop\",\
  5858. \009\009\009xTextPos = 2,\
  5859. \009\009\009yTextPos = 2,\
  5860. \009\009},\
  5861. \009\009Security = {\
  5862. \009\009\009name = \"Security\",\
  5863. \009\009\009label = \"Security\",\
  5864. \009\009\009xPos = 2,\
  5865. \009\009\009yPos = 9,\
  5866. \009\009\009fgColor = colors.white,\
  5867. \009\009\009bgColor = colors.green,\
  5868. \009\009\009xLength = 11,\
  5869. \009\009\009yLength = 3,\
  5870. \009\009\009returnValue = \"Security\",\
  5871. \009\009\009xTextPos = 2,\
  5872. \009\009\009yTextPos = 2,\
  5873. \009\009},\
  5874. \009\009Close = {\
  5875. \009\009\009name = \"Close\",\
  5876. \009\009\009label = \"x\",\
  5877. \009\009\009xPos = w-2,\
  5878. \009\009\009yPos = 2,\
  5879. \009\009\009fgColor = colors.white,\
  5880. \009\009\009bgColor = colors.orange,\
  5881. \009\009\009xLength = 1,\
  5882. \009\009\009yLength = 1,\
  5883. \009\009\009returnValue = \"Close\",\
  5884. \009\009\009xTextPos = 1,\
  5885. \009\009\009yTextPos = 1,\
  5886. \009\009},\
  5887. \009},\
  5888. \009Text = {\
  5889. \009\009Title = {\
  5890. \009\009\009name = \"Title\",\
  5891. \009\009\009text = \"TheOS Settings\",\
  5892. \009\009\009xPos = 2,\
  5893. \009\009\009yPos = 2,\
  5894. \009\009\009fgColor = colors.white,\
  5895. \009\009\009bgColor = colors.orange,\
  5896. \009\009},\
  5897. \009},\
  5898. }\
  5899. return MainTable",
  5900.         },
  5901.       },
  5902.     },
  5903.     BetterPaint = {
  5904.       [ "BetterPaint.ico" ] = "error('This is an image, not a program!')\
  5905. {\
  5906.  {\
  5907.    {\
  5908.      1,\
  5909.      \"'\",\
  5910.      256,\
  5911.    },\
  5912.    {\
  5913.      8,\
  5914.      \" \",\
  5915.      8,\
  5916.    },\
  5917.    {\
  5918.      32,\
  5919.      \"w\",\
  5920.      8192,\
  5921.    },\
  5922.  },\
  5923.  {\
  5924.    {\
  5925.      8,\
  5926.      \" \",\
  5927.      8,\
  5928.    },\
  5929.    {\
  5930.      8,\
  5931.      \" \",\
  5932.      8,\
  5933.    },\
  5934.    {\
  5935.      32,\
  5936.      \"w\",\
  5937.      8192,\
  5938.    },\
  5939.  },\
  5940.  {\
  5941.    {\
  5942.      8,\
  5943.      \"@\",\
  5944.      16,\
  5945.    },\
  5946.    {\
  5947.      8,\
  5948.      \" \",\
  5949.      8,\
  5950.    },\
  5951.    {\
  5952.      32,\
  5953.      \"w\",\
  5954.      8192,\
  5955.    },\
  5956.  },\
  5957.  [ 0 ] = {\
  5958.    {\
  5959.      1,\
  5960.      \"'\",\
  5961.      256,\
  5962.    },\
  5963.    {\
  5964.      8,\
  5965.      \" \",\
  5966.      8,\
  5967.    },\
  5968.    {\
  5969.      32,\
  5970.      \"w\",\
  5971.      8192,\
  5972.    },\
  5973.  },\
  5974. }",
  5975.       [ "main.lua" ] = "transparentIcon = \"-\"\
  5976. args = {...}\
  5977. tX, tY = term.getSize()\
  5978. \
  5979. function drawImage(file,xSet,ySet,redirection)\
  5980. init()\
  5981. lastImage = file\
  5982. lastX = xSet\
  5983. lastY = ySet\
  5984. lastRedirection = redirection\
  5985. if redirection then\
  5986.  current = term.current()\
  5987.  term.redirect(redirection)\
  5988. end\
  5989. drawData(xSet,ySet,file)\
  5990. if redirection then\
  5991.  term.redirect(current)\
  5992. end\
  5993. end\
  5994. \
  5995. function overWrite(textSet,xSet,ySet,colorSet)\
  5996. init()\
  5997. exists = true\
  5998. if not lastImage then\
  5999.  error(\"Use drawImage first!\")\
  6000. end\
  6001. if not writeBuffer then\
  6002.  writeBuffer = {}\
  6003. end\
  6004. if not writeBuffer[lastImage] then\
  6005.  writeBuffer[lastImage] = {}\
  6006. end\
  6007. plusPos = 0\
  6008. for char in string.gmatch(textSet,\".\") do\
  6009.  if not writeBuffer[lastImage][xSet+plusPos] then\
  6010.   writeBuffer[lastImage][xSet+plusPos] = {}\
  6011.  end\
  6012.  if not writeBuffer[lastImage][xSet+plusPos][ySet] then\
  6013.   writeBuffer[lastImage][xSet+plusPos][ySet] = {colors.black,\" \",colors.white}\
  6014.  end\
  6015.  writeBuffer[lastImage][xSet+plusPos][ySet][2] = char\
  6016.  writeBuffer[lastImage][xSet+plusPos][ySet][3] = colorSet\
  6017.  plusPos = plusPos + 1 \
  6018. end\
  6019. drawImage(lastImage,lastX,lastY,lastRedirection)\
  6020. end\
  6021. \
  6022. function init()\
  6023. function eventHandler()\
  6024. while true do\
  6025.  event = {os.pullEvent()}\
  6026.  if event[1] == \"key\" then\
  6027.   if event[2] == keys.leftCtrl or event[2] == 157 then\
  6028.    menuStatus = not menuStatus\
  6029.    writeMenuBar(menuStatus)\
  6030.   end\
  6031.   if menuStatus == true then\
  6032.    if event[2] == keys.right or event[2] == keys.left then\
  6033.     if menuItemSelected == 1 then\
  6034.      menuItemSelected = 2\
  6035.     else\
  6036.      menuItemSelected = 1\
  6037.     end\
  6038.     writeMenuBar(menuStatus)\
  6039.    elseif event[2] == keys.enter then\
  6040.     if menuItemSelected == 1 then\
  6041.      save()\
  6042.      writeMenuBar(false)\
  6043.     else\
  6044.      term.setTextColor(colors.yellow)\
  6045.      term.setBackgroundColor(colors.black)\
  6046.      term.clear()\
  6047.      term.setCursorPos(1,1)\
  6048.      error()\
  6049.     end\
  6050.    end\
  6051.   else   \
  6052.    if event[2] == keys.right then\
  6053.     drawData(offSetX-1,offSetY)\
  6054.     drawMenu()\
  6055.     writeMenuBar(menuStatus)\
  6056.    elseif event[2] == keys.left then\
  6057.     drawData(offSetX+1,offSetY)\
  6058.     drawMenu()\
  6059.     writeMenuBar(menuStatus)\
  6060.    elseif event[2] == keys.up then\
  6061.     drawData(offSetX,offSetY+1)\
  6062.     drawMenu()\
  6063.     writeMenuBar(menuStatus)\
  6064.    elseif event[2] == keys.down then\
  6065.     drawData(offSetX,offSetY-1)\
  6066.     drawMenu()\
  6067.     writeMenuBar(menuStatus)\
  6068.    end\
  6069.   end\
  6070.  end\
  6071.  if event[1] == \"mouse_click\" or event[1] == \"mouse_drag\" then\
  6072.   if event[3] > 2 and event[4] ~= tY then\
  6073.    insertItem(event[3],event[4],event[2])\
  6074.   elseif event[4] < 18 and event[4] > 1 then\
  6075.    if event[3] == 1 then\
  6076.     bgSelected = 2^(event[4]-2)\
  6077.    elseif event[3] == 2 then\
  6078.     tSelected = 2^(event[4]-2)\
  6079.    end\
  6080.    drawMenu()\
  6081.   elseif event[4] == tY - 1 and event[3] == 1 then\
  6082.    setLetter()\
  6083.    drawData(offSetX,offSetY)\
  6084.    drawMenu()\
  6085.   elseif event[3] == tX and event[4] == tY and menuStatus == false then\
  6086.    writeHelp()\
  6087.   end\
  6088.  end\
  6089.  if event[1] == \"char\" then\
  6090.   textSelected = string.sub(event[2],1,1)\
  6091.   drawMenu()\
  6092.  end\
  6093.  --drawData(offSetX,offSetY)\
  6094. end\
  6095. end\
  6096. \
  6097. function writeHelp()\
  6098. term.setBackgroundColor(colors.black)\
  6099. term.setTextColor(colors.green)\
  6100. term.clear()\
  6101. term.setCursorPos(1,1)\
  6102. term.write(\"Help:\")\
  6103. term.setTextColor(colors.white)\
  6104. term.setCursorPos(1,3)\
  6105. print(\"Usage:\")\
  6106. term.setTextColor(colors.lightGray)\
  6107. print(\"  Select color: Click on the color on the left\")\
  6108. print(\"  Change draw char: Press a key on the keyboard\")\
  6109. print(\"  Change transparent icon: Click on the icon's char in the menu\")\
  6110. print(\"  Change text color: Click on a color in the menu on the right side\")\
  6111. print(\"  Change background color: Click on a color in the menu on the left side\")\
  6112. term.setTextColor(colors.white)\
  6113. print()\
  6114. print(\"Controls:\")\
  6115. term.setTextColor(colors.lightGray)\
  6116. print(\"  Arrow keys to pan\")\
  6117. print(\"  Left mouse button to select and draw\")\
  6118. print(\"  Right mouse button to delete\")\
  6119. print(\"  Ctrl to open menu\")\
  6120. print()\
  6121. term.setTextColor(colors.white)\
  6122. term.write(\"Click a mouse button to exit.\")\
  6123. term.setTextColor(colors.orange)\
  6124. term.setCursorPos(tX-9,1)\
  6125. term.write(\"API help >\")\
  6126. event = {os.pullEvent(\"mouse_click\")}\
  6127. if event[3] > tX-10 and event[4] == 1 then\
  6128.  drawAPIhelp()\
  6129. end\
  6130. drawData(offSetX,offSetY)\
  6131. drawMenu()\
  6132. writeMenuBar(menuStatus)\
  6133. end\
  6134. \
  6135. function drawAPIhelp()\
  6136. term.clear()\
  6137. term.setCursorPos(1,1)\
  6138. term.setTextColor(colors.orange)\
  6139. print(\"API help menu:\")\
  6140. print()\
  6141. term.setTextColor(colors.white)\
  6142. print(\"Drawing an image: \")\
  6143. term.setTextColor(colors.lightGray)\
  6144. print(shell.getRunningProgram(),\".drawImage(<file name>,<x pos>,<y pos>,[redirection object])\")\
  6145. print()\
  6146. term.setTextColor(colors.white)\
  6147. print(\"Overlaying text on the last image:\")\
  6148. term.setTextColor(colors.lightGray)\
  6149. print(shell.getRunningProgram(),\".overWrite(<Text>,<x pos>,<y pos>,<text color>\")\
  6150. print()\
  6151. term.setTextColor(colors.red)\
  6152. print(\"Overwriting text will only work AFTER drawing an image!\")\
  6153. term.setTextColor(colors.white)\
  6154. print()\
  6155. print(\"Example:\")\
  6156. term.setTextColor(colors.lightGray)\
  6157. print(\"os.loadAPI(\\\"\",shell.getRunningProgram(),\"\\\")\")\
  6158. print(shell.getRunningProgram(),\".drawImage(\\\"myPicture\\\",1,1)\")\
  6159. print(shell.getRunningProgram(),\".overWrite(\\\"Hello!\\\",2,3,colors.orange)\")\
  6160. os.pullEvent(\"mouse_click\")\
  6161. end\
  6162. \
  6163. function setLetter()\
  6164. term.setBackgroundColor(colors.red)\
  6165. term.setTextColor(colors.black)\
  6166. for i=1,4 do\
  6167.  term.setCursorPos(tX/2-11,(tY/2-4)+i)\
  6168.  term.write(\"                     \")\
  6169. end\
  6170. term.setCursorPos(tX/2-10,tY/2-2)\
  6171. term.write(\"Change transparancy\")\
  6172. term.setCursorPos(tX/2-10,tY/2-1)\
  6173. term.write(\"character to: (key)\")\
  6174. event = {os.pullEvent(\"char\")}\
  6175. transparentIcon = event[2]\
  6176. end\
  6177. \
  6178. function insertItem(xPos,yPos,modeSet)\
  6179. if saved == true then\
  6180.  saved = false\
  6181.  writeMenuBar(false)\
  6182. end\
  6183. --bgSelected\
  6184. --tSelected\
  6185. --textSelected\
  6186. if not painted then\
  6187.  painted = {}\
  6188. end\
  6189. if not painted[xPos-offSetX] then\
  6190.  painted[xPos-offSetX] = {}\
  6191. end\
  6192. if modeSet == 1 then\
  6193.   if not textSelected then\
  6194.    textSelected = \" \"\
  6195.   end\
  6196.   TMPtextSelected = textSelected\
  6197.   painted[xPos-offSetX][yPos-offSetY] = {bgSelected,textSelected,tSelected}\
  6198.   term.setBackgroundColor(bgSelected)\
  6199.   term.setTextColor(tSelected)\
  6200.  else\
  6201.   TMPtextSelected = transparentIcon\
  6202.   term.setBackgroundColor(colors.black)\
  6203.   term.setTextColor(colors.gray)\
  6204.   painted[xPos-offSetX][yPos-offSetY] = nil\
  6205. end\
  6206. term.setCursorPos(xPos,yPos)\
  6207. term.write(TMPtextSelected)\
  6208. end\
  6209. \
  6210. --if #args ~= 1 then\
  6211. -- print(\"Usage: \"..shell.getRunningProgram()..\" <path>\")\
  6212. -- return\
  6213. --end\
  6214. \
  6215. if args[1] and fs.exists(args[1]) == true then\
  6216. buff = fs.open(args[1],\"r\")\
  6217. previousData = buff.readAll()\
  6218. buff.close()\
  6219. processed = string.sub(previousData,43)\
  6220. painted = textutils.unserialize(processed)\
  6221. else\
  6222. painted = {}\
  6223. end\
  6224. \
  6225. function save()\
  6226. file = fs.open(args[1],\"w\")\
  6227. file.write(\"error('This is an image, not a program!')\\n\"..textutils.serialize(painted))\
  6228. file.close()\
  6229. saved = true\
  6230. end\
  6231. \
  6232. function drawData(xStart, yStart, file)\
  6233. offSetX = xStart\
  6234. offSetY = yStart\
  6235. if not file then\
  6236.  term.setBackgroundColor(colors.black)\
  6237.  term.setTextColor(colors.gray)\
  6238.  transparentLine = \"\"\
  6239.  for x=1,tX-2 do\
  6240.   transparentLine = transparentIcon..transparentLine\
  6241.  end\
  6242.  for y=1,tY-1 do\
  6243.   term.setCursorPos(3,y)\
  6244.   term.write(transparentLine)\
  6245.  end\
  6246. else\
  6247.  if fs.exists(file) == false then\
  6248.   error(\"File given doesnt exists! file name: \"..file)\
  6249.  else\
  6250.   local fileD = fs.open(file,\"r\")\
  6251.   raw = fileD.readAll()\
  6252.   --ignoreL = string.len(fileD.readLine(1))\
  6253.   processed = string.sub(raw,43)\
  6254.   --term.redirect(term.native())\
  6255.  -- textutils.pagedPrint(processed)\
  6256.   painted = textutils.unserialize(processed)\
  6257.   fileD.close()\
  6258.  end\
  6259. end\
  6260. if not painted then\
  6261.  painted = {}\
  6262. end\
  6263. paintedF = painted\
  6264. count = 0\
  6265. repeat ---------\
  6266. count = count + 1\
  6267. for xPos,v in pairs(paintedF) do\
  6268.  for yPos in pairs (paintedF[xPos]) do\
  6269.    overWrite = true\
  6270.    if not lastImage or not writeBuffer or not writeBuffer[lastImage] then\
  6271.     overWrite = false\
  6272.    end\
  6273.    if overWrite == true then\
  6274.     if not writeBuffer[lastImage][xPos] then\
  6275.      overWrite = false\
  6276.     end\
  6277.    end\
  6278.    if overWrite == true then\
  6279.     if not writeBuffer[lastImage][xPos][yPos] then\
  6280.      overWrite = false\
  6281.     end\
  6282.    end\
  6283.    if overWrite == false then\
  6284.     bgColor = paintedF[xPos][yPos][1]\
  6285.     text = paintedF[xPos][yPos][2]\
  6286.     tColor = paintedF[xPos][yPos][3]\
  6287.    else\
  6288.     if painted and painted[xPos] and painted[xPos][yPos] and painted[xPos][yPos][1] then\
  6289.      bgColor = painted[xPos][yPos][1]\
  6290.      else\
  6291.      bgColor = colors.black\
  6292.     end\
  6293.     --if not bgColor then\
  6294.    --  bgColor = colors.black\
  6295.     --end\
  6296.     text = writeBuffer[lastImage][xPos][yPos][2]\
  6297.     tColor = writeBuffer[lastImage][xPos][yPos][3]\
  6298.    end\
  6299.    if not tColor then\
  6300.     tColor = colors.white\
  6301.    end\
  6302.    if not text then\
  6303.     text = \" \"\
  6304.    end\
  6305.    term.setCursorPos(xPos+xStart,yPos+yStart)\
  6306.    term.setBackgroundColor(bgColor)\
  6307.    term.setTextColor(tColor)\
  6308.    term.write(text)\
  6309.   end\
  6310.  end\
  6311.  if count == 1 and writeBuffer and lastImage then\
  6312.   paintedF = writeBuffer[lastImage]\
  6313.  elseif count == 1 and not lastImage or not writeBuffer then\
  6314.   count = 2\
  6315.  end\
  6316. until count == 2\
  6317. term.setCursorPos(1,tY)\
  6318. end\
  6319. \
  6320. function drawMenu()\
  6321. term.setCursorPos(1,1)\
  6322. term.setTextColor(colors.white)\
  6323. if not bgSelected then\
  6324.  bgSelected = colors.black\
  6325. elseif bgSelected == colors.white then\
  6326.  term.setTextColor(colors.black)\
  6327. end\
  6328. if not tSelected then\
  6329.  tSelected = colors.white\
  6330. elseif tSelected == colors.white then\
  6331.  term.setTextColor(colors.black)\
  6332. end\
  6333. if not textSelected then\
  6334.  textSelected = \" \"\
  6335. end\
  6336. term.setBackgroundColor(bgSelected)\
  6337. term.write(\"B\")\
  6338. term.setBackgroundColor(tSelected)\
  6339. term.write(\"T\")\
  6340. for i=1,16 do\
  6341.  i=i-1\
  6342.  term.setCursorPos(1,i+2)\
  6343.  term.setBackgroundColor(2^i)\
  6344.  term.write(\"  \")\
  6345. end\
  6346. term.setCursorPos(1,18)\
  6347. term.setBackgroundColor(colors.black)\
  6348. if not textSelected then\
  6349.  textSelected = \" \"\
  6350. elseif string.len(textSelected) > 1 then\
  6351.  textSelected = string.sub(textSelected,1,1)\
  6352. end\
  6353. term.setTextColor(colors.gray)\
  6354. term.setBackgroundColor(colors.black)\
  6355. term.write(transparentIcon)\
  6356. term.setTextColor(tSelected)\
  6357. term.setBackgroundColor(bgSelected)\
  6358. term.write(textSelected)\
  6359. end\
  6360. \
  6361. function writeMenuBar(booly)\
  6362. menuStatus = booly\
  6363. term.setBackgroundColor(colors.black)\
  6364. if booly == true then\
  6365.  term.setCursorPos(1,tY)\
  6366.  term.clearLine()\
  6367.  if not menuItemSelected then\
  6368.   menuItemSelected = 1\
  6369.  end\
  6370.  term.setTextColor(colors.white)\
  6371.  term.write(\" Save  Exit \")\
  6372.  term.setCursorPos(6*menuItemSelected-5,tY)\
  6373.  term.setTextColor(colors.yellow)\
  6374.  term.write(\"[\")\
  6375.  term.setCursorPos(6*menuItemSelected,tY)\
  6376.  term.write(\"]\")\
  6377. elseif booly == false then\
  6378.  term.setCursorPos(1,tY)\
  6379.  term.setTextColor(colors.yellow)\
  6380.  term.clearLine()\
  6381.  if saved == true then\
  6382.   term.write(\"Saved to \"..args[1])\
  6383.  else\
  6384.   term.write(\"Press Ctrl to access menu\")\
  6385.  end\
  6386.  term.setCursorPos(tX,tY)\
  6387.  term.setTextColor(colors.lightGray)\
  6388.  term.write(\"?\")\
  6389. end\
  6390. end\
  6391. end\
  6392. \
  6393. if #args > 0 then\
  6394.  init()\
  6395.  menuStatus = false\
  6396.  saved = false\
  6397.  writeMenuBar(menuStatus)\
  6398.  menuItemSelected = 1\
  6399.  drawData(3,0)\
  6400.  drawMenu()\
  6401.  eventHandler()\
  6402. else\
  6403.  term.setBackgroundColor(colors.black)\
  6404.  term.setTextColor(colors.white)\
  6405.  term.clear()\
  6406.  term.setCursorPos(1,1)\
  6407.  term.write(\"Do you want to create a new file? <y/n>\")\
  6408.  local answ = read()\
  6409.  if answ:sub(1,1) == \"y\" then\
  6410.    print()\
  6411.    term.write(\"Path please!\")\
  6412.    args[1] = \"OmniOS/Data/\"..read()\
  6413.     init()\
  6414.      menuStatus = false\
  6415.      saved = false\
  6416.      writeMenuBar(menuStatus)\
  6417.      menuItemSelected = 1\
  6418.      drawData(3,0)\
  6419.      drawMenu()\
  6420.      eventHandler()\
  6421.  end\
  6422. end",
  6423.     },
  6424.   },
  6425.   [ "README.md" ] = "Install by running\
  6426. \
  6427. pastebin run 2DMDuHci",
  6428.   [ "boot.lua" ] = "os.run(_G, \"OmniOS/grub/grub.lua\")",
  6429.   dev = {},
  6430.   etc = {
  6431.     grub = {
  6432.       [ "oslist.cfg" ] = "OmniOS:OmniOS/kernel/tlco.lua\
  6433. CraftOS:OmniOS/kernel/craftos.lua\
  6434. LyqydOS:Lyqyd/Stuff",
  6435.     },
  6436.     os = {
  6437.       [ "filetype_dependencies.cfg" ] = "{\
  6438. \009txt = \"OmniOS/bin/edit.lua\",\
  6439. \009log = \"OmniOS/bin/edit.lua\",\
  6440. \009cfg = \"OmniOS/bin/edit.lua\",\
  6441. }",
  6442.       [ "aliases.cfg" ] = "{\
  6443. \009ls = \"list\",\
  6444. }",
  6445.     },
  6446.   },
  6447.   grub = {
  6448.     [ "grub.lua" ] = "--[[\
  6449. \009OS selector by Creator\
  6450. ]]--\
  6451. \
  6452. local oses = {}\
  6453. local paths = {}\
  6454. local selected = 1\
  6455. local amount\
  6456. local timer\
  6457. local timeLeft = 5\
  6458. local fromEnter = false\
  6459. \
  6460. local function loadoses()\
  6461. \009local file = fs.open(\"OmniOS/etc/grub/oslist.cfg\",\"r\")\
  6462. \009local data = file.readAll()\
  6463. \009file.close()\
  6464. \009for token in data:gmatch(\"[^\\n]+\") do\
  6465. \009\009local o, path = token:match(\"([^:]+):([^:]+)\")\
  6466. \009\009oses[selected] = o\
  6467. \009\009paths[selected] = path\
  6468. \009\009selected = selected + 1\
  6469. \009end\
  6470. \009amount = selected - 1\
  6471. \009selected = 1\
  6472. end\
  6473. \
  6474. local function draw()\
  6475. \009term.clear()\
  6476. \009term.setCursorPos(2,1)\
  6477. \009term.write(\"Please select the OS you want to boot.\")\
  6478. \009for i, v in pairs(oses) do\
  6479. \009\009term.setTextColor(colors.blue)\
  6480. \009\009term.setCursorPos(2,i+1)\
  6481. \009\009term.write(\"[ ] \"..v)\
  6482. \009end\
  6483. \009term.setCursorPos(2, #oses+2)\
  6484. \009term.setTextColor(colors.lightGray)\
  6485. \009term.write(\"For settings, press <S>\")\
  6486. \009term.setCursorPos(2, #oses+3)\
  6487. \009term.write(\"Time left: \"..tostring(timeLeft))\
  6488. end\
  6489. \
  6490. local function redraw()\
  6491. \009term.setTextColor(colors.orange)\
  6492. \009for i, v in pairs(oses) do\
  6493. \009\009term.setCursorPos(3,i+1)\
  6494. \009\009if i == selected then\
  6495. \009\009\009term.write(\"x\")\
  6496. \009\009else\
  6497. \009\009\009term.write(\" \")\
  6498. \009\009end\
  6499. \009end\
  6500. \009term.setCursorPos(2, #oses+3)\
  6501. \009term.write(\"Time left: \"..tostring(timeLeft))\
  6502. end\
  6503. \
  6504. loadoses()\
  6505. draw()\
  6506. timer = os.startTimer(1)\
  6507. \
  6508. while true do\
  6509. \009\
  6510. \009local e = {os.pullEvent()}\
  6511. \009if e[1] == \"key\" then\
  6512. \009\009if e[2] == keys.up then\
  6513. \009\009\009selected = selected - 1\
  6514. \009\009\009if selected == 0 then\
  6515. \009\009\009\009selected = amount\
  6516. \009\009\009end\
  6517. \009\009elseif e[2] == keys.down then\
  6518. \009\009\009selected = selected + 1\
  6519. \009\009\009if selected == amount + 1 then\
  6520. \009\009\009\009selected = 1\
  6521. \009\009\009end\
  6522. \009\009elseif e[2] == keys.enter then\
  6523. \009\009\009fromEnter = true\
  6524. \009\009\009break\
  6525. \009\009end\
  6526. \009elseif e[1] == \"timer\" and e[2] == timer then\
  6527. \009\009timeLeft = timeLeft - 1\
  6528. \009\009if timeLeft == 0 then\
  6529. \009\009\009break\
  6530. \009\009else\
  6531. \009\009\009timer = os.startTimer(1)\
  6532. \009\009end\
  6533. \009end\
  6534. \009redraw()\
  6535. end\
  6536. \
  6537. local function dofile(path,...)\
  6538. \009local func, err = loadfile(path)\
  6539. \009if func then\
  6540. \009\009setfenv(func,_G)\
  6541. \009\009return pcall(func, ...)\
  6542. \009else\
  6543. \009\009return false, err\
  6544. \009end\
  6545. end\
  6546. \
  6547. if fromEnter then\
  6548. \009--os.run(_G,paths[selected])\
  6549. \009print(paths[selected])\
  6550. \009ok, err = dofile(paths[selected])\
  6551. \009local file = fs.open(\"logs/boot.log\",\"a\")\
  6552. \009file.write(tostring(ok)..err..\"\\n\")\
  6553. \009file.close()\
  6554. else\
  6555. \009os.run(_G,paths[1])\
  6556. end",
  6557.     [ "BIOS.lua" ] = "--[[\
  6558. \009BIOS by Creator\
  6559. \009for OmniOS\
  6560. ]]--\
  6561. \
  6562. term.redirect(term.native())\
  6563. \
  6564. --Variables--\
  6565. local gui = {}\
  6566. local OS = {}\
  6567. local timeLeft = 5\
  6568. local currentOS = 1\
  6569. local defaultOS = \"\"\
  6570. local toBoot = 0\
  6571. local layout = [[\
  6572.     +-----------------------------------------+\
  6573.     | Current selection:                      |\
  6574.     |                                         |\
  6575.     +-----------------------------------------+\
  6576.     | Available OSes:                         |\
  6577.     |                                         |\
  6578.     |                                         |\
  6579.     |                                         |\
  6580.     |                                         |\
  6581.     |                                         |\
  6582.     |                                         |\
  6583.     +-----------------------------------------+\
  6584.     | S: Settings                             |\
  6585.     +-----------------------------------------+\
  6586.     | Time left:                              |\
  6587.     +-----------------------------------------+\
  6588. ]]\
  6589. --Functions\
  6590. function gui.clear()\
  6591. \009term.setBackgroundColor(colors.blue)\
  6592. \009term.setTextColor(colors.white)\
  6593. \009term.setCursorPos(1,2)\
  6594. \009term.clear()\
  6595. end\
  6596. \
  6597. function gui.drawMain()\
  6598. \009gui.clear()\
  6599. \009term.setCursorPos(1,3)\
  6600. \009print(layout)\
  6601. \009term.setCursorPos(8,4)\
  6602. \009term.write(OS[currentOS][1])\
  6603. \009for i = 1, #OS do\
  6604. \009\009term.setCursorPos(8,i+6)\
  6605. \009\009term.write(i..\") \"..OS[i][1])\
  6606. \009end\
  6607. \009term.setCursorPos(19,16)\
  6608. \009term.write(timeLeft)\
  6609. end\
  6610. \
  6611. local function loadOS()\
  6612. \009return dofile(\"OmniOS/BIOS/List\")\
  6613. end\
  6614. \
  6615. local function loadDefault()\
  6616. \009return dofile(\"OmniOS/BIOS/default\")\
  6617. end\
  6618. \
  6619. local function findCurrent()\
  6620. \009for i, v in pairs(OS) do\
  6621. \009\009if defaultOS == v[1] then\
  6622. \009\009\009return i\
  6623. \009\009end\
  6624. \009end\
  6625. \009error(\"The OS you are searching does not exist!\")\
  6626. end\
  6627. \
  6628. local function settings()\
  6629.  while true do\
  6630.    local char, _\
  6631.    gui.clear()\
  6632.    print(\"To add an OS, press A, to quit, press Q.\")\
  6633.    repeat\
  6634.      _, char = coroutine.yield(\"char\")\
  6635.    until char == \"a\" or char == \"q\"\
  6636.    if char == \"a\" then\
  6637.      print(\"Please write the name you wish to be displayed:\")\
  6638.      local name = read()\
  6639.      print(\"Now write the path to the file that should be run first:\")\
  6640.      local path = read()\
  6641.      print(\"If you want to confirm, write \\\"YES\\\".\")\
  6642.      local confirm = read()\
  6643.      if confirm == \"YES\" then\
  6644.        local list = dofile(\"OmniOS/BIOS/List\")\
  6645.        local free = 0\
  6646.        while true do\
  6647.          free = free + 1\
  6648.          if not list[free] then\
  6649.            break\
  6650.          end\
  6651.        end\
  6652.        list[free] = {name,path}\
  6653.        local file = fs.open(\"OmniOS/BIOS/List\",\"w\")\
  6654.        file.write(\"return \"..textutils.serialize(list))\
  6655.        file.close()\
  6656.        list = nil\
  6657.        free = nil\
  6658.      else\
  6659.        print(\"You cancelled the process!\")\
  6660.      end\
  6661.    elseif char == \"r\" then\
  6662. \
  6663.    elseif char == \"q\" then\
  6664.      break\
  6665.    end\
  6666.  end\
  6667. end\
  6668. \
  6669. --Code\
  6670. OS = loadOS()\
  6671. defaultOS = loadDefault()\
  6672. currentOS = findCurrent()\
  6673. timerID = os.startTimer(1)\
  6674. while true do\
  6675. \009gui.drawMain()\
  6676. \009local event = {os.pullEvent()}\
  6677. \009if timeLeft == 0 then\
  6678. \009\009toBoot = currentOS\
  6679. \009\009break\
  6680. \009end\
  6681. \009if event[1] == \"key\" then\
  6682. \009\009--os.cancelTimer(timerID)\
  6683. \009\009if 2 <= event[2] and event[2] <= 11 then\
  6684. \009\009\009event[2] = event[2] == 11 and 0 or event[2] - 1\
  6685. \009\009\009if OS[event[2]] then\
  6686. \009\009\009\009toBoot = event[2]\
  6687. \009\009\009\009break\
  6688. \009\009\009end\
  6689. \009\009elseif event[2] == keys.up then\
  6690. \009\009\009currentOS = currentOS - 1\
  6691. \009\009\009currentOS = currentOS == 0 and #OS or currentOS\
  6692. \009\009elseif event[2] == keys.down then\
  6693. \009\009\009currentOS = currentOS + 1\
  6694. \009\009\009currentOS = currentOS == #OS + 1 and 1 or currentOS\
  6695. \009\009elseif event[2] == keys.enter then\
  6696. \009\009\009toBoot = currentOS\
  6697. \009\009\009break\
  6698.    elseif event[2] == 31 then\
  6699.      settings()\
  6700.      timeLeft = 0\
  6701.      timerID = os.startTimer(1)\
  6702. \009\009end\
  6703. \009elseif event[1] == \"timer\" and event[2] == timerID then\
  6704. \009\009timeLeft = timeLeft - 1\
  6705.    timerID = os.startTimer(1)\
  6706. \009end\
  6707. end\
  6708. \
  6709. if OS[toBoot][1] == \"CraftOS\" then\
  6710. \009term.setBackgroundColor(colors.black)\
  6711. \009term.setTextColor(colors.yellow)\
  6712. \009term.clear()\
  6713. \009term.setCursorPos(1,1)\
  6714. \009term.write(\"CraftOS 1.7\")\
  6715. else\
  6716. \009dofile(OS[toBoot][2])\
  6717. end",
  6718.   },
  6719.   doc = {
  6720.     [ "shutdown.txt" ] = "[[shutdown]]",
  6721.     [ "pstree.txt" ] = "[[pstree]]",
  6722.     [ "G.txt" ] = "[[G]]",
  6723.     [ "launch.txt" ] = "[[launch]]",
  6724.     [ "uname.txt" ] = "[[uname]]",
  6725.     [ "removedir.txt" ] = "[[removedir]]",
  6726.     bin = {
  6727.       [ "pstree.txt" ] = "[[pstree]]",
  6728.       [ "G.txt" ] = "[[G]]",
  6729.       [ "removedir.txt" ] = "[[removedir]]",
  6730.       [ "setfs.txt" ] = "[[setfs]]",
  6731.       [ "spam.txt" ] = "[[spam]]",
  6732.       [ "who.txt" ] = "[[who]]",
  6733.       [ "grep.txt" ] = "[[grep]]",
  6734.       [ "cat.txt" ] = "[[cat]]\
  6735. \
  6736. cat reads from a file and writes the contents to the standart output. Using pipes, the content can be sent to other processes. The content is written with a single print call.",
  6737.       [ "true.txt" ] = "[[true]]",
  6738.       [ "fgrep.txt" ] = "[[fgrep]]",
  6739.       [ "man.txt" ] = "[[man]]",
  6740.       [ "clear.txt" ] = "[[clear]]",
  6741.       [ "uptime.txt" ] = "[[uptime]]",
  6742.       [ "logname.txt" ] = "[[logname]]",
  6743.       [ "setlink.txt" ] = "[[setlink]]",
  6744.       [ "remove.txt" ] = "[[remove]]",
  6745.       [ "makedir.txt" ] = "[[makedir]]",
  6746.       [ "talk.txt" ] = "[[talk]]",
  6747.       [ "ps.txt" ] = "[[ps]]",
  6748.       [ "copy.txt" ] = "[[copy]]",
  6749.       [ "sudo.txt" ] = "[[sudo]]",
  6750.       [ "kill.txt" ] = "[[kill]]",
  6751.       [ "list.txt" ] = "[[list]]",
  6752.       [ "killall.txt" ] = "[[killall]]",
  6753.       [ "false.txt" ] = "[[false]]",
  6754.       [ "link.txt" ] = "[[link]]",
  6755.       [ "shell.txt" ] = "[[shell]]",
  6756.       [ "tac.txt" ] = "[[tac]]",
  6757.       [ "login.txt" ] = "[[login]]",
  6758.       [ "getlink.txt" ] = "[[getlink]]",
  6759.       [ "find.txt" ] = "[[find]]",
  6760.       [ "event.txt" ] = "[[event]]",
  6761.       [ "pwd.txt" ] = "[[pwd]]",
  6762.       [ "id.txt" ] = "[[id]]",
  6763.       [ "whoami.txt" ] = "[[whoami]]",
  6764.       [ "pidof.txt" ] = "[[pidof]]",
  6765.       [ "users.txt" ] = "[[users]]",
  6766.       [ "ping.txt" ] = "[[ping]]",
  6767.       [ "switch.txt" ] = "[[switch]]",
  6768.       [ "getfs.txt" ] = "[[getfs]]",
  6769.       [ "echo.txt" ] = "[[echo]]",
  6770.       [ "reboot.txt" ] = "[[reboot]]",
  6771.       [ "tty.txt" ] = "[[tty]]",
  6772.       [ "cd.txt" ] = "[[cd]]\
  6773. \
  6774. cd changes the working directory of the shell. The possible inputs are /path, path, and \"..\" The first one sets it tot he absolute path, ther second appends to the current path, and the third goes up a directory.",
  6775.       [ "su.txt" ] = "[[su]]",
  6776.       [ "uname.txt" ] = "[[uname]]",
  6777.       [ "move.txt" ] = "[[move]]",
  6778.       [ "exit.txt" ] = "[[exit]]",
  6779.       [ "cksum.txt" ] = "[[cksum]]",
  6780.       [ "shutdown.txt" ] = "[[shutdown]]",
  6781.       [ "np.txt" ] = "[[np]]",
  6782.     },
  6783.     [ "setfs.txt" ] = "[[setfs]]",
  6784.     [ "0.txt" ] = "[[0]]",
  6785.     [ "spam.txt" ] = "[[spam]]",
  6786.     [ "cat.txt" ] = "[[cat]]",
  6787.     [ "edit.txt" ] = "[[edit]]",
  6788.     [ "man.txt" ] = "[[man]]",
  6789.     [ "clear.txt" ] = "[[clear]]",
  6790.     [ "makedir.txt" ] = "[[makedir]]",
  6791.     [ "logname.txt" ] = "[[logname]]",
  6792.     [ "remove.txt" ] = "[[remove]]",
  6793.     [ "true.txt" ] = "[[true]]",
  6794.     [ "setlink.txt" ] = "[[setlink]]",
  6795.     [ "uptime.txt" ] = "[[uptime]]",
  6796.     [ "who.txt" ] = "[[who]]",
  6797.     [ "ps.txt" ] = "[[ps]]",
  6798.     [ "talk.txt" ] = "[[talk]]",
  6799.     [ "copy.txt" ] = "[[copy]]",
  6800.     [ "sudo.txt" ] = "[[sudo]]",
  6801.     [ "kill.txt" ] = "[[kill]]",
  6802.     [ "list.txt" ] = "[[list]]",
  6803.     [ "killall.txt" ] = "[[killall]]",
  6804.     [ "false.txt" ] = "[[false]]",
  6805.     [ "link.txt" ] = "[[link]]",
  6806.     [ "shell.txt" ] = "[[shell]]",
  6807.     [ "crc.txt" ] = "[[crc]]",
  6808.     [ "login.txt" ] = "[[login]]",
  6809.     [ "getlink.txt" ] = "[[getlink]]",
  6810.     [ "find.txt" ] = "[[find]]",
  6811.     [ "event.txt" ] = "[[event]]",
  6812.     [ "pwd.txt" ] = "[[pwd]]",
  6813.     [ "id.txt" ] = "[[id]]",
  6814.     [ "tac.txt" ] = "[[tac]]",
  6815.     [ "pidof.txt" ] = "[[pidof]]",
  6816.     [ "users.txt" ] = "[[users]]",
  6817.     [ "ping.txt" ] = "[[ping]]",
  6818.     [ "whoami.txt" ] = "[[whoami]]",
  6819.     [ "getfs.txt" ] = "[[getfs]]",
  6820.     [ "switch.txt" ] = "[[switch]]",
  6821.     [ "echo.txt" ] = "[[echo]]",
  6822.     [ "lua.txt" ] = "[[lua]]",
  6823.     [ "reboot.txt" ] = "[[reboot]]",
  6824.     [ "tty.txt" ] = "[[tty]]",
  6825.     [ "cd.txt" ] = "[[cd]]",
  6826.     [ "su.txt" ] = "[[su]]",
  6827.     [ "exit.txt" ] = "[[exit]]",
  6828.     [ "move.txt" ] = "[[move]]",
  6829.     [ "catt.txt" ] = "[[catt]]",
  6830.     [ "args.txt" ] = "[[args]]",
  6831.     [ "cksum.txt" ] = "[[cksum]]",
  6832.     [ "np.txt" ] = "[[np]]",
  6833.   },
  6834.   [ ".gitignore" ] = "*.log\
  6835. usr\
  6836. sysset",
  6837.   bin = {
  6838.     [ "devctrl.lua" ] = "print(\"Enter address of device to control\")\
  6839. \
  6840. local path = read()\
  6841. \
  6842. local file = fs.open(path,\"w\")\
  6843. print(file)\
  6844. for i,v in pairs(file) do\
  6845. \009print(i,v)\
  6846. end\
  6847. \
  6848. term.write(\">\")\
  6849. local input = read()\
  6850. \
  6851. while input ~= \"exit\" do\
  6852. \009print(\"The command returned\", file.write(input))\
  6853. \009term.write(\">\")\
  6854. \009input = read()\
  6855. end",
  6856.     [ "tac.lua" ] = "--[[\
  6857. \009Shell script: tac by Creator\
  6858. \009for OmniOS\
  6859. ]]--\
  6860. \
  6861. local function isEmpty(str)\
  6862. \009if str == nil then\
  6863. \009\009stdlog(\"Input is nil\")\
  6864. \009\009return true\
  6865. \009end\
  6866. \009strstr = tostring(str)\
  6867. \
  6868. \009local yes = true\
  6869. \009for i=1, #strstr do\
  6870. \009\009yes = strstr:sub(i,i) == \" \" and yes \
  6871. \009end\
  6872. \009stdlog(tostring(str)..type(str)..tostring(yes))\
  6873. \009return yes\
  6874. end\
  6875. \
  6876. local path = ...\
  6877. \
  6878. local data = read()\
  6879. \
  6880. local file = fs.open(path,\"a\")\
  6881. \
  6882. while data do\
  6883. \009if isEmpty(data) then\
  6884. \009\009break\
  6885. \009end\
  6886. \009data = tostring(data)\
  6887. \009file.write(data..\"\\n\")\
  6888. \009file.flush()\
  6889. \009data = read()\
  6890. end\
  6891. data = tostring(data)\
  6892. file.write(data..\"\\n\")\
  6893. file.close()\
  6894. \
  6895. stdlog(\"Closing because no more input.\")",
  6896.     [ "ps.lua" ] = "--[[\
  6897. \009Shell script: ps by Creator\
  6898. \009for OmniOS\
  6899. ]]--\
  6900. \
  6901. l = kernel.list()\
  6902. \
  6903. for i, v in pairs(l) do\
  6904. \009print(v[1], v[2])\
  6905. end",
  6906.     [ "uname.lua" ] = "--[[\
  6907. \009Shell script: uname by Creator\
  6908. \009for OmniOS\
  6909. ]]--",
  6910.     [ "crc.lua" ] = "--[[\
  6911. \009CRC script\
  6912. ]]\
  6913. \
  6914. local function isEmpty(str)\
  6915. \009local yes = true\
  6916. \009for i=1, #str do\
  6917. \009\009yes = str:sub(i,i) == \" \" and yes \
  6918. \009end\
  6919. \009return yes\
  6920. end\
  6921. \
  6922. local data = stdin()\
  6923. \
  6924. local c = lib.crc\
  6925. \
  6926. while data do\
  6927. \009if isEmpty(data) then\
  6928. \009\009break\
  6929. \009end\
  6930. \009stdp(c(data))\
  6931. \009data = read()\
  6932. end",
  6933.     [ "G.lua" ] = "--[[\
  6934. \009Shell script: _G by Creator\
  6935. \009for OmniOS\
  6936. ]]--\
  6937. \
  6938. \
  6939. local amount = 0\
  6940. for i,v in pairs(_G) do\
  6941. \009print(i)\
  6942. \009amount = amount + 1\
  6943. \009if amount%5 == 0 then\
  6944. \009\009read()\
  6945. \009end\
  6946. end",
  6947.     [ "exit.lua" ] = "--[[\
  6948. \009Shell script: exit by Creator\
  6949. \009for OmniOS\
  6950. ]]--\
  6951. \
  6952. OmniOS.exit()",
  6953.     [ "man.lua" ] = "--[[\
  6954. \009Shell script: man by Creator\
  6955. \009for OmniOS\
  6956. ]]--",
  6957.     [ "kill.lua" ] = "--[[\
  6958. \009Shell script: kill by Creator\
  6959. \009for OmniOS\
  6960. ]]--\
  6961. \
  6962. local id = ...\
  6963. \
  6964. id = tonumber(id)\
  6965. \
  6966. if id then\
  6967. \009OmniOS.kill(id)\
  6968. \009print(\"Killed process\", id)\
  6969. else\
  6970. \009OmniOS.kill(OmniOS.getRunning())\
  6971. end",
  6972.     [ "launch.lua" ] = "--[[\
  6973. \009Shell script: man by Creator\
  6974. \009for OmniOS\
  6975. ]]--\
  6976. \
  6977. local name = ...\
  6978. \
  6979. OmniOS.launch(name, \"term\")",
  6980.     [ "pidof.lua" ] = "--[[\
  6981. \009Shell script: pidof by Creator\
  6982. \009for OmniOS\
  6983. ]]--",
  6984.     [ "makedir.lua" ] = "--[[\
  6985. \009Shell script: makedir by Creator\
  6986. \009for OmniOS\
  6987. ]]--",
  6988.     [ "crc_old.lua" ] = "--[[\
  6989. \009Shell script: crc by Creator\
  6990. \009for OmniOS\
  6991. ]]--\
  6992. \
  6993. local function isEmpty(str)\
  6994. \009local yes = true\
  6995. \009for i=1, #str do\
  6996. \009\009yes = str:sub(i,i) == \" \" and yes \
  6997. \009end\
  6998. \009return yes\
  6999. end\
  7000. \
  7001. local data = read()\
  7002. \
  7003. while data do\
  7004. \009if isEmpty(data) then\
  7005. \009\009break\
  7006. \009end\
  7007. \009print(lib.crc(data))\
  7008. \009data = read()\
  7009. end",
  7010.     [ "lua.lua" ] = "\
  7011. local tArgs = { ... }\
  7012. if #tArgs > 0 then\
  7013. \009print( \"This is an interactive Lua prompt.\" )\
  7014. \009print( \"To run a lua program, just type its name.\" )\
  7015. \009error((...))\
  7016. end\
  7017. \
  7018. local bRunning = true\
  7019. local tCommandHistory = {}\
  7020. local tEnv = {\
  7021. \009[\"exit\"] = function()\
  7022. \009\009bRunning = false\
  7023. \009end,\
  7024. \009[\"_echo\"] = function( ... )\
  7025. \009    return ...\
  7026. \009end,\
  7027. }\
  7028. setmetatable( tEnv, { __index = _ENV } )\
  7029. \
  7030. if term.isColour() then\
  7031. \009term.setTextColour( colours.yellow )\
  7032. end\
  7033. print( \"Interactive Lua prompt.\" )\
  7034. print( \"Call exit() to exit.\" )\
  7035. term.setTextColour( colours.white )\
  7036. \
  7037. while bRunning do\
  7038. \009--if term.isColour() then\
  7039. \009--\009term.setTextColour( colours.yellow )\
  7040. \009--end\
  7041. \009write( \"lua> \" )\
  7042. \009--term.setTextColour( colours.white )\
  7043. \
  7044. \009local s = read( nil, tCommandHistory, function( sLine )\
  7045. \009\009--if settings.get( \"lua.autocomplete\" ) then\
  7046. \009\009    local nStartPos = string.find( sLine, \"[a-zA-Z0-9_%.]+$\" )\
  7047. \009\009    if nStartPos then\
  7048. \009\009        sLine = string.sub( sLine, nStartPos )\
  7049. \009\009    end\
  7050. \009\009    if #sLine > 0 then\
  7051. \009            return textutils.complete( sLine, tEnv )\
  7052. \009        end\
  7053. \009\009--end\
  7054.        return nil\
  7055. \009end )\
  7056. \009table.insert( tCommandHistory, s )\
  7057. \009\
  7058. \009local nForcePrint = 0\
  7059. \009local func, e = load( s, \"lua\", \"t\", tEnv )\
  7060. \009local func2, e2 = load( \"return _echo(\"..s..\");\", \"lua\", \"t\", tEnv )\
  7061. \009if not func then\
  7062. \009\009if func2 then\
  7063. \009\009\009func = func2\
  7064. \009\009\009e = nil\
  7065. \009\009\009nForcePrint = 1\
  7066. \009\009end\
  7067. \009else\
  7068. \009\009if func2 then\
  7069. \009\009\009func = func2\
  7070. \009\009end\
  7071. \009end\
  7072. \009\
  7073. \009if func then\
  7074.        local tResults = { pcall( func ) }\
  7075.        if tResults[1] then\
  7076.        \009local n = 1\
  7077.        \009while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do\
  7078.        \009    local value = tResults[ n + 1 ]\
  7079.        \009    if type( value ) == \"table\" then\
  7080.        \009        local metatable = getmetatable( value )\
  7081.        \009        if type(metatable) == \"table\" and type(metatable.__tostring) == \"function\" then\
  7082.        \009            print( tostring( value ) )\
  7083.        \009        else\
  7084.                        local ok, serialised = pcall( textutils.serialise, value )\
  7085.                        if ok then\
  7086.                            print( serialised )\
  7087.                        else\
  7088.                            print( tostring( value ) )\
  7089.                        end\
  7090.            \009    end\
  7091.            \009else\
  7092.            \009    print( tostring( value ) )\
  7093.            \009end\
  7094.        \009\009n = n + 1\
  7095.        \009end\
  7096.        else\
  7097.        \009printError( tResults[2] )\
  7098.        end\
  7099.    else\
  7100.    \009printError( e )\
  7101.    end\
  7102.    \
  7103. end",
  7104.     [ "su.lua" ] = "--[[\
  7105. \009Shell script: su by Creator\
  7106. \009for OmniOS\
  7107. ]]--",
  7108.     [ "cd.lua" ] = "--[[\
  7109. \009Shell script: cd by Creator\
  7110. \009for OmniOS\
  7111. ]]--\
  7112. \
  7113. local nPath = ...\
  7114. \
  7115. if nPath == \"..\" then\
  7116. \009OmniOS.setPath(fs.combine(OmniOS.getPath(), \"..\"))\
  7117. else\
  7118. \009if nPath:sub(1,1) == \"/\" then\
  7119. \009\009nPath = fs.combine(nPath, \"\")\
  7120. \009\009OmniOS.setPath(nPath)\
  7121. \009else\
  7122. \009\009nPath = fs.combine(nPath, \"\")\
  7123. \009\009print(fs.combine(OmniOS.getPath(), nPath))\
  7124. \009\009OmniOS.setPath(fs.combine(OmniOS.getPath(), nPath))\
  7125. \009end\
  7126. end",
  7127.     [ "id.lua" ] = "--[[\
  7128. \009Shell script: id by Creator\
  7129. \009for OmniOS\
  7130. ]]--\
  7131. \
  7132. stdp(OmniOS.getRunning())",
  7133.     [ "event.lua" ] = "--[[\
  7134. \009Shell script: event by Creator\
  7135. \009for OmniOS\
  7136. ]]--\
  7137. \
  7138. while true do\
  7139. \009local event = {os.pullEvent()}\
  7140. \009print(unpack(event))\
  7141. \009if event[1] == \"terminate\" then\
  7142. \009\009break\
  7143. \009end\
  7144. end",
  7145.     [ "users.lua" ] = "--[[\
  7146. \009Shell script: users by Creator\
  7147. \009for OmniOS\
  7148. ]]--",
  7149.     [ "uptime.lua" ] = "--[[\
  7150. \009Shell script: uptime by Creator\
  7151. \009for OmniOS\
  7152. ]]--",
  7153.     [ "setfs.lua" ] = "--[[\
  7154. \009Shell script: setfs by Creator\
  7155. \009for OmniOS\
  7156. ]]--",
  7157.     [ "mkevent.lua" ] = "--[[\
  7158. \009Shell script: mkevent by Creator\
  7159. \009for OmniOS\
  7160. ]]--\
  7161. \
  7162. local function isEmpty(str)\
  7163. \009local yes = true\
  7164. \009for i=1, #str do\
  7165. \009\009yes = str:sub(i,i) == \" \" and yes \
  7166. \009end\
  7167. \009return yes\
  7168. end\
  7169. \
  7170. local data = stdin()\
  7171. while data do\
  7172. \009if isEmpty(data) then\
  7173. \009\009break\
  7174. \009end\
  7175. \009os.queueEvent(data, \"lol\")\
  7176. \009data = read()\
  7177. end",
  7178.     [ "catt.lua" ] = "--[[\
  7179. \009Shell script: cat by Creator\
  7180. \009for OmniOS\
  7181. \009edited by Piorjade: http://www.computercraft.info/forums2/index.php?/user/41415-piorjade/\
  7182. ]]--\
  7183. \
  7184. local path = (...) or shell.getPath()\
  7185. \
  7186. local file = fs.open(path,\"r\")\
  7187. local data = file.readAll()\
  7188. file.close()\
  7189. print(data)",
  7190.     [ "link.lua" ] = "--[[\
  7191. \009Shell script: ln by Creator\
  7192. \009for OmniOS\
  7193. ]]--",
  7194.     [ "whoami.lua" ] = "--[[\
  7195. \009Shell script: whoami by Creator\
  7196. \009for OmniOS\
  7197. ]]--",
  7198.     [ "relay.lua" ] = "print(\"Starting relay\", OmniOS.getRunning())\
  7199. \
  7200. local data\
  7201. \
  7202. while data ~= \"exit\" do\
  7203. \009data = stdin()\
  7204. \009stdlog(data)\
  7205. \009stdp(data)\
  7206. end",
  7207.     [ "setlink.lua" ] = "--[[\
  7208. \009Shell script: setlink by Creator\
  7209. \009for OmniOS\
  7210. ]]--",
  7211.     [ "find.lua" ] = "--[[\
  7212. \009Shell script: find by Creator\
  7213. \009for OmniOS\
  7214. ]]--\
  7215. \
  7216. local search = ...\
  7217. local path = OmniOS.getPath()\
  7218. \
  7219. local res = fs.find(fs.combine(path, search))\
  7220. \
  7221. for i,v in pairs(res) do\
  7222. \009if fs.isDir(path..\"/\"..v) then\
  7223. \009\009term.setTextColor(colors.green)\
  7224. \009\009print(v)\
  7225. \009else\
  7226. \009\009term.setTextColor(colors.white)\
  7227. \009\009print(v)\
  7228. \009end\
  7229. end\
  7230. \
  7231. term.setTextColor(colors.white)",
  7232.     [ "0.txt" ] = "https://www.howtoforge.com/useful_linux_commands#arch\
  7233. http://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/bin.html",
  7234.     [ "who.lua" ] = "--[[\
  7235. \009Shell script: who by Creator\
  7236. \009for OmniOS\
  7237. ]]--",
  7238.     [ "login.lua" ] = "--[[\
  7239. \009Shell script: login by Creator\
  7240. \009for OmniOS\
  7241. ]]--",
  7242.     [ "getfs.lua" ] = "--[[\
  7243. \009Shell script: getfs by Creator\
  7244. \009for OmniOS\
  7245. ]]--\
  7246. \
  7247. local path = (...) or OmniOS.getPath() \
  7248. path = OmniOS.resolvePath(path)\
  7249. \
  7250. print(fs.getfs(path))",
  7251.     [ "cat.lua" ] = "--[[\
  7252. \009Shell script: cat by Creator\
  7253. \009for OmniOS\
  7254. \009edited by Piorjade: http://www.computercraft.info/forums2/index.php?/user/41415-piorjade/\
  7255. ]]--\
  7256. \
  7257. --[[local path = ...\
  7258. \
  7259. if fs.exists(path) then\
  7260. \009local file = fs.open(path,\"r\")\
  7261. \009local data = file.readAll()\
  7262. \009file.close()\
  7263. \009print(data)\
  7264. else\
  7265. \009print(\"File does not exist.\")\
  7266. end]]--\
  7267. \
  7268. local path = (...) or shell.getPath()\
  7269. \
  7270. if fs.exists(path) and not fs.isDir(path) then\
  7271. \009local file = fs.open(path,\"r\")\
  7272. \009local data = file.readAll()\
  7273. \009file.close()\
  7274. \009--print(data)\
  7275. \009for token in data:gmatch(\"[^\\n]+\") do\
  7276. \009\009stdp(token)\
  7277. \009end\
  7278. elseif fs.isDir(path) then\
  7279. \009term.setTextColor(colors.red)\
  7280. \009print(\"Path is a folder.\")\
  7281. else\
  7282. \009print(\"File does not exist.\")\
  7283. end",
  7284.     [ "true.lua" ] = "--[[\
  7285. \009Shell script: true by Creator\
  7286. \009for OmniOS\
  7287. ]]--",
  7288.     [ "edit.lua" ] = "-- Get file to edit\
  7289. local tArgs = { ... }\
  7290. if #tArgs == 0 then\
  7291. \009print( \"Usage: edit <path>\" )\
  7292. \009return\
  7293. end\
  7294. \
  7295. -- Error checking\
  7296. local sPath\
  7297. if shell then\
  7298. \009sPath = shell.resolvePath(tArgs[1])\
  7299. else\
  7300. \009sPath = tArgs[1]\
  7301. \009shell = {}\
  7302. end\
  7303. local bReadOnly = fs.isReadOnly( sPath )\
  7304. if fs.exists( sPath ) and fs.isDir( sPath ) then\
  7305. \009print( \"Cannot edit a directory.\" )\
  7306. \009return\
  7307. end\
  7308. \
  7309. local x,y = 1,1\
  7310. local w,h = term.getSize()\
  7311. local scrollX, scrollY = 0,0\
  7312. \
  7313. local tLines = {}\
  7314. local bRunning = true\
  7315. \
  7316. -- Colours\
  7317. local highlightColour, keywordColour, commentColour, textColour, bgColour, stringColour\
  7318. if term.isColour() then\
  7319. \009bgColour = colours.black\
  7320. \009textColour = colours.white\
  7321. \009highlightColour = colours.yellow\
  7322. \009keywordColour = colours.yellow\
  7323. \009commentColour = colours.green\
  7324. \009stringColour = colours.red\
  7325. else\
  7326. \009bgColour = colours.black\
  7327. \009textColour = colours.white\
  7328. \009highlightColour = colours.white\
  7329. \009keywordColour = colours.white\
  7330. \009commentColour = colours.white\
  7331. \009stringColour = colours.white\
  7332. end\
  7333. \
  7334. -- Menus\
  7335. local bMenu = false\
  7336. local nMenuItem = 1\
  7337. local tMenuItems = {}\
  7338. if not bReadOnly then\
  7339.    table.insert( tMenuItems, \"Save\" )\
  7340. end\
  7341. if shell.openTab then\
  7342.    table.insert( tMenuItems, \"Run\" )\
  7343. end\
  7344. if peripheral.find( \"printer\" ) then\
  7345.    table.insert( tMenuItems, \"Print\" )\
  7346. end\
  7347. table.insert( tMenuItems, \"Exit\" )\
  7348. \
  7349. local sStatus = \"Press Ctrl to access menu\"\
  7350. if string.len( sStatus ) > w - 5 then\
  7351.    sStatus = \"Press Ctrl for menu\"\
  7352. end\
  7353. \
  7354. local function load( _sPath )\
  7355. \009tLines = {}\
  7356. \009if fs.exists( _sPath ) then\
  7357. \009\009local file = io.open( _sPath, \"r\" )\
  7358. \009\009local sLine = file:read()\
  7359. \009\009while sLine do\
  7360. \009\009\009table.insert( tLines, sLine )\
  7361. \009\009\009sLine = file:read()\
  7362. \009\009end\
  7363. \009\009file:close()\
  7364. \009end\
  7365. \009\
  7366. \009if #tLines == 0 then\
  7367. \009\009table.insert( tLines, \"\" )\
  7368. \009end\
  7369. end\
  7370. \
  7371. local function save( _sPath )\
  7372. \009-- Create intervening folder\
  7373. \009local sDir = _sPath:sub(1, _sPath:len() - fs.getName(_sPath):len() )\
  7374. \009if not fs.exists( sDir ) then\
  7375. \009\009fs.makeDir( sDir )\
  7376. \009end\
  7377. \
  7378. \009-- Save\
  7379. \009local file = nil\
  7380. \009local function innerSave()\
  7381. \009\009file = fs.open( _sPath, \"w\" )\
  7382. \009\009if file then\
  7383. \009\009\009for n, sLine in ipairs( tLines ) do\
  7384. \009\009\009\009file.write( sLine .. \"\\n\" )\
  7385. \009\009\009end\
  7386. \009\009else\
  7387. \009\009\009error( \"Failed to open \".._sPath )\
  7388. \009\009end\
  7389. \009end\
  7390. \009\
  7391. \009local ok, err = pcall( innerSave )\
  7392. \009if file then \
  7393. \009\009file.close()\
  7394. \009end\
  7395. \009return ok, err\
  7396. end\
  7397. \
  7398. local tKeywords = {\
  7399. \009[\"and\"] = true,\
  7400. \009[\"break\"] = true,\
  7401. \009[\"do\"] = true,\
  7402. \009[\"else\"] = true,\
  7403. \009[\"elseif\"] = true,\
  7404. \009[\"end\"] = true,\
  7405. \009[\"false\"] = true,\
  7406. \009[\"for\"] = true,\
  7407. \009[\"function\"] = true,\
  7408. \009[\"if\"] = true,\
  7409. \009[\"in\"] = true,\
  7410. \009[\"local\"] = true,\
  7411. \009[\"nil\"] = true,\
  7412. \009[\"not\"] = true,\
  7413. \009[\"or\"] = true,\
  7414. \009[\"repeat\"] = true,\
  7415. \009[\"return\"] = true,\
  7416. \009[\"then\"] = true,\
  7417. \009[\"true\"] = true,\
  7418. \009[\"until\"]= true,\
  7419. \009[\"while\"] = true,\
  7420. }\
  7421. \
  7422. local function tryWrite( sLine, regex, colour )\
  7423. \009local match = string.match( sLine, regex )\
  7424. \009if match then\
  7425. \009\009if type(colour) == \"number\" then\
  7426. \009\009\009term.setTextColour( colour )\
  7427. \009\009else\
  7428. \009\009\009term.setTextColour( colour(match) )\
  7429. \009\009end\
  7430. \009\009term.write( match )\
  7431. \009\009term.setTextColour( textColour )\
  7432. \009\009return string.sub( sLine, string.len(match) + 1 )\
  7433. \009end\
  7434. \009return nil\
  7435. end\
  7436. \
  7437. local function writeHighlighted( sLine )\
  7438. \009while string.len(sLine) > 0 do\009\
  7439. \009\009sLine = \
  7440. \009\009\009tryWrite( sLine, \"^%-%-%[%[.-%]%]\", commentColour ) or\
  7441. \009\009\009tryWrite( sLine, \"^%-%-.*\", commentColour ) or\
  7442. \009\009\009tryWrite( sLine, \"^\\\"\\\"\", stringColour ) or\
  7443. \009\009\009tryWrite( sLine, \"^\\\".-[^\\\\]\\\"\", stringColour ) or\
  7444. \009\009\009tryWrite( sLine, \"^\\'\\'\", stringColour ) or\
  7445. \009\009\009tryWrite( sLine, \"^\\'.-[^\\\\]\\'\", stringColour ) or\
  7446. \009\009\009tryWrite( sLine, \"^%[%[.-%]%]\", stringColour ) or\
  7447. \009\009\009tryWrite( sLine, \"^[%w_]+\", function( match )\
  7448. \009\009\009\009if tKeywords[ match ] then\
  7449. \009\009\009\009\009return keywordColour\
  7450. \009\009\009\009end\
  7451. \009\009\009\009return textColour\
  7452. \009\009\009end ) or\
  7453. \009\009\009tryWrite( sLine, \"^[^%w_]\", textColour )\
  7454. \009end\
  7455. end\
  7456. \
  7457. local tCompletions\
  7458. local nCompletion\
  7459. \
  7460. local tCompleteEnv = _ENV\
  7461. local function complete( sLine )\
  7462. \009if settings.get( \"edit.autocomplete\" ) then\
  7463. \009    local nStartPos = string.find( sLine, \"[a-zA-Z0-9_%.]+$\" )\
  7464. \009    if nStartPos then\
  7465. \009        sLine = string.sub( sLine, nStartPos )\
  7466. \009    end\
  7467. \009    if #sLine > 0 then\
  7468. \009        return textutils.complete( sLine, tCompleteEnv )\
  7469. \009    end\
  7470. \009end\
  7471.    return nil\
  7472. end\
  7473. \
  7474. local function recomplete()\
  7475.    local sLine = tLines[y]\
  7476.    if not bMenu and not bReadOnly and x == string.len(sLine) + 1 then\
  7477.        tCompletions = complete( sLine )\
  7478.        if tCompletions and #tCompletions > 0 then\
  7479.            nCompletion = 1\
  7480.        else\
  7481.            nCompletion = nil\
  7482.        end\
  7483.    else\
  7484.        tCompletions = nil\
  7485.        nCompletion = nil\
  7486.    end\
  7487. end\
  7488. \
  7489. local function writeCompletion( sLine )\
  7490.    if nCompletion then\
  7491.        local sCompletion = tCompletions[ nCompletion ]\
  7492.        term.setTextColor( colours.white )\
  7493.        term.setBackgroundColor( colours.grey )\
  7494.        term.write( sCompletion )\
  7495.        term.setTextColor( textColour )\
  7496.        term.setBackgroundColor( bgColour )\
  7497.    end\
  7498. end\
  7499. \
  7500. local function redrawText()\
  7501.    local cursorX, cursorY = x, y\
  7502. \009for y=1,h-1 do\
  7503. \009\009term.setCursorPos( 1 - scrollX, y )\
  7504. \009\009term.clearLine()\
  7505. \
  7506. \009\009local sLine = tLines[ y + scrollY ]\
  7507. \009\009if sLine ~= nil then\
  7508. \009\009\009writeHighlighted( sLine )\
  7509.            if cursorY == y and cursorX == #sLine + 1 then\
  7510.                writeCompletion()\
  7511.            end\
  7512. \009\009end\
  7513. \009end\
  7514. \009term.setCursorPos( x - scrollX, y - scrollY )\
  7515. end\
  7516. \
  7517. local function redrawLine(_nY)\
  7518. \009local sLine = tLines[_nY]\
  7519. \009if sLine then\
  7520.        term.setCursorPos( 1 - scrollX, _nY - scrollY )\
  7521.        term.clearLine()\
  7522.        writeHighlighted( sLine )\
  7523.        if _nY == y and x == #sLine + 1 then\
  7524.            writeCompletion()\
  7525.        end\
  7526.        term.setCursorPos( x - scrollX, _nY - scrollY )\
  7527.    end\
  7528. end\
  7529. \
  7530. local function redrawMenu()\
  7531.    -- Clear line\
  7532.    term.setCursorPos( 1, h )\
  7533. \009term.clearLine()\
  7534. \
  7535.    -- Draw line numbers\
  7536.    term.setCursorPos( w - string.len( \"Ln \"..y ) + 1, h )\
  7537.    term.setTextColour( highlightColour )\
  7538.    term.write( \"Ln \" )\
  7539.    term.setTextColour( textColour )\
  7540.    term.write( y )\
  7541. \
  7542.    term.setCursorPos( 1, h )\
  7543. \009if bMenu then\
  7544.        -- Draw menu\
  7545. \009\009term.setTextColour( textColour )\
  7546. \009\009for nItem,sItem in pairs( tMenuItems ) do\
  7547. \009\009\009if nItem == nMenuItem then\
  7548. \009\009\009\009term.setTextColour( highlightColour )\
  7549. \009\009\009\009term.write( \"[\" )\
  7550. \009\009\009\009term.setTextColour( textColour )\
  7551. \009\009\009\009term.write( sItem )\
  7552. \009\009\009\009term.setTextColour( highlightColour )\
  7553. \009\009\009\009term.write( \"]\" )\
  7554.        \009\009term.setTextColour( textColour )\
  7555. \009\009\009else\
  7556. \009\009\009\009term.write( \" \"..sItem..\" \" )\
  7557. \009\009\009end\
  7558. \009\009end\
  7559.    else\
  7560.        -- Draw status\
  7561.        term.setTextColour( highlightColour )\
  7562.        term.write( sStatus )\
  7563.        term.setTextColour( textColour )\
  7564.    end\
  7565. \
  7566. \009-- Reset cursor\
  7567. \009term.setCursorPos( x - scrollX, y - scrollY )\
  7568. end\
  7569. \
  7570. local tMenuFuncs = { \
  7571. \009Save = function()\
  7572. \009\009if bReadOnly then\
  7573. \009\009\009sStatus = \"Access denied\"\
  7574. \009\009else\
  7575. \009\009\009local ok, err = save( sPath )\
  7576. \009\009\009if ok then\
  7577. \009\009\009\009sStatus=\"Saved to \"..sPath\
  7578. \009\009\009else\
  7579. \009\009\009\009sStatus=\"Error saving to \"..sPath\
  7580. \009\009\009end\
  7581. \009\009end\
  7582. \009\009redrawMenu()\
  7583. \009end,\
  7584. \009Print = function()\
  7585. \009\009local printer = peripheral.find( \"printer\" )\
  7586. \009\009if not printer then\
  7587. \009\009\009sStatus = \"No printer attached\"\
  7588. \009\009\009return\
  7589. \009\009end\
  7590. \
  7591. \009\009local nPage = 0\
  7592. \009\009local sName = fs.getName( sPath )\
  7593. \009\009if printer.getInkLevel() < 1 then\
  7594. \009\009\009sStatus = \"Printer out of ink\"\
  7595. \009\009\009return\
  7596. \009\009elseif printer.getPaperLevel() < 1 then\
  7597. \009\009\009sStatus = \"Printer out of paper\"\
  7598. \009\009\009return\
  7599. \009\009end\
  7600. \
  7601. \009\009local screenTerminal = term.current()\
  7602. \009\009local printerTerminal = {\
  7603. \009\009\009getCursorPos = printer.getCursorPos,\
  7604. \009\009\009setCursorPos = printer.setCursorPos,\
  7605. \009\009\009getSize = printer.getPageSize,\
  7606. \009\009\009write = printer.write,\
  7607. \009\009}\
  7608. \009\009printerTerminal.scroll = function()\
  7609. \009\009\009if nPage == 1 then\
  7610. \009\009\009\009printer.setPageTitle( sName..\" (page \"..nPage..\")\" )\009\009\009\
  7611. \009\009\009end\
  7612. \009\009\009\
  7613. \009\009\009while not printer.newPage()\009do\
  7614. \009\009\009\009if printer.getInkLevel() < 1 then\
  7615. \009\009\009\009\009sStatus = \"Printer out of ink, please refill\"\
  7616. \009\009\009\009elseif printer.getPaperLevel() < 1 then\
  7617. \009\009\009\009\009sStatus = \"Printer out of paper, please refill\"\
  7618. \009\009\009\009else\
  7619. \009\009\009\009\009sStatus = \"Printer output tray full, please empty\"\
  7620. \009\009\009\009end\
  7621. \009\
  7622. \009\009\009\009term.redirect( screenTerminal )\
  7623. \009\009\009\009redrawMenu()\
  7624. \009\009\009\009term.redirect( printerTerminal )\
  7625. \009\009\009\009\
  7626. \009\009\009\009local timer = os.startTimer(0.5)\
  7627. \009\009\009\009sleep(0.5)\
  7628. \009\009\009end\
  7629. \
  7630. \009\009\009nPage = nPage + 1\
  7631. \009\009\009if nPage == 1 then\
  7632. \009\009\009\009printer.setPageTitle( sName )\
  7633. \009\009\009else\
  7634. \009\009\009\009printer.setPageTitle( sName..\" (page \"..nPage..\")\" )\
  7635. \009\009\009end\
  7636. \009\009end\
  7637. \009\009\
  7638. \009\009bMenu = false\
  7639. \009\009term.redirect( printerTerminal )\
  7640. \009\009local ok, error = pcall( function()\
  7641. \009\009\009term.scroll()\
  7642. \009\009\009for n, sLine in ipairs( tLines ) do\
  7643. \009\009\009\009print( sLine )\
  7644. \009\009\009end\
  7645. \009\009end )\
  7646.        term.redirect( screenTerminal )\
  7647. \009\009if not ok then\
  7648. \009\009\009print( error )\
  7649. \009\009end\
  7650. \009\009\
  7651. \009\009while not printer.endPage() do\
  7652. \009\009\009sStatus = \"Printer output tray full, please empty\"\
  7653. \009\009\009redrawMenu()\
  7654. \009\009\009sleep( 0.5 )\
  7655. \009\009end\
  7656. \009\009bMenu = true\
  7657. \009\009\009\
  7658. \009\009if nPage > 1 then\
  7659. \009\009\009sStatus = \"Printed \"..nPage..\" Pages\"\
  7660. \009\009else\
  7661. \009\009\009sStatus = \"Printed 1 Page\"\
  7662. \009\009end\
  7663. \009\009redrawMenu()\
  7664. \009end,\
  7665. \009Exit = function()\
  7666. \009\009bRunning = false\
  7667. \009end,\
  7668. \009Run = function()\
  7669. \009    local sTempPath = \"/.temp\"\
  7670.        local ok, err = save( sTempPath )\
  7671.        if ok then\
  7672.            local nTask = shell.openTab( sTempPath )\
  7673.            if nTask then\
  7674.                shell.switchTab( nTask )\
  7675.            else\
  7676.                sStatus=\"Error starting Task\"\
  7677.            end\
  7678.            fs.delete( sTempPath )\
  7679.        else\
  7680.            sStatus=\"Error saving to \"..sTempPath\
  7681.        end\
  7682. \009\009redrawMenu()\
  7683.    end\
  7684. }\
  7685. \
  7686. local function doMenuItem( _n )\
  7687. \009tMenuFuncs[tMenuItems[_n]]()\
  7688. \009if bMenu then\
  7689. \009\009bMenu = false\
  7690. \009\009term.setCursorBlink( true )\
  7691. \009end\
  7692. \009redrawMenu()\
  7693. end\
  7694. \
  7695. local function setCursor( newX, newY )\
  7696.    local oldX, oldY = x, y\
  7697.    x, y = newX, newY\
  7698. \009local screenX = x - scrollX\
  7699. \009local screenY = y - scrollY\
  7700. \009\
  7701. \009local bRedraw = false\
  7702. \009if screenX < 1 then\
  7703. \009\009scrollX = x - 1\
  7704. \009\009screenX = 1\
  7705. \009\009bRedraw = true\
  7706. \009elseif screenX > w then\
  7707. \009\009scrollX = x - w\
  7708. \009\009screenX = w\
  7709. \009\009bRedraw = true\
  7710. \009end\
  7711. \009\
  7712. \009if screenY < 1 then\
  7713. \009\009scrollY = y - 1\
  7714. \009\009screenY = 1\
  7715. \009\009bRedraw = true\
  7716. \009elseif screenY > h-1 then\
  7717. \009\009scrollY = y - (h-1)\
  7718. \009\009screenY = h-1\
  7719. \009\009bRedraw = true\
  7720. \009end\
  7721. \
  7722. \009recomplete()\
  7723. \009if bRedraw then\
  7724. \009\009redrawText()\
  7725. \009elseif y ~= oldY then\
  7726. \009    redrawLine( oldY )\
  7727. \009    redrawLine( y )\
  7728. \009else\
  7729. \009    redrawLine( y )\
  7730. \009end\
  7731. \009term.setCursorPos( screenX, screenY )\
  7732. \
  7733. \009redrawMenu()\
  7734. end\
  7735. \
  7736. -- Actual program functionality begins\
  7737. load(sPath)\
  7738. \
  7739. term.setBackgroundColour( bgColour )\
  7740. term.clear()\
  7741. term.setCursorPos(x,y)\
  7742. term.setCursorBlink( true )\
  7743. \
  7744. recomplete()\
  7745. redrawText()\
  7746. redrawMenu()\
  7747. \
  7748. local function acceptCompletion()\
  7749.    if nCompletion then\
  7750.        -- Append the completion\
  7751.        local sCompletion = tCompletions[ nCompletion ]\
  7752.        tLines[y] = tLines[y] .. sCompletion\
  7753.        setCursor( x + string.len( sCompletion ), y )\
  7754.    end\
  7755. end\
  7756. \
  7757. -- Handle input\
  7758. while bRunning do\
  7759. \009local sEvent, param, param2, param3 = os.pullEvent()\
  7760. \009if sEvent == \"key\" then\
  7761. \009    local oldX, oldY = x, y\
  7762. \009\009if param == keys.up then\
  7763. \009\009\009-- Up\
  7764. \009\009\009if not bMenu then\
  7765. \009\009\009    if nCompletion then\
  7766. \009\009\009        -- Cycle completions\
  7767.                    nCompletion = nCompletion - 1\
  7768.                    if nCompletion < 1 then\
  7769.                        nCompletion = #tCompletions\
  7770.                    end\
  7771.                    redrawLine(y)\
  7772. \
  7773. \009\009\009\009elseif y > 1 then\
  7774. \009\009\009\009\009-- Move cursor up\
  7775. \009\009\009\009\009setCursor(\
  7776. \009\009\009\009\009    math.min( x, string.len( tLines[y - 1] ) + 1 ),\
  7777. \009\009\009\009\009    y - 1\
  7778. \009\009\009\009\009)\
  7779. \009\009\009\009end\
  7780. \009\009\009end\
  7781. \
  7782. \009\009elseif param == keys.down then\
  7783. \009\009\009-- Down\
  7784. \009\009\009if not bMenu then\
  7785. \009\009\009\009-- Move cursor down\
  7786. \009\009\009    if nCompletion then\
  7787. \009\009\009        -- Cycle completions\
  7788.                    nCompletion = nCompletion + 1\
  7789.                    if nCompletion > #tCompletions then\
  7790.                        nCompletion = 1\
  7791.                    end\
  7792.                    redrawLine(y)\
  7793. \
  7794. \009\009\009\009elseif y < #tLines then\
  7795. \009\009\009\009    -- Move cursor down\
  7796. \009\009\009\009\009setCursor(\
  7797.                        math.min( x, string.len( tLines[y + 1] ) + 1 ),\
  7798.                        y + 1\
  7799.                    )\
  7800. \009\009\009\009end\
  7801. \009\009\009end\
  7802. \
  7803. \009\009elseif param == keys.tab then\
  7804. \009\009\009-- Tab\
  7805. \009\009\009if not bMenu and not bReadOnly then\
  7806. \009\009\009    if nCompletion and x == string.len(tLines[y]) + 1 then\
  7807. \009\009\009        -- Accept autocomplete\
  7808.                    acceptCompletion()\
  7809.                else\
  7810.                    -- Indent line\
  7811.                    local sLine = tLines[y]\
  7812.                    tLines[y] = string.sub(sLine,1,x-1) .. \"  \" .. string.sub(sLine,x)\
  7813.                    setCursor( x + 2, y )\
  7814.                end\
  7815. \009\009\009end\
  7816. \
  7817. \009\009elseif param == keys.pageUp then\
  7818. \009\009\009-- Page Up\
  7819. \009\009\009if not bMenu then\
  7820. \009\009\009\009-- Move up a page\
  7821. \009\009\009\009local newY\
  7822. \009\009\009\009if y - (h - 1) >= 1 then\
  7823. \009\009\009\009\009newY = y - (h - 1)\
  7824. \009\009\009\009else\
  7825. \009\009\009\009    newY = 1\
  7826. \009\009\009\009end\
  7827.                setCursor(\
  7828. \009\009\009\009    math.min( x, string.len( tLines[newY] ) + 1 ),\
  7829. \009\009\009\009    newY\
  7830. \009\009\009\009)\
  7831. \009\009\009end\
  7832. \
  7833. \009\009elseif param == keys.pageDown then\
  7834. \009\009\009-- Page Down\
  7835. \009\009\009if not bMenu then\
  7836. \009\009\009\009-- Move down a page\
  7837. \009\009\009\009local newY\
  7838. \009\009\009\009if y + (h - 1) <= #tLines then\
  7839. \009\009\009\009\009newY = y + (h - 1)\
  7840. \009\009\009\009else\
  7841. \009\009\009\009\009newY = #tLines\
  7842. \009\009\009\009end\
  7843. \009\009\009\009local newX = math.min( x, string.len( tLines[newY] ) + 1 )\
  7844. \009\009\009\009setCursor( newX, newY )\
  7845. \009\009\009end\
  7846. \
  7847. \009\009elseif param == keys.home then\
  7848. \009\009\009-- Home\
  7849. \009\009\009if not bMenu then\
  7850. \009\009\009\009-- Move cursor to the beginning\
  7851. \009\009\009\009if x > 1 then\
  7852.                    setCursor(1,y)\
  7853.                end\
  7854. \009\009\009end\
  7855. \
  7856. \009\009elseif param == keys[\"end\"] then\
  7857. \009\009\009-- End\
  7858. \009\009\009if not bMenu then\
  7859. \009\009\009\009-- Move cursor to the end\
  7860. \009\009\009\009local nLimit = string.len( tLines[y] ) + 1\
  7861. \009\009\009\009if x < nLimit then\
  7862.    \009\009\009\009setCursor( nLimit, y )\
  7863.    \009\009    end\
  7864. \009\009\009end\
  7865. \
  7866. \009\009elseif param == keys.left then\
  7867. \009\009\009-- Left\
  7868. \009\009\009if not bMenu then\
  7869. \009\009\009\009if x > 1 then\
  7870. \009\009\009\009\009-- Move cursor left\
  7871.    \009\009\009\009setCursor( x - 1, y )\
  7872. \009\009\009\009elseif x==1 and y>1 then\
  7873.    \009\009\009\009setCursor( string.len( tLines[y-1] ) + 1, y - 1 )\
  7874. \009\009\009\009end\
  7875. \009\009\009else\
  7876. \009\009\009\009-- Move menu left\
  7877. \009\009\009\009nMenuItem = nMenuItem - 1\
  7878. \009\009\009\009if nMenuItem < 1 then\
  7879. \009\009\009\009\009nMenuItem = #tMenuItems\
  7880. \009\009\009\009end\
  7881. \009\009\009\009redrawMenu()\
  7882. \009\009\009end\
  7883. \
  7884. \009\009elseif param == keys.right then\
  7885. \009\009\009-- Right\
  7886. \009\009\009if not bMenu then\
  7887. \009\009\009    local nLimit = string.len( tLines[y] ) + 1\
  7888. \009\009\009\009if x < nLimit then\
  7889. \009\009\009\009\009-- Move cursor right\
  7890. \009\009\009\009\009setCursor( x + 1, y )\
  7891. \009\009\009    elseif nCompletion and x == string.len(tLines[y]) + 1 then\
  7892.                    -- Accept autocomplete\
  7893.                    acceptCompletion()\
  7894. \009\009\009\009elseif x==nLimit and y<#tLines then\
  7895. \009\009\009\009    -- Go to next line\
  7896. \009\009\009\009    setCursor( 1, y + 1 )\
  7897. \009\009\009\009end\
  7898. \009\009\009else\
  7899. \009\009\009\009-- Move menu right\
  7900. \009\009\009\009nMenuItem = nMenuItem + 1\
  7901. \009\009\009\009if nMenuItem > #tMenuItems then\
  7902. \009\009\009\009\009nMenuItem = 1\
  7903. \009\009\009\009end\
  7904. \009\009\009\009redrawMenu()\
  7905. \009\009\009end\
  7906. \
  7907. \009\009elseif param == keys.delete then\
  7908. \009\009\009-- Delete\
  7909. \009\009\009if not bMenu and not bReadOnly then\
  7910. \009\009\009    local nLimit = string.len( tLines[y] ) + 1\
  7911. \009\009\009\009if x < nLimit then\
  7912. \009\009\009\009\009local sLine = tLines[y]\
  7913. \009\009\009\009\009tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)\
  7914. \009\009\009\009\009recomplete()\
  7915. \009\009\009\009\009redrawLine(y)\
  7916. \009\009\009\009elseif y<#tLines then\
  7917. \009\009\009\009\009tLines[y] = tLines[y] .. tLines[y+1]\
  7918. \009\009\009\009\009table.remove( tLines, y+1 )\
  7919. \009\009\009\009\009recomplete()\
  7920. \009\009\009\009\009redrawText()\
  7921. \009\009\009\009end\
  7922. \009\009\009end\
  7923. \
  7924. \009\009elseif param == keys.backspace then\
  7925. \009\009\009-- Backspace\
  7926. \009\009\009if not bMenu and not bReadOnly then\
  7927. \009\009\009\009if x > 1 then\
  7928. \009\009\009\009\009-- Remove character\
  7929. \009\009\009\009\009local sLine = tLines[y]\
  7930. \009\009\009\009\009tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)\
  7931. \009\009\009        setCursor( x - 1, y )\
  7932. \009\009\009\009elseif y > 1 then\
  7933. \009\009\009\009\009-- Remove newline\
  7934. \009\009\009\009\009local sPrevLen = string.len( tLines[y-1] )\
  7935. \009\009\009\009\009tLines[y-1] = tLines[y-1] .. tLines[y]\
  7936. \009\009\009\009\009table.remove( tLines, y )\
  7937. \009\009\009\009\009setCursor( sPrevLen + 1, y - 1 )\
  7938. \009\009\009\009\009redrawText()\
  7939. \009\009\009\009end\
  7940. \009\009\009end\
  7941. \
  7942. \009\009elseif param == keys.enter then\
  7943. \009\009\009-- Enter\
  7944. \009\009\009if not bMenu and not bReadOnly then\
  7945. \009\009\009\009-- Newline\
  7946. \009\009\009\009local sLine = tLines[y]\
  7947. \009\009\009\009local _,spaces=string.find(sLine,\"^[ ]+\")\
  7948. \009\009\009\009if not spaces then\
  7949. \009\009\009\009\009spaces=0\
  7950. \009\009\009\009end\
  7951. \009\009\009\009tLines[y] = string.sub(sLine,1,x-1)\
  7952. \009\009\009\009table.insert( tLines, y+1, string.rep(' ',spaces)..string.sub(sLine,x) )\
  7953. \009\009\009\009setCursor( spaces + 1, y + 1 )\
  7954. \009\009\009\009redrawText()\
  7955. \
  7956. \009\009\009elseif bMenu then\
  7957. \009\009\009\009-- Menu selection\
  7958. \009\009\009\009doMenuItem( nMenuItem )\
  7959. \
  7960. \009\009\009end\
  7961. \
  7962. \009\009elseif param == keys.leftCtrl or param == keys.rightCtrl or param == keys.rightAlt then\
  7963. \009\009\009-- Menu toggle\
  7964. \009\009\009bMenu = not bMenu\
  7965. \009\009\009if bMenu then\
  7966. \009\009\009\009term.setCursorBlink( false )\
  7967. \009\009\009else\
  7968. \009\009\009\009term.setCursorBlink( true )\
  7969. \009\009\009end\
  7970. \009\009\009redrawMenu()\
  7971. \
  7972. \009\009end\
  7973. \009\009\
  7974. \009elseif sEvent == \"char\" then\
  7975. \009\009if not bMenu and not bReadOnly then\
  7976. \009\009\009-- Input text\
  7977. \009\009\009local sLine = tLines[y]\
  7978. \009\009\009tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)\
  7979. \009\009\009setCursor( x + 1, y )\
  7980. \
  7981. \009\009elseif bMenu then\
  7982. \009\009\009-- Select menu items\
  7983. \009\009\009for n,sMenuItem in ipairs( tMenuItems ) do\
  7984. \009\009\009\009if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then\
  7985. \009\009\009\009\009doMenuItem( n )\
  7986. \009\009\009\009\009break\
  7987. \009\009\009\009end\
  7988. \009\009\009end\
  7989. \009\009end\
  7990. \
  7991. \009elseif sEvent == \"paste\" then\
  7992. \009\009if not bMenu and not bReadOnly then\
  7993. \009\009\009-- Input text\
  7994. \009\009\009local sLine = tLines[y]\
  7995. \009\009\009tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)\
  7996. \009\009\009setCursor( x + string.len( param ), y )\
  7997. \009\009end\
  7998. \009\009\
  7999. \009elseif sEvent == \"mouse_click\" then\
  8000. \009\009if not bMenu then\
  8001. \009\009\009if param == 1 then\
  8002. \009\009\009\009-- Left click\
  8003. \009\009\009\009local cx,cy = param2, param3\
  8004. \009\009\009\009if cy < h then\
  8005. \009\009\009\009\009local newY = math.min( math.max( scrollY + cy, 1 ), #tLines )\
  8006. \009\009\009\009\009local newX = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[newY] ) + 1 )\
  8007. \009\009\009\009\009setCursor( newX, newY )\
  8008. \009\009\009\009end\
  8009. \009\009\009end\
  8010. \009\009end\
  8011. \009\009\
  8012. \009elseif sEvent == \"mouse_scroll\" then\
  8013. \009\009if not bMenu then\
  8014. \009\009\009if param == -1 then\
  8015. \009\009\009\009-- Scroll up\
  8016. \009\009\009\009if scrollY > 0 then\
  8017. \009\009\009\009\009-- Move cursor up\
  8018. \009\009\009\009\009scrollY = scrollY - 1\
  8019. \009\009\009\009\009redrawText()\
  8020. \009\009\009\009end\
  8021. \009\009\009\
  8022. \009\009\009elseif param == 1 then\
  8023. \009\009\009\009-- Scroll down\
  8024. \009\009\009\009local nMaxScroll = #tLines - (h-1)\
  8025. \009\009\009\009if scrollY < nMaxScroll then\
  8026. \009\009\009\009\009-- Move cursor down\
  8027. \009\009\009\009\009scrollY = scrollY + 1\
  8028. \009\009\009\009\009redrawText()\
  8029. \009\009\009\009end\
  8030. \009\009\009\009\
  8031. \009\009\009end\
  8032. \009\009end\
  8033. \
  8034. \009elseif sEvent == \"term_resize\" then\
  8035. \009    w,h = term.getSize()\
  8036.        setCursor( x, y )\
  8037.        redrawMenu()\
  8038.        redrawText()\
  8039. \
  8040. \009end\
  8041. end\
  8042. \
  8043. -- Cleanup\
  8044. term.clear()\
  8045. term.setCursorBlink( false )\
  8046. term.setCursorPos( 1, 1 )",
  8047.     [ "tty.lua" ] = "--[[\
  8048. \009Shell script: tty by Creator\
  8049. \009for OmniOS\
  8050. ]]--",
  8051.     [ "getlink.lua" ] = "--[[\
  8052. \009Shell script: getlink by Creator\
  8053. \009for OmniOS\
  8054. ]]--\
  8055. \
  8056. local path = (...) or OmniOS.getPath()\
  8057. \
  8058. print(fs.getMountPath())",
  8059.     [ "clear.lua" ] = "--[[\
  8060. \009Shell script: clear by Creator\
  8061. \009for OmniOS\
  8062. ]]--\
  8063. \
  8064. term.clear()\
  8065. term.setCursorPos(1,1)",
  8066.     [ "exec.lua" ] = "stdp(\"OmniOS execute!\")\
  8067. \
  8068. local prev = {}\
  8069. \
  8070. stdout(\">\")\
  8071. local i = stdin()\
  8072. nPrev = 1\
  8073. while i ~= \"exit\" do\
  8074. \009prev[nPrev] = i\
  8075. \009nPrev = nPrev + 1\
  8076. \009stdp(OmniOS.execute(i))\
  8077. \009stdout(\">\")\
  8078. \009i = read(nil, prev)\
  8079. end",
  8080.     [ "empty.lua" ] = "local function isEmpty(str)\
  8081. \009stdlog(tostring(str)..type(str))\
  8082. \009if not str then return true end\
  8083. \009str = tostring(str)\
  8084. \
  8085. \009local yes = true\
  8086. \009for i=1, #str do\
  8087. \009\009yes = str:sub(i,i) == \" \" and yes \
  8088. \009end\
  8089. \009return yes\
  8090. end\
  8091. \
  8092. print(\"This determines the emptyness of the string\")\
  8093. \
  8094. local data = read()\
  8095. while data  and data ~= \"exit\" do\
  8096. \009print(isEmpty(data))\
  8097. \009data = read()\
  8098. end",
  8099.     [ "cksum.lua" ] = "--[[\
  8100. \009Shell script: cksum by Creator\
  8101. \009for OmniOS\
  8102. ]]--\
  8103. \
  8104. local path = ...\
  8105. \
  8106. if path and fs.exists(path) and not fs.isDir(path) then\
  8107. \009local file = fs.open(path,\"r\")\
  8108. \009local data = file.readAll()\
  8109. \009file.close()\
  8110. \009print(lib.crc(data))\
  8111. else\
  8112. \009term.setTextColor(colors.red)\
  8113. \009print(\"Not a file.\")\
  8114. end\
  8115. \
  8116. term.setTextColor(colors.white)",
  8117.     [ "talk.lua" ] = "--[[\
  8118. \009Shell script: talk by Creator\
  8119. \009for OmniOS\
  8120. ]]--",
  8121.     [ "args.lua" ] = "print(...)\
  8122. stdlog(...)\
  8123. \
  8124. while true do\
  8125. \009if read() == \"exit\" then\
  8126. \009\009break\
  8127. \009end\
  8128. end",
  8129.     [ "copy.lua" ] = "--[[\
  8130. \009Shell script: cp by Creator\
  8131. \009for OmniOS\
  8132. ]]--\
  8133. \
  8134. local path1, path2 = ...\
  8135. \
  8136. if path1 and path2 then\
  8137. \009if fs.exists(path1) then\
  8138. \009\009if fs.exists(path2) then\
  8139. \009\009\009term.setTextColor(colors.red)\
  8140. \009\009\009print(\"The second path already exists.\")\
  8141. \009\009else\
  8142. \009\009\009fs.copy(path1, path2)\
  8143. \009\009end\
  8144. \009else\
  8145. \009\009term.setTextColor(colors.red)\
  8146. \009\009print(\"One of the two paths is not valid.\")\
  8147. \009end\
  8148. else\
  8149. \009term.setTextColor(colors.red)\
  8150. \009print(\"Provide 2 arguments.\")\
  8151. end\
  8152. \
  8153. term.setTextColor(colors.white)",
  8154.     [ "logname.lua" ] = "--[[\
  8155. \009Shell script: logname by Creator\
  8156. \009for OmniOS\
  8157. ]]--",
  8158.     [ "removedir.lua" ] = "--[[\
  8159. \009Shell script: removedir by Creator\
  8160. \009for OmniOS\
  8161. ]]--",
  8162.     [ "sudo.lua" ] = "--[[\
  8163. \009Shell script: sudo by Creator\
  8164. \009for OmniOS\
  8165. ]]--",
  8166.     [ "pwd.lua" ] = "--[[\
  8167. \009Shell script: pwd by Creator\
  8168. \009for OmniOS\
  8169. ]]--\
  8170. \
  8171. print(shell.getPath())",
  8172.     [ "killall.lua" ] = "--[[\
  8173. \009Shell script: killall by Creator\
  8174. \009for OmniOS\
  8175. ]]--",
  8176.     [ "switch.lua" ] = "--[[\
  8177. \009Shell script: switch by Creator\
  8178. \009for OmniOS\
  8179. ]]--\
  8180. \
  8181. local nID = ...\
  8182. \
  8183. nID = tonumber(nID)\
  8184. if nID then\
  8185. \009kernel.switch(nID)\
  8186. end",
  8187.     [ "echo.lua" ] = "--[[\
  8188. \009Shell script: echo by Creator\
  8189. \009for OmniOS\
  8190. ]]--\
  8191. \
  8192. local function isEmpty(str)\
  8193. \009local yes = true\
  8194. \009for i=1, #str do\
  8195. \009\009yes = str:sub(i,i) == \" \" and yes \
  8196. \009end\
  8197. \009return yes\
  8198. end\
  8199. \
  8200. local data = stdin()\
  8201. while data do\
  8202. \009if isEmpty(data) then\
  8203. \009\009break\
  8204. \009end\
  8205. \009stdp(data)\
  8206. \009data = read()\
  8207. end",
  8208.     [ "np.lua" ] = "--[[\
  8209. \009Shell script: np by Creator\
  8210. \009for OmniOS\
  8211. ]]--\
  8212. \
  8213. local path, name = ...\
  8214. \
  8215. name = name or tostring(path)\
  8216. name = tostring(name)\
  8217. \
  8218. path = OmniOS.fix(path)\
  8219. \
  8220. if not (name or path) or not fs.exists(path) or fs.isDir(path) then\
  8221. \009print(\"Some error occured.\")\
  8222. \009return\
  8223. end\
  8224. \
  8225. local file = fs.open(path, \"r\")\
  8226. local data = file.readAll()\
  8227. file.close()\
  8228. \
  8229. local func, err = loadstring(data, name)\
  8230. \
  8231. local function stripTwo(a, b, ...)\
  8232. \009return ...\
  8233. end\
  8234. \
  8235. if func then\
  8236. \009print(OmniOS.newProc(func, \"term\", 1, name, nil, stripTwo(...)))\
  8237. else\
  8238. \009print(err)\
  8239. end",
  8240.     [ "shell.lua" ] = "--[[\
  8241. \009The OmniOS shell\
  8242. \009by Creator\
  8243. ]]--\
  8244. \
  8245. local shell = {}\
  8246. local aliases = {}\
  8247. local prev = {}\
  8248. local nPrev = 1\
  8249. local doPrint = true\
  8250. local runningChildren = 0\
  8251. local runShell = true\
  8252. \
  8253. function onDeath()\
  8254. \009runningChildren = runningChildren - 1\
  8255. \009lib.log.stdout(\" a priocess has died\", runningChildren)\
  8256. \009if runningChildren == 0 then\
  8257. \009\009doPrint = true\
  8258. \009end\
  8259. end\
  8260. \
  8261. local path = \"/\"\
  8262. \
  8263. local function getPath()\
  8264. \009return path\
  8265. end\
  8266. \
  8267. local function setPath(pth)\
  8268. \009if fs.exists(pth) then\
  8269. \009\009path = pth\
  8270. \009else\
  8271. \009\009return false, \"Path does not exist\"\
  8272. \009end\
  8273. end\
  8274. \
  8275. local function getLib(name)\
  8276. \009if lib[name] then\
  8277. \009\009return lib[name]\
  8278. \009end\
  8279. end\
  8280. \
  8281. local function stopShell()\
  8282. \009runShell = false\
  8283. end\
  8284. \
  8285. local function resolvePath(pth)\
  8286. \009if type(pth) == \"string\" then\
  8287. \009\009if pth:sub(1,1) == \"/\" then\
  8288. \009\009\009return fs.combine(pth, \"\")\
  8289. \009\009else\
  8290. \009\009\009return fs.combine(path, pth)\
  8291. \009\009end\
  8292. \009else\
  8293. \009\009return false, \"<path> has to be type string.\"\
  8294. \009end\
  8295. end\
  8296. \
  8297. local shell = {\
  8298. \009getPath = getPath,\
  8299. \009setPath = setPath,\
  8300. \009getLib = getLib,\
  8301. \009stopShell = stopShell,\
  8302. \009resolvePath = resolvePath,\
  8303. }\
  8304. \
  8305. local function fix(t)\
  8306. \009local first = t[1]\
  8307. \009if fs.exists(path..first) and not fs.isDir(path..first) then\
  8308. \009\009t[1] = path..first\
  8309. \009\009return t\
  8310. \009end\
  8311. \009if fs.exists(path..first..\".lua\") and not fs.isDir(path..first..\".lua\") then\
  8312. \009\009t[1] = path..first..\".lua\"\
  8313. \009\009return t\
  8314. \009end\
  8315. \009if fs.exists(\"OmniOS/bin/\"..first) and not fs.isDir(\"OmniOS/bin/\"..first) then\
  8316. \009\009t[1] = \"OmniOS/bin/\"..first\
  8317. \009\009return t\
  8318. \009end\
  8319. \009if fs.exists(\"OmniOS/bin/\"..first..\".lua\") and not fs.isDir(\"OmniOS/bin/\"..first..\".lua\") then\
  8320. \009\009t[1] = \"OmniOS/bin/\"..first..\".lua\"\
  8321. \009\009return t\
  8322. \009end\
  8323. \009if aliases[first] then\
  8324. \009\009t[1] = \"OmniOS/bin/\"..aliases[first]..\".lua\"\
  8325. \009\009return t\
  8326. \009end\
  8327. \009return t, 0\
  8328. end\
  8329. \
  8330. local function subTokenize(nstr)\
  8331. \009local result = {}\
  8332. \009local current = \"\"\
  8333. \009local quotes = false\
  8334. \009local qtype = \"\"\
  8335. \009while nstr:sub(1,1) == \" \" do\
  8336. \009\009nstr = nstr:sub(2,-1)\
  8337. \009end\
  8338. \009for i=1, #nstr do\
  8339. \009\009local char = nstr:sub(i,i)\
  8340. \009\009if char == \"\\\"\" or char == \"'\" then\
  8341. \009\009\009if quotes then\
  8342. \009\009\009\009if qtype == char then\
  8343. \009\009\009\009\009quotes = false\
  8344. \009\009\009\009\009result[#result+1] = current\
  8345. \009\009\009\009\009current = \"\"\
  8346. \009\009\009\009\009qtype = \"\"\
  8347. \009\009\009\009else\
  8348. \009\009\009\009\009current = current..char\
  8349. \009\009\009\009end\
  8350. \009\009\009else\
  8351. \009\009\009\009quotes = true\
  8352. \009\009\009\009qtype = char\
  8353. \009\009\009\009if not (current == \"\") then\
  8354. \009\009\009\009\009result[#result+1] = current\
  8355. \009\009\009\009\009current = \"\"\
  8356. \009\009\009\009end\
  8357. \009\009\009end\
  8358. \009\009elseif char == \" \" then\
  8359. \009\009\009if quotes then\
  8360. \009\009\009\009current = current..char\
  8361. \009\009\009else\
  8362. \009\009\009\009if not (current == \"\") then\
  8363. \009\009\009\009\009result[#result+1] = current\
  8364. \009\009\009\009\009current = \"\"\
  8365. \009\009\009\009end\
  8366. \009\009\009end\
  8367. \009\009else\
  8368. \009\009\009current = current..char\
  8369. \009\009end\
  8370. \009\009if i == #nstr and current ~= \"\" then\
  8371. \009\009\009result[#result+1] = current\
  8372. \009\009\009current = \"\"\
  8373. \009\009end\
  8374. \009end\
  8375. \009return result\
  8376. end\
  8377. \
  8378. local function tokenize(str)\
  8379. \009local parts = {}\
  8380. \009for token in str:gmatch(\"[^|]+\") do\
  8381. \009\009parts[#parts+1] = subTokenize(token)\
  8382. \009end\
  8383. \009return parts\
  8384. end\
  8385. \
  8386. local function getFileContent(pth)\
  8387. \009local file = fs.open(pth,\"r\")\
  8388. \009local data = file.readAll()\
  8389. \009file.close()\
  8390. \009return loadstring(data, pth)\
  8391. end\
  8392. \
  8393. local function run(input)\
  8394. \009local parsed = tokenize(input)\
  8395. \009local code = nil\
  8396. \009local which\
  8397. \009for i=1, #parsed do\
  8398. \009\009parsed[i], code = fix(parsed[i])\
  8399. \009\009if code == 0 then\
  8400. \009\009\009which = which or i\
  8401. \009\009end\
  8402. \009end\
  8403. \009if code then\
  8404. \009\009print(parsed[which][1]..\" does not exist.\")\
  8405. \009\009return false\
  8406. \009end\
  8407. \009local ids = {}\
  8408. \009if parsed[2] then\
  8409. \009\009doPrint = false\
  8410. \009\009log.stdout(\"Found several programs with piping.\")\
  8411. \009\009local pipes = {}\
  8412. \009\009local handles = {}\
  8413. \009\009for i=1, #parsed do\
  8414. \009\009\009if i > 1 then\
  8415. \009\009\009\009local id, handle = kernel.newProc(getFileContent(parsed[i][1]), term.current(), 1, parsed[i][1], {[\"shell\"] = shell, [\"log\"] = log}, unpack(parsed[i], 2))\
  8416. \009\009\009\009pipes[i-1] = kernel.newPipe(id)\
  8417. \009\009\009\009handles[i-1].pipe(pipes[i-1].deathHandle)\
  8418. \009\009\009\009handles[i] = handle\
  8419. \009\009\009else\
  8420. \009\009\009\009local id, handle = kernel.newProc(getFileContent(parsed[i][1]), term.current(), 1, parsed[i][1], {[\"shell\"] = shell, [\"log\"] = log}, unpack(parsed[i], 2))\
  8421. \009\009\009\009handles[i] = handle\
  8422. \009\009\009end\
  8423. \009\009end\
  8424. \009\009for i=1, #parsed do\009\
  8425. \009\009\009log.stdout(\"Initialized\", parsed[i][1])\
  8426. \009\009\009handles[i].onDeath(onDeath)\
  8427. \009\009\009if i == 1 then\
  8428. \009\009\009\009handles[i].stdout(pipes[1].write)\
  8429. \009\009\009\009handles[i].sendEvents(true)\
  8430. \009\009\009\009handles[i].getOutput(true)\
  8431. \009\009\009elseif i == #parsed then\
  8432. \009\009\009\009handles[i].stdin(pipes[i-1].read)\
  8433. \009\009\009\009handles[i].getOutput(true)\
  8434. \009\009\009else\
  8435. \009\009\009\009handles[i].stdout(pipes[1].write)\
  8436. \009\009\009\009handles[i].stdin(pipes[i-1].read)\
  8437. \009\009\009\009handles[i].getOutput(true)\
  8438. \009\009\009end \
  8439. \009\009end\
  8440. \009\009runningChildren = #parsed\
  8441. \009\009log.stdclose()\
  8442. \009else\
  8443. \009\009--dofile(parsed[1][1], unpack(parsed[1], 2))\
  8444. \009\009local loadedfile, err = getFileContent(parsed[1][1])\
  8445. \009\009if not loadedfile then print(err) end\
  8446. \009\009local shellEnv = {\
  8447. \009\009\009kernel = kernel,\
  8448. \009\009\009sandbox = lib.sandbox,\
  8449. \009\009\009log = lib.log,\
  8450. \009\009\009lib = lib,\
  8451. \009\009\009OmniOS = lib.OmniOS,\
  8452. \009\009\009stdout = write,\
  8453. \009\009\009stdp = print,\
  8454. \009\009\009stdin = read,\
  8455. \009\009\009stdlog = function(...)\
  8456. \009\009\009\009lib.log.log(\"proc/\"..name, ...)\
  8457. \009\009\009end,\
  8458. \009\009\009tderr = function(...)\
  8459. \009\009\009\009lib.log.log(\"error\", ...)\
  8460. \009\009\009end,\
  8461. \009\009}\
  8462. \009\009local env = sandbox(shellEnv)\
  8463. \009\009setfenv(loadedfile, env)\
  8464. \009\009local ok, err = pcall(loadedfile,unpack(parsed[1], 2))\
  8465. \009\009if not ok then print(err) end\
  8466. \009end\
  8467. end\
  8468. \
  8469. local function isEmpty(str)\
  8470. \009local yes = true\
  8471. \009for i=1, #str do\
  8472. \009\009yes = str:sub(i,i) == \" \" and yes \
  8473. \009end\
  8474. \009return yes\
  8475. end\
  8476. \
  8477. term.setTextColor(colors.yellow)\
  8478. print(\"OmniOS Shell v1.0\")\
  8479. while runShell do\
  8480. \009if doPrint then\
  8481. \009\009term.setTextColor(colors.yellow)\
  8482. \009\009term.write((path == \"/\" and \"\" or path)..\009\"> \")\
  8483. \009\009term.setTextColor(colors.white)\
  8484. \009end\
  8485. \009local input = read(nil, prev)\
  8486. \009if not isEmpty(input) then\
  8487. \009\009prev[nPrev] = input\
  8488. \009\009nPrev = nPrev + 1\
  8489. \009\009--run(input)\
  8490. \009\009lib.OmniOS.execute(input)\
  8491. \009end\
  8492. end",
  8493.     [ "remove.lua" ] = "--[[\
  8494. \009Shell script: remove by Creator\
  8495. \009for OmniOS\
  8496. ]]--",
  8497.     [ "stdtest.lua" ] = "--[[\
  8498. \009Std interfaces tester\
  8499. ]]\
  8500. \
  8501. stdout(\"Testing std outputs\\n\")\
  8502. stdout(\"Input somethig: \")\
  8503. local i = stdin()\
  8504. while i ~= \"exit\" do\
  8505. \009stdlog(i)\
  8506. \009stderr(i)\
  8507. \009stdout(i..\"\\n\")\
  8508. \009stdout(\"Input somethig: \")\
  8509. \009i = stdin()\
  8510. end",
  8511.     [ "ping.lua" ] = "--[[\
  8512. \009Shell script: ping by Creator\
  8513. \009for OmniOS\
  8514. ]]--",
  8515.     [ "spam.lua" ] = "--[[\
  8516. \009Shell script: spam by Creator\
  8517. \009for OmniOS\
  8518. ]]--\
  8519. \
  8520. local amount, name, path = ...\
  8521. amount = tonumber(amount)\
  8522. \
  8523. if not amount or amount <= 0 then\
  8524. \009return false\
  8525. end\
  8526. \
  8527. if not (name or path) or not fs.exists(path) then\
  8528. \009print(\"Some error occured.\")\
  8529. \009return\
  8530. end\
  8531. \
  8532. local file = fs.open(path, \"r\")\
  8533. local data = file.readAll()\
  8534. file.close()\
  8535. \
  8536. local func, err = loadstring(data, name)\
  8537. \
  8538. if func then\
  8539. \009--kernel.newProc(func, parent, level, name, ...)\
  8540. \009for i=1, amount do\
  8541. \009\009kernel.newProc(func, term.current(), 0, name..\"_\"..tostring(i), ...)\
  8542. \009end\
  8543. else\
  8544. \009print(err)\
  8545. end",
  8546.     [ "shutdown.lua" ] = "--[[\
  8547. \009Shell script: shutdown by Creator\
  8548. \009for OmniOS\
  8549. ]]--\
  8550. \
  8551. OmniOS.shutdown()",
  8552.     [ "dev.lua" ] = "local list = lib.devbus.list()\
  8553. \
  8554. for i,v in pairs(list) do\
  8555. \009print(i, v, lib.devbus.getInfo(v).type)\
  8556. end",
  8557.     [ "move.lua" ] = "--[[\
  8558. \009Shell script: move by Creator\
  8559. \009for OmniOS\
  8560. ]]--",
  8561.     [ "false.lua" ] = "--[[\
  8562. \009Shell script: false by Creator\
  8563. \009for OmniOS\
  8564. ]]--\
  8565. \
  8566. print(false)",
  8567.     [ "pstree.lua" ] = "--[[\
  8568. \009Shell script: pstree by Creator\
  8569. \009for OmniOS\
  8570. ]]--",
  8571.     [ "reboot.lua" ] = "--[[\
  8572. \009Shell script: reboot by Creator\
  8573. \009for OmniOS\
  8574. ]]--\
  8575. \
  8576. OmniOS.reboot()",
  8577.     [ "list.lua" ] = "--[[\
  8578.    Shell script: list by Creator\
  8579.    for OmniOS\
  8580.    Edited by Piorjade: http://www.computercraft.info/forums2/index.php?/user/41415-piorjade/\
  8581. ]]--\
  8582. \
  8583. local path = (...) or OmniOS.getPath()\
  8584. \
  8585. \
  8586. if fs.isDir(path) then\
  8587. \009for i, v in pairs(fs.list(path)) do\
  8588. \009\009if fs.isDir(path..\"/\"..v) then\
  8589. \009\009\009term.setTextColor(colors.green)\
  8590. \009\009\009print(v)\
  8591. \009\009else\
  8592. \009\009\009term.setTextColor(colors.white)\
  8593. \009\009\009print(v)\
  8594. \009\009end\
  8595. \009end\
  8596. else\
  8597. \009term.setTextColor(colors.red)\
  8598. \009print(\"Not a directory.\")\
  8599. end\
  8600. \
  8601. term.setTextColor(colors.white)",
  8602.   },
  8603.   [ "rednet.lua" ] = "\
  8604. CHANNEL_BROADCAST = 65535\
  8605. CHANNEL_REPEAT = 65533\
  8606. \
  8607. local tReceivedMessages = {}\
  8608. local tReceivedMessageTimeouts = {}\
  8609. local tHostnames = {}\
  8610. \
  8611. function open( sModem )\
  8612. \009if type( sModem ) ~= \"string\" then\
  8613. \009\009error( \"expected string\", 2 )\
  8614. \009end\
  8615. \009if peripheral.getType( sModem ) ~= \"modem\" then\009\
  8616. \009\009error( \"No such modem: \"..sModem, 2 )\
  8617. \009end\
  8618. \009peripheral.call( sModem, \"open\", os.getComputerID() )\
  8619. \009peripheral.call( sModem, \"open\", CHANNEL_BROADCAST )\
  8620. end\
  8621. \
  8622. function close( sModem )\
  8623.    if sModem then\
  8624.        -- Close a specific modem\
  8625.        if type( sModem ) ~= \"string\" then\
  8626.            error( \"expected string\", 2 )\
  8627.        end\
  8628.        if peripheral.getType( sModem ) ~= \"modem\" then\
  8629.            error( \"No such modem: \"..sModem, 2 )\
  8630.        end\
  8631.        peripheral.call( sModem, \"close\", os.getComputerID() )\
  8632.        peripheral.call( sModem, \"close\", CHANNEL_BROADCAST )\
  8633.    else\
  8634.        -- Close all modems\
  8635.        for n,sModem in ipairs( peripheral.getNames() ) do\
  8636.            if isOpen( sModem ) then\
  8637.                close( sModem )\
  8638.            end\
  8639.        end\
  8640.    end\
  8641. end\
  8642. \
  8643. function isOpen( sModem )\
  8644.    if sModem then\
  8645.        -- Check if a specific modem is open\
  8646.        if type( sModem ) ~= \"string\" then\
  8647.            error( \"expected string\", 2 )\
  8648.        end\
  8649.        if peripheral.getType( sModem ) == \"modem\" then\
  8650.            return peripheral.call( sModem, \"isOpen\", os.getComputerID() ) and peripheral.call( sModem, \"isOpen\", CHANNEL_BROADCAST )\
  8651.        end\
  8652.    else\
  8653.        -- Check if any modem is open\
  8654.        for n,sModem in ipairs( peripheral.getNames() ) do\
  8655.            if isOpen( sModem ) then\
  8656.                return true\
  8657.            end\
  8658.        end\
  8659.    end\
  8660. \009return false\
  8661. end\
  8662. \
  8663. function send( nRecipient, message, sProtocol )\
  8664.    -- Generate a (probably) unique message ID\
  8665.    -- We could do other things to guarantee uniqueness, but we really don't need to\
  8666.    -- Store it to ensure we don't get our own messages back\
  8667.    local nMessageID = math.random( 1, 2147483647 )\
  8668.    tReceivedMessages[ nMessageID ] = true\
  8669.    tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = nMessageID\
  8670. \
  8671.    -- Create the message\
  8672.    local nReplyChannel = os.getComputerID()\
  8673.    local tMessage = {\
  8674.        nMessageID = nMessageID,\
  8675.        nRecipient = nRecipient,\
  8676.        message = message,\
  8677.        sProtocol = sProtocol,\
  8678.    }\
  8679. \
  8680.    if nRecipient == os.getComputerID() then\
  8681.        -- Loopback to ourselves\
  8682.        os.queueEvent( \"rednet_message\", nReplyChannel, message, sProtocol )\
  8683. \
  8684.    else\
  8685.        -- Send on all open modems, to the target and to repeaters\
  8686.        local sent = false\
  8687.        for n,sModem in ipairs( peripheral.getNames() ) do\
  8688.            if isOpen( sModem ) then\
  8689.                peripheral.call( sModem, \"transmit\", nRecipient, nReplyChannel, tMessage );\
  8690.                peripheral.call( sModem, \"transmit\", CHANNEL_REPEAT, nReplyChannel, tMessage );\
  8691.                sent = true\
  8692.            end\
  8693.        end\
  8694.    end\
  8695. end\
  8696. \
  8697. function broadcast( message, sProtocol )\
  8698. \009send( CHANNEL_BROADCAST, message, sProtocol )\
  8699. end\
  8700. \
  8701. function receive( sProtocolFilter, nTimeout )\
  8702.    -- The parameters used to be ( nTimeout ), detect this case for backwards compatibility\
  8703.    if type(sProtocolFilter) == \"number\" and nTimeout == nil then\
  8704.        sProtocolFilter, nTimeout = nil, sProtocolFilter\
  8705.    end\
  8706. \
  8707.    -- Start the timer\
  8708. \009local timer = nil\
  8709. \009local sFilter = nil\
  8710. \009if nTimeout then\
  8711. \009\009timer = os.startTimer( nTimeout )\
  8712. \009\009sFilter = nil\
  8713. \009else\
  8714. \009\009sFilter = \"rednet_message\"\
  8715. \009end\
  8716. \
  8717. \009-- Wait for events\
  8718. \009while true do\
  8719. \009\009local sEvent, p1, p2, p3 = os.pullEvent( sFilter )\
  8720. \009\009if sEvent == \"rednet_message\" then\
  8721. \009\009    -- Return the first matching rednet_message\
  8722. \009\009\009local nSenderID, message, sProtocol = p1, p2, p3\
  8723. \009\009\009if sProtocolFilter == nil or sProtocol == sProtocolFilter then\
  8724.    \009\009\009return nSenderID, message, sProtocol\
  8725.    \009    end\
  8726. \009\009elseif sEvent == \"timer\" then\
  8727. \009\009    -- Return nil if we timeout\
  8728. \009\009    if p1 == timer then\
  8729.    \009\009\009return nil\
  8730.    \009\009end\
  8731. \009\009end\
  8732. \009end\
  8733. end\
  8734. \
  8735. function host( sProtocol, sHostname )\
  8736.    if type( sProtocol ) ~= \"string\" or type( sHostname ) ~= \"string\" then\
  8737.        error( \"expected string, string\", 2 )\
  8738.    end\
  8739.    if sHostname == \"localhost\" then\
  8740.        error( \"Reserved hostname\", 2 )\
  8741.    end\
  8742.    if tHostnames[ sProtocol ] ~= sHostname then\
  8743.        if lookup( sProtocol, sHostname ) ~= nil then\
  8744.            error( \"Hostname in use\", 2 )\
  8745.        end\
  8746.        tHostnames[ sProtocol ] = sHostname\
  8747.    end\
  8748. end\
  8749. \
  8750. function unhost( sProtocol )\
  8751.    if type( sProtocol ) ~= \"string\" then\
  8752.        error( \"expected string\", 2 )\
  8753.    end\
  8754.    tHostnames[ sProtocol ] = nil\
  8755. end\
  8756. \
  8757. function lookup( sProtocol, sHostname )\
  8758.    if type( sProtocol ) ~= \"string\" then\
  8759.        error( \"expected string\", 2 )\
  8760.    end\
  8761. \
  8762.    -- Build list of host IDs\
  8763.    local tResults = nil\
  8764.    if sHostname == nil then\
  8765.        tResults = {}\
  8766.    end\
  8767. \
  8768.    -- Check localhost first\
  8769.    if tHostnames[ sProtocol ] then\
  8770.        if sHostname == nil then\
  8771.            table.insert( tResults, os.getComputerID() )\
  8772.        elseif sHostname == \"localhost\" or sHostname == tHostnames[ sProtocol ] then\
  8773.            return os.getComputerID()\
  8774.        end\
  8775.    end\
  8776. \
  8777.    if not isOpen() then\
  8778.        if tResults then\
  8779.            return table.unpack( tResults )\
  8780.        end\
  8781.        return nil\
  8782.    end\
  8783. \
  8784.    -- Broadcast a lookup packet\
  8785.    broadcast( {\
  8786.        sType = \"lookup\",\
  8787.        sProtocol = sProtocol,\
  8788.        sHostname = sHostname,\
  8789.    }, \"dns\" )\
  8790. \
  8791.    -- Start a timer\
  8792.    local timer = os.startTimer( 2 )\
  8793. \
  8794.    -- Wait for events\
  8795.    while true do\
  8796.        local event, p1, p2, p3 = os.pullEvent()\
  8797.        if event == \"rednet_message\" then\
  8798.            -- Got a rednet message, check if it's the response to our request\
  8799.            local nSenderID, tMessage, sMessageProtocol = p1, p2, p3\
  8800.            if sMessageProtocol == \"dns\" and type(tMessage) == \"table\" and tMessage.sType == \"lookup response\" then\
  8801.                if tMessage.sProtocol == sProtocol then\
  8802.                    if sHostname == nil then\
  8803.                        table.insert( tResults, nSenderID )\
  8804.                    elseif tMessage.sHostname == sHostname then\
  8805.                        return nSenderID\
  8806.                    end\
  8807.                end\
  8808.            end\
  8809.        else\
  8810.            -- Got a timer event, check it's the end of our timeout\
  8811.            if p1 == timer then\
  8812.                break\
  8813.            end\
  8814.        end\
  8815.    end\
  8816.    if tResults then\
  8817.        return table.unpack( tResults )\
  8818.    end\
  8819.    return nil\
  8820. end\
  8821. \
  8822. local bRunning = false\
  8823. function run()\
  8824. \009if bRunning then\
  8825. \009\009error( \"rednet is already running\", 2 )\
  8826. \009end\
  8827. \009bRunning = true\
  8828. \009\
  8829. \009while bRunning do\
  8830. \009\009local sEvent, p1, p2, p3, p4 = os.pullEventRaw()\
  8831. \009\009if sEvent == \"modem_message\" then\
  8832. \009\009\009-- Got a modem message, process it and add it to the rednet event queue\
  8833.    \009\009local sModem, nChannel, nReplyChannel, tMessage = p1, p2, p3, p4\
  8834. \009\009    if isOpen( sModem ) and ( nChannel == os.getComputerID() or nChannel == CHANNEL_BROADCAST ) then\
  8835.    \009\009\009if type( tMessage ) == \"table\" and tMessage.nMessageID then\
  8836. \009    \009\009\009if not tReceivedMessages[ tMessage.nMessageID ] then\
  8837. \009\009    \009\009\009tReceivedMessages[ tMessage.nMessageID ] = true\
  8838.                        tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = nMessageID\
  8839. \009\009\009    \009\009os.queueEvent( \"rednet_message\", nReplyChannel, tMessage.message, tMessage.sProtocol )\
  8840. \009\009\009\009    end\
  8841. \009\009\009    end\
  8842. \009\009\009end\
  8843. \
  8844. \009\009elseif sEvent == \"rednet_message\" then\
  8845. \009\009    -- Got a rednet message (queued from above), respond to dns lookup\
  8846. \009\009    local nSenderID, tMessage, sProtocol = p1, p2, p3\
  8847. \009\009    if sProtocol == \"dns\" and type(tMessage) == \"table\" and tMessage.sType == \"lookup\" then\
  8848. \009\009        local sHostname = tHostnames[ tMessage.sProtocol ]\
  8849. \009\009        if sHostname ~= nil and (tMessage.sHostname == nil or tMessage.sHostname == sHostname) then\
  8850. \009\009            rednet.send( nSenderID, {\
  8851. \009\009                sType = \"lookup response\",\
  8852. \009\009                sHostname = sHostname,\
  8853. \009\009                sProtocol = tMessage.sProtocol,\
  8854. \009\009            }, \"dns\" )\
  8855. \009\009        end\
  8856. \009\009    end\
  8857. \
  8858. \009\009elseif sEvent == \"timer\" then\
  8859.            -- Got a timer event, use it to clear the event queue\
  8860.            local nTimer = p1\
  8861.            local nMessage = tReceivedMessageTimeouts[ nTimer ]\
  8862.            if nMessage then\
  8863.                tReceivedMessageTimeouts[ nTimer ] = nil\
  8864.                tReceivedMessages[ nMessage ] = nil\
  8865.            end\
  8866. \009\009end\
  8867. \009end\
  8868. end",
  8869.   kernel = {
  8870.     peripherals = {
  8871.       [ "disk.lua" ] = "local function mountDisk(event,side)\
  8872. \009if not disk.hasAudio(side) then\
  8873. \009\009\009fs.link(\"OmniOS/usr/\"..tostring(disk.getMountPath(side)),tostring(disk.getMountPath(side)))\
  8874. \009end\
  8875. end\
  8876. \
  8877. for i,side in pairs(peripheral.getNames()) do\
  8878. \009if peripheral.getType(side) == \"drive\" then\
  8879. \009\009if not disk.hasAudio(side) and disk.getMountPath(side) then\
  8880. \009\009\009\009fs.link(\"OmniOS/usr/\"..tostring(disk.getMountPath(side)),tostring(disk.getMountPath(side)))\
  8881. \009\009end\
  8882. \009end\
  8883. end\
  8884. \
  8885. Kernel.bindEvent(\"disk\",mountDisk)",
  8886.       [ "multi_mon.lua" ] = "--[[\
  8887.  Author: Creator\
  8888. ]]--\
  8889. local function toRet(...)\
  8890.  local monTable = (...)\
  8891.  assert(type(monTable) == \"table\",\"Expected table!\",2)\
  8892. \
  8893.  local num = \"\"\
  8894.  local x,y,isColor = 0,0,0\
  8895.  local temp = {x = 0,y = 0}\
  8896. \
  8897.  for i,v in pairs(monTable) do\
  8898.    assert(type(v) == \"table\",\"Expected table!\",2)\
  8899.    if num == \"\" then\
  8900.      num = #v\
  8901.    elseif #v ~= num then\
  8902.      error(\"Table is invalid\",2)\
  8903.    end\
  8904.  end\
  8905.  for i,v in pairs(monTable) do\
  8906.    for k,m in pairs(v) do\
  8907.      assert(peripheral.isPresent(m),\"The monitor \"..m..\" does not exist!\",2)\
  8908.      assert(peripheral.getType(m) == \"monitor\",\"Not a monitor, sorry!\",2)\
  8909.      if x == 0 and y == 0 then\
  8910.        x,y = peripheral.call(m,\"getSize\")\
  8911.      else\
  8912.        temp.x,temp.y = peripheral.call(m,\"getSize\")\
  8913.        assert(temp.x == x and temp.y == y,\"The monitors don't have the same size!\",2)\
  8914.      end\
  8915.      if isColor == 0 then\
  8916.        isColor = peripheral.call(m,\"isColor\")\
  8917.      else\
  8918.        assert(isColor == peripheral.call(m,\"isColor\"),\"The monitors are not of the same type!\",2)\
  8919.      end\
  8920.    end\
  8921.  end\
  8922.  --Check done!\
  8923.  --Functions\
  8924.  local monitors = {}\
  8925.  local handle = {}\
  8926.  local x,y = 1,1\
  8927.  local w,h = peripheral.call(monTable[1][1],\"getSize\")\
  8928.  local isBlink = false\
  8929. \
  8930.  for i,v in pairs(monTable) do\
  8931.    monitors[#monitors+1] = (function()\
  8932.      local ret = {}\
  8933.      for k,m in pairs(v) do\
  8934.        ret[#ret+1] = peripheral.wrap(m)\
  8935.      end\
  8936.      return ret\
  8937.    end)()\
  8938.  end\
  8939. \
  8940.  handle.isColor = monitors[1][1].isColor\
  8941.  handle.isColour = monitors[1][1].isColor\
  8942. \
  8943.  function handle.getSize()\
  8944.    return w*#monitors[1],h*#monitors\
  8945.  end\
  8946. \
  8947.  function handle.setCursorPos(tx,ty)\
  8948.    x = tx\
  8949.    y = ty\
  8950.    handle.setCursorBlink(isBlink)\
  8951.  end\
  8952. \
  8953.  function handle.setTextScale(scale)\
  8954.    for i,v in pairs(monitors) do\
  8955.      for k,m in pairs(v) do\
  8956.        m.setTextScale(scale)\
  8957.      end\
  8958.    end\
  8959.    w,h = monitors[1][1].getSize()\
  8960.  end\
  8961. \
  8962.  function handle.clear()\
  8963.    for i,v in pairs(monitors) do\
  8964.      for k,m in pairs(v) do\
  8965.        m.clear()\
  8966.      end\
  8967.    end\
  8968.  end\
  8969. \
  8970.  function handle.setTextColor(color)\
  8971.    for i,v in pairs(monitors) do\
  8972.      for k,m in pairs(v) do\
  8973.        m.setTextColor(color)\
  8974.      end\
  8975.    end\
  8976.  end\
  8977. \
  8978.  handle.setTextColour = handle.setTextColor\
  8979. \
  8980.  function handle.setBackgroundColor(color)\
  8981.    for i,v in pairs(monitors) do\
  8982.      for k,m in pairs(v) do\
  8983.        m.setBackgroundColor(color)\
  8984.      end\
  8985.    end\
  8986.  end\
  8987. \
  8988.  handle.setBackgroundColour = handle.setBackgroundColor\
  8989. \
  8990.  function handle.scroll(lines)\
  8991.    for i,v in pairs(monitors) do\
  8992.      for k,m in pairs(v) do\
  8993.        m.scroll(lines)\
  8994.      end\
  8995.    end\
  8996.  end\
  8997. \
  8998.  function handle.setCursorBlink(blink)\
  8999.    for i,v in pairs(monitors) do\
  9000.      for k,m in pairs(v) do\
  9001.        m.setCursorBlink(false)\
  9002.      end\
  9003.    end\
  9004.    local whichY = math.ceil(y/h)\
  9005.    local whichX = math.ceil(x/w)\
  9006.    monitors[whichY][whichX].setCursorPos(x%w,y%h)\
  9007.    monitors[whichY][whichX].setCursorBlink(blink)\
  9008.    isBlink = blink\
  9009.  end\
  9010. \
  9011.  function handle.clearLine()\
  9012.    local whichY = math.ceil(y/h)\
  9013.    local height = y%h\
  9014.    local whichX = math.ceil(x/w)\
  9015.    for i,v in pairs(monitors[whichY]) do\
  9016.      if i == whichX then\
  9017.        v.setCursorPos(x%w,height)\
  9018.        v.clearLine()\
  9019.      else\
  9020.        v.setCursorPos(1,height)\
  9021.        v.clearLine()\
  9022.      end\
  9023.    end\
  9024.  end\
  9025. \
  9026.  function handle.getCursorPos()\
  9027.    return x,y\
  9028.  end\
  9029. \
  9030.  function handle.write(text)\
  9031.    local tempX = x\
  9032.    local tempBlink = isBlink\
  9033.    handle.setCursorBlink(false)\
  9034.    text = tostring(text)\
  9035.    for i = 1, text:len() do\
  9036.      local whichY = math.ceil(y/h)\
  9037.      local height = y%h\
  9038.      local whichX = math.ceil(tempX/w)\
  9039.      local width = tempX%w\
  9040.      handle.setCursorPos(tempX,y)\
  9041.      monitor[whichY][whichX].write(text:sub(i,i))\
  9042.      tempX = tempX + 1\
  9043.    end\
  9044.    handle.setCursorPos(tempX+1,y)\
  9045.    x = tempX\
  9046.    handle.setCursorBlink(tempBlink)\
  9047.  end\
  9048. \
  9049.  return handle\
  9050. end\
  9051. return toRet",
  9052.       [ "peripheral.lua" ] = "--[[\
  9053.  Author: Creator\
  9054. ]]--\
  9055. \
  9056. local per = Utils.table.copy(peripheral)\
  9057. local extra = {}\
  9058. for i,v in pairs(fs.list(\"OmniOS/Drivers/peripheral\")) do\
  9059.  extra[v:match(\"[^%.]+\")] = dofile(\"OmniOS/Drivers/peripheral/\"..v)\
  9060. end\
  9061. \
  9062. function peripheral.wrap(side,...)\
  9063.  if extra[side] then\
  9064.    return extra[side](...)\
  9065.  else\
  9066.    return per.wrap(side)\
  9067.  end\
  9068. end\
  9069. \
  9070. error(\"reeeeeeee\")",
  9071.     },
  9072.     [ "kernel.lua" ] = "--[[\
  9073. \009New OmniOS Kernel\
  9074. \009by Creator\
  9075. ]]--\
  9076. \
  9077. local kernel, processes, windows, lib, dofile, pts, path = ...\
  9078. \
  9079. local active = 1\
  9080. local daemons = {}\
  9081. local eventFilter = {[\"key\"] = true, [\"mouse_click\"] = true, [\"paste\"] = true,[\"char\"] = true, [\"terminate\"] = true, [\"mouse_scroll\"] = true, [\"mouse_drag\"] = true, [\"key_up\"] = true, [\"mouse_up\"] = true}\
  9082. local eventBuffer = {}\
  9083. local running = active\
  9084. local nW = lib.window\
  9085. local internal = {}\
  9086. local currTerm = term.current()\
  9087. local tasks = {}\
  9088. local w,h = term.getSize()\
  9089. local skipEvent = false\
  9090. local ids = {}\
  9091. local hooks = {}\
  9092. local procWait = {}\
  9093. local continue = true\
  9094. local exitMsg\
  9095. \
  9096. --[[Utils]]\
  9097. \
  9098. local function removeFirst(_, ...)\
  9099. \009return ...\
  9100. end\
  9101. \
  9102. function internal.queueTask(...)\
  9103. \009--lib.log.log(\"tasking\", \"queueTask\", ...)\
  9104. \009tasks[#tasks+1] = {...}\
  9105. \009--lib.log.log(\"tasking\", \"tasks\", lib.textutils.serialize(tasks))\
  9106. end\
  9107. \
  9108. function internal.reboot()\
  9109. \009continue = false\
  9110. \009exitMsg = \"reboot\"\
  9111. end\
  9112. \
  9113. --[[function internal.getIDs()\
  9114. \009local ret = {}\
  9115. \009for i,v in pairs(processes) do\
  9116. \009\009ret[#ret +1] = i\
  9117. \009end\
  9118. \009return ret\
  9119. end]]\
  9120. \
  9121. function internal.fKill(id)\
  9122. \009if tonumber(id) and processes[id] then\
  9123. \009\009--lib.log.stdout(\"FInally killed process #\", id)\
  9124. \009\009processes[id] = nil\
  9125. \009end\
  9126. end\
  9127. \
  9128. function internal.kill(id)\
  9129. \009if id == 1 then\
  9130. \009\009return\
  9131. \009elseif id == active then\
  9132. \009\009kernel.switch(1)\
  9133. \009end\
  9134. \009internal.queueTask(\"fKill\", id)\
  9135. end\
  9136. \
  9137. function internal.resume(id)\
  9138. \009processes[id].resume()\
  9139. end\
  9140. \
  9141. internal.newProc = lib.process.init\
  9142. internal.newPipe = lib.pipe.init\
  9143. \
  9144. function internal.setRunning(val)\
  9145. \009running = val\
  9146. \009-- add sec checks\
  9147. end\
  9148. \
  9149. function internal.getActive()\
  9150. \009return active\
  9151. end\
  9152. \
  9153. internal.inter = {\
  9154. \009setRunning = internal.setRunning,\
  9155. \009getActive = internal.getActive,\
  9156. \009currTerm = currTerm,\
  9157. }\
  9158. \
  9159. function internal.switch(id)\
  9160. \009if not type(id) == \"number\" then return false end\
  9161. \009if processes[id] and processes[id].created then\
  9162. \009\009active = id\
  9163. \009\009processes[id].redraw()\
  9164. \009end\
  9165. end\
  9166. \
  9167. function internal.skipEvent(bool)\
  9168. \009skipEvent = bool\
  9169. end\
  9170. \
  9171. function internal.subDraw(list, tempID)\
  9172. \009term.redirect(currTerm)\
  9173. \009term.setCursorBlink(false)\
  9174. \009paintutils.drawFilledBox(w-15,1,w,h,colors.black)\
  9175. \009term.setTextColor(colors.white) \
  9176. \009for i, v in pairs(list) do\
  9177. \009\009if active == v[1] or tempID == i then paintutils.drawLine(w-15, i, w, i, active == v[1] and colors.blue or colors.orange) end\
  9178. \009\009term.setCursorPos(w-15, i)\
  9179. \009\009term.setBackgroundColor(active == v[1] and colors.blue or tempID == i and colors.orange or colors.black)\
  9180. \009\009term.write(\" x \"..v[1]..\" \"..v[2]..\"                   \")\
  9181. \009end\
  9182. \009term.setCursorPos(w-14, h)\
  9183. \009term.setBackgroundColor(colors.black)\
  9184. \009term.write(\"selected: \"..tostring(tempID))\
  9185. end\
  9186. \
  9187. function internal.drawOpen()\
  9188. \009local list = kernel.list()\
  9189. \009local usedArrow = false  \
  9190. \009local tempID = 0\
  9191. \009local max = 0\
  9192. \009for i=1, #list do\
  9193. \009\009if list[i][1] == active then\
  9194. \009\009\009tempID = i\
  9195. \009\009end\
  9196. \009\009max = i\
  9197. \009end\
  9198. \009\
  9199. \009internal.subDraw(list, tempID)\
  9200. \
  9201. \009while true do\
  9202. \009\009local event = {coroutine.yield()}\
  9203. \009\009if event[1] == \"mouse_click\" then\
  9204. \009\009\009if event[3] < w-15 then\
  9205. \009\009\009\009break\
  9206. \009\009\009elseif event[3] == w-14 and event[4] <= max then\
  9207. \009\009\009\009kernel.kill(list[event[4]][1])\
  9208. \009\009\009\009--if links[evnt[4]] == active then break end\
  9209. \009\009\009\009break\
  9210. \009\009\009elseif event[3] >= w-12 and event[4] <= max then\
  9211. \009\009\009\009kernel.switch(list[event[4]][1])\
  9212. \009\009\009\009break\
  9213. \009\009\009end\
  9214. \009\009elseif event[1] == \"char\" then\
  9215. \009\009\009local tn = tonumber(event[2])\
  9216. \009\009\009if tn then\
  9217. \009\009\009\009if tn == 0 then\
  9218. \009\009\009\009\009kernel.switch(10)\
  9219. \009\009\009\009\009break\
  9220. \009\009\009\009else\
  9221. \009\009\009\009\009kernel.switch(tn)\
  9222. \009\009\009\009\009break\
  9223. \009\009\009\009end\
  9224. \009\009\009else\
  9225. \009\009\009\009eventBuffer[#eventBuffer+1] = event\
  9226. \009\009\009end\
  9227. \009\009elseif event[1] == \"key\" then\
  9228. \009\009\009local tn = event[2]\
  9229. \009\009\009if tn == 56 or tn == 29 then\
  9230. \009\009\009\009break\
  9231. \009\009\009elseif tn == 200 then\
  9232. \009\009\009\009usedArrow = true\
  9233. \009\009\009\009tempID = tempID-1\
  9234. \009\009\009\009if tempID < 1 then\
  9235. \009\009\009\009\009tempID = max\
  9236. \009\009\009\009end\
  9237. \009\009\009\009processes[list[tempID][1]].redraw()\
  9238. \009\009\009\009internal.subDraw(list, tempID)\
  9239. \009\009\009elseif tn == 208 then\
  9240. \009\009\009\009usedArrow = true\
  9241. \009\009\009\009tempID = tempID+1\
  9242. \009\009\009\009if tempID > max then\
  9243. \009\009\009\009\009tempID = 1\
  9244. \009\009\009\009end\
  9245. \009\009\009\009processes[list[tempID][1]].redraw()\
  9246. \009\009\009\009internal.subDraw(list, tempID)\
  9247. \009\009\009elseif tn == 28 then\
  9248. \009\009\009\009if usedArrow then\
  9249. \009\009\009\009\009kernel.switch(list[tempID][1])\
  9250. \009\009\009\009\009break\
  9251. \009\009\009\009else\
  9252. \009\009\009\009\009break\
  9253. \009\009\009\009end\
  9254. \009\009\009else\
  9255. \009\009\009\009eventBuffer[#eventBuffer+1] = event\
  9256. \009\009\009end\
  9257. \009\009elseif not (event[1] == \"mouse_up\" or event[1] == \"mouse_drag\") then\
  9258. \009\009\009eventBuffer[#eventBuffer+1] = event\
  9259. \009\009end\
  9260. \009end\
  9261. \009if processes[active] then\
  9262. \009\009processes[active].redraw()\
  9263. \009end\
  9264. end\
  9265. \
  9266. --Kernel Stuff\
  9267. \
  9268. function kernel.newPipe(...)\
  9269. \009return internal.newPipe(...)\
  9270. end\
  9271. \
  9272. \
  9273. function kernel.newProc(func, parent, level, name, add, ...)\
  9274. \009local id = 1\
  9275. \009local env = {}\
  9276. \009local info = {}\
  9277. \009while true do\
  9278. \009\009if not processes[id] then break end\
  9279. \009\009id = id + 1\
  9280. \009end\
  9281. \
  9282. \009name = name:gsub(\"/\", \"_\")\
  9283. \009name = name == \"..\" and \"_\" or name\
  9284. \009--lib.log.log(\"varagr\", id, tostring(name)..\"plc\", ...)\
  9285. \009level = level or 0\
  9286. \009if level < processes[running].getLevel() then\
  9287. \009\009level = processes[running].getLevel()\
  9288. \009end\
  9289. \
  9290. \009parent = type(parent) == \"table\" and parent or currTerm\
  9291. \
  9292. \009if level == 0 then\
  9293. \009\009env = lib.sandbox({[\"kernel\"] = kernel, [\"internal\"] = internal, OmniOS = lib.OmniOS})\
  9294. \009elseif level == 1 then\
  9295. \009\009env = lib.sandbox({kernel = kernel, OmniOS = lib.OmniOS})\
  9296. \009else\
  9297. \009\009env = lib.sandbox({OmniOS = lib.OmniOS})\
  9298. \009end\
  9299. \
  9300. \009if type(add) == \"table\" then\
  9301. \009\009for i,v in pairs(add) do\
  9302. \009\009\009env[i] = v\
  9303. \009\009end\
  9304. \009end\
  9305. \
  9306. \009env[\"stdout\"] = env.write\
  9307. \009env[\"stdp\"] = env.print\
  9308. \009env[\"stdin\"] = env.read\
  9309. \009env[\"stdlog\"] = function(...)\
  9310. \009\009lib.log.log(\"proc/\"..name..\"_\"..tostring(id), ...)\
  9311. \009end\
  9312. \009env[\"stderr\"] = function(...)\
  9313. \009\009lib.log.log(\"error\", ...)\
  9314. \009end\
  9315. \
  9316. \009local handle = {\
  9317. \009\009stdout = function(pout)\
  9318. \009\009\009--env[\"write\"] = pout\
  9319. \009\009\009env[\"print\"] = pout\
  9320. \009\009\009--env.term[\"write\"] = pout\
  9321. \009\009\009env.stdout = pout\
  9322. \009\009\009env.stdp = pout\
  9323. \009\009end,\
  9324. \009\009stdin = function(pin)\
  9325. \009\009\009env[\"read\"] = pin\
  9326. \009\009end,\
  9327. \009\009set = function(a,b)\
  9328. \009\009\009if not type(a) == \"string\" then\
  9329. \009\009\009\009return false\
  9330. \009\009\009end\
  9331. \009\009\009env[a] = b\
  9332. \009\009end,\
  9333. \009\009pipe = function(p)\
  9334. \009\009\009info.pipe = p\
  9335. \009\009end,\
  9336. \009\009getOutput = function(b)\
  9337. \009\009\009if b then\
  9338. \009\009\009\009info.windowID = running\
  9339. \009\009\009else\
  9340. \009\009\009\009info.windowID = id\
  9341. \009\009\009end\
  9342. \009\009end,\
  9343. \009\009sendEvents = function(yesorno)\
  9344. \009\009\009--lib.log.stdout(\"Process \",running, \" to \", id)\
  9345. \009\009\009if yesorno then\
  9346. \009\009\009\009info.getInput = true\
  9347. \009\009\009\009info.getInputFrom = running\
  9348. \009\009\009\009processes[running].redirectingEventsTo = id\
  9349. \009\009\009else\
  9350. \009\009\009\009info.getInput = false\
  9351. \009\009\009\009info.getInputFrom = false\
  9352. \009\009\009\009processes[running].redirectingEventsTo = false\
  9353. \009\009\009end\
  9354. \009\009end,\
  9355. \009\009onDeath = function(func)\
  9356. \009\009\009info.onDeath = func\
  9357. \009\009end\
  9358. \009}\
  9359. \
  9360. \009info.windowID = id\
  9361. \009windows[id] = nW(parent, 1, 1, w, h, false)\
  9362. \009processes[id] = {\
  9363. \009\009created = false\
  9364. \009}\
  9365. \009internal.queueTask(\"newProc\", internal.inter, func, parent, level, id, name, env, info, ...)\
  9366. \009return id, handle\
  9367. end\
  9368. \
  9369. function kernel.list()\
  9370. \009local ret = {}\
  9371. \009for i,v in pairs(processes) do\
  9372. \009\009ret[#ret+1] = {i, processes[i].getName and processes[i].getName() or \"no_name\"}\
  9373. \009end\
  9374. \009return ret\
  9375. end\
  9376. \
  9377. function kernel.getParent(id)\
  9378. \009if not type(id) == \"number\" then return false end\
  9379. \009if processes[id] and processes[id].created then\
  9380. \009\009return processes[id].parent()\
  9381. \009end\
  9382. end\
  9383. \
  9384. function kernel.switch(id)\
  9385. \009internal.queueTask(\"switch\", id)\
  9386. end\
  9387. \
  9388. function kernel.getpid()\
  9389. \009return running\
  9390. end\
  9391. \
  9392. function kernel.kill(id)\
  9393. \009if tonumber(id) and id > 1 then\
  9394. \009\009internal.queueTask(\"kill\", id)\
  9395. \009end\
  9396. end\
  9397. \
  9398. function kernel.getRunning()\
  9399. \009return running\
  9400. end\
  9401. \
  9402. function kernel.getName(id)\
  9403. \009return processes[id] and processes[id].getName and processes[id].getName() or false\
  9404. end\
  9405. \
  9406. function kernel.addHook(eventName, func)\
  9407. \009if not (type(eventName) == \"string\") then\
  9408. \009\009return false, \"<eventName> is no string.\"\
  9409. \009end\
  9410. \009if not (type(func) == \"function\" or type(func) == \"thread\") then\
  9411. \009\009return false, \"<func> is no string.\"\
  9412. \009end\
  9413. \
  9414. \009if not hooks[eventName] then\
  9415. \009\009hooks[eventName] = {}\
  9416. \009end\
  9417. \009hooks[eventName][#hooks[eventName]+1] = func\
  9418. end\
  9419. \
  9420. function kernel.resume(id)\
  9421. \009local nID = tonumber(id)\
  9422. \009if nID then\
  9423. \009\009if processes[nID] then\
  9424. \009\009\009internal.queueTask(\"resume\", nID)\
  9425. \009\009end\
  9426. \009end\
  9427. end\
  9428. \
  9429. function kernel.reboot()\
  9430. \009internal.queueTask(\"reboot\")\
  9431. end\
  9432. \
  9433. --[[Initializations]]\
  9434. \
  9435. --[[Shell]]\
  9436. local file = fs.open(path, \"r\")\
  9437. local data = file.readAll()\
  9438. file.close()\
  9439. local sh, err = loadstring(data, \"OmniOS Shell\")\
  9440. if not sh then\
  9441. \009print(err)\
  9442. \009read()\
  9443. \009error(err)\
  9444. end\
  9445. \
  9446. --[[FileX]]\
  9447. local file = fs.open(\"OmniOS/programs/FileX/main.lua\", \"r\")\
  9448. local data = file.readAll()\
  9449. file.close()\
  9450. local fx = loadstring(data, \"FileX\")\
  9451. \
  9452. --[[Hooks]]\
  9453. \
  9454. local function keySwitch(_event,_code)\
  9455. \009local time = 0\
  9456. \009while true do\
  9457. \009\009if _event == \"key\" and _code == 56 then\
  9458. \009\009\009time = os.clock()\
  9459. \009\009elseif _event == \"char\" and tonumber(_code) then\
  9460. \009\009\009if os.clock() - time <= 0.50 then\
  9461. \009\009\009\009kernel.switch(tonumber(_code))\
  9462. \009\009\009\009internal.skipEvent(true)\
  9463. \009\009\009end\
  9464. \009\009end\
  9465. \009\009_event, _code = coroutine.yield()\
  9466. \009end\
  9467. end\
  9468. \
  9469. local keySwitchCoro = coroutine.create(keySwitch)\
  9470. \
  9471. local function openProcList(_event, _code)\
  9472. \009local time = 0\
  9473. \009local key = \"\"\
  9474. \009while true do\
  9475. \009\009if _event == \"key\" and _code == 56 then\
  9476. \009\009\009if os.clock() - time <= 0.50 and key == \"ctrl\" then\
  9477. \009\009\009\009time = 0\
  9478. \009\009\009\009internal.queueTask(\"drawOpen\")\
  9479. \009\009\009\009key = \"\"\
  9480. \009\009\009else\
  9481. \009\009\009\009time = os.clock()\
  9482. \009\009\009\009key = \"alt\"\
  9483. \009\009\009end\
  9484. \009\009elseif _event == \"key\" and _code == 29 and key == \"alt\" then\
  9485. \009\009\009if os.clock() - time <= 0.50 then\
  9486. \009\009\009\009time = 0\
  9487. \009\009\009\009internal.queueTask(\"drawOpen\")\
  9488. \009\009\009\009key = \"\"\
  9489. \009\009\009else\
  9490. \009\009\009\009time = os.clock()\
  9491. \009\009\009\009key = \"ctrl\"\
  9492. \009\009\009end\
  9493. \009\009end\
  9494. \009\009_event, _code = coroutine.yield()\
  9495. \009end\
  9496. end\
  9497. \
  9498. local openProcListCoro = coroutine.create(openProcList)\
  9499. \
  9500. kernel.addHook(\"key\", keySwitchCoro)\
  9501. kernel.addHook(\"char\", keySwitchCoro)\
  9502. kernel.addHook(\"key\", openProcListCoro)\
  9503. \
  9504. kernel.addHook(\"peripheral\", lib.devbus.hook)\
  9505. kernel.addHook(\"peripheral_detach\", lib.devbus.hook)\
  9506. \
  9507. \
  9508. windows[1] = nW(currTerm, 1, 1, w, h, false)\
  9509. windows[2] = nW(currTerm, 1, 1, w, h, false)\
  9510. \
  9511. local shellEnv = {\
  9512. \009kernel = kernel,\
  9513. \009sandbox = lib.sandbox,\
  9514. \009log = lib.log,\
  9515. \009lib = lib,\
  9516. \009OmniOS = lib.OmniOS,\
  9517. \009stdout = write,\
  9518. \009stdp = print,\
  9519. \009stdin = read,\
  9520. \009stdlog = function(...)\
  9521. \009\009lib.log.log(\"proc/\"..name, ...)\
  9522. \009end,\
  9523. \009tderr = function(...)\
  9524. \009\009lib.log.log(\"error\", ...)\
  9525. \009end,\
  9526. }\
  9527. \
  9528. lib.process.init(internal.inter, sh, term.current(), 0, 1, \"Shell\", lib.sandbox(shellEnv), {windowID = 1})\
  9529. lib.process.init(internal.inter, fx, term.current(), 1, 2, \"FileX\", lib.sandbox({[\"kernel\"] = kernel, OmniOS = lib.OmniOS}), {windowID = 2})\
  9530. \
  9531. --[[Initializations end]]\
  9532. \
  9533. --kernel.switch(2)\
  9534. \
  9535. while continue do\
  9536. \009--lib.log.log(\"procs\", lib.textutils.serialize(processes))\
  9537. \009while not (#tasks == 0) do\
  9538. \009\009--lib.log.log(\"tasking\", \"tasks\", lib.textutils.serialize(tasks))\
  9539. \009\009local first = table.remove(tasks,1)\
  9540. \009\009--lib.log.log(\"tasking\",\"Executing internal task \", unpack(first))\
  9541. \009\009if internal[first[1]] then\
  9542. \009\009\009internal[first[1]](removeFirst(unpack(first)))\
  9543. \009\009end\
  9544. \009end\
  9545. \009local event = #eventBuffer == 0 and {coroutine.yield()} or table.remove(eventBuffer, 1)\
  9546. \
  9547. \009if hooks[event[1]] then\
  9548. \009\009for i, v in pairs(hooks[event[1]]) do\
  9549. \009\009\009if type(v) == \"function\" then\
  9550. \009\009\009\009v(unpack(event))\
  9551. \009\009\009elseif type(v) == \"thread\" then\
  9552. \009\009\009\009local ok, err = coroutine.resume(v, unpack(event))\
  9553. \009\009\009\009if not ok then\
  9554. \009\009\009\009\009lib.log.stdout(err)\
  9555. \009\009\009\009end\
  9556. \009\009\009end\
  9557. \009\009end\
  9558. \009end\
  9559. \
  9560. \009if skipEvent then\
  9561. \009\009skipEvent = false\
  9562. \009else\
  9563. \009\009if eventFilter[event[1]] then\
  9564. \009\009\009if not processes[active].waiting and not processes[active].info.getInput then\
  9565. \009\009\009\009lib.log.log(\"mt\", \"Kernel is resuming process\", active, \"as a fg event\")\
  9566. \009\009\009\009processes[active].resume(unpack(event))\
  9567. \009\009\009end\
  9568. \009\009else\
  9569. \009\009\009for i,v in pairs(processes) do\
  9570. \009\009\009\009if (not processes[i].waiting) and (not processes[i].info.getInput) then\
  9571. \009\009\009\009\009lib.log.log(\"mt\", \"Kernel is resuming process\", i, \"as a bg event\", tostring(processes[i]))\
  9572. \009\009\009\009\009processes[i].resume(unpack(event))\
  9573. \009\009\009\009end\
  9574. \009\009\009end\
  9575. \009\009end\
  9576. \009\
  9577. \009\009lib.log.log(\"mt\", \"Kernel is iterating through processes that are ready+waiting\")\
  9578. \009\009local bu = {}\
  9579. \009\009for i,v in pairs(processes) do\
  9580. \009\009\009bu[#bu + 1] = tostring(i)..\": \"..tostring(processes[i].ready)..tostring(processes[i].waiting)\
  9581. \009\009end\
  9582. \009\009lib.log.log(\"mt\", table.concat( bu, \" :: \"))\
  9583. \009\009for i,v in pairs(processes) do\
  9584. \009\009\009if processes[i].ready then\
  9585. \009\009\009\009lib.log.log(\"mt\", \"Kernel is resuming process\", i, \"from sleep.\")\
  9586. \009\009\009\009processes[i].ready = false\
  9587. \009\009\009\009processes[i].resume()\
  9588. \009\009\009end\
  9589. \009\009end\
  9590. \009end\
  9591. end\
  9592. \
  9593. return exitMsg",
  9594.     [ "panic.lua" ] = "os.pullEvent = os.pullEventRaw\
  9595. tArgs = {...}\
  9596. \
  9597. err = \"\"\
  9598. \
  9599. term.redirect(term.native())\
  9600. term.setBackgroundColor(colors.blue)\
  9601. term.clear()\
  9602. term.setTextColor(colors.white)\
  9603. term.setCursorPos(2,4)\
  9604. term.write(\"OmniOS has crashed :(\")\
  9605. term.setCursorPos(2,6)\
  9606. print(\"   Click anywhere to restart the system, if the\\n problem persists send the error message to Creator\\n         on the CC forums on GitHub.\")\
  9607. term.setCursorPos(2,11)\
  9608. term.write(\"Error message: \")\
  9609. term.setCursorPos(4,13)\
  9610. for i,v in pairs(tArgs) do\
  9611. \009err = err..\" \"..v\
  9612. end\
  9613. print(err)\
  9614. log.log(\"Error\",err,\"Missing\")\
  9615. os.pullEvent(\"mouse_click\")\
  9616. --os.reboot()",
  9617.     [ "libload.lua" ] = "--Code--\
  9618. \
  9619. local kernel, processes, windows, pts, dofile = ...\
  9620. \
  9621. local lib = {}\
  9622. local errtrack = false\
  9623. lib.dofile = dofile\
  9624. \
  9625. pts(\"Loading libraries...\")\
  9626. \
  9627. for i,v in pairs(fs.list(\"OmniOS/kernel/lib\")) do\
  9628. \009local smth = v:match(\"([^%.]+)\")\
  9629. \009local err = nil\
  9630. \009pts(\"Loading \\027+d@\"..smth)\
  9631. \009lib[smth], err = dofile(\"OmniOS/kernel/lib/\"..v, kernel, processes, windows, lib)\
  9632. \009if not lib[smth] then\
  9633. \009\009errtrack = true\
  9634. \009\009pts(err)\
  9635. \009\009lib.log.log(\"lib\", smth, err)\
  9636. \009end\
  9637. end\
  9638. \
  9639. for i,v in pairs(fs.list(\"OmniOS/lib\")) do\
  9640. \009local smth = v:match(\"([^%.]+)\")\
  9641. \009local err = nil\
  9642. \009pts(\"Loading \\027+d@\"..smth)\
  9643. \009lib[smth], err = dofile(\"OmniOS/lib/\"..v, lib)\
  9644. \009if not lib[smth] then\
  9645. \009\009errtrack = true\
  9646. \009\009pts(err)\
  9647. \009\009lib.log.log(\"lib\", smth, err)\
  9648. \009end\
  9649. end\
  9650. \
  9651. for i,v in pairs(lib) do\
  9652. \009if type(v) == \"table\" and v.init and type(v.init) == \"function\" then\
  9653. \009\009lib[i].init()\
  9654. \009end\
  9655. end\
  9656. \
  9657. if errtrack then\
  9658. \009print(\"There was an error\")\
  9659. \009read()\
  9660. end\
  9661. \
  9662. return lib",
  9663.     lib = {
  9664.       [ "devbus.lua" ] = "--[[\
  9665. \009Device Manager Interface\
  9666. ]]\
  9667. \
  9668. local kernel, processes, windows, lib = ...\
  9669. \
  9670. local devbus = {}\
  9671. local devices = {}\
  9672. local names = {}\
  9673. local drivers = {}\
  9674. local list = {}\
  9675. local actions = {\
  9676. \009peripheral_detach = {},\
  9677. \009peripheral = {}\
  9678. }\
  9679. \
  9680. local log\
  9681. \
  9682. function devbus.generateName(address)\
  9683. \009log(\"Generating name for device at\", address)\
  9684. \009assert(type(address) == \"string\")\
  9685. \009if devices[address] then\
  9686. \009\009local t = devices[address].dType\
  9687. \009\009local index\
  9688. \009\009if names[t] then\
  9689. \009\009\009index = names[t] + 1\
  9690. \009\009\009names[t] = index\
  9691. \009\009else\
  9692. \009\009\009names[t] = 0\
  9693. \009\009\009index = 0\
  9694. \009\009end\
  9695. \009\009return true, t..\"-\"..tostring(index)\
  9696. \009else\
  9697. \009\009return false, \"No device at \"..address\
  9698. \009end\
  9699. end\
  9700. \
  9701. function devbus.hook(event, address)\
  9702. \009log(\"Call from hook\", event, address)\
  9703. \009assert(type(address) == \"string\")\
  9704. \009if event == \"peripheral\" then\
  9705. \009\009log(devbus.addDevice(address))\
  9706. \009\009for i,v in pairs(actions.peripheral) do\
  9707. \009\009\009log(\"Calling action attach\")\
  9708. \009\009\009v()\
  9709. \009\009end\
  9710. \009elseif event == \"peripheral_detach\" then\
  9711. \009\009log(devbus.removeDevice(address))\
  9712. \009\009for i,v in pairs(actions.peripheral_detach) do\
  9713. \009\009\009log(\"Calling action detach\")\
  9714. \009\009\009v()\
  9715. \009\009end\
  9716. \009else\
  9717. \009\009return false, \"Wrong event type\"\
  9718. \009end\
  9719. end\
  9720. \
  9721. function devbus.onEvent(event, func)\
  9722. \009log(\"On event\", event, func)\
  9723. \009assert(type(event) == \"string\")\
  9724. \009assert(type(func) == \"function\")\
  9725. \009assert(event == \"peripheral\" or event == \"peripheral_detach\")\
  9726. \009--log(\"Passed tests\")\
  9727. \009local index = #actions[event]\
  9728. \009actions[event][index+1] = func\
  9729. \009--log(\"Actions:\", lib.textutils.serialize(actions))\
  9730. end\
  9731. \
  9732. function devbus.addDevice(address, dType)\
  9733. \009log(\"Adding device at\", address, \"of type\", dType)\
  9734. \009assert(type(address) == \"string\")\
  9735. \009dType = type(dType) == \"string\" and dType or peripheral.getType(address)\
  9736. \009if devices[address] and dType == devices[address].dType then\
  9737. \009\009\009return false, \"Device already present on address.\"\
  9738. \009end\
  9739. \009devices[address] = {\
  9740. \009\009dType = dType,\
  9741. \009}\
  9742. \009local driver, errmsg = devbus.initDriver(address)\
  9743. \009log(\"Driver loding\", driver, errmsg)\
  9744. \009if not driver then\
  9745. \009\009driver = peripheral.wrap(address)\
  9746. \009end\
  9747. \009devices[address].driver = driver\
  9748. \009local success, name = devbus.generateName(address)\
  9749. \009if not success then\
  9750. \009\009return false, name\
  9751. \009end\
  9752. \009devices[address].name = name\
  9753. \009list = devbus.generateList()\
  9754. \009--log(\"Newly generated list is\", lib.textutils.serialize(list))\
  9755. \009return true, \"Successfully added device at\", address\
  9756. end\
  9757. \
  9758. function devbus.removeDevice(address)\
  9759. \009log(\"Removing device\", address)\
  9760. \009assert(type(address) == \"string\")\
  9761. \009devices[address] = nil\
  9762. \009list = devbus.generateList()\
  9763. \009return true, \"Removed device\"\
  9764. end\
  9765. \
  9766. function devbus.populate()\
  9767. \009for i,v in pairs(peripheral.getNames()) do\
  9768. \009\009log(devbus.addDevice(v))\
  9769. \009end\
  9770. \009list = devbus.generateList()\
  9771. \009--log(\"End result\", lib.textutils.serialize(list), lib.textutils.serialize(devices))\
  9772. end\
  9773. \
  9774. function devbus.addDriver(driver, dType)\
  9775. \009assert(type(dType) == \"string\")\
  9776. \009assert(type(driver) == \"function\")\
  9777. \009log(\"Adding driver for type\", dType)\
  9778. \009if drivers[dType] then\
  9779. \009\009log(\"Failed, driver already exists\")\
  9780. \009\009return false, \"Driver already exists\"\
  9781. \009else\
  9782. \009\009log(\"Successfully added driver.\")\
  9783. \009\009drivers[dType] = driver\
  9784. \009end\
  9785. \009return true\
  9786. end\
  9787. \
  9788. function devbus.initDriver(address)\
  9789. \009assert(type(address) == \"string\")\
  9790. \009log(\"Inituialising driver for\", address)\
  9791. \009if devices[address] then\
  9792. \009\009local dType = devices[address].dType\
  9793. \009\009if drivers[dType] then\
  9794. \009\009\009return drivers[dType](address)\
  9795. \009\009else\
  9796. \009\009\009return false, \"No driver loaded for device of type \"..dType\
  9797. \009\009end\
  9798. \009else\
  9799. \009\009return false, \"No device on address\"..address\
  9800. \009end\
  9801. end\
  9802. \
  9803. function devbus.getDriver(address)\
  9804. \009assert(type(address) == \"string\")\
  9805. \009assert(devices[address] and devices[address].driver)\
  9806. \009local ret = {}\
  9807. \009for i,v in pairs(devices[address].driver) do\
  9808. \009\009ret[i] = v\
  9809. \009end\
  9810. \009return ret\
  9811. end\
  9812. \
  9813. function devbus.generateList()\
  9814. \009log(\"Generating list\")\
  9815. \009local ret = {}\
  9816. \009for i,v in pairs(devices) do\
  9817. \009\009ret[v.name] = i\
  9818. \009end\
  9819. \009return ret\
  9820. end\
  9821. \
  9822. function devbus.getInfo(address)\
  9823. \009assert(type(address) == \"string\")\
  9824. \009assert(devices[address])\
  9825. \009return {\
  9826. \009\009name = devices[address].name,\
  9827. \009\009type = devices[address].dType,\
  9828. \009}\
  9829. end\
  9830. \
  9831. function devbus.list()\
  9832. \009return list\
  9833. end\
  9834. \
  9835. function devbus.init()\
  9836. \009log = lib.log.generate(\"devbus\")\
  9837. end\
  9838. \
  9839. return devbus",
  9840.       [ "vfs.lua" ] = "--[[\
  9841. \009Virtual File System Driver for OmniOS\
  9842. \009Makes working with a table easier\
  9843. ]]\
  9844. \
  9845. local vfs = {}\
  9846. \
  9847. function vfs.isDir(path,tabl)\
  9848. \009path = fs.combine(\"\",path)\
  9849. \009local first = vfs.getFirstElement(path)\
  9850. \009if first == path and tabl[first] then\
  9851. \009\009if type(tabl[first]) == \"table\" then\
  9852. \009\009\009return true\
  9853. \009\009elseif type(tabl[first]) == \"string\" then\
  9854. \009\009\009return false\
  9855. \009\009end\
  9856. \009elseif tabl[first] then\
  9857. \009\009if type(tabl[first]) == \"table\" then\
  9858. \009\009\009return vfs.isDir(string.sub(path,#first + 2,-1),tabl[first])\
  9859. \009\009elseif type(tabl[first]) == \"string\" then\
  9860. \009\009\009return false\
  9861. \009\009end\
  9862. \009end\
  9863. \009return false\
  9864. end\
  9865. \
  9866. function vfs.getFirstElement(path)\
  9867. \009if not type(path) == \"string\" then error(\"This is not a string: \"..tostring(path),2) end\
  9868. \009return string.sub(path,1,string.find(path,\"/\") and string.find(path,\"/\")-1 or -1)\
  9869. end\
  9870. \
  9871. function vfs.exists(path,tabl)\
  9872. \009path = fs.combine(\"\",path)\
  9873. \009local first = vfs.getFirstElement(path)\
  9874. \009if tabl[first] then\
  9875. \009\009if type(tabl[first]) == \"table\" then\
  9876. \009\009\009if first == path then return true end\
  9877. \009\009\009return vfs.exists(string.sub(path,#first + 2,-1),tabl[first])\
  9878. \009\009elseif type(tabl[first]) == \"string\" and first == path then\
  9879. \009\009\009return true\
  9880. \009\009end\
  9881. \009end\
  9882. \009return false\
  9883. end\
  9884. \
  9885. function vfs.read(path,tabl)\
  9886. \009path = fs.combine(\"\",path)\
  9887. \009local first = vfs.getFirstElement(path)\
  9888. \009if tabl[first] then\
  9889. \009\009if type(tabl[first]) == \"table\" then\
  9890. \009\009\009if first == path then return false end\
  9891. \009\009\009return vfs.read(string.sub(path,#first + 2,-1),tabl[first])\
  9892. \009\009elseif type(tabl[first]) == \"string\" then\
  9893. \009\009\009if first == path then\
  9894. \009\009\009\009return tabl[first]\
  9895. \009\009\009else\
  9896. \009\009\009\009return false\
  9897. \009\009\009end\
  9898. \009\009end\
  9899. \009end\
  9900. \009return false\
  9901. end\
  9902. \
  9903. function vfs.makeFile(path, tabl)\
  9904. \009path = fs.combine(\"\",path)\
  9905. \009--lib.log.log(\"ramfs\", \"Makign file\", path)\
  9906. \009local first = vfs.getFirstElement(path)\
  9907. \009if not tabl[first] then\
  9908. \009\009if first == path then\
  9909. \009\009\009tabl[first] = \"\"\
  9910. \009\009\009return true\
  9911. \009\009else\
  9912. \009\009\009tabl[first] = {}\
  9913. \009\009end\
  9914. \009end\
  9915. \009if type(tabl[first]) == \"table\" then\
  9916. \009\009if first == path then return false end\
  9917. \009\009return vfs.makeFile(string.sub(path,#first + 2,-1),tabl[first])\
  9918. \009end\
  9919. \009return false\
  9920. end\
  9921. \
  9922. function vfs.write(data,path,tabl)\
  9923. \009path = fs.combine(\"\",path)\
  9924. \009--lib.log.log(\"ramfs\", \"Writing\", path)\
  9925. \009local first = vfs.getFirstElement(path)\
  9926. \009if tabl[first] then\
  9927. \009\009if type(tabl[first]) == \"table\" then\
  9928. \009\009\009if first == path then return false end\
  9929. \009\009\009return vfs.write(data,string.sub(path,#first + 2,-1),tabl[first])\
  9930. \009\009elseif type(tabl[first]) == \"string\" then\
  9931. \009\009\009if first == path then\
  9932. \009\009\009\009tabl[first] = data\
  9933. \009\009\009else\
  9934. \009\009\009\009return false\
  9935. \009\009\009end\
  9936. \009\009end\
  9937. \009end\
  9938. \009return false\
  9939. end\
  9940. \
  9941. function vfs.delete(path,tabl)\
  9942. \009path = fs.combine(\"\",path)\
  9943. \009local first = vfs.getFirstElement(path)\
  9944. \009if tabl[first] then\
  9945. \009\009if type(tabl[first]) == \"table\" then\
  9946. \009\009\009if first == path then\
  9947. \009\009\009\009tabl[first] = nil\
  9948. \009\009\009\009return true\
  9949. \009\009\009end\
  9950. \009\009\009return vfs.delete(string.sub(path,#first + 2,-1),tabl[first])\
  9951. \009\009elseif type(tabl[first]) == \"string\" then\
  9952. \009\009\009if first == path then\
  9953. \009\009\009\009tabl[first] = nil\
  9954. \009\009\009else\
  9955. \009\009\009\009return false\
  9956. \009\009\009end\
  9957. \009\009end\
  9958. \009end\
  9959. \009return false\
  9960. end\
  9961. \
  9962. function vfs.list(path,tabl)\
  9963. \009--lib.log.log(\"ramfs\", textutils.serialize(filesystem))\
  9964. \009path = fs.combine(\"\",path)\
  9965. \009local first = vfs.getFirstElement(path)\
  9966. \009if tabl[first] then\
  9967. \009\009if type(tabl[first]) == \"table\" then\
  9968. \009\009\009if first == path then\
  9969. \009\009\009\009return tabl[first]\
  9970. \009\009\009end\
  9971. \009\009\009return vfs.list(string.sub(path,#first + 2,-1),tabl[first])\
  9972. \009\009else\
  9973. \009\009\009return false\
  9974. \009\009end\
  9975. \009end\
  9976. \009return false\
  9977. end\
  9978. \
  9979. function vfs.makeDir(path,tabl)\
  9980. \009if path == nil or path == \"\" then\
  9981. \009\009return\
  9982. \009end\
  9983. \009--lib.log.log(\"ramfs\", \"makeDir: Making folder at \", path)\
  9984. \009path = fs.combine(\"\",path)\
  9985. \009local first = vfs.getFirstElement(path)\
  9986. \009--lib.log.log(\"ramfs\", \"makeDir: First is \", first)\
  9987. \009if tabl[first] then\
  9988. \009\009lib.log.log(\"ramfs\", \"makeDir: \", path, \"exists\", tabl[first])\
  9989. \009\009if type(tabl[first]) == \"table\" then\
  9990. \009\009\009if first == path then\
  9991. \009\009\009\009return true\
  9992. \009\009\009end\
  9993. \009\009elseif type(tabl[first]) == \"string\" then\
  9994. \009\009\009return false\
  9995. \009\009else\
  9996. \009\009\009return false\
  9997. \009\009end\
  9998. \009else\
  9999. \009\009tabl[first] = {}\
  10000. \009\009if first == path then return end\
  10001. \009end\
  10002. \009return vfs.makeDir(string.sub(path,#first + 2,-1),tabl[first])\
  10003. end\
  10004. \
  10005. return vfs",
  10006.       [ "log.lua" ] = "--[[\
  10007. \009Log API\
  10008. \009by Creator\
  10009. \009for TheOS\
  10010. ]]--\
  10011. \
  10012. local doLog = true\
  10013. local logPath = \"logs\"\
  10014. local stdout = fs.open(logPath..\"/stdout.log\",\"a\")\
  10015. local isOpen = true\
  10016. \
  10017. local log = {}\
  10018. \
  10019. local function concat(...)\
  10020. \009local str = \"\"\
  10021. \009for i,v in pairs({...}) do\
  10022. \009\009str = str..\" \"..tostring(v)\
  10023. \009end\
  10024. \009return str:sub(2,-1)\
  10025. end\
  10026. \
  10027. \
  10028. function log.log(category, ...)\
  10029. \009local file = fs.open(logPath..\"/\"..tostring(category)..\".log\",\"a\")\
  10030. \009file.write(concat(...)..\"\\n\")\
  10031. \009file.close()\
  10032. end\
  10033. \
  10034. function log.stdout(...)\
  10035. \009if not isOpen then\
  10036. \009\009stdout = fs.open(logPath..\"/stdout.log\",\"a\")\
  10037. \009end\
  10038. \009isOpen = true\
  10039. \009stdout.write(concat(...)..\"\\n\")\
  10040. \009stdout.flush()\
  10041. end\
  10042. \
  10043. function log.stdclose()\
  10044. \009stdout.close()\
  10045. \009isOpen = false\
  10046. end\
  10047. \
  10048. function log.generate(category, disable)\
  10049. \009if disable then\
  10050. \009\009return function()end\
  10051. \009else\
  10052. \009\009return function(...)\
  10053. \009\009\009local file = fs.open(logPath..\"/\"..tostring(category)..\".log\",\"a\")\
  10054. \009\009\009file.write(concat(...)..\"\\n\")\
  10055. \009\009\009file.close()\
  10056. \009\009end\
  10057. \009end\
  10058. end\
  10059. \
  10060. return log\
  10061. \
  10062. --cat logs/error.log | tac logs/out.txt\
  10063. --np STD OmniOS/bin/stdtest.lua\
  10064. --np Exec OmniOS/bin/exec.lua\
  10065. --cat logs/error.log",
  10066.       [ "utils.lua" ] = "--[[\
  10067. \009utils\
  10068. \009by Creator\
  10069. \009OmniOS\
  10070. ]]--\
  10071. \
  10072. \
  10073. local function split(toSplit,sep)\
  10074. \009if type(toSplit) ~= \"string\" then return false end\
  10075. \009sep = sep or \"%s\"\
  10076. \009local ret = {}\
  10077. \009local count = 1\
  10078. \009for token in toSplit:gmatch(\"[^\"..sep..\"]+\") do\
  10079. \009\009ret[count] = token\
  10080. \009\009count = count + 1\
  10081. \009end\
  10082. \009return ret\
  10083. end\
  10084. \
  10085. local function tablecopy(source,destination)\
  10086. \009source = type(source) == \"table\" and source or {}\
  10087. \009for i,v in pairs(source) do\
  10088. \009\009if type(v) == \"table\" then\
  10089. \009\009\009destination[i] = {}\
  10090. \009\009\009local mt = getmetatable(v)\
  10091. \009\009\009if mt and type(mt) == \"table\" then\
  10092. \009\009\009\009setmetatable(destnation[i],mt)\
  10093. \009\009\009end\
  10094. \009\009\009tablecopy(v,destination[i])\
  10095. \009\009else\
  10096. \009\009\009destination[i] = v\
  10097. \009\009end\
  10098. \009end\
  10099. end\
  10100. \
  10101. local function getExtension(path)\
  10102. \009path = type(path) == \"string\" and path or \"\"\
  10103. \009local name = fs.getName(path)\
  10104. \009ind = string.find(name,\"%.\") or false\
  10105. \009if ind then\
  10106. \009\009return name:sub(ind+1, -1)\
  10107. \009else\
  10108. \009\009return \"\"\
  10109. \009end\
  10110. end\
  10111. \
  10112. local table = {\
  10113. \009copy = function(source,destination)\
  10114. \009\009destination = destination or {}\
  10115. \009\009tablecopy(source,destination)\
  10116. \009\009return destination\
  10117. \009end\
  10118. }\
  10119. \
  10120. return {\
  10121. \009split = split,\
  10122. \009table = table,\
  10123. \009getExtension = getExtension,\
  10124. }",
  10125.       [ "OmniOS.lua" ] = "--[[\
  10126. \009OmniOS Interface for Sandboxed Processes\
  10127. ]]--\
  10128. \
  10129. local kernel, processes, windows, lib = ...\
  10130. \
  10131. local OmniOS = {}\
  10132. \
  10133. local dependencies = {}\
  10134. local usr_dep = {}\
  10135. \
  10136. local aliases = {}\
  10137. local usr_aliases = {}\
  10138. \
  10139. --local realm\
  10140. \
  10141. local function getEnv(id)\
  10142. \009id = type(id) == \"number\" and id or kernel.getRunning()\
  10143. \009if processes[id] then\
  10144. \009\009return processes[id].getEnv()\
  10145. \009else\
  10146. \009\009return false, \"No process with this ID\"\
  10147. \009end\
  10148. end\
  10149. \
  10150. local function subTokenize(nstr)\
  10151. \009local result = {}\
  10152. \009local current = \"\"\
  10153. \009local quotes = false\
  10154. \009local qtype = \"\"\
  10155. \009while nstr:sub(1,1) == \" \" do\
  10156. \009\009nstr = nstr:sub(2,-1)\
  10157. \009end\
  10158. \009for i=1, #nstr do\
  10159. \009\009local char = nstr:sub(i,i)\
  10160. \009\009if char == \"\\\"\" or char == \"'\" then\
  10161. \009\009\009if quotes then\
  10162. \009\009\009\009if qtype == char then\
  10163. \009\009\009\009\009quotes = false\
  10164. \009\009\009\009\009result[#result+1] = current\
  10165. \009\009\009\009\009current = \"\"\
  10166. \009\009\009\009\009qtype = \"\"\
  10167. \009\009\009\009else\
  10168. \009\009\009\009\009current = current..char\
  10169. \009\009\009\009end\
  10170. \009\009\009else\
  10171. \009\009\009\009quotes = true\
  10172. \009\009\009\009qtype = char\
  10173. \009\009\009\009if not (current == \"\") then\
  10174. \009\009\009\009\009result[#result+1] = current\
  10175. \009\009\009\009\009current = \"\"\
  10176. \009\009\009\009end\
  10177. \009\009\009end\
  10178. \009\009elseif char == \" \" then\
  10179. \009\009\009if quotes then\
  10180. \009\009\009\009if not (current == \"\") then\
  10181. \009\009\009\009\009current = current..char\
  10182. \009\009\009\009end\
  10183. \009\009\009else\
  10184. \009\009\009\009if not (current == \"\") then\
  10185. \009\009\009\009\009result[#result+1] = current\
  10186. \009\009\009\009\009current = \"\"\
  10187. \009\009\009\009end\
  10188. \009\009\009end\
  10189. \009\009elseif char == \"|\" then\
  10190. \009\009\009if quotes then\
  10191. \009\009\009\009current = current..char\
  10192. \009\009\009else\
  10193. \009\009\009\009if not (current == \"\") then\
  10194. \009\009\009\009\009result[#result+1] = current\
  10195. \009\009\009\009end\
  10196. \009\009\009\009result[#result+1] = \"|\"\
  10197. \009\009\009\009current = \"\"\
  10198. \009\009\009end\
  10199. \009\009else\
  10200. \009\009\009current = current..char\
  10201. \009\009end\
  10202. \009\009if i == #nstr and current ~= \"\" then\
  10203. \009\009\009result[#result+1] = current\
  10204. \009\009\009current = \"\"\
  10205. \009\009end\
  10206. \009end\
  10207. \009return result\
  10208. end\
  10209. \
  10210. local function fix(t)\
  10211. \009local first = t\
  10212. \009local path = OmniOS.getPath()\
  10213. \009if fs.exists(path..first) and not fs.isDir(path..first) then\
  10214. \009\009t = path..first\
  10215. \009\009return t\
  10216. \009end\
  10217. \009if fs.exists(path..first..\".lua\") and not fs.isDir(path..first..\".lua\") then\
  10218. \009\009t = path..first..\".lua\"\
  10219. \009\009return t\
  10220. \009end\
  10221. \009if fs.exists(\"OmniOS/bin/\"..first) and not fs.isDir(\"OmniOS/bin/\"..first) then\
  10222. \009\009t = \"OmniOS/bin/\"..first\
  10223. \009\009return t\
  10224. \009end\
  10225. \009if fs.exists(\"OmniOS/bin/\"..first..\".lua\") and not fs.isDir(\"OmniOS/bin/\"..first..\".lua\") then\
  10226. \009\009t = \"OmniOS/bin/\"..first..\".lua\"\
  10227. \009\009return t\
  10228. \009end\
  10229. \009if aliases[first] then\
  10230. \009\009t = \"OmniOS/bin/\"..aliases[first]..\".lua\"\
  10231. \009\009return t\
  10232. \009end\
  10233. \009return t, 0\
  10234. end\
  10235. \
  10236. local function tokenize(str)\
  10237. \009local tokens = subTokenize(str)\
  10238. \009local parts = {[1] = {}}\
  10239. \009local index = 1\
  10240. \009local subindex = 1\
  10241. \009for i,v in pairs(tokens) do\
  10242. \009\009if v ==\"|\" then\
  10243. \009\009\009index = index + 1\
  10244. \009\009\009parts[index] = {}\
  10245. \009\009\009subindex = 1\
  10246. \009\009else\
  10247. \009\009\009parts[index]\
  10248. \009\009\009[subindex] = v\
  10249. \009\009\009subindex = subindex + 1\
  10250. \009\009end\
  10251. \009end\
  10252. \009return parts\
  10253. end\
  10254. \
  10255. local function getFileContent(pth)\
  10256. \009local file = fs.open(pth,\"r\")\
  10257. \009local data = file.readAll()\
  10258. \009file.close()\
  10259. \009return loadstring(data, pth)\
  10260. end\
  10261. \
  10262. local function run(input)\
  10263. \009local parsed = tokenize(input)\
  10264. \009local code = nil\
  10265. \009local which\
  10266. \009for i=1, #parsed do\
  10267. \009\009parsed[i][1], code = fix(parsed[i][1])\
  10268. \009\009if code == 0 then\
  10269. \009\009\009which = which or i\
  10270. \009\009end\
  10271. \009end\
  10272. \009if code then\
  10273. \009\009print(parsed[which][1]..\" does not exist.\")\
  10274. \009\009return false\
  10275. \009end\
  10276. \009local ids = {}\
  10277. \009if parsed[2] then\
  10278. \009\009doPrint = false\
  10279. \009\009lib.log.stdout(\"Found several programs with piping.\")\
  10280. \009\009local pipes = {}\
  10281. \009\009local handles = {}\
  10282. \009\009for i=1, #parsed do\
  10283. \009\009\009if i > 1 then\
  10284. \009\009\009\009local id, handle = kernel.newProc(getFileContent(parsed[i][1]), \"term\", 1, parsed[i][1], {[\"log\"] = lib.log}, unpack(parsed[i], 2))\
  10285. \009\009\009\009pipes[i-1] = kernel.newPipe(id)\
  10286. \009\009\009\009handles[i-1].pipe(pipes[i-1].deathHandle)\
  10287. \009\009\009\009handles[i] = handle\
  10288. \009\009\009else\
  10289. \009\009\009\009local id, handle = kernel.newProc(getFileContent(parsed[i][1]), \"term\", 1, parsed[i][1], {[\"log\"] = lib.log}, unpack(parsed[i], 2))\
  10290. \009\009\009\009handles[i] = handle\
  10291. \009\009\009end\
  10292. \009\009end\
  10293. \009\009for i=1, #parsed do\009\
  10294. \009\009\009lib.log.stdout(\"Initialized\", parsed[i][1])\
  10295. \009\009\009--handles[i].onDeath(onDeath)\
  10296. \009\009\009if i == 1 then\
  10297. \009\009\009\009handles[i].stdout(pipes[1].write)\
  10298. \009\009\009\009handles[i].sendEvents(true)\
  10299. \009\009\009\009handles[i].getOutput(true)\
  10300. \009\009\009elseif i == #parsed then\
  10301. \009\009\009\009handles[i].stdin(pipes[i-1].read)\
  10302. \009\009\009\009handles[i].getOutput(true)\
  10303. \009\009\009else\
  10304. \009\009\009\009handles[i].stdout(pipes[1].write)\
  10305. \009\009\009\009handles[i].stdin(pipes[i-1].read)\
  10306. \009\009\009\009handles[i].getOutput(true)\
  10307. \009\009\009end \
  10308. \009\009end\
  10309. \009\009runningChildren = #parsed\
  10310. \009\009lib.log.stdclose()\
  10311. \009else\
  10312. \009\009--dofile(parsed[1][1], unpack(parsed[1], 2))\
  10313. \009\009local loadedfile, err = getFileContent(parsed[1][1])\
  10314. \009\009if not loadedfile then print(err) end\
  10315. \009\009local env = getEnv()\
  10316. \009\009for i,v in pairs(lib.sandbox({[\"OmniOS\"] = OmniOS})) do\
  10317. \009\009\009env[i] = v\
  10318. \009\009end\
  10319. \009\009setfenv(loadedfile, env)\
  10320. \009\009local ok, err = pcall(loadedfile,unpack(parsed[1], 2))\
  10321. \009\009if not ok then print(err) end\
  10322. \009end\
  10323. end\
  10324. \
  10325. local function isEmpty(str)\
  10326. \009local yes = true\
  10327. \009for i=1, #str do\
  10328. \009\009yes = str:sub(i,i) == \" \" and yes \
  10329. \009end\
  10330. \009return yes\
  10331. end\
  10332. \
  10333. --lib realm\
  10334. \
  10335. \
  10336. function OmniOS.register(extension, path)\
  10337. \009if type(extension) == \"string\" and type(path) == \"string\" then\
  10338. \009\009usr_dep[extension] = path\
  10339. \009\009local file = fs.open(\"OmniOS/usr/cfg/filetype_dependencies.cfg\",\"w\")\
  10340. \009\009file.write(lib.textutils.serialise(usr_dep))\
  10341. \009\009file.close()\
  10342. \009\009dependencies[extension] = path\
  10343. \009else\
  10344. \009\009return false, \"Arguments have to be strings. \"..type(extension)..\" \"..type(path)\
  10345. \009end\
  10346. end\
  10347. \
  10348. function OmniOS.execute(input)\
  10349. \009if not isEmpty(input) then\
  10350. \009\009return run(input)\
  10351. \009else\
  10352. \009\009return false, \"Input was empty.\"\
  10353. \009end\
  10354. end\
  10355. \
  10356. function OmniOS.launch(program,parent,...)\
  10357. \009local file = fs.open(\"OmniOS/programs/\"..program..\"/main.lua\", \"r\")\
  10358. \009local data = file.readAll()\
  10359. \009file.close()\
  10360. \009local fx = loadstring(data, program)\
  10361. \009kernel.newProc(fx, parent, 0, program, {}, ...)\
  10362. end\
  10363. \
  10364. function OmniOS.newProc(...)\
  10365. \009kernel.newProc(...)\
  10366. end\
  10367. \
  10368. function OmniOS.message(thread,message)\
  10369. \009kernel.addMessage(thread,Kernel.getRunning(),message)\
  10370. end\
  10371. \
  10372. function OmniOS.getName(...)\
  10373. \009return kernel.getName(...)\
  10374. end\
  10375. function OmniOS.getRunning()\
  10376. \009return kernel.getRunning()\
  10377. end\
  10378. function OmniOS.getMessages()\
  10379. \009return kernel.getMessages(kernel.getRunning())\
  10380. end\
  10381. \
  10382. function OmniOS.broadcast(message)\
  10383. \009kernel.broadcast(kernel.getRunning(),message)\
  10384. end\
  10385. \
  10386. function OmniOS.getParent()\
  10387. \009return kernel.getParent(kernel.getRunning())\
  10388. end\
  10389. \
  10390. function OmniOS.switch(id)\
  10391. \009kernel.switch(id)\
  10392. end\
  10393. \
  10394. function OmniOS.list()\
  10395. \009return kernel.list()\
  10396. end\
  10397. \
  10398. function OmniOS.reboot()\
  10399. \009kernel.reboot()\
  10400. end\
  10401. \
  10402. function OmniOS.init()\
  10403. \009local file = fs.open(\"OmniOS/etc/os/filetype_dependencies.cfg\",\"r\")\
  10404. \009if file then\
  10405. \009\009dependencies = lib.textutils.unserialize(file.readAll())\
  10406. \009\009file.close()\
  10407. \009end\
  10408. \009local dep = {}\
  10409. \009local file = fs.open(\"OmniOS/usr/cfg/filetype_dependencies.cfg\",\"r\")\
  10410. \009if file then\
  10411. \009\009usr_dep = lib.textutils.unseralize(file.readAll()) or {}\
  10412. \009\009file.close()\
  10413. \009end\
  10414. \009for i,v in pairs(usr_dep) do\
  10415. \009\009dependencies[i] = v\
  10416. \009end\
  10417. \
  10418. \009local file = fs.open(\"OmniOS/etc/os/aliases.cfg\",\"r\")\
  10419. \009if file then\
  10420. \009\009aliases = lib.textutils.unserialize(file.readAll())\
  10421. \009\009file.close()\
  10422. \009end\
  10423. \009local file = fs.open(\"OmniOS/usr/cfg/aliases.cfg\",\"r\")\
  10424. \009if file then\
  10425. \009\009usr_aliases = lib.textutils.unseralize(file.readAll()) or {}\
  10426. \009\009file.close()\
  10427. \009end\
  10428. \009for i,v in pairs(usr_aliases) do\
  10429. \009\009aliases[i] = v\
  10430. \009end\
  10431. end\
  10432. \
  10433. function OmniOS.setPath(path)\
  10434. \009if type(path) == \"string\" then\
  10435. \009\009local id = kernel.getRunning()\
  10436. \009\009if type(id) == \"number\" and processes[kernel.getRunning()] then\
  10437. \009\009\009processes[kernel.getRunning()].path = fs.combine(path, \"/\")\
  10438. \009\009else\
  10439. \009\009\009return false, \"process does not exist\"\
  10440. \009\009end\
  10441. \009else\
  10442. \009\009return false, \"path has to be a string\"\
  10443. \009end\
  10444. end\
  10445. \
  10446. function OmniOS.getPath()\
  10447. \009local id = kernel.getRunning()\
  10448. \009if type(id) == \"number\" and processes[kernel.getRunning()] then\
  10449. \009\009return processes[kernel.getRunning()].path..\"/\"\
  10450. \009else\
  10451. \009\009return false, \"process does not exist\"\
  10452. \009end\
  10453. end\
  10454. \
  10455. function OmniOS.exit()\
  10456. \009kernel.kill(kernel.getRunning())\
  10457. end\
  10458. \
  10459. function OmniOS.resolvePath(pth)\
  10460. \009if type(pth) == \"string\" then\
  10461. \009\009if pth:sub(1,1) == \"/\" then\
  10462. \009\009\009return fs.combine(pth, \"\")\
  10463. \009\009else\
  10464. \009\009\009return fs.combine(OmniOS.getPath(), pth)\
  10465. \009\009end\
  10466. \009else\
  10467. \009\009return false, \"<path> has to be type string.\"\
  10468. \009end\
  10469. end\
  10470. \
  10471. function OmniOS.kill(id) --fix perms\
  10472. \009return kernel.kill(id)\
  10473. end\
  10474. \
  10475. function OmniOS.fix(str)\
  10476. \009if type(str) == \"string\" then\
  10477. \009\009return fix(str)\
  10478. \009else\
  10479. \009\009return false, \"Not a string\"\
  10480. \009end\
  10481. end\
  10482. \
  10483. return OmniOS",
  10484.       [ "window.lua" ] = "local kernel, processes, windows, lib = ...\
  10485. local lll = lib.log.log\
  10486. \
  10487. local tHex = {\
  10488.    [ colors.white ] = \"0\",\
  10489.    [ colors.orange ] = \"1\",\
  10490.    [ colors.magenta ] = \"2\",\
  10491.    [ colors.lightBlue ] = \"3\",\
  10492.    [ colors.yellow ] = \"4\",\
  10493.    [ colors.lime ] = \"5\",\
  10494.    [ colors.pink ] = \"6\",\
  10495.    [ colors.gray ] = \"7\",\
  10496.    [ colors.lightGray ] = \"8\",\
  10497.    [ colors.cyan ] = \"9\",\
  10498.    [ colors.purple ] = \"a\",\
  10499.    [ colors.blue ] = \"b\",\
  10500.    [ colors.brown ] = \"c\",\
  10501.    [ colors.green ] = \"d\",\
  10502.    [ colors.red ] = \"e\",\
  10503.    [ colors.black ] = \"f\",\
  10504. }\
  10505. \
  10506. local string_rep = string.rep\
  10507. local string_sub = string.sub\
  10508. \
  10509. return function( parent, nX, nY, nWidth, nHeight, bStartVisible )\
  10510. \
  10511.    if type( parent ) ~= \"table\" or\
  10512.       type( nX ) ~= \"number\" or\
  10513.       type( nY ) ~= \"number\" or\
  10514.       type( nWidth ) ~= \"number\" or\
  10515.       type( nHeight ) ~= \"number\" or\
  10516.       (bStartVisible ~= nil and type( bStartVisible ) ~= \"boolean\") then\
  10517.        error( \"Expected object, number, number, number, number, [boolean]\", 2 )\
  10518.    end\
  10519. \
  10520.    if parent == term then\
  10521.        error( \"term is not a recommended window parent, try term.current() instead\", 2 )\
  10522.    end\
  10523. \
  10524.    local sEmptySpaceLine\
  10525.    local tEmptyColorLines = {}\
  10526.    local function createEmptyLines( nWidth )\
  10527.        sEmptySpaceLine = string_rep( \" \", nWidth )\
  10528.        for n=0,15 do\
  10529.            local nColor = 2^n\
  10530.            local sHex = tHex[nColor]\
  10531.            tEmptyColorLines[nColor] = string_rep( sHex, nWidth )\
  10532.        end\
  10533.    end\
  10534. \
  10535.    createEmptyLines( nWidth )\
  10536. \
  10537.    -- Setup\
  10538.    local bVisible = (bStartVisible ~= false)\
  10539.    local nCursorX = 1\
  10540.    local nCursorY = 1\
  10541.    local bCursorBlink = false\
  10542.    local nTextColor = colors.white\
  10543.    local nBackgroundColor = colors.black\
  10544.    local tLines = {}\
  10545.    do\
  10546.        local sEmptyText = sEmptySpaceLine\
  10547.        local sEmptyTextColor = tEmptyColorLines[ nTextColor ]\
  10548.        local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]\
  10549.        for y=1,nHeight do\
  10550.            tLines[y] = {\
  10551.                text = sEmptyText,\
  10552.                textColor = sEmptyTextColor,\
  10553.                backgroundColor = sEmptyBackgroundColor,\
  10554.            }\
  10555.        end\
  10556.    end\
  10557. \
  10558.    -- Helper functions\
  10559.    local function updateCursorPos()\
  10560.        if nCursorX >= 1 and nCursorY >= 1 and\
  10561.           nCursorX <= nWidth and nCursorY <= nHeight then\
  10562.            parent.setCursorPos( nX + nCursorX - 1, nY + nCursorY - 1 )\
  10563.        else\
  10564.            parent.setCursorPos( 0, 0 )\
  10565.        end\
  10566.    end\
  10567.    \
  10568.    local function updateCursorBlink()\
  10569.        parent.setCursorBlink( bCursorBlink )\
  10570.    end\
  10571.    \
  10572.    local function updateCursorColor()\
  10573.        parent.setTextColor( nTextColor )\
  10574.    end\
  10575.    \
  10576.    local function redrawLine( n )\
  10577.        local tLine = tLines[ n ]\
  10578.        parent.setCursorPos( nX, nY + n - 1 )\
  10579.        parent.blit( tLine.text, tLine.textColor, tLine.backgroundColor )\
  10580.    end\
  10581. \
  10582.    local function redraw()\
  10583.        for n=1,nHeight do\
  10584.            redrawLine( n )\
  10585.        end\
  10586.    end\
  10587. \
  10588.    local function internalBlit( sText, sTextColor, sBackgroundColor )\
  10589.        local nStart = nCursorX\
  10590.        local nEnd = nStart + #sText - 1\
  10591.        if nCursorY >= 1 and nCursorY <= nHeight then\
  10592.            if nStart <= nWidth and nEnd >= 1 then\
  10593.                -- Modify line\
  10594.                local tLine = tLines[ nCursorY ]\
  10595.                if nStart == 1 and nEnd == nWidth then\
  10596.                    tLine.text = sText\
  10597.                    tLine.textColor = sTextColor\
  10598.                    tLine.backgroundColor = sBackgroundColor\
  10599.                else\
  10600.                    local sClippedText, sClippedTextColor, sClippedBackgroundColor\
  10601.                    if nStart < 1 then\
  10602.                        local nClipStart = 1 - nStart + 1\
  10603.                        local nClipEnd = nWidth - nStart + 1\
  10604.                        sClippedText = string_sub( sText, nClipStart, nClipEnd )\
  10605.                        sClippedTextColor = string_sub( sTextColor, nClipStart, nClipEnd )\
  10606.                        sClippedBackgroundColor = string_sub( sBackgroundColor, nClipStart, nClipEnd )\
  10607.                    elseif nEnd > nWidth then\
  10608.                        local nClipEnd = nWidth - nStart + 1\
  10609.                        sClippedText = string_sub( sText, 1, nClipEnd )\
  10610.                        sClippedTextColor = string_sub( sTextColor, 1, nClipEnd )\
  10611.                        sClippedBackgroundColor = string_sub( sBackgroundColor, 1, nClipEnd )\
  10612.                    else\
  10613.                        sClippedText = sText\
  10614.                        sClippedTextColor = sTextColor\
  10615.                        sClippedBackgroundColor = sBackgroundColor\
  10616.                    end\
  10617. \
  10618.                    local sOldText = tLine.text\
  10619.                    local sOldTextColor = tLine.textColor\
  10620.                    local sOldBackgroundColor = tLine.backgroundColor\
  10621.                    local sNewText, sNewTextColor, sNewBackgroundColor\
  10622.                    if nStart > 1 then\
  10623.                        local nOldEnd = nStart - 1\
  10624.                        sNewText = string_sub( sOldText, 1, nOldEnd ) .. sClippedText\
  10625.                        sNewTextColor = string_sub( sOldTextColor, 1, nOldEnd ) .. sClippedTextColor\
  10626.                        sNewBackgroundColor = string_sub( sOldBackgroundColor, 1, nOldEnd ) .. sClippedBackgroundColor\
  10627.                    else\
  10628.                        sNewText = sClippedText\
  10629.                        sNewTextColor = sClippedTextColor\
  10630.                        sNewBackgroundColor = sClippedBackgroundColor\
  10631.                    end\
  10632.                    if nEnd < nWidth then\
  10633.                        local nOldStart = nEnd + 1\
  10634.                        sNewText = sNewText .. string_sub( sOldText, nOldStart, nWidth )\
  10635.                        sNewTextColor = sNewTextColor .. string_sub( sOldTextColor, nOldStart, nWidth )\
  10636.                        sNewBackgroundColor = sNewBackgroundColor .. string_sub( sOldBackgroundColor, nOldStart, nWidth )\
  10637.                    end\
  10638. \
  10639.                    tLine.text = sNewText\
  10640.                    tLine.textColor = sNewTextColor\
  10641.                    tLine.backgroundColor = sNewBackgroundColor\
  10642.                end\
  10643. \
  10644.                -- Redraw line\
  10645.                if bVisible then\
  10646.                    redrawLine( nCursorY )\
  10647.                end\
  10648.            end\
  10649.        end\
  10650. \
  10651.        -- Move and redraw cursor\
  10652.        nCursorX = nEnd + 1\
  10653.        if bVisible then\
  10654.            updateCursorColor()\
  10655.            updateCursorPos()\
  10656.        end\
  10657.    end\
  10658. \
  10659.    -- Terminal implementation\
  10660.    local window = {}\
  10661. \
  10662.    function window.write( sText )\
  10663.        sText = tostring( sText )\
  10664.        internalBlit( sText, string_rep( tHex[ nTextColor ], #sText ), string_rep( tHex[ nBackgroundColor ], #sText ) )\
  10665.    end\
  10666. \
  10667.    function window.blit( sText, sTextColor, sBackgroundColor )\
  10668.        if type(sText) ~= \"string\" or type(sTextColor) ~= \"string\" or type(sBackgroundColor) ~= \"string\" then\
  10669.            error( \"Expected string, string, string\", 2 )\
  10670.        end\
  10671.        if #sTextColor ~= #sText or #sBackgroundColor ~= #sText then\
  10672.            error( \"Arguments must be the same length\", 2 )\
  10673.        end\
  10674.        internalBlit( sText, sTextColor, sBackgroundColor )\
  10675.    end\
  10676. \
  10677.    function window.clear()\
  10678.        local sEmptyText = sEmptySpaceLine\
  10679.        local sEmptyTextColor = tEmptyColorLines[ nTextColor ]\
  10680.        local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]\
  10681.        for y=1,nHeight do\
  10682.            tLines[y] = {\
  10683.                text = sEmptyText,\
  10684.                textColor = sEmptyTextColor,\
  10685.                backgroundColor = sEmptyBackgroundColor,\
  10686.            }\
  10687.        end\
  10688.        if bVisible then\
  10689.            redraw()\
  10690.            updateCursorColor()\
  10691.            updateCursorPos()\
  10692.        end\
  10693.    end\
  10694. \
  10695.    function window.clearLine()\
  10696.        if nCursorY >= 1 and nCursorY <= nHeight then\
  10697.            local sEmptyText = sEmptySpaceLine\
  10698.            local sEmptyTextColor = tEmptyColorLines[ nTextColor ]\
  10699.            local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]\
  10700.            tLines[ nCursorY ] = {\
  10701.                text = sEmptyText,\
  10702.                textColor = sEmptyTextColor,\
  10703.                backgroundColor = sEmptyBackgroundColor,\
  10704.            }\
  10705.            if bVisible then\
  10706.                redrawLine( nCursorY )\
  10707.                updateCursorColor()\
  10708.                updateCursorPos()\
  10709.            end\
  10710.        end\
  10711.    end\
  10712. \
  10713.    function window.getCursorPos()\
  10714.        return nCursorX, nCursorY\
  10715.    end\
  10716. \
  10717.    function window.setCursorPos( x, y )\
  10718.        nCursorX = math.floor( x )\
  10719.        nCursorY = math.floor( y )\
  10720.        if bVisible then\
  10721.            updateCursorPos()\
  10722.        end\
  10723.    end\
  10724. \
  10725.    function window.setCursorBlink( blink )\
  10726.        bCursorBlink = blink\
  10727.        if bVisible then\
  10728.            updateCursorBlink()\
  10729.        end\
  10730.    end\
  10731. \
  10732.    local function isColor()\
  10733.        return parent.isColor()\
  10734.    end\
  10735. \
  10736.    function window.isColor()\
  10737.        return isColor()\
  10738.    end\
  10739. \
  10740.    function window.isColour()\
  10741.        return isColor()\
  10742.    end\
  10743. \
  10744.    local function setTextColor( color )\
  10745.        if not parent.isColor() then\
  10746.            if color ~= colors.white and color ~= colors.black and color ~= colors.gray and color ~= colors.lightGray then\
  10747.                error( \"Color not supported\", 3 )\
  10748.            end\
  10749.        end\
  10750.        nTextColor = color\
  10751.        if bVisible then\
  10752.            updateCursorColor()\
  10753.        end\
  10754.    end\
  10755. \
  10756.    function window.setTextColor( color )\
  10757.        setTextColor( color )\
  10758.    end\
  10759. \
  10760.    function window.setTextColour( color )\
  10761.        setTextColor( color )\
  10762.    end\
  10763. \
  10764.    local function setBackgroundColor( color )\
  10765.        if not parent.isColor() then\
  10766.            if color ~= colors.white and color ~= colors.black and color ~= colors.gray and color ~= colors.lightGray then\
  10767.                error( \"Color not supported\", 3 )\
  10768.            end\
  10769.        end\
  10770.        nBackgroundColor = color\
  10771.    end\
  10772. \
  10773.    function window.setBackgroundColor( color )\
  10774.        setBackgroundColor( color )\
  10775.    end\
  10776. \
  10777.    function window.setBackgroundColour( color )\
  10778.        setBackgroundColor( color )\
  10779.    end\
  10780. \
  10781.    function window.getSize()\
  10782.        return nWidth, nHeight\
  10783.    end\
  10784. \
  10785.    function window.scroll( n )\
  10786.        if n ~= 0 then\
  10787.            local tNewLines = {}\
  10788.            local sEmptyText = sEmptySpaceLine\
  10789.            local sEmptyTextColor = tEmptyColorLines[ nTextColor ]\
  10790.            local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]\
  10791.            for newY=1,nHeight do\
  10792.                local y = newY + n\
  10793.                if y >= 1 and y <= nHeight then\
  10794.                    tNewLines[newY] = tLines[y]\
  10795.                else\
  10796.                    tNewLines[newY] = {\
  10797.                        text = sEmptyText,\
  10798.                        textColor = sEmptyTextColor,\
  10799.                        backgroundColor = sEmptyBackgroundColor,\
  10800.                    }\
  10801.                end\
  10802.            end\
  10803.            tLines = tNewLines\
  10804.            if bVisible then\
  10805.                redraw()\
  10806.                updateCursorColor()\
  10807.                updateCursorPos()\
  10808.            end\
  10809.        end\
  10810.    end\
  10811. \
  10812.    function window.getTextColor()\
  10813.        return nTextColor\
  10814.    end\
  10815. \
  10816.    function window.getTextColour()\
  10817.        return nTextColor\
  10818.    end\
  10819. \
  10820.    function window.getBackgroundColor()\
  10821.        return nBackgroundColor\
  10822.    end\
  10823. \
  10824.    function window.getBackgroundColour()\
  10825.        return nBackgroundColor\
  10826.    end\
  10827. \
  10828.    -- Other functions\
  10829.    function window.setVisible( bVis )\
  10830.        if bVisible ~= bVis then\
  10831.            bVisible = bVis\
  10832.            if bVisible then\
  10833.                window.redraw()\
  10834.            end\
  10835.        end\
  10836.    end\
  10837. \
  10838.    function window.redraw()\
  10839.        if bVisible then\
  10840.            redraw()\
  10841.            updateCursorBlink()\
  10842.            updateCursorColor()\
  10843.            updateCursorPos()\
  10844.        end\
  10845.    end\
  10846. \
  10847.    function window.restoreCursor()\
  10848.        if bVisible then\
  10849.            updateCursorBlink()\
  10850.            updateCursorColor()\
  10851.            updateCursorPos()\
  10852.        end\
  10853.    end\
  10854. \
  10855.    function window.getPosition()\
  10856.        return nX, nY\
  10857.    end\
  10858. \
  10859.    function window.reposition( nNewX, nNewY, nNewWidth, nNewHeight )\
  10860.        nX = nNewX\
  10861.        nY = nNewY\
  10862.        if nNewWidth and nNewHeight then\
  10863.            local tNewLines = {}\
  10864.            createEmptyLines( nNewWidth )\
  10865.            local sEmptyText = sEmptySpaceLine\
  10866.            local sEmptyTextColor = tEmptyColorLines[ nTextColor ]\
  10867.            local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]\
  10868.            for y=1,nNewHeight do\
  10869.                if y > nHeight then\
  10870.                    tNewLines[y] = {\
  10871.                        text = sEmptyText,\
  10872.                        textColor = sEmptyTextColor,\
  10873.                        backgroundColor = sEmptyBackgroundColor\
  10874.                    }\
  10875.                else\
  10876.                    local tOldLine = tLines[y]\
  10877.                    if nNewWidth == nWidth then\
  10878.                        tNewLines[y] = tOldLine\
  10879.                    elseif nNewWidth < nWidth then\
  10880.                        tNewLines[y] = {\
  10881.                            text = string_sub( tOldLine.text, 1, nNewWidth ),\
  10882.                            textColor = string_sub( tOldLine.textColor, 1, nNewWidth ),\
  10883.                            backgroundColor = string_sub( tOldLine.backgroundColor, 1, nNewWidth ),\
  10884.                        }\
  10885.                    else\
  10886.                        tNewLines[y] = {\
  10887.                            text = tOldLine.text .. string_sub( sEmptyText, nWidth + 1, nNewWidth ),\
  10888.                            textColor = tOldLine.textColor .. string_sub( sEmptyTextColor, nWidth + 1, nNewWidth ),\
  10889.                            backgroundColor = tOldLine.backgroundColor .. string_sub( sEmptyBackgroundColor, nWidth + 1, nNewWidth ),\
  10890.                        }\
  10891.                    end\
  10892.                end\
  10893.            end\
  10894.            nWidth = nNewWidth\
  10895.            nHeight = nNewHeight\
  10896.            tLines = tNewLines\
  10897.        end\
  10898.        if bVisible then\
  10899.            window.redraw()\
  10900.        end\
  10901.    end\
  10902. \
  10903.    if bVisible then\
  10904.        window.redraw()\
  10905.    end\
  10906.    return window\
  10907. end",
  10908.       [ "process.lua" ] = "--[[\
  10909. \009Process handler\009\
  10910. ]]\
  10911. \
  10912. local kernel, processes, windows, lib = ...\
  10913. \
  10914. local process = {}\
  10915. \
  10916. function process.init(inter, func, parent, level, id, name, env, info, ...)\
  10917. \009--lib.log.log(\"process\", inter, func, parent, level, id, name, env, info, ...)\
  10918. \009env = env or lib.sandbox({})\
  10919. \009if not (type(func) == \"function\") then\
  10920. \009\009return false, \"Argument func has to be a function.\"\
  10921. \009end\
  10922. \009if not (type(level) == \"number\") then\
  10923. \009\009return false, \"Argument level has to be a number.\"\
  10924. \009end\
  10925. \009if not (type(id) == \"number\") then\
  10926. \009\009return false, \"Argument level has to be a number.\"\
  10927. \009end\
  10928. \009name = name or \"Nameless\"\
  10929. \009if not (type(name) == \"string\") then\
  10930. \009\009return false, \"Argument name has to be a string.\"\
  10931. \009end\
  10932. \
  10933. \009setfenv(func, env)\
  10934. \009\
  10935. \009local process = coroutine.create(func)\
  10936. \009local filter = nil\
  10937. \009local first, ok, err\
  10938. \009local parent = parent\
  10939. \
  10940. \009--Public\
  10941. \009local self = {}\
  10942. \
  10943. \009self.info = info\
  10944. \
  10945. \009function self.isDead()\
  10946. \009\009local sts = coroutine.status(process)\
  10947. \009\009if sts == \"dead\" then\
  10948. \009\009\009if info.pipe then\
  10949. \009\009\009\009--lib.log.stdout(\"Closing pipe from id \", id)\
  10950. \009\009\009\009info.pipe()\
  10951. \009\009\009end\
  10952. \009\009\009if info.getInput then\
  10953. \009\009\009\009processes[info.getInputFrom].redirectingEventsTo = false\
  10954. \009\009\009end\
  10955. \009\009\009if info.onDeath then\
  10956. \009\009\009\009info.onDeath()\
  10957. \009\009\009end\
  10958. \009\009\009return true\
  10959. \009\009end\
  10960. \009end\
  10961. \
  10962. \009function self.setVisible(isVisible)\
  10963. \009\009window.setVisible(isVisible)\
  10964. \009end\
  10965. \
  10966. \009function self.resume(...)\
  10967. \009\009lib.log.log(\"mt\", \"Resuming process\", id)\
  10968. \009\009first = ...\
  10969. \
  10970. \
  10971. \009\009if self.redirectingEventsTo and processes[self.redirectingEventsTo] then\
  10972. \009\009\009lib.log.log(\"mt\", \"Attempting redirection to process \", self.redirectingEventsTo)\
  10973. \009\009\009if processes[self.redirectingEventsTo].resume then\
  10974. \009\009\009\009--lib.log.stdout(\"Process \", id, \" is sending output to \", self.redirectingEventsTo, ...)\
  10975. \009\009\009\009lib.log.log(\"mt\", \"Redirecting to process \", self.redirectingEventsTo)\
  10976. \009\009\009\009processes[self.redirectingEventsTo].resume(...)\
  10977. \009\009\009end\
  10978. \009\009\009return\
  10979. \009\009end\
  10980. \
  10981. \009\009if self.isDead() then\
  10982. \009\009\009kernel.kill(id)\
  10983. \009\009\009return\
  10984. \009\009end\
  10985. \
  10986. \009\009if filter == nil or first == filter then\
  10987. \009\009\009--Window switching magic\
  10988. \009\009\009inter.setRunning(id)\
  10989. \009\009\009\
  10990. \009\009\009term.redirect(windows[info.windowID])\
  10991. \009\009\009if info.windowID == inter.getActive() then\
  10992. \009\009\009\009--lib.log.log(\"window\", name, \"has process id\", id, \"and window id\", info.windowID, windows[info.windowID], windows[id], currTerm)\
  10993. \009\009\009\009windows[info.windowID].setVisible(true)\
  10994. \009\009\009\009--windows[info.windowID].redraw()\
  10995. \009\009\009\009windows[info.windowID].restoreCursor()\
  10996. \009\009\009end\
  10997. \009\009\009--lib.log.log(\"varagr\", id, name, ...)\
  10998. \009\009\009ok, err = coroutine.resume(process, ...)\
  10999. \009\009\009term.redirect(inter.currTerm)\
  11000. \
  11001. \009\009\009--window.setVisible(false)\
  11002. \
  11003. \009\009\009if self.isDead() or not ok then\
  11004. \009\009\009\009lib.log.log(\"Error\", id, name, tostring(err)..\"\\n\")\
  11005. \009\009\009\009kernel.kill(id)\
  11006. \009\009\009\009return\
  11007. \009\009\009else\
  11008. \009\009\009\009filter = err\
  11009. \009\009\009end\
  11010. \009\009else\
  11011. \009\009\009return true\
  11012. \009\009end\
  11013. \009end\
  11014. \
  11015. \009function self.redraw()\
  11016. \009\009local wID = info.windowID\
  11017. \009\009windows[wID].setVisible(true)\
  11018. \009\009windows[wID].redraw()\
  11019. \009\009windows[wID].setVisible(false)\
  11020. \009\009--term.setCursorBlink(false)\
  11021. \009end\
  11022. \
  11023. \009function self.getName()\
  11024. \009\009return name\
  11025. \009end\
  11026. \
  11027. \009function self.getLevel()\
  11028. \009\009return level\
  11029. \009end\
  11030. \
  11031. \009function self.getStatus()\
  11032. \009\009return coroutine.status(process)\
  11033. \009end\
  11034. \
  11035. \009function self.getParent()\
  11036. \009\009return parent\
  11037. \009end\
  11038. \
  11039. \009function self.getEnv()\
  11040. \009\009return env\
  11041. \009end\
  11042. \
  11043. \009function self.getID()\
  11044. \009\009return id\
  11045. \009end\
  11046. \
  11047. \009self.waiting = nil\
  11048. \009self.ready = nil\
  11049. \009self.path = \"\"\
  11050. \009self.resume(...)\
  11051. \009self.created = true\
  11052. \
  11053. \009processes[id] = self\
  11054. \009lib.log.stdout(\"Done loading\", name)\
  11055. end\
  11056. \
  11057. return process",
  11058.       [ "pipe.lua" ] = "--[[\
  11059. \009Pipe library\
  11060. ]]\
  11061. \
  11062. local kernel, processes, windows, lib = ...\
  11063. \
  11064. local pipe = {}\
  11065. \
  11066. local function getFirst(t)\
  11067. \009local f = t[1]\
  11068. \009if #t > 1 then\
  11069. \009\009for i=1, #t - 1 do\
  11070. \009\009\009t[i] = t[i + 1]\
  11071. \009\009\009t[i + 1] = nil\
  11072. \009\009end\
  11073. \009else\
  11074. \009\009t[1] = nil\
  11075. \009end\
  11076. \009return f, t\
  11077. end\
  11078. \
  11079. function pipe.init(toID)\
  11080. \009lib.log.log(\"mt\", \"Creating pipe\", processes[toID], processes[toID])\
  11081. \009local queue = {}\
  11082. \009local reads = 0\
  11083. \009local inputIsDead = false\
  11084. \009return {\
  11085. \009\009write = function(...)\
  11086. \009\009\009lib.log.log(\"mt\", \"Writing to queue\", toID, \"Is the process waiting?\", processes[toID].waiting, tostring(processes[toID]))\
  11087. \009\009\009--kernel.resume(toID)\
  11088. \009\009\009for i,v in pairs({...}) do\
  11089. \009\009\009\009queue[#queue+1] = v\
  11090. \009\009\009end\
  11091. \009\009\009--if processes[toID].waiting then\
  11092. \009\009\009\009processes[toID].waiting = false\
  11093. \009\009\009\009processes[toID].ready = true\
  11094. \009\009\009--end\
  11095. \009\009end,\
  11096. \009\009read = function()\
  11097. \009\009\009lib.log.log(\"mt\", toID, \"is requesting read from \")\
  11098. \009\009\009if #queue == 0 then\
  11099. \009\009\009\009lib.log.log(\"mt\", \"Queue is empty, waiting\")\
  11100. \009\009\009\009if inputIsDead then\
  11101. \009\009\009\009\009lib.log.log(\"mt\", \"Input is ded\")\
  11102. \009\009\009\009\009--lib.log.stdout(\"Input is real dead\")\
  11103. \009\009\009\009\009return nil\
  11104. \009\009\009\009else\
  11105. \009\009\009\009\009processes[toID].waiting = true\
  11106. \009\009\009\009\009lib.log.log(\"mt\", toID, \"is waiting\", processes[toID].waiting)\
  11107. \009\009\009\009\009coroutine.yield()\
  11108. \009\009\009\009\009lib.log.log(\"mt\", toID, \"Got control\")\
  11109. \009\009\009\009\009local f, queue = getFirst(queue)\
  11110. \009\009\009\009\009lib.log.log(\"mt\", \"Returning\", f)\
  11111. \009\009\009\009\009return f\
  11112. \009\009\009\009end\
  11113. \009\009\009else\
  11114. \009\009\009\009lib.log.log(\"mt\", \"Queue is non-empty, waiting\")\
  11115. \009\009\009\009local f, queue = getFirst(queue)\
  11116. \009\009\009\009lib.log.log(\"mt\", \"Returning\", f)\
  11117. \009\009\009\009return f\
  11118. \009\009\009end\
  11119. \009\009end,\
  11120. \009\009deathhanle = function()\
  11121. \009\009\009--lib.log.stdout(\"Input died\")\
  11122. \009\009\009inputIsDead = true\
  11123. \009\009end\
  11124. \009}\
  11125. end\
  11126. \
  11127. return pipe",
  11128.       [ "sandbox.lua" ] = "--[[\
  11129. \009Sandbox\
  11130. \009environment\
  11131. \009by Creator\
  11132. \009for OmniOS\
  11133. ]]--\
  11134. \
  11135. local kernel, processes, windows, lib = ...\
  11136. \
  11137. local default = {}\
  11138. local exceptions = {}\
  11139. local oldGetfenv = getfenv\
  11140. \
  11141. --getfenv(rs.get)\
  11142. \
  11143. local function nCopy(t, nt, recr, env)\
  11144. \009if recr[tostring(t)] then\
  11145. \009\009for i,v in pairs(t) do\
  11146. \009\009\009nt[i] = v\
  11147. \009\009end\
  11148. \009\009return\
  11149. \009end\
  11150. \009for i,v in pairs(t) do\
  11151. \009\009if type(v) == \"table\" then\
  11152. \009\009\009local nnt = {}\
  11153. \009\009\009recr[tostring(v)] = nnt\
  11154. \009\009\009nt[i] = nnt\
  11155. \009\009\009nCopy(v, nnt, recr)\
  11156. \009\009else\
  11157. \009\009\009nt[i] = v\
  11158. \009\009end\
  11159. \009end\
  11160. end\
  11161. \
  11162. local function copy(t, env)\
  11163. \009if type(t) == \"table\" then\
  11164. \009\009local nTable = {}\
  11165. \009\009nCopy(t, nTable, {}, env)\
  11166. \009\009return nTable\
  11167. \009else\
  11168. \009\009return t\
  11169. \009end\
  11170. end\
  11171. \
  11172. for i, v in pairs(_G) do\
  11173. \009default[i] = true\
  11174. end\
  11175. \
  11176. \
  11177. local function generateGet(ret)\
  11178. \009local ret = ret\
  11179. \009return function(env)\
  11180. \009\009if env == nil then \
  11181. \009\009\009return ret \
  11182. \009\009elseif type( env ) == \"number\" and env > 0 then \
  11183. \009\009\009env = env + 1 \
  11184. \009\009end \
  11185. \009\009local fenv = getfenv(env)\
  11186. \009\009if fenv == _G then\
  11187. \009\009\009return ret\
  11188. \009\009end\
  11189. \009\009return fenv\
  11190. \009end\
  11191. end\
  11192. \
  11193. local function sandbox(include)\
  11194. \009local ret = {}\
  11195. \009for i,v in pairs(default) do\
  11196. \009\009if i == \"_G\" then\
  11197. \009\009\009ret._G = ret\
  11198. \009\009elseif i == \"getfenv\" then\
  11199. \009\009\009ret[i] = generateGet(ret)\
  11200. \009\009else\
  11201. \009\009\009ret[i] = copy(_G[i], ret)\
  11202. \009\009end\
  11203. \009end\
  11204. \009if type(include) == \"table\" then\
  11205. \009\009for i,v in pairs(include) do\
  11206. \009\009\009ret[i] = copy(include[i], ret)\
  11207. \009\009end\
  11208. \009end\
  11209. \
  11210. \009return ret\
  11211. end\
  11212. \
  11213. return sandbox",
  11214.       [ "thread.lua" ] = "--[[\
  11215. \009Thread library\
  11216. ]]--\
  11217. \
  11218. local kernel, processes, windows, lib = ...\
  11219. local nW = lib.window\
  11220. \
  11221. return function(func, parent, level, env, name,  ...)\
  11222. \009\
  11223. \009if not (type(env) == \"table\") then\
  11224. \009\009return false, \"Argument env has to be a table.\"\
  11225. \009end \
  11226. \009if not (type(func) == \"function\") then\
  11227. \009\009return false, \"Argument func has to be a function.\"\
  11228. \009end\
  11229. \009if not (type(level) == \"number\") then\
  11230. \009\009return false, \"Argument level has to be a number.\"\
  11231. \009end\
  11232. \009name = name or \"Nameless\"\
  11233. \009if not (type(name) == \"string\") then\
  11234. \009\009return false, \"Argument name has to be a string.\"\
  11235. \009end\
  11236. \
  11237. \009setfenv(func, env)\
  11238. \009local process = coroutine.create(func)\
  11239. \009local filter = nil\
  11240. \009local first, ok, err\
  11241. \009local parent = parent\
  11242. \009local window = nW(parent, 1, 1, 51, 19, false)\
  11243. \
  11244. \009ok, err = coroutine.resume(process, ...)\
  11245. \009if ok then\
  11246. \009\009filter = err\
  11247. \009else\
  11248. \009\009print(err)\
  11249. \009\009return false, err\
  11250. \009end\
  11251. \
  11252. \009--Public\
  11253. \009local self = {}\
  11254. \
  11255. \009function self.isDead()\
  11256. \009\009return coroutine.status(process) == \"dead\" and true or false\
  11257. \009end\
  11258. \
  11259. \009function self.kill()\
  11260. \
  11261. \009end\
  11262. \
  11263. \009function self.setVisible(isVisible)\
  11264. \009\009parent.setVisible(isVisible)\
  11265. \009end\
  11266. \
  11267. \009self.resume = function(...)\
  11268. \009\009first = ...\
  11269. \009\009if self.isDead() then\
  11270. \009\009\009return false, \"Is dead.\"\
  11271. \009\009end\
  11272. \009\009if filter == nil or first == filter then\
  11273. \009\009\009ok, err = coroutine.resume(process,...)\
  11274. \009\009\009if ok then\
  11275. \009\009\009\009filter = err\
  11276. \009\009\009\009return true\
  11277. \009\009\009else\
  11278. \009\009\009\009return false, err\
  11279. \009\009\009end\
  11280. \009\009end\
  11281. \009end\
  11282. \
  11283. \
  11284. \
  11285. \009return self\
  11286. end",
  11287.     },
  11288.     [ "boot.lua" ] = "term.clear()\
  11289. term.setCursorPos(1,1)\
  11290. term.redirect(term.native())\
  11291. \
  11292. local function printf(...)\
  11293. \009 --[[\
  11294. \009\009Author: Creator\
  11295. \009 ]]--\
  11296. \009local clrs = {\
  11297. \009\009[\"0\"] = colors.white,\
  11298. \009\009[\"1\"] = colors.orange,\
  11299. \009\009[\"2\"] = colors.magenta,\
  11300. \009\009[\"3\"] = colors.lightBlue,\
  11301. \009\009[\"4\"] = colors.yellow,\
  11302. \009\009[\"5\"] = colors.lime,\
  11303. \009\009[\"6\"] = colors.pink,\
  11304. \009\009[\"7\"] = colors.gray,\
  11305. \009\009[\"8\"] = colors.lightGray,\
  11306. \009\009[\"9\"] = colors.cyan,\
  11307. \009\009[\"a\"] = colors.purple,\
  11308. \009\009[\"b\"] = colors.blue,\
  11309. \009\009[\"c\"] = colors.brown,\
  11310. \009\009[\"d\"] = colors.green,\
  11311. \009\009[\"e\"] = colors.red,\
  11312. \009\009[\"f\"] = colors.black,\
  11313. \009}\
  11314. \009local prefixes = {\
  11315. \009\009[\"+\"] = term.setTextColor,\
  11316. \009\009[\"-\"] = term.setBackgroundColor,\
  11317. \009}\
  11318. \009local str = \"\"\
  11319. \009for i,v in pairs({...}) do\
  11320. \009\009str = str..\" \"..tostring(v)\
  11321. \009end\
  11322. \009str = str:sub(2,-1)\
  11323. \009local pos = 1\
  11324. \009local toPrint = \"\"\
  11325. \009while true do\
  11326. \009\009local skip = 0\
  11327. \009\009local char = str:sub(pos,pos)\
  11328. \009\009if char == \"\\027\" then\
  11329. \009\009\009term.write(toPrint)\
  11330. \009\009\009toPrint = \"\"\
  11331. \009\009\009local n = str:sub(pos+1,pos+1)\
  11332. \009\009\009local nn = str:sub(pos+2,pos+2)\
  11333. \009\009\009if prefixes[n] and clrs[nn] then\
  11334. \009\009\009\009prefixes[n](clrs[nn])\
  11335. \009\009\009\009skip = skip +2\
  11336. \009\009\009\009if str:sub(pos+3,pos+3) ~= \"@\" then\
  11337. \009\009\009\009\009local n = str:sub(pos+3,pos+3)\
  11338. \009\009\009\009\009local nn = str:sub(pos+4,pos+4)\
  11339. \009\009\009\009\009if prefixes[n] and clrs[nn] then\
  11340. \009\009\009\009\009\009prefixes[n](clrs[nn])\
  11341. \009\009\009\009\009\009skip = skip +2\
  11342. \009\009\009\009\009end\
  11343. \009\009\009\009else\
  11344. \009\009\009\009\009skip = skip + 1\
  11345. \009\009\009\009end\
  11346. \009\009\009end\
  11347. \009\009elseif char == \"\\n\" then\
  11348. \009\009\009print(\"\")\
  11349. \009\009else\
  11350. \009\009\009toPrint = toPrint..char\
  11351. \009\009end\
  11352. \009\009if pos == #str then break end\
  11353. \009\009pos = pos + 1 + skip\
  11354. \009end\
  11355. \009if toPrint ~= \"\" then\
  11356. \009\009term.write(toPrint)\
  11357. \009end\
  11358. end\
  11359. \
  11360. local counter = 0\
  11361. \
  11362. local function pts(str)\
  11363. \009printf(\"\\027+1@[\"..tostring(counter)..\"] \\027+b@\"..tostring(str))\
  11364. \009counter = counter + 1\
  11365. \009print(\"\")\
  11366. end\
  11367. \
  11368. local function handle(check, ...)\
  11369. \009if check then\
  11370. \009\009return ...\
  11371. \009end\
  11372. \009print(...)\
  11373. \009read()\
  11374. \009return false, ...\
  11375. end\
  11376. \
  11377. function _G.dofile(path,...)\
  11378. \009local func, err = loadfile(path)\
  11379. \009if func then\
  11380. \009\009setfenv(func,_G)\
  11381. \009\009return handle(pcall(func, ...))\
  11382. \009else\
  11383. \009\009return false, err\
  11384. \009end\
  11385. end\
  11386. \
  11387. local kernel = {}\
  11388. local windows = {}\
  11389. local processes = {}\
  11390. \
  11391. pts(\"Loading OmniOS into memory...\")\
  11392. \
  11393. local lib = dofile(\"OmniOS/kernel/libload.lua\", kernel, processes, windows, pts, dofile)\
  11394. \
  11395. pts(\"Finished loading kernel libs...\")\
  11396. \
  11397. pts(\"Now loading drivers into memory...\")\
  11398. \
  11399. dofile(\"OmniOS/kernel/drivers/drivers.lua\", pts, dofile, lib)\
  11400. \
  11401. pts(\"Drivers were successfully loaded into memory...\")\
  11402. \
  11403. local status, err = dofile(\"OmniOS/kernel/kernel.lua\", kernel, processes, windows, lib, dofile, pts, \"OmniOS/bin/shell.lua\")\
  11404. lib.log.log(\"shutdown\",status, err)\
  11405. \
  11406. if status == \"reboot\" then\
  11407. \009os.reboot()\
  11408. end",
  11409.     old = {
  11410.       [ "oldkernel.lua" ] = "--[[\
  11411. \009Kernel\
  11412. \009For OmniOS\
  11413. ]]--\
  11414. --[[\
  11415. \009Specifications:\
  11416. \009newProcess:\
  11417. \009\009@parent: the object on which the process will be drawn\
  11418. \009\009@func: the function, usually from a file\
  11419. ]]--\
  11420. os.pullEvent = os.pullEventRaw\
  11421. --Variables\
  11422. local ids = {}\
  11423. local process = {}\
  11424. local pids = {}\
  11425. local tasks = {}\
  11426. local active = 1\
  11427. local bindings = {}\
  11428. local eventFilter = {[\"key\"] = true, [\"mouse_click\"] = true, [\"paste\"] = true,[\"char\"] = true, [\"terminate\"] = true, [\"mouse_scroll\"] = true, [\"mouse_drag\"] = true, [\"key_up\"] = true, [\"mouse_up\"] = true}\
  11429. local Internal = {}\
  11430. local eventBuffer = {}\
  11431. local nameIDLink = {}\
  11432. local currTerm = term.current()\
  11433. local w,h = term.getSize()\
  11434. local running = 1\
  11435. local skipEvent = false\
  11436. \
  11437. Kernel = {}\
  11438. Internal.messages = {}\
  11439. \
  11440. --Overwrites\
  11441. function assert(check,msg,level)\
  11442. \009if not type(level) == \"number\" then error(\"The third argument has to be a number!\",2) end\
  11443. \009if not check then error(msg,level+1) end\
  11444. end\
  11445. \
  11446. --Functions\
  11447. --Local\
  11448. function Internal.finalKill(id)\
  11449. \009if tonumber(id) and process[id] then\
  11450. \009\009process[id] = nil\
  11451. \009end\
  11452. end\
  11453. \
  11454. function Internal.kill(id)\
  11455. \009if id == 1 then\
  11456. \009\009return\
  11457. \009elseif id == active then\
  11458. \009\009Kernel.switch(1)\
  11459. \009end\
  11460. \009Internal.queueTask(\"finalKill\",id)\
  11461. end\
  11462. \
  11463. function Internal.drawClosed()\
  11464. \009local col = process[active].preferences.sidebarColor or colors.black\
  11465. \009paintutils.drawLine(w,1,w,h,col)\
  11466. \009term.setBackgroundColor(col)\
  11467. \009term.setCursorPos(w,math.floor(h/2)+1)\
  11468. \009term.setTextColor(process[active].preferences.sidebarTextColor or colors.white)\
  11469. \009term.write(\"<\")\
  11470. end\
  11471. \
  11472. function Internal.drawOpen()\
  11473. \009process[active].window.setVisible(false)\
  11474. \009term.setCursorBlink(false)\
  11475. \009paintutils.drawFilledBox(w-15,1,w,h,colors.black)\
  11476. \009term.setTextColor(colors.white)\
  11477. \009local amount = 1\
  11478. \009local links = {}\
  11479. \009for i,v in pairs(process) do\
  11480. \009\009if active == i then paintutils.drawLine(w-15,amount,w,amount,colors.blue) end\
  11481. \009\009term.setCursorPos(w-14,amount)\
  11482. \009\009term.setBackgroundColor(active == i and colors.blue or colors.black)\
  11483. \009\009term.write(\"x \"..i..\" \"..v.name)\
  11484. \009\009links[amount] = i\
  11485. \009\009amount = amount + 1\
  11486. \009end\
  11487. \
  11488. \009while true do\
  11489. \009\009local evnt = {os.pullEventRaw()}\
  11490. \009\009if evnt[1] == \"mouse_click\" then\
  11491. \009\009\009if evnt[3] < w-15 then break\
  11492. \009\009\009elseif evnt[3] == w-14 and evnt[4] <= amount then\
  11493. \009\009\009\009Kernel.kill(links[evnt[4]])\
  11494. \009\009\009\009--if links[evnt[4]] == active then break end\
  11495. \009\009\009\009break\
  11496. \009\009\009elseif evnt[3] >= w-12 and evnt[4] <= amount then\
  11497. \009\009\009\009Kernel.switch(links[evnt[4]])\
  11498. \009\009\009\009break\
  11499. \009\009\009end\
  11500. \009\009elseif not (evnt[1] == \"mouse_up\" or evnt[1] == \"mouse_drag\") then\
  11501. \009\009\009eventBuffer[#eventBuffer+1] = evnt\
  11502. \009\009end\
  11503. \009end\
  11504. \009process[active].window.setVisible(true)\
  11505. \009process[active].window.redraw()\
  11506. \009Internal.drawClosed()\
  11507. end\
  11508. \
  11509. \
  11510. function Internal.newProcess(func,parent,permission,name,id,...)\
  11511. \009id = id or 1\
  11512. \009--Utils.debug(\"ID is \"..tostring(id))\
  11513. \009if permission == \"user\" then\
  11514. \009\009local env = Sandbox.newEnv(name)\
  11515. \009\009setfenv(func,env)\
  11516. \009end\
  11517. \009process[id] = {\
  11518. \009\009--[\"environement\"] = env,\
  11519. \009\009[\"routine\"] = coroutine.create(func or (function()print(\"erroe\")end)),\
  11520. \009\009[\"name\"] = name,\
  11521. \009\009[\"filter\"] = \"\",\
  11522. \009\009[\"parent\"] = parent,\
  11523. \009\009[\"window\"] = window.create(parent == \"term\" and term.current() or peripheral.wrap(parent),1,1,w-1,h,false),\
  11524. \009\009[\"preferences\"] = {}\
  11525. \009}\
  11526. \
  11527. \009if not nameIDLink[name] then nameIDLink[name] = {id} else nameIDLink[name][#nameIDLink[name]+1] = id end\
  11528. \009--Utils.debug(\"Added links.\")\
  11529. \009running = id\
  11530. \009if id == active then\
  11531. \009\009process[id].window.setVisible(true)\
  11532. \009end\
  11533. \009term.redirect(process[id].window)\
  11534. \009ok, process[id].filter = coroutine.resume(process[id].routine,...)\
  11535. \009if not ok then\
  11536. \009\009print(process[id].filter)\
  11537. \009\009Utils.log(\"Error\",tostring(ok)..\" && \"..tostring(process[id].filter),process[id].name)\
  11538. \009elseif coroutine.status(process[id].routine) == \"dead\" then\
  11539. \009\009Kernel.kill(id)\
  11540. \009\009print(process[id].filter)\
  11541. \009end\
  11542. \009Utils.log(\"Filter\",tostring(ok)..\" && \"..tostring(process[id].filter),process[id].name)\
  11543. \009term.redirect(currTerm)\
  11544. \009return id\
  11545. end\
  11546. \
  11547. function Internal.runBinds(event)\
  11548. \009if bindings[event[1]] then\
  11549. \009\009for i,v in pairs(bindings[event[1]]) do\
  11550. \009\009\009if type(v) == \"function\" then\
  11551. \009\009\009\009pcall(v,unpack(event))\
  11552. \009\009\009else\
  11553. \009\009\009\009coroutine.resume(v,unpack(event))\
  11554. \009\009\009end\
  11555. \009\009end\
  11556. \009end\
  11557. end\
  11558. \
  11559. function Internal.queueTask(...)\
  11560. \009tasks[#tasks+1] = {...}\
  11561. end\
  11562. \
  11563. function Internal.switch(...)\
  11564.  local args={...}\
  11565.  Utils.debug(\"Switch in kernel \"..(function(tabl) local x = \"\" for i,v in pairs(tabl) do x = x..\" \"..tostring(i)..\": \"..tostring(v) end return x end)(args))\
  11566.  if not type(args[1]) == \"number\" then Utils.debug(\"Expected number, got \"..type(args[1])) return end\
  11567. \009Utils.debug(\"Args \"..tostring(args[1])..type(args[1]))\
  11568. \009if process[args[1]] then\
  11569. \009\009process[active].window.setVisible(false)\
  11570. \009\009process[args[1]].window.setVisible(true)\
  11571.  \009process[args[1]].window.redraw()\
  11572. \009\009active = args[1]\
  11573. \009end\
  11574. end\
  11575. \
  11576. function Internal.skipEvent(bool)\
  11577. \009skipEvent = type(bool) == \"boolean\" and bool or false\
  11578. end\
  11579. \
  11580. --API\
  11581. function Kernel.newProcess(func,parent,permission,name,...)\
  11582. \009local id = 1\
  11583. \009while true do\
  11584. \009\009if not process[id] then break end\
  11585. \009\009id = id + 1\
  11586. \009end\
  11587. \009Utils.debug(\"ID is t \"..tostring(id))\
  11588. \009process[id] = {}\
  11589. \009Internal.queueTask(\"newProcess\",func,parent,permission,name,id,...)\
  11590. \009Utils.debug(\"New ID: \"..tostring(id))\
  11591. \009return id\
  11592. end\
  11593. \
  11594. function Kernel.list()\
  11595. \009local ret = {}\
  11596. \009for i,v in pairs(process) do\
  11597. \009\009ret[#ret+1] = {i,v.name,coroutine.status(v.routine)}\
  11598. \009end\
  11599. \009return ret\
  11600. end\
  11601. \
  11602. function Kernel.switch(newTaskID)\
  11603. \009Utils.debug(\"Queueing switch event: \"..tostring(newTaskID))\
  11604. \009Internal.queueTask(\"switch\",newTaskID)\
  11605. end\
  11606. \
  11607. function Kernel.bindEvent(event,func)\
  11608. \009if not (type(func) == \"function\" or type(func) == \"thread\") then error(\"Ecxpected function, got \"..type(func)..\"!\",2) end\
  11609. \009if bindings[event] then\
  11610. \009\009bindings[event][#bindings[event]+1] = func\
  11611. \009else\
  11612. \009\009bindings[event] = {[1] = func}\
  11613. \009end\
  11614. end\
  11615. \
  11616. function Kernel.kill(id)\
  11617. \009Utils.debug(\"Killing \"..tostring(id))\
  11618. \009Internal.queueTask(\"kill\",id)\
  11619. end\
  11620. \
  11621. function Kernel.getRunning()\
  11622. \009return running\
  11623. end\
  11624. \
  11625. function Kernel.addMessage(destination,sender,message)\
  11626. \009if not Internal.messages[destination] then\
  11627. \009\009Internal.messages[destination] = {}\
  11628. \009end\
  11629. \009Internal.messages[destination][#Internal.messages[destination]+1] = {sender,message}\
  11630. end\
  11631. \
  11632. function Kernel.setSidebarColor(id,color)\
  11633. \009if not process[id] then return end\
  11634. \009process[id].preferences.sidebarColor = color\
  11635. end\
  11636. \
  11637. function Kernel.setSidebarTextColor(id,color)\
  11638. \009if not process[id] then return end\
  11639. \009process[id].preferences.sidebarTextColor = color\
  11640. end\
  11641. \
  11642. function Kernel.getName(id)\
  11643. \009return process[id] and process[id].name or \"unknown\"\
  11644. end\
  11645. \
  11646. function Kernel.broadcast(sender,message)\
  11647. \009for i,v in pairs(process) do\
  11648. \009\009if not Internal.messages[i] then\
  11649. \009\009\009Internal.messages[i] = {}\
  11650. \009\009end\
  11651. \009\009Internal.messages[i][#Internal.messages[i]+1] = {sender,message}\
  11652. \009end\
  11653. end\
  11654. \
  11655. function Kernel.getMessages(id)\
  11656. \009local buffer = Internal.messages[id]\
  11657. \009Internal.messages[id] = nil\
  11658. \009return buffer\
  11659. end\
  11660. \
  11661. --Code\
  11662. dofile(\"OmniOS/Drivers/FS.lua\")\
  11663. dofile(\"OmniOS/Drivers/disk.lua\")\
  11664. \
  11665. local function keySwitch(_event,_code)\
  11666. \009while true do\
  11667.  \009if _event == \"key\" and _code == 56 then\
  11668.    \009time = os.clock()\
  11669. \009  elseif _event == \"char\" and tonumber(_code) then\
  11670.    \009if os.clock()-time <= 0.50 then\
  11671.      \009Kernel.switch(tonumber(_code))\
  11672.    \009end\
  11673. \009  end\
  11674. \009\009_event, _code = coroutine.yield()\
  11675. \009end\
  11676. end\
  11677. \
  11678. local keySwitchCoro = coroutine.create(keySwitch)\
  11679. \
  11680. Kernel.bindEvent(\"key\",keySwitchCoro)\
  11681. Kernel.bindEvent(\"char\",keySwitchCoro)\
  11682. \
  11683. Internal.newProcess(...)\
  11684. \
  11685. fs.link(\"Programs\",\"OmniOS/Programs\")\
  11686. fs.link(\"OmniOS/usr/Programs\",\"OmniOS/Programs\")\
  11687. Internal.drawClosed()\
  11688. \
  11689. while true do\
  11690. \009if tasks[1] then\
  11691. \009\009if Internal[tasks[1][1]] then\
  11692. \009\009\009Internal[tasks[1][1]](unpack(tasks[1],2))\
  11693. \009\009\009Utils.debug(\"Completed task \"..tasks[1][1])\
  11694. \009\009\009table.remove(tasks,1)\
  11695. \009\009end\
  11696. \009else\
  11697. \009\009local event = #eventBuffer == 0 and {os.pullEvent()} or table.remove(eventBuffer,1)\
  11698. \009\009Internal.runBinds(event)\
  11699. \009\009if event[1] == \"mouse_click\" and event[3] == w then\
  11700. \009\009\009Internal.drawOpen()\
  11701. \009\009elseif event[1] == \"terminate\" then\
  11702. \009\009\009Utils.log(\"Terminate\",active,Kernel.getName(active))\
  11703. \009\009\009Kernel.kill(active)\
  11704. \009\009elseif not skipEvent then\
  11705. \009\009\009if eventFilter[event[1]] then\
  11706. \009\009\009\009if process[active].filter == nil or process[active].filter == \"\" or process[active].filter == event[1] then\
  11707. \009\009\009\009\009running = active\
  11708. \009\009\009\009\009Internal.drawClosed()\
  11709. \009\009\009\009\009Utils.debug(\"Running main routine with event \"..tostring(event[1]))\
  11710. \009\009\009\009\009term.redirect(process[active].window)\
  11711. \009\009\009\009\009process[active].window.setVisible(true)\
  11712. \009\009\009\009\009ok, process[active].filter = coroutine.resume(process[active].routine,unpack(event))\
  11713. \009\009\009\009\009if not ok then\
  11714. \009\009\009\009\009\009print(process[active].filter)\
  11715. \009\009\009\009\009\009Utils.log(\"Error\",tostring(ok)..\" && \"..tostring(process[active].filter),process[active].name)\
  11716. \009\009\009\009\009elseif coroutine.status(process[active].routine) == \"dead\" then\
  11717. \009\009\009\009\009\009Kernel.kill(active)\
  11718. \009\009\009\009\009\009print(process[active].filter)\
  11719. \009\009\009\009\009end\
  11720. \009\009\009\009\009Utils.log(\"Filter\",tostring(ok)..\" && \"..tostring(process[active].filter),process[active].name)\
  11721. \009\009\009\009\009term.redirect(currTerm)\
  11722. \009\009\009\009end\
  11723. \009\009\009else\
  11724. \009\009\009\009for i,v in pairs(process) do\
  11725. \009\009\009\009\009if process[i].filter == nil or process[i].filter == \"\" or process[i].filter == event[1] then\
  11726. \009\009\009\009\009\009Utils.debug(\"Running secondary routine with event \"..tostring(event[1]))\
  11727. \009\009\009\009\009\009term.redirect(v.window)\
  11728. \009\009\009\009\009\009running = i\
  11729. \009\009\009\009\009\009ok, process[i].filter = coroutine.resume(v.routine,unpack(event))\
  11730. \009\009\009\009\009\009if coroutine.status(process[i].routine) == \"dead\" or not ok then\
  11731. \009\009\009\009\009\009\009Utils.log(\"Error\",tostring(ok)..\" && \"..tostring(process[i].filter),process[i].name) --Kernel.kill(i)\
  11732. \009\009\009\009\009\009\009--term.clear()\
  11733. \009\009\009\009\009\009\009--term.setCursorPos(1,1)\
  11734. \009\009\009\009\009\009\009print(process[i].filter)\
  11735. \009\009\009\009\009\009end\
  11736. \009\009\009\009\009\009Utils.log(\"Filter\",tostring(ok)..\" && \"..tostring(process[i].filter),process[i].name)\
  11737. \009\009\009\009\009\009term.redirect(currTerm)\
  11738. \009\009\009\009\009end\
  11739. \009\009\009\009end\
  11740. \009\009\009end\
  11741. \009\009\009local buffer = {}\
  11742. \009\009\009for i,v in pairs(process) do\
  11743. \009\009\009\009if v.filter == \"kernel_messages\" and Internal.messages[i] then\
  11744. \009\009\009\009\009running = i\
  11745. \009\009\009\009\009term.redirect(process[i].window)\
  11746. \009\009\009\009\009ok, process[i].filter = coroutine.resume(process[i].routine,unpack(Internal.messages[i]))\
  11747. \009\009\009\009\009term.redirect(currTerm)\
  11748. \009\009\009\009\009Utils.log(\"MessageDelivered\",Kernel.getName(i))\
  11749. \009\009\009\009\009Utils.log(\"Filter\",tostring(ok)..\" && \"..tostring(process[i].filter),process[i].name)\
  11750. \009\009\009\009\009if coroutine.status(process[i].routine) == \"dead\" or not ok then\
  11751. \009\009\009\009\009\009Utils.log(\"Error\",tostring(ok)..\" && \"..tostring(process[i].filter),process[i].name) --Kernel.kill(i)\
  11752. \009\009\009\009\009\009--term.clear()\
  11753. \009\009\009\009\009\009--term.setCursorPos(1,1)\
  11754. \009\009\009\009\009\009print(process[i].filter)\
  11755. \009\009\009\009\009end\
  11756. \009\009\009\009\009Internal.messages[i] = nil\
  11757. \009\009\009\009end\
  11758. \009\009\009end\
  11759. \009\009else\
  11760. \009\009\009skipEvent = false\
  11761. \009\009end\
  11762. \009end\
  11763. end",
  11764.     },
  11765.     drivers = {
  11766.       fs = {
  11767.         devfs = {
  11768.           [ "devfs.lua" ] = "--[[\
  11769. \009Everything is a file!\
  11770. \009Author: Creator\
  11771. ]]--\
  11772. \
  11773. lib = ...\
  11774. \
  11775. local devfs = {}\
  11776. local filesystem = {}\
  11777. local locations = {}\
  11778. local drivers = {}\
  11779. \
  11780. local vfs = lib.vfs\
  11781. local log = lib.log.generate(\"devfs\")\
  11782. \
  11783. function devfs.open(path,mode)\
  11784. \009log(\"Opening file\", path, \"as\", mode)\
  11785. \009if vfs.exists(path, filesystem) and not vfs.isDir(path, filesystem) then\
  11786. \009\009local x = vfs.read(path, filesystem)\
  11787. \009\009log(\"Address is\", x)\
  11788. \009\009return drivers[x]\
  11789. \009else\
  11790. \009\009return nil\
  11791. \009end\
  11792. end\
  11793. \
  11794. function devfs.isReadOnly()\
  11795. \009return false\
  11796. end\
  11797. \
  11798. function devfs.delete(path)\
  11799. \009return vfs.delete(path,filesystem)\
  11800. end\
  11801. \
  11802. function devfs.isDir(path)\
  11803. \009return vfs.isDir(path,filesystem)\
  11804. end\
  11805. \
  11806. function devfs.getDrive(path)\
  11807. \009fs.fs.ccfs.getDrive(vfs.getFirstElement(path))\
  11808. end\
  11809. \
  11810. function devfs.getSize(path)\
  11811. \009return 0\
  11812. end\
  11813. \
  11814. function devfs.exists(path)\
  11815. \009vfs.exists(path,filesystem)\
  11816. end\
  11817. \
  11818. function devfs.list(path)\
  11819. \009if devfs.isDir(path) then\
  11820. \009\009local final = {}\
  11821. \009\009for i,v in pairs(vfs.list(path,filesystem)) do\
  11822. \009\009\009final[#final+1] = i\
  11823. \009\009end\
  11824. \009\009return final\
  11825. \009else\
  11826. \009\009return false\
  11827. \009end\
  11828. end\
  11829. \
  11830. function devfs.makeDir(path)\
  11831. \009return vfs.makeDir(path, filesystem)\
  11832. end\
  11833. \
  11834. function devfs.setfs(path)\
  11835. \009--lib.log.log(\"devfs\", \"setFS: Making folder at \", path)\
  11836. \009path = fs.combine(\"\",path)\
  11837. \009log(\"Calling setfs to\", path)\
  11838. \009locations[#locations + 1] = path\
  11839. \009--log(lib.textutils.serialize(locations))\
  11840. \009devfs.makeDir(path)\
  11841. \009for i,v in pairs(fs.list(path)) do\
  11842. \009\009if v ~= \"doc\" then\
  11843. \009\009local f, err = dofile(path..\"/\"..v)\
  11844. \009\009\009if f then\
  11845. \009\009\009\009vfs.write(f, fs.combine(path, v:match(\"[^%.]+\")), filesystem)\
  11846. \009\009\009\009print(\"Successfully loaded\", v, fs.combine(path, v))\
  11847. \009\009\009else\
  11848. \009\009\009\009print(\"Failed to load\", v, err)\
  11849. \009\009\009end\
  11850. \009\009end\
  11851. \009end\
  11852. \009devfs.populate()\
  11853. end\
  11854. \
  11855. function devfs.populate()\
  11856. \009local list = lib.devbus.list()\
  11857. \009--log(\"Populating\", lib.textutils.serialize(list), lib.textutils.serialize(locations))\
  11858. \009for i,v in pairs(locations) do\
  11859. \009\009for name, address in pairs(list) do\
  11860. \009\009\009vfs.makeFile(v..\"/\"..name, filesystem)\
  11861. \009\009\009vfs.write(address, v..\"/\"..name, filesystem)\
  11862. \009\009end\
  11863. \009end\
  11864. \009local new = {}\
  11865. \009for name,address in pairs(list) do\
  11866. \009\009new[address] = drivers[address] or lib.devbus.getDriver(address)\
  11867. \009end\
  11868. \009drivers = new\
  11869. end\
  11870. \
  11871. lib.devbus.onEvent(\"peripheral\", devfs.populate)\
  11872. lib.devbus.onEvent(\"peripheral_detach\", devfs.populate)\
  11873. \
  11874. \
  11875. return devfs",
  11876.         },
  11877.         ccfs = {
  11878.           [ "ccfs.lua" ] = "--[[\
  11879.  Standart ComputerCraft File System\
  11880. ]]--\
  11881. \
  11882. local lib = ...\
  11883. \
  11884. return lib.utils.table.copy(fs)",
  11885.         },
  11886.         [ "fs.lua" ] = "--[[\
  11887. \009FS overwrite\
  11888. \009Author: Creator\
  11889. ]]--\
  11890. \
  11891. local lib, pts, dofile = ...\
  11892. \
  11893. local altFS = {}\
  11894. \
  11895. for i,v in pairs(fs.list(\"OmniOS/kernel/drivers/fs\")) do\
  11896. \009if fs.isDir(\"OmniOS/kernel/drivers/fs/\"..v) then\
  11897. \009\009altFS[v:match(\"[^%.]+\")] = dofile(\"OmniOS/kernel/drivers/fs/\"..v..\"/\"..v..\".lua\", lib, altFS)\
  11898. \009\009pts(\"Loading fs: \\027+d@\"..v:match(\"[^%.]+\"))\
  11899. \009end\
  11900. end\
  11901. \
  11902. pts(\"Loading fsh...\")\
  11903. local fsh = dofile(\"OmniOS/kernel/drivers/fs/fsh.lua\", altFS)\
  11904. --Functions\
  11905. \
  11906. fs.getfs = fsh.getfs\
  11907. fs.getMountPath = fsh.resolveLinks\
  11908. fs.setfs = fsh.setfs\
  11909. fs.link = fsh.link\
  11910. fs.fs = lib.utils.table.copy(altFS)\
  11911. \
  11912. function fs.isReadOnly(path)\
  11913. \009local path = fsh.resolveLinks(path)\
  11914. \009local fss = fs.getfs(path)\
  11915. \009return altFS[fss].isReadOnly(path)\
  11916. end\
  11917. \
  11918. function fs.list(path)\
  11919. \009local path = fsh.resolveLinks(path)\
  11920. \009local fss = fs.getfs(path)\
  11921. \009return altFS[fss].list(path)\
  11922. end\
  11923. \
  11924. function fs.exists(path)\
  11925. \009local path = fsh.resolveLinks(path)\
  11926. \009local fss = fs.getfs(path)\
  11927. \009return altFS[fss].exists(path)\
  11928. end\
  11929. \
  11930. function fs.isDir(path)\
  11931. \009local path = fsh.resolveLinks(path)\
  11932. \009local fss = fs.getfs(path)\
  11933. \009--lib.log.log(\"fs\", path, fss)\
  11934. \009--ffs = altFS[fss] or altFS.ccfs\
  11935. \009return altFS[fss].isDir(path)\
  11936. end\
  11937. \
  11938. function fs.getDrive(path)\
  11939. \009local path = fsh.resolveLinks(path)\
  11940. \009local fss = fs.getfs(path)\
  11941. \009--local ffs = altFS[fss] or altFS.ccfs\
  11942. \009return altFS[fss].getDrive(path)\
  11943. end\
  11944. \
  11945. function fs.getSize(path)\
  11946. \009local path = fsh.resolveLinks(path)\
  11947. \009local fss = fs.getfs(path)\
  11948. \009--local ffs = altFS[fss] or altFS.ccfs\
  11949. \009return altFS[fss].getSize(path)\
  11950. end\
  11951. \
  11952. function fs.makeDir(path)\
  11953. \009local path = fsh.resolveLinks(path)\
  11954. \009local fss = fs.getfs(path)\
  11955. \009return altFS[fss].makeDir(path)\
  11956. end\
  11957. --[[Elaborate]]--\
  11958. function fs.copy(path1,path2)\
  11959. \009if fs.isDir(path1) then\
  11960. \009\009local function explore(dir)\
  11961. \009\009\009local buffer = {}\
  11962. \009\009\009for i,v in pairs(fs.list(dir)) do\
  11963. \009\009\009\009if fs.isDir(dir..\"/\"..v) then\
  11964. \009\009\009\009\009buffer[v] = explore(dir..\"/\"..v)\
  11965. \009\009\009\009else\
  11966. \009\009\009\009\009buffer[v] = readFile(dir..\"/\"..v)\
  11967. \009\009\009\009end\
  11968. \009\009\009end\
  11969. \009\009\009return buffer\
  11970. \009\009end\
  11971. \009\009local function writeFile(path,content)\
  11972. \009\009\009local file = fs.open(path,\"w\")\
  11973. \009\009\009file.write(content)\
  11974. \009\009\009file.close()\
  11975. \009\009end\
  11976. \009\009local function writeDown(input,dir)\
  11977. \009\009\009\009for i,v in pairs(input) do\
  11978. \009\009\009\009if type(v) == \"table\" then\
  11979. \009\009\009\009\009writeDown(v,dir..\"/\"..i)\
  11980. \009\009\009\009elseif type(v) == \"string\" then\
  11981. \009\009\009\009\009writeFile(dir..\"/\"..i,v)\
  11982. \009\009\009\009end\
  11983. \009\009\009end\
  11984. \009\009end\
  11985. \009\009writeDown(explore(path1),path2)\
  11986. \009else\
  11987. \009\009local f = fs.open(path1,\"r\")\
  11988. \009\009local m = fs.open(path2,\"w\")\
  11989. \009\009m.write(f.readAll())\
  11990. \009\009f.close()\
  11991. \009\009m.close()\
  11992. \009end\
  11993. end\
  11994. \
  11995. function fs.move(path1,path2)\
  11996. \009if fs.isDir(path1) then\
  11997. \009\009local function explore(dir)\
  11998. \009\009\009local buffer = {}\
  11999. \009\009\009for i,v in pairs(fs.list(dir)) do\
  12000. \009\009\009\009if fs.isDir(dir..\"/\"..v) then\
  12001. \009\009\009\009\009buffer[v] = explore(dir..\"/\"..v)\
  12002. \009\009\009\009else\
  12003. \009\009\009\009\009buffer[v] = readFile(dir..\"/\"..v)\
  12004. \009\009\009\009end\
  12005. \009\009\009end\
  12006. \009\009\009return buffer\
  12007. \009\009end\
  12008. \009\009local function writeFile(path,content)\
  12009. \009\009\009local file = fs.open(path,\"w\")\
  12010. \009\009\009file.write(content)\
  12011. \009\009\009file.close()\
  12012. \009\009end\
  12013. \009\009local function writeDown(input,dir)\
  12014. \009\009\009\009for i,v in pairs(input) do\
  12015. \009\009\009\009if type(v) == \"table\" then\
  12016. \009\009\009\009\009writeDown(v,dir..\"/\"..i)\
  12017. \009\009\009\009elseif type(v) == \"string\" then\
  12018. \009\009\009\009\009writeFile(dir..\"/\"..i,v)\
  12019. \009\009\009\009end\
  12020. \009\009\009end\
  12021. \009\009end\
  12022. \009\009writeDown(explore(path1),path2)\
  12023. \009else\
  12024. \009\009local f = fs.open(path1,\"r\")\
  12025. \009\009local m = fs.open(path2,\"w\")\
  12026. \009\009local buff = f.readAll()\
  12027. \009\009m.write(buff)\
  12028. \009\009f.close()\
  12029. \009\009m.close()\
  12030. \009end\
  12031. \009fs.delete(path1)\
  12032. end\
  12033. \
  12034. function fs.delete(path)\
  12035. \009local path = fsh.resolveLinks(path)\
  12036. \009local fss = fs.getfs(path)\
  12037. \009return altFS[fss].delete(path)\
  12038. end\
  12039. \
  12040. function fs.open(path,mode)\
  12041. \009local path = fsh.resolveLinks(path)\
  12042. \009local fss = fs.getfs(path)\
  12043. \009return altFS[fss].open(path,mode)\
  12044. end\
  12045. \
  12046. --fs.setfs(\"OmniOS/dev\",\"devfs\")\
  12047. pts(\"Setting \\027+d@devfs\")\
  12048. fs.setfs(\"dev\",\"devfs\")\
  12049. pts(\"Setting \\027+d@ramfs\")\
  12050. fs.setfs(\"ramfs\",\"ramfs\")",
  12051.         [ "fsh.lua" ] = "--[[\
  12052. \009VFS helper and moumting manager\
  12053. \009Author: Creator\
  12054. ]]--\
  12055. \
  12056. --local \
  12057. \
  12058. --Variables\
  12059. local altFS = ...\
  12060. local mounts = {}\
  12061. local Internal = {}\
  12062. local filesystems = {}\
  12063. local fsh = {}\
  12064. \
  12065. --Functions\
  12066. function Internal.getFirstElement(path)\
  12067. \009if not type(path) == \"string\" then error(\"This is not a string: \"..tostring(path),2) end\
  12068. \009return string.sub(path,1,string.find(path,\"/\") and string.find(path,\"/\")-1 or -1)\
  12069. end\
  12070. \
  12071. function Internal.makeTable(path,tabl)\
  12072. \009if type(path) ~= \"string\" then error(\"Expected string, got \"..type(path)..\"!\",2) end\
  12073. \009if type(tabl) ~= \"table\" then error(\"Expected table, got \"..type(tabl)..\"!\",2) end\
  12074. \009path = fs.combine(\"\",path)\
  12075. \009local first = Internal.getFirstElement(path)\
  12076. \009if first == path then\
  12077. \009\009return tabl, first\
  12078. \009else\
  12079. \009\009if not tabl[first] then tabl[first] = {} end\
  12080. \009\009return Internal.makeTable(path:sub(path:find(\"/\")+1,-1),tabl[first])\
  12081. \009end\
  12082. end\
  12083. \
  12084. function fsh.setfs(path, fss)\
  12085. \009if not fs.isDir(path) then fs.makeDir(path) end\
  12086. \009if fss ~= \"ccfs\" then\
  12087. \009\009altFS[fss].setfs(path)\
  12088. \009\009local tabl, dir = Internal.makeTable(path,filesystems)\
  12089. \009\009tabl[dir] = fss\
  12090. \009end\
  12091. end\
  12092. \
  12093. function fsh.link(from, to)\
  12094. \009if not fs.exists(from) then fs.makeDir(from) end\
  12095. \009if not fs.exists(to) then fs.makeDir(to) end\
  12096. \009local tabl, dir = Internal.makeTable(from, mounts)\
  12097. \009tabl[dir] = to\
  12098. end\
  12099. \
  12100. function Internal.resolveLinks(path,tabl)\
  12101. \009local first = Internal.getFirstElement(path)\
  12102. \009if tabl[first] then\
  12103. \009\009if type(tabl[first]) == \"table\" then\
  12104. \009\009\009if first == path then return false end\
  12105. \009\009\009return Internal.resolveLinks(path:sub(#first+2,-1),tabl[first])\
  12106. \009\009elseif type(tabl[first]) == \"string\" then\
  12107. \009\009\009return tabl[first]..\"/\"..path:sub(#first+2,-1)\
  12108. \009\009end\
  12109. \009end\
  12110. \009return false\
  12111. end\
  12112. \
  12113. function Internal.intermediateResolveLinks(path)\
  12114. \009local pathBuffer = path\
  12115. \009repeat\
  12116. \009\009pathBuffer = fs.combine(\"\",path)\
  12117. \009\009path = Internal.resolveLinks(pathBuffer,mounts)\
  12118. \009until path == false\
  12119. \009return pathBuffer\
  12120. end\
  12121. \
  12122. function fsh.resolveLinks(path)\
  12123. \009if type(path) ~= \"string\" then\
  12124. \009\009--print(path)\
  12125. \009\009error(path,3)\
  12126. \009end\
  12127. \009local toReturn =  Internal.intermediateResolveLinks(fs.combine(\"\", path))\
  12128. \009return toReturn\
  12129. end\
  12130. \
  12131. function Internal.getfs(path,tabl)\
  12132. \009local first = Internal.getFirstElement(path)\
  12133. \009if tabl[first] then\
  12134. \009\009if type(tabl[first]) == \"table\" then\
  12135. \009\009\009return Internal.getfs(path:sub(#first+2,-1),tabl[first])\
  12136. \009\009elseif type(tabl[first]) == \"string\" then\
  12137. \009\009\009return tabl[first]\
  12138. \009\009end\
  12139. \009end\
  12140. \009return false\
  12141. end\
  12142. \
  12143. function fsh.getfs(path)\
  12144. \009resolved = Internal.getfs(fs.combine(\"\", path), filesystems)\
  12145. \009return resolved or \"ccfs\"\
  12146. end\
  12147. \
  12148. return fsh",
  12149.         ramfs = {
  12150.           [ "ramfs.lua" ] = "--[[\
  12151. \009TMPFS driver developed by Creator\
  12152. ]]--\
  12153. \
  12154. lib = ...\
  12155. local tmpfs = {}\
  12156. local filesystem = {}\
  12157. \
  12158. local vfs = lib.vfs\
  12159. \
  12160. function tmpfs.open(path,mode)\
  12161. \009--lib.log.log(\"ramfs\", \"entire filesystem\", textutils.serialize(filesystem))\
  12162. \009--lib.log.log(\"ramfs\", \"Opening\", path, \"as\", mode)\
  12163. \009if mode == \"r\" then\
  12164. \009\009if vfs.exists(path,filesystem) and not vfs.isDir(path,filesystem) then\
  12165. \009\009\009local data = vfs.read(path,filesystem)\
  12166. \009\009\009local index = 1\
  12167. \009\009\009local linedData = (function(data)\
  12168. \009\009\009\009local buffer = {}\
  12169. \009\009\009\009for token in string.gmatch(data,\"[^\\n]+\") do\
  12170. \009\009\009\009\009buffer[#buffer+1] = token\
  12171. \009\009\009\009end\
  12172. \009\009\009\009return buffer\
  12173. \009\009\009end)(data)\
  12174. \009\009\009local handle = {}\
  12175. \009\009\009function handle.readAll()\
  12176. \009\009\009\009return data\
  12177. \009\009\009end\
  12178. \009\009\009function handle.readLine()\
  12179. \009\009\009\009toRet = linedData[index]\
  12180. \009\009\009\009if not linedData[index] then\
  12181. \009\009\009\009\009index = 1\
  12182. \009\009\009\009end\
  12183. \009\009\009\009index = index + 1\
  12184. \009\009\009\009return toRet\
  12185. \009\009\009end\
  12186. \009\009\009function handle.close()\
  12187. \009\009\009\009handle = nil\
  12188. \009\009\009end\
  12189. \009\009\009return handle\
  12190. \009\009else\
  12191. \009\009\009return nil\
  12192. \009\009end\
  12193. \009elseif mode == \"w\" then\
  12194. \009\009if not vfs.exists(path,filesystem) then\
  12195. \009\009\009vfs.makeFile(path, filesystem)\
  12196. \009\009end\
  12197. \009\009if not vfs.isDir(path,filesystem) then\
  12198. \009\009\009local finalData = \"\"\
  12199. \009\009\009local handle = {}\
  12200. \009\009\009function handle.write(data)\
  12201. \009\009\009\009finalData = finalData..data\
  12202. \009\009\009end\
  12203. \009\009\009function handle.writeLine(data)\
  12204. \009\009\009\009finalData = finalData..data..\"\\n\"\
  12205. \009\009\009end\
  12206. \009\009\009function handle.flush()\
  12207. \009\009\009\009vfs.write(finalData,path,filesystem)\
  12208. \009\009\009end\
  12209. \009\009\009function handle.close()\
  12210. \009\009\009\009vfs.write(finalData,path,filesystem)\
  12211. \009\009\009\009handle = nil\
  12212. \009\009\009end\
  12213. \009\009\009return handle\
  12214. \009\009else\
  12215. \009\009\009return nil\
  12216. \009\009end\
  12217. \009elseif mode == \"a\" then\
  12218. \009\009if vfs.exists(path,filesystem) and not vfs.isDir(path,filesystem) then\
  12219. \009\009\009local finalData = vfs.read(path,filesystem)\
  12220. \009\009\009local handle = {}\
  12221. \009\009\009function handle.write(data)\
  12222. \009\009\009\009finalData = finalData..data\
  12223. \009\009\009end\
  12224. \009\009\009function handle.writeLine(data)\
  12225. \009\009\009\009finalData = finalData..data..\"\\n\"\
  12226. \009\009\009end\
  12227. \009\009\009function handle.flush()\
  12228. \009\009\009\009vfs.write(finalData,path,filesystem)\
  12229. \009\009\009end\
  12230. \009\009\009function handle.close()\
  12231. \009\009\009\009vfs.write(finalData,path,filesystem)\
  12232. \009\009\009\009handle = nil\
  12233. \009\009\009end\
  12234. \009\009\009return handle\
  12235. \009\009elseif not vfs.isDir(path,filesystem) then\
  12236. \009\009\009vfs.makeFile(path, filesystem)\
  12237. \009\009\009local finalData = \"\"\
  12238. \009\009\009local handle = {}\
  12239. \009\009\009function handle.write(data)\
  12240. \009\009\009\009finalData = finalData..data\
  12241. \009\009\009end\
  12242. \009\009\009function handle.writeLine(data)\
  12243. \009\009\009\009finalData = finalData..data..\"\\n\"\
  12244. \009\009\009end\
  12245. \009\009\009function handle.flush()\
  12246. \009\009\009\009vfs.write(finalData,path,filesystem)\
  12247. \009\009\009end\
  12248. \009\009\009function handle.close()\
  12249. \009\009\009\009vfs.write(finalData,path,filesystem)\
  12250. \009\009\009\009handle = nil\
  12251. \009\009\009end\
  12252. \009\009\009return handle\
  12253. \009\009else\
  12254. \009\009\009return nil\
  12255. \009\009end\
  12256. \009elseif mode == \"wb\" then\
  12257. \009\009if vfs.exists(path,filesystem) and not vfs.isDir(path,filesystem) then\
  12258. \
  12259. \009\009else\
  12260. \009\009\009return nil\
  12261. \009\009end\
  12262. \009elseif mode == \"rb\" then\
  12263. \009\009if vfs.exists(path,filesystem) and not vfs.isDir(path,filesystem) then\
  12264. \
  12265. \009\009else\
  12266. \009\009\009return nil\
  12267. \009\009end\
  12268. \009elseif mode == \"ab\" then\
  12269. \009\009if vfs.exists(path,filesystem) and not vfs.isDir(path,filesystem) then\
  12270. \
  12271. \009\009else\
  12272. \009\009\009return nil\
  12273. \009\009end\
  12274. \009else\
  12275. \009\009return nil\
  12276. \009end\
  12277. end\
  12278. \
  12279. function tmpfs.isReadOnly()\
  12280. \009return false\
  12281. end\
  12282. \
  12283. function tmpfs.delete(path)\
  12284. \009return vfs.delete(path,filesystem)\
  12285. end\
  12286. \
  12287. function tmpfs.isDir(path)\
  12288. \009return vfs.isDir(path,filesystem)\
  12289. end\
  12290. \
  12291. function tmpfs.getDrive(path)\
  12292. \009fs.fs.ccfs.getDrive(vfs.getFirstElement(path))\
  12293. end\
  12294. \
  12295. function tmpfs.getSize(path)\
  12296. \009if tmpfs.exists(path) and not tmpfs.isDir(path) then\
  12297. \009\009return #vfs.read(path)\
  12298. \009else\
  12299. \009\009return 0\
  12300. \009end\
  12301. end\
  12302. \
  12303. function tmpfs.exists(path)\
  12304. \009vfs.exists(path,filesystem)\
  12305. end\
  12306. \
  12307. function tmpfs.list(path)\
  12308. \009if tmpfs.isDir(path) then\
  12309. \009\009local final = {}\
  12310. \009\009for i,v in pairs(vfs.list(path,filesystem)) do\
  12311. \009\009\009final[#final+1] = i\
  12312. \009\009end\
  12313. \009\009return final\
  12314. \009else\
  12315. \009\009return false\
  12316. \009end\
  12317. end\
  12318. \
  12319. function tmpfs.makeDir(path)\
  12320. \009return vfs.makeDir(path, filesystem)\
  12321. end\
  12322. \
  12323. function tmpfs.setfs(path)\
  12324. \009--lib.log.log(\"ramfs\", \"setFS: Making folder at \", path)\
  12325. \009tmpfs.makeDir(path)\
  12326. end\
  12327. \
  12328. return tmpfs\
  12329. \
  12330. --print(vfs.exists(\"hi/wow/r/m\",filesystem))\
  12331. --print(vfs.isDir(\"hi/wow/r\",filesystem))\
  12332. --print(vfs.read(\"hi/wow/r\",filesystem))\
  12333. --vfs.write(\"wow\",\"hi/wow/r\",filesystem)\
  12334. --print(vfs.read(\"hi/wow/r\",filesystem))\
  12335. --vfs.delete(\"hi/wow\",filesystem)\
  12336. --print(vfs.read(\"hi/wow/r\",filesystem))\
  12337. --print(text--Utils.serialize(tmpfs.list(\"hi/wow/\",filesystem)))",
  12338.         },
  12339.       },
  12340.       doc = "r\
  12341. \
  12342. \009local handle = {}\
  12343. \
  12344. \009function handle.readLine()\
  12345. \
  12346. \009end\
  12347. \
  12348. \009function handle.readAll()\
  12349. \
  12350. \009end\
  12351. \
  12352. \009function handle.close()\
  12353. \009\009handle.readLine = nil\
  12354. \009\009handle.readAll = nil\
  12355. \009\009handle.close = nil\
  12356. \009end\
  12357. \
  12358. \009return handle\
  12359. \
  12360. rb\
  12361. \
  12362. \009local handle = {}\
  12363. \
  12364. \009function handle.read()\
  12365. \
  12366. \009end\
  12367. \
  12368. \009function handle.close()\
  12369. \009\009handle.read = nil\
  12370. \009\009handle.close = nil\
  12371. \009end\
  12372. \
  12373. \009return handle\
  12374. \
  12375. w\
  12376. \
  12377. \009local handle = {}\
  12378. \
  12379. \009function handle.write(input)\
  12380. \
  12381. \009end\
  12382. \
  12383. \009function handle.writeLine(input)\
  12384. \009\
  12385. \009end\
  12386. \
  12387. \009function handle.flush()\
  12388. \
  12389. \009end\
  12390. \
  12391. \009function handle.close()\
  12392. \009\009handle.write = nil\
  12393. \009\009handle.writeLine = nil\
  12394. \009\009handle.flush = nil\
  12395. \009\009handle.close = nil\
  12396. \009end\
  12397. \
  12398. \009return handle\
  12399. \
  12400. wb\
  12401. \
  12402. \009local handle = {}\
  12403. \
  12404. \009function handle.write(input)\
  12405. \
  12406. \009end\
  12407. \
  12408. \009function handle.flush()\
  12409. \
  12410. \009end\
  12411. \
  12412. \009function handle.close()\
  12413. \009\009handle.write = nil\
  12414. \009\009handle.flush = nil\
  12415. \009\009handle.close = nil\
  12416. \009end\
  12417. \
  12418. \009return handle\
  12419. \
  12420. a\
  12421. \
  12422. \009local handle = {}\
  12423. \
  12424. \009function handle.write(input)\
  12425. \
  12426. \009end\
  12427. \
  12428. \009function handle.writeLine(input)\
  12429. \009\
  12430. \009end\
  12431. \
  12432. \009function handle.flush()\
  12433. \
  12434. \009end\
  12435. \
  12436. \009function handle.close()\
  12437. \009\009handle.write = nil\
  12438. \009\009handle.writeLine = nil\
  12439. \009\009handle.flush = nil\
  12440. \009\009handle.close = nil\
  12441. \009end\
  12442. \
  12443. \009return handle\
  12444. \
  12445. ab\
  12446. \
  12447. \009local handle = {}\
  12448. \
  12449. \009function handle.write(input)\
  12450. \
  12451. \009end\
  12452. \
  12453. \009function handle.flush()\
  12454. \
  12455. \009end\
  12456. \
  12457. \009function handle.close()\
  12458. \009\009handle.write = nil\
  12459. \009\009handle.flush = nil\
  12460. \009\009handle.close = nil\
  12461. \009end\
  12462. \
  12463. \009return handle",
  12464.       vdev = {
  12465.         [ "zero.lua" ] = "--[[\
  12466. \009Zero device\
  12467. ]]\
  12468. \
  12469. local function instance(mode)\
  12470. \009if mode == \"r\" then\
  12471. \009\009local handle = {}\
  12472. \
  12473. \009\009function handle.readLine()\
  12474. \009\009\009return \"\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\"\
  12475. \009\009end\
  12476. \009\
  12477. \009\009function handle.readAll()\
  12478. \009\009\009return \"\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\"\
  12479. \009\009end\
  12480. \009\
  12481. \009\009function handle.close()\
  12482. \009\009\009handle.readLine = nil\
  12483. \009\009\009handle.readAll = nil\
  12484. \009\009\009handle.close = nil\
  12485. \009\009end\
  12486. \009\
  12487. \009\009return handle\
  12488. \
  12489. \009elseif mode == \"rb\" then\
  12490. \
  12491. \009\009local handle = {}\
  12492. \009\
  12493. \009\009function handle.read()\
  12494. \009\009\009read \"\\0\"\
  12495. \009\009end\
  12496. \009\
  12497. \009\009function handle.close()\
  12498. \009\009\009handle.read = nil\
  12499. \009\009\009handle.close = nil\
  12500. \009\009end\
  12501. \009\
  12502. \009\009return handle\
  12503. \
  12504. \009elseif mode == \"w\" or mode == \"a\" or mode == \"wb\" or mode == \"ab\" then -- try if mode:match '^[aw]b?$' then\
  12505. \009\009local handle = {}\
  12506. \
  12507. \009\009function handle.write(input)\
  12508. \
  12509. \009\009end\
  12510. \
  12511. \009\009function handle.writeLine(input)\
  12512. \009\009\009\
  12513. \009\009end\
  12514. \
  12515. \009\009function handle.flush()\
  12516. \
  12517. \009\009end\
  12518. \
  12519. \009\009function handle.close()\
  12520. \009\009\009handle.write = nil\
  12521. \009\009\009handle.writeLine = nil\
  12522. \009\009\009handle.flush = nil\
  12523. \009\009\009handle.close = nil\
  12524. \009\009end\
  12525. \
  12526. \009\009return handle\
  12527. \
  12528. \009end\
  12529. end\
  12530. \
  12531. return instance",
  12532.         [ "vdev.lua" ] = "return {}",
  12533.       },
  12534.       [ "drivers.lua" ] = "--[[\
  12535. \009Driver loading mechanism for OmniOS\
  12536. ]]--\
  12537. \
  12538. local pts, dofile, lib = ...\
  12539. \
  12540. for i, v in pairs(fs.list(\"OmniOS/kernel/drivers\")) do\
  12541. \009if fs.isDir(\"OmniOS/kernel/drivers/\"..v) then\
  12542. \009\009pts(\"Loading \\027+d@\"..v..\" \\027+b@driver into memory...\")\
  12543. \009\009dofile(\"OmniOS/kernel/drivers/\"..v..\"/\"..v..\".lua\", lib, pts, dofile)\
  12544. \009end\
  12545. end\
  12546. \
  12547. pts(\"Done\")",
  12548.       dev = {
  12549.         [ "dev.lua" ] = "--[[\
  12550. \009Device Driver Loader\
  12551. ]]\
  12552. \
  12553. local lib, pts, dofile = ...\
  12554. \
  12555. local list = {\
  12556. \009\"computer.lua\",\
  12557. \009\"modem.lua\",\
  12558. }\
  12559. \
  12560. for i,v in pairs(list) do\
  12561. \009local name = v:match(\"([^%.]+)\")\
  12562. \009local driver = dofile(\"OmniOS/kernel/drivers/dev/\"..v, lib)\
  12563. \009if driver and driver.type then\
  12564. \009\009lib.devbus.addDriver(driver.init, driver.type)\
  12565. \009\009pts(\"Loaded\", name)\
  12566. \009else\
  12567. \009\009pts(\"Failed loading\", name)\
  12568. \009end\
  12569. end\
  12570. \
  12571. lib.devbus.populate()",
  12572.         [ "computer.lua" ] = "--[[\
  12573. \009Computer Driver\
  12574. ]]--\
  12575. \
  12576. local computer = {\
  12577. \009type = \"computer\",\
  12578. \009init = function(address)\
  12579. \009\009assert(type(address) == \"string\")\
  12580. \009\009local ret = peripheral.wrap(address)\
  12581. \009\009function ret.write(action)\
  12582. \009\009\009if action == \"on\" or action == \"turnOn\" then\
  12583. \009\009\009\009ret.turnOn()\
  12584. \009\009\009elseif action == \"off\" or action == \"shutdown\" then\
  12585. \009\009\009\009ret.shutdown()\
  12586. \009\009\009elseif action == \"reboot\" then\
  12587. \009\009\009\009ret.reboot()\
  12588. \009\009\009else\
  12589. \009\009\009\009return false, \"Wrong specified argument.\"\
  12590. \009\009\009end\
  12591. \009\009end\
  12592. \009\009return ret\
  12593. \009end,\
  12594. }\
  12595. \
  12596. return computer",
  12597.         [ "modem.lua" ] = "--[[\
  12598. \009Modem Driver\
  12599. ]]--\
  12600. \
  12601. local modem = {\
  12602. \009type = \"modem\",\
  12603. \009init = function(address)\
  12604. \009\009assert(type(address) == \"string\")\
  12605. \009\009local ret = peripheral.wrap(address)\
  12606. \009\009function ret.write(action)\
  12607. \009\009\009if action == \"on\" or action == \"turnOn\" then\
  12608. \009\009\009\009ret.turnOn()\
  12609. \009\009\009elseif action == \"off\" or action == \"shutdown\" then\
  12610. \009\009\009\009ret.shutdown()\
  12611. \009\009\009elseif action == \"reboot\" then\
  12612. \009\009\009\009ret.reboot()\
  12613. \009\009\009else\
  12614. \009\009\009\009return false, \"Wrong specified argument.\"\
  12615. \009\009\009end\
  12616. \009\009end\
  12617. \009\009return ret\
  12618. \009end,\
  12619. }\
  12620. \
  12621. return modem",
  12622.       },
  12623.     },
  12624.     [ "tlco.lua" ] = "local function dofile(path,...)\
  12625. \009local func, err = loadfile(path)\
  12626. \009if func then\
  12627. \009\009setfenv(func,_G)\
  12628. \009\009return pcall(func, ...)\
  12629. \009else\
  12630. \009\009return false, err\
  12631. \009end\
  12632. end\
  12633. \
  12634. local a = _G.os.shutdown\
  12635. function _G.os.shutdown()\
  12636. \009_G.os.shutdown=a\
  12637. \009term.redirect(term.native())\
  12638. \009multishell=nil\
  12639. \009ok, err = dofile(\"OmniOS/kernel/boot.lua\")\
  12640. \009local file = fs.open(\"logs/boot.log\",\"a\")\
  12641. \009file.write(err..\"\\n\")\
  12642. \009file.close()\
  12643. end\
  12644. \
  12645. os.queueEvent(\"modem_message\", {})\
  12646. os.pullEvent()\
  12647. os.queueEvent(\"key\")",
  12648.   },
  12649. }
  12650.  
  12651.  
  12652.  
  12653.  
  12654.  
  12655.  
  12656.  
  12657.  
  12658. local function writeFile(path,content)
  12659.     local file = fs.open(path,"w")
  12660.     file.write(content)
  12661.     file.close()
  12662. end
  12663. function writeDown(input,dir)
  12664.         for i,v in pairs(input) do
  12665.         if type(v) == "table" then
  12666.             writeDown(v,dir.."/"..i)
  12667.         elseif type(v) == "string" then
  12668.             writeFile(dir.."/"..i,v)
  12669.         end
  12670.     end
  12671. end
  12672.  
  12673. args = {...}
  12674. if #args == 0 then
  12675.     print("Please input a destination folder.")
  12676. else
  12677.     writeDown(inputTable,args[1])
  12678. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement