CGI : List environment variables

While running some tests for a new project, I wanted to see if I could list the CGI Environment variables in a less efficient way:

Why the linked list? It makes searching the environment variables faster. Here's the source of the running program:
MODULE	aboutvars;

IMPORT	Arguments, InOut, MemPools, Strings, SYSTEM;

TYPE	VarPtr	= POINTER TO VarNode;
	VarNode	= RECORD
		    varName, value	: Strings.String;
		    next		: VarPtr
		  END;

VAR	envTab		: Arguments.ArgTable;
	Env0		: VarPtr;
	VarPool		: MemPools.MemPool;
	str		: Strings.String;


PROCEDURE spaces (n : CARDINAL);

BEGIN
  WHILE  n > 0  DO  InOut.Write (" ");  DEC (n)  END
END spaces;


PROCEDURE Init;

BEGIN
  Arguments.GetEnv (envTab);
  MemPools.NewPool (VarPool);
  MemPools.PoolAllocate (VarPool, Env0, SYSTEM.TSIZE (VarNode));
END Init;


PROCEDURE Split (VAR src, dest  : Strings.String) : INTEGER;

VAR	delta, eq	: CARDINAL;

BEGIN
  eq := Strings.pos ("=", src);
  IF  eq > HIGH (src)  THEN   RETURN -1  END;
  delta := Strings.Length (src) - eq;
  Strings.Copy (src, eq + 1, delta, dest);
  src [eq] := 0C;
  RETURN eq
END Split;


PROCEDURE StoreEnv () : BOOLEAN;

VAR	this, prev		: VarPtr;
	result, i		: INTEGER;
	cgivar, load		: Strings.String;
	  
BEGIN
  this := Env0;		prev := NIL;
  i := 0;
  WHILE  envTab^ [i] # NIL  DO
    Strings.Assign (cgivar, envTab^ [i]^);
    IF  Split (cgivar, load) = -1  THEN
      InOut.WriteString ("String cannot be split! ");
      InOut.WriteString (cgivar);
      InOut.WriteLn;
      RETURN FALSE
    END;
    WITH  this^  DO
      varName := cgivar;
      value := load
    END;
    IF  prev # NIL  THEN  prev^.next := this  END;
    prev := this;
    MemPools.PoolAllocate (VarPool, this, SYSTEM.TSIZE (VarNode));
    IF  this = NIL  THEN
      InOut.WriteString ("Cannot allocate memory; aborting.");
      InOut.WriteLn;
      RETURN FALSE
    END;
    INC (i)
  END;
  this^.next := NIL;
  this^.varName := "The End";
  RETURN TRUE
END StoreEnv;


PROCEDURE SearchEnv (var  : Strings.String; VAR res  : Strings.String) : BOOLEAN;

VAR	this		: VarPtr;

BEGIN
  this := Env0;
  REPEAT
    IF  Strings.compare (var, this^.varName) = 0  THEN
      res := this^.value;
      RETURN TRUE
    END;
    this := this^.next
  UNTIL  this = NIL;
  RETURN FALSE
END SearchEnv;


PROCEDURE  ShowList;

VAR     thisOne		: VarPtr;
	str		: Strings.String;

BEGIN
  thisOne := Env0;
  REPEAT
    str := thisOne^.varName;
    spaces (24 - Strings.Length (str));
    InOut.WriteString (str);
    InOut.WriteString ("  =  ");
    InOut.WriteString (thisOne^.value);		InOut.WriteLn;
    thisOne := thisOne^.next
  UNTIL thisOne = NIL
END  ShowList;


PROCEDURE run;

BEGIN
  InOut.WriteString ("Content-type: text/html");
  InOut.WriteLn;
  InOut.WriteLn;
  InOut.WriteString ("<html>");			InOut.WriteLn;
  InOut.WriteString ("<head><title>CGI vars</title></head>");
  InOut.WriteLn;
  InOut.WriteString ("<body>");			InOut.WriteLn;
  InOut.WriteString ("<h1>CGI VARs</h1>");	InOut.WriteLn;
  InOut.WriteString ("<hr><pre>");		InOut.WriteLn;
  ShowList;
  IF SearchEnv ("SCRIPT_NAME", str) = TRUE  THEN  InOut.WriteString (str); InOut.WriteLn END;
  InOut.WriteString ("</pre><hr>");		InOut.WriteLn;
  InOut.WriteString ("</body></html>");		InOut.WriteLn;
END run;


BEGIN
  Init;
  IF  StoreEnv () = TRUE  THEN  run  END
END aboutvars.
   
It runs! As can be seen in the picture below

At least, it ran, then...

But will it run again in 2020? Not sure. For some silly reasons I have great difficulties in getting the programs to work again. First dowload aboutvars.tgz extract the executable, store it in your local cgi-bin and run aboutvars from this webpage. And it bombs out, not even halfway the listing. Don't use this software... On the other hand, other CGI programs work just fine.

Also when I run ./aboutvars as-is on the commandline it lists all the bash environment variables without any problem. So apparently it is something strange.


Page created on 23 March 2013 and