Reuse parsers from QEMU, C99 boolean. Signed-off-by: David Marchand <david.marchand@xxxxxxxxx> --- contrib/ivshmem-client/ivshmem-client.c | 12 +---- contrib/ivshmem-client/ivshmem-client.h | 4 +- contrib/ivshmem-client/main.c | 12 +---- contrib/ivshmem-server/ivshmem-server.c | 14 +----- contrib/ivshmem-server/ivshmem-server.h | 4 +- contrib/ivshmem-server/main.c | 73 +++++-------------------------- 6 files changed, 20 insertions(+), 99 deletions(-) diff --git a/contrib/ivshmem-client/ivshmem-client.c b/contrib/ivshmem-client/ivshmem-client.c index ce3a5d2..2ba40a7 100644 --- a/contrib/ivshmem-client/ivshmem-client.c +++ b/contrib/ivshmem-client/ivshmem-client.c @@ -6,19 +6,11 @@ * top-level directory. */ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> -#include <signal.h> -#include <unistd.h> -#include <inttypes.h> - #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> +#include "qemu-common.h" #include "qemu/queue.h" #include "ivshmem-client.h" @@ -149,7 +141,7 @@ handle_server_msg(IvshmemClient *client) int ivshmem_client_init(IvshmemClient *client, const char *unix_sock_path, ivshmem_client_notif_cb_t notif_cb, void *notif_arg, - int verbose) + bool verbose) { unsigned i; diff --git a/contrib/ivshmem-client/ivshmem-client.h b/contrib/ivshmem-client/ivshmem-client.h index e3b284d..45f2b64 100644 --- a/contrib/ivshmem-client/ivshmem-client.h +++ b/contrib/ivshmem-client/ivshmem-client.h @@ -78,7 +78,7 @@ struct IvshmemClient { ivshmem_client_notif_cb_t notif_cb; /**< notification callback */ void *notif_arg; /**< notification argument */ - int verbose; /**< true to enable debug */ + bool verbose; /**< true to enable debug */ }; /** @@ -101,7 +101,7 @@ struct IvshmemClient { */ int ivshmem_client_init(IvshmemClient *client, const char *unix_sock_path, ivshmem_client_notif_cb_t notif_cb, void *notif_arg, - int verbose); + bool verbose); /** * Connect to the server diff --git a/contrib/ivshmem-client/main.c b/contrib/ivshmem-client/main.c index 778d0f2..f8a7b66 100644 --- a/contrib/ivshmem-client/main.c +++ b/contrib/ivshmem-client/main.c @@ -6,15 +6,7 @@ * top-level directory. */ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> -#include <signal.h> -#include <unistd.h> -#include <inttypes.h> -#include <getopt.h> +#include "qemu-common.h" #include "ivshmem-client.h" @@ -22,7 +14,7 @@ #define DEFAULT_UNIX_SOCK_PATH "/tmp/ivshmem_socket" typedef struct IvshmemClientArgs { - int verbose; + bool verbose; char *unix_sock_path; } IvshmemClientArgs; diff --git a/contrib/ivshmem-server/ivshmem-server.c b/contrib/ivshmem-server/ivshmem-server.c index e58864d..0afa6e8 100644 --- a/contrib/ivshmem-server/ivshmem-server.c +++ b/contrib/ivshmem-server/ivshmem-server.c @@ -6,23 +6,13 @@ * top-level directory. */ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> -#include <signal.h> -#include <unistd.h> -#include <inttypes.h> -#include <fcntl.h> - #include <sys/mman.h> -#include <sys/stat.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <sys/eventfd.h> +#include "qemu-common.h" #include "qemu/queue.h" #include "ivshmem-server.h" @@ -246,7 +236,7 @@ ivshmem_ftruncate(int fd, unsigned shmsize) int ivshmem_server_init(IvshmemServer *server, const char *unix_sock_path, const char *shm_path, size_t shm_size, unsigned n_vectors, - int verbose) + bool verbose) { memset(server, 0, sizeof(*server)); diff --git a/contrib/ivshmem-server/ivshmem-server.h b/contrib/ivshmem-server/ivshmem-server.h index acd17a8..5ccc7af 100644 --- a/contrib/ivshmem-server/ivshmem-server.h +++ b/contrib/ivshmem-server/ivshmem-server.h @@ -69,7 +69,7 @@ typedef struct IvshmemServer { int shm_fd; /**< shm file descriptor */ unsigned n_vectors; /**< number of vectors */ long cur_id; /**< id to be given to next client */ - int verbose; /**< true in verbose mode */ + bool verbose; /**< true in verbose mode */ IvshmemServerPeerList peer_list; /**< list of peers */ } IvshmemServer; @@ -97,7 +97,7 @@ typedef struct IvshmemServer { int ivshmem_server_init(IvshmemServer *server, const char *unix_sock_path, const char *shm_path, size_t shm_size, unsigned n_vectors, - int verbose); + bool verbose); /** * Open the shm, then create and bind to the unix socket diff --git a/contrib/ivshmem-server/main.c b/contrib/ivshmem-server/main.c index a4504c3..f00e6f9 100644 --- a/contrib/ivshmem-server/main.c +++ b/contrib/ivshmem-server/main.c @@ -6,17 +6,7 @@ * top-level directory. */ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> -#include <signal.h> -#include <unistd.h> -#include <inttypes.h> -#include <sys/types.h> -#include <limits.h> -#include <getopt.h> +#include "qemu-common.h" #include "ivshmem-server.h" @@ -30,8 +20,8 @@ /* arguments given by the user */ typedef struct IvshmemServerArgs { - int verbose; - int foreground; + bool verbose; + bool foreground; char *pid_file; char *unix_socket_path; char *shm_path; @@ -67,59 +57,13 @@ usage(const char *name, int code) exit(code); } -/* parse the size of shm */ -static int -parse_size(const char *val_str, size_t *val) -{ - char *endptr; - unsigned long long tmp; - - errno = 0; - tmp = strtoull(val_str, &endptr, 0); - if ((errno == ERANGE && tmp == ULLONG_MAX) || (errno != 0 && tmp == 0)) { - return -1; - } - if (endptr == val_str) { - return -1; - } - if (endptr[0] == 'K' && endptr[1] == '\0') { - tmp *= 1024; - } else if (endptr[0] == 'M' && endptr[1] == '\0') { - tmp *= 1024 * 1024; - } else if (endptr[0] == 'G' && endptr[1] == '\0') { - tmp *= 1024 * 1024 * 1024; - } else if (endptr[0] != '\0') { - return -1; - } - - *val = tmp; - return 0; -} - -/* parse an unsigned int */ -static int -parse_uint(const char *val_str, unsigned *val) -{ - char *endptr; - unsigned long tmp; - - errno = 0; - tmp = strtoul(val_str, &endptr, 0); - if ((errno == ERANGE && tmp == ULONG_MAX) || (errno != 0 && tmp == 0)) { - return -1; - } - if (endptr == val_str || endptr[0] != '\0') { - return -1; - } - *val = tmp; - return 0; -} - /* parse the program arguments, exit on error */ static void parse_args(IvshmemServerArgs *args, int argc, char *argv[]) { char c; + unsigned long long v; + Error *errp; while ((c = getopt(argc, argv, "h" /* help */ @@ -158,17 +102,20 @@ parse_args(IvshmemServerArgs *args, int argc, char *argv[]) break; case 'l': /* shm_size */ - if (parse_size(optarg, &args->shm_size) < 0) { + parse_option_size("shm_size", optarg, &args->shm_size, &errp); + if (errp) { + error_free(errp); fprintf(stderr, "cannot parse shm size\n"); usage(argv[0], 1); } break; case 'n': /* n_vectors */ - if (parse_uint(optarg, &args->n_vectors) < 0) { + if (parse_uint_full(optarg, &v, 0) < 0) { fprintf(stderr, "cannot parse n_vectors\n"); usage(argv[0], 1); } + args->n_vectors = v; break; default: -- 1.7.10.4 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html