File: mkcproc.pl

package info (click to toggle)
beast 0.7.4-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 33,500 kB
  • sloc: ansic: 197,348; cpp: 59,114; sh: 11,030; perl: 2,523; makefile: 2,474; xml: 1,026; lisp: 549; awk: 270; sed: 8
file content (318 lines) | stat: -rwxr-xr-x 8,484 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl -w

use Cwd 'abs_path';

my $gen_preprocess = 0;
my $gen_externs = 0;
my $gen_funcs = 0;
my $funcname = 0;

# parse options
while ($_ = $ARGV[0], defined $_ && /^-/) {
    shift;
    last if /^--$/;
    if (/^--preprocess$/) { $gen_preprocess = 1 }
    elsif (/^--externs$/) { $gen_externs = 1 }
    elsif (/^--funcname$/) { $funcname = shift }
    elsif (/^--functions$/) { $gen_funcs = 1 }
}

if (@ARGV < 1) {
    die "$0: missing file name\n";
}

sub ncanon {
    my $name = shift;
    $name =~ s/[^a-zA-Z0-9]/_/g;
    return $name;
}

sub func_name {
    my $name = shift;
    return "bse__builtin_init_". ncanon ($name) ."";
}

if ($gen_externs) {
    for (@ARGV) {
	print "BseExportNode* ". func_name ($_) ." (void);\n";
    }
    exit 0;
} elsif ($gen_funcs) {
    for (@ARGV) {
	print "  ". func_name ($_) .",\n";
    }
    exit 0;
} elsif (!$gen_preprocess) {
    exit 0;
}

if (@ARGV > 1) {
    die "$0: too many file names\n";
}

my $var_pattern = "HELP|OPTIONS|AUTHORS|LICENSE";
my %var_defs = ();
my %proc_defs = ();

print "\n/*\n * Generated data (by mkcproc.pl";
print ")\n */\n";

sub has_semicolon {
    my $line = shift;

    # eat up strings
    $line =~ s/\"([^\"])*\"//g;
    # eat up chars
    $line =~ s/\'([^\'])*\'//g;

    return m/;/;
}

sub print_assignment {
    my $assignment = shift;
    my $var = shift;
    my $val;
    my $vfile;
    my $vline;
    if (defined $proc_defs{$var}) {
	( $val, $vfile, $vline ) = @{$proc_defs{$var}};
    } elsif (defined $var_defs{$var}) {
	( $val, $vfile, $vline ) = @{$var_defs{$var}};
    } else {
	# print STDERR "no assignment for $var\n" ;
	return;
    }
    print "#line $vline \"$vfile\"\n";
    print "  $assignment = $val;\n";
}

sub get_variable {
    my $var = shift;
    my $fallback = shift;
    my $val;
    my $vfile;
    my $vline;
    if (defined $proc_defs{$var}) {
	( $val, $vfile, $vline ) = @{$proc_defs{$var}};
    } elsif (defined $var_defs{$var}) {
	( $val, $vfile, $vline ) = @{$var_defs{$var}};
    } else {
	$val = $fallback;
    }
    return "$val";
}
sub get_variable_file {
    my $var = shift;
    my $fallback = shift;
    my $val;
    my $vfile;
    my $vline;
    if (defined $proc_defs{$var}) {
	( $val, $vfile, $vline ) = @{$proc_defs{$var}};
    } elsif (defined $var_defs{$var}) {
	( $val, $vfile, $vline ) = @{$var_defs{$var}};
    } else {
	$vfile = $fallback;
    }
    return "$vfile";
}
sub get_variable_line {
    my $var = shift;
    my $fallback = shift;
    my $val;
    my $vfile;
    my $vline;
    if (defined $proc_defs{$var}) {
	( $val, $vfile, $vline ) = @{$proc_defs{$var}};
    } elsif (defined $var_defs{$var}) {
	( $val, $vfile, $vline ) = @{$var_defs{$var}};
    } else {
	$vline = $fallback;
    }
    return "$vline";
}

my $proc_name;
my $proc_method;
my $proc_category;
my $match_contents = 1;
my $externs = "";
my $last_node = "NULL";
my $line_jump = 1;
my $file = "";

while (<>) {
    my $type = 0;
    my $line = $.;
    $file = $ARGV;
    $afile = abs_path ($file);
    
    if (eof) {
	close (ARGV);          # reset line numbering
    }

    # read lines until comment end is matched
    while (m@/\*([^*]|\*[^/*])*\**$@x) {
        my $new = <>;
        (defined ($new) && ($file eq $ARGV)) or die "$file:$.: Unmatched comment\n";
        $_ .= $new; $line_jump = 1;
    }
    # strip comments
    if (s@/\*([^*]|\*[^/*])*\**\*/@@gx) {
	$line_jump = 1;
    }

    # find PROCEDURE() directive
    if (m@^\s*(METHOD|PROCEDURE)\s*\(@) {
	my $rest;
	if (defined $proc_name) {
	    die "$file:$.: METHOD/PROCEDURE within METHOD/PROCEDURE\n";
	}
        while (!m@\)@) {
	    my $new = <>;
	    (defined ($new) && ($file eq $ARGV)) or die "$file:$.: Invalid expression\n";
	    $_ .= $new; $line_jump = 1;
	}
	if (m@.*PROCEDURE\s*\( ([a-zA-Z0-9_-]*)\s* , \s*"([^\"\)]*)" \);?(.*)$@x) {
	    $rest = $3;
	    $proc_name = $1;
	    $proc_category = $2;
	    if ($proc_category =~ m@^/.*@ || $proc_category =~ m@.*/$@) {
		die "$file:$.: Procedure category contains extraneous slahses: \"$proc_category\"";
	    }
	    $proc_category = "\"" . "/Proc/" . $proc_category . "\"";
	    $proc_method = "\"" . $proc_name . "\"";
	} elsif (m@.*METHOD\s*\( ([a-zA-Z0-9_-]*)\s* , \s*([a-zA-Z0-9_-]*) , \s*"([^\"\)]*)" \);?(.*)$@x ||
		 m@.*METHOD\s*\( ([a-zA-Z0-9_-]*)\s* , \s*([a-zA-Z0-9_-]*) \);?(.*)$@x) {
	    my $type = $1;
	    my $subcat;
	    $proc_name = $2;
	    if (defined $4) {
		$rest = $4;
		$subcat = $3;
		if ($subcat =~ m@^/.*@ || $subcat =~ m@.*/$@) {
		    die "$file:$.: Method category contains extraneous slahses: \"$subcat\"";
		}
	    } else {
		$rest = $3;
		$subcat = $proc_name;
		$subcat =~ s/^(.)/\U$1/;
		$subcat =~ s/-(.)/ \U$1/g;
		$subcat = "General/" . $subcat;
	    }
	    $proc_method = "\"" . $type . "+" . $proc_name . "\"";
	    $proc_category = "\"" . "/Methods/" . $type . "/" . $subcat . "\"";
	} else {
	    die "$file:$.: Invalid METHOD/PROCEDURE directive\n";
	}

	print "/* --- $proc_name --- */\n";
	print "static void\n";
	print ncanon ($proc_name) . "_setup (BseProcedureClass *proc, ";
	print "GParamSpec **in_pspecs, GParamSpec **out_pspecs) {\n";
	print "#line $line \"$file\"\n$rest\n" if (defined $rest);

	$match_contents = 1; $line_jump = 1; next;
    }

    # find BODY directive
    if ($match_contents && m@[^\)]*BODY\s*\(@) {
	if (!defined $proc_name || !defined $proc_method) {
	    die "$file:$.: BODY() without PROCEDURE() or METHOD()\n";
	}
	while (!m@\)@) {
	    my $new = <>;
	    (defined ($new) && ($file eq $ARGV)) or die "$file:$.: Invalid expression\n";
	    $_ .= $new; $line_jump = 1;
	}
	if (!m@^(.*)BODY\s*\( ([^\)]*) \)\s*@x) {
	    die "$file:$.: Invalid BODY() directive\n";
	}

	$externs .= "static void\n";
	$externs .= "__enode_". ncanon ($proc_name) ."__fill_strings (BseExportStrings *es)\n";
        $externs .= "{\n";
	$externs .= "  es->blurb = ". get_variable ("HELP", "NULL") .";\n";
	$externs .= "  es->file = \"". get_variable_file ("HELP", "") ."\";\n";
	$externs .= "  es->line = ". get_variable_line ("HELP", "0") .";\n";
	$externs .= "  es->authors = ". get_variable ("AUTHORS", "NULL") .";\n";
	$externs .= "  es->license = ". get_variable ("LICENSE", "NULL") .";\n";
        $externs .= "}\n";
	$externs .= "static BseExportNodeProc __enode_". ncanon ($proc_name) ." = {\n";
	$externs .= "  { $last_node, BSE_EXPORT_NODE_PROC,\n";
	$externs .= "    $proc_method, \n";
	$externs .= "    ". get_variable ("OPTIONS", "NULL") .",\n";
	$externs .= "    $proc_category,\n";
	$externs .= "    NULL,\n"; # pixmaps
	$externs .= "    __enode_". ncanon ($proc_name) ."__fill_strings,\n";
        $externs .= "  },\n";
	$externs .= "  0, ";                               # private_id
	$externs .= ncanon ($proc_name) . "_setup, ";      # init
	$externs .= ncanon ($proc_name) . "_exec, ";       # exec
	$externs .= "\n};\n";
	$last_node = "(BseExportNode*) &__enode_". ncanon ($proc_name);

	print "#line $line \"$file\"\n$1 }\n";
	print "static BseErrorType\n";
	print "#line $line \"$file\"\n";
	print ncanon ($proc_name) . "_exec (" . $2 . ")\n";
	
	undef %proc_defs;
	undef $proc_name;
	
	$match_contents = 0; $line_jump = 1; next;
    }
    
    # read variables lines
    if ($match_contents && m@[^;]*\b($var_pattern)\s*=@) {
	my $var = $1;
	while (!has_semicolon ($_)) {
	    my $new = <>;
	    (defined ($new) && ($file eq $ARGV)) or die "$file:$.: Invalid expression\n";
	    $_ .= $new; $line_jump = 1;
	}
	$value = $_;
	$value =~ s/^.*$var[^=]*=\s*//;
	$value =~ s/;\s*$//;
	if (defined $proc_name) {
	    $proc_defs{$var} = [ $value, $afile, $line ];
	} else {
	    $var_defs{$var} = [ $value, $afile, $line ];
	}

	$match_contents = 1; $line_jump = 1; next;
    }

    # warn verbose
    if (m@\b($var_pattern|BODY|PROCEDURE|METHOD)@) {
	print STDERR "$file:$.: warning: reserved word `$1' remains unmatched\n";
    }

    # rewrite parameter assignments
    if ($match_contents && m@^\s*(IN|OUT)\s*=@) {
	$_ =~ s/^\s*IN\s/  \*\(in_pspecs++\) /;
	$_ =~ s/^\s*OUT\s/  \*\(out_pspecs++\) /;
    }
    
    # normal line dump
    if ($line_jump) {
	print "#line $line \"$file\"\n";
	$line_jump = 0;
    }
    print $_;
}


# BSE export stuff
print "\n";
print "/* --- Export to BSE --- */\n";
print $externs;
my $func = func_name ($file);
if ($funcname) {
    $func = func_name ($funcname);
}
print "BseExportNode* $func (void);\n";
print "BseExportNode* $func (void)\n{\n  return $last_node;\n}\n";


print "\n/*\n * Generated data ends here\n */\n";