1 | #! /usr/bin/env ruby
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 |
|
---|
4 | #
|
---|
5 | # TECS merger
|
---|
6 | # Merger for TECS generated templates
|
---|
7 | #
|
---|
8 | # Copyright (C) 2008-2017 by TOPPERS Project
|
---|
9 | #--
|
---|
10 | # ä¸è¨èä½æ¨©è
|
---|
11 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
12 | # ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
13 | # å¤ã»åé
|
---|
14 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
15 | # (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
16 | # 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
17 | # ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
18 | # (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
19 | # ç¨ã§ããå½¢ã§åé
|
---|
20 | å¸ããå ´åã«ã¯ï¼åé
|
---|
21 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
22 | # è
|
---|
23 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
24 | # ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
25 | # (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
26 | # ç¨ã§ããªãå½¢ã§åé
|
---|
27 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
28 | # ã¨ï¼
|
---|
29 | # (a) åé
|
---|
30 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
31 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
32 | # ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
33 | # (b) åé
|
---|
34 | å¸ã®å½¢æ
|
---|
35 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
36 | # å ±åãããã¨ï¼
|
---|
37 | # (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
38 | # 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
39 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
40 | 責ãããã¨ï¼
|
---|
41 | # ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
42 | # ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
43 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
44 | # å
|
---|
45 | 責ãããã¨ï¼
|
---|
46 | #
|
---|
47 | # æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
48 | ã
|
---|
49 | # ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
50 | # ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
51 | # ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
52 | # ã®è²¬ä»»ãè² ããªãï¼
|
---|
53 | #
|
---|
54 | # $Id$
|
---|
55 | #++
|
---|
56 |
|
---|
57 | #= tecsgen : TECS ã®ãã¼ã¸ã£
|
---|
58 | #
|
---|
59 | #Authors:: 大山 åå¸
|
---|
60 | #Version::
|
---|
61 | #Copyright:: Copyright (C) TOPPERS Project, 2008-2017. All rights reserved.
|
---|
62 | #License:: TOPPERS ã©ã¤ã»ã³ã¹ã«æºæ
|
---|
63 |
|
---|
64 | =begin
|
---|
65 | tecsmerge ã¯ãã³ãã¬ã¼ããã¡ã¤ã«ãå
|
---|
66 | ã«ä½æãããã»ã«ã¿ã¤ãã³ã¼ãï¼å«ã¤ã³ã©ã¤ã³é¢æ°ãè¨è¿°ããã»ã«ã¿ã¤ãã¤ã³ã©ã¤ã³ãããï¼ãä¿å®ãããã®ã§ããã
|
---|
67 | ã»ã«ã¿ã¤ãã«ä»¥ä¸ã®æ¹å¤ãå ããããå ´åã«å¯¾å¿ããã
|
---|
68 | ã»åãå£é¢æ°ãå¢ãã
|
---|
69 | ã»åãå£ã®ååãå¤æ´ããã
|
---|
70 | ã»åãå£é¢æ°ã®ååãå¤æ´ããã
|
---|
71 |
|
---|
72 | % tecsmerge source_dir dest_dir
|
---|
73 | % tecsmerge source_files dest_dir
|
---|
74 |
|
---|
75 | % tecsmerge -p port_name -f old_name:new_name source_file dest_dir
|
---|
76 | % tecsmerge -p old_name:new_name source_file dest_dir
|
---|
77 |
|
---|
78 | source_file ã®åå㯠_teml.c, _templ.h ã§çµãã£ã¦ããªãã¦ã¯ãªããªã
|
---|
79 | dest_file 㯠source_file ã® _templ ãåãé¤ããååã§ãªãã¦ã¯ãªããªã
|
---|
80 | dest_file ãåå¨ããªãå ´åã_templ ãåãé¤ããååã§ã³ãã¼ããã ãã§ãã
|
---|
81 |
|
---|
82 | 以ä¸ã®ãã¼ã¯ã¼ãã å«ãè¡ãæ¢ã
|
---|
83 |
|
---|
84 | 0) å
|
---|
85 | é
|
---|
86 | 1) /* #[<PREAMBLE>]#,
|
---|
87 | 2) * #[</PREAMBLE>]#
|
---|
88 | 3) /* #[<ENTRY_PORT>]#
|
---|
89 | 4) * #[</ENTRY_PORT>]#
|
---|
90 | 5) /* #[<ENTRY_FUNC>]#
|
---|
91 | 6) { # new mode (function header is changed)
|
---|
92 | * #[/<ENTRY_FUNC>]# # when --old-mode
|
---|
93 | * #[/ENTRY_FUNC>]# # for Bug compatibility
|
---|
94 | 7) /* #[<POSTAMBLE>]#
|
---|
95 | 8) * #[</POSTAMBLE>]#
|
---|
96 | 9) çµãã
|
---|
97 |
|
---|
98 | ä¸è¨ã®ãã¡ 1), 3), 5), 7) ãéå§ãã¼ã¯ã¼ãã¨å¼ã¶ã
|
---|
99 | ãªããç¹ã«æãã®ãªãå ´åã9) ãéå§ãã¼ã¯ã¼ãã«å«ãã
|
---|
100 |
|
---|
101 | ååã®å¤æ´
|
---|
102 |
|
---|
103 | a) åãå£åã®å¤æ´
|
---|
104 |
|
---|
105 | -p old_port_name:new_port_name
|
---|
106 |
|
---|
107 | b) åãå£é¢æ°åã®å¤æ´
|
---|
108 |
|
---|
109 | -f old_port_name:new_port_name
|
---|
110 |
|
---|
111 | old_port_name, new_port_name ã¯ã次ã®å½¢å¼ã§ãªãã¦ã¯ãªããªãã
|
---|
112 |
|
---|
113 | åãå£å + '_' + é¢æ°å
|
---|
114 |
|
---|
115 |
|
---|
116 | i) ãããï¼
|
---|
117 |
|
---|
118 | 0) ã¨éå§ãã¼ã¯ã¼ãã®é
|
---|
119 |
|
---|
120 | ii) ãããï¼
|
---|
121 |
|
---|
122 | 1) ã¨éå§ãã¼ã¯ã¼ãã®é
|
---|
123 |
|
---|
124 | é常ããããè¤æ°åå¨ãããã¨ã¯ãªãããè¤æ°åå¨ããå ´åããã¹ã¦ãã¡ã¤ã«
|
---|
125 | ã®å
|
---|
126 | é ã«åºåãããã
|
---|
127 |
|
---|
128 | iii) åãå£ããã
|
---|
129 |
|
---|
130 | 3) ã¨éå§ãã¼ã¯ã¼ãã®é
|
---|
131 |
|
---|
132 | åãå£ãããã¯ãå¾ç¶ã®åãå£é¢æ°ã®åãå£ã表ããã®ã¨ä»®å®ãããã
|
---|
133 | åãå£ãããã¯ãéè¦ã§ã¯ãªãã
|
---|
134 |
|
---|
135 | iv) åãå£é¢æ°
|
---|
136 |
|
---|
137 | 5) ã¨éå§ãã¼ã¯ã¼ãã®é
|
---|
138 |
|
---|
139 | =end
|
---|
140 |
|
---|
141 | require 'optparse'
|
---|
142 | $n_err = 0
|
---|
143 | $b_show = false
|
---|
144 | $b_exist = false # ã³ãã¼å
|
---|
145 | ã«ãã¡ã¤ã«ãããå ´åã®ã¿ãã¼ã¸
|
---|
146 | $old_mode = false # old_mode (é¢æ°æ¬ä½ã¨ã㦠/<ENTRY_FUNC> ã®ä»£ããã« '{' ã使ã
|
---|
147 |
|
---|
148 | #2.0
|
---|
149 | def write_array( file, array )
|
---|
150 | array.each{ |line|
|
---|
151 | file.write line
|
---|
152 | }
|
---|
153 | end
|
---|
154 |
|
---|
155 | class PortRenamer
|
---|
156 | @@port_renamer_list = {}
|
---|
157 | @@port_renamer_current = nil
|
---|
158 |
|
---|
159 | def initialize( old_port_name, new_port_name )
|
---|
160 | old_port_name = old_port_name.to_sym
|
---|
161 | if new_port_name then
|
---|
162 | new_port_name = new_port_name.to_sym
|
---|
163 | end
|
---|
164 | # p "port_renamer: #{old_port_name}"
|
---|
165 | @@port_renamer_list[old_port_name] = self
|
---|
166 | @@port_renamer_current = self
|
---|
167 | @old_port_name = old_port_name
|
---|
168 | @new_port_name = new_port_name
|
---|
169 | @func_renamer_list = {}
|
---|
170 | end
|
---|
171 |
|
---|
172 | def self.add_func( old_name, new_name )
|
---|
173 | # p "add_func: #{old_name}"
|
---|
174 | if @@port_renamer_current == nil then
|
---|
175 | STDERR.puts( "error: #{old_name}: specify -p before -f" )
|
---|
176 | n_err += 1
|
---|
177 | else
|
---|
178 | @@port_renamer_current.add_func( old_name, new_name )
|
---|
179 | end
|
---|
180 | end
|
---|
181 |
|
---|
182 | def add_func( old_name, new_name )
|
---|
183 | old_name = old_name.to_sym
|
---|
184 | new_name = new_name.to_sym
|
---|
185 | if @func_renamer_list[old_name] then
|
---|
186 | STDERR.puts( "error: #{old_name}: duplicate in port #{@old_port_name}" )
|
---|
187 | $n_err += 1
|
---|
188 | elsif old_name == new_name then
|
---|
189 | STDERR.puts( "error: #{old_name}: old and new are the same name" )
|
---|
190 | $n_err += 1
|
---|
191 | end
|
---|
192 | @func_renamer_list[old_name] = new_name
|
---|
193 | end
|
---|
194 |
|
---|
195 | def self.show
|
---|
196 | if @@port_renamer_list then
|
---|
197 | @@port_renamer_list.each { |pr,pc|
|
---|
198 | pc.show
|
---|
199 | }
|
---|
200 | end
|
---|
201 | end
|
---|
202 |
|
---|
203 | def self.get_list
|
---|
204 | @@port_renamer_list
|
---|
205 | end
|
---|
206 |
|
---|
207 | attr_accessor :old_port_name, :new_port_name, :func_renamer_list
|
---|
208 |
|
---|
209 | def show
|
---|
210 | print "port: #{@old_port_name}"
|
---|
211 | if @new_port_name then
|
---|
212 | print " => #{@new_port_name}"
|
---|
213 | end
|
---|
214 | puts ""
|
---|
215 |
|
---|
216 | @func_renamer_list.each { |old,new|
|
---|
217 | print " func: #{old} => #{new}\n"
|
---|
218 | }
|
---|
219 | end
|
---|
220 | end
|
---|
221 |
|
---|
222 |
|
---|
223 | class CDLContents
|
---|
224 | #@head:: [string]
|
---|
225 | #@preamble_comment:: [string]
|
---|
226 | #@preamble_body:: [string]
|
---|
227 | #@entry_port:: {ep_name=>CDLEntryPort}
|
---|
228 | #@entry_port_array:: [CDLEntryPort]
|
---|
229 | #@postamble_comment::[string]
|
---|
230 | #@postamble_body:: [string]
|
---|
231 |
|
---|
232 | @@DELIMITERS = {
|
---|
233 | # state => [ regexp_original, [previous states] , Regexp ]
|
---|
234 | :HEAD => [ "#[<BeginingOfFile>]#", [] ],
|
---|
235 | :PREAMBLE_COMMENT => [ "/* #[<PREAMBLE>]#", [:HEAD] ],
|
---|
236 | :PREAMBLE_BODY => [ " * #[</PREAMBLE>]#", [:PREAMBLE_COMMENT] ],
|
---|
237 | :ENTRY_COMMENT => [ "/* #[<ENTRY_PORT>]# PORT_NAME", [:PREAMBLE_BODY, :ENTRY_FUNC_BODY, :ENTRY_FUNC_BODY2] ],
|
---|
238 | :ENTRY_BODY => [ " * #[</ENTRY_PORT>]#", [:ENTRY_COMMENT] ],
|
---|
239 | :ENTRY_FUNC_COMMENT => [ "/* #[<ENTRY_FUNC>]# FUNC_NAME", [:ENTRY_BODY, :ENTRY_FUNC_BODY, :ENTRY_FUNC_BODY2] ],
|
---|
240 | :POSTAMBLE_COMMENT => [ "/* #[<POSTAMBLE>]#", [:ENTRY_FUNC_BODY, :ENTRY_FUNC_BODY2, :PREAMBLE_BODY] ],
|
---|
241 | :POSTAMBLE_BODY => [ " * #[</POSTAMBLE>]#", [:POSTAMBLE_COMMENT] ],
|
---|
242 | :EOF => [ "#[</EndOfFile>]#", [:POSTAMBLE_BODY,:ENTRY_FUNC_BODY, :ENTRY_FUNC_BODY2, :PREAMBLE_BODY] ],
|
---|
243 | }
|
---|
244 |
|
---|
245 | @@DELIMITERS_FUNC_BY_COMMENT = {
|
---|
246 | :ENTRY_FUNC_BODY => [ " * #[</ENTRY_FUNC>]#", [:ENTRY_FUNC_COMMENT] ],
|
---|
247 | :ENTRY_FUNC_BODY2 => [ " * #[/ENTRY_FUNC>]#", [:ENTRY_FUNC_COMMENT] ], # for Bug compatibility
|
---|
248 | }
|
---|
249 |
|
---|
250 | @@DELIMITERS_FUNC_BY_BRACKET = {
|
---|
251 | :ENTRY_FUNC_BODY => [ "\\\{", [:ENTRY_FUNC_COMMENT] ],
|
---|
252 | }
|
---|
253 |
|
---|
254 | def self.rewrite_DELIMITERS delimiters
|
---|
255 | delimiters.each{ |stat, stat_info|
|
---|
256 | s = stat_info[0].gsub( /([\*\[\]])/, "\\\\\\1" ) # *, [, ] ã®åã« \\ ãæ¿å
|
---|
257 | ¥
|
---|
258 | s.gsub!( /\s*\w*_NAME/, "\\s*(\\w*)" ) # ..._NAME ã (w*) ã«å¤æ´
|
---|
259 | s = "^" + s # ^ ãå
|
---|
260 | é ã«æ¿å
|
---|
261 | ¥
|
---|
262 | stat_info[2] = Regexp.new( s )
|
---|
263 | # p stat_info[2]
|
---|
264 | }
|
---|
265 | end
|
---|
266 |
|
---|
267 | rewrite_DELIMITERS @@DELIMITERS
|
---|
268 | rewrite_DELIMITERS @@DELIMITERS_FUNC_BY_COMMENT
|
---|
269 | rewrite_DELIMITERS @@DELIMITERS_FUNC_BY_BRACKET
|
---|
270 |
|
---|
271 | #=== ã¢ã¼ãã«å¾ã DELIMITERS ã« DELIMITERS_FUNC_BY_* ããã¼ã¸ãã
|
---|
272 | # mode :OLD_FUNC_BODY, :NEW_FUNC_BODY
|
---|
273 | def self.merge_DELIMITERS mode
|
---|
274 | case mode
|
---|
275 | when :OLD_FUNC_BODY
|
---|
276 | @@DELIMITERS.merge! @@DELIMITERS_FUNC_BY_COMMENT
|
---|
277 | when :NEW_FUNC_BODY
|
---|
278 | @@DELIMITERS.merge! @@DELIMITERS_FUNC_BY_BRACKET
|
---|
279 | else
|
---|
280 | raise "unknown mode"
|
---|
281 | end
|
---|
282 | end
|
---|
283 |
|
---|
284 | #all_contents:: [string,...]
|
---|
285 | def initialize all_contents
|
---|
286 | part = []
|
---|
287 | line_no = 0
|
---|
288 | stat = :HEAD
|
---|
289 | arg = nil
|
---|
290 | port_name = nil
|
---|
291 | func_name = nil
|
---|
292 |
|
---|
293 | @entry_port = {}
|
---|
294 | @entry_port_array = []
|
---|
295 |
|
---|
296 | @head = []
|
---|
297 | @preamble_comment = []
|
---|
298 | @preamble_body = []
|
---|
299 | @postamble_comment = []
|
---|
300 | @postamble_body = []
|
---|
301 |
|
---|
302 | (all_contents+[nil]).each{ |line| # nil: EOF
|
---|
303 | line_no += 1
|
---|
304 | # p "L: #{line}"
|
---|
305 | b_delim = false # ããªãã¿ãã¼ã¯ã¼ãã®è¡
|
---|
306 | @@DELIMITERS.each { |next_stat,stat_info|
|
---|
307 |
|
---|
308 | next if next_stat == :HEAD || ( next_stat == :EOF && line != nil )
|
---|
309 | # #1002 tecsmerge ã®éåãå£é¢æ° (POSTAMBLEé¨) ã®è¡é ã« '{' ãããã¨ã¨ã©ã¼ã«ãªã
|
---|
310 | if (! $old_mode) && ( /^\{/ =~ line ) && ( stat == :PREAMBLE_BODY || stat == :POSTAMBLE_BODY )
|
---|
311 | p line + " next_stat=" + next_stat.to_s + "stat=" + stat.to_s
|
---|
312 | next
|
---|
313 | end
|
---|
314 |
|
---|
315 | # p "R: #{stat_info[0]}"
|
---|
316 | if stat_info[2] =~ line || ( line == nil && next_stat == :EOF ) then
|
---|
317 | # p "D: #{line}: #{stat}"
|
---|
318 | b_delim = true
|
---|
319 |
|
---|
320 | found = false
|
---|
321 | stat_info[1].each{ |prev_stat|
|
---|
322 | if stat == prev_stat then
|
---|
323 | found = true
|
---|
324 | end
|
---|
325 | }
|
---|
326 | if ! found then
|
---|
327 | STDERR.puts "error #{line_no}: unsuitable previous keyword"
|
---|
328 | STDERR.puts "error #{line_no}: previous: \"#{@@DELIMITERS[stat][0]}\""
|
---|
329 | STDERR.puts "error #{line_no}: current: \"#{@@DELIMITERS[next_stat][0]}\""
|
---|
330 | expect = ""; delim = ""
|
---|
331 | stat_info[1].each{ |prev_stat| expect = "#{expect+delim}\"#{@@DELIMITERS[prev_stat][0]}\""; delim = ", " }
|
---|
332 | STDERR.puts "error #{line_no}: suitable previous: #{expect}"
|
---|
333 | $n_err += 1
|
---|
334 | end
|
---|
335 |
|
---|
336 | case stat
|
---|
337 | when :PREAMBLE_COMMENT, :ENTRY_COMMENT, :ENTRY_FUNC_COMMENT, :POSTAMBLE_COMMENT
|
---|
338 | part << line
|
---|
339 | end
|
---|
340 |
|
---|
341 | case stat # åã®ç¶æ
|
---|
342 |
|
---|
343 | when :HEAD
|
---|
344 | @head = part
|
---|
345 | when :PREAMBLE_COMMENT
|
---|
346 | @preamble_comment = part
|
---|
347 | when :PREAMBLE_BODY
|
---|
348 | @preamble_body = part
|
---|
349 | when :ENTRY_COMMENT
|
---|
350 | port_name = arg.to_sym
|
---|
351 | @entry_port[ port_name ] = CDLEntryPort.new
|
---|
352 | @entry_port_array << port_name
|
---|
353 | @entry_port[ port_name ].entry_comment = part
|
---|
354 | when :ENTRY_BODY
|
---|
355 | @entry_port[ port_name ].entry_body = part
|
---|
356 | when :ENTRY_FUNC_COMMENT
|
---|
357 | func_name = arg.to_sym
|
---|
358 | if @entry_port[ port_name ] then # nil ãªãæ¢ã«ã¨ã©ã¼
|
---|
359 | @entry_port[ port_name ].entry_func_comment[ func_name ] = part
|
---|
360 | @entry_port[ port_name ].entry_func_array << func_name
|
---|
361 | end
|
---|
362 | when :ENTRY_FUNC_BODY, :ENTRY_FUNC_BODY2
|
---|
363 | if @entry_port[ port_name ] then # nil ãªãæ¢ã«ã¨ã©ã¼
|
---|
364 | @entry_port[ port_name ].entry_func_body[ func_name ] = part
|
---|
365 | end
|
---|
366 | when :POSTAMBLE_COMMENT
|
---|
367 | @postamble_comment = part
|
---|
368 | when :POSTAMBLE_BODY
|
---|
369 | @postamble_body = part
|
---|
370 | else
|
---|
371 | raise "Unknown state #{stat}"
|
---|
372 | end
|
---|
373 |
|
---|
374 | case next_stat
|
---|
375 | when :PREAMBLE_COMMENT, :ENTRY_COMMENT, :ENTRY_FUNC_COMMENT, :POSTAMBLE_COMMENT
|
---|
376 | part = [ line ]
|
---|
377 | else
|
---|
378 | part = []
|
---|
379 | end
|
---|
380 |
|
---|
381 | stat = next_stat
|
---|
382 | arg = $1 # arg ã«åã£ã¦ãã
|
---|
383 | # p stat, arg
|
---|
384 | break
|
---|
385 | end
|
---|
386 | }
|
---|
387 |
|
---|
388 | if ! b_delim then
|
---|
389 | part << line
|
---|
390 | end
|
---|
391 | }
|
---|
392 |
|
---|
393 | end
|
---|
394 |
|
---|
395 | def check template
|
---|
396 | # template ã«ãªããã®ããã§ãã¯
|
---|
397 | @entry_port.each{ |port_name, entry_port|
|
---|
398 | temp_entry_port = template.entry_port[port_name]
|
---|
399 | if temp_entry_port == nil then
|
---|
400 | STDERR.puts "info: #{port_name} is deleted port"
|
---|
401 | next
|
---|
402 | end
|
---|
403 | # temp_entry_port.entry_func_body.each{ |f,b| p f }
|
---|
404 | entry_port.entry_func_body.each{ |func_name, func_body|
|
---|
405 | if temp_entry_port.entry_func_body[func_name] == nil then
|
---|
406 | STDERR.puts "info: #{func_name} is deleted function"
|
---|
407 | end
|
---|
408 | }
|
---|
409 | }
|
---|
410 | end
|
---|
411 |
|
---|
412 | def rename
|
---|
413 |
|
---|
414 | renamed_entry_port = {}
|
---|
415 | PortRenamer.get_list.each{ |pon,pr|
|
---|
416 |
|
---|
417 | # 対象åãå£ãæã
|
---|
418 | ep = @entry_port[pon]
|
---|
419 | if ep == nil then
|
---|
420 | STDERR.puts "warning: #{pon}: renaming port not found"
|
---|
421 | next
|
---|
422 | end
|
---|
423 |
|
---|
424 | # ãã¼ãã® rename
|
---|
425 | pnn = pr.new_port_name # ç½®æå¾ã®åå
|
---|
426 | if pnn then
|
---|
427 | # ç½®æããååãããã°ãç»é²ããªãã
|
---|
428 | renamed_entry_port[pnn] = @entry_port[pon]
|
---|
429 | @entry_port.delete pon
|
---|
430 | end
|
---|
431 |
|
---|
432 | # æå®ãããé¢æ°ã®ç½®æ
|
---|
433 | renamed_func_comment = {}
|
---|
434 | renamed_func_body = {}
|
---|
435 | pr.func_renamer_list.each{ |old,new|
|
---|
436 | ofn = "#{pon}_#{old}".to_sym
|
---|
437 | nfn = "#{pon}_#{new}".to_sym
|
---|
438 | # p "fnn: #{ofn} #{nfn} #{pon}"
|
---|
439 | if ep.entry_func_comment[ofn] == nil then
|
---|
440 | STDERR.puts "warning: #{old}: renaming function not found"
|
---|
441 | next
|
---|
442 | end
|
---|
443 | ep.entry_func_array.map! { |fn|
|
---|
444 | # p fn, nfn, ofn, fn==ofn ? nfn : fn
|
---|
445 | fn==ofn ? nfn : fn
|
---|
446 | }
|
---|
447 | renamed_func_comment[nfn] = ep.entry_func_comment[ofn]
|
---|
448 | renamed_func_body[nfn] = ep.entry_func_body[ofn]
|
---|
449 | ep.entry_func_comment.delete ofn
|
---|
450 | ep.entry_func_body.delete ofn
|
---|
451 | }
|
---|
452 | ep.entry_func_comment.merge! renamed_func_comment
|
---|
453 | ep.entry_func_body.merge! renamed_func_body
|
---|
454 |
|
---|
455 | # ãã¼ãåã®å¤æ´ã«ããé¢æ°åã®ç½®æ
|
---|
456 | renamed_func_comment = {}
|
---|
457 | renamed_func_body = {}
|
---|
458 | if pnn then
|
---|
459 | ep.entry_func_array.map! { |ofn|
|
---|
460 | nfn = ofn.to_s.sub( /#{pon.to_s}/, pnn.to_s ).to_sym
|
---|
461 | # p "pnn: #{ofn} #{nfn} #{pon} #{pnn}"
|
---|
462 | if nfn != ofn then
|
---|
463 | renamed_func_comment[nfn] = ep.entry_func_comment[ofn]
|
---|
464 | renamed_func_body[nfn] = ep.entry_func_body[ofn]
|
---|
465 | ep.entry_func_comment.delete ofn
|
---|
466 | ep.entry_func_body.delete ofn
|
---|
467 | nfn
|
---|
468 | else
|
---|
469 | ofn
|
---|
470 | end
|
---|
471 | }
|
---|
472 | ep.entry_func_comment.merge! renamed_func_comment
|
---|
473 | ep.entry_func_body.merge! renamed_func_body
|
---|
474 |
|
---|
475 | # ep.entry_func_comment.each { |f,e| p "FF: #{f}" }
|
---|
476 | # ep.entry_func_array.each { |f,e| p "FA: #{f}" }
|
---|
477 | end
|
---|
478 | }
|
---|
479 | # p renamed_entry_port
|
---|
480 | @entry_port.merge! renamed_entry_port
|
---|
481 | end
|
---|
482 |
|
---|
483 | def merge src
|
---|
484 | @head = src.head
|
---|
485 | @preamble_body = src.preamble_body
|
---|
486 | @postamble_body = src.postamble_body
|
---|
487 |
|
---|
488 | @entry_port_array.each{ |port_name|
|
---|
489 | # p "merging #{port_name}"
|
---|
490 | entry_port = @entry_port[ port_name ]
|
---|
491 | src_entry_port = src.entry_port[port_name]
|
---|
492 | if src_entry_port == nil then
|
---|
493 | print "port merged: #{port_name}\n"
|
---|
494 | next
|
---|
495 | end
|
---|
496 | entry_port.entry_body = src_entry_port.entry_body
|
---|
497 | entry_port.entry_func_array.each{ |func_name|
|
---|
498 | # p "merging #{func_name}"
|
---|
499 | func_body = entry_port.entry_func_body[ func_name ]
|
---|
500 | if src_entry_port.entry_func_body[func_name] == nil then
|
---|
501 | print "func merged: #{func_name}\n"
|
---|
502 | next
|
---|
503 | end
|
---|
504 | print "func remained: #{func_name}\n"
|
---|
505 | # entry_port.entry_func_comment[func_name] = src_entry_port.entry_func_comment[func_name]
|
---|
506 | entry_port.entry_func_body[func_name] = src_entry_port.entry_func_body[func_name]
|
---|
507 | }
|
---|
508 | }
|
---|
509 | end
|
---|
510 |
|
---|
511 | def write file
|
---|
512 | #2.0 file.write @head
|
---|
513 | write_array( file, @head )
|
---|
514 | #2.0 file.write @preamble_comment
|
---|
515 | write_array( file, @preamble_comment )
|
---|
516 | #2.0 file.write @preamble_body
|
---|
517 | write_array( file, @preamble_body )
|
---|
518 | @entry_port_array.each{ |port_name| @entry_port[port_name].write file }
|
---|
519 | #2.0 file.write @postamble_comment
|
---|
520 | write_array( file, @postamble_comment )
|
---|
521 | #2.0 file.write @postamble_body
|
---|
522 | write_array( file, @postamble_body )
|
---|
523 | end
|
---|
524 |
|
---|
525 | attr_accessor :head, :preamble_comment, :preamble_body, :entry_port, :postamble_body
|
---|
526 | end
|
---|
527 |
|
---|
528 | class CDLEntryPort
|
---|
529 | #@entry_comment:: [string]
|
---|
530 | #@entry_body:: [string]
|
---|
531 | #@entry_func_comment:: {ep_func_name=>string}
|
---|
532 | #@entry_func_body:: {ep_func_name=>string}
|
---|
533 | #@entry_func_array:: [string]
|
---|
534 |
|
---|
535 | def initialize
|
---|
536 | @entry_comment = []
|
---|
537 | @entry_body = []
|
---|
538 | @entry_func_comment = {}
|
---|
539 | @entry_func_body = {}
|
---|
540 | @entry_func_array = []
|
---|
541 | end
|
---|
542 |
|
---|
543 | def write file
|
---|
544 | #2.0 file.write @entry_comment
|
---|
545 | write_array( file, @entry_comment )
|
---|
546 | #2.0 file.write @entry_body
|
---|
547 | write_array( file, @entry_body )
|
---|
548 | @entry_func_array.each{ |fnm|
|
---|
549 | # p @entry_func_comment[fnm][0]
|
---|
550 |
|
---|
551 | #2.0 file.write @entry_func_comment[fnm]
|
---|
552 | write_array( file, @entry_func_comment[fnm] )
|
---|
553 | #2.0 file.write @entry_func_body[fnm]
|
---|
554 | write_array( file, @entry_func_body[fnm] )
|
---|
555 | }
|
---|
556 | end
|
---|
557 |
|
---|
558 | attr_accessor :entry_comment, :entry_body, :entry_func_comment, :entry_func_body, :entry_func_array
|
---|
559 | end
|
---|
560 |
|
---|
561 | def merge( src_file, dst_dir )
|
---|
562 | unless src_file =~ /(.*)_templ(.[ch])$/ then
|
---|
563 | STDERR.puts( "error: #{src_file}: not end with _templ.c/h" )
|
---|
564 | exit 1
|
---|
565 | end
|
---|
566 |
|
---|
567 | fname = "#{$1}#{$2}"
|
---|
568 | dst_file = "#{dst_dir}/#{File.basename fname}"
|
---|
569 | if FileTest.file?( dst_file ) then
|
---|
570 | print( "merging #{src_file} to #{dst_file}\n" )
|
---|
571 | # dst_file ã®èªè¾¼ã¿
|
---|
572 | begin
|
---|
573 | dst = open( dst_file )
|
---|
574 | #2.0
|
---|
575 | set_encoding dst
|
---|
576 | old_contents = dst.readlines
|
---|
577 | rescue
|
---|
578 | STDERR.puts "error: cannot open #{dst_file}"
|
---|
579 | $n_err += 1
|
---|
580 | exit 1
|
---|
581 | ensure
|
---|
582 | dst.close
|
---|
583 | end
|
---|
584 | old = CDLContents.new( old_contents )
|
---|
585 |
|
---|
586 | # template ã®èªè¾¼ã¿
|
---|
587 | begin
|
---|
588 | src = open( src_file )
|
---|
589 | #2.0
|
---|
590 | set_encoding src
|
---|
591 | new_contents = src.readlines
|
---|
592 | rescue
|
---|
593 | STDERR.puts "error: cannot open #{src_file}"
|
---|
594 | $n_err += 1
|
---|
595 | exit 1
|
---|
596 | ensure
|
---|
597 | src.close
|
---|
598 | end
|
---|
599 | templ = CDLContents.new( new_contents )
|
---|
600 |
|
---|
601 | old.rename
|
---|
602 | error_check dst_file, 1
|
---|
603 |
|
---|
604 | old.check templ
|
---|
605 | error_check dst_file, 2
|
---|
606 |
|
---|
607 | templ.merge old
|
---|
608 | error_check dst_file, 3
|
---|
609 |
|
---|
610 | bkup_file = rename_dst( dst_file )
|
---|
611 |
|
---|
612 | begin
|
---|
613 | dst = open( dst_file, "w" )
|
---|
614 | #2.0
|
---|
615 | set_encoding dst
|
---|
616 | templ.write dst
|
---|
617 | ensure
|
---|
618 | dst.close
|
---|
619 | end
|
---|
620 |
|
---|
621 | elsif $b_exist == false then
|
---|
622 | # src_file ã dst_file ã¸ã³ãã¼
|
---|
623 | begin
|
---|
624 | src = File.open( src_file )
|
---|
625 | #2.0
|
---|
626 | set_encoding src
|
---|
627 | contents = src.readlines
|
---|
628 | rescue
|
---|
629 | print "#{src_file}: fail to read\n"
|
---|
630 | ensure
|
---|
631 | src.close
|
---|
632 | end
|
---|
633 |
|
---|
634 | begin
|
---|
635 | dst = File.open( dst_file, "w" )
|
---|
636 | set_encoding dst
|
---|
637 | #2.0 dst.write( contents )
|
---|
638 | contents.each{ |line|
|
---|
639 | dst.print line
|
---|
640 | }
|
---|
641 | rescue
|
---|
642 | print "#{dst_file}: fail to write\n"
|
---|
643 | ensure
|
---|
644 | dst.close
|
---|
645 | end
|
---|
646 | else
|
---|
647 | print "info: #{dst_file} skipped\n"
|
---|
648 | end
|
---|
649 | end
|
---|
650 |
|
---|
651 | def error_check dst_file, level
|
---|
652 | if $n_err > 0 then
|
---|
653 | STDERR.puts "=== #{dst_file} not generated because of error ==="
|
---|
654 | exit level
|
---|
655 | end
|
---|
656 | end
|
---|
657 |
|
---|
658 |
|
---|
659 | #=== ãã¡ã¤ã«ã®ã¨ã³ã³ã¼ãã£ã³ã°ã ASCII-8BIT ã«å¤æ´
|
---|
660 | # Ruby 1.9 以ä¸ã®å ´åã«å¤æ´
|
---|
661 | def set_encoding file
|
---|
662 | if RUBY_VERSION >= "1.9.0" then
|
---|
663 | file.set_encoding "ASCII-8BIT"
|
---|
664 | end
|
---|
665 | end
|
---|
666 |
|
---|
667 | #=== rename_dst
|
---|
668 | # dst_file ã®ããã¯ã¢ãããã¡ã¤ã«åã決å®ãããªãã¼ã ãã
|
---|
669 | # æåããã°ããªãã¼ã å¾ã®ãã¡ã¤ã«åãè¿ã
|
---|
670 | # dst_file ãåå¨ããªããã°ï¼ãªãã¼ã ã¯è¡ãããï¼nil ãè¿ãã
|
---|
671 | def rename_dst( dst_file )
|
---|
672 | begin
|
---|
673 | File.stat dst_file
|
---|
674 | rescue
|
---|
675 | STDERR.puts( "info: backup not generated for #{dst_file}" )
|
---|
676 | # ãªããã°çµãã
|
---|
677 | return nil
|
---|
678 | end
|
---|
679 |
|
---|
680 | i = 0
|
---|
681 | found = true
|
---|
682 | while found == true
|
---|
683 | begin
|
---|
684 | i = i + 1
|
---|
685 | bkup_file = "#{dst_file}_#{i}"
|
---|
686 | File.stat bkup_file
|
---|
687 | rescue
|
---|
688 | found = false
|
---|
689 | end
|
---|
690 | end
|
---|
691 |
|
---|
692 | begin
|
---|
693 | File.rename( dst_file, bkup_file )
|
---|
694 | STDERR.puts( "info: backup generated for #{dst_file} to #{bkup_file}" )
|
---|
695 | return bkup_file
|
---|
696 | rescue
|
---|
697 | STDERR.puts( "error: fail to rename backup file: #{bkup_file}" )
|
---|
698 | exit 1
|
---|
699 | end
|
---|
700 |
|
---|
701 | return nil
|
---|
702 | end
|
---|
703 |
|
---|
704 | ARGV.options {|parser|
|
---|
705 | parser.banner =<<EOT
|
---|
706 | Usage: tecsmerge [options] files_templ.c src_dir
|
---|
707 | tecsmerge [options] gen_dir src_dir
|
---|
708 | EOT
|
---|
709 | parser.on('-e', '--exist-only', "merge if exist in destination directory") { |fs|
|
---|
710 | $b_exist = true
|
---|
711 | }
|
---|
712 | parser.on('-p', '--port=old_name:new_name', "specify entry port name change") { |ps|
|
---|
713 | p = ps.split( ':' )
|
---|
714 | PortRenamer.new( p[0], p[1] )
|
---|
715 | }
|
---|
716 | parser.on('-f', '--func=old_name:new_name', "specify entry function name change") { |fs|
|
---|
717 | f = fs.split( ':' )
|
---|
718 | PortRenamer.add_func( f[0], f[1] )
|
---|
719 | }
|
---|
720 | parser.on('-s', '--show_change_set', "show change set. all substitute") {
|
---|
721 | $b_show = true
|
---|
722 | }
|
---|
723 | parser.on('-o', '--old-mode', "old mode (function head not substituted)") { |fs|
|
---|
724 | $old_mode = true
|
---|
725 | }
|
---|
726 | parser.version = #{$version}
|
---|
727 | parser.release = nil
|
---|
728 | parser.parse!
|
---|
729 | # if ARGV.empty?
|
---|
730 | if ARGV.length < 2 then
|
---|
731 | puts parser.help
|
---|
732 | exit 1
|
---|
733 | end
|
---|
734 | }
|
---|
735 |
|
---|
736 | if $b_show then
|
---|
737 | PortRenamer.show
|
---|
738 | end
|
---|
739 |
|
---|
740 | if $old_mode then
|
---|
741 | CDLContents.merge_DELIMITERS :OLD_FUNC_BODY
|
---|
742 | else
|
---|
743 | CDLContents.merge_DELIMITERS :NEW_FUNC_BODY
|
---|
744 | end
|
---|
745 |
|
---|
746 | if $n_err > 0 then
|
---|
747 | STDERR.puts( "#{$n_err} errors" )
|
---|
748 | exit 1
|
---|
749 | end
|
---|
750 |
|
---|
751 | src = ARGV[0,ARGV.length-1]
|
---|
752 | dst = ARGV[-1]
|
---|
753 |
|
---|
754 | begin
|
---|
755 | stat = File.stat dst
|
---|
756 | rescue
|
---|
757 | STDERR.puts( "error: #{dst}: not found" )
|
---|
758 | exit 1
|
---|
759 | ensure
|
---|
760 | if stat && ! stat.directory? then
|
---|
761 | STDERR.puts( "error: #{dst}: not directory" )
|
---|
762 | exit 1
|
---|
763 | end
|
---|
764 | end
|
---|
765 |
|
---|
766 | src.each { |s|
|
---|
767 | begin
|
---|
768 | stat = File.stat s
|
---|
769 | rescue
|
---|
770 | STDERR.puts( "error: #{s}: not found or cannot access" )
|
---|
771 | exit 1
|
---|
772 | end
|
---|
773 |
|
---|
774 | if stat.directory? then
|
---|
775 | Dir.foreach(s) {|file|
|
---|
776 | if file =~ /_templ.[ch]$/ then
|
---|
777 | merge( "#{s}/#{file}", dst )
|
---|
778 | end
|
---|
779 | }
|
---|
780 | elsif stat.file? then
|
---|
781 | merge( s, dst )
|
---|
782 | end
|
---|
783 | }
|
---|