* mikeOS 16 bit and amd64 baremetal
[mascara-docs.git] / i86 / mikeos-4.1.2 / source / features / cli.asm
blobbb7cb6974a56dbe7b9da3c30a08c94876eff11cc
1 ; ==================================================================
2 ; MikeOS -- The Mike Operating System kernel
3 ; Copyright (C) 2006 - 2011 MikeOS Developers -- see doc/LICENSE.TXT
5 ; COMMAND LINE INTERFACE
6 ; ==================================================================
8 os_command_line:
9 call os_clear_screen
11 mov si, version_msg
12 call os_print_string
13 mov si, help_text
14 call os_print_string
16 get_cmd:
17 mov si, prompt ; Main loop; prompt for input
18 call os_print_string
20 mov ax, input ; Get command string from user
21 call os_input_string
23 call os_print_newline
25 mov ax, input ; Remove trailing spaces
26 call os_string_chomp
28 mov si, input ; If just enter pressed, prompt again
29 cmp byte [si], 0
30 je get_cmd
32 mov si, input ; Convert to uppercase for comparison
33 call os_string_uppercase
36 mov si, input
38 mov di, exit_string ; 'EXIT' entered?
39 call os_string_compare
40 jc near exit
42 mov di, help_string ; 'HELP' entered?
43 call os_string_compare
44 jc near print_help
46 mov di, cls_string ; 'CLS' entered?
47 call os_string_compare
48 jc near clear_screen
50 mov di, dir_string ; 'DIR' entered?
51 call os_string_compare
52 jc near list_directory
54 mov di, ver_string ; 'VER' entered?
55 call os_string_compare
56 jc near print_ver
58 mov di, time_string ; 'TIME' entered?
59 call os_string_compare
60 jc near print_time
62 mov di, date_string ; 'DATE' entered?
63 call os_string_compare
64 jc near print_date
66 mov di, cat_string ; 'CAT' entered?
67 mov cl, 3
68 call os_string_strincmp
69 jc near cat_file
73 ; If the user hasn't entered any of the above commands, then we
74 ; need to check if an executable filename (.BIN) was entered...
76 mov si, input ; User entered dot in filename?
77 mov al, '.'
78 call os_find_char_in_string
79 cmp ax, 0
80 je suffix
82 jmp full_name
84 suffix:
85 mov ax, input
86 call os_string_length
88 mov si, input
89 add si, ax ; Move to end of input string
91 mov byte [si], '.'
92 mov byte [si+1], 'B'
93 mov byte [si+2], 'I'
94 mov byte [si+3], 'N'
95 mov byte [si+4], 0 ; Zero-terminate string
98 full_name:
99 mov si, input ; User tried to execute kernel?
100 mov di, kern_file_string
101 call os_string_compare
102 jc near kern_warning
104 mov ax, input ; If not, try to load specified program
105 mov bx, 0
106 mov cx, 32768
107 call os_load_file
108 mov word [file_size], bx
110 jc bin_fail ; Skip next part if program with .BIN not found
112 mov ax, input ; Get into right place in filename to check extension
113 call os_string_length
114 mov si, input
115 add si, ax
116 sub si, 3
118 mov di, bin_extension ; Is it BIN?
119 call os_string_compare
120 jnc is_basic_file
122 mov ax, 0 ; Clear all registers
123 mov bx, 0
124 mov cx, 0
125 mov dx, 0
126 mov si, 0
127 mov di, 0
129 call 32768 ; Call the external program
131 jmp get_cmd ; When program has finished, start again
134 is_basic_file:
135 mov ax, input
136 call os_string_length
137 mov si, input
138 add si, ax
139 sub si, 3
141 mov di, bas_extension
142 call os_string_compare
143 jnc total_fail
145 mov ax, 32768
146 mov word bx, [file_size]
147 call os_run_basic
149 jmp get_cmd
154 bin_fail:
155 mov ax, input
156 call os_string_length
158 mov si, input
159 add si, ax ; Move to end of input string
160 sub si, 4 ; Subtract 4 chars, because we added .BIN to it before!
162 mov byte [si], '.' ; See if there's a .BAS extension for this
163 mov byte [si+1], 'B'
164 mov byte [si+2], 'A'
165 mov byte [si+3], 'S'
166 mov byte [si+4], 0 ; Zero-terminate string
168 mov ax, input ; Try to load the .BAS code
169 mov bx, 0
170 mov cx, 32768
171 call os_load_file
172 jc total_fail ; Skip if program not found
174 mov ax, 32768
175 call os_run_basic ; Otherwise execute the code!
177 jmp get_cmd
179 total_fail:
180 mov si, invalid_msg
181 call os_print_string
183 jmp get_cmd
186 ; ------------------------------------------------------------------
188 print_help:
189 mov si, help_text
190 call os_print_string
191 jmp get_cmd
194 ; ------------------------------------------------------------------
196 clear_screen:
197 call os_clear_screen
198 jmp get_cmd
201 ; ------------------------------------------------------------------
203 print_time:
204 mov bx, tmp_string
205 call os_get_time_string
206 mov si, bx
207 call os_print_string
208 call os_print_newline
209 jmp get_cmd
212 ; ------------------------------------------------------------------
214 print_date:
215 mov bx, tmp_string
216 call os_get_date_string
217 mov si, bx
218 call os_print_string
219 call os_print_newline
220 jmp get_cmd
223 ; ------------------------------------------------------------------
225 print_ver:
226 mov si, version_msg
227 call os_print_string
228 jmp get_cmd
231 ; ------------------------------------------------------------------
233 kern_warning:
234 mov si, kern_warn_msg
235 call os_print_string
236 jmp get_cmd
239 ; ------------------------------------------------------------------
241 list_directory:
242 mov cx, 0 ; Counter
244 mov ax, dirlist ; Get list of files on disk
245 call os_get_file_list
247 mov si, dirlist
248 mov ah, 0Eh ; BIOS teletype function
250 .repeat:
251 lodsb ; Start printing filenames
252 cmp al, 0 ; Quit if end of string
253 je .done
255 cmp al, ',' ; If comma in list string, don't print it
256 jne .nonewline
257 pusha
258 call os_print_newline ; But print a newline instead
259 popa
260 jmp .repeat
262 .nonewline:
263 int 10h
264 jmp .repeat
266 .done:
267 call os_print_newline
268 jmp get_cmd
271 ; ------------------------------------------------------------------
273 cat_file:
274 mov si, input
275 call os_string_parse
276 cmp bx, 0 ; Was a filename provided?
277 jne .filename_provided
279 mov si, nofilename_msg ; If not, show error message
280 call os_print_string
281 jmp get_cmd
283 .filename_provided:
284 mov ax, bx
286 call os_file_exists ; Check if file exists
287 jc .not_found
289 mov cx, 32768 ; Load file into second 32K
290 call os_load_file
292 mov word [file_size], bx
294 cmp bx, 0 ; Nothing in the file?
295 je get_cmd
297 mov si, 32768
298 mov ah, 0Eh ; int 10h teletype function
299 .loop:
300 lodsb ; Get byte from loaded file
302 cmp al, 0Ah ; Move to start of line if we get a newline char
303 jne .not_newline
305 call os_get_cursor_pos
306 mov dl, 0
307 call os_move_cursor
309 .not_newline:
310 int 10h ; Display it
311 dec bx ; Count down file size
312 cmp bx, 0 ; End of file?
313 jne .loop
315 jmp get_cmd
317 .not_found:
318 mov si, notfound_msg
319 call os_print_string
320 jmp get_cmd
324 ; ------------------------------------------------------------------
326 exit:
330 ; ------------------------------------------------------------------
332 input times 255 db 0
333 dirlist times 1024 db 0
334 tmp_string times 15 db 0
335 file_size dw 0
337 bin_extension db 'BIN', 0
338 bas_extension db 'BAS', 0
340 prompt db '> ', 0
341 help_text db 'Inbuilt commands: DIR, CAT, CLS, HELP, TIME, DATE, VER, EXIT', 13, 10, 0
342 invalid_msg db 'No such command or program', 13, 10, 0
343 nofilename_msg db 'No filename specified', 13, 10, 0
344 notfound_msg db 'File not found', 13, 10, 0
345 version_msg db 'MikeOS ', MIKEOS_VER, 13, 10, 0
347 exit_string db 'EXIT', 0
348 help_string db 'HELP', 0
349 cls_string db 'CLS', 0
350 dir_string db 'DIR', 0
351 time_string db 'TIME', 0
352 date_string db 'DATE', 0
353 ver_string db 'VER', 0
354 cat_string db 'CAT', 0
356 kern_file_string db 'KERNEL.BIN', 0
357 kern_warn_msg db 'Cannot execute kernel file!', 13, 10, 0
360 ; ==================================================================