-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.pl
executable file
·139 lines (107 loc) · 3.35 KB
/
rename.pl
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
#!/usr/bin/perl -w
use strict;
my $tests = 0;
my $out = '.';
my($prefix, $filename, $out_tests, $inline, $include, $tests_name, $header_guard, $lowercase_status, $no_usage);
my $usage = "usage: $0 [--out=<dir>] [--filename=<name>] [--out-tests=<dir>] [--with-tests=<testname>] [--without-usage] [--include=<include>] [--inline=<inline_func>] [--header-guard=<headerguard>] [--lowercase-status] <prefix>\n";
sub die_usage() { die $usage; }
foreach my $a (@ARGV) {
my $arg = $a;
if ($arg =~ /^--with-tests/) {
$tests = 1;
if ($arg =~ s/^--with-tests=//) {
$tests_name = $arg;
}
}
elsif ($arg =~ s/^--out=//) {
$out = $arg;
}
elsif ($arg =~ s/^--filename=//) {
$filename = $arg;
}
elsif ($arg =~ s/^--out-tests=//) {
$out_tests = $arg;
}
elsif ($arg =~ s/^--include=//) {
$include = $arg;
}
elsif ($arg =~ s/^--inline=//) {
$inline = $arg;
}
elsif ($arg =~ s/^--header-guard=//) {
$header_guard = $arg;
}
elsif ($arg !~ /^--/ && ! $prefix) {
$prefix = $arg;
}
elsif ($arg eq '--lowercase-status') {
$lowercase_status = 1;
}
elsif ($arg eq '--without-usage') {
$no_usage = 1;
}
elsif ($arg eq '--help') {
print STDOUT $usage;
exit;
}
else {
print STDERR "$0: unknown argument: $arg\n";
die_usage();
}
}
die_usage() unless $prefix;
$filename = $prefix unless($filename);
my $filename_upper = $filename;
$filename_upper =~ tr/a-z/A-Z/;
my $prefix_upper = $prefix;
$prefix_upper =~ tr/a-z/A-Z/;
$header_guard = "${filename_upper}_H" unless($header_guard);
translate("adopt.c", "${out}/${filename}.c");
translate("adopt.h", "${out}/${filename}.h");
if ($tests)
{
$out_tests = $out unless($out_tests);
if ($tests_name) {
$tests_name =~ s/::/_/g;
} else {
$tests_name = $prefix;
}
my $tests_filename = $tests_name;
$tests_filename =~ s/.*_//;
translate("tests/adopt.c", "${out_tests}/${tests_filename}.c");
}
sub translate {
my($in, $out) = @_;
open(IN, $in) || die "$0: could not open ${in}: $!\n";
my $contents = join('', <IN>);
close(IN);
$contents =~ s/\n\/\*\*\n( \*[^\n]*\n)* \*\/\nint adopt_usage_fprint\(.*?\);\n//s
if ($no_usage);
$contents =~ s/\nint adopt_usage_fprint.*}\n//s
if ($no_usage);
$contents =~ s/test_adopt__/test_${filename}__/g;
# if a prefix becomes foo_opt, we want to rewrite adopt_opt specially
# to avoid it becoming foo_opt_opt
$contents =~ s/adopt_opt/${prefix}/g if ($prefix =~ /_opt$/);
$contents =~ s/ifndef ADOPT_H/ifndef ${header_guard}/g;
$contents =~ s/define ADOPT_H/define ${header_guard}/g;
$contents =~ s/endif \/\* ADOPT_H/endif \/* ${header_guard}/g;
$contents =~ s/adopt\.h/${filename}\.h/g;
$contents =~ s/adopt_/${prefix}_/g;
$contents =~ s/ADOPT_/${prefix_upper}_/g;
$contents =~ s/fprintf\(file, "([A-Z])/fprintf\(file, "\l$1/g if($lowercase_status);
if ($include) {
$contents =~ s/^(#include "opt.h")$/#include "${include}"\n$1/mg;
}
if ($inline) {
$contents =~ s/^INLINE/${inline}/mg;
$contents =~ s/\n#ifdef _MSC_VER\n.*?\n#endif\n//s;
}
if ($tests) {
$contents =~ s/test_adopt__/test_${tests_name}__/g;
}
$contents =~ s/\n \*\/\n/\n *\n * THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT.\n *\n * This file was produced by using the `rename.pl` script included with\n * adopt. The command-line specified was:\n *\n * $0 @ARGV\n *\/\n/s;
open(OUT, '>' . $out) || die "$0: could not open ${out}: $!\n";
print OUT $contents;
close(OUT);
}