Debian Codegraph @

texk/texlive/linked_scripts/makedtx/makedtx.pl

raw
1 #! /usr/bin/env perl
2
3 # File : makedtx
4 # Author : Nicola L. C. Talbot
5 # Date : 29 Oct 2004
6 # Last Modified : $Date: 2026-02-15 01:18:48 +0100 (dim., 15 févr. 2026) $
7 # Version : 1.2 $Rev: 106 $
8
9 # usage : makedtx [options] -src <expr>=><expr> -doc <filename> <basename>
10 #
11 # -h : help message
12 # -src <expr>=><expr> : e.g. -src "(foo)src\.(bar)=>$1.$2" will add foosrc.bar to <basename>.dtx to be extracted to foo.bar
13 # -doc <filename> : file containing documentation.
14 # -prefinale <string> : text to add to dtx file just before \Finale (added to version 0.91b)
15 # <basename> : create <basename>.dtx and <basename>.ins
16
17 use strict;
18 use warnings;
19 use Getopt::Long;
20
21 my $version = "1.3";
22
23 my $srcdir = ".";
24 my $patternop = "=";
25 my $verbose = 0;
26 my $ins = 1;
27 my $testmode = 0;
28 my $askforoverwrite = 0;
29 my $preamble = "";
30 my $postamble = "";
31 my $author;
32 my @comment;
33 my @source;
34 my @setambles;
35 my @macrocode;
36 my $docsrc;
37 my $drivendoc = 1;
38
39 if ($^O =~ m/MSWin/)
40 {
41 $author = $ENV{"USERNAME"};
42 if ($author eq ""){ $author = 'Unknown';}
43 }
44 else
45 {
46 $author = (getpwuid($<))[6] || 'Unknown';
47 }
48 my $stopeventually = "";
49 my $prefinale = "";
50 my $codetitle = "The Code";
51 my $section = "section";
52 my $license = "lppl";
53
54 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
55
56 $year = $year + 1900;
57 my $copyright_date = $year;
58 my $creation_date = "$year/" . ($mon+1) . "/$mday $hour:" . ($min<10?"0$min" : $min);
59
60 my @cmdline_args = capture_cmdline(@ARGV);
61
62 # process command line options
63 # v1.1 added section switch
64 &GetOptions("h|help" => \&help,
65 "v" => \$verbose,
66 "src=s@" => \@source,
67 "doc=s" => \$docsrc,
68 "dir=s" => \$srcdir,
69 "op=s" => \$patternop,
70 "askforoverwrite!" => \$askforoverwrite,
71 "ins!" => \$ins,
72 "preamble=s" => \$preamble,
73 "postamble=s" => \$postamble,
74 "setambles=s@" => \@setambles,
75 "testmode" => \$testmode,
76 "macrocode=s@" => \@macrocode,
77 "author=s" => \$author,
78 "date=s" => \$copyright_date,
79 "stopeventually=s" => \$stopeventually,
80 "prefinale=s" => \$prefinale,
81 "codetitle=s" => \$codetitle,
82 "comment=s@" => \@comment,
83 "drivendoc!" => \$drivendoc,
84 "version" => \&print_version,
85 "license=s" => \$license,
86 "section=s" => \$section
87 ) or &syntaxerror();
88
89 if($testmode)
90 {
91 $version = "x.y";
92 $copyright_date = "YYYY";
93 $creation_date = "YYYY/MM/DD hh:mm";
94 }
95
96 if ($#ARGV != 0)
97 {
98 print "No basename specified\n";
99 &syntaxerror();
100 }
101
102 my $basename = $ARGV[0];
103
104 if ($docsrc eq "")
105 {
106 print "No document source specified (missing -doc)\n";
107 &syntaxerror();
108 }
109
110 if ($#source == -1)
111 {
112 print "No source code specified (missing -src)\n";
113 &syntaxerror();
114 }
115
116 open DTX, ">:unix","$basename.dtx" or die "Can't open '$basename.dtx'\n";
117
118 if ($verbose)
119 {
120 print "Documentation source : " . $docsrc . "\n";
121 }
122
123 # work out the derived files
124
125 my @srcdirfile = glob("$srcdir/*");
126
127 my @derivedfiles = ();
128
129 my @outputfiles = ();
130
131 my $numoutput = 0;
132
133 foreach my $source (@source)
134 {
135 my ($infile, $outfile, $remainder) = split /=>/, $source;
136
137 if ($outfile eq "")
138 {
139 print "-src $source argument invalid (no output file specified)\n";
140
141 &syntaxerror();
142 }
143
144 if (defined $remainder)
145 {
146 print "-src $source argument invalid (too many => specified)\n";
147
148 &syntaxerror();
149 }
150
151 foreach my $srcdirfile (@srcdirfile)
152 {
153 my $fileexp = $srcdir . "/" . $infile;
154
155 $_ = $srcdirfile;
156
157 my $expr = "s$patternop$fileexp$patternop$outfile$patternop";
158
159 if (eval($expr))
160 {
161 my $thisoutfile = $_;
162
163 my $thisinfile = $srcdirfile;
164
165 $derivedfiles[$numoutput]{'in'} = $thisinfile;
166 $derivedfiles[$numoutput]{'out'} = $thisoutfile;
167 $outputfiles[$numoutput] = $thisoutfile;
168
169 $numoutput++;
170 }
171 }
172 }
173
174 if ($preamble eq "")
175 {
176 if ($license eq "lppl")
177 {
178 # v1.2 fixed typo in preamble
179 $preamble = <<_END_LICENSE;
180
181 $basename.dtx
182 Copyright $copyright_date $author
183
184 This work may be distributed and/or modified under the
185 conditions of the LaTeX Project Public License, either version 1.3
186 of this license or (at your option) any later version.
187 The latest version of this license is in
188 http://www.latex-project.org/lppl.txt
189 and version 1.3 or later is part of all distributions of LaTeX
190 version 2005/12/01 or later.
191
192 This work has the LPPL maintenance status `maintained'.
193
194 The Current Maintainer of this work is $author.
195
196 _END_LICENSE
197 }
198 elsif ($license eq 'bsd')
199 {
200 $preamble = <<_END_LICENSE;
201
202 $basename.dtx
203 Copyright (c) $copyright_date $author
204 All rights reserved.
205
206 Redistribution and use in source and binary forms, with or without
207 modification, are permitted provided that the following conditions
208 are met:
209 1. Redistributions of source code must retain the above copyright
210 notice, this list of conditions and the following disclaimer.
211 2. Redistributions in binary form must reproduce the above copyright
212 notice, this list of conditions and the following disclaimer in the
213 documentation and/or other materials provided with the distribution.
214 3. The name of the author may not be used to endorse or promote products
215 derived from this software without specific prior written permission.
216
217 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
218 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
219 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
220 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
221 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
222 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
224 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
226 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
227
228 _END_LICENSE
229 }
230 elsif ($license eq 'gpl')
231 {
232 $preamble = <<_END_LICENSE;
233
234 $basename.dtx
235 Copyright (c) $copyright_date $author
236
237 This program is free software; you can redistribute it and/or modify
238 it under the terms of the GNU General Public License as published by
239 the Free Software Foundation; either version 2 of the License, or
240 (at your option) any later version.
241
242 This program is distributed in the hope that it will be useful,
243 but WITHOUT ANY WARRANTY; without even the implied warranty of
244 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
245 GNU General Public License for more details.
246
247 You should have received a copy of the GNU General Public License
248 along with this program; if not, write to the Free Software
249 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
250
251 _END_LICENSE
252 }
253 else
254 {
255 die "Unknown license '$license'\n".
256 "Known licenses: lppl, bsd, gpl";
257 }
258
259 $preamble .= " This work consists of the files "
260 . "$basename.dtx and $basename.ins and the derived "
261 . ($numoutput > 1 ? "files" : "file")
262 . " " . join(', ', @outputfiles) . ".\n";
263 }
264
265 open DOC, '<:crlf',$docsrc or die "Can't open '$docsrc'\n";
266
267 print DTX "\%\\iffalse\n";
268 print DTX "\% $basename.dtx generated using makedtx version $version (c) Nicola Talbot\n";
269 print DTX "\% Command line args:\n";
270
271 if($testmode){
272 @cmdline_args = sort @cmdline_args;
273 }
274 foreach my $arg (@cmdline_args){
275 print DTX "\% ", $arg , "\n";
276 }
277
278 print DTX <<__EOF_HEADER
279 \% Created on $creation_date
280 \%\\fi
281 \%\\iffalse
282 \%<*package>
283 \%\% \\CharacterTable
284 \%\% {Upper-case \\A\\B\\C\\D\\E\\F\\G\\H\\I\\J\\K\\L\\M\\N\\O\\P\\Q\\R\\S\\T\\U\\V\\W\\X\\Y\\Z
285 \%\% Lower-case \\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\l\\m\\n\\o\\p\\q\\r\\s\\t\\u\\v\\w\\x\\y\\z
286 \%\% Digits \\0\\1\\2\\3\\4\\5\\6\\7\\8\\9
287 \%\% Exclamation \\! Double quote \\\" Hash (number) \\#
288 \%\% Dollar \\\$ Percent \\\% Ampersand \\&
289 \%\% Acute accent \\\' Left paren \\( Right paren \\)
290 \%\% Asterisk \\* Plus \\+ Comma \\,
291 \%\% Minus \\- Point \\. Solidus \\/
292 \%\% Colon \\: Semicolon \\; Less than \\<
293 \%\% Equals \\= Greater than \\> Question mark \\?
294 \%\% Commercial at \\\@ Left bracket \\[ Backslash \\\\
295 \%\% Right bracket \\] Circumflex \\^ Underscore \\_
296 \%\% Grave accent \\\` Left brace \\{ Vertical bar \\|
297 \%\% Right brace \\} Tilde \\~}
298 \%</package>
299 \%\\fi
300 \% \\iffalse
301 \% Doc-Source file to use with LaTeX2e
302 \% Copyright (C) $copyright_date $author, all rights reserved.
303 \% \\fi
304 \% \\iffalse
305 \%<*driver>
306 __EOF_HEADER
307 ;
308
309 my $stopfound=0;
310 my $indoc=0;
311
312 while (<DOC>)
313 {
314 s/\\usepackage\{creatdtx}//;
315
316 my $restofline = $_;
317
318 my $beginline = "";
319 my $line = $restofline;
320 my $group;
321
322 while ($restofline =~ /(.*)\\ifmakedtx(.*)/)
323 {
324 $beginline = $1;
325
326 my $done;
327
328 ($group,$restofline,$done) = &getnextgroup($2);
329
330 my $startline = $.;
331
332 while (!$done)
333 {
334 if (my $nextline = <DOC>)
335 {
336 $line = $line . $nextline;
337
338 $restofline = $restofline . $nextline;
339
340 ($group,$restofline,$done) = &getnextgroup($restofline);
341 }
342 else
343 {
344 die "EOF found whilst scanning first argument to \\ifmakedtx on line $startline\n";
345 }
346 }
347
348 # print first arg, ignore second
349
350 $beginline = $beginline . $group;
351
352 ($group,$restofline,$done) = &getnextgroup($restofline);
353
354 while (!$done)
355 {
356 if (my $nextline = <DOC>)
357 {
358 $line = $line . $nextline;
359
360 $restofline = $restofline . $nextline;
361
362 ($group,$restofline,$done) = &getnextgroup($restofline);
363 }
364 else
365 {
366 die "EOF found whilst scanning second argument to \\ifmakedtx on line $startline\n";
367 }
368 }
369
370 $line = $restofline;
371 }
372
373 $line = $beginline . $restofline;
374
375 if (!$drivendoc && $indoc && $line=~/\\end *\{document}/) {
376 $indoc = 0;
377 last;
378 }
379
380 print DTX $line;
381
382 if ($line=~/\\begin\{document}/)
383 {
384 $indoc = 1;
385
386 if ($drivendoc) { last; }
387 }
388 }
389
390 print DTX "\\DocInput{$basename.dtx}\n";
391 print DTX "\\end{document}\n";
392 print DTX "\%</driver>\n";
393 print DTX "\%\\fi\n";
394
395 my $inverb=0;
396
397 if ($drivendoc) {
398 print DTX "\%";
399
400 while (<DOC>)
401 {
402 if (/\\begin\{verbatim}/)
403 {
404 $inverb=1;
405 }
406
407 if (/\\end\{verbatim}/)
408 {
409 $inverb=0;
410 }
411
412 if (/\\StopEventually/ && ($inverb==0))
413 {
414 $stopfound=1;
415 }
416
417 my $restofline = $_;
418
419 my $beginline = "";
420 my $line = $restofline;
421 my $group;
422
423 while ($restofline =~ /(.*)\\ifmakedtx(.*)/)
424 {
425 $beginline = $1;
426
427 my $done;
428
429 ($group,$restofline,$done) = &getnextgroup($2);
430
431 my $startline = $.;
432
433 while (!$done)
434 {
435 if (my $nextline = <DOC>)
436 {
437 $line = $line . $nextline;
438
439 $restofline = $restofline . $nextline;
440
441 ($group,$restofline,$done) = &getnextgroup($restofline);
442 }
443 else
444 {
445 die "EOF found whilst scanning first argument to \\ifmakedtx on line $startline\n";
446 }
447 }
448
449 # print first arg, ignore second
450
451 $beginline = $beginline . $group;
452
453 ($group,$restofline,$done) = &getnextgroup($restofline);
454
455 while (!$done)
456 {
457 if (my $nextline = <DOC>)
458 {
459 $line = $line . $nextline;
460
461 $restofline = $restofline . $nextline;
462
463 ($group,$restofline,$done) = &getnextgroup($restofline);
464 }
465 else
466 {
467 die "EOF found whilst scanning second argument to \\ifmakedtx on line $startline\n";
468 }
469 }
470
471 $line = $restofline;
472 }
473
474 $line = $beginline . $restofline;
475
476 if (($line=~/\\end\{document}/) and not $inverb)
477 {
478 $indoc=0;
479
480 $line=~s/\\end\{document}//;
481 }
482
483 $line=~s/\n/\n\%/mg;
484
485 print DTX "$line";
486 }
487 print DTX "\n";
488 }
489 close DOC;
490
491 if ($stopfound==0)
492 {
493 print DTX "\%\\StopEventually{$stopeventually}\n";
494 }
495
496 # v1.0 added check
497 if ($codetitle)
498 {
499 # v1.1 added section
500 print DTX "\%\\$section\{$codetitle\}\n";
501 }
502
503 for (my $idx = 0; $idx <= $#derivedfiles; $idx++)
504 {
505 my $thisinfile = $derivedfiles[$idx]{'in'};
506 my $thisoutfile = $derivedfiles[$idx]{'out'};
507
508 if ($verbose)
509 {
510 print "$thisinfile -> $thisoutfile\n";
511 }
512
513 open SRC, "<:crlf", $thisinfile or die "Can't open $thisinfile\n";
514
515 print DTX "\%\\iffalse\n";
516 print DTX "\% \\begin{macrocode}\n";
517 print DTX "\%<*$thisoutfile>\n";
518 print DTX "\% \\end{macrocode}\n";
519 print DTX "\%\\fi\n";
520
521 my $macrocode = 0;
522 my $comment = 0;
523
524 foreach my $expr (@comment)
525 {
526 if ($thisoutfile =~ m/$expr/)
527 {
528 print DTX "\%\\iffalse\n";
529
530 $comment = 1;
531
532 last;
533 }
534 }
535
536 foreach my $expr (@macrocode)
537 {
538 if ($thisoutfile =~ m/$expr/)
539 {
540 print DTX "\% \\begin{macrocode}\n";
541
542 $macrocode = 1;
543
544 last;
545 }
546 }
547
548 while (<SRC>)
549 {
550 print DTX "$_";
551 }
552
553 if ($macrocode == 1)
554 {
555 print DTX "\% \\end{macrocode}\n";
556 }
557
558 if ($comment == 1)
559 {
560 print DTX "\%\\fi\n";
561 }
562
563 print DTX "\%\\iffalse\n";
564 print DTX "\% \\begin{macrocode}\n";
565 print DTX "\%</$thisoutfile>\n";
566 print DTX "\% \\end{macrocode}\n";
567 print DTX "\%\\fi\n";
568
569 close SRC;
570 }
571
572 print DTX "\%$prefinale\n" if ($prefinale);
573 print DTX "\%\\Finale\n";
574 print DTX "\\endinput\n";
575
576 close DTX;
577
578 if ($ins)
579 {
580 open INS, ">:unix","$basename.ins" or die "Can't open '$basename.ins'\n";
581
582 print INS "\% $basename.ins generated using makedtx version $version $creation_date\n";
583
584 print INS "\\input docstrip\n\n";
585 print INS "\\preamble\n";
586 print INS "$preamble\n";
587 print INS "\\endpreamble\n\n";
588
589 if ($postamble ne "")
590 {
591 print INS "\\postamble\n";
592 print INS "$postamble\n";
593 print INS "\\endpostamble\n\n";
594 }
595
596 if ($askforoverwrite)
597 {
598 print INS "\\askforoverwritetrue\n\n";
599 }
600 else
601 {
602 print INS "\\askforoverwritefalse\n\n";
603 }
604
605 print INS "\\generate{";
606
607 for (my $idx = 0; $idx <= $#derivedfiles; $idx++)
608 {
609 my $thisinfile = $derivedfiles[$idx]{'in'};
610 my $thisoutfile = $derivedfiles[$idx]{'out'};
611
612 print INS "\\file{$thisoutfile}{";
613
614 my $ambleset = 0;
615 my $noamble = 0;
616
617 foreach my $setamble (@setambles)
618 {
619 my ($fileexp, $amble, $remainder) = split /=>/, $setamble;
620
621 if (defined $remainder)
622 {
623 die "-setambles $setamble argument invalid (too many => specified)\n";
624 }
625
626 if ($thisoutfile =~ m/$fileexp/)
627 {
628 if ($verbose)
629 {
630 print "$fileexp matches $thisoutfile -> setting \"$amble\"\n";
631 }
632
633 print INS $amble;
634
635 $ambleset = 1;
636
637 if ($amble =~ m/\\nopreamble/)
638 {
639 $noamble = 1;
640 }
641 }
642 }
643
644 if (!$ambleset)
645 {
646 print INS "\\usepreamble\\defaultpreamble\n\\usepostamble\\defaultpostamble";
647 }
648
649 print INS "\\from{$basename.dtx}{$thisoutfile";
650
651 if ($noamble == 0)
652 {
653 # this will add the character table to all files except those that use \nopreamble
654
655 print INS ",package";
656 }
657
658 print INS "}}\n";
659 }
660
661 print INS "}\n\n";
662
663 print INS "\\endbatchfile\n";
664
665 close INS;
666 }
667
668 sub syntaxerror
669 {
670 die "Syntax : makedtx [options] <basename>\nUse -h for help\n";
671 }
672
673 sub help
674 {
675 print <<__EOF_HELP
676 makedtx Help
677
678 Current Version : $version
679
680 usage : makedtx [options] -src "<expr>=><expr>" -doc <filename> <basename>
681
682 makedtx can be used to construct a LaTeX2e dtx and ins file from
683 the specified source code. The final command line argument
684 <basename> should be used to specify the basename of the dtx
685 and ins files.
686
687 -src "<expr1>=><expr2>"
688 The command line switch -src identifies the original source code and the name
689 of the file to which it will utimately be extracted on latexing the ins file
690 <expr1> can be a Perl expression, such as (foo)src.(sty), and <expr2> can
691 a Perl substitution style expression, such as \$1.\$2
692 Note that double quotes must be used to prevent shell expansion
693 Multiple invocations of -src are permitted
694 See examples below.
695
696 -doc <filename>
697 The name of the documentation source code. This should be a LaTeX2e document
698
699 Optional Arguments:
700
701 -dir <directory> : search for source files in <directory>
702 -op <character> : set the pattern matching operator (default '$patternop')
703 -askforoverwrite : set askforoverwrite switch in INS file to true
704 -noaskforoverwrite : set askforoverwrite switch in INS file to false (default)
705 -preamble <text> : set the preamble. Standard one inserted if omitted
706 -postamble <text> : set the postamble.
707 -setambles "<pattern>=><text>" : set pre- and postambles to <text> if file matches
708 pattern
709 -author <text> : name of author (inserted into standard preamble. User name
710 inserted if omitted)
711 -date <text> : copyright date
712 -ins : create the ins file (default)
713 -noins : don't create the ins file
714 -prefinale <text> : add <text> immediately prior to \\Finale
715 -macrocode <expr> : surround any file which matches <expr> in a macrocode
716 environment
717 -comment <expr> : surround any file which matches <expr> with \\iffalse \\fi pair
718 -codetitle <text> : The title for the documented code section (default: The Code)
719 -section <cs> : The sectioning command to use for the documented code section
720 (default: 'section')
721 -license <license> : use the given license for the preamble.
722 Known licenses: lppl (default), bsd, gpl.
723 -h : help message
724 -nodrivendoc : documentation body is placed in driver block as is rather than
725 being make docstrippable (default: not set).
726 -v : verbose
727
728 Examples:
729
730 Example 1:
731 Documenation is in foodoc.tex
732 Source code is in foosrc.sty. The final extracted version should be
733 called foo.sty. The dtx file should be called foo.dtx and the ins file
734 should be called foo.ins
735
736 makedtx -src "foosrc\\.sty=>foo.sty" -doc foodoc.tex foo
737
738 Example 2:
739 Documenation is in bardoc.tex
740 Source code is in barsrc.sty. The final extracted version should be
741 called bar.sty. Source code is also in barsrc.bst. The final extracted
742 version should be called bar.bst. The dtx file should be called bar.dtx and
743 the ins file should be called bar.ins
744
745 makedtx -src "barsrc\\.sty=>bar.sty" -src "barsrc\\.bst=>bar.bst" -doc bardoc.tex bar
746
747 Or
748
749 makedtx -src "barsrc\\.(bst|sty)=>bar.\$1" -doc bardoc.tex bar
750
751 __EOF_HELP
752 ;
753 die;
754 }
755
756 sub print_version
757 {
758 print "makedtx version $version\n";
759 exit 0;
760 }
761
762 sub eatinitialspaces
763 {
764 my ($STR) = @_;
765
766 while (substr($STR,0,1) =~ m/\s/)
767 {
768 $STR = substr($STR,1);
769 }
770
771 return $STR;
772 }
773
774 sub getnextgroup
775 {
776 my($curline) = @_;
777
778 $curline = &eatinitialspaces($curline);
779
780 # check to see if current string is blank
781
782 if ($curline!~/[^\s]+/m)
783 {
784 return ("","",0);
785 }
786
787 if ((my $group = substr($curline,0,1)) ne "{")
788 {
789 # next group hasn't been delimited with braces
790 # return first non-whitespace character
791
792 $curline = substr($curline,1);
793
794 # unless it's a backslash, in which case get command name
795
796 if ($group eq "\\")
797 {
798 if ($curline=~/([a-zA-Z]+)(^[a-zA-Z].*)/m)
799 {
800 $group = $1;
801
802 $curline = $2;
803 }
804 else
805 {
806 # command is made up of backslash followed by symbol
807
808 $curline=~/([\W_0-9\s\\])(.*)/m;
809
810 $group = $1;
811
812 $curline = $2;
813 }
814 }
815
816 return ($group,$curline,1);
817 }
818
819 my $pos=index($curline, "{");
820 my $startpos=$pos;
821 my $posopen=0;
822 my $posclose=0;
823
824 my $bracelevel = 1;
825
826 my $done=0;
827
828 while (!$done)
829 {
830 $pos++;
831
832 $posopen = index($curline, "{", $pos);
833
834 # check to make sure it's not a \{
835
836 while ((substr($curline, $posopen-1,1) eq "\\") and ($posopen > 0))
837 {
838 # count how many backlashes come before it.
839
840 my $i = $posopen-1;
841
842 my $numbs = 1;
843
844 while ((substr($curline, $i-1,1) eq "\\") and ($i > 0))
845 {
846 $numbs++;
847 $i--;
848 }
849
850 # is $numbs is odd, we have a \{, otherwise we have \\{
851
852 if ($numbs%2 == 0)
853 {
854 last;
855 }
856 else
857 {
858 $posopen = index($curline, "{", $posopen+1);
859 }
860 }
861
862 $posclose= index($curline, "}", $pos);
863
864 # check to make sure it's not a \}
865
866 while ((substr($curline, $posclose-1,1) eq "\\") and ($posclose > 0))
867 {
868 # count how many backlashes come before it.
869
870 my $i = $posclose-1;
871
872 my $numbs = 1;
873
874 while ((substr($curline, $i-1,1) eq "\\") and ($i > 0))
875 {
876 $numbs++;
877 $i--;
878 }
879
880 # is $numbs is odd, we have a \}, otherwise we have \\}
881
882 if ($numbs%2 == 0)
883 {
884 last;
885 }
886 else
887 {
888 $posclose = index($curline, "}", $posclose+1);
889 }
890 }
891
892 if (($posopen==-1) and ($posclose==-1))
893 {
894 $done=1;
895 }
896 elsif ($posopen==-1)
897 {
898 $pos=$posclose;
899
900 $bracelevel--;
901
902 if ($bracelevel==0)
903 {
904 my $group = substr($curline, $startpos+1, $pos-$startpos-1);
905
906 $curline = substr($curline, $pos+1);
907
908 return ($group,$curline,1);
909 }
910 }
911 elsif ($posclose==-1)
912 {
913 $pos=$posopen;
914
915 $bracelevel++;
916 }
917 elsif ($posopen<$posclose)
918 {
919 $pos=$posopen;
920
921 $bracelevel++;
922 }
923 elsif ($posclose<$posopen)
924 {
925 $pos=$posclose;
926
927 $bracelevel--;
928
929 if ($bracelevel==0)
930 {
931 my $group = substr($curline, $startpos+1, $pos-$startpos-1);
932
933 $curline = substr($curline, $pos+1);
934
935 return ($group,$curline,1);
936 }
937 }
938 }
939
940 # closing brace must be on another line
941
942 return ("", $curline, 0);
943 }
944
945 sub make_argset {
946 my @args = @_;
947 my @escaped_args = ();
948
949 while ($#args >= 0) {
950 my $arg = shift @args;
951 # Quoting is needed when the string $arg contains a special character
952 if ($arg =~ m/[`"\\\$ '*]/) {
953 # Precede characters ` " \ and $ by a backslash to quote them
954 $arg =~ s/([`"\\\$])/\\$1/g;
955 # backslash preceded characters w/o a special meaning (ie ` " \ and $) are left unmodified
956 $arg =~ s/\\([^`"\\\$])/$1/g;
957 $arg = "\"$arg\"";
958 }
959 push @escaped_args, $arg;
960 }
961 return join " ", @escaped_args;
962 }
963
964 sub capture_cmdline
965 {
966 my @args = @_;
967 my @cmdline_args = ();
968 # number of arguments following option switch, 2 means 1 or more
969 my %options = (
970 h => 0,
971 help => 0,
972 v => 0,
973 src => 2,
974 doc => 1,
975 dir => 1,
976 op => 1,
977 askforoverwrite => 0,
978 noaskforoverwrite => 0,
979 ins => 0,
980 noins => 0,
981 preamble => 1,
982 postamble => 1,
983 setambles => 2,
984 testmode => 0,
985 macrocode => 2,
986 author => 1,
987 date => 1,
988 stopeventually => 1,
989 prefinale => 1,
990 codetitle => 1,
991 comment => 2,
992 drivendoc => 0,
993 nodrivendoc => 0,
994 version => 0,
995 license => 1,
996 section => 1
997 );
998 while ($#args >= 0) {
999 my $arg = shift @args;
1000 my $switch = $arg =~ s/\A-{1,2}([a-z])/$1/r;
1001 unless (exists $options{$switch}) {
1002 push @args, $arg;
1003 $arg = '--';
1004 }
1005 if ($arg eq '--') {
1006 while ($#args >= 0) {
1007 my @argset = (shift @args);
1008 push @cmdline_args, make_argset(@argset);
1009 }
1010 last;
1011 }
1012 my $arg_count = $options{$switch};
1013 if ($arg_count == 0) {
1014 push @cmdline_args, $arg;
1015 } elsif ($arg_count == 1) {
1016 $#args >= 0 or die "Missing argument after $arg";
1017 my @argset = ($arg, shift @args);
1018 push @cmdline_args, make_argset(@argset);
1019 } elsif ($arg_count == 2) {
1020 $#args >= 0 or die "Missing argument after $arg";
1021 my @argset = ($arg, shift @args);
1022 while ($#args >= 0 and do {
1023 $arg = $args[0];
1024 $switch = $arg =~ s/\A-{1,2}([a-z])/$1/r;
1025 !exists($options{$switch});
1026 }) {
1027 push @argset, (shift @args);
1028 }
1029 push @cmdline_args, make_argset(@argset);
1030 } else {
1031 die "INTERNAL";
1032 }
1033 }
1034 return @cmdline_args;
1035 }