Update
[ZeXOS.git] / kernel / kernel / commands.c
blobaddfc7e84c1b0aad72827da443e55747af861315
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <partition.h>
23 #include <env.h>
24 #include <dev.h>
25 #include <fs.h>
26 #include <tty.h>
27 #include <proc.h>
29 unsigned char test[10];
31 typedef void (*void_fn_void_t)(void);
33 extern task_t task_list;
35 char *argparse (char *cmd)
37 unsigned cmd_len = strlen (cmd);
38 unsigned p = 1;
40 while (p < cmd_len) {
41 if (cmd[p] == ' ')
42 return cmd + p + 1;
44 p ++;
47 return "";
50 #define WAIT 0xFFFFFL
51 static void wait (void)
53 unsigned long wait;
55 for(wait = WAIT; wait != 0; wait--)
56 /* nothing */;
59 unsigned thread ()
61 while (1) {
62 schedule ();
63 //timer_wait (18);
64 putch ('*');
65 wait ();
68 return 1;
71 void commands (int i)
73 if (!cstrcmp ("help", currtty->shell))
75 //printf("fda [sector] Read sector from floppy drive A\n");
76 //printf("fda Read all sectors on floppy drive A\n");
77 printf ("hdd Detect HDD\n");
78 printf ("help Print help\n");
79 printf ("reboot Reboot computer\n");
80 printf ("tasks Print all tasks\n");
81 printf ("ps Print all process\n");
82 printf ("uptime Print uptime in seconds\n");
83 printf ("version Displays a version of system\n");
84 printf ("debug Change to developer mode\n");
85 printf ("mount Mount device to selected directory\n");
86 printf ("env Displays all env variables\n");
87 printf ("cd Change directory\n");
88 printf ("ls Displays files in current directory\n");
89 printf ("cat Displays text in selected file\n");
90 printf ("mkdir Create a directory\n");
91 printf ("exec Execute a selected program\n");
92 printf ("lsdev Displays found devices\n");
93 printf ("login Login as another user\n");
94 printf ("serialw Write data on rs232\n");
95 printf ("serialr Read data from rs232\n");
96 printf ("fdisk Partition table manipulator\n");
97 printf ("hdcat Read selected block of data from drive\n");
99 return;
102 if (!cstrcmp ("tasks", currtty->shell))
104 puts ("name\tpriority\n");
106 task_t *task;
107 for (task = task_list.next; task != &task_list; task = task->next)
108 printf ("%s\t%u\n", task->name, task->priority);
110 return;
113 if (!cstrcmp ("reboot", currtty->shell))
115 outportb (0x64, 0xFE);
116 while(1)
117 /* nothing */;
120 if (!cstrcmp ("uptime", currtty->shell))
122 uptime ();
124 return;
127 if (!cstrcmp ("ls", currtty->shell))
129 strcpy (test, argparse (currtty->shell));
131 ls (test);
133 return;
136 if (!cstrcmp ("cd", currtty->shell))
138 strcpy (test, argparse (currtty->shell));
140 cd (test);
142 return;
145 if (!cstrcmp ("cat", currtty->shell))
147 strcpy (test, argparse (currtty->shell));
149 cat (test);
151 return;
154 if (!cstrcmp ("mkdir", currtty->shell))
156 strcpy (test, argparse (currtty->shell));
158 mkdir (test);
160 return;
163 if (!cstrcmp ("hdd", currtty->shell))
165 int t;
166 int m;
167 int c;
168 int c2;
169 int h;
170 int s;
172 outportb (0x70, 0x12);
173 t = inportb (0x71);
175 if (t >> 4 == 0)
177 printf ("/dev/hda not installed\n");
179 else
181 outportb (0x70, 0x1b);
182 c = inportb (0x71);
183 outportb (0x70, 0x1c);
184 c2 = inportb (0x71);
185 outportb (0x70, 0x1d);
186 h = inportb (0x71);
187 outportb (0x70, 0x23);
188 s = inportb (0x71);
189 printf ("/dev/hda installed - CHS=%d-%d:%d:%d\n", c, c2, h, s);
192 if (t & 0xF == 0)
194 printf ("/dev/hdb not installed\n");
196 else
198 outportb (0x70, 0x24);
199 c = inportb (0x71);
200 outportb (0x70, 0x25);
201 c2 = inportb (0x71);
202 outportb (0x70, 0x26);
203 h = inportb (0x71);
204 outportb (0x70, 0x2c);
205 s = inportb (0x71);
206 printf ("/dev/hdb installed - CHS=%d-%d:%d:%d\n", c, c2, h, s);
209 return;
212 if (!cstrcmp ("debug", currtty->shell))
214 if (!debug) {
215 debug = 1;
216 DPRINT ("developer mode was enabled.");
218 return;
221 DPRINT ("developer mode was disabled.");
222 debug = 0;
224 return;
227 /* if (!cstrcmp ("thread", currtty->shell))
229 tty_lock (currtty);
231 proc_t *proc = proc_create (currtty, "thread", thread);
233 return;
236 if (!strncmp ("./", currtty->shell, 2) || !cstrcmp ("exec", currtty->shell))
238 if (!strncmp ("./", currtty->shell, 2))
239 strcpy (test, currtty->shell+2);
240 else
241 strcpy (test, argparse (currtty->shell));
243 if (!exec (test))
244 return;
246 unsigned entry;
247 int err = exec_elf (file_cache, &entry);
249 if (err != NULL) {
250 printf ("ERROR -> invalid ELF exec\n");
251 return;
254 tty_lock (currtty);
256 proc_t *proc = proc_create (currtty, test, entry);
258 /*while (1) {
259 schedule ();
260 if (!proc_find (proc->task))
261 break;
262 wait ();
265 ((void_fn_void_t) entry)();
267 flush_elf ();
269 proc_done (proc);
271 return;
274 if (!cstrcmp ("ps", currtty->shell))
276 proc_display ();
278 return;
281 if (!cstrcmp ("mount", currtty->shell))
284 strcpy (test, argparse (currtty->shell));
285 char devname[20];
286 char mountpoint[32];
288 unsigned l = strlen (test);
289 unsigned x = 0;
290 while (x < l) {
291 if (test[x] == ' ')
292 break;
293 x ++;
296 memcpy (devname, test, x);
297 devname[x] = '\0';
298 strcpy (mountpoint, argparse (test));
299 unsigned y = strlen (mountpoint);
301 if (mountpoint[y-1] != '/') {
302 mountpoint[y] = '/';
303 mountpoint[y+1] = '\0';
306 if (x && y) {
307 partition_t *p = partition_find (devname);
309 if (p)
310 mount (p, "", mountpoint);
311 else
312 printf ("ERROR -> partition %s does not exists\n", devname);
313 } else
314 mount_display ();
316 return;
319 if (!cstrcmp ("env", currtty->shell))
321 env_display ();
323 return;
326 if (!cstrcmp ("lsdev", currtty->shell))
328 dev_display ();
330 return;
333 if (!cstrcmp ("login", currtty->shell))
335 currtty->user = NULL;
336 currtty->logged = false;
338 settextcolor (7, 0);
339 puts ("login: ");
341 return;
344 if (!cstrcmp ("version", currtty->shell))
346 osinfo ();
348 return;
351 if (!cstrcmp ("serialr", currtty->shell))
353 dev_t *dev = dev_find ("/dev/com0");
355 if (dev) {
356 char data[11];
358 dev->handler (DEV_ACT_READ, data, 10);
360 printf ("serialr: %s\n", data);
363 return;
366 if (!cstrcmp ("serialw", currtty->shell))
368 dev_t *dev = dev_find ("/dev/com0");
370 if (dev) {
371 unsigned len = strlen (argparse (currtty->shell));
372 char data[len+1];
373 memcpy (data, argparse (currtty->shell), len);
374 data[len] = '\0';
376 dev->handler (DEV_ACT_WRITE, data, len);
378 printf ("serialw: %s\n", data);
381 return;
384 if (!cstrcmp ("fdisk", currtty->shell))
386 fdisk ();
387 return;
390 if (!cstrcmp ("hdcat", currtty->shell))
392 partition_t *p = partition_find (argparse (currtty->shell));
394 if (p) {
395 dev_t *dev = dev_findbypartition (p);
396 int c, d = 0;
397 unsigned char block[512];
399 while (!key_pressed (1)) {
400 if (key_pressed (72) == 1) {
401 printf ("##block: %d\n", d);
402 dev->handler (DEV_ACT_READ, p, block, "", d);
404 for (c = 0; c < 512; c ++)
405 putch ((unsigned)block[c]);
406 d ++;
409 usleep (5);
411 } else
412 printf ("Please specify partition, example: hdcat /dev/hda0\n");
414 return;
417 /* error -> command not found */
418 printf ("%s: command not found\n", currtty->shell);