home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netdor2.zip / DISK_10 / IMAGE9.ZIP / MODPATH.CMD < prev    next >
OS/2 REXX Batch file  |  1993-08-20  |  12KB  |  283 lines

  1. /*****************************************************************************
  2.  * ModPath - Modify Path Information in Environment Statements               *
  3.  * Author:   Tom Heald (Heald at Almaden)                                    *
  4.  *****************************************************************************
  5.  *                    Licensed Materials-Property of IBM                     *
  6.  *               5604-472 (c) Copyright IBM Corporation, 1993                *
  7.  *                           All rights reserved.                            *
  8.  *                  US Government Users Restricted Rights -                  *
  9.  *                 Use, duplication or disclosure restricted                 *
  10.  *                by GSA ADP Schedule Contract with IBM Corp.                *
  11.  *****************************************************************************
  12.  * Change History:                                                           *
  13.  * 04/19/90 - ATH - Original version written.                                *
  14.  * 02/05/91 - ATH - Placed on the ALTools Disk.                              *
  15.  * 05/07/91 - ATH - Add the "BOTH" option to set both Path and DPath.        *
  16.  * 06/12/91 - ATH - Fix trailing ";" & defaulting "First" with an option.    *
  17.  * 01/23/92 - TEB - Bug fix for null path entries (C:\XXX;;C:\YYY)           *
  18.  * 02/17/92 - ATH - Add '@ECHO OFF' in Quiet mode.                           *
  19.  * 03/19/92 - ATH - Change Quiet to Silent make Dupcheck the default.        *
  20.  * 04/24/92 - TEB - Add (undocumented) /Force option to force changes        *
  21.  *                  even if the path is not currently defined.               *
  22.  * 04/30/92 - TJM - Checked for C: and corrected some misspellings.          *
  23.  * 05/27/92 - ATH - Add /Force and default to /Silent if called from Rexx    *
  24.  * 08/18/92 - TEB - Find_Path would not lowercase path entries.              *
  25.  *  2/05/93 - ATH - Add New "Help" information.                              *
  26.  * 06/04/93 - teb - Treat HELP as "?" if book not found.                     *
  27.  *****************************************************************************/
  28. Trace 'e'
  29. '@ECHO OFF'                                           /*  ATH 02/17/92 */
  30. /* Default Option Settings can be changed for individual preferences.  */
  31. Quiet = 0             /* Show all messages                             */
  32. DupCk = 1             /* Check for duplicate paths.       ATH  3/19/92 */
  33. Force = 0             /* Make mods even if path not def.  TEB  4/24/92 */
  34.  
  35. Parse Upper Source Env Type Command                    /* ATH  5/27/92 */
  36. If Type = "COMMAND"                                    /* ATH  5/27/92 */
  37.    Then Echo  = 1     /* Show new path                    ATH  5/27/92 */
  38.    Else Echo  = 0     /* Don't Echo the Path              ATH  5/27/92 */
  39.  
  40. parse upper arg Operands "/" Option etc               /*  ATH 02/17/92 */
  41.   Do while Option <> ""
  42.     Select
  43.       When Abbrev("QUIET",   Option,1) Then Quiet = 1; /* ATH  3/19/92 */
  44.       When Abbrev("NOQUIET", Option,3) Then Quiet = 0; /* ATH  3/19/92 */
  45.       When Abbrev("SILENT",  Option,1) Then Quiet = 1; /* ATH  3/19/92 */
  46.       When Abbrev("NOSILENT",Option,3) Then Quiet = 0; /* ATH  3/19/92 */
  47.       When Abbrev("ECHO",    Option,1) Then Echo  = 1; /* ATH  3/19/92 */
  48.       When Abbrev("NOECHO",  Option,3) Then Echo  = 0; /* ATH  3/19/92 */
  49.       When Abbrev("DUPCHECK",Option,1) Then DupCk = 1; /* ATH  3/19/92 */
  50.       When Abbrev("NODUPCHECK",Option,3) Then DupCk=0; /* ATH  3/19/92 */
  51.       When abbrev('FORCE',   Option,1) then Force = 1  /* TEB  4/24/92 */
  52.       When abbrev('NOFORCE', Option,3) then Force = 0  /* ATH  5/27/92 */
  53.       Otherwise Do;
  54.         If \Quiet then Say 'Invalid Option:' Option
  55.         Exit 1;
  56.         End
  57.       End
  58.     Parse var etc junk "/" Option etc
  59.     End
  60.  
  61. parse upper var Operands Set_Target Word_1 Path_1 Word_2 Path_2 etc
  62. if Word_1 = "" then do
  63.   Select
  64.     When Set_Target = ""             Then signal Help;
  65.     When Set_Target = "?"            Then signal Help;
  66.     When Abbrev("HELP",Set_Target,1)   /* Show the Book    ATH  2/05/93 */
  67.       Then Call View_Help "DirTools.Inf ModPath BookShelf Path DPath"
  68.     Otherwise Do;
  69.       If Quiet then Exit 1;
  70.       Say 'Missing keywords.'
  71.       Exit 1;
  72.       End
  73.     End
  74.   End
  75.  
  76. /* OS/2 requires Double Quotes around strings containing ";=," etc.     */
  77. /* Remove any "" from the path strings.                                 */
  78.    If left(Path_1,1) = '"' & right(Path_1,1) = '"'
  79.       then Path_1 = substr(Path_1,2,length(Path_1)-2)
  80.    If left(Path_2,1) = '"' & right(Path_2,1) = '"'
  81.       then Path_2 = substr(Path_2,2,length(Path_2)-2)
  82. /* Remove any trailing ";" from the path strings.          ATH 06/12/91 */
  83.    If right(Path_1,1) = ';' then Path_1 = Left(Path_1,length(Path_1)-1)
  84.    If right(Path_2,1) = ';' then Path_2 = Left(Path_2,length(Path_2)-1)
  85.  
  86.  
  87. /* If "BOTH" then do "Path" and "DPath" */
  88. If Set_Target = "BOTH"
  89.    Then Do
  90.      Set_Target = "PATH";  Call Do_Path
  91.      Set_Target = "DPATH"; Call Do_Path
  92.    end;
  93.    Else Call Do_Path
  94.  
  95. Exit 0;
  96.  
  97. Do_Path:
  98. Path_Data = Value(Set_Target,,'OS2ENVIRONMENT')
  99. If Path_Data = ''
  100.   then if Force       /* teb */
  101.     then Path_I = 0
  102.     else do
  103.       If \Quiet then Say 'No path information set for the Environment Statement "'Set_Target'".'
  104.       Exit 2
  105.     end
  106.   else do Path_I = 1 until Path_Data = ''     /* teb */
  107.       parse var Path_Data Path.Path_I ";" Path_Data
  108.       if Path.Path_I = '' & Path_Data <> ''   /* teb - Remove null entry */
  109.         then Path_I = Path_I - 1              /* teb */
  110.     end
  111.  
  112.   Select
  113.     When Abbrev("ADD",Word_1,1) Then Do
  114.          If Word_2 = "" Then Word_2 = "FIRST"    /* Default to first */
  115.          If DupCk then Call Del_Path Path_1   /* Delete the New Path */
  116.          Select
  117.            When Abbrev("FIRST", Word_2,1) Then Do
  118.                   Call Move_Path 1 Path_I
  119.                   Path.1 = Path_1
  120.                 End
  121.            When Abbrev("LAST",  Word_2,1) Then Do
  122.                   Path_I = Path_I + 1
  123.                   Path.Path_I = Path_1
  124.                 End
  125.            When Abbrev("BEFORE",Word_2,1) Then Do
  126.                   I1 = Find_Path(Path_2)
  127.                   Call Move_Path I1 Path_I;
  128.                   Path.I1 = Path_1
  129.                 End
  130.            When Abbrev("AFTER", Word_2,1) Then Do
  131.                   I1 = Find_Path(Path_2)
  132.                   I1 = I1 + 1
  133.                   Call Move_Path I1 Path_I;
  134.                   Path.I1 = Path_1
  135.                 End
  136.            Otherwise Do;
  137.              If \Quiet then do
  138.                If Word_2 <> ""
  139.                  then Say '"'Word_2'" is not a valid keyword.'
  140.                  else Say "Missing keyword."
  141.                Say "  Use First, Last, Before or After."
  142.                End
  143.              Exit 1
  144.            End
  145.          End
  146.          End
  147.     When Abbrev("DELETE",Word_1,1) Then Do
  148.            I1 = Find_Path(Path_1)
  149.            Path_I = Path_I - 1
  150.            Do I = I1 to Path_I
  151.               I2 = I + 1
  152.               Path.I = Path.I2
  153.            End
  154.          End
  155.     When Abbrev("REPLACE",Word_1,1) Then Do
  156.            If \Abbrev("WITH",Word_2,1) Then Do
  157.               If \Quiet then do
  158.                 If Word_2 <> ""
  159.                   then Say '"'Word_2'" is not a valid keyword.  Use "With".'
  160.                   else Say 'Missing the "With" keyword.'
  161.               End
  162.               Exit 1
  163.             End
  164.            If DupCk then Call Del_Path Path_2  /* Delete the New Path */
  165.            I1 = Find_Path(Path_1)
  166.            Path.I1 = Path_2
  167.          End
  168.     When Abbrev("TEST",Word_1,1) Then Do
  169.            I1 = Find_Path(Path_1)
  170.            Exit 0;  /* If control returns from Find_Path the dir. was found */
  171.          End
  172.     Otherwise Do;
  173.       If \Quiet then Say '"'Word_1'" is not a valid function.  Use Add, Delete, Replace or Test.'
  174.       Exit 1
  175.       End
  176.     End
  177.  
  178.   New_Path = ""
  179.   Do I = 1 to Path_I
  180.     New_Path = New_Path || Path.I";"
  181.   End
  182.  
  183.   /* Set the Path in the Rexx environment and the OS/2 parent environment. */
  184.   /*"SET" Set_Target"="New_Path   This is no longer needed ATH  3/19/92 */
  185.   Path_Data = Value(Set_Target,New_Path,'OS2ENVIRONMENT')   /* The parent. */
  186.   If \Quiet & Echo Then Say Set_Target" = "New_Path
  187.  
  188. Return
  189.  
  190. Move_Path: Procedure Expose Path. Path_I;
  191.   Parse arg I1 I2 etc
  192.   Do I3 = I2 to I1 by -1
  193.     I4 = I3 + 1
  194.     Path.I4 = Path.I3  /* Move everything down 1 */
  195.   End
  196.   Path_I = Path_I + 1
  197. Return;
  198.  
  199. Find_Path: Procedure Expose Path. Path_I Quiet;
  200.   Parse arg Path_1 etc
  201.   Found = 0
  202.   Do I = 1 to Path_I
  203.     If translate(Path.I) = Path_1 then Do    /* teb */
  204.        Found = 1
  205.        Leave
  206.        End
  207.     End
  208.   If \Found then Do
  209.        If \Quiet then Say 'Target path "'Path_1'" not found.'
  210.        Exit 3
  211.      End
  212. Return I;
  213.  
  214. Del_Path: Procedure Expose Path. Path_I Quiet;
  215.   Parse arg Path_1 ";" etc
  216.   Do while Path_1 <> ""
  217.      Do I1 = 1 to Path_I
  218.        If Path.I1 = Path_1 & I1 <= Path_I
  219.           then do
  220.              Path_I = Path_I - 1
  221.              Do I2 = I1 to Path_I
  222.                 I3 = I2 + 1
  223.                 Path.I2 = Path.I3  /* Move everything up 1 */
  224.              End
  225.           End
  226.      End
  227.      Parse var etc Path_1 ";" etc
  228.    End
  229. Return;
  230.  
  231. /*----------------------------------------------------------------------*/
  232. View_Help:       /* View the Help for this command    ATH  2/05/93 */
  233.   Parse Arg Help_Book Subject Paths
  234.   /* Load the RxUtils if not already loaded               */
  235.   Call RxFuncAdd 'RxFileExist', 'RXUTILS', 'RxFileExist'
  236.   Call RxFuncAdd 'RxSearchPath', 'RXUTILS', 'RxSearchPath'
  237.   Book_Spec = Help_Book
  238.   Parse Var Paths Path Etc
  239.   If \RxFileExist(Help_Book) /* Is the file in the current directory? */
  240.      Then Do until Path = "" /* No - look for it.  */
  241.        Book_Spec = RxSearchPath(Path, Help_Book)
  242.        If Book_Spec <> "" Then leave   /* Found it */
  243.        Parse Var Etc Path Etc          /* Next */
  244.      End
  245.   If Book_Spec <> ""  /* Did we find the Book? */
  246.      Then Do
  247.        Say "View" Book_Spec Subject
  248.        "View" Book_Spec Subject
  249.        Exit RC
  250.      End
  251.      else signal Help
  252.   End
  253. Return
  254.  
  255. Help:
  256.   "@ECHO OFF"
  257.   Opt.1 = ''; Opt.0 = 'NO'  /* Used to display defaults ATH  3/19/92 */
  258.   "CLS"
  259.   Say
  260.   Say ' ModPath (Modify Path) will change an OS/2 Environment variable containing'
  261.   Say ' "Path" information by modifying a single path element within the variable.  A'
  262.   Say ' single path element is any valid path statement separated by a semicolon (;).'
  263.   Say ''
  264.   Say ' M ─┬─ PATH ─┬┬ Delete ── old_path ──────────────────────────┬┬───────────┬┬'
  265.   Say ' O  ├─ DPATH ┤├ Replace ─ old_path ──── With ─── new_path ───┤├ /Dupcheck ┤│'
  266.   Say ' D  ├─ BOTH ─┤├ Test ──── target_path ───────────────────────┤├ /Silent ──┤│'
  267.   Say ' P  ├─ ... ──┘└ Add ───── new_path ───┬────────┬─────────────┤├ /Echo ────┤│'
  268.   Say ' A  │                                 ├ First ─┤             │├ /Force ───┤│'
  269.   Say ' T  │                                 ├ Last ──┘             │├ /NO...────┤│'
  270.   Say ' H  │                                 ├ Before ─ target_path ┤└──────────┘│' /* TJM 04/30/92 */
  271.   Say '    ├─ ? ───┐                         └ After ── target_path ┘             │' /* TJM 04/30/92 */
  272.   Say '    └─ Help ┴──────────────────────────────────────────────────────────────┘'
  273.   Say ''
  274.   Say ' Where: PATH or DPATH can be any valid environment statement using the same'
  275.   Say '                    data format set by the OS/2 "PATH" command.  BOTH will '
  276.   Say '                    modify both the PATH and DPATH environment statements.'
  277.   Say '        new_path    one or more paths to be set in the environment statement.'
  278.   Say '        old_path    a path to be modified in the environment statement.'
  279.   Say '        target_path a path to be searched for in the environment statement.'
  280.   Say ''
  281.   Say ' Defaults Options = /'opt.DupCk'Dupcheck, /'opt.Quiet'Silent, /'opt.Echo'Echo, /'opt.Force'Force.'
  282. Exit;
  283.