source: git/doc/doc2tex.pl @ 0a2ba89

spielwiese
Last change on this file since 0a2ba89 was fdbd6d, checked in by Tim Wichmann <wichmann@…>, 25 years ago
*wichmann: Removed typo (option output was parsed as ooutput) git-svn-id: file:///usr/local/Singular/svn/trunk@3323 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100755
File size: 15.0 KB
Line 
1#!/usr/local/bin/perl
2# $Id: doc2tex.pl,v 1.13 1999-07-22 07:21:31 wichmann 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] [lib_fun, lib_ex, no_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$Singular_opts = " -teqr12345678";
59$clean = 0;
60$verbose = 1;
61$reuse = 1;
62$no_ex = 0;
63$no_fun = 0;
64$doc_subdir = "./d2t_singular";
65@include_dirs = (".", "../Singular/LIB");
66$doc_examples_db = "$doc_subdir/examples";
67$make = 0;
68
69#
70# misc. defaults
71#
72$ex_length = 73;
73$ERROR = "\n**** Error:";
74$WARNING = "\n** Warning:";
75
76#
77# flush stdout and stderr after every write
78#
79select(STDERR);
80$| = 1;
81select(STDOUT);
82$| = 1;
83
84#
85# get file to convert
86#
87$doc_file = pop(@ARGV);
88if ($doc_file =~  /(.+)\..*$/)
89{
90  die "*** Error: Need .doc file as input\n" . &Usage 
91    unless ($doc_file =~ /(.+)\.doc$/);
92}
93else
94{
95  if ($doc_file =~ /^-h(elp)?$/) { print &Usage; exit;}
96  $doc_file .= ".doc";
97}
98
99#
100# parse command line options
101#
102$args = join(" ", @ARGV);
103while (@ARGV && $ARGV[0] =~ /^-/) 
104{
105  $_ = shift(@ARGV);
106  if (/^-S(ingular)?$/)  { $Singular = shift(@ARGV); next;}
107  if (/^-o(utput)?$/)    { $tex_file = shift(@ARGV); next;}
108  if (/^-no_r(euse)?$/)  { $reuse = 0; next;}
109  if (/^-c(lean)?$/)     { $clean = 1; next;}
110  if (/^-no_e(x)?$/)     { $no_ex = 1; next;}
111  if (/^-no_fu(n)?$/)    { $no_fun = 1;next;}
112  if (/^-m(ake)?$/)      { $make = 1;next;}
113  if (/^-s(ubdir)?$/)    { $doc_subdir = shift(@ARGV); next;}
114  if (/^-I$/)            { unshift(@include_dirs, shift(@ARGV)); next;}
115  if (/^-v(erbose)?$/)   { $verbose = shift(@ARGV); next;}
116  if (/^-h(elp)?$/)      { print &Usage; exit;}
117  Error("Unknown Option $_\n" .&Usage);
118}
119$verbose = ($verbose < 0 ? 0 : $verbose);
120
121#
122# construct filenames
123#
124($doc_dir = $doc_file) =~ s/(.+)\.doc$/$1/;
125if ($doc_dir =~ /(.*)\//)
126{
127  $doc = $';
128  $doc_dir = $1;
129}
130else
131{
132  $doc = $doc_dir;
133  $doc_dir = ".";
134}
135unless ($tex_file)
136{
137  $tex_file = ($no_ex ? "$doc_dir/${doc}_noEx.tex" : "$doc_dir/$doc.tex");
138}
139
140#
141# open files
142#
143open(DOC, "<$doc_file") 
144  || Error("can't open $doc_file for reading: $!\n" . &Usage);
145open(TEX, ">$tex_file") || Error("can't open $tex_file for writing: $!\n");
146print "d2t: Generating $tex_file from $doc_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
160# dbmopen(%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);
185#dbmclose(%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  if ($reuse && ($include = $EXAMPLES{$thisexample}))
245  {
246    print "<$example>" if ($verbose);
247    print TEX "\@c reused example $example $doc_file:$lline \n";
248  }
249  else
250  {
251    print "($example" if ($verbose == 1);
252    my ($ex_file, $res_file, $inc_file);
253    $inc_file = "$doc_subdir/$doc"."_$example.inc";
254    $ex_file = "$doc_subdir/$doc"."_$example.tst";
255    $res_file = "$doc_subdir/$doc"."_$example.res";
256
257    print TEX "\@c computed example $example $doc_file:$lline \n";
258
259    # run singular
260    open(EX, ">$ex_file") || Error("can't open $ex_file for writing: $!\n");
261    print EX "$thisexample\$\n";
262    close(EX);
263
264    &System("$Singular $Singular_opts $ex_file > $res_file");
265    print ")" if ($verbose == 1);
266
267    open(RES, "<$res_file") || Error("can't open $res_file for reading: $!\n");
268    open(INC, ">$inc_file") || Error("can't open $inc_file for writing: $!\n");
269
270    $include = '';
271    # get result, manipulate it and put it into inc file
272    while (<RES>)
273    {
274      last if (/^$ex_file\s*([0-9]+)..\$/);
275      # check for error
276      Error("while running example $example from $doc_file:$lline.\nCall: '$Singular $Singular_opts $ex_file > $res_file'\n")
277        if (/error occurred/ && ! $error_ok);
278      # remove stuff from echo
279      if (/^$ex_file\s*([0-9]+)../)
280      {
281        $_ = $';
282        &protect_texi;
283      }
284      else
285      {
286        local($to_do, $done);
287        # remove absolute path names from laoded messages
288        s/^(\/\/ \*\* loaded )(.*)\/(.+).lib(.*)/$1$3.lib$4/;
289        # shorten error occurred in messages
290        s/\? error occurred in [^ ]* line/\? error occurred in line/;
291        # break after $ex_length characters
292        $to_do = $_;
293        while (length($to_do) > $ex_length && $to_do =~ /\w/)
294        {
295          $done .= substr($to_do, 0, $ex_length)."\\\n   ";
296          $to_do = substr($to_do, $ex_length + 1);
297        }
298        $_ = $done.$to_do if($done);
299        &protect_texi;
300        $_ = "\@expansion{} ".$_;
301      }
302      $include .= $_;
303      print INC $_;
304    }
305    close(RES);
306    close(INC);
307    unlink $ex_file, $res_file, $inc_file if ($clean);
308    $EXAMPLES{$thisexample} = $include;
309  }
310  print TEX $include;
311  print TEX "\@c end example $example $doc_file:$lline\n";
312}
313 
314######################################################################
315# @c include file
316#    -> copy content of file into output file protecting texi special chars
317sub HandleInclude
318{
319  s/^\@c\s*include\s+([^\s]+)\s/$1/;
320  s/\s*$//;
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{(.*?)}/)
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 $header ? "$header\n" : "See also:\n";
377  print TEX "\@ifinfo\n\@menu\n";
378  foreach $ref (@refs) {print TEX "* ".$ref."::\n";}
379  print TEX "\@end menu\n\@end ifinfo\n\@iftex\n";
380
381  if ($header ne " ")
382  {
383    print TEX "$header\n" unless ($header eq " ");
384  }
385  else
386  {
387    print TEX "\@strong{See also:}\n";
388  }
389  $lref = pop(@refs);
390  foreach $ref (@refs) {print TEX "\@ref{".$ref."};\n";}
391  print TEX "\@ref{".$lref."}.\n" if ($lref); 
392  print TEX "\@end iftex\n\@c end inserted refs from $doc_file:$lline\n";
393}
394
395###################################################################
396#
397# @c lib libname.lib[:proc] [no_ex, no_fun, (\w*)section]
398#   --> replaced by @include $texfile where
399#        $texfile = $subdir/libname_lib[_noFun,_noEx].tex
400#   --> if $make, calls "make $texfile"
401#   --> Error, if $tex_file does not exist
402#   --> if [:proc] is given, then includes only of respective
403#       proc body
404#   --> if (\w*)section is given, replaces @subsection by @$1section
405#       and pastes in content of tex file directly
406
407sub HandleLib
408{
409  my($lib, $proc, $n_fun, $n_ex, $section, $tex_file);
410
411  if (/^\@c\s*lib\s+([^\.]+)\.lib(.*)/)
412  {
413    $lib = $1;
414    $_ = $2;
415  }
416  else
417  {
418    warn "$WARNING need .lib file to process '$_'\n";
419    print TEX $_;
420    return;
421  }
422
423  $proc = $1 if (/^:(.*?) /);
424  $n_fun = 1 if ($no_fun || /no_fun/);
425  $n_ex = 1 if ($no_ex || /no_ex/);
426  $section = $1 if /\w*section/;
427 
428  # contruct tex file name
429  $tex_file = "$doc_subdir/$lib"."_lib";
430  if ($n_fun)
431  {
432    $tex_file .= "_noFun";
433  }
434  elsif ($n_ex)
435  {
436    $tex_file .= "_noEx";
437  }
438  $tex_file .= ".tex";
439
440  if ($make)
441  {
442    print "<lib $lib" if ($verbose);
443    if ($verbose <= 1)
444    {
445      System("make $tex_file > /dev/null") 
446    }
447    else
448    {
449      System("make $tex_file"); 
450    }
451  }
452 
453  # make sure file exists
454  if (-r $tex_file)
455  {
456    if ($verbose)
457    {
458      print "<lib $lib" unless $make;
459      print ":$proc" if $proc;
460      print ">";
461    }
462  }
463  else
464  {
465    Error("Can't read $tex_file\n") unless -r $tex_file;
466  }
467
468  # see whether we have to paste something in
469  if ($proc || $section)
470  {
471    open(LTEX, "<$tex_file") 
472      || Error("Can't open $tex_file for reading: $!\n");
473
474    print TEX "\@c start include of docu for $lib.lib:$proc\n";
475    print TEX "\@c replaced \@subsection by \@$section\n" if ($section);
476    if ($proc)
477    {
478      my $found = 0;
479      while (<LTEX>)
480      {
481        $found = 1 if /c ---content $proc---/;
482        if ($found)
483        {
484          s/subsection/$section/ if $section;
485          print TEX $_; 
486        }
487        last if $found && /c ---end content $proc---/;
488      }
489      if ($found)
490      {
491        Error("no end content found for lib proc docu for $lib.lib:$proc $doc_file:$line \n")
492          unless (/c ---end content $proc---/);
493        print TEX "\@c generated lib proc docu for $lib.lib:$proc $doc_file:$line \n";
494      }
495      else
496      {
497        Error("did not find lib proc docu for $lib.lib:$proc $doc_file:$line \n");
498      }
499    }
500    else
501    {
502      while (<LTEX>)
503      {
504        s/subsection/$section/;
505        print TEX $_;
506      }
507    }
508    print TEX "\@c end include of docu for $lib.lib:$proc\n";
509    close(LTEX);
510  }
511  else
512  {
513    print TEX "\@c include of docu for $lib.lib\n";
514    print TEX "\@include $tex_file\n";
515  }
516}
517
518####################################################################
519# Auxillary routines
520#
521
522# protect texi special characters
523sub protect_texi
524{
525  s/\@/\@\@/g;
526  s/{/\@{/g;
527  s/}/\@}/g;
528}       
529
530# open w.r.t. include_dirs
531sub Open
532{
533  local(*FH, $file) = @_;
534  local($mode);
535  $file =~ s/^(.{1})(.*)/$2/;
536  $mode = $1;
537
538  foreach $dir (@include_dirs)
539  {
540    return $dir if(open(FH, $mode.$dir."/".$file));
541  }
542}
543   
544# system call with echo on verbose > 1 and die on fail
545sub System
546{
547  local($call) = @_;
548  print "\nd2t system: $call\n" if ($verbose > 1);
549  Error("non-zero exit status of system call: '$call': $!\n")
550    if (system($call));
551}
552
553sub Error
554{
555  print "$ERROR $_[0]";
556  close(TEX);
557  unlink $tex_file if $tex_file && -e $tex_file;
558  exit(1);
559}
560
561#
562# leave this here --otherwise fontification in my emacs gets screwd up
563#
564sub Usage
565{
566  return <<EOT;
567This is doc2tex: a utility to generate Singular texinfo from doc file
568To convert a doc file to texinfo: $0 [options] input_file.doc
569where options can be (abbreviated to shortest possible prefix):
570  -Singular prog: use 'prog' as Singular program to generate ex output
571                          (default: '../Singular/Singular')
572  -output file  : use 'file' as output file
573                          (default: input_file.tex)
574  -clean        : delete intermediate files
575  -make         : use make to generate tex files for libraries
576  -no_reuse     : don't reuse intermediate files
577  -no_ex        : skip computation of examples
578  -no_fun       : don't include help for library functions
579  -subdir  dir  : put intermediate files into 'dir'
580                          (default: './d2t_singular')
581  -I dir        : look also into 'dir' for include  and lib files
582                          (default: ".", "../Singular/LIB")
583  -verbose  val : Set verbosity to 'val' (0=quiet, 1=prot, >1=all)
584  -help         : print help and exit
585EOT
586}
587
Note: See TracBrowser for help on using the repository browser.