1. Edit procedure check exception 12 (overflow stack)
[kolibrios.git] / programs / shell.inc
bloba8bf4106f167dbf5c6cf3820c36c5b1f1ae286b6
1 ; PRIVATE FIELDS\r
2 \r
3  __shell.buf__  rd 1\r
4  __shell.name__ rb 16\r
5  __shell.str__  rb 16\r
6 \r
7 ;-----------------------------------\r
8 ; a = 0\r
9 ;-----------------------------------\r
11  macro clear a {\r
12     xor         a, a\r
13  }\r
15 ;-----------------------------------\r
16 ; if a == b then c\r
17 ;-----------------------------------\r
19  macro cmpe a, b, c {\r
20     cmp         a, b\r
21     je          c\r
22  }\r
24 ;-----------------------------------\r
25 ; if a != b then c\r
26 ;-----------------------------------\r
28  macro cmpne a, b, c {\r
29     cmp         a, b\r
30     jne         c\r
31  }\r
33 ;-----------------------------------\r
34 ; Shell`s initialization\r
35 ; without arguments\r
36 ;-----------------------------------\r
38  shell.init:\r
39     pusha\r
41   ; Get PID\r
42     mov         eax, 68\r
43     mov         ebx, 11\r
44     int         64\r
46     mov         eax, 68\r
47     mov         ebx, 12\r
48     mov         ecx, 1024\r
49     int         64\r
51     mov         esi, eax\r
53     mov         eax, 9\r
54     mov         ebx, esi\r
55     mov         ecx, -1\r
56     int         64\r
58     mov         eax, dword [esi + 30]\r
59     mov         [@pid], eax\r
61     mov         eax, 68\r
62     mov         ebx, 13\r
63     mov         ecx, esi\r
64     int         64\r
66   ; Convert PID to STR\r
67     mov         eax, [@pid]\r
68     mov         ebx, 10\r
69     clear       ecx\r
70   @@:\r
71     clear       edx\r
72     div         ebx\r
73     push        edx\r
74     inc         ecx\r
75     cmpne       eax, 0, @b\r
77     mov         ebx, __shell.name__\r
78   @@:\r
79     pop         eax\r
80     add         al, "0"\r
81     mov         [ebx], al\r
82     inc         ebx\r
83     loop        @b\r
85   ; Add postfix\r
86     mov         [ebx + 0], dword "-SHE"\r
87     mov         [ebx + 4], word "LL"\r
88     mov         [ebx + 6], byte 0\r
90   ; Open buffer\r
91     mov         eax, 68\r
92     mov         ebx, 22\r
93     mov         ecx, __shell.name__\r
94     mov         edx, 4096\r
95     mov         esi, 101b\r
96     int         64\r
98     mov         [__shell.buf__], eax\r
100     mov         eax, 5\r
101     mov         ebx, 5\r
102     int         64\r
104     popa\r
105     ret\r
107 ;-----------------------------------\r
108 ; Wait answer from shell\r
110 ; INPUT:\r
111 ; edi - shell-buffer\r
112 ;-----------------------------------\r
114  shell.wait:\r
115     pusha\r
117   @@:\r
118     mov         eax, 5\r
119     mov         ebx, 1\r
120     int         64\r
122     cmpne       [edi], byte 0, @b\r
124     popa\r
125     ret\r
127 ;-----------------------------------\r
128 ; Exit from shell\r
129 ; without arguments\r
130 ;-----------------------------------\r
132  shell.destroy:\r
133     pusha\r
135     mov         edi, [__shell.buf__]\r
136     mov         [edi], byte 1\r
137     call        shell.wait\r
139     mov         eax, 68\r
140     mov         ebx, 23\r
141     mov         ecx, __shell.name__\r
142     int         64\r
144     popa\r
145     ret\r
147 ;-----------------------------------\r
148 ; Print char to shell\r
150 ; INPUT:\r
151 ; al - char\r
152 ;-----------------------------------\r
154  shell.print_char:\r
155     pusha\r
157     mov         edi, [__shell.buf__]\r
158     mov         [edi], byte 2\r
159     mov         [edi + 1], al\r
160     call        shell.wait\r
162     popa\r
163     ret\r
165 ;-----------------------------------\r
166 ; Print string to shell\r
168 ; INPUT:\r
169 ; eax - string\r
170 ;-----------------------------------\r
172  shell.print_string:\r
173     pusha\r
175     mov         edi, [__shell.buf__]\r
176     mov         [edi], byte 3\r
177     inc         edi\r
179   @@:\r
180     mov         bl, [eax]\r
181     mov         [edi], bl\r
182     inc         edi\r
183     inc         eax\r
184     cmpne       [eax], byte 0, @b\r
186     mov         [edi], byte 0\r
187     mov         edi, [__shell.buf__]\r
188     call        shell.wait\r
190     popa\r
191     ret\r
193 ;-----------------------------------\r
194 ; Print number to shell\r
196 ; INPUT:\r
197 ; eax - number\r
198 ;-----------------------------------\r
200  shell.print_num:\r
201     pusha\r
203     mov         ecx, eax\r
204     and         ecx, 1 shl 31\r
205     cmpe        ecx, 0, @f\r
206     mov         [__shell.str__], "-"\r
207     not         eax\r
208     inc         eax\r
210   @@:\r
211     mov         ebx, 10\r
212     clear       ecx\r
213   @@:\r
214     clear       edx\r
215     div         ebx\r
216     push        edx\r
217     inc         ecx\r
218     cmpne       eax, 0, @b\r
220     mov         ebx, __shell.str__\r
221     inc         ebx\r
223   @@:\r
224     pop         eax\r
225     add         al, "0"\r
226     mov         [ebx], al\r
227     inc         ebx\r
228     loop        @b\r
230     mov         [ebx], byte 0\r
231     mov         eax, __shell.str__\r
232     cmpe        [eax], byte "-", @f\r
233     inc         eax\r
234   @@:\r
235     call        shell.print_string\r
237     popa\r
238     ret\r
240 ;-----------------------------------\r
241 ; Get char from shell\r
243 ; OUTPUT:\r
244 ; al - char\r
245 ;-----------------------------------\r
247  shell.get_char:\r
248     push        ebx ecx edx esi edi\r
250     mov         edi, [__shell.buf__]\r
251     mov         [edi], byte 4\r
252     call        shell.wait\r
253     mov         al, [edi + 1]\r
255     pop         edi esi edx ecx ebx\r
256     ret\r
258 ;-----------------------------------\r
259 ; Get string from shell\r
261 ; INPUT:\r
262 ; eax - addres of memory for str\r
263 ;-----------------------------------\r
265  shell.get_string:\r
266     pusha\r
268     mov         edi, [__shell.buf__]\r
269     mov         [edi], byte 5\r
270     call        shell.wait\r
272     inc         edi\r
273   @@:\r
274     mov         bl, [edi]\r
275     mov         [eax], bl\r
276     inc         eax\r
277     inc         edi\r
278     cmpne       [edi], byte 0, @b\r
280     mov         [eax], byte 0\r
282     popa\r
283     ret\r
285 ;-----------------------------------\r
286 ; Get number from shell\r
288 ; OUTPUT:\r
289 ; eax - number\r
290 ;-----------------------------------\r
292  shell.get_num:\r
293     push        ebx ecx edx esi edi\r
295     mov         eax, __shell.str__\r
296     call        shell.get_string\r
298     mov         ebx, eax\r
299     clear       eax\r
300     clear       edi\r
301     cmpne       [ebx], byte "-", @f\r
302     mov         edi, 1\r
303     inc         ebx\r
305   @@:\r
306     sub         [ebx], byte "0"\r
307     imul        eax, 10\r
308     add         al, [ebx]\r
309     inc         ebx\r
310     cmpne       [ebx], byte 10, @b\r
312     cmpe        edi, 0, @f\r
313     not         eax\r
314     inc         eax\r
316   @@:\r
317     pop         edi esi edx ecx ebx\r
318     ret\r
321 ;-----------------------------------\r
322 ; Clear shell\r
323 ; without arguments\r
324 ;-----------------------------------\r
326  shell.clear:\r
327     pusha\r
329     mov         edi, [__shell.buf__]\r
330     mov         [edi], byte 6\r
331     call        shell.wait\r
333     popa\r
334     ret\r
336 ;-----------------------------------\r
337 ; Print char to shell\r
339 ; INPUT:\r
340 ; arg1 - char\r
341 ;-----------------------------------\r
343  macro shpc [char] {\r
344     push        eax\r
345     mov         al, char\r
346     call        shell.print_char\r
347     pop         eax\r
348  }\r
350 ;-----------------------------------\r
351 ; Go to new line in shell\r
352 ; without arguments\r
353 ;-----------------------------------\r
355  macro shln {\r
356     shpc        10\r
357     shpc        13\r
358  }\r
360 ;-----------------------------------\r
361 ; Print string to shell\r
363 ; INPUT:\r
364 ; arg1 - string\r
365 ;-----------------------------------\r
367  macro shps [string] {\r
368     local       ..string, ..label\r
369     jmp         ..label\r
370  ..string       db string, 0\r
371  ..label:\r
373     push        eax\r
374     mov         eax, ..string\r
375     call        shell.print_string\r
376     pop         eax\r
377  }\r
379 ;-----------------------------------\r
380 ; Print string to shell\r
382 ; INPUT:\r
383 ; arg1 - addres of string\r
384 ;-----------------------------------\r
386  macro shpsa [addr] {\r
387     push        eax\r
388     mov         eax, addr\r
389     call        shell.print_string\r
390     pop         eax\r
391  }\r
393 ;-----------------------------------\r
394 ; Print number to shell\r
396 ; INPUT:\r
397 ; arg1 - number\r
398 ;-----------------------------------\r
400  macro shpn [num] {\r
401     push        eax\r
402     mov         eax, num\r
403     call        shell.print_num\r
404     pop         eax\r
405  }\r
408 ;-----------------------------------\r
409 ; Get char from shell\r
411 ; OUTPUT:\r
412 ; al - char\r
413 ;-----------------------------------\r
415  macro shgc {\r
416     call        shell.get_char\r
417  }\r
419 ;-----------------------------------\r
420 ; Get string from shell\r
422 ; INPUT:\r
423 ; arg1 - addres of memory for str\r
424 ;-----------------------------------\r
426  macro shgs [addr] {\r
427     mov         eax, addr\r
428     call        shell.get_string\r
429  }\r
431 ;-----------------------------------\r
432 ; Get number from shell\r
434 ; INPUT:\r
435 ; arg1 - addres of memory for num\r
436 ;-----------------------------------\r
438  macro shgn [addr] {\r
439     push        eax\r
440     call        shell.get_num\r
441     mov         [addr], eax\r
442     pop         eax\r
443  }\r
446 ;-----------------------------------\r
447 ; Wait any char from shell\r
448 ; without arguments\r
449 ;-----------------------------------\r
451  macro shw {\r
452     push        eax\r
453     call        shell.get_char\r
454     pop         eax\r
455  }\r
457 ;-----------------------------------\r
458 ; Clear shell\r
459 ; without arguments\r
460 ;-----------------------------------\r
462  macro shc {\r
463     call        shell.clear\r
464  }\r
466 ;-----------------------------------\r
467 ; Choice\r
469 ; INPUT:\r
470 ; arg1 - question\r
471 ; arg2 - asnwers\r
473 ; OUTPUT:\r
474 ; al - answer\r
475 ;-----------------------------------\r
477  macro shchs str, chs {\r
478     local       ..loop1, ..loop2, ..chs, ..start, ..res\r
479     jmp         ..start\r
480   ..chs         db chs, 0\r
481   ..start:\r
482     push        ebx\r
484     shps        str, " ["\r
485     shpsa       ..chs\r
486     shps        "]: "\r
488   ..loop1:\r
489     call        shell.get_char\r
491     mov         ebx, ..chs\r
492   ..loop2:\r
493     cmpe        al, [ebx], ..res\r
494     inc         ebx\r
495     cmpe        [ebx], byte 0, ..loop1\r
496     jmp         ..loop2\r
498   ..res:\r
499     mov         al, [ebx]\r
500     call        shell.print_char\r
501     shln\r
503     mov         eax, ebx\r
504     sub         eax, ..chs\r
506     pop         ebx\r