Commit 4590fd80b867cca0cad87ec8d3cb7be2cd56f8b4
Committed by
Blue Swirl
1 parent
7869001b
monitor: Introduce get_command_name()
Move code to extract command name into a function of its own, this clearifies the code and let us remove two variables from monitor_handle_command(). Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Showing
1 changed file
with
30 additions
and
14 deletions
monitor.c
@@ -2381,6 +2381,32 @@ static int get_str(char *buf, int buf_size, const char **pp) | @@ -2381,6 +2381,32 @@ static int get_str(char *buf, int buf_size, const char **pp) | ||
2381 | return 0; | 2381 | return 0; |
2382 | } | 2382 | } |
2383 | 2383 | ||
2384 | +/* | ||
2385 | + * Store the command-name in cmdname, and return a pointer to | ||
2386 | + * the remaining of the command string. | ||
2387 | + */ | ||
2388 | +static const char *get_command_name(const char *cmdline, | ||
2389 | + char *cmdname, size_t nlen) | ||
2390 | +{ | ||
2391 | + size_t len; | ||
2392 | + const char *p, *pstart; | ||
2393 | + | ||
2394 | + p = cmdline; | ||
2395 | + while (qemu_isspace(*p)) | ||
2396 | + p++; | ||
2397 | + if (*p == '\0') | ||
2398 | + return NULL; | ||
2399 | + pstart = p; | ||
2400 | + while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | ||
2401 | + p++; | ||
2402 | + len = p - pstart; | ||
2403 | + if (len > nlen - 1) | ||
2404 | + len = nlen - 1; | ||
2405 | + memcpy(cmdname, pstart, len); | ||
2406 | + cmdname[len] = '\0'; | ||
2407 | + return p; | ||
2408 | +} | ||
2409 | + | ||
2384 | static int default_fmt_format = 'x'; | 2410 | static int default_fmt_format = 'x'; |
2385 | static int default_fmt_size = 4; | 2411 | static int default_fmt_size = 4; |
2386 | 2412 | ||
@@ -2388,8 +2414,8 @@ static int default_fmt_size = 4; | @@ -2388,8 +2414,8 @@ static int default_fmt_size = 4; | ||
2388 | 2414 | ||
2389 | static void monitor_handle_command(Monitor *mon, const char *cmdline) | 2415 | static void monitor_handle_command(Monitor *mon, const char *cmdline) |
2390 | { | 2416 | { |
2391 | - const char *p, *pstart, *typestr; | ||
2392 | - int c, nb_args, len, i, has_arg; | 2417 | + const char *p, *typestr; |
2418 | + int c, nb_args, i, has_arg; | ||
2393 | const mon_cmd_t *cmd; | 2419 | const mon_cmd_t *cmd; |
2394 | char cmdname[256]; | 2420 | char cmdname[256]; |
2395 | char buf[1024]; | 2421 | char buf[1024]; |
@@ -2413,19 +2439,9 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline) | @@ -2413,19 +2439,9 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline) | ||
2413 | #endif | 2439 | #endif |
2414 | 2440 | ||
2415 | /* extract the command name */ | 2441 | /* extract the command name */ |
2416 | - p = cmdline; | ||
2417 | - while (qemu_isspace(*p)) | ||
2418 | - p++; | ||
2419 | - if (*p == '\0') | 2442 | + p = get_command_name(cmdline, cmdname, sizeof(cmdname)); |
2443 | + if (!p) | ||
2420 | return; | 2444 | return; |
2421 | - pstart = p; | ||
2422 | - while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) | ||
2423 | - p++; | ||
2424 | - len = p - pstart; | ||
2425 | - if (len > sizeof(cmdname) - 1) | ||
2426 | - len = sizeof(cmdname) - 1; | ||
2427 | - memcpy(cmdname, pstart, len); | ||
2428 | - cmdname[len] = '\0'; | ||
2429 | 2445 | ||
2430 | /* find the command */ | 2446 | /* find the command */ |
2431 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { | 2447 | for(cmd = mon_cmds; cmd->name != NULL; cmd++) { |