Fixed ZDE build - missing header file
[ZeXOS.git] / kernel / core / commands.c
blob3f0d4250b6049ac5118a2dddc7b77c6f53f416fd
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <build.h>
24 #include <system.h>
25 #include <arch/io.h>
26 #include <string.h>
27 #include <partition.h>
28 #include <env.h>
29 #include <dev.h>
30 #include <vfs.h>
31 #include <fs.h>
32 #include <tty.h>
33 #include <proc.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <net/socket.h>
37 #include <net/dhcp.h>
38 #include <module.h>
39 #include <commands.h>
40 #include <signal.h>
41 #include <smp.h>
42 #include <build.h>
43 #include <config.h>
44 #include <net/if.h>
45 #include <net/net.h>
46 #include <net/ip.h>
47 #include <net/ndp.h>
48 #include <net/tun.h>
49 #include <net/icmp.h>
50 #include <net/hostname.h>
51 #include <mouse.h>
52 #include <pipe.h>
53 #include <cache.h>
54 #include <sound/audio.h>
56 command_t command_list;
57 extern partition_t partition_list;
59 unsigned char cmd_buf[256];
61 typedef void (*void_fn_void_t)(void);
63 extern task_t task_list;
64 extern vfs_t vfs_list;
65 extern unsigned long file_cache_id;
66 extern task_t *_curr_task;
67 extern module_t *module_load (char *modname);
68 extern unsigned long timer_ticks;
71 char *argparse (char *cmd)
73 unsigned cmd_len = strlen (cmd);
74 unsigned p = 1;
76 while (p < cmd_len) {
77 if (cmd[p] == ' ')
78 return cmd + p + 1;
80 p ++;
83 return "";
86 /*************************************************************\
87 | OMMANDs |
88 \*************************************************************/
91 unsigned command_help (char *command, unsigned len)
93 command_t *ctx;
95 tty_t *tty = currtty;
97 tty_cls (tty);
99 for (ctx = command_list.next; ctx != &command_list; ctx = ctx->next) {
100 printf ("%s : %s\n", ctx->name, ctx->desc);
102 if (tty->screen_y >= (tty_char.y-1)) {
103 if (!vgagui)
104 video_color (0, 15);
106 printf ("Press enter to continue");
108 video_color (15, 0);
109 printf (" ");
111 /* wait, until enter key is pressed */
112 for (;;)
113 if (tty == currtty) {
114 if (getkey () != '\n')
115 schedule ();
116 else
117 break;
118 } else
119 schedule ();
121 tty_cls (tty);
125 return 1;
128 unsigned command_hdd (char *command, unsigned len)
130 #ifdef ARCH_i386
131 int t;
132 int m;
133 int c;
134 int c2;
135 int h;
136 int s;
138 outb (0x70, 0x12);
139 t = inb (0x71);
141 if (t >> 4 == 0)
142 printf ("/dev/hda not installed\n");
143 else {
144 outb (0x70, 0x1b);
145 c = inb (0x71);
146 outb (0x70, 0x1c);
147 c2 = inb (0x71);
148 outb (0x70, 0x1d);
149 h = inb (0x71);
150 outb (0x70, 0x23);
151 s = inb (0x71);
153 printf ("/dev/hda installed - CHS=%d-%d:%d:%d\n", c, c2, h, s);
156 if (t & 0xF == 0)
157 printf ("/dev/hdb not installed\n");
158 else {
159 outb (0x70, 0x24);
160 c = inb (0x71);
161 outb (0x70, 0x25);
162 c2 = inb (0x71);
163 outb (0x70, 0x26);
164 h = inb (0x71);
165 outb (0x70, 0x2c);
166 s = inb (0x71);
168 printf ("/dev/hdb installed - CHS=%d-%d:%d:%d\n", c, c2, h, s);
170 #endif
172 return 1;
175 unsigned command_reboot (char *command, unsigned len)
177 arch_cpu_reset ();
179 while (1);
181 return 1;
184 unsigned command_halt (char *command, unsigned len)
186 arch_cpu_shutdown ();
188 kprintf ("\nSystem halted\nPlease press power button ..");
190 int_disable ();
192 while (1);
194 return 1;
197 unsigned command_tasks (char *command, unsigned len)
199 printf ("id\tname\tpriority\n");
201 task_t *task;
202 for (task = task_list.next; task != &task_list; task = task->next)
203 printf ("%d\t%s\t%u\n", task->id, task->name, task->priority);
205 return 1;
208 unsigned command_ps (char *command, unsigned len)
210 proc_display ();
212 return 1;
215 unsigned command_uptime (char *command, unsigned len)
217 uptime ();
219 return 1;
222 unsigned command_version (char *command, unsigned len)
224 osinfo ();
226 return 1;
229 unsigned command_debug (char *command, unsigned len)
231 strcpy (cmd_buf, argparse (command));
233 unsigned l = strlen (cmd_buf);
235 if (l) {
236 debug = atoi (cmd_buf);
238 printf ("Developer mode was enabled with mask 0x%x.\n", debug);
240 return 1;
243 printf ("Developer mode was disabled.\n");
244 debug = 0;
246 return 1;
249 unsigned command_mount (char *command, unsigned len)
251 strcpy (cmd_buf, argparse (command));
252 char devname[20];
253 char mountpoint[32];
255 unsigned l = strlen (cmd_buf);
256 unsigned x = 0;
257 while (x < l) {
258 if (cmd_buf[x] == ' ')
259 break;
260 x ++;
263 memcpy (devname, cmd_buf, x);
264 devname[x] = '\0';
265 strcpy (mountpoint, argparse (cmd_buf));
266 unsigned y = strlen (mountpoint);
268 if (mountpoint[y-1] != '/') {
269 mountpoint[y] = '/';
270 mountpoint[y+1] = '\0';
273 if (x && y) {
274 partition_t *p = partition_find (devname);
276 if (p)
277 mount (p, "", mountpoint);
278 else
279 printf ("ERROR -> partition %s does not exists\n", devname);
280 } else
281 mount_display ();
283 return 1;
286 unsigned command_umount (char *command, unsigned len)
288 char mountpoint[32];
289 strcpy (mountpoint, argparse (command));
291 unsigned y = strlen (mountpoint);
292 unsigned x = 0;
294 if (mountpoint[y-1] != '/') {
295 mountpoint[y] = '/';
296 mountpoint[y+1] = '\0';
299 if (y) {
300 partition_t *partition;
301 for (partition = partition_list.next; partition != &partition_list; partition = partition->next) {
302 if (umount (partition, mountpoint))
303 x ++;
306 if (!x)
307 printf ("ERROR -> directory %s is not mounted\n", mountpoint);
309 if (x > 1)
310 printf ("WARNING -> multiple (%d) umount of %s\n", x, mountpoint);
311 } else
312 return 0;
314 return 1;
317 unsigned command_env (char *command, unsigned len)
319 env_display ();
321 return 1;
324 unsigned command_cd (char *command, unsigned len)
326 strcpy (cmd_buf, argparse (command));
328 cd (cmd_buf);
330 return 1;
333 unsigned command_ls (char *command, unsigned len)
335 strcpy (cmd_buf, argparse (command));
337 ls (cmd_buf);
339 return 1;
342 unsigned command_cat (char *command, unsigned len)
344 strcpy (cmd_buf, argparse (command));
346 cat (cmd_buf);
348 return 1;
351 unsigned command_cp (char *command, unsigned len)
353 strcpy (cmd_buf, argparse (command));
355 unsigned l = strlen (cmd_buf);
356 unsigned p = 0;
358 while (l) {
359 if (cmd_buf[l] == ' ') {
360 cmd_buf[l] = '\0';
361 p = l;
364 l --;
367 if (!p) {
368 printf ("Syntax:\ncp <source> <destination>\n");
369 return 0;
372 cp (cmd_buf, cmd_buf+p+1);
374 return 1;
378 unsigned command_rm (char *command, unsigned len)
380 strcpy (cmd_buf, argparse (command));
382 rm (cmd_buf);
384 return 1;
387 unsigned command_mkdir (char *command, unsigned len)
389 strcpy (cmd_buf, argparse (command));
391 mkdir (cmd_buf);
393 return 1;
396 unsigned command_touch (char *command, unsigned len)
398 strcpy (cmd_buf, argparse (command));
400 touch (cmd_buf);
402 return 1;
405 unsigned command_exec (char *command, unsigned len)
407 if (!strncmp ("./", command, 2))
408 strcpy (cmd_buf, command+2);
409 else
410 strcpy (cmd_buf, argparse (command));
412 unsigned arg_len = strlen (argparse (cmd_buf));
414 if (arg_len >= 128)
415 return 0;
417 char arg[128];
418 strcpy (arg, argparse (cmd_buf));
419 arg[arg_len] = '\0';
421 unsigned cmd_buf_len = strlen (cmd_buf);
423 tty_t *tty = currtty;
425 if (!tty)
426 return 0;
428 /* lock current tty keyboard */
429 tty_lock (tty);
431 /* if there is argument after name, rewrite space character to zero */
432 if (cmd_buf[cmd_buf_len-(arg_len+1)] == ' ')
433 cmd_buf[cmd_buf_len-(arg_len+1)] = '\0';
434 else
435 cmd_buf[cmd_buf_len-arg_len] = '\0';
437 vfs_content_t content;
438 /* read file data from filesystem */
439 int ret = vfs_read (cmd_buf, strlen (cmd_buf), &content);
441 if (ret < 0) {
442 printf ("%s: command not found\n", cmd_buf);
443 DPRINT (DBG_CMDS, "ERROR -> vfs_read () - '%s'\n", cmd_buf);
444 tty_unlock (tty);
445 return 0;
448 unsigned bin_len = content.len;
450 unsigned char *bin = (unsigned char *) swmalloc (bin_len);
452 if (!bin) {
453 printf ("ERROR -> out of memory\n");
454 return 0;
457 //paging_disable ();
459 /* copy image to page aligned memory */
460 memcpy (bin, content.ptr, bin_len);
462 //paging_enable ();
464 cache_closebyptr (content.ptr);
466 unsigned entry, code, data, data_off, bss;
467 int err = exec_elf (bin, &entry, &data, &data_off, &bss);
469 if (err) {
470 printf ("ERROR -> invalid ELF exec\n");
472 /* free program image */
473 swfree (bin, bin_len);
475 tty_unlock (tty);
476 return 0;
479 /* create process */
480 proc_t *proc = proc_create (tty, cmd_buf, entry);
482 /* check for & character as app parameter */
483 unsigned d = 0;
484 while (d < arg_len) {
485 if (arg[d] == '&') {
486 if (proc_flag_set (proc, PROC_FLAG_DAEMON))
487 printf ("%s: started as daemon\n", cmd_buf);
489 arg[d] = '\0';
491 if (arg_len > 2 && arg[d-1] == ' ') {
492 arg[d-1] = '\0';
493 arg_len --;
496 arg_len --;
498 break;
500 d ++;
503 if (!proc) {
504 printf ("ERROR -> Invalid process: %s\n", cmd_buf);
506 /* free program image */
507 swfree (bin, bin_len);
509 tty_unlock (tty);
510 return 0;
513 /* save process information to structure for later use */
514 proc->start = (unsigned) bin;
515 proc->code = entry;
516 proc->data = data;
517 proc->data_off = data_off;
518 proc->bss = bss;
519 proc->end = (unsigned) (bin + bin_len);
521 /* well, set argument for out app */
522 proc_arg_set (proc, arg, arg_len);
524 proc_vmem_map (proc);
526 /* Is this process started as daemon ? */
527 /* wait here, until pid != 0*/
528 if (!(proc->flags & PROC_FLAG_DAEMON))
529 while (proc->pid)
530 schedule ();
532 /* enable interrupts */
533 int_enable ();
535 /* unlock tty console - user can write in */
536 tty_unlock (tty);
538 _curr_task = proc->tty->task; // this is pretty needed for correct return to our tty task
540 if (!(proc->flags & PROC_FLAG_DAEMON)) {
541 if (!proc_done (proc))
542 for (;;) // NOTE: heh, this is good stuff :P
543 schedule ();
545 /* free program image */
546 swfree (bin, bin_len);
549 /* switch to next task */
550 schedule ();
552 return 1;
555 unsigned command_lsdev (char *command, unsigned len)
557 dev_display ();
559 return 1;
562 unsigned command_login (char *command, unsigned len)
564 currtty->user = NULL;
565 currtty->logged = false;
567 video_color (7, 0);
568 printf ("login: ");
570 return 1;
573 unsigned command_serialw (char *command, unsigned len)
575 dev_t *dev = dev_find ("/dev/com0");
577 if (dev) {
578 unsigned len = strlen (argparse (command));
579 char data[len+1];
580 memcpy (data, argparse (command), len);
581 data[len] = '\0';
583 dev->handler (DEV_ACT_WRITE, data, len);
585 printf ("serialw: %s\n", data);
588 return 1;
591 unsigned command_serialr (char *command, unsigned len)
593 dev_t *dev = dev_find ("/dev/com0");
595 if (dev) {
596 char data[11];
598 dev->handler (DEV_ACT_READ, data, 1);
600 printf ("serialr: %s\n", data);
603 return 1;
606 unsigned command_fdisk (char *command, unsigned len)
608 char *parm = argparse (command);
610 fdisk (parm, strlen (parm));
612 return 1;
615 unsigned command_hdcat (char *command, unsigned len)
617 tty_lock (currtty);
619 dev_t *dev = (dev_t *) dev_find (argparse (command));
621 if (dev) {
622 partition_t p;
623 p.base_io = 0;
625 switch (dev->devname[7]) {
626 case 'a':
627 p.id = 0;
628 p.base_io = 0x1F0;
629 break;
630 case 'b':
631 p.id = 1;
632 p.base_io = 0x1F0;
633 break;
634 case 'c':
635 p.id = 0;
636 p.base_io = 0x170;
637 break;
638 case 'd':
639 p.id = 1;
640 p.base_io = 0x170;
641 break;
644 int c, d = 0;
645 unsigned char block[512];
647 printf ("dev: %s\n", dev->devname);
649 while (!key_pressed (1)) {
650 if (key_pressed (72) == 1) {
651 printf ("##block: %d\n", d);
652 dev->handler (DEV_ACT_READ, &p, block, "", d);
654 for (c = 0; c < 512; c ++)
655 printf ("%c", (unsigned) block[c]);
656 d ++;
659 usleep (5);
661 } else
662 printf ("Please specify block device, example: hdcat /dev/hda\n");
664 tty_unlock (currtty);
666 return 1;
669 unsigned command_free (char *command, unsigned len)
671 util_cmdfree ();
673 return 1;
676 unsigned command_top (char *command, unsigned len)
678 tty_lock (currtty);
680 proc_top ();
682 tty_unlock (currtty);
684 return 1;
687 unsigned command_date (char *command, unsigned len)
689 tm *t = rtc_getcurrtime ();
691 printf ("%02u:%02u:%02u, %u.%u.%u\n",
692 t->tm_hour, t->tm_min, t->tm_sec, t->tm_mday, t->tm_mon, t->tm_year);
694 return 1;
698 Tone Frequency (Hz)
699 C 130.8
700 D 146.8
701 E 164.8
702 F 174.6
703 G 196.0
704 A 220.0
705 H 246.9
707 C 261.6
708 D 293.7
709 E 329.6
710 F 349.2
711 G 392.0
712 A 440.0
713 H 493.9
715 C 523.3
716 D 587.3
717 E 659.3
718 F 698.5
719 G 784.0
720 A 880.0
721 H 987.8
723 C 1046.5
724 D 1174.7
725 E 1318.5
726 F 1396.9
727 G 1568.0
728 A 1760.0
729 H 1975.5
732 void play_tone (dev_t *dev, char tone, char type, char time)
734 unsigned t = 2;
736 switch (type) {
737 case '1':
738 t = 1;
739 break;
740 case '2':
741 t = 2;
742 break;
743 case '3':
744 t = 3;
745 break;
746 case '4':
747 t = 4;
748 break;
752 unsigned tt = 1;
754 printf ("play_tone (.., %c, %d, %d);\n", tone, t, tt);
756 switch (tone) {
757 case 'c':
758 dev->handler (DEV_ACT_PLAY, 131*t);
759 break;
760 case 'd':
761 dev->handler (DEV_ACT_PLAY, 147*t);
762 break;
763 case 'e':
764 dev->handler (DEV_ACT_PLAY, 165*t);
765 break;
766 case 'f':
767 dev->handler (DEV_ACT_PLAY, 175*t);
768 break;
769 case 'g':
770 dev->handler (DEV_ACT_PLAY, 196*t);
771 break;
772 case 'a':
773 dev->handler (DEV_ACT_PLAY, 220*t);
774 break;
775 case 'h':
776 dev->handler (DEV_ACT_PLAY, 247*t);
777 break;
778 case 'C':
779 dev->handler (DEV_ACT_PLAY, 136*t);
780 break;
781 case 'F':
782 dev->handler (DEV_ACT_PLAY, 188*t);
783 break;
786 switch (time) {
787 case '0':
788 timer_wait (2500);
789 break;
790 case '1':
791 timer_wait (1250);
792 break;
793 case '2':
794 timer_wait (625);
795 break;
800 printf ("end: play_tone ();\n");
802 dev->handler (DEV_ACT_PLAY, 0);
805 void play_song (dev_t *dev, char *data)
807 printf ("play_song ();\n");
808 unsigned i = 0, l = strlen (data);
809 unsigned time = 1;
810 while (i != l) {
811 switch (data[i]) {
812 printf ("switch ()\n");
813 case 'p':
815 switch (data[i+1]) {
816 case '0':
817 timer_wait (2500);
818 break;
819 case '1':
820 timer_wait (1250);
821 break;
822 case '2':
823 timer_wait (625);
824 break;
828 i ++;
829 break;
830 case ' ':
831 break;
832 default:
833 play_tone (dev, data[i], data[i+1], data[i+2]);
834 i += 2;
835 break;
838 i ++;
843 unsigned command_spk (char *command, unsigned len)
845 unsigned int freq = 20;
847 dev_t *dev = dev_find ("/dev/pcspk");
849 if (dev) {
851 We wish you a merry christmas v1.0
853 D2(1/8) G2(1/8) G2(1/16) A2(1/16) G2(1/16) Fis2(1/16) E2(1/8) C2(1/8) E2(1/8) A2(1/8) A2(1/16) H2(1/16) A2(1/16) G2(1/16) Fis2(1/8) D2(1/4) H2(1/8) H2(1/16) C3(1/16) H2(1/16) A2(1/16) G2(1/8) E2(1/8) D2(1/16) D2(1/16) E2(1/8) A2(1/8) Fis2(1/8) G2(1/8)
856 play_song (dev, "d21 g21 g22 a22 g22 F22 e21 c21 e21 a21 a22 h22 a22 g22 F21 d20 h21 h22 c32 h22 a22 g21 e21 d22 d22 e21 a21 F21 g21");
858 timer_wait (2000);
861 We wish you a merry christmas v3.0
863 G2(1/8) C3(1/8) C3(1/16) D3(1/16) C3(1/16) H2(1/16) A2(1/4) D3(1/8) D3(1/16) E3(1/16) D3(1/16) C3(1/16) H2(1/4) G2(1/8) E3(1/8) E3(1/16) F3(1/16) E3(1/16) D3(1/16) C3(1/8) A2(1/8) G2(1/8) A2(1/8) D3(1/8) H2(1/8) C3(1/8)
866 play_song (dev, "g21 c31 c32 d32 c32 h22 a20 d31 d32 e32 d32 c32 h20 g21 e31 e32 f32 e32 d32 c31 a21 g21 a21 d31 h21 c31");
868 timer_wait (2000);
871 Dallas v2.0
872 G2(1/8) C3(1/8) P(1/16) G2(1/16) G3(1/8) C3(1/16) E3(1/8) D3(1/16) E3(1/16) C3(1/8) G2(1/8) C3(1/8) A3(1/8) G3(1/8) E3(1/16) F3(1/16) G3(1/8) G3(1/16) P(1/16) G2(1/8) C3(1/8) A3(1/8) G3(1/8) E3(1/16) F3(1/16) G3(1/8) D3(1/16) E3(1/16) C3(1/8) G2(1/8) C3(1/8) E3(1/16) F3(1/16) D3(1/8) G3(1/8) G3(1/8)
874 /* play_song (dev, "g21 c31 p2 g22 g31 c32 e31 d32 e32 c31 g21 c31 a31 g31 e32 f32 g31 g32 p2 g21 c31 a31 g31 e32 f32 g31 d32 e32 c31 g21 c31 e32 f32 d31 g31 g31");*/
876 //timer_wait (2000);
879 return 1;
882 extern bool bus_pci_acthandler (unsigned act, char *block, unsigned block_len);
883 extern netif_t netif_list;
884 extern void ext2_dump ();
885 void thread_test ()
887 while (1) {
889 kprintf ("hu\n");
891 unsigned long timer_start = timer_ticks + ((unsigned) 1 * 1000);
893 while (timer_start > timer_ticks)
894 schedule ();
897 unsigned command_netcp (char *command, unsigned len);
898 unsigned command_test (char *command, unsigned len)
900 /* partition_t *p = partition_find ("/dev/hda0");
902 if (p)
903 mount (p, "", "/mnt/hdd/");
904 else
905 return 0;
907 cd ("/mnt/hdd");*/
909 //command_znfscl ("znfcl 192.168.0.2", strlen ("znfcl 192.168.0.2"));
911 char *file = "netcp 192.168.0.2 iso/file /bin/file";
912 char *border = "netcp 192.168.0.2 iso/border /bin/border";
913 char *folder = "netcp 192.168.0.2 iso/folder /bin/folder";
914 char *zde = "netcp 192.168.0.2 iso/zde /bin/zde";
916 command_netcp (file, strlen (file));
917 command_netcp (border, strlen (border));
918 command_netcp (folder, strlen (folder));
919 command_netcp (zde, strlen (zde));
921 cd ("/bin");
923 command_exec ("exec zde", 8);
925 return 1;
928 unsigned command_test2 (char *command, unsigned len)
930 partition_t *p = partition_find ("/dev/cdc0");
932 if (p)
933 mount (p, "", "/mnt/cdrom/");
934 else
935 return 0;
937 cd ("/mnt/cdrom");
939 /* tty_t *tty = tty_find ("tty0");
941 if (!tty)
942 return 0;
943 char str[513];
944 int r = tty_read (tty, str, 512);
946 if (r > 0) {
947 printf ("str: r: %d\n", r);
948 int i;
949 for (i = 0; i < r; i ++)
950 tty_putch (str[i]);
954 /* int ret = vfs_read ("wav", 3);
956 if (ret < 0) {
957 printf ("chyba\n");
958 return 0;
961 cache_t *cache = cache_read ();
963 unsigned char *p = (unsigned char *) &cache->data;
965 unsigned bin_len = cache->limit;
967 unsigned char *bin = (unsigned char *) 0x400000 - 44;
969 memcpy (bin, p, bin_len);
971 typedef struct {
972 unsigned chunkid;
973 unsigned chunksize;
974 unsigned format;
975 } riff_head_t;
977 typedef struct {
978 unsigned subchunk1id;
979 unsigned subchunk1size;
980 unsigned short audioformat;
981 unsigned short numchannels;
982 unsigned samplerate;
983 unsigned byterate;
984 unsigned short blockalign;
985 unsigned short bitspersample;
986 } fmt_head_t;
988 typedef struct {
989 unsigned subchunk2id;
990 unsigned subchunk2size;
991 } data_head_t;
993 riff_head_t *r = (riff_head_t *) bin;
994 fmt_head_t *fmt = (fmt_head_t *) ((char *) bin + sizeof (riff_head_t));
995 data_head_t *data = (data_head_t *) ((char *) bin + sizeof (riff_head_t) + sizeof (fmt_head_t));
997 printf ("riff_head_t 0x%x : %d : 0x%x\n", r->chunkid, r->chunksize, r->format);
998 printf ("fmt_head_t %d : %d : %d : %d : %d : %d\n", fmt->audioformat, fmt->subchunk1size, fmt->numchannels, fmt->samplerate, fmt->byterate, fmt->bitspersample);
999 printf ("data_head_t 0x%x : %d\n", data->subchunk2id, data->subchunk2size);
1000 //memset (bin+44, 0, bin_len-44);
1001 //dev->handler (DEV_ACT_WRITE, bin+44, bin_len-44);
1003 snd_cfg_t cfg;
1004 cfg.dev = "/dev/ac97";
1005 cfg.rate = fmt->samplerate;
1006 cfg.format = SOUND_FORMAT_S16;
1007 cfg.channels = fmt->numchannels;
1009 snd_audio_t *aud = audio_open (&cfg);
1011 if (!aud)
1012 return 0;
1014 audio_write (aud, bin+44, (bin_len-44));
1016 //swfree (bin, bin_len);*/
1020 return 1;
1023 unsigned command_test3 (char *command, unsigned len)
1025 netif_t *netif = netif_findbyname ("eth0");
1026 dhcp_config_if(netif);
1028 return 1;
1031 unsigned command_test4 (char *command, unsigned len)
1035 return 1;
1038 unsigned command_mouse (char *command, unsigned len)
1040 printf ("PS/2 Mouse test\n");
1042 dev_t *dev = dev_find ("/dev/mousecom");
1044 if (!dev)
1045 return 0;
1047 dev_mouse_t mouse;
1049 int ps2_fd = open ("/dev/mousecom", O_RDONLY);
1051 if (ps2_fd < 1)
1052 return 0;
1054 while (!key_pressed (1)) {
1055 int r = read (ps2_fd, &mouse, sizeof (dev_mouse_t));
1057 if (r < 1) {
1058 kprintf ("oj oj\n");
1059 return 0;
1062 kprintf ("ps2mouse: x,y: %d, %d | 0x%x \n", mouse.pos_x, mouse.pos_y, mouse.flags);
1064 lseek (ps2_fd, 0, SEEK_SET);
1066 schedule ();
1069 return 1;
1072 unsigned command_vesa (char *command, unsigned len)
1074 if (!init_video_vesa ())
1075 printf ("vesa -> failed\n");
1077 return 1;
1080 unsigned command_kill (char *command, unsigned len)
1082 strcpy (cmd_buf, argparse (currtty->shell));
1084 pid_t pid = (pid_t) atoi (cmd_buf);
1086 proc_t *proc = proc_findbypid (pid);
1088 if (!proc) {
1089 printf ("kill: (%s) - No such process\n", cmd_buf);
1090 return 0;
1093 /* send SIGTERM to selected process */
1094 if (!proc_signal (proc, SIGTERM))
1095 return 0;
1097 /* check for daemon */
1098 if (proc->flags & PROC_FLAG_DAEMON)
1099 if (!proc_done (proc))
1100 return 0;
1102 return 1;
1105 unsigned command_modprobe (char *command, unsigned len)
1107 unsigned arg_len = strlen (argparse (currtty->shell));
1108 char arg[arg_len+1];
1109 strcpy (arg, argparse (currtty->shell));
1110 arg[arg_len] = '\0';
1112 module_load (arg);
1114 return 1;
1117 unsigned command_lsmod (char *command, unsigned len)
1119 module_display ();
1121 return 1;
1124 #ifdef CONFIG_DRV_PCI
1125 unsigned command_lspci (char *command, unsigned len)
1127 #ifdef ARCH_i386
1128 pcidev_display ();
1129 #endif
1130 return 1;
1132 #endif
1134 unsigned command_iflist (char *command, unsigned len)
1136 iflist_display ();
1138 return 1;
1141 unsigned command_ifconfig (char *command, unsigned len)
1143 strcpy (cmd_buf, argparse (command));
1144 char ifid[20];
1145 char ip[32];
1147 unsigned l = strlen (cmd_buf);
1148 unsigned x = 0;
1149 while (x < l) {
1150 if (cmd_buf[x] == ' ')
1151 break;
1152 x ++;
1155 memcpy (ifid, cmd_buf, x);
1156 ifid[x] = '\0';
1158 strcpy (ip, argparse (cmd_buf));
1159 unsigned y = strlen (ip);
1161 netif_t *netif = netif_findbyname (ifid);
1163 if (!netif) {
1164 printf ("ifconfig -> bad network interface name, example: eth0\n");
1165 return 0;
1168 net_ipv4 ipv4;
1169 net_ipv6 ipv6;
1170 if (ipv4 = net_proto_ip_convert (ip)) {
1171 netif_ip_addr (netif, ipv4, IF_CFG_TYPE_STATIC);
1173 printf ("%s: IPv4 ", ifid);
1174 net_proto_ip_print (ipv4);
1175 printf (" - OK\n");
1176 } else if (net_proto_ipv6_convert (ipv6, ip)) {
1177 netif_ipv6_addr (netif, ipv6, IF_CFG_TYPE_STATIC);
1179 printf ("%s: IPv6 ", ifid);
1180 net_proto_ipv6_print (ipv6);
1181 printf (" - OK\n");
1182 } else {
1183 printf ("ifconfig -> bad ip address format, example:\n\t\t192.168.1.1 for IPv4\n\t\tfc00:0:0:0:0:0:0:10 for IPv6\n");
1184 return 0;
1187 return 1;
1190 unsigned command_ifroute (char *command, unsigned len)
1192 strcpy (cmd_buf, argparse (command));
1193 char ifid[20];
1194 char ip[32];
1196 unsigned l = strlen (cmd_buf);
1197 unsigned x = 0;
1198 while (x < l) {
1199 if (cmd_buf[x] == ' ')
1200 break;
1201 x ++;
1204 memcpy (ifid, cmd_buf, x);
1205 ifid[x] = '\0';
1207 strcpy (ip, argparse (cmd_buf));
1208 unsigned y = strlen (ip);
1210 netif_t *netif = netif_findbyname (ifid);
1212 if (!netif) {
1213 printf ("ifroute -> bad network interface name, example: eth0\n");
1214 return 0;
1217 unsigned char a;
1218 unsigned char b;
1219 unsigned char c;
1220 unsigned char d;
1222 unsigned g = 0;
1223 unsigned i = 0;
1225 unsigned h[4];
1227 while (i < y) {
1228 if (ip[i] == '.') {
1229 ip[i] = '\0';
1230 h[g] = i+1;
1231 g ++;
1234 i ++;
1237 if (g != 3) {
1238 printf ("ifroute -> bad ip address format, example: 192.168.1.254\n");
1239 return 0;
1242 a = atoi (ip);
1243 b = atoi (ip+h[0]);
1244 c = atoi (ip+h[1]);
1245 d = atoi (ip+h[2]);
1247 printf ("%s->gw: %d.%d.%d.%d - OK\n", ifid, a, b, c, d);
1249 return netif_gw_addr (netif, NET_IPV4_TO_ADDR (a, b, c, d));
1252 unsigned command_dnsconfig (char *command, unsigned len)
1254 strcpy (cmd_buf, argparse (currtty->shell));
1256 if (!cmd_buf)
1257 return 0;
1259 unsigned l = strlen (cmd_buf);
1261 if (!l) {
1262 printf ("dnsconfig -> your domain name server is: ");
1263 net_proto_ip_print (dns_addr_get ());
1264 printf ("\n");
1266 return 0;
1269 printf ("dnsconfig: %s - OK\n", cmd_buf);
1271 net_ipv4 dns = net_proto_ip_convert (cmd_buf);
1273 if (!dns)
1274 return 0;
1276 dns_addr (dns);
1278 return 1;
1281 unsigned command_dhcpcl (char *command, unsigned len)
1283 strcpy (cmd_buf, argparse (currtty->shell));
1285 if (!cmd_buf)
1286 return 0;
1288 unsigned l = strlen (cmd_buf);
1290 if (!l) {
1291 printf ("dhcpcl -> please specify interface\n");
1292 return 0;
1295 netif_t *netif = netif_findbyname (cmd_buf);
1297 if (!netif) {
1298 printf ("dhcpcl -> bad network interface name, example: eth0\n");
1299 return 0;
1302 unsigned ret = dhcp_config_if (netif);
1304 if (ret < 0) {
1305 switch (ret) {
1306 case DHCP_OUT_OF_MEMORY:
1307 printf ("dhcpcl -> out of memory\n");
1308 break;
1309 case DHCP_SOCKET_FAILED:
1310 case DHCP_CONNECT_FAILED:
1311 case DHCP_CANT_SEND_REQUEST:
1312 printf ("dhcpcl -> problem with network layer\n");
1313 break;
1314 case DHCP_CANT_RECV_RESPONSE:
1315 printf ("dhcpcl -> can't receive response\n");
1316 break;
1317 case DHCP_BAD_PACKET:
1318 printf ("dhcpcl -> bad packet received\n");
1319 break;
1320 case DHCP_SRV_DIDNT_UNDERSTAND:
1321 printf ("dhcpcl -> server didn't understand\n");
1322 break;
1324 } else
1325 printf ("dhcpcl -> interface %s successfully configured\n", cmd_buf);
1327 return 0;
1330 unsigned command_tunconfig (char *command, unsigned len)
1332 strcpy (cmd_buf, argparse (currtty->shell));
1334 if (!cmd_buf)
1335 return 0;
1337 unsigned l = strlen (cmd_buf);
1339 if (!l) {
1340 printf ("tunconfig -> wrong syntax !\ntunconfig <ipv4tunnel> for enable or tunconfig 0 for disable\n");
1342 return 0;
1345 /* you can disable this service over character '0' as parameter */
1346 if (l == 1 && cmd_buf[0] == '0') {
1347 tun6_addr (0);
1349 printf ("tunconfig: disabled - OK\n");
1351 return 1;
1354 /* classical ipv4 address from tunnel server */
1355 net_ipv4 tunnel = net_proto_ip_convert (cmd_buf);
1357 if (!tunnel) {
1358 printf ("tunconfig -> wrong syntax !\nExample: tunconfig 216.66.80.30\n");
1360 return 0;
1363 tun6_addr (tunnel);
1365 printf ("tunconfig: ::%s - OK\n", cmd_buf);
1367 return 1;
1371 unsigned command_hostname (char *command, unsigned len)
1373 strcpy (cmd_buf, argparse (currtty->shell));
1375 if (!cmd_buf)
1376 return 0;
1378 unsigned l = strlen (cmd_buf);
1380 if (!l) {
1381 printf ("%s\n", hostname_get ());
1383 return 0;
1386 printf ("hostname: %s - OK\n", cmd_buf);
1388 hostname_set (cmd_buf);
1390 return 1;
1393 unsigned command_kbdmap (char *command, unsigned len)
1395 strcpy (cmd_buf, argparse (currtty->shell));
1397 if (!cmd_buf)
1398 return 0;
1400 unsigned ret = keyboard_setlayout (cmd_buf);
1402 if (ret)
1403 printf ("Keyboard layout was changed to '%s'\n", cmd_buf);
1405 return ret;
1408 unsigned command_mkzexfs (char *command, unsigned len)
1410 #ifdef ARCH_i386
1411 strcpy (cmd_buf, argparse (currtty->shell));
1413 partition_t *p = partition_find (cmd_buf);
1415 if (!p)
1416 return 0;
1418 mkzexfs (p);
1419 #endif
1420 return 1;
1423 unsigned command_mkext2 (char *command, unsigned len)
1425 #ifdef ARCH_i386
1426 strcpy (cmd_buf, argparse (currtty->shell));
1428 if (!cmd_buf)
1429 return 0;
1431 partition_t *p = partition_find ("/dev/hda0");
1433 mkext2 (p);
1434 #endif
1435 return 1;
1438 unsigned command_ping (char *command, unsigned len)
1441 strcpy (cmd_buf, argparse (currtty->shell));
1443 if (!cmd_buf)
1444 return 0;
1446 netif_t *netif = netif_findbyname ("eth0");
1448 if (!netif) {
1449 printf ("ping -> network interface does not exists\n");
1450 return 0;
1453 unsigned char a;
1454 unsigned char b;
1455 unsigned char c;
1456 unsigned char d;
1458 unsigned g = 0;
1459 unsigned i = 0;
1460 unsigned y = strlen (cmd_buf);
1462 if (!y) {
1463 printf ("ping -> wrong syntax, please specify address\n");
1464 return 0;
1467 unsigned h[4];
1469 while (i < y) {
1470 if (cmd_buf[i] == '.') {
1471 cmd_buf[i] = '\0';
1472 h[g] = i+1;
1473 g ++;
1476 i ++;
1479 net_ipv4 target = 0;
1481 if (g != 3) {
1482 strcpy (cmd_buf, argparse (currtty->shell));
1484 if (dns_cache_get (cmd_buf, &target, 4) != 1)
1485 if (dns_send_request (cmd_buf, &target, 4) != 1) {
1486 printf ("ping -> bad hostname or ip address format, example: 192.168.1.1\n");
1487 return 0;
1489 } else {
1490 a = atoi (cmd_buf);
1491 b = atoi (cmd_buf+h[0]);
1492 c = atoi (cmd_buf+h[1]);
1493 d = atoi (cmd_buf+h[2]);
1495 target = NET_IPV4_TO_ADDR (a, b, c, d);
1497 strcpy (cmd_buf, argparse (currtty->shell));
1500 char address[20];
1501 net_proto_ip_convert2 (target, address);
1503 i = 0;
1505 while (i < 8) {
1506 unsigned long time_a = timer_ticks;
1507 unsigned ret = net_proto_icmp_ping (netif, target);
1508 if (ret) {
1509 printf ("ping -> %s (%s) - %ums\n", cmd_buf, address, (timer_ticks - time_a));
1510 } else {
1511 printf ("ping -> Host %s is unreachable\n", cmd_buf);
1512 break;
1515 i ++;
1518 return 1;
1521 unsigned command_ping6 (char *command, unsigned len)
1523 strcpy (cmd_buf, argparse (currtty->shell));
1525 if (!cmd_buf)
1526 return 0;
1528 netif_t *netif = netif_findbyname ("eth0");
1530 if (!netif) {
1531 printf ("ping6 -> network interface does not exists\n");
1532 return 0;
1535 unsigned short a;
1536 unsigned short b;
1537 unsigned short c;
1538 unsigned short d;
1539 unsigned short e;
1540 unsigned short f;
1541 unsigned short g;
1542 unsigned short h;
1544 unsigned j = 0;
1545 unsigned i = 0;
1546 unsigned y = strlen (cmd_buf);
1548 if (!y) {
1549 printf ("ping6 -> wrong syntax, please specify ipv6 address\n");
1550 return 0;
1553 unsigned k[8];
1555 while (i < y) {
1556 if (cmd_buf[i] == ':') {
1557 cmd_buf[i] = '\0';
1558 k[j] = i+1;
1559 j ++;
1562 i ++;
1565 net_ipv6 target;
1567 if (j != 7) {
1568 strcpy (cmd_buf, argparse (currtty->shell));
1570 if (dns_cache_get (cmd_buf, &target, 16) != 1)
1571 if (dns_send_request (cmd_buf, &target, 16) != 1) {
1572 printf ("ping6 -> bad hostname or ip address format, example: 2001:4d3:8fe:2:0:0:0:1\n");
1573 return 0;
1575 } else {
1576 char *endptr;
1578 a = strtol (cmd_buf, &endptr, 16);
1579 b = strtol (cmd_buf+k[0], &endptr, 16);
1580 c = strtol (cmd_buf+k[1], &endptr, 16);
1581 d = strtol (cmd_buf+k[2], &endptr, 16);
1582 e = strtol (cmd_buf+k[3], &endptr, 16);
1583 f = strtol (cmd_buf+k[4], &endptr, 16);
1584 g = strtol (cmd_buf+k[5], &endptr, 16);
1585 h = strtol (cmd_buf+k[6], &endptr, 16);
1587 NET_IPV6_TO_ADDR (target, a, b, c, d, e, f, g, h);
1589 //printf ("ipv6: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", a, b, c, d, e, f, g, h);
1591 strcpy (cmd_buf, argparse (currtty->shell));
1594 char address[40];
1595 //net_proto_ipv6_convert2 (target, address);
1597 i = 0;
1599 while (i < 8) {
1600 unsigned long time_a = timer_ticks;
1601 unsigned ret = net_proto_icmp6_ping (netif, target);
1602 if (ret) {
1603 printf ("ping6 -> %s (%s) - %ums\n", cmd_buf, cmd_buf, (timer_ticks - time_a));
1604 } else {
1605 printf ("ping6 -> Host %s is unreachable\n", cmd_buf);
1606 break;
1609 i ++;
1612 return 1;
1615 unsigned command_netexec (char *command, unsigned len)
1617 strcpy (cmd_buf, argparse (command));
1619 if (!cmd_buf)
1620 return 0;
1622 if (!strlen (cmd_buf)) {
1623 printf ("Syntax:\nnetexec <ip_of_tftp> <filename> [parameters]\n");
1624 return 0;
1627 unsigned arg_len = strlen (argparse (cmd_buf));
1629 if (arg_len >= 128)
1630 return 0;
1632 char arg[128];
1633 strcpy (arg, argparse (cmd_buf));
1634 arg[arg_len] = '\0';
1636 unsigned cmd_buf_len = strlen (cmd_buf);
1637 cmd_buf [cmd_buf_len-arg_len] = '\0';
1639 if (cmd_buf[cmd_buf_len-arg_len-1] == ' ') {
1640 cmd_buf[cmd_buf_len-arg_len-1] = '\0';
1641 cmd_buf_len --;
1644 char file[128];
1645 unsigned file_len = 0;
1646 memcpy (file, arg, arg_len);
1647 while (file_len < arg_len) {
1648 if (file[file_len] == ' ') {
1649 file[file_len] = '\0';
1650 break;
1652 file_len ++;
1655 if (file[arg_len] != '\0')
1656 file[arg_len] = '\0';
1658 unsigned l = file_len;
1660 unsigned ret = tftp_client_connect (cmd_buf, 69, file, &l);
1662 if (!ret) {
1663 printf ("tftp -> something go wrong !\n");
1664 return 0;
1667 printf ("tftp -> file length: %ubytes\t\t[OK]\n", l);
1669 char *bin = (unsigned char *) swmalloc (sizeof (char) * (l+1));
1671 if (!bin) {
1672 printf ("ERROR -> out of memory\n");
1673 return 0;
1676 int ret2 = tftp_client_read (ret, bin, l);
1678 if (!ret2) {
1679 printf ("tftp -> tftp_client_read () error\n");
1680 swfree (bin, l);
1681 return 0;
1684 unsigned entry, code, data, data_off, bss;
1685 int err = exec_elf (bin, &entry, &data, &data_off, &bss);
1687 if (err != NULL) {
1688 printf ("ERROR -> invalid ELF exec\n");
1689 return 0;
1692 tty_lock (currtty);
1694 /* create process */
1695 proc_t *proc = proc_create (currtty, "netexec", entry);
1697 /* check for & character as app parameter */
1698 /* unsigned d = 0;
1699 while (d < arg_len) {
1700 if (arg[d] == '&') {
1701 if (proc_flag_set (proc, PROC_FLAG_DAEMON))
1702 printf ("%s: started as daemon\n", cmd_buf);
1704 arg[d] = '\0';
1706 if (arg_len > 2 && arg[d-1] == ' ') {
1707 arg[d-1] = '\0';
1708 arg_len --;
1711 arg_len --;
1713 break;
1715 d ++;
1718 if (!proc) {
1719 printf ("ERROR -> Invalid process: %s\n", cmd_buf);
1721 /* free program image */
1722 swfree (bin, l);
1724 tty_unlock (currtty);
1725 return 0;
1728 /* save process information to structure for later use */
1729 proc->start = (unsigned) bin;
1730 proc->code = entry;
1731 proc->data = data;
1732 proc->data_off = data_off;
1733 proc->bss = bss;
1734 proc->end = (unsigned) (bin + l);
1736 /* well, set argument for out app */
1737 proc_arg_set (proc, arg+file_len+1, arg_len-file_len-1);
1739 proc_vmem_map (proc);
1741 /* Is this process started as daemon ? */
1742 /* wait here, until pid != 0*/
1743 if (!(proc->flags & PROC_FLAG_DAEMON))
1744 while (proc->pid)
1745 schedule ();
1747 /* enable interrupts */
1748 int_enable ();
1750 /* unlock tty console - user can write in */
1751 tty_unlock (proc->tty);
1753 _curr_task = proc->tty->task; // this is pretty needed for correct return to our tty task
1755 if (!(proc->flags & PROC_FLAG_DAEMON)) {
1756 if (!proc_done (proc))
1757 for (;;) // NOTE: heh, this is good stuff :P
1758 schedule ();
1760 /* free image of app */
1761 swfree (bin, l);
1764 /* switch to next task */
1765 schedule ();
1767 sclose (ret);
1769 return 1;
1772 unsigned command_netcp (char *command, unsigned len)
1774 strcpy (cmd_buf, argparse (command));
1776 if (!cmd_buf)
1777 return 0;
1779 if (!strlen (cmd_buf)) {
1780 printf ("Syntax:\nnetcp <ip_of_tftp> <tftp_file> <output_file>\n");
1781 return 0;
1784 unsigned arg_len = strlen (argparse (cmd_buf));
1786 if (arg_len >= 128)
1787 return 0;
1789 char arg[128];
1790 strcpy (arg, argparse (cmd_buf));
1791 arg[arg_len] = '\0';
1793 unsigned cmd_buf_len = strlen (cmd_buf);
1794 cmd_buf [cmd_buf_len-arg_len] = '\0';
1796 if (cmd_buf[cmd_buf_len-arg_len-1] == ' ') {
1797 cmd_buf[cmd_buf_len-arg_len-1] = '\0';
1798 cmd_buf_len --;
1801 char file[128];
1802 unsigned file_len = 0;
1803 memcpy (file, arg, arg_len);
1804 while (file_len < arg_len) {
1805 if (file[file_len] == ' ') {
1806 file[file_len] = '\0';
1807 break;
1809 file_len ++;
1812 if (file[arg_len] != '\0')
1813 file[arg_len] = '\0';
1815 unsigned l = file_len;
1817 unsigned ret = tftp_client_connect (cmd_buf, 69, file, &l);
1819 if (!ret) {
1820 printf ("tftp -> something go wrong !\n");
1821 return 0;
1824 printf ("tftp -> file length: %ubytes\t\t[OK]\n", l);
1826 char *bin = (unsigned char *) swmalloc (sizeof (char) * (l+1));
1828 if (!bin)
1829 return 0;
1831 int ret2 = tftp_client_read (ret, bin, l);
1833 if (!ret2) {
1834 printf ("tftp -> tftp_client_read () error\n");
1835 swfree (bin, l);
1836 return 0;
1839 sclose (ret);
1841 char *output = argparse (arg);
1843 if (!output) {
1844 swfree (bin, l);
1845 printf ("ERROR -> output file name is empty\n");
1846 return 0;
1849 int fd = open (output, O_WRONLY | O_CREAT);
1851 if (!fd) {
1852 swfree (bin, l);
1853 printf ("ERROR -> something go wrong with %s\n", output);
1854 return 0;
1857 ret = write (fd, bin, l);
1859 if (!ret) {
1860 swfree (bin, l);
1861 printf ("ERROR -> File '%s' could'nt be writed\n", output);
1864 return 1;
1867 extern bool ide_acthandler (unsigned act, partition_t *p, char *block, char *more, int n);
1868 unsigned command_znfscl (char *command, unsigned len)
1870 #ifdef ARCH_i386
1871 strcpy (cmd_buf, argparse (command));
1873 if (!strlen (cmd_buf)) {
1874 printf ("Syntax:\nznfscl <ip_of_znfs>\n");
1875 return 0;
1878 dev_t *dev = dev_find ("/dev/vbd");
1880 if (dev) {
1881 printf ("znfscl: You are already connected to znfs server\n");
1882 return 0;
1885 dev = (dev_t *) dev_register ("vbd", "Virtual Block Device", DEV_ATTR_BLOCK, (dev_handler_t *) &ide_acthandler);
1887 if (!dev)
1888 return 0;
1890 partition_t *p = partition_add (dev, fs_supported ("znfs"), 0);
1892 if (!p)
1893 return 0;
1895 /* connect to specified address */
1896 if (znfs_init (p, cmd_buf) != 1) {
1897 printf ("znfs: Wrong znfs server\n");
1898 return 0;
1901 char str[16];
1903 sprintf (str, "/mnt/%s", p->name+5);
1905 mkdir (str);
1907 //mount (p, "", str);
1909 command_mount ("mount /dev/vbd0 /mnt/vbd0", 25);
1911 printf ("znfs: You are succefully connected to ZNFS server '%s'\nZNFS was mounted into '%s'\n", cmd_buf, str);
1912 #else
1913 printf ("ERROR -> this architecture is not supported yet\n");
1914 #endif
1915 return 1;
1918 void task_savemode ()
1920 /* disable cpu for cycle */
1921 for (;; schedule ())
1922 arch_cpu_hlt ();
1925 unsigned command_savemode (char *command, unsigned len)
1927 task_t *task = (task_t *) task_create ("savemode", (unsigned) &task_savemode, 16);
1929 if (!task) {
1930 printf ("ERROR -> Save mode thread was'nt created\n");
1931 return 0;
1934 printf ("Save mode was succefully started\n");
1936 return 1;
1939 unsigned command_cpuinfo (char *command, unsigned len)
1941 printf ("Processor info:\n");
1943 cat ("/proc/cpuinfo");
1945 return 1;
1948 unsigned command_adm (char *command, unsigned len)
1950 printf ("Automatic Device Mounter - init ()\n");
1952 init_adm ();
1954 return 1;
1957 unsigned command_ttyctrl (char *command, unsigned len)
1959 char *parm = argparse (command);
1961 if (!strlen (parm)) {
1962 printf ("ttyctrl <operation> [parameter]\n\toperations: add; list; change <ttyid>\n");
1963 return 0;
1966 if (!strncmp (parm, "add", 3)) {
1967 tty_t *tty = tty_create ();
1969 if (!tty)
1970 return 0;
1972 video_color (7, 0);
1974 tty_write (tty, "\nlogin: ", 8);
1976 printf ("New tty '%s' was created\n", tty->name);
1977 } else
1978 if (!strncmp (parm, "list", 4)) {
1979 tty_listview ();
1980 } else
1981 if (!strncmp (parm, "change", 6)) {
1982 tty_t *tty = tty_find ((char *) argparse (parm));
1984 if (!tty)
1985 return 0;
1987 tty_change (tty);
1989 printf ("\n");
1992 return 1;
1995 unsigned command_cmd (char *command, unsigned len)
1997 unsigned s = 0;
1998 unsigned e = 0;
2000 if (!strlen (argparse (command))) {
2001 printf ("cmd <command1>;<command2>;...;\n");
2002 return 0;
2005 char *cmds = kmalloc (len - 3);
2007 if (!cmds)
2008 return 0;
2010 memcpy (cmds, command + 4, len - 4);
2011 cmds[len - 4] = '\0';
2013 unsigned i;
2014 for (i = 1; i < len - 4; i ++) {
2015 if (cmds[i] == ';') {
2016 cmds[i] = '\0';
2017 e = i-1;
2019 command_parser (cmds+s, e-s+1);
2021 s = i+1;
2025 kfree (cmds);
2027 return 1;
2030 /*************************************************************\
2031 | COMMAND's UTILS |
2032 \*************************************************************/
2034 unsigned command_parser (char *command, unsigned len)
2036 /* find first non-space in command */
2037 int i = 0;
2038 for (i = 0; i < len; i ++) {
2039 if (command[i] != ' ')
2040 break;
2042 command_t *ctx;
2044 /* try to find the command */
2045 for (ctx = command_list.next; ctx != &command_list; ctx = ctx->next) {
2046 if (!cstrcmp (ctx->name, command+i)) {
2047 ctx->handler (command+i, len-i); /* execute context function */
2048 return 1;
2052 printf ("%s: command not found\n", command);
2054 return 0;
2057 void commands (int i)
2059 // currtty->shell_len has been set to zero already
2060 command_parser (currtty->shell, strlen(currtty->shell));
2063 unsigned command_register (char *name, char *desc, command_handler_t *handler, unsigned flags)
2065 command_t *c;
2066 /* we dont want commands with same name */
2067 for (c = command_list.next; c != &command_list; c = c->next)
2068 if (!strcmp(c->name, name))
2069 return 0;
2071 command_t *ctx;
2073 // alloc and init context
2074 ctx = (command_t *) kmalloc (sizeof (command_t));
2076 if (!ctx)
2077 return 0;
2079 unsigned name_len = strlen (name);
2080 ctx->name = (char *) kmalloc (sizeof (char) * name_len +1);
2082 if (!ctx->name)
2083 return 0;
2085 memcpy (ctx->name, name, name_len);
2086 ctx->name[name_len] = '\0';
2088 unsigned desc_len = strlen (desc);
2089 ctx->desc = (char *) kmalloc (sizeof (char) * desc_len +1);
2091 if (!ctx->desc)
2092 return 0;
2094 memcpy (ctx->desc, desc, desc_len);
2095 ctx->desc[desc_len] = '\0';
2097 ctx->handler = handler;
2098 ctx->flags = flags;
2100 ctx->next = &command_list;
2101 ctx->prev = command_list.prev;
2102 ctx->prev->next = ctx;
2103 ctx->next->prev = ctx;
2105 return 1;
2108 unsigned command_unregister (char *name)
2110 command_t *ctx;
2112 for (ctx = command_list.next; ctx != &command_list; ctx = ctx->next) {
2113 if (!strcmp(ctx->name, name)) {
2114 ctx->next->prev = ctx->prev;
2115 ctx->prev->next = ctx->next;
2117 return 1;
2121 return 0;
2124 /*************************************************************\
2125 | INITIALIZATION OF COMMANDS |
2126 \*************************************************************/
2128 unsigned int init_commands ()
2130 command_list.next = &command_list;
2131 command_list.prev = &command_list;
2133 // reg commands
2134 command_register ("help", "Displays list of available commands", &command_help, 0);
2135 command_register ("hdd", "Detect HDD", &command_hdd, 0);
2136 command_register ("reboot", "Reboot computer", &command_reboot, 0);
2137 command_register ("halt", "Shutdown computer", &command_halt, 0);
2138 command_register ("tasks", "Print all tasks", &command_tasks, 0);
2139 command_register ("ps", "Print all process", &command_ps, 0);
2140 command_register ("uptime", "Print uptime in seconds", &command_uptime, 0);
2141 command_register ("version", "Displays a version of system", &command_version, 0);
2142 command_register ("debug", "Change to developer mode", &command_debug, 0);
2143 command_register ("mount", "Mount device to selected directory", &command_mount, 0);
2144 command_register ("umount", "Unmount mounted directory", &command_umount, 0);
2145 command_register ("env", "Displays all env variables", &command_env, 0);
2146 command_register ("cd", "Change directory", &command_cd, 0);
2147 command_register ("ls", " Displays files in current directory", &command_ls, 0);
2148 command_register ("cat", "Displays text in selected file", &command_cat, 0);
2149 command_register ("cp", "Copy a file", &command_cp, 0);
2150 command_register ("rm", "Remove a file", &command_rm, 0);
2151 command_register ("mkdir", "Create a directory", &command_mkdir, 0);
2152 command_register ("touch", "Create a file", &command_touch, 0);
2153 command_register ("exec", "Execute a selected program", &command_exec, 0);
2154 command_register ("lsdev", "Displays found devices", &command_lsdev, 0);
2155 command_register ("login", "Login as another user", &command_login, 0);
2156 command_register ("fdisk", "Partition table manipulator", &command_fdisk, 0);
2157 command_register ("hdcat", "Read selected block of data from drive", &command_hdcat, 0);
2158 command_register ("free", "Display amount of free and used memory", &command_free, 0);
2159 command_register ("top", "Stats of cpu usage", &command_top, 0);
2160 command_register ("date", "Show current date and time", &command_date, 0);
2161 //command_register ("xmastime", "play noels on pc-speaker", &command_spk, 0);
2162 command_register ("test", "Some test", &command_test, 0);
2163 //command_register ("test2", "Some test", &command_test2, 0);
2164 //command_register ("test3", "Some test", &command_test3, 0);
2165 //command_register ("testd", "Some test", &command_test4, 0);
2166 //command_register ("vesa", "Some test", &command_vesa, 0);
2167 command_register ("kill", "Send a signal to process", &command_kill, 0);
2168 command_register ("modprobe", "program to add modules from the kernel", &command_modprobe, 0);
2169 command_register ("lsmod", "program to show the status of modules", &command_lsmod, 0);
2170 #ifdef CONFIG_DRV_PCI
2171 command_register ("lspci", "list all pci devices", &command_lspci, 0);
2172 #endif
2173 command_register ("mouse", "ps2 mouse test", &command_mouse, 0);
2174 command_register ("iflist", "Show network interface", &command_iflist, 0);
2175 command_register ("ifconfig", "Configure network interface", &command_ifconfig, 0);
2176 command_register ("ifroute", "Configure gateway address", &command_ifroute, 0);
2177 command_register ("dnsconfig", "Configure domain name server address", &command_dnsconfig, 0);
2178 command_register ("dhcpcl", "Configure interface using DHCP", &command_dhcpcl, 0);
2179 #ifdef CONFIG_PROTO_TUN6
2180 command_register ("tunconfig", "Configure IPv6 tunnel server address", &command_tunconfig, 0);
2181 #endif
2182 command_register ("hostname", "Configure network hostname", &command_hostname, 0);
2183 command_register ("kbdmap", "Change keyboard layout", &command_kbdmap, 0);
2184 #ifdef CONFIG_DRV_ZEXFS
2185 command_register ("mkzexfs", "Create zexfs filesystem on hdd", &command_mkzexfs, 0);
2186 #endif
2187 #ifdef CONFIG_DRV_EXT2
2188 command_register ("mkext2", "Create ext2 filesystem on hdd", &command_mkext2, 0);
2189 #endif
2190 #ifdef CONFIG_PROTO_IPV4
2191 command_register ("ping", "Send ICMP echo request", &command_ping, 0);
2192 #endif
2193 #ifdef CONFIG_PROTO_IPV6
2194 command_register ("ping6", "Send ICMPv6 echo request", &command_ping6, 0);
2195 #endif
2196 command_register ("netexec", "Execute file from network", &command_netexec, 0);
2197 command_register ("netcp", "Copy file from network to filesystem", &command_netcp, 0);
2198 #ifdef CONFIG_DRV_ZNFS
2199 command_register ("znfscl", "Connect to znfs server - network filesystem", &command_znfscl, 0);
2200 #endif
2201 command_register ("savemode", "Make less cpu load", &command_savemode, 0);
2202 command_register ("cpuinfo", "Show CPU info", &command_cpuinfo, 0);
2204 command_register ("adm", "Automatic Device Mounter", &command_adm, 0);
2205 command_register ("ttyctrl", "TTY Console control", &command_ttyctrl, 0);
2206 command_register ("cmd", "Execute command sequence", &command_cmd, 0);
2208 return 1;