source: git/doc/doc2tex.pl @ 4deddb

fieker-DuValspielwiese
Last change on this file since 4deddb was dc3a44, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* generation of library docu git-svn-id: file:///usr/local/Singular/svn/trunk@3245 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100755
File size: 22.7 KB
Line 
1#!/usr/local/bin/perl
2# $Id: doc2tex.pl,v 1.4 1999-07-07 16:38:34 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, $ref);
498  # vars from executing the library perl scrip
499  local($info, $libary, $version, @procs, %example, %help, $table_is_open);
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    undef @procs;
542    $ref = OutLibInfo(\*LDOC, $info, $l_fun);
543    OutRef(\*LDOC, $ref) if $ref;
544    # print menu of available functions
545    if ($l_fun)
546    {
547      # print help and example of each function
548      for ($i = 0; $i <= $#procs; $i++)
549      {
550        # print node and section heading
551        print LDOC "\n\@c ------------------- " . $procs[$i]." -------------\n";
552        print LDOC "\@node " . $procs[$i].",";
553        print LDOC " ".$procs[$i+1] if ($i < $#procs);
554        print LDOC ",";
555        print LDOC " ".$procs[$i-1] if ($i > 0);
556        print LDOC ", " . $lib ."_lib\n";
557        print LDOC "\@subsection " . $procs[$i] . "\n";
558        print LDOC "\@findex ". $procs[$i] . "\n";
559        print LDOC "\@cindex ". $procs[$i] . "\n" if ($lib eq "standard");
560
561        print LDOC "\@c ---content $procs[$i]---\n";
562        print LDOC "\@table \@asis\n";
563        $table_is_open = 1;
564        # print help
565        $ref = OutInfo(\*LDOC, $help{$procs[$i]});
566        print LDOC "\@end table\n";
567        $table_is_open = 0;
568        # print example
569        if ($example = &CleanUpExample($lib, $example{$procs[$i]}))
570        {
571          print LDOC "\@strong{Example:}\n";
572          print LDOC "\@example\n\@c example\n";
573          print LDOC $example;
574          print LDOC "\n\@c example\n\@end example\n";
575        }
576        OutRef(\*LDOC, $ref) if $ref;
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  print $FH "\@c ---content LibInfo---\n";
602  print $FH "\@table \@asis\n";
603  $table_is_open = 1;
604 
605  my ($ref) = OutInfo($FH, $info, $l_fun);
606
607  print $FH "\@end table\n" if $table_is_open;
608  print $FH "\@c ---end content LibInfo---\n";
609  $table_is_open = 0;
610  return $ref;
611}
612
613sub OutInfo
614{
615  my ($FH, $info, $l_fun) = @_;
616  $info =~ s/^\s*//;
617  $info =~ s/\s*$//;
618  $info .= "\n";
619
620  my ($item, $text, $line, $ref);
621  while ($info =~ m/(.*\n)/g)
622  {
623    $line = $1;
624    if ($1 =~ /^(\w.+?):(.*\n)/)
625    {
626      $ref .= OutInfoItem($FH, $item, $text, $l_fun) if $item && $text;
627      $item = $1;
628      $text = $2;
629    }
630    else
631    {
632      $text .= $line;
633    }
634  }
635  $ref .= OutInfoItem($FH, $item, $text, $l_fun) if $item && $text;
636  return $ref;
637}
638
639sub FormatInfoText
640{
641  s/^\s*//; # remove whitespaces from beginning and end
642  s/\s*$//;
643  s/ +/ /g;  # replace double whitespeces by one
644  &protect_texi; # protect texinfo special chars
645  s/\n/\n\@*/g; # replace newline by forced newline
646}
647
648sub OutInfoItem
649{
650  my ($FH, $item, $text, $l_fun) = @_;
651
652  $item = lc $item;
653  $item = ucfirst $item;
654
655  if ($item =~ /see also/i)
656  {
657    # return references
658    return $text;
659  }
660  elsif ($item =~ m/example/i)
661  {
662    # forget about example, since it comes explicitely
663    return '';
664  }
665  elsif ($item =~ m/procedure/i)
666  {
667    if ($l_fun && $table_is_open)
668    {
669      print $FH "\@end table\n\n";
670      $table_is_open = 0;
671    }
672    $text =~ s/^\s*//;
673    $text =~ s/\s*$//;
674    $text =~ s/.*$// if ($text=~/parameters.*brackets.*are.*optional.*$/);
675    $text .= "\n";
676   
677    my ($proc, $pargs, $pinfo, $line);
678    if ($l_fun)
679    {
680      print $FH "\@strong{$item}\n\@menu\n";
681    }
682    else
683    {
684      print $FH "\@item \@strong{$item}\n\@table \@asis\n";
685    }
686    while ($text =~ /(.*\n)/g)
687    {
688      $line = $1;
689      if ($1 =~ /^\s*(\w+)\((.*?)\)/)
690      {
691        OutProcInfo($FH, $proc, $procargs, $pinfo, $l_fun) if $proc && $pinfo;
692        $proc = $1;
693        $procargs = $2;
694        $pinfo = $';
695      }
696      else
697      {
698        $pinfo .= $line; 
699      }
700    }
701    OutProcInfo($FH, $proc, $procargs, $pinfo, $l_fun) if $proc && $pinfo;
702    print $FH ($l_fun ? "\@end menu\n" : "\@end table\n");
703    return '';
704  }
705
706  if (! $table_is_open)
707  {
708    print $FH "\@table \@asis\n";
709    $table_is_open = 1;
710  }
711  print $FH '@item @strong{'. "$item:}\n";
712  # prepare text:
713  local $_ = $text;
714  if (($item =~ m/^library/i)  && m/\s*(\w*)\.lib/)
715  {
716    print $FH "$1.lib\n";
717    $text = $';
718    if ($text =~ /\w/)
719    {
720      print $FH "\n" . '@item @strong{Purpose:'."}\n";
721      print $FH lc $text;
722    }
723  }
724  else
725  {
726    # just print the text
727    &FormatInfoText;
728    # if functions are in text, then make it in code
729    s/(\w+\(.*?\))/\@code{$1}/g
730      if ($item =~ /usage/i || $item =~ /Return/i);
731
732    print $FH "$_\n\n";
733  }
734  return '';
735}
736
737sub OutProcInfo
738{
739  my($FH, $proc, $procargs, $pinfo, $l_fun) = @_;
740  local $_ = $pinfo;
741  s/^[;\s]*//;
742  s/\n/ /g;
743  &FormatInfoText;
744 
745  if ($l_fun)
746  {
747    print $FH "* ${proc}:: $_\n";
748    push @procs, $proc;
749  }
750  else
751  {
752    print $FH "\@item \@code{$proc($procargs)}  ";
753    print $FH "\n\@findex $proc\n$_\n";
754  }
755}
756
757sub OutRef
758{
759  my ($FH, $refs) = @_;
760  $refs =~ s/^\s*//;
761  $refs =~ s/\s*$//;
762  my @refs = split (/[\s,]+/, $refs);
763  my $ref;
764
765  print $FH "\@c ref\nSee\n";
766  $ref = shift @refs;
767  print $FH "\@ref{$ref}";
768  for $ref (@refs)
769  {
770    print $FH ", \@ref{$ref}";
771  }
772  print $FH "\n\@c ref\n";
773}
774
775sub CleanUpExample
776{
777  local($lib, $example) = @_;
778 
779  # find portion in {}
780  $example =~ s/^[^{]*{(.*)}[^}]*$/$1/s;
781
782  if ($example =~ /EXAMPLE: \(not executed\)/)
783  {
784    # erase first three lines
785    $example =~ s/^.*\n.*\n.*\n/\n/;
786    # erase enclosing " " in every line
787    $example =~ s/\n\s*"/\n/g;
788    $example =~  s/";\n/\n/g;
789  }
790  # erase EXAMPLE, echo and pause statements
791  $example =~ s/"EXAMPLE.*"[^;]*;//g;
792  $example =~ s/echo[^;\n]*;//g; 
793  $example =~ s/pause\(.*?\)[^;]*;//g;
794 
795  # prepend LIB command
796  $example = "LIB \"$lib.lib\";\n".$example 
797    if ($example && $lib ne "standard");
798  # erase empty lines
799  $example =~ s/^\s*\n//g;
800  # erase spaces from beginning of lines
801  $example =~ s/\n\s*/\n/g;
802  $example =~ s/\s*$//g;
803  return $example;
804}
805
806###################################################################
807#
808# Auxiallary functions
809#
810sub IsNewer
811{
812  my $f1 = shift;
813  my $f2 = shift;
814  my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime)
815    = stat($f1);
816  my $m1 = $mtime;
817  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime) = stat($f2);
818
819  return ($m1 > $mtime);
820}
821
822####################################################################
823# Auxillary routines
824#
825
826# protect texi special characters
827sub protect_texi
828{
829  s/\@/\@\@/g;
830  s/{/\@{/g;
831  s/}/\@}/g;
832}       
833
834# open w.r.t. include_dirs
835sub Open
836{
837  local(*FH, $file) = @_;
838  local($mode);
839  $file =~ s/^(.{1})(.*)/$2/;
840  $mode = $1;
841
842  foreach $dir (@include_dirs)
843  {
844    return $dir if(open(FH, $mode.$dir."/".$file));
845  }
846}
847   
848# system call with echo on verbose > 1 and die on fail
849sub System
850{
851  local($call) = @_;
852  print "\nd2t system: $call\n" if ($verbose > 1);
853  Error("non-zero exit status of system call: '$call': $!\n")
854    if (system($call));
855}
856
857sub Error
858{
859  print "$ERROR $_[0]";
860  close(TEX);
861  unlink $tex_file if $tex_file && -e $tex_file;
862  exit(1);
863}
864
865#
866# leave this here --otherwise fontification in my emacs gets screwd up
867#
868sub Usage
869{
870  return <<EOT;
871This is doc2tex: a utility to generate Singular texinfo from doc file
872To convert a doc file to texinfo: $0 [options] input_file.doc
873where options can be (abbreviated to shortest possible prefix):
874  -Singular prog: use 'prog' as Singular program to generate ex output
875                          (default: '../Singular/Singular')
876  -libparse prog: use 'prog' as libparse program to generate lib docu
877                          (default: '../Singular/libparse')
878  -output file  : use 'file' as output file
879                          (default: input_file.tex)
880  -clean        : delete intermediate files
881  -no_reuse     : don't reuse intermediate files
882  -no_ex        : skip computation of examples
883  -lib_fun      : include help for library functions
884  -lib_ex       : include example for library functions
885  -subdir  dir  : put intermediate files into 'dir'
886                          (default: './d2t_singular')
887  -I dir        : look also into 'dir' for include  and lib files
888                          (default: ".", "../Singular/LIB")
889  -verbose  val : Set verbosity to 'val' (0=quiet, 1=prot, >1=all)
890  -help         : print help and exit
891EOT
892}
893
Note: See TracBrowser for help on using the repository browser.