summaryrefslogtreecommitdiff
path: root/ext/socket
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-01 06:28:34 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-01 06:28:34 +0000
commit1c0e798d5c7e2c326fc3116a2f8dc259c71eea72 (patch)
tree57f8158d726acbaed6948f7c3d198f74ecda7147 /ext/socket
parent212e90ce3a056aaf5915a11c061a80e893f8e2fd (diff)
* ext/socket/mkconstants.rb: use erb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21225 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket')
-rw-r--r--ext/socket/mkconstants.rb57
1 files changed, 37 insertions, 20 deletions
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index 948eda8be1b..d74310cb34a 100644
--- a/ext/socket/mkconstants.rb
+++ b/ext/socket/mkconstants.rb
@@ -1,4 +1,5 @@
require 'optparse'
+require 'erb'
opt = OptionParser.new
@@ -12,43 +13,59 @@ opt.def_option('-o FILE', 'specify output file') {|filename|
opt_o = filename
}
-$out = ''
-def $out.puts(str="")
- str += "\n" if /\n\z/ !~ str
- self << str
+C_ESC = {
+ "\\" => "\\\\",
+ '"' => '\"',
+ "\n" => '\n',
+}
+
+0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\%03o" % ch }
+0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\%03o" % ch }
+C_ESC_PAT = Regexp.union(*C_ESC.keys)
+
+def c_str(str)
+ '"' + str.gsub(C_ESC_PAT, C_ESC) + '"'
end
opt.parse!
+result = ''
+
# workaround for NetBSD, OpenBSD and etc.
-$out.puts("#define pseudo_AF_FTIP pseudo_AF_RTIP")
+result << "#define pseudo_AF_FTIP pseudo_AF_RTIP\n"
-# skip empty lines and comment lines
-DATA.each_line do |s|
- name, value = s.scan(/\S+/)
- if name && name[0] != ?#
+def each_data
+ DATA.each_line {|s|
+ name, default_value = s.scan(/\S+/)
+ next unless name && name[0] != ?#
if name =~ /\AINADDR_/
define = "sock_define_uconst"
else
define = "sock_define_const"
end
- $out.puts("#ifdef #{name}")
- $out.puts(" #{define}(\"#{name}\", #{name});")
- if value
- $out.puts("#else")
- $out.puts(" #{define}(\"#{name}\", #{value});")
- end
- $out.puts("#endif")
- $out.puts
- end
+ yield define, name, default_value
+ }
end
+result << ERB.new(<<'EOS', nil, '%').result(binding)
+% each_data {|define, name, default_value|
+#ifdef <%=name%>
+ <%=define%>(<%=c_str name%>, <%=name%>);
+% if default_value
+#else
+ <%=define%>(<%=c_str name%>, <%=default_value%>);
+% end
+#endif
+
+% }
+EOS
+
if opt_o
File.open(opt_o, 'w') {|f|
- f << $out
+ f << result
}
else
- $stdout << $out
+ $stdout << result
end
__END__