home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / cfgcmd.zip / CFG.CMD next >
OS/2 REXX Batch file  |  1994-09-26  |  6KB  |  161 lines

  1. /* NCLrcvf */
  2. '@echo off'
  3. say '┌─────────────────────────────────────────┐'
  4. say '│ CFG - configuration file update utility │'
  5. say '│                                         │'
  6. say '│ Written September 1994 by Jeff J. Langr │'
  7. say '│         Public domain software          │'
  8. say '└─────────────────────────────────────────┘'
  9. say ' '
  10.  
  11. /*─────────────────────────────────────────────────────────────────────>
  12.    load Rexx system functions
  13. <─────────────────────────────────────────────────────────────────────*/
  14. if RxFuncQuery('SysLoadFuncs') then
  15. do
  16.    call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  17.    call SysLoadFuncs
  18. end
  19.  
  20. /*─────────────────────────────────────────────────────────────────────>
  21.    gather & verify arguments
  22. <─────────────────────────────────────────────────────────────────────*/
  23. parse upper arg Parm_File Parm_Var Parm_Value Parm_Location
  24. if Parm_File = '' | Parm_Var = '' | Parm_Value = '' then do
  25.    say 'Syntax:'
  26.    say '  CFG cfgfile environmentvar value [location]'
  27.    say ' '
  28.    say '  where'
  29.    say '    cfgfile        = name of configuration file to update; often C:\CONFIG.SYS'
  30.    say '    environmentvar = environment variable; for example, PATH, LIBPATH, COBDIR'
  31.    say '    value          = value string to ADD to the environment variable setting'
  32.    say '    [location]     = optional positioning of added value: F=front, B=back'
  33.    exit 10
  34. end
  35.  
  36. if Parm_Location = '' then do
  37.    Parm_Location = 'B'
  38. end
  39.  
  40. if Parm_Location <> 'F' & Parm_Location <> 'B' then do
  41.    say 'Location parameter must be either F or B'
  42.    exit 12
  43. end
  44.  
  45. /*─────────────────────────────────────────────────────────────────────>
  46.    open input file passed as parameter
  47. <─────────────────────────────────────────────────────────────────────*/
  48. rc = stream(Parm_File, 'C', 'OPEN READ')
  49. if rc <> 'READY:' then do
  50.    say 'Unable to open configuration file' Parm_File '- program halted.'
  51.    exit 11
  52. end
  53.  
  54. /*─────────────────────────────────────────────────────────────────────>
  55.    create temporary file to hold copy of input file
  56. <─────────────────────────────────────────────────────────────────────*/
  57. TmpCfg=SysTempFileName(FILESPEC('drive',Parm_File)||FILESPEC('path',Parm_File)||'CFGTEMP.???')
  58.  
  59. /*─────────────────────────────────────────────────────────────────────>
  60.    loop through input control file
  61. <─────────────────────────────────────────────────────────────────────*/
  62. env_match = 0
  63.  
  64. do while lines(Parm_File)
  65.   Rec_Out = LineIn(Parm_File)
  66.   Rec_In = Strip(Rec_Out, 'b', ' ')
  67.  
  68.   match = 0
  69.  
  70.   /*──────────────────────────────────────────────────────────────────>
  71.      check for LIBPATH statement; parse accordingly
  72.   <──────────────────────────────────────────────────────────────────*/
  73.   if Parm_Var = 'LIBPATH' then do
  74.      if translate(left(Rec_In,7))='LIBPATH' then do
  75.         parse value Rec_In with cfgVar '=' cfgRest
  76.         match = 1
  77.         cfgStart = cfgVar||'='
  78.      end
  79.   end
  80.   else do
  81.      /*───────────────────────────────────────────────────────────────>
  82.          check for SET statement; parse accordingly
  83.      <───────────────────────────────────────────────────────────────*/
  84.      if translate(left(Rec_In,4))='SET ' then do
  85.         parse value Rec_In with cfgSet cfgVar '=' cfgRest
  86.         if cfgVar = Parm_Var then do
  87.            match = 1
  88.            cfgStart = cfgSet cfgVar||'='
  89.         end
  90.      end
  91.   end
  92.  
  93.   /*──────────────────────────────────────────────────────────────────>
  94.      match made on environment variable name - update entry
  95.   <──────────────────────────────────────────────────────────────────*/
  96.   if match = 1 then do
  97.      env_match=1
  98.      cfgValues=translate(cfgRest,' ',';') /* change ; to spaces            */
  99.      numValues=words(cfgValues)           /* count # of entries in string  */
  100.  
  101.      match_value = 0
  102.  
  103.      /*───────────────────────────────────────────────────────────────>
  104.         loop through all entries in environment string
  105.      <───────────────────────────────────────────────────────────────*/
  106.      do i=1 to numValues
  107.        if subword(cfgValues, i, 1) = Parm_Value then do
  108.           match_value = 1
  109.           say 'Value' Parm_Value 'already exists for' Parm_Var||';' Parm_File 'not updated.'
  110.           call lineout TmpCfg
  111.           call SysFileDelete(TmpCfg)
  112.           exit 1
  113.        end
  114.      end
  115.      /*───────────────────────────────────────────────────────────────>
  116.         add new value if not found in environment string entries
  117.      <───────────────────────────────────────────────────────────────*/
  118.      if match_value = 0 then do /* value not already in list  */
  119.         say 'Adding value' Parm_Value
  120.         if Parm_Location = 'F' then do
  121.            Rec_Out = cfgStart||Parm_Value||';'||cfgRest
  122.         end
  123.         else do
  124.            if right(cfgRest,1)=';' then do
  125.              Rec_Out = cfgStart||cfgRest||Parm_Value
  126.            end
  127.            else do
  128.              Rec_Out = cfgStart||cfgRest||separator||Parm_Value
  129.            end
  130.         end
  131.      end
  132.   end
  133.   rc=lineout(TmpCfg,Rec_Out)
  134. end
  135.  
  136. /*─────────────────────────────────────────────────────────────────────>
  137.    no environment variable matched; add SET line at end of file
  138. <─────────────────────────────────────────────────────────────────────*/
  139. if env_match = 0 then do  /* no match; add new set statement  */
  140.    Rec_Add = 'SET' Parm_Var||'='||Parm_Value
  141.    say 'Adding line to' Parm_File||':'
  142.    say Rec_Add
  143.    rc=lineout(TmpCfg,Rec_Add)
  144. end
  145.  
  146. /*─────────────────────────────────────────────────────────────────────>
  147.    close files; delete input file; rename temp file to input file
  148. <─────────────────────────────────────────────────────────────────────*/
  149. call lineout TmpCfg
  150. rc=stream(Parm_File, 'C', 'CLOSE')
  151. rc=SysFileDelete(Parm_File)
  152. if rc=0 then do
  153.    'rename' TmpCfg FILESPEC('name',Parm_File)
  154. end
  155. else do
  156.    say 'Could not update' Parm_File||'; new version saved as' TmpCfg||'.'
  157.    say 'Error code='||rc
  158. end
  159.  
  160. exit 0
  161.