Changeset f4338d2 in mainline
- Timestamp:
- 2005-11-26T22:48:17Z (19 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a8c48241
- Parents:
- ff3b3197
- Files:
-
- 6 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
Makefile
rff3b3197 rf4338d2 96 96 generic/src/console/chardev.c \ 97 97 generic/src/console/console.c \ 98 generic/src/console/kconsole.c \ 98 99 generic/src/cpu/cpu.c \ 99 100 generic/src/main/main.c \ 100 generic/src/main/kconsole.c \101 101 generic/src/main/kinit.c \ 102 102 generic/src/main/uinit.c \ -
generic/include/console/kconsole.h
rff3b3197 rf4338d2 44 44 cmd_arg_type_t type; /**< Type descriptor. */ 45 45 void *buffer; /**< Buffer where to store data. */ 46 size_t buflen; /**< Size of the buffer. */46 size_t len; /**< Size of the buffer. */ 47 47 }; 48 48 … … 53 53 const char *name; /**< Command name. */ 54 54 const char *description; /**< Textual description. */ 55 int (* func)(cmd_arg_t * cmd); /**< Function implementing the command. */55 int (* func)(cmd_arg_t *); /**< Function implementing the command. */ 56 56 count_t argc; /**< Number of arguments. */ 57 57 cmd_arg_t *argv; /**< Argument vector. */ 58 void (* help)(void); /**< Function for printing detailed help. */ 58 59 }; 59 60 -
generic/include/func.h
rff3b3197 rf4338d2 38 38 39 39 extern size_t strlen(const char *str); 40 extern int strcmp(const char *src, const char *dst, size_t len); 40 extern int strncmp(const char *src, const char *dst, size_t len); 41 extern void strncpy(char *dest, const char *src, size_t len); 41 42 42 43 #endif -
generic/include/macros.h
rff3b3197 rf4338d2 37 37 #define is_white(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r')) 38 38 39 #define min(a,b) ((a)<(b)?(a):(b)) 40 #define max(a,b) ((a)>(b)?(a):(b)) 41 39 42 #endif -
generic/src/console/kconsole.c
rff3b3197 rf4338d2 27 27 */ 28 28 29 #include < main/kconsole.h>29 #include <console/kconsole.h> 30 30 #include <console/console.h> 31 31 #include <console/chardev.h> … … 38 38 #include <func.h> 39 39 #include <macros.h> 40 #include <debug.h> 40 41 41 42 #define MAX_CMDLINE 256 … … 44 45 * 45 46 * The console is realized by kernel thread kconsole. 46 * It doesn't understand any commands on its own, but47 * makes it possible for other kernel subsystems to47 * It doesn't understand any useful command on its own, 48 * but makes it possible for other kernel subsystems to 48 49 * register their own commands. 49 50 */ … … 68 69 69 70 static cmd_info_t *parse_cmdline(char *cmdline, size_t len); 70 static int cmd_help(cmd_arg_t *cmd); 71 71 static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end); 72 73 /** Data and methods for 'help' command. */ 74 static int cmd_help(cmd_arg_t *argv); 72 75 static cmd_info_t help_info; 76 77 /** Data and methods for 'description' command. */ 78 static int cmd_desc(cmd_arg_t *argv); 79 static void desc_help(void); 80 static cmd_info_t desc_info; 81 static char desc_buf[MAX_CMDLINE+1]; 82 static cmd_arg_t desc_argv = { 83 .type = ARG_TYPE_STRING, 84 .buffer = desc_buf, 85 .len = sizeof(desc_buf) 86 }; 73 87 74 88 /** Initialize kconsole data structures. */ … … 81 95 help_info.description = "List supported commands."; 82 96 help_info.func = cmd_help; 97 help_info.help = NULL; 83 98 help_info.argc = 0; 84 99 help_info.argv = NULL; 85 100 86 101 spinlock_initialize(&help_info.lock); 87 102 link_initialize(&help_info.link); 88 103 89 104 if (!cmd_register(&help_info)) 90 panic("could not register command\n"); 105 panic("could not register command %s\n", help_info.name); 106 107 108 desc_info.name = "describe"; 109 desc_info.description = "Describe specified command."; 110 desc_info.help = desc_help; 111 desc_info.func = cmd_desc; 112 desc_info.argc = 1; 113 desc_info.argv = &desc_argv; 114 115 spinlock_initialize(&desc_info.lock); 116 link_initialize(&desc_info.link); 117 118 if (!cmd_register(&desc_info)) 119 panic("could not register command %s\n", desc_info.name); 91 120 } 92 121 … … 130 159 } 131 160 132 if ((str cmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) {161 if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) { 133 162 /* The command is already there. */ 134 163 spinlock_unlock(&hlp->lock); … … 170 199 while (true) { 171 200 printf("%s> ", __FUNCTION__); 172 len = gets(stdin, cmdline, sizeof(cmdline)); 201 if (!(len = gets(stdin, cmdline, sizeof(cmdline)))) 202 continue; 173 203 cmdline[len] = '\0'; 174 204 cmd_info = parse_cmdline(cmdline, len); 175 if (!cmd_info) { 176 printf("?\n"); 205 if (!cmd_info) 177 206 continue; 178 }179 207 (void) cmd_info->func(cmd_info->argv); 180 208 } … … 191 219 { 192 220 index_t start = 0, end = 0; 193 bool found_start = false;194 221 cmd_info_t *cmd = NULL; 195 222 link_t *cur; … … 197 224 int i; 198 225 199 for (i = 0; i < len; i++) { 226 if (!parse_argument(cmdline, len, &start, &end)) { 227 /* Command line did not contain alphanumeric word. */ 228 return NULL; 229 } 230 231 ipl = interrupts_disable(); 232 spinlock_lock(&cmd_lock); 233 234 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { 235 cmd_info_t *hlp; 236 237 hlp = list_get_instance(cur, cmd_info_t, link); 238 spinlock_lock(&hlp->lock); 239 240 if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) { 241 cmd = hlp; 242 break; 243 } 244 245 spinlock_unlock(&hlp->lock); 246 } 247 248 spinlock_unlock(&cmd_lock); 249 250 if (!cmd) { 251 /* Unknown command. */ 252 printf("Unknown command.\n"); 253 interrupts_restore(ipl); 254 return NULL; 255 } 256 257 /* cmd == hlp is locked */ 258 259 /* 260 * The command line must be further analyzed and 261 * the parameters therefrom must be matched and 262 * converted to those specified in the cmd info 263 * structure. 264 */ 265 266 for (i = 0; i < cmd->argc; i++) { 267 char *buf; 268 start = end + 1; 269 if (!parse_argument(cmdline, len, &start, &end)) { 270 printf("Too few arguments.\n"); 271 spinlock_unlock(&cmd->lock); 272 interrupts_restore(ipl); 273 return NULL; 274 } 275 276 switch (cmd->argv[i].type) { 277 case ARG_TYPE_STRING: 278 buf = cmd->argv[i].buffer; 279 strncpy(buf, (const char *) &cmdline[start], min((end - start) + 1, cmd->argv[i].len - 1)); 280 buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; 281 break; 282 case ARG_TYPE_INT: 283 case ARG_TYPE_INVALID: 284 default: 285 panic("invalid argument type\n"); 286 break; 287 } 288 } 289 290 start = end + 1; 291 if (parse_argument(cmdline, len, &start, &end)) { 292 printf("Too many arguments.\n"); 293 spinlock_unlock(&cmd->lock); 294 interrupts_restore(ipl); 295 return NULL; 296 } 297 298 spinlock_unlock(&cmd->lock); 299 interrupts_restore(ipl); 300 return cmd; 301 } 302 303 /** Parse argument. 304 * 305 * Find start and end positions of command line argument. 306 * 307 * @param cmdline Command line as read from the input device. 308 * @param len Number of characters in cmdline. 309 * @param start On entry, 'start' contains pointer to the index 310 * of first unprocessed character of cmdline. 311 * On successful exit, it marks beginning of the next argument. 312 * @param end Undefined on entry. On exit, 'end' points to the last character 313 * of the next argument. 314 * 315 * @return false on failure, true on success. 316 */ 317 bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) 318 { 319 int i; 320 bool found_start = false; 321 322 ASSERT(start != NULL); 323 ASSERT(end != NULL); 324 325 for (i = *start; i < len; i++) { 200 326 if (!found_start) { 201 327 if (is_white(cmdline[i])) 202 start++;328 (*start)++; 203 329 else 204 330 found_start = true; … … 208 334 } 209 335 } 210 end = i - 1; 211 212 if (!found_start) { 213 /* Command line did not contain alphanumeric word. */ 214 return NULL; 215 } 336 *end = i - 1; 337 338 return found_start; 339 } 340 341 342 /** List supported commands. 343 * 344 * @param argv Argument vector. 345 * 346 * @return 0 on failure, 1 on success. 347 */ 348 int cmd_help(cmd_arg_t *argv) 349 { 350 link_t *cur; 351 ipl_t ipl; 216 352 217 353 ipl = interrupts_disable(); … … 224 360 spinlock_lock(&hlp->lock); 225 361 226 if (strcmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) { 227 cmd = hlp; 228 break; 229 } 230 362 printf("%s - %s\n", hlp->name, hlp->description); 363 231 364 spinlock_unlock(&hlp->lock); 232 365 } 233 366 234 367 spinlock_unlock(&cmd_lock); 235 236 if (!cmd) {237 /* Unknown command. */238 interrupts_restore(ipl);239 return NULL;240 }241 242 /* cmd == hlp is locked */243 244 /*245 * TODO:246 * The command line must be further analyzed and247 * the parameters therefrom must be matched and248 * converted to those specified in the cmd info249 * structure.250 */251 252 spinlock_unlock(&cmd->lock);253 368 interrupts_restore(ipl); 254 return cmd; 255 } 256 257 /** List supported commands. 258 * 259 * @param cmd Argument vector. 369 370 return 1; 371 } 372 373 /** Describe specified command. 374 * 375 * @param argv Argument vector. 260 376 * 261 377 * @return 0 on failure, 1 on success. 262 378 */ 263 int cmd_ help(cmd_arg_t *cmd)379 int cmd_desc(cmd_arg_t *argv) 264 380 { 265 381 link_t *cur; … … 274 390 hlp = list_get_instance(cur, cmd_info_t, link); 275 391 spinlock_lock(&hlp->lock); 276 277 printf("%s\t%s\n", hlp->name, hlp->description); 392 393 if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) { 394 printf("%s - %s\n", hlp->name, hlp->description); 395 if (hlp->help) 396 hlp->help(); 397 spinlock_unlock(&hlp->lock); 398 break; 399 } 278 400 279 401 spinlock_unlock(&hlp->lock); … … 285 407 return 1; 286 408 } 409 410 /** Print detailed description of 'describe' command. */ 411 void desc_help(void) 412 { 413 printf("Syntax: describe command_name\n"); 414 } -
generic/src/lib/func.c
rff3b3197 rf4338d2 83 83 * 84 84 */ 85 int str cmp(const char *src, const char *dst, size_t len)85 int strncmp(const char *src, const char *dst, size_t len) 86 86 { 87 87 int i; … … 96 96 } 97 97 98 /** Copy NULL terminated string. 99 * 100 * Copy at most 'len' characters from string 'src' to 'dest'. 101 * If 'src' is shorter than 'len', '\0' is inserted behind the 102 * last copied character. 103 * 104 * @param src Source string. 105 * @param dst Destination buffer. 106 * @param len Size of destination buffer. 107 */ 108 void strncpy(char *dest, const char *src, size_t len) 109 { 110 int i; 111 for (i = 0; i < len; i++) { 112 if (!(dest[i] = src[i])) 113 return; 114 } 115 } -
generic/src/main/kinit.c
rff3b3197 rf4338d2 28 28 29 29 #include <main/kinit.h> 30 #include <main/kconsole.h>31 30 #include <main/uinit.h> 32 31 #include <config.h> … … 46 45 #include <memstr.h> 47 46 #include <console/console.h> 47 #include <console/kconsole.h> 48 48 49 49 #ifdef CONFIG_SMP -
generic/src/main/main.c
rff3b3197 rf4338d2 37 37 #include <proc/task.h> 38 38 #include <main/kinit.h> 39 #include < main/kconsole.h>39 #include <console/kconsole.h> 40 40 #include <cpu.h> 41 41 #include <align.h>
Note:
See TracChangeset
for help on using the changeset viewer.