-
Notifications
You must be signed in to change notification settings - Fork 11
/
gen_vcl.pl
executable file
·76 lines (61 loc) · 1.97 KB
/
gen_vcl.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
#!/usr/bin/perl
#
# Generates the VCL file from accept-language.c
# depending on your language preferences
#
use strict;
use warnings;
my @languages = @ARGV;
if (! @languages) {
die "Usage; $0 <default_language> <list-of-languages>\n" .
"example: $0 en de en es fr it no ru zh-cn ...\n";
}
my $delim = ":";
my $default_lang = shift @languages;
# Add default language to the list if there isn't already
if (! grep { $_ eq $default_lang } @languages ) {
unshift @languages, $default_lang;
}
# Build up list for easy matching
my $lang_list = join($delim, @languages);
$lang_list = ":$lang_list:";
my @c_code = <STDIN>;
my @vcl_code;
READ_C: while (my $line = shift @c_code) {
# Parse VCL ifdefs
if ($line =~ m{^ \s* \#(ifn?def) \s+ __VCL__}mx) {
#warn "\$1 = $1 \$line = $line\n";
my $include = $1 eq 'ifdef' ? 1 : 0;
IFDEF: while ($line = shift @c_code) {
#warn "\tread_line $line\n";
if ($line =~ m{^ \s* \#endif}mx) {
last IFDEF;
}
elsif ($line =~ m{^ \s* \#else}mx) {
$include = 1 - $include;
}
push @vcl_code, $line if $include;
}
next;
}
# Change the supported languages
if ($line =~ m{^ \s* \#define \s+ SUPPORTED_LANGUAGES}mx) {
push @vcl_code, qq{#define SUPPORTED_LANGUAGES "$lang_list"\n};
next;
}
if ($line =~ m{^ \s* \#define \s+ DEFAULT_LANGUAGE}mx) {
push @vcl_code, qq{#define DEFAULT_LANGUAGE "$default_lang"\n};
next;
}
push @vcl_code, $line;
}
@vcl_code = (
"C{\n\n",
"/* ------------------------------------------------------" . ("-" x length($0)) . " */\n",
"/* THIS FILE IS AUTOMATICALLY GENERATED BY $0. DO NOT EDIT. */\n\n",
@vcl_code,
"\n/* THIS FILE IS AUTOMATICALLY GENERATED BY $0. DO NOT EDIT. */\n",
"/* ------------------------------------------------------" . ("-" x length($0)) . " */\n",
"}C\n",
);
print @vcl_code;