mysql-admutils/pwfile.c

81 lines
1.5 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_user = "root";
const char* db_server = "mysql.stud.ntnu.no";
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* password;
char errbuf[200];
static char *tomlfile = SYSCONFDIR "/mysql-admutils.toml";
int
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: %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, "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 'host'\n");
toml_free(conf);
exit(1);
}
toml_free(conf);
db_passwd = password;
return db_passwd;
free(password);
}