bmsd/rdconfig.c

Wed, 04 Mar 2020 10:48:21 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 04 Mar 2020 10:48:21 +0100
changeset 622
39902353b159
parent 194
d202777ebae5
child 696
f4de55f587c1
permissions
-rw-r--r--

Workaround for hang during startup. Changed order of displayed fields on the yeast tab. Tooltip text changes. More decimals on the checklist for misc ingredients. More decimals for the starter on product print.

/*****************************************************************************
 * Copyright (C) 2017-2019
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of the bms (Brewery Management System)
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * bms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "bms.h"
#include "xutil.h"
#include "futil.h"
#include "rdconfig.h"


static char	*mypath;
static char	*k, *v;
static int	linecnt = 0;

extern int	debug;
extern char	*Private_Path;


sys_config	Config;


static int getstr(char **);
static int getint(char **);


key_list keytab[] = {
    {(char *)"bms_name",		getstr,		(char **)&Config.bms_name},
    {(char *)"bms_uuid",		getstr,		(char **)&Config.bms_uuid},
    {(char *)"mysql_host",		getstr,		(char **)&Config.mysql_host},
    {(char *)"mysql_port",		getint,		(char **)&Config.mysql_port},
    {(char *)"mysql_user",		getstr,		(char **)&Config.mysql_user},
    {(char *)"mysql_pass",		getstr,		(char **)&Config.mysql_pass},
    {(char *)"mysql_database",		getstr,		(char **)&Config.mysql_database},
    {(char *)"mqtt_host",		getstr,		(char **)&Config.mqtt_host},
    {(char *)"mqtt_port",		getint,		(char **)&Config.mqtt_port},
    {(char *)"mqtt_user",		getstr,		(char **)&Config.mqtt_user},
    {(char *)"mqtt_pass",		getstr,		(char **)&Config.mqtt_pass},
    {(char *)"web_root",		getstr,		(char **)&Config.web_root},
    {NULL,				NULL,		NULL}
};



/*
 * Kill configuration data
 */
void killconfig(void)
{
    if (Config.bms_name)
	free(Config.bms_name);
    Config.bms_name = NULL;
    if (Config.bms_uuid)
	free(Config.bms_uuid);
    Config.bms_uuid = NULL;

    if (Config.mysql_host)
	free(Config.mysql_host);
    Config.mysql_host = NULL;
    Config.mysql_port = 3306;
    if (Config.mysql_user)
	free(Config.mysql_user);
    Config.mysql_user = NULL;
    if (Config.mysql_pass)
	free(Config.mysql_pass);
    Config.mysql_pass = NULL;
    if (Config.mysql_database)
	free(Config.mysql_database);
    Config.mysql_database = NULL;

    if (Config.mqtt_host)
	free(Config.mqtt_host);
    Config.mqtt_host = NULL;
    Config.mqtt_port = 1883;
    if (Config.mqtt_user)
	free(Config.mqtt_user);
    Config.mqtt_user = NULL;
    if (Config.mqtt_pass)
	free(Config.mqtt_pass);
    Config.mqtt_pass = NULL;

    if (Config.web_root)
	free(Config.web_root);
    Config.web_root = NULL;
}



int wrconfig(void)
{
    FILE	*fp;

    if (mypath)
	free(mypath);
    mypath = xstrcpy(Private_Path);
    mypath = xstrcat(mypath, (char *)"/bms.config");
    if (mkdirs(mypath, 0755) == FALSE)
	return 1;

    if ((fp = fopen(mypath, "w")) == NULL) {
	syslog(LOG_NOTICE, "[rdconfig] could not rewrite %s", mypath);
	return 1;
    }

    fprintf(fp, "# Configuration file for BMS %s\n", VERSION);
    fprintf(fp, "#\n");
    if (Config.bms_name)
	fprintf(fp, "bms_name              %s\n", Config.bms_name);
    if (Config.bms_uuid)
	fprintf(fp, "bms_uuid              %s\n", Config.bms_uuid);
    fprintf(fp, "#\n");

    if (Config.mysql_host)
	fprintf(fp, "mysql_host            %s\n", Config.mysql_host);
    fprintf(fp, "mysql_port            %d\n", Config.mysql_port);
    if (Config.mysql_user)
	fprintf(fp, "mysql_user            %s\n", Config.mysql_user);
    else
	fprintf(fp, "#mysql_user            <username>\n");
    if (Config.mysql_pass)
	fprintf(fp, "mysql_pass             %s\n", Config.mysql_pass);
    else
    	fprintf(fp, "#mysql_pass             <passwd>\n");
    if (Config.mysql_database)
	fprintf(fp, "mysql_database        %s\n", Config.mysql_database);
    else
	fprintf(fp, "#mysql_database        <dbname>\n");
    fprintf(fp, "#\n");

    if (Config.mqtt_host)
	fprintf(fp, "mqtt_host             %s\n", Config.mqtt_host);
    fprintf(fp, "mqtt_port             %d\n", Config.mqtt_port);
    if (Config.mqtt_user)
	fprintf(fp, "mqtt_user             %s\n", Config.mqtt_user);
    else
	fprintf(fp, "#mqtt_user             <username>\n");
    if (Config.mqtt_pass)
	fprintf(fp, "mqtt_pass             %s\n", Config.mqtt_pass);
    else
	fprintf(fp, "#mqtt_pwd              <passwd>\n");
    fprintf(fp, "#\n");

    if (Config.web_root)
	fprintf(fp, "web_root              %s\n", Config.web_root);
    else
	fprintf(fp, "#web_root              </path/to/webroot>\n");
    fprintf(fp, "#\n");
    fprintf(fp, "# End of generated configuration\n");
    fclose(fp);
    free(mypath);
    mypath = NULL;
    return 0;
}



int rdconfig(void)
{
    FILE    	*fp;
    uuid_t	uu;
    char	buf[256], *p;
    int		i, rc = 0;

    if (mypath)
	free(mypath);
    mypath = xstrcpy(Private_Path);
    mypath = xstrcat(mypath, (char *)"/bms.config");

    killconfig();

    if (debug)
	fprintf(stdout, "[rdconfig] reading %s\n", mypath);

    if ((fp = fopen(mypath, "r")) == NULL) {
	syslog(LOG_NOTICE, "[rdconfig] cannot open %s", mypath);
	Config.bms_name = xstrcpy((char *)"My Brewery");
	Config.bms_uuid = malloc(37);
	uuid_generate(uu);
	uuid_unparse(uu, Config.bms_uuid);
	Config.mysql_port = 3306;
	Config.mysql_host = xstrcpy((char *)"localhost");
	Config.mqtt_port = 1883;
	Config.mqtt_host = xstrcpy((char *)"localhost");
	return wrconfig();
    }

    linecnt = 0;
    while (fgets(buf, sizeof(buf) -1, fp)) {
	linecnt++;
	if (*(p = buf + strlen(buf) -1) != '\n') {
	    syslog(LOG_NOTICE, "[rdconfig] %s(%d): \"%s\" - line too long", mypath, linecnt, buf);
	    rc = 1;
	    break;
	}
	*p-- = '\0';
	while ((p >= buf) && isspace(*p))
	    *p-- = '\0';
	k = buf;
	while (*k && isspace(*k))
	    k++;
	p = k;
	while (*p && !isspace(*p))
	    p++;
	*p++='\0';
	v = p;
	while (*v && isspace(*v)) 
	    v++;

	if ((*k == '\0') || (*k == '#')) {
	    continue;
	}

	for (i = 0; keytab[i].key; i++)
	    if (strcasecmp(k,keytab[i].key) == 0)
		break;

	if (keytab[i].key == NULL) {
	    syslog(LOG_NOTICE, "[rdconfig] %s(%d): %s %s - unknown keyword", mypath, linecnt, S(k), S(v));
	    rc = 1;
	    break;
	} else if ((keytab[i].prc(keytab[i].dest))) {
	    rc = 1;
	    break;
	}
    }
    fclose(fp);

    free(mypath);
    mypath = NULL;

    return rc;
}



static int getstr(char **dest)
{
    if (debug)
    	fprintf(stdout, "[rdconfig] getstr: %s(%d): %s %s\n", mypath, linecnt, S(k), S(v));

    *dest = xstrcpy(v);
    return 0;
}



static int getint(char **dest)
{
    if (debug)
    	fprintf(stdout, "[rdconfig] getint: %s(%d): %s %s\n", mypath, linecnt, k, v);

    if (strspn(v,"0123456789") != strlen(v)) 
        syslog(LOG_NOTICE, "[rdconfig] %s(%d): %s %s - bad numeric", mypath, linecnt, S(k), S(v));
    else 
        *((int*)dest)=atoi(v);
    return 0;
}


mercurial