forked from google/crush-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgener
executable file
·555 lines (478 loc) · 15.1 KB
/
cgener
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/perl -w
#-*-perl-*-
# copyright 2004 - 2008, Jeremy Hinds <jeremy.hinds AT gmail DOT com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
use strict;
use Getopt::Long;
my $version = '0.3.3';
my ($show_help, $show_version, $dir, $argfile);
$dir='./';
&GetOptions("help" => \$show_help,
"version" => \$show_version,
"directory:s" => \$dir,
"argfile:s" => \$argfile,
);
if ( $show_version ){
version();
exit(1);
}
if ( $show_help || ! defined($argfile) ){
usage();
exit(1);
}
my (%global, @args);
# this is to get the data structures into the main namespace
# without having to turn off strict
open(ARGFILE, $argfile) or die "$argfile: $!\n";
my @argfile_a = <ARGFILE>;
close(ARGFILE);
my $argfile_content = join('', @argfile_a);
eval "$argfile_content";
if (! @args){
print STDERR "$argfile: missing args array.\n";
exit(1);
}
if (! %global){
print STDERR "$argfile: missing global hash.\n";
exit(1);
}
if (! defined($global{name})) {
print STDERR "global{name} must be defined in $argfile.\n";
exit(1);
}
if ( ! defined( $global{version} ) ){
$global{version} = '0.1';
}
if ( ! defined( $global{description} ) ){
$global{description} = '';
}
if ( ! defined( $global{preproc_extra} ) ){
$global{preproc_extra} = '';
}
if ( ! defined( $global{copyright} ) ){
$global{copyright} = '';
}
if ( ! defined( $global{language} ) ) {
$global{language} = 'c';
}
my $c_usage = make_usage();
my $c_arg_struct = make_arg_struct();
my $c_arg_parse;
if ($global{do_long_opts}){
$c_arg_parse = make_arg_parse_long();
} else {
$c_arg_parse = make_arg_parse();
}
my $c_required_args = make_requirement_check();
if ($dir !~ /[\/\\]$/) {
$dir .= '/';
}
while(<DATA>){
s/%appname%/$global{name}/g;
s/%language%/$global{language}/g;
s/%APPNAME%/\U$global{name}/g;
s/%appdescription%/$global{description}/g;
s/%appversion%/$global{version}/g;
s/%appcopyright%/$global{copyright}/g;
s/%user_defined_preproc%/$global{preproc_extra}/g;
s/%usage_function%/$c_usage/g;
s/%required_args%/$c_required_args/g;
s/%args_struct%/$c_arg_struct/g;
s/%parse_function%/$c_arg_parse/g;
if (/^----(.*)----$/) {
if ( defined(fileno(OUT)) ) {
close(OUT);
}
open(OUT, "> $dir$1") or die "$dir$1: $!\n";
}
else {
print OUT $_;
}
}
close(OUT);
exit(0);
sub usage {
print STDERR <<DISCLAIMER;
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
DISCLAIMER
print STDERR "\ncgen: generates files for a new c application.\n\n",
"usage: $0 <-h|-n <name>> [-d <dir> -e <desc>]\n\n",
" -h | --help print this message and exit\n",
" -a | --argfile <file> specifies the argument table file\n",
" -d | --directory <dir> specifies output directory\n\n",
"you may want to use newcapp to generate the files to get you started.\n\n";
}
sub version {
print STDERR "$0: $version\n";
}
sub make_usage {
my $uline = 'fprintf(stderr, "\nusage: %s <options>';
if (defined($global{trailingopts})){
$uline .= " $global{trailingopts} ";
}
if (defined($global{trailing_opts})){
$uline .= " $global{trailing_opts} ";
}
# figure out column widths for the option/description list
my %col_widths = ();
my $max_width = 0;
foreach my $arg (@args) {
my $this_arg_width = 2;
if ($global{do_long_opts} && $arg->{longopt}) {
# add room for '--', comma & space
$this_arg_width += length($arg->{longopt}) + 4;
}
if ($arg->{type} !~ /flag/) {
# add room for '<', '>' & space
$this_arg_width += length($arg->{name}) + 3;
}
$col_widths{$arg->{shortopt}}[0] = $this_arg_width;
$col_widths{$arg->{shortopt}}[1] = length($arg->{description});
if ($this_arg_width > $max_width && $this_arg_width < 28) {
$max_width = $this_arg_width;
}
}
my $ustr =<<__END_USAGE__;
/** prints application help information.
* \@param bin application name as invoked on the commandline
*/
void usage( char *bin ) {
fprintf(stderr, "\\n$global{name}: $global{description}\\n");
$uline\\n", bin);
fprintf(stderr, "options:\\n");
__END_USAGE__
foreach my $arg ( @args ){
$ustr .= qq(\t) . 'fprintf' . '(stderr, " -' . $arg->{shortopt} ;
if ( $global{do_long_opts} && $arg->{longopt} ){
$ustr .= ', --' . $arg->{longopt};
}
if ($arg->{type} !~ /flag/) {
$ustr .= ' <' . $arg->{name} . '>';
}
if ($col_widths{$arg->{shortopt}}[0] <= $max_width) {
$ustr .= ' ' x ($max_width - $col_widths{$arg->{shortopt}}[0]);
} else {
$ustr .= '\\n ' . ' ' x $max_width;
}
$ustr .= ' ' .
wrap_description($max_width + 2, 80 - $max_width - 2,
$arg->{description}) .
'\n");' . qq(\n);
}
if ( defined($global{usage_extra}) && length($global{usage_extra}) ){
my $usage_extra = $global{usage_extra};
$usage_extra =~ s/[\r\n]/\\n/smog;
$ustr .= qq(\t) . 'fprintf' .
'(stderr, "\n' . $usage_extra . '");' . qq(\n);
}
$ustr .= qq(\tprintf("\\n\\n");\n\treturn;\n} );
return $ustr;
}
sub make_arg_struct {
my $astr =<<ARGSTRUCT;
/** structure to hold commandline options & arguments.
*/
struct cmdargs {
ARGSTRUCT
foreach my $arg ( @args ){
if ( ! defined($arg->{name}) || ! defined($arg->{type}) ){
print STDERR "WARNING: arg missing required field(s)\n";
next;
}
$astr .= qq(\t) . ($arg->{type} =~ /flag/ ? 'int ' : 'char *').
$arg->{name} . qq(;\t/**< \@brief ) . $arg->{description}. qq( */\n);
}
$astr .= q(};) . qq(\n);
return $astr;
}
sub make_arg_parse {
my $optstr = '';
my $pstr = '';
foreach my $arg ( @args ) {
$optstr .= $arg->{shortopt};
if ( $arg->{type} !~ /flag/ ){
$optstr .= ':';
}
}
$optstr = <<__OPT_PARSE__;
/** parses commandline arguments and puts the info into a cmdargs struct.
* \@param argc number of arguments
* \@param argv array of argument strings
* \@param args structure to hold the parsed options & arguments
*/
void parse_args ( int argc, char **argv, struct cmdargs *args){
int c;
init_cmdargs(args);
while( (c = getopt(argc, argv, "$optstr")) != -1 ){
switch (c) {
__OPT_PARSE__
foreach my $arg ( @args ){
$optstr .= "\t\t\tcase '" . $arg->{shortopt} . "':\n" .
"\t\t\t\t" ;
if ( $arg->{type} eq 'flag' ){
$optstr .= 'args->' . $arg->{name} . " = 1;\n";
} elsif ( $arg->{type} eq 'var' ) {
$optstr .= 'args->' . $arg->{name} . " = optarg;\n";
} elsif ( $arg->{type} eq 'var_optional' ) {
$optstr .= 'if ( optarg ) args->' . $arg->{name} . " = optarg;\n";
$optstr .= 'else args->' . $arg->{name} . " = (char *) 1;\n";
} elsif ( $arg->{type} =~ /custom/ ) {
$optstr .= $arg->{parseopt_code} . ";\n";
} else {
print STDERR "WARNING: unknown type for arg ", $arg->{name}, "\n";
}
$optstr .= "\t\t\t\tbreak;\n";
}
$optstr .= "\t\t\tdefault: break;\n\t\t}\n\t}\n\treturn;\n}\n";
return $optstr;
}
sub make_arg_parse_long {
my $optstr = '';
my $longopts = '#include <getopt.h>' . "\n\n" .
'/** long option structure list */' . "\n" .
'static struct option long_options[] = {';
my $pstr = '';
foreach my $arg ( @args ) {
$optstr .= $arg->{shortopt};
if ( $arg->{type} !~ /flag/ ){
$optstr .= ':';
}
if ( $arg->{type} =~ /optional/ ){
$optstr .= ':';
}
if ( defined($arg->{longopt}) ){
$longopts .= qq(\n\t{") . $arg->{longopt} . q(", );
if ( $arg->{type} =~ /flag/ ){
$longopts .= 'no_argument';
} elsif ( $arg->{type} =~ /optional/ ){
$longopts .= 'optional_argument';
} else {
$longopts .= 'required_argument';
}
$longopts .= q(, 0, ') . $arg->{shortopt} . q('},);
}
}
$longopts .= qq(\n\t{0, 0, 0, 0}\n};\n\n);
$optstr = <<__OPT_PARSE__;
/** parses commandline arguments and puts the info into a cmdargs struct.
* \@param argc number of arguments
* \@param argv array of argument strings
* \@param args structure to hold the parsed options & arguments
*/
void parse_args ( int argc, char **argv, struct cmdargs *args){
int c, option_index=0;
init_cmdargs(args);
while( (c = getopt_long(argc, argv, "$optstr", long_options, &option_index)) != -1 ){
switch (c) {
case '0':
if ( long_options[option_index].flag != 0 )
break;
__OPT_PARSE__
foreach my $arg ( @args ){
$optstr .= "\t\t\tcase '" . $arg->{shortopt} . "':\n" .
"\t\t\t\t" ;
if ( $arg->{type} eq 'flag' ){
$optstr .= 'args->' . $arg->{name} . " = 1;\n";
} elsif ( $arg->{type} eq 'var' ) {
$optstr .= 'args->' . $arg->{name} . " = optarg;\n";
} elsif ( $arg->{type} eq 'var_optional' ) {
$optstr .= 'if ( optarg ) args->' . $arg->{name} . " = optarg;\n";
$optstr .= 'else args->' . $arg->{name} . " = (char *) 1;\n";
} elsif ( $arg->{type} =~ /custom/ ) {
$optstr .= $arg->{parseopt_code} . ";\n";
} else {
print STDERR "WARNING: unknown type for arg ", $arg->{name}, "\n";
}
$optstr .= "\t\t\t\tbreak;\n";
}
$optstr .= "\t\t\tdefault: break;\n\t\t}\n\t}\n\treturn;\n}\n";
return $longopts . $optstr;
}
sub make_requirement_check {
my $rstr = '';
my $exitfunc = '';
my $hasrequired = 0;
foreach my $arg ( @args ){
if ( $arg->{required} && $arg->{type} !~ /flag/ ){
$hasrequired = 1;
$rstr .= "\n\tif ( args." . $arg->{name} . " == NULL ) {\n".
"\t\tfprintf(stderr, \"" . $arg->{longopt} .
" must be specified.\\n\");\n" .
"exit( EXIT_HELP );\n\t}\n";
}
if ( defined($arg->{main_code}) && $arg->{main_code} ne '' ){
$exitfunc .= "\n\tif ( args." . $arg->{name} . " ){\n\t\t" .
$arg->{main_code} .
"\n\t}\n";
}
}
$rstr = $exitfunc . $rstr;
return $rstr;
}
sub wrap_description {
# inserts escaped line breaks and spaces so that the result in usage() will
# print out wrapped.
my $lpad = shift;
my $width = shift;
my $text = shift;
if (length($text) <= $width) {
return $text;
}
my $wrapped_text = '';
my $current_line = '';
while ($text) {
$text =~ s/^(\s+)//;
my $current_ws = $1 || '';
$text =~ s/^(\S+)//;
my $current_word = $1;
if (length($current_line) + length($current_ws)
+ length($current_word) > $width) {
if ($wrapped_text) {
$wrapped_text .= '\\n ' . (' ' x $lpad);
}
$wrapped_text .= $current_line;
$current_line = $current_word;
} else {
$current_line .= $current_ws . $current_word;
}
}
return $wrapped_text . '\\n ' . (' ' x $lpad) . $current_line;
}
__DATA__
----%appname%_main.h----
/************************************
%appcopyright%
************************************/
/** @file %appname%_main.h
* @brief defines structure to hold command-line arguments and protypes
* some functions.
*
* generated by cgener. http://code.google.com/p/cgener/
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef %APPNAME%_MAIN_H
#define %APPNAME%_MAIN_H
/* user-defined things in arg table */
%user_defined_preproc%
/* end user-defined things */
/** exit code to use when invoked with usage() or version(). */
#define EXIT_HELP 1
/** exit code to use when everything was okay. */
#define EXIT_OKAY 0
/** exit code to use when there was an i/o-related error. */
#define EXIT_FILE_ERR -1
/** exit code to use when there was an error allocating memory. */
#define EXIT_MEM_ERR -2
/** current version of the application. */
#define APPLICATION_VERSION "%appversion%"
/** short description of what the app does. */
#define APPLICATION_DESCRIPTION "%appdescription%"
%args_struct%
/** parses commandline arguments and puts the info into a cmdargs struct.
* @param argc number of arguments
* @param argv array of argument strings
* @param args structure to hold the parsed options & arguments
*/
void parse_args ( int, char **, struct cmdargs *);
/** prints application help information.
* @param bin application name as invoked on the commandline
*/
void usage ( char * );
/** @brief prints application version.
*/
void version ( void );
/** @brief beginning of primary application logic section called by main().
*
* @param args contains the parsed cmd-line options & arguments.
* @param argc number of cmd-line arguments.
* @param argv list of cmd-line arguments
* @param optind index of the first non-option cmd-line argument.
*
* @return exit status for main() to return.
*/
int %appname%( struct cmdargs *args, int argc, char *argv[], int optind );
#endif
#ifdef __cplusplus
}
#endif
----main.%language%----
/************************************
%appcopyright%
************************************/
/** @file main.%language%
* @brief just contains the main() function.
*
* generated by cgener. http://code.google.com/p/cgener/
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "%appname%_main.h"
/** @brief entry point into the application.
* as this is a generated function, it just calls apropriate fucntions to
* parse commandlines, may do some user-defined code from the argument table,
* and calls user-defined function %appname%(), which is where all the real
* work will begin.
*
* @param argc number of command-line arguments.
* @param argv list of arguments.
*
* @return
*/
int main ( int argc, char *argv[] ){
struct cmdargs args;
parse_args ( argc, argv, &args );
%required_args%
return %appname%( &args, argc, argv, optind );
}
----usage.%language%----
/************************************
%appcopyright%
************************************/
/** @file usage.%language%
* @brief contains functions for parsing arguments and printing usage info.
*
* generated by cgener. http://code.google.com/p/cgener/
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "%appname%_main.h"
%usage_function%
/* this won't be used outside of this file */
void init_cmdargs( struct cmdargs * );
%parse_function%
/** @brief initializes all elements in the cmdargs struct to NULL or 0.
* @param args pointer to the argument structure to be cleared out.
*/
void init_cmdargs( struct cmdargs *args ){
/* this set all pointers to null, int values to 0 */
memset( args, 0, sizeof(struct cmdargs) );
}
/** @brief prints application version.
*/
void version ( void ){
fprintf(stderr, "%s\n", APPLICATION_VERSION);
}