File: pltclgen

package info (click to toggle)
plplot 5.10.0%2Bdfsg2-0.4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 25,792 kB
  • ctags: 13,517
  • sloc: ansic: 83,001; xml: 27,081; ada: 18,878; cpp: 15,966; tcl: 11,651; python: 7,075; f90: 7,058; ml: 6,974; java: 6,665; perl: 5,029; sh: 2,208; makefile: 210; lisp: 75; sed: 25; fortran: 7
file content (372 lines) | stat: -rwxr-xr-x 11,275 bytes parent folder | download | duplicates (4)
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
#!/usr/local/bin/perl
# $Id: pltclgen 8681 2008-08-19 19:26:48Z andrewross $
#
# Geoffrey Furnish
# Institute for Fusion Studies
# University of Texas at Austin
# 27 June 1995
#
# This script is used to automatically generate most of the functions needed
# to implement the PLplot Tcl API.  It reads a file which specifies the
# PLplot API command arguments and return value (basically like a C
# prototype, but easier to parse) and generates a Tcl command procedure to
# call that function.
#
# Currently it can support arguments of type PLINT, PLFLT, char *, PLINT&
# and PLFLT& where the last two are used for the 'g' series functions which
# provide data to the caller.
#
# What is not supported at this time, but needs to be, is support for (those
# few) PLplot API functions with non-void return types, and (the many)
# PLplot API functions which accept arrays (PLFLT *, etc).  The latter can
# in many cases be correctly treated as 1-d Tcl Matricies.  The code for
# using these should be sufficiently perfunctory to be amenable to an
# approach like that used here.  Automatic support for the 2-d API is
# probably unrealistic.
###############################################################################

require "getopts.pl";

&Getopts('v');

$verbose = $opt_v;

# Find the source tree directory that must be specified on the command line.
$sourcedir = $ARGV[0];
$specfile = "$sourcedir/plapi.tpl";	# PLplot API template specification file.
$genfile  = "tclgen.c";			# Generated functions go here.
$genhead  = "tclgen.h";			# Prototypes for generated functions.
$genstruct= "tclgen_s.h";		# Initializers for CmdInfo struct.
$cmdfile  = "$sourcedir/tclcmd.tpl";	# Template file for generated functions.

open( SPECFILE, "<$specfile") || die "Can't open PLplot API spec file.";
open( GENFILE,  ">$genfile" ) || die "Can't open output file.";
open( GENHEAD,  ">$genhead" ) || die "Can't open output header file.";
open( GENSTRUCT,">$genstruct")|| die "Can't open second output header file.";

# Scan the PLplot API template specification file looking for function
# "prototypes".  These are introduced with the token "pltclcmd".  When
# we find one, go process it.  Anything other than a comment or a
# valid function "prototype" is considered an error, and is printed to
# stdout. 

while( <SPECFILE> ) {
    chomp;			# skip the \n.

    if (/([^\#]*)\#.*/) {	# Discard # to end of line.
        $_ = $1;
    }
    next if /^\s*$/;            # Discard empty lines.

    if (/^pltclcmd (\w+) (.*)/) {
	$cmd = $1;
	$rtype = $2;

	&process_pltclcmd( $cmd, $rtype );
	next;
    }

# Just print the unrecognized output to stdout.

    print "$_\n";
}

# Process a function "prototype".  Suck up the args, then perform the
# needed substitutions to the Tcl command procedure template.
# Generate the three outputs needed for use in tclAPI.c:  the C
# function prototype, the CmdInfo structure initializer, and the
# actual function definition.

sub process_pltclcmd {
    local( $cmd, $rtype ) = @_;
    local( $i, $refargs );
    local( $vname, $vtype );
    local( $args );

    print "autogenerating Tcl command proc for $rtype $cmd ()\n" if $verbose;

    print GENHEAD "static int ${cmd}Cmd( ClientData, Tcl_Interp *, int, char **);\n";
    print GENSTRUCT "    {\"$cmd\",          ${cmd}Cmd},\n";

    $args = "";
    $nargs = 0;
    $ndefs = 0;
    $refargs = 0;
    while( <SPECFILE> ) {
	chomp;

	last if /^$/;
	if (/^(\w+)\s+(.*)$/) {
	    $vname = $1;
	    $vtype = $2;
	    $defval = "";
	    print "vname=$vname vtype=$vtype\n" if $verbose;
	    if ($vtype =~ /\s*(.*)\s+Def:\s+(.*)/) {
		$vtype = $1;
		$defval= $2;
		print "default arg: vtype=$vtype defval=$defval\n" if $verbose;
	    }
	    $argname[ $nargs ] = $vname;
	    $argtype[ $nargs ] = $vtype;
	    $argdef[  $nargs ] = $defval;
	    $argref[  $nargs ] = 0; # default.

	# Check to see if this arg is for fetching something from PLplot.

	    if ($vtype =~ /&/ || $vtype eq "char *") {
		$refargs = 1;
		$argref[ $nargs ] = 1;
	    }

	    if ($nargs == 0) {
		$args .= "$vname";
	    } else {
		$args .= " $vname";
	    }
	    $ndefs++ if $defval ne "";
	    $nargs++;
	    next;
	}

    # Unrecognized output.

	print "bogus: $_\n";
    }

    if ($verbose) {
	print "There are $nargs arguments, $ndefs are defaultable.\n";
	for( $i=0; $i < $nargs; $i++ ) {
	    print "$argtype[$i] $argname[$i]\n";
	}
	print "return string required.\n" if $refargs;
    }

    open( TEMPLATE, "<$cmdfile" ) || die "Can't open tcl cmd template file.";

    while( <TEMPLATE> ) {

    # Emit the argument declarations.  Reference args require special
    # handling, the others should be perfunctory.

	if (/^<argdecls>$/) {
	    for( $i=0; $i < $nargs; $i++ ) {
		$_ = $argtype[$i];
	      argdecl: {
		  /PLINT&/ && do {
		      print GENFILE "    PLINT $argname[$i];\n";
		      last argdecl;
		  };
		  /PLUNICODE&/ && do {
		      print GENFILE "    PLUNICODE $argname[$i];\n";
		      last argdecl;
		  };
		  /PLFLT&/ && do {
		      print GENFILE "    PLFLT $argname[$i];\n";
		      last argdecl;
		  };
		  /char&/ && do {
		      print GENFILE "    char $argname[$i];\n";
		      last argdecl;
		  };
		  /PLINT \*/ && do {
		      print GENFILE "    PLINT *$argname[$i];\n";
		      print GENFILE "    tclMatrix *mat$argname[$i];\n";
		      last argdecl;
		  };
		  /PLUNICODE \*/ && do {
		      print GENFILE "    PLUNICODE *$argname[$i];\n";
		      print GENFILE "    tclMatrix *mat$argname[$i];\n";
		      last argdecl;
		  };
		  /PLFLT \*/ && do {
		      print GENFILE "    PLFLT *$argname[$i];\n";
		      print GENFILE "    tclMatrix *mat$argname[$i];\n";
		      last argdecl;
		  };
		  /const char \*/ && do {
		      print GENFILE "    char *$argname[$i];\n";
		      last argdecl;
		  };
		  /char \*/ && do {
		      print GENFILE "    char $argname[$i]\[200\];\n";
		      last argdecl;
		  };
		  if ($argdef[$i] ne "") {
		      print GENFILE "    $argtype[$i] $argname[$i] = $argdef[$i];\n";
		  } else {
		      print GENFILE "    $argtype[$i] $argname[$i];\n";
		  }
	      }
	    }
	    next;
	}

    # Obtain the arguments which we need to pass to the PLplot API call,
    # from the argc/argv list passed to the Tcl command proc.  Each
    # supported argument type will need special handling here.

	if (/^<getargs>$/) {
	    for( $i=0; $i < $nargs; $i++ ) {
		$_ = $argtype[$i];
		if ($ndefs) {
		    print GENFILE "    if (argc > $i+1) {\n    ";
		}
	      getarg: {
		  $argref[$i] && do {
		      print GENFILE "/* $argname[$i] is for output. */\n";
		      last getarg;
		  };
		  /PLINT \*/ && do {
		      print GENFILE "    mat$argname[$i] = Tcl_GetMatrixPtr( interp, argv[1+$i] );\n";
		      print GENFILE "    if (mat$argname[$i] == NULL) return TCL_ERROR;\n";		      
		      print GENFILE "    $argname[$i] = mat$argname[$i]-\>idata;\n";
		      last getarg;
		  };
		  /PLUNICODE \*/ && do {
		      print GENFILE "    mat$argname[$i] = Tcl_GetMatrixPtr( interp, argv[1+$i] );\n";
		      print GENFILE "    if (mat$argname[$i] == NULL) return TCL_ERROR;\n";		      
		      print GENFILE "    $argname[$i] = mat$argname[$i]-\>idata;\n";
		      last getarg;
		  };
		  /PLFLT \*/ && do {
		      print GENFILE "    mat$argname[$i] = Tcl_GetMatrixPtr( interp, argv[1+$i] );\n";
		      print GENFILE "    if (mat$argname[$i] == NULL) return TCL_ERROR;\n";		      
		      print GENFILE "    $argname[$i] = mat$argname[$i]-\>fdata;\n";
		      last getarg;
		  };
		  /PLINT/ && do {
		      print GENFILE "    $argname[$i] = atoi(argv[1+$i]);\n";
		      last getarg;
		  };
		  /PLUNICODE/ && do {
		      print GENFILE "    $argname[$i] = strtoul(argv[1+$i],NULL,10);\n";
		      last getarg;
		  };
		  /PLFLT/ && do {
		      print GENFILE "    $argname[$i] = atof(argv[1+$i]);\n";
		      last getarg;
		  };
		  /const char \*/ && do {
		      print GENFILE "    $argname[$i] = argv[1+$i];\n";
		      last getarg;
		  };
		  /char/ && do {
		      print GENFILE "    $argname[$i] = argv[1+$i][0];\n";
		      last getarg;
		  };
		  die "Unrecognized argtype :$_:\n";
	      }
		if ($ndefs) {
		    print GENFILE "    }\n";
		}
	    }
	    next;
	}

    # Call the PLplot API function.

	if (/^<plcmd>$/) {
	    print GENFILE "    $cmd ( ";
	    for( $i=0; $i < $nargs; $i++ ) {
		$_ = $argtype[$i];
	      passarg: {
		  /&/ && do {
		      print GENFILE "&$argname[$i]";
		      last passarg;
		  };
		  print GENFILE "$argname[$i]";
	      }
		if ($i < $nargs-1) {
		    print GENFILE ", ";
		}
	    }
	    print GENFILE " );\n";
	    next;
	}

    # If there were reference arguments, fetch their contents and stuff them
    # into their corresponding Tcl variables.

    # NOTE: This should be improved so take into account the current value
    # of tcl_precision.

	if (/^<fetch_result>$/) {
	    if ($refargs) {
		for( $i=0; $i < $nargs; $i++ ) {
		    next if !$argref[$i];
		    if ($i > 0) {
			print GENFILE "    if (argc == 1)\n";
			print GENFILE "        Tcl_AppendResult( interp, \" \", (char *) NULL );\n";
		    }
		    $_ = $argtype[$i];
		  fetch: {
		      /PLINT&/ && do {
			  print GENFILE "    sprintf( buf, \"%d\", $argname[$i] );\n";
			  print GENFILE "    if (argc > 1)\n";
			  print GENFILE "        Tcl_SetVar( interp, argv[1+$i], buf, 0 );\n";
			  print GENFILE "    else\n";
			  print GENFILE "        Tcl_AppendResult( interp, buf, (char *) NULL );\n";
			  last fetch;
		      };

		      /PLUNICODE&/ && do {
			  print GENFILE "    sprintf( buf, \"%u\", $argname[$i] );\n";
			  print GENFILE "    if (argc > 1)\n";
			  print GENFILE "        Tcl_SetVar( interp, argv[1+$i], buf, 0 );\n";
			  print GENFILE "    else\n";
			  print GENFILE "        Tcl_AppendResult( interp, buf, (char *) NULL );\n";
			  last fetch;
		      };

		      /char \*/ && do {
			  print GENFILE "    if (argc > 1)\n";
			  print GENFILE "       Tcl_SetVar( interp, argv[1+$i], $argname[$i], 0 );\n";
			  print GENFILE "    else\n";
			  print GENFILE "        Tcl_AppendResult( interp, $argname[$i], (char *) NULL );\n";
			  last fetch;
		      };

		      /char&/ && do {
			  print GENFILE "    sprintf( buf, \"%c\", $argname[$i] );\n";
			  print GENFILE "    if (argc > 1)\n";
			  print GENFILE "        Tcl_SetVar( interp, argv[1+$i], buf, 0 );\n";
			  print GENFILE "    else\n";
			  print GENFILE "        Tcl_AppendResult( interp, buf, (char *) NULL );\n";
			  last fetch;
		      };

		  # The following needs to be corrected to work with the Tcl
		  # precision standard (global var tcl_precision).

		      /PLFLT&/ && do {
			  print GENFILE "    sprintf( buf, \"%f\", $argname[$i] );\n";
			  print GENFILE "    if (argc > 1)\n";
			  print GENFILE "        Tcl_SetVar( interp, argv[1+$i], buf, 0 );\n";
			  print GENFILE "    else\n";
			  print GENFILE "        Tcl_AppendResult( interp, buf, (char *) NULL );\n";
			  last fetch;
		      };
		      print GENFILE "Unsupported arg type.\n";
		  }
		}
	    }
	    next;
	}

    # substitutions here...

	$_ =~ s/%cmd%/$cmd/g;
	$_ =~ s/%nargs%/$nargs/g;
	if ($refargs) {
	    $_ =~ s/%args%/\?$args\?/g;
	} else {
	    $_ =~ s/%args%/$args/g;
	}
	$_ =~ s/%ndefs%/$ndefs/g;
	$_ =~ s/%isref%/$refargs/g;

	print GENFILE $_;
    }

    close( TEMPLATE );
}