Пример #1
0
int CpCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 2)
	{
		throw invalid_argument ("wrong number of arguments, 2 needed");
	}

	KeySet conf;
	Key sourceKey = cl.createKey (0);
	if (!sourceKey.isValid ())
	{
		throw invalid_argument ("Source given is not a valid keyname");
	}

	Key destKey = cl.createKey (1);
	if (!destKey.isValid ())
	{
		throw invalid_argument ("Destination given is not a valid keyname");
	}
	string newDirName = destKey.getName ();

	kdb.get (conf, sourceKey);
	kdb.get (conf, destKey);
	KeySet tmpConf = conf;
	KeySet oldConf;

	oldConf.append (tmpConf.cut (sourceKey));

	KeySet newConf;

	oldConf.rewind ();
	std::string sourceName = sourceKey.getName ();
	if (cl.verbose) cout << "common name: " << sourceName << endl;
	if (cl.recursive)
	{
		// copy all keys with new name
		Key k;
		while ((k = oldConf.next ()))
		{
			Key rk = rename_key (k, sourceName, newDirName, cl.verbose);
			copySingleKey (cl, rk, tmpConf, newConf);
		}
	}
	else
	{
		// just copy one key
		Key k = oldConf.next ();
		Key rk = rename_key (k, sourceName, newDirName, cl.verbose);
		copySingleKey (cl, rk, tmpConf, newConf);
	}

	newConf.append (tmpConf); // these are unrelated keys
	newConf.append (oldConf); // these are the original keys

	newConf.rewind ();
	kdb.set (newConf, destKey);

	return 0;
}
Пример #2
0
int FstabCommand::execute (Cmdline const & cl)
{
	int argc = cl.arguments.size ();
	if (argc != 5 && argc != 6 && argc != 7)
	{
		throw invalid_argument ("number of arguments not correct, need 5, 6 or 7");
	}

	KeySet conf;
	Key parentKey = cl.createKey (0);
	kdb.get (conf, parentKey);
	printWarnings (cerr, parentKey);
	Key k = conf.lookup (parentKey);

	if (!k)
	{
		k = cl.createKey (0);
		conf.append (k);
	}

	std::string keyname = k.getName ();

	string dumpfreq = "0";
	if (argc >= 6)
	{
		dumpfreq = cl.arguments[5].c_str ();
	}

	string passno = "0";
	if (argc >= 7)
	{
		passno = cl.arguments[6].c_str ();
	}

	kdb::KeySet config (20, *kdb::Key (keyname + "/ZZZNewFstabName", KEY_END),
			    *kdb::Key (keyname + "/ZZZNewFstabName/device", KEY_VALUE, cl.arguments[1].c_str (), KEY_END),
			    *kdb::Key (keyname + "/ZZZNewFstabName/mpoint", KEY_VALUE, cl.arguments[2].c_str (), KEY_END),
			    *kdb::Key (keyname + "/ZZZNewFstabName/type", KEY_VALUE, cl.arguments[3].c_str (), KEY_END),
			    *kdb::Key (keyname + "/ZZZNewFstabName/options", KEY_VALUE, cl.arguments[4].c_str (), KEY_END),
			    *kdb::Key (keyname + "/ZZZNewFstabName/dumpfreq", KEY_VALUE, dumpfreq.c_str (), KEY_END),
			    *kdb::Key (keyname + "/ZZZNewFstabName/passno", KEY_VALUE, passno.c_str (), KEY_END), KS_END);

	conf.append (config);

	if (cl.verbose)
	{
		cout << conf;
	}

	kdb.set (conf, parentKey);
	printWarnings (cerr, parentKey);

	return 0;
}
Пример #3
0
int LsCommand::execute (Cmdline const & cl)
{
	checkArguments (cl);

	printWarnings (cerr, root);

	root = cl.createKey (0);

	kdb.get (ks, root);

	if (cl.verbose) cout << "size of all keys in mountpoint: " << ks.size () << endl;

	KeySet part (ks.cut (root));

	if (cl.verbose) cout << "size of requested keys: " << part.size () << endl;
	cout.setf (std::ios_base::unitbuf);
	if (cl.null)
	{
		cout.unsetf (std::ios_base::skipws);
	}

	printResults (part, getDepth (root), cl);

	printWarnings (cerr, root);

	return 0;
}
Пример #4
0
int FileCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument");

	KeySet conf;
	Key x = cl.createKey (0);
	if (x.getName ()[0] == '/')
	{
		x.setName (cl.ns + x.getName ());
		std::cerr << "Using name " << x.getName () << std::endl;
	}
	if (!x.isValid ())
	{
		throw invalid_argument (cl.arguments[0] + " is not a valid keyname");
	}

	kdb.get (conf, x);
	cout << x.getString ();

	if (!cl.noNewline)
	{
		cout << endl;
	}

	return 0;
}
Пример #5
0
int GetCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument");

	KeySet conf;

	kdb::Key root = cl.createKey (0);
	kdb::KDB kdb (root);

	std::string n;
	if (cl.all)
	{
		n = root.getName ();
		root.setName ("/");
	}

	kdb.get (conf, root);

	if (cl.all)
	{
		root.setName (n);
	}

	// do a lookup without tracer to warm up default cache
	conf.lookup (root);

	root.setCallback (warnOnMeta);
	if (cl.verbose)
	{
		cout << "got " << conf.size () << " keys" << std::endl;
		root.setCallback (printTrace);
	}
	Key k = conf.lookup (root);

	int ret = 0;

	if (k)
	{
		if (cl.verbose)
		{
			cout << "The resulting keyname is " << k.getName () << std::endl;
		}
		cout << k.getString ();
	}
	else
	{
		cerr << "Did not find key";
		ret = 1;
	}

	if (!cl.noNewline)
	{
		cout << endl;
	}

	printWarnings (cerr, root);
	printError (cerr, root);

	return ret;
}
Пример #6
0
void MountCommand::buildBackend (Cmdline const & cl)
{
	MountBackendBuilder backend;

	Key mpk (mp, KEY_CASCADING_NAME, KEY_END);

	if (!mpk.isValid ())
	{
		throw invalid_argument (mp + " is not a valid mountpoint");
	}

	backend.setMountpoint (mpk, mountConf);

	backend.setBackendConfig (cl.getPluginsConfig ("system/"));

	PluginSpec resolver (cl.resolver);
	if (cl.debug)
	{
		cout << "Trying to load the resolver plugin " << resolver.getName () << endl;
	}

	backend.addPlugin (PluginSpec (resolver));

	if (cl.interactive)
	{
		cout << endl;
		cout << "Enter a path to a file in the filesystem." << endl;
		cout << "The path must either not exist or be a file." << endl;
		cout << "For user or cascading mountpoints it must be a relative path." << endl;
		cout << "Then, the path will be resolved dynamically." << endl;
		cout << "Path: ";
		cin >> path;
	}
Пример #7
0
int LsCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 1)
	{
		throw invalid_argument ("1 argument required");
	}

	printWarnings (cerr, root);

	root = cl.createKey (0);

	kdb.get (ks, root);

	if (cl.verbose) cout << "size of all keys in mountpoint: " << ks.size () << endl;

	KeySet part (ks.cut (root));

	if (cl.verbose) cout << "size of requested keys: " << part.size () << endl;
	cout.setf (std::ios_base::unitbuf);
	if (cl.null)
	{
		cout.unsetf (std::ios_base::skipws);
	}

	cout << part;

	printWarnings (cerr, root);

	return 0;
}
Пример #8
0
int MetaRemoveCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 2)
	{
		throw invalid_argument ("Need 2 arguments");
	}
	Key parentKey = cl.createKey (0);
	string metaname = cl.arguments[1];

	KeySet conf;
	kdb.get (conf, parentKey);
	printWarnings (cerr, parentKey);

	Key k = conf.lookup (parentKey);

	if (!k)
	{
		cerr << "Key not found" << endl;
		return 1;
	}

	k.delMeta (metaname);

	kdb.set (conf, parentKey);

	return 0;
}
Пример #9
0
int ExportCommand::execute (Cmdline const & cl)
{
	size_t argc = cl.arguments.size ();
	if (argc != 1 && argc != 2 && argc != 3)
	{
		throw invalid_argument ("need 1 to 3 arguments");
	}

	Key root = cl.createKey (0);

	kdb.get (ks, root);
	printWarnings (cerr, root);

	KeySet part (ks.cut (root));

	if (cl.withoutElektra)
	{
		Key systemElektra ("system/elektra", KEY_END);
		part.cut (systemElektra);
	}

	string format = cl.format;
	if (argc > 1) format = cl.arguments[1];

#ifdef _WIN32
	string file = "CON";
#else
	string file = "/dev/stdout";
#endif
	if (argc > 2 && cl.arguments[2] != "-") file = cl.arguments[2];

	Modules modules;
	PluginPtr plugin = modules.load (format, cl.getPluginsConfig ());

	Key errorKey (root);
	errorKey.setString (file);

	plugin->set (part, errorKey);

	printWarnings (cerr, errorKey);
	printError (cerr, errorKey);

	return 0;
}
Пример #10
0
int MetaSetCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () < 2 || cl.arguments.size () > 3)
	{
		throw invalid_argument ("Need 2 or 3 arguments");
	}
	string metaname = cl.arguments[1];

	Key parentKey = cl.createKey (0);
	string keyname = parentKey.getName ();
	if (keyname[0] == '/')
	{
		// fix name for lookup
		keyname = "spec" + keyname;
		if (!cl.quiet) std::cout << "Using keyname " << keyname << std::endl;

		// fix k for kdb.set later
		parentKey.setName (keyname);
	}

	KeySet conf;
	kdb.get (conf, parentKey);
	Key k = conf.lookup (parentKey);

	if (!k)
	{
		k = Key (keyname, KEY_END);
		// k.setBinary(0, 0); // conceptually maybe better, but would have confusing "binary" metadata
		conf.append (k);
		if (cl.verbose) cout << "Creating key " << keyname << endl;
	}
	if (!k.isValid ())
	{
		cerr << "Could not create key " << keyname << endl;
		return 1;
	}

	if (cl.arguments.size () == 2)
	{
		if (!cl.quiet) cout << "Only two arguments, thus deleting metaname " << metaname << endl;
		k.delMeta (metaname);
	}
	else
	{
		std::string metavalue = cl.arguments[2];
		if (metaname == "atime" || metaname == "mtime" || metaname == "ctime")
		{
			stringstream str (metavalue);
			time_t t;
			str >> t;
			if (!str.good ()) throw "conversion failure";
			k.setMeta<time_t> (metaname, t);
		}
		else
		{
Пример #11
0
void SpecMountCommand::setMountpoint (Cmdline const & cl)
{
	if (cl.arguments.empty ())
	{
		throw invalid_argument ("you need to provide one argument: spec-mountpoint");
	}

	mp = cl.createKey (0).getName ();

	if (mp.at (0) != '/')
	{
		throw invalid_argument (mp + " is not a cascading mountpoint");
	}
}
Пример #12
0
Move
DecideMove(NodePtr& parent,
           NodePtr& bestChild,
           Position& currPosition, Piece::Color color,
           Cmdline const & cmdline,
           int & nLeavesEvaluated)
{
    Trace t("DecideMove()");
    Move bestMove;
    double bestValue;
    double alphaBetaPruneValue = g_bigval[color];
    bestChild = parent->MinMaxValue(bestValue, &bestMove, currPosition,
                                    cmdline.MaxDepth(),
                                    alphaBetaPruneValue, nLeavesEvaluated);
    return bestMove;
}
Пример #13
0
void SpecMountCommand::buildBackend (Cmdline const & cl)
{
	SpecReader sr;

	kdb::KeySet specToRead;
	kdb.get (specToRead, "spec" + mp);
	specToRead = specToRead.cut (Key ("spec" + mp, KEY_END));

	sr.readSpecification (specToRead);

	SpecReader::Backends const & backends = sr.getBackends ();

	for (auto & p : backends)
	{
		auto backend = p.second;
		if (cl.verbose)
		{
			std::cout << "Got mountpoint from " << p.first.getName () << " with " << backend.nodes
				  << " nodes, configfile: " << backend.getConfigFile () << " and mountpoint: " << backend.getMountpoint ()
				  << std::endl;
		}

		backend.setBackendConfig (cl.getPluginsConfig ("system/"));
		backend.needPlugin (cl.resolver);
		backend.needPlugin ("storage");

		backend.addPlugins (parseArguments (cl.plugins));

		const int alreadyRead = 1; // we already read mountpoint
		if (cl.arguments.size () <= alreadyRead)
		{
			backend.addPlugins (parseArguments (cl.arguments.begin () + alreadyRead, cl.arguments.end ()));
		}

		// Call it a day
		outputMissingRecommends (backend.resolveNeeds (cl.withRecommends));
		Backends::umount (backend.getMountpoint (), mountConf);
		backend.serialize (mountConf);
	}

	if (!cl.quiet && backends.empty ())
	{
		std::cout << "No metadata \"mountpoint\" found below spec" << mp << std::endl;
	}
}
Пример #14
0
int MetaLsCommand::execute (Cmdline const & cl)
{
	int ret = 0;
	if (cl.arguments.size () != 1)
	{
		throw invalid_argument ("1 argument required");
	}

	Key root = cl.createKey (0);

	kdb.get (ks, root);

	Key k = ks.lookup (root);

	if (k)
	{
		if (cl.verbose)
		{
			std::cout << "Got key " << k.getName () << std::endl;
		}

		k.rewindMeta ();
		while (const Key meta = k.nextMeta ())
		{
			cout << meta.getName ();
			if (cl.null)
			{
				cout << '\0' << std::flush;
			}
			else
			{
				cout << endl;
			}
		}
	}
	else
	{
		std::cerr << "Did not find key" << std::endl;
		ret = 1;
	}

	printWarnings (cerr, root);

	return ret;
}
Пример #15
0
int TestCommand::execute(Cmdline const& cl)
{
	if (cl.arguments.size() < 1)
	{
		throw invalid_argument ("need at least one argument");
	}

	// do a basic check on every argument
	for (size_t i=1; i<cl.arguments.size(); ++i)
	{
		string name = " ";
		name += cl.arguments[i];
		name += " ";
		if (testNames.find(name) == std::string::npos)
		{
			throw invalid_argument ("test name " +
					cl.arguments[i] +
					" does not exist in:" +
					testNames);
		}
	}

	printWarnings(cerr, root);

	root = cl.createKey(0);

	KDB kdb;
	KeySet original;
	kdb.get(original, root);
	original.rewind();

	doTests(cl.arguments);

	cerr << "We got " << nrError << " errors in " << nrTest << " testcases." << endl;

	cout << "Test suite is now finished." << endl;
	cout << "Now restoring the original keyset." << endl;
	kdb.set(original, root);

	printWarnings(cerr, root);

	return nrError;
}
Пример #16
0
int ValidationCommand::execute(Cmdline const& cl)
{
	size_t argc = cl.arguments.size();
	if (argc != 3 && argc != 4)
	{
		throw invalid_argument("need 3 or 4 arguments");
	}

	KeySet conf;
	Key parentKey = cl.createKey(0);
	string keyname = parentKey.getName();
	kdb.get(conf, parentKey);
	Key k = conf.lookup(keyname);

	if (!k)
	{
		k = Key(keyname, KEY_END);
		conf.append (k);
	}

	if (!k.isValid())
	{
		throw invalid_argument("keyname not valid");
	}

	string value = cl.arguments[1];
	string validationregex = cl.arguments[2];
	string validationmessage;
	if (argc == 4) validationmessage = cl.arguments[3];
	else validationmessage = "Regular expression " + validationregex + " does not match the supplied value";

	k.setString (value);
	k.setMeta<string> ("validation/regex", validationregex);
	k.setMeta<string> ("validation/message", validationmessage);

	kdb.set(conf,parentKey);

	return 0;
}
Пример #17
0
int UmountCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 1) throw invalid_argument ("1 argument required");

	KeySet conf;
	Key parentKey (Backends::mountpointsPath, KEY_END);
	kdb.get (conf, parentKey);
	printWarnings (cerr, parentKey);

	std::string name = cl.createKey (0).getName ();

	if (cl.verbose) Backends::findBackend (name, conf, true);

	if (Backends::umount (name, conf) == 0)
	{
		cerr << "Mountpoint " << name << " does not exist" << endl;
		return 1;
	}

	kdb.set (conf, parentKey);

	return 0;
}
Пример #18
0
int MetaGetCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 2)
	{
		throw invalid_argument ("Need 2 arguments");
	}
	Key parentKey = cl.createKey (0);
	string metaname = cl.arguments[1];

	KeySet conf;
	kdb.get (conf, parentKey);
	printWarnings (cerr, parentKey);

	Key k = conf.lookup (parentKey);

	if (!k)
	{
		cerr << "Key not found" << endl;
		return 1;
	}

	if (!k.getMeta<const Key> (metaname))
	{
		cerr << "Metakey not found" << endl;
		return 2;
	}

	cout << k.getMeta<string> (metaname);

	if (!cl.noNewline)
	{
		cout << endl;
	}

	return 0;
}
Пример #19
0
int ImportCommand::execute (Cmdline const & cl)
{
	size_t argc = cl.arguments.size ();
	if (argc != 1 && argc != 2 && argc != 3)
	{
		throw invalid_argument ("need 1 to 3 arguments");
	}

	Key root = cl.createKey (0);
	if (!root.isValid ())
	{
		throw invalid_argument ("root key \"" + cl.arguments[0] + "\" is not a valid key name");
	}

	KeySet originalKeys;
	kdb.get (originalKeys, root);
	KeySet base = originalKeys.cut (root);
	printWarnings (cerr, root);

	string format = cl.format;
	if (argc > 1) format = cl.arguments[1];

	string file = "/dev/stdin";
	if (argc > 2 && cl.arguments[2] != "-") file = cl.arguments[2];

	Modules modules;
	PluginPtr plugin = modules.load (format, cl.getPluginsConfig ());

	Key errorKey (root);
	errorKey.setString (file);

	KeySet importedKeys;
	plugin->get (importedKeys, errorKey);
	importedKeys = importedKeys.cut (root);

	printWarnings (cerr, errorKey);
	printError (cerr, errorKey);

	ThreeWayMerge merger;
	MergeHelper helper;

	helper.configureMerger (cl, merger);
	MergeResult result = merger.mergeKeySet (
		MergeTask (BaseMergeKeys (base, root), OurMergeKeys (base, root), TheirMergeKeys (importedKeys, root), root));

	helper.reportResult (cl, result, cout, cerr);

	int ret = -1;
	if (!result.hasConflicts ())
	{
		if (cl.verbose)
		{
			cout << "The merged keyset with strategy " << cl.strategy << " is:" << endl;
			cout << result.getMergedKeys ();
		}

		KeySet resultKeys = result.getMergedKeys ();
		originalKeys.append (resultKeys);
		kdb.set (originalKeys, root);
		ret = 0;
	}

	return ret;
}
Пример #20
0
int MvCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 2)
	{
		throw invalid_argument ("wrong number of arguments, 2 needed");
	}

	KeySet conf;
	Key sourceKey = cl.createKey (0, false);

	Key destKey = cl.createKey (1, false);
	string newDirName = destKey.getName ();

	Key root = tools::helper::commonKeyName (sourceKey, destKey);
	if (cl.verbose) std::cout << "using common basename: " << root.getName () << std::endl;
	kdb.get (conf, root);
	KeySet tmpConf = conf;
	KeySet oldConf;

	oldConf.append (tmpConf.cut (sourceKey));
	std::string sourceName = sourceKey.getName ();

	if (!oldConf.size ())
	{
		std::cerr << "No key to copy found below '" << sourceName << "'" << std::endl;
		return 11;
	}

	KeySet newConf;

	Key k;
	oldConf.rewind ();

	if (cl.recursive)
	{
		while ((k = oldConf.next ()))
		{
			newConf.append (rename_key (k, sourceName, newDirName, cl.verbose));
		}
	}
	else
	{
		// just rename one key
		k = oldConf.next ();
		if (k != sourceKey)
		{
			cerr << "First key found " << k.getName () << " does not exactly match given key " << sourceKey.getName ()
			     << ", aborting (use -r to move hierarchy)\n";
			return 11;
		}
		newConf.append (rename_key (k, sourceName, newDirName, cl.verbose));
	}
	newConf.append (tmpConf); // these are unrelated keys
	// drop the original configuration

	newConf.rewind ();
	if (cl.verbose)
	{
		cout << "Will write out:" << endl;
		cout << newConf;
	}

	kdb.set (newConf, root);
	printWarnings (cerr, root);

	return 0;
}
Пример #21
0
int GetCommand::execute (Cmdline const & cl)
{
	if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument");

	KeySet conf;

	kdb::Key root = cl.createKey (0);
	kdb::KDB kdb (root);

	std::string n;
	if (cl.all)
	{
		n = root.getName ();
		root.setName ("/");
	}

	kdb.get (conf, root);

	if (cl.all)
	{
		root.setName (n);
	}

	// do a lookup without tracer to warm up default cache
	conf.lookup (root);

	root.setCallback (warnOnMeta);
	if (cl.verbose)
	{
		cout << "got " << conf.size () << " keys" << std::endl;
		root.setCallback (printTrace);
	}
	Key k = conf.lookup (root);

	int ret = 0;

	if (k)
	{
		if (cl.verbose)
		{
			cout << "The resulting keyname is " << k.getName () << std::endl;
			cout << "The resulting value size is " << k.getStringSize () << std::endl;
		}

		if (k.isBinary ())
		{
			if (cl.verbose)
			{
				if (k.getBinarySize () == 0)
				{
					cout << "The key is null." << std::endl;
				}
				else
				{
					cout << "The key is binary." << std::endl;
				}
			}
			cout << std::hex;
			const uint8_t * data = static_cast<const uint8_t *> (k.getValue ());
			for (auto position = 0; position < k.getBinarySize (); position++)
			{
				cout << "\\x" << unsigned(data[position]);
			}
			cout << std::dec;
		}
		else
		{
			cout << k.getString ();
		}
	}
	else
	{
		cerr << "Did not find key '" << root.getName () << "'";
		ret = 11;
	}

	if (!cl.noNewline)
	{
		cout << endl;
	}

	printWarnings (cerr, root);
	printError (cerr, root);

	return ret;
}
Пример #22
0
int MergeCommand::execute(Cmdline const& cl)
{

	if (cl.arguments.size () < 4)
	{
		throw invalid_argument ("wrong number of arguments, 4 needed");
	}

	Key oursRoot = cl.createKey(0);
	Key theirsRoot = cl.createKey(1);
	Key baseRoot  = cl.createKey(2);
	Key resultRoot = cl.createKey(3);

	KeySet ours;
	KeySet theirs;
	KeySet base;

	{
		KDB lkdb;
		lkdb.get (ours, oursRoot);
		ours = ours.cut (oursRoot);
		ours.lookup(oursRoot, KDB_O_POP);
		if (cl.verbose) std::cout << "we got ours: " << oursRoot << " with keys " << ours << std::endl;
	}
	{
		KDB lkdb;
		lkdb.get (theirs, theirsRoot);
		theirs = theirs.cut (theirsRoot);
		ours.lookup(oursRoot, KDB_O_POP);
		if (cl.verbose) std::cout << "we got theirs: " << theirsRoot << " with keys " << theirs << std::endl;
	}
	{
		KDB lkdb;
		lkdb.get (base, baseRoot);
		base = base.cut (baseRoot);
		ours.lookup(oursRoot, KDB_O_POP);
		if (cl.verbose) std::cout << "we got base: " << baseRoot << " with keys " << base << std::endl;
	}

	KeySet resultKeys;
	kdb.get (resultKeys, resultRoot);

	KeySet discard = resultKeys.cut (resultRoot);
	if (discard.size () != 0)
	{
		if (cl.force)
		{
			if (cl.verbose)
			{
				std::cout << "will remove " << discard.size () << " keys, because -f was given" << std::endl;
			}
		}
		else
		{
			std::cerr << discard.size ()
					<< " keys exist in merge resultroot, will quit. Use -f to override the keys there." << std::endl;
		}
	}

	MergeHelper helper;
	ThreeWayMerge merger;

	helper.configureMerger (cl, merger);

	MergeResult result = merger.mergeKeySet (
			MergeTask (BaseMergeKeys (base, baseRoot), OurMergeKeys (ours, oursRoot),
					TheirMergeKeys (theirs, theirsRoot), resultRoot));

	helper.reportResult (cl, result, cout, cerr);

	int ret = 0;
	if (!result.hasConflicts ())
	{
		resultKeys.append(result.getMergedKeys());
		kdb.set (resultKeys, resultRoot);
	}
	else
	{
		ret = -1;
	}

	return ret;
}
Пример #23
0
NodePtr
MakeMove(NodePtr& parent,
         int i_move, Piece::Color pieceColor, Cmdline const & cfg,
         Position & currPosition)
{
    Move move;
    int nLeavesEvaluated = 0;
    NodePtr bestChild;

    if(((pieceColor == Piece::white) 
             && strchr(cfg.MachinePlays().c_str(), 'w'))
    ||  ((pieceColor == Piece::black)
             && strchr(cfg.MachinePlays().c_str(), 'b')))
    {
        move = DecideMove(parent, bestChild,
                          currPosition, pieceColor, cfg, nLeavesEvaluated);

        if (pieceColor == Piece::white)
        {
            cout << (i_move+1) << ". ";
        } else {
            cout << "... ";
        }
        cout << move.Algebraic(currPosition) << "\n";
    } else
    {
        string humanMove;  // In algebraic notation
        bool legal(false);
        while(!legal)
        {
            cin >> humanMove;
            move = Move(humanMove);
            if(currPosition.IsLegalMove(move))
            {
                legal = true;
            } else
            {
                cout << "Illegal move.  Try again.\n";
            }
        }
    }
    currPosition += move;

    if (cfg.DebugLevel() > 2)
    {
        cout << "Value = " << currPosition.Evaluate() << '\n';
        cout << "Evaluated " << nLeavesEvaluated << " moves.\n";
    }

    if(cfg.DebugLevel() > 3)
    {
        currPosition.Display();
    }
    if(currPosition.NoKing())
    {
        cout << "Mate\n";
        exit(0);
    }

    return bestChild;
}
Пример #24
0
int EditorCommand::execute(Cmdline const& cl)
{
#ifdef _WIN32
	throw EditorNotAvailable();
#endif

	int argc = cl.arguments.size ();
	if (argc < 1)
	{
		throw invalid_argument ("wrong number of arguments, 1 needed");
	}
	Key root = cl.createKey(0);

	KeySet ours;
	KDB kdb;
	kdb.get (ours, root);
	KeySet oursToEdit = ours.cut (root);

	// export it to file
	string format = cl.format;
	if (argc > 1) format = cl.arguments[1];

	Modules modules;
	PluginPtr plugin = modules.load(format);

	tmpFile();
	if (cl.verbose) std::cout << "filename set to " << filename << std::endl;
	Key errorKey(root);
	errorKey.setString(filename);

	if (plugin->set(oursToEdit, errorKey) == -1)
	{
		printWarnings(cerr, errorKey);
		printError(cerr, errorKey);
		return 11;
	}

	printWarnings(cerr, errorKey);


	// start editor
	if (cl.verbose) std::cout << "running editor with " << filename << std::endl;
	if (!cl.editor.empty())
	{
		if (!runEditor (cl.editor, filename))
		{
			std::cerr << "Could not run editor " << cl.editor << std::endl;
			return 12;
		}
	} else {
		if (!runAllEditors(filename))
		{
			std::cerr << "Could not run any editor, please change /sw/elektra/kdb/#0/current/editor" << std::endl;
			return 12;
		}
	}

	// import from the file
	KeySet importedKeys;
	plugin->get(importedKeys, errorKey);
	importedKeys = importedKeys.cut(root);

	printWarnings (cerr, errorKey);
	printError (cerr, errorKey);

	ThreeWayMerge merger;
	MergeHelper helper;

	helper.configureMerger (cl, merger);
	MergeResult result = merger.mergeKeySet (
			MergeTask (BaseMergeKeys (oursToEdit, root), OurMergeKeys (oursToEdit, root),
					TheirMergeKeys (importedKeys, root), root));

	helper.reportResult (cl, result, cout, cerr);

	int ret = 13;
	if (!result.hasConflicts ())
	{
		if (cl.verbose)
		{
			cout << "The merged keyset with strategy " << cl.strategy << " is:" << endl;
			cout << result.getMergedKeys();
		}

		KeySet resultKeys = result.getMergedKeys();
		if (cl.verbose) std::cout << "about to write result keys " << resultKeys << std::endl;
		ours.append(resultKeys);
		kdb.set (ours, root);
		if (cl.verbose) std::cout << "successful, cleaning up " << filename << std::endl;
		unlink(filename.c_str());
		ret = 0;
	}
	else
	{
		std::cout << "Import not successful, please import and remove \"" << filename << '"' << std::endl;
	}

	return ret;
}
Пример #25
0
int InfoCommand::execute (Cmdline const & cl)
{
	std::string subkey;
	if (cl.arguments.size () == 1)
	{
	}
	else if (cl.arguments.size () == 2)
	{
		subkey = cl.arguments[1];
	}
	else
	{
		throw invalid_argument ("Need at 1 or 2 argument(s)");
	}
	std::string name = cl.arguments[0];

	KeySet conf;
	Key parentKey (std::string ("system/elektra/modules/") + name, KEY_END);

	if (!cl.load)
	{
		kdb.get (conf, parentKey);
	}

	if (!conf.lookup (parentKey))
	{
		if (!cl.load)
		{
			cerr << "Module does not seem to be loaded." << endl;
			cerr << "Now in fallback code. Will directly load config from plugin." << endl;
		}

		Modules modules;
		KeySet ks = cl.getPluginsConfig ();
		PluginPtr plugin;
		if (ks.size () == 0)
		{
			plugin = modules.load (name);
		}
		else
		{
			plugin = modules.load (name, ks);
		}
		conf.append (plugin->getInfo ());
	}

	Key root (std::string ("system/elektra/modules/") + name + "/exports", KEY_END);

	if (!subkey.empty ())
	{
		root.setName (std::string ("system/elektra/modules/") + name + "/infos/" + subkey);
		Key k = conf.lookup (root);
		if (k)
		{
			cout << k.getString () << std::endl;
			return 0;
		}
		else
		{
			cerr << "clause not found" << std::endl;
			return 1;
		}
	}

	root.setName (std::string ("system/elektra/modules/") + name + "/exports");
	Key k = conf.lookup (root);

	if (k)
	{
		cout << "Exported symbols: ";
		while ((k = conf.next ()) && k.isBelow (root))
		{
			cout << k.getBaseName () << " ";
		}
		cout << endl;
	}
	else
		cout << "no exported symbols found" << endl;

	root.setName (std::string ("system/elektra/modules/") + name + "/infos");
	k = conf.lookup (root);

	if (k)
	{
		while ((k = conf.next ()) && k.isBelow (root))
		{
			cout << k.getBaseName () << ": " << k.getString () << endl;
		}
	}
	else
		cout << "no information found" << endl;

	return 0;
}