summaryrefslogtreecommitdiff
path: root/ext/socket/mkconstants.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ext/socket/mkconstants.rb')
-rw-r--r--ext/socket/mkconstants.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/ext/socket/mkconstants.rb b/ext/socket/mkconstants.rb
index 6c6be0f6e2..09f0f030c7 100644
--- a/ext/socket/mkconstants.rb
+++ b/ext/socket/mkconstants.rb
@@ -93,6 +93,43 @@ ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_name_to_int(str_var, len_var,
}
EOS
+MAYBE_ALIAS = [
+ ["AF_UNIX", "AF_LOCAL"]
+]
+
+def each_alias(pat)
+ h = {}
+ each_name(pat) {|name|
+ h[name] = [name]
+ }
+ MAYBE_ALIAS.each {|names|
+ a = []
+ names.each {|n|
+ a << n if h.delete n
+ }
+ h[a.first] = a
+ }
+ h.each_value {|names|
+ yield names
+ }
+end
+
+ERB.new(<<'EOS', nil, '%').def_method(Object, "gen_int_to_name(int_var, pat)")
+ switch (<%=int_var%>) {
+% each_alias(pat) {|names|
+% names.each_with_index {|n, i|
+% cond = ["defined(#{n})"]
+% (0...i).each {|j| cond << "(!defined(#{names[j]}) || #{n} != #{names[j]})" }
+#if <%=cond.join(" && ")%>
+ case <%=n%>: return <%=c_str n%>;
+#endif
+% }
+% }
+ default:
+ return NULL;
+ }
+EOS
+
result << ERB.new(<<'EOS', nil, '%').result(binding)
static void
init_constants(VALUE mConst)
@@ -127,6 +164,12 @@ socktype_to_int(char *str, int len)
<%= gen_name_to_int("str", "len", /\ASOCK_/) %>
}
+static char *
+family_to_str(int val)
+{
+<%= gen_int_to_name("val", /\AAF_/) %>
+}
+
EOS
if opt_o