-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.rb
413 lines (317 loc) · 7.52 KB
/
options.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/bin/env ruby -w
require 'erb'
require 'optparse'
class Option
attr_reader :name
attr_reader :description
attr_reader :argument # nil, :, =, ?
# name of the field, in the struct
# will be null if virtual.
attr_reader :field
# array of custom code.
attr_accessor :code
attr_reader :modifiers
def _fieldName(name, modifiers)
return nil if modifiers[:virtual]
return modifiers[:field] if modifiers[:field]
return "_#{name}"
end
def initialize(name, argument, modifiers)
@code = []
modifiers ||= {}
@name = name
@field = _fieldName(name, modifiers)
@argument = argument
@modifiers = modifiers
end
def to_s()
return "Option: #{description}#{argument} #{field}"
end
# def addCode(x)
# @code.push(x)
# end
# def finishCode
# # remove all blank lines at the end
# while @code.length && @code.last =~ /^\s*$/
# @code.pop
# end
# end
end
class ShortOption < Option
def initialize(name, argument, modifiers)
super(name, argument, modifiers)
@description = "-#{name}"
end
def generateCase()
rv = []
indent = " " * 8
rv.push indent, "case '#{@name}':\n"
indent += " "
if (@argument)
#print indent, "GETOPTARG(#{@description})\n"
rv.push indent, "++j;\n"
rv.push indent, "if (cp[j]) {\n"
rv.push indent, " optarg = cp + j;\n"
rv.push indent, "} else {\n"
rv.push indent, " ++i;\n"
rv.push indent, " if (i < argc) optarg = argv[i];\n"
rv.push indent, "}\n"
rv.push indent, "if (!optarg) {\n"
rv.push indent, " fputs(\"#{@description} requires an argument\\n\", stderr);\n"
rv.push indent, " exit(1);\n"
rv.push indent, "}\n"
end
if @field
if @argument
rv.push indent, "options->#{@field} = optarg;\n"
else
if @modifiers[:increment]
rv.push indent, "options->#{@field} += 1;\n"
else
rv.push indent, "options->#{@field} = 1;\n"
end
end
end
@code.each {|x| rv.push indent, x }
rv.push indent,"break;\n"
end
def generateField()
# generate the field for the struct.
return "" unless @field
return " char *#{@field};\n" if @argument
return " unsigned #{@field};\n" if @modifiers[:increment]
return " unsigned #{@field}:1;\n"
end
end
def arrayTrim(x)
while x.length && x.last =~ /^\s*$/
x.pop
end
x
end
def parseModifiers(string)
# splits a key=value (, key=value)* string
# key is equivalent to key={true}
rv = {}
return rv unless string
args = string.split(',')
args.each{|x|
x.strip!
next unless x.length
key, value = x.split('=')
value ||= true # key == key={true}
key = key.intern
rv[key] = value
}
return rv;
end
@shortRE = /
^
-([\w]) # 1. letter
([:])? # 2. argument type?
\s*
(?:\[
([\w\s,=]*) # 3. modifiers?
\])?
\s*
(->)? # 4. code?
$
/x
@longRE = /
^
--([\w-]*) # 1. name
([=?])? # 2. argument type?
\s*
(?:\[
([\w\s,=]*) # 3. modifiers?
\])?
\s*
(->)? # 4. code?
$
/x
@headerTemplate = <<EOD
#ifndef __<%= config[:prefix] %>Options__
#define __<%= config[:prefix] %>Options__
typedef struct <%= config[:prefix] %>Options
{
<%= (config[:extra_fields] || []).join() %>
<%= (options.map {|x| x.generateField }).join() %>
} <%= config[:prefix] %>Options;
int Get<%= config[:prefix] %>Options(int argc, char **argv,
<%= config[:prefix] %>Options *options);
#endif
EOD
#
def stripExtension(path)
return $1 if path =~ /^(.*?)\.([^.\/]*)$/
return path
end
def process(infile, keepName)
opt = nil # current option being processed
tmp = [] # code block array
callback = nil # callback when code is finished.
options = [] # list of options
config = {}
infile.each_line { |line|
line.chomp!
line.rstrip!
if callback
if line == "" || line =~ /^\s+/
tmp.push(line + "\n")
next
end
# store...
callback.call(arrayTrim(tmp));
callback = nil
tmp = []
code = false
end
next if line =~ /^\s*#/
if line =~ /^%/
if m = line.match(/^%(\w+)=(\w+)$/)
key = m[1].intern
config[key] = m[2]
next
end
if m = line.match(/^%(\w+)$/)
key = m[1].intern
config[key] = true
next
end
if m = line.match(/^%(\w+)\s*->$/)
key = m[1].intern
callback = Proc.new {|code| config[key] = code }
next
end
$stderr.puts "Not supported: #{line}"
next
end
if m = line.match(@shortRE)
tmp = []
flag = m[1]
arg = m[2]
modifiers = m[3]
modifiers = parseModifiers(modifiers)
opt = ShortOption.new(flag, arg, modifiers)
options.push(opt)
# any code?
if m[4]
callback = Proc.new {|code| opt.code = code }
end
end
#
}
# if it ended within a code block...
if callback
callback.call(arrayTrim(tmp))
callback = nil
tmp = []
end
options.sort! {|a, b| a.name <=> b.name }
keepName = nil if keepName == ""
keepBase = stripExtension(keepName)
headerName = (config[:prefix] || '') + 'Options.h'
headerName = keepBase + '.h' if keepBase
io = $stdout
io = File.open(keepName, "w") if keepName
b = binding
erb = ERB.new(DATA.read(), 0, "%<>")
io.write(erb.result(b))
io.close unless io == $stdout
# header file.
io = $stdout
io = File.open(headerName, "w") if headerName && keepName
erb = ERB.new(@headerTemplate, 0, "%<>")
io.write(erb.result(b))
io.close unless io == $stdout
end
keepFile = nil
op = OptionParser.new {|opts|
opts.banner = "Usage: options.rb [options] [infile]"
opts.on("-o", "--keep OUTFILE", "Specify output file name") do |filename|
keepFile = filename
end
}
op.parse!
keepFile = nil if keepFile == ""
case ARGV.length
when 0
process($stdin, keepFile)
when 1
file = ARGV[0]
io = $stdin
if file != '-'
keepFile = stripExtension(file) + '.c' if keepFile == nil
io = File.open(file, "r")
end
process(io, keepFile)
io.close unless io == $stdin
else
op.help
exit(1)
end
exit(0)
__END__
#ifdef __ORCAC__
#pragma optimize 79
#pragma noroot
#endif
#include <stdio.h>
#include <stdlib.h>
#include "<%= File.basename(headerName) %>"
<%= (config[:extra_includes] || []).join() %>
int Get<%= config[:prefix] %>Options(int argc, char **argv,
struct <%= config[:prefix] %>Options *options)
{
int i, j;
int eof = 0;
int mindex = 1; /* mutation index */
for (i = 1; i < argc; ++i)
{
char *cp = argv[i];
char c = cp[0];
<% if config[:posixly_correct] %>
// stop processing at first non-opt.
if (c != '-')
{
eof = 1;
if (i == mindex) return argc;
argv[mindex] = argv[i];
++mindex;
continue;
}
<% end %>
if (eof || c != '-')
{
if (mindex != i) argv[mindex] = argv[i];
++mindex;
continue;
}
// long opt check would go here...
if (cp[1] == '-' && cp[2] == 0)
{
eof = 1;
continue;
}
// special case for '-'
j = 0;
if (cp[1] != 0) j = 1;
for (; ; ++j)
{
char *optarg = 0;
c = cp[j];
if (!c) break;
switch(c)
{
<%= options.map{|o| o.generateCase() }.join() %>
default:
fprintf(stderr, "-%c : invalid option\n", c);
exit(1);
break;
}
// could optimize out if no options have flags.
if (optarg) break;
}
}
return mindex;
}