diff --git a/src/common/rclconfig.cpp b/src/common/rclconfig.cpp index 13d56926..8081063c 100644 --- a/src/common/rclconfig.cpp +++ b/src/common/rclconfig.cpp @@ -1,5 +1,5 @@ #ifndef lint -static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.23 2006-03-20 09:51:45 dockes Exp $ (C) 2004 J.F.Dockes"; +static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.24 2006-03-22 14:25:46 dockes Exp $ (C) 2004 J.F.Dockes"; #endif /* * This program is free software; you can redistribute it and/or modify @@ -30,7 +30,6 @@ static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.23 2006-03-20 09:51:45 dockes E #include "conftree.h" #include "debuglog.h" #include "smallut.h" -#include "copyfile.h" #ifndef NO_NAMESPACES using namespace std; @@ -55,53 +54,58 @@ RclConfig::RclConfig() m_confdir = path_home(); m_confdir += ".recoll/"; } - string cfilename = path_cat(m_confdir, "recoll.conf"); - if (access(m_confdir.c_str(), 0) != 0 || - access(cfilename.c_str(), 0) != 0) { + if (access(m_confdir.c_str(), 0) < 0) { if (!initUserConfig()) return; } - // Open readonly here so as not to casually create a config file - m_conf = new ConfTree(cfilename.c_str(), true); - if (m_conf == 0 || - (m_conf->getStatus() != ConfSimple::STATUS_RO && - m_conf->getStatus() != ConfSimple::STATUS_RW)) { - m_reason = string("No main configuration file: ") + cfilename + - " does not exist or cannot be parsed"; + listcfns; + string cpath; + + cpath = path_cat(m_confdir, "recoll.conf"); + cfns.push_back(cpath); + cpath = path_cat(m_datadir, "examples/recoll.conf"); + cfns.push_back(cpath); + m_conf = new ConfStack(cfns, true); + if (m_conf == 0 || !m_conf->ok()) { + m_reason = string("No main configuration file: "); + for (list::const_iterator it = cfns.begin(); it != cfns.end(); + it++) + m_reason += "[" + *it + "] "; + m_reason += " do not exist or cannot be parsed"; return; } - string mimemapfile; - if (!m_conf->get("mimemapfile", mimemapfile, "")) { - mimemapfile = "mimemap"; - } - string mpath = path_cat(m_confdir, mimemapfile); - mimemap = new ConfTree(mpath.c_str(), true); - if (mimemap == 0 || - (mimemap->getStatus() != ConfSimple::STATUS_RO && - mimemap->getStatus() != ConfSimple::STATUS_RW)) { - m_reason = string("No mime map configuration file: ") + mpath + - " does not exist or cannot be parsed"; + cfns.clear(); + cpath = path_cat(m_confdir, "mimemap"); + cfns.push_back(cpath); + cpath = path_cat(m_datadir, "examples/mimemap"); + cfns.push_back(cpath); + mimemap = new ConfStack(cfns, true); + if (mimemap == 0 || !mimemap->ok()) { + m_reason = string("No mime map configuration file: "); + for (list::const_iterator it = cfns.begin(); it != cfns.end(); + it++) + m_reason += "[" + *it + "] "; + m_reason += " do not exist or cannot be parsed"; return; } - // mimemap->list(); - string mimeconffile; - if (!m_conf->get("mimeconffile", mimeconffile, "")) { - mimeconffile = "mimeconf"; - } - mpath = path_cat(m_confdir, mimeconffile); - mimeconf = new ConfTree(mpath.c_str(), true); - if (mimeconf == 0 || - (mimeconf->getStatus() != ConfSimple::STATUS_RO && - mimeconf->getStatus() != ConfSimple::STATUS_RW)) { - m_reason = string("No mime configuration file: ") + mpath + - " does not exist or cannot be parsed"; + cfns.clear(); + cpath = path_cat(m_confdir, "mimeconf"); + cfns.push_back(cpath); + cpath = path_cat(m_datadir, "examples/mimeconf"); + cfns.push_back(cpath); + mimeconf = new ConfStack(cfns, true); + if (mimeconf == 0 || !mimeconf->ok()) { + m_reason = string("No mime configuration file: "); + for (list::const_iterator it = cfns.begin(); it != cfns.end(); + it++) + m_reason += "[" + *it + "] "; + m_reason += " do not exist or cannot be parsed"; return; } - // mimeconf->list(); setKeyDir(""); @@ -300,27 +304,44 @@ bool RclConfig::getUncompressor(const string &mtype, list& cmd) return true; } +static const char blurb0[] = +"# The system-wide configuration files for recoll are located in:\n" +"# %s\n" +"# The default configuration files are commented, you should take a look\n" +"# at them for an explanation of what can be set (you could also take a look\n" +"# at the manual instead).\n" +"# Values set in this file will override the system-wide values for the file\n" +"# with the same name in the central directory. The syntax for setting\n" +"# values is identical.\n" + ; -// Create initial user config by copying sample files +// Create initial user config by creating commented empty files static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf"}; static int ncffiles = sizeof(configfiles) / sizeof(char *); bool RclConfig::initUserConfig() { - // Samples directory + // Explanatory text + char blurb[sizeof(blurb0)+1025]; string exdir = path_cat(m_datadir, "examples"); - // User's - string recolldir = path_tildexpand("~/.recoll"); - if (mkdir(recolldir.c_str(), 0755) < 0) { - m_reason += string("mkdir(") + recolldir + ") failed: " + + sprintf(blurb, blurb0, exdir.c_str()); + + if (access(m_confdir.c_str(), 0) < 0 && + mkdir(m_confdir.c_str(), 0755) < 0) { + m_reason += string("mkdir(") + m_confdir + ") failed: " + strerror(errno); return false; } for (int i = 0; i < ncffiles; i++) { - string src = path_cat((const string&)exdir, string(configfiles[i])); - string dst = path_cat((const string&)recolldir, string(configfiles[i])); - if (!copyfile(src.c_str(), dst.c_str(), m_reason)) { - LOGERR(("Copyfile failed: %s\n", m_reason.c_str())); - return false; + string dst = path_cat(m_confdir, string(configfiles[i])); + if (access(dst.c_str(), 0) < 0) { + FILE *fp = fopen(dst.c_str(), "w"); + if (fp) { + fprintf(fp, "%s\n", blurb); + fclose(fp); + } else { + m_reason += string("fopen ") + dst + ": " + strerror(errno); + return false; + } } } return true; @@ -337,13 +358,11 @@ void RclConfig::initFrom(const RclConfig& r) m_keydir = r.m_datadir; // We should use reference-counted objects instead! if (r.m_conf) - m_conf = new ConfTree(*(r.m_conf)); + m_conf = new ConfStack(*(r.m_conf)); if (r.mimemap) - mimemap = new ConfTree(*(r.mimemap)); + mimemap = new ConfStack(*(r.mimemap)); if (r.mimeconf) - mimeconf = new ConfTree(*(r.mimeconf)); - if (r.mimemap_local) - mimemap_local = new ConfTree(*(r.mimemap_local)); + mimeconf = new ConfStack(*(r.mimeconf)); if (r.stopsuffixes) stopsuffixes = new std::list(*(r.stopsuffixes)); defcharset = r.defcharset; diff --git a/src/common/rclconfig.h b/src/common/rclconfig.h index f1bfe460..9cd30653 100644 --- a/src/common/rclconfig.h +++ b/src/common/rclconfig.h @@ -16,7 +16,7 @@ */ #ifndef _RCLCONFIG_H_INCLUDED_ #define _RCLCONFIG_H_INCLUDED_ -/* @(#$Id: rclconfig.h,v 1.16 2006-03-20 09:51:45 dockes Exp $ (C) 2004 J.F.Dockes */ +/* @(#$Id: rclconfig.h,v 1.17 2006-03-22 14:25:46 dockes Exp $ (C) 2004 J.F.Dockes */ #include @@ -117,10 +117,10 @@ class RclConfig { string m_datadir; // Example: /usr/local/share/recoll string m_keydir; // Current directory used for parameter fetches. - ConfTree *m_conf; // Parsed main configuration - ConfTree *mimemap; // These are independant of current keydir. - ConfTree *mimeconf; - ConfTree *mimemap_local; // + ConfStack *m_conf; // Parsed configuration files + ConfStack *mimemap; // The files don't change with keydir, but their + ConfStack *mimeconf; // content may depend on it. + std::list *stopsuffixes; // Parameters auto-fetched on setkeydir @@ -138,7 +138,6 @@ class RclConfig { m_conf = 0; mimemap = 0; mimeconf = 0; - mimemap_local = 0; stopsuffixes = 0; } /** Free data then zero pointers */ @@ -146,7 +145,6 @@ class RclConfig { delete m_conf; delete mimemap; delete mimeconf; - delete mimemap_local; delete stopsuffixes; // just in case zeroMe(); diff --git a/src/utils/conftree.cpp b/src/utils/conftree.cpp index 99044bf5..16babf80 100755 --- a/src/utils/conftree.cpp +++ b/src/utils/conftree.cpp @@ -1,5 +1,5 @@ #ifndef lint -static char rcsid [] = "@(#$Id: conftree.cpp,v 1.5 2006-01-23 13:32:28 dockes Exp $ (C) 2003 J.F.Dockes"; +static char rcsid [] = "@(#$Id: conftree.cpp,v 1.6 2006-03-22 14:25:46 dockes Exp $ (C) 2003 J.F.Dockes"; #endif /* * This program is free software; you can redistribute it and/or modify @@ -202,24 +202,6 @@ int ConfSimple::get(const string &nm, string &value, const string &sk) value = s->second; return 1; } - -const char *ConfSimple::get(const char *nm, const char *sk) -{ - if (status == STATUS_ERROR) - return 0; - if (sk == 0) - sk = ""; - // Find submap - map >::iterator ss; - if ((ss = submaps.find(sk)) == submaps.end()) - return 0; - - // Find named value - map::iterator s; - if ((s = ss->second.find(nm)) == ss->second.end()) - return 0; - return (s->second).c_str(); -} static ConfSimple::WalkerCode swalker(void *f, const string &nm, const string &value) @@ -540,26 +522,19 @@ int main(int argc, char **argv) ConfSimple parms(filename); if (parms.getStatus() != ConfSimple::STATUS_ERROR) { // It's ok for the file to not exist here - - const char *cp = parms.get("mypid"); - if (cp) { - printf("Value for mypid is '%s'\n", cp); + string value; + + if (parms.get("mypid", value)) { + printf("Value for mypid is '%s'\n", value.c_str()); } else { printf("mypid not set\n"); } - cp = parms.get("unstring"); - if (cp) { - printf("Value for unstring is '%s'\n", cp); + + if (parms.get("unstring", value)) { + printf("Value for unstring is '%s'\n", value.c_str()); } else { printf("unstring not set\n"); } - string myval; - if (parms.get(string("unstring"), myval, "")) { - printf("std::string value for 'unstring' is '%s'\n", - myval.c_str()); - } else { - printf("unstring not set (std::string)\n"); - } } char spid[100]; sprintf(spid, "%d", getpid()); diff --git a/src/utils/conftree.h b/src/utils/conftree.h index d36cccdc..dfea3eee 100755 --- a/src/utils/conftree.h +++ b/src/utils/conftree.h @@ -16,6 +16,7 @@ */ #ifndef _CONFTREE_H_ #define _CONFTREE_H_ + /** * A simple configuration file implementation. * @@ -31,6 +32,7 @@ * Any line without a '=' is discarded when rewriting the file. * All 'set' calls currently cause an immediate file rewrite. */ + #include #include #include @@ -77,15 +79,9 @@ class ConfSimple { * global space if sk is empty). * @return 0 if name not found, 1 else */ - virtual int get(const std::string &name, string &value, const string &sk); - virtual int get(const std::string &name, string &value) { - return get(name, value, string("")); - } - /* - * See comments for std::string variant - * @return 0 if name not found, const C string else - */ - virtual const char *get(const char *name, const char *sk = 0); + virtual int get(const std::string &name, string &value, + const string &sk = string("")); + /* Note: the version returning char* was buggy and has been removed */ /** * Set value for named parameter in specified subsection (or global) @@ -155,8 +151,8 @@ class ConfSimple { protected: bool dotildexpand; - private: StatusCode status; + private: string filename; // set if we're working with a file string *data; // set if we're working with an in-memory string map > submaps; @@ -180,9 +176,6 @@ class ConfSimple { */ class ConfTree : public ConfSimple { - /* Dont want this to be accessible: keep only the string-based one */ - virtual const char *get(const char *, const char *) {return 0;} - public: /** * Build the object by reading content from file. @@ -208,4 +201,111 @@ class ConfTree : public ConfSimple { } }; +/** + * Use several config files, trying to get values from each in order. Used to + * have a central config, with possible overrides from more specific + * (ie personal) ones. + * + * Notes: it's ok for some of the files in the list to not exist, but the last + * one must or we generate an error. We open all trees readonly. + */ +template class ConfStack { + public: + ConfStack(const std::list &fns, bool ro = true) { + construct(fns, ro); + } + + ConfStack(const char *nm, bool ro = true) { + std::list fns; + fns.push_back(string(nm)); + construct(fns, ro); + } + + ~ConfStack() { + erase(); + m_ok = false; + } + + ConfStack(const ConfStack &rhs) { + init_from(rhs); + } + + ConfStack& operator=(const ConfStack &rhs) { + if (this != &rhs){ + erase(); + m_ok = rhs.m_ok; + if (m_ok) + init_from(rhs); + } + return *this; + } + + int get(const std::string &name, string &value, const string &sk) { + typename std::list::iterator it; + for (it = m_confs.begin();it != m_confs.end();it++) { + if ((*it)->get(name, value, sk)) + return true; + } + return false; + } + + int get(const char *name, string &value, const char *sk) { + return get(string(name), value, sk ? string(sk) : string("")); + } + + std::list getNames(const string &sk) { + std::list nms; + typename std::list::iterator it; + for (it = m_confs.begin();it != m_confs.end();it++) { + std::list lst; + lst = (*it)->getNames(sk); + nms.splice(nms.end(), lst); + } + return nms; + } + + bool ok() {return m_ok;} + + private: + bool m_ok; + std::list m_confs; + + void erase() { + typename std::list::iterator it; + for (it = m_confs.begin();it != m_confs.end();it++) { + delete (*it); + } + m_confs.clear(); + } + + /// Common code to initialize from existing object + void init_from(const ConfStack &rhs) { + if ((m_ok = rhs.m_ok)) { + typename std::list::const_iterator it; + for (it = rhs.m_confs.begin();it != rhs.m_confs.end();it++) { + m_confs.push_back(new T(**it)); + } + } + } + + /// Common constructor code + void construct(const std::list &fns, bool ro) { + if (!ro) { + m_ok = false; + return; + } + std::list::const_iterator it; + bool lastok = false; + for (it = fns.begin();it != fns.end();it++) { + T* p = new T(it->c_str(), true); + if (p && p->ok()) { + m_confs.push_back(p); + lastok = true; + } else + lastok = false; + } + m_ok = lastok; + } +}; + #endif /*_CONFTREE_H_ */