2005-11-09 16:43:05 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// FILE: configuration.cpp
|
|
|
|
// AUTHOR: Johannes Winkelmann, jw@tks6.net
|
|
|
|
// COPYRIGHT: (c) 2002 by Johannes Winkelmann
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "stringhelper.h"
|
|
|
|
#include "argparser.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace StringHelper;
|
|
|
|
|
|
|
|
Configuration::Configuration( const std::string& configFile,
|
|
|
|
const ArgParser* parser )
|
|
|
|
: m_configFile( configFile ),
|
|
|
|
m_parser( parser ),
|
|
|
|
m_writeLog( false ),
|
|
|
|
m_appendLog( false ),
|
2006-04-04 12:22:35 -04:00
|
|
|
m_removeLogOnSuccess( false ),
|
2005-11-09 16:43:05 -05:00
|
|
|
m_logFilePattern( "" ),
|
|
|
|
m_cacheFile( "" ),
|
|
|
|
m_readmeMode( VERBOSE_README ),
|
|
|
|
m_runScripts( false ),
|
|
|
|
m_preferHigher( false ),
|
|
|
|
m_useRegex( false ),
|
|
|
|
m_makeCommand( "" ), m_addCommand( "" ),
|
|
|
|
m_removeCommand( "" ), m_runscriptCommand( "" )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Configuration::parse()
|
|
|
|
{
|
|
|
|
FILE* fp = fopen( m_configFile.c_str(), "r" );
|
|
|
|
if ( !fp ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int length = BUFSIZ;
|
|
|
|
char input[length];
|
|
|
|
string s;
|
|
|
|
while ( fgets( input, length, fp ) ) {
|
|
|
|
s = input;
|
|
|
|
s = getValueBefore( s, '#' );
|
|
|
|
s = stripWhiteSpace( s );
|
|
|
|
parseLine(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
// command line arguments override config:
|
|
|
|
if ( m_parser->writeLog() ) {
|
|
|
|
m_writeLog = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::writeLog() const
|
|
|
|
{
|
|
|
|
return m_writeLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::appendLog() const
|
|
|
|
{
|
|
|
|
return m_appendLog;
|
|
|
|
}
|
|
|
|
|
2006-04-04 12:22:35 -04:00
|
|
|
bool Configuration::removeLogOnSuccess() const
|
|
|
|
{
|
|
|
|
return m_removeLogOnSuccess;
|
|
|
|
}
|
|
|
|
|
2005-11-09 16:43:05 -05:00
|
|
|
string Configuration::logFilePattern() const
|
|
|
|
{
|
|
|
|
return m_logFilePattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const list< pair<string, string> >& Configuration::rootList() const
|
|
|
|
{
|
|
|
|
return m_rootList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Configuration::ReadmeMode Configuration::readmeMode() const
|
|
|
|
{
|
|
|
|
return m_readmeMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Configuration::cacheFile() const
|
|
|
|
{
|
|
|
|
return m_cacheFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Configuration::addConfig(const string& line,
|
|
|
|
bool configSet,
|
|
|
|
bool configPrepend)
|
|
|
|
{
|
2006-09-27 11:41:25 -04:00
|
|
|
if (configSet && startsWithNoCase( line, "prtdir" )) {
|
2005-11-09 16:43:05 -05:00
|
|
|
m_rootList.clear();
|
|
|
|
}
|
|
|
|
parseLine(line, configPrepend);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
parse a line and set the configuration data accordingly; if \a
|
|
|
|
prepend is set, prepend the data for configuration options which are lists
|
|
|
|
*/
|
|
|
|
void Configuration::parseLine(const string& line, bool prepend)
|
|
|
|
{
|
|
|
|
string s = line;
|
|
|
|
if ( s.empty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-27 11:41:25 -04:00
|
|
|
if ( startsWithNoCase( s, "prtdir" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 6, "" ) );
|
|
|
|
string path = stripWhiteSpace( getValueBefore( s, ':' ) );
|
|
|
|
string packages = getValue( s, ':' );
|
|
|
|
DIR* dir = opendir( path.c_str() );
|
|
|
|
if ( dir ) {
|
|
|
|
closedir( dir );
|
|
|
|
|
|
|
|
if (prepend) {
|
|
|
|
m_rootList.push_front( make_pair( path, packages ) );
|
|
|
|
} else {
|
|
|
|
m_rootList.push_back( make_pair( path, packages ) );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cerr << "[Config error: can't access " << path
|
|
|
|
<< "]" << endl;
|
|
|
|
}
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "cachefile" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 9, "" ) );
|
|
|
|
m_cacheFile = s;
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "writelog" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 8, "" ) );
|
|
|
|
if ( s == "enabled" ) {
|
|
|
|
// it's already set to false, so we can just enable it.
|
|
|
|
// like this, the command line switch works as well
|
|
|
|
m_writeLog = true;
|
|
|
|
}
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "logfile" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 7, "" ) );
|
|
|
|
m_logFilePattern = s;
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "logmode" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 7, "" ) );
|
|
|
|
if ( s == "append" ) {
|
|
|
|
m_appendLog = true;
|
|
|
|
}
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "rmlog_on_success" ) ) {
|
2006-04-04 12:22:35 -04:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 16, "" ) );
|
|
|
|
if ( s == "yes" ) {
|
|
|
|
m_removeLogOnSuccess = true;
|
|
|
|
}
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "readme" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 6, "" ) );
|
|
|
|
if ( s == "compact" ) {
|
|
|
|
m_readmeMode = COMPACT_README;
|
|
|
|
} else if ( s == "disabled" ) {
|
|
|
|
m_readmeMode = NO_README;
|
|
|
|
}
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "runscripts" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 10, "" ) );
|
|
|
|
if ( s == "yes" ) {
|
|
|
|
m_runScripts = true;
|
|
|
|
}
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "preferhigher" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 12, "" ) );
|
|
|
|
if ( s == "yes" ) {
|
|
|
|
m_preferHigher = true;
|
|
|
|
}
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "useregex" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
s = stripWhiteSpace( s.replace( 0, 8, "" ) );
|
|
|
|
if ( s == "yes" ) {
|
|
|
|
m_useRegex = true;
|
|
|
|
}
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "makecommand" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
m_makeCommand = stripWhiteSpace( s.replace( 0, 11, "" ) );
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "addcommand" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
m_addCommand = stripWhiteSpace( s.replace( 0, 10, "" ) );
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "removecommand" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
m_removeCommand = stripWhiteSpace( s.replace( 0, 13, "" ) );
|
2006-09-27 11:41:25 -04:00
|
|
|
} else if ( startsWithNoCase( s, "runscriptcommand" ) ) {
|
2005-11-09 16:43:05 -05:00
|
|
|
m_runscriptCommand = stripWhiteSpace( s.replace( 0, 16, "" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::runScripts() const
|
|
|
|
{
|
|
|
|
return m_runScripts;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Configuration::makeCommand() const
|
|
|
|
{
|
|
|
|
return m_makeCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Configuration::addCommand() const
|
|
|
|
{
|
|
|
|
return m_addCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Configuration::removeCommand() const
|
|
|
|
{
|
|
|
|
return m_removeCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Configuration::runscriptCommand() const
|
|
|
|
{
|
|
|
|
return m_runscriptCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::preferHigher() const
|
|
|
|
{
|
|
|
|
return m_preferHigher;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::useRegex() const
|
|
|
|
{
|
|
|
|
return m_useRegex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|