Added dhcpcl command; Some updates in kernel/core/net/dhcp.c;
[ZeXOS.git] / kernel / core / commands.c
blob28b66d297e450c1267f85e5b8e4d298ebae04a82
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <build.h>
23 #include <system.h>
24 #include <arch/io.h>
25 #include <string.h>
26 #include <partition.h>
27 #include <env.h>
28 #include <dev.h>
29 #include <vfs.h>
30 #include <fs.h>
31 #include <tty.h>
32 #include <proc.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <net/socket.h>
36 #include <net/dhcp.h>
37 #include <module.h>
38 #include <commands.h>
39 #include <signal.h>
40 #include <smp.h>
41 #include <build.h>
42 #include <config.h>
43 #include <net/if.h>
44 #include <net/net.h>
45 #include <net/ip.h>
46 #include <net/ndp.h>
47 #include <net/tun.h>
48 #include <net/icmp.h>
49 #include <net/hostname.h>
50 #include <mouse.h>
51 #include <pipe.h>
52 #include <cache.h>
53 #include <sound/audio.h>
55 command_t command_list;
56 extern partition_t partition_list;
58 unsigned char test[80];
60 typedef void (*void_fn_void_t)(void);
62 extern task_t task_list;
63 extern vfs_t vfs_list;
64 extern int cstrcmp (char *one, char *two);
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 while (1)
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 (test, argparse (command));
233 unsigned l = strlen (test);
235 if (l) {
236 debug = atoi (test);
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 (test, argparse (command));
252 char devname[20];
253 char mountpoint[32];
255 unsigned l = strlen (test);
256 unsigned x = 0;
257 while (x < l) {
258 if (test[x] == ' ')
259 break;
260 x ++;
263 memcpy (devname, test, x);
264 devname[x] = '\0';
265 strcpy (mountpoint, argparse (test));
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 (test, argparse (command));
328 cd (test);
330 return 1;
333 unsigned command_ls (char *command, unsigned len)
335 strcpy (test, argparse (command));
337 ls (test);
339 return 1;
342 unsigned command_cat (char *command, unsigned len)
344 strcpy (test, argparse (command));
346 cat (test);
348 return 1;
351 unsigned command_cp (char *command, unsigned len)
353 strcpy (test, argparse (command));
355 unsigned l = strlen (test);
356 unsigned p = 0;
358 while (l) {
359 if (test[l] == ' ') {
360 test[l] = '\0';
361 p = l;
364 l --;
367 if (!p) {
368 printf ("Syntax:\ncp <source> <destination>\n");
369 return 0;
372 cp (test, test+p+1);
374 return 1;
378 unsigned command_rm (char *command, unsigned len)
380 strcpy (test, argparse (command));
382 rm (test);
384 return 1;
387 unsigned command_mkdir (char *command, unsigned len)
389 strcpy (test, argparse (command));
391 mkdir (test);
393 return 1;
396 unsigned command_touch (char *command, unsigned len)
398 strcpy (test, argparse (command));
400 touch (test);
402 return 1;
405 unsigned command_exec (char *command, unsigned len)
407 if (!strncmp ("./", command, 2))
408 strcpy (test, command+2);
409 else
410 strcpy (test, argparse (command));
412 unsigned arg_len = strlen (argparse (test));
413 char arg[arg_len+1];
414 strcpy (arg, argparse (test));
415 arg[arg_len] = '\0';
417 unsigned test_len = strlen (test);
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 (test [test_len-(arg_len+1)] == ' ')
429 test [test_len-(arg_len+1)] = '\0';
430 else
431 test [test_len-arg_len] = '\0';
433 vfs_content_t content;
434 /* read file data from filesystem */
435 int ret = vfs_read (test, strlen (test), &content);
437 if (ret < 0) {
438 printf ("%s: command not found\n", test);
439 DPRINT (DBG_CMDS, "ERROR -> vfs_read () - '%s'\n", test);
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, test, 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", test);
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", test);
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 image of elf-bss section */
542 //flush_elf (bss);
544 /* free program image */
545 swfree (bin, bin_len);
548 /* switch to next task */
549 schedule ();
551 return 1;
554 unsigned command_lsdev (char *command, unsigned len)
556 dev_display ();
558 return 1;
561 unsigned command_login (char *command, unsigned len)
563 currtty->user = NULL;
564 currtty->logged = false;
566 video_color (7, 0);
567 printf ("login: ");
569 return 1;
572 unsigned command_serialw (char *command, unsigned len)
574 dev_t *dev = dev_find ("/dev/com0");
576 if (dev) {
577 unsigned len = strlen (argparse (command));
578 char data[len+1];
579 memcpy (data, argparse (command), len);
580 data[len] = '\0';
582 dev->handler (DEV_ACT_WRITE, data, len);
584 printf ("serialw: %s\n", data);
587 return 1;
590 unsigned command_serialr (char *command, unsigned len)
592 dev_t *dev = dev_find ("/dev/com0");
594 if (dev) {
595 char data[11];
597 dev->handler (DEV_ACT_READ, data, 1);
599 printf ("serialr: %s\n", data);
602 return 1;
605 unsigned command_fdisk (char *command, unsigned len)
607 char *parm = argparse (command);
609 fdisk (parm, strlen (parm));
611 return 1;
614 unsigned command_hdcat (char *command, unsigned len)
616 tty_lock (currtty);
618 dev_t *dev = (dev_t *) dev_find (argparse (command));
620 if (dev) {
621 partition_t p;
622 p.base_io = 0;
624 switch (dev->devname[7]) {
625 case 'a':
626 p.id = 0;
627 p.base_io = 0x1F0;
628 break;
629 case 'b':
630 p.id = 1;
631 p.base_io = 0x1F0;
632 break;
633 case 'c':
634 p.id = 0;
635 p.base_io = 0x170;
636 break;
637 case 'd':
638 p.id = 1;
639 p.base_io = 0x170;
640 break;
643 int c, d = 0;
644 unsigned char block[512];
646 printf ("dev: %s\n", dev->devname);
648 while (!key_pressed (1)) {
649 if (key_pressed (72) == 1) {
650 printf ("##block: %d\n", d);
651 dev->handler (DEV_ACT_READ, &p, block, "", d);
653 for (c = 0; c < 512; c ++)
654 printf ("%c", (unsigned) block[c]);
655 d ++;
658 usleep (5);
660 } else
661 printf ("Please specify block device, example: hdcat /dev/hda\n");
663 tty_unlock (currtty);
665 return 1;
668 unsigned command_free (char *command, unsigned len)
670 util_cmdfree ();
672 return 1;
675 unsigned command_top (char *command, unsigned len)
677 tty_lock (currtty);
679 proc_top ();
681 tty_unlock (currtty);
683 return 1;
686 unsigned command_date (char *command, unsigned len)
688 tm *t = rtc_getcurrtime ();
690 printf ("%02u:%02u:%02u, %u.%u.%u\n",
691 t->tm_hour, t->tm_min, t->tm_sec, t->tm_mday, t->tm_mon, t->tm_year);
693 return 1;
697 Tone Frequency (Hz)
698 C 130.8
699 D 146.8
700 E 164.8
701 F 174.6
702 G 196.0
703 A 220.0
704 H 246.9
706 C 261.6
707 D 293.7
708 E 329.6
709 F 349.2
710 G 392.0
711 A 440.0
712 H 493.9
714 C 523.3
715 D 587.3
716 E 659.3
717 F 698.5
718 G 784.0
719 A 880.0
720 H 987.8
722 C 1046.5
723 D 1174.7
724 E 1318.5
725 F 1396.9
726 G 1568.0
727 A 1760.0
728 H 1975.5
731 void play_tone (dev_t *dev, char tone, char type, char time)
733 unsigned t = 2;
735 switch (type) {
736 case '1':
737 t = 1;
738 break;
739 case '2':
740 t = 2;
741 break;
742 case '3':
743 t = 3;
744 break;
745 case '4':
746 t = 4;
747 break;
751 unsigned tt = 1;
753 printf ("play_tone (.., %c, %d, %d);\n", tone, t, tt);
755 switch (tone) {
756 case 'c':
757 dev->handler (DEV_ACT_PLAY, 131*t);
758 break;
759 case 'd':
760 dev->handler (DEV_ACT_PLAY, 147*t);
761 break;
762 case 'e':
763 dev->handler (DEV_ACT_PLAY, 165*t);
764 break;
765 case 'f':
766 dev->handler (DEV_ACT_PLAY, 175*t);
767 break;
768 case 'g':
769 dev->handler (DEV_ACT_PLAY, 196*t);
770 break;
771 case 'a':
772 dev->handler (DEV_ACT_PLAY, 220*t);
773 break;
774 case 'h':
775 dev->handler (DEV_ACT_PLAY, 247*t);
776 break;
777 case 'C':
778 dev->handler (DEV_ACT_PLAY, 136*t);
779 break;
780 case 'F':
781 dev->handler (DEV_ACT_PLAY, 188*t);
782 break;
785 switch (time) {
786 case '0':
787 timer_wait (2500);
788 break;
789 case '1':
790 timer_wait (1250);
791 break;
792 case '2':
793 timer_wait (625);
794 break;
799 printf ("end: play_tone ();\n");
801 dev->handler (DEV_ACT_PLAY, 0);
804 void play_song (dev_t *dev, char *data)
806 printf ("play_song ();\n");
807 unsigned i = 0, l = strlen (data);
808 unsigned time = 1;
809 while (i != l) {
810 switch (data[i]) {
811 printf ("switch ()\n");
812 case 'p':
814 switch (data[i+1]) {
815 case '0':
816 timer_wait (2500);
817 break;
818 case '1':
819 timer_wait (1250);
820 break;
821 case '2':
822 timer_wait (625);
823 break;
827 i ++;
828 break;
829 case ' ':
830 break;
831 default:
832 play_tone (dev, data[i], data[i+1], data[i+2]);
833 i += 2;
834 break;
837 i ++;
842 unsigned command_spk (char *command, unsigned len)
844 unsigned int freq = 20;
846 dev_t *dev = dev_find ("/dev/pcspk");
848 if (dev) {
850 We wish you a merry christmas v1.0
852 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)
855 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");
857 timer_wait (2000);
860 We wish you a merry christmas v3.0
862 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)
865 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");
867 timer_wait (2000);
870 Dallas v2.0
871 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)
873 /* 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");*/
875 //timer_wait (2000);
878 return 1;
881 extern bool bus_pci_acthandler (unsigned act, char *block, unsigned block_len);
882 extern netif_t netif_list;
883 extern void ext2_dump ();
884 void thread_test ()
886 while (1) {
888 kprintf ("hu\n");
890 unsigned long timer_start = timer_ticks + ((unsigned) 1 * 1000);
892 while (timer_start > timer_ticks)
893 schedule ();
897 extern unsigned start;
898 extern unsigned end;
899 extern bool ide_acthandler (unsigned act, partition_t *p, char *block, char *more, int n);
900 unsigned command_test (char *command, unsigned len)
902 partition_t *p = partition_find ("/dev/hda0");
904 if (p)
905 mount (p, "", "/mnt/hdd/");
906 else
907 return 0;
909 cd ("/mnt/hdd");
911 return 1;
913 extern char *vgafb;
914 extern char *vgadb;
915 unsigned command_test2 (char *command, unsigned len)
917 partition_t *p = partition_find ("/dev/hdc0");
919 if (p)
920 mount (p, "", "/mnt/cdrom/");
921 else
922 return 0;
924 cd ("/mnt/cdrom");
926 /* tty_t *tty = tty_find ("tty0");
928 if (!tty)
929 return 0;
930 char str[513];
931 int r = tty_read (tty, str, 512);
933 if (r > 0) {
934 printf ("str: r: %d\n", r);
935 int i;
936 for (i = 0; i < r; i ++)
937 tty_putch (str[i]);
941 /* int ret = vfs_read ("wav", 3);
943 if (ret < 0) {
944 printf ("chyba\n");
945 return 0;
948 cache_t *cache = cache_read ();
950 unsigned char *p = (unsigned char *) &cache->data;
952 unsigned bin_len = cache->limit;
954 unsigned char *bin = (unsigned char *) 0x400000 - 44;
956 memcpy (bin, p, bin_len);
958 typedef struct {
959 unsigned chunkid;
960 unsigned chunksize;
961 unsigned format;
962 } riff_head_t;
964 typedef struct {
965 unsigned subchunk1id;
966 unsigned subchunk1size;
967 unsigned short audioformat;
968 unsigned short numchannels;
969 unsigned samplerate;
970 unsigned byterate;
971 unsigned short blockalign;
972 unsigned short bitspersample;
973 } fmt_head_t;
975 typedef struct {
976 unsigned subchunk2id;
977 unsigned subchunk2size;
978 } data_head_t;
980 riff_head_t *r = (riff_head_t *) bin;
981 fmt_head_t *fmt = (fmt_head_t *) ((char *) bin + sizeof (riff_head_t));
982 data_head_t *data = (data_head_t *) ((char *) bin + sizeof (riff_head_t) + sizeof (fmt_head_t));
984 printf ("riff_head_t 0x%x : %d : 0x%x\n", r->chunkid, r->chunksize, r->format);
985 printf ("fmt_head_t %d : %d : %d : %d : %d : %d\n", fmt->audioformat, fmt->subchunk1size, fmt->numchannels, fmt->samplerate, fmt->byterate, fmt->bitspersample);
986 printf ("data_head_t 0x%x : %d\n", data->subchunk2id, data->subchunk2size);
987 //memset (bin+44, 0, bin_len-44);
988 //dev->handler (DEV_ACT_WRITE, bin+44, bin_len-44);
990 snd_cfg_t cfg;
991 cfg.dev = "/dev/ac97";
992 cfg.rate = fmt->samplerate;
993 cfg.format = SOUND_FORMAT_S16;
994 cfg.channels = fmt->numchannels;
996 snd_audio_t *aud = audio_open (&cfg);
998 if (!aud)
999 return 0;
1001 audio_write (aud, bin+44, (bin_len-44));
1003 //swfree (bin, bin_len);*/
1007 return 1;
1010 unsigned command_test3 (char *command, unsigned len)
1012 netif_t *netif = netif_findbyname ("eth0");
1013 dhcp_config_if(netif);
1015 return 1;
1018 unsigned command_test4 (char *command, unsigned len)
1022 return 1;
1025 unsigned command_mouse (char *command, unsigned len)
1027 printf ("PS/2 Mouse test\n");
1029 dev_t *dev = dev_find ("/dev/mouseps2");
1031 if (!dev)
1032 return 0;
1034 dev_mouse_t mouse;
1036 int ps2_fd = open ("/dev/mouseps2", O_RDONLY);
1038 if (!ps2_fd)
1039 return 0;
1041 printf ("dddd\n");
1043 while (!key_pressed (1)) {
1044 int r = read (ps2_fd, &mouse, sizeof (dev_mouse_t));
1046 if (!r) {
1047 kprintf ("oj oj\n");
1048 return 0;
1051 kprintf ("ps2mouse: x,y: %d, %d | 0x%x \n", mouse.pos_x, mouse.pos_y, mouse.flags);
1053 lseek (ps2_fd, 0, SEEK_SET);
1055 schedule ();
1058 return 1;
1061 unsigned command_vesa (char *command, unsigned len)
1063 if (!init_video_vesa ())
1064 printf ("vesa -> failed\n");
1066 return 1;
1069 unsigned command_kill (char *command, unsigned len)
1071 strcpy (test, argparse (currtty->shell));
1073 pid_t pid = (pid_t) atoi (test);
1075 proc_t *proc = proc_findbypid (pid);
1077 if (!proc) {
1078 printf ("kill: (%s) - No such process\n", test);
1079 return 0;
1082 /* send SIGTERM to selected process */
1083 if (!proc_signal (proc, SIGTERM))
1084 return 0;
1086 /* check for daemon */
1087 if (proc->flags & PROC_FLAG_DAEMON)
1088 if (!proc_done (proc))
1089 return 0;
1091 return 1;
1094 unsigned command_modprobe (char *command, unsigned len)
1096 unsigned arg_len = strlen (argparse (currtty->shell));
1097 char arg[arg_len+1];
1098 strcpy (arg, argparse (currtty->shell));
1099 arg[arg_len] = '\0';
1101 module_load (arg);
1103 return 1;
1106 unsigned command_lsmod (char *command, unsigned len)
1108 module_display ();
1110 return 1;
1113 #ifdef CONFIG_DRV_PCI
1114 unsigned command_lspci (char *command, unsigned len)
1116 #ifdef ARCH_i386
1117 pcidev_display ();
1118 #endif
1119 return 1;
1121 #endif
1123 unsigned command_iflist (char *command, unsigned len)
1125 iflist_display ();
1127 return 1;
1130 unsigned command_ifconfig (char *command, unsigned len)
1132 strcpy (test, argparse (command));
1133 char ifid[20];
1134 char ip[32];
1136 unsigned l = strlen (test);
1137 unsigned x = 0;
1138 while (x < l) {
1139 if (test[x] == ' ')
1140 break;
1141 x ++;
1144 memcpy (ifid, test, x);
1145 ifid[x] = '\0';
1147 strcpy (ip, argparse (test));
1148 unsigned y = strlen (ip);
1150 netif_t *netif = netif_findbyname (ifid);
1152 if (!netif) {
1153 printf ("ifconfig -> bad network interface name, example: eth0\n");
1154 return 0;
1157 net_ipv4 ipv4;
1158 net_ipv6 ipv6;
1159 if (ipv4 = net_proto_ip_convert (ip)) {
1160 netif_ip_addr (netif, ipv4, IF_CFG_TYPE_STATIC);
1162 printf ("%s: IPv4 ", ifid);
1163 net_proto_ip_print (ipv4);
1164 printf (" - OK\n");
1165 } else if (net_proto_ipv6_convert (ipv6, ip)) {
1166 netif_ipv6_addr (netif, ipv6, IF_CFG_TYPE_STATIC);
1168 printf ("%s: IPv6 ", ifid);
1169 net_proto_ipv6_print (ipv6);
1170 printf (" - OK\n");
1171 } else {
1172 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");
1173 return 0;
1176 return 1;
1179 unsigned command_ifroute (char *command, unsigned len)
1181 strcpy (test, argparse (command));
1182 char ifid[20];
1183 char ip[32];
1185 unsigned l = strlen (test);
1186 unsigned x = 0;
1187 while (x < l) {
1188 if (test[x] == ' ')
1189 break;
1190 x ++;
1193 memcpy (ifid, test, x);
1194 ifid[x] = '\0';
1196 strcpy (ip, argparse (test));
1197 unsigned y = strlen (ip);
1199 netif_t *netif = netif_findbyname (ifid);
1201 if (!netif) {
1202 printf ("ifroute -> bad network interface name, example: eth0\n");
1203 return 0;
1206 unsigned char a;
1207 unsigned char b;
1208 unsigned char c;
1209 unsigned char d;
1211 unsigned g = 0;
1212 unsigned i = 0;
1214 unsigned h[4];
1216 while (i < y) {
1217 if (ip[i] == '.') {
1218 ip[i] = '\0';
1219 h[g] = i+1;
1220 g ++;
1223 i ++;
1226 if (g != 3) {
1227 printf ("ifroute -> bad ip address format, example: 192.168.1.254\n");
1228 return 0;
1231 a = atoi (ip);
1232 b = atoi (ip+h[0]);
1233 c = atoi (ip+h[1]);
1234 d = atoi (ip+h[2]);
1236 printf ("%s->gw: %d.%d.%d.%d - OK\n", ifid, a, b, c, d);
1238 return netif_gw_addr (netif, NET_IPV4_TO_ADDR (a, b, c, d));
1241 unsigned command_dnsconfig (char *command, unsigned len)
1243 strcpy (test, argparse (currtty->shell));
1245 if (!test)
1246 return 0;
1248 unsigned l = strlen (test);
1250 if (!l) {
1251 printf ("dnsconfig -> your domain name server is: ");
1252 net_proto_ip_print (dns_addr_get ());
1253 printf ("\n");
1255 return 0;
1258 printf ("dnsconfig: %s - OK\n", test);
1260 net_ipv4 dns = net_proto_ip_convert (test);
1262 if (!dns)
1263 return 0;
1265 dns_addr (dns);
1267 return 1;
1270 unsigned command_dhcpcl (char *command, unsigned len)
1272 strcpy (test, argparse (currtty->shell));
1274 if (!test)
1275 return 0;
1277 unsigned l = strlen (test);
1279 if (!l) {
1280 printf ("dhcpcl -> please specify interface\n");
1281 return 0;
1284 netif_t *netif = netif_findbyname (test);
1286 if (!netif) {
1287 printf ("dhcpcl -> bad network interface name, example: eth0\n");
1288 return 0;
1291 unsigned ret = dhcp_config_if (netif);
1293 if (ret < 0) {
1294 switch (ret) {
1295 case DHCP_OUT_OF_MEMORY:
1296 printf ("dhcpcl -> out of memory\n");
1297 break;
1298 case DHCP_SOCKET_FAILED:
1299 case DHCP_CONNECT_FAILED:
1300 case DHCP_CANT_SEND_REQUEST:
1301 printf ("dhcpcl -> problem with network layer\n");
1302 break;
1303 case DHCP_CANT_RECV_RESPONSE:
1304 printf ("dhcpcl -> can't receive response\n");
1305 break;
1306 case DHCP_BAD_PACKET:
1307 printf ("dhcpcl -> bad packet received\n");
1308 break;
1309 case DHCP_SRV_DIDNT_UNDERSTAND:
1310 printf ("dhcpcl -> server didn't understand\n");
1311 break;
1313 } else {
1314 printf ("dhcpcl -> interface %s successfully configured\n");
1318 unsigned command_tunconfig (char *command, unsigned len)
1320 strcpy (test, argparse (currtty->shell));
1322 if (!test)
1323 return 0;
1325 unsigned l = strlen (test);
1327 if (!l) {
1328 printf ("tunconfig -> wrong syntax !\ntunconfig <ipv4tunnel> for enable or tunconfig 0 for disable\n");
1330 return 0;
1333 /* you can disable this service over character '0' as parameter */
1334 if (l == 1 && test[0] == '0') {
1335 tun6_addr (0);
1337 printf ("tunconfig: disabled - OK\n");
1339 return 1;
1342 /* classical ipv4 address from tunnel server */
1343 net_ipv4 tunnel = net_proto_ip_convert (test);
1345 if (!tunnel) {
1346 printf ("tunconfig -> wrong syntax !\nExample: tunconfig 216.66.80.30\n");
1348 return 0;
1351 tun6_addr (tunnel);
1353 printf ("tunconfig: ::%s - OK\n", test);
1355 return 1;
1359 unsigned command_hostname (char *command, unsigned len)
1361 strcpy (test, argparse (currtty->shell));
1363 if (!test)
1364 return 0;
1366 unsigned l = strlen (test);
1368 if (!l) {
1369 printf ("%s\n", hostname_get ());
1371 return 0;
1374 printf ("hostname: %s - OK\n", test);
1376 hostname_set (test);
1378 return 1;
1381 unsigned command_kbdmap (char *command, unsigned len)
1383 strcpy (test, argparse (currtty->shell));
1385 if (!test)
1386 return 0;
1388 unsigned ret = keyboard_setlayout (test);
1390 if (ret)
1391 printf ("Keyboard layout was changed to '%s'\n", test);
1393 return ret;
1396 unsigned command_mkzexfs (char *command, unsigned len)
1398 #ifdef ARCH_i386
1399 strcpy (test, argparse (currtty->shell));
1401 partition_t *p = partition_find (test);
1403 if (!p)
1404 return 0;
1406 mkzexfs (p);
1407 #endif
1408 return 1;
1411 unsigned command_mkext2 (char *command, unsigned len)
1413 #ifdef ARCH_i386
1414 strcpy (test, argparse (currtty->shell));
1416 if (!test)
1417 return 0;
1419 partition_t *p = partition_find ("/dev/hda0");
1421 mkext2 (p);
1422 #endif
1423 return 1;
1426 unsigned command_ping (char *command, unsigned len)
1429 strcpy (test, argparse (currtty->shell));
1431 if (!test)
1432 return 0;
1434 netif_t *netif = netif_findbyname ("eth0");
1436 if (!netif) {
1437 printf ("ping -> network interface does not exists\n");
1438 return 0;
1441 unsigned char a;
1442 unsigned char b;
1443 unsigned char c;
1444 unsigned char d;
1446 unsigned g = 0;
1447 unsigned i = 0;
1448 unsigned y = strlen (test);
1450 if (!y) {
1451 printf ("ping -> wrong syntax, please specify address\n");
1452 return 0;
1455 unsigned h[4];
1457 while (i < y) {
1458 if (test[i] == '.') {
1459 test[i] = '\0';
1460 h[g] = i+1;
1461 g ++;
1464 i ++;
1467 net_ipv4 target = 0;
1469 if (g != 3) {
1470 strcpy (test, argparse (currtty->shell));
1472 if (dns_cache_get (test, &target, 4) != 1)
1473 if (dns_send_request (test, &target, 4) != 1) {
1474 printf ("ping -> bad hostname or ip address format, example: 192.168.1.1\n");
1475 return 0;
1477 } else {
1478 a = atoi (test);
1479 b = atoi (test+h[0]);
1480 c = atoi (test+h[1]);
1481 d = atoi (test+h[2]);
1483 target = NET_IPV4_TO_ADDR (a, b, c, d);
1485 strcpy (test, argparse (currtty->shell));
1488 char address[20];
1489 net_proto_ip_convert2 (target, address);
1491 i = 0;
1493 while (i < 8) {
1494 unsigned long time_a = timer_ticks;
1495 unsigned ret = net_proto_icmp_ping (netif, target);
1496 if (ret) {
1497 printf ("ping -> %s (%s) - %ums\n", test, address, (timer_ticks - time_a));
1498 } else {
1499 printf ("ping -> Host %s is unreachable\n", test);
1500 break;
1503 i ++;
1506 return 1;
1509 unsigned command_ping6 (char *command, unsigned len)
1511 strcpy (test, argparse (currtty->shell));
1513 if (!test)
1514 return 0;
1516 netif_t *netif = netif_findbyname ("eth0");
1518 if (!netif) {
1519 printf ("ping6 -> network interface does not exists\n");
1520 return 0;
1523 unsigned short a;
1524 unsigned short b;
1525 unsigned short c;
1526 unsigned short d;
1527 unsigned short e;
1528 unsigned short f;
1529 unsigned short g;
1530 unsigned short h;
1532 unsigned j = 0;
1533 unsigned i = 0;
1534 unsigned y = strlen (test);
1536 if (!y) {
1537 printf ("ping6 -> wrong syntax, please specify ipv6 address\n");
1538 return 0;
1541 unsigned k[8];
1543 while (i < y) {
1544 if (test[i] == ':') {
1545 test[i] = '\0';
1546 k[j] = i+1;
1547 j ++;
1550 i ++;
1553 net_ipv6 target;
1555 if (j != 7) {
1556 strcpy (test, argparse (currtty->shell));
1558 if (dns_cache_get (test, &target, 16) != 1)
1559 if (dns_send_request (test, &target, 16) != 1) {
1560 printf ("ping6 -> bad hostname or ip address format, example: 2001:4d3:8fe:2:0:0:0:1\n");
1561 return 0;
1563 } else {
1564 char *endptr;
1566 a = strtol (test, &endptr, 16);
1567 b = strtol (test+k[0], &endptr, 16);
1568 c = strtol (test+k[1], &endptr, 16);
1569 d = strtol (test+k[2], &endptr, 16);
1570 e = strtol (test+k[3], &endptr, 16);
1571 f = strtol (test+k[4], &endptr, 16);
1572 g = strtol (test+k[5], &endptr, 16);
1573 h = strtol (test+k[6], &endptr, 16);
1575 NET_IPV6_TO_ADDR (target, a, b, c, d, e, f, g, h);
1577 //printf ("ipv6: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", a, b, c, d, e, f, g, h);
1579 strcpy (test, argparse (currtty->shell));
1582 char address[40];
1583 //net_proto_ipv6_convert2 (target, address);
1585 i = 0;
1587 while (i < 8) {
1588 unsigned long time_a = timer_ticks;
1589 unsigned ret = net_proto_icmp6_ping (netif, target);
1590 if (ret) {
1591 printf ("ping6 -> %s (%s) - %ums\n", test, test, (timer_ticks - time_a));
1592 } else {
1593 printf ("ping6 -> Host %s is unreachable\n", test);
1594 break;
1597 i ++;
1600 return 1;
1603 unsigned command_netexec (char *command, unsigned len)
1605 strcpy (test, argparse (command));
1607 if (!test)
1608 return 0;
1610 if (!strlen (test)) {
1611 printf ("Syntax:\nnetexec <ip_of_tftp> <filename> [parameters]\n");
1612 return 0;
1615 unsigned arg_len = strlen (argparse (test));
1616 char arg[arg_len+1];
1617 strcpy (arg, argparse (test));
1618 arg[arg_len] = '\0';
1620 unsigned test_len = strlen (test);
1621 test [test_len-arg_len] = '\0';
1623 if (test[test_len-arg_len-1] == ' ') {
1624 test[test_len-arg_len-1] = '\0';
1625 test_len --;
1628 char file[arg_len+1];
1629 unsigned file_len = 0;
1630 memcpy (file, arg, arg_len);
1631 while (file_len < arg_len) {
1632 if (file[file_len] == ' ') {
1633 file[file_len] = '\0';
1634 break;
1636 file_len ++;
1639 if (file[arg_len] != '\0')
1640 file[arg_len] = '\0';
1642 unsigned l = file_len;
1644 unsigned ret = tftp_client_connect (test, 69, file, &l);
1646 if (!ret) {
1647 printf ("tftp -> something go wrong !\n");
1648 return 0;
1651 printf ("tftp -> file length: %ubytes\t\t[OK]\n", l);
1653 char *bin = (unsigned char *) swmalloc (sizeof (char) * (l+1));
1655 if (!bin)
1656 return 0;
1658 int ret2 = tftp_client_read (ret, bin, l);
1660 if (!ret2) {
1661 printf ("tftp -> tftp_client_read () error\n");
1662 swfree (bin, l);
1663 return 0;
1666 unsigned entry, code, data, data_off, bss;
1667 int err = exec_elf (bin, &entry, &data, &data_off, &bss);
1669 if (err != NULL) {
1670 printf ("ERROR -> invalid ELF exec\n");
1671 return 0;
1674 tty_lock (currtty);
1675 timer_wait (1);
1676 /* create process */
1677 proc_t *proc = proc_create (currtty, "netexec", entry);
1678 timer_wait (1);
1681 /* check for & character as app parameter */
1682 /* unsigned d = 0;
1683 while (d < arg_len) {
1684 if (arg[d] == '&') {
1685 if (proc_flag_set (proc, PROC_FLAG_DAEMON))
1686 printf ("%s: started as daemon\n", test);
1688 arg[d] = '\0';
1690 if (arg_len > 2 && arg[d-1] == ' ') {
1691 arg[d-1] = '\0';
1692 arg_len --;
1695 arg_len --;
1697 break;
1699 d ++;
1702 if (!proc) {
1703 printf ("ERROR -> Invalid process: %s\n", test);
1704 return 0;
1707 timer_wait (1);
1709 /* save process information to structure for later use */
1710 proc->start = (unsigned) bin;
1711 proc->code = entry;
1712 proc->data = data;
1713 proc->data_off = data_off;
1714 proc->bss = bss;
1715 proc->end = (unsigned) (bin + l);
1717 timer_wait (1);
1719 /* well, set argument for out app */
1720 proc_arg_set (proc, arg+file_len+1, arg_len-file_len-1);
1722 proc_vmem_map (proc);
1724 timer_wait (1);
1726 /* Is this process started as daemon ? */
1727 /* wait here, until pid != 0*/
1728 if (!(proc->flags & PROC_FLAG_DAEMON))
1729 while (proc->pid)
1730 schedule ();
1732 timer_wait (1);
1734 /* enable interrupts */
1735 int_enable ();
1737 /* unlock tty console - user can write in */
1738 tty_unlock (proc->tty);
1740 _curr_task = proc->tty->task; // this is pretty needed for correct return to our tty task
1742 if (!(proc->flags & PROC_FLAG_DAEMON)) {
1743 if (!proc_done (proc))
1744 while (1) // NOTE: heh, this is good stuff :P
1745 schedule ();
1747 /* free image of app */
1748 //flush_elf ();
1749 swfree (bin, l);
1752 /* switch to next task */
1753 schedule ();
1755 sclose (ret);
1757 return 1;
1760 unsigned command_netcp (char *command, unsigned len)
1762 strcpy (test, argparse (command));
1764 if (!test)
1765 return 0;
1767 if (!strlen (test)) {
1768 printf ("Syntax:\nnetcp <ip_of_tftp> <tftp_file> <output_file>\n");
1769 return 0;
1772 unsigned arg_len = strlen (argparse (test));
1773 char arg[arg_len+1];
1774 strcpy (arg, argparse (test));
1775 arg[arg_len] = '\0';
1777 unsigned test_len = strlen (test);
1778 test [test_len-arg_len] = '\0';
1780 if (test[test_len-arg_len-1] == ' ') {
1781 test[test_len-arg_len-1] = '\0';
1782 test_len --;
1785 char file[arg_len+1];
1786 unsigned file_len = 0;
1787 memcpy (file, arg, arg_len);
1788 while (file_len < arg_len) {
1789 if (file[file_len] == ' ') {
1790 file[file_len] = '\0';
1791 break;
1793 file_len ++;
1796 if (file[arg_len] != '\0')
1797 file[arg_len] = '\0';
1799 unsigned l = file_len;
1801 unsigned ret = tftp_client_connect (test, 69, file, &l);
1803 if (!ret) {
1804 printf ("tftp -> something go wrong !\n");
1805 return 0;
1808 printf ("tftp -> file length: %ubytes\t\t[OK]\n", l);
1810 char *bin = (unsigned char *) swmalloc (sizeof (char) * (l+1));
1812 if (!bin)
1813 return 0;
1815 int ret2 = tftp_client_read (ret, bin, l);
1817 if (!ret2) {
1818 printf ("tftp -> tftp_client_read () error\n");
1819 swfree (bin, l);
1820 return 0;
1823 sclose (ret);
1825 char *output = argparse (arg);
1827 if (!output) {
1828 swfree (bin, l);
1829 printf ("ERROR -> output file name is empty\n");
1830 return 0;
1833 int fd = open (output, O_WRONLY | O_CREAT);
1835 if (!fd) {
1836 swfree (bin, l);
1837 printf ("ERROR -> something go wrong with %s\n", output);
1838 return 0;
1841 ret = write (fd, bin, l);
1843 if (!ret) {
1844 swfree (bin, l);
1845 printf ("ERROR -> File '%s' could'nt be writed\n", output);
1848 return 1;
1852 unsigned command_znfscl (char *command, unsigned len)
1854 #ifdef ARCH_i386
1855 strcpy (test, argparse (command));
1857 if (!strlen (test)) {
1858 printf ("Syntax:\nznfscl <ip_of_znfs>\n");
1859 return 0;
1862 dev_t *dev = dev_find ("/dev/vbd");
1864 if (dev) {
1865 printf ("znfscl: You are already connected to znfs server\n");
1866 return 0;
1869 dev = (dev_t *) dev_register ("vbd", "Virtual Block Device", DEV_ATTR_BLOCK, (dev_handler_t *) &ide_acthandler);
1871 if (!dev)
1872 return 0;
1874 partition_t *p = partition_add (dev, fs_supported ("znfs"), 0);
1876 if (!p)
1877 return 0;
1879 /* connect to specified address */
1880 if (znfs_init (p, test) != 1) {
1881 printf ("znfs: Wrong znfs server\n");
1882 return 0;
1885 char str[16];
1887 sprintf (str, "/mnt/%s", p->name+5);
1889 mkdir (str);
1891 mount (p, "", str);
1893 printf ("znfs: You are succefully connected to ZNFS server '%s'\nZNFS was mounted into '%s'\n", test, str);
1894 #else
1895 printf ("ERROR -> this architecture is not supported yet\n");
1896 #endif
1897 return 1;
1900 void task_savemode ()
1902 /* disable cpu for cycle */
1903 for (;; schedule ())
1904 arch_cpu_hlt ();
1907 unsigned command_savemode (char *command, unsigned len)
1909 task_t *task = (task_t *) task_create ("savemode", (unsigned) &task_savemode, 16);
1911 if (!task) {
1912 printf ("ERROR -> Save mode thread was'nt created\n");
1913 return 0;
1916 printf ("Save mode was succefully started\n");
1918 return 1;
1921 unsigned command_cpuinfo (char *command, unsigned len)
1923 printf ("Processor info:\n");
1925 cat ("/proc/cpuinfo");
1927 return 1;
1930 unsigned command_adm (char *command, unsigned len)
1932 printf ("Automatic Device Mounter - init ()\n");
1934 init_adm ();
1936 return 1;
1939 unsigned command_ttyctrl (char *command, unsigned len)
1941 char *parm = argparse (command);
1943 if (!strlen (parm)) {
1944 printf ("ttyctrl <operation> [parameter]\n\toperations: add; list; change <ttyid>\n");
1945 return 0;
1948 if (!strncmp (parm, "add", 3)) {
1949 tty_t *tty = tty_create ();
1951 if (!tty)
1952 return 0;
1954 video_color (7, 0);
1956 tty_write (tty, "\nlogin: ", 8);
1958 printf ("New tty '%s' was created\n", tty->name);
1959 } else
1960 if (!strncmp (parm, "list", 4)) {
1961 tty_listview ();
1962 } else
1963 if (!strncmp (parm, "change", 6)) {
1964 tty_t *tty = tty_find ((char *) argparse (parm));
1966 if (!tty)
1967 return 0;
1969 tty_change (tty);
1971 printf ("\n");
1974 return 1;
1977 unsigned command_cmd (char *command, unsigned len)
1979 unsigned s = 0;
1980 unsigned e = 0;
1982 if (!strlen (argparse (command))) {
1983 printf ("cmd <command1>;<command2>;...;\n");
1984 return 0;
1987 char *cmds = kmalloc (len - 3);
1989 if (!cmds)
1990 return 0;
1992 memcpy (cmds, command + 4, len - 4);
1993 cmds[len - 4] = '\0';
1995 unsigned i;
1996 for (i = 1; i < len - 4; i ++) {
1997 if (cmds[i] == ';') {
1998 cmds[i] = '\0';
1999 e = i-1;
2001 command_parser (cmds+s, e-s+1);
2003 s = i+1;
2007 kfree (cmds);
2009 return 1;
2012 /*************************************************************\
2013 | COMMAND's UTILS |
2014 \*************************************************************/
2016 unsigned command_parser (char *command, unsigned len)
2018 /* find first non-space in command */
2019 int i = 0;
2020 for (i = 0; i < len; i ++) {
2021 if (command[i] != ' ')
2022 break;
2024 command_t *ctx;
2026 /* try to find the command */
2027 for (ctx = command_list.next; ctx != &command_list; ctx = ctx->next) {
2028 if (!cstrcmp (ctx->name, command+i)) {
2029 ctx->handler (command+i, len-i); /* execute context function */
2030 return 1;
2034 printf ("%s: command not found\n", command);
2036 return 0;
2039 void commands (int i)
2041 // currtty->shell_len has been set to zero already
2042 command_parser (currtty->shell, strlen(currtty->shell));
2045 unsigned command_register (char *name, char *desc, command_handler_t *handler, unsigned flags)
2047 command_t *c;
2048 /* we dont want commands with same name */
2049 for (c = command_list.next; c != &command_list; c = c->next)
2050 if (!strcmp(c->name, name))
2051 return 0;
2053 command_t *ctx;
2055 // alloc and init context
2056 ctx = (command_t *) kmalloc (sizeof (command_t));
2058 if (!ctx)
2059 return 0;
2061 unsigned name_len = strlen (name);
2062 ctx->name = (char *) kmalloc (sizeof (char) * name_len +1);
2064 if (!ctx->name)
2065 return 0;
2067 memcpy (ctx->name, name, name_len);
2068 ctx->name[name_len] = '\0';
2070 unsigned desc_len = strlen (desc);
2071 ctx->desc = (char *) kmalloc (sizeof (char) * desc_len +1);
2073 if (!ctx->desc)
2074 return 0;
2076 memcpy (ctx->desc, desc, desc_len);
2077 ctx->desc[desc_len] = '\0';
2079 ctx->handler = handler;
2080 ctx->flags = flags;
2082 ctx->next = &command_list;
2083 ctx->prev = command_list.prev;
2084 ctx->prev->next = ctx;
2085 ctx->next->prev = ctx;
2087 return 1;
2090 unsigned command_unregister (char *name)
2092 command_t *ctx;
2094 for (ctx = command_list.next; ctx != &command_list; ctx = ctx->next) {
2095 if (!strcmp(ctx->name, name)) {
2096 ctx->next->prev = ctx->prev;
2097 ctx->prev->next = ctx->next;
2099 return 1;
2103 return 0;
2106 /*************************************************************\
2107 | INITIALIZATION OF COMMANDS |
2108 \*************************************************************/
2110 unsigned int init_commands ()
2112 command_list.next = &command_list;
2113 command_list.prev = &command_list;
2115 // reg commands
2116 command_register ("help", "Displays list of available commands", &command_help, 0);
2117 command_register ("hdd", "Detect HDD", &command_hdd, 0);
2118 command_register ("reboot", "Reboot computer", &command_reboot, 0);
2119 command_register ("halt", "Shutdown computer", &command_halt, 0);
2120 command_register ("tasks", "Print all tasks", &command_tasks, 0);
2121 command_register ("ps", "Print all process", &command_ps, 0);
2122 command_register ("uptime", "Print uptime in seconds", &command_uptime, 0);
2123 command_register ("version", "Displays a version of system", &command_version, 0);
2124 command_register ("debug", "Change to developer mode", &command_debug, 0);
2125 command_register ("mount", "Mount device to selected directory", &command_mount, 0);
2126 command_register ("umount", "Unmount mounted directory", &command_umount, 0);
2127 command_register ("env", "Displays all env variables", &command_env, 0);
2128 command_register ("cd", "Change directory", &command_cd, 0);
2129 command_register ("ls", " Displays files in current directory", &command_ls, 0);
2130 command_register ("cat", "Displays text in selected file", &command_cat, 0);
2131 command_register ("cp", "Copy a file", &command_cp, 0);
2132 command_register ("rm", "Remove a file", &command_rm, 0);
2133 command_register ("mkdir", "Create a directory", &command_mkdir, 0);
2134 command_register ("touch", "Create a file", &command_touch, 0);
2135 command_register ("exec", "Execute a selected program", &command_exec, 0);
2136 command_register ("lsdev", "Displays found devices", &command_lsdev, 0);
2137 command_register ("login", "Login as another user", &command_login, 0);
2138 command_register ("fdisk", "Partition table manipulator", &command_fdisk, 0);
2139 command_register ("hdcat", "Read selected block of data from drive", &command_hdcat, 0);
2140 command_register ("free", "Display amount of free and used memory", &command_free, 0);
2141 command_register ("top", "Stats of cpu usage", &command_top, 0);
2142 command_register ("date", "Show current date and time", &command_date, 0);
2143 command_register ("xmastime", "play noels on pc-speaker", &command_spk, 0);
2144 command_register ("test", "Some test", &command_test, 0);
2145 command_register ("test2", "Some test", &command_test2, 0);
2146 //command_register ("test3", "Some test", &command_test3, 0);
2147 //command_register ("testd", "Some test", &command_test4, 0);
2148 //command_register ("vesa", "Some test", &command_vesa, 0);
2149 command_register ("kill", "Send a signal to process", &command_kill, 0);
2150 command_register ("modprobe", "program to add modules from the kernel", &command_modprobe, 0);
2151 command_register ("lsmod", "program to show the status of modules", &command_lsmod, 0);
2152 #ifdef CONFIG_DRV_PCI
2153 command_register ("lspci", "list all pci devices", &command_lspci, 0);
2154 #endif
2155 command_register ("mouse", "ps2 mouse test", &command_mouse, 0);
2156 command_register ("iflist", "Show network interface", &command_iflist, 0);
2157 command_register ("ifconfig", "Configure network interface", &command_ifconfig, 0);
2158 command_register ("ifroute", "Configure gateway address", &command_ifroute, 0);
2159 command_register ("dnsconfig", "Configure domain name server address", &command_dnsconfig, 0);
2160 command_register ("dhcpcl", "Configure interface using DHCP", &command_dhcpcl, 0);
2161 #ifdef CONFIG_PROTO_TUN6
2162 command_register ("tunconfig", "Configure IPv6 tunnel server address", &command_tunconfig, 0);
2163 #endif
2164 command_register ("hostname", "Configure network hostname", &command_hostname, 0);
2165 command_register ("kbdmap", "Change keyboard layout", &command_kbdmap, 0);
2166 #ifdef CONFIG_DRV_ZEXFS
2167 command_register ("mkzexfs", "Create zexfs filesystem on hdd", &command_mkzexfs, 0);
2168 #endif
2169 #ifdef CONFIG_DRV_EXT2
2170 command_register ("mkext2", "Create ext2 filesystem on hdd", &command_mkext2, 0);
2171 #endif
2172 #ifdef CONFIG_PROTO_IPV4
2173 command_register ("ping", "Send ICMP echo request", &command_ping, 0);
2174 #endif
2175 #ifdef CONFIG_PROTO_IPV6
2176 command_register ("ping6", "Send ICMPv6 echo request", &command_ping6, 0);
2177 #endif
2178 command_register ("netexec", "Execute file from network", &command_netexec, 0);
2179 command_register ("netcp", "Copy file from network to filesystem", &command_netcp, 0);
2180 #ifdef CONFIG_DRV_ZNFS
2181 command_register ("znfscl", "Connect to znfs server - network filesystem", &command_znfscl, 0);
2182 #endif
2183 command_register ("savemode", "Make less cpu load", &command_savemode, 0);
2184 command_register ("cpuinfo", "Show CPU info", &command_cpuinfo, 0);
2186 command_register ("adm", "Automatic Device Mounter", &command_adm, 0);
2187 command_register ("ttyctrl", "TTY Console control", &command_ttyctrl, 0);
2188 command_register ("cmd", "Execute command sequence", &command_cmd, 0);
2190 return 1;