1/* Part of SWISH 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@cs.vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (C): 2014-2016, VU University Amsterdam 7 CWI Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(patch, 37 [ patch/4 % +Data, +Diff, -Merged, +Options 38 ]). 39:- use_module(library(process)). 40:- use_module(library(option)). 41 42/** <module> Run patch program 43 44This library uses the GNU patch(1) program to merge changes. 45*/ 46 47:- predicate_options(patch/4, 4, 48 [ status(-compound), 49 stderr(-string) 50 ]). 51 52%% patch(+Orig:string, +Diff:string, -Merged:string, +Options) 53% 54% Patch the string Orig using Diff. Options: 55% 56% - status(-Status) 57% Unify Status with the completion status of patch. This 58% is exit(0) for smooth completion and exit(1) if there are 59% merge conflicts. 60% - stderr(-String) 61% Unify String with the data patch(1) sent to standard error. 62 63patch(Orig, Diff, Merged, Options) :- 64 setup_call_cleanup( 65 tmp_file_stream(utf8, TmpFile, Out), 66 ( call_cleanup(format(Out, '~s', [Orig]), 67 close(Out)), 68 run_patch(TmpFile, Diff, Merged, Options) 69 ), 70 delete_file(TmpFile)). 71 72run_patch(File, Diff, Merged, Options) :- 73 thread_self(Me), 74 setup_call_cleanup( 75 process_create(path(patch), 76 [ '--force', '--merge', '--silent', 77 '--output=-', file(File) ], 78 [ stdin(pipe(In)), 79 stdout(pipe(Out)), 80 stderr(pipe(Err)), 81 process(PID) 82 ]), 83 ( set_stream(In, encoding(utf8)), 84 set_stream(Out, encoding(utf8)), 85 set_stream(Err, encoding(utf8)), 86 thread_create(copy_diff(Diff, In), _, [detached(true)]), 87 thread_create(read_stderr(Me, Err), ErrThread, []), 88 read_string(Out, _, Merged) 89 ), 90 ( close(Out), 91 process_wait(PID, Status) 92 )), 93 get_errors(ErrThread, Options), 94 ( option(status(VarStat), Options) 95 -> VarStat = Status 96 ; true 97 ). 98 99copy_diff(Diff, To) :- 100 call_cleanup( 101 format(To, '~s', [Diff]), 102 close(To)). 103 104read_stderr(Master, Stderr) :- 105 read_string(Stderr, _, Errors), 106 thread_send_message(Master, patch_errors(Errors)). 107 108get_errors(Thread, Options) :- 109 thread_join(Thread, Status), 110 ( Status == true 111 -> thread_get_message(patch_errors(Errors)) 112 ; Status == exception(Error) 113 -> throw(Error) 114 ), 115 ( option(stderr(Var), Options) 116 -> Var = Errors 117 ; true 118 )