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

  1. /*
  2.   EASys! System © TNE) 1998
  3.  
  4.   RemoteEd.rexx
  5.   Insert, replace, append, prepend, extract text parts from,to ASCII arg
  6.  
  7.   Date:        08/99
  8.   Author:      Thomas Neidhardt (TNE), Deutschland, Erlangen
  9.  
  10.   InterNet:    thomas.neidhardt@fen-net.de
  11.  
  12.   Needs:       The EASys! Configuration Environment.
  13.  
  14.   Referenced by:  ${RefTable/Text-RemoteEd}
  15. */
  16.  
  17. PARSE ARG AllArguments
  18. CALL ParseArgs
  19. CALL Init
  20.  
  21. IF (arg.1='' | arg.1='h' | arg.1='help') THEN CALL Help
  22. IF (arg.1='appendtext') THEN CALL AppendText(arg.2,arg.3)
  23. IF (arg.1='prependtext') THEN CALL PrependText(arg.2,arg.3)
  24. IF (arg.1='replacestring') THEN CALL ReplaceString(arg.2,arg.3,arg.4)
  25. IF (arg.1='inserttext') THEN CALL InsertText(arg.2,arg.3,arg.4)
  26. IF (arg.1='replacetext') THEN CALL ReplaceText(arg.2,arg.3,arg.4,arg.5)
  27. IF (arg.1='movetext') THEN CALL MoveText(arg.2,arg.3,arg.4,arg.5)
  28.  
  29. CALL ExitMe
  30.  
  31. /* -------------------------- */
  32. AppendText:
  33. PARSE ARG modifyFile,appendFile
  34. IF (appendFile='') THEN CALL Help
  35.  
  36. tmpFile=modifyFile'_t'
  37.  
  38. IF (~EXISTS(modifyFile) | ~EXISTS(appendFile)) THEN RETURN
  39. IF EXISTS(tmpFile) THEN ADDRESS COMMAND 'delete "'tmpFile'" FORCE QUIET'
  40.  
  41. ADDRESS COMMAND 'join "'modifyFile'" "'appendFile'" TO "'tmpFile'"'
  42.  
  43. CALL BackupFile(modifyFile)
  44. CALL MakeModifiedFile
  45. RETURN
  46.  
  47. /* -------------------------- */
  48. PrependText:
  49. PARSE ARG preFile,modifyFile
  50. IF (modifyFile='') THEN CALL Help
  51.  
  52. tmpFile=modifyFile'_t'
  53.  
  54. IF (~EXISTS(preFile) | ~EXISTS(modifyFile)) THEN RETURN
  55. IF EXISTS(tmpFile) THEN ADDRESS COMMAND 'delete "'tmpFile'" FORCE QUIET'
  56.  
  57. ADDRESS COMMAND 'join "'preFile'" "'modifyFile'" TO "'tmpFile'"'
  58.  
  59. CALL BackupFile(modifyFile)
  60. CALL MakeModifiedFile
  61. RETURN
  62.  
  63. /* -------------------------- */
  64. ReplaceString:
  65. PARSE ARG modifyFile,string,newstring
  66. IF (modifyFile='') THEN CALL Help
  67. CALL BackupFile(modifyFile)
  68. ADDRESS COMMAND 'Install_C:ReplaceStrings "'modifyFile'" "'string'" "'newstring'"'
  69. RETURN
  70.  
  71. /* -------------------------- */
  72. InsertText:
  73. PARSE ARG insertFile,StartString,modifyFile
  74. IF (modifyFile='') THEN CALL Help
  75.  
  76. CALL Prepare
  77. tmpFile='T:SrcFile_'ProcessNumber
  78.  
  79. IF StartString='sTaRt' THEN
  80. DO
  81.  ADDRESS COMMAND 'JOIN "'modifyFile'" "'insertFile'" TO 'tmpFile
  82.  found=1
  83. END
  84. ELSE
  85. DO
  86.  IF ~Open(fModifyfile,modifyFile,'R') THEN CALL Err_ShowMessage(modifyFile)
  87.  IF ~Open(fInsertFile,insertFile,'R') THEN CALL Err_ShowMessage(insertFile)
  88.  IF ~Open(ftmpFile,tmpFile,'W') THEN CALL Err_ShowMessage(tmpFile)
  89.  
  90.  DO FOREVER
  91.    Line=ReadLn(fModifyfile)
  92.    IF EOF(fModifyfile) THEN LEAVE
  93.  
  94.    IF found=1 | POS(UPPER(StartString),UPPER(Line))=0 THEN
  95.     r=WriteLn(ftmpFile,Line) /* keep lines until found StartString and after inserting */
  96.    ELSE
  97.    DO
  98.     found=1
  99.     DO FOREVER /* write lines of InsertFile */
  100.      newLine=ReadLN(fInsertFile)
  101.      IF EOF(fInsertFile) THEN LEAVE
  102.      ELSE r=WriteLn(ftmpFile,newLine)
  103.     END
  104.     r=WriteLn(ftmpFile,Line) /* and write the line after the inserted lines */
  105.    END
  106.  END
  107.  
  108.  r=Close(fModifyfile)
  109.  r=Close(fInsertFile)
  110.  r=Close(ftmpFile)
  111. END
  112.  
  113. IF found=1 THEN
  114. DO
  115.  CALL BackupFile(modifyFile)
  116.  CALL MakeModifiedFile
  117. END
  118. RETURN
  119.  
  120. /* -------------------------- */
  121. ReplaceText:
  122. PARSE ARG replaceFile,StartString,EndString,modifyFile
  123. IF (modifyFile='') THEN CALL Help
  124.  
  125. CALL Prepare
  126. tmpFile='T:SrcFile_'ProcessNumber
  127.  
  128. IF ~Open(fModifyfile,modifyFile,'R') THEN CALL Err_ShowMessage(modifyFile)
  129. IF ~Open(freplaceFile,replaceFile,'R') THEN CALL Err_ShowMessage(replaceFile)
  130. IF ~Open(ftmpFile,tmpFile,'W') THEN CALL Err_ShowMessage(tmpFile)
  131.  
  132. DO FOREVER /* read and put all lines until StartString */
  133.   Line=ReadLn(fModifyfile)
  134.   IF EOF(fModifyfile) THEN LEAVE
  135.  
  136.   IF POS(uStartString,UPPER(Line))=0 & ExcludeStart=0 & ~(StartString='sTaRt') THEN
  137.     r=WriteLn(ftmpFile,Line)
  138.   ELSE
  139.   DO
  140.     found=1
  141.     LEAVE
  142.   END
  143. END
  144.  
  145. IF found=1 THEN
  146. DO
  147.  
  148.  IF ExcludeStart=1 THEN
  149.    r=WriteLn(ftmpFile,Line) /* write first line to file, do not replace it */
  150.  
  151.  DO FOREVER /* write the replacing lines */
  152.    replaceLine=ReadLN(fReplaceFile)
  153.    IF EOF(fReplaceFile) THEN LEAVE
  154.    ELSE r=WriteLn(ftmpFile,replaceLine)
  155.  END
  156.  
  157.  DO FOREVER /* scan for EndString, don't write the replaced lines */
  158.   Line=ReadLn(fModifyfile)
  159.   uLine=UPPER(Line)
  160.  
  161.   SELECT
  162.     WHEN EOF(fModifyfile) THEN LEAVE
  163.  
  164.     WHEN ExcludeEnd=1 & POS(uEndString,uLine)>0 THEN
  165.     DO /* Line(EndString) is excluded from being replaced */
  166.       r=WriteLn(ftmpFile,Line)
  167.       LEAVE
  168.     END
  169.  
  170.     WHEN POS(uEndString,uLine)>0 THEN LEAVE
  171.  
  172.     OTHERWISE NOP
  173.   END
  174.  END
  175.  
  176.  IF ~EOF(fModifyFile) THEN
  177.  DO FOREVER /* write rest of source back */
  178.    Line=ReadLn(fModifyfile)
  179.    IF EOF(fModifyFile) THEN LEAVE
  180.    ELSE r=WriteLn(ftmpFile,Line)
  181.  END
  182. END
  183.  
  184. r=Close(fModifyfile)
  185. r=Close(freplaceFile)
  186. r=Close(ftmpFile)
  187.  
  188. IF found=1 THEN
  189. DO
  190.  CALL BackupFile(modifyFile)
  191.  CALL MakeModifiedFile
  192. END
  193. RETURN
  194.  
  195. /* -------------------------- */
  196. MoveText:
  197. PARSE ARG modifyFile,StartString,EndString,destFile
  198. IF (destFile='') THEN CALL Help
  199.  
  200. CALL Prepare
  201. tmpFile='T:SrcFile_'ProcessNumber
  202. tmpDest='T:DestFile_'ProcessNumber
  203.  
  204. IF ~Open(fModifyfile,modifyFile,'R') THEN CALL Err_ShowMessage(modifyFile)
  205. IF ~Open(fDestFile,tmpDest,'W') THEN CALL Err_ShowMessage(tmpDest)
  206. IF ~Open(ftmpFile,tmpFile,'W') THEN CALL Err_ShowMessage(tmpFile)
  207.  
  208. DO FOREVER /* handle part of file until StartString */
  209.   Line=ReadLn(fModifyfile)
  210.   IF EOF(fModifyfile) THEN LEAVE
  211.  
  212.   IF POS(uStartString,UPPER(Line))>0 | StartString='sTaRt' THEN
  213.   DO
  214.     found=1
  215.     LEAVE
  216.   END
  217.   ELSE
  218.     r=WriteLn(ftmpFile,Line)
  219. END
  220.  
  221. IF found=1 THEN
  222. DO
  223.  /* handle first found line */
  224.  
  225.  IF IncludeStart=1 | StartString='sTaRt' | EndString='1LiNe' | PatternEnd=1 THEN
  226.    r=WriteLn(fDestFile,Line) /* move first found line */
  227.  ELSE
  228.    r=WriteLn(ftmpFile,Line) /* keep first found line */
  229.  
  230.  
  231.  DO FOREVER   /* handle rest of File */
  232.  
  233.   Line=ReadLn(fModifyfile)
  234.   IF EOF(fModifyfile) THEN LEAVE
  235.   uLine=UPPER(Line)
  236.  
  237.   IF POS(uEndString,uLine)>0 | EndString='1LiNe' | ((EndString='*sAmE' | PatternEnd=1) & POS(uStartString,uLine)=0) THEN
  238.      end_found=1
  239.  
  240.   SELECT
  241.     WHEN end_found=0 & (ExcludeEnd=1 | EndString='*sAmE' | PatternEnd=1) THEN
  242.      r=WriteLn(fDestFile,Line)
  243.  
  244.     WHEN end_found=1 & IncludeEnd=1 & end_included=0 THEN
  245.     DO
  246.      end_included=1
  247.      r=WriteLn(fDestFile,Line)
  248.     END
  249.  
  250.     WHEN end_found=0 THEN r=WriteLn(fDestFile,Line)
  251.  
  252.     OTHERWISE r=WriteLn(ftmpFile,Line)
  253.   END
  254.  END
  255. END
  256.  
  257. r=Close(fModifyfile)
  258. r=Close(fDestFile)
  259. r=Close(ftmpFile)
  260.  
  261. IF found=1 THEN
  262. DO
  263.  ADDRESS COMMAND 'type "'tmpDest'" >>"'DestFile'"'
  264.  
  265.  CALL BackupFile(modifyFile)
  266.  CALL MakeModifiedFile
  267. END
  268.  
  269. ADDRESS COMMAND 'delete "'tmpDest'" QUIET'
  270. RETURN
  271.  
  272. /* -------------------------- */
  273. Prepare:
  274.  
  275. IF ~EXISTS(modifyFile) THEN CALL Err_ShowMessage(modifyFile' does not exist!')
  276.  
  277. IncludeStart=0
  278. ExcludeStart=0
  279.  
  280. IncludeEnd=0
  281. ExcludeEnd=0
  282. PatternEnd=0
  283.  
  284. done=0
  285. found=0
  286. end_included=0
  287. end_1Line=0
  288. end_found=0
  289.  
  290. IF POS('~~',StartString)=1 THEN
  291. DO
  292.  StartString=RIGHT(StartString,LENGTH(StartString)-2)
  293.  ExcludeStart=1
  294. END
  295. ELSE
  296.  IncludeStart=1
  297.  
  298. SELECT
  299.   WHEN POS('~~',EndString)=1 THEN
  300.   DO
  301.    EndString=RIGHT(EndString,LENGTH(EndString)-2)
  302.    ExcludeEnd=1
  303.   END
  304.  
  305.   WHEN RIGHT(EndString,2)='#?' THEN
  306.   DO
  307.    EndString=LEFT(EndString,LENGTH(EndString)-2)
  308.    PatternEnd=1
  309.   END
  310.  
  311.   WHEN EndString='*sAmE' THEN NOP
  312.  
  313.   WHEN EndString='1LiNe' THEN NOP
  314.  
  315.   OTHERWISE IncludeEnd=1
  316. END
  317.  
  318. uStartString=UPPER(StartString)
  319. uEndString=UPPER(EndString)
  320.  
  321. RETURN
  322.  
  323. /* -------------------------- */
  324. MakeModifiedFile:
  325.  
  326. ADDRESS COMMAND
  327. 'protect "'modifyFile'" +wd'
  328. 'copy "'tmpFile'" "'modifyFile'"'
  329.  
  330. /* CALL Present(modifyFile) */
  331. RETURN
  332.  
  333. /* -------------------------- */
  334. BackupFile:
  335. PARSE ARG bakFile
  336.  
  337. n=2
  338. IF EXISTS(bakFile'.'n) THEN
  339. DO
  340.   DO i=2 to n
  341.     k=i-1
  342.     ADDRESS COMMAND
  343.     'protect "'bakFile'.'i'" +wd'
  344.     'copy "'bakFile'.'i'" "'bakFile'.'k'" CLONE QUIET'
  345.   END
  346.   'copy "'bakFile'" "'bakFile'.'n'" CLONE QUIET'
  347. END
  348. ELSE
  349. DO i=1 to n
  350.  IF ~EXISTS(bakFile'.'i) THEN
  351.  DO
  352.    ADDRESS COMMAND
  353.    'copy "'bakFile'" "'bakFile'.'i'" CLONE QUIET'
  354.    LEAVE i
  355.  END
  356. END
  357.  
  358. RETURN
  359.  
  360. /* -------------------------- */
  361. Init:
  362.  
  363. CR='0a'x
  364. OPTIONS RESULTS
  365. OPTIONS FAILAT 21
  366. ProcessNumber=PRAGMA('ID')
  367. TimeStamp=MyGetENV('TimeStamp')
  368. RETURN
  369.  
  370. /* -------------------------- */
  371. MyGetENV:     PROCEDURE
  372. PARSE ARG name
  373.  
  374. TheFile="ENV:" || name
  375. IF (Open(fTmp, TheFile, 'read')) THEN
  376. DO
  377.   ENVvalue=ReadLn(fTmp)
  378.   r=Close(fTmp)
  379. END
  380. ELSE ENVvalue="???"
  381.  
  382. RETURN ENVvalue
  383.  
  384. /* -------------------------- */
  385. ParseArgs:
  386.  
  387. DO i=1 TO 5
  388.   arg.i=''
  389. END
  390.  
  391. i=0
  392. DO FOREVER
  393.  i=i+1
  394.  
  395.  IF POS('"', AllArguments)=1 THEN
  396.    PARSE VAR AllArguments '"'arg.i'"' AllArguments       /* parse the line */
  397.  ELSE
  398.    PARSE VAR AllArguments arg.i' 'AllArguments
  399.  
  400.  IF (arg.i = " ") THEN
  401.  DO
  402.    IF POS('"',AllArguments)=1 THEN
  403.         PARSE VAR AllArguments '"'arg.i'"' AllArguments       /* parse the line */
  404.    ELSE
  405.         PARSE VAR AllArguments arg.i' 'AllArguments
  406.  END
  407.  
  408.  arg.i = strip(arg.i,T,' ')
  409.  arg.i = strip(arg.i,B,'"')
  410.  
  411.  IF (arg.i='') THEN
  412.   DO                    /* Break if end of Command-line */
  413.     arg.count=i-1
  414.     LEAVE
  415.   END
  416. END
  417.  
  418. RETURN
  419.  
  420. /* -------------------------- */
  421. Err_ShowMessage:
  422. PARSE ARG ErrText
  423.  
  424. say CR' Error while accessing File "'ErrText'"'CR
  425. CALL ExitMe
  426.  
  427. /* -------------------------- */
  428. Help:
  429. say ' ERROR: Wrong commandline options'
  430. CALL ExitMe
  431.  
  432. /* -------------------------- */
  433. ExitMe:
  434.  
  435. ADDRESS COMMAND 'delete "'tmpFile'" QUIET'
  436. EXIT 0
  437.  
  438.