2021-06-21 05:08:02 +03:00
|
|
|
<% #-*- mode: ruby -*-
|
2008-01-13 03:10:00 +03:00
|
|
|
#
|
|
|
|
# OnigEncodingDefine(foo, Foo) = {
|
|
|
|
# ..
|
|
|
|
# "Shift_JIS", /* Canonical Name */
|
|
|
|
# ..
|
|
|
|
# };
|
|
|
|
# ENC_ALIAS("SJIS", "Shift_JIS")
|
|
|
|
# ENC_REPLICATE("Windows-31J", "Shift_JIS")
|
|
|
|
# ENC_ALIAS("CP932", "Windows-31J")
|
|
|
|
#
|
|
|
|
|
2008-01-19 23:15:13 +03:00
|
|
|
def check_duplication(defs, name, fn, line)
|
|
|
|
if defs[name]
|
|
|
|
raise ArgumentError, "%s:%d: encoding %s is already registered(%s:%d)" %
|
|
|
|
[fn, line, name, *defs[name]]
|
|
|
|
else
|
|
|
|
defs[name.upcase] = [fn,line]
|
2008-01-13 23:46:00 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
lines = []
|
2013-07-01 18:12:10 +04:00
|
|
|
BUILTIN_ENCODINGS = {
|
|
|
|
'ASCII-8BIT' => 0,
|
|
|
|
'UTF-8' => 1,
|
|
|
|
'US-ASCII' => 2,
|
|
|
|
}
|
|
|
|
encodings = %w[ASCII-8BIT UTF-8 US-ASCII] # BUILTIN_ENCODINGS.keys is not available on cross compiling and used ruby 1.8
|
|
|
|
count = encodings.size
|
2008-01-19 23:15:13 +03:00
|
|
|
defs = {}
|
2008-08-08 11:15:52 +04:00
|
|
|
encdirs = ARGV.dup
|
|
|
|
encdirs << 'enc' if encdirs.empty?
|
|
|
|
files = {}
|
|
|
|
encdirs.each do |encdir|
|
2008-08-09 01:37:43 +04:00
|
|
|
next unless File.directory?(encdir)
|
2024-03-17 13:09:37 +03:00
|
|
|
Dir.glob("*.[ch]", base: encdir).sort_by {|e|
|
2008-08-08 11:15:52 +04:00
|
|
|
e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten
|
|
|
|
}.each do |fn|
|
|
|
|
next if files[fn]
|
|
|
|
files[fn] = true
|
2024-03-17 13:09:37 +03:00
|
|
|
File.open(File.join(encdir, fn)) do |f|
|
2008-08-08 11:15:52 +04:00
|
|
|
name = nil
|
|
|
|
f.each_line do |line|
|
2024-03-17 12:28:28 +03:00
|
|
|
if (/^#ifndef RUBY/ =~ line)..(/^#endif/ =~ line)
|
2024-03-17 13:09:37 +03:00
|
|
|
elsif /^OnigEncodingDefine/.match?(line)
|
|
|
|
if (n = f.gets("\n\};")[/"(.*?)"/, 1]) # swallow the initializer block
|
2024-03-17 12:28:28 +03:00
|
|
|
if name
|
2024-03-17 13:09:37 +03:00
|
|
|
lines << %[ENC_SET_BASE("#{n}", "#{name}");]
|
2024-03-17 12:28:28 +03:00
|
|
|
else
|
2024-03-17 13:09:37 +03:00
|
|
|
name = n
|
2024-03-17 12:28:28 +03:00
|
|
|
end
|
2024-03-17 13:09:37 +03:00
|
|
|
check_duplication(defs, n, fn, f.lineno)
|
2024-03-17 12:28:28 +03:00
|
|
|
next if BUILTIN_ENCODINGS[name]
|
2024-03-17 13:09:37 +03:00
|
|
|
encodings << n
|
2024-03-17 12:28:28 +03:00
|
|
|
count += 1
|
2008-08-08 11:15:52 +04:00
|
|
|
end
|
2008-01-17 17:56:22 +03:00
|
|
|
else
|
2008-08-08 11:15:52 +04:00
|
|
|
case line
|
|
|
|
when /^\s*rb_enc_register\(\s*"([^"]+)"/
|
|
|
|
count += 1
|
|
|
|
line = nil
|
2013-07-01 18:12:10 +04:00
|
|
|
encodings << $1
|
2008-08-08 11:15:52 +04:00
|
|
|
when /^ENC_REPLICATE\(\s*"([^"]+)"\s*,\s*"([^"]+)"/
|
|
|
|
raise ArgumentError,
|
2021-06-21 05:08:02 +03:00
|
|
|
'%s:%d: ENC_REPLICATE: %s is not defined yet. (replica %s)' %
|
|
|
|
[fn, f.lineno, $2, $1] unless defs[$2.upcase]
|
2008-08-08 11:15:52 +04:00
|
|
|
count += 1
|
|
|
|
when /^ENC_ALIAS\(\s*"([^"]+)"\s*,\s*"([^"]+)"/
|
|
|
|
raise ArgumentError,
|
2021-06-21 05:08:02 +03:00
|
|
|
'%s:%d: ENC_ALIAS: %s is not defined yet. (alias %s)' %
|
|
|
|
[fn, f.lineno, $2, $1] unless defs[$2.upcase]
|
2012-10-06 04:42:14 +04:00
|
|
|
when /^ENC_DUMMY\w*\(\s*"([^"]+)"/
|
2008-08-08 11:15:52 +04:00
|
|
|
count += 1
|
|
|
|
else
|
|
|
|
next
|
|
|
|
end
|
2020-01-20 10:53:31 +03:00
|
|
|
check_duplication(defs, $1, fn, f.lineno)
|
2009-01-13 12:05:29 +03:00
|
|
|
lines << line.sub(/;.*/m, "").chomp + ";" if line
|
2008-01-17 17:56:22 +03:00
|
|
|
end
|
2008-01-13 03:10:00 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-06-21 05:08:02 +03:00
|
|
|
-%>
|
|
|
|
% encodings.each_with_index do |e, i|
|
|
|
|
ENC_DEFINE("<%=e%>");
|
2009-01-13 12:05:29 +03:00
|
|
|
% end
|
2013-07-01 18:12:10 +04:00
|
|
|
% encidx = encodings.size - 1
|
2009-01-13 12:05:29 +03:00
|
|
|
% lines.each do |line|
|
2021-06-21 05:20:13 +03:00
|
|
|
<%=line%><%#=%% %>
|
2009-01-13 12:05:29 +03:00
|
|
|
% end
|
2008-01-13 17:29:12 +03:00
|
|
|
|
2009-01-13 12:05:29 +03:00
|
|
|
#define ENCODING_COUNT <%=count%>
|