Replaced custom config file reader with libconfig implementation. Created stub class for future changes in config reader.
clean-loggraph-extra:
rm -f src/graph/*.moc.cpp
-bin_lognet_SOURCES = src/net/lognet.cpp src/net/lognet.moc.cpp
+bin_lognet_SOURCES = src/net/lognet.cpp src/net/lognet.moc.cpp src/global/AppConfiguration.cpp
bin_lognet_CPPFLAGS = $(bin_lognet_CFLAGS)
bin_lognet_LDADD = $(bin_lognet_LIBS)
src/net/lognet.moc.cpp:
clean-lognet-extra:
rm -f src/net/*.moc.cpp
-bin_logker_SOURCES = src/kernel/kernel.cpp src/kernel/kernel.moc
+bin_logker_SOURCES = src/kernel/kernel.cpp src/kernel/kernel.moc src/global/AppConfiguration.cpp
bin_logker_CPPFLAGS = $(bin_logker_CFLAGS)
bin_logker_LDADD = $(bin_logker_LIBS)
src/kernel/kernel.moc:
# libqt3-mt-dev:i386
# qt3-dev-tools:i386
PKG_CHECK_MODULES([bin_loggraph], [qt-mt < 4.0])
-PKG_CHECK_MODULES([bin_lognet], [qt-mt < 4.0])
-PKG_CHECK_MODULES([bin_logker], [qt-mt < 4.0])
+PKG_CHECK_MODULES([bin_lognet], [qt-mt < 4.0 libconfig])
+PKG_CHECK_MODULES([bin_logker], [qt-mt < 4.0 libconfig])
#PKG_CHECK_MODULES([bin_logint], [qt-mt < 4.0])
PKG_CHECK_MODULES([bin_logedit], [qt-mt < 4.0])
PKG_CHECK_MODULES([bin_lgconfig], [qt-mt < 4.0])
--- /dev/null
+#include "AppConfiguration.h"
+
+void AppConfiguration::error(config_t *cfg)
+{
+ fprintf(stderr, "%s: In file %s, line %d\n",
+ config_error_text(cfg),
+ config_error_file(cfg),
+ config_error_line(cfg));
+}
--- /dev/null
+#include <libconfig.h>
+
+/**
+ * Class for maintaining application configuration.
+ */
+class AppConfiguration {
+protected:
+public:
+ static void error(config_t *cfg);
+};
#include "socu.h"
#include <netinet/in.h>
+#include <libconfig.h>
+#include "AppConfiguration.h"
+
#define GPATH "loggr"
#define IPATH "logi"
#define NPATH "logn"
bool info_messages;
- void LoadConfig();
+ void LoadConfig(char *);
void RunGraphModule(char*);
void RunNetModule();
InterpEntry *findINTbySocket(int);
freeINTid = 1;
ActiveConnections = 0;
strcpy(LockPasswd,"");
- LoadConfig();
+ LoadConfig("vlp.cfg");
RunNetModule();
Net_Notify = new QSocketNotifier(net_sock,QSocketNotifier::Read,this);
/* ########### load configuration from file ############# */
-void QKernel::LoadConfig()
+void QKernel::LoadConfig(char * fname)
{
-QFile f("vlp.cfg");
- QString line;
- QString val;
- int br=0;
+ config_t cfg;
+ const char *str;
- if (!f.exists())
- {
- WriteMessage("Cannot load configuration file!");sleep(2);exit(3);
+ config_init(&cfg);
+
+ /* Hack for checking if file exists without using external libs.*/
+ FILE * file = fopen(fname, "r");
+ if (!file) {
+ fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname);
+ exit(3);
}
- f.open(IO_ReadOnly);
- br = f.readLine(line,256);
- while (br>0)
- {
- QStringList l = QStringList::split("=",line,FALSE);
- QStringList::Iterator it = l.begin();
- line = *it;
- ++it;
- val = *it;
- val = val.stripWhiteSpace();
- if (line == "node_number") {NodeNumber = val.toInt();};
- if (line == "type") {if (val=="explicit") ConType=1; else
- ConType = 2; };
- if (line == "host" ) {ConnectList.append(new ConnectEntry((char*)val.ascii()));};
- if (line == "progdir") { strcpy(progdir,val.ascii());};
- if (line == "homedir") { strcpy(HomeDir,val.ascii());};
- br = f.readLine(line,256);
- }
- f.close();
+ /* File exists, so file has been locked. Release it. */
+ fclose(file);
+
+ /* Read the file. If there is an error, report it and exit. */
+ if(!config_read_file(&cfg, fname))
+ {
+ AppConfiguration::error(&cfg);
+ config_destroy(&cfg);
+ exit(3);/* from original code. */
+ }
+
+ if(!config_lookup_int(&cfg, "node_number", &NodeNumber))
+ {
+ AppConfiguration::error(&cfg);
+ config_destroy(&cfg);
+ exit(3);
+ }
+
+ if(config_lookup_string(&cfg, "type", &str)){
+ ConType = (strcmp(str, "explicit") == 0) ? 1 : 2;
+ }
+ else {
+ AppConfiguration::error(&cfg);
+ }
+
+
+ if(config_lookup_string(&cfg, "host", &str)) {
+ char host[255];
+ strcpy(host, str);//FIXME: buffer overflow
+ ConnectList.append(new ConnectEntry(host));
+ }
+ else {
+ AppConfiguration::error(&cfg);
+ }
+
+ if(config_lookup_string(&cfg, "progdir", &str)){
+ strcpy(progdir, str);//FIXME: buffer overflow
+ }
+ else {
+ AppConfiguration::error(&cfg);
+ }
+
+ if(config_lookup_string(&cfg, "homedir", &str)){
+ strcpy(HomeDir, str);//FIXME: buffer overflow
+ }
+ else {
+ AppConfiguration::error(&cfg);
+ }
+
+ config_destroy(&cfg);
}
/* +++++++++++++++++++++++++++++++++++++++++++++++ */
#include <qstringlist.h>
#include <unistd.h>
+#include <libconfig.h>
+#include "AppConfiguration.h"
+
#define REMOTE_PATH "REMOTE"
#define MAXLINKS 30
#define LOGPORT 3600
void NETMOD::load_config(char *fname)
{
- QFile f(fname);
- QString line;
- QString val;
- int br=0,on,k=0;
- NETlink *pomlink;
+ config_t cfg;
+ const char *str;
+ int on,k=0;
+ NETlink *pomlink;
- if (!f.exists())
- {
- write_at_console("Cannot load configuration file!");sleep(2);exit(3);
- }
- f.open(IO_ReadOnly);
- br = f.readLine(line,256);
- while (br>0)
- {
- QStringList l = QStringList::split("=",line,FALSE);
- QStringList::Iterator it = l.begin();
- line = *it;
- ++it;
- val = *it;
- val = val.stripWhiteSpace();
- if (line == "node_number") {MyNode = val.toInt();};
- if (line == "host" ) {
- k++;
- pomlink = new NETlink;
- strcpy(pomlink->addr,val.ascii());
- pomlink->connected = FALSE;
- pomlink->sock = socket(AF_INET, SOCK_STREAM, 0);
- fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0));
- on=1;
- setsockopt(pomlink->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&on,sizeof(on));
- Links.append(pomlink);
- to_connect++;
- };
- br = f.readLine(line,256);
- }
- f.close();
- if (k==0) all_connected=TRUE;
- if (MyNode==-1) {write_at_console("Node number must be specified");exit(1);};
+ config_init(&cfg);
+
+
+ /* Hack for checking if file exists without using external libs.*/
+ FILE * file = fopen(fname, "r");
+ if (!file) {
+ fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname);
+ write_at_console("Cannot load configuration file!");
+ exit(3);
+ }
+ /* File exists, so file has been locked. Release it. */
+ fclose(file);
+
+ /* Read the file. If there is an error, report it and exit. */
+ if(!config_read_file(&cfg, fname))
+ {
+ AppConfiguration::error(&cfg);
+ config_destroy(&cfg);
+ exit(3);/* from original code. */
+ }
+
+ if(config_lookup_int(&cfg, "node_number", &MyNode))
+ {
+ if (MyNode==-1) {
+ write_at_console("Node number must be specified");
+ config_destroy(&cfg);
+ exit(1);
+ };
+ }
+ else
+ {
+ AppConfiguration::error(&cfg);
+ config_destroy(&cfg);
+ exit(1);
+ }
+
+ if(config_lookup_string(&cfg, "host", &str)) {
+ k++;
+ pomlink = new NETlink;
+ strcpy(pomlink->addr, str);
+ pomlink->connected = FALSE;
+ pomlink->sock = socket(AF_INET, SOCK_STREAM, 0);
+ fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0));
+ on=1;
+ setsockopt(pomlink->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&on,sizeof(on));
+ Links.append(pomlink);
+ to_connect++;
+ }
+ else {
+ AppConfiguration::error(&cfg);
+ }
+
+ config_destroy(&cfg);
+
+ if (k==0) all_connected=TRUE;
}