OSDN Git Service

Fixed the wrong implementation of show errormessage.
[portsreinstall/current.git] / lib / libfileedit.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Editing operations on files -
5 # Copyright (C) 2013-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Remove lines exactly matching a string from a file =============
10 fileedit_rm_a_line ()
11 {
12         local item dstpath tmpfile
13         item=$1
14         dstpath=$2
15         [ -n "$item" ] || return 0
16         [ -e "$dstpath" ] || return 0
17         tmpfile=$dstpath.fileedit_rm_a_line.tmp
18         grep -v -Fx "$item" "$dstpath" 2> /dev/null > $tmpfile || :
19         mv "$tmpfile" "$dstpath"
20 }
21
22 # ============= Add a line to a file if no matching one exists in the file =============
23 fileedit_add_a_line_if_new ()
24 {
25         local item dstpath tmpfile dstdir
26         item=$1
27         dstpath=$2
28         [ -n "$item" ] || return 0
29         [ -n "$dstpath" ] || return 0
30         tmpfile=$dstpath.fileedit_add_a_line_if_new.tmp
31         if [ -e "$dstpath" ]
32         then
33                 cp "$dstpath" "$tmpfile"
34                 grep -q -Fx "$item" "$dstpath" \
35                         || echo "$item" >> $tmpfile
36         else
37                 dstdir=`dirname "$dstpath"`
38                 mkdir -p "$dstdir"
39                 echo "$item" > $tmpfile
40         fi
41         mv "$tmpfile" "$dstpath"
42 }
43
44 # ============= Remove lines exactly matching a string from multiple files =============
45 fileedit_rm_a_line_from_files ()
46 {
47         local item filepath
48         item=$1
49         [ -n "$item" ] || return 0
50         while read filepath
51         do
52                 fileedit_rm_a_line "$item" "$filepath"
53         done
54 }
55
56 # ============= Add a line to each of multiple files if no matching one exists in the file =============
57 fileedit_add_a_line_to_files_if_new ()
58 {
59         local item filepath
60         item=$1
61         [ -n "$item" ] || return 0
62         while read filepath
63         do
64                 fileedit_add_a_line_if_new "$item" "$filepath"
65         done
66 }
67
68 # ============= Add multiple lines to a file for what don't match any existing ones in the file =============
69 fileedit_add_lines_if_new ()
70 {
71         local dstpath advance tmpfile tmpclip origin dstdir
72         dstpath=$1
73         advance=$2
74         tmpfile=$dstpath.fileedit_add_lines_if_new.tmp
75         tmpclip=${TMPDIR}/fileedit_add_lines_if_new:add
76         if [ -e "$dstpath" ]
77         then
78                 while read origin
79                 do
80                         grep -q -Fx "$origin" "$dstpath" \
81                                 || echo "$origin"
82                 done > $tmpclip
83                 if [ "@$advance" = @advance ]
84                 then
85                         cat "$tmpclip" "$dstpath"
86                 else
87                         cat "$dstpath" "$tmpclip"
88                 fi > $tmpfile
89         else
90                 dstdir=`dirname "$dstpath"`
91                 mkdir -p "$dstdir"
92                 cat > $tmpfile
93         fi
94         mv "$tmpfile" "$dstpath"
95 }
96
97 # ============= Combine (possibly missing) lists into one =============
98 fileedit_combine_lists ()
99 {
100         cat "$@" 2> /dev/null | sort -u || :
101 }
102
103 # ============= Manipulate old and new lines from an old and a new versions of a file =============
104 fileedit_manipulate_old_new_lines ()
105 {
106         local oldsrc newsrc olddiff newdiff tmpdiff
107         oldsrc=$1
108         newsrc=$2
109         olddiff=$3
110         newdiff=$4
111         tmpdiff=${TMPDIR}/fileedit_manipulate_old_new_lines::diff
112         [ -e "$oldsrc" ] || oldsrc=/dev/null
113         [ -e "$newsrc" ] || newsrc=/dev/null
114         diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
115         grep '^<' "$tmpdiff" | sed 's/^< //' > $olddiff || :
116         grep '^>' "$tmpdiff" | sed 's/^> //' > $newdiff || :
117 }
118
119 # ============= Manipulate old lines from an old and a new versions of a file =============
120 fileedit_manipulate_old_lines ()
121 {
122         local oldsrc newsrc tmpdiff
123         oldsrc=$1
124         newsrc=$2
125         tmpdiff=${TMPDIR}/fileedit_manipulate_old_lines::diff
126         [ -e "$oldsrc" ] || oldsrc=/dev/null
127         [ -e "$newsrc" ] || newsrc=/dev/null
128         diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
129         grep '^<' "$tmpdiff" | sed 's/^< //'
130 }
131
132 # ============= Manipulate new lines from an old and a new versions of a file =============
133 fileedit_manipulate_new_lines ()
134 {
135         local oldsrc newsrc tmpdiff
136         oldsrc=$1
137         newsrc=$2
138         tmpdiff=${TMPDIR}/fileedit_manipulate_new_lines::diff
139         [ -e "$oldsrc" ] || oldsrc=/dev/null
140         [ -e "$newsrc" ] || newsrc=/dev/null
141         diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
142         grep '^>' "$tmpdiff" | sed 's/^> //'
143 }
144
145 # ============= Check whether removed lines exists in updating from an old and a new versions of a file =============
146 fileedit_exists_old_lines ()
147 {
148         local oldsrc newsrc tmpdiff
149         oldsrc=$1
150         newsrc=$2
151         tmpdiff=${TMPDIR}/fileedit_exists_old_lines::diff
152         [ -e "$oldsrc" ] || oldsrc=/dev/null
153         [ -e "$newsrc" ] || newsrc=/dev/null
154         diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
155         grep -q '^<' "$tmpdiff"
156 }