Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 28ea4fe3740e3a6c19ac96dfadc719c1b0e09cea @hecomi committed Jul 8, 2012
@@ -0,0 +1,6 @@
+test.js
+data/
+*.o
+.lock-wscript
+oll.js
+.deps
@@ -0,0 +1,31 @@
+node-execSync
+==============
+
+はじめに
+--------------
+node.js の child_process 下の exec 関数の同期版です。
+shell のコマンドを引数で指定して実行して、その実行結果を戻り値で返してくれます。
+
+ライセンス
+--------------
+NYSL ライセンスです。
+
+インストール
+--------------
+git clone して次のコマンドを実行して下さい。
+
+ $ node-waf configure build
+
+wscript ファイル内の gcc のバージョンは適宜変更して下さい。デフォルトでは、g++-4.8 でコンパイルしています。
+
+使い方
+--------------
+次のように使います。
+
+ var exec = require('./build/Release/shell').execSync;
+ console.log(exec('ls -all'));
+
+詳細
+--------------
+その他詳細は http://d.hatena.ne.jp/hecomi/ をご参照下さい。
+
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,49 @@
+AR = '/usr/bin/ar'
+ARFLAGS = 'rcs'
+CCFLAGS = ['-g']
+CCFLAGS_MACBUNDLE = ['-fPIC']
+CCFLAGS_NODE = ['-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64']
+CC_VERSION = ('4', '2', '1')
+COMPILER_CXX = 'g++'
+CPP = '/usr/bin/cpp'
+CPPFLAGS_NODE = ['-D_GNU_SOURCE']
+CPPPATH_NODE = '/Users/hecomi/.nave/installed/0.6.9/include/node'
+CPPPATH_ST = '-I%s'
+CXX = 'g++-4.8'
+CXXDEFINES_ST = '-D%s'
+CXXFLAGS = '-std=c++0x'
+CXXFLAGS_DEBUG = ['-g']
+CXXFLAGS_NODE = ['-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64']
+CXXFLAGS_RELEASE = ['-O2']
+CXXLNK_SRC_F = ''
+CXXLNK_TGT_F = ['-o', '']
+CXX_NAME = 'gcc'
+CXX_SRC_F = ''
+CXX_TGT_F = ['-c', '-o', '']
+DEST_CPU = 'x86_64'
+DEST_OS = 'darwin'
+FULLSTATIC_MARKER = '-static'
+LIB = ['boost_iostreams']
+LIBDIR = '/Users/hecomi/.node_libraries'
+LIBPATH_NODE = '/Users/hecomi/.nave/installed/0.6.9/lib'
+LIBPATH_ST = '-L%s'
+LIB_ST = '-l%s'
+LINKFLAGS_MACBUNDLE = ['-bundle', '-undefined', 'dynamic_lookup']
+LINK_CXX = ['/usr/bin/g++']
+NODE_PATH = '/Users/hecomi/.node_libraries'
+PREFIX = '/usr/local'
+PREFIX_NODE = '/Users/hecomi/.nave/installed/0.6.9'
+RANLIB = '/usr/bin/ranlib'
+RPATH_ST = '-Wl,-rpath,%s'
+SHLIB_MARKER = ''
+SONAME_ST = ''
+STATICLIBPATH_ST = '-L%s'
+STATICLIB_MARKER = ''
+STATICLIB_ST = '-l%s'
+macbundle_PATTERN = '%s.bundle'
+program_PATTERN = '%s'
+shlib_CXXFLAGS = ['-fPIC', '-compatibility_version', '1', '-current_version', '1']
+shlib_LINKFLAGS = ['-dynamiclib']
+shlib_PATTERN = 'lib%s.dylib'
+staticlib_LINKFLAGS = []
+staticlib_PATTERN = 'lib%s.a'
@@ -0,0 +1,2 @@
+version = 0x105016
+tools = [{'tool': 'ar', 'tooldir': None, 'funs': None}, {'tool': 'cxx', 'tooldir': None, 'funs': None}, {'tool': 'gxx', 'tooldir': None, 'funs': None}, {'tool': 'compiler_cxx', 'tooldir': None, 'funs': None}, {'tool': 'osx', 'tooldir': None, 'funs': None}, {'tool': 'node_addon', 'tooldir': None, 'funs': None}]
@@ -0,0 +1,36 @@
+# project noname (0.0.1) configured on Sun Jul 8 15:08:25 2012 by
+# waf 1.5.16 (abi 7, python 20701f0 on darwin)
+# using /Users/hecomi/.nave/installed/0.6.9/bin/node-waf configure build
+#
+
+----------------------------------------
+Checking for program g++ or c++
+ find program=['g++', 'c++'] paths=[] var='CXX'
+ -> '/usr/bin/g++'
+
+----------------------------------------
+Checking for program cpp
+ find program=['cpp'] paths=[] var='CPP'
+ -> '/usr/bin/cpp'
+
+----------------------------------------
+Checking for program ar
+ find program=['ar'] paths=[] var='AR'
+ -> '/usr/bin/ar'
+
+----------------------------------------
+Checking for program ranlib
+ find program=['ranlib'] paths=[] var='RANLIB'
+ -> '/usr/bin/ranlib'
+
+----------------------------------------
+Checking for g++
+ok
+
+----------------------------------------
+Checking for node path
+not found
+
+----------------------------------------
+Checking for node prefix
+ok /Users/hecomi/.nave/installed/0.6.9
@@ -0,0 +1,4 @@
+var exec = require('./build/Release/shell').execSync;
+
+console.log(exec('ls -all'));
+
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <string>
+#include <cstdlib>
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/device/file_descriptor.hpp>
+#include <node.h>
+
+using namespace v8;
+namespace io = boost::iostreams;
+typedef io::stream<io::file_descriptor_source> boost_stream;
+
+Handle<Value> execSync(const Arguments& args)
+{
+ HandleScope scope;
+
+ // 引数が文字列かどうかチェック
+ if (!args[0]->IsString()) {
+ Local<String> msg = String::New("[execSync] Error! Argument of 'execSync' must be String.");
+ ThrowException(Exception::TypeError(msg));
+ return scope.Close(Undefined());
+ }
+ String::Utf8Value command(args[0]);
+
+ FILE *fp = popen(*command, "r");
+ if (fp == nullptr) {
+ Local<String> msg = String::New("[execSync] Error! 'popen' failed.");
+ ThrowException(Exception::TypeError(msg));
+ return scope.Close(Undefined());
+ }
+
+ boost_stream bs(fileno(fp), io::close_handle);
+ std::string line, result;
+ while (std::getline(bs, line)) {
+ result += line + "\n";
+ }
+
+ return scope.Close(String::New(result.c_str()));
+}
+
+/**
+ * Node.js の世界へいってらっしゃい
+ */
+void init(Handle<Object> target) {
+ target->Set(
+ String::NewSymbol("execSync"),
+ FunctionTemplate::New(execSync)->GetFunction()
+ );
+}
+
+NODE_MODULE(shell, init)
+
19 wscript
@@ -0,0 +1,19 @@
+srcdir = '.'
+blddir = 'build'
+VERSION = '0.0.1'
+
+def set_options(opt):
+ opt.tool_options('compiler_cxx')
+
+def configure(conf):
+ conf.check_tool('compiler_cxx')
+ conf.check_tool('node_addon')
+ conf.env['CXX'] = 'g++-4.8'
+ conf.env['CXXFLAGS'] = '-std=c++0x'
+ conf.env['LIB'] = ['boost_iostreams']
+
+def build(bld):
+ obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
+ obj.target = 'shell'
+ obj.source = 'export_to_nodejs.cpp'
+

0 comments on commit 28ea4fe

Please sign in to comment.