vipsi - example scripts
Author: kio@little-bat.de
Keywords: vipsi shell script interpreter programming

# vipsi - shell and script language

## cgi example scripts

#### prerequisites

the script must be copied into a directory which is visible by a web server. on a unix installation this may be a web or Sites folder in your home directory. in addition, the web server must be configured to execute scripts at the location where you installed the script. this may urge you to edit .htaccess files or the server setup. this problem is not covered here. see Chapter 1 installation for that.

if all is set up properly then you can run the script from your web browser by typing in it's URL; e.g. http://localhost/cgi-bin/name_of_the_script.cgi.

#### basic notes on cgi scripts

the cgi script must write a http file type declaration header plus the page data as specified in the file type declaration header to stdout.

in the most common case this is a html file:

http-header:

    content-type: text/html\n\n    or:
    content-type: text/html; charset=utf-8\n\n

the html page itself:

    <!doctype html public "-//w3c//dtd html 4.01 strict//en">
    <html>

    <head>
        <title>a title is required!</title>
        <META http-equiv=Content-Type content="text/html; charset=UTF-8">    <!--opt.-->
    </head>

    <body>
        <!-- Contents of page here -->
    </body>

    </html>

### show_environment.cgi

this example script demonstrates how to get the environment variables of the current process. this is simply done using the `env` built-in function, but this script also shows how a simple cgi-script can look like. the data returned by `env` is a list with one item per environment variable. the item contains the data of that variable and is named according to the environment variable. for instance you can read and display the PATH variable just like that: `log env.PATH`.

    #!/usr/bin/vipsi
    //
    // the above line indicates that this is a script and where to find the interpreter

    // print the http header:
    //
    put "content-type: text/html; charset=utf-8\n\n"

    // print the html header:
    //
    put "<!doctype html public \"-//w3c//dtd html 4.01 strict//en\">\n",
        "<html>\n",
        "<head>\n",
        "<META http-equiv=Content-Type content=\"text/html; charset=UTF-8\">\n",
        "<title>vipsi test script for cgi'</title>\n",
        "</head>\n"

    // print the html body:
    //
    put "<body style='font-family:sans-serif;'>\n",
        "<h2>••• env ••• test script</h2>\n"

    //  print environment variables
    //  the script can request the process environment variables with the built-in function env.
    //  the returned list contains one named item per environment variable
    //  e.g. the CGI GET arguments can be found in the variable "QUERY_STRING"

    put "<h4>env environment variables</h4>\n",
        "<table border width='100%'>",
        "<tr><td width='10%'><b>index</b></td><td width='20%'><b>name</b></td><td><b>value</b></td></tr>\n"

        var env_var = env
        convert env_var to printable        // escape control characters
        convert env_var to html             // convert '<' to '&lt;' etc.

        var i = 0
        do
            while ++i <= count env_var

            var ename = name env_var[i]  if ename==""  ename = "<i>unnamed variable</i>" then
            var edata = env_var[i]       if edata==""  edata = "<i>empty</i>" then

            put "<tr><td>env[",i,"]</td><td>", ename, "</td><td>", edata, "</td></tr>\n"
        loop

        put "</table>\n"

    put "</body></html>\n"

    end