void begin_config(const std::string& block, int *pln, int *pcode, int *cp) throw(ParserError) { string block_name(block); ConfigSection* section = g_Config.getSection(block_name); if (section == NULL) { g_throw_parser_error("unrecognized config section '", block_name.c_str(), "'"); } // Don't do config blocks in safe mode (except in RC file) GLEInterface* iface = GLEGetInterfacePointer(); if (iface->getCmdLine()->hasOption(GLE_OPT_SAFEMODE)) { GLEGlobalConfig* config = iface->getConfig(); if (!config->allowConfigBlocks()) { g_throw_parser_error("safe mode - config blocks not allowed"); } } // Start with pcode from the next line (*pln)++; begin_init(); while (true) { int st = begin_token(&pcode,cp,pln,srclin,tk,&ntk,outbuff); if (!st) { /* exit loop */ break; } int ct = 1; int mode = 0; bool plus_is = false; CmdLineOption* option = NULL; while (ct <= ntk) { skipspace; if (section != NULL) { if (mode == 0) { option = section->getOption(tk[ct]); if (option == NULL) { gprint("Not a valid setting for section '%s': {%s}\n", block_name.c_str(), tk[ct]); } } else if (mode == 1) { if (strcmp(tk[ct], "=") == 0) { plus_is = false; } else if (strcmp(tk[ct], "+=") == 0) { plus_is = true; } else { gprint("Expected '=' or '+=', not {%s}\n", tk[ct]); } } else if (option != NULL) { CmdLineOptionArg* arg = option->getArg(0); if (!plus_is) arg->reset(); arg->appendValue(tk[ct]); } mode++; } ct++; } } }
void init_installed_versions(CmdLineObj& cmdline, ConfigCollection* collection) { CmdLineArgSet* versions = (CmdLineArgSet*)cmdline.getOption(GLE_OPT_VERSION)->getArg(0); ConfigSection* gle = collection->getSection(GLE_CONFIG_GLE); CmdLineArgSPairList* installs = (CmdLineArgSPairList*)gle->getOption(GLE_CONFIG_GLE_INSTALL)->getArg(0); if (installs->size() == 0) { versions->addPossibleValue("no older GLE versions found (run \"gle -finddeps\")"); } else { for (int i = 0; i < installs->size(); i++) { versions->addPossibleValue(installs->getValue1(i).c_str()); } } }
bool try_save_config(const string& fname, GLEInterface* iface, bool isUser) { ConfigCollection* collection = iface->getConfig()->getRCFile(); if (collection->allDefaults()) { return true; } if (fname == "") { return false; } if (IsAbsPath(fname)) { std::string dirname; GetDirName(fname, dirname); EnsureMkDir(dirname); } ofstream fout(fname.c_str()); if (!fout.is_open()) { return false; } CmdLineOption* versionOption = collection->getSection(GLE_CONFIG_GLE)->getOption(GLE_CONFIG_GLE_VERSION); ostringstream out; out << "Save configuration to: '" << fname << "'"; GLEOutputStream* output = iface->getOutput(); output->println(out.str().c_str()); for (int i = 0; i < collection->getNbSections(); i++) { ConfigSection* sec = collection->getSection(i); if (!sec->allDefaults()) { fout << "begin config " << sec->getName() << endl; for (int j = 0; j < sec->getNbOptions(); j++) { CmdLineOption* option = sec->getOption(j); if (!option->allDefaults() && (!isUser || option != versionOption)) { fout << "\t" << option->getName() << " = "; for (int k = 0; k < option->getMaxNbArgs(); k++) { if (k != 0) fout << " "; CmdLineOptionArg* arg = option->getArg(k); arg->write(fout); } fout << endl; } } fout << "end config" << endl << endl; } } fout.close(); return true; }
void find_deps(const string& loc, GLEInterface* iface) { vector<GLEFindEntry*> tofind; vector<string*> result; string gle_paths = ";"; ConfigCollection* collection = iface->getConfig()->getRCFile(); #ifdef __WIN32__ GLEFindEntry* findGLE = new GLEFindEntry(&gle_paths); findGLE->addToFind("gle.exe"); findGLE->addToFind("gle_ps.exe"); tofind.push_back(findGLE); #endif // Create GLEFindEntry for each tool (ghostscript, pdflatex, ...) ConfigSection* tools = collection->getSection(GLE_CONFIG_TOOLS); for (int j = 0; j <= GLE_TOOL_GHOSTSCRIPT_LIB; j++) { CmdLineArgString* strarg = (CmdLineArgString*)tools->getOption(j)->getArg(0); GLEFindEntry* findTool = new GLEFindEntry(strarg->getValuePtr()); char_separator separator(",", ";"); tokenizer<char_separator> tokens(strarg->getDefault(), separator); while (tokens.has_more()) { const string& toolName = tokens.next_token(); if (toolName == ";") { if (tokens.has_more() && strarg->isDefault()) { findTool->setNotFound(tokens.next_token()); } break; } else { if (!IsAbsPath(toolName)) { findTool->addToFind(toolName); } } } if (findTool->getNbFind() != 0) tofind.push_back(findTool); else delete findTool; } // Initialize output and progress indicator GLEOutputStream* output = iface->getOutput(); ostringstream out1; out1 << "Running GLE -finddeps \""; out1 << loc; out1 << ("\" to locate installed software (e.g., Ghostscript and LaTeX): "); output->println(out1.str().c_str()); GLEProgressIndicatorInterface progress(iface); // Perform search at specified location if (loc != "") { if (IsDirectory(loc, true)) { GLEFindFiles(loc, tofind, &progress); for (unsigned int i = 0; i < tofind.size(); i++) { tofind[i]->updateResult(false); } } else { // Name of old GLERC file given if (try_load_config(loc)) { // Override old version number collection->setStringValue(GLE_CONFIG_GLE, GLE_CONFIG_GLE_VERSION, GLEVN); } else { ostringstream err; err << "Can't load configuration from '" << loc << "'" << endl; output->println(err.str().c_str()); } } } #ifdef __UNIX__ // Find programs in search path on Unix GLEFindPrograms(tofind, &progress); #endif #ifdef __MACOS__ // Search for frameworks on Mac GLEFindFiles(string("/Library/Frameworks"), tofind, &progress); string home = GetHomeDir(); if (home != "") { home += "Library/Frameworks"; GLEFindFiles(home, tofind, &progress); } #endif for (unsigned int i = 0; i < tofind.size(); i++) { tofind[i]->updateResult(true); } #ifdef __UNIX__ // Search for libraries in typical directories and in LD_LIBRARY_PATH string gslibloc = GLEFindLibrary("libgs", &progress); if (gslibloc != "") { CmdLineArgString* gslib_stra = (CmdLineArgString*)tools->getOption(GLE_TOOL_GHOSTSCRIPT_LIB)->getArg(0); gslib_stra->setValue(gslibloc.c_str()); } #endif output->println(); // Write installed GLE's to config section ConfigSection* gle = collection->getSection(GLE_CONFIG_GLE); CmdLineArgSPairList* installs = (CmdLineArgSPairList*)gle->getOption(GLE_CONFIG_GLE_INSTALL)->getArg(0); char_separator separator(";", ""); tokenizer<char_separator> tokens(gle_paths, separator); while (tokens.has_more()) { string path = tokens.next_token(); if (path.length() > 0 && !installs->hasValue2(path)) { installs->addPair("?", path); } } // Find versions of installed GLEs and set value of gleexe ostringstream out; string gle_version = GLEVN; if (installs->size() > 1) { // Only need to find out versions if more than one installed // otherwise assume it is "this" version for (int i = 0; i < installs->size(); i++) { const string& cr_gle = installs->getValue2(i); string& version = installs->getValue1(i); if (version == "?") { get_version_soft(cr_gle, version); if (version == "?") { // cout << "Use hard method for: " << cr_gle << endl; get_version_hard(cr_gle, version); } } if (str_i_equals(version, gle_version)) { out << "Found: GLE " << version << " in " << cr_gle << " (*)" << endl; } else { out << "Found: GLE " << version << " in " << cr_gle << endl; } } } else if (installs->size() == 1) { out << "Found: GLE in " << installs->getValue2(0) << endl; // Do not need to remember installed GLEs if there is only one // because then the "-v" option makes no sense installs->reset(); } // Show locations of other tools for (int j = 0; j <= GLE_TOOL_GHOSTSCRIPT_LIB; j++) { CmdLineOption* opt = tools->getOption(j); CmdLineArgString* strarg = (CmdLineArgString*)opt->getArg(0); if (strarg->isDefault()) { out << "Found: " << opt->getName() << " in '?'" << endl; } else { out << "Found: " << opt->getName() << " in '" << strarg->getValue() << "'" << endl; } } output->println(out.str().c_str()); for (unsigned int i = 0; i < tofind.size(); i++) { delete tofind[i]; } }