mysql-admutils/pwfile.c

107 lines
2.3 KiB
C

/*
* @($) $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"
// #include "pwyacc.h"
#include "toml.h"
/* defaults for configurable values */
const char* db_server = NULL;
const char* db_user = NULL;
const char* db_passwd = NULL ;
const char* db_name = "mysql";
extern int yyparse(void);
extern FILE *yyin;
int config_line = 1;
FILE* fp;
toml_table_t* conf;
toml_table_t* server;
const char* raw;
char* host;
char* user;
char* password;
char errbuf[200];
static char *tomlfile = SYSCONFDIR "/mysql-admutils.toml";
const char* read_toml_file(void)
{
if (0 == (fp = fopen(tomlfile, "r"))) {
perror("fopen");
exit(1);
}
conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if (0 == conf) {
fprintf(stderr, "ERROR: parsing %s\n", errbuf);
exit(1);
}
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);
}
if (0 == (raw = toml_raw_in(server, "password"))) {
fprintf(stderr, "ERROR: missing 'password' in [server]\n");
toml_free(conf);
exit(1);
}
if (toml_rtos(raw, &password)) {
fprintf(stderr, "ERROR: bad value in 'password'\n");
toml_free(conf);
exit(1);
}
toml_free(conf);
db_server = host;
db_user = user;
db_passwd = password;
return db_server;
return db_user;
return db_passwd;
free(host);
free(user);
free(password);
}