Table of Contents
The YaST-language YCP is a scripting language to be interpreted by the YCP-engine (YCP interpreter) specially designed for manipulation with a system configuration. Its syntax is very similar to C programming language. Because YCP can make use of the whole infrastructure that YaST provides, the actions that can be accomplished with YCP are very powerful.
YCP has the usual features of procedural languages and some more, partially originating from the functional programming paradigm:
Control structures like if/then/else, foreach-loops.
Compound data types like strings, lists and maps.
Function definition (procedures)
Variable scopes
Name spaces
Include files
UNIX command execution (via the YaST infrastructure)
On the following pages we will explore the YCP language definition and find out how to use YCP to write “programs” that can be executed by YaST.
Probably the best way to get into the matter is by means of a simple example.
The following little program opens a window that displays the string “Hello, World!” and provides a push button for termination.
Example 3.1. “Hello World” in YCP
{ string message = "Hello, World!"; UI::OpenDialog( `VBox( `Label( message ), `PushButton("&OK") ) ); UI::UserInput(); UI::CloseDialog(); } |
In the following this code will be explained shortly in a line-by-line manner thereby touching some topics we will examine in detail later on.
{
The opening curly opens a so-called block in YCP. Blocks are used to “glue” several YCP-statements together to form an entity that can be handled just like a single statement.
string message = "Hello, World!";
In this line we define a variable named “message” that is of type string. In YCP any variable definition must imply a value assignment to avoid all errors that might occur due to uninitialized variables. Here we assign the constant string “Hello, World!”. Furthermore the terminating semicolon is mandatory in YCP to indicate the end of a statement (just like C).
UI::OpenDialog(
This command opens a dialog on screen. Because we want to display something, the code describing our dialog has to be sent to the UI. This is being done by the leading name space identifier UI::. The (single) parameter that is supplied here determines the content of the dialog.
`VBox(
This is a UI-statement related to the geometry of the dialog to be defined. As the name indicates it opens a (virtual) vertical box that displays all content in a column-wise manner. (Geometry management is described in more detail in Section 3.9, “Controlling The User Interface”).
The leading back-quote introduces a YCP-feature that stems from the functional programming paradigm. In YCP-speak the `VBox() is a term. In YCP, terms are used as a structured constants and are typically passed to functions provided by YaST infrastructure as parameters as is done here with OpenDialog().
`Label( message ),
Displaying strings in YCP is done by means of Labels. This statement gets one parameter, the string variable we defined in the beginning. Because it is the first of two parameters passed to `VBox() this line is not terminated with a semicolon but with a comma. As in most programming languages commas are used to separate parameters in YCP.
`PushButton("&OK")
This statement displays a labelled push button. Since it is the next element in the enclosing `VBox(), it is displayed immediately below the preceding label. The & in the label string is a YaST feature declaring the subsequent character to be a key-shortcut. As a result the button can not only be clicked with the mouse but also be activated by typing ALT-O.
) and );
The next two lines first close the open `VBox() and then the open OpenDialog(). Because `VBox() is passed as a parameter to OpenDialog() there is no need to terminate the statement with a semicolon. OpenDialog() on the other hand is a statement in the UI and hence must be terminated with a semicolon.
UI::UserInput();
Here we hand over control to the UI which then awaits some sort of user input. In this case it simply waits for the push button to be pressed by the user. Consequently our program blocks at this point until the user really does it.
UI::CloseDialog();
After all the UI-related action has finished, i.e. when UI::UserInput() returns, we want to remove the dialog we just created. This is done here.
}
Indicating the end of the block, the closing curly bracket ends our little YCP-program.
Now we can start the program using YaST. For this, we will use a script /sbin/yast2. It is an envelope for easier setup of a running YaST environment.
So if you are reading this document with a browser, you could copy-and-paste the program listed above into a file hello.ycp, and then run /sbin/yast2 hello.ycp which should render the following “spectacular” result.
Starting off with this simple example we will now explore the more subtle details of YCP. Since all programming is about handling of data there must be a way to hold it in variables of different types. In the next section you will get to know the various data types that YCP knows about.