3 # Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 # Copyright (C) 2009-2010 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
6 # This file must be used under the terms of the CeCILL.
7 # This source file is licensed as described in the file COPYING, which
8 # you should have received as part of this distribution. The terms
9 # are also available at
10 # http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
17 # perl script directory
18 # ------------------------------------------------
20 my $directory = getcwd();
22 if( dirname($0) ne '.' )
24 $directory .= '/'.dirname($0);
25 $directory =~ s/\/\.\//\//g;
28 if( dirname($0) =~ m/^\// )
30 $directory = dirname($0);
34 # ------------------------------------------------
37 $SCI =~ s/\/modules\/development_tools\/src\/perl\/xml2test//g;
39 my $sci_modules_dir = $SCI.'/modules';
43 # ------------------------------------------------
45 $languages{'en_US'} = 1;
49 # ------------------------------------------------
53 $modules{$ARGV[0]} = 1;
57 %modules = get_module_list();
61 # ------------------------------------------------
64 $valid_tags{'File_gateway'} = 1;
65 $valid_tags{'File_scilab'} = 1;
66 $valid_tags{'Lib_name'} = 1;
67 $valid_tags{'Func_list'} = 1;
70 # ------------------------------------------------
75 # ==============================================================================
76 # First step : get the XML list
77 # ==============================================================================
79 foreach my $module (sort keys %modules)
81 foreach my $language (sort keys %languages)
83 my $this_directory = $sci_modules_dir.'/'.$module.'/help/'.$language;
85 if( -d $this_directory )
87 get_xml_list($this_directory,$module);
92 # ==============================================================================
93 # Second step : parse each XML file
94 # ==============================================================================
98 foreach my $xmlfile (sort keys %xmllist)
105 foreach my $xmlfile (sort keys %xmllist)
108 my $module = $xmllist{$xmlfile};
110 my $xmlfile_print = 'SCI/modules'.substr($xmlfile,length($sci_modules_dir));
111 printf('%04d/%04d - %s'."\n",$count,$xmllist_size,$xmlfile_print);
112 my %tags = get_tag_values($xmlfile);
115 # ==========================================================================
117 foreach my $tag (sort keys %valid_tags)
119 if( (! exists($tags{$tag}) ) || ($tags{$tag} eq '') )
121 print "\t".'ERROR : Tag "'.$tag.'" has not been found in the file "'.$xmlfile.'"'."\n";
125 if( $tag eq 'File_gateway' )
127 unless( $tags{$tag} =~ m/^SCI(.)*\.(c|cpp)$/ )
129 print "\t".'ERROR : Value of the tag "'.$tag.'" found in the file "'.$xmlfile.'" is not valid'."\n";
134 if( $tag eq 'File_scilab' )
136 unless( $tags{$tag} =~ m/^SCI(.)*\.(tst)$/ )
138 print "\t".'ERROR : Value of the tag "'.$tag.'" found in the file "'.$xmlfile.'" is not valid'."\n";
144 # Get the test content
145 # ==========================================================================
147 $tags{'scilab_code'} = get_scilab_code($xmlfile);
149 if( $tags{'scilab_code'} eq '' )
151 print "\t".'ERROR : The scilab code has not been found in the file "'.$xmlfile.'"'."\n";
155 # Get the gateway content
156 # ==========================================================================
158 $tags{'gateway_code'} = get_gateway_code($xmlfile);
160 if( $tags{'gateway_code'} eq '' )
162 print "\t".'ERROR : The gateway code has not been found in the file "'.$xmlfile.'"'."\n";
166 # write the gateway code
167 # ==========================================================================
169 my $gateway_file = $tags{'File_gateway'};
170 $gateway_file = $SCI . substr($gateway_file,3);
171 write_gateway_code($gateway_file,\%tags);
172 print "\t".'The file "'.$gateway_file.'" code has been created'."\n";
175 # write the scilab code
176 # ==========================================================================
178 my $scilab_file = $tags{'File_scilab'};
179 $scilab_file = $SCI . substr($scilab_file,3);
180 write_scilab_code($scilab_file,\%tags);
181 print "\t".'The file "'.$scilab_file.'" code has been created'."\n";
187 # ==============================================================================
189 # ==============================================================================
195 unless( chdir($sci_modules_dir) )
197 print 'The directory '.$sci_modules_dir.' doesn\'t exist or read access denied'."\n";
202 my @candidates = <*>;
204 foreach my $candidate (@candidates)
206 if( -e $sci_modules_dir.'/'.$candidate.'/help' )
208 $list{$candidate} = 1;
215 # ==============================================================================
217 # ==============================================================================
226 my $current_directory;
228 # On enregistre le répertoire dans lequel on se situe à l'entrée de la fonction
229 my $previous_directory = getcwd();
235 foreach my $list_dir (@list_dir)
237 $current_directory = getcwd();
239 if( (-d $list_dir) && ( ! ($list_dir =~ m/^scilab_[a-z][a-z]_[A-Z][A-Z]_help$/ )) )
241 get_xml_list($current_directory.'/'.$list_dir,$module);
245 && ($list_dir =~ m/\.xml$/)
246 && ($list_dir ne 'master.xml')
247 && ($list_dir ne 'master_help.xml') )
249 unless( open(XMLFILE,$list_dir) )
251 die 'get_xml_list: The file "'.$current_directory.'/'.$list_dir.'" cannot be opened'."\n";
259 if( $_ =~ /^<!--File_gateway:\s(.)+-->$/ )
261 $xmllist{$current_directory.'/'.$list_dir} = $module;
270 chdir($previous_directory);
273 # ==============================================================================
275 # ==============================================================================
282 unless( open(XMLFILE,$xmlfile) )
284 die 'get_tag_values: The file "'.$xmlfile.'" cannot be opened'."\n";
292 if( $_ =~ /^<!--[A-Z][a-z_]*:\s(.)+-->$/ )
299 my $start = index($_,':');
300 my $tag = substr($_,0,$start);
302 if( ! exists($valid_tags{$tag}) )
307 my $value = substr($_,$start+1);
311 $tags{$tag} = $value;
320 # ==============================================================================
322 # ==============================================================================
326 my $xmltree = XMLin($_[0]); # $_[0] : path absolu du fichier XML
328 my $refsections = $xmltree->{'refsection'};
329 my @fields = @$refsections;
331 foreach my $field (@fields)
333 if( (exists($field->{'programlisting'}->{'role'}) ) &&
334 ($field->{'programlisting'}->{'role'} eq 'code_gateway') )
336 $field->{'programlisting'}->{'content'} =~ s/\s+\n/\n/g;
337 return $field->{'programlisting'}->{'content'}."\n";
344 # ==============================================================================
346 # ==============================================================================
350 my $xmltree = XMLin($_[0]); # $_[0] : path absolu du fichier XML
352 my $refsections = $xmltree->{'refsection'};
353 my @fields = @$refsections;
355 foreach my $field (@fields)
357 if((exists($field->{'programlisting'}->{'role'}) )
358 && ($field->{'programlisting'}->{'role'} eq 'code_scilab'))
360 my $scilab_code = $field->{'programlisting'}->{'content'}."\n";
361 $scilab_code =~ s/\s+\n/\n/g;
362 $scilab_code =~ s/then(\s)+error\((\s)*\"failed\"(\s)*\)(\s)*[;,](\s)*end/then pause;end/g;
370 # ==============================================================================
372 # ==============================================================================
374 sub write_gateway_code
378 my %tags = %$tagsref;
380 unless( open(FILEOUT,'> '.$fileout) )
382 print "\t".'Le fichier "'.$fileout.'" n\'a pu être ouvert en écriture'."\n";
386 # Ecriture de l'entête
387 # ==========================================================================
389 unless( open(LICENSE,$directory.'/gateway_code_license.txt') )
391 print "\t".'Le fichier "'.$directory.'/gateway_code_license.txt" n\'a pu être ouvert en lecture'."\n";
402 # Ecriture des includes
403 # ==========================================================================
405 unless( open(INCLUDES,$directory.'/includes.txt') )
407 print "\t".'Le fichier "'.$directory.'/includes.txt" n\'a pu être ouvert en lecture'."\n";
419 # ==========================================================================
420 $tags{'gateway_code'} =~ s/\s+\n/\n/g;
421 print FILEOUT $tags{'gateway_code'};
423 # Fermeture du fichier de sortie
424 # ==========================================================================
428 # ==============================================================================
430 # ==============================================================================
432 sub write_scilab_code
436 my %tags = %$tagsref;
439 # ==========================================================================
441 my $TMPDIR = basename($fileout);
442 $TMPDIR =~ s/\.tst$//g;
443 $TMPDIR = 'pathconvert(TMPDIR+"/'.$TMPDIR.'")';
445 # table management (ilib_build 2nd input argument)
446 # ==========================================================================
448 my @functions = split(/,/,$tags{'Func_list'});
452 for( my $i=0 ; $i<length(@functions) ; $i++ )
459 $table_str .= '"'.$functions[$i].'","'.$functions[$i].'"';
466 # ==========================================================================
468 my $cfile = 'SCI+"'.substr($tags{'File_gateway'},3).'"';
470 my $cfiletmpdir = basename($fileout);
471 $cfiletmpdir =~ s/\.tst$//g;
472 $cfiletmpdir = 'pathconvert(TMPDIR+"/'.$cfiletmpdir.'/'.basename(substr($tags{'File_gateway'},3)).'",%F)';
474 # Open the fileout file
475 # ==========================================================================
477 unless( open(FILEOUT,'> '.$fileout) )
479 print "\t".'Le fichier "'.$fileout.'" n\'a pu être ouvert en écriture'."\n";
483 # Ecriture de l'entête
484 # ==========================================================================
486 unless( open(LICENSE,$directory.'/scilab_code_license.txt') )
488 print "\t".'Le fichier "'.$directory.'/scilab_code_license.txt" n\'a pu être ouvert en lecture'."\n";
499 # Ecriture de la ligne ilib_build
500 # ==========================================================================
502 print FILEOUT 'ilib_verbose(0);'."\n";
503 print FILEOUT 'mkdir('.$TMPDIR.');'."\n";
504 print FILEOUT 'cd('.$TMPDIR.');'."\n";
505 print FILEOUT 'copyfile('.$cfile.','.$cfiletmpdir.');'."\n";
506 print FILEOUT 'cflags = "-I"+SCI+"/modules/localization/includes";'."\n";
508 print FILEOUT 'ilib_build(';
509 print FILEOUT '"'.$tags{'Lib_name'}.'",'; # lib_name
510 print FILEOUT $table_str.','; # table
511 print FILEOUT '"'.basename(substr($tags{'File_gateway'},3)).'",'; # files
512 print FILEOUT '[],'; # libs
513 print FILEOUT '"",'; # makename
514 print FILEOUT '"",'; # ldflags
515 print FILEOUT 'cflags);'."\n"; # cflags
517 print FILEOUT 'exec("loader.sce");'."\n";
520 # ==========================================================================
521 print FILEOUT $tags{'scilab_code'};
523 # Fermeture du fichier de sortie
524 # ==========================================================================