From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id A2C756DE0943 for ; Thu, 8 Dec 2016 04:26:46 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: 0.559 X-Spam-Level: X-Spam-Status: No, score=0.559 tagged_above=-999 required=5 tests=[AWL=-0.412, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-0.001, SPF_SOFTFAIL=0.972] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6IEQoghn4fq3 for ; Thu, 8 Dec 2016 04:26:44 -0800 (PST) Received: from ni.com (skprod3.natinst.com [130.164.80.24]) by arlo.cworth.org (Postfix) with ESMTPS id 4E4A66DE0A5B for ; Thu, 8 Dec 2016 04:26:44 -0800 (PST) Received: from us-aus-exhub1.ni.corp.natinst.com (us-aus-exhub1.ni.corp.natinst.com [130.164.68.41]) by us-aus-skprod3.natinst.com (8.15.0.59/8.15.0.59) with ESMTPS id uB8CQWtf008318 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Thu, 8 Dec 2016 06:26:33 -0600 Received: from us-aus-exch5.ni.corp.natinst.com (130.164.68.15) by us-aus-exhub1.ni.corp.natinst.com (130.164.68.41) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 8 Dec 2016 06:26:32 -0600 Received: from us-aus-exhub2.ni.corp.natinst.com (130.164.68.32) by us-aus-exch5.ni.corp.natinst.com (130.164.68.15) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 8 Dec 2016 06:26:32 -0600 Received: from adi-pc-linux.natinst.com (130.164.49.7) by us-aus-exhub2.ni.corp.natinst.com (130.164.68.32) with Microsoft SMTP Server id 15.0.1156.6 via Frontend Transport; Thu, 8 Dec 2016 06:26:31 -0600 From: Ioan-Adrian Ratiu To: , Subject: [PATCH v5 1/2] cli: abstract config file reading to a separate function Date: Thu, 8 Dec 2016 14:24:44 +0200 Message-ID: <20161208122445.30918-2-adi@adirat.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161208122445.30918-1-adi@adirat.com> References: <20161208122445.30918-1-adi@adirat.com> MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-12-08_06:, , signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=2 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1609300000 definitions=main-1612080197 X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 Dec 2016 12:26:46 -0000 From: Jani Nikula Simplify and fix the coding style while at it. --- notmuch-config.c | 65 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/notmuch-config.c b/notmuch-config.c index e5d42a0..bd52790 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -202,6 +202,38 @@ get_username_from_passwd_file (void *ctx) return name; } +static notmuch_bool_t +get_config_from_file (notmuch_config_t *config, notmuch_bool_t create_new) +{ + GError *error = NULL; + notmuch_bool_t ret = FALSE; + + if (g_key_file_load_from_file (config->key_file, config->filename, + G_KEY_FILE_KEEP_COMMENTS, &error)) + return TRUE; + + if (error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT) { + /* If create_new is true, then the caller is prepared for a + * default configuration file in the case of FILE NOT FOUND. + */ + if (create_new) { + config->is_new = TRUE; + ret = TRUE; + } else { + fprintf (stderr, "Configuration file %s not found.\n" + "Try running 'notmuch setup' to create a configuration.\n", + config->filename); + } + } else { + fprintf (stderr, "Error reading configuration file %s: %s\n", + config->filename, error->message); + } + + g_error_free (error); + + return ret; +} + /* Open the named notmuch configuration file. If the filename is NULL, * the value of the environment variable $NOTMUCH_CONFIG will be used. * If $NOTMUCH_CONFIG is unset, the default configuration file @@ -289,36 +321,9 @@ notmuch_config_open (void *ctx, config->search_exclude_tags_length = 0; config->crypto_gpg_path = NULL; - if (! g_key_file_load_from_file (config->key_file, - config->filename, - G_KEY_FILE_KEEP_COMMENTS, - &error)) - { - if (error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT) { - /* If create_new is true, then the caller is prepared for a - * default configuration file in the case of FILE NOT - * FOUND. - */ - if (create_new) { - g_error_free (error); - config->is_new = TRUE; - } else { - fprintf (stderr, "Configuration file %s not found.\n" - "Try running 'notmuch setup' to create a configuration.\n", - config->filename); - talloc_free (config); - g_error_free (error); - return NULL; - } - } - else - { - fprintf (stderr, "Error reading configuration file %s: %s\n", - config->filename, error->message); - talloc_free (config); - g_error_free (error); - return NULL; - } + if (! get_config_from_file (config, create_new)) { + talloc_free (config); + return NULL; } /* Whenever we know of configuration sections that don't appear in -- 2.10.2