Handling search of config file in multiple directories, additional debug messages
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 2 Feb 2016 22:48:42 +0000 (23:48 +0100)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 2 Feb 2016 22:48:42 +0000 (23:48 +0100)
Makefile.am
src/global/vlp/ConfigurationFinder.cpp [new file with mode: 0644]
src/global/vlp/ConfigurationFinder.h [new file with mode: 0644]
src/global/vlp/QtConfigurationFinder.cpp [new file with mode: 0644]
src/global/vlp/QtConfigurationFinder.h [new file with mode: 0644]
src/global/vlp/exception/ConfigFileNotFound.h [new file with mode: 0644]
src/int/cint.c
src/kernel/kernel.cpp

index 25ac710df92557acd7245b9212042199b038a12e..d94f59663ce410c7be7fa4306c5d5d07f5f17fa3 100644 (file)
@@ -7,7 +7,16 @@ MOC = ${MOCDIR}/${MOCNAME}
 #AM_CXXFLAGS = -m32
 
 # @TODO: move --std=c++11 to the autoconf
-AM_CXXFLAGS = -I$(top_srcdir)/src/global -I${QTDIR}/include/qt4 -pedantic -Wall -Wextra -DQT3_SUPPORT --std=c++11
+AM_CPPFLAGS =
+AM_CXXFLAGS = \
+       -I$(top_srcdir)/src/global \
+       -I${QTDIR}/include/qt4 \
+       -pedantic \
+       -Wall \
+       -Wextra \
+       -DQT3_SUPPORT \
+       -DSYSCONFDIR='"$(sysconfdir)"' \
+       --std=c++11
 AM_LDFLAGS= -fPIC -L$(QTDIR)/lib -lQtCore -lQtGui -lQt3Support
 
 EXTRA_DIST = \
@@ -36,8 +45,8 @@ BUILT_SOURCES = \
   src/net/lognet.moc.cpp \
   src/kernel/kernel.moc.cpp \
   src/edit/editor.moc.cpp \
-  src/lgconfig/lgconfig.moc.cpp \
-  src/preproc/prep.moc.cpp
+  src/lgconfig/lgconfig.moc.cpp
+
 #  \
 #  src/help/help.moc
 
@@ -85,7 +94,9 @@ bin_logker_SOURCES = \
        src/kernel/OptionsDialog.cpp \
        src/kernel/OptionsDialog.moc.cpp \
        src/kernel/MessageDialog.cpp \
-       src/global/vlp/config.cpp
+       src/global/vlp/config.cpp \
+       src/global/vlp/ConfigurationFinder.cpp \
+       src/global/vlp/QtConfigurationFinder.cpp
 bin_logker_CPPFLAGS = $(bin_logker_CFLAGS)
 bin_logker_LDADD = $(bin_logker_LIBS) -lconfig++ -lX11
 bin_logkerdir = src/kernel
@@ -102,7 +113,10 @@ bin_logker_HEADERS = \
        src/kernel/ui/dialogs/LockDialog.h \
        src/kernel/ui/dialogs/OptionsDialog.h \
        src/kernel/ui/dialogs/MessageDialog.h \
-       src/global/vlp/config.h
+       src/global/vlp/config.h \
+       src/global/vlp/ConfigurationFinder.h \
+       src/global/vlp/QtConfigurationFinder.h \
+       src/global/vlp/exception/ConfigFileNotFound.h
 src/kernel/kernel.moc.cpp: \
                src/kernel/kernel.h \
                src/kernel/ui/KernelWindow.h \
@@ -291,9 +305,16 @@ clean-lgconfig-extra:
 
 bin_logcomp_SOURCES = \
        src/preproc/prep.cpp \
-       src/preproc/prep.moc.cpp
+       src/preproc/prep.moc.cpp \
+       src/global/vlp/ConfigurationFinder.cpp \
+       src/global/vlp/QtConfigurationFinder.cpp
 bin_logcomp_CPPFLAGS = $(bin_logcomp_CFLAGS)
 bin_logcomp_LDADD = $(bin_logcomp_LIBS)
+bin_lgconfigdir = src/preproc
+bin_lgconfig_HEADERS = \
+       src/global/vlp/ConfigurationFinder.h \
+       src/global/vlp/QtConfigurationFinder.h \
+       src/global/vlp/exception/ConfigFileNotFound.h
 
 src/preproc/prep.moc.cpp: src/preproc/prep.cpp 
        $(MOC) src/preproc/prep.cpp -o src/preproc/prep.moc.cpp
diff --git a/src/global/vlp/ConfigurationFinder.cpp b/src/global/vlp/ConfigurationFinder.cpp
new file mode 100644 (file)
index 0000000..236b1db
--- /dev/null
@@ -0,0 +1,61 @@
+#include <string>
+#include <fstream>
+
+#include <stdio.h>
+
+#include "ConfigurationFinder.h"
+#include "config.h"
+#include "exception/ConfigFileNotFound.h"
+
+namespace loglan {
+namespace vlp {
+
+ConfigurationFinder::ConfigurationFinder()
+{
+}
+
+std::string ConfigurationFinder::findConfig(std::string filename)
+{
+       for (std::string dir : searchDirs) {
+               std::ifstream infile(dir + filename);
+               if (infile.good()) {
+                       return dir + filename;
+               }
+       }
+
+       throw exception::ConfigFileNotFound();
+}
+
+/**
+ * Gets path to system-wide settings directory
+ *
+ * @return directory of user settings
+ */
+std::string ConfigurationFinder::getSystemConfigurationDir()
+{
+       std::string dir(SYSCONFDIR);
+
+       dir += VLP_CONFIG_PATH_SEPARATOR;
+       dir += "loglan";
+       dir += VLP_CONFIG_PATH_SEPARATOR;
+
+       return dir;
+}
+
+
+void ConfigurationFinder::addSearchDir(std::string dir)
+{
+       searchDirs.push_back(dir);
+}
+
+void ConfigurationFinder::initSearchDirs()
+{
+       searchDirs.clear();
+
+       addSearchDir("./");
+       addSearchDir(getUserConfigurationDir());
+       addSearchDir(getSystemConfigurationDir());
+}
+
+}
+}
\ No newline at end of file
diff --git a/src/global/vlp/ConfigurationFinder.h b/src/global/vlp/ConfigurationFinder.h
new file mode 100644 (file)
index 0000000..9005139
--- /dev/null
@@ -0,0 +1,83 @@
+#ifndef __VLP_CONFIGURATIONFINDER_H
+#define __VLP_CONFIGURATIONFINDER_H
+
+#include <string>
+#include <vector>
+
+#include "config.h"
+
+/**
+ * @file
+ * @brief Configuration file finding utilities
+ * @author Rafał Długołęcki
+ */
+
+namespace loglan {
+namespace vlp {
+
+#if defined(WIN32) || defined(_WIN32) 
+# define VLP_CONFIG_PATH_SEPARATOR "\\"
+#else
+# define VLP_CONFIG_PATH_SEPARATOR "/"
+#endif
+
+class ConfigurationFinder {
+private:
+       std::vector<std::string> searchDirs;
+
+       /**
+        * Adds directory to the list of search directories
+        */
+       void addSearchDir(std::string dir);
+public:
+       /**
+        * Default name of configuration file
+        */
+       static const constexpr char * DEFAULT_CONFIG_FILENAME = "vlp.cfg";
+
+       ConfigurationFinder();
+
+       /**
+        * Initializes list of search directories
+        */
+       void initSearchDirs();
+
+       /**
+        * Finds config file in possible config directories
+        *
+        * Search is made in following steps:
+        *  1. Check existence of filename in current execution path dir
+        *  2. If not found, check in user home directory, e.g:
+        *      - C:/Documents and Settings/Username
+        *      - ~/
+        *  3. If not found, check in system config directory (build dependent),
+        *     e.g.:
+        *      - /usr/local/etc/loglan/
+        *      - /etc/loglan/
+        *
+        * @param filename name of the file to search for
+        * @return path to the filename
+        *
+        */
+       std::string findConfig(std::string filename = DEFAULT_CONFIG_FILENAME);
+
+       /**
+        * Gets path to user local settings directory
+        *
+        * @return directory of user settings
+        */
+       virtual std::string getUserConfigurationDir() = 0;
+
+       /**
+        * Gets path to system-wide settings directory
+        *
+        * @return directory of user settings
+        */
+       std::string getSystemConfigurationDir();
+};
+
+}
+}
+
+
+#endif /* __VLP_CONFIGURATIONFINDER_H */
\ No newline at end of file
diff --git a/src/global/vlp/QtConfigurationFinder.cpp b/src/global/vlp/QtConfigurationFinder.cpp
new file mode 100644 (file)
index 0000000..23a33cb
--- /dev/null
@@ -0,0 +1,20 @@
+#include <QtCore/QString>
+#include <QtCore/QDir>
+#include <string>
+
+#include "QtConfigurationFinder.h"
+
+namespace loglan {
+namespace vlp {
+
+std::string QtConfigurationFinder::getUserConfigurationDir()
+{
+       QString dir = QDir::homePath()
+               + QDir::separator()
+               + ".loglan"
+               + QDir::separator();
+       return dir.toStdString();
+}
+
+}
+}
\ No newline at end of file
diff --git a/src/global/vlp/QtConfigurationFinder.h b/src/global/vlp/QtConfigurationFinder.h
new file mode 100644 (file)
index 0000000..178df8a
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef __VLP_QTCONFIGURATIONFINDER_H
+#define __VLP_QTCONFIGURATIONFINDER_H
+
+#include <string>
+#include "ConfigurationFinder.h"
+
+/**
+ * @file
+ * @brief Qt Configuration file finding utilities
+ * @author Rafał Długołęcki
+ */
+
+namespace loglan {
+namespace vlp {
+
+/**
+ * Qt implementation of ConfigurationFinder
+ */
+class QtConfigurationFinder : public ConfigurationFinder {
+public:
+       virtual std::string getUserConfigurationDir();
+};
+
+}
+}
+
+
+#endif /* __VLP_QTCONFIGURATIONFINDER_H */
\ No newline at end of file
diff --git a/src/global/vlp/exception/ConfigFileNotFound.h b/src/global/vlp/exception/ConfigFileNotFound.h
new file mode 100644 (file)
index 0000000..25464a3
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef __VLP_EXCEPTION_CONFIGFILENOTFOUND_H
+#define __VLP_EXCEPTION_CONFIGFILENOTFOUND_H
+
+#include <stdexcept>
+
+/**
+ * @file
+ * @author Rafał Długołęcki
+ */
+
+namespace loglan {
+namespace vlp {
+namespace exception {
+
+/**
+ * Exception thrwon when configuration file has not been found
+ */
+class ConfigFileNotFound : public std::runtime_error
+{
+public:
+       ConfigFileNotFound() :
+               std::runtime_error("Configuration file has not been found") {
+       }
+};
+
+}
+}
+}
+
+#endif /* __VLP_EXCEPTION_CONFIGFILENOTFOUND_H */
index e67e8b6c10d86ee6fc4df0c261a5551893a2f59f..03bc54642800052ba2b18b3730f21149699756a3 100644 (file)
@@ -196,6 +196,7 @@ static void initiate(int argc, char **argv)
        svr.sun_family = AF_UNIX;
        strcpy(svr.sun_path, mynname);
        len = strlen(svr.sun_path) + sizeof(svr.sun_family);
+       fprintf(stderr, "logint: Binding to socket: %s\n", svr.sun_path);
        bind(sock, (struct sockaddr*)&svr, len);
        listen(sock, 5);
 
@@ -212,13 +213,14 @@ static void initiate(int argc, char **argv)
        strcpy(svr.sun_path, argv[1]);
        strcpy(mykname, argv[1]);
        len = strlen(svr.sun_path) + sizeof(svr.sun_family);
+       fprintf(stderr, "logint: Connecting to socket: %s\n", svr.sun_path);
        i = connect(internal_sock, (struct sockaddr*)&svr, len);
 
        if (i==0) {
                fcntl(internal_sock,F_SETFL, O_NONBLOCK |
                                                fcntl(internal_sock,F_GETFL,0));
        }
-       else
+       else {
                while (i!=0) {
                        close(internal_sock);
                        internal_sock = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -226,6 +228,7 @@ static void initiate(int argc, char **argv)
                                        fcntl(internal_sock, F_GETFL, 0));
                        i = connect(internal_sock, (struct sockaddr*)&svr, len);
                }
+       }
        on = 1;
        setsockopt(internal_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on,
                                                                sizeof(on));
@@ -247,10 +250,13 @@ static void initiate(int argc, char **argv)
        close(sock);
 
        /* load code and prototypes */
-       if (filename != NULL)
+       if (filename != NULL) {
+               fprintf(stderr, "logint: loading code: %s\n", filename);
                load(filename);
-       else
+       }
+       else {
                usage();
+       }
 }
 
 /**
@@ -796,14 +802,20 @@ void send_ready()
  */
 int main(int argc, char **argv)
 {
+       fprintf(stderr, "logint: initializing\n");
        /* initialize executor */
        initiate(argc, argv);
        /* initialize running system */
+       fprintf(stderr, "logint: runsys\n");
        runsys();
+       fprintf(stderr, "logint: initializing scheduler\n");
        init_scheduler();
+       fprintf(stderr, "logint: acquiring GRAPH resource\n");
        GraphRes = get_graph_res();
-       if (GraphRes < 0)
+       if (GraphRes < 0) {
+               fprintf(stderr, "logint:  > acquiring GRAPH resource failed\n");
                exit(12);
+       }
 
        request_id();
        if (remote)
index 1f7d383ebc8f6500ef184fe7943a5d64d4753574..1c0b7921b96806dd5accaecddd8398e0973090b9 100644 (file)
@@ -73,6 +73,8 @@
 #include "MessageDialog.h"
 
 #include "vlp/config.h"
+#include "vlp/QtConfigurationFinder.h"
+#include "vlp/exception/ConfigFileNotFound.h"
 
 #include <sys/prctl.h>
 /* File resides in top directory (where are Makefiles)*/
@@ -126,12 +128,6 @@ QKernel::QKernel(int argc, char **argv)
 {
        setupUi(this);
 
-       QString arg0(argv[0]);
-       arg0 += "/";
-       homeDir = QDir(arg0);
-       homeDir.cdUp();
-
-       
        int i;
        for(i = 0; (i < 5) && (i < argc-1); i++) {
                strcpy(myargs[i], "");
@@ -140,6 +136,9 @@ QKernel::QKernel(int argc, char **argv)
                }
        }
 
+       loglan::vlp::QtConfigurationFinder configFinder;
+       configFinder.initSearchDirs();
+
        QDir q(getRemoteDir());
 
        if (!q.exists()) {
@@ -158,7 +157,7 @@ QKernel::QKernel(int argc, char **argv)
        ActiveConnections = 0;
        strcpy(LockPasswd, "");
 
-       loadConfig(getConfigFilePath());
+       loadConfig(configFinder.findConfig().c_str());
 
        RunNetModule();
 
@@ -215,6 +214,8 @@ void QKernel::loadConfig(const char * fname)
                }
 
                strncpy(progdir, config.getProgramDir(), 256);
+
+               homeDir = QDir(QCoreApplication::applicationDirPath());
        }
 }
 
@@ -249,6 +250,7 @@ void QKernel::on_actionEditor_triggered()
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
+                       "logedit",
                        getHomeDir(),
                        myargs[0],
                        myargs[1],
@@ -285,6 +287,7 @@ void QKernel::on_actionHelp_triggered()
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
+                       "loghelp",
                        docDir.toStdString().c_str(),
                        myargs[0],
                        myargs[1],
@@ -319,6 +322,7 @@ void QKernel::RunGraphModule(char *sk)
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
+                       "loggraph",
                        program.toStdString().c_str(),
                        sk,
                        myargs[0],
@@ -362,7 +366,7 @@ void QKernel::RunNetModule()
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
-                       program.toStdString().c_str(),
+                       "lognet",
                        getNetModuleSocket(),
                        getConfigFilePath().toStdString().c_str(),
                        myargs[0],
@@ -922,7 +926,7 @@ InterpEntry *QKernel::RunIntModule(char *ss, int r)
        strcpy(newINT->shortname, b);
        strcpy(newINT->fullname, ss);
 
-       sprintf(a, "%s%d", IPATH, newint);
+       sprintf(a, "%s%d", homeDir.absoluteFilePath(IPATH).toStdString().c_str(), newint);
        sprintf(cmd, "%s/modules/logint %s %s",
                getHomeDir(),
                a,
@@ -930,9 +934,11 @@ InterpEntry *QKernel::RunIntModule(char *ss, int r)
        if (r) {
                strcat(cmd, " r");
        }
-       sprintf(b, " %s %s %s %s %s",
-               myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
-       strcat(cmd, b);
+//     sprintf(b, " %s %s %s %s %s",
+//             myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
+//     sprintf(cmd, "%s %s", cmd, b);
+//     strcat(cmd, b);
+       fprintf(stderr, "%s\n", cmd);
        strcat(cmd, " &");
 
        sock = socket(AF_UNIX, SOCK_STREAM, 0);