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
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
|
# $Id$
# The inevitable utilities for TeX Live.
#
# Copyright 2007, 2008 Norbert Preining, Reinhard Kotucha
# This file is licensed under the GNU General Public License version 2
# or any later version.
package TeXLive::TLUtils;
=pod
=head1 NAME
C<TeXLive::TLUtils> -- utilities used in the TeX Live infrastructure
=head1 SYNOPSIS
use TeXLive::TLUtils;
=head2 Platform Detection
TeXLive::TLUtils::platform;
TeXLive::TLUtils::platform_desc($platform);
TeXLive::TLUtils::win32;
TeXLive::TLUtils::unix;
=head2 System Tools
TeXLive::TLUtils::getenv($string);
TeXLive::TLUtils::which($string);
TeXLive::TLUtils::program_exists($program);
TeXLive::TLUtils::get_system_tmpdir;
=head2 File Utilities
TeXLive::TLUtils::dirname($path);
TeXLive::TLUtils::basename($path);
TeXLive::TLUtils::dirname_and_basename($path);
TeXLive::TLUtils::dir_writable($path);
TeXLive::TLUtils::mkdirhier($path);
TeXLive::TLUtils::rmtree($root, $verbose, $safe);
TeXLive::TLUtils::copy($file, $target_dir);
TeXLive::TLUtils::download_file($path, $destination [, $progs ]);
TeXLive::TLUtils::setup_programs($bindir, $platform);
=head2 Installer Functions
TeXLive::TLUtils::media;
TeXLive::TLUtils::initialize_installer;
TeXLive::TLUtils::make_var_skeleton($path);
TeXLive::TLUtils::make_local_skeleton($path);
TeXLive::TLUtils::create_fmtutil($tlpdb,$dest,$localconf);
TeXLive::TLUtils::create_updmap($tlpdb,$dest,$localconf);
TeXLive::TLUtils::create_language_dat($tlpdb,$dest,$localconf);
TeXLive::TLUtils::create_language_def($tlpdb,$dest,$localconf);
TeXLive::TLUtils::install_packages($from_tlpdb,$to_tlpdb,$what,$opt_src, $opt_doc)>);
TeXLive::TLUtils::install_package($what, $filelistref, $target, $platform);
=head2 Miscellaneous
TeXLive::TLUtils::sort_uniq(@list);
TeXLive::TLUtils::push_uniq(\@list, @items);
TeXLive::TLUtils::member($item, @list);
TeXLive::TLUtils::merge_into(\%to, \%from);
TeXLive::TLUtils::texdir_check($texdir);
TeXLive::TLUtils::kpsewhich;
TeXLive::TLUtils::conv_to_win_path($path);
TeXLive::TLUtils::give_ctan_mirror($path);
TeXLive::TLUtils::tlmd5($path);
=head1 DESCRIPTION
=cut
BEGIN {
use Exporter ();
use vars qw( @ISA @EXPORT_OK @EXPORT);
@ISA = qw(Exporter);
@EXPORT_OK = qw(
&platform
&platform_desc
&win32
&unix
&getenv
&which
&program_exists
&get_system_tmpdir
&dirname
&basename
&dirname_and_basename
&dir_writable
&mkdirhier
&rmtree
©
&install_package
&install_packages
&media
&initialize_installer
&make_var_skeleton
&make_local_skeleton
&create_fmtutil
&create_updmap
&create_language_dat
&create_language_def
&sort_uniq
&push_uniq
&texdir_check
&member
&kpsewhich
"ewords
&conv_to_win_path
&setup_programs
&download_file
&merge_into
&process_logging_options
&welcome
&welcome_paths
&give_ctan_mirror
&tlmd5
);
@EXPORT = qw(setup_programs download_file info log debug ddebug tlwarn process_logging_options win32);
}
use TeXLive::TLConfig;
use Getopt::Long;
use Digest::MD5;
$::opt_verbosity = 0;
=pod
=head2 Platform Detection
=over 6
=item C<platform>
If C<$^O=~/MSWin(32|64)$/i> is true we know that we're on
Windows and we set the global variable C<$::_platform_> to C<win32>.
Otherwise we call C<config.guess>. The output of C<config.guess>
is filtered as described below.
CPU type is determined by a regexp. And it's necessary to
C<s/i.86/i386/>.
For OS we need a list because we probably have something like
C<linux-gnu> but we need C<linux>. This list might/should contain OSs
which are not currently supported. The list currently supports all
platforms supported by TeX Live 2007 plus Cygwin.
If a particular platform is not found in this list we use the regexp
C</.*-(.*$)/> as a last resort and hope it provides something useful.
The result is stored in a global variable C<$::_platform_>. If you call
C<platform> repeatedly, only the first call of C<platform> will access
the HD/CD/DVD.
=cut
sub platform {
unless (defined $::_platform_) {
if ($^O=~/^MSWin(32|64)$/i) {
$::_platform_="win32";
} else {
my $config_guess="$::installerdir/tlpkg/installer/config.guess";
my @OSs=qw(aix cygwin darwin freebsd hpux irix linux netbsd
openbsd solaris);
my $CPU; # CPU type as reported by config.guess.
my $OS; # O/S type as reported by config.guess.
# We cannot rely on #! in config.guess but have to call /bin/sh
# explicitly because sometimes the 'noexec' flag is set in
# /etc/fstab for ISO9660 file systems.
my $guessed_platform=`/bin/sh $config_guess`;
chomp $guessed_platform;
$guessed_platform=~s/^x86_64-(.*)-freebsd$/amd64-$1-freebsd/;
($CPU=$guessed_platform)=~s/(.*?)-.*/$1/;
foreach my $os (@OSs) {
$OS=$os if $guessed_platform=~/$os/;
}
if ($OS eq "darwin") {
$CPU = "universal"; # TL provides universal binaries
} elsif ($CPU=~/^i.86$/) {
$CPU=~s/i.86/i386/;
}
unless (defined $OS) {
($OS=$guessed_platform)=~s/.*-(.*)/$1/;
}
$::_platform_="$CPU-$OS";
}
}
return $::_platform_;
}
=pod
=item C<platform_desc($platform)>
Return a string which describes a particular platform identifier, e.g.,
given C<i386-linux> we return C<Intel x86 with GNU/Linux>.
=cut
sub platform_desc {
my ($platform) = @_;
my %platform_name=(
'alpha-linux' => 'DEC Alpha with GNU/Linux',
'alphaev5-osf' => 'DEC Alphaev5 OSF',
'amd64-freebsd' => 'x86_64 with FreeBSD',
'hppa-hpux' => 'HP-UX',
'i386-cygwin' => 'Intel x86 with Cygwin',
'i386-darwin' => 'Intel x86 with MacOSX/Darwin',
'i386-freebsd' => 'Intel x86 with FreeBSD',
'i386-openbsd' => 'Intel x86 with OpenBSD',
'i386-netbsd' => 'Intel x86 with NetBSD',
'i386-linux' => 'Intel x86 with GNU/Linux',
'i386-solaris' => 'Intel x86 with Sun Solaris',
'mips-irix' => 'SGI IRIX',
'powerpc-aix' => 'PowerPC with AIX',
'powerpc-darwin' => 'PowerPC with MacOSX/Darwin',
'powerpc-linux' => 'PowerPC with GNU/Linux',
'sparc-linux' => 'Sparc with GNU/Linux',
'sparc-solaris' => 'Sparc with Solaris',
'universal-darwin' => 'universal binaries for MacOSX/Darwin',
'win32' => 'Windows',
'x86_64-linux' => 'x86_64 with GNU/Linux'
);
# the inconsistency between amd64-freebsd and x86_64-linux is
# unfortunate (it's the same hardware), but the os people say those
# are the conventional names on the respective os's, so ...
if (exists $platform_name{$platform}) {
return "$platform_name{$platform}";
} else {
my ($CPU,$OS) = split ('-', $platform);
return "$CPU with " . ucfirst "$OS";
}
}
=pod
=item C<win32>
Return C<1> if platform is Windows and C<0> otherwise. The test is
currently based on the value of Perl's C<$^O> variable.
=cut
sub win32
{
if ($^O=~/^MSWin(32|64)$/i) {
return 1;
} else {
return 0;
}
# the following needs config.guess, which is quite bad ...
# return (&platform eq "win32")? 1:0;
}
=pod
=item C<unix>
Return C<1> if platform is UNIX and C<0> otherwise.
=cut
sub unix {
return (&platform eq "win32")? 0:1;
}
=pod
=back
=head2 System Tools
=over 6
=item C<getenv($string)>
Get an environment variable. It is assumed that the environment
variable contains a path. On Windows all backslashes are replaced by
forward slashes as required by Perl. If this behavior is not desired,
use C<$ENV{"$variable"}> instead. C<0> is returned if the
environment variable is not set.
=cut
sub getenv {
my $envvar=shift;
my $var=$ENV{"$envvar"};
return 0 unless (defined $var);
if (&win32) {
$var=~s!\\!/!g; # change \ -> / (required by Perl)
}
return "$var";
}
=pod
=item C<which($string)>
C<which> does the same as the UNIX command C<which(1)>, but it is
supposed to work on Windows too. On Windows we have to try all the
extensions given in the C<PATHEXT> environment variable. We also try
without appending an extension because if C<$string> comes from an
environment variable, an extension might aleady be present.
=cut
sub which {
my $prog=shift;
my @PATH;
my $PATH=getenv('PATH');
if (&win32) {
my @PATHEXT=split ';', getenv('PATHEXT');
push @PATHEXT, ''; # if argument contains an extension
@PATH=split ';', $PATH;
for my $dir (@PATH) {
for my $ext (@PATHEXT) {
if (-f "$dir/$prog$ext") {
return "$dir/$prog$ext";
}
}
}
} else {
@PATH=split ':', $PATH;
for my $dir (@PATH) {
if (-x "$dir/$prog") {
return "$dir/$prog";
}
}
}
return 0;
}
=pod
=item C<program_exists($program)>
Return C<1> if C<$program> is in C<PATH> and C<0> otherwise.
=cut
sub program_exists {
my $program=shift;
return (&which ("$program"))? 0:1;
}
=pod
=item C<get_system_tmpdir>
Evaluate the environment variables C<TMPDIR>, C<TMP>, and C<TEMP> in
order to find the system temporary directory.
=cut
sub get_system_tmpdir {
my $systmp=0;
$systmp||=getenv 'TMPDIR';
$systmp||=getenv 'TMP';
$systmp||=getenv 'TEMP';
$systmp||='/tmp';
return "$systmp";
}
=pod
=back
=head2 File Utulities
=over 6
=item C<dirname($path)>
Return C<$path> with its trailing C</component> removed.
=cut
sub dirname {
my $path=shift;
if (win32) {
$path=~s!\\!/!g;
}
if ($path=~m!/!) { # dirname("foo/bar/baz") -> "foo/bar"
$path=~m!(.*)/.*!;
return $1;
} else { # dirname("ignore") -> "."
return ".";
}
}
=item C<basename($path)>
Return C<$path> with any leading directory components removed.
=cut
sub basename {
my $path=shift;
if (win32) {
$path=~s!\\!/!g;
}
if ($path=~m!/!) { # basename("foo/bar") -> "bar"
$path=~m!.*/(.*)!;
return $1;
} else { # basename("ignore") -> "ignore"
return $path;
}
}
=item C<dirname_and_basename($path)>
Return both C<dirname> and C<basename>. Example:
($dirpart,$filepart) = dirname_and_basename ($path);
=cut
sub dirname_and_basename {
my $path=shift;
if (win32) {
$path=~s!\\!/!g;
}
$path=~/(.*)\/(.*)/;
return ("$1", "$2");
}
=pod
=item C<dir_writable($path)>
Tests whether its argument is writable by trying to write to
it. This function is necessary because the built-in C<-w> test just
looks at mode and uid/guid, which on Windows always returns true and
even on Unix is not always good enough for directories mounted from
a fileserver.
=cut
# Theoretically, the test below, which uses numbers as names, might
# lead to a race condition. OTOH, it should work even on a very
# broken Perl.
# The Unix test gives the wrong answer when used under Windows Vista
# with one of the `virtualized' directories such as Program Files:
# lacking administrative permissions, it would write successfully to
# the virtualized Program Files rather than fail to write to the
# real Program Files. Ugh.
sub dir_writable {
$path=shift;
return 0 unless -d $path;
$path =~ s!\\!/!g if win32;
$path =~ s!/$!!g;
my $i = 0;
while (-e $path . "/" . $i) { $i++; }
my $f = $path."/".$i;
if (win32) {
my $fb = $f;
$fb =~ s!/!\\!g;
return 0 if
system('copy /b ' . $ENV{'COMSPEC'} . ' "' . $fb . '" >nul 2>&1');
unlink $f if -e $f;
return 1;
} else {
return 0 unless open TEST, ">".$f;
my $written = 0;
$written = (print TEST "\n");
close TEST;
unlink $f;
return $written;
}
}
=pod
=item C<mkdirhier($path, [$mode])>
The function C<mkdirhier> does the same as the UNIX command C<mkdir -p>.
The optional parameter sets the permission flags.
=cut
sub mkdirhier {
my $tree=shift;
my $mode=shift;
my $subdir;
return if (-d "$tree");
@dirs=split /\//, $tree;
foreach $dir (@dirs) {
$subdir .= ("$dir" . "/");
unless (-d $subdir) {
if (defined $mode) {
mkdir ("$subdir", $mode) or die "Can't mkdir '$subdir': $!.\n";
} else {
mkdir "$subdir" or die "Can't mkdir '$subdir': $!.\n";
}
}
}
}
=pod
=item C<rmtree($root, $verbose, $safe)>
The C<rmtree> function provides a convenient way to delete a
subtree from the directory structure, much like the Unix command C<rm -r>.
C<rmtree> takes three arguments:
=over 4
=item *
the root of the subtree to delete, or a reference to
a list of roots. All of the files and directories
below each root, as well as the roots themselves,
will be deleted.
=item *
a boolean value, which if TRUE will cause C<rmtree> to
print a message each time it examines a file, giving the
name of the file, and indicating whether it's using C<rmdir>
or C<unlink> to remove it, or that it's skipping it.
(defaults to FALSE)
=item *
a boolean value, which if TRUE will cause C<rmtree> to
skip any files to which you do not have delete access
(if running under VMS) or write access (if running
under another OS). This will change in the future when
a criterion for 'delete permission' under OSs other
than VMS is settled. (defaults to FALSE)
=back
It returns the number of files successfully deleted. Symlinks are
simply deleted and not followed.
B<NOTE:> There are race conditions internal to the implementation of
C<rmtree> making it unsafe to use on directory trees which may be
altered or moved while C<rmtree> is running, and in particular on any
directory trees with any path components or subdirectories potentially
writable by untrusted users.
Additionally, if the third parameter is not TRUE and C<rmtree> is
interrupted, it may leave files and directories with permissions altered
to allow deletion (and older versions of this module would even set
files and directories to world-read/writable!)
Note also that the occurrence of errors in C<rmtree> can be determined I<only>
by trapping diagnostic messages using C<$SIG{__WARN__}>; it is not apparent
from the return value.
=cut
#taken from File/Path.pm
#
my $Is_VMS = $^O eq 'VMS';
my $Is_MacOS = $^O eq 'MacOS';
# These OSes complain if you want to remove a file that you have no
# write permission to:
my $force_writeable = ($^O eq 'os2' || $^O eq 'dos' || $^O eq 'MSWin32' ||
$^O eq 'amigaos' || $^O eq 'MacOS' || $^O eq 'epoc');
sub rmtree {
my($roots, $verbose, $safe) = @_;
my(@files);
my($count) = 0;
$verbose ||= 0;
$safe ||= 0;
if ( defined($roots) && length($roots) ) {
$roots = [$roots] unless ref $roots;
} else {
warn "No root path(s) specified";
return 0;
}
my($root);
foreach $root (@{$roots}) {
if ($Is_MacOS) {
$root = ":$root" if $root !~ /:/;
$root =~ s#([^:])\z#$1:#;
} else {
$root =~ s#/\z##;
}
(undef, undef, my $rp) = lstat $root or next;
$rp &= 07777; # don't forget setuid, setgid, sticky bits
if ( -d _ ) {
# notabene: 0700 is for making readable in the first place,
# it's also intended to change it to writable in case we have
# to recurse in which case we are better than rm -rf for
# subtrees with strange permissions
chmod($rp | 0700, ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
or warn "Can't make directory $root read+writeable: $!"
unless $safe;
if (opendir my $d, $root) {
no strict 'refs';
if (!defined ${"\cTAINT"} or ${"\cTAINT"}) {
# Blindly untaint dir names
@files = map { /^(.*)$/s ; $1 } readdir $d;
} else {
@files = readdir $d;
}
closedir $d;
} else {
warn "Can't read $root: $!";
@files = ();
}
# Deleting large numbers of files from VMS Files-11 filesystems
# is faster if done in reverse ASCIIbetical order
@files = reverse @files if $Is_VMS;
($root = VMS::Filespec::unixify($root)) =~ s#\.dir\z## if $Is_VMS;
if ($Is_MacOS) {
@files = map("$root$_", @files);
} else {
@files = map("$root/$_", grep $_!~/^\.{1,2}\z/s,@files);
}
$count += rmtree(\@files,$verbose,$safe);
if ($safe &&
($Is_VMS ? !&VMS::Filespec::candelete($root) : !-w $root)) {
print "skipped $root\n" if $verbose;
next;
}
chmod $rp | 0700, $root
or warn "Can't make directory $root writeable: $!"
if $force_writeable;
print "rmdir $root\n" if $verbose;
if (rmdir $root) {
++$count;
} else {
warn "Can't remove directory $root: $!";
chmod($rp, ($Is_VMS ? VMS::Filespec::fileify($root) : $root))
or warn("and can't restore permissions to "
. sprintf("0%o",$rp) . "\n");
}
} else {
if ($safe &&
($Is_VMS ? !&VMS::Filespec::candelete($root)
: !(-l $root || -w $root)))
{
print "skipped $root\n" if $verbose;
next;
}
chmod $rp | 0600, $root
or warn "Can't make file $root writeable: $!"
if $force_writeable;
print "unlink $root\n" if $verbose;
# delete all versions under VMS
for (;;) {
unless (unlink $root) {
warn "Can't unlink file $root: $!";
if ($force_writeable) {
chmod $rp, $root
or warn("and can't restore permissions to "
. sprintf("0%o",$rp) . "\n");
}
last;
}
++$count;
last unless $Is_VMS && lstat $root;
}
}
}
$count;
}
=pod
=item C<copy($file, $target_dir)>
Copy file C<$file> to directory C<$target_dir>. No external programs
are involved. Since we need C<sysopen()>, the Perl module C<Fcntl.pm>
is required. The time stamps are preserved and symlinks are created
on UNIX systems. On Windows, C<(-l $file)> will certainly never
return 'C<true>' and symlinks will be copied as regular files.
C<copy> invokes C<mkdirhier> if target directories do not exist.
Files have mode C<0777>-I<umask> if they are executable and
C<0666>-I<umask> otherwise.
Note that C<copy> will work with file:/ prefixes, too.
=cut
sub copy {
my $infile=shift;
my $destdir=shift;
my $outfile;
my @stat;
my $mode;
my $buffer;
my $offset;
my $filename;
my $dirmode=0755;
my $blocksize=2048;
mkdirhier ("$destdir") unless -d "$destdir";
$infile =~ s!^file://*!/!i;
$filename=basename "$infile";
$outfile="$destdir/$filename";
if (-l "$infile") {
symlink readlink "$infile", "$destdir/$filename";
} else {
open IN, "$infile" or die "Can't open '$infile': $!\n";
binmode IN;
$mode=(-x "$infile")? oct("0777"):oct("0666");
$mode-=umask;
open OUT, ">$outfile" or die "Can't open '$outfile': $!\n";
binmode OUT;
chmod $mode, "$outfile";
while ($read=sysread IN, $buffer, $blocksize) {
die "system read error: $!\n" unless defined $read;
$offset=0;
while ($read) {
$written=syswrite OUT, $buffer, $read, $offset;
die "system write error: $!\n" unless defined $written;
$read-=$written;
$offset+=$written;
}
}
close OUT;
close IN;
@stat=lstat "$infile";
utime $stat[8], $stat[9], "$outfile";
}
}
=pod
=item C<install_packages($from_tlpdb, $to_tlpdb, $what, $opt_src, $opt_doc)>
Installs the list of packages found in C<@$what> (a ref to a list) into
the TLPDB given by C<$to_tlpdb>. Information on files are taken from
the TLPDB C<$from_tlpdb>.
C<$opt_src> and C<$opt_doc> specify whether srcfiles and docfiles should be
installed (currently implemented only for installation from DVD).
=cut
sub install_packages {
my ($fromtlpdb,$totlpdb,$what,$opt_src,$opt_doc) = @_;
my $container_src_split = $fromtlpdb->config_src_container;
my $container_doc_split = $fromtlpdb->config_doc_container;
foreach my $package (@$what) {
info("Installing: $package\n");
foreach my $h (@::install_packages_hook) {
&$h("Installing: $package");
}
my $tlpobj=$fromtlpdb->get_package($package);
if (!defined($tlpobj)) {
die "STRANGE: $package not to be found in ", $fromtlpdb->root;
}
my $real_opt_doc = $opt_doc;
# if we install a package from the Documentation class we
# reactivate the do_doc in any case. It should apply ONLY for fonts
# and macros!
if ($tlpobj->category =~ m/documentation/i) {
$real_opt_doc = 1;
}
my $container;
my @installfiles;
push @installfiles, $tlpobj->runfiles;
push @installfiles, $tlpobj->allbinfiles;
push @installfiles, $tlpobj->srcfiles if ($opt_src);
push @installfiles, $tlpobj->docfiles if ($real_opt_doc);
if (&media eq 'DVD') {
$container = \@installfiles;
} elsif (&media eq 'CD') {
if (-r "$::installerdir/$DiskArchive/$package.zip") {
$container = "$::installerdir/$DiskArchive/$package.zip";
} elsif (-r "$::installerdir/$DiskArchive/$package.tar.lzma") {
$container = "$::installerdir/$DiskArchive/$package.tar.lzma";
} else {
# for now only warn and return, should (?) be die!?
tlwarn("Cannot find a package $package (.zip or .lzma) in $::installerdir/$DiskArchive\n");
next;
}
} elsif (&media eq 'NET') {
$container = "$TeXLiveURL/$NetArchive/$package.$DefaultContainerExtension";
}
install_package($container, $tlpobj->containersize, $tlpobj->containermd5, \@installfiles,$totlpdb->root,$vars{'this_platform'});
# if we are installing from CD or NET we have to fetch the respective
# source and doc packages $pkg.source and $pkg.doc and install them, too
if ((&media eq 'NET') || (&media eq 'CD')) {
# we install split containers under the following conditions:
# - the container were split generated
# - src/doc files should be installed
# - the package is not already a split one (like .i386-linux)
# - there are actually src/doc files present
if ($container_src_split && $opt_src &&
($package !~ m/\./) && $tlpobj->srcfiles) {
my $srccontainer = $container;
$srccontainer =~ s/(\.tar\.lzma|\.zip)$/.source$1/;
install_package($srccontainer, $tlpobj->srccontainersize, $tlpobj->srccontainermd5, \@installfiles,$totlpdb->root,$vars{'this_platform'});
}
if ($container_doc_split && $real_opt_doc &&
($package !~ m/\./) && $tlpobj->docfiles) {
my $doccontainer = $container;
$doccontainer =~ s/(\.tar\.lzma|\.zip)$/.doc$1/;
install_package($doccontainer, $tlpobj->doccontainersize, $tlpobj->doccontainermd5, \@installfiles,$totlpdb->root,$vars{'this_platform'});
}
}
# we don't want to have wrong information in the tlpdb, so remove the
# src/doc files if they are not installed ...
if (!$opt_src) {
$tlpobj->clear_srcfiles;
}
if (!$real_opt_doc) {
$tlpobj->clear_docfiles;
}
$totlpdb->add_tlpobj($tlpobj);
# we have to write out the tlpobj file since it is contained in the
# archives (.tar.lzma) but at DVD install time we don't have them
my $tlpod = $totlpdb->root . "/tlpkg/tlpobj";
mkdirhier( $tlpod );
open(TMP,">$tlpod/".$tlpobj->name.".tlpobj") or
die("Cannot open tlpobj file for ".$tlpobj->name);
$tlpobj->writeout(\*TMP);
close(TMP);
}
$totlpdb->save;
}
=pod
=item C<install_package($what, $size, $filelistref, $target, $platform)>
This function installs the files given in @$filelistref from C<$what>
into C<$target>.
C<$size> gives the size in bytes of the container, or -1 if we are
installing from DVD, i.e., from a list of files to be copied.
If C<$what> is a reference to a list of files then these files are
assumed to be readable and are copied to C<$target>, creating dirs on
the way. In this case the list C<@$filelistref> is not taken into
account.
If C<$what> starts with C<http://> or C<ftp://> then C<$what> is
downloaded from the net and piped through c<lzmadec> and C<tar>.
If $what ends with C<.tar.lzma> (but does not start with C<http://> or
C<ftp://>, but possibly with C<file:/>) it is assumed to be a readable
file on the system and is
piped through C<lzmadec> and C<tar> in the very same way.
In both of the last cases currently the list C<$@filelistref> currently
is not taken into account (should be fixed!).
=cut
sub install_package {
my ($what, $whatsize, $whatmd5, $filelistref, $target, $platform) = @_;
my @filelist = @$filelistref;
# we assume that $::progs has been set up!
my $wget = $::progs{'wget'};
my $lzmadec = $::progs{'lzmadec'};
my $tar = $::progs{'tar'};
if (!defined($wget) || !defined($lzmadec) || !defined($tar)) {
die "Programs have not been set up properly, strange!";
}
if (ref $what) {
# we are getting a ref to a list of files, so install from DVD
foreach my $file (@$what) {
# @what is taken, not @filelist!
# is this still needed?
my $dn=dirname($file);
mkdirhier("$target/$dn");
copy "$file", "$target/$dn";
}
} elsif ($what =~ m,\.tar.lzma$,) {
# this is the case when we install from CD or the NET
#
# in all other cases we create temp files .tar.lzma (or use the present
# one), lzmadec them, and then call tar
my $fn = basename($what);
mkdirhier("$target/temp");
my $lzmafile = "$target/temp/$fn";
my $tarfile = "$target/temp/$fn"; $tarfile =~ s/\.lzma$//;
my $lzmafile_quote = $lzmafile;
my $tarfile_quote = $tarfile;
my $target_quote = $target;
if (win32()) {
$lzmafile =~ s!/!\\!g;
$tarfile =~ s!/!\\!g;
$target =~ s!/!\\!g;
}
$lzmafile_quote = "\"$lzmafile\"";
$tarfile_quote = "\"$tarfile\"";
$target_quote = "\"$target\"";
my $gotfiledone = 0;
if (-r $lzmafile) {
# check that the downloaded file is not partial
if ($whatsize >= 0) {
# we have the size given, so check that first
my $size = (stat $lzmafile)[7];
if ($size == $whatsize) {
# we want to check also the md5sum if we have it present
if ($whatmd5) {
if (tlmd5($what) eq $whatmd5) {
$gotfiledone = 1;
} else {
tlwarn("Downloaded file present, size equal, but md5sum differs\nredownloading $what!\n");
}
} else {
# size ok, no md5sum
tlwarn("Downloaded container present, size equal, but no md5sum available. Hope that is fine!\n");
$gotfiledone = 1;
}
} else {
tlwarn("Partial download of $what found, removing it!\n");
unlink($tarfile, $lzmafile);
}
} else {
# ok no size information, hopefully we have md5 sums
if ($whatmd5) {
if (tlmd5($what) eq $whatmd5) {
$gotfiledone = 1;
} else {
tlwarn("Downloaded file present, but md5sum differs, removing it!\n");
}
} else {
tlwarn("container found, but we cannot verify size of md5sum!\nContinuing with it and hope that it is ok!\n");
$gotfiledone = 1;
}
}
debug("Reusing already downloaded container $lzmafile\n")
if ($gotfiledone);
}
if (!$gotfiledone) {
if ($what =~ m,http://|ftp://,) {
# we are installing from the NET
# download the file and put it into temp
if (!download_file($what, $lzmafile) || (! -r $lzmafile)) {
&main::create_profile("./installation.profile");
tlwarn("Downloading $what did not succeed!\n");
tlwarn("Please retry the install process.\n");
tlwarn("Starting the installer will try to restart the installation!\n");
tlwarn("If that doesn't work you can restart by running the installer with\n");
if (win32()) {
tlwarn(" install-tl.bat --profile texlive.profile [other args]\n");
} else {
tlwarn(" install-tl --profile texlive.profile [other args]\n");
}
die "Downloading $what did not succeed!\n";
}
} else {
# we are installing from CD
# copy it to temp
copy($what, "$target/temp");
}
}
debug("Un-lzmaing $lzmafile to $tarfile\n");
system("$lzmadec < $lzmafile_quote > $tarfile_quote");
if (! -f $tarfile) {
die "Unpacking $lzmafile did not succeed, please retry!";
}
debug("Unpacking $tarfile\n");
system($tar,"-x","-C",$target,"-f",$tarfile);
unlink($tarfile);
debug("Keeping $lzmafile for restarting ...\n");
# unlink($lzmafile);
} else {
die "Don't know how to install $what!\n";
}
}
=pod
=item C<setup_programs( $bindir, $platform )>
Populate the global $::progs hash containing the paths to the programs
C<wget>, C<tar>, C<lzmadec>. The C<$bindir> argument specifies the path
to the location of the C<lzmadec> binaries, the C<$platform> gives the
TeX Live platform name, used as the extension on the executables.
=cut
sub setup_programs {
my ($bindir, $platform) = @_;
$::progs{'wget'} = "wget";
if (!defined($platform) || ($platform eq "")) {
# we assume that we run from the DVD, so we can call platform() and
# thus also the config.guess script
# but we have to setup $::installerdir because that stupid
# platform script relies on it
$::installerdir = "$bindir/../..";
$platform = platform();
}
$::progs{'lzmadec'} = "$bindir/lzma/lzmadec.$platform";
$::progs{'lzma'} = "$bindir/lzma/lzma.$platform";
$::progs{'tar'} = "tar";
if ($^O =~ /^MSWin(32|64)$/i) {
$::progs{'wget'} = conv_to_win_path("$bindir/wget/wget.exe");
$::progs{'tar'} = conv_to_win_path("$bindir/tar.exe");
$::progs{'lzmadec'} = conv_to_win_path("$bindir/lzma/lzmadec.win32.exe");
$::progs{'lzma'} = conv_to_win_path("$bindir/lzma/lzma.exe");
} elsif (-r "$bindir/wget/wget.$platform") {
$::progs{'wget'} = "$bindir/wget/wget.$platform";
}
}
=pod
=item C<download_file( $relpath, $destination [, $progs ] )>
Try to download the file given in C<$relpath> from either C<$TeXLiveURL>
or C<TeXLiveAlternativeURL> into C<$destination>, which can be either
a filename of simply C<|>. In the latter case a file handle is returned.
The optional argument C<$progs> is a reference to a hash giving full pathes
to the respective programs, at least C<wget>. In case that C<$progs> is not
given the C<%::progs> is consulted, and if this also does not exist we
try "wget".
Downloading honors two environment variables C<TL_DOWNLOAD_PROGRAM> and
C<TL_DOWNLOAD_ARGS>. The former overrides C<wget> (or what is given in the
C<$progs> argument, the latter overrides the default wget arguments
given in C<$TeXLive::TLConfig::DefaultWgetArgs>.
C<TL_DOWNLOAD_ARGS> must be set up in a way that the place the output should
go to is the first argument after the C<TL_DOWNLOAD_ARGS>. Use with care.
=cut
sub download_file {
my ($relpath, $dest, $progs) = @_;
my $wget;
if (defined($progs) && defined($progs->{'wget'})) {
$wget = $progs->{'wget'};
} elsif (defined($::progs{'wget'})) {
$wget = $::progs{'wget'};
} else {
tlwarn ("Programs have not been set up, trying simply wget");
$wget = "wget";
}
my $url;
if ($relpath =~ m;^file://*(.*)$;) {
# $dest is a file name, we have to get the respective dirname
my $par = dirname($dest);
copy("/$1", $par);
return 1;
}
if ($relpath =~ /^(http|ftp):\/\//) {
$url = $relpath;
} else {
$url = "$TeXLiveURL/$relpath";
}
my $ret = _download_file($url, $dest, $wget);
return($ret) if $ret;
# if we have a full url given do NOT try an alternative url
if ($relpath =~ /^(http|ftp):\/\//) {
# we did get a full url, give up immediately
return undef;
}
# we are still here ... try it with Alternative URL:
return undef unless ($TeXLiveAlternativeURL);
# we are still here and TeXLiveAlternativeURL is defined
$url = "$TeXLiveAlternativeURL/$relpath";
$ret = _download_file($url, $dest, $wget);
return($ret) if $ret;
# still not found, give up
return undef;
}
sub _download_file {
my ($url, $dest, $wgetdefault) = @_;
if (win32()) {
$dest =~ s!/!\\!g;
}
my $wget = $ENV{"TL_DOWNLOAD_PROGRAM"} || $wgetdefault;
# we set now the args in installer/wgetrc
my $wgetargs = $ENV{"TL_DOWNLOAD_ARGS"} || "-q -O";
debug("Trying to download $url\n");
my $ret;
if ($dest eq "|") {
open(RETFH, "$wget $wgetargs - $url|") or
die("Cannot open $url via wget for reading");
# opening to a pipe always succeeds, so we return immediately
return \*RETFH;
} else {
my @wgetargs = split ' ', $wgetargs;
$ret = system $wget, @wgetargs, $dest, $url;
# we have to reverse the meaning of ret because systems returns
# the inverse
$ret = ($ret ? 0 : 1);
}
# return false/undef in case the download did not succeed.
return ($ret) unless $ret;
debug("Download $url did succeed\n");
if ($dest eq "|") {
return \*RETFH;
} else {
return 1;
}
}
sub install_package_old {
# either a list of files or a name of one .zip or .tar.lzma file.
my @what=@_;
my $target="$::vars{'TEXDIR'}";
my $bindir="$::installerdir/tlpkg/bin";
my $platform=&platform;
my $buffer;
my $offset;
# shouldn't it be 4096
my $blocksize=2048;
#my $lzmadec="$bindir/lzmadec_$platform";
my $lzmadec="lzmadec";
my $tar="/bin/tar -xf - -C";
my $wget="wget -nv -O-";
if (win32) {
$lzmadec="$bindir/lzma/lzma.exe -si -so";
$tar="$bindir/tar.exe -xf - -C";
$wget="$bindir/wget.exe -nv -O-";
}
if (&media eq 'DVD') {
foreach my $file (@what) {
my $dn=dirname($file);
mkdirhier("$target/$dn");
copy "$file", "$target/$dn";
}
} elsif (&media eq 'CD') {
my $package="$::installerdir/$DiskArchive/$what[0]";
open IN, "$package"
or die "Can't open '$package': $!\n";
binmode IN;
open OUT, "|$lzmadec|$tar $target";
binmode OUT;
while ($read=sysread IN, $buffer, $blocksize) {
die "system read error: $!\n" unless defined $read;
$offset=0;
while ($read) {
$written=syswrite OUT, $buffer, $read, $offset;
die "system write error: $!\n" unless defined $written;
$read-=$written;
$offset+=$written;
}
}
close OUT;
close IN;
} elsif (&media eq 'NET') {
my $package="$TeXLiveURL/$NetArchive/$what[0]";
system "$wget $package|$lzmadec|$tar $target";
} else {
die "This can't happen.";
}
}
=pod
=back
=head2 Installer Functions
=over 6
=item C<media>
Return media type. Either C<CD>, C<DVD>, or C<NET>.
The result is stored in a global variable C<$::_media_>. If you call
C<media> repeatedly, only the first call of C<media> will access the
CD/DVD.
=cut
sub media {
unless (defined $::_media_) {
if (-d "$::installerdir/$DiskArchive") {
$::_media_='CD';
} elsif (-d "$::installerdir/texmf/web2c") {
$::_media_='DVD';
} else {
$::_media_='NET';
}
}
return $::_media_;
}
=pod
=item C<initialize_installer>
The function C<initialize_installer> calls C<media> and C<platform> in
order to set the variables C<$::_media_> and C<$::_platform_>.
Note that C<$0> contains backslashes as path separators on Windows.
Repeatedly calls to C<media> or C<platform> do not access the CD/DVD
again.
=cut
sub initialize_installer {
&media;
&platform;
$ENV{'WGETRC'}="$::installerdir/tlpkg/installer/wgetrc"
}
=pod
=item C<make_var_skeleton($prefix)>
Generate a skeleton of empty directories in the C<TEXMFSYSVAR> tree.
=cut
sub make_var_skeleton {
my $prefix=shift;
mkdirhier "$prefix/tex/generic/config";
mkdirhier "$prefix/dvipdfm/config";
mkdirhier "$prefix/dvips/config";
mkdirhier "$prefix/fonts/map/dvipdfm/updmap";
mkdirhier "$prefix/fonts/map/dvips/updmap";
mkdirhier "$prefix/fonts/map/pdftex/updmap";
mkdirhier "$prefix/fonts/pk";
mkdirhier "$prefix/fonts/tfm";
mkdirhier "$prefix/web2c";
mkdirhier "$prefix/xdvi";
mkdirhier "$prefix/tex/context/config";
}
=pod
=item C<make_local_skeleton($prefix)>
Generate a skeleton of empty directories in the C<TEXMFLOCAL> tree.
=cut
sub make_local_skeleton {
my $prefix=shift;
mkdirhier "$prefix/tex/latex/local";
mkdirhier "$prefix/tex/plain/local";
mkdirhier "$prefix/dvips/local";
mkdirhier "$prefix/bibtex/bib/local";
mkdirhier "$prefix/bibtex/bst/local";
mkdirhier "$prefix/fonts/tfm/local";
mkdirhier "$prefix/fonts/vf/local";
mkdirhier "$prefix/fonts/source/local";
mkdirhier "$prefix/fonts/type1/local";
mkdirhier "$prefix/metapost/local";
mkdirhier "$prefix/web2c";
}
=pod
=item C<create_fmtutil($tlpdb, $dest, $localconf)>
=item C<create_updmap($tlpdb, $dest, $localconf)>
=item C<create_language_dat($tlpdb, $dest, $localconf)>
=item C<create_language_def($tlpdb, $dest, $localconf)>
These four functions create C<fmtutil.cnf>, C<updmap.cfg>, C<language.dat>,
and C<language.def> respectively, in C<$dest> (which by default is below
C<$TEXMFSYSVAR>). These functions merge the information present in the
TLPDB C<$tlpdb> (formats, maps, hyphenations) with local configuration
additions: C<$localconf>.
Currently the "merging" is done trivially by appending the content of
the local configuration files at the end of the file. This should be
improved (checking for duplicates).
=cut
sub create_fmtutil {
my ($tlpdb,$dest,$localconf) = @_;
my @lines = $tlpdb->fmtutil_cnf_lines;
_create_config_files ($tlpdb, "texmf/fmtutil/fmtutil-hdr.cnf", $dest, $localconf, 0, '#', \@lines);
}
sub create_updmap {
my ($tlpdb,$dest,$localconf) = @_;
my @lines = $tlpdb->updmap_cfg_lines;
_create_config_files ($tlpdb, "texmf/web2c/updmap-hdr.cfg", $dest, $localconf, 0, '#', \@lines);
}
sub create_language_dat {
my ($tlpdb,$dest,$localconf) = @_;
my @lines = $tlpdb->language_dat_lines;
_create_config_files ($tlpdb, "texmf/tex/generic/config/language.us", $dest, $localconf, 0, '%', \@lines);
}
sub create_language_def {
my ($tlpdb,$dest,$localconf) = @_;
my @lines = $tlpdb->language_def_lines;
my @postlines;
push @postlines, "%%% No changes may be made beyond this point.\n";
push @postlines, "\n";
push @postlines, "\\uselanguage {USenglish} %%% This MUST be the last line of the file.\n";
_create_config_files ($tlpdb, "texmf/tex/generic/config/language.us.def", $dest, $localconf, 1, '%', \@lines, @postlines);
}
sub _create_config_files {
my ($tlpdb, $headfile, $dest,$localconf, $keepfirstline, $cc, $tlpdblinesref, @postlines) = @_;
my $root = $tlpdb->root;
open(INFILE,"<$root/$headfile") or die("Cannot open $root/$headfile");
my @lines = <INFILE>;
push @lines, @$tlpdblinesref;
close (INFILE);
if (-r "$localconf") {
#
# this should be done more intelligently, but for now only add those
# lines without any duplication check ...
open FOO, "<$localconf"
or die "strange, -r ok but cannot open $localconf: $!";
my @tmp = <FOO>;
push @lines, @tmp;
}
if (@postlines) {
push @lines, @postlines;
}
if ($#lines >= 0) {
open(OUTFILE,">$dest")
or die("Cannot open $dest for writing: $!");
if (!$keepfirstline) {
print OUTFILE $cc;
printf OUTFILE " Generated by %s on %s\n", "$0", scalar localtime;
}
print OUTFILE @lines;
close(OUTFILE) || warn "close(>$dest) failed: $!";
}
}
=pod
=back
=head2 Miscellaneous
Ideas from Fabrice Popineau's C<FileUtils.pm>.
=over 6
=item C<sort_uniq(@list)>
The C<sort_uniq> function sorts the given array and throws away multiple
occurrences of elements. It returns a sorted and unified array.
=cut
sub sort_uniq {
my (@l) = @_;
my ($e, $f, @r);
$f = "";
@l = sort(@l);
foreach $e (@l) {
if ($e ne $f) {
$f = $e;
push @r, $e;
}
}
return @r;
}
=pod
=item C<push_uniq(\@list, @items)>
The C<push_uniq> function pushes the last elements on the list referenced
by the first argument.
=cut
sub push_uniq {
# can't we use $l as a reference, and then use my? later ...
local (*l, @le) = @_;
foreach my $e (@le) {
if (! &member($e, @l)) {
push @l, $e;
}
}
}
=pod
=item C<member($item, @list)>
The C<member> function returns true if the the first argument is contained
in the list of the remaining arguments.
=cut
sub member {
my ($e, @l) = @_;
my ($f);
foreach $f (@l) {
if ($e eq $f) {
return 1;
}
}
return 0;
}
=pod
=item C<merge_into(\%to, \%from)>
Merges the keys of %from into %to.
=cut
sub merge_into {
my ($to, $from) = @_;
foreach my $k (keys %$from) {
if (defined($to->{$k})) {
push @{$to->{$k}}, @{$from->{$k}};
} else {
$to->{$k} = [ @{$from->{$k}} ];
}
}
}
=pod
=item C<texdir_check($texdir)>
Test whether installation with TEXDIR set to $texdir would succeed due to
writing permissions.
=cut
sub texdir_check {
my ($texdir) = shift; # PATH/texlive/2008
my $texdirparent = dirname($texdir); # PATH/texlive
my $texdirpparent = dirname($texdirparent); # PATH
if ( (dir_writable($texdirpparent)) ||
( (-d $texdirparent) && (dir_writable($texdirparent)) ) ||
( (-d $texdir) && (dir_writable($texdir)) ) ) {
return 1;
}
return 0;
}
sub logit {
my ($out, $level, @rest) = @_;
_logit($out, $level, @rest) unless $::opt_quiet;
_logit('file', $level, @rest);
}
sub _logit {
my ($out, $level, @rest) = @_;
if ($::opt_verbosity >= $level) {
# if $out is a ref/glob to STDOUT or STDERR, print it there
if (ref($out) eq "GLOB") {
print $out @rest;
} else {
# we should log it into the logfile, but that might be not initialized
# so either print it to the filehandle $::LOGFILE, or push it onto
# the to be printed log lines @::LOGLINES
if (defined($::LOGFILE)) {
print $::LOGFILE @rest;
} else {
push @::LOGLINES, "@rest";
}
}
}
}
sub info {
logit(\*STDOUT, 0, @_);
}
sub debug {
logit(\*STDOUT, 1, "D:", @_);
}
sub ddebug {
logit(\*STDOUT, 2, "DD:", @_);
}
sub log {
# warning should always done to stderr and the log file,
# independently from the verbosity level
my $savequiet = $::opt_quiet;
$::opt_quiet = 0;
_logit('file', -100, @_);
$::opt_quiet = $savequiet;
}
sub tlwarn {
# warning should always done to stderr and the log file,
# independently from the verbosity level
my $savequiet = $::opt_quiet;
$::opt_quiet = 0;
logit(\*STDERR, -100, @_);
$::opt_quiet = $savequiet;
}
sub process_logging_options {
$::opt_verbosity = 0;
$::opt_quiet = 0;
my $opt_logfile;
# check all the command line options for occurrences of -q and -v
# do not report errors
my $oldconfig = Getopt::Long::Configure(qw(pass_through permute));
GetOptions("logfile=s" => \$opt_logfile,
"v+" => \$::opt_verbosity,
"q" => \$::opt_quiet);
Getopt::Long::Configure($oldconfig);
if ($opt_logfile) {
open(TLUTILS_LOGFILE, ">$opt_logfile") || die "open(>$opt_logfile) failed: $!\n";
$::LOGFILE = \*TLUTILS_LOGFILE;
}
}
=pod
=item C<kpsewhich($varname)>
This function expands the variable name using kpsewhich. The variable
name should not have a leading $. The kpsewhich binary has to be in
the path.
=cut
sub kpsewhich {
my $var = shift;
my $ret = `kpsewhich -var-value=$var`;
chomp ($ret);
return($ret);
}
=pod
=item C<welcome>
The welcome message.
=cut
sub welcome {
my $welcome=<<"EOF";
See ./index.html for links to documentation. The TeX Live web site
(http://tug.org/texlive/) contains any updates and corrections.
TeX Live is a joint project of the TeX user groups around the world;
please consider supporting it by joining the group best for you. The
list of groups is available on the web at http://tug.org/usergroups.html.
Welcome to TeX Live!
EOF
return $welcome;
}
=pod
=item C<welcome>
The same welcome message as above but with hints about C<PATH>, C<MANPATH>,
and C<INFOPATH>.
=cut
sub welcome_paths {
my $welcome=<<"EOF";
See ./index.html for links to documentation. The TeX Live web site
(http://tug.org/texlive/) contains any updates and corrections.
TeX Live is a joint project of the TeX user groups around the world;
please consider supporting it by joining the group best for you. The
list of groups is available on the web at http://tug.org/usergroups.html.
Add $::vars{'TEXDIR'}/texmf/doc/man to MANPATH.
Add $::vars{'TEXDIR'}/texmf/doc/info to INFOPATH.
Most importantly, add $::vars{'TEXDIR'}/bin/$::vars{'this_platform'}
to your PATH for current and future sessions.
Welcome to TeX Live!
EOF
return $welcome;
}
=pod
This function returns a "windowsified" path, i.e., replacing all forward
slashes to backslashes, and adding additional " around (in case of spaces
in $path). Furthermore it makes the path absolute. So if $path does not
start with an arbitrary char followed by : we add the output of `cd`.
=cut
sub conv_to_win_path {
my $p = shift;
$p =~ s!/!\\!g;
# we need absolute pathes, too
if ($p !~ m!^.:!) {
my $cwd = `cd`;
chomp($cwd);
$p = "$cwd\\$p";
}
if ($p =~ m/ /) { $p = "\"$p\""; }
return($p);
}
sub give_ctan_mirror
{
my $wget = $::progs{'wget'};
if (!defined ($wget)) {
tlwarn ("programs not set up, trying wget\n");
$wget = "wget";
}
my $cmd = "$wget $TeXLiveServerURL -O "
. (win32() ? "nul" : "/dev/null") . " 2>&1";
my @out = `$cmd`;
# now analyze the output
my $mirror = $TeXLiveURL;
foreach (@out) {
if (m/^Location: (\S*)\s*.*$/) {
(my $mhost = $1) =~ s,/*$,,; # remove trailing slashes since we add it:
$mirror = "$mhost/$TeXLiveServerPath";
last;
}
}
# if we cannot find a mirror, return the default (mirror.ctan.org) itself
return $mirror;
}
sub tlmd5 {
my $file = shift;
if (-r $file) {
open(FILE, $file) or die "Can't open '$file': $!";
binmode(FILE);
return Digest::MD5->new->addfile(*FILE)->hexdigest;
close(FILE);
} else {
return "";
}
}
#############################################
#
# Taken from Text::ParseWords
#
sub quotewords {
my($delim, $keep, @lines) = @_;
my($line, @words, @allwords);
foreach $line (@lines) {
@words = parse_line($delim, $keep, $line);
return() unless (@words || !length($line));
push(@allwords, @words);
}
return(@allwords);
}
sub parse_line {
my($delimiter, $keep, $line) = @_;
my($word, @pieces);
no warnings 'uninitialized'; # we will be testing undef strings
while (length($line)) {
$line =~ s/^(["']) # a $quote
((?:\\.|(?!\1)[^\\])*) # and $quoted text
\1 # followed by the same quote
| # --OR--
^((?:\\.|[^\\"'])*?) # an $unquoted text
(\Z(?!\n)|(?-x:$delimiter)|(?!^)(?=["']))
# plus EOL, delimiter, or quote
//xs or return; # extended layout
my($quote, $quoted, $unquoted, $delim) = ($1, $2, $3, $4);
return() unless( defined($quote) || length($unquoted) || length($delim));
if ($keep) {
$quoted = "$quote$quoted$quote";
} else {
$unquoted =~ s/\\(.)/$1/sg;
if (defined $quote) {
$quoted =~ s/\\(.)/$1/sg if ($quote eq '"');
$quoted =~ s/\\([\\'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'");
}
}
$word .= substr($line, 0, 0); # leave results tainted
$word .= defined $quote ? $quoted : $unquoted;
if (length($delim)) {
push(@pieces, $word);
push(@pieces, $delim) if ($keep eq 'delimiters');
undef $word;
}
if (!length($line)) {
push(@pieces, $word);
}
}
return(@pieces);
}
=pod
=back
=head1 SEE ALSO
The modules L<TeXLive::TLPSRC>, L<TeXLive::TLPOBJ>,
L<TeXLive::TLPDB>, L<TeXLive::TLTREE>, and the
document L<Perl-API.txt> and the specification in the TeX Live
repository trunk/Master/tlpkg/doc/.
=head1 AUTHORS AND COPYRIGHT
This script and its documentation were written for the TeX Live
distribution (L<http://tug.org/texlive>) and both are licensed under the
GNU General Public License Version 2 or later.
=cut
1;
### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2 expandtab: #
|