summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/glossaries/makeglossaries
blob: 609409dca74eb62a38b24a151a2ed1792a7d201d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#!/usr/bin/env perl

# File    : makeglossaries
# Author  : Nicola Talbot
# Version : 1.6 (2009/05/24)
# Description: simple Perl script that calls makeindex or xindy.
# Intended for use with "glossaries.sty" (saves having to remember
# all the various switches)

# This file is distributed as part of the glossaries LaTeX package.
# Copyright 2007 Nicola L.C. Talbot
# This work may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3
# of this license of (at your option) any later version.
# The latest version of this license is in
#   http://www.latex-project.org/lppl.txt
# and version 1.3 or later is part of all distributions of LaTeX
# version 2005/12/01 or later.
#
# This work has the LPPL maintenance status `maintained'.
#
# The Current Maintainer of this work is Nicola Talbot.

# This work consists of the files glossaries.dtx and glossaries.ins 
# and the derived files glossaries.sty, mfirstuc.sty,
# glossary-hypernav.sty, glossary-list.sty, glossary-long.sty,
# glossary-super.sty, glossaries.perl.
# Also makeglossaries and makeglossaries.
#
# History:
# v1.6 (2009-05-24) :
#   * main glossary no longer automatically added
#     (only added if information in aux file)
#   * if file extension is specified, check added to ensure it
#     corresponds to a known glossary extension.
#   * added file existance test and file empty test
# v1.5 (2008-12-26) :
#   * added support for xindy
#   * picks up ordering information from aux file
# v1.4 (2008-05-10) :
#   * added support for filenames with spaces.
# v1.3 (2008-03-08) :
#   * changed first line from /usr/bin/perl -w to /usr/bin/env perl
#     (Thanks to Karl Berry for suggesting this.)
# v1.2 (2008-03-02) :
#   * added support for --help and --version
#   * improved error handling
# v1.1 (2008-02-13) :
#   * added -w and strict
#   * added check to ensure .tex file not passed to makeglossaries
#
# v1.0 (2007-05-10) : Initial release.

use Getopt::Std;
use strict;
use vars qw($opt_q $opt_t $opt_o $opt_s $opt_p $opt_g $opt_c $opt_r
   $opt_l $opt_i $opt_L $opt_n $opt_C);


$Getopt::Std::STANDARD_HELP_VERSION = 1;

my $version="1.5 (2008-12-26)";

# v1.5 added -L <lang> for xindy (but language can be specified in 
# .tex file)
# v1.5 added -n (print the command that would be issued but
# don't actually run the command)
getopts('s:o:t:p:L:C:ilqrcgn');

unless ($#ARGV == 0)
{
   die "makeglossaries: Need exactly one file argument.\nUse `makeglossaries --help' for help.\n";
}

# define known extensions

# v1.6: removed adding main glossary here as there's no guarantee
# that it's been used. If it has been used, the information will
# be picked up later in the aux file
my %exttype = (
#   main => {in=>'glo', out=>'gls', 'log'=>'glg'},
 );

# v1.5 define require languages for xindy

my %language = ();
my %codepage = ();

my $ext = '';
my $name = $ARGV[0];

# modified this to make sure users don't try passing the
# tex file:
if (length($ARGV[0]) > 3 and substr($ARGV[0],-4,1) eq ".")
{
  $name = substr($ARGV[0],0,length($ARGV[0])-4);

  $ext = substr($ARGV[0],-3,3);

  if (lc($ext) eq 'tex')
  {
     die("Don't pass the tex file to makeglossaries:\n"
        ."either omit the extension to make all the glossaries, "
        ."or specify one of the glossary files, e.g. $name.glo, to "
        ."make just that glossary.\n")
  }
}

my $istfile = "";

# should letter ordering be used? (v1.5 added)

my $letterordering = defined($opt_l);

# check aux file for other glossary types
# and for ist file name

if (open AUXFILE, "$name.aux")
{
   while (<AUXFILE>)
   {
      if (m/\\\@newglossary\s*\{(.*)\}{(.*)}{(.*)}{(.*)}/)
      {
         $exttype{$1}{'log'} = $2;
         $exttype{$1}{'out'} = $3;
         $exttype{$1}{'in'}  = $4;

         unless ($opt_q)
         {
            print "added glossary type '$1' ($2,$3,$4)\n";
         }
      }

      if (m/\\\@istfilename\s*{([^}]*)}/)
      {
         $istfile = $1;

         # check if double quotes were added to \jobname
         $istfile=~s/^"(.*)"\.ist$/$1.ist/;
      }

      # v1.5 added
      if (m/\\\@xdylanguage\s*{([^}]+)}{([^}]*)}/)
      {
         $language{$1} = $2;
      }

      # v1.5 added
      if (m/\\\@gls\@codepage\s*{([^}]+)}{([^}]*)}/)
      {
         $codepage{$1} = $2;
      }

      # v1.5 added
      # Allow -l switch to override specification in aux file
      unless (defined($opt_l))
      {
         if (m/\\\@glsorder\s*{([^}]+)}/)
         {
            my $ordering = $1;

            if ($ordering eq "word")
            {
               $letterordering = 0;
            }
            elsif ($ordering eq "letter")
            {
               $letterordering = 1;
            }
            else
            {
               print STDERR
                 "WARNING: Unknown ordering '$letterordering'\n",
                 "Assuming word ordering\n";
               $letterordering = 0;
            }
         }
      }
   }

   close AUXFILE;
}
else
{
   print STDERR "Unable to open $name.aux: $!\n";
}

# has the style file been specified?
unless ($istfile)
{
   die "No \\\@istfilename found in '$name.aux'.\n",
       "Did your LaTeX run fail?\n",
       "Did your LaTeX run produce any output?\n",
       "Did you remember to use \\makeglossaries?\n";
}

# v1.5 save the general xindy switches

my $xdyopts = '';

unless ($opt_L eq "")
{
  $xdyopts .= " -L $opt_L";
}

# save all the general makeindex switches

my $mkidxopts = '';

if ($opt_i)
{
   $mkidxopts .= " -i";
}

if ($letterordering)
{
   $mkidxopts .= " -l";
   $xdyopts .= " -M ord/letorder";
}

if ($opt_q)
{
   $mkidxopts .= " -q";
   $xdyopts .= " -q";
}

if ($opt_r)
{
   $mkidxopts .= " -r";
}

if ($opt_c)
{
   $mkidxopts .= " -c";
}

if ($opt_g)
{
   $mkidxopts .= " -g";
}

unless ($opt_p eq "")
{
   $mkidxopts .= " -p $opt_p";
}

unless ($opt_s eq "")
{
   $istfile = $opt_s;
}

# Use xindy if style file ends with .xdy otherwise use makeindex

my $usexindy = $istfile=~m/\.xdy\Z/;

if ($ext ne '')
{
   # an extension has been specified, so only process
   # the specified file

   # v1.6 %thistype is no longer given a default value
   my %thistype;

   my $thislang = "";
   my $thiscodepage = "";

   foreach my $type (keys %exttype)
   {
      if ($exttype{$type}{'in'} eq $ext)
      {
         %thistype = %{$exttype{$type}};

         $thislang = $language{$type};

         $thiscodepage = $codepage{$type};

         last;
      }
   }

   # v1.6 If %thistype hasn't been defined, then the given
   # extension doesn't correspond to any known glossary type

   unless (defined %thistype)
   {
      die "The file extension '$ext' doesn't correspond to any\n",
          "known glossary extension. Try running makeglossaries\n",
          "without an extension, e.g. makeglossaries \"$name\".\n";
   }

   my $outfile;

   if ($opt_o eq "")
   {
      $outfile = "$name.$thistype{out}";
   }
   else
   {
      $outfile = $opt_o;
   }

   my $transcript;

   if ($opt_t eq "")
   {
      $transcript = "$name.$thistype{'log'}";
   }
   else
   {
      $transcript = $opt_t;
   }

   if ($usexindy)
   {
      &xindy("$name.$ext", $outfile, $transcript,$istfile,
        $thislang, $thiscodepage, $xdyopts, $opt_q, $opt_n);
   }
   else
   {
      &makeindex("$name.$ext",$outfile,$transcript,$istfile,
                 $mkidxopts,$opt_q, $opt_n);
   }
}
else
{
   # no file extension specified so process all glossary types

   foreach my $type (keys %exttype)
   {
      my %thistype = %{$exttype{$type}};

      my $inputfile = "$name.$thistype{in}";

      # v1.6 added file existance test
      unless (-e $inputfile)
      {
         print STDERR "File '$inputfile' doesn't exist.\n",
             "*** Skipping glossary '$type'. ***\n";
         next;
      }

      unless (-r $inputfile)
      {
         print STDERR "No read access for '$inputfile' $!\n",
             "*** Skipping glossary '$type'. ***\n";
         next;
      }

      # v1.6 added file empty test
      if (-z $inputfile)
      {
         print STDERR "File '$inputfile' is empty.\n",
             "Have you used any entries defined in glossary '$type'?\n",
             "*** Skipping glossary '$type'. ***\n";
         next;
      }

      my $outfile;

      if ($opt_o eq "")
      {
         $outfile = "$name.$thistype{out}";
      }
      else
      {
         $outfile = $opt_o;
      }

      my $transcript;

      if ($opt_t eq "")
      {
         $transcript = "$name.$thistype{'log'}";
      }
      else
      {
        $transcript = $opt_t;
      }

      if ($usexindy)
      {
         &xindy($inputfile,$outfile,$transcript,$istfile,
                $language{$type},$codepage{$type},
                $xdyopts,$opt_q,$opt_n);
      }
      else
      {
         &makeindex($inputfile,$outfile,$transcript,
                    $istfile,$mkidxopts,$opt_q,$opt_n);
      }
   }
}

sub makeindex{
   my($in,$out,$trans,$ist,$rest,$quiet,$dontexec) = @_;
   my($name,$cmdstr,$buffer,$n,$i,$j);
   my(@stuff,@item);

   $cmdstr = "$rest -s \"$ist\" -t \"$trans\" -o \"$out\" \"$in\"";

   unless ($quiet)
   {
      print "makeindex $cmdstr\n";
   }

   unless ($dontexec)
   {
      open STATUS, "makeindex $cmdstr 2>&1 |"
         or die "Can't fork: $!\n";

      my $status = '';

      while (<STATUS>)
      {
         $status .= $_;
      }

      close STATUS;

      if ($?)
      {
         my $errno = $?;

         if (open LOGFILE, ">>$trans")
         {
            print LOGFILE "\n$status";
            close LOGFILE;
         }
         else
         {
            print STDERR "Unable to open '$trans' $!\n";
         }

         die "Call to makeindex failed (errno=$errno):\n", $status;
      }

      print $status;
   }
}

sub xindy{
   my($in,$out,$trans,$ist,$language,$codepage,$rest,$quiet,
     $dontexec) = @_;
   my($cmdstr, $langparam, $main);
   my($module);

   $module = $ist;
   $module=~s/\.xdy\Z//;

   # map babel names to xindy names
   if ($language eq "frenchb")
   {
      $language = "french";
   }
   elsif ($language=~/^n?germanb?$/)
   {
      $language = "german";
   }
   elsif ($language eq "magyar")
   {
      $language = "hungarian";
   }
   elsif ($language eq "lsorbian")
   {
      $language = "lower-sorbian";
   }
   elsif ($language eq "norsk")
   {
      $language = "norwegian";
   }
   elsif ($language eq "portuges")
   {
      $language = "portuguese";
   }
   elsif ($language eq "russianb")
   {
      $language = "russian";
   }
   elsif ($language eq "slovene")
   {
      $language = "slovenian";
   }
   elsif ($language eq "ukraineb")
   {
      $language = "ukrainian";
   }
   elsif ($language eq "usorbian")
   {
      $language = "upper-sorbian";
   }

   if ($language)
   {
     $langparam = "-L $language";
   }
   else
   {
     $langparam = "";
   }

   # most languages work with xindy's default codepage, but
   # some don't, so if the codepage isn't specific, check
   # the known cases that will generate an error
   # and supply a default. (For all other cases, it's up to the 
   # user to supply a codepage.)

   if ($codepage eq '')
   {
      if ($language eq 'dutch')
      {
         $codepage = "ij-as-ij";
      }
      elsif ($language eq 'german')
      {
         $codepage = "din";
      }
      elsif ($language eq 'gypsy')
      {
         $codepage = "northrussian";
      }
      elsif ($language eq 'hausa')
      {
         $codepage = "utf";
      }
      elsif ($language eq 'klingon')
      {
         $codepage = "utf";
      }
      elsif ($language eq 'latin')
      {
         $codepage = "utf";
      }
      elsif ($language eq 'mongolian')
      {
         $codepage = "cyrillic";
      }
      elsif ($language eq 'slovak')
      {
         $codepage = "small";
      }
      elsif ($language eq 'spanish')
      {
         $codepage = "modern";
      }
      elsif ($language eq 'vietnamese')
      {
         $codepage = "utf";
      }
   }

   my $codepageparam = "";

   if ($codepage)
   {
      $codepageparam = "-C $codepage";
   }

   $main = join(' ',
      "-I xindy",
      "-M \"$module\"",
      "-t \"$trans\"",
      "-o \"$out\"",
      "\"$in\"");

   $cmdstr = join(' ', $rest, $langparam, $codepageparam, $main);

   unless ($quiet)
   {
      print "xindy $cmdstr\n";
   }

   unless ($dontexec)
   {
      open STATUS, "xindy $cmdstr 2>&1 |" or die "Can't fork: $!\n";

      my $status = '';
      my $warnings = "";
      my $retry ="";

      while (<STATUS>)
      {
         $status .= $_;

         $warnings .= $_ if /WARNING:/;
      }

      close STATUS;

      if ($status=~/Cannot locate xindy module for language ([^\s]+) in codepage ([^\s]+)/)
      {
         $cmdstr = join(' ', $rest, $langparam, $main);

         unless ($quiet)
         {
            my $message = "$&\nRetrying using default codepage.\n";

            print STDERR $message;

            $retry .= $message;

            print "xindy $cmdstr\n";
         }

         open STATUS, "xindy $cmdstr 2>&1 |" 
           or die "Can't fork: $!\n";

         $status = '';

         while (<STATUS>)
         {
            $status .= $_;
         }

         close STATUS;
      }

      if ($status=~/Cannot locate xindy module for language ([^\s]+)/
       and $1 ne 'general')
      {
         $cmdstr = join(' ', $rest, "-L general", $main);

         unless ($quiet)
         {
            my $message = "$&\nRetrying with -L general\n";

            print STDERR $message;
            $retry .= $message;

            print "xindy $cmdstr\n";
         }

         open STATUS, "xindy $cmdstr 2>&1 |" 
           or die "Can't fork: $!\n";

         $status = '';

         while (<STATUS>)
         {
            $status .= $_;
         }

         close STATUS;
      }

      if ($?)
      {
         my $errno = $?;

         if (open LOGFILE, ">>$trans")
         {
            print LOGFILE "\n$status";
            close LOGFILE;
         }
         else
         {
            print STDERR "Unable to open '$trans' $!\n";
         }

         if ($language)
         {
            die "Call to xindy failed (errno=$errno):\n", $status;
         }
         else
         {
            # If the language hasn't been set, then it may be
            # because the document doesn't contain
            # \printglossaries/\printglossary or it may be
            # because the user has a customized style file that
            # contains the language settings.

            die "Call to xindy failed (errno=$errno):\n", $status,
             "\n\nNo language detected.",
             "\nHave you remembered to use \\printglossary\n",
             "or \\printglossaries in your document?\n\n";
         }
      }

      print $status;

      if (open LOGFILE, ">>$trans")
      {
         print LOGFILE "\n$warnings";

         if ($retry)
         {
            print LOGFILE "\nmakeglossaries messages:\n\n", $retry;
         }

         close LOGFILE;
      }
      else
      {
         print STDERR "Unable to open '$trans' $!\n";
      }

   }
}

sub HELP_MESSAGE{
   print "\nSyntax : makeglossaries [options] <filename>\n\n";
   print "For use with the glossaries package to pass relevant\n";
   print "files to makeindex or xindy\n\n";
   print "<filename>\tBase name of glossary file(s). This should\n";
   print "\t\tbe the name of your main LaTeX document without any\n";
   print "\t\textension.\n";
   print "\n General Options:\n";
   print "-o <gls>\tUse <gls> as the output file.\n";
   print "-q\t\tQuiet mode\n";
   print "-s <sty>\tEmploy <sty> as the style file\n";
   print "-t <log>\tEmploy <log> as the transcript file\n";

   print "\n Xindy Options:\n";
   print "-L <language>\tUse <language>.\n";

   print "\n Makeindex Options:\n";
   print "-c\t\tCompress intermediate blanks\n";
   print "-g\t\tEmploy German word ordering\n";
   print "-l\t\tLetter ordering\n";   
   print "-p <num>\tSet the starting page number to be <num>\n";
   print "-r\t\tDisable implicit page range formation\n";
   print "\nSee makeindex documentation for further details on these ";
   print "options\n";
}

sub VERSION_MESSAGE{
   print "Makeglossaries Version $version\n";
   print "Copyright (C) 2007 Nicola L C Talbot\n";
   print "This material is subject to the LaTeX Project Public License.\n";
}

1;