home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lcut.zip / lcut.cmd next >
OS/2 REXX Batch file  |  1996-04-22  |  10KB  |  279 lines

  1. /* Yes FOLKS!  This is ReXX ! */
  2.  
  3. /* This script cuts down T-MAIL log to last XX days         */
  4. /*                                                          */
  5. /* Author:                                                  */
  6. /* Radim Kolar             Fidonet: 2:423/66.111            */
  7. /*                         Dark side of Avalon              */
  8. /*                                                          */
  9. /* Errorlevels:                                             */
  10. /*       0 no work required                                 */
  11. /*       1 log file succesfully processed                   */
  12. /*       2 error when processing logfile, no work done      */
  13. /*       3 removing temp file failed, but work is done      */
  14. /*       9 log-file doesn't exits                           */
  15. /*      10 bad arguments count                     */
  16. /*                                                          */
  17. /* Version 0.14                                             */
  18. /*                                                          */
  19. /* Rev. history:                                            */
  20. /*                                                          */
  21. /* 22/04.96        0.14 added new working mode=generic      */
  22. /*                 works like other mode but, instead age of*/
  23. /*                 days, all numbers are size in KB         */
  24. /*                 cut any text logfile to spec. size(in KB)*/
  25. /* 27/10/95        0.13 GA version                          */
  26. /*                 Fixed bug, when logfile is not in the    */
  27. /*                 same directory as lcut.cmd.              */
  28. /*                 better error handling in rename phase    */
  29. /* 09/10/95        0.12 GA version. Fixed some typos        */
  30. /*                                  improved help           */
  31. /* 09/10/95        0.11 added Binkley - Term working mode   */
  32. /*                      added log minsize in KB             */
  33. /* 05/10/95        0.10 GA version, added HELP, some bugs   */
  34. /*                 fixed                                    */
  35. /* 05/10/95        0.03.001 - first working version         */
  36. /* 05/10/95        development started                      */
  37.           
  38. Version='Version 0.14.000'
  39. Progname='T-MAIL log file cutter'
  40. Copyright='Copyright (C) Radim Kolar 1995. (Fidonet 2:423/66.111)'
  41. Copyright2='This program is free to use without any modifications only.'
  42.  
  43. START:
  44. select
  45.   when 0=RXFuncQuery('SysDropFuncs') then udrop=0
  46.   when 0=RxFuncAdd("SysLoadFuncs", "RexxUtil", "SysLoadFuncs") then udrop=1
  47.   otherwise
  48.   /* something may be wrong, reload all Package */
  49.   say 'Reloading REXXUtil Package.'
  50.   RXFuncDrop("SysLoadFuncs")
  51.   RxFuncAdd("SysLoadFuncs", "RexxUtil", "SysLoadFuncs")
  52.   call SysLoadFuncs;
  53.   udrop=0
  54. end
  55.  
  56. if udrop=1 then call SysLoadFuncs
  57.  
  58. say Progname
  59. say Version
  60. say Copyright
  61. say Copyright2
  62. say
  63.  
  64. /* parse */
  65. wmode=1; /* 1 Tmail style log, 2 Binkley Term LOG, 3 Generic. */
  66.  
  67. parse arg logname days archname mdays msize
  68. if logname='' then call helptext;
  69. if days='' then days=90
  70. if left(logname,1)='+' then do
  71.                               wmode=2
  72.                               logname=right(logname,length(logname)-1)
  73.                             end  
  74.  
  75. if left(logname,1)='@' then do
  76.                               wmode=3
  77.                               logname=right(logname,length(logname)-1)
  78.                             end  
  79.                                                         
  80. if ''=stream(logname,'c','QUERY EXISTS') then do
  81.   say logname 'doesn''t exists!'
  82.   call cleanup 9
  83.   end
  84. if archname='' then archname='NUL'
  85.                else if POS('?',archname)>0 then 
  86.                         archname=SysTempFileName(archname);
  87. if mdays='' then mdays=0;               
  88. if msize='' then msize=0;
  89.   
  90. /* begin */
  91. tmpname=SysTempFileName('LC??.???');
  92.  
  93. lastday=''
  94. lastpos=0
  95. lastpos2=0
  96. daycount=0
  97.  
  98. say 'Options:'
  99. say 'Working mode (1-TMAIL, 2-BT, 3-Generic):' wmode
  100. say 'Work only if' logname 'is older than' mdays 'days.'
  101. say 'Work only if' logname 'is greater than' msize 'KB.'
  102. say 'Actions:'
  103. say 'Cut' logname 'to' days 'days.'
  104. say 'Archive cutted lines to' archname
  105. say
  106. say 'Scanning log file: 'logname
  107. say
  108. xx=stream(logname,'C','QUERY SIZE')/1000
  109.  
  110. if (xx<msize)|( (wmode=3)& ( (xx<mdays)|(xx<days))) then
  111.   do
  112.     say 'Log file is too small.'
  113.     call cleanup 0
  114.   end  
  115. if wmode\=3 then  
  116. do while \(tmlparse(logname)=2)
  117.  nop
  118. end
  119.             else
  120.               daycount=format(xx,,0);
  121.  
  122. say daycount 'days found in logfile.'
  123.  
  124. if (daycount<=days) | (mdays>=daycount) then do
  125.    say 'No work required'
  126.    call cleanup 0
  127.    end
  128.  
  129. stopon=daycount-days+1
  130. lastday=''
  131. lastpos=0
  132. lastpos2=0
  133. daycount=0
  134. call stream logname,'C','CLOSE'
  135. say 'Searching cut position ...'
  136. if wmode\=3 then
  137. do while \(tmlparse(logname)=2)
  138.  if stopon=daycount then leave;
  139. end
  140. else
  141.  do
  142.    call stream logname,'C','OPEN READ' 
  143.    say 'stopon' stopon
  144.    call stream logname,'C','SEEK 'stopon*1000
  145.    call linein logname
  146.    lastpos2=stream(logname,'C','SEEK');
  147.    say 'LP2' lastpos2
  148.  end
  149. call stream logname,'C','CLOSE'
  150. say 'Cutting ...'
  151. /* rozdelujeme soubor */
  152. daycount=splitfile(logname,lastpos2,archname,tmpname);
  153. call stream logname,'C','CLOSE'
  154. /* osetreni chyb pri rozdelavani */
  155. if daycount=1 then 
  156.   do
  157.     SysFileDelete(archname);
  158.     SysFileDelete(tmpname);
  159.     call cleanup 2
  160.   end
  161. say 'Renaming temporary file' tmpname 'to' logname'...'
  162. /* a sachy s cleanupem zacinaji */
  163. "@copy "tmpname logname ">NUL"   
  164. /* say 'DEBUG: copy returns rc='rc */
  165. if rc\=0 then 
  166.   do
  167.     SysFileDelete(archname);
  168.     SysFileDelete(tmpname);
  169.     call cleanup 2
  170.   end
  171. daycount=SysFileDelete(tmpname);
  172. if daycount>0 then
  173.   do
  174.     say 'WARNING: Temporary file' tmpname 'is NOT removed, remove it manualy.'
  175.     call cleanup 3
  176.   end
  177.  
  178. say 'Work Succesfully ended.'    
  179. call cleanup 1
  180. /* Procedures BEGINS */
  181.  
  182. cleanup: procedure expose udrop
  183.    arg n
  184.    if udrop=1 then call SysDropFuncs
  185.    exit n
  186. return
  187.  
  188. helptext: procedure
  189.     say 'lcut.cmd <logname> [days2keep] [archive to] [minage] [minsize]'
  190.     say
  191.     say '<logname> name of T-MAIL''s style log-file. *REQUIRED*'
  192.     say '   when logname begins with +, than lcut.cmd is switched to Binkley Term'
  193.     say '   log-file mode.'
  194.     say '   when logname begins with @, than lcut.cmd is switched to generic log mode.'
  195.     say '   in generic logfile mode days2keep parameter is size2keep (in KB)'
  196.     say '[days2keep] max days to keep in logfile - default is 90'
  197.     say '   note: days2keep means number of different days in your logfile, not time'
  198.     say '         difference between now and log-file entry.'
  199.     say '[archive to] filename where will be cutted entries stored, default is NUL'
  200.     say '   note: You may use ? in filename to get unique name, but never use'
  201.     say '         more then five ? in filename!'
  202.     say '[minage] Minimum age of logfile, required for any lcut''s action. default 0 days'
  203.     say '[minsize] Minimum log-file size in KB required for any lcut''s action. default 0'
  204.     say '   note: 1K = 1000 bytes'
  205.     say
  206.     say 'Examples:'
  207.     say '========='
  208.     say
  209.     say 'lcut t-mail.log 40 '
  210.     say '      Cut Tmails log to 40 days'
  211.     say' lcut t-mail.log 40 NUL 0 100'
  212.     say  '     If Tmail.log is greater than 100K, then cut it to last 40 days.'
  213.     say 'lcut t-mail.log 40 TM????.LOG'
  214.     say '      The same, but store cutted entries in TMXXXX.LOG'
  215.     say 'lcut t-mail.log 7 TM????.LOG 100'
  216.     say '      If log is older than 100 days, then will be cutted into 7 days and cutted'
  217.     say '      entries will be stored in TM????.LOG file.'
  218.     say 'lcut +squish.log'
  219.     say '      Cut Binkley-Style squish.log to 90 days'
  220.     
  221.     say
  222.     say 'Please send bugs, typos, suggestions to Radim Kolar 2:423/66.111@Fidonet.Org'
  223.     
  224.     call cleanup 10
  225. return
  226.  
  227. /* vraci 2 konec souboru, 1 novy den, 0 OK */
  228. tmlparse: procedure expose lastday lastpos daycount lastpos2 wmode
  229.     arg logname
  230.     ln=linein(logname);
  231.     np=stream(logname,'C','SEEK');
  232.     if np=lastpos then return 2
  233.     lastpos2=lastpos;
  234.     lastpos=np;
  235.     if length(ln)=0 then return 0 /* 0 */
  236.     if wmode=1 then do
  237.       ld=left(ln,5);
  238.       if \(substr(ln,3,1)='/') then return 0 /* nejaka zla radka */
  239.       end
  240.       else
  241.       do
  242.       ld=left(ln,9)
  243.       if \(substr(ln,2,1)=' ') then return 0 /* nejaka zla radka */
  244.       if \(substr(ln,5,1)=' ') then return 0 /* nejaka zla radka */
  245.       if \(substr(ln,9,1)=' ') then return 0 /* nejaka zla radka */
  246.       /* konec simple BT- testu */
  247.       ld=right(ld,7);
  248.       end
  249.     if ld=lastday then return 0
  250.     /* novy den detected */
  251.     lastday=ld;
  252.     daycount=daycount+1;
  253. return 1
  254.  
  255. /* rozreza logname na dva soubory A a B, kde A je soubor pred bodem zlomu */
  256. /* t.j. ten co se ma vyhodit a B je po bodu zlomu t.j. ten co by se mel nechat */
  257. /* vraci 0 OK ci 1 pri chybe */
  258. splitfile: procedure
  259.     arg logname,splitpos, a, b 
  260.     /* test souboru */
  261.     wfile=a; /* nejdrive do acka ! */
  262.     call SysFileDelete(a);
  263.     if\('READY:'=stream(a,'C','OPEN WRITE')) then return 1;
  264.     call SysFileDelete(b);
  265.     if\('READY:'=stream(b,'C','OPEN WRITE')) then return 1;
  266.     lastpos=0
  267.     do forever
  268.       ln=linein(logname);
  269.       np=stream(logname,'C','SEEK');
  270.       if np=lastpos then leave /* kaneec! */
  271.       lastpos=np;
  272.       junk=lineout(wfile,ln)
  273.       if junk=1 then return 1;
  274.       if np=splitpos then wfile=b;
  275.     end
  276.     call stream a,'C','CLOSE'  
  277.     call stream b,'C','CLOSE'
  278. return 0
  279.