8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 04:03:03 +01:00
firebird-mirror/src/config/ConfigFile.cpp

374 lines
8.1 KiB
C++
Raw Normal View History

2005-05-28 00:45:31 +02:00
/*
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/idpl.html.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
2005-05-28 00:45:31 +02:00
* language governing rights and limitations under the License.
*
* The contents of this file or any work derived from this file
* may not be distributed under any other license whatsoever
* without the express prior written permission of the original
2005-05-28 00:45:31 +02:00
* author.
*
*
* The Original Code was created by James A. Starkey for IBPhoenix.
*
* Copyright (c) 1997 - 2000, 2001, 2003 James A. Starkey
* Copyright (c) 1997 - 2000, 2001, 2003 Netfrastructure, Inc.
* All Rights Reserved.
*/
#include "firebird.h"
2005-05-28 00:45:31 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
2006-06-16 14:36:09 +02:00
#include "../common/classes/alloc.h"
2005-05-28 00:45:31 +02:00
#include "ConfigFile.h"
#include "ConfObject.h"
#include "InputFile.h"
#include "Element.h"
#include "AdminException.h"
#include "ScanDir.h"
#include "../common/config/config.h"
#ifdef _WIN32
#include <windows.h>
#define IS_SEPARATOR(c) (c == '\\' || c == '/')
#ifndef strcasecmp
#define strcasecmp stricmp
//#define strncasecmp strnicmp
2005-05-28 00:45:31 +02:00
#endif
//#define KEY "SOFTWARE\\Firebird Project\\Firebird Server\\Instances"
//#define SUB_KEY NULL
//#define PATH_KEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Firebird"
2005-05-28 00:45:31 +02:00
#else
#define IS_SEPARATOR(c) (c == '/')
#endif
ConfigFile::ConfigFile(const LEX_flags configFlags) :
Lex ("/<>=", configFlags),
rootDirectory(getPool()), installDirectory(getPool()), currentDirectory(getPool())
2005-05-28 00:45:31 +02:00
{
init (configFlags);
}
ConfigFile::ConfigFile(const char* configFile, const LEX_flags configFlags) :
Lex ("/<>=", configFlags),
rootDirectory(getPool()), installDirectory(getPool()), currentDirectory(getPool())
2005-05-28 00:45:31 +02:00
{
init (configFlags);
InputFile *inputFile = new InputFile;
Firebird::PathName expandedFile (expand (configFile));
if (!inputFile->openInputFile(expandedFile.c_str()))
{
2005-05-28 00:45:31 +02:00
delete inputFile;
throw AdminException ("can't open configuration file \"%s\"", configFile);
}
2005-05-28 00:45:31 +02:00
pushStream (inputFile);
parse();
}
void ConfigFile::init(const LEX_flags configFlags)
2005-05-28 00:45:31 +02:00
{
flags = configFlags;
setLineComment ("#");
setContinuationChar ('\\');
objects = NULL;
memset (hashTable, 0, sizeof (hashTable));
}
ConfigFile::~ConfigFile()
2005-05-28 00:45:31 +02:00
{
delete objects;
2005-05-28 00:45:31 +02:00
for (int n = 0; n < HASH_SIZE; ++n)
{
2005-05-28 00:45:31 +02:00
for (Element *element; element = hashTable [n];)
{
2005-05-28 00:45:31 +02:00
hashTable [n] = element->sibling;
delete element;
}
}
2005-05-28 00:45:31 +02:00
}
InputFile* ConfigFile::openConfigFile()
2005-05-28 00:45:31 +02:00
{
fb_assert(false);
// Vulcan specific code removed
return NULL;
}
void ConfigFile::addText(const char* text)
{
InputStream *stream = new InputStream(text);
pushStream(stream);
}
void ConfigFile::parse()
2005-05-28 00:45:31 +02:00
{
objects = new Element ("ConfObjects");
getToken();
while (tokenType != END_OF_STREAM)
{
2005-05-28 00:45:31 +02:00
while (match ("include"))
{
Firebird::PathName fileName = expand (reparseFilename());
if (fileName.find('*') != Firebird::PathName::npos)
wildCardInclude(fileName.c_str());
2005-05-28 00:45:31 +02:00
else
pushStream (new InputFile (fileName.c_str()));
2005-05-28 00:45:31 +02:00
getToken();
}
2005-05-28 00:45:31 +02:00
if (match ("<"))
objects->addChild (parseObject());
else
{
2005-05-28 00:45:31 +02:00
Element *element = parseAttribute();
const int slot = element->name.hash (HASH_SIZE);
2005-05-28 00:45:31 +02:00
element->sibling = hashTable [slot];
hashTable [slot] = element;
}
}
2005-05-28 00:45:31 +02:00
}
Element* ConfigFile::parseObject()
2005-05-28 00:45:31 +02:00
{
const Firebird::string name = getName();
2005-05-28 00:45:31 +02:00
Element *element = new Element (name);
element->setSource (priorLineNumber, priorInputStream);
while (!match (">"))
{
element->addAttribute (new Element (reparseFilename().ToString()));
2005-05-28 00:45:31 +02:00
getToken();
}
2005-05-28 00:45:31 +02:00
for (;;)
{
2005-05-28 00:45:31 +02:00
if (match ("<"))
{
2005-05-28 00:45:31 +02:00
if (match ("/"))
{
if (!match (element->name.c_str()))
2005-05-28 00:45:31 +02:00
syntaxError ("closing element");
if (!match (">"))
syntaxError ("\">\"");
element->numberLines = priorLineNumber - element->lineNumber + 1;
return element;
}
element->addChild (parseObject());
}
2005-05-28 00:45:31 +02:00
else
element->addChild (parseAttribute());
}
2005-05-28 00:45:31 +02:00
}
Element* ConfigFile::parseAttribute()
2005-05-28 00:45:31 +02:00
{
Element *element = new Element (getName());
element->setSource (priorLineNumber, priorInputStream);
match ("=");
while (!eol)
{
element->addAttribute (new Element (reparseFilename().ToString()));
2005-05-28 00:45:31 +02:00
getToken();
}
2005-05-28 00:45:31 +02:00
element->numberLines = priorLineNumber - element->lineNumber + 1;
return element;
}
ConfObject* ConfigFile::findObject(const char* objectType, const char* objectName)
{
if (!objects)
return NULL;
ConfObject *object = new ConfObject (this);
2005-05-28 00:45:31 +02:00
for (Element *child = objects->children; child; child = child->sibling)
{
2005-05-28 00:45:31 +02:00
if (object->matches (child, objectType, objectName))
return object;
}
2005-05-28 00:45:31 +02:00
object->release();
2005-05-28 00:45:31 +02:00
return NULL;
}
const char* ConfigFile::getRootDirectory()
2005-05-28 00:45:31 +02:00
{
return Config::getRootDirectory();
}
ConfObject* ConfigFile::getObject(const char* /*objectType*/)
2005-05-28 00:45:31 +02:00
{
return new ConfObject (this);
}
Element* ConfigFile::findGlobalAttribute(const char *attributeName)
{
for (Element *element = hashTable [Firebird::string::hash (attributeName, HASH_SIZE)]; element; element = element->sibling)
{
2005-05-28 00:45:31 +02:00
if (element->name == attributeName)
return element;
}
2005-05-28 00:45:31 +02:00
return NULL;
}
const char* ConfigFile::getInstallDirectory()
2005-05-28 00:45:31 +02:00
{
return Config::getInstallDirectory();
}
Firebird::PathName ConfigFile::expand(const Firebird::PathName& rawString)
2005-05-28 00:45:31 +02:00
{
const char* const errstr = "filename expansion reached implementation limit at %d";
2005-05-28 00:45:31 +02:00
char temp [1024];
char* p = temp;
const char* const temp_end = temp + sizeof(temp) - 1;
2005-05-28 00:45:31 +02:00
bool change = false;
for (const char *s = rawString.c_str(); *s;)
{
2005-05-28 00:45:31 +02:00
char c = *s++;
if (c == '$')
{
2005-05-28 00:45:31 +02:00
if (*s == '(')
{
2005-05-28 00:45:31 +02:00
++s;
change = true;
char name [256];
const char* const name_end = name + sizeof(name) - 1;
char* n = name;
bool overflow = false;
while (*s && (c = *s++) != ')' && !overflow)
{
if (n < name_end)
*n++ = c;
else
overflow = true;
}
2005-05-28 00:45:31 +02:00
*n = 0;
if (overflow)
{
n[-3] = n[-2] = n[-1] = '.';
throw AdminException("name to be substituted \"%s\" is too long", name);
}
2005-05-28 00:45:31 +02:00
const char *subst = translate (name, NULL);
if (!subst)
throw AdminException ("can't substitute for \"%s\"", name);
for (const char *t = subst; *t;)
{
if (p < temp_end)
*p++ = *t++;
else
throw AdminException(errstr, sizeof(temp) - 1);
2005-05-28 00:45:31 +02:00
}
}
}
2006-08-17 14:08:49 +02:00
else if (p < temp_end)
2005-05-28 00:45:31 +02:00
*p++ = c;
else
throw AdminException(errstr, sizeof(temp) - 1);
}
2005-05-28 00:45:31 +02:00
if (!change)
return rawString;
2005-05-28 00:45:31 +02:00
*p = 0;
return temp;
2005-05-28 00:45:31 +02:00
}
const char* ConfigFile::translate(const char* value, const Element* object)
2005-05-28 00:45:31 +02:00
{
if (strcasecmp (value, "root") == 0)
return getRootDirectory();
2005-05-28 00:45:31 +02:00
if (strcasecmp (value, "install") == 0)
return getInstallDirectory();
if (strcasecmp (value, "this") == 0)
{
2005-05-28 00:45:31 +02:00
const char *source = NULL;
2005-05-28 00:45:31 +02:00
if (object && object->inputStream)
source = object->inputStream->getFileName();
2005-05-28 00:45:31 +02:00
if (!source && inputStream)
source = inputStream->getFileName();
2005-05-28 00:45:31 +02:00
if (!source)
throw AdminException("no context for $(this)");
const Firebird::PathName currentFile = expand (source);
const char *fileName = currentFile.c_str();
2005-05-28 00:45:31 +02:00
const char *p = NULL;
2005-05-28 00:45:31 +02:00
for (const char *q = fileName; *q; ++q)
{
2005-05-28 00:45:31 +02:00
if (IS_SEPARATOR(*q))
p = q;
}
2005-05-28 00:45:31 +02:00
if (p)
currentDirectory = Firebird::PathName(fileName, p - fileName);
2005-05-28 00:45:31 +02:00
else
currentDirectory = ".";
return currentDirectory.c_str();
}
2005-05-28 00:45:31 +02:00
return NULL;
}
void ConfigFile::wildCardInclude(const char* fileName)
{
char directory [256];
if (strlen(fileName) >= sizeof(directory))
throw AdminException("Too long filename in wildCardInclude()");
2005-05-28 00:45:31 +02:00
strcpy (directory, fileName);
const char *wildcard = fileName;
char *p = strrchr (directory, '/');
if (p)
{
2005-05-28 00:45:31 +02:00
*p = 0;
wildcard = p + 1;
}
2005-05-28 00:45:31 +02:00
else
directory [0] = 0;
// OK, expand wildcard
ScanDir scanDir (directory, wildcard);
while (scanDir.next())
pushStream (new InputFile (scanDir.getFilePath()));
}