/*! */ QString Config::copyFile(const Location& location, const QString& sourceFilePath, const QString& userFriendlySourceFilePath, const QString& targetDirPath) { QFile inFile(sourceFilePath); if (!inFile.open(QFile::ReadOnly)) { location.fatal(tr("Cannot open input file '%1': %2") .arg(inFile.fileName()).arg(inFile.errorString())); return ""; } QString outFileName = userFriendlySourceFilePath; int slash = outFileName.lastIndexOf("/"); if (slash != -1) outFileName = outFileName.mid(slash); QFile outFile(targetDirPath + "/" + outFileName); if (!outFile.open(QFile::WriteOnly)) { location.fatal(tr("Cannot open output file '%1': %2") .arg(outFile.fileName()).arg(outFile.errorString())); return ""; } char buffer[1024]; int len; while ((len = inFile.read(buffer, sizeof(buffer))) > 0) { outFile.write(buffer, len); } return outFileName; }
QT_BEGIN_NAMESPACE void executeCommand(const Location& location, const QString& format, const QStringList& args) { QString actualCommand; for (int i = 0; i < (int) format.length(); i++) { int ch = format[i].unicode(); if (ch > 0 && ch < 8) { actualCommand += args[ch - 1]; } else { actualCommand += format[i]; } } QString toolName = actualCommand; int space = toolName.indexOf(QLatin1Char(' ')); if (space != -1) toolName.truncate(space); #ifdef QT_BOOTSTRAPPED int status = system(qPrintable(actualCommand)); int exitCode = WEXITSTATUS(status); if (status == -1 || exitCode != EXIT_SUCCESS) location.fatal(QString("Error executing '$1': $2").arg(toolName).arg(exitCode)); #else QProcess process; process.start(QLatin1String("sh"), QStringList() << QLatin1String("-c") << actualCommand); process.waitForFinished(); if (process.exitCode() == 127) location.fatal(tr("Couldn't launch the '%1' tool") .arg(toolName), tr("Make sure the tool is installed and in the" " path.")); QString errors = QString::fromLocal8Bit(process.readAllStandardError()); while (errors.endsWith(QLatin1Char('\n'))) errors.truncate(errors.length() - 1); if (!errors.isEmpty()) location.fatal(tr("The '%1' tool encountered some problems") .arg(toolName), tr("The tool was invoked like this:\n%1\n" "It emitted these errors:\n%2") .arg(actualCommand).arg(errors)); #endif }
/*! \a fileName is a master qdocconf file. It contains a list of qdocconf files and nothing else. Read the list and return it. */ QStringList Config::loadMaster(const QString& fileName) { Location location = Location::null; QFile fin(fileName); if (!fin.open(QFile::ReadOnly | QFile::Text)) { if (!Config::installDir.isEmpty()) { int prefix = location.filePath().length() - location.fileName().length(); fin.setFileName(Config::installDir + QLatin1Char('/') + fileName.right(fileName.length() - prefix)); } if (!fin.open(QFile::ReadOnly | QFile::Text)) location.fatal(tr("Cannot open master qdocconf file '%1': %2").arg(fileName).arg(fin.errorString())); } QTextStream stream(&fin); #ifndef QT_NO_TEXTCODEC stream.setCodec("UTF-8"); #endif QStringList qdocFiles; QString line = stream.readLine(); while (!line.isNull()) { qdocFiles.append(line); line = stream.readLine(); } fin.close(); return qdocFiles; }
/*! \a fileName is the path of the file to find. \a files and \a dirs are the lists where we must find the components of \a fileName. \a location is used for obtaining the file and line numbers for report qdoc errors. */ QString Config::findFile(const Location& location, const QStringList& files, const QStringList& dirs, const QString& fileName, QString& userFriendlyFilePath) { if (fileName.isEmpty() || fileName.startsWith(QLatin1Char('/'))) { userFriendlyFilePath = fileName; return fileName; } QFileInfo fileInfo; QStringList components = fileName.split(QLatin1Char('?')); QString firstComponent = components.first(); QStringList::ConstIterator f = files.constBegin(); while (f != files.constEnd()) { if (*f == firstComponent || (*f).endsWith(QLatin1Char('/') + firstComponent)) { fileInfo.setFile(*f); if (!fileInfo.exists()) location.fatal(tr("File '%1' does not exist").arg(*f)); break; } ++f; } if (fileInfo.fileName().isEmpty()) { QStringList::ConstIterator d = dirs.constBegin(); while (d != dirs.constEnd()) { fileInfo.setFile(QDir(*d), firstComponent); if (fileInfo.exists()) break; ++d; } } userFriendlyFilePath = QString(); if (!fileInfo.exists()) return QString(); QStringList::ConstIterator c = components.constBegin(); for (;;) { bool isArchive = (c != components.constEnd() - 1); QString userFriendly = *c; userFriendlyFilePath += userFriendly; if (isArchive) { QString extracted = extractedDirs[fileInfo.filePath()]; ++c; fileInfo.setFile(QDir(extracted), *c); } else { break; } userFriendlyFilePath += QLatin1Char('?'); } return fileInfo.filePath(); }
QStringList MetaStack::getExpanded(const Location& location) { if (count() > 1) location.fatal(tr("Missing '}'")); top().close(); return top().accum; }
void PageGenerator::beginSubPage(const Location& location, const QString& fileName) { QFile *outFile = new QFile(outputDir() + "/" + fileName); if (!outFile->open(QFile::WriteOnly)) location.fatal(tr("Cannot open output file '%1'") .arg(outFile->fileName())); QTextStream *out = new QTextStream(outFile); out->setCodec("ISO-8859-1"); outStreamStack.push(out); }
void MetaStack::process(QChar ch, const Location& location) { if (ch == QLatin1Char('{')) { push(MetaStackEntry()); top().open(); } else if (ch == QLatin1Char('}')) { if (count() == 1) location.fatal(tr("Unexpected '}'")); top().close(); QStringList suffixes = pop().accum; QStringList prefixes = top().next; top().next.clear(); QStringList::ConstIterator pre = prefixes.begin(); while (pre != prefixes.end()) { QStringList::ConstIterator suf = suffixes.begin(); while (suf != suffixes.end()) { top().next << (*pre + *suf); ++suf; } ++pre; } } else if (ch == QLatin1Char(',') && count() > 1) { top().close(); top().open(); } else { QStringList::Iterator pre = top().next.begin(); while (pre != top().next.end()) { *pre += ch; ++pre; } } }
/*! Processes the qdoc config file \a fileName. This is the controller for all of qdoc. */ static void processQdocconfFile(const QString &fileName) { #ifndef QT_NO_TRANSLATION QList<QTranslator *> translators; #endif /* The Config instance represents the configuration data for qdoc. All the other classes are initialized with the config. Here we initialize the configuration with some default values. */ Config config(tr("qdoc")); int i = 0; while (defaults[i].key) { config.setStringList(defaults[i].key, QStringList() << defaults[i].value); ++i; } config.setStringList(CONFIG_SLOW, QStringList(slow ? "true" : "false")); config.setStringList(CONFIG_SHOWINTERNAL, QStringList(showInternal ? "true" : "false")); config.setStringList(CONFIG_OBSOLETELINKS, QStringList(obsoleteLinks ? "true" : "false")); /* With the default configuration values in place, load the qdoc configuration file. Note that the configuration file may include other configuration files. The Location class keeps track of the current location in the file being processed, mainly for error reporting purposes. */ Location::initialize(config); config.load(fileName); /* Add the defines to the configuration variables. */ QStringList defs = defines + config.getStringList(CONFIG_DEFINES); config.setStringList(CONFIG_DEFINES,defs); Location::terminate(); QString prevCurrentDir = QDir::currentPath(); QString dir = QFileInfo(fileName).path(); if (!dir.isEmpty()) QDir::setCurrent(dir); /* Initialize all the classes and data structures with the qdoc configuration. */ Location::initialize(config); Tokenizer::initialize(config); Doc::initialize(config); CppToQsConverter::initialize(config); CodeMarker::initialize(config); CodeParser::initialize(config); Generator::initialize(config); #ifndef QT_NO_TRANSLATION /* Load the language translators, if the configuration specifies any. */ QStringList fileNames = config.getStringList(CONFIG_TRANSLATORS); QStringList::Iterator fn = fileNames.begin(); while (fn != fileNames.end()) { QTranslator *translator = new QTranslator(0); if (!translator->load(*fn)) config.lastLocation().error(tr("Cannot load translator '%1'") .arg(*fn)); QCoreApplication::instance()->installTranslator(translator); translators.append(translator); ++fn; } #endif //QSet<QString> outputLanguages = config.getStringSet(CONFIG_OUTPUTLANGUAGES); /* Get the source language (Cpp) from the configuration and the location in the configuration file where the source language was set. */ QString lang = config.getString(CONFIG_LANGUAGE); Location langLocation = config.lastLocation(); /* Initialize the tree where all the parsed sources will be stored. The tree gets built as the source files are parsed, and then the documentation output is generated by traversing the tree. */ Tree *tree = new Tree; tree->setVersion(config.getString(CONFIG_VERSION)); /* There must be a code parser for the source code language, e.g. C++. If there isn't one, give up. */ CodeParser *codeParser = CodeParser::parserForLanguage(lang); if (codeParser == 0) config.lastLocation().fatal(tr("Cannot parse programming language '%1'").arg(lang)); /* By default, the only output format is HTML. */ QSet<QString> outputFormats = config.getStringSet(CONFIG_OUTPUTFORMATS); Location outputFormatsLocation = config.lastLocation(); /* There must be a code marker for the source code language, e.g. C++. If there isn't one, give up. */ CodeMarker *marker = CodeMarker::markerForLanguage(lang); if (!marker && !outputFormats.isEmpty()) langLocation.fatal(tr("Cannot output documentation for programming language '%1'").arg(lang)); /* Read some XML indexes. What are they??? */ QStringList indexFiles = config.getStringList(CONFIG_INDEXES); tree->readIndexes(indexFiles); /* Get all the header files: "*.ch *.h *.h++ *.hh *.hpp *.hxx" Put them in a set. */ QSet<QString> excludedDirs; QStringList excludedDirsList = config.getStringList(CONFIG_EXCLUDEDIRS); foreach (const QString &excludeDir, excludedDirsList) excludedDirs.insert(QDir::fromNativeSeparators(excludeDir)); QSet<QString> headers = QSet<QString>::fromList( config.getAllFiles(CONFIG_HEADERS, CONFIG_HEADERDIRS, codeParser->headerFileNameFilter(), excludedDirs)); /* Parse each header file in the set and add it to the big tree. */ QSet<QString>::ConstIterator h = headers.begin(); while (h != headers.end()) { codeParser->parseHeaderFile(config.location(), *h, tree); ++h; } codeParser->doneParsingHeaderFiles(tree); /* Get all the source text files: "*.cpp *.qdoc *.mm" Put them in a set. */ QSet<QString> sources = QSet<QString>::fromList( config.getAllFiles(CONFIG_SOURCES, CONFIG_SOURCEDIRS, codeParser->sourceFileNameFilter(), excludedDirs)); /* Parse each source text file in the set and add it to the big tree. */ QSet<QString>::ConstIterator s = sources.begin(); while (s != sources.end()) { codeParser->parseSourceFile(config.location(), *s, tree); ++s; } codeParser->doneParsingSourceFiles(tree); /* Now the big tree has been built from all the header and source files. Resolve all the class names, function names, targets, URLs, links, and other stuff that needs resolving. */ tree->resolveGroups(); tree->resolveTargets(); /* Now the tree has been built, and all the stuff that needed resolving has been resolved. Now it is time to traverse the big tree and generate the documentation output. */ QSet<QString>::ConstIterator of = outputFormats.begin(); while (of != outputFormats.end()) { Generator *generator = Generator::generatorForFormat(*of); if (generator == 0) outputFormatsLocation.fatal(tr("Unknown output format '%1'") .arg(*of)); generator->generateTree(tree, marker); ++of; } /* Generate the XML tag file, if it was requested. */ QString tagFile = config.getString(CONFIG_TAGFILE); if (!tagFile.isEmpty()) { tree->generateTagFile(tagFile); } tree->setVersion(""); Generator::terminate(); CodeParser::terminate(); CodeMarker::terminate(); CppToQsConverter::terminate(); Doc::terminate(); Tokenizer::terminate(); Location::terminate(); QDir::setCurrent(prevCurrentDir); #ifndef QT_NO_TRANSLATION qDeleteAll(translators); #endif #ifdef DEBUG_SHUTDOWN_CRASH qDebug() << "main(): Delete tree"; #endif delete tree; #ifdef DEBUG_SHUTDOWN_CRASH qDebug() << "main(): Tree deleted"; #endif }
/*! Load, parse, and process a qdoc configuration file. This function is only called by the other load() function, but this one is recursive, i.e., it calls itself when it sees an \c{include} statement in the qdog configuration file. */ void Config::load(Location location, const QString& fileName) { QRegExp keySyntax("\\w+(?:\\.\\w+)*"); #define SKIP_CHAR() \ do { \ location.advance(c); \ ++i; \ c = text.at(i); \ cc = c.unicode(); \ } while (0) #define SKIP_SPACES() \ while (c.isSpace() && cc != '\n') \ SKIP_CHAR() #define PUT_CHAR() \ word += c; \ SKIP_CHAR(); if (location.depth() > 16) location.fatal(tr("Too many nested includes")); QFile fin(fileName); if (!fin.open(QFile::ReadOnly | QFile::Text)) { fin.setFileName(fileName + ".qdoc"); if (!fin.open(QFile::ReadOnly | QFile::Text)) location.fatal(tr("Cannot open file '%1': %2").arg(fileName).arg(fin.errorString())); } QString text = fin.readAll(); text += QLatin1String("\n\n"); text += QChar('\0'); fin.close(); location.push(fileName); location.start(); int i = 0; QChar c = text.at(0); uint cc = c.unicode(); while (i < (int) text.length()) { if (cc == 0) ++i; else if (c.isSpace()) { SKIP_CHAR(); } else if (cc == '#') { do { SKIP_CHAR(); } while (cc != '\n'); } else if (isMetaKeyChar(c)) { Location keyLoc = location; bool plus = false; QString stringValue; QStringList stringListValue; QString word; bool inQuote = false; bool prevWordQuoted = true; bool metWord = false; MetaStack stack; do { stack.process(c, location); SKIP_CHAR(); } while (isMetaKeyChar(c)); QStringList keys = stack.getExpanded(location); SKIP_SPACES(); if (keys.count() == 1 && keys.first() == "include") { QString includeFile; if (cc != '(') location.fatal(tr("Bad include syntax")); SKIP_CHAR(); SKIP_SPACES(); while (!c.isSpace() && cc != '#' && cc != ')') { includeFile += c; SKIP_CHAR(); } SKIP_SPACES(); if (cc != ')') location.fatal(tr("Bad include syntax")); SKIP_CHAR(); SKIP_SPACES(); if (cc != '#' && cc != '\n') location.fatal(tr("Trailing garbage")); /* Here is the recursive call. */ load(location, QFileInfo(QFileInfo(fileName).dir(), includeFile) .filePath()); } else { /* It wasn't an include statement, so it;s something else. */ if (cc == '+') { plus = true; SKIP_CHAR(); } if (cc != '=') location.fatal(tr("Expected '=' or '+=' after key")); SKIP_CHAR(); SKIP_SPACES(); for (;;) { if (cc == '\\') { int metaCharPos; SKIP_CHAR(); if (cc == '\n') { SKIP_CHAR(); } else if (cc > '0' && cc < '8') { word += QChar(c.digitValue()); SKIP_CHAR(); } else if ((metaCharPos = QString(QLatin1String("abfnrtv")).indexOf(c)) != -1) { word += "\a\b\f\n\r\t\v"[metaCharPos]; SKIP_CHAR(); } else { PUT_CHAR(); } } else if (c.isSpace() || cc == '#') { if (inQuote) { if (cc == '\n') location.fatal(tr("Unterminated string")); PUT_CHAR(); } else { if (!word.isEmpty()) { if (metWord) stringValue += QLatin1Char(' '); stringValue += word; stringListValue << word; metWord = true; word.clear(); prevWordQuoted = false; } if (cc == '\n' || cc == '#') break; SKIP_SPACES(); } } else if (cc == '"') { if (inQuote) { if (!prevWordQuoted) stringValue += QLatin1Char(' '); stringValue += word; if (!word.isEmpty()) stringListValue << word; metWord = true; word.clear(); prevWordQuoted = true; } inQuote = !inQuote; SKIP_CHAR(); } else if (cc == '$') { QString var; SKIP_CHAR(); while (c.isLetterOrNumber() || cc == '_') { var += c; SKIP_CHAR(); } if (!var.isEmpty()) { char *val = getenv(var.toLatin1().data()); if (val == 0) { location.fatal(tr("Environment variable '%1' undefined").arg(var)); } else { word += QString(val); } } } else { if (!inQuote && cc == '=') location.fatal(tr("Unexpected '='")); PUT_CHAR(); } } QStringList::ConstIterator key = keys.begin(); while (key != keys.end()) { if (!keySyntax.exactMatch(*key)) keyLoc.fatal(tr("Invalid key '%1'").arg(*key)); if (plus) { if (locMap[*key].isEmpty()) { locMap[*key] = keyLoc; } else { locMap[*key].setEtc(true); } if (stringValueMap[*key].isEmpty()) { stringValueMap[*key] = stringValue; } else { stringValueMap[*key] += QLatin1Char(' ') + stringValue; } stringListValueMap[*key] += stringListValue; } else { locMap[*key] = keyLoc; stringValueMap[*key] = stringValue; stringListValueMap[*key] = stringListValue; } ++key; } } } else { location.fatal(tr("Unexpected character '%1' at beginning of line") .arg(c)); } } }
/*! */ QString Config::findFile(const Location& location, const QStringList& files, const QStringList& dirs, const QString& fileName, QString& userFriendlyFilePath) { if (fileName.isEmpty() || fileName.startsWith(QLatin1Char('/'))) { userFriendlyFilePath = fileName; return fileName; } QFileInfo fileInfo; QStringList components = fileName.split(QLatin1Char('?')); QString firstComponent = components.first(); QStringList::ConstIterator f = files.begin(); while (f != files.end()) { if (*f == firstComponent || (*f).endsWith(QLatin1Char('/') + firstComponent)) { fileInfo.setFile(*f); if (!fileInfo.exists()) location.fatal(tr("File '%1' does not exist").arg(*f)); break; } ++f; } if (fileInfo.fileName().isEmpty()) { QStringList::ConstIterator d = dirs.begin(); while (d != dirs.end()) { fileInfo.setFile(QDir(*d), firstComponent); if (fileInfo.exists()) { break; } ++d; } } userFriendlyFilePath = QString(); if (!fileInfo.exists()) return QString(); QStringList::ConstIterator c = components.begin(); for (;;) { bool isArchive = (c != components.end() - 1); ArchiveExtractor *extractor = 0; QString userFriendly = *c; if (isArchive) { extractor = ArchiveExtractor::extractorForFileName(userFriendly); } if (extractor == 0) { Uncompressor *uncompressor = Uncompressor::uncompressorForFileName(userFriendly); if (uncompressor != 0) { QString fileNameWithCorrectExtension = uncompressor->uncompressedFilePath( fileInfo.filePath()); QString uncompressed = uncompressedFiles[fileInfo.filePath()]; if (uncompressed.isEmpty()) { uncompressed = QTemporaryFile(fileInfo.filePath()).fileName(); uncompressor->uncompressFile(location, fileInfo.filePath(), uncompressed); uncompressedFiles[fileInfo.filePath()] = uncompressed; } fileInfo.setFile(uncompressed); if (isArchive) { extractor = ArchiveExtractor::extractorForFileName( fileNameWithCorrectExtension); } else { userFriendly = fileNameWithCorrectExtension; } } } userFriendlyFilePath += userFriendly; if (isArchive) { if (extractor == 0) location.fatal(tr("Unknown archive type '%1'") .arg(userFriendlyFilePath)); QString extracted = extractedDirs[fileInfo.filePath()]; if (extracted.isEmpty()) { extracted = QTemporaryFile(fileInfo.filePath()).fileName(); if (!QDir().mkdir(extracted)) location.fatal(tr("Cannot create temporary directory '%1'") .arg(extracted)); extractor->extractArchive(location, fileInfo.filePath(), extracted); extractedDirs[fileInfo.filePath()] = extracted; } ++c; fileInfo.setFile(QDir(extracted), *c); } else { break; } userFriendlyFilePath += "?"; } return fileInfo.filePath(); }
static void processQdocconfFile(const QString &fileName) { QList<QTranslator *> translators; Config config( tr("qdoc") ); int i = 0; while (defaults[i].key) { config.setStringList(defaults[i].key, QStringList() << defaults[i].value); ++i; } config.setStringList(CONFIG_SLOW, QStringList(slow ? "true" : "false")); Location::initialize( config ); config.load( fileName ); config.setStringList(CONFIG_DEFINES, defines + config.getStringList(CONFIG_DEFINES)); Location::terminate(); QString prevCurrentDir = QDir::currentPath(); QString dir = QFileInfo( fileName ).path(); if ( !dir.isEmpty() ) QDir::setCurrent( dir ); Location::initialize( config ); Tokenizer::initialize( config ); Doc::initialize( config ); CppToQsConverter::initialize( config ); CodeMarker::initialize( config ); CodeParser::initialize( config ); Generator::initialize( config ); QStringList fileNames = config.getStringList( CONFIG_TRANSLATORS ); QStringList::Iterator fn = fileNames.begin(); while ( fn != fileNames.end() ) { QTranslator *translator = new QTranslator( 0 ); if ( !translator->load(*fn) ) config.lastLocation().error( tr("Cannot load translator '%1'") .arg(*fn) ); QCoreApplication::instance()->installTranslator( translator ); translators.append( translator ); ++fn; } QString lang = config.getString(CONFIG_LANGUAGE); Location langLocation = config.lastLocation(); Tree *tree = treeForLanguage(lang); tree->setVersion(config.getString(CONFIG_VERSION)); CodeParser *codeParser = CodeParser::parserForLanguage( lang ); if ( codeParser == 0 ) config.lastLocation().fatal(tr("Cannot parse programming language '%1'").arg(lang)); QSet<QString> outputFormats = config.getStringSet(CONFIG_OUTPUTFORMATS); Location outputFormatsLocation = config.lastLocation(); CodeMarker *marker = CodeMarker::markerForLanguage(lang); if (!marker && !outputFormats.isEmpty()) langLocation.fatal(tr("Cannot output documentation for programming language '%1'") .arg(lang)); QStringList indexFiles = config.getStringList(CONFIG_INDEXES); tree->readIndexes(indexFiles); QStringList headers = config.getAllFiles( CONFIG_HEADERS, CONFIG_HEADERDIRS, codeParser->headerFileNameFilter() ); QStringList::ConstIterator h = headers.begin(); while ( h != headers.end() ) { codeParser->parseHeaderFile( config.location(), *h, tree ); ++h; } codeParser->doneParsingHeaderFiles( tree ); QStringList sources = config.getAllFiles( CONFIG_SOURCES, CONFIG_SOURCEDIRS, codeParser->sourceFileNameFilter() ); QStringList::ConstIterator s = sources.begin(); while ( s != sources.end() ) { codeParser->parseSourceFile( config.location(), *s, tree ); ++s; } codeParser->doneParsingSourceFiles( tree ); tree->resolveGroups(); tree->resolveTargets(); QSet<QString>::ConstIterator of = outputFormats.begin(); while ( of != outputFormats.end() ) { Generator *generator = Generator::generatorForFormat( *of ); if ( generator == 0 ) outputFormatsLocation.fatal(tr("Unknown output format '%1'").arg(*of)); generator->generateTree( tree, marker ); ++of; } tree->setVersion(""); Generator::terminate(); CodeParser::terminate(); CodeMarker::terminate(); CppToQsConverter::terminate(); Doc::terminate(); Tokenizer::terminate(); Location::terminate(); QDir::setCurrent( prevCurrentDir ); foreach (QTranslator *translator, translators) delete translator; }
/*! Load, parse, and process a qdoc configuration file. This function is only called by the other load() function, but this one is recursive, i.e., it calls itself when it sees an \c{include} statement in the qdoc configuration file. */ void Config::load(Location location, const QString& fileName) { QFileInfo fileInfo(fileName); QString path = fileInfo.canonicalPath(); pushWorkingDir(path); QDir::setCurrent(path); QRegExp keySyntax(QLatin1String("\\w+(?:\\.\\w+)*")); #define SKIP_CHAR() \ do { \ location.advance(c); \ ++i; \ c = text.at(i); \ cc = c.unicode(); \ } while (0) #define SKIP_SPACES() \ while (c.isSpace() && cc != '\n') \ SKIP_CHAR() #define PUT_CHAR() \ word += c; \ SKIP_CHAR(); if (location.depth() > 16) location.fatal(tr("Too many nested includes")); QFile fin(fileInfo.fileName()); if (!fin.open(QFile::ReadOnly | QFile::Text)) { if (!Config::installDir.isEmpty()) { int prefix = location.filePath().length() - location.fileName().length(); fin.setFileName(Config::installDir + QLatin1Char('/') + fileName.right(fileName.length() - prefix)); } if (!fin.open(QFile::ReadOnly | QFile::Text)) location.fatal(tr("Cannot open file '%1': %2").arg(fileName).arg(fin.errorString())); } QTextStream stream(&fin); #ifndef QT_NO_TEXTCODEC stream.setCodec("UTF-8"); #endif QString text = stream.readAll(); text += QLatin1String("\n\n"); text += QLatin1Char('\0'); fin.close(); location.push(fileName); location.start(); int i = 0; QChar c = text.at(0); uint cc = c.unicode(); while (i < (int) text.length()) { if (cc == 0) { ++i; } else if (c.isSpace()) { SKIP_CHAR(); } else if (cc == '#') { do { SKIP_CHAR(); } while (cc != '\n'); } else if (isMetaKeyChar(c)) { Location keyLoc = location; bool plus = false; QString stringValue; QStringList rhsValues; QString word; bool inQuote = false; bool prevWordQuoted = true; bool metWord = false; MetaStack stack; do { stack.process(c, location); SKIP_CHAR(); } while (isMetaKeyChar(c)); QStringList keys = stack.getExpanded(location); SKIP_SPACES(); if (keys.count() == 1 && keys.first() == QLatin1String("include")) { QString includeFile; if (cc != '(') location.fatal(tr("Bad include syntax")); SKIP_CHAR(); SKIP_SPACES(); while (!c.isSpace() && cc != '#' && cc != ')') { if (cc == '$') { QString var; SKIP_CHAR(); while (c.isLetterOrNumber() || cc == '_') { var += c; SKIP_CHAR(); } if (!var.isEmpty()) { const QByteArray val = qgetenv(var.toLatin1().data()); if (val.isNull()) { location.fatal(tr("Environment variable '%1' undefined").arg(var)); } else { includeFile += QString::fromLatin1(val); } } } else { includeFile += c; SKIP_CHAR(); } } SKIP_SPACES(); if (cc != ')') location.fatal(tr("Bad include syntax")); SKIP_CHAR(); SKIP_SPACES(); if (cc != '#' && cc != '\n') location.fatal(tr("Trailing garbage")); /* Here is the recursive call. */ load(location, QFileInfo(QDir(path), includeFile).filePath()); } else { /* It wasn't an include statement, so it's something else. We must see either '=' or '+=' next. If not, fatal error. */ if (cc == '+') { plus = true; SKIP_CHAR(); } if (cc != '=') location.fatal(tr("Expected '=' or '+=' after key")); SKIP_CHAR(); SKIP_SPACES(); for (;;) { if (cc == '\\') { int metaCharPos; SKIP_CHAR(); if (cc == '\n') { SKIP_CHAR(); } else if (cc > '0' && cc < '8') { word += QChar(c.digitValue()); SKIP_CHAR(); } else if ((metaCharPos = QString::fromLatin1("abfnrtv").indexOf(c)) != -1) { word += QLatin1Char("\a\b\f\n\r\t\v"[metaCharPos]); SKIP_CHAR(); } else { PUT_CHAR(); } } else if (c.isSpace() || cc == '#') { if (inQuote) { if (cc == '\n') location.fatal(tr("Unterminated string")); PUT_CHAR(); } else { if (!word.isEmpty()) { if (metWord) stringValue += QLatin1Char(' '); stringValue += word; #if 0 if (metWord) rhsValues << QString(" " + word); else #endif rhsValues << word; metWord = true; word.clear(); prevWordQuoted = false; } if (cc == '\n' || cc == '#') break; SKIP_SPACES(); } } else if (cc == '"') { if (inQuote) { if (!prevWordQuoted) stringValue += QLatin1Char(' '); stringValue += word; if (!word.isEmpty()) rhsValues << word; metWord = true; word.clear(); prevWordQuoted = true; } inQuote = !inQuote; SKIP_CHAR(); } else if (cc == '$') { QString var; SKIP_CHAR(); while (c.isLetterOrNumber() || cc == '_') { var += c; SKIP_CHAR(); } if (!var.isEmpty()) { const QByteArray val = qgetenv(var.toLatin1().constData()); if (val.isNull()) { location.fatal(tr("Environment variable '%1' undefined").arg(var)); } else { word += QString::fromLatin1(val); } } } else { if (!inQuote && cc == '=') location.fatal(tr("Unexpected '='")); PUT_CHAR(); } } QStringList::ConstIterator key = keys.constBegin(); while (key != keys.constEnd()) { if (!keySyntax.exactMatch(*key)) keyLoc.fatal(tr("Invalid key '%1'").arg(*key)); ConfigVarMultimap::Iterator i; i = configVars_.insert(*key, ConfigVar(*key, rhsValues, QDir::currentPath(), keyLoc)); i.value().plus_ = plus; ++key; } } } else { location.fatal(tr("Unexpected character '%1' at beginning of line").arg(c)); } } popWorkingDir(); if (!workingDirs_.isEmpty()) QDir::setCurrent(workingDirs_.top()); }