From patchwork Wed Mar 22 07:34:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsO8cmdlbiBHcm/Dnw==?= X-Patchwork-Id: 13183642 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B690BC6FD1F for ; Wed, 22 Mar 2023 07:35:13 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.513127.793682 (Exim 4.92) (envelope-from ) id 1peszw-0005O5-EA; Wed, 22 Mar 2023 07:35:04 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 513127.793682; Wed, 22 Mar 2023 07:35:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1peszw-0005Nw-B9; Wed, 22 Mar 2023 07:35:04 +0000 Received: by outflank-mailman (input) for mailman id 513127; Wed, 22 Mar 2023 07:35:02 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1peszu-000573-7W for xen-devel@lists.xenproject.org; Wed, 22 Mar 2023 07:35:02 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [2001:67c:2178:6::1c]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 0df7312a-c884-11ed-85db-49a42c6b2330; Wed, 22 Mar 2023 08:35:01 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4AF9F3382B; Wed, 22 Mar 2023 07:35:01 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1EF4F13416; Wed, 22 Mar 2023 07:35:01 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id J7kgBqWvGmTEDAAAMHmgww (envelope-from ); Wed, 22 Mar 2023 07:35:01 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0df7312a-c884-11ed-85db-49a42c6b2330 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1679470501; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AqAAdYJutUgTcEYFEIRv9pmaALr54jUwXPZoDBAn7a8=; b=CTdafhiVTPmrVvUOTaP2NF+U9FyVdDyicV/O/GOQlLl6N/a+ehThQKDPeGdZfTFxhb0XCA rPlDs1bFBdAoSfgLomKOp3giYMwtvwrrJZnwXjr6QqWJNKwDDxUTIxoZkB+YUrU/sHiHW0 OKa1tHP5Vkin19FujDOmlmTDowaUtyw= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Anthony PERARD , Jason Andryuk Subject: [PATCH v2 1/3] tools/xl: allow split_string_into_pair() to trim values Date: Wed, 22 Mar 2023 08:34:51 +0100 Message-Id: <20230322073453.7853-2-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230322073453.7853-1-jgross@suse.com> References: <20230322073453.7853-1-jgross@suse.com> MIME-Version: 1.0 Most use cases of split_string_into_pair() are requiring the returned strings to be white space trimmed. In order to avoid the same code pattern multiple times, add a predicate parameter to split_string_into_pair() which can be specified to call trim() with that predicate for the string pair returned. Specifying NULL for the predicate will avoid the call of trim(). Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk Acked-by: Anthony PERARD --- tools/xl/xl_parse.c | 42 +++++++++++++++++++----------------------- tools/xl/xl_parse.h | 4 ++-- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index e344d4fda3..0e1b6907fa 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -646,7 +646,7 @@ static void parse_vnuma_config(const XLU_Config *config, conf_count++) { if (xlu_cfg_value_type(conf_option) == XLU_STRING) { - char *buf, *option_untrimmed, *value_untrimmed; + char *buf; char *option, *value; unsigned long val; @@ -654,15 +654,12 @@ static void parse_vnuma_config(const XLU_Config *config, if (!buf) continue; - if (split_string_into_pair(buf, "=", - &option_untrimmed, - &value_untrimmed)) { + if (split_string_into_pair(buf, "=", &option, &value, + isspace)) { fprintf(stderr, "xl: failed to split \"%s\" into pair\n", buf); exit(EXIT_FAILURE); } - trim(isspace, option_untrimmed, &option); - trim(isspace, value_untrimmed, &value); if (!strcmp("pnode", option)) { val = parse_ulong(value); @@ -715,8 +712,6 @@ static void parse_vnuma_config(const XLU_Config *config, } free(option); free(value); - free(option_untrimmed); - free(value_untrimmed); } } } @@ -838,7 +833,7 @@ int parse_vdispl_config(libxl_device_vdispl *vdispl, char *token) rc = split_string_into_pair(connectors[i], ":", &vdispl->connectors[i].unique_id, - &resolution); + &resolution, NULL); rc= sscanf(resolution, "%ux%u", &vdispl->connectors[i].width, &vdispl->connectors[i].height); @@ -2361,18 +2356,15 @@ void parse_config_data(const char *config_source, split_string_into_string_list(buf, ",", &pairs); len = libxl_string_list_length(&pairs); for (i = 0; i < len; i++) { - char *key, *key_untrimmed, *value, *value_untrimmed; + char *key, *value; int rc; - rc = split_string_into_pair(pairs[i], "=", - &key_untrimmed, - &value_untrimmed); + rc = split_string_into_pair(pairs[i], "=", &key, &value, + isspace); if (rc != 0) { fprintf(stderr, "failed to parse channel configuration: %s", pairs[i]); exit(1); } - trim(isspace, key_untrimmed, &key); - trim(isspace, value_untrimmed, &value); if (!strcmp(key, "backend")) { replace_string(&chn->backend_domname, value); @@ -2395,9 +2387,7 @@ void parse_config_data(const char *config_source, " ignoring\n", key); } free(key); - free(key_untrimmed); free(value); - free(value_untrimmed); } switch (chn->connection) { case LIBXL_CHANNEL_CONNECTION_UNKNOWN: @@ -3021,10 +3011,8 @@ void trim(char_predicate_t predicate, const char *input, char **output) *output = result; } -int split_string_into_pair(const char *str, - const char *delim, - char **a, - char **b) +int split_string_into_pair(const char *str, const char *delim, + char **a, char **b, char_predicate_t predicate) { char *s, *p, *saveptr, *aa = NULL, *bb = NULL; int rc = 0; @@ -3036,13 +3024,21 @@ int split_string_into_pair(const char *str, rc = ERROR_INVAL; goto out; } - aa = xstrdup(p); + if (predicate) { + trim(predicate, p, &aa); + } else { + aa = xstrdup(p); + } p = strtok_r(NULL, delim, &saveptr); if (p == NULL) { rc = ERROR_INVAL; goto out; } - bb = xstrdup(p); + if (predicate) { + trim(predicate, p, &bb); + } else { + bb = xstrdup(p); + } *a = aa; aa = NULL; diff --git a/tools/xl/xl_parse.h b/tools/xl/xl_parse.h index bab2861f8c..ab35c68545 100644 --- a/tools/xl/xl_parse.h +++ b/tools/xl/xl_parse.h @@ -45,14 +45,14 @@ int match_option_size(const char *prefix, size_t len, void split_string_into_string_list(const char *str, const char *delim, libxl_string_list *psl); -int split_string_into_pair(const char *str, const char *delim, - char **a, char **b); void replace_string(char **str, const char *val); /* NB: this follows the interface used by . See 'man 3 ctype' and look for CTYPE in libxl_internal.h */ typedef int (*char_predicate_t)(const int c); void trim(char_predicate_t predicate, const char *input, char **output); +int split_string_into_pair(const char *str, const char *delim, + char **a, char **b, char_predicate_t predicate); const char *get_action_on_shutdown_name(libxl_action_on_shutdown a); From patchwork Wed Mar 22 07:34:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsO8cmdlbiBHcm/Dnw==?= X-Patchwork-Id: 13183643 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EE859C6FD1F for ; Wed, 22 Mar 2023 07:35:16 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.513128.793693 (Exim 4.92) (envelope-from ) id 1pet01-0005hp-RB; Wed, 22 Mar 2023 07:35:09 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 513128.793693; Wed, 22 Mar 2023 07:35:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pet01-0005hi-O0; Wed, 22 Mar 2023 07:35:09 +0000 Received: by outflank-mailman (input) for mailman id 513128; Wed, 22 Mar 2023 07:35:09 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pet01-0005gx-3U for xen-devel@lists.xenproject.org; Wed, 22 Mar 2023 07:35:09 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 114a09b7-c884-11ed-b464-930f4c7d94ae; Wed, 22 Mar 2023 08:35:07 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id DCB153382B; Wed, 22 Mar 2023 07:35:06 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B203213416; Wed, 22 Mar 2023 07:35:06 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id GfwsKqqvGmTjDAAAMHmgww (envelope-from ); Wed, 22 Mar 2023 07:35:06 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 114a09b7-c884-11ed-b464-930f4c7d94ae DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1679470506; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PjEAqwHz5FepjmHP5AZRztMb7s0s81/7SZNZzGRzni4=; b=kMnWicPXpJAwWrrs9WAMz4UHTp3PAb4d7RziXIiG4P26B7NNtfYQsxzWuR67XpgxUPHgTY ftqaxULJLGd1CBtetv4Lp4jHJn+kxLV41WbopvhUE/1hy3/cWZ0o4Ji2B4krDmFC78lNDL e8tIVhtXbopql16JAsw6JdkGwG7cqX4= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Anthony PERARD Subject: [PATCH v2 2/3] tools/xl: make split_string_into_pair() more usable Date: Wed, 22 Mar 2023 08:34:52 +0100 Message-Id: <20230322073453.7853-3-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230322073453.7853-1-jgross@suse.com> References: <20230322073453.7853-1-jgross@suse.com> MIME-Version: 1.0 Today split_string_into_pair() will not really do what its name is suggesting: instead of splitting a string into a pair of strings using a delimiter, it will return the first two strings of the initial string by using the delimiter. This is never what the callers want, so modify split_string_into_pair() to split the string only at the first delimiter found, resulting in something like "x=a=b" to be split into "x" and "a=b" when being called with "=" as the delimiter. Today the returned strings would be "x" and "a". At the same time switch the delimiter from "const char *" (allowing multiple delimiter characters) to "char" (a single character only), as this makes the function more simple without breaking any use cases. Suggested-by: Anthony PERARD Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk Acked-by: Anthony PERARD --- V2: - new patch --- tools/xl/xl_parse.c | 23 ++++++++++++----------- tools/xl/xl_parse.h | 4 ++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index 0e1b6907fa..09cabd2732 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -654,7 +654,7 @@ static void parse_vnuma_config(const XLU_Config *config, if (!buf) continue; - if (split_string_into_pair(buf, "=", &option, &value, + if (split_string_into_pair(buf, '=', &option, &value, isspace)) { fprintf(stderr, "xl: failed to split \"%s\" into pair\n", buf); @@ -831,7 +831,7 @@ int parse_vdispl_config(libxl_device_vdispl *vdispl, char *token) { char *resolution; - rc = split_string_into_pair(connectors[i], ":", + rc = split_string_into_pair(connectors[i], ':', &vdispl->connectors[i].unique_id, &resolution, NULL); @@ -2358,7 +2358,7 @@ void parse_config_data(const char *config_source, for (i = 0; i < len; i++) { char *key, *value; int rc; - rc = split_string_into_pair(pairs[i], "=", &key, &value, + rc = split_string_into_pair(pairs[i], '=', &key, &value, isspace); if (rc != 0) { fprintf(stderr, "failed to parse channel configuration: %s", @@ -3011,26 +3011,27 @@ void trim(char_predicate_t predicate, const char *input, char **output) *output = result; } -int split_string_into_pair(const char *str, const char *delim, - char **a, char **b, char_predicate_t predicate) +int split_string_into_pair(const char *str, char delim, char **a, char **b, + char_predicate_t predicate) { - char *s, *p, *saveptr, *aa = NULL, *bb = NULL; + char *s, *p, *aa = NULL, *bb = NULL; int rc = 0; s = xstrdup(str); - p = strtok_r(s, delim, &saveptr); + p = strchr(s, delim); if (p == NULL) { rc = ERROR_INVAL; goto out; } + *p = 0; if (predicate) { - trim(predicate, p, &aa); + trim(predicate, s, &aa); } else { - aa = xstrdup(p); + aa = xstrdup(s); } - p = strtok_r(NULL, delim, &saveptr); - if (p == NULL) { + p++; + if (!*p) { rc = ERROR_INVAL; goto out; } diff --git a/tools/xl/xl_parse.h b/tools/xl/xl_parse.h index ab35c68545..fe0d586cdd 100644 --- a/tools/xl/xl_parse.h +++ b/tools/xl/xl_parse.h @@ -51,8 +51,8 @@ void replace_string(char **str, const char *val); and look for CTYPE in libxl_internal.h */ typedef int (*char_predicate_t)(const int c); void trim(char_predicate_t predicate, const char *input, char **output); -int split_string_into_pair(const char *str, const char *delim, - char **a, char **b, char_predicate_t predicate); +int split_string_into_pair(const char *str, char delim, char **a, char **b, + char_predicate_t predicate); const char *get_action_on_shutdown_name(libxl_action_on_shutdown a); From patchwork Wed Mar 22 07:34:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsO8cmdlbiBHcm/Dnw==?= X-Patchwork-Id: 13183644 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C50FDC6FD1F for ; Wed, 22 Mar 2023 07:35:22 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.513130.793703 (Exim 4.92) (envelope-from ) id 1pet08-0006ER-4P; Wed, 22 Mar 2023 07:35:16 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 513130.793703; Wed, 22 Mar 2023 07:35:16 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pet08-0006EG-0r; Wed, 22 Mar 2023 07:35:16 +0000 Received: by outflank-mailman (input) for mailman id 513130; Wed, 22 Mar 2023 07:35:14 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pet06-0005gx-8w for xen-devel@lists.xenproject.org; Wed, 22 Mar 2023 07:35:14 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 149c1540-c884-11ed-b464-930f4c7d94ae; Wed, 22 Mar 2023 08:35:12 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 74BAF20AD9; Wed, 22 Mar 2023 07:35:12 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 49C1D13416; Wed, 22 Mar 2023 07:35:12 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 38WeELCvGmQADQAAMHmgww (envelope-from ); Wed, 22 Mar 2023 07:35:12 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 149c1540-c884-11ed-b464-930f4c7d94ae DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1679470512; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YtiGh+qpU8bhgvUDsffds7Kvt+PAjE83auDMMPS8CWk=; b=P5Qud/Y725woH3pW9dgAoEv14k/3mIDTXLzYKPLAUoUM51zWeOKRWCYLP1bWB1qNjwMHSW syd5fSWzdofh6iDNgstloR1u6sU6hwHdMk/j7P9sZpE1xC5/NM5aweDg9bfBS/K4YFymtj q+/WeYrgaf3I7EmNq0eGhpU24UNM630= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Wei Liu , Anthony PERARD Subject: [PATCH v2 3/3] tools/xl: rework p9 config parsing Date: Wed, 22 Mar 2023 08:34:53 +0100 Message-Id: <20230322073453.7853-4-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230322073453.7853-1-jgross@suse.com> References: <20230322073453.7853-1-jgross@suse.com> MIME-Version: 1.0 Rework the config parsing of a p9 device to use the split_string_into_pair() function instead of open coding it. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk Acked-by: Anthony PERARD --- V2: - add libxl_string_list_dispose() call (Jason Andryuk) --- tools/xl/xl_parse.c | 74 ++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index 09cabd2732..1f6f47daf4 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -2180,54 +2180,54 @@ void parse_config_data(const char *config_source, if (!xlu_cfg_get_list(config, "p9", &p9devs, 0, 0)) { libxl_device_p9 *p9; - char *security_model = NULL; - char *path = NULL; - char *tag = NULL; - char *backend = NULL; - char *p, *p2, *buf2; d_config->num_p9s = 0; d_config->p9s = NULL; while ((buf = xlu_cfg_get_listitem (p9devs, d_config->num_p9s)) != NULL) { + libxl_string_list pairs; + int len; + p9 = ARRAY_EXTEND_INIT(d_config->p9s, d_config->num_p9s, libxl_device_p9_init); libxl_device_p9_init(p9); - buf2 = strdup(buf); - p = strtok(buf2, ","); - if(p) { - do { - while(*p == ' ') - ++p; - if ((p2 = strchr(p, '=')) == NULL) - break; - *p2 = '\0'; - if (!strcmp(p, "security_model")) { - security_model = strdup(p2 + 1); - } else if(!strcmp(p, "path")) { - path = strdup(p2 + 1); - } else if(!strcmp(p, "tag")) { - tag = strdup(p2 + 1); - } else if(!strcmp(p, "backend")) { - backend = strdup(p2 + 1); - } else { - fprintf(stderr, "Unknown string `%s' in 9pfs spec\n", p); - exit(1); - } - } while ((p = strtok(NULL, ",")) != NULL); - } - if (!path || !security_model || !tag) { - fprintf(stderr, "9pfs spec missing required field!\n"); - exit(1); + split_string_into_string_list(buf, ",", &pairs); + len = libxl_string_list_length(&pairs); + for (i = 0; i < len; i++) { + char *key, *value; + int rc; + + rc = split_string_into_pair(pairs[i], '=', &key, &value, + isspace); + if (rc != 0) { + fprintf(stderr, "failed to parse 9pfs configuration: %s", + pairs[i]); + exit(1); + } + + if (!strcmp(key, "security_model")) { + replace_string(&p9->security_model, value); + } else if (!strcmp(key, "path")) { + replace_string(&p9->path, value); + } else if (!strcmp(key, "tag")) { + replace_string(&p9->tag, value); + } else if (!strcmp(key, "backend")) { + replace_string(&p9->backend_domname, value); + } else { + fprintf(stderr, "Unknown 9pfs parameter '%s'\n", key); + exit(1); + } + free(key); + free(value); } - free(buf2); - replace_string(&p9->tag, tag); - replace_string(&p9->security_model, security_model); - replace_string(&p9->path, path); - if (backend) - replace_string(&p9->backend_domname, backend); + libxl_string_list_dispose(&pairs); + + if (!p9->path || !p9->security_model || !p9->tag) { + fprintf(stderr, "9pfs spec missing required field!\n"); + exit(1); + } } }