summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/glossaries/makeglossaries
blob: 3082593df0b9b8819848adb01269a7fa65a2ff9e (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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
#!/usr/bin/env perl

# File    : makeglossaries
# Author  : Nicola Talbot
# Version : 2.14 (2014/03/06)
# 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 or 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.

my $version="2.14 (2014-03-06)";

# History:
# v2.14 (2014/03/06)
#   * Added -Q and -k options
# v2.13 (2014/01/21)
#   * Added check for leading and trailing quotes in filename
# v2.12 (2014/01/20)
#   * Added check for '*' in istfilename
#   * Fixed bug report
#   http://www.dickimaw-books.com/cgi-bin/bugtracker.cgi?action=view&key=33
#   * Added error text hashes
# v2.11 (2014/01/17):
#   * Added check for backslashes in -x, -m and -d paths
#   * Added double-quotes around $appname in &run_app
# v2.10 (2013/12/01):
#   * initialise $language to suppress warnings
# v2.09 (2013-11-12):
#   * added check for -q switch when issuing warnings.
# v2.08 (2013-10-14):
#   * added -x and -m options
# v2.07 (2013-06-17):
#   * added check for success on chdir if -d used
#   * added dialect map
# v2.06 (2013-04-21):
#   * added "din5007" as default if language is set to "german"
# v2.05 (2012-11-12):
#   * added -d option
# v2.04 (2012-04-19):
#   * fixed bug in &scan_aux
# v2.03 (2011-04-12):
#   * added warning about possibly needing 'nomain' package option
# v2.02 (2011-04-2):
#   * Prints version number at start of run unless -q
#   * Added more diagnostics.
# v2.01 (2010-09-29):
#   * Added 'use warnings'
# v2.0 (2010-06-30) :
#   * Made file handle local in &scan_aux
# v1.9 (2010-06-14) :
#   * Check for \@input
# v1.8 (2009-11-03) :
#   * Create an empty output file if the input file is empty
#     without calling xindy/makeindex
# v1.7 (2009-09-23) :
#   * Issue warning rather than error when empty/non existant file 
#     checks fail
# 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;
# v2.01 added the following line
use warnings;
# v2.05 added $opt_d
# v2.08 added $opt_x and $opt_m
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 $opt_d $opt_x $opt_m $opt_Q
   $opt_k);


$Getopt::Std::STANDARD_HELP_VERSION = 1;

# 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:ilqQkrcgnd:x:m:');

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

# v2.02: added:
print "makeglossaries version $version\n" unless ($opt_q);

# v2.07: added babel dialect -> xindy language map:
my %languagemap =
(
  'american'  => 'english',
  'austrian'  => 'german',
  'british'   => 'english',
  'francais'  => 'french',
  'frenchb'   => 'french',
  'germanb'   => 'german',
  'lsorbian'  => 'lower-sorbian',
  'magyar'    => 'hungarian',
  'naustrian' => 'german',
  'ngermanb'  => 'german',
  'norsk'     => 'norwegian',
  'portuges'  => 'portuguese',
  'russianb'  => 'russian',
  'slovene'   => 'slovenian',
  'UKenglish' => 'english',
  'USenglish' => 'english',
  'ukraineb'  => 'ukrainian',
  'usorbian'  => 'upper-sorbian'
);

# v2.07 added check for success and for -q
# v2.05 added:

if ($opt_d)
{
  # v2.11 check for backslash character if on Windows
  $opt_d=~s/\\/\//g if $^O=~/Win/;

  if (chdir $opt_d)
  {
     print "Changed to '$opt_d'\n" unless ($opt_q);
  }
  else
  {
     die "Unable to chdir to '$opt_d' $!\n";
  }
}

# v2.02: added:
my $xdynotist = 1;
# v2.13: removed
#my $needcompatibilitymode=2;

# v2.13: added $istnotfound
my $istnotfound = 3;

# v2.13: added error text:
my %makeindex_error_text =
 (
   $xdynotist => "Style name indicates makeindex, but may be in xindy format.\n"
          . "Remember to use \\setStyleFile to specify the name\n"
          . "of the style file rather than redefining \\istfilename\n"
          . "explicitly.",
   $istnotfound => "Style file not found."
 );

my %xindy_error_text =
 (
   'nosort'    => "Sort key required for entries only containing command names",
   'istnotxdy' =>
      "Style name has xdy extension, but may be in makeindex format.\n"
     . "Remember to use \\setStyleFile to specify the name\n"
     . "of the style file rather than redefining \\istfilename\n"
     . "explicitly.",
   'missingendquote' =>
       "You may have missed a \" character in a command such as \\GlsAddXdyLocation",
    'nolanguage' =>
       "No language detected.\nHave you remembered to use \\printglossary\n".
       "or \\printglossaries in your document?",
    'nomain' =>
        "\nRemember to use package option 'nomain' if you don't\n".
        "want to use the main glossary.",
    'badlocation' =>
        "You may have forgotten to add a location \n"
        . "class with \\GlsAddLocation or you may have \n"
        . "the format incorrect.\n",
    'comp207' => "You may need to add 'compatible-2.07' package option.\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];

# v2.13 added:
$name=~s/^"(.*)"$/$1/;

# 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")
  }
}

# v2.01 add check to see if aux file exists

unless (-e "$name.aux")
{
   die "Auxiliary file '$name.aux' doesn't exist. Have you run LaTeX?\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

&scan_aux($name);

# 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";
}

# v2.08 added $xindyapp and $makeindexapp
# By default assume they are on the operating system's path
my $xindyapp = ($opt_x ? $opt_x :'xindy');
my $makeindexapp = ($opt_m ? $opt_m :'makeindex');

# v2.11 check for backslash character if on Windows
if ($^O=~/Win/)
{
  $xindyapp=~s/\\/\//g;
  $makeindexapp=~s/\\/\//g;
}

# v1.5 save the general xindy switches

my $xdyopts = '';

# v2.01 replaced 'unless ($opt_L eq "")' with 'if ($opt_L)'
if ($opt_L)
{
  $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)
{
# v2.01 removed following lines. (This script now deals with
# printing messages to STDOUT.)
   #$mkidxopts .= " -q";
   #$xdyopts .= " -q";
}

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

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

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

# v2.01 replaced 'unless ($opt_p eq "")' with 'if ($opt_p)'
if ($opt_p)
{
   $mkidxopts .= " -p $opt_p";
}

# v2.01 replaced 'unless ($opt_s eq "")' with 'if ($opt_s)'
if ($opt_s)
{
   # v2.01 check if user has specified -s <file>.ist but aux file
   # indicates .xdy ought to be used and vice-versa. Also check if
   # requested style file exists

   unless (-e $opt_s)
   {
      die "\n",
         "Requested style file '$opt_s' doesn't exist.\n\n";
   }

   if ($istfile=~/\.xdy$/ and $opt_s!~/\.xdy$/)
   {
      die "\n",
        "The auxiliary file indicates that you should be using xindy,\n",
        "but you have specified makeindex style file '$opt_s'\n",
        "Make sure you don't specify 'xindy' as a package option if\n",
        "you want to use makeindex.\n\n",
        "\\usepackage[makeindex]{glossaries}\n\n";
   }
   elsif ($istfile!~/\.xdy$/ and $opt_s=~/\.xdy$/)
   {
      die "\n",
        "The auxiliary file indicates that you should be using\n",
        "makeindex, but you have specified xindy style file '$opt_s'.\n",
        "Make sure you specify 'xindy' as a package option if you\n",
        "want to use xindy.\n\n",
        "\\usepackage[xindy]{glossaries}\n\n";
   }

   $istfile = $opt_s;
}

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

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

if ($ext)
{
   # 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

   # v2.01 replaced deprecated 'defined(%thistype)' with %thistype
   unless (%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;

# v2.01 replaced 'if ($opt_o eq "")' with 'unless ($opt_o)'
   unless ($opt_o)
   {
      $outfile = "$name.$thistype{out}";
   }
   else
   {
      $outfile = $opt_o;
   }

   my $transcript;

# v2.01 replaced 'if ($opt_t eq "")' with 'unless ($opt_t)'
   unless ($opt_t)
   {
      $transcript = "$name.$thistype{'log'}";
   }
   else
   {
      $transcript = $opt_t;
   }

# v2.01 remove old transcript file

   unless ($opt_n)
   {
      unlink($transcript);
   }

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

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

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

      my $outfile;

# v2.01 changed 'if ($opt_o eq "")' with 'unless ($opt_o)'
      unless ($opt_o)
      {
         $outfile = "$name.$thistype{out}";
      }
      else
      {
         $outfile = $opt_o;
      }

      # v1.7 print warnings to STDOUT instead of STDERR

      # v1.6 added file existence test
      unless (-e $inputfile)
      {
         # v2.09 suppress warning if -q switch in use
         print "Warning: File '$inputfile' doesn't exist.\n",
             "*** Skipping glossary '$type'. ***\n" unless ($opt_q);
         next;
      }

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

      my $transcript;

# v2.01 changed 'if ($opt_t eq "")' with 'unless ($opt)'
      unless ($opt_t)
      {
         $transcript = "$name.$thistype{'log'}";
      }
      else
      {
        $transcript = $opt_t;
      }

      # v1.6 added file empty test
      if (-z $inputfile)
      {
         my $message =
             "Warning: File '$inputfile' is empty.\n".
             "Have you used any entries defined in glossary '$type'?\n";

         if ($type eq 'main')
         {
            $message .=
               "Remember to use package option 'nomain' if you\n".
               "don't want to use the main glossary.\n";
         }

         # v2.09 suppress warning if -q switch in use
         warn $message unless $opt_q;

         # Write warning to transcript file.

         if (open TRANSFD, ">$transcript")
         {
            print TRANSFD $message;
            close TRANSFD;
         }

         # create an empty output file and move on to the next glossary

         if (open OFD, ">$outfile")
         {
            print OFD "\\null\n";
            close OFD;
         }
         else
         {
            warn "Unable to create '$outfile' $!\n";
         }

         next;
      }

# v2.01 remove old transcript file

      unless ($opt_n)
      {
         unlink($transcript);
      }

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

sub scan_aux{
  my $name = shift;

  # v2.0 added
  local(*AUXFILE);

  if (open AUXFILE, "$name.aux")
  {
     while (<AUXFILE>)
     {
        #v2.14 added
        if (m/\\\@gls\@reference/)
        {
           die "Your document has used \\makenoidxglossaries\n",
               "You don't need makeindex or xindy.\n";
        }

        # v1.9 added
        if (m/\\\@input{(.+)\.aux}/)
        {
           &scan_aux($1);

           # v2.04 added
           # (Fix provided by Daniel Grund) 
           next;
        }

        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
           # v2.12 added check for xdy (bug ID 33)
           $istfile=~s/^"(.*)"\.(ist|xdy)$/$1.$2/;

           # v2.12 if on Windows, substitute any '*' with ' '
           # to compensate for MiKTeX bug
           # http://sourceforge.net/p/miktex/bugs/2301/

           if ($^O=~/Win/)
           {
              $istfile=~tr/\*/ /;
           }
        }

        # 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
              {
                 warn
                   "Unknown ordering '$ordering'\n",
                   "Assuming word ordering\n" unless ($opt_q);
                 $letterordering = 0;
              }
           }
        }
     }

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

# v2.01 new subroutine run_app added
sub run_app{
   my($appname, $appargs, $trans, $quiet, $dontexec) = @_;

   local(*STATUS);

   my $status   = '';
   my $warnings = '';
   my $errno = 0;
   my $log = '';

   print "$appname $appargs\n" if ($dontexec or not $quiet);

   return if ($dontexec);

   # v2.11 added double-quotes around $appname
   # v2.14 added -k switch
   if (not $opt_k and open (STATUS, "\"$appname\" $appargs 2>&1 |"))
   {
      while (<STATUS>)
      {
         print unless ($quiet);

         $warnings .= $_ if (/WARNING:/);

         $status .= $_ unless (/^[\w]+ing/ or /^Finished/ or /^Usage:/);

         $log .= $_;
      }

      close STATUS;
   }
   else
   {
      unless ($opt_k)
      {
         $warnings = "Unable to fork \"$appname\" with redirection $!\n";

         # v2.14 added check for quiet switches
         warn $warnings,  
           "Retrying without redirection.\n" unless ($quiet or $opt_Q);
      }

      $status = `\"$appname\" $appargs`;

      $log = $status;

      print $status unless ($quiet);
   }

   if ($?)
   {
      # v2.14 Fixed error number assignment
      $errno = ($? >> 8);

      # v2.14 Added check for existence of transcript
      unless (-e $trans)
      {
         $log .= "\n$appname didn't create a transcript.\n";

         if (not $appname=~/\//)
         {
            # No path specified
            $log .= "Check that $appname has been installed and\nis on your system's PATH.\n";

            if (defined $ENV{PATH})
            {
               $log .= "Your PATH environment variable is set to: \n"
                    . $ENV{PATH} . "\n";
            }
            else
            {
               $log .= "I'm sorry, I can't detect your PATH environment variable\n";
            }

            $log .= "Try manually running:\n\"$appname\" $appargs\n";
         }
         elsif (not -e $appname)
         {
            if ($^O=~/Win/)
            {
               if (-e "$appname.exe" or -e "$appname.bat")
               {
                  $log .= "Try manually running:\n\"$appname\" $appargs\n";
               }
               else
               {
                  $log .= "File '$appname' doesn't exist.\n";
               }
            }
            else
            {
               $log .= "File '$appname' doesn't exist.\n";
            }
         }
         else
         {
            $log .= "Try manually running:\n\"$appname\" $appargs\n";
         }
      }

      if (open LOGFILE, ">>$trans")
      {
         print LOGFILE "\n\n*** Unable to execute: '\"$appname\" $appargs' ***\n\n";
         print LOGFILE "Failed with error code $errno.\n\n";

         if ($log)
         {
            print LOGFILE "Status report:\n\n";
            print LOGFILE "$log", "\n";
         }

         print LOGFILE "Warnings:\n\n",$warnings, "\n" if $warnings;
         close LOGFILE;
      }
      else
      {
         warn "Unable to open '$trans' $!\n";
      }
   }

   if ($log=~/\(0 entries accepted,/)
   {
      # Attempt to diagnose what's gone wrong

      if (open TRANS, $trans)
      {
         while (<TRANS>)
         {
            if (/Index style file .* not found/)
            {
               $errno = $istnotfound;
               last;
            }

            if (/Unknown specifier ;/)
            {
               $errno = $xdynotist;

               last;
            }
         }

         close TRANS;
      }

   }

   return ($status, $warnings, $errno);
}

# v2.08 added $app parameter
sub makeindex{
   my($in,$out,$trans,$ist,$rest,$quiet,$dontexec,$app) = @_;

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

# v2.01 replaced code with call to &run_app

   my ($status, $warnings, $errno) 
     = &run_app($app, $args, $trans, $quiet, $dontexec);

   return if ($dontexec);

   if ($errno)
   {
      my $diagnostic = '';

      if ($makeindex_error_text{$errno})
      {
         $diagnostic = $makeindex_error_text{$errno};
      }
      else
      {
         $diagnostic = "Can't find diagnostic message for error number $errno";
      }

      die "\n***Call to makeindex failed***\n", 
        ($diagnostic ?
          "\nPossible cause of problem:\n\n".  $diagnostic . "\n\n":
          "\n"
        ),
        "Check '$trans' for details\n";
   }
}

# v2.08 added $app parameter
sub xindy{
   my($in,$out,$trans,$ist,$language,$codepage,$rest,$quiet,
     $dontexec, $app) = @_;
   my($args, $langparam, $main, $retry);
   my($module);

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

   if ($language)
   {
     # map babel names to xindy names

     $language = $languagemap{$language} if ($languagemap{$language});

     $langparam = "-L $language";
   }
   else
   {
     $langparam = "";
     # v2.10 initialise $language to suppress warnings
     $language = "";
   }

   # 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.)

# v2.01 changed 'if ($codepage eq "")' to 'unless ($codepage)'
   unless ($codepage)
   {
      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\"");

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

# v2.01 replaced code with call to &run_app

   my ($status, $warnings, $errno) 
     = &run_app($app, $args, $trans, $quiet, $dontexec);

   return if ($dontexec);

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

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

         warn $message;

         $retry .= $message;
      }

      ($status, $warnings, $errno) 
         = &run_app('xindy', $args, $trans, $quiet, $dontexec);

   }

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

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

         warn $message;
         $retry .= $message;
      }

      ($status, $warnings, $errno) 
         = &run_app('xindy', $args, $trans, $quiet, $dontexec);

   }

   if ($errno)
   {
      # attempt further diagnostic

      my $diagnostic = '';

      if ($status=~/index 0 should be less than the length of the string/m)
      {
         $diagnostic = $xindy_error_text{nosort};
      }
      elsif ($status=~/variable % has no value/m)
      {
         $diagnostic = $xindy_error_text{istnotxindy};
      }
      elsif ($status=~/Possible read-error due to ill-formed string " :sep/m)
      {
         $diagnostic = $xindy_error_text{missingendquote};
      }
      elsif (not $language)
      {
         # 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.

          $diagnostic = $xindy_error_text{nolanguage};

          if ($in eq 'glo')
          {
             # or it may be that the user doesn't want to use the main
             # glossary and has forgotten to suppress it with the
             # "nomain" package option

             $diagnostic .= $xindy_error_text{nomain};
          }
      }

      die "\n***Call to xindy failed***\n", 
          ($diagnostic ? 
          "\nPossible cause of problem:\n\n".  $diagnostic . "\n\n":
          "\n"),
          "Check '$trans' for details\n";
   }

   # Check xindy warnings

   if ($status=~/^WARNING:/m)
   {
      my $diagwarn = '';

      if ($status=~/did not match any location-class/m)
      {
         $diagwarn = $xindy_error_text{badlocation};
      }

      if ($status=~/unknown attribute `pageglsnumberformat'/m)
      {
         $diagwarn .= $xindy_error_text{comp207};
      }

      if ($diagwarn)
      {
         warn "\n**Warning:**\n\n", $diagwarn, "\n";

         $warnings .= "\nmakeglossaries diagnostic messages:\n\n"
                   . $diagwarn;
      }
   }

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

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

         close LOGFILE;
      }
      else
      {
         warn "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 "\nGeneral Options:\n\n";
   print "-o <gls>\tUse <gls> as the output file.\n";
   print "\t\t(Don't use -o if you have more than one glossary.)\n";
   print "-q\t\tQuiet mode.\n";
   print "-Q\t\tSilence unable to fork warning.\n";
   print "-k\t\tDon't try to use piped redirection.\n";
   print "-s <sty>\tEmploy <sty> as the style file.\n";
   print "-t <log>\tEmploy <log> as the transcript file.\n";
   print "\t\t(Don't use -t if you have more than one glossary\n";
   print "\t\tor the transcripts will be overwritten.)\n";
   print "-d <directory>\tDirectory in which the .aux, .glo etc files are located.\n",
     "\t\t(Default is the directory in which <filename> resides.)\n";
   print "-n\t\tPrint the command that would normally be executed,\n",
         "\t\tbut don't execute it.\n";

   print "\nXindy Options:\n\n";
   print "-L <language>\tUse <language>.\n";
   print "-x <file>\tFull path to xindy executable.\n",
    "\t\t(Default assumes xindy is on the operating system's path.)\n";

   print "\nMakeindex Options:\n";
   print "(See makeindex documentation for further details on these ";
   print "options.)\n\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 "-m <file>\tFull path to makeindex executable.\n",
    "\t\t(Default assumes makeindex is on the operating system's path.)\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;

=head1 NAME

makeglossaries  - Calls makeindex/xindy for LaTeX documents using glossaries package

=head1 SYNOPSIS

B<makeglossaries> [B<-o> I<file>] [B<-q>] [B<-Q>] [B<-s> I<file>]
[B<-t> I<file>] [B<-L> I<language>] [B<-c>] [B<-g>] [B<-l>]
[B<-p> I<num>] [B<-r>] [B<-d> I<aux dir>] [B<-m> I<file>] [B<-x> I<file>]
[B<--version>] [B<--help>] I<filename>

=head1 DESCRIPTION

B<makeglossaries> is designed for use with LaTeX documents that
use the glossaries package. The mandatory argument I<filename> should
be the name of the LaTeX document without the .tex extension. 
B<makeglossaries> will read the auxiliary file to determine whether
B<makeindex> or B<xindy> should be called. All the information
required to be passed to the relevant indexing application should
also be contained in the auxiliary file, but may be overridden by
the option arguments to B<makeglossaries>.

=head1 OPTIONS

=over 4

=item B<-q>

Quiet mode. Reduces chatter to standard output.

=item B<-Q>

Silences warning about being unable to fork.

=item B<-k>

Don't try forking with piped redirection. (Lessens the
effect of -q)

=item B<-o> I<file>

Use I<file> as the output file. (Only suitable for documents 
containing a single glossary, otherwise each glossary will be
overridden.)

=item B<-s> I<file>

Use I<file> as the style file. Note that if you use this option,
you need to know whether B<makeindex> or B<xindy> will be called, as
they have different style files.

=item B<-t> I<file>

Use I<file> as the transcript file.

=item B<-L> I<language>

This option only has an effect if B<xindy> is called. Sets the
language. See B<xindy> documentation for further details.

=item B<-c>

Compress intermediate blanks (B<makeindex> only).

=item B<-g>

Employ German word ordering (B<makeindex> only).

=item B<-l>

Letter ordering (B<makeindex> only).

=item B<-p> I<num>

Sets the starting page number to be I<num> (B<makeindex> only).

=item B<-r>

Disable implicit page range formation (B<makeindex> only).

=item B<-d> I<aux dir>

Specify the directory the .aux, .glo etc files are located.
Defaults to the parent directory of the base file I<filename>.

=item B<-m> I<file>

Specify the full path name for B<makeindex> to I<file> in the 
event that B<makeindex> isn't on the operating system's path.

=item B<-x> I<file>

Specify the full path name for B<xindy> to I<file> in the 
event that B<xindy> isn't on the operating system's path.

=item B<--version>

Prints version number and exits.

=item B<--help>

Prints help message and exits.

=back

=head1 REQUIRES

Perl, Getopt::Std, and makeindex or xindy (depending on glossaries
package options).

=head1 LICENSE

This is free software distributed under the LaTeX Project Public 
License. There is NO WARRANTY.
See L<http://www.latex-project.org/lppl.txt> for details.

=head1 AUTHOR

Nicola L. C. Talbot,
L<http://www.dickimaw-books.com/>

=head1 RECOMMENDED READING

The glossaries manual:

	texdoc glossaries

=cut