From: nobu.nakada@... Date: 2002-02-07T11:38:10+09:00 Subject: [ruby-dev:15861] syscall なかだです。 syscallがいまいち使いようがないので、こんなものを作ってみました。 Index: Makefile.in =================================================================== RCS file: /cvs/ruby/src/ruby/Makefile.in,v retrieving revision 1.30 diff -u -2 -p -r1.30 Makefile.in --- Makefile.in 2002/01/11 15:36:11 1.30 +++ Makefile.in 2002/02/07 02:17:00 @@ -84,5 +84,5 @@ OBJS = array.@OBJEXT@ \ $(MISSING) -all: miniruby$(EXEEXT) @PREP@ rbconfig.rb $(LIBRUBY) +all: miniruby$(EXEEXT) @PREP@ rbconfig.rb $(LIBRUBY) @syscall_rb@ @@MINIRUBY@ -Cext extmk.rb @EXTSTATIC@ @@ -144,4 +144,7 @@ fake.rb: miniruby$(EXEEXT) end \ ' > $@ + +syscall.rb: mksyscall.rb miniruby$(EXEEXT) + @./miniruby$(EXEEXT) $< $@ config.status: $(srcdir)/configure Index: configure.in =================================================================== RCS file: /cvs/ruby/src/ruby/configure.in,v retrieving revision 1.109 diff -u -2 -p -r1.109 configure.in --- configure.in 2002/02/06 08:23:46 1.109 +++ configure.in 2002/02/07 01:27:42 @@ -303,4 +303,6 @@ AC_CHECK_FUNCS(fmod killpg drand48 rando dlopen sigprocmask sigaction _setjmp setsid telldir seekdir fchmod\ mktime timegm) +test x$ac_cv_func_syscall = xyes && syscall_rb=syscall.rb +AC_SUBST(syscall_rb) AC_STRUCT_TIMEZONE AC_CACHE_CHECK(for struct tm.tm_gmtoff, rb_cv_member_struct_tm_tm_gmtoff, Index: instruby.rb =================================================================== RCS file: /cvs/ruby/src/ruby/instruby.rb,v retrieving revision 1.19 diff -u -2 -p -r1.19 instruby.rb --- instruby.rb 2001/04/30 17:55:56 1.19 +++ instruby.rb 2002/02/07 02:17:45 @@ -100,3 +100,4 @@ Dir.chdir wdir File.install "config.h", archlibdir, 0644, true File.install "rbconfig.rb", archlibdir, 0644, true +File.install "syscall.rb", archlibdir, 0644, true if File.exist? "syscall.rb" # vi:set sw=2: Index: mksyscall.rb =================================================================== RCS file: mksyscall.rb diff -N mksyscall.rb --- /dev/null Thu Jan 1 09:00:00 1970 +++ mksyscall.rb Thu Feb 7 11:31:08 2002 @@ -0,0 +1,114 @@ +#!./miniruby -I. + +require 'mkmf' + +class Syscall < Hash + CONFTEST = "conftest.c" + + def initialize + headers = {} + + open(CONFTEST, "w") do |f| + f.print < +#else +!!!NotImplemented +#endif +SRC + end + IO.foreach("|"+format(CPP, $CPPFLAGS, $CFLAGS, "")) do |l| + case l + when /^\s*\#\s*(?:line)?\s+\d+\s+"((?!conftest\.c).+)"/ + headers[$1] = true + when /^!!!NotImplemented/ + raise NotImplementedError + else + l.scan(/\bSYS_\w+/) {|s| self[s] = true} + end + end + + if empty? + raise NotImplementedError if headers.empty? + headers.each_key do |f| + IO.foreach(f) do |l| + self[$1] = true if /^\s*\#\s*define\s+(SYS_\w+)/ =~ l + end + end + raise NotImplementedError if empty? + end + + return unless find {|n, s| s == true} + + open(CONFTEST, "w") do |f| + f.print "#include \n" + each_key {|s| f.print "\"#{s}\"=#{s}\n"} + end + IO.foreach("|"+format(CPP, $CPPFLAGS, $CFLAGS, "")) do |l| + if /^\"(SYS_\w+)\"=\s*(.*)/ =~ l + s, n = $1, $2 + self[s] = n.to_i if /^(?:0x[0-9a-f]+|0[0-7]+|\d+)$/ =~ n + end + end + + src = "" + each do |s, n| + src << "printf(\"#{s}=%d\n\", #{s});\n" if n == true + end + if !src.empty? and try_link0("#include \n\nint main(){\n"+src+"}\n") + IO.foreach("|./conftest") do |l| + if /^(SYS_\w+)=(\d+)/ =~ l + s, n = $1, $2 + self[s] = n.to_i + end + end + end + ensure + File.unlink(*Dir["conftest*"]) + end + + def write_to(f) + syscalls = [] + each {|s, n| syscalls << [n, s.sub(/^SYS_/, ""), s]} + syscalls.sort! + f.print "module Syscall\n" + f.print " Platform = %[", `uname -srm`.chomp, "].each {|s| s.freeze}.freeze\n" + f.print "\n NR = {}\n" + syscalls.each do |n, t, s| + f.print " NR[:#{t}] = #{s} = #{n}\n" + end + f.print "\n module_function\n" + syscalls.each do |n, t, s| + f.print " def #{t}(*args) syscall(#{n}, *args) end\n" + end + f.print "end\n" + end +end + +case ARGV.size +when 0, 1 +else + STDERR.print "usage: #{$0} [output]\n" + exit 1 +end + +begin + syscalls = Syscall.new +rescue NotImplementedError + STDERR.print "#{$0}: syscall.h or syscall number not found.\n" + exit(1) +end + +if output = ARGV.shift + open(output, "w") {|f| syscalls.write_to(f)} +else + syscalls.write_to(STDOUT) +end + +# Local Variables: +# mode : ruby +# tab-width : 8 +# ruby-indent-level : 2 +# End: +# vi: sw=2 ts=8 -- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦