Clean up the warning messages
[thunix.git] / kernel / shell.c
blob628d3cf1c9055994bf2edb3cfcfe0118576baf94
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 <tfs.h>
18 #include <hexdump.h>
20 #define BUFFER_SIZE 512
23 char buf[BUFFER_SIZE];
24 static char *version_string = "v0.3";
27 static int atoi_hex(char *str)
29 int sum = 0;
31 while (*str) {
32 int rest;
34 rest = *str < '9' ? *str - '0' : (*str | 0x20) - 'a';
35 sum = sum * 16 + rest;
36 str++;
39 return sum;
42 static int atoi(char *str)
44 int sum = 0;
46 if (str[0] == '0' && (str[1] | 0x20) == 'x')
47 return atoi_hex(str + 2);
49 while (*str) {
50 sum = sum * 10 + *str - '0';
51 str++;
53 return sum;
56 static int try_hex(char *str)
58 while (*str) {
59 if ((*str >= '0' && *str <= '9') ||
60 ((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f')) {
61 str++;
62 continue;
65 return 0;
68 return 1;
71 /*
72 * Test if the string is number or not, return 1 if true or 0
74 * It just recongnize hexdump and normal digits now
76 static int is_digits(char *str)
79 if (str[0] == '0' && (str[1] | 0x20) == 'x')
80 if (try_hex(str + 2))
81 return 1;
83 while (*str) {
84 if (*str > '9' || *str < '0')
85 return 0;
86 str++;
89 return 1;
92 static int is_command(char *com_buf, char *command)
94 if ( strcmp(com_buf, command) == 0)
95 return 1;
96 else
97 return 0;
101 static void about()
103 puts("\n");
104 printk("\tthunix, a 32-bit operating system, made by Alexander.\n");
105 printk("\tmostly written by c and little asm language.\n");
106 printk("\tthe lastest version is thunix-0.2\n");
107 printk("\tthe website is http://www.osdever.cn\n");
108 puts("\n");
109 printk("\tas the ram_fs we used, that's to say our all data crated.\n");
110 printk("\twill cleard after the shutdown\n");
111 printk("\tso, it's just a test system, Yeah, that's it!\n");
112 printk("\n\tThat's it ...\n");
113 puts("\n");
116 static void help()
118 puts("\n");
119 printk("%s%s%s%s%s%s%s%s%s%s%s%s%s","thunix, version 0.2\n", \
120 "\n", \
121 "about something about thunix\n", \
122 "clear clear the screen\n", \
123 "date display the current date\n", \
124 "help will print this page\n", \
125 "hexdump dump the data or file as hex number\n", \
126 "mkdir will make a new directory\n", \
127 "ls will list the '/' root driectory\n", \
128 "halt shut down the computer\n", \
129 "hello a test command, just print 'hello thunix'\n", \
130 "reboot reboot the computer\n", \
131 "version print the current version\n" \
133 puts("\n");
136 static void version()
138 printk("%s\n",version_string);
141 static void date()
143 struct tm time;
144 get_current_time(&time);
146 printk("%d/%02d/%02d %d:%d:%d\n", \
147 time.tm_year+2000, time.tm_mon, \
148 time.tm_mday,time.tm_hour, \
149 time.tm_min, time.tm_sec \
154 extern void cls(void);
155 extern void reboot();
156 extern void ls(struct tfs_sb_info *sbi, char *);
157 extern void cd(struct tfs_sb_info *sbi, char *);
158 extern void cp(struct tfs_sb_info *sbi, char *, char *);
159 extern void cat(struct tfs_sb_info *sbi, char *);
160 extern void mkdir(struct tfs_sb_info *sbi, char *);
161 extern void rmdir(struct tfs_sb_info *sbi, char *);
162 extern void rm(struct tfs_sb_info *sbi, char *);
163 extern void touch(struct tfs_sb_info *sbi, char *);
164 extern void halt(void);
165 void run_command(char *command, int argc, char **argv)
169 if ( is_command(command, "about") )
170 about();
172 else if ( is_command(command, "cat") )
173 cat(tfs_sbi, argv[1]);
174 else if ( is_command(command, "cd") )
175 cd(tfs_sbi, argv[1]);
177 else if (is_command(command, "clear") )
178 cls();
179 else if (is_command(command, "cp") )
180 cp(tfs_sbi, argv[1], argv[2]);
182 else if (is_command(command, "date") )
183 date();
185 else if (is_command(command, "halt") )
186 halt();
188 else if ( is_command(command, "hello") )
189 printk("hello thunix\n");
191 else if ( is_command(command, "help") )
192 help();
194 else if ( is_command(command, "hexdump") ) {
195 if (argc != 3) {
196 printk("hexdump usage: hexdump 'mem_addr or string' length\n");
197 } else {
198 if(is_digits(argv[1]))
199 hexdump((char *)atoi(argv[1]), atoi(argv[2]));
200 else {
201 printk("Not a valid memory addrss, try hexdump it as string\n");
202 hexdump(argv[1], atoi(argv[2]));
207 else if ( is_command(command, "mkdir") ) {
208 if (argc != 2) {
209 printk("mkdir usage: mkdir dir\n");
210 } else {
211 mkdir(tfs_sbi, argv[1]);
214 else if ( is_command(command, "rmdir") ) {
215 rmdir(tfs_sbi, argv[1]);
218 else if ( is_command(command, "rm") ) {
219 rm(tfs_sbi, argv[1]);
222 else if ( is_command(command, "touch") ) {
223 touch(tfs_sbi, argv[1]);
226 else if ( is_command(command, "ls") ) {
227 if (argc == 1)
228 argv[1] = ".";
229 ls(tfs_sbi, argv[1]);
232 else if ( is_command(command, "version") )
233 version ();
235 else if ( is_command(command, "reboot") )
236 reboot ();
237 #if 1
238 else if ( is_command(command, "debug") )
239 Debug();
240 #endif
241 else
242 printk("unknown command, please type 'help' for more information\n");
246 void debug_command(char *command, int argc, char **argv)
248 int i = 0;
249 printk("command = %s\n", command);
251 while ( i < argc) {
252 printk("argv[%d] = %s\n", i, argv[i]);
253 i ++;
260 /* For now it's enough */
261 #define MAX_OPTION 4
262 void parse_command(char * command_buffer)
264 char *command;
265 char *argv[MAX_OPTION] = {};
266 char *p;
267 int argc = 0;
269 command = command_buffer;
271 #if 0
272 printk("cmdline: %s\n", command);
273 hexdump(command, strlen(command));
274 #endif
276 p = command;
278 while ( (*p != ' ') && (*p) )
279 p ++;
280 argv[argc++] = command;
282 while (*p && argc < MAX_OPTION) {
283 *p = '\0';
284 p++;
285 argv[argc++] = p;
286 while (*p && *p != ' ')
287 p++;
288 while (*p && *p == ' ')
289 p++;
290 if ( *p == '\0' )
291 break;
292 p--;
296 run_command(command, argc, argv);
306 void shell_init()
308 puts("thunix $ ");