ACC SHELL

Path : /usr/share/YaST2/include/sysconfig/
File Upload :
Current File : //usr/share/YaST2/include/sysconfig/cmdline.ycp

/**
 * File:
 *   include/sysconfig/cmdline.ycp
 *
 * Package:
 *   Editor for /etc/sysconfig files
 *
 * Summary:
 *   Command line interface functions.
 *
 * Authors:
 *   Ladislav Slezak <lslezak@suse.cz>
 *
 * $Id: cmdline.ycp 46193 2008-04-07 14:54:17Z lslezak $
 *
 * All command line interface functions (handlers).
 *
 */

{
    import "Sysconfig";
    import "RichText";
    import "CommandLine";
    import "Progress";

    include "sysconfig/complex.ycp";

    textdomain "sysconfig";

     define string variable2id(string variable);

    /**
     * Command line interface - handler for list command
     * @param options list command options
     * @return boolean Returns true (succeess)
     */
     define boolean listHandler(map options) ``{
	// display all variables or only modified ones?
	boolean all = haskey(options, "all");

	// header (command line mode output)
	CommandLine::Print((all == true) ? _("All Variables:\n") : _("Modified Variables:\n"));

	list<string> modif = (all == false) ? Sysconfig::get_modified() : Sysconfig::get_all();
	string result = "";

	foreach(string v, modif, ``{
		map descr = Sysconfig::get_description(v);
		// display a new value for modified variables
		string value = (descr["new_value"]:nil != nil) ? descr["new_value"]:"" : descr["value"]:"";;

		result = result + sformat("%1=\"%2\"\n", Sysconfig::get_name_from_id(v), value);
	    }
	);

	CommandLine::Print(result);

	return true;
    }

    define boolean setHanlerProcess(string variable, string value, boolean force) {
	    string vid = variable2id(variable);

	    if (vid == nil)
	    {
		return false;
	    }

	    symbol result = Sysconfig::set_value(vid, value, force, false);

	    // status message - %1 is a device name (/dev/hdc), %2 is a mode name (udma2), %3 is a result (translated Success/Failed text)
	    CommandLine::Print(sformat(_("\nSetting variable '%1' to '%2': %3"), variable, value,
	    // result message
	    (result == `ok) ? _("Success") : _("Failed")));

	    return (result == `ok);
    }

    /**
     * Command line interface - handler for set command
     * @param options list command options
     * @return boolean True on success
     */
     define boolean setHandler(map<string, any> options) ``{
	string variable = "";
	string value = "";
	boolean force = options["force"]:true;

	if (haskey(options, "variable") && haskey(options, "value"))
	{
	    variable = (string) (options["variable"]:nil);
	    value = (string) (options["value"]:nil);
	}
	// there is just one pair in the option map,
	// user has called the module with option VARIABLE=value
	else if (size(options) == 1)
	{
	    y2milestone("options: %1", options);

	    foreach(string key, any val, options, {
		    variable = key;
		    value = (string)val;
		}
	    );
	}

	if (variable != "")
	{
	    string vid = variable2id(variable);

	    if (vid == nil)
	    {
		// the variable was not found
		return false;
	    }

	    // set the value
	    return setHanlerProcess(variable, value, force);
	}

	return false;
    }


    /**
     * Command line interface - handler for clear command
     * @param options command options
     * @return boolean True on success
     */
     define boolean clearHandler(map<string, any> options) ``{
	// set empty value
	options["value"] = "";

	// do not check if the value is valid
	options["force"] = true;

	// call set handler
	return setHandler(options);
    }


    /**
     * Command line interface - handler for details command
     * @param options details command options
     * @return boolean True on success
     */
     define boolean detailsHandler(map<string, any> options) ``{
	string variable = (string) (options["variable"]:nil);
	string varid = variable2id(variable);

	if (varid == nil)
	{
	    return false;
	}

	// header (command line mode output)
	CommandLine::Print("\nDescription:\n");
	map<string, any> description = Sysconfig::get_description(varid);

	// display a new value for modified variables
	string value = ((description["new_value"]:nil != nil) ? (_("New Value: ") + description["new_value"]:"") : (_("Value: ") + description["value"]:"")) + "\n";

	// convert description into plain text
	string plaintext = value + create_description(description, false);

	CommandLine::Print(plaintext);

	return true;
    }

    /**
     * Convert variable name to the full variable identification
     * If there are conflicts (same variable is defined more files)
     * or variable is not found nil is returned
     * @param variable Variable name
     * @return string variable identification
     */
     define string variable2id(string variable) ``{
	if (variable != nil)
	{
	    map<string, list<string> > all_names = Sysconfig::get_all_names();
	    list<string> vids = all_names[variable]:nil;

	    if (vids == nil)
	    {
		// variable was not found
		// check whether variable name is complete variable identification
		list all_vids = Sysconfig::get_all();
		y2milestone("variable: %2 all_vids: %1", all_vids, variable);

		if (contains(all_vids, variable))
		{
		    return variable;
		}
		else
		{
		    // command line output
		    CommandLine::Print(sformat(_("Variable %1 was not found."), variable));
		}
	    }
	    else if (size(vids) == 1)
	    {
		return vids[0]:nil;
	    }
	    else
	    {
		// duplicated variable, print found files
		CommandLine::Print(sformat("Variable %1 is located in the following files:\n", variable));

		foreach(string vid, vids, ``{
			string fname = Sysconfig::get_file_from_id(vid);
			CommandLine::Print(fname);
		    }
		);

		// variable name conflict - full name (with file name) is required
		CommandLine::Print(sformat(_("
Use a full variable name in the form <VARIABLE_NAME>$<FILE_NAME>
(e.g., %1$%2).
"), variable, Sysconfig::get_file_from_id(vids[0]:"/etc/sysconfig/unknown")));
	    }
	}

	return nil;
    }

    /**
     * Write handler - disable progress bar (there is no UI) and write settings to the system.
     * @return boolean True on sucess
     */
     define boolean writeHandler() ``{
	// disable progress bar
	Progress::off();

	// write changes, start activation commands
	return Sysconfig::Write();
    }

}

ACC SHELL 2018