cli.4th 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. .( cli.4th - some simple commands )
  2. base @ decimal
  3. \ just some simple commands to work with the file system
  4. : print-file-size ( b u -- )
  5. r/o open-file
  6. if drop 0
  7. else dup file-size drop \ fileid size
  8. swap close-file drop \ size
  9. then
  10. 10 .r ;
  11. \ -
  12. .( ls - list the root directory )
  13. : ls ( -- )
  14. cr s" /" open-directory ?dup
  15. if cr ." open-directory error = "
  16. dec. drop exit
  17. then
  18. >r \ save dirid
  19. begin
  20. here 256 r@ read-directory ?dup
  21. if cr ." directory read error = "
  22. dec. drop
  23. r> close-directory drop exit
  24. then
  25. dup
  26. while
  27. here swap 2dup print-file-size
  28. space type cr
  29. repeat
  30. drop
  31. r> close-directory drop ;
  32. \ -
  33. .( dir - list the root directory )
  34. : dir ( -- ) ls ;
  35. \ -
  36. .( mkdir <dir> - create a new directory )
  37. : mkdir ( -- \ <string><space> )
  38. bl parse \ b u
  39. create-directory \ ior
  40. ?dup if
  41. cr ." mkdir error = " dec. drop exit
  42. then
  43. ;
  44. \ -
  45. .( display <file> - display a text file on the console )
  46. : display-file ( b u -- )
  47. cr r/o open-file ?dup
  48. if cr ." open error = " dec. drop exit
  49. then
  50. >r \ save fileid
  51. begin
  52. here 256 r@ read-line ?dup \ u2 f ior ior?
  53. if cr ." read error = " dec. 2drop
  54. r> close-file drop exit
  55. then
  56. while \ u2
  57. here swap type cr
  58. repeat
  59. drop
  60. r> close-file drop ;
  61. \ display a specific file
  62. : display ( -- \ <string><space> ) bl parse display-file ;
  63. \ -
  64. .( delete <file> - erase a file )
  65. : delete ( -- \ <string><space> ) bl parse delete-file
  66. ?dup if cr ." delete error = " dec. then ;
  67. \ -
  68. .( rm <file> - erase a file )
  69. : rm delete ;
  70. \ -
  71. .( rename <file-old> <file-new> - change the name of a file )
  72. : rename ( -- \ <string><space><string><space )
  73. bl parse bl parse rename-file
  74. ?dup if cr ." rename error = " dec. then ;
  75. \ -
  76. .( mv <file-old> <file-new> - change the name of a file )
  77. : mv rename ;
  78. \ -
  79. .( mkfile <file> - create a text file with dummy data )
  80. : mkfile ( -- \ filename )
  81. bl parse w/o create-file ?dup
  82. if cr ." create error = " dec. drop exit
  83. then
  84. >r \ save fileid
  85. s" this is the first line of text" r@ write-line drop
  86. s" this is the second line of text" r@ write-line drop
  87. s" this is the third line of text" r@ write-line drop
  88. s" 0123456789" r@ write-line drop
  89. s" abcdefghijklmnopqrstuvwxyz" r@ write-line drop
  90. r> close-file drop ;
  91. \ -
  92. .( spinner - draw a spinner )
  93. variable spin-pos
  94. \ display spinner + a number (updated when mod == 0)
  95. : spin-char ( u -- u' c )
  96. case
  97. 0 of 1 [char] - endof
  98. 1 of 2 [char] \ endof
  99. 2 of 3 [char] | endof
  100. 3 of 0 [char] / endof
  101. drop 0 [char] * 0
  102. endcase
  103. ;
  104. : spinner ( number modulo -- )
  105. 13 emit \ back to start of line
  106. spin-pos @ spin-char emit spin-pos !
  107. over swap mod 0= if 8 u.r else drop then
  108. ;
  109. \ -
  110. .( scan <file> - read a file, no output )
  111. : scan-file ( b u -- )
  112. cr r/o open-file ?dup
  113. if cr ." open error = " dec. drop exit
  114. then
  115. >r \ save fileid
  116. -1
  117. begin
  118. 1+
  119. here 1024 r@ read-file ?dup \ u2 ior ior?
  120. if cr ." read error = " dec. drop
  121. r> close-file drop
  122. cr ." 1kB blocks =" .
  123. exit
  124. then
  125. while
  126. [char] . emit
  127. repeat
  128. cr ." 1kB blocks =" .
  129. r> close-file drop ;
  130. \ scan a specific file
  131. : scan ( -- \ <string><space> ) bl parse scan-file ;
  132. \ -
  133. .( stress <file> - create a big text file with dummy data )
  134. 100000 constant stress-level
  135. : create-big-file ( b u -- )
  136. w/o create-file ?dup
  137. if cr ." create error = " dec. drop exit
  138. then
  139. >r \ save fileid
  140. cr
  141. stress-level begin
  142. s" 0123456789!@#$%^&*()-_=+[]{};;:,./?><`~|\ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" r@ write-line
  143. 0= if r> close-file drop
  144. cr ." counts =" dec. dec. exit
  145. then
  146. dup 100 spinner
  147. r@ flush-file drop
  148. 1- ?dup while
  149. repeat
  150. r> close-file drop ;
  151. : stress ( -- \ filename )
  152. bl parse 2dup delete-file drop
  153. create-big-file ;
  154. \ -
  155. .( scan-disk - read absolute sectors )
  156. : dump-sector ( u -- )
  157. >r here 1024 + 1 r> read-sectors
  158. cr ." rc =" .
  159. here 1024 + 511 dump
  160. ;
  161. : scan-sector ( u -- f )
  162. >r here 1024 + 1 r> read-sectors ?dup
  163. if cr ." rc =" . true else false then
  164. ;
  165. : dump-disk-from ( u -- )
  166. begin
  167. dup cr ." sector =" .
  168. dup dump-sector
  169. 1+
  170. enough? until
  171. drop
  172. ;
  173. : scan-disk-from ( u -- )
  174. cr
  175. begin
  176. dup 100 spinner
  177. dup scan-sector if drop exit then
  178. 1+
  179. enough? until
  180. drop
  181. ;
  182. : scan-disk ( -- )
  183. 0 scan-disk-from ;
  184. \ -
  185. .( write-test - write absolute sectors )
  186. 1 constant write-count
  187. 512 constant sector-size
  188. : buffer-1 ( -- b ) here 1024 + ;
  189. : buffer-2 ( -- b ) here 1024 +
  190. write-count sector-size * + ;
  191. : write-sector ( u -- f )
  192. >r
  193. buffer-1 write-count r@ read-sectors ?dup
  194. if cr ." read rc =" .
  195. r> drop
  196. true exit
  197. then
  198. buffer-1 write-count r@ write-sectors ?dup
  199. if cr ." write rc =" .
  200. r> drop
  201. true exit
  202. then
  203. buffer-2 write-count r@ read-sectors ?dup
  204. if cr ." verify1 rc =" . cr
  205. buffer-2 write-count r@ read-sectors ?dup
  206. if cr ." verify2 rc =" .
  207. r> drop
  208. true exit
  209. then
  210. then
  211. buffer-1 write-count buffer-2 write-count compare
  212. if cr ." verify failed"
  213. r> drop
  214. true exit
  215. then
  216. r> drop
  217. false
  218. ;
  219. : write-test-from ( u -- )
  220. cr
  221. begin
  222. dup 100 spinner
  223. dup write-sector if drop exit then
  224. 1+
  225. enough? until
  226. drop
  227. ;
  228. : write-test ( -- )
  229. 0 write-test-from
  230. ;
  231. .( complete )
  232. base !