2 * thunix/kernel/shell.c
4 * The shell part, it just supported a limit number of commands now.
6 * Copyright (C) Aleaxander 2008-2009
19 #define BUFFER_SIZE 512
22 char buf
[BUFFER_SIZE
];
23 static char *version_string
= "v0.3";
26 static int atoi_hex(char *str
)
33 rest
= *str
< '9' ? *str
- '0' : (*str
| 0x20) - 'a';
34 sum
= sum
* 16 + rest
;
41 static int atoi(char *str
)
45 if (str
[0] == '0' && (str
[1] | 0x20) == 'x')
46 return atoi_hex(str
+ 2);
49 sum
= sum
* 10 + *str
- '0';
55 static int try_hex(char *str
)
58 if ((*str
>= '0' && *str
<= '9') ||
59 ((*str
| 0x20) >= 'a' && (*str
| 0x20) <= 'f')) {
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')
83 if (*str
> '9' || *str
< '0')
91 static int is_command(char *com_buf
, char *command
)
93 if ( strcmp(com_buf
, command
) == 0)
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");
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");
118 printk("%s%s%s%s%s%s%s%s%s%s%s%s%s","thunix, version 0.2\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" \
135 static void version()
137 printk("%s\n",version_string
);
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") )
165 else if (is_command(command
, "clear") )
168 else if (is_command(command
, "date") )
171 else if (is_command(command
, "halt") )
174 else if ( is_command(command
, "hello") )
175 printk("hello thunix\n");
177 else if ( is_command(command
, "help") )
180 else if ( is_command(command
, "hexdump") ) {
182 printk("hexdump usage: hexdump 'mem_addr or string' length\n");
184 if(is_digits(argv
[1]))
185 hexdump((char *)atoi(argv
[1]), atoi(argv
[2]));
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") ) {
196 printk("mkdir usage: please input at least one dir name\n");
206 else if ( is_command(command
, "ls") ) {
210 } else if (argc == 1) {
215 printk("%s:\n", argv[i]);
223 else if ( is_command(command
, "version") )
226 else if ( is_command(command
, "reboot") )
229 else if ( is_command(command
, "debug") )
233 printk("unknown command, please type 'help' for more information\n");
237 void debug_command(char *command
, int argc
, char **argv
)
240 printk("command = %s\n", command
);
243 printk("argv[%d] = %s\n", i
, argv
[i
]);
251 /* For now it's enough */
253 void parse_command(char * command_buffer
)
256 char *argv
[MAX_OPTION
] = {};
260 command
= command_buffer
;
263 printk("cmdline: %s\n", command
);
264 hexdump(command
, strlen(command
));
269 while ( (*p
!= ' ') && (*p
) )
271 argv
[argc
++] = command
;
273 while (*p
&& argc
< MAX_OPTION
) {
277 while (*p
&& *p
!= ' ')
279 while (*p
&& *p
== ' ')
287 run_command(command
, argc
, argv
);