Skip to content

Commit

Permalink
lang: remove code for parsing deprecated config file
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Blechmann <tim@klingt.org>
  • Loading branch information
timblechmann committed Sep 27, 2012
1 parent d6e4da3 commit 8ae1d46
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 299 deletions.
7 changes: 0 additions & 7 deletions README_LINUX.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,6 @@ to put Extensions to the class library, in a folder called Extensions.
the runtime directory is either the current working directory or the
path specified with the `-d' option.

for advanced setups, sclang's compilation search path can be
customized with a library configuration file. an example is provided
in `linux/examples/sclang.cfg'; install it as `/etc/sclang.cfg' or
`~/.config/SuperCollider/sclang.cfg'. This config file is only needed when you want to
_exclude_ directories from the class library. Otherwise (so in most cases)
it is not needed.

------------------------------------------------------------------------
environment
------------------------------------------------------------------------
Expand Down
240 changes: 0 additions & 240 deletions lang/LangSource/SC_LanguageConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "SC_LanguageConfig.hpp"
#include "SCBase.h"
#include "SC_StringBuffer.h"
#include "SC_DirUtils.h"

#include <assert.h>
Expand Down Expand Up @@ -150,26 +149,6 @@ void SC_LanguageConfig::removeExcludedDirectory(const char *path)
mExcludedDirectories.erase(end, mExcludedDirectories.end());
}

bool SC_LanguageConfig::readLibraryConfig(const char* fileName)
{
freeLibraryConfig();
gLanguageConfig = new SC_LanguageConfig();

SC_LibraryConfigFile file(::post);
bool success = file.open(fileName);
if (!success)
return false;

bool error = file.read(fileName, gLanguageConfig);
file.close();

if (!error)
return true;

freeLibraryConfig();
return false;
}

extern bool gPostInlineWarnings;
bool SC_LanguageConfig::readLibraryConfigYAML(const char* fileName)
{
Expand Down Expand Up @@ -280,7 +259,6 @@ static bool file_exists(std::string const & fileName)
return file_exists(fileName.c_str());
}


bool SC_LanguageConfig::readDefaultLibraryConfig()
{
char config_dir[PATH_MAX];
Expand All @@ -297,235 +275,17 @@ bool SC_LanguageConfig::readDefaultLibraryConfig()
configured = readLibraryConfigYAML(global_yaml_config_file);
}

std::string config_file = std::string(config_dir) + SC_PATH_DELIMITER + "sclang.cfg";

// deprecated config files
const char* paths[4] = { config_file.c_str(), ".sclang.cfg", "~/.sclang.cfg", "/etc/sclang.cfg"};

bool deprecatedConfigFileDetected = false;
for (int i=0; i < 4; i++) {
const char * ipath = paths[i];
char opath[PATH_MAX];
if (sc_StandardizePath(ipath, opath)) {
if (!configured) {
if (file_exists(opath)) {
deprecatedConfigFileDetected = true;
postfl("reading deprecated config file: %s\n", opath);
configured = readLibraryConfig(opath);
}
}
}
}

if (deprecatedConfigFileDetected)
postfl("Please migrate your sclang config file to %s.\n", user_yaml_config_file.c_str());

if (configured)
return true;

SC_LanguageConfig::defaultLibraryConfig();
return false;
}


void SC_LanguageConfig::freeLibraryConfig()
{
if (gLanguageConfig) {
delete gLanguageConfig;
gLanguageConfig = 0;
}
}

// =====================================================================
// SC_LibraryConfigFile
// =====================================================================

SC_LibraryConfigFile::SC_LibraryConfigFile(ErrorFunc errorFunc)
: mErrorFunc(errorFunc ? errorFunc : &defaultErrorFunc),
mFile(0)
{ }

bool SC_LibraryConfigFile::open(const char* filePath)
{
close();
#ifdef SC_WIN32
mFile = fopen(filePath, "rb");
#else
mFile = fopen(filePath, "r");
#endif
return mFile != 0;
}

void SC_LibraryConfigFile::close()
{
if (mFile) {
fclose(mFile);
mFile = 0;
}
}

bool SC_LibraryConfigFile::read(const char* fileName, SC_LanguageConfig* libConf)
{
return read(0, fileName, libConf);
}

bool SC_LibraryConfigFile::read(int depth, const char* fileName, SC_LanguageConfig* libConf)
{
if (!mFile) return false;

bool error = false;
size_t lineNumber = 1;
SC_StringBuffer line;

while (true) {
int c = fgetc(mFile);
bool eof = c == EOF;

if (eof || (c == '\n')) {
line.finish();
// go on if line parse failed
error |= parseLine(depth, fileName, lineNumber, line.getData(), libConf);
line.reset();
lineNumber++;
if (eof) break;
} else {
line.append(c);
}
}

return error;
}

bool SC_LibraryConfigFile::parseLine(int depth, const char* fileName, int lineNumber, const char* line, SC_LanguageConfig* libConf)
{
char action = 0;
SC_StringBuffer path;
SC_StringBuffer envVarName;
State state = kBegin;

while (true) {
// NOTE: in some parser states the character just read is
// written back to be consumed by the following state in the
// next iteration; this may be slightly inefficient, but makes
// control flow more obvious.

char c = *line++;

if ((c == '\0') || ((c == '#') && (state != kEscape))) {
break;
}

switch (state) {
case kBegin:
if (!isspace(c)) {
line--;
state = kAction;
}
break;
case kAction:
if ((c == '+') || (c == '-') || (c == ':')) {
action = c;
state = kPath;
} else {
(*mErrorFunc)("%s,%d: invalid action '%c'\n", fileName, lineNumber, c);
return false;
}
break;
case kPath:
if (c == '\\') {
state = kEscape;
} else if (c == '$') {
state = kEnvVar;
} else if (isspace(c)) {
state = kEnd;
} else {
path.append(c);
}
break;
case kEscape:
path.append(c);
state = kPath;
break;
case kEnvVar:
if (isalpha(c)) {
line--;
state = kEnvVarName;
envVarName.reset();
} else {
(*mErrorFunc)("%s,%d: empty variable reference\n", fileName, lineNumber);
return false;
}
break;
case kEnvVarName:
if (isalpha(c) || (c == '_')) {
envVarName.append(c);
} else {
envVarName.finish();
char* envVarValue = getenv(envVarName.getData());
if (envVarValue) {
line--;
state = kPath;
path.append(envVarValue);
} else {
(*mErrorFunc)("%s,%d: undefined variable '%s'\n", fileName, lineNumber, envVarName.getData());
return false;
}
}
break;
case kEnd:
if (!isspace(c)) {
(*mErrorFunc)("%s,%d: trailing garbage\n", fileName, lineNumber);
return false;
}
break;
default:
(*mErrorFunc)("%s,%d: [internal error] invalid parser state %d\n", fileName, lineNumber, state);
return false;
}
}

if (!action) return true;

if (path.getSize() == 0) {
(*mErrorFunc)("%s,%d: empty path\n", fileName, lineNumber);
return false;
}

path.finish();
char realPath[MAXPATHLEN];

if (sc_StandardizePath(path.getData(), realPath) == 0) {
(*mErrorFunc)("%s,%d: couldn't resolve path %s\n", fileName, lineNumber, path.getData());
return false;
}

if (action == ':') {
if (++depth > kMaxIncludeDepth) {
(*mErrorFunc)("%s,%d: maximum include depth of %d exceeded\n", fileName, lineNumber, kMaxIncludeDepth);
return false;
}
SC_LibraryConfigFile file(mErrorFunc);
if (!file.open(realPath)) return true;
const char* fileName = basename(realPath);
bool success = file.read(depth, fileName, libConf);
file.close();
return success;
}

if (action == '+') {
libConf->addIncludedDirectory(realPath);
} else if (action == '-') {
libConf->addExcludedDirectory(realPath);
}

return true;
}

void SC_LibraryConfigFile::defaultErrorFunc(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
}

// EOF
48 changes: 0 additions & 48 deletions lang/LangSource/SC_LanguageConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,9 @@
#ifndef SC_LANGUAGECONFIG_HPP_INCLUDED
#define SC_LANGUAGECONFIG_HPP_INCLUDED

#include <stdarg.h>
#include <stdio.h>
#include <vector>
#include <string>

// =====================================================================
// SC_LibraryConfigFile
// simple library configuration file parser
// =====================================================================

class SC_LanguageConfig;

class SC_LibraryConfigFile
{
public:
typedef void (*ErrorFunc)(const char* fmt, ...);

public:
SC_LibraryConfigFile(ErrorFunc errorFunc=0);

bool open(const char* filePath);
bool read(const char* fileName, SC_LanguageConfig* libConf);
void close();

protected:
enum State
{
kBegin,
kAction,
kPath,
kEscape,
kEnvVar,
kEnvVarName,
kEnd
};

enum
{
kMaxIncludeDepth = 10
};

bool read(int depth, const char* fileName, SC_LanguageConfig* libConf);
bool parseLine(int depth, const char* fileName, int lineNumber, const char* line, SC_LanguageConfig* libConf);
static void defaultErrorFunc(const char* fmt, ...);

private:
ErrorFunc mErrorFunc;
FILE* mFile;
};

class SC_LanguageConfig
{
public:
Expand All @@ -94,7 +47,6 @@ class SC_LanguageConfig
void removeExcludedDirectory(const char *name);

// convenience functions to access the global library config
static bool readLibraryConfig(const char* fileName);
static bool readLibraryConfigYAML(const char* fileName);
static bool writeLibraryConfigYAML(const char* fileName);
static void freeLibraryConfig();
Expand Down
5 changes: 1 addition & 4 deletions lang/LangSource/SC_TerminalClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,7 @@ int SC_TerminalClient::run(int argc, char** argv)
// read library configuration file
if (opt.mLibraryConfigFile) {
int argLength = strlen(opt.mLibraryConfigFile);
if (strcmp(opt.mLibraryConfigFile + argLength - 5, ".yaml"))
SC_LanguageConfig::readLibraryConfig(opt.mLibraryConfigFile);
else
SC_LanguageConfig::readLibraryConfigYAML(opt.mLibraryConfigFile);
SC_LanguageConfig::readLibraryConfigYAML(opt.mLibraryConfigFile);
} else
SC_LanguageConfig::readDefaultLibraryConfig();

Expand Down

0 comments on commit 8ae1d46

Please sign in to comment.