home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / SREFV12J / SREFAST2.CMD < prev    next >
OS/2 REXX Batch file  |  1997-06-30  |  36KB  |  1,172 lines

  1. /* The SSI cache daemon for SREFAST. */
  2.  
  3. foo=rxfuncquery('sysloadfuncs')
  4. if foo=1 then do
  5.   call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  6.  
  7.   call SysLoadFuncs
  8. end
  9. foo=rxfuncquery('rexxlibregister')
  10. if foo=1 then do
  11.  call rxfuncadd 'rexxlibregister','rexxlib', 'rexxlibregister'
  12.  call rexxlibregister
  13. end
  14.  
  15. parse arg port,basedir,refresh,force_refresh,datdir,tdir,verbose,key_preface,enmadd,servername0,diskfree
  16.  
  17. if port=" " | basedir=" " then do
  18.    say "This is an SRE-Filter procedure "
  19.    exit
  20. end  /* Do */
  21.  
  22. if datatype(diskfree)<>'NUM' then  diskfree=10
  23. diskfree=diskfree*1000000
  24.  
  25. if datatype(REFRESH)<>'NUM' then  refresh=60
  26. if datatype(force_REFRESH)<>'NUM' then  force_refresh=60
  27.  
  28.  
  29. ooindx=basedir||'\INITFAST.'||port
  30. datdir=translate(datdir,'\','/')
  31. tdir=strip(translate(tdir,'\','/'),,'\')||'\'
  32. transaction=1000000                             /* arbitratry value */
  33.  
  34. thd=dostid()
  35. call pmprintf_sref(' SREFAST ssi daemon started on thread 'thd' for server: ' servername0)
  36.  
  37. afilt2=tdir||'_fstindx.'||port          /* delete old index */
  38. foo=sysfiledelete(afilt2)
  39. thedrive=filespec('d',afilt2)
  40.  
  41. THEsem='\SEM32\'||enmadd
  42.  
  43. mysem=thesem||"t"||THD
  44. myqueue="SREF_"||port||'_t'||THD
  45.  
  46. goserve_ver=server('H')
  47. aa=make_semmi()                 /* create own queue and thread */
  48.  
  49. foogoo2:                /* load version: but might need to wait for a second */
  50. signal on syntax name foogoo
  51. signal on error name foogoo
  52. iok=0
  53. filter_name=sref_version()
  54. iok=1
  55. foogoo:
  56. signal off syntax ; signal off error ;
  57. if iok=0 then do
  58.    call syssleep 3
  59.    signal foogoo0
  60. end
  61.  
  62. signal on error name tdog
  63. signal on syntax name tdog
  64.  
  65. call syssleep 2                /* give srefiltr's other threads a chance to catch up */
  66. iter=0
  67.  
  68. dynlist=" cgi_inc_timefmt date_local date_gmt server_protocol request_method username remote_addr auth_name "
  69. dynlist=dynlist||' http_accept browser referer date_local2 time time_gmt date_gmt2 read_head  '
  70. dynlist=upper(dynlist)
  71.  
  72. /* create file stamps */
  73. initfilt_file=basedir||"\INITFILT."||port
  74. aa=sysfiletree(initfilt_file,bb,'FT')
  75. initfilt_stamp=space(upper(bb.1))
  76.  
  77. virtual_file=get_value('VIRTUAL_FILE')
  78. if virtual_file=' ' then virtual_file=0
  79. virtual_stamp=' '
  80. if virtual_file<>0 then do
  81.    aa=sysfiletree(virtual_file,bb,'FT')
  82.   virtual_stamp=space(upper(bb.1))
  83. end
  84.  
  85. repstrgs_file=get_value('REPSTRGS_FILE')
  86. if repstrgs_file=' '  then repstrgs_file=0
  87. repstrgs_stamp=' '
  88. if repstrgs_file<>0 then do
  89.    aa=sysfiletree(repstrgs_file,bb,'FT')
  90.   repstrgs_stamp=space(upper(bb.1))
  91. end
  92.  
  93. if stream(ooindx,'c','query exists')="" then   do
  94.    initfast_stamp=' '
  95. end
  96. else do
  97.    aa=sysfiletree(ooindx,bb,'FT')
  98.    initfast_stamp=space(upper(bb.1))
  99. end
  100.  
  101. DO FOREVER
  102.  
  103.  
  104.  makechange=0
  105.  do mm=1 to force_refresh
  106.    iter=iter+1
  107.     if iter=1 then leave                /* do it on first iteration */
  108.     call syssleep refresh
  109. /* check stamps */
  110.      AChAnge=check_stamps(ooindx,initfilt_file,repstrgs_file,virtual_file)
  111.      IF ACHANGE=1 then LEAVE     /* CHANGE DETECTED */
  112.      IF VERBOSE>1 then CALL pmprintf_sref(' SREFAST ssi daemon: no change in configuration files ')
  113.  END
  114.  
  115.  
  116. /* IF HERE, THEN REFRESH CONFIGURATION FILE */
  117. /* suppress use of ssis (for now) */
  118.    oo=cvread(afilt2,daindx)
  119.    if oo=1 then do
  120.      daindx.!nossi=1
  121.      oo=cvwrite(afilt2,daindx)
  122.   end
  123.   drop daindx.
  124.  
  125.  
  126. /* delete old cache files (and then recreate them */
  127.    lookstar=tdir||'_*.sf1'
  128.    foo=sysfiletree(lookstar,stuff,'FO')
  129.    do mm=1 to stuff.0
  130.       foo=sysfiledelete(stuff.mm)
  131.    end /* do */
  132.    if stuff.0>0 t& verbose>0 then
  133.       call pmprintf_sref('SREFAST ssi daemon:: Deleted ' stuff.0 ' cache files from: 'tdir)
  134.  
  135.    if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon: checking configuration file ... ')
  136.  
  137.   oo=get_index(ooindx)
  138.  
  139.   if oo=0 then do
  140.      call pmprintf_sref(" SREFAST ssi daemon: index " ooindx " not available")
  141.      iterate
  142.   end
  143.  
  144.   if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon:  Checking ' daindx.0 ' entries ')
  145.  
  146. /* configuration file is okay, look for ssi entries */
  147.   do mm=1 to daindx.0
  148.      if symbol('DAINDX.'||mm||.'!SSI')<>'VAR' then  iterate
  149.      if daindx.mm.!ssi=0 then iterate
  150.      if symbol('DAINDX.'||mm||.'!NODO')='VAR' then do
  151.          if daindx.mm.!nodo=1 then iterate
  152.      end
  153.  
  154. /* do this one.  Determine file name */
  155.      ddir=datdir
  156.      dnameonly=0
  157.      if symbol('Daindx.'mm'.!DIR')='VAR' then do
  158.         ddir=strip(upper(daindx.mm.!dir))
  159.         if abbrev(ddir,'HTTP://')=1 then iterate  /* can't do this one */
  160.         if symbol('Daindx.'mm'.!DIR_NAMEONLY')='VAR' then do
  161.               dnameonly=daindx.mm.!dir_nameonly
  162.         end
  163.      end
  164.      afile=fig_afile(ddir,daindx.mm.!sel,dnameonly)
  165.  
  166.     if pos('*',afile)>0 then iterate
  167.     froot=tdir'_'port||mm
  168.     cshfile=froot||'.SF1'
  169.  
  170.     host_nickname=' '
  171.     servername=servername0
  172.     if symbol('Daindx.'||mm||.'!HOST')='VAR' then host_nickname=daindx.mm.!host
  173.     if symbol('Daindx.'||mm||.'!SERVER')='VAR' then servername=daindx.mm.!server
  174.  
  175.     drop eek.
  176.  
  177.  
  178. include_list=' '
  179.     status=do_ssi(afile,daindx.mm.!sel,cshfile)
  180. /*call pmprintf_sref(' include list ' include_list)*/
  181.  
  182.     if status=1 then do         /* save as is */
  183.  
  184.         ibig=length(outbig)
  185.         mwow=strip(word(sysdriveinfo(thedrive),2))
  186.         if (mwow-ibig)<diskfree then do
  187.                 call pmprintf_sref(' SREFAST ssi daemon: disk too full ('mwow)
  188.                 signal death
  189.         end  /* Do */
  190.         foo=charout(cshfile,outbig,1)
  191.         a=stream(cshfile,'c','close')
  192.         if foo>0 then do
  193.            call pmprintf_sref('SREFAST ssi daemon: Problem writing asis cache file 'cshfile)
  194.            signal death
  195.        end
  196.  
  197.         daindx.mm.!type='ASIS'
  198.         daindx.mm.!file=cshfile
  199.         aa=sysfiletree(afile,a1,'T')
  200.         daindx.mm.!stamp=upper(space(a1.1))
  201.         if symbol('Daindx.'||mm||.'!TRIGGER')='VAR' then do
  202.               aa=sysfiletree(daindx.mm.!trigger,a1,'T')
  203.               if a1.0>0 then
  204.                  daindx.mm.!trigger=upper(space(a1.1))
  205.               else
  206.                  drop daindx.mm.!trigger
  207.         end
  208.        daindx.mm.!origfile=afile
  209.        if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon:  asis cache of ' daindx.mm.!sel ' to 'cshfile)
  210.     end /* status */
  211.  
  212.     if status=2  then do    /* dynamic includes in cache file */
  213.         daindx.mm.!type='DYNAMIC'
  214.         daindx.mm.!file=cshfile
  215.  
  216.         mwow=strip(word(sysdriveinfo(thedrive),2))
  217.         if (mwow-ioutbig)<diskfree then do
  218.                 call pmprintf_sref(' SREFAST ssi daemon: disk too full ('mwow)
  219.                 signal death
  220.         end  /* Do */
  221.         oo=cvwrite(cshfile,eek)
  222.         aa=sysfiletree(afile,a1,'T')
  223.         daindx.mm.!stamp=upper(space(a1.1))
  224.  
  225.         if symbol('Daindx.'||mm||.'!TRIGGER')='VAR' then do
  226.               aa=sysfiletree(daindx.mm.!trigger,a1,'T')
  227.               if a1.0>0 then
  228.                  daindx.mm.!trigger=upper(space(a1.1))
  229.               else
  230.                  drop daindx.mm.!trigger
  231.         end
  232.  
  233.         daindx.mm.!origfile=afile
  234.         if oo=0 then do
  235.               call pmprintf_sref(' SREFAST ssi daemon: Problem writing dynamic cache file ' cshfile)
  236.                signal death
  237.          end  /* Do */
  238.         if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon: dynamic cache of  ' daindx.mm.!sel ' using ' eek.0 ' parts to 'cshfile)
  239.     end  /* Do */
  240.  
  241.     if status=0 then do        /* status=0 -- don't do it (srefiltr will)*/
  242.           daindx.mm.!type='SKIP'
  243.           if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon: SKIP ' daindx.mm.!sel)
  244.     end
  245.  
  246.   end   /* looking for ssi entries */
  247.  
  248. /* write working version of configuration file*/
  249.     itmp=daindx.0
  250.    aoo=fix_daindx()   /* first remove SKIP entries and otherwise bogus entries  */
  251.  
  252.    daindx.!nossi=0      /* no suppression */
  253.    oo=cvwrite(afilt2,daindx)
  254.    if oo=0 then do
  255.        call pmprintf_sref(' SREFAST ssi daemon: Error writing working index 'afilt2)
  256.        signal death
  257.    end
  258.    if verbose>0 then call pmprintf_sref(" SREFAST ssi daemon: " daindx.0 " of " itmp " entries to working index: " afilt2)
  259.  
  260. end /* do  forever*/
  261.  
  262. death:
  263. call pmprintf_sref(' SREFAST ssi daemon: Error, caching stopped ')
  264. foo=sysfiledelete(afilt2)
  265. exit
  266.  
  267. tdog:
  268. call pmprintf_sref('SREFAST ssi daemon: Error: line 'sigl)
  269. exit
  270.  
  271.  
  272. /***********************************/
  273. fig_afile:procedure
  274. parse arg ddir,asel,dnameonly
  275.  
  276.    ddir=strip(translate(ddir,'\','/'),,'\')||'\'
  277.    addme=strip(translate(asel,'\','/'),,'\')
  278.    if dnameonly=1 then addme=filespec('n',addme)
  279.    afile=ddir||addme  /* the absolute filename of the file to be processed */
  280. return afile
  281.  
  282.  
  283. /***************/
  284. /* remove useless entries from daindx */
  285. fix_daindx:procedure expose daindx. datdir verbose                         
  286.  
  287. il=0
  288. drop doit.
  289. do mm=1 to daindx.0
  290.  
  291.    if symbol('DAINDX.'||mm||'.!SEL')<>'VAR' then iterate
  292.  
  293. /* nodos */
  294.    if symbol('DAINDX.'||mm||'.!NODO')='VAR'  then do
  295.         if daindx.mm.!nodo=1 then do
  296.                 il=il+1
  297.                 oo=cvcopy(daindx.mm,doit.il)
  298.         end
  299.         iterate
  300.     end
  301.  
  302. /* skip? */
  303.    if symbol('DAINDX.'||mm||'.!TYPE')='VAR'  then do
  304.         if  daindx.mm.!type<>'SKIP' then do
  305.                 il=il+1
  306.                 oo=cvcopy(daindx.mm,doit.il)
  307.         end
  308.         else do
  309.            if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon: Skipping, can not do SSIs in 'daindx.mm.!sel ' ( ' mm)
  310.        end
  311.        iterate
  312.    end
  313.  
  314. /* wildcards */
  315.    if pos('*',daindx.mm.!sel)>0 then do         /* wildcards, too hard to check */
  316.           il=il+1
  317.           oo=cvcopy(daindx.mm,doit.il)
  318.           iterate
  319.    end
  320.  
  321. /*  check for existence of file */
  322.    ddir=datdir
  323.    dnameonly=0
  324.    if symbol('DAINDX.'mm'.!DIR')='VAR' then do
  325.         ddir=strip(upper(daindx.mm.!dir))
  326.         if abbrev(ddir,'HTTP://')=1  then do     /* redirect or wildcard-- keep it */
  327.                 il=il+1
  328.                 oo=cvcopy(daindx.mm,doit.il)
  329.                 iterate
  330.         end
  331.         ddir=strip(translate(ddir,'\','/'),,'\')||'\'
  332.         if symbol('DAINDX.'mm'.!DIR_NAMEONLY')='VAR' then do
  333.               dnameonly=daindx.mm.!dir_nameonly
  334.         end
  335.     end
  336.     afile=fig_afile(ddir,daindx.mm.!sel,dnameonly)
  337.  
  338.     oug=sysfiletree(afile,ibug)
  339.     if ibug.0>0 then do             /* exists, keep it */
  340.                 il=il+1
  341.                 oo=cvcopy(daindx.mm,doit.il)
  342.                 iterate
  343.      end
  344.      else do
  345.          if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon: Skipping, no matches for 'daindx.mm.!sel' ('afile)
  346.      end  /* Do */
  347. end /* do */
  348. doit.0=il
  349. aa=cvcopy(doit,daindx)
  350. return 0
  351.  
  352. /***************/
  353.  
  354. /* -----------Sre-filter Server side include routine -------------- */
  355.  
  356.  
  357. do_ssi:procedure expose filter_name key_preface enmadd host_nickname port servername ,
  358.                    transaction ddir goserve_ver outbig verbose eek. dynlist ioutbig basedir include_list
  359. parse arg afile,thesel,cshfile
  360.  
  361.  crlf='0d0a'x
  362. /* default value for cgi includes */
  363. cgi_inc_errmsg="Error in CGI Include "
  364. cgi_inc_sizefmt="ABBREV"
  365. cgi_inc_timefmt="%c"
  366. bigin=0
  367.  
  368. bigin=sref_grab_file(afile,30)
  369. if bigin=0 | bigin="" then  return 0
  370.  
  371. if c2d(right(bigin,1))=26 then
  372.       bigin=left(bigin,length(bigin)-1)
  373.  
  374. tbigin=translate(bigin)
  375.  
  376. inctype.1="INTERPRET"  
  377.  inctype.2="OPTION"     /* killer */
  378.  inctype.3="INCLUDE"
  379.  inctype.4="REPLACE"
  380.  inctype.5="SELECT"    /* killer */
  381.  inctype.6='#'
  382.  inctype.7='CACHE'   /* ignored */
  383.  i5=7
  384.  
  385. if key_preface<>" "  & key_preface<>0 then do
  386.         do mm=1 to 5
  387.            inctype.mm=key_preface||inctype.mm
  388.         end
  389.         inctype.7=key_preface||inctype.7
  390. end
  391.  
  392. apre=1 ;  apost=1
  393.  headers=get_value('HEADERS')
  394.  footers=get_value('FOOTERS')
  395.  homedir=get_value('HOME_DIR')
  396.  if headers=0 then headers=" "
  397.  if footers=0 then footers=" "
  398.  if headers=" " then apre=0
  399.  if footers=" " then apost=0
  400.  
  401. /* note that j.i may or may not be in a keyphrase! */
  402. booger=apre+apost
  403. do mm=1 to i5
  404.     if booger>0 then leave
  405.     booger=booger+pos(inctype.mm,tbigin)
  406. end
  407.  
  408. /* NO include keyphrases  */
  409. if booger = 0 then do
  410.    outbig=bigin
  411.    return 1    /* no ssi's, return as is */
  412. end /* do */
  413.  
  414. /* add pre or post blocks if they have been specified (typically in initfilt) */
  415.  if apre=1 & astatus=0  & ssino.!no_header=0 then do
  416.      foo=upper(space(strip(headers),0))
  417.      idog=1
  418.      if abbrev(foo,'<BODY')=1 then idog=2
  419.      bigin=sref_insert_block(bigin,'BODY',headers,idog,'<','>')
  420.  end
  421.  if apost=1  & astatus=0 & ssino.!no_footer=0 then do
  422.       bigin=bigf_footer(footers)
  423.  end
  424.  
  425. /* now start processing  bigin */
  426.  
  427. /* For flexibility, we process this for each set of "keyphrase delimiters",
  428. where set k=1..K is defined using delim_1.k and delim_2.k (typically set
  429. in initfilt) */
  430.  
  431. adelim1=get_value('DELIM_1.1')
  432. adelim2=get_value('DELIM_2.1')
  433.  
  434. if adelim1="" | adelim2="" | adelim1=0 | adelim2=0 then return 0  /* bad initial delimiter -- don't do includes */
  435.  
  436. outbig=''
  437. yeck=0
  438.  
  439. do forever                      /* done when done */
  440.    parse var bigin t1 (adelim1) in1 (adelim2) bigin
  441.  
  442.    outbig=outbig||t1
  443.  
  444.    if in1="" & bigin="" then leave
  445.  
  446. /* see if in1 is one of the inctypes (inctypes are aka keywords)*/
  447.   in2=translate(in1)
  448.   in2=translate(in2,' ','=:;')    /* space is the generic seperater */
  449.  
  450.   aninc=0
  451.                   /*  is first one of our keywords ? */
  452.   do mm=1 to i5
  453.      if abbrev(word(in2,1),inctype.mm)=1 then  do     /* it's the mm'th inc type */
  454.         aninc=mm
  455.         select
  456.           when mm=2 | mm=5  then return 0   /*  option and select are killers */
  457.           when mm=1 then do
  458.                if verbose>2 then call pmprintf_sref(' SREFAST ssi daemon: INTERPRET entry can not be cached')
  459.                return 0 /* interpret is killer  */
  460.           end
  461.           when  mm=3 | mm=4  then  thearg=word(in2,2)
  462.           when mm=6 then do
  463.               foo=pos('#',translate(in1))
  464.               thearg=substr(in1,foo+1)
  465.            end
  466.            when mm=7 then do
  467.                aninc=0           /* ignore CACHE */
  468.            end
  469.            otherwise nop
  470.         end             /* select */
  471.         leave
  472.      end      /*abbrev */
  473.   end         /* mm loop */
  474.  
  475. /* Not a keyword, or a syntactically bad keyphrase    */
  476.  if aninc=0  then do
  477.        if bigin<>"" then
  478.             t1=adelim1||in1||adelim2  /* leave it be */
  479.        else
  480.             t1=adelim1||in1  /* openended "comment" */
  481.       outbig=outbig||t1
  482.       iterate
  483.   end
  484.  
  485.  
  486. /* ------- DO SOME WORK (if here, we have a (possibly) good keyphrase) */
  487.   select
  488.      when aninc=6  then   putme=do_cgi_include(thearg)    /* NCSA stuff */
  489.       when aninc=3  then   putme=line_include(thearg)    /* INCLUDE */
  490.      when aninc=4  then  putme=line_replace(thearg) /* REPLACE */
  491.      otherwise  putme=""        /* possibly a suppressed keyphrase */
  492.   end
  493. if verbose>2 then call pmprintf_sref(left(aninc' 'thearg ' : putme= ' putme,79))
  494. if yeck=1 & verbose>2 then call pmprintf_sref(aninc' yeck is ' thearg)
  495. if yeck=1 then return 0         /* yeck means got a killer */
  496.  
  497.   if c2d(right(putme,1))=26 then putme=left(putme,length(putme)-1)  /* strip trailing ctl-z */
  498.  
  499.   bigin=putme||bigin   /* this is the recursive part */
  500. end                     /*of bigin parse loop */
  501.  
  502.  
  503. /* we are done. */
  504.  
  505. if c2d(right(outbig,1))=26 then
  506.       outbig=left(outbig,length(outbig)-1)
  507.  
  508. IF YECK=1 then RETURN 0         /* can't do it */
  509.  
  510. ai=pos('<!--=!  ',outbig)
  511. if ai=0 then do
  512.   return 1
  513. end  /* Do */
  514.  
  515. /* dynamic: break into parts */
  516. iat=0
  517. ioutbig=length(outbig)
  518. do until outbig=""
  519.     parse var outbig p1 '<!--=!' anarg '-->' outbig
  520.     iat=iat+1
  521.     eek.iat.!type=1             /*1 means literal */
  522.     eek.iat=p1
  523.     if anarg=""  then iterate
  524.     iat=iat+1
  525.     parse var anarg v1 v2
  526.     v1=upper(strip(v1))
  527.     if wordpos(v1,dynlist)=0 then do
  528.         eek.iat.!type=1                 /* 1 symbols literal (comment, in this case */
  529.         eek.iat='<!--=!'|| anarg|| '-->'
  530.     end  /* Do */
  531.     else do             /* 2 signals dyamic */
  532.         eek.iat.!type=2
  533.         eek.iat=upper(v1)||' '||v2
  534.     end  /* Do */
  535. end /* do */
  536. eek.0=iat
  537.  
  538. return 2
  539.  
  540.  
  541.  
  542. /* -----------------------------------------------------------
  543.  Evaluate a cgi-bin (NSCA HTTPD style server side include
  544.  ----------------------------------------------------------- */
  545. do_cgi_include:
  546. parse arg thearg
  547.  
  548. parse var thearg atype aval
  549.  
  550. /* valid atypes:
  551.   INCLUDE = Include a file
  552.   ECHO = "replace" with a cgi-bin variable
  553.   FSIZE= Size of a file
  554.   FLASTMOD = Last modification date of a file
  555.   EXEC = killer
  556. */
  557. tatype=strip(translate(atype))
  558.  
  559. figit=0
  560. if wordpos(tatype,' INCLUDE FLASTMOD FSIZE ')>0  then do
  561.      parse upper var aval ftype '=' aval
  562.      aval=strip(strip(aval),,'"') ; ftype=strip(ftype) ; figit=0
  563.      if ftype='FILE' then do    /* relative to absolute directory" */
  564.          if usef=1 then do
  565.                 return cgi_inc_errmsg
  566.           end
  567.           adr=filespec('d',afile)
  568.           apt=filespec('p',afile)
  569.           aval=adr||apt||aval
  570.           figit=1
  571.      end
  572. end
  573.  
  574.  
  575. if tatype="INCLUDE" then do
  576.         putme=line_include(aval,figit)
  577.         if putme=0 | putme="" then putme=cgi_inc_errmsg
  578.         return putme
  579.   end
  580.  
  581. if (tatype="FSIZE" | tatype="FLASTMOD") then do
  582.  
  583.     cfile=line_include(aval,'YES',figit,1)
  584.     oy=sysfiletree(cfile,'stuff','FT')
  585.     if stuff.0=0 then   return cgi_inc_errmsg
  586.  
  587.     parse var stuff.1 adate asize .
  588.     parse var adate yy '/' mo '/' dd '/' hh '/' mm '/' ss
  589.     if ss=' ' then ss='00'
  590.     yip=yy||'/'||mo||'/'||dd
  591.     adatef=dateconv(yip,'O','B')
  592.     atimef=hh||':'||mm||':'||ss
  593.  
  594.  
  595.     if tatype="FLASTMOD" then do
  596.        asis_ok=0
  597.        putme=sref_datetime_convert(cgi_inc_timefmt,adatef,atimef)
  598.        return putme
  599.     end
  600.  
  601.     if tatype="FSIZE" then do
  602.         asis_ok=0
  603.         if translate(cgi_inc_sizefmt)="ABBREV" then do
  604.                if asize>=1000000 then return format(asize/1000000,,0)||'M'
  605.                if asize>=1000 then  return format(asize/1000,,0)||'K'
  606.         end
  607. /* convert to xxx,yyy,zzz */
  608.         il=length(asize)
  609.         if il>3 then do
  610.            oop=""
  611.            do mm=il to 3 by -3
  612.                tt=substr(asize,mm-2,3)
  613.                if mm=il then
  614.                   oop=tt
  615.                else
  616.                  oop=tt||','||oop
  617.            end /* do */
  618.            if mm<>0 then oop=substr(asize,1,mm)||','||oop
  619.            asize=oop
  620.         end
  621.         return asize    /* not abbrev, or < 1000 */
  622.     end
  623.  
  624. end
  625.  
  626. if tatype="CONFIG"  then do
  627.  
  628.       asis_ok=0
  629.       parse var aval t1 '=' t2  ;t1=translate(t1)
  630.       t2a=strip(strip(t2),,'"')
  631.  
  632.       putme=""
  633.       select
  634.         when t1="ERRMSG" then DO
  635.                 cgi_inc_errmsg=t2a
  636.                 PUTME=' '
  637.         end
  638.         when t1="TIMEFMT" then    do
  639.               cgi_inc_timefmt=t2a
  640.               PUTME=' <!--=!  cgi_inc_timefmt 't2a' --> '
  641.         end
  642.         when t1="SIZEFMT" then do
  643.               cgi_inc_sizefmt=translate(t2a)
  644.               PUTME='  '
  645.         end
  646.         otherwise nop
  647.       end
  648.       return putme
  649. end
  650.  
  651. if tatype="ECHO" then do
  652.       parse var aval foo '=' findme ; findme=translate(findme)
  653.       findme=strip(strip(findme),,'"')
  654.       select
  655.          when findme="DOCUMENT_NAME" then do
  656.                 putme=afile
  657.          end
  658.          when findme="DOCUMENT_URI" then do
  659.                 putme=thesel
  660.          end
  661.          when findme="DATE_LOCAL" then do
  662.                putme='<!--=!  DATE_LOCAL --> '
  663.          end
  664.          when findme="DATE_GMT" then do
  665.                putme='<!--=!  DATE_GMT --> '
  666.          end
  667.          when findme="LAST_MODIFIED" then do
  668.                  eek=sysfiletree(translate(afile,'\','/'),gosh,'FT')
  669.                  parse var gosh.1 adate asize .
  670.                  parse var adate yy '/' mo '/' dd '/' hh '/' mm '/' ss
  671.                  if ss=' ' then ss='00'
  672.                  adatef=dateconv(yy||'/'||mo||'/'||dd,'O','B')
  673.                  atimef=hh||':'||mm||':'||ss
  674.                  putme=sref_datetime_convert(cgi_inc_timefmt,adatef,atimef)
  675.         end     /* last modified */
  676.         when findme="SERVER_SOFTWARE" then do
  677.                  putme=goserve_ver||' '||filter_name
  678.         end
  679.         when findme="SERVER_NAME" then do
  680.                 putme=servername
  681.         end
  682.         when findme="GATEWAY_INTERFACE" then do
  683.                 putme="CGI/1.1"
  684.         end
  685.         when findme="SERVER_PROTOCOL" then do
  686.                 putme='<!--=!  SERVER_PROTOCOL --> '
  687.         end  /* Do */
  688.         when findme="SERVER_PORT" Then do
  689.                 putme=port
  690.         end
  691.         when findme="REQUEST_METHOD" then do
  692.                putme='<!--=!  REQUEST_METHOD --> '
  693.         end
  694.         when findme="PATH_INFO" then do
  695.                 putme=" Path_info n.a. "
  696.         end
  697.         when findme="PATH_TRANSLATED" Then do
  698.                 putme="Path_translated n.a. "
  699.         end
  700.         when findme="SCRIPT_NAME" then do
  701.               putme=thesel
  702.         end
  703.         when findme="QUERY_STRING" then  do
  704.                 putme=""  /* query string requests don't get here */
  705.         end
  706.         when findme="REMOTE_HOST" then do
  707.                putme='<!--=!  USERNAME --> '
  708.          end
  709.         when findme="REMOTE_ADDR" then do
  710.                putme='<!--=!  REMOTE_ADDR --> '
  711.         end
  712.         when findme="AUTH_TYPE" then
  713.                 putme="Basic Access Authentication Scheme"
  714.         when findme="AUTH_NAME" then
  715.                putme='<!--=!  AUTH_NAME --> '
  716.         when findme="REMOTE_IDENT" then
  717.                 putme="Remote_ident n.a. "
  718.         when findme="CONTENT_TYPE" then
  719.                 putme="Content_type n.a."
  720.         when findme="CONTENT_LENGTH" then
  721.                 putme="Content_length n.a."
  722.         when abbrev(findme,"HTTP_ACCEPT") then
  723.                putme='<!--=! HTTP_ACCEPT -->'
  724.         when abbrev(findme,"HTTP_USER-AGENT") then
  725.                putme='<!--=! BROWSER   --> '
  726.         when abbrev(findme,"HTTP_REFERER") then
  727.                putme='<!--=! REFERER  --> '
  728.  
  729.         otherwise  putme=cgi_inc_errmsg
  730.      end                /* select */
  731.      return putme
  732. end
  733.  
  734.  
  735. if tatype="EXEC"  then do
  736.      YECK=1
  737. END
  738.  
  739. RETURN 0
  740.  
  741.  
  742. /* ----------------------------------------------------------------------- */
  743. /*  Look for INCLUDE  keyphrases
  744. .    If present, pull in lines from INCLUDE file.
  745. .
  746. .   Note: INCLUDE files are subject to further processing
  747. .      -- so BEWARE of recursive TRAPS !
  748. .  Justfile argument used by CGI_INCLUDE routine
  749. */
  750. /* ----------------------------------------------------------------------- */
  751.  
  752.  
  753. line_include:procedure expose yeck ddir enmadd transaction homedir host_nickname ,
  754.                         verbose include_list
  755. parse arg incfil0,absname,justfile
  756. justfile=translate(justfile)
  757.  
  758. IF POS('~',INCFIL0)>0 then DO   /* TOO MUCH TROUBLE -- GIVE UP */
  759.      YECK=1
  760.      RETURN 0
  761. end  /* Do */
  762.  
  763.    if absname=1 then do
  764.       incfile=incfil0
  765.    end
  766.    else do
  767.       incfile=sref_do_virtual(ddir,incfil0,enmadd,1,transaction,homedir,host_nickname)
  768.    end
  769.  
  770.    if justfile=1 then return incfile
  771.  
  772.    putme=sref_grab_file(incfile,120)
  773.    if putme=0 then do
  774.       yeck=1
  775.       return 0
  776.    end
  777.  
  778.    include_list=include_list||' '||upper(incfile)
  779.  
  780.    return putme
  781.  
  782.  
  783.  
  784.  
  785. /* ----------------------------------------------------------------------- */
  786. /* This will do a REPLACE: on a line.  The currently supported values are
  787.         DATE        todays date
  788.        TIMEGMT     current time (GMT)
  789.        TIME    current time
  790.        CREATION    A message on the creation date & time of the file
  791.        CREATION_DATE Just the creation date
  792.        CREATION_TIME  Just the creation time (use with CREATION_DATE and your own message)
  793.        WEBMASTER  The contents of the WEBMASTER parameter
  794.        REFERER   The referer (from the request header)
  795.       BROWSER   The requesters browser (from the request header)
  796.         CLIENTNAME USERNAME   ABC.DDD.GOV type name, or ip address if n.a.
  797.  
  798.        INHOUSE.n    n = an integer.  Used for messages to INHOUSE  users only.
  799.        SUPERUSER.n   n = an integer.  Used for messages to Superusers only
  800.  
  801.        HITS         The nth hit for this file (requires looking at Counter_file
  802.                Also COUNTS, OPTION_HITS.n and OPTION_COUNT.n variants
  803.        WEBMASTER = Webmaster address
  804.        SERVERNAME = Name of server (i.e.; WWW.FOO.ORG)
  805.        SERVER  =    Server software (i.e. GOSERV 2.45)
  806.        FILTER_NAME = The name of this filter (set at the top of this file)
  807.  
  808.        VARIABLE.varname = Extract value of varname from environment
  809.                 If not in environment, see if a built in variable
  810.                 (such as SERVERPORT and PRIVSET).
  811.                 See srefmon.cmd for examples of environment vars.
  812.  
  813.        READ_HEAD    Do a READ HEADER VAR PUTME , append <PRE>
  814.  
  815.       and ...  Check replacestrg_file (static replacements) if no match from above.
  816.  
  817. /* ----------------------------------------------------------------------- */
  818.  
  819. */
  820.  
  821. line_replace:
  822.  
  823. parse arg targ
  824. parse upper var targ reparg "." repargn   /* parse out VAR.j types of labels */
  825.  
  826. issuper=0 ; isin=0;
  827. if wordpos('SUPERUSER',translate(privset))>0  then
  828.     issuper=1
  829. else
  830.    if wordpos('INHOUSE',translate(privset))>0 then
  831.        isin=1
  832.  
  833.    select
  834.  
  835.         when reparg="DATE" | reparg="DATE_LOCAL"  then do
  836.                putme='<!--=!  DATE_LOCAL2 --> '
  837.         end
  838.         when reparg="TIME" | reparg="TIMELOCAL" | reparg="TIME_LOCAL" then do
  839.                putme='<!--=!  TIME --> '
  840.         end
  841.         when reparg="TIMEGMT" | reparg="TIME_GMT" then do
  842.                putme='<!--=!  TIME_GMT --> '
  843.         end
  844.  
  845.        when reparg="DATEGMT" | reparg="DATE_GMT" then do
  846.                putme='<!--=!  DATE_GMT2 --> '
  847.        end
  848.  
  849.        when reparg="USERNAME" | reparg="USER" | reparg="USER_NAME" ,
  850.              | reparg="CLIENTNAME" | reparg='CLIENT'  then do
  851.                putme='<!--=! USERNAME --> '
  852.        end
  853.        when reparg="FILTER_NAME" | reparg="FILTER_NAME" then
  854.                 putme=filter_name
  855.  
  856.       when reparg="HOME_NAME" | reparg="HOMENAME" then do
  857.                putme=get_value('HOME_NAME')
  858.       end
  859.  
  860.        when reparg="CREATION" then do           /* a creation-date-time message */
  861.             if usef=1 then do
  862.                  putme='n.a.'
  863.             end
  864.             else do
  865.                eek=sysfiletree(translate(afile,'\','/'),gosh,'F')
  866.                poop=gosh.1
  867.                parse var poop adate atime  .
  868.                adate=dateconv(adate,'u','n')
  869.                putme =' <em> This document last modified at '||atime||', on '||adate|| '. </em>'
  870.             end
  871.         end
  872.  
  873.        when reparg="CREATION_DATE" then do       /*just the creation date */
  874.            if usef=1 then do
  875.                  putme='n.a.'
  876.             end
  877.             else do
  878.               eek=sysfiletree(translate(afile,'\','/'),gosh,'F')
  879.               poop=gosh.1
  880.               parse var poop adate atime .
  881.               adate=dateconv(adate,'u','n')
  882.               putme =adate
  883.            END
  884.         end
  885.  
  886.        when reparg="CREATION_TIME" then do              /* just the creation time */
  887.            if usef=1 then do
  888.                  putme='n.a.'
  889.             end
  890.             else do
  891.                eek=sysfiletree(translate(afile,'\','/'),gosh,'F')
  892.                poop=gosh.1
  893.                parse var poop adate atime .
  894.                putme =atime
  895.             END
  896.         end
  897.  
  898.       when reparg="READ_HEAD" | reparg='HEADER'then do    /* read/display the request header */
  899.                putme='<!--=! READ_HEAD --> '
  900.       end
  901.  
  902.         when (reparg="INHMESS" | reparg="INHOUSE" )&  isin=1 then do
  903.               yeck=1
  904.         end
  905.  
  906.         when (reparg="SUPMESS" | reparg="SUPERUSER") & issuper=1 then do
  907.               yeck=1
  908.         end
  909.  
  910.         when abbrev(reparg,"REFER")=1 then do
  911.                putme='<!--=! REFERER --> '
  912.         end
  913.         when (reparg="USER-AGENT" | reparg="BROWSER" ) THen do
  914.                putme='<!--=! BROWSER --> '
  915.         end
  916.         when (reparg="URL") then do
  917.                 servername=servername
  918.                 putme=sref_fix_url(thesel,servername,port)
  919.         end
  920.  
  921.         when wordpos(reparg,'HIT_FILE HITS_FILE COUNT_FILE COUNTS_FILE')>0 then do
  922.              yeck=1
  923.         end
  924.  
  925.    when reparg="SERVERNAME" | reparg="SERVER_NAME" then  /* servers ip name */
  926.           putme=servername       /* servername set at top of filter */
  927.  
  928.  when reparg="VARIABLE" then do   /* get a variable defined in this filter program */
  929.      yeck=1
  930.  end
  931.  when reparg="SERVER" then do
  932.           putme=goserve_ver
  933.  end
  934.  when reparg="WEBMASTER" then     do
  935.           putme=get_value('WEBMASTER')
  936.  end
  937.  otherwise do           /* see if in the replacement strings file*/
  938. if verbose>2 then call pmprintf_sref(' checking value of  ' targ)
  939.              putme=get_value(targ)
  940.  end
  941.  
  942. END    /* select */
  943.  
  944. return putme            /* return it */
  945.  
  946.  
  947. /* ---------------------- */
  948. /* insert footers in a large file */
  949. bigf_footer:procedure expose bigin servername host_nickname enmadd
  950. parse  arg footers
  951. iat=length(bigin)
  952. iaa:
  953. foo=lastpos('/BODY',translate(bigin),iat)
  954. if foo=0 then do
  955.   return bigin
  956. end /* do */
  957.  
  958. foo2=lastpos('<',bigin,foo)
  959. if foo2=0 then
  960.    return bigin
  961.  
  962. yow=strip(substr(bigin,foo2+1,foo-(1+foo2)))
  963. if yow=" " then  do
  964.    return insert(footers,bigin,foo2-1)
  965. end
  966.  
  967. iat=foo2                /* look again */
  968. signal iaa
  969.  
  970.  
  971.  
  972. /* ----------- */
  973. /* get environment value, possibly host specific */
  974. /* ------------ */
  975. get_value: procedure expose enmadd host_nickname
  976. parse arg vname
  977.  
  978. vname=strip(vname) ; hname=strip(host_nickname)
  979. if hname<>' ' then do
  980.    aval=value(enmadd||vname||'.'||hname,,'os2environment')
  981.    if aval<>' ' Then do
  982.         return aval
  983.    end
  984. end
  985. aval=value(enmadd||vname,,'os2environment')
  986. return aval
  987.  
  988.  
  989.  
  990. /***************/
  991. /* create a personallized queue & semaphore */
  992. make_semmi:procedure expose myqueue mysem
  993. a=rxqueue('s',myqueue)
  994. noq=1
  995. signal on syntax name badq
  996. queue 0
  997. noq=0
  998. badq:
  999. signal off syntax
  1000. if noq=1 then do
  1001.   foo=rxqueue('c',myqueue)
  1002.   if upper(foo)<>upper(myqueue) then
  1003.        call pmprintf_sref('SREFAST ssi daemon: wierd queue error ' foo' ' myqueue)
  1004.   a=rxqueue('s',myqueue)
  1005. end
  1006. yyq=queued()
  1007. if yyq>100 then do
  1008.   if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon: Killing residual queue ' myqueue' ' yyq)
  1009.   aa2=rxqueue('d',myqueue)
  1010.   aa2A=rxqueue('c',myqueue)
  1011.   aa2B=rxqueue('s',myqueue)
  1012.   yyq=queued()
  1013. end
  1014. do mmy=1 to yyq
  1015.     pull poo .
  1016. end
  1017.  
  1018. wow=eventsem_query(mysem)
  1019. if wow=-187 then do
  1020.    a=eventsem_create(mysem)
  1021.    if a=0  then do
  1022.        nop
  1023.   end
  1024.   else do
  1025.      call pmprintf_sref(' SREFAST ssi daemon: Could not create semaphore: 'mysem' , error='a)
  1026.      dienow=0
  1027.      return 0
  1028.    end
  1029. end
  1030. foo=eventsem_reset(MYSEM)
  1031. dienow=1
  1032. return 1
  1033.  
  1034.  
  1035. /******************/
  1036. /* read text index file, create daindx
  1037.   return 0 if none, otherwise # entries
  1038.   Note that daindx might be expanded, if there are * selectors for
  1039.   SSI files */
  1040. get_index:procedure expose daindx. verbose datdir initfast_stamp initfilt_stamp basedir port initfilt_file
  1041. parse arg ooindx
  1042.  
  1043. aa=sref_fileread(ooindx,todos,,'E')
  1044. if aa=0 then return 0
  1045.  
  1046. ddir=datdir
  1047. drop doit.
  1048. signal on error name badline ; signal on syntax name badline
  1049. do kk=1 to todos.0              /* look for errors */
  1050.    aline=strip(todos.kk)
  1051.    if aline=' ' then iterate
  1052.    if abbrev(aline,';')=1 then iterate
  1053.    interpret aline                      /* read a doit. entry */
  1054. end /* do */
  1055. signal off error ; signal off syntax
  1056.  
  1057. /* now, expand the *.HTM "ssi" */
  1058. i1=0
  1059. do mm=1 to doit.0
  1060.    if symbol('DOIT.'||mm||'.!SEL')<>'VAR' then iterate  /* bad entry */
  1061.    if symbol('DOIT.'||mm||'.!SSI')<>'VAR' then do   /* not an * ssi */
  1062.           i1=i1+1
  1063.           a=cvcopy(doit.mm,daindx.i1)
  1064.           iterate
  1065.    end  /* Do */
  1066.    astrat=pos('*',doit.mm.!sel)
  1067.    if doit.mm.!ssi<>1 |  astrat=0   then do
  1068.           i1=i1+1
  1069.           a=cvcopy(doit.mm,daindx.i1)
  1070.           iterate
  1071.   end
  1072.  
  1073. /* expand 'em */
  1074.     dnameonly=0
  1075.     boog=translate(doit.mm.!sel,'\','/')
  1076.     boog=delstr(boog,astrat)
  1077.     oou=lastpos('\',boog)
  1078.     if oou=0 then
  1079.         selhead='\'
  1080.     else
  1081.         selhead=left(boog,oou)
  1082.     if symbol('DOIT.'mm'.!DIR')='VAR' then do
  1083.         ddir=strip(upper(doit.mm.!dir))
  1084.         if abbrev(ddir,'HTTP://')=1 then iterate     /* can't do this one */
  1085.         if symbol('DOIT.'mm'.!DIR_NAMEONLY')='VAR' then do
  1086.               dnameonly=doit.mm.!dir_nameonly
  1087.         end
  1088.     end
  1089.     ddir=strip(translate(ddir,'\','/'),,'\')
  1090.     ldir=length(ddir)+1
  1091.  
  1092.     afile=fig_afile(ddir,doit.mm.!sel,dnameonly)
  1093.  
  1094.     wow=sysfiletree(afile,fils,'F')
  1095.     if fils.0=0 then iterate            /* no matches */
  1096.     do ir=1 to fils.0
  1097.         i1=i1+1
  1098.         bfile=strip(word(fils.ir,5))
  1099.         if dnameonly=0 then do
  1100.              cfile=substr(bfile,ldir)
  1101.         end
  1102.         else do
  1103.              quse=filespec('n',bfile)
  1104.              cfile=selhead||quse
  1105.         end
  1106.         foo=cvcopy(doit.mm,daindx.i1)
  1107.         daindx.i1.!sel=translate(cfile,'/','\')
  1108.      end
  1109.  
  1110. end /* do */
  1111. daindx.0=i1             
  1112. if verbose>1 then call pmprintf_sref('SREFAST ssi daemon: read 'todos.0 ' lines, wrote ' daindx.0 ' entries ')
  1113.  
  1114.  
  1115. return daindx.mm
  1116.  
  1117. badline:
  1118. signal off error ; signal off syntax
  1119. call pmprintf_sref(' SREFAST ssi daemon: error in  configuration file ('ooindx ') near line 'kk)
  1120.  
  1121. return 0
  1122.  
  1123.  
  1124. /***************************************/
  1125. /* check and change stamps */
  1126. check_stamps:procedure expose initfast_stamp initfilt_stamp virtual_stamp repstrgs_stamp VERBOSE
  1127. parse arg ooindx,initfilt_file,repstrgs_file,virtual_file
  1128.  
  1129. ACHANGE=0
  1130.  
  1131. redo=value('sref_fast_redo',0,'os2environment')
  1132. if redo=1  then do
  1133.   if verbose>1 then call pmprintf_sref(' SREFAST ssi daemon: SREF_FAST_REDO set ')
  1134.   achange=1
  1135. end
  1136.  
  1137.  aa=sysfiletree(ooindx,bb,'FT')
  1138.  astamp=space(upper(bb.1))
  1139.  if astamp<>initfast_stamp then do
  1140.         if verbose>1  then call pmprintf_sref(' SREFAST ssi daemon: Change in INITFAST detected ')
  1141.         INITFAST_STAMP=ASTAMP
  1142.         achange=1
  1143.   end   /* iter > 0 */
  1144.  
  1145.  aa=sysfiletree(INITFILT_FILE,bb,'FT')
  1146.  astamp=space(upper(bb.1))
  1147.  if astamp<>INITFILT_stamp then do
  1148.         if verbose>1  then call pmprintf_sref(' SREFAST ssi daemon: Change in INITFILT detected ')
  1149.         INITFILT_STAMP=ASTAMP
  1150.         achange=1
  1151.   end   /* iter > 0 */
  1152.  
  1153.  aa=sysfiletree(VIRTUAL_FILE,bb,'FT')
  1154.  astamp=space(upper(bb.1))
  1155.  if astamp<>VIRTUAL_stamp then do
  1156.         if verbose>1  then call pmprintf_sref(' SREFAST ssi daemon: Change in VIRTUAL.IN detected ')
  1157.         VIRTUAL_STAMP=ASTAMP
  1158.         achange=1
  1159.   end   /* iter > 0 */
  1160.  
  1161.  aa=sysfiletree(REPSTRGS_FILE,bb,'FT')
  1162.  astamp=space(upper(bb.1))
  1163.  if astamp<>REPSTRGS_stamp then do
  1164.         if verbose>1  then call pmprintf_sref(' SREFAST ssi daemon: Change in REPSTRGS.IN detected ')
  1165.         REPSTRGS_STAMP=ASTAMP
  1166.         achange=1
  1167.   end   /* iter > 0 */
  1168.  
  1169.  
  1170. RETURN ACHANGE
  1171.  
  1172.