Add the implementation of tfs
[thunix.git] / kernel / shell.c
blobcc77357e3050a251c573bd72387f52622bc4e0ff
1 /**
2 * thunix/kernel/shell.c
4 * The shell part, it just supported a limit number of commands now.
5 *
6 * Copyright (C) Aleaxander 2008-2009
7 *
8 * Aleaxander@gmail.com
9 */
11 #include <thunix.h>
12 #include <time.h>
13 #include <console.h>
14 #include <stdio.h>
15 #include <string.h>
17 #include <hexdump.h>
19 #define BUFFER_SIZE 512
22 char buf[BUFFER_SIZE];
23 static char *version_string = "v0.3";
26 static int atoi_hex(char *str)
28 int sum = 0;
30 while (*str) {
31 int rest;
33 rest = *str < '9' ? *str - '0' : (*str | 0x20) - 'a';
34 sum = sum * 16 + rest;
35 str++;
38 return sum;
41 static int atoi(char *str)
43 int sum = 0;
45 if (str[0] == '0' && (str[1] | 0x20) == 'x')
46 return atoi_hex(str + 2);
48 while (*str) {
49 sum = sum * 10 + *str - '0';
50 str++;
52 return sum;
55 static int try_hex(char *str)
57 while (*str) {
58 if ((*str >= '0' && *str <= '9') ||
59 ((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f')) {
60 str++;
61 continue;
64 return 0;
67 return 1;
70 /*
71 * Test if the string is number or not, return 1 if true or 0
73 * It just recongnize hexdump and normal digits now
75 static int is_digits(char *str)
78 if (str[0] == '0' && (str[1] | 0x20) == 'x')
79 if (try_hex(str + 2))
80 return 1;
82 while (*str) {
83 if (*str > '9' || *str < '0')
84 return 0;
85 str++;
88 return 1;
91 static int is_command(char *com_buf, char *command)
93 if ( strcmp(com_buf, command) == 0)
94 return 1;
95 else
96 return 0;
100 static void about()
102 puts("\n");
103 printk("\tthunix, a 32-bit operating system, made by Alexander.\n");
104 printk("\tmostly written by c and little asm language.\n");
105 printk("\tthe lastest version is thunix-0.2\n");
106 printk("\tthe website is http://www.osdever.cn\n");
107 puts("\n");
108 printk("\tas the ram_fs we used, that's to say our all data crated.\n");
109 printk("\twill cleard after the shutdown\n");
110 printk("\tso, it's just a test system, Yeah, that's it!\n");
111 printk("\n\tThat's it ...\n");
112 puts("\n");
115 static void help()
117 puts("\n");
118 printk("%s%s%s%s%s%s%s%s%s%s%s%s%s","thunix, version 0.2\n", \
119 "\n", \
120 "about something about thunix\n", \
121 "clear clear the screen\n", \
122 "date display the current date\n", \
123 "help will print this page\n", \
124 "hexdump dump the data or file as hex number\n", \
125 "mkdir will make a new directory\n", \
126 "ls will list the '/' root driectory\n", \
127 "halt shut down the computer\n", \
128 "hello a test command, just print 'hello thunix'\n", \
129 "reboot reboot the computer\n", \
130 "version print the current version\n" \
132 puts("\n");
135 static void version()
137 printk("%s\n",version_string);
140 static void date()
142 struct tm time;
143 get_current_time(&time);
145 printk("%d/%02d/%02d %d:%d:%d\n", \
146 time.tm_year+2000, time.tm_mon, \
147 time.tm_mday,time.tm_hour, \
148 time.tm_min, time.tm_sec \
153 extern void cls(void);
154 //extern void ls(char *, char *);
155 extern void reboot();
156 //extern void mkdir(char *);
157 extern void halt(void);
158 void run_command(char *command, int argc, char **argv)
162 if ( is_command(command, "about") )
163 about();
165 else if (is_command(command, "clear") )
166 cls();
168 else if (is_command(command, "date") )
169 date();
171 else if (is_command(command, "halt") )
172 halt();
174 else if ( is_command(command, "hello") )
175 printk("hello thunix\n");
177 else if ( is_command(command, "help") )
178 help();
180 else if ( is_command(command, "hexdump") ) {
181 if (argc != 3) {
182 printk("hexdump usage: hexdump 'mem_addr or string' length\n");
183 } else {
184 if(is_digits(argv[1]))
185 hexdump((char *)atoi(argv[1]), atoi(argv[2]));
186 else {
187 printk("Not a valid memory addrss, try hexdump it as string\n");
188 hexdump(argv[1], atoi(argv[2]));
193 else if ( is_command(command, "mkdir") ) {
195 if (argc < 2) {
196 printk("mkdir usage: please input at least one dir name\n");
197 } else {
198 while (argc) {
199 argc--;
200 mkdir(argv[argc]);
206 else if ( is_command(command, "ls") ) {
208 if ( argc == 1) {
209 ls ("/", "-l");
210 } else if (argc == 1) {
211 ls(argv[1], 0);
212 } else {
213 int i = 1;
214 while ( i < argc) {
215 printk("%s:\n", argv[i]);
216 ls (argv[i], "-l");
217 i++;
223 else if ( is_command(command, "version") )
224 version ();
226 else if ( is_command(command, "reboot") )
227 reboot ();
228 #if 0
229 else if ( is_command(command, "debug") )
230 Debug();
231 #endif
232 else
233 printk("unknown command, please type 'help' for more information\n");
237 void debug_command(char *command, int argc, char **argv)
239 int i = 0;
240 printk("command = %s\n", command);
242 while ( i < argc) {
243 printk("argv[%d] = %s\n", i, argv[i]);
244 i ++;
251 /* For now it's enough */
252 #define MAX_OPTION 4
253 void parse_command(char * command_buffer)
255 char *command;
256 char *argv[MAX_OPTION] = {};
257 char *p;
258 int argc = 0;
260 command = command_buffer;
262 #if 0
263 printk("cmdline: %s\n", command);
264 hexdump(command, strlen(command));
265 #endif
267 p = command;
269 while ( (*p != ' ') && (*p) )
270 p ++;
271 argv[argc++] = command;
273 while (*p && argc < MAX_OPTION) {
274 *p = '\0';
275 p++;
276 argv[argc++] = p;
277 while (*p && *p != ' ')
278 p++;
279 while (*p && *p == ' ')
280 p++;
281 if ( *p == '\0' )
282 break;
283 p--;
287 run_command(command, argc, argv);
297 void shell_init()
299 puts("thunix $ ");