Fixed some bugs after merge;
[ZeXOS.git] / kernel / core / commands.c
blob890891cab36267172bfae613b152f183730e99cd
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));
413 char arg[arg_len+1];
414 strcpy (arg, argparse (cmd_buf));
415 arg[arg_len] = '\0';
417 unsigned cmd_buf_len = strlen (cmd_buf);
419 tty_t *tty = currtty;
421 if (!tty)
422 return 0;
424 /* lock current tty keyboard */
425 tty_lock (tty);
427 /* if there is argument after name, rewrite space character to zero */
428 if (cmd_buf [cmd_buf_len-(arg_len+1)] == ' ')
429 cmd_buf [cmd_buf_len-(arg_len+1)] = '\0';
430 else
431 cmd_buf [cmd_buf_len-arg_len] = '\0';
433 vfs_content_t content;
434 /* read file data from filesystem */
435 int ret = vfs_read (cmd_buf, strlen (cmd_buf), &content);
437 if (ret < 0) {
438 printf ("%s: command not found\n", cmd_buf);
439 DPRINT (DBG_CMDS, "ERROR -> vfs_read () - '%s'\n", cmd_buf);
440 tty_unlock (tty);
441 return 0;
444 unsigned bin_len = content.len;
446 unsigned char *bin = (unsigned char *) swmalloc (bin_len);
448 if (!bin) {
449 printf ("ERROR -> out of memory\n");
450 return 0;
453 //paging_disable ();
455 /* copy image to page aligned memory */
456 memcpy (bin, content.ptr, bin_len);
458 //paging_enable ();
460 cache_closebyptr (content.ptr);
462 unsigned entry, code, data, data_off, bss;
463 int err = exec_elf (bin, &entry, &data, &data_off, &bss);
465 if (err) {
466 printf ("ERROR -> invalid ELF exec\n");
468 /* free program image */
469 swfree (bin, bin_len);
471 tty_unlock (tty);
472 return 0;
475 /* create process */
476 proc_t *proc = proc_create (tty, cmd_buf, entry);
478 /* check for & character as app parameter */
479 unsigned d = 0;
480 while (d < arg_len) {
481 if (arg[d] == '&') {
482 if (proc_flag_set (proc, PROC_FLAG_DAEMON))
483 printf ("%s: started as daemon\n", cmd_buf);
485 arg[d] = '\0';
487 if (arg_len > 2 && arg[d-1] == ' ') {
488 arg[d-1] = '\0';
489 arg_len --;
492 arg_len --;
494 break;
496 d ++;
499 if (!proc) {
500 printf ("ERROR -> Invalid process: %s\n", cmd_buf);
502 /* free program image */
503 swfree (bin, bin_len);
505 tty_unlock (tty);
506 return 0;
509 /* save process information to structure for later use */
510 proc->start = (unsigned) bin;
511 proc->code = entry;
512 proc->data = data;
513 proc->data_off = data_off;
514 proc->bss = bss;
515 proc->end = (unsigned) (bin + bin_len);
517 /* well, set argument for out app */
518 proc_arg_set (proc, arg, arg_len);
520 proc_vmem_map (proc);
522 /* Is this process started as daemon ? */
523 /* wait here, until pid != 0*/
524 if (!(proc->flags & PROC_FLAG_DAEMON))
525 while (proc->pid)
526 schedule ();
528 /* enable interrupts */
529 int_enable ();
531 /* unlock tty console - user can write in */
532 tty_unlock (tty);
534 _curr_task = proc->tty->task; // this is pretty needed for correct return to our tty task
536 if (!(proc->flags & PROC_FLAG_DAEMON)) {
537 if (!proc_done (proc))
538 for (;;) // NOTE: heh, this is good stuff :P
539 schedule ();
541 /* free program image */
542 swfree (bin, bin_len);
545 /* switch to next task */
546 schedule ();
548 return 1;
551 unsigned command_lsdev (char *command, unsigned len)
553 dev_display ();
555 return 1;
558 unsigned command_login (char *command, unsigned len)
560 currtty->user = NULL;
561 currtty->logged = false;
563 video_color (7, 0);
564 printf ("login: ");
566 return 1;
569 unsigned command_serialw (char *command, unsigned len)
571 dev_t *dev = dev_find ("/dev/com0");
573 if (dev) {
574 unsigned len = strlen (argparse (command));
575 char data[len+1];
576 memcpy (data, argparse (command), len);
577 data[len] = '\0';
579 dev->handler (DEV_ACT_WRITE, data, len);
581 printf ("serialw: %s\n", data);
584 return 1;
587 unsigned command_serialr (char *command, unsigned len)
589 dev_t *dev = dev_find ("/dev/com0");
591 if (dev) {
592 char data[11];
594 dev->handler (DEV_ACT_READ, data, 1);
596 printf ("serialr: %s\n", data);
599 return 1;
602 unsigned command_fdisk (char *command, unsigned len)
604 char *parm = argparse (command);
606 fdisk (parm, strlen (parm));
608 return 1;
611 unsigned command_hdcat (char *command, unsigned len)
613 tty_lock (currtty);
615 dev_t *dev = (dev_t *) dev_find (argparse (command));
617 if (dev) {
618 partition_t p;
619 p.base_io = 0;
621 switch (dev->devname[7]) {
622 case 'a':
623 p.id = 0;
624 p.base_io = 0x1F0;
625 break;
626 case 'b':
627 p.id = 1;
628 p.base_io = 0x1F0;
629 break;
630 case 'c':
631 p.id = 0;
632 p.base_io = 0x170;
633 break;
634 case 'd':
635 p.id = 1;
636 p.base_io = 0x170;
637 break;
640 int c, d = 0;
641 unsigned char block[512];
643 printf ("dev: %s\n", dev->devname);
645 while (!key_pressed (1)) {
646 if (key_pressed (72) == 1) {
647 printf ("##block: %d\n", d);
648 dev->handler (DEV_ACT_READ, &p, block, "", d);
650 for (c = 0; c < 512; c ++)
651 printf ("%c", (unsigned) block[c]);
652 d ++;
655 usleep (5);
657 } else
658 printf ("Please specify block device, example: hdcat /dev/hda\n");
660 tty_unlock (currtty);
662 return 1;
665 unsigned command_free (char *command, unsigned len)
667 util_cmdfree ();
669 return 1;
672 unsigned command_top (char *command, unsigned len)
674 tty_lock (currtty);
676 proc_top ();
678 tty_unlock (currtty);
680 return 1;
683 unsigned command_date (char *command, unsigned len)
685 tm *t = rtc_getcurrtime ();
687 printf ("%02u:%02u:%02u, %u.%u.%u\n",
688 t->tm_hour, t->tm_min, t->tm_sec, t->tm_mday, t->tm_mon, t->tm_year);
690 return 1;
694 Tone Frequency (Hz)
695 C 130.8
696 D 146.8
697 E 164.8
698 F 174.6
699 G 196.0
700 A 220.0
701 H 246.9
703 C 261.6
704 D 293.7
705 E 329.6
706 F 349.2
707 G 392.0
708 A 440.0
709 H 493.9
711 C 523.3
712 D 587.3
713 E 659.3
714 F 698.5
715 G 784.0
716 A 880.0
717 H 987.8
719 C 1046.5
720 D 1174.7
721 E 1318.5
722 F 1396.9
723 G 1568.0
724 A 1760.0
725 H 1975.5
728 void play_tone (dev_t *dev, char tone, char type, char time)
730 unsigned t = 2;
732 switch (type) {
733 case '1':
734 t = 1;
735 break;
736 case '2':
737 t = 2;
738 break;
739 case '3':
740 t = 3;
741 break;
742 case '4':
743 t = 4;
744 break;
748 unsigned tt = 1;
750 printf ("play_tone (.., %c, %d, %d);\n", tone, t, tt);
752 switch (tone) {
753 case 'c':
754 dev->handler (DEV_ACT_PLAY, 131*t);
755 break;
756 case 'd':
757 dev->handler (DEV_ACT_PLAY, 147*t);
758 break;
759 case 'e':
760 dev->handler (DEV_ACT_PLAY, 165*t);
761 break;
762 case 'f':
763 dev->handler (DEV_ACT_PLAY, 175*t);
764 break;
765 case 'g':
766 dev->handler (DEV_ACT_PLAY, 196*t);
767 break;
768 case 'a':
769 dev->handler (DEV_ACT_PLAY, 220*t);
770 break;
771 case 'h':
772 dev->handler (DEV_ACT_PLAY, 247*t);
773 break;
774 case 'C':
775 dev->handler (DEV_ACT_PLAY, 136*t);
776 break;
777 case 'F':
778 dev->handler (DEV_ACT_PLAY, 188*t);
779 break;
782 switch (time) {
783 case '0':
784 timer_wait (2500);
785 break;
786 case '1':
787 timer_wait (1250);
788 break;
789 case '2':
790 timer_wait (625);
791 break;
796 printf ("end: play_tone ();\n");
798 dev->handler (DEV_ACT_PLAY, 0);
801 void play_song (dev_t *dev, char *data)
803 printf ("play_song ();\n");
804 unsigned i = 0, l = strlen (data);
805 unsigned time = 1;
806 while (i != l) {
807 switch (data[i]) {
808 printf ("switch ()\n");
809 case 'p':
811 switch (data[i+1]) {
812 case '0':
813 timer_wait (2500);
814 break;
815 case '1':
816 timer_wait (1250);
817 break;
818 case '2':
819 timer_wait (625);
820 break;
824 i ++;
825 break;
826 case ' ':
827 break;
828 default:
829 play_tone (dev, data[i], data[i+1], data[i+2]);
830 i += 2;
831 break;
834 i ++;
839 unsigned command_spk (char *command, unsigned len)
841 unsigned int freq = 20;
843 dev_t *dev = dev_find ("/dev/pcspk");
845 if (dev) {
847 We wish you a merry christmas v1.0
849 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)
852 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");
854 timer_wait (2000);
857 We wish you a merry christmas v3.0
859 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)
862 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");
864 timer_wait (2000);
867 Dallas v2.0
868 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)
870 /* 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");*/
872 //timer_wait (2000);
875 return 1;
878 extern bool bus_pci_acthandler (unsigned act, char *block, unsigned block_len);
879 extern netif_t netif_list;
880 extern void ext2_dump ();
881 void thread_test ()
883 while (1) {
885 kprintf ("hu\n");
887 unsigned long timer_start = timer_ticks + ((unsigned) 1 * 1000);
889 while (timer_start > timer_ticks)
890 schedule ();
894 extern unsigned start;
895 extern unsigned end;
896 extern bool ide_acthandler (unsigned act, partition_t *p, char *block, char *more, int n);
897 unsigned command_test (char *command, unsigned len)
899 partition_t *p = partition_find ("/dev/hda0");
901 if (p)
902 mount (p, "", "/mnt/hdd/");
903 else
904 return 0;
906 cd ("/mnt/hdd");
908 return 1;
910 extern char *vgafb;
911 extern char *vgadb;
912 unsigned command_test2 (char *command, unsigned len)
914 partition_t *p = partition_find ("/dev/cdc0");
916 if (p)
917 mount (p, "", "/mnt/cdrom/");
918 else
919 return 0;
921 cd ("/mnt/cdrom");
923 /* tty_t *tty = tty_find ("tty0");
925 if (!tty)
926 return 0;
927 char str[513];
928 int r = tty_read (tty, str, 512);
930 if (r > 0) {
931 printf ("str: r: %d\n", r);
932 int i;
933 for (i = 0; i < r; i ++)
934 tty_putch (str[i]);
938 /* int ret = vfs_read ("wav", 3);
940 if (ret < 0) {
941 printf ("chyba\n");
942 return 0;
945 cache_t *cache = cache_read ();
947 unsigned char *p = (unsigned char *) &cache->data;
949 unsigned bin_len = cache->limit;
951 unsigned char *bin = (unsigned char *) 0x400000 - 44;
953 memcpy (bin, p, bin_len);
955 typedef struct {
956 unsigned chunkid;
957 unsigned chunksize;
958 unsigned format;
959 } riff_head_t;
961 typedef struct {
962 unsigned subchunk1id;
963 unsigned subchunk1size;
964 unsigned short audioformat;
965 unsigned short numchannels;
966 unsigned samplerate;
967 unsigned byterate;
968 unsigned short blockalign;
969 unsigned short bitspersample;
970 } fmt_head_t;
972 typedef struct {
973 unsigned subchunk2id;
974 unsigned subchunk2size;
975 } data_head_t;
977 riff_head_t *r = (riff_head_t *) bin;
978 fmt_head_t *fmt = (fmt_head_t *) ((char *) bin + sizeof (riff_head_t));
979 data_head_t *data = (data_head_t *) ((char *) bin + sizeof (riff_head_t) + sizeof (fmt_head_t));
981 printf ("riff_head_t 0x%x : %d : 0x%x\n", r->chunkid, r->chunksize, r->format);
982 printf ("fmt_head_t %d : %d : %d : %d : %d : %d\n", fmt->audioformat, fmt->subchunk1size, fmt->numchannels, fmt->samplerate, fmt->byterate, fmt->bitspersample);
983 printf ("data_head_t 0x%x : %d\n", data->subchunk2id, data->subchunk2size);
984 //memset (bin+44, 0, bin_len-44);
985 //dev->handler (DEV_ACT_WRITE, bin+44, bin_len-44);
987 snd_cfg_t cfg;
988 cfg.dev = "/dev/ac97";
989 cfg.rate = fmt->samplerate;
990 cfg.format = SOUND_FORMAT_S16;
991 cfg.channels = fmt->numchannels;
993 snd_audio_t *aud = audio_open (&cfg);
995 if (!aud)
996 return 0;
998 audio_write (aud, bin+44, (bin_len-44));
1000 //swfree (bin, bin_len);*/
1004 return 1;
1007 unsigned command_test3 (char *command, unsigned len)
1009 netif_t *netif = netif_findbyname ("eth0");
1010 dhcp_config_if(netif);
1012 return 1;
1015 unsigned command_test4 (char *command, unsigned len)
1019 return 1;
1022 unsigned command_mouse (char *command, unsigned len)
1024 printf ("PS/2 Mouse test\n");
1026 dev_t *dev = dev_find ("/dev/mouseps2");
1028 if (!dev)
1029 return 0;
1031 dev_mouse_t mouse;
1033 int ps2_fd = open ("/dev/mouseps2", O_RDONLY);
1035 if (!ps2_fd)
1036 return 0;
1038 printf ("dddd\n");
1040 while (!key_pressed (1)) {
1041 int r = read (ps2_fd, &mouse, sizeof (dev_mouse_t));
1043 if (!r) {
1044 kprintf ("oj oj\n");
1045 return 0;
1048 kprintf ("ps2mouse: x,y: %d, %d | 0x%x \n", mouse.pos_x, mouse.pos_y, mouse.flags);
1050 lseek (ps2_fd, 0, SEEK_SET);
1052 schedule ();
1055 return 1;
1058 unsigned command_vesa (char *command, unsigned len)
1060 if (!init_video_vesa ())
1061 printf ("vesa -> failed\n");
1063 return 1;
1066 unsigned command_kill (char *command, unsigned len)
1068 strcpy (cmd_buf, argparse (currtty->shell));
1070 pid_t pid = (pid_t) atoi (cmd_buf);
1072 proc_t *proc = proc_findbypid (pid);
1074 if (!proc) {
1075 printf ("kill: (%s) - No such process\n", cmd_buf);
1076 return 0;
1079 /* send SIGTERM to selected process */
1080 if (!proc_signal (proc, SIGTERM))
1081 return 0;
1083 /* check for daemon */
1084 if (proc->flags & PROC_FLAG_DAEMON)
1085 if (!proc_done (proc))
1086 return 0;
1088 return 1;
1091 unsigned command_modprobe (char *command, unsigned len)
1093 unsigned arg_len = strlen (argparse (currtty->shell));
1094 char arg[arg_len+1];
1095 strcpy (arg, argparse (currtty->shell));
1096 arg[arg_len] = '\0';
1098 module_load (arg);
1100 return 1;
1103 unsigned command_lsmod (char *command, unsigned len)
1105 module_display ();
1107 return 1;
1110 #ifdef CONFIG_DRV_PCI
1111 unsigned command_lspci (char *command, unsigned len)
1113 #ifdef ARCH_i386
1114 pcidev_display ();
1115 #endif
1116 return 1;
1118 #endif
1120 unsigned command_iflist (char *command, unsigned len)
1122 iflist_display ();
1124 return 1;
1127 unsigned command_ifconfig (char *command, unsigned len)
1129 strcpy (cmd_buf, argparse (command));
1130 char ifid[20];
1131 char ip[32];
1133 unsigned l = strlen (cmd_buf);
1134 unsigned x = 0;
1135 while (x < l) {
1136 if (cmd_buf[x] == ' ')
1137 break;
1138 x ++;
1141 memcpy (ifid, cmd_buf, x);
1142 ifid[x] = '\0';
1144 strcpy (ip, argparse (cmd_buf));
1145 unsigned y = strlen (ip);
1147 netif_t *netif = netif_findbyname (ifid);
1149 if (!netif) {
1150 printf ("ifconfig -> bad network interface name, example: eth0\n");
1151 return 0;
1154 net_ipv4 ipv4;
1155 net_ipv6 ipv6;
1156 if (ipv4 = net_proto_ip_convert (ip)) {
1157 netif_ip_addr (netif, ipv4, IF_CFG_TYPE_STATIC);
1159 printf ("%s: IPv4 ", ifid);
1160 net_proto_ip_print (ipv4);
1161 printf (" - OK\n");
1162 } else if (net_proto_ipv6_convert (ipv6, ip)) {
1163 netif_ipv6_addr (netif, ipv6, IF_CFG_TYPE_STATIC);
1165 printf ("%s: IPv6 ", ifid);
1166 net_proto_ipv6_print (ipv6);
1167 printf (" - OK\n");
1168 } else {
1169 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");
1170 return 0;
1173 return 1;
1176 unsigned command_ifroute (char *command, unsigned len)
1178 strcpy (cmd_buf, argparse (command));
1179 char ifid[20];
1180 char ip[32];
1182 unsigned l = strlen (cmd_buf);
1183 unsigned x = 0;
1184 while (x < l) {
1185 if (cmd_buf[x] == ' ')
1186 break;
1187 x ++;
1190 memcpy (ifid, cmd_buf, x);
1191 ifid[x] = '\0';
1193 strcpy (ip, argparse (cmd_buf));
1194 unsigned y = strlen (ip);
1196 netif_t *netif = netif_findbyname (ifid);
1198 if (!netif) {
1199 printf ("ifroute -> bad network interface name, example: eth0\n");
1200 return 0;
1203 unsigned char a;
1204 unsigned char b;
1205 unsigned char c;
1206 unsigned char d;
1208 unsigned g = 0;
1209 unsigned i = 0;
1211 unsigned h[4];
1213 while (i < y) {
1214 if (ip[i] == '.') {
1215 ip[i] = '\0';
1216 h[g] = i+1;
1217 g ++;
1220 i ++;
1223 if (g != 3) {
1224 printf ("ifroute -> bad ip address format, example: 192.168.1.254\n");
1225 return 0;
1228 a = atoi (ip);
1229 b = atoi (ip+h[0]);
1230 c = atoi (ip+h[1]);
1231 d = atoi (ip+h[2]);
1233 printf ("%s->gw: %d.%d.%d.%d - OK\n", ifid, a, b, c, d);
1235 return netif_gw_addr (netif, NET_IPV4_TO_ADDR (a, b, c, d));
1238 unsigned command_dnsconfig (char *command, unsigned len)
1240 strcpy (cmd_buf, argparse (currtty->shell));
1242 if (!cmd_buf)
1243 return 0;
1245 unsigned l = strlen (cmd_buf);
1247 if (!l) {
1248 printf ("dnsconfig -> your domain name server is: ");
1249 net_proto_ip_print (dns_addr_get ());
1250 printf ("\n");
1252 return 0;
1255 printf ("dnsconfig: %s - OK\n", cmd_buf);
1257 net_ipv4 dns = net_proto_ip_convert (cmd_buf);
1259 if (!dns)
1260 return 0;
1262 dns_addr (dns);
1264 return 1;
1267 unsigned command_dhcpcl (char *command, unsigned len)
1269 strcpy (cmd_buf, argparse (currtty->shell));
1271 if (!cmd_buf)
1272 return 0;
1274 unsigned l = strlen (cmd_buf);
1276 if (!l) {
1277 printf ("dhcpcl -> please specify interface\n");
1278 return 0;
1281 netif_t *netif = netif_findbyname (cmd_buf);
1283 if (!netif) {
1284 printf ("dhcpcl -> bad network interface name, example: eth0\n");
1285 return 0;
1288 unsigned ret = dhcp_config_if (netif);
1290 if (ret < 0) {
1291 switch (ret) {
1292 case DHCP_OUT_OF_MEMORY:
1293 printf ("dhcpcl -> out of memory\n");
1294 break;
1295 case DHCP_SOCKET_FAILED:
1296 case DHCP_CONNECT_FAILED:
1297 case DHCP_CANT_SEND_REQUEST:
1298 printf ("dhcpcl -> problem with network layer\n");
1299 break;
1300 case DHCP_CANT_RECV_RESPONSE:
1301 printf ("dhcpcl -> can't receive response\n");
1302 break;
1303 case DHCP_BAD_PACKET:
1304 printf ("dhcpcl -> bad packet received\n");
1305 break;
1306 case DHCP_SRV_DIDNT_UNDERSTAND:
1307 printf ("dhcpcl -> server didn't understand\n");
1308 break;
1310 } else {
1311 printf ("dhcpcl -> interface %s successfully configured\n", cmd_buf);
1315 unsigned command_tunconfig (char *command, unsigned len)
1317 strcpy (cmd_buf, argparse (currtty->shell));
1319 if (!cmd_buf)
1320 return 0;
1322 unsigned l = strlen (cmd_buf);
1324 if (!l) {
1325 printf ("tunconfig -> wrong syntax !\ntunconfig <ipv4tunnel> for enable or tunconfig 0 for disable\n");
1327 return 0;
1330 /* you can disable this service over character '0' as parameter */
1331 if (l == 1 && cmd_buf[0] == '0') {
1332 tun6_addr (0);
1334 printf ("tunconfig: disabled - OK\n");
1336 return 1;
1339 /* classical ipv4 address from tunnel server */
1340 net_ipv4 tunnel = net_proto_ip_convert (cmd_buf);
1342 if (!tunnel) {
1343 printf ("tunconfig -> wrong syntax !\nExample: tunconfig 216.66.80.30\n");
1345 return 0;
1348 tun6_addr (tunnel);
1350 printf ("tunconfig: ::%s - OK\n", cmd_buf);
1352 return 1;
1356 unsigned command_hostname (char *command, unsigned len)
1358 strcpy (cmd_buf, argparse (currtty->shell));
1360 if (!cmd_buf)
1361 return 0;
1363 unsigned l = strlen (cmd_buf);
1365 if (!l) {
1366 printf ("%s\n", hostname_get ());
1368 return 0;
1371 printf ("hostname: %s - OK\n", cmd_buf);
1373 hostname_set (cmd_buf);
1375 return 1;
1378 unsigned command_kbdmap (char *command, unsigned len)
1380 strcpy (cmd_buf, argparse (currtty->shell));
1382 if (!cmd_buf)
1383 return 0;
1385 unsigned ret = keyboard_setlayout (cmd_buf);
1387 if (ret)
1388 printf ("Keyboard layout was changed to '%s'\n", cmd_buf);
1390 return ret;
1393 unsigned command_mkzexfs (char *command, unsigned len)
1395 #ifdef ARCH_i386
1396 strcpy (cmd_buf, argparse (currtty->shell));
1398 partition_t *p = partition_find (cmd_buf);
1400 if (!p)
1401 return 0;
1403 mkzexfs (p);
1404 #endif
1405 return 1;
1408 unsigned command_mkext2 (char *command, unsigned len)
1410 #ifdef ARCH_i386
1411 strcpy (cmd_buf, argparse (currtty->shell));
1413 if (!cmd_buf)
1414 return 0;
1416 partition_t *p = partition_find ("/dev/hda0");
1418 mkext2 (p);
1419 #endif
1420 return 1;
1423 unsigned command_ping (char *command, unsigned len)
1426 strcpy (cmd_buf, argparse (currtty->shell));
1428 if (!cmd_buf)
1429 return 0;
1431 netif_t *netif = netif_findbyname ("eth0");
1433 if (!netif) {
1434 printf ("ping -> network interface does not exists\n");
1435 return 0;
1438 unsigned char a;
1439 unsigned char b;
1440 unsigned char c;
1441 unsigned char d;
1443 unsigned g = 0;
1444 unsigned i = 0;
1445 unsigned y = strlen (cmd_buf);
1447 if (!y) {
1448 printf ("ping -> wrong syntax, please specify address\n");
1449 return 0;
1452 unsigned h[4];
1454 while (i < y) {
1455 if (cmd_buf[i] == '.') {
1456 cmd_buf[i] = '\0';
1457 h[g] = i+1;
1458 g ++;
1461 i ++;
1464 net_ipv4 target = 0;
1466 if (g != 3) {
1467 strcpy (cmd_buf, argparse (currtty->shell));
1469 if (dns_cache_get (cmd_buf, &target, 4) != 1)
1470 if (dns_send_request (cmd_buf, &target, 4) != 1) {
1471 printf ("ping -> bad hostname or ip address format, example: 192.168.1.1\n");
1472 return 0;
1474 } else {
1475 a = atoi (cmd_buf);
1476 b = atoi (cmd_buf+h[0]);
1477 c = atoi (cmd_buf+h[1]);
1478 d = atoi (cmd_buf+h[2]);
1480 target = NET_IPV4_TO_ADDR (a, b, c, d);
1482 strcpy (cmd_buf, argparse (currtty->shell));
1485 char address[20];
1486 net_proto_ip_convert2 (target, address);
1488 i = 0;
1490 while (i < 8) {
1491 unsigned long time_a = timer_ticks;
1492 unsigned ret = net_proto_icmp_ping (netif, target);
1493 if (ret) {
1494 printf ("ping -> %s (%s) - %ums\n", cmd_buf, address, (timer_ticks - time_a));
1495 } else {
1496 printf ("ping -> Host %s is unreachable\n", cmd_buf);
1497 break;
1500 i ++;
1503 return 1;
1506 unsigned command_ping6 (char *command, unsigned len)
1508 strcpy (cmd_buf, argparse (currtty->shell));
1510 if (!cmd_buf)
1511 return 0;
1513 netif_t *netif = netif_findbyname ("eth0");
1515 if (!netif) {
1516 printf ("ping6 -> network interface does not exists\n");
1517 return 0;
1520 unsigned short a;
1521 unsigned short b;
1522 unsigned short c;
1523 unsigned short d;
1524 unsigned short e;
1525 unsigned short f;
1526 unsigned short g;
1527 unsigned short h;
1529 unsigned j = 0;
1530 unsigned i = 0;
1531 unsigned y = strlen (cmd_buf);
1533 if (!y) {
1534 printf ("ping6 -> wrong syntax, please specify ipv6 address\n");
1535 return 0;
1538 unsigned k[8];
1540 while (i < y) {
1541 if (cmd_buf[i] == ':') {
1542 cmd_buf[i] = '\0';
1543 k[j] = i+1;
1544 j ++;
1547 i ++;
1550 net_ipv6 target;
1552 if (j != 7) {
1553 strcpy (cmd_buf, argparse (currtty->shell));
1555 if (dns_cache_get (cmd_buf, &target, 16) != 1)
1556 if (dns_send_request (cmd_buf, &target, 16) != 1) {
1557 printf ("ping6 -> bad hostname or ip address format, example: 2001:4d3:8fe:2:0:0:0:1\n");
1558 return 0;
1560 } else {
1561 char *endptr;
1563 a = strtol (cmd_buf, &endptr, 16);
1564 b = strtol (cmd_buf+k[0], &endptr, 16);
1565 c = strtol (cmd_buf+k[1], &endptr, 16);
1566 d = strtol (cmd_buf+k[2], &endptr, 16);
1567 e = strtol (cmd_buf+k[3], &endptr, 16);
1568 f = strtol (cmd_buf+k[4], &endptr, 16);
1569 g = strtol (cmd_buf+k[5], &endptr, 16);
1570 h = strtol (cmd_buf+k[6], &endptr, 16);
1572 NET_IPV6_TO_ADDR (target, a, b, c, d, e, f, g, h);
1574 //printf ("ipv6: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", a, b, c, d, e, f, g, h);
1576 strcpy (cmd_buf, argparse (currtty->shell));
1579 char address[40];
1580 //net_proto_ipv6_convert2 (target, address);
1582 i = 0;
1584 while (i < 8) {
1585 unsigned long time_a = timer_ticks;
1586 unsigned ret = net_proto_icmp6_ping (netif, target);
1587 if (ret) {
1588 printf ("ping6 -> %s (%s) - %ums\n", cmd_buf, cmd_buf, (timer_ticks - time_a));
1589 } else {
1590 printf ("ping6 -> Host %s is unreachable\n", cmd_buf);
1591 break;
1594 i ++;
1597 return 1;
1600 unsigned command_netexec (char *command, unsigned len)
1602 strcpy (cmd_buf, argparse (command));
1604 if (!cmd_buf)
1605 return 0;
1607 if (!strlen (cmd_buf)) {
1608 printf ("Syntax:\nnetexec <ip_of_tftp> <filename> [parameters]\n");
1609 return 0;
1612 unsigned arg_len = strlen (argparse (cmd_buf));
1613 char arg[arg_len+1];
1614 strcpy (arg, argparse (cmd_buf));
1615 arg[arg_len] = '\0';
1617 unsigned cmd_buf_len = strlen (cmd_buf);
1618 cmd_buf [cmd_buf_len-arg_len] = '\0';
1620 if (cmd_buf[cmd_buf_len-arg_len-1] == ' ') {
1621 cmd_buf[cmd_buf_len-arg_len-1] = '\0';
1622 cmd_buf_len --;
1625 char file[arg_len+1];
1626 unsigned file_len = 0;
1627 memcpy (file, arg, arg_len);
1628 while (file_len < arg_len) {
1629 if (file[file_len] == ' ') {
1630 file[file_len] = '\0';
1631 break;
1633 file_len ++;
1636 if (file[arg_len] != '\0')
1637 file[arg_len] = '\0';
1639 unsigned l = file_len;
1641 unsigned ret = tftp_client_connect (cmd_buf, 69, file, &l);
1643 if (!ret) {
1644 printf ("tftp -> something go wrong !\n");
1645 return 0;
1648 printf ("tftp -> file length: %ubytes\t\t[OK]\n", l);
1650 char *bin = (unsigned char *) swmalloc (sizeof (char) * (l+1));
1652 if (!bin)
1653 return 0;
1655 int ret2 = tftp_client_read (ret, bin, l);
1657 if (!ret2) {
1658 printf ("tftp -> tftp_client_read () error\n");
1659 swfree (bin, l);
1660 return 0;
1663 unsigned entry, code, data, data_off, bss;
1664 int err = exec_elf (bin, &entry, &data, &data_off, &bss);
1666 if (err != NULL) {
1667 printf ("ERROR -> invalid ELF exec\n");
1668 return 0;
1671 tty_lock (currtty);
1672 timer_wait (1);
1673 /* create process */
1674 proc_t *proc = proc_create (currtty, "netexec", entry);
1675 timer_wait (1);
1678 /* check for & character as app parameter */
1679 /* unsigned d = 0;
1680 while (d < arg_len) {
1681 if (arg[d] == '&') {
1682 if (proc_flag_set (proc, PROC_FLAG_DAEMON))
1683 printf ("%s: started as daemon\n", cmd_buf);
1685 arg[d] = '\0';
1687 if (arg_len > 2 && arg[d-1] == ' ') {
1688 arg[d-1] = '\0';
1689 arg_len --;
1692 arg_len --;
1694 break;
1696 d ++;
1699 if (!proc) {
1700 printf ("ERROR -> Invalid process: %s\n", cmd_buf);
1701 return 0;
1704 timer_wait (1);
1706 /* save process information to structure for later use */
1707 proc->start = (unsigned) bin;
1708 proc->code = entry;
1709 proc->data = data;
1710 proc->data_off = data_off;
1711 proc->bss = bss;
1712 proc->end = (unsigned) (bin + l);
1714 timer_wait (1);
1716 /* well, set argument for out app */
1717 proc_arg_set (proc, arg+file_len+1, arg_len-file_len-1);
1719 proc_vmem_map (proc);
1721 timer_wait (1);
1723 /* Is this process started as daemon ? */
1724 /* wait here, until pid != 0*/
1725 if (!(proc->flags & PROC_FLAG_DAEMON))
1726 while (proc->pid)
1727 schedule ();
1729 timer_wait (1);
1731 /* enable interrupts */
1732 int_enable ();
1734 /* unlock tty console - user can write in */
1735 tty_unlock (proc->tty);
1737 _curr_task = proc->tty->task; // this is pretty needed for correct return to our tty task
1739 if (!(proc->flags & PROC_FLAG_DAEMON)) {
1740 if (!proc_done (proc))
1741 while (1) // NOTE: heh, this is good stuff :P
1742 schedule ();
1744 /* free image of app */
1745 swfree (bin, l);
1748 /* switch to next task */
1749 schedule ();
1751 sclose (ret);
1753 return 1;
1756 unsigned command_netcp (char *command, unsigned len)
1758 strcpy (cmd_buf, argparse (command));
1760 if (!cmd_buf)
1761 return 0;
1763 if (!strlen (cmd_buf)) {
1764 printf ("Syntax:\nnetcp <ip_of_tftp> <tftp_file> <output_file>\n");
1765 return 0;
1768 unsigned arg_len = strlen (argparse (cmd_buf));
1769 char arg[arg_len+1];
1770 strcpy (arg, argparse (cmd_buf));
1771 arg[arg_len] = '\0';
1773 unsigned cmd_buf_len = strlen (cmd_buf);
1774 cmd_buf [cmd_buf_len-arg_len] = '\0';
1776 if (cmd_buf[cmd_buf_len-arg_len-1] == ' ') {
1777 cmd_buf[cmd_buf_len-arg_len-1] = '\0';
1778 cmd_buf_len --;
1781 char file[arg_len+1];
1782 unsigned file_len = 0;
1783 memcpy (file, arg, arg_len);
1784 while (file_len < arg_len) {
1785 if (file[file_len] == ' ') {
1786 file[file_len] = '\0';
1787 break;
1789 file_len ++;
1792 if (file[arg_len] != '\0')
1793 file[arg_len] = '\0';
1795 unsigned l = file_len;
1797 unsigned ret = tftp_client_connect (cmd_buf, 69, file, &l);
1799 if (!ret) {
1800 printf ("tftp -> something go wrong !\n");
1801 return 0;
1804 printf ("tftp -> file length: %ubytes\t\t[OK]\n", l);
1806 char *bin = (unsigned char *) swmalloc (sizeof (char) * (l+1));
1808 if (!bin)
1809 return 0;
1811 int ret2 = tftp_client_read (ret, bin, l);
1813 if (!ret2) {
1814 printf ("tftp -> tftp_client_read () error\n");
1815 swfree (bin, l);
1816 return 0;
1819 sclose (ret);
1821 char *output = argparse (arg);
1823 if (!output) {
1824 swfree (bin, l);
1825 printf ("ERROR -> output file name is empty\n");
1826 return 0;
1829 int fd = open (output, O_WRONLY | O_CREAT);
1831 if (!fd) {
1832 swfree (bin, l);
1833 printf ("ERROR -> something go wrong with %s\n", output);
1834 return 0;
1837 ret = write (fd, bin, l);
1839 if (!ret) {
1840 swfree (bin, l);
1841 printf ("ERROR -> File '%s' could'nt be writed\n", output);
1844 return 1;
1848 unsigned command_znfscl (char *command, unsigned len)
1850 #ifdef ARCH_i386
1851 strcpy (cmd_buf, argparse (command));
1853 if (!strlen (cmd_buf)) {
1854 printf ("Syntax:\nznfscl <ip_of_znfs>\n");
1855 return 0;
1858 dev_t *dev = dev_find ("/dev/vbd");
1860 if (dev) {
1861 printf ("znfscl: You are already connected to znfs server\n");
1862 return 0;
1865 dev = (dev_t *) dev_register ("vbd", "Virtual Block Device", DEV_ATTR_BLOCK, (dev_handler_t *) &ide_acthandler);
1867 if (!dev)
1868 return 0;
1870 partition_t *p = partition_add (dev, fs_supported ("znfs"), 0);
1872 if (!p)
1873 return 0;
1875 /* connect to specified address */
1876 if (znfs_init (p, cmd_buf) != 1) {
1877 printf ("znfs: Wrong znfs server\n");
1878 return 0;
1881 char str[16];
1883 sprintf (str, "/mnt/%s", p->name+5);
1885 mkdir (str);
1887 mount (p, "", str);
1889 printf ("znfs: You are succefully connected to ZNFS server '%s'\nZNFS was mounted into '%s'\n", cmd_buf, str);
1890 #else
1891 printf ("ERROR -> this architecture is not supported yet\n");
1892 #endif
1893 return 1;
1896 void task_savemode ()
1898 /* disable cpu for cycle */
1899 for (;; schedule ())
1900 arch_cpu_hlt ();
1903 unsigned command_savemode (char *command, unsigned len)
1905 task_t *task = (task_t *) task_create ("savemode", (unsigned) &task_savemode, 16);
1907 if (!task) {
1908 printf ("ERROR -> Save mode thread was'nt created\n");
1909 return 0;
1912 printf ("Save mode was succefully started\n");
1914 return 1;
1917 unsigned command_cpuinfo (char *command, unsigned len)
1919 printf ("Processor info:\n");
1921 cat ("/proc/cpuinfo");
1923 return 1;
1926 unsigned command_adm (char *command, unsigned len)
1928 printf ("Automatic Device Mounter - init ()\n");
1930 init_adm ();
1932 return 1;
1935 unsigned command_ttyctrl (char *command, unsigned len)
1937 char *parm = argparse (command);
1939 if (!strlen (parm)) {
1940 printf ("ttyctrl <operation> [parameter]\n\toperations: add; list; change <ttyid>\n");
1941 return 0;
1944 if (!strncmp (parm, "add", 3)) {
1945 tty_t *tty = tty_create ();
1947 if (!tty)
1948 return 0;
1950 video_color (7, 0);
1952 tty_write (tty, "\nlogin: ", 8);
1954 printf ("New tty '%s' was created\n", tty->name);
1955 } else
1956 if (!strncmp (parm, "list", 4)) {
1957 tty_listview ();
1958 } else
1959 if (!strncmp (parm, "change", 6)) {
1960 tty_t *tty = tty_find ((char *) argparse (parm));
1962 if (!tty)
1963 return 0;
1965 tty_change (tty);
1967 printf ("\n");
1970 return 1;
1973 unsigned command_cmd (char *command, unsigned len)
1975 unsigned s = 0;
1976 unsigned e = 0;
1978 if (!strlen (argparse (command))) {
1979 printf ("cmd <command1>;<command2>;...;\n");
1980 return 0;
1983 char *cmds = kmalloc (len - 3);
1985 if (!cmds)
1986 return 0;
1988 memcpy (cmds, command + 4, len - 4);
1989 cmds[len - 4] = '\0';
1991 unsigned i;
1992 for (i = 1; i < len - 4; i ++) {
1993 if (cmds[i] == ';') {
1994 cmds[i] = '\0';
1995 e = i-1;
1997 command_parser (cmds+s, e-s+1);
1999 s = i+1;
2003 kfree (cmds);
2005 return 1;
2008 /*************************************************************\
2009 | COMMAND's UTILS |
2010 \*************************************************************/
2012 unsigned command_parser (char *command, unsigned len)
2014 /* find first non-space in command */
2015 int i = 0;
2016 for (i = 0; i < len; i ++) {
2017 if (command[i] != ' ')
2018 break;
2020 command_t *ctx;
2022 /* try to find the command */
2023 for (ctx = command_list.next; ctx != &command_list; ctx = ctx->next) {
2024 if (!cstrcmp (ctx->name, command+i)) {
2025 ctx->handler (command+i, len-i); /* execute context function */
2026 return 1;
2030 printf ("%s: command not found\n", command);
2032 return 0;
2035 void commands (int i)
2037 // currtty->shell_len has been set to zero already
2038 command_parser (currtty->shell, strlen(currtty->shell));
2041 unsigned command_register (char *name, char *desc, command_handler_t *handler, unsigned flags)
2043 command_t *c;
2044 /* we dont want commands with same name */
2045 for (c = command_list.next; c != &command_list; c = c->next)
2046 if (!strcmp(c->name, name))
2047 return 0;
2049 command_t *ctx;
2051 // alloc and init context
2052 ctx = (command_t *) kmalloc (sizeof (command_t));
2054 if (!ctx)
2055 return 0;
2057 unsigned name_len = strlen (name);
2058 ctx->name = (char *) kmalloc (sizeof (char) * name_len +1);
2060 if (!ctx->name)
2061 return 0;
2063 memcpy (ctx->name, name, name_len);
2064 ctx->name[name_len] = '\0';
2066 unsigned desc_len = strlen (desc);
2067 ctx->desc = (char *) kmalloc (sizeof (char) * desc_len +1);
2069 if (!ctx->desc)
2070 return 0;
2072 memcpy (ctx->desc, desc, desc_len);
2073 ctx->desc[desc_len] = '\0';
2075 ctx->handler = handler;
2076 ctx->flags = flags;
2078 ctx->next = &command_list;
2079 ctx->prev = command_list.prev;
2080 ctx->prev->next = ctx;
2081 ctx->next->prev = ctx;
2083 return 1;
2086 unsigned command_unregister (char *name)
2088 command_t *ctx;
2090 for (ctx = command_list.next; ctx != &command_list; ctx = ctx->next) {
2091 if (!strcmp(ctx->name, name)) {
2092 ctx->next->prev = ctx->prev;
2093 ctx->prev->next = ctx->next;
2095 return 1;
2099 return 0;
2102 /*************************************************************\
2103 | INITIALIZATION OF COMMANDS |
2104 \*************************************************************/
2106 unsigned int init_commands ()
2108 command_list.next = &command_list;
2109 command_list.prev = &command_list;
2111 // reg commands
2112 command_register ("help", "Displays list of available commands", &command_help, 0);
2113 command_register ("hdd", "Detect HDD", &command_hdd, 0);
2114 command_register ("reboot", "Reboot computer", &command_reboot, 0);
2115 command_register ("halt", "Shutdown computer", &command_halt, 0);
2116 command_register ("tasks", "Print all tasks", &command_tasks, 0);
2117 command_register ("ps", "Print all process", &command_ps, 0);
2118 command_register ("uptime", "Print uptime in seconds", &command_uptime, 0);
2119 command_register ("version", "Displays a version of system", &command_version, 0);
2120 command_register ("debug", "Change to developer mode", &command_debug, 0);
2121 command_register ("mount", "Mount device to selected directory", &command_mount, 0);
2122 command_register ("umount", "Unmount mounted directory", &command_umount, 0);
2123 command_register ("env", "Displays all env variables", &command_env, 0);
2124 command_register ("cd", "Change directory", &command_cd, 0);
2125 command_register ("ls", " Displays files in current directory", &command_ls, 0);
2126 command_register ("cat", "Displays text in selected file", &command_cat, 0);
2127 command_register ("cp", "Copy a file", &command_cp, 0);
2128 command_register ("rm", "Remove a file", &command_rm, 0);
2129 command_register ("mkdir", "Create a directory", &command_mkdir, 0);
2130 command_register ("touch", "Create a file", &command_touch, 0);
2131 command_register ("exec", "Execute a selected program", &command_exec, 0);
2132 command_register ("lsdev", "Displays found devices", &command_lsdev, 0);
2133 command_register ("login", "Login as another user", &command_login, 0);
2134 command_register ("fdisk", "Partition table manipulator", &command_fdisk, 0);
2135 command_register ("hdcat", "Read selected block of data from drive", &command_hdcat, 0);
2136 command_register ("free", "Display amount of free and used memory", &command_free, 0);
2137 command_register ("top", "Stats of cpu usage", &command_top, 0);
2138 command_register ("date", "Show current date and time", &command_date, 0);
2139 command_register ("xmastime", "play noels on pc-speaker", &command_spk, 0);
2140 command_register ("test", "Some test", &command_test, 0);
2141 command_register ("test2", "Some test", &command_test2, 0);
2142 //command_register ("test3", "Some test", &command_test3, 0);
2143 //command_register ("testd", "Some test", &command_test4, 0);
2144 //command_register ("vesa", "Some test", &command_vesa, 0);
2145 command_register ("kill", "Send a signal to process", &command_kill, 0);
2146 command_register ("modprobe", "program to add modules from the kernel", &command_modprobe, 0);
2147 command_register ("lsmod", "program to show the status of modules", &command_lsmod, 0);
2148 #ifdef CONFIG_DRV_PCI
2149 command_register ("lspci", "list all pci devices", &command_lspci, 0);
2150 #endif
2151 command_register ("mouse", "ps2 mouse test", &command_mouse, 0);
2152 command_register ("iflist", "Show network interface", &command_iflist, 0);
2153 command_register ("ifconfig", "Configure network interface", &command_ifconfig, 0);
2154 command_register ("ifroute", "Configure gateway address", &command_ifroute, 0);
2155 command_register ("dnsconfig", "Configure domain name server address", &command_dnsconfig, 0);
2156 command_register ("dhcpcl", "Configure interface using DHCP", &command_dhcpcl, 0);
2157 #ifdef CONFIG_PROTO_TUN6
2158 command_register ("tunconfig", "Configure IPv6 tunnel server address", &command_tunconfig, 0);
2159 #endif
2160 command_register ("hostname", "Configure network hostname", &command_hostname, 0);
2161 command_register ("kbdmap", "Change keyboard layout", &command_kbdmap, 0);
2162 #ifdef CONFIG_DRV_ZEXFS
2163 command_register ("mkzexfs", "Create zexfs filesystem on hdd", &command_mkzexfs, 0);
2164 #endif
2165 #ifdef CONFIG_DRV_EXT2
2166 command_register ("mkext2", "Create ext2 filesystem on hdd", &command_mkext2, 0);
2167 #endif
2168 #ifdef CONFIG_PROTO_IPV4
2169 command_register ("ping", "Send ICMP echo request", &command_ping, 0);
2170 #endif
2171 #ifdef CONFIG_PROTO_IPV6
2172 command_register ("ping6", "Send ICMPv6 echo request", &command_ping6, 0);
2173 #endif
2174 command_register ("netexec", "Execute file from network", &command_netexec, 0);
2175 command_register ("netcp", "Copy file from network to filesystem", &command_netcp, 0);
2176 #ifdef CONFIG_DRV_ZNFS
2177 command_register ("znfscl", "Connect to znfs server - network filesystem", &command_znfscl, 0);
2178 #endif
2179 command_register ("savemode", "Make less cpu load", &command_savemode, 0);
2180 command_register ("cpuinfo", "Show CPU info", &command_cpuinfo, 0);
2182 command_register ("adm", "Automatic Device Mounter", &command_adm, 0);
2183 command_register ("ttyctrl", "TTY Console control", &command_ttyctrl, 0);
2184 command_register ("cmd", "Execute command sequence", &command_cmd, 0);
2186 return 1;