crun 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ = Version history
2
+
3
+ [0.0.1] Initial version.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 tero.isannainen@gmail.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,183 @@
1
+ = Crun
2
+
3
+ * Introduction
4
+ * Example runs
5
+ * Options
6
+ * Issues
7
+
8
+
9
+ = Introduction
10
+
11
+ Crun is an utility for compiling and running C-programs straight from
12
+ C source files. Crun supports compile-run and compile-debug flows.
13
+
14
+ You can embed custom compile options into the main c-file, if you
15
+ need, for example, an external library for the program. You can also
16
+ control the command line parameters passed to the program through
17
+ Crun.
18
+
19
+ Crun is usually used with one c-file, i.e. the main file. However
20
+ program can be split into multiple files, but main has to be first in
21
+ the list of files. Crun is targeted for small C-programs, so it is not
22
+ typical to have multiple files.
23
+
24
+ User of Crun is most probably trying out some basic C-features or maybe
25
+ prototyping with some algorithm.
26
+
27
+
28
+ = Example runs
29
+
30
+ In order to run "Hello World" example with Crun, you need to have the
31
+ C source file with "Hello World" program in it. This is available in
32
+ the "examples" directory for you.
33
+
34
+ "hello.c" content:
35
+
36
+ #include <stdio.h>
37
+ int main( int argc, char** argv )
38
+ {
39
+ printf( "Hello World!\n" );
40
+ return 0;
41
+ }
42
+
43
+ You can execute compile-run flow by:
44
+
45
+ shell> crun -f examples/hello.c
46
+
47
+ The output from program is:
48
+
49
+ Hello World!
50
+
51
+ You can execute compile-debug flow by:
52
+
53
+ shell> crun -f examples/hello.c -d
54
+
55
+ Program is compiled and gdb is started so that the program stops at
56
+ "main" (i.e. default behavior).
57
+
58
+ Check available command line interface, CLI, options for Crun:
59
+
60
+ shell> crun -h
61
+
62
+
63
+ = Options
64
+
65
+ You can specify options for Crun in multiple ways. Typically CLI is
66
+ used to control Crun, but if the program uses static compilation
67
+ options, those live most naturally in the source file itself.
68
+
69
+ Options are stored to a hash called "@crun". Each option name is a key
70
+ in the hash, and the option values are Arrays.
71
+
72
+ For example the entry:
73
+
74
+ @crun['progopts']
75
+
76
+ specify the command line parameters given to the program compiled and
77
+ run by Crun.
78
+
79
+ Here is the complete list of options that control Crun:
80
+
81
+ [ compopts ]
82
+
83
+ Array of user's compilation options. E.g. "-l lm" to include the
84
+ math lib. Default is: [].
85
+
86
+ [ progopts ]
87
+
88
+ Array of command line parameters for the program run. E.g. ["-c
89
+ 12"]. Default is: [].
90
+
91
+ [ stopopts ]
92
+
93
+ Array of breakpoints for the debugger. E.g. ["add"] defines that the
94
+ debugger stops at "add" function. Default is: ["main"].
95
+
96
+ [ compprog ]
97
+
98
+ Array including the compiler name and base options. This options
99
+ array should only have one entry, but in theory you could have
100
+ multiple entries. The entries are concatenated with SPACE. The
101
+ compiler command has to have same command line parameters as
102
+ "gcc". Default is: ["gcc", "-Wall", "-std=c11"].
103
+
104
+ [ dbugprog ]
105
+
106
+ Array including the debugger name and base options. This options
107
+ array should only have one entry, but in theory you could have
108
+ multiple entries. The entries are concatenated with SPACE. The
109
+ debugger command has to have same command line parameters as
110
+ "gdb". Default is: ["gdb", "--nx"].
111
+
112
+ [ crunopts ]
113
+
114
+ Array of compiler options managed by Crun. User don't have to
115
+ control this option. Default is: [].
116
+
117
+ Crun sets Options to default values and then applies the user
118
+ overrides from various sources, given below.
119
+
120
+ User can embed the options to the C source file. This is the
121
+ recommended way because this way the settings will follow the code.
122
+ Additional compilation options and CLI options for the compiled
123
+ program would be given as:
124
+
125
+ /*
126
+ * Options for crun:
127
+ * -crun-compopts:=-O2
128
+ * crun-compopts:+-lm
129
+ * crun-progopts:=-c 12
130
+ */
131
+
132
+ The syntax is "crun-<option>". If there is no space before "crun", the
133
+ setting is not used, i.e. first "compopts" is neglected above.
134
+
135
+ User can specify static overrides in "$HOME/.crun" file and/or in
136
+ "./.crun". In addition to these predefined files, user can use the
137
+ "-c" Crun CLI option to read in option overrides.
138
+
139
+ For example if the program is always run with "-c 10" command line
140
+ options, you use this in ".crun":
141
+
142
+ @crun['progopts'] = [ "-c 10" ]
143
+
144
+ An alternative way to achieve the same thing is to use Crun CLI and
145
+ use the -o option:
146
+
147
+ shell> crun -o "progopts:=-c 10" ...
148
+
149
+ This sets the list of program options to "-c 10". ":=" syntax means
150
+ setting the Array of options. If you need to add to existing list of
151
+ options, you should do:
152
+
153
+ shell> crun -o "progopts:+-c 10" ...
154
+
155
+ Thus ":+" is the syntax for appending more options to existing list.
156
+
157
+ The simplest way to pass 'progopts' to target program is using the
158
+ "-p" CLI option. However user must be aware of the shell option
159
+ parsing, since it is very easy to miss the target program and actually
160
+ pass the option to Crun.
161
+
162
+ If you want to pass '-' based option to target program:
163
+
164
+ shell> crun -p "\-c 10" ...
165
+
166
+ The backslash before "-c" will ensure that "-c" does not become an
167
+ option for Crun.
168
+
169
+ Summary of option setting from first to last:
170
+
171
+ * Defaults (from crun program).
172
+ * ".crun" from home directory.
173
+ * ".crun" from current directory.
174
+ * "-c" options from files.
175
+ * "-o" options from Crun CLI.
176
+ * "-p" options for program (only).
177
+ * Options embedded to C source file (main file).
178
+
179
+
180
+ = Issues
181
+
182
+ If you take C source input from STDIN, you can't use the debug
183
+ option. Only compile-run flow is possible.
@@ -0,0 +1,225 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # crun compiles the given c-file and executes it using RAM FS. It can
4
+ # be considered as c-scripting. crun can dig out compilation options
5
+ # from the c-file if special linking or such is required.
6
+ #
7
+ # See README.rdoc for full documentation.
8
+
9
+
10
+ require 'como'
11
+ include Como
12
+ require 'tempfile'
13
+ require_relative '../lib/version'
14
+
15
+ Spec.command( 'crun', 'Tero Isannainen', '2016',
16
+ [
17
+ [ :opt_multi, 'file', '-f', "C-file(s) for program." ],
18
+ [ :opt_single, 'prog', '-p', "Add program options (i.e. progopts)." ],
19
+ [ :switch, 'debug', '-d', "Debug program." ],
20
+ [ :opt_multi, 'opts', '-o', "Set/add crun option." ],
21
+ [ :opt_multi, 'conf', '-c', "Crun option file(s)." ],
22
+ [ :switch, 'input', '-i', "Read stdin for C-code." ],
23
+ [ :exclusive, 'template','-t', "Output a template file for program." ],
24
+ [ :switch, 'verbose', '-v', "Verbose (to stderr)." ],
25
+ ] )
26
+
27
+
28
+ # Execute shell command with optional verbosity.
29
+ def crun_exe( cmd )
30
+ if Opt['verbose'].given
31
+ STDERR.puts "crun execution: \"#{cmd}\" ..."
32
+ end
33
+ system( cmd )
34
+ end
35
+
36
+ # Parse and apply option setting.
37
+ def apply_opt( opt )
38
+ if opt.split(':=').length == 2
39
+ parts = opt.split(':=')
40
+ @crun[parts[0]] = [ parts[1] ]
41
+ elsif opt.split(':+').length == 2
42
+ parts = opt.split(':+')
43
+ @crun[parts[0]].push parts[1]
44
+ end
45
+ end
46
+
47
+ if Opt['template'].given
48
+ puts "\
49
+ /*
50
+ * crun-compopts:+-O2
51
+ */
52
+
53
+ #include <stdio.h>
54
+
55
+ int main( int argc, char** argv )
56
+ {
57
+
58
+ return 0;
59
+ }
60
+ "
61
+ exit( false )
62
+ end
63
+
64
+
65
+ if Opt['file'].given
66
+ cfiles = Opt['file'].value
67
+ elsif Opt['input'].given
68
+ # Create a temp c-file for input.
69
+ input = STDIN.read
70
+ cfile = "#{Tempfile.new( "crun-source", '/dev/shm' ).path}.c"
71
+ File.write( cfile, input )
72
+ cfiles = [ cfile ]
73
+ else
74
+ Spec.usage
75
+ end
76
+
77
+
78
+ # ------------------------------------------------------------
79
+ # Configurations:
80
+
81
+ # Default crun configuration.
82
+ @crun = {
83
+
84
+ # Users compiler options.
85
+ 'compopts' => [],
86
+
87
+ # Program's command line options.
88
+ 'progopts' => [],
89
+
90
+ # Debugger breakpoints.
91
+ 'stopopts' => %w{main},
92
+
93
+ # Used compiler.
94
+ 'compprog' => %w{gcc -Wall -std=c11},
95
+
96
+ # Used compiler.
97
+ 'dbugprog' => %w{gdb --nx},
98
+
99
+ # Compiler options managed by crun.
100
+ 'crunopts' => [],
101
+ }
102
+
103
+
104
+ # Read user conf.
105
+ userconf = "#{ENV['HOME']}/.crun"
106
+ load userconf if File.exist? userconf
107
+
108
+ # Read local user conf.
109
+ userconf = "./.crun"
110
+ load userconf if File.exist? userconf
111
+
112
+
113
+ # Compile with '-g' in debug mode.
114
+ if Opt['debug'].given
115
+ @crun['crunopts'].push '-g'
116
+ end
117
+
118
+ # Read in selected user confs.
119
+ if Opt['conf'].given
120
+ Opt['conf'].value.each do |userconf|
121
+ load userconf if File.exist? userconf
122
+ end
123
+ end
124
+
125
+
126
+ # ------------------------------------------------------------
127
+ # Collect compilation options from c-file.
128
+ compopts = @crun['compopts']
129
+ crun_re = /[\s]+crun-([a-z]+)/
130
+
131
+ File.readlines( cfiles[0] ).each do |line|
132
+ m = line.match( crun_re )
133
+ if m
134
+ option = m[1]
135
+ setting = line.chomp.split( 'crun-' )[1]
136
+ apply_opt( setting )
137
+ end
138
+ end
139
+
140
+
141
+ # ------------------------------------------------------------
142
+ # Collect compilation options from CLI.
143
+
144
+ # Get options from CLI.
145
+ if Opt['opts'].given
146
+ Opt['opts'].value.each do |opt|
147
+ apply_opt( opt )
148
+ end
149
+ end
150
+
151
+ # Get program execution options.
152
+ if Opt['prog'].given
153
+ @crun['progopts'].push Opt['prog'].value
154
+ end
155
+
156
+
157
+ # ------------------------------------------------------------
158
+ # Create tempfile for executable.
159
+ exefile = File.basename( cfiles[0], '.c' )
160
+ exepath = Tempfile.new( "crun-#{exefile}", '/dev/shm' ).path
161
+
162
+
163
+ # ------------------------------------------------------------
164
+ # Create list of commands to run:
165
+ cmds = []
166
+
167
+ class Array
168
+ # Join array elements with space.
169
+ def crun
170
+ self.join(' ')
171
+ end
172
+ end
173
+
174
+ cmds.push "#{@crun['compprog'].crun} #{@crun['crunopts'].crun} #{@crun['compopts'].crun} #{cfiles.crun} -o #{exepath}"
175
+
176
+ # In order to avoid the "text file busy" issue, copy back and forth the exefile.
177
+ cmds.push "cp #{exepath} #{exepath}-tmp"
178
+ cmds.push "rm -f #{exepath}"
179
+ cmds.push "mv #{exepath}-tmp #{exepath}"
180
+
181
+ if Opt['debug'].given
182
+
183
+ # compile-debug flow:
184
+ cmd = @crun['dbugprog'].crun
185
+ if @crun['progopts'].any?
186
+ cmd += " -ex \"set args #{@crun['progopts'].crun}\""
187
+ end
188
+ breaks = ""
189
+ if @crun['stopopts'].any?
190
+ breaks = @crun['stopopts'].map{|i| " -ex \"break #{i}\""}.crun
191
+ end
192
+ cmd += breaks
193
+ cmd += " -ex run"
194
+ cmd += " #{exepath}"
195
+ cmds.push cmd
196
+
197
+ else
198
+
199
+ # compile-run flow:
200
+ if @crun['progopts'].any?
201
+ cmds.push "#{exepath} #{@crun['progopts'].crun}"
202
+ else
203
+ cmds.push exepath
204
+ end
205
+
206
+ end
207
+
208
+
209
+ # ------------------------------------------------------------
210
+ # Execute collected commands:
211
+ ret = nil
212
+ cmds.each do |cmd|
213
+ ret = crun_exe( cmd )
214
+ if ret == false
215
+ break
216
+ end
217
+ end
218
+
219
+ # ------------------------------------------------------------
220
+ # Always cleanup, but not necessary if using "/dev/shm".
221
+ if Opt['input'].given
222
+ system( "rm -f #{cfiles[0]}" )
223
+ end
224
+
225
+ FileUtils.rm_f exepath
@@ -0,0 +1,202 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Class: Array
8
+
9
+ &mdash; Documentation by YARD 0.8.7.6
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ hasFrames = window.top.frames.main ? true : false;
19
+ relpath = '';
20
+ framesUrl = "frames.html#!Array.html";
21
+ </script>
22
+
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
25
+
26
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
27
+
28
+
29
+ </head>
30
+ <body>
31
+ <div id="header">
32
+ <div id="menu">
33
+
34
+ <a href="_index.html">Index (A)</a> &raquo;
35
+
36
+
37
+ <span class="title">Array</span>
38
+
39
+
40
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
41
+ </div>
42
+
43
+ <div id="search">
44
+
45
+ <a class="full_list_link" id="class_list_link"
46
+ href="class_list.html">
47
+ Class List
48
+ </a>
49
+
50
+ <a class="full_list_link" id="method_list_link"
51
+ href="method_list.html">
52
+ Method List
53
+ </a>
54
+
55
+ <a class="full_list_link" id="file_list_link"
56
+ href="file_list.html">
57
+ File List
58
+ </a>
59
+
60
+ </div>
61
+ <div class="clear"></div>
62
+ </div>
63
+
64
+ <iframe id="search_frame"></iframe>
65
+
66
+ <div id="content"><h1>Class: Array
67
+
68
+
69
+
70
+ </h1>
71
+
72
+ <dl class="box">
73
+
74
+ <dt class="r1">Inherits:</dt>
75
+ <dd class="r1">
76
+ <span class="inheritName">Object</span>
77
+
78
+ <ul class="fullTree">
79
+ <li>Object</li>
80
+
81
+ <li class="next">Array</li>
82
+
83
+ </ul>
84
+ <a href="#" class="inheritanceTree">show all</a>
85
+
86
+ </dd>
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+ <dt class="r2 last">Defined in:</dt>
97
+ <dd class="r2 last">bin/crun</dd>
98
+
99
+ </dl>
100
+ <div class="clear"></div>
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+ <h2>
111
+ Instance Method Summary
112
+ <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
113
+ </h2>
114
+
115
+ <ul class="summary">
116
+
117
+ <li class="public ">
118
+ <span class="summary_signature">
119
+
120
+ <a href="#crun-instance_method" title="#crun (instance method)">- (Object) <strong>crun</strong> </a>
121
+
122
+
123
+
124
+ </span>
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+ <span class="summary_desc"><div class='inline'>
135
+ <p>Join array elements with space.</p>
136
+ </div></span>
137
+
138
+ </li>
139
+
140
+
141
+ </ul>
142
+
143
+
144
+
145
+
146
+ <div id="instance_method_details" class="method_details_list">
147
+ <h2>Instance Method Details</h2>
148
+
149
+
150
+ <div class="method_details first">
151
+ <h3 class="signature first" id="crun-instance_method">
152
+
153
+ - (<tt>Object</tt>) <strong>crun</strong>
154
+
155
+
156
+
157
+
158
+
159
+ </h3><div class="docstring">
160
+ <div class="discussion">
161
+
162
+ <p>Join array elements with space.</p>
163
+
164
+
165
+ </div>
166
+ </div>
167
+ <div class="tags">
168
+
169
+
170
+ </div><table class="source_code">
171
+ <tr>
172
+ <td>
173
+ <pre class="lines">
174
+
175
+
176
+ 169
177
+ 170
178
+ 171</pre>
179
+ </td>
180
+ <td>
181
+ <pre class="code"><span class="info file"># File 'bin/crun', line 169</span>
182
+
183
+ <span class='kw'>def</span> <span class='id identifier rubyid_crun'>crun</span>
184
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'> </span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
185
+ <span class='kw'>end</span></pre>
186
+ </td>
187
+ </tr>
188
+ </table>
189
+ </div>
190
+
191
+ </div>
192
+
193
+ </div>
194
+
195
+ <div id="footer">
196
+ Generated on Sun Dec 25 20:31:45 2016 by
197
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
198
+ 0.8.7.6 (ruby-2.3.1).
199
+ </div>
200
+
201
+ </body>
202
+ </html>