home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / System / EASys / EASys!_update_481_to_482 / c / ReplaceFileContents.rexx < prev    next >
OS/2 REXX Batch file  |  2000-04-21  |  11KB  |  531 lines

  1. /*
  2.    EASys! System © TNE) 1998
  3.  
  4.    ReplaceFileContents.rexx
  5.    Find and change Contents of Files by an intuitive dialog-process.
  6.  
  7.    Date:        07/98
  8.    Author:      Thomas Neidhardt (TNE), Deutschland, Erlangen
  9.  
  10.    InterNet:    thomas.neidhardt@fen-net.de
  11.  
  12.    Needs:       RexxReqTools Package by Rafael D'Halleweyn ( rdhall@rug.ac.be )
  13.                 and the EASys! Configuration Environment.
  14.  
  15.    Referenced by:  ${RefTable/File-ReplaceContents}
  16.  
  17.    Complies to Standard Workbench-CLI-Command "search ... QUICK"
  18. */
  19.  
  20. PARSE ARG args
  21.  
  22. OPTIONS RESULTS
  23. OPTIONS FAILAT 21
  24.  
  25. CALL Init
  26.  
  27. /*--------------------------------------------*/
  28. IF ~(args='' | args='ARGS') THEN
  29.  DO
  30.   CALL ArgParser
  31.  
  32.   SearchedName=args.1
  33.   Search=SearchedName
  34.  
  35.   NewName=args.2
  36.   Replace=NewName
  37.  
  38.   SearchPath=args.3
  39.   IF SearchPath='' THEN CALL ExitMe
  40.   IF args.4="AUTO" THEN AutomaticRun=1
  41.  
  42.   CALL Searcher
  43.   CALL ExitMe
  44.  END
  45. ELSE
  46. DO
  47.   IF (Search="" | Search="SEARCH") THEN Search="???"
  48.  
  49.   Replace=MyGetENV("Flags/ActualReplace2")
  50.   IF (Replace="" | Replace="REPLACE") THEN Replace="???"
  51.  
  52.   DO FOREVER
  53.     DO FOREVER
  54.      SearchedName=''
  55.      SearchedName=ShowMessage('ReplaceFileContents.1',,,Search,AppName)
  56.      If (Action=2) THEN CALL Help
  57.      ELSE LEAVE
  58.     END
  59.  
  60.     If (SearchedName='') THEN CALL ExitMe
  61.     Search=SearchedName
  62.  
  63.     DO FOREVER
  64.      NewName=''
  65.      NewName=ShowMessage('ReplaceFileContents.2',,,Replace,AppName)
  66.      If (Action=2) THEN CALL Help
  67.      ELSE LEAVE
  68.     END
  69.  
  70.     If (NewName='') THEN CALL ExitMe
  71.     Replace=NewName
  72.  
  73.     CALL PathReq
  74.     CALL Searcher
  75.  
  76.     again=0
  77.     IF (Count=0) THEN
  78.     DO
  79.       CALL CloseCon
  80.       again=ShowMessage('ReplaceFileContents.6',,,,AppName,SearchedText)
  81.     END
  82.     ELSE
  83.       again=ShowMessage('ReplaceFileContents.7',,,,AppName)
  84.  
  85.     IF (again=0) THEN LEAVE
  86.   END
  87. END
  88. CALL ExitMe
  89.  
  90. /* ----------------------------------------- */
  91. PathReq:
  92.  
  93. ActualPath=SearchPath
  94. IF (RIGHT(ActualPath,1)=":" | ActualPath='???') THEN
  95. DO
  96.   ADDRESS COMMAND 'assign >NIL: "'ActualPath'" EXISTS'
  97.   IF (RC>1) THEN ActualPath="SYS:"
  98. END
  99. ELSE
  100.   IF (~EXISTS(ActualPath) | ActualPath="") THEN ActualPath="SYS:"
  101.  
  102. SearchPath=''
  103. SearchPath=ShowMessage('ReplaceFileContents.3',ActualPath,,,AppName)
  104. IF (SearchPath='') THEN CALL ExitMe
  105.  
  106. RETURN
  107.  
  108. /* ----------------------------------------- */
  109. Searcher:
  110.  
  111. SearchedText='"'SearchedName'"'
  112. ReplacingText ='"'NewName'"'
  113.  
  114. CALL ShowMessage('ReplaceFileContents.4',,,,AppName,SearchedText,ReplacingText,SearchPath)
  115.  
  116. ADDRESS COMMAND 'sys:c/search >'FoundLog '"'SearchPath'" 'SearchedText' QUICK ALL'
  117.  
  118. CALL CloseCon
  119.  
  120. IF AutomaticRun=1 THEN
  121.  AUTOREPLACE=1
  122. ELSE
  123.  AUTOREPLACE=0
  124.  
  125. REPLACEIT=0
  126. Count=0
  127.  
  128. IF ~(open(SearchFile, FoundLog, 'read')) THEN
  129.   DO
  130.      MsgFile=FoundLog
  131.      CALL Err_ShowMessage
  132.   END
  133.  
  134. DO UNTIL (EOF(SearchFile))
  135.   r=READLN(SearchFile)     /* Read each line then analyze it */
  136.   IF Right(r,1)="
  137. " THEN
  138.   DO
  139.     Count=Count + 1
  140.  
  141.     endpos=LastPos("
  142. ",r)-4
  143.     begpos=LastPos("
  144. ",r,endpos-1)+3
  145.     IF (begpos=3) THEN begpos=0
  146.     FilePath=RIGHT(r,LENGTH(r)-BegPos)
  147.     FilePath='"'LEFT(FilePath,LENGTH(FilePath)-4)'"'
  148.  
  149.     IF(AUTOREPLACE=0) THEN
  150.     DO
  151.       Action=ShowMessage('ReplaceFileContents.5',,,,AppName,SearchedText,ReplacingText,FilePath)
  152.       IF(Action=0) THEN LEAVE
  153.       IF(Action=1) THEN REPLACEIT=1
  154.       IF(Action=2) THEN AUTOREPLACE=1
  155.       IF(Action=3) THEN REPLACEIT=0
  156.       IF(Action=4) THEN LEAVE
  157.     END
  158.  
  159.     IF ((REPLACEIT=1) | (AUTOREPLACE=1)) THEN
  160.     DO
  161.      REPLACEIT=0
  162.      ADDRESS COMMAND 'Install_C:ReplaceStrings 'FilePath SearchedText ReplacingText' I'
  163.     END
  164.   END
  165. END
  166.  
  167. Result=close(SearchFile)
  168.  
  169. RETURN
  170.  
  171. /*--------------------------------------------*/
  172. ShowMessage:
  173.  
  174.  PARSE ARG Mesg,ReqPath,ReqFile,PresetString,Parameter1,Parameter2,Parameter3,Parameter4
  175.  
  176.  drop Action
  177.  ReqType=""
  178.  
  179.  MsgFile='EASys!_rexx:'Language'/'Mesg
  180.  IF (Open(TEMPFILE, MsgFile, 'read')) THEN
  181.   DO
  182.     ReqType=ReadLn(TEMPFILE)  /* read Requester-Type */
  183.  
  184.     Line=ReadLn(TEMPFILE)  /* read Title-Line */
  185.     CALL Substitutions
  186.     ReqTitle=Line
  187.  
  188.     Line=ReadLn(TEMPFILE)  /* free line after Title */
  189.  
  190.     Message=""
  191.  
  192.     DO UNTIL EOF(TEMPFILE)
  193.  
  194.       Line=ReadLn(TEMPFILE)
  195.       CALL Substitutions
  196.  
  197.       IF POS(">>>", Line) > 0 THEN
  198.         DO
  199.           rtBtns=STRIP(Line, 'L', '>')
  200.           LEAVE
  201.         END
  202.       ELSE
  203.           Message=Message || CR || Line
  204.     END
  205.  
  206.     r=Close(TEMPFILE)
  207.   END
  208.  ELSE
  209.     CALL Err_ShowMessage
  210.  
  211.   uReqType=UPPER(ReqType)
  212.  
  213.   IF (uReqType='MULTI REQUEST') THEN
  214.    DO
  215.       r=rtezrequest(Message,rtBtns,ReqTitle,PosTags)
  216.       RETURN r
  217.    END
  218.  
  219.   IF (uReqType='GET ONE FILE') THEN
  220.    DO
  221.       r=rtfilerequest(ReqPath,ReqFile,ReqTitle,rtBtns,ONEFILE_TAG, Files)
  222.       RETURN r
  223.    END
  224.  
  225.   IF (uReqType='GETFILES') THEN
  226.    DO
  227.       r=rtfilerequest(ReqPath,ReqFile,ReqTitle,rtBtns, FILE_TAGS, Files)
  228.       RETURN r
  229.    END
  230.  
  231.   IF (uReqType='GETPATH') THEN
  232.    DO
  233.       r=rtfilerequest(ReqPath,,ReqTitle,rtBtns, DIR_TAGS)
  234.       RETURN r
  235.    END
  236.  
  237.   IF (uReqType='GETSTRING') THEN
  238.    DO
  239.       r=rtgetstring(PresetString,Message,ReqTitle,rtBtns,GStags,Action)
  240.       RETURN r
  241.    END
  242.  
  243.   IF (uReqType='GETSTRING SIMPLE') THEN
  244.    DO
  245.       r=rtgetstring(PresetString,Message,ReqTitle,,GStags,Action)
  246.       RETURN r
  247.    END
  248.  
  249.   IF (uReqType='CONSOLE') THEN
  250.    DO
  251.       ShowConsole='CON:'ReqLE'/'ReqTE'/'ConW'/'ConH'/'ReqTitle'/NOCLOSE'
  252.       IF ~(Open(Con,ShowConsole,'write')) THEN
  253.       DO
  254.           MsgFile=ThisConsole
  255.           CALL Err_ShowMessage
  256.           ConStat=0
  257.       END
  258.  
  259.       ConStat=1
  260.       r=WriteLn(Con,Message)
  261.  
  262.       RETURN
  263.    END
  264.  
  265.   CALL Err_ShowMessage  /* Error if arrived here */
  266.  
  267. RETURN
  268.  
  269. /*--------------------------------------------*/
  270. Substitutions:
  271.  
  272.  IF POS("%1", Line) > 0 THEN             /* substitution parameters for each message */
  273.  DO UNTIL POS("%1", Line)=0            /* and per line */
  274.      parse var Line part1 '%1' part2
  275.      Line=part1''parameter1''part2
  276.  END
  277.  
  278.  IF POS("%2", Line) > 0 THEN
  279.  DO UNTIL POS("%2", Line)=0
  280.      parse var Line part1 '%2' part2
  281.      Line=part1''parameter2''part2
  282.  END
  283.  
  284.  IF POS("%3", Line) > 0 THEN
  285.  DO UNTIL POS("%3", Line)=0
  286.      parse var Line part1 '%3' part2
  287.      Line=part1''parameter3''part2
  288.  END
  289.  
  290.  IF POS("%4", Line) > 0 THEN
  291.  DO UNTIL POS("%4", Line)=0
  292.      parse var Line part1 '%4' part2
  293.      Line=part1''parameter4''part2
  294.  END
  295.  
  296. RETURN
  297.  
  298. /*--------------------------------------------*/
  299. Err_ShowMessage:
  300.  
  301.  Message=AppName':' CR CR "Sorry, an error has occured accessing the MessageFile" CR CR "    "MsgFile CR CR "... exiting."
  302.  rtBtns='Oh no!'
  303.  Action=rtezrequest(Message,rtBtns,ReqTitle,PosTags)
  304.  CALL ExitMe
  305.  
  306. RETURN
  307.  
  308. /*--------------------------------------------*/
  309. CloseCon:
  310.  
  311. IF (ConStat=1) THEN
  312. DO
  313.  ADDRESS COMMAND 'wait 1'
  314.  r=Close(Con)
  315.  ConStat=0
  316. END
  317.  
  318. RETURN
  319.  
  320. /*--------------------------------------------*/
  321. MyGetENV:     PROCEDURE
  322.  PARSE ARG name
  323.  
  324.  TheFile="ENV:" || name
  325.  IF (open(TEMPFILE, TheFile, 'read')) THEN
  326.   DO
  327.    ENVvalue=READLN(TEMPFILE)
  328.    Closed=close(TEMPFILE)
  329.   END
  330.  ELSE
  331.    ENVvalue="???"
  332.  
  333. RETURN ENVvalue
  334.  
  335. /*--------------------------------------------*/
  336. GetLanguage:
  337.  
  338. LanguageNumber=1      /* Presets */
  339. Language=''
  340.  
  341. IF (open(TEMPFILE, 'ENV:sys/locale.prefs', 'read')) THEN
  342.  DO
  343.    DO i=1 to 50
  344.        r=READCH(TEMPFILE)
  345.    END
  346.  
  347.    Land=''
  348.  
  349.    i=0
  350.    DO UNTIL EOF(TEMPFILE)
  351.        i=i+1
  352.        Char=ReadCh(TEMPFILE)
  353.        IF (C2D(Char)=0) THEN
  354.            LEAVE
  355.        ELSE
  356.            Land=Land''Char
  357.    END
  358.  
  359.    /* 32 characters from begin of Lang_x to begin of Lang_y */
  360.    DO j=i to 31 UNTIL EOF(TEMPFILE)
  361.     r=READCH(TEMPFILE)
  362.    END
  363.  
  364.    Language=ReadLanguage()
  365.    r=Close(TEMPFILE)
  366.  END
  367.  
  368.  IF (Language='') THEN Language='english'
  369.  
  370.  drop Char
  371.  drop r
  372.  
  373.  ADDRESS COMMAND 'setenv LANGUAGE 'Language
  374.  
  375.  IF ~EXISTS('EASys!_rexx:'Language'/FileManager.1') THEN
  376.      Language='english'
  377.  
  378. RETURN
  379.  
  380. /*----------------------------------*/
  381. ReadLanguage:
  382.  
  383.  Langua=''
  384.  
  385.  Langua=ReadCh(TEMPFILE)
  386.  IF (C2D(Langua)=0) THEN RETURN ''
  387.  
  388.  i=1
  389.  DO UNTIL EOF(TEMPFILE)
  390.     i=i+1
  391.     Char=ReadCh(TEMPFILE)
  392.     IF (C2D(Char)=0) THEN
  393.       LEAVE
  394.     ELSE
  395.       Langua=Langua''Char
  396.  END
  397.  
  398.  /* 30 characters from begin of Lang_x to begin of Lang_y */
  399.  DO j=i to 29 UNTIL EOF(TEMPFILE)
  400.     r=READCH(TEMPFILE)
  401.  END
  402.  
  403. RETURN Langua
  404.  
  405. /*--------------------------------------------*/
  406. ArgParser:
  407.  
  408. i=0
  409. DO forever
  410.  i=i+1
  411.  args.i=''
  412.  
  413.  IF POS('"',WORD(args,1))=0 THEN PARSE VAR args args.i args
  414.  ELSE IF POS('"',WORD(args,1))=1 THEN PARSE VAR args'"'args.i'"'args
  415.  
  416.  args.i=strip(args.i,B,'"')
  417.  
  418.  IF (args.i='' & POS('20'x,args.i)=0) THEN
  419.   DO                    /* Break if end of Command-line */
  420.     args.count=i-1
  421.     LEAVE
  422.   END
  423. END
  424.  
  425. i=args.count  /* set last path to Actual path */
  426. Path=Path.i
  427.  
  428. RETURN 1
  429.  
  430. /*--------------------------------------------*/
  431. Init:
  432.  
  433. AppName="© TNE) Text-Replacer"
  434.  
  435. CR='0a'x
  436.  
  437. CALL GetLanguage
  438.  
  439. if ~show('L','rexxreqtools.library') THEN Result=ADDLIB('rexxreqtools.library',0,-30,0)
  440.  
  441. if ~show('L','rexxsupport.library') THEN Result=ADDLIB('rexxsupport.library',0,-30,0)
  442.  
  443. ProcessNumber=PRAGMA('ID')
  444.  
  445.  
  446. CALL FORBID
  447.  
  448. Intui =showlist(l, 'intuition.library',, a)
  449. Screen=next(intui, 56)
  450. Font  =next(screen, 136)
  451. FontSize=c2d(import(offset(font, 20), 2))
  452.  
  453. CALL PERMIT
  454.  
  455.  
  456. MaxTitleChars=MyGetENV("RefTable/GUI-ShellTitleChars")
  457.  
  458. ConsoleType=MyGetENV("RefTable/GUI-ConsoleType")
  459.  
  460. ConsoleSize=MyGetENV("SHELLsize")
  461. IF (ConsoleSize="") THEN ConsoleSize="40/50/600/200"
  462.  
  463. ConW=520
  464. ConH=210
  465.  
  466. ReqLE=MyGetENV("RefTable/GUI-ReqLE")  /* Main Window */
  467. ReqTE=MyGetENV("RefTable/GUI-ReqTE")
  468.  
  469. ReqHeight=MyGetENV("RefTable/GUI-ReqHeight")
  470.  
  471. rt_TE1=ReqTE + FontSize + 4  /* Requester-Windows */
  472. rt_LE1=ReqLE + 6
  473.  
  474. PosTags="rt_reqpos=reqpos_topleftscr rt_leftoffset=" ReqLE " rt_topoffset=" ReqTE
  475. EZtags=PosTags
  476. GLtags=PosTags || "rtgl_min=0 rtgl_max=1500 rtgl_backfill=true "
  477. GStags=PosTags || " rtgs_backfill=false rtgs_width=300"
  478.  
  479. POINTER_TAGS="rt_reqpos=reqpos_pointer"
  480. DIR_TAGS=PosTags || " rtfi_flags=freqf_nofiles|freqf_save rtfi_height=" ReqHeight
  481. FILE_TAGS=PosTags || " rtfi_flags=freqf_multiselect|freqf_selectdirs" || " rtfi_height=" ReqHeight
  482.  
  483. Console='>"'ConsoleType || ConsoleSize'/'AppName' Messages.../AUTO/NOCLOSE/ALT'ReqLE'/'ReqTE'/500/40"'
  484.  
  485. /*-----------------------*/
  486.  
  487. FoundLog='T:RS_FoundInFiles_'ProcessNumber
  488. SearchPath=MyGetENV("Flags/ActualPath")
  489. Search=MyGetENV("Flags/ActualReplace1")
  490.  
  491. RETURN
  492.  
  493. /*--------------------------------------------*/
  494. Help:
  495. ADDRESS COMMAND '${RefTable/Text-ViewGUIDE} EASys!:Help/'Language'/EASys!_TextFunctions.guide'
  496. RETURN
  497.  
  498. /*--------------------------------------------*/
  499. ExitMe:
  500.  
  501. Len=LENGTH(SearchPath)
  502. DevicePos=LASTPOS(':', SearchPath)
  503. DirPos=LASTPOS('/', SearchPath)
  504.  
  505. IF (DirPos > 0) THEN
  506.   SearchPath=LEFT(SearchPath, DirPos-1)
  507. ELSE
  508.   SearchPath=LEFT(SearchPath, DevicePos)
  509.  
  510. ADDRESS COMMAND
  511. 'setenv Flags/ActualReplace1 "'Search'"'
  512. 'setenv Flags/ActualReplace2 "'Replace'"'
  513. 'delete 'FoundLog' QUIET'
  514. 'assign >NIL: Profile: EXISTS'
  515. IF RC=0 THEN 
  516. DO
  517.  ADDRESS COMMAND 
  518.  'copy ENV:Flags/ActualPath Profile:ENV/Flags CLONE'
  519.  'copy ENV:Flags/ActualReplace1 Profile:ENV/Flags CLONE QUIET'
  520.  'copy ENV:Flags/ActualReplace2 Profile:ENV/Flags clone quiet'
  521. END
  522. ELSE
  523. DO
  524.  ADDRESS COMMAND 
  525.  'copy ENV:Flags/ActualPath EASys!:Flags CLONE'
  526.  'copy ENV:Flags/ActualReplace1 EASys!:Flags CLONE QUIET'
  527.  'copy ENV:Flags/ActualReplace2 EASys!:Flags clone quiet'
  528. END
  529.  
  530. EXIT 0
  531.