home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / cmdpk164.zip / ln.cmd < prev    next >
OS/2 REXX Batch file  |  1997-12-12  |  5KB  |  195 lines

  1. /* ln.cmd - create shadows                                            */
  2. /* (w) (c) 1996, 1997 Martin Lafaix, Ulrich Möller                    */
  3.  
  4. debug=0
  5.  
  6. unknownoptionMsg = 'Unknown option (-%a). Type "ln -h" for help.'
  7. interruptMsg = "ln was interrupted externally."
  8. noFilesMsg = "Error in ln: No files found to link."
  9. norootMsg = "Error in ln: Root directory cannot be linked."
  10. failedMsg = "Error in ln: Shadow creation failed (%a)."
  11. invDestMsg = "Error in ln: Invalid destination."
  12.  
  13. signal on halt; trace off
  14.  
  15. if RxFuncQuery("SysLoadFuncs") then do
  16.     call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  17.     call SysLoadFuncs
  18. end
  19.  
  20. parse arg args
  21.  
  22. linkfrom = ""
  23. linkto = ""
  24. if args = "" then do
  25.     'call xhelp ln'
  26.     exit
  27. end
  28.  
  29. verbose = 0
  30. force = 0
  31.  
  32. do while (args \= "")
  33.     parse value args with opt1 args
  34.     if debug then Say "Parsing" opt1
  35.     if (substr(opt1, 1, 1)="-") | (substr(opt1, 1, 1)="/") then do
  36.         do optcount = 2 to length(opt1) by 1
  37.             optchar = substr(opt1, optcount, 1)
  38.             if debug then
  39.                 Say "Subparsing" optchar
  40.             select
  41.                 when (optchar="D") then do
  42.                     Say "Debug messages turned on."
  43.                     debug = 1
  44.                     verbose = 1
  45.                 end
  46.                 when (optchar="f") then
  47.                     force = 1
  48.                 when (optchar="v") then
  49.                     verbose = 1
  50.                 when (optchar="h") | (optchar="?") then do
  51.                     'call xhelp ln'
  52.                     exit
  53.                 end
  54.             otherwise
  55.                 say strReplace(unknownoptionMsg, "%a", optchar)
  56.             end /* select */
  57.         end /* do */
  58.     end /* if */
  59.     else
  60.         if linkfrom = "" then
  61.             linkfrom = opt1
  62.         else linkto = opt1
  63. end /* do while */
  64.  
  65. curdir = directory()
  66.  
  67. if debug then
  68.     say '  Given linkto:    "'linkto'"'
  69.  
  70. if (linkto = "") then
  71.     linkto = directory()
  72. else
  73.     linkto = directory(linkto)
  74.  
  75. if debug then
  76.     Say '  Enhanced linkto: "'linkto'"'
  77.  
  78. if (linkto = "") then do
  79.     say invDestMsg
  80.     exit
  81. end
  82.  
  83. if debug then
  84.     say '  linkfrom1: "'linkfrom'"'
  85.  
  86. linkfrom = expandPath(linkfrom)
  87. frompath = filespec('drive', linkfrom)||filespec('path', linkfrom)
  88.  
  89. if debug then do
  90.     Say '  linkfrom2: "'linkfrom'"'
  91.     Say '  frompath: "'frompath'"'
  92. end
  93.  
  94. call directory curdir
  95.  
  96. if (substr(linkfrom, 2)=":\") then
  97.     say noRootMsg
  98. else do
  99.     rc = SysFileTree(linkfrom, linkfrom.)
  100.     if (linkfrom.0 = 0) then
  101.         Say noFilesMsg
  102.     else do i=1 to linkfrom.0
  103.         if debug then say i "-" linkfrom.i
  104.         parse var linkfrom.i fdate ftime fsize fattr fname
  105.         fromname = filespec('name', fname)
  106.         if debug then say i '- frompath: "'frompath'"' 'fromname: "'fromname'"'
  107.         if \CreateShadow(frompath||fromname, linkto) then
  108.             say strReplace(failedMsg, '%a', fromname)
  109.     end
  110. end
  111. exit
  112.  
  113. CreateShadow: /* syntax: rc=CreateShadow(fromfile, todir) */
  114.     fname = filespec('name', arg(1))
  115.     setup = 'SHADOWID='||arg(1)
  116.     dest = strip(arg(2), 't', '\')
  117.     if verbose then
  118.         say "Creating shadow: "arg(1)
  119.     if debug then do
  120.         if force then call charout , "FORCING "
  121.         say "  SysCreateObject('WPShadow', "fname", "dest", "setup")"
  122.     end
  123.  
  124.     if force then
  125.         rc = SysCreateObject('WPShadow', fname, dest, setup, 'F')
  126.     else
  127.         rc = SysCreateObject('WPShadow', fname, dest, setup)
  128. return rc
  129.  
  130. halt:
  131.     Say ""
  132.     Say interruptMsg
  133.     exit
  134.  
  135. strReplace:
  136.     /* syntax: result = strReplace(str, old, new) */
  137.     /* will replace a by b in oldstr */
  138.     parse arg str, old, new
  139.     p = pos(old, str)
  140.     if (p > 0) then
  141.         return left(str, p-1)||new||substr(str,p+length(old))
  142.     else
  143.         return str
  144.  
  145. expandPath:
  146.     /* syntax: fullpath = expandPath(path) */
  147.     /* Expl.: expandPath('bin\a.exe') ==> G:\Tools\bin\ if "G:\Tools" is current dir */
  148.  
  149.     file = arg(1)
  150.     curdir2 = directory()
  151.  
  152.     if debug then say "  expandPath {"
  153.     fdrive = filespec('drive', file)
  154.     fpath = filespec('path', file)
  155.     fname = filespec('name', file)
  156.     if debug then say '    given:    "'fdrive'" "'fpath'" "'fname'"'
  157.  
  158.     /* first find out drive */
  159.     if (fdrive = "") then
  160.         fdrive = filespec('drive', curdir2)
  161.  
  162.     /* now expand the actual path */
  163.     if (fpath = "\") then
  164.         if (fname = "") then do
  165.             fpath = ""
  166.             fname = "\"
  167.         end
  168.         else
  169.             fpath = "\"
  170.     else do
  171.         select
  172.             when (fpath = "") | (fpath = ".\") then
  173.                 fpath = substr(directory(fdrive), 3)
  174.             otherwise do
  175.                 /* add drive, strip trailing "\" */
  176.                 fpath = fdrive||left(fpath, length(fpath)-1)
  177.                 if debug then say '    fpath1: "'fpath'"'
  178.                 /* now check directory and remove drive again */
  179.                 fpath = substr(directory(fpath), 3)
  180.             end
  181.         end /* select */
  182.         if (right(fpath, 1) \= "\") then
  183.             fpath = fpath||"\"
  184.     end
  185.  
  186.     fullPath = fdrive||fpath||fname
  187.     if debug then do
  188.         say '    returned: "'fdrive'" "'fpath'" "'fname'"'
  189.         say '    --->      "'fullPath'"'
  190.     end
  191.     if debug then say "  } expandPath"
  192.     call directory curdir2
  193. return fullPath
  194.  
  195.