mysql-admutils/pwfile.c

107 lines
2.3 KiB
C
Raw Permalink Normal View History

/*
* @($) $Id: pwfile.c,v 1.3 2007-02-27 14:10:08 geirha Exp $
*
* functions for parsing the config file.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/resource.h>
#include "mysql-admutils.h"
2018-10-26 16:53:47 +02:00
// #include "pwyacc.h"
#include "toml.h"
/* defaults for configurable values */
const char* db_server = NULL;
const char* db_user = NULL;
2018-10-26 16:53:47 +02:00
const char* db_passwd = NULL ;
const char* db_name = "mysql";
extern int yyparse(void);
extern FILE *yyin;
int config_line = 1;
2018-10-26 16:53:47 +02:00
FILE* fp;
toml_table_t* conf;
toml_table_t* server;
const char* raw;
char* host;
char* user;
2018-10-26 16:53:47 +02:00
char* password;
char errbuf[200];
2018-10-26 16:53:47 +02:00
static char *tomlfile = SYSCONFDIR "/mysql-admutils.toml";
const char* read_toml_file(void)
{
2018-10-26 16:53:47 +02:00
if (0 == (fp = fopen(tomlfile, "r"))) {
perror("fopen");
exit(1);
}
2018-10-26 16:53:47 +02:00
conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if (0 == conf) {
fprintf(stderr, "ERROR: parsing %s\n", errbuf);
2018-10-26 16:53:47 +02:00
exit(1);
}
2018-10-26 16:53:47 +02:00
if (0 == (server = toml_table_in(conf, "server"))) {
fprintf(stderr, "ERROR: missing [server]\n");
toml_free(conf);
exit(1);
}
if (0 == (raw = toml_raw_in(server, "host"))) {
fprintf(stderr, "ERROR: missing 'host' in [server]\n");
toml_free(conf);
exit(1);
}
if (toml_rtos(raw, &host)) {
fprintf(stderr, "ERROR: bad value in 'host'\n");
toml_free(conf);
exit(1);
}
if (0 == (raw = toml_raw_in(server, "user"))) {
fprintf(stderr, "ERROR: missing 'user' in [server]\n");
toml_free(conf);
exit(1);
}
if (toml_rtos(raw, &user)) {
fprintf(stderr, "ERROR: bad value in 'user'\n");
toml_free(conf);
exit(1);
}
2018-10-26 16:53:47 +02:00
if (0 == (raw = toml_raw_in(server, "password"))) {
fprintf(stderr, "ERROR: missing 'password' in [server]\n");
toml_free(conf);
exit(1);
}
2018-10-26 16:53:47 +02:00
if (toml_rtos(raw, &password)) {
fprintf(stderr, "ERROR: bad value in 'password'\n");
2018-10-26 16:53:47 +02:00
toml_free(conf);
exit(1);
}
toml_free(conf);
db_server = host;
db_user = user;
2018-10-26 16:53:47 +02:00
db_passwd = password;
return db_server;
return db_user;
2018-10-26 16:53:47 +02:00
return db_passwd;
free(host);
free(user);
2018-10-26 16:53:47 +02:00
free(password);
}