File: const_str_hash

package info (click to toggle)
rtpengine 13.5.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,676 kB
  • sloc: ansic: 86,764; perl: 59,422; python: 3,193; sh: 1,030; makefile: 693; asm: 211
file content (240 lines) | stat: -rwxr-xr-x 5,748 bytes parent folder | download
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
#!/usr/bin/perl

use strict;
use warnings;
use IPC::Open2;
use IPC::Cmd qw[can_run];

can_run('gperf') or die 'No gperf binary found, make sure to have gperf installed!';

my (%defines_0, %defines_1, %defines_2, %defines_4);

{
	my @argv;
	for my $arg (@ARGV) {
		if ($arg !~ /^-/) {
			push(@argv, $arg);
			next;
		}
		if ($arg =~ /^-D(.*?)=(.*)$/) {
			$defines_0{$1} = $2;
			next;
		}
		if ($arg =~ /^-D(.*?)$/) {
			$defines_0{$1} = '1';
			next;
		}
	}

	@ARGV = @argv; ## no critic (RequireLocalizedPunctuationVars)
}

print("/******** GENERATED FILE ********/\n");

my $rewritten_input = '';
my @sections;
my @slots;
my $ifdefs = 0;

# collect keywords and rewrite input file with in lookup keys

sub def_sub {
	my ($subs, @vals) = @_;
	my $i = 0;
	my $repl = $subs->[$i++];
	for my $r (@vals) {
		my $key = $subs->[$i++];
		$repl =~ s/(\W|^)\Q$key\E(\W|$)/$1$r$2/g;
	}
	return $repl;
}

while (my $line = <STDIN>) {
	my $num = scalar(@sections);
	my $new_section;
	if ($line =~ /CSH_SECTION|CSH_LOOKUP|CSH_NUM_LOOKUPS/
			&& $line =~ /^\s*#define\s+(\w+)(?:\s+(.*?)|\(\s*(\w+)\s*\)\s+(.*?)|\(\s*(\w+)\s*,\s*(\w+)\s*\)\s+(.*?)|\(\s*(\w+)\s*,\s*(\w+)\s*,\s*(\w+)\s*,\s*(\w+)\s*\)\s+(.*?))$/) {
		if ($2) {
			$defines_0{$1} = $2;
		}
		elsif ($3) {
			$defines_1{$1} = [$4, $3];
		}
		elsif ($5) {
			$defines_2{$1} = [$7, $5, $6];
		}
		elsif ($8) {
			$defines_4{$1} = [$12, $8, $9, $10, $11];
		}
		$rewritten_input .= "\n";
		next;
	}
	if ($line =~ /^\s*#endif/) {
		if ($ifdefs) {
			$ifdefs--;
		}
		$rewritten_input .= $line;
		next;
	}
	if ($line =~ /^\s*#if(n?)def\s+(\S+)/) {
		if ((!$1 && !$defines_0{$2}) || ($1 && $defines_0{$2}) || $ifdefs) {
			$ifdefs++;
		}
		$rewritten_input .= $line;
		next;
	}
	if ($line =~ /^\s*#if\s+/) {
		if ($ifdefs) {
			$ifdefs++;
		}
		$rewritten_input .= $line;
		next;
	}
	if ($ifdefs) {
		$rewritten_input .= $line;
		next;
	}
	for my $def (keys(%defines_0)) {
		my $sub = $defines_0{$def};
		$line =~ s/(\W|^)\Q$def\E(\W|$)/$1$sub$2/g;
	}
	for my $def (keys(%defines_1)) {
		my $subs = $defines_1{$def};
		$line =~ s/(\W|^)\Q$def\E\(\s*(\S+|"[^"]*")\s*\)(\W|$)/$1 . def_sub($subs, $2) . $3/eg;
	}
	for my $def (keys(%defines_2)) {
		my $subs = $defines_2{$def};
		$line =~ s/(\W|^)\Q$def\E\(\s*(\S+|"[^"]*")\s*,\s*(\S+|"[^"]*")\s*\)(\W|$)/$1 . def_sub($subs, $2, $3) . $4/eg;
	}
	for my $def (keys(%defines_4)) {
		my $subs = $defines_4{$def};
		$line =~ s/(\W|^)\Q$def\E\(\s*(\S+|"[^"]*")\s*,\s*(\S+|"[^"]*")\s*,\s*(\S+|"[^"]*")\s*,\s*(\S+|"[^"]*")\s*\)(\W|$)/$1 . def_sub($subs, $2, $3, $4, $5) . $6/eg;
	}
	if (($line =~ s/(__csh_lookup)(\s*\()/$1_$num$2/)) {
		$new_section = 0;
	}
	elsif (($line =~ s/(__csh_lookup)_n(\s*\()\s*(\d+)\s*,\s*/$1_$num$2/)) {
		$new_section = $3;
	}
	elsif (($line =~ s/CSH_SECTION/$num/)) {
		$new_section = 0;
	}
	if (defined($new_section)) {
		$rewritten_input .= $line;
		my $section = { keys => '', vals => {}, num => $num };
		push(@sections, $section);
		$slots[$new_section] = $num;
		next;
	}
	if ($line =~ s/CSH_NUM_LOOKUPS/{}/) {
		my $section = $sections[$slots[0]];
		die unless $section;
		my $n = values(%{$section->{vals}});
		$line =~ s/{}/$n/;
		$rewritten_input .= $line;
		next;
	}
	my ($rewrite, $key);
	if ($line =~ s/CSH_LOOKUP\(\s*"(.*?)"\s*\)/{}/) {
		$rewrite = 0;
		$key = $1;
	}
	elsif ($line =~ s/CSH_LOOKUP_N\(\s*(\d+)\s*,\s*"(.*?)"\s*\)/{}/) {
		$rewrite = $1;
		$key = $2;
	}
	if (!defined($rewrite)) {
		$rewritten_input .= $line;
		next;
	}
	my $section = $sections[$slots[$rewrite]];
	die unless $section;
	if (exists($section->{vals}{$key})) {
		$line =~ s/{}/$section->{vals}{$key}/;
	}
	else {
		my $iter = values(%{$section->{vals}});
		$line =~ s/{}/$iter/;
		$section->{keys} .= "$key,$iter\n";
		$section->{vals}{$key} = $iter;
	}
	$rewritten_input .= $line;
}

# pass collected outputs to gperf

print "struct __csh_hash_lookup { char *name; int num; };\n";

for my $section (@sections) {
	my $num = $section->{num};

	my ($rd, $wr);
	my $pid = open2($rd, $wr, qw(gperf -t -E -l -c -I -C -H), "__csh_hash_$num", '-N', "__csh_lookup_raw_$num");

	# gperf header and keys

	print { $wr } "struct __csh_hash_lookup;\n%%\n";
	print { $wr } $section->{keys};

	# read gperf output

	close($wr);
	my $hash_func_code;
	{
		local $/ = undef;
		$hash_func_code = <$rd>;
	}
	close($rd);
	waitpid($pid, 0);
	exit(1) if $?;

	# convert lookup function to static

	$hash_func_code =~ s/(^|\s)(const\s+)?struct\s+__csh_hash_lookup\s*\*/\nstatic$&/s;

	# print combined output

	print "#pragma GCC diagnostic push\n";
	print "#pragma GCC diagnostic ignored \"-Wmissing-field-initializers\"\n";
	print "#pragma GCC diagnostic ignored \"-Wshadow=global\"\n";
	print $hash_func_code;
	print "#pragma GCC diagnostic pop\n";
	# add convenience function
	print <<END;
#include "str.h"
#pragma GCC diagnostic ignored \"-Wunused-function\"
static int __csh_lookup_$num(const str *s) {
	if (!s->s)
		return -1;
	const struct __csh_hash_lookup *h = __csh_lookup_raw_$num(s->s, s->len);
	if (!h)
		return -1;
	return h->num;
}
#pragma GCC diagnostic pop
END
}

print("static const struct __csh_hash_lookup *(*__csh_lookup_funcs[])(const char *, size_t) = {\n");
for my $section (@sections) {
	print("\t__csh_lookup_raw_$section->{num},\n");
}
print <<END;
};
#pragma GCC diagnostic ignored \"-Wunused-function\"
static int __csh_lookup_section(unsigned int n, const str *s) {
	if (n >= G_N_ELEMENTS(__csh_lookup_funcs))
		return -1;
	if (!s->s)
		return -1;
	const struct __csh_hash_lookup *h = __csh_lookup_funcs[n](s->s, s->len);
	if (!h)
		return -1;
	return h->num;
}
#pragma GCC diagnostic pop
END

# adjust diagnostic line numbers and originating file
print $ARGV[0] ? "#line 1 \"$ARGV[0]\"\n" : "#line 1\n";
print $rewritten_input;