aUCBLogo Demos and Tests / cgitest4
			
				 #!D:/Users/AndreasM/aucblogo/aucblogo_client.exe
			
			#!D:/Users/AndreasM/aucblogo/aucblogo_client.exe
setCaseIgnored false
be cgitest4
   bury [cgitest4 cgiproc cgi cgi4call]
   tmp=runSecure security_connect [cgiproc]
   output tmp
   be cgiproc
      local [out err result]
      buryname "out
      catch "error
      [
         out=(Word "Content-type: Text/html; charset=iso-8859-1
<HTML>)
         changeDir "D:/Users/AndreasM/aucblogo
         result=cgi
               "D:/Apache2.2/htdocs/
               &out
;         closeall
         if result=="reset [reset]
         (GC true)
      ]
      err=error
      if not emptyP err
      [   out=(Word out first butFirst err 
            "\ in\  first bF bF err
            "( first bF bF bF bF err ")</br>
</HTML>)
      ]
      print Nodes
      output out
   end
   be cgi dname &out
      local 
      [   err errout 
         request_method query_string content_length 
         query_length maxArgs argCount name value 
         start_name start_value end_value end_name 
         namename i gr ch1 ch2
         %run %action %animate %stop %reset 
         %commands %console %history 
         %framenr %errout %time
         %username %lastusername %lastlastusername
         %splitter1 %splitter12 %splitter2 %splitter22 %splitter23
         docommands
         fname
         graphhtml graphhtmlpath 
         consolehtml consolehtmlpath 
         outputhtml
         framename framepath
         txt o
         memsize oldmemsize maxmem minmem
      ]
      
      maxmem=10*1000*1000
      minmem=10*1000
      
      %username="guest
      %lastusername="
      %lastlastusername="
      %splitter1="50%
      %splitter12="50%
      %splitter2="50%
      %splitter22="40%
      %splitter23="10%
      %commands="
      %console="
      %history="
      %action="
      %errout="
   
      request_method=getEnv "REQUEST_METHOD
      query_string  =getEnv "QUERY_STRING
      content_length=getEnv "CONTENT_LENGTH
   ;request_method="GET
   ;query_string  ="heading=10&time=1234
   ;content_length=getenv "CONTENT_LENGTH
   ;print query_string
   
      if (empty? request_method) or2 empty? query_string
      [   goTo "nix
      ]
   
      query_length=0
      if request_method==" [output []]
   
      if request_method=="GET or2 request_method=="POST
      [   if query_string==" [output []]
   ;      ifElse content_length=="
   ;      [
            query_length=count query_string
   ;      ][   query_length=Int content_length
   ;      ]
      ]
   ;pr query_length
   ;pr count query_string
   
   ;   if request_method=="POST
   ;   [   if content_length==0 [output []]
   ;      query_length=Int content_length
   ;      setReader "stdin
   ;      query_string=readRawLine
   ;      setReader []
   ;   ]
      maxArgs=20
      name=Array maxArgs
      value=Array maxArgs
      for [i 1 maxArgs 1]
      [   name.i=[]
         value.i=[]
      ]
      end_value=0
      argCount=1
   ;pause
      while [end_value < query_length]
      [   start_name=end_value+1
         end_name=start_name
         while [and [end_name<=query_length][query_string.end_name !="=]]
         [   end_name=end_name+1
         ]
         name.argCount=Items start_name end_name-1 query_string
         name.argCount=decode_string name.argCount
   
         start_value=end_name+1
         end_value=start_value
         while [end_value<query_length]
         [   if query_string.end_value =="& [break]
            if query_string.end_value =="+  
            [   query_string.end_value="\ 
            ]
            end_value=end_value+1
         ]
         ifElse end_value<=query_length
         [   value.argCount=Items start_value end_value query_string
            value.argCount=decode_string value.argCount
         ][   value.argCount="
         ]
         argCount=argCount+1
      ]
      argCount=argCount-1
      
      if argCount > 0
      [   for [i 1 argCount 1]
         [   if not empty? name.i
            [   namename=Word "% name.i
               make namename  value.i
               bury namelist namename
            ]   
         ]
      ]
   
   tag "nix
      if not name? "%reset [%reset="]
      if not name? "%animate [%animate="]
      if not name? "%stop [%stop="]
      if not Name? "graphs
      [   graphs=Table 31
         frames=Table 31
         commands=Table 31
         historys=Table 31
         memsizes=Table 31
         users=
         [   guest andreas stefan swirke christian robert 
            clem pavel brian david michael gerhard
         ]
         bury [[][users commands historys memsizes]]
      ]
      if not Member? %username users
      [   %username="guest
      ]
      if empty? graphs.%username
      [   frames.%username=Frame [] %username wxdefault_frame_style [0 0][400 300]
         graphs.%username=Graph frames.%username
         FrameSetClientSize frames.%username 400 300
         FrameIconize frames.%username true
         memsizes.%username=maxmem
      ]
      ifelse Name? "%commands
      [   ifelse %username == %lastusername
         [   commands.%username=%commands
            historys.%username=%history
         ][   if not empty? commands.%username
            [   %commands=commands.%username
               %history=historys.%username
            ]
   (pr %lastlastusername %lastusername %username)
            %lastlastusername=%lastusername
            %lastusername=%username
         ]
      ][   %commands=commands.%username
         if %commands==[] [%commands=" ]
         %history=historys.%username
         if %history==[] [%history=" ]
      ]
   
;      fname=BitAnd butFirst genSym 63
      
      GraphSetCurrent graphs.%username
      fname=%username
      local [txtname]
      outputhtml=Word %username ".output.html
      txtname=(Word dname outputhtml)
      ifElse name? "%framenr [::framenr=%framenr][%framenr=0 ::framenr=0]
      if not Number? ::framenr [pr ::framenr ::framenr=0]
      ::singleshot=Name? "::framenr
      ifElse %reset !="reset
      [   
         ifelse %action=="console
         [   docommands=%console
         ][   docommands=%commands
         ]
         (type "; %username ":
            docommands "
         )
         logname=Word dname "cgitest4.txt
         openAppend logname
         setWriter logname
         (print (Word "; %username ":
         docommands))
         setWriter []
         close logname
   
;         %commands=strip_html %commands
;(pr [B] %commands [Y])
         if Member? txtname allopen
         [   close txtname
         ]
         openWrite txtname
         setWriter txtname
         type "<pre>
         setCaseIgnored true
         oldmemsize=Nodes.3
         cgi4call docommands %username memsizes.%username oldmemsize
         memsizes.%username=memsizes.%username-Nodes.3+oldmemsize
         if memsizes.%username < minmem
         [   memsizes.%username=minmem
         ]
         if memsizes.%username > maxmem
         [   memsizes.%username=maxmem
         ]
         setCaseIgnored false
         changeDir "D:/Users/AndreasM/aucblogo
         err=error
         ifElse not emptyP err
         [   errout=(Word "
         <tr><td style="height:20; color:yellow" bgcolor=red><b>
             
               first butFirst err 
               "\ in\  first bF bF err
               "( first bF bF bF bF err ")</br>
               "
         </b></td></tr>)
         ][   errout=" 
         ]
         type "</pre>
         setWriter []
         close txtname
      ][   errout="
      ]
      setSaveSize [400 300]
      saveScreen (Word dname fname ".png)
   ;   openRead (Word dname fname ".png)
   ;   close (Word dname fname ".png)
      GraphSetCurrent []
      
      graphhtml=(Word %username ".graph.html)
      graphhtmlpath=(Word dname graphhtml)
      openWrite graphhtmlpath
      setWriter graphhtmlpath
      (type "
<html>
   <head>
      <script type="text/javascript">
function sizeit()
{
   var graph=document.getElementById('graph');
   var w=window.innerWidth;
   var h=window.innerHeight;
   if (w==undefined || h==undefined)
   {   w=document.body.clientWidth;
      h=document.body.clientHeight;
   }
   if (w==NaN || h==NaN)
      return;
   if (w < 2)
      w=2;
   if (h < 2)
      h=2;
   if (w < h*4/3)
   {   document.images.graph.style.width=w-2;
      document.images.graph.style.height=w*3/4-2;
   }else
   {   document.images.graph.style.width=h*4/3-2;
      document.images.graph.style.height=h-2;
   }
}
window.onresize=sizeit;
window.onload=sizeit;
      </script>
   </head>
   <body scroll="no"
   marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" rightmargin="0">
      <table Border="0" rules="rows"
      style="width:100%; height:100%;">
         <tr><td valign="middle">
            <center>
               <image id="graph" name="graph" src=" %username ".png"
               style="width:100%; height:100%;">
            </center>
         </td></tr>
      </table>
   </body>
</html>)
      setWriter []
      close graphhtmlpath
   
      consolehtml=(Word %username ".console.html)
      consolehtmlpath=(Word dname  consolehtml)
      openWrite consolehtmlpath
      setWriter consolehtmlpath
      (type "
<html>
   <head>
      <script type="text/javascript">
function condosubmit()
{
   top.editorframe.document.Form1.console.value=
      document.Form2.console.value;
   top.editorframe.document.Form1.history.value+=
      document.Form2.console.value+'\n';
   top.editorframe.document.Form1.action.value='console';
   top.editorframe.dosubmit();
   top.editorframe.document.Form1.submit();
   return true;
}
var line=0;
var hist=[];
function doonkeydown(event)
{
   var key;
   if (!event)
      event = window.event;
   if (event.which) 
   {   key = event.which;
   }else if (event.keyCode) 
   {   key = event.keyCode;
   }
   if (key==13)
      condosubmit();
   else if (key==38)
   {   if (line > 0)
         line--;
      document.Form2.console.value=
         hist[line];
   }   
   else if (key==40)
   {   if (line < hist.length-1)
         line++;
      document.Form2.console.value=
         hist[line];
   }   
}
document.onkeydown=doonkeydown;
function doonload()
{
   var i=0,start=0,
      s=document.Form2.history.value;
   var len=s.length;
   while (i < len)
   {   if (s.charAt(i)=='\n')
      {   hist[line]=s.substr(start, i-start);
         start=i+1;
         line++;
      }
      i++;
   }
   hist[line]='';
   document.Form2.console.focus();
}
window.onload=doonload;
      </script>
      <base target="_top">
   </head>
   <body
   marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" rightmargin="0">
      <form id="Form2" target="_top" action="cgitest4.lg" method="post" 
      name="Form2" style="height:95%" onsubmit="return dosubmit();">
         <textarea id="history" name="history" rows="10" cols="70"
         style="display:none; width:100%; height:100%; font-size:10; border:0;"
         tabindex="0"> %history "</textarea>
         <table width="100%">
            <tr><td>
               <textarea id="console" name="console" rows="1" cols="70"
               style="width:100%; height:1.2em; border:0;"
               tabindex="0"></textarea>
            </td><td>
               <input name="go" type="submit" value="go" tabindex="2">
            </td></tr>
         </table>
      </form>
   </body>
</html>)
      setWriter []
      close consolehtmlpath
   
      txt=" 
      if File? txtname
      [   openRead txtname
         setReader txtname
         txt=readChars FileSize txtname
         setReader []
         close txtname
         eraseFile txtname
      ]
   ;      This is process number  GetProcessId ".
   ;      </br>)
      o=(Word 
"<html>
   <head>
      <style type="Text/css">
         .output { Background-color:#FFFFFF; font-size:80%; }
      </style>
      <script type="Text/javascript">
function setFocusRun()
{      )
      o=(Word o
         ;o=(word o
         ;[document.Form1.commands.rows=
         ;   (window.innerHeight-500)/20;
         ;])
         ifelse %animate=="animate and2 %stop !="stop and2 empty? errout
         [   "document.Form1.submit();
         ][   "
         ]
      )
      o=(Word o
         "               
}
      </script>
      <script type="text/javascript">
function cgitest_init()
{
   setFocusRun();
}
window.onload=cgitest_init;
function dosubmit()
{
   if (window.innerWidth==undefined)
   {   document.Form1.splitter1.value=top.graphframe.document.body.clientWidth;
      document.Form1.splitter12.value=top.editorframe.document.body.clientWidth;
      document.Form1.splitter2.value=top.graphframe.document.body.clientHeight;
      document.Form1.splitter22.value=top.outputframe.document.body.clientHeight;
      document.Form1.splitter23.value=top.consoleframe.document.body.clientHeight;
   }else
   {   document.Form1.splitter1.value=top.graphframe.window.innerWidth;
      document.Form1.splitter12.value=top.editorframe.window.innerWidth;
      document.Form1.splitter2.value=top.graphframe.window.innerHeight;
      document.Form1.splitter22.value=top.outputframe.window.innerHeight;
      document.Form1.splitter23.value=top.consoleframe.window.innerHeight;
   }
   return true;
}
      </script>
      <base target="_top">
   </head>
   <body
   marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" rightmargin="0">
      <table Border="0" rules="rows"
      style="width:95%; height:95%;"> errout)
;         if name? "argCount
;         [   for [i 1 argCount]
;            [   o=(word o name.i "= value.i "\ )
;            ]
;         ]
;         o=word o getEnvTable
      o=(Word o "
         <tr><td>
            <form id="Form1" action="cgitest4.lg" method="post" 
            name="Form1" style="height:95%" onsubmit="return dosubmit();">
               <textarea id="commands" name="commands" rows="2" cols="10"
               style="width:100%; height:90%; font-size:10; border:0;"
               tabindex="0"> %commands "</textarea>
               )
         ifElse %animate=="animate and2 %stop !="stop
         [   o=(Word o 
            "<input name="run" type="hidden" value="run"> 
;            "<input name="animate" type="hidden" value="animate"> 
            "<input name="stop" type="submit" value="stop" tabindex="1" accesskey="s"> 
            "<input name="framenr" size="10" border="0" value="
            %framenr+1
            "" tabindex="2">
            )
         ][   o=(Word o
               "<input id="run" name="run" type="submit" value="run" tabindex="1" accesskey="r"> 
;                  "<input name="animate" type="submit" value="animate" tabindex="2" accesskey="a"> 
               "<input name="framenr" size="4" border="0" value="
            %framenr+1
                  "" tabindex="4"> )
         ]
         o=(Word o "
               <input name="username" size="16" border="0" 
               value=" %username "" tabindex="5"> )
         o=(Word o Int memsizes.%username/1000 "KB
            "<input name="console" type="hidden" value=" %console "">
               <textarea id="history" name="history" style="display:none"> %history "</textarea>
               <input name="action" type="hidden" value="run">
               <input name="time" type="hidden" value=" TimeFine "">
               <input name="lastusername" type="hidden" value=" %lastusername "">
               <input name="lastlastusername" type="hidden" value=" %lastlastusername "">
               <input name="splitter1" type="hidden" value=" %splitter1 "">
               <input name="splitter12" type="hidden" value=" %splitter12 "">
               <input name="splitter2" type="hidden" value=" %splitter2 "">
               <input name="splitter22" type="hidden" value=" %splitter22 "">
               <input name="splitter23" type="hidden" value=" %splitter23 "">
            </form>
         </td></tr>
      </table>
   </body>
</html>
)
      framename=(Word %username ".html)
      framepath=Word dname framename
      openWrite framepath
      setWriter framepath
      type o
      setWriter []
      close framepath
   
      out=(Word out "
   <frameset cols=" %splitter1 ", %splitter12 "">
      <frameset rows=" %splitter2 ", %splitter22 ",20">
         <frame src=" graphhtml "" name="graphframe">
         <frame src=" outputhtml "" name="outputframe">
         <frame src=" consolehtml "" name="consoleframe">
      </frameset>
      <frame src=" framename "" name="editorframe">
   </frameset>
</html>
)
   ;   updateVars
      if %reset=="reset [output "reset]
      output []
   end
   be decode_string str
   ;   local [length outpos ch1 ch2 n]
      length=count str
      outpos=1
      for [i 1 length 1]
      [   ifElse str.i == "&
         [;   str.outpos=" 
         ;   outpos=outpos+1
         ][   ifElse str.i == "%
            [   i=i+1   ch1=upperCase str.i
               i=i+1   ch2=upperCase str.i
               n=toInt  Word ch1 ch2  16
               if n != 10
               [   str.outpos=Char n
                  outpos=outpos+1
               ]
            ][   str.outpos=str.i
               outpos=outpos+1
            ]
         ]
      ]
      output Items 1 outpos-1 str
   end
   
   be strip_html in
      inmax=count in
      inp=1
      outp=1
      while [inp <= inmax]
      [   ifelse in.inp=="<
         [   ifelse and [inp+5<=inmax][(items inp inp+5 in)=="<br />]
            [   in.outp=Char 13
               inp=inp+6
               outp=outp+1
            ][
            ifelse and [inp+3<=inmax][(items inp inp+3 in)=="</p>]
            [   in.outp=Char 13
               inp=inp+4
               outp=outp+1
            ][
               while [and [inp <=inmax][in.inp != ">]]
               [   inp=inp+1
               ]
               inp=inp+1
            ]]
         ][   
         ifelse in.inp=="&
         [   ifelse and [inp+5<=inmax][(items inp inp+5 in)==" ]
            [   inp=inp+6
               in.outp="\ 
               outp=outp+1
            ][   ifelse and [inp+5<=inmax][(items inp inp+5 in)==""]
            [   inp=inp+6
               in.outp=""
               outp=outp+1
            ][   inp=inp+1
            ]]
         ][
         ifelse (or (in.inp==char 13)
         (and in.inp=="\  [inp+6<=inmax][(items inp+1 inp+6 in)=="<br />])
         (and in.inp=="\  inp+1>inmax))
         [   inp=inp+1
         ][
            in.outp=in.inp
            inp=inp+1
            outp=outp+1
         ]]]
      ]
      output items 1 outp-1 in
   end
   
   be test_strip
      foreach strip_html "a<b>c<br />d e
      [   (pr ? ascii ?)
      ]
      s=strip_html 
      "to p1<br />   pr 5<br />end<br />p1
      pr s
      foreach s
      [   (pr ? ascii ?)
      ]
   end
end   ;cgitest4
be reset
   setCaseIgnored false
;   foreach allopen [close ?]
   clearShadows
   setUpdateGraph true
   setPenColor 0
   setScreenColor 7
   setFloodColor 0
   setPenSize [1 1]
   disableCylinderLines
   disableTexture
   enableLineSmooth
   setDepthFunc 1
   setLabelFont [Times]
   setLabelAlign 0 0
   showTurtle
   PenDown
   setLightAmbient RGB 0.1 0.1 0.1
   setLightDiffuse RGB 1 1 1
   unperspective
   clearScreen
   refresh
   wrap
   insertMode
   notFullScreen
   deleteTextures
   (GC true)
   erase 
      remove "cgitest4 
      remove "cgitest4::cgiproc 
      remove "cgitest4::cgi 
      remove "cgitest4::reset 
      Procedures
   erase 
      remove "cgitest4::cgiproc::out 
      Names
;   erase (List [] [] 
;      remove "cgitest4 
;      remove "cgitest4::cgiproc 
;      remove "cgitest4::cgi 
;      first bF bF Tables)
;   erase buried
   (GC true)
end
to cgi4call docommands username memsize oldmemsize
   catch "error
   [
      (runSecure 
         security_dlcall
         +security_iodll
         +security_shell
         +security_filewrite 
         ;+security_fileread
         ;+security_dirs
         +security_thread
         ;+security_keyboard
         +security_gui
         ;+security_erase
         +security_buried
         +security_namespace
         docommands
         1024   ;stack size (in bytes)
         1000000   ;graphic record size (in graphic primitives)
         memsize+oldmemsize   ;memory size (in bytes)
         65536   ;max. number of nodes
         7000   ;timeout (in milliseconds)
         username
         [username memsize framenr]   
            ; ^-- get pushed on the varStack in Namespace username
      )
   ]
end