home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 308.lha / treewalk / files.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1980-12-05  |  6.0 KB  |  244 lines

  1. /*
  2.  * Make treewalk pretend that it's a files command. Uses rexx treewalk
  3.  * macros basename.ftw, rootname.ftw, copy.ftw, patname.ftw and
  4.  * nonrecursive.ftw. Note that this version only allows one copy destination,
  5.  * and one grep pattern. Files isn't documented as doing that, but behaves
  6.  * that way in practice.
  7.  *
  8.  *    Copyright (C) 1989  Mike Meyer
  9.  *
  10.  *    This program is free software; you can redistribute it and/or modify
  11.  *    it under the terms of the GNU General Public License as published by
  12.  *    the Free Software Foundation; either version 1, or any later version.
  13.  *
  14.  *    This program is distributed in the hope that it will be useful,
  15.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *    GNU General Public License for more details.
  18.  *
  19.  *    You should have received a copy of the GNU General Public License
  20.  *    along with this program; if not, write to the Free Software
  21.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. /*
  25.  * Start by being obnoxious, just like the real thing.
  26.  */
  27. /*
  28. say "Files version 1a: Copyright 1989 by Mike Meyer"
  29. say "Interface design Copyright 1985,1988 by Lattice, Inc."
  30. */
  31.  
  32. /*
  33.  * Initalize variables used for arguments.
  34.  */
  35. basename = ""
  36. copy = ""
  37. daydelta = 0
  38. days = ""
  39. erase = ""
  40. namecomp = "#="        /* Changes to *= if we see -m */
  41. recursive = 1
  42. matchname = ""
  43. match = ""
  44. newer = ""
  45. older = ""
  46. patname = ""
  47. rootname = ""
  48. size = ""
  49. type = ""
  50. verbose = ""
  51.  
  52. /*
  53.  * Now, scan args getting options
  54.  */
  55. parse arg args
  56.  
  57. i = 1
  58. do forever
  59.     if i > words(args) then do
  60.         Say "files [options] dir1 dir2 ..."
  61.         exit 10
  62.         end
  63.  
  64.     arg = word(args, i)
  65.     select
  66.         when arg = "-b" then basename = "&&basename&&false"
  67.         when arg = "-m" then namecomp = "*="
  68.         when arg = "-n" then recursive = 0
  69.         when arg = "-r" then rootname = "&&rootname&&false"
  70.         when arg = "-rerase" then erase = "post single delete"
  71.         when arg = "-v" then verbose = "verbose"
  72.         when arg = "-copy" then do
  73.             i = i + 1
  74.             call setclip "tw.copyname", word(args, i)
  75.             copy = "&&file&©"
  76.             end
  77.         when arg = "-days" then do
  78.             i = i + 1
  79.             daydelta = word(args, i)
  80.             end
  81.         when arg = "-erase" then do
  82.             erase = "single delete"
  83.             recursive = 0
  84.             end
  85.         when arg = "-name" then do
  86.             i = i + 1
  87.             matchname = word(args, i)
  88.             end
  89.         when arg = "-newer" then do
  90.             i = i + 1
  91.             newer = "&&date>"word(args, i)
  92.             end
  93.         when arg = "-older" then do
  94.             i = i + 1
  95.             older = "&&date<"word(args, i)
  96.             end
  97.         when arg = "-pat" then do
  98.             i = i + 1
  99.             call setclip "tw.patname", word(args, i)
  100.             patname = "&&patname"
  101.             end
  102.         when arg = "-size" then do
  103.             i = i + 1
  104.             size = "&&size>"word(args, i)
  105.             end
  106.         when arg = "-type" then do
  107.             i = i + 1
  108.             arg = word(args, i)
  109.             select
  110.                 when arg = "f" then type = "&&file"
  111.                 when arg = "d" then type = "&&dir"
  112.                 otherwise
  113.                     say "Unrecognized -type option"
  114.                     exit 10
  115.                     end
  116.             end
  117.         otherwise
  118.             leave
  119.         end
  120.         i = i + 1
  121.     end
  122.  
  123. /*
  124.  * all options done, so get the directory list.
  125.  */
  126. directorylist = substr(args, wordindex(args, i))
  127.  
  128. /*
  129.  * Now, check the options for interference.
  130.  */
  131. if (erase ~= "" | copy ~= "") & (rootname != "" | basename ~= "") then do
  132.     say "-b and -r flags are inconsistent with -erase, -rerase, and -copy flags"
  133.     exit 10
  134.     end
  135.  
  136. if basename ~= "" & rootname ~= "" then do
  137.     say "Warning: -r flag overrides -b flag"
  138.     basename = ""
  139.     end
  140.  
  141.  
  142. /*
  143.  * If we are doing a copy & not an erase, then we need to fail after the
  144.  * copy. Basename & rootname don't need this, as they just fail.
  145.  */
  146. if copy ~= "" and erase = "" then copy = copy'&&false'
  147.  
  148. /*
  149.  * Check to see if we need to do a name comparison. Decide whether we're
  150.  * going to use filename or name based on contents.
  151.  *
  152.  * This code doesn't do what the documentation does. Then again, neither
  153.  * does the real files. In fact, I can't make it do much at all in when
  154.  * it's got a "/" or ":" in the matchname.
  155.  */
  156. if matchname ~= "" then do
  157.     if verify(":/", matchname) = 0 then
  158.         matchname = "&&filename"namecomp"`"matchname"`"
  159.     else if left(matchcomp, 1) = '#' then
  160.         matchname = "&&name"namecomp"`#?"matchname"`"
  161.     else matchname = "&&name"namecomp"`*"matchname"`"
  162.     end
  163.  
  164. /*
  165.  * If we had days specified, then count backwards over years & then months.
  166.  */
  167. if daydelta > 0 then do
  168.     parse value date('USA') with month '/' dom '/' year
  169.     days = date('Days')
  170.     do while daydelta > 0
  171.         if daydelta < dom then do
  172.             dom = dom - daydelta
  173.             daydelta = 0
  174.             end
  175.         else if daydelta > days then do
  176.             month = 12
  177.             dom = 31
  178.             year = year - 1
  179.             daydelta = daydelta - days
  180.             if year // 4 = 0 then days = 366
  181.             else days = 365
  182.             end
  183.         else do
  184.             daydelta = daydelta - dom
  185.             if month > 7 then
  186.                 if month // 2 = 1 then dom = 31
  187.                 else dom = 30
  188.             else if month = 3 then
  189.                 if year // 4 = 0 then dom = 29
  190.                 else dom = 28
  191.             else if month // 2 = 1 then dom = 30
  192.             else dom = 31
  193.  
  194.             month = month - 1
  195.             if month = 0 then do
  196.                 month = 12
  197.                 year = year - 1
  198.                 dom = 31        /* oh well... */
  199.                 if year // 4 = 0 then days = 366
  200.                 else days = 365
  201.                 end
  202.             end
  203.         end
  204.  
  205.     days = "&&day>`"dom"/"month"/"year"`"
  206.     end
  207.  
  208. /*
  209.  * Check recursive, cause we don't do anything else..
  210.  */
  211. if ~recursive then do
  212.     say "We don't do that yet"
  213.     exit 5
  214.     end
  215.  
  216. /*
  217.  * ok, all done. Now build the filter, and run the sucker.
  218.  */
  219.  
  220. filter = days||matchname||newer||older||patname||size||type||basename||rootname||copy
  221. if filter ~= "" then filter = 'filter "true'filter'"' verbose erase
  222. else filter = verbose erase
  223.  
  224. do i = 1 to words(directorylist)
  225.     dir = word(directorylist, i)
  226.     /*
  227.      * Various options depend on knowing the "full path" to the directory
  228.      * that roots the tree we're going to walk. To make that work, we put
  229.      * the directory name in a clip, and clear a clip for paths. The first
  230.      * time something needs the path, the fullpath clip will be found
  231.      * empty, so it will be computed and filled. Ugly, but needed.
  232.      */
  233.     call setclip "tw.dirname", dir
  234.     call setclip "tw.fullpath"
  235.     "treewalk dir" dir filter
  236.     end
  237.  
  238. /* Clean up the clips */
  239. call setclip "tw.dirname"
  240. call setclip "tw.fullpath"
  241. call setclip "tw.copyname"
  242. call setclip "tw.patname"
  243. exit 0
  244.