source: git/doc/doc2tex.pl @ 472f39

spielwiese
Last change on this file since 472f39 was acfbb5a, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* as we go along git-svn-id: file:///usr/local/Singular/svn/trunk@3234 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100755
File size: 21.5 KB
Line 
1#!/usr/local/bin/perl
2# $Id: doc2tex.pl,v 1.3 1999-07-06 11:58:09 obachman Exp $
3###################################################################
4#  Computer Algebra System SINGULAR
5#
6#  doc2tex: utility to generate the Singular manual
7#
8####
9# @c example [error]
10#    -> the text till the next @c example is feed into Singular,
11#       the text is then substituted by
12#       @c computed example $example $doc_file:$line
13#       <text from the input>
14#       @expansion{} <corresponding output>
15#       ....
16#       @c end computed example $example $doc_file:$line
17#       reuse computed examples if ($reuse && -r $example.inc)
18#       cut absolute directory names from the loaded messages
19#       substituted @,{ and } by @@, @{ resp. @}
20#       wrap around output lines  longer than $ex_length = 73;
21#       Processing is aborted if error occures in Singular run,
22#       unless 'error' is specified
23#
24####
25# @c include file
26#    -> copy content of file into output file protecting texi special chars
27#
28####
29# @c ref
30# ....
31# @c ref
32#    -> scans intermediate lines for @ref{..} strings
33#    Creates menu of (sorted) refs for ifinfo
34#    Creates comma-separated (sorted) refs for iftex, prepended with
35#    the text before first @ref.
36#
37####
38# @c lib libname.lib[:proc] [no_ex, lib_fun, lib_ex]
39#   Without :proc
40#   --> includes info of libname.lib in output file
41#   --> includes function names of info into function index
42#   --> if lib_fun is given, includes listing of functions and
43#                      their help into output file
44#   --> if lib_ex is given, includes computed examples of functions, as well
45#   With :proc
46#   --> includes content of procedure 'proc' from library libname:lib
47#
48#   Optional no_ex, lib_fun, lib_ex arguments overwrite respective
49#    command-line arguments
50#
51#
52###################################################################
53
54#
55# default settings of command-line arguments
56#
57$Singular = "../Singular/Singular"; # should be overwritten
58$libparse = "../Singular/libparse"; # change in final version
59$Singular_opts = " -teqr12345678";
60$clean = 0;
61$verbose = 1;
62$reuse = 1;
63$no_ex = 0;
64$lib_fun = 0;
65$lib_ex = 0;
66$doc_subdir = "./d2t_singular";
67@include_dirs = (".", "../Singular/LIB");
68$doc_examples_db = "$doc_subdir/examples";
69
70#
71# misc. defaults
72#
73$ex_length = 73;
74$ERROR = "\n**** Error:";
75$WARNING = "\n** Warning:";
76
77#
78# flush stdout and stderr after every write
79#
80select(STDERR);
81$| = 1;
82select(STDOUT);
83$| = 1;
84
85#
86# get file to convert
87#
88$doc_file = pop(@ARGV);
89if ($doc_file =~  /(.+)\..*$/)
90{
91  die "*** Error: Need .doc file as input\n" . &Usage 
92    unless ($doc_file =~ /(.+)\.doc$/);
93}
94else
95{
96  if ($doc_file =~ /^-h(elp)?$/) { print &Usage; exit;}
97  $doc_file .= ".doc";
98}
99
100#
101# parse command line options
102#
103$args = join(" ", @ARGV);
104while (@ARGV && $ARGV[0] =~ /^-/) 
105{
106  $_ = shift(@ARGV);
107  if (/^-S(ingular)?$/)  { $Singular = shift(@ARGV); next;}
108  if (/^-l(ibparse)?$/)  { $libparse = shift(@ARGV); next;}
109  if (/^-o(output)?$/)   { $tex_file = shift(@ARGV); next;}
110  if (/^-no_r(euse)?$/)  { $reuse = 0; next;}
111  if (/^-c(lean)?$/)     { $clean = 1; next;}
112  if (/^-no_e(x)?$/)     { $no_ex = 1; next;}
113  if (/^-lib_fu(n)?$/)   { $lib_fun = 1;next;}
114  if (/^-lib_e(x)?$/)    { $lib_ex = 1; next;}
115  if (/^-s(ubdir)?$/)    { $doc_subdir = shift(@ARGV); next;}
116  if (/^-I$/)            { unshift(@include_dirs, shift(@ARGV)); next;}
117  if (/^-v(erbose)?$/)   { $verbose = shift(@ARGV); next;}
118  if (/^-h(elp)?$/)      { print &Usage; exit;}
119  die &Usage;
120}
121$verbose = ($verbose < 0 ? 0 : $verbose);
122
123#
124# construct filenames
125#
126($doc_dir = $doc_file) =~ s/(.+)\.doc$/$1/;
127if ($doc_dir =~ /(.*)\//)
128{
129  $doc = $';
130  $doc_dir = $1;
131}
132else
133{
134  $doc = $doc_dir;
135  $doc_dir = ".";
136}
137$tex_file = "$doc_dir/$doc.tex" unless ($tex_file);
138               
139#
140# open files
141#
142open(DOC, "<$doc_file") 
143  || Error("can't open $doc_file for reading: $!\n" . &Usage);
144open(TEX, ">$tex_file") || Error("can't open $tex_file for writing: $!\n");
145print "d2t: Generating $tex_file from $doc_file ...\n" if ($verbose > 1);
146print "d2t: $doc_file ==> $tex_file\n" if ($verbose == 1);
147if (-d $doc_subdir)
148{
149  print "d2t: Using $doc_subdir for intermediate files\n" 
150    if ($verbose > 1);
151}
152else
153{
154  mkdir($doc_subdir, oct(755)) 
155    || Error("can't create directory $doc_subdir: $!\n");
156  print "d2t: Created $doc_subdir for intermediate files\n" 
157    if ($verbose > 1);
158}
159
160dbmopen(%EXAMPLES, $doc_examples_db, oct(755)) || die "$ERROR: can't open examples data base: $!\n";
161
162#######################################################################
163#
164# go !
165#
166while (<DOC>)
167{
168  $line++;
169 
170  if (/^\@c\s*example/)     {&HandleExample; next;}
171  if (/^\@c\s*include\s+/)  {&HandleInclude; next;}
172  if (/^\@c\s*ref\s*$/)     {&HandleRef; next;}
173  if (/^\@c\s*lib\s+/)      {&HandleLib; next;}
174  if (/^\@setfilename/)     {print TEX "\@setfilename $doc.hlp\n"; next;}
175                             
176  print TEX $_;
177
178  if (/^\@bye$/)            {last;}
179}
180
181#
182# wrap up
183#
184close(TEX);
185dbmclose(%EXAMPLES);
186print "\nd2t: Finished generation of $tex_file \n" if ($verbose > 1);
187print "\n" if ($verbose == 1);
188
189
190######################################################################
191# @c example [error]
192#    -> the text till the next @c example is feed into Singular,
193#       the text is then substituted by
194#       @c computed example $example $doc_file:$line
195#       <text from the input>
196#       @expansion{} <corresponding output>
197#       ....
198#       @c end computed example $example $doc_file:$line
199#       reuse computed examples if ($reuse && -r $example.inc)
200#       cut absolute directory names from the loaded messages
201#       substituted @,{ and } by @@, @{ resp. @}
202#       wrap around output lines  longer than $ex_length = 73;
203#       Processing is aborted if error occures in Singular run,
204#       unless 'error' is specified
205sub HandleExample
206{
207  my($lline, $thisexample, $include, $error_ok);
208 
209  $lline = $line;
210  $example++;
211
212  if ($no_ex)
213  {
214    print "{$example}" if ($verbose);
215    print TEX "\@c skipped computation of example $example $doc_file:$lline \n";
216   
217  }
218
219  $thisexample = '';
220  $error_ok = 1 if /error/;
221  # print content in example file till next @c example
222  while (<DOC>)
223  {
224    $line++;
225    last if (/^\@c\s*example\s*$/);
226    s/^\s*//; # remove preceeding white spaces
227    if ($no_ex)
228    {
229      &protect_texi;
230      print TEX $_;
231    }
232    else
233    {
234      $thisexample .= $_ unless (/^\s*$/);
235    }
236  }
237  Error("no matching '\@c example' found for $doc_file:$lline\n")
238    unless (/^\@c\s*example\s*$/);
239
240  # done, if no examples
241  return if ($no_ex);
242
243  # check whether it can be reused
244  $include = $EXAMPLES{$thisexample};
245  if ($reuse && ($include = $EXAMPLES{$thisexample}))
246  {
247    print "<$example>" if ($verbose);
248    print TEX "\@c reused example $example $doc_file:$lline \n";
249  }
250  else
251  {
252    print "($example" if ($verbose == 1);
253    my ($ex_file, $res_file, $inc_file);
254    $inc_file = "$doc_subdir/$doc"."_$example.inc";
255    $ex_file = "$doc_subdir/$doc"."_$example.tst";
256    $res_file = "$doc_subdir/$doc"."_$example.res";
257
258    print TEX "\@c computed example $example $doc_file:$lline \n";
259
260    # run singular
261    open(EX, ">$ex_file") || Error("can't open $ex_file for writing: $!\n");
262    print EX "$thisexample\$\n";
263    close(EX);
264
265    &System("$Singular $Singular_opts $ex_file > $res_file");
266    print ")" if ($verbose == 1);
267
268    open(RES, "<$res_file") || Error("can't open $res_file for reading: $!\n");
269    open(INC, ">$inc_file") || Error("can't open $inc_file for writing: $!\n");
270
271    $include = '';
272    # get result, manipulate it and put it into inc file
273    while (<RES>)
274    {
275      last if (/^$ex_file\s*([0-9]+)..\$/);
276      # check for error
277      Error("while running example $example from $doc_file:$lline.\nCall: '$Singular $Singular_opts $ex_file > $res_file'\n")
278        if (/error occurred/ && ! $error_ok);
279      # remove stuff from echo
280      if (/^$ex_file\s*([0-9]+)../)
281      {
282        $_ = $';
283        &protect_texi;
284      }
285      else
286      {
287        local($to_do, $done);
288        # remove absolute path names from laoded messages
289        s/^(\/\/ \*\* loaded )(.*)\/(.+).lib(.*)/$1$3.lib$4/;
290        # shorten error occurred in messages
291        s/\? error occurred in [^ ]* line/\? error occurred in line/;
292        # break after $ex_length characters
293        $to_do = $_;
294        while (length($to_do) > $ex_length)
295        {
296          $done .= substr($to_do, 0, $ex_length)."\\\n   ";
297          $to_do = substr($to_do, $ex_length + 1);
298        }
299        $_ = $done.$to_do if($done);
300        &protect_texi;
301        $_ = "\@expansion{} ".$_;
302      }
303      $include .= $_;
304      print INC $_;
305    }
306    close(RES);
307    close(INC);
308    unlink $ex_file, $res_file, $inc_file if ($clean);
309    $EXAMPLES{$thisexample} = $include;
310  }
311  print TEX $include;
312  print TEX "\@c end example $example $doc_file:$lline\n";
313}
314 
315######################################################################
316# @c include file
317#    -> copy content of file into output file protecting texi special chars
318sub HandleInclude
319{
320  s/^\@c\s*include\s+([^\s]+)\s/$1/;
321  unless (&Open(*INC, "<$_"))
322  {
323    warn "$WARNING HandleInclude: can't open $_ for reading\n";
324    print TEX "\@c include file $_ not found\n";
325    return;
326  }
327  print "<$_>" if ($verbose);
328  print TEX "\@c begin included file $_ from $doc_file:$line\n";
329  while (<INC>)
330  {
331    &protect_texi;
332    print TEX $_;
333  }
334  print TEX "\@c end included file from $doc_file:$line\n";
335  close (INC);
336}
337
338######################################################################
339# @c ref
340# ....
341# @c ref
342#    -> scans intermediate lines for @ref{..} strings
343#    Creates menu of (sorted) refs for ifinfo
344#    Creates comma-separated (sorted) refs for iftex, prepended with
345#    the text before first @ref.
346
347sub HandleRef
348{
349  local(%refs, @refs, $header, $lline, $lref);
350 
351  print TEX "\@c inserted refs from $doc_file:$line\n";
352 
353  # scan lines use %ref to remove duplicates
354  $lline = $line;
355  while (<DOC>)
356  {
357    $line++;
358    last if (/^\@c\s*ref\s*$/);
359   
360    while (/\@ref{([^\s]*)}/)
361    {
362      $refs{$1} = 1;
363      $_ = $';
364      unless ($header)
365      {
366        $header = $`;
367        $header = " " unless ($header);
368      }
369    }
370  }
371  die "$ERRROR no matching \@c ref found for $doc_file:$lline\n" 
372    unless (/^\@c\s*ref\s*$/);
373  # sort refs
374  @refs = sort(keys(%refs));
375  # put them out
376  print TEX "\@ifinfo\n\@menu\n";
377  foreach $ref (@refs) {print TEX "* ".$ref."::\n";}
378  print TEX "\@end menu\n\@end ifinfo\n\@iftex\n";
379
380  if ($header ne " ")
381  {
382    print TEX "$header\n" unless ($header eq " ");
383  }
384  else
385  {
386    print TEX "\@strong{See also:} ";
387  }
388  $lref = pop(@refs);
389  foreach $ref (@refs) {print TEX "\@ref{".$ref."};\n";}
390  print TEX "\@ref{".$lref."}.\n" if ($lref); 
391  print TEX "\@end iftex\n\@c end inserted refs from $doc_file:$lline\n";
392}
393
394###################################################################
395#
396# @c lib libname.lib [no_ex, lib_fun, lib_ex]
397#   --> includes info of libname.lib in output file
398#   --> includes function names of info into function index
399#   --> if lib_fun is given, includes listing of functions and
400#                      their help into output file
401#   --> if lib_ex is given, includes computed examples of functions, as well
402#
403# Optional no_ex, lib_fun, lib_ex arguments overwrite respective
404# command-line arguments
405#
406
407sub HandleLib
408{
409  local($lib, $lib_name, $ltex_file, $l_ex, $l_fun);
410  my ($func);
411
412  if (/^\@c\s*lib\s+([^\.]+)\.lib(.*)/)
413  {
414    $lib = $1;
415    $lib_name = $lib.".lib";
416    $_ = $2;
417  }
418  else
419  {
420    warn "$WARNING need .lib file to process '$_'\n";
421    print TEX $_;
422    return;
423  }
424
425  $func = $1 if (/^:(.*?) /);
426  $l_fun = 1 if (($lib_fun || (/lib_fun/)) && !/no_fun/);
427  $l_ex = 1 if (($lib_ex || /lib_ex/) && !/no_ex/ && $l_fun);
428
429  $ltex_file = "$doc_subdir/$lib"."_lib";
430  unless ($l_ex)
431  {
432    if ($l_fun)
433    {
434      $ltex_file .= "_noEx";
435    }
436    else
437    {
438      $ltex_file .= "_noFun";
439    }
440  }
441  $ltex_file .= ".tex";
442 
443  Error("can't open $lib.lib for reading: $!\n") 
444    unless  ($lib_dir = &Open(*LIB, "<$lib.lib"));
445  close (LIB);
446  if ($reuse && open(LTEX, "<$ltex_file") && 
447      IsNewer($ltex_file, "$lib_dir/$lib.lib"))
448  {
449    unless ($func)
450    {
451      print "<lib $lib>" if ($verbose);
452      print TEX "\@c reused lib docu for $lib_name $doc_file:$line \n";
453    }
454  }
455  elsif (&GenerateLibDoc($lib, $ltex_file, $l_fun, $l_ex))
456  {
457    print TEX "\@c generated lib docu for $lib_name $doc_file:$line \n"
458      unless $func;
459    open(LTEX, "<$ltex_file") 
460      || Error("can't open $ltex_file for reading: $!\n");
461  }
462  if ($func)
463  {
464    print "<$lib:$func" if $verbose;
465    print TEX "\@c generated lib proc docu for $lib_name:$func $doc_file:$line \n";
466    my $found = 0;
467    while (<LTEX>)
468    {
469      $found = 1 if /c ---content $func---/;
470      print TEX $_ if $found;
471      last if $found && /c ---end content $func---/;
472    }
473    if ($found)
474    {
475      Error("no end content found for lib proc docu for $lib_name:$func $doc_file:$line \n")
476        unless (/c ---end content $func---/);
477      print TEX "\@c generated lib proc docu for $lib_name:$func $doc_file:$line \n";
478    }
479    else
480    {
481      Error("did not find lib proc docu for $lib_name:$func $doc_file:$line \n");
482    }
483    print ">" if $verbose;
484  }
485  else
486  {
487    while (<LTEX>) {print TEX $_;}
488    print TEX "\@c end generated lib docu for $lib_name $doc_file:$line \n";
489  }
490  close(LTEX);
491  unlink $ltex_file if ($clean);
492}
493 
494sub GenerateLibDoc
495{
496  my($lib, $tex_file, $l_fun, $l_ex) = @_;
497  my($lib_dir, $scall, $pl_file, $doc_file, $i, $example,$largs);
498  # vars from executing the library perl scrip
499  local($info, $libary, $version, @procs, %example, %help);
500 
501  print "(lib $lib: " if ($verbose == 1);
502  # construct doc/tex file name
503  $doc_file = "$doc_subdir/$lib"."_lib";
504  $doc_file .= "_noFun" unless ($l_fun);
505  $doc_file .= ".doc";
506
507  Error("can't open $lib.lib for reading: $!\n")
508    unless  ($lib_dir = &Open(*LIB, "<$lib.lib"));
509  close (LIB);
510  if (-r $doc_file && $reuse && IsNewer($doc_file, "$lib_dir/$lib.lib"))
511  {
512    print "<doc>" if ($verbose == 1);
513  }
514  else
515  {
516    # generate perl file, if necessary
517    $pl_file = "$doc_subdir/$lib"."_lib.pl";
518    if (-r $pl_file && $reuse && IsNewer($pl_file, "$lib_dir/$lib.lib"))
519    {
520      print "<pl>" if ($verbose == 1);
521    }
522    else
523    {
524      print "(pl" if ($verbose == 1);
525      &System("$libparse -i $lib_dir/$lib.lib > $pl_file");
526      print ")" if ($verbose == 1);
527    }
528
529    print "(doc" if ($verbose == 1);
530   
531    do $pl_file;
532    Error("error while executing $pl_file: $@\n") if ($@);
533    unlink ($pl_file) if ($clean);
534   
535    # generate doc file
536    open(LDOC, ">$doc_file") || die"$ERROR can't open $doc_file for writing: $!\n";
537   
538    # print header
539    print LDOC "\@c library version: $version\n";
540    print LDOC "\@c library file: $library\n";
541    OutLibInfo(\*LDOC, $info, $l_fun);
542    # print menu of available functions
543    if ($l_fun)
544    {
545      @procs = sort(@procs);
546      print LDOC "\@menu\n";
547      foreach $proc (@procs) {print LDOC "* $proc"."::\n";}
548      print LDOC "\@end menu\n";
549     
550      # print help and example of each function
551      for ($i = 0; $i <= $#procs; $i++)
552      {
553        # print node and section heading
554        print LDOC "\n\@c ------------------- " . $procs[$i]." -------------\n";
555        print LDOC "\@node " . $procs[$i].",";
556        print LDOC " ".$procs[$i+1] if ($i < $#procs);
557        print LDOC ",";
558        print LDOC " ".$procs[$i-1] if ($i > 0);
559        print LDOC ", " . $lib ."_lib\n";
560        print LDOC "\@subsection " . $procs[$i] . "\n";
561        print LDOC "\@findex ". $procs[$i] . "\n";
562        print LDOC "\@cindex ". $procs[$i] . "\n" if ($lib eq "standard");
563
564        print LDOC "\@c ---content $procs[$i]---\n";
565        # print help section
566        print LDOC "\@strong{Info:}\n";
567        print LDOC "\@example\n";
568        print LDOC &CleanUpHelp($help{$procs[$i]});
569        print LDOC "\n\@end example\n";
570       
571        # print example section
572        next unless ($example = &CleanUpExample($lib, $example{$procs[$i]}));
573        print LDOC "\@strong{Example:}\n";
574        print LDOC "\@example\n\@c example\n";
575        print LDOC $example;
576        print LDOC "\n\@c example\n\@end example\n";
577        print LDOC "\@c ---end content $procs[$i]---\n";
578      }
579    }
580    close(LDOC);
581    print ")" if ($verbose == 1);
582  }
583
584  $largs = "-no_ex" unless ($l_ex);
585  # call doc2tex on generated doc file
586  print "\n" if ($verbose == 1);
587  dbmclose(%EXAMPLES);
588  &System("$0 $args $largs -o $tex_file $doc_file");
589  dbmopen(%EXAMPLES, $doc_examples_db, oct(755)) 
590    || die "$ERROR: can't open examples data base: $!\n";
591 
592  unlink($doc_file) if ($clean);
593  return 1;
594}
595
596###########################################################################
597# parse and print-out libinfo
598sub OutLibInfo
599{
600  my ($FH, $info, $l_fun) = @_;
601  my ($item, $text, $line);
602  $info =~ s/^\s*//;
603  $info =~ s/\s*$//;
604  $info .= "\n";
605
606  print $FH "\@c ---content LibInfo---\n";
607  print $FH "\@table \@code\n";
608  while ($info =~ m/(.*\n)/g)
609  {
610    $line = $1;
611    if ($1 =~ /^(\w.+?):(.*\n)/)
612    {
613      OutLibInfoItem($FH, $item, $text, $l_fun) if $item && $text;
614      $item = $1;
615      $text = $2;
616    }
617    else
618    {
619      $text .= $line;
620    }
621  }
622  OutLibInfoItem($FH, $item, $text, $l_fun) if $item && $text;
623  print $FH "\@end table\n";
624  print $FH "\@c ---end content LibInfo---\n";
625}
626
627sub OutLibInfoItem
628{
629  my ($FH, $item, $text, $l_fun) = @_;
630
631  $item = lc $item;
632  $item = ucfirst $item;
633  print $FH '@item @strong{'. "$item:}\n";
634
635  if (($item =~ m/^Library/)  && ($text =~ m/\s*(\w*)\.lib/))
636  {
637    print $FH "$1.lib\n";
638    $text = $';
639    if ($text =~ /\w/)
640    {
641      print $FH "\n" . '@item @strong{Purpose:'."}\n";
642      print $FH lc $text;
643    }
644  }
645  elsif ($item =~ m/rocedure/)
646  {
647    $text =~ s/^\s*//;
648    $text =~ s/\s*$//;
649    $text =~ s/.*$//
650      if ($text =~ /parameters.*brackets\*are optional.*$/);
651
652    my ($proc, $pargs, $pinfo, $line);
653    print $FH "\@table \@asis\n";
654    $text .= "\n";
655    while ($text =~ /(.*\n)/g)
656    {
657      $line = $1;
658      if ($1 =~ /^\s*(\w+)\((.*?)\)/)
659      {
660        OutProcInfo($FH, $proc, $procargs, $pinfo, $l_fun) if $proc && $pinfo;
661        $proc = $1;
662        $procargs = $2;
663        $pinfo = $';
664      }
665      else
666      {
667        $pinfo .= $line; 
668      }
669    }
670    OutProcInfo($FH, $proc, $procargs, $pinfo, $l_fun) if $proc && $pinfo;
671    print $FH "\@end table\n\n";
672  }
673  else
674  {
675    local $_ = $text;
676    s/^\s*//;
677    s/\s*$//;
678    s/ +/ /;
679    &protect_texi;
680    print $FH "$_\n\n";
681  }
682}
683
684sub OutProcInfo
685{
686  my($FH, $proc, $procargs, $pinfo, $l_fun) = @_;
687  local $_ = $pinfo;
688  s/^[;\s]*//;
689  s/\s*$//;
690  s/ +/ /g;
691  s/\n */\n/g;
692  &protect_texi;
693 
694  print $FH "\@item \@code{$proc($procargs)}  ";
695  print $FH "\@ref{$proc}" if ($l_fun);
696  print $FH "\n$_\n";
697}
698
699###################################################################
700#
701# Auxiallary functions
702#
703sub CleanUpHelp
704{
705  local($_) = @_;
706 
707  # remove spaces quotations, etc from beginning and end
708  s/^[^\w]*//;
709  s/[\s\n"]*$//; #"
710  # replace
711  s/\\\\/\\/g;
712  s/\\"/"/g;
713  # remove line beginning with example
714  s/\nEXAMPLE.*//;
715  &protect_texi;
716  return ($_);
717}
718 
719sub CleanUpExample
720{
721  local($lib, $example) = @_;
722 
723  # find portion in {}
724  $example =~ s/^[^{]*{(.*)}[^}]*$/$1/s;
725
726  if ($example =~ /EXAMPLE: \(not executed\)/)
727  {
728    # erase first three lines
729    $example =~ s/^.*\n.*\n.*\n/\n/;
730    # erase enclosing " " in every line
731    $example =~ s/\n\s*"/\n/g;
732    $example =~  s/";\n/\n/g;
733  }
734  # erase EXAMPLE, echo and pause statements
735  $example =~ s/"EXAMPLE.*"[^;]*;//g;
736  $example =~ s/echo[^;]*;//g;
737  $example =~ s/pause[^;]*;//g;
738 
739  # prepend LIB command
740  $example = "LIB \"$lib.lib\";\n".$example 
741    if ($example && $lib ne "standard");
742  # erase empty lines
743  $example =~ s/^\s*\n//g;
744  # erase spaces from beginning of lines
745  $example =~ s/\n\s*/\n/g;
746  return $example;
747}
748
749sub IsNewer
750{
751  my $f1 = shift;
752  my $f2 = shift;
753  my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime)
754    = stat($f1);
755  my $m1 = $mtime;
756  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime) = stat($f2);
757
758  return ($m1 > $mtime);
759}
760
761####################################################################
762# Auxillary routines
763#
764
765# protect texi special characters
766sub protect_texi
767{
768  s/\@/\@\@/g;
769  s/{/\@{/g;
770  s/}/\@}/g;
771}       
772
773# open w.r.t. include_dirs
774sub Open
775{
776  local(*FH, $file) = @_;
777  local($mode);
778  $file =~ s/^(.{1})(.*)/$2/;
779  $mode = $1;
780
781  foreach $dir (@include_dirs)
782  {
783    return $dir if(open(FH, $mode.$dir."/".$file));
784  }
785}
786   
787# system call with echo on verbose > 1 and die on fail
788sub System
789{
790  local($call) = @_;
791  print "d2t system: $call\n" if ($verbose > 1);
792  Error("non-zero exit status of system call: '$call': $!\n")
793    if (system($call));
794}
795
796sub Error
797{
798  print "$ERROR $_[0]";
799  close(TEX);
800  unlink $tex_file if $tex_file && -e $tex_file;
801  exit(1);
802}
803
804#
805# leave this here --otherwise fontification in my emacs gets screwd up
806#
807sub Usage
808{
809  return <<EOT;
810This is doc2tex: a utility to generate Singular texinfo from doc file
811To convert a doc file to texinfo: $0 [options] input_file.doc
812where options can be (abbreviated to shortest possible prefix):
813  -Singular prog: use 'prog' as Singular program to generate ex output
814                          (default: '../Singular/Singular')
815  -libparse prog: use 'prog' as libparse program to generate lib docu
816                          (default: '../Singular/libparse')
817  -output file  : use 'file' as output file
818                          (default: input_file.tex)
819  -clean        : delete intermediate files
820  -no_reuse     : don't reuse intermediate files
821  -no_ex        : skip computation of examples
822  -lib_fun      : include help for library functions
823  -lib_ex       : include example for library functions
824  -subdir  dir  : put intermediate files into 'dir'
825                          (default: './d2t_singular')
826  -I dir        : look also into 'dir' for include  and lib files
827                          (default: ".", "../Singular/LIB")
828  -verbose  val : Set verbosity to 'val' (0=quiet, 1=prot, >1=all)
829  -help         : print help and exit
830EOT
831}
832
Note: See TracBrowser for help on using the repository browser.