/* test sref_value */

/* note: with this utility, you can temporarily change system variables.
   Thus, superuser privileges are required (by default).
   You can relax this by changingt the reqpriv parameter */


REQPRIV='SUPERUSER'                     /* set to '' to allow free access */

parse arg  ddir, tempfile, reqstrg,list,verb ,uri,user, ,
          basedir ,workdir,privset,enmadd,transaction,verbose, ,
         servername,host_nickname,homedir

if verb="" then do
    say "Sorry, this SRE-http can not be run from the command line."
end /* do */

PARSE ARG R1, R2,servername,stale,amessage

if reqpriv<>'' then do
    if wordpos(reqpriv,translate(privset))=0 then do
         aa=sref_ask_auth('TEST_VALUE',,servername)
         return aa
    end /* do */
end /* do */

if upper(verb)='GET' then  parse var uri . '?' list

newval="" ; anenv="OS2" 
varname="" 
miscval=""

readonly=0;treadonly=0

do until list=""
   parse var list a1 '&' list
   parse var a1 avar '=' aval
   avar=upper(avar) ; aval=packur(translate(aval,' ','+'))
   select 
      when avar='ENV' then anenv=aval
      when avar="VARNAME" then varname=aval
      when avar="NEWVAL" then newval=aval
      when avar="MISC"  then miscval=aval
      when avar='READONLY' then 
         if wordpos(strip(aval),'Y YES 1')>0 then readonly=1
   otherwise
   end  /* select */
end /* do */
miscval=strip(miscval)
if readonly=1 then newval=""  /* suppress writing */

parse var uri app '?' .
app='<a href="'app'?env='anenv'&readonly=1&MISC='miscval'&Varname='


/* responses to * contain a space delimited list of variable,pairs.
  Each variable,pair has the "sre_name" first, then a comma, then real name.
  For os/2, request, and user envirnoments, the sre_name and the real_name are the
  same. For the INI environment, the real_name will have a SREF_80, SRE_, SREF_, or
  some such similar "this is an sre variable" prefix appended. (the "real name"
  is the named used by os/2 when storing the value in the environment */
  
gee=sref_value(varname,newval,anenv,miscval)
mx=0
if pos('*',varname)>0 & gee<>' 'then do          /* sort the "list of all variables */
  gob.0=words(gee) ;mx=-1
  do imm=1 to gob.0
     gob=word(gee,imm) 
     parse var gob  g1 ',' g2
     g1=strip(g1)
     if g2='' then g2=g1
     mx=max(mx,length(g1))
     gob.imm=left(g1,60,' ')||g2
  end /* do */
  yee=arraysort('gob',1,,,59)
  gee='<pre>';gee2=' ' ; mgee=0
  do jjj=1 to gob.0
     agobsre=strip(left(gob.jjj,59))
     agobori=strip(substr(gob.jjj,60))
     agob=app||agobori','agobsre||'">'agobsre'</a>'
     aruba=mx+1-length(agobsre)
     agob=agob||copies(' ',aruba)
     gee2=gee2' 'agob
     mgee=mgee+mx+1
     if mgee>65 then do
           gee=gee||gee2||'0d0a'x ; gee2=' ' ; mgee=0
     end /* do */
  end /* do */
  if gee2<>'' then    gee=gee||gee2||'<br></pre>'
end
else do                         /* not a list of all vars */
  gee=sref_replacestrg(gee,'<','&lt;','ALL')  /* html encode dangerous  characters */
  gee=sref_replacestrg(gee,'>','&gt;','ALL')  
  gee=sref_replacestrg(gee,'"','&quot;','ALL')  
  gee=sref_replacestrg(gee,' ',' &nbsp;','ALL')  
end


crlf='0d0a'x
call lineout tempfile,'<html><head><title>Test SREF_VALUE results </title></head><body>'
call lineout tempfile,'<h3>Results of SREF_VALUE </h3>'
call lineout tempfile,' <b>Environment: </b> 'anenv
parse upper var varname  varnamex hn
parse var  varname v1 ',' v2
if v2<>'' then v1=v2 
call lineout tempfile,'<br><b>Variable:</b> 'v1 '<p>'
call lineout tempfile," <b>Value returned=</b>" gee
call lineout tempfile,'</body></html> '
call lineout tempfile
foo=sref_gos('FILE type text/html name 'tempfile)
return ' '


