Feb 2009 -- Due to time pressures, the ongoing development of 'rope' has been suspended temporarily.

IpTables Rope - The Stack

The Rope programming language is a stack-based, reverse-polish language. In order to understand the language properly, it is important to understand the idea of a "stack".

Rope's stack is loosely equivalent to a pile of plates. You can either put new plates onto the top of the pile or take existing plates off the top. In order to access the plates at the bottom of the pile, you must first remove those that are above them. Each item of data (or value) that Rope manipulates is a "plate" in this analogy.

The Plates

In Rope, the simplest plates that can be placed on the stack are string and integer values. Here's an example of a stack with four values in it..

 --[top]-->  29
             "HTTP/1.0"
             "X-Field-Value"
             0

In this example, the top value is 29 and the bottom one is 0.

Pushing

The process of adding a new value to the top of the stack is commonly called "pushing" the value. In Rope and other reverse-polish languages, all you need to do is to mention the value in your script, and it gets pushed onto the stack for you.

Heres an example Rope script fragment that pushes the string "Hello" and the number 198 on to the stack from the previous example

 "Hello" 198

Note that all that you've done is to specify the two values you want pushed, no commands or special characters are required. The resulting stack now contains 6 plates, and looks like this..

 --[top]-->  198
             "Hello"
             29
             "HTTP/1.0"
             "X-Field-Value"
             0

Notice that the value that is now on the top of the stack is the last one that was put there by the script, and the item below it is the one that was put there just before.

Popping

Once values have been stored on the stack, they are ready to be manipulated in various ways by Rope's action words. To take a simple example, the neg action can be used to negate (make negative) the top value on the stack. So; if you run the following simple script with the stack shown above..

 neg

The new stack would look like this..

 --[top]-->  -198
             "Hello"
             29
             "HTTP/1.0"
             "X-Field-Value"
             0

What neg has actually done for you here is..

If you then run a script with just the command..

 println

the result would be a smaller stack...

 --[top]-->  "Hello"
             29
             "HTTP/1.0"
             "X-Field-Value"
             0

and the number -198 printed out on the screen.

The println command has done this for you..

The neg and println actions (which is what Rope calls commands) have done different things with the stack, but they have both "popped" a value off the top of it and done something with that value. This is how you pass arguments to actions in Rope.

Some actions (such as add) work with more than one value from the top of stack. For example, if you push the values 29 and 78 onto our example stack, thus..

 29 78

the resulting grown stack will look like this..

 --[top]-->  78
             29
             "Hello"
             29
             "HTTP/1.0"
             "X-Field-Value"
             0

The add action would then "pop" the top two items, add them together and "push" back the result. The resulting changed stack would look like this..

 --[top]-->  57
             "Hello"
             29
             "HTTP/1.0"
             "X-Field-Value"
             0

You could then use the println action to print the result of the calculation. Here's the complete script we've just been talking about.

 29 78 add println

Dropping

Sometimes you wind up with too many plates on the stack, and you need to simply throw some away. The command "drop" does this for you; it simply pops one item off the top of the stack and does nothing with it. It is important to keep the stack from growing too large in a complex script because it has a limited size. If you try to push too many items onto the stack, your script will abort with a "Stack Full" error.

You can also use drop2, which "drops" the top two items, "drop3" (you guessed) or drop4.

Finally: you can use "dropall" which empties the stack down completely - a grand plate-smashing time!