funky-cli 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +1 -0
- data/build_config.rb +50 -2
- data/exe/i386-apple-darwin14 +0 -0
- data/exe/i686-pc-linux-gnu +0 -0
- data/exe/i686-w64-mingw32.exe +0 -0
- data/exe/x86_64-apple-darwin14 +0 -0
- data/exe/x86_64-pc-linux-gnu +0 -0
- data/exe/x86_64-w64-mingw32.exe +0 -0
- data/mrbgem.rake +45 -1
- data/mrblib/funky-cli/helper.rb +13 -0
- data/mrblib/funky-cli/mruby.rb +24 -0
- data/mrblib/funky-cli/setup.rb +140 -0
- data/mrblib/funky-cli/version.rb +1 -1
- data/mrblib/funky-cli.rb +13 -2
- data/mrblib/util.rb +10 -0
- data/src/funky_cli_gem.c +316 -0
- data/src/mirb.c +415 -0
- data/src/mruby.c +244 -0
- data/tools/funky-cli/funky-cli.c +7 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60569a154fee04e94e0a645a553e1b11dd6f6acc
|
4
|
+
data.tar.gz: 2d2777172480c008f8646076e39d4cbde708e3f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3b9ffca21d7cc07a00417a205c0868ac2abdea28a51e411cb7615a5902dbcaccd8199e9596d51d3c0405e1b3f3d616bbd197aeb7c76746f51a9b5284c238baa
|
7
|
+
data.tar.gz: cdcd0fd7e955b299e794576ba1d261363d5a7654994a5a7d6065f271bd5527147f8cf7aece9f732cddebec12e22d89482aa47b949c7523864758afef1b1e63a7
|
data/Rakefile
CHANGED
data/build_config.rb
CHANGED
@@ -1,5 +1,49 @@
|
|
1
1
|
def gem_config(conf)
|
2
|
-
|
2
|
+
conf.gem :core => "mruby-sprintf"
|
3
|
+
conf.gem :mgem => 'mruby-mtest'
|
4
|
+
conf.gem :core => "mruby-print"
|
5
|
+
conf.gem :core => "mruby-math"
|
6
|
+
conf.gem :core => "mruby-time"
|
7
|
+
conf.gem :core => "mruby-struct"
|
8
|
+
conf.gem :core => "mruby-enum-ext"
|
9
|
+
conf.gem :core => "mruby-string-ext"
|
10
|
+
conf.gem :core => "mruby-numeric-ext"
|
11
|
+
conf.gem :core => "mruby-array-ext"
|
12
|
+
conf.gem :core => "mruby-hash-ext"
|
13
|
+
conf.gem :core => "mruby-range-ext"
|
14
|
+
conf.gem :core => "mruby-proc-ext"
|
15
|
+
conf.gem :core => "mruby-symbol-ext"
|
16
|
+
conf.gem :core => "mruby-random"
|
17
|
+
conf.gem :core => "mruby-object-ext"
|
18
|
+
conf.gem :core => "mruby-objectspace"
|
19
|
+
conf.gem :core => "mruby-fiber"
|
20
|
+
conf.gem :core => "mruby-enumerator"
|
21
|
+
conf.gem :core => "mruby-enum-lazy"
|
22
|
+
conf.gem :core => "mruby-toplevel-ext"
|
23
|
+
conf.gem :core => "mruby-kernel-ext"
|
24
|
+
conf.gem :core => "mruby-eval"
|
25
|
+
conf.gem :core => "mruby-exit"
|
26
|
+
|
27
|
+
conf.gem :github => "scalone/mruby-hs-regexp"
|
28
|
+
conf.gem :mgem => "mruby-io"
|
29
|
+
conf.gem :mgem => "mruby-dir"
|
30
|
+
conf.gem :mgem => "mruby-tempfile"
|
31
|
+
conf.gem :github => "iij/mruby-require"
|
32
|
+
conf.gem :mgem => "mruby-socket"
|
33
|
+
conf.gem :mgem => "mruby-pack"
|
34
|
+
conf.gem :mgem => "mruby-polarssl"
|
35
|
+
#conf.gem :github => "sadasant/mruby-qrcode", :check_sum => "83205181508fe77f9fa7e556002334e439bcbc7b"
|
36
|
+
conf.gem :github => "scalone/mruby-miniz"
|
37
|
+
conf.gem :mgem => "mruby-sleep"
|
38
|
+
conf.gem :mgem => "mruby-msgpack"
|
39
|
+
conf.gem :mgem => "mruby-json"
|
40
|
+
conf.gem :github => "scalone/mruby-context"
|
41
|
+
conf.gem :github => "scalone/mruby-ripemd"
|
42
|
+
conf.gem :github => "scalone/mruby-hmac"
|
43
|
+
conf.gem :mgem => "mruby-sha2"
|
44
|
+
conf.gem :mgem => "mruby-sha1"
|
45
|
+
conf.gem :mgem => "mruby-base58"
|
46
|
+
conf.gem :mgem => "mruby-bignum"
|
3
47
|
|
4
48
|
# be sure to include this gem (the cli app)
|
5
49
|
conf.gem File.expand_path(File.dirname(__FILE__))
|
@@ -12,7 +56,10 @@ MRuby::Build.new do |conf|
|
|
12
56
|
conf.enable_debug
|
13
57
|
conf.enable_test
|
14
58
|
|
15
|
-
|
59
|
+
conf.gem :mgem => 'mruby-mtest'
|
60
|
+
conf.gem :core => "mruby-print"
|
61
|
+
|
62
|
+
#gem_config(conf)
|
16
63
|
end
|
17
64
|
|
18
65
|
MRuby::Build.new('x86_64-pc-linux-gnu') do |conf|
|
@@ -92,3 +139,4 @@ MRuby::CrossBuild.new('i686-w64-mingw32') do |conf|
|
|
92
139
|
|
93
140
|
gem_config(conf)
|
94
141
|
end
|
142
|
+
|
data/exe/i386-apple-darwin14
CHANGED
Binary file
|
data/exe/i686-pc-linux-gnu
CHANGED
Binary file
|
data/exe/i686-w64-mingw32.exe
CHANGED
Binary file
|
data/exe/x86_64-apple-darwin14
CHANGED
Binary file
|
data/exe/x86_64-pc-linux-gnu
CHANGED
Binary file
|
data/exe/x86_64-w64-mingw32.exe
CHANGED
Binary file
|
data/mrbgem.rake
CHANGED
@@ -4,6 +4,50 @@ MRuby::Gem::Specification.new('funky-cli') do |spec|
|
|
4
4
|
spec.summary = 'funky-cli'
|
5
5
|
spec.bins = ['funky-cli']
|
6
6
|
|
7
|
-
spec.add_dependency
|
7
|
+
spec.add_dependency "mruby-sprintf", :core => "mruby-sprintf"
|
8
8
|
spec.add_dependency 'mruby-mtest', :mgem => 'mruby-mtest'
|
9
|
+
spec.add_dependency "mruby-print", :core => "mruby-print"
|
10
|
+
spec.add_dependency "mruby-math", :core => "mruby-math"
|
11
|
+
spec.add_dependency "mruby-time", :core => "mruby-time"
|
12
|
+
spec.add_dependency "mruby-struct", :core => "mruby-struct"
|
13
|
+
spec.add_dependency "mruby-enum-ext", :core => "mruby-enum-ext"
|
14
|
+
spec.add_dependency "mruby-string-ext", :core => "mruby-string-ext"
|
15
|
+
spec.add_dependency "mruby-numeric-ext", :core => "mruby-numeric-ext"
|
16
|
+
spec.add_dependency "mruby-array-ext", :core => "mruby-array-ext"
|
17
|
+
spec.add_dependency "mruby-hash-ext", :core => "mruby-hash-ext"
|
18
|
+
spec.add_dependency "mruby-range-ext", :core => "mruby-range-ext"
|
19
|
+
spec.add_dependency "mruby-proc-ext", :core => "mruby-proc-ext"
|
20
|
+
spec.add_dependency "mruby-symbol-ext", :core => "mruby-symbol-ext"
|
21
|
+
spec.add_dependency "mruby-random", :core => "mruby-random"
|
22
|
+
spec.add_dependency "mruby-object-ext", :core => "mruby-object-ext"
|
23
|
+
spec.add_dependency "mruby-objectspace", :core => "mruby-objectspace"
|
24
|
+
spec.add_dependency "mruby-fiber", :core => "mruby-fiber"
|
25
|
+
spec.add_dependency "mruby-enumerator", :core => "mruby-enumerator"
|
26
|
+
spec.add_dependency "mruby-enum-lazy", :core => "mruby-enum-lazy"
|
27
|
+
spec.add_dependency "mruby-toplevel-ext", :core => "mruby-toplevel-ext"
|
28
|
+
spec.add_dependency "mruby-kernel-ext", :core => "mruby-kernel-ext"
|
29
|
+
spec.add_dependency "mruby-eval", :core => "mruby-eval"
|
30
|
+
spec.add_dependency "mruby-exit", :core => "mruby-exit"
|
31
|
+
|
32
|
+
spec.add_dependency "mruby-hs-regexp", :github => "scalone/mruby-hs-regexp"
|
33
|
+
spec.add_dependency "mruby-io", :mgem => "mruby-io"
|
34
|
+
spec.add_dependency "mruby-dir", :mgem => "mruby-dir"
|
35
|
+
spec.add_dependency "mruby-tempfile", :mgem => "mruby-tempfile"
|
36
|
+
spec.add_dependency "mruby-require", :github => "iij/mruby-require"
|
37
|
+
spec.add_dependency "mruby-socket", :mgem => "mruby-socket"
|
38
|
+
spec.add_dependency "mruby-pack", :mgem => "mruby-pack"
|
39
|
+
spec.add_dependency "mruby-polarssl", :mgem => "mruby-polarssl"
|
40
|
+
#spec.add_dependency "mruby-qrcode", :github => "sadasant/mruby-qrcode", :check_sum => "83205181508fe77f9fa7e556002334e439bcbc7b"
|
41
|
+
spec.add_dependency "mruby-miniz", :github => "scalone/mruby-miniz"
|
42
|
+
spec.add_dependency "mruby-sleep", :mgem => "mruby-sleep"
|
43
|
+
spec.add_dependency "mruby-msgpack", :mgem => "mruby-msgpack"
|
44
|
+
spec.add_dependency "mruby-json", :mgem => "mruby-json"
|
45
|
+
spec.add_dependency "mruby-context", :github => "scalone/mruby-context"
|
46
|
+
spec.add_dependency "mruby-ripemd", :github => "scalone/mruby-ripemd"
|
47
|
+
spec.add_dependency "mruby-hmac", :github => "scalone/mruby-hmac"
|
48
|
+
spec.add_dependency "mruby-sha2", :mgem => "mruby-sha2"
|
49
|
+
spec.add_dependency "mruby-sha1", :mgem => "mruby-sha1"
|
50
|
+
spec.add_dependency "mruby-base58", :mgem => "mruby-base58"
|
51
|
+
spec.add_dependency "mruby-bignum", :mgem => "mruby-bignum"
|
9
52
|
end
|
53
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module FunkyCli
|
2
|
+
class Helper
|
3
|
+
def self.run
|
4
|
+
puts "funky-cli [switches] [arguments]"
|
5
|
+
puts "funky-cli help : show this message"
|
6
|
+
puts "funky-cli new <name> : create app"
|
7
|
+
puts "funky-cli compile -o<out> <file1> <file2> : compile ruby to mrb"
|
8
|
+
puts "funky-cli console : mirb console"
|
9
|
+
puts "funky-cli run -b <out>, <file1> : run ruby or binary file"
|
10
|
+
puts "funky-cli version : print funky-cli version"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module FunkyCli
|
2
|
+
class Mruby
|
3
|
+
def self.run(*args)
|
4
|
+
FunkyCli.mruby(args.size + 1, args.insert(0, "mruby"))
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.compile(*args)
|
8
|
+
FunkyCli.mrbc(args.size + 1, args.insert(0, "mrbc"))
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.console(*args)
|
12
|
+
path = File.expand_path("./")
|
13
|
+
app = File.basename(path)
|
14
|
+
if File.exists?(File.join(path, "out", app, "main.mrb"))
|
15
|
+
Dir.chdir("out")
|
16
|
+
$LOAD_PATH.unshift "./#{app}"
|
17
|
+
require "da_funk"
|
18
|
+
require "main"
|
19
|
+
end
|
20
|
+
FunkyCli.mirb
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,140 @@
|
|
1
|
+
module FunkyCli
|
2
|
+
class Setup
|
3
|
+
def self.run(name)
|
4
|
+
Dir.mkdir(name) unless Dir.exist?(name)
|
5
|
+
Dir.chdir(name) do
|
6
|
+
write_file(".gitignore", gitignore)
|
7
|
+
write_file("Gemfile", gemfile)
|
8
|
+
write_file("Rakefile", rakefile)
|
9
|
+
create_dir_p("lib")
|
10
|
+
write_file("lib/main.rb", main)
|
11
|
+
create_dir_p("test/unit")
|
12
|
+
create_dir_p("test/integration")
|
13
|
+
write_file("test/test_helper.rb", helper)
|
14
|
+
write_file("test/unit/#{name}_test.rb", unit(name))
|
15
|
+
write_file("test/integration/#{name}_test.rb", integration)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.gitignore
|
20
|
+
<<IGNORE
|
21
|
+
out/
|
22
|
+
IGNORE
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.create_dir_p(dir)
|
26
|
+
dir.split("/").inject("") do |parent, base|
|
27
|
+
new_dir =
|
28
|
+
if parent == ""
|
29
|
+
base
|
30
|
+
else
|
31
|
+
"#{parent}/#{base}"
|
32
|
+
end
|
33
|
+
|
34
|
+
create_dir(new_dir)
|
35
|
+
|
36
|
+
new_dir
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.create_dir(dir)
|
41
|
+
if Dir.exist?(dir)
|
42
|
+
puts " skip #{dir}"
|
43
|
+
else
|
44
|
+
puts " create #{dir}/"
|
45
|
+
Dir.mkdir(dir)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.write_file(file, contents)
|
50
|
+
puts " create #{file}"
|
51
|
+
File.open(file, 'w') {|file| file.puts contents }
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.gemfile
|
55
|
+
<<GEMFILE
|
56
|
+
source 'https://rubygems.org'
|
57
|
+
|
58
|
+
gem 'da_funk'
|
59
|
+
gem 'funky-cli'
|
60
|
+
gem 'rake'
|
61
|
+
GEMFILE
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.rakefile
|
65
|
+
<<RAKEFILE
|
66
|
+
#!/usr/bin/env rake
|
67
|
+
|
68
|
+
require 'rake'
|
69
|
+
require 'fileutils'
|
70
|
+
require 'bundler/setup'
|
71
|
+
|
72
|
+
Bundler.require(:default)
|
73
|
+
|
74
|
+
DaFunk::RakeTask.new do |t|
|
75
|
+
t.mrbc = "funky-cli compile"
|
76
|
+
t.mruby = "funky-cli run -b"
|
77
|
+
end
|
78
|
+
RAKEFILE
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.main
|
82
|
+
<<MAIN
|
83
|
+
class Main < Device
|
84
|
+
def self.call
|
85
|
+
puts "Hello World!"
|
86
|
+
getc
|
87
|
+
true
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.foo
|
91
|
+
:foo
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.version
|
95
|
+
"0.0.1"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
MAIN
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.helper
|
102
|
+
<<HELPER
|
103
|
+
ROOT_PATH = File.expand_path("../")
|
104
|
+
APP_NAME = File.basename(ROOT_PATH)
|
105
|
+
|
106
|
+
$LOAD_PATH.unshift "./\#{APP_NAME}"
|
107
|
+
require 'da_funk'
|
108
|
+
|
109
|
+
DaFunk::Test.configure do |t|
|
110
|
+
t.root_path = ROOT_PATH
|
111
|
+
t.name = APP_NAME
|
112
|
+
end
|
113
|
+
HELPER
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.integration
|
117
|
+
<<INTEGRATION
|
118
|
+
class MainTest < DaFunk::Test.case
|
119
|
+
def test_true
|
120
|
+
assert Main.call
|
121
|
+
end
|
122
|
+
end
|
123
|
+
INTEGRATION
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.unit(name)
|
127
|
+
<<UNIT
|
128
|
+
class #{Util.camelize(name)}Test < DaFunk::Test.case
|
129
|
+
def test_foo0
|
130
|
+
assert_equal :foo, Main.foo
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_foo1
|
134
|
+
assert_equal :foo, Main.foo
|
135
|
+
end
|
136
|
+
end
|
137
|
+
UNIT
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
data/mrblib/funky-cli/version.rb
CHANGED
data/mrblib/funky-cli.rb
CHANGED
@@ -1,7 +1,18 @@
|
|
1
1
|
def __main__(argv)
|
2
|
-
|
2
|
+
case argv[1]
|
3
|
+
when "version"
|
3
4
|
puts "v#{FunkyCli::VERSION}"
|
5
|
+
when "new"
|
6
|
+
FunkyCli::Setup.run(argv[2])
|
7
|
+
when "help"
|
8
|
+
FunkyCli::Helper.run
|
9
|
+
when "compile"
|
10
|
+
FunkyCli::Mruby.compile(*argv[2..-1])
|
11
|
+
when "run"
|
12
|
+
FunkyCli::Mruby.run(*argv[2..-1])
|
13
|
+
when "console"
|
14
|
+
FunkyCli::Mruby.console
|
4
15
|
else
|
5
|
-
|
16
|
+
FunkyCli::Helper.run
|
6
17
|
end
|
7
18
|
end
|
data/mrblib/util.rb
ADDED
data/src/funky_cli_gem.c
ADDED
@@ -0,0 +1,316 @@
|
|
1
|
+
#include <stdlib.h>
|
2
|
+
#include <stdio.h>
|
3
|
+
#include <string.h>
|
4
|
+
|
5
|
+
#include "mruby.h"
|
6
|
+
#include "mruby/compile.h"
|
7
|
+
#include "mruby/value.h"
|
8
|
+
#include "mruby/array.h"
|
9
|
+
#include "mruby/string.h"
|
10
|
+
#include "mruby/hash.h"
|
11
|
+
#include "mruby/dump.h"
|
12
|
+
#include "mruby/proc.h"
|
13
|
+
|
14
|
+
#define DONE mrb_gc_arena_restore(mrb, 0)
|
15
|
+
|
16
|
+
#define RITEBIN_EXT ".mrb"
|
17
|
+
#define C_EXT ".c"
|
18
|
+
|
19
|
+
struct mrbc_args {
|
20
|
+
int argc;
|
21
|
+
char **argv;
|
22
|
+
int idx;
|
23
|
+
const char *prog;
|
24
|
+
const char *outfile;
|
25
|
+
const char *initname;
|
26
|
+
mrb_bool check_syntax : 1;
|
27
|
+
mrb_bool verbose : 1;
|
28
|
+
unsigned int flags : 4;
|
29
|
+
};
|
30
|
+
|
31
|
+
static char *
|
32
|
+
get_outfilename(mrb_state *mrb, char *infile, const char *ext)
|
33
|
+
{
|
34
|
+
size_t infilelen;
|
35
|
+
size_t extlen;
|
36
|
+
char *outfile;
|
37
|
+
char *p;
|
38
|
+
|
39
|
+
infilelen = strlen(infile);
|
40
|
+
extlen = strlen(ext);
|
41
|
+
outfile = (char*)mrb_malloc(mrb, infilelen + extlen + 1);
|
42
|
+
memcpy(outfile, infile, infilelen + 1);
|
43
|
+
if (*ext) {
|
44
|
+
if ((p = strrchr(outfile, '.')) == NULL)
|
45
|
+
p = outfile + infilelen;
|
46
|
+
memcpy(p, ext, extlen + 1);
|
47
|
+
}
|
48
|
+
|
49
|
+
return outfile;
|
50
|
+
}
|
51
|
+
|
52
|
+
static int
|
53
|
+
parse_args(mrb_state *mrb, int argc, char **argv, struct mrbc_args *args)
|
54
|
+
{
|
55
|
+
char *outfile = NULL;
|
56
|
+
static const struct mrbc_args args_zero = { 0 };
|
57
|
+
int i;
|
58
|
+
|
59
|
+
*args = args_zero;
|
60
|
+
args->argc = argc;
|
61
|
+
args->argv = argv;
|
62
|
+
args->prog = argv[0];
|
63
|
+
|
64
|
+
for (i=1; i<argc; i++) {
|
65
|
+
if (argv[i][0] == '-') {
|
66
|
+
/*fprintf(stderr, "[%d]", (argv[i])[1]);*/
|
67
|
+
switch ((argv[i])[1]) {
|
68
|
+
case 'o':
|
69
|
+
if (args->outfile) {
|
70
|
+
fprintf(stderr, "%s: an output file is already specified. (%s)\n",
|
71
|
+
args->prog, outfile);
|
72
|
+
return -1;
|
73
|
+
}
|
74
|
+
if (argv[i][2] == '\0' && argv[i+1]) {
|
75
|
+
i++;
|
76
|
+
args->outfile = get_outfilename(mrb, argv[i], "");
|
77
|
+
}
|
78
|
+
else {
|
79
|
+
args->outfile = get_outfilename(mrb, argv[i] + 2, "");
|
80
|
+
}
|
81
|
+
break;
|
82
|
+
case 'B':
|
83
|
+
if (argv[i][2] == '\0' && argv[i+1]) {
|
84
|
+
i++;
|
85
|
+
args->initname = argv[i];
|
86
|
+
}
|
87
|
+
else {
|
88
|
+
args->initname = argv[i]+2;
|
89
|
+
}
|
90
|
+
if (*args->initname == '\0') {
|
91
|
+
fprintf(stderr, "%s: function name is not specified.\n", args->prog);
|
92
|
+
return -1;
|
93
|
+
}
|
94
|
+
break;
|
95
|
+
case 'c':
|
96
|
+
args->check_syntax = TRUE;
|
97
|
+
break;
|
98
|
+
case 'v':
|
99
|
+
if (!args->verbose) mrb_show_version(mrb);
|
100
|
+
args->verbose = TRUE;
|
101
|
+
break;
|
102
|
+
case 'g':
|
103
|
+
args->flags |= DUMP_DEBUG_INFO;
|
104
|
+
break;
|
105
|
+
case 'E':
|
106
|
+
args->flags = DUMP_ENDIAN_BIG | (args->flags & ~DUMP_ENDIAN_MASK);
|
107
|
+
break;
|
108
|
+
case 'e':
|
109
|
+
args->flags = DUMP_ENDIAN_LIL | (args->flags & ~DUMP_ENDIAN_MASK);
|
110
|
+
break;
|
111
|
+
case 'h':
|
112
|
+
return -1;
|
113
|
+
case '-':
|
114
|
+
if (argv[i][1] == '\n') {
|
115
|
+
return i;
|
116
|
+
}
|
117
|
+
if (strcmp(argv[i] + 2, "version") == 0) {
|
118
|
+
mrb_show_version(mrb);
|
119
|
+
exit(EXIT_SUCCESS);
|
120
|
+
}
|
121
|
+
else if (strcmp(argv[i] + 2, "verbose") == 0) {
|
122
|
+
args->verbose = TRUE;
|
123
|
+
break;
|
124
|
+
}
|
125
|
+
else if (strcmp(argv[i] + 2, "copyright") == 0) {
|
126
|
+
mrb_show_copyright(mrb);
|
127
|
+
exit(EXIT_SUCCESS);
|
128
|
+
}
|
129
|
+
return -1;
|
130
|
+
default:
|
131
|
+
return i;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
else {
|
135
|
+
break;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
if (args->verbose && args->initname && (args->flags & DUMP_ENDIAN_MASK) == 0) {
|
139
|
+
fprintf(stderr, "%s: generating %s endian C file. specify -e/-E for cross compiling.\n",
|
140
|
+
args->prog, bigendian_p() ? "big" : "little");
|
141
|
+
}
|
142
|
+
return i;
|
143
|
+
}
|
144
|
+
|
145
|
+
static int
|
146
|
+
partial_hook(struct mrb_parser_state *p)
|
147
|
+
{
|
148
|
+
mrbc_context *c = p->cxt;
|
149
|
+
struct mrbc_args *args = (struct mrbc_args *)c->partial_data;
|
150
|
+
const char *fn;
|
151
|
+
|
152
|
+
if (p->f) fclose(p->f);
|
153
|
+
if (args->idx >= args->argc) {
|
154
|
+
p->f = NULL;
|
155
|
+
return -1;
|
156
|
+
}
|
157
|
+
fn = args->argv[args->idx++];
|
158
|
+
p->f = fopen(fn, "r");
|
159
|
+
if (p->f == NULL) {
|
160
|
+
fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, fn);
|
161
|
+
return -1;
|
162
|
+
}
|
163
|
+
mrb_parser_set_filename(p, fn);
|
164
|
+
return 0;
|
165
|
+
}
|
166
|
+
|
167
|
+
static mrb_value
|
168
|
+
load_file(mrb_state *mrb, struct mrbc_args *args)
|
169
|
+
{
|
170
|
+
mrbc_context *c;
|
171
|
+
mrb_value result;
|
172
|
+
char *input = args->argv[args->idx];
|
173
|
+
FILE *infile;
|
174
|
+
mrb_bool need_close = FALSE;
|
175
|
+
|
176
|
+
c = mrbc_context_new(mrb);
|
177
|
+
if (args->verbose)
|
178
|
+
c->dump_result = TRUE;
|
179
|
+
c->no_exec = TRUE;
|
180
|
+
if (input[0] == '-' && input[1] == '\0') {
|
181
|
+
infile = stdin;
|
182
|
+
}
|
183
|
+
else {
|
184
|
+
need_close = TRUE;
|
185
|
+
if ((infile = fopen(input, "r")) == NULL) {
|
186
|
+
fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input);
|
187
|
+
return mrb_nil_value();
|
188
|
+
}
|
189
|
+
}
|
190
|
+
mrbc_filename(mrb, c, input);
|
191
|
+
args->idx++;
|
192
|
+
if (args->idx < args->argc) {
|
193
|
+
need_close = FALSE;
|
194
|
+
mrbc_partial_hook(mrb, c, partial_hook, (void*)args);
|
195
|
+
}
|
196
|
+
|
197
|
+
result = mrb_load_file_cxt(mrb, infile, c);
|
198
|
+
if (need_close) fclose(infile);
|
199
|
+
mrbc_context_free(mrb, c);
|
200
|
+
if (mrb_undef_p(result)) {
|
201
|
+
return mrb_nil_value();
|
202
|
+
}
|
203
|
+
return result;
|
204
|
+
}
|
205
|
+
|
206
|
+
static int
|
207
|
+
dump_file(mrb_state *mrb, FILE *wfp, const char *outfile, struct RProc *proc, struct mrbc_args *args)
|
208
|
+
{
|
209
|
+
int n = MRB_DUMP_OK;
|
210
|
+
mrb_irep *irep = proc->body.irep;
|
211
|
+
|
212
|
+
if (args->initname) {
|
213
|
+
n = mrb_dump_irep_cfunc(mrb, irep, args->flags, wfp, args->initname);
|
214
|
+
if (n == MRB_DUMP_INVALID_ARGUMENT) {
|
215
|
+
fprintf(stderr, "%s: invalid C language symbol name\n", args->initname);
|
216
|
+
}
|
217
|
+
}
|
218
|
+
else {
|
219
|
+
n = mrb_dump_irep_binary(mrb, irep, args->flags, wfp);
|
220
|
+
}
|
221
|
+
if (n != MRB_DUMP_OK) {
|
222
|
+
fprintf(stderr, "%s: error in mrb dump (%s) %d\n", args->prog, outfile, n);
|
223
|
+
}
|
224
|
+
return n;
|
225
|
+
}
|
226
|
+
|
227
|
+
static mrb_value
|
228
|
+
mrb_mruby_cli_mrbc(mrb_state *mrb, mrb_value klass)
|
229
|
+
{
|
230
|
+
FILE *wfp;
|
231
|
+
char *argv[255];
|
232
|
+
struct mrbc_args args;
|
233
|
+
mrb_int n, result, i, argc;
|
234
|
+
mrb_value load, array, element;
|
235
|
+
|
236
|
+
mrb_get_args(mrb, "io", &argc, &array);
|
237
|
+
|
238
|
+
for (i = 0; i < argc; i++) {
|
239
|
+
element = mrb_ary_shift(mrb, array);
|
240
|
+
argv[i] = RSTRING_PTR(element);
|
241
|
+
}
|
242
|
+
|
243
|
+
if (mrb == NULL) {
|
244
|
+
fputs("Invalid mrb_state, exiting funky-cli\n", stderr);
|
245
|
+
return mrb_fixnum_value(EXIT_FAILURE);
|
246
|
+
}
|
247
|
+
|
248
|
+
n = parse_args(mrb, argc, argv, &args);
|
249
|
+
if (n == argc) {
|
250
|
+
fprintf(stderr, "%s: no program file given\n", args.prog);
|
251
|
+
return mrb_fixnum_value(EXIT_FAILURE);
|
252
|
+
}
|
253
|
+
if (args.outfile == NULL && !args.check_syntax) {
|
254
|
+
if (n + 1 == argc) {
|
255
|
+
args.outfile = get_outfilename(mrb, argv[n], args.initname ? C_EXT : RITEBIN_EXT);
|
256
|
+
}
|
257
|
+
else {
|
258
|
+
fprintf(stderr, "%s: output file should be specified to compile multiple files\n", args.prog);
|
259
|
+
return mrb_fixnum_value(EXIT_FAILURE);
|
260
|
+
}
|
261
|
+
}
|
262
|
+
|
263
|
+
args.idx = n;
|
264
|
+
load = load_file(mrb, &args);
|
265
|
+
if (mrb_nil_p(load)) {
|
266
|
+
return mrb_fixnum_value(EXIT_FAILURE);
|
267
|
+
}
|
268
|
+
if (args.check_syntax) {
|
269
|
+
printf("%s:%s:Syntax OK\n", args.prog, argv[n]);
|
270
|
+
}
|
271
|
+
|
272
|
+
if (args.check_syntax) {
|
273
|
+
return mrb_fixnum_value(EXIT_SUCCESS);
|
274
|
+
}
|
275
|
+
|
276
|
+
if (args.outfile) {
|
277
|
+
if (strcmp("-", args.outfile) == 0) {
|
278
|
+
wfp = stdout;
|
279
|
+
}
|
280
|
+
else if ((wfp = fopen(args.outfile, "wb")) == NULL) {
|
281
|
+
fprintf(stderr, "%s: cannot open output file:(%s)\n", args.prog, args.outfile);
|
282
|
+
return mrb_fixnum_value(EXIT_FAILURE);
|
283
|
+
}
|
284
|
+
}
|
285
|
+
else {
|
286
|
+
fprintf(stderr, "Output file is required\n");
|
287
|
+
return mrb_fixnum_value(EXIT_FAILURE);
|
288
|
+
}
|
289
|
+
result = dump_file(mrb, wfp, args.outfile, mrb_proc_ptr(load), &args);
|
290
|
+
fclose(wfp);
|
291
|
+
if (result != MRB_DUMP_OK) {
|
292
|
+
return mrb_fixnum_value(EXIT_FAILURE);
|
293
|
+
}
|
294
|
+
|
295
|
+
return mrb_fixnum_value(EXIT_SUCCESS);
|
296
|
+
}
|
297
|
+
|
298
|
+
mrb_value mrb_mruby_cli_mirb(mrb_state *mrb, mrb_value klass);
|
299
|
+
mrb_value mrb_mruby_cli_mruby(mrb_state *mrb, mrb_value klass);
|
300
|
+
|
301
|
+
void
|
302
|
+
mrb_funky_cli_gem_init(mrb_state* mrb)
|
303
|
+
{
|
304
|
+
struct RClass *cli;
|
305
|
+
|
306
|
+
cli = mrb_define_module(mrb, "FunkyCli");
|
307
|
+
mrb_define_class_method(mrb , cli , "mrbc" , mrb_mruby_cli_mrbc , MRB_ARGS_REQ(2));
|
308
|
+
mrb_define_class_method(mrb , cli , "mirb" , mrb_mruby_cli_mirb , MRB_ARGS_NONE());
|
309
|
+
mrb_define_class_method(mrb , cli , "mruby" , mrb_mruby_cli_mruby , MRB_ARGS_REQ(2));
|
310
|
+
DONE;
|
311
|
+
}
|
312
|
+
|
313
|
+
void
|
314
|
+
mrb_funky_cli_gem_final(mrb_state* mrb)
|
315
|
+
{
|
316
|
+
}
|