Writing CGI executbales with Modula-2.

Nothing beats an example. This is my first CGI executable, written in Modula-2 and compiled for Linux. Although it also runs on a server running FreeBSD.
The file is called 'testCGI' and it does just that. It lists all the environment variables that belong to the CGI system on the server it is installed on.

'testCGI' was written with the Mocka Modula-2 compiler. This results in clear sources and fast execution since this is not an interpreted language. OK, the executable is slightly bigger than a few lines of Perl, but a compiled executable is tamperproof. You can only change it if you know:

I consider Perl a 'write-only' language. If you aren't experienced in it, you cannot read or understand it. And I dare say that everyone can read and even understand a Modula-2 source. Clarity above all.

Show the CGI environment with Modula-2.


FROM  InOut	  IMPORT  WriteCard, WriteLn, WriteString, WriteBf;
FROM  Arguments   IMPORT  ArgTable, GetEnv;
FROM  Strings	  IMPORT  Assign, Length;

TYPE aString = ARRAY [0 .. 511] OF CHAR;

VAR  EnvVars		 : ArgTable;


VAR   Value	     : aString;
      i		     : CARDINAL;

   WriteString ('<table border="1" width="80%" align="center" cellpadding="4">');
   i := 0;
      IF  EnvVars^ [i] = NIL  THEN  EXIT  END;
      Assign (Value, EnvVars^ [i]^);
      WriteString ("<tr><td>");
      WriteCard (i, 4);
      WriteString (' :  [');          WriteCard (Length (Value), 4);
      WriteString ('] </td><td>');    WriteString (Value);
      WriteString ("</td></tr>");
      INC (i)
END ReadEnvVar;

   GetEnv (EnvVars);
   WriteString ('Content-type:text/html');
   WriteString ('<html><head>');
   WriteString ('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">');
   WriteString ('</head><body>');
   WriteString ('<h3 align=center><tt>cgi compiled with Mocka Modula-2</tt></h3>');
   WriteString ('</body></html>');
END testCGI.

How it works.

You can run 'testCGI' in a terminal to list the Unix environment variables (although it will be a bit tough to read, try it!) but it was meant to explore the environment variables that are active when you're browsing the web.
These environment variables are then the CGI (Common Gateway Interface) variables. They enable the browser to communicate with the (remote) server and exchange data.

The 'testCGI' program gets these environment variables from the operating system and prints them one by one in the active window of your web browser. To make reading a bit easier, testCGI exports it's data in HTML format so the browser will print a two column table.

testCGI is fully based upon the operation of 'GetEnv' (qv). The GetEnv program (called 'trye') just prints characters to the user console. But we're in HTML country now so we'd better fit in with the rest.
So I made 'testCGI' to deliver HTML data to be interpreted by a browser. In order to facilitate this, there are some STRICT rules that 'must' be followed to the letter:

  1. Tell the browser what's coming up
  2. Issue two linefeeds
  3. Send the HTML data
Skip one linefeed and the program will execute but nothing will be printed in your browser. Also the position of the 'upcoming format' identifier is crucial. Anything before it, will be ignored.

Explanation of the Modula-2 CGI executable.

This is a Modula-2 program, so if you know some english, you can at least read the sources and understand 90% of it's operation. In order to fully understand the flow of execution of the program for those 10%, I will explain in some more detail below:

What you saw here is a typical Modula-2 programsource. It does what it says. Sometimes you need to get used to the abbreviations, but the language is clear from moment one. Compare that to Perl. An impossible language to learn due to the intensely obscure syntax.

Wrapping up.

You can download the sources and the executable for 'testCGI' in the download section (see navigator frame on the right). The source is handy for studying this program and can be printed well with soup and emit (consult navigator frame).
You will need to do the following if you want to run the 'testCGI' executable in a Unix environment:

Unpack the tar.gz file gzip -d testcgi.tar.gz
tar xvf testcgi.tar
for testing the executable chmod 755 testCGI
for running the CGI 'script' su root
<enter password>
cp testCGI /var/www/cgi-bin/
<start your browser>
surf to 'https://yourserver/cgi-bin/testCGI'

This last command may be different between Linux verions and flavours. This data is valid for Slackware 8.0 but your milage may vary.

Making it active on the web.

What use is a bicycle in the shed? Nothing. You need to ride it to make it worth the effort of making it.

Same with this testCGI executable. You need to run it. Not on your own computer, but out there, in the harsh world of the internet. So I put it on-line at my webhost.

One remark however: testCGI is a 32 bit executable, not a script. So it will only run on a server running Linux-32. If your webhost runs under Windows (or next to a door), you cannot run testCGI. You could try, but chances are you will halt this magnificent Windows computer...

Nuf said. Time to act. The following steps were taken by me to get testCGI online and working:

  1. ncftp
  2. open connection
  3. cd to directory
  4. put testCGI
  5. chmod 755 testCGI
  6. close
  7. exit
Keep in mind that I run Slackware Linux and my favorite ftp program is ncftp. After connection, it's just like you're running 'bash' at a remote host.

Horsing around with 'testCGI'.

If you want to check it out, run the script in your browser (it will open a new window for your convenience): run testCGI now via my webhost: https://verhoeven272.nl/cgi-bin/testCGI.
If you are running this on a local machine, like I do when debugging, run it local via http://localhost/cgi-bin/testCGI.

Now you can also do some tests with the commandline of a CGI script. In the URL window, add the following strings after '/testCGI' and watch how the output changes:

See what happens? Pay special attention to the lower part of the table. In my case, it's options 21 and 22. What's the hassle with the '%20'? Are you lassie? :o)

By now you should have the knack of CGI operation and production. If I can do it in Modula-2, it should be a snap to do a comparable thing with gcc.
But keep in mind that we do NOT make scripts that are interpreted on all platforms. We make executables that will only run on the platform it was developed for. In my case: Linux.

Have fun with the experiments. Come back now and then to see what I added to the CGI events.

Page created on August 25, 2004.