2 * thunix/kernel/shell.c
4 * The shell part, it just supported a limit number of commands now.
6 * Copyright (C) Aleaxander 2008-2009
20 #define BUFFER_SIZE 512
23 char buf
[BUFFER_SIZE
];
24 static char *version_string
= "v0.3";
27 static int atoi_hex(char *str
)
34 rest
= *str
< '9' ? *str
- '0' : (*str
| 0x20) - 'a';
35 sum
= sum
* 16 + rest
;
42 static int atoi(char *str
)
46 if (str
[0] == '0' && (str
[1] | 0x20) == 'x')
47 return atoi_hex(str
+ 2);
50 sum
= sum
* 10 + *str
- '0';
56 static int try_hex(char *str
)
59 if ((*str
>= '0' && *str
<= '9') ||
60 ((*str
| 0x20) >= 'a' && (*str
| 0x20) <= 'f')) {
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')
84 if (*str
> '9' || *str
< '0')
92 static int is_command(char *com_buf
, char *command
)
94 if ( strcmp(com_buf
, command
) == 0)
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");
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");
119 printk("%s%s%s%s%s%s%s%s%s%s%s%s%s","thunix, version 0.2\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" \
136 static void version()
138 printk("%s\n",version_string
);
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") )
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") )
179 else if (is_command(command
, "cp") )
180 cp(tfs_sbi
, argv
[1], argv
[2]);
182 else if (is_command(command
, "date") )
185 else if (is_command(command
, "halt") )
188 else if ( is_command(command
, "hello") )
189 printk("hello thunix\n");
191 else if ( is_command(command
, "help") )
194 else if ( is_command(command
, "hexdump") ) {
196 printk("hexdump usage: hexdump 'mem_addr or string' length\n");
198 if(is_digits(argv
[1]))
199 hexdump((char *)atoi(argv
[1]), atoi(argv
[2]));
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") ) {
209 printk("mkdir usage: mkdir dir\n");
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") ) {
229 ls(tfs_sbi
, argv
[1]);
232 else if ( is_command(command
, "version") )
235 else if ( is_command(command
, "reboot") )
238 else if ( is_command(command
, "debug") )
242 printk("unknown command, please type 'help' for more information\n");
246 void debug_command(char *command
, int argc
, char **argv
)
249 printk("command = %s\n", command
);
252 printk("argv[%d] = %s\n", i
, argv
[i
]);
260 /* For now it's enough */
262 void parse_command(char * command_buffer
)
265 char *argv
[MAX_OPTION
] = {};
269 command
= command_buffer
;
272 printk("cmdline: %s\n", command
);
273 hexdump(command
, strlen(command
));
278 while ( (*p
!= ' ') && (*p
) )
280 argv
[argc
++] = command
;
282 while (*p
&& argc
< MAX_OPTION
) {
286 while (*p
&& *p
!= ' ')
288 while (*p
&& *p
== ' ')
296 run_command(command
, argc
, argv
);