shell.l 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. (
  2. (def and (fn a b (if a (if b 1 0) 0)))
  3. (def or (fn a b (+ a b)))
  4. (def not (fn a (if a 0 1)))
  5. (def eq (fn a b (lt (+ (lt a b) (gt a b)) 1)))
  6. (def item (fn lst idx (do
  7. (let i 0)
  8. (let l lst)
  9. (while (gt idx i) (do
  10. (let l (cdr l))
  11. (let i (+ i 1))))
  12. (car l)
  13. )))
  14. (def list-size (fn lst (do
  15. (let i 0)
  16. (let l lst)
  17. (while (car l) (do
  18. (let l (cdr l))
  19. (let i (+ i 1))))
  20. i
  21. )))
  22. (def split (fn str sepstr (do
  23. (let sep (get sepstr 0))
  24. (let result (quote ()))
  25. (let sz (size str))
  26. (let i 0)
  27. (let i (- sz 1))
  28. (let last-i 0)
  29. (let last-i (+ i 1))
  30. (let partsize 0)
  31. (while (gt i -2) (do
  32. (if (or (eq (get str i) sep) (eq i -1)) (do
  33. (let partsize (- (- last-i i) 1))
  34. (if (gt partsize -1)
  35. (let result (cons (substr str (+ i 1) partsize) result)) 0)
  36. (let last-i i)
  37. ) 0)
  38. (let i (- i 1))
  39. ))
  40. result
  41. )))
  42. (def sintab [80828486888b8d8f919496989a9c9ea1a3a5a7a9abadafb2b4b6b8babcbec0c1c3c5c7c9cbcdced0d2d3d5d7d8dadcdddfe0e2e3e4e6e7e8eaebecedeeeff1f2f3f4f4f5f6f7f8f9f9fafbfbfcfcfdfdfefefefffffffffffffffffffffffffffffffefefefdfdfcfcfbfbfaf9f9f8f7f6f5f4f4f3f2f1efeeedecebeae8e7e6e4e3e2e0dfdddcdad8d7d5d3d2d0cecdcbc9c7c5c3c1c0bebcbab8b6b4b2afadaba9a7a5a3a19e9c9a989694918f8d8b88868482807d7b79777472706e6b69676563615e5c5a58565452504d4b49474543413f3e3c3a38363432312f2d2c2a2827252322201f1d1c1b1918171514131211100e0d0c0b0b0a0908070606050404030302020101010000000000000000000000000000000101010202030304040506060708090a0b0b0c0d0e1011121314151718191b1c1d1f2022232527282a2c2d2f31323436383a3c3e3f41434547494b4d50525456585a5c5e61636567696b6e70727477797b7d])
  43. (def sin (fn deg (get sintab (% deg 360))))
  44. (def cos (fn deg (get sintab (% (+ deg 90) 360))))
  45. (def abs (fn a (if (lt a 0) (- 0 a) a)))
  46. (def load (fn path (recv (open path))))
  47. (def ls (fn (do
  48. (split (load "/sd/") [0a])
  49. )))
  50. (def import (fn path (eval (read (recv (open path))))))
  51. (def scr (open "/framebuffer"))
  52. (def fb (mmap "/framebuffer"))
  53. (def screen-pitch 3840) ; // TODO read from framebuffer
  54. (def stroke-color 0x0000)
  55. (def set-pixel (fn x y c (do
  56. (let ofs (+ (* y screen-pitch) (shl x 1)))
  57. (put fb ofs (shr c 8))
  58. (put fb (+ 1 ofs) c)
  59. c
  60. )))
  61. (def pt list)
  62. (def line (fn a b (do
  63. (let xa (car a))
  64. (let ya (car (cdr a)))
  65. (let xb (car b))
  66. (let yb (car (cdr b)))
  67. (let dx (abs (- xb xa)))
  68. (let dy (abs (- yb ya)))
  69. (let sx (if (lt xa xb) 1 -1))
  70. (let sy (if (lt ya yb) 1 -1))
  71. (let err (if (gt dx dy) dx (- 0 dy)))
  72. (let err (/ err 2))
  73. (let e2 0)
  74. (while (not (and (eq xa xb) (eq ya yb))) (do
  75. (set-pixel xa ya stroke-color)
  76. (let e2 err)
  77. (if (gt e2 (- 0 dx)) (do (let err (- err dy)) (let xa (+ xa sx))) 0)
  78. (if (lt e2 dy) (do (let err (+ err dx)) (let ya (+ ya sy))) 0)
  79. ))
  80. 0
  81. )))
  82. (def draw-logo (fn ox oy (do
  83. (def stroke-color 0xff8e)
  84. (line (pt (+ ox 16) (- oy 38)) (pt (+ ox 16) (- oy 102)))
  85. (line (pt (+ ox 16) (- oy 102)) (pt (+ ox 80) (- oy 38)))
  86. (line (pt (+ ox 80) (- oy 38)) (pt (+ ox 80) (- oy 102)))
  87. (line (pt (+ ox 80) (- oy 102)) (pt (+ ox 144) (- oy 38)))
  88. (line (pt (+ ox 144) (- oy 38)) (pt (+ ox 144) (- oy 102)))
  89. (line (pt (+ ox 144) (- oy 102)) (pt (+ ox 208) (- oy 38)))
  90. (line (pt (+ ox 208) (- oy 38)) (pt (+ ox 208) (- oy 102)))
  91. (line (pt (+ ox 208) (- oy 102)) (pt (+ ox 272) (- oy 102)))
  92. )))
  93. (draw-logo 824 550)
  94. (draw-logo 825 550)
  95. (draw-logo 824 551)
  96. (def f (open "/sd/unifont.bin"))
  97. (def unifont (recv f))
  98. (def unifont-pitch 4096)
  99. (def font unifont)
  100. (def font-pitch unifont-pitch)
  101. (def rune-w 16)
  102. (def rune-spacing 8)
  103. (def rune-h 16)
  104. (def rune-mod 256)
  105. (def set-unifont (fn (do
  106. (def font unifont)
  107. (def font-pitch unifont-pitch)
  108. (def rune-w 16)
  109. (def rune-spacing 8)
  110. (def rune-h 16)
  111. (def rune-mod 256)
  112. )))
  113. (def fghi 0xff)
  114. (def fglo 0x00)
  115. (def blit-char16 (fn rune x y (do
  116. (let sx 0)
  117. (let sy 0)
  118. (let so 0)
  119. (let do 0)
  120. (let iy 0)
  121. (let rune-ww 0)
  122. (let c 0)
  123. (let d 0)
  124. (let sx (* rune-w (% rune rune-mod)))
  125. (let sy (* rune-h (/ rune rune-mod)))
  126. (let so (+ (* sx 2) (* sy font-pitch)))
  127. (let do (+ (* x 2) (* y screen-pitch)))
  128. (let rune-ww (+ rune-spacing rune-spacing))
  129. (while (lt iy rune-h) (do
  130. (let ix 0)
  131. (while (lt ix rune-ww) (do
  132. (let c (get font (+ so ix)))
  133. (let d (get font (+ 1 (+ so ix))))
  134. (put fb (+ do ix) c)
  135. (put fb (+ (+ do ix) 1) d)
  136. (let ix (+ ix 2))
  137. ))
  138. (let so (+ so font-pitch))
  139. (let do (+ do screen-pitch))
  140. (let iy (+ iy 1))
  141. ))
  142. 0
  143. )))
  144. (def blit-char (fn rune x y (do
  145. (let sx 0)
  146. (let sy 0)
  147. (let so 0)
  148. (let do 0)
  149. (let iy 0)
  150. (let rune-ww 0)
  151. (let c 0)
  152. (let d 0)
  153. (let sx (* rune-w (% rune rune-mod)))
  154. (let sy (* rune-h (/ rune rune-mod)))
  155. (let so (+ sx (* sy font-pitch)))
  156. (let do (+ (* x 2) (* y screen-pitch)))
  157. (let rune-ww rune-spacing)
  158. (while (lt iy rune-h) (do
  159. (let ix 0)
  160. (let dx 0)
  161. (while (lt ix rune-ww) (do
  162. (let c (get font (+ so ix)))
  163. (let dx (+ do (shl ix 1)))
  164. (put fb dx c)
  165. (put fb (+ dx 1) c)
  166. (let ix (+ ix 1))
  167. ))
  168. (let so (+ so font-pitch))
  169. (let do (+ do screen-pitch))
  170. (let iy (+ iy 1))
  171. ))
  172. 0
  173. )))
  174. (def grab-from fb)
  175. (def grab-pitch screen-pitch)
  176. (def grab (fn x y w h (do
  177. (let xx 0)
  178. (let yy 0)
  179. (let di 0)
  180. (let yy (+ y 0))
  181. (let xw (+ x w))
  182. (let yh (+ y h))
  183. (let res (alloc (* (shl w 1) h)))
  184. (let from grab-from)
  185. (let pitch grab-pitch)
  186. (while (lt yy yh) (do
  187. (let xx (+ x 0))
  188. (while (lt xx xw) (do
  189. (put res di (get from (+ xx (* pitch yy))))
  190. (let di (+ di 1))
  191. (put res di (get from (+ (+ xx (* pitch yy)) 1)))
  192. (let di (+ di 1))
  193. (let xx (+ xx 1))
  194. ))
  195. (let yy (+ yy 1))
  196. ))
  197. res
  198. )))
  199. (def paste (fn from x y w h (do
  200. (let xx 0)
  201. (let yy 0)
  202. (let di 0)
  203. (let si 0)
  204. (let yy (+ y 0))
  205. (let xw (+ x w))
  206. (let yh (+ y h))
  207. (let to grab-from)
  208. (let pitch (+ grab-pitch 0))
  209. (while (lt yy yh) (do
  210. (let xx (+ x 0))
  211. (while (lt xx xw) (do
  212. (let di (+ xx (* pitch yy)))
  213. (put to di (get from si))
  214. (put to (+ di 1) (get from (+ si 1)))
  215. (let si (+ si 2))
  216. (let di (+ di 2))
  217. (let xx (+ xx 1))
  218. ))
  219. (let yy (+ yy 1))
  220. ))
  221. 1
  222. )))
  223. ; 112 x 30 chars at scale 2
  224. (def scale 2)
  225. (def maxx (/ 1847 scale))
  226. (def maxy 1015)
  227. (def minx 32)
  228. (def miny 32)
  229. (def blit-str (fn str x y (do
  230. (let i 0)
  231. (let xx 0)
  232. (let yy 0)
  233. (let xx (+ x 0))
  234. (let yy (+ y 0))
  235. (let sz (+ (size str) 0))
  236. (let c 0)
  237. (while (lt i sz) (do
  238. (let c (get str i))
  239. (blit-char c xx yy)
  240. (let xx (+ xx rune-spacing))
  241. ; newline
  242. (if (or (eq c 10) (gt xx maxx)) (do
  243. (let xx minx)
  244. (let yy (+ yy rune-h))
  245. (if (gt yy maxy) (do
  246. (let yy miny)) 0)
  247. ) 0)
  248. (let i (+ i 1))
  249. (if (get str i) 0 (let i sz)) ; stop at 0
  250. ))
  251. yy
  252. )))
  253. (def boxfill (fn x y w h color (do
  254. (let ofs 0)
  255. (let xi 0)
  256. (let yi 0)
  257. (let xi (+ x 0))
  258. (let yi (+ y 0))
  259. (let xx (+ x w))
  260. (let yy (+ y h))
  261. (let chi 0)
  262. (let clo 0)
  263. (let chi (shr color 8))
  264. (let clo (bitand color 0xff))
  265. (let ofs (+ (* y screen-pitch) (shl x 1)))
  266. (let ww (shl w 1))
  267. (while (lt yi yy) (do
  268. (let xi (+ x 0))
  269. (while (lt xi xx) (do
  270. (put fb ofs chi)
  271. (put fb (+ 1 ofs) clo)
  272. (let xi (+ xi 1))
  273. (let ofs (+ ofs 2))
  274. ))
  275. (let ofs (- (+ ofs screen-pitch) ww))
  276. (let yi (+ yi 1))
  277. ))
  278. 0 ; crashes x64 if this is not here
  279. )))
  280. (def clear (fn (do
  281. (boxfill 0 0 maxx maxy 0xffff)
  282. (def term-x minx)
  283. (def term-y miny)
  284. 0)))
  285. (blit-str "Welcome to Interim OS." 32 32)
  286. (def evbuf (alloc-str 4096))
  287. (def p (fn xp x y (do
  288. (write xp evbuf)
  289. (blit-str evbuf x y)
  290. )))
  291. (def keyboard (open "/keyboard"))
  292. (def strlen (fn s (if s (do
  293. (let i 0)
  294. (let sz (size s))
  295. (let c (get s 0))
  296. (while (* (gt c 0) (lt i sz)) (do
  297. (let i (+ i 1))
  298. (let c (get s i))
  299. ))
  300. i) 0)
  301. ))
  302. (def term-x minx)
  303. (def term-y (+ miny 32))
  304. (def history (list))
  305. (def future (list))
  306. (def buffer "")
  307. (def history-back (fn (do
  308. (def buffer (car history))
  309. (def future (cons (car history) future))
  310. (def history (cdr history))
  311. (print (list "history:" history "future:" future))
  312. (def term-x (+ minx (* rune-spacing (strlen buffer))))
  313. (blit-str buffer minx term-y)
  314. )))
  315. (def history-forth (fn (do
  316. (def buffer (car future))
  317. (def history (cons (car future) history))
  318. (def future (cdr future))
  319. (print (list "history:" history "future:" future))
  320. (def term-x (+ minx (* rune-spacing (strlen buffer))))
  321. (blit-str buffer minx term-y)
  322. )))
  323. (def tasks (list))
  324. (def add-task (fn t (do
  325. (def tasks (cons t tasks))
  326. )))
  327. (def mouse (open "/mouse"))
  328. (def mouse-x 0)
  329. (def mouse-y 0)
  330. (def mouse-dx 0)
  331. (def mouse-dy 0)
  332. (def mouse-btn 0)
  333. (def mouse-task (fn (do
  334. (add-task (fn (do
  335. (blit-char 32 mouse-x mouse-y)
  336. (let mouse-info (recv mouse))
  337. (def mouse-dx (car (car mouse-info)))
  338. (def mouse-dy (cdr (car mouse-info)))
  339. (def mouse-x (+ mouse-x mouse-dx))
  340. (def mouse-y (+ mouse-y mouse-dy))
  341. (if (lt mouse-x 0) (def mouse-x 0) 0)
  342. (if (lt mouse-y 0) (def mouse-y 0) 0)
  343. (if (gt mouse-x maxx) (def mouse-x maxx) 0)
  344. (if (gt mouse-y maxy) (def mouse-y maxy) 0)
  345. (def mouse-btn (cdr mouse-info))
  346. (if mouse-btn (blit-char 0x219c mouse-x mouse-y)
  347. (blit-char 0x2196 mouse-x mouse-y))
  348. )))
  349. )))
  350. (def net (open "/net"))
  351. (def net-y 32)
  352. (def temp-minx minx)
  353. (def temp-maxx maxx)
  354. (def irc-msg (fn msg (do
  355. (let ircbuf (concat "PRIVMSG #nodrama.de :" msg))
  356. (send net ircbuf)
  357. (send net [0a])
  358. )))
  359. (def cmdbuf (alloc-str 512))
  360. (def cmd-read (list))
  361. (def remote-cmd (fn msg (do
  362. (let parts (split msg "$"))
  363. (if (gt (list-size parts) 1) (do
  364. (let cmd (concat (concat "(" (item parts 1)) ")"))
  365. (print (list "remote cmd" cmd))
  366. (def cmdbuf (alloc-str 512))
  367. (def cmd-read (read cmd))
  368. (write (eval cmd-read) cmdbuf)
  369. (print (list "result" cmdbuf))
  370. (irc-msg cmdbuf)
  371. ) 0)
  372. 0
  373. )))
  374. (def freenode "/net/tcp/62.231.75.133/6667")
  375. (def sternfreunde "/net/tcp/46.101.207.85/80")
  376. (def interim-os "/net/tcp/91.250.115.15/80")
  377. (def connect (fn net-path (do
  378. (def net (open net-path))
  379. )))
  380. (def net-task (fn (do
  381. (add-task (fn (do
  382. (let packet (recv net))
  383. (if (size packet) (do
  384. (def temp-minx minx)
  385. (def temp-maxx maxx)
  386. (def minx 1000)
  387. (def maxx 1700)
  388. (let msg (bytes->str packet))
  389. (boxfill 1000 net-y 716 64 0xffff)
  390. (let ofsy (+ (blit-str msg minx net-y) rune-h))
  391. (def minx temp-minx)
  392. (def maxx temp-maxx)
  393. (def net-y (+ 0 ofsy))
  394. (if (gt net-y maxy) (def net-y miny) 0)
  395. (remote-cmd msg)
  396. ) 0)
  397. )))
  398. 1
  399. )))
  400. (def http-get (fn host path (do
  401. (boxfill 1000 0 800 1000 0xffff)
  402. (let header (concat (concat "Host: " host) (bytes->str [0d0a0d0a])))
  403. (send net (concat (concat (concat (concat "GET " path) " HTTP/1.1") (bytes->str [0d0a])) header))
  404. )))
  405. (def irc-join (fn nick (do
  406. (send net "PASS *")
  407. (send net [0a])
  408. (send net (concat "NICK " nick))
  409. (send net [0a])
  410. (send net (concat "USER " (concat nick " 8 * :Interim OS")))
  411. (send net [0a])
  412. (send net "JOIN #nodrama.de")
  413. (send net [0a])
  414. )))
  415. (def task-func (fn (print "empty task-func")))
  416. (def run-tasks (fn (do
  417. (let tl tasks)
  418. (while (car tl) (do
  419. (def task-func (car tl))
  420. ; (print (list "run-task " task-func))
  421. (task-func)
  422. (let tl (cdr tl))
  423. ))
  424. )))
  425. (def triangle (fn a b c (do
  426. (line a b)
  427. (line b c)
  428. (line a c)
  429. )))
  430. (def box (fn tl br (do
  431. (let tr (list (car br) (car (cdr tl))))
  432. (let bl (list (car tl) (car (cdr br))))
  433. (line tl tr)
  434. (line bl br)
  435. (line tr br)
  436. (line tl bl)
  437. )))
  438. (def circle (fn cx cy r (do
  439. (let x 0)
  440. (while (lt x 359) (do
  441. (set-pixel (+ cx (* (sin x) r)) (+ cy (* (cos x) r)) stroke-color)
  442. (let x (+ x 1))
  443. ))
  444. x
  445. )))
  446. (def ed (fn (import "/sd/tests/editlite.l") ))
  447. (def buffer-read (list))
  448. (def zz (fn (import "/sd/tests/gtn.l")))
  449. (def main (fn (do
  450. (let blink 0)
  451. (let running 1)
  452. (while running (do
  453. (let str (recv keyboard))
  454. (let c (get str 0))
  455. (if (gt c 0) (print c) 0)
  456. ; FIXME this aint working
  457. (if (* (gt c 0x1f) (not (eq 0x7f c))) (do
  458. (def term-y (blit-str str term-x term-y))
  459. (def buffer (concat buffer str))
  460. (def term-x (+ term-x rune-spacing)) ) 0)
  461. (if (eq c 9) ; tab
  462. (do
  463. (blit-char 32 term-x term-y)
  464. (def term-y (+ term-y 16))
  465. (def term-x 32) (def buffer "")) 0)
  466. (if (eq c 10) ; return
  467. (do
  468. (blit-char 32 term-x term-y)
  469. (def history (cons buffer history))
  470. (def buffer-read (list (read buffer))) ; FIXME let here crashes
  471. (let result (eval buffer-read))
  472. (def buffer "")
  473. (def term-x minx)
  474. (def term-y (+ term-y rune-h))
  475. (def term-y (+ rune-h (p result term-x term-y)))
  476. 0
  477. ) 0)
  478. (if (eq c 17) ; cursor up
  479. (history-back) 0)
  480. (if (eq c 18) ; cursor down
  481. (history-forth) 0)
  482. (if (eq c 0x7f) ; bksp
  483. (if (gt (strlen buffer) 0)
  484. (do
  485. (blit-char 32 term-x term-y)
  486. (def term-x (- term-x rune-spacing))
  487. (let nl (- (strlen buffer) 1))
  488. (def buffer (substr buffer 0 nl)) ) 0) 0)
  489. (if (gt term-x maxx) (do (def term-x minx) (def term-y (+ term-y rune-h))) 0)
  490. (if (gt term-y maxy) (def term-y miny) 0)
  491. (if (lt term-x 32) (def term-x minx) 0)
  492. (if (eq blink 9)
  493. (blit-char 0x2588 term-x term-y) 0)
  494. (if (eq blink 0)
  495. (blit-char 32 term-x term-y) 0)
  496. (let blink (% (+ blink 1) 20))
  497. (run-tasks)
  498. (send scr 0)
  499. (gc)
  500. ))
  501. )))
  502. (main)
  503. )