Menu

[r251]: / branches / texdist2 / Library / Tools.m  Maximize  Restore  History

Download this file

447 lines (438 with data), 16.6 kB

  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
//
// Tools.m
// TeXDist2
//
// Created by coder on 08/12/06.
// Copyright 2006-2011 Laurens'Tribune. All rights reserved.
//
#import "Common.h"
#import "SystemVersion.h"
#import "Tools.h"
#import <sys/sysctl.h>
#import <sys/stat.h>
#define I386_STRING "i386"
#define X86_64_STRING "x86_64"
#define POWERPC_STRING "ppc"
#define POWERPC64_STRING "ppc64"
@implementation NSURL(Tools)
- (NSString *)Contents_Path4TeXDist2;
{
return [self.path stringByAppendingPathComponent:[Tools Contents_Component]];
}
- (NSString *)Programs_Path4TeXDist2;
{
return [self.path stringByAppendingPathComponent:[Tools Programs_RelativePath]];
}
- (NSString *)Programs_texbin_Path4TeXDist2;
{
return [self.path stringByAppendingPathComponent:[Tools Programs_texbin_RelativePath]];
}
- (NSString *)Programs_texbin_Destination4TeXDist2;
{
return [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:self.Programs_texbin_Path4TeXDist2 error:NULL];
}
@end
@interface Tools()
@property (readonly,atomic,weak) NSDictionary * defaults;
@end
@implementation Tools
+ (NSBundle *)bundle;
{
return [NSBundle bundleForClass:[Tools class]];
}
+ (NSString *)Contents_Component;
{
return [[self Programs_RelativePath] stringByDeletingLastPathComponent];
}
+ (NSString *)Programs_RelativePath;
{
return kTeXDist2SetUpProgramsRelativeKey;
}
+ (NSString *)Programs_texbin_RelativePath;
{
return [[self Programs_RelativePath] stringByAppendingPathComponent:kTeXDist2_texbin];
}
+ (NSString *)rootRelativePath;
{
return kTeXDist2SetUpRootRelativeKey;
}
+ (NSString *)LTD_Path;
{
return kTeXDist2SetUpLibraryTeXKey;
}
+ (NSString *)LTD_DefaultTeX_Path;
{
return kTeXDist2SetUpDefaultTeXKey;
}
+ (NSString *)LTD_texdistmgr_Path;
{
return kTeXDist2SetUp_texdistmgr_Key;
}
+ (NSString *)ULB_texdistmgr_Path;
{
return kTeXDist2SetUp_ULB_texdistmgr_Key;
}
+ (NSString *)LTD_DefaultTeX_Contents_Path;
{
return [self LTD_texbin_Path].stringByDeletingLastPathComponent.stringByDeletingLastPathComponent;
}
+ (NSString *)LTD_DefaultTeX_Contents_Destination;
{
return [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:[self LTD_DefaultTeX_Contents_Path] error:NULL];
}
+ (NSString *)texdistmgr_DestinationForName:(NSString *)name;
{
return [[[@"/Library/PreferencePanes/" stringByAppendingPathComponent:name]
stringByAppendingPathComponent:@"Contents"]
stringByAppendingPathComponent:@"Executables/texdistmgr"];
}
+ (NSString *)LTD_texbin_Path;
{
return [[self LTD_DefaultTeX_Path] stringByAppendingPathComponent:[Tools Programs_texbin_RelativePath]];
}
+ (NSString *)usr_texbin_Destination;
{
return kTeXDist2SetUp_usr_texbin_destination_Key;
}
+ (NSString *)Programs_texbin_Destination;
{
return kTeXDist2SetUp_Programs_texbin_destination_Key;
}
+ (NSString *)usr_texbin_Path;
{
return kTeXDist2SetUp_usr_texbin_Key;
}
+ (NSString *)Programs_texbin_Path;
{
return kTeXDist2SetUp_Programs_texbin_Key;
}
+ (NSString *)texdistExtension;
{
return kTeXDist2ExtensionKey;
}
+ (NSDictionary *)defaults;
{
return @{
kLSMinimumSystemVersionByArchitecture:@{
@POWERPC_STRING :[SystemVersion systemVersionWithString:@"10.0.0"],
@I386_STRING :[SystemVersion systemVersionWithString:@"10.4.4"],
@X86_64_STRING:[SystemVersion systemVersionWithString:@"10.4.7"],
@POWERPC64_STRING:[SystemVersion systemVersionWithString:@"10.4.7"]
},
kLSMaximumSystemVersionByArchitecture:@{
@POWERPC_STRING :[SystemVersion systemVersionWithString:@"10.6.0"],
@POWERPC64_STRING:[SystemVersion systemVersionWithString:@"10.6.0"]
},
kTeXDist2SetUpArchitectureNamesPriorityByArchKey:@{
@I386_STRING :@[@I386_STRING,@POWERPC_STRING],
@POWERPC_STRING :@[@POWERPC_STRING],
@X86_64_STRING:@[@X86_64_STRING,@I386_STRING,@POWERPC_STRING],
@POWERPC64_STRING:@[@POWERPC64_STRING,@POWERPC_STRING]
},
kTeXDist2SetUpArchitecturePrioritiesByArchKey:@{
@I386_STRING:@[
@I386_STRING"-darwin",
@I386_STRING,
@"x86-darwin",
@"x86",
@"universal-darwin",
@"universal",
@POWERPC_STRING"-darwin",
@POWERPC_STRING,
@"powerpc-darwin",
@"powerpc"
],
@POWERPC_STRING:@[
@POWERPC_STRING"-darwin",
@POWERPC_STRING,
@"powerpc-darwin",
@"powerpc",
@"universal-darwin",
@"universal"
],
@X86_64_STRING:@[
@X86_64_STRING"-darwin",
@X86_64_STRING,
@"universal_64-darwin",
@"universal_64",
@I386_STRING"-darwin",
@I386_STRING,
@"x86-darwin",
@"x86",
@"universal-darwin",
@"universal",
@POWERPC_STRING"-darwin",
@POWERPC_STRING,
@"powerpc-darwin",
@"powerpc"
],
@POWERPC64_STRING:@[
@POWERPC64_STRING"-darwin",
@POWERPC64_STRING,
@"powerpc64-darwin",
@"powerpc64",
@"universal_64-darwin",
@POWERPC_STRING"-darwin",
@POWERPC_STRING,
@"powerpc-darwin",
@"powerpc",
@"universal-darwin",
@"universal"
]
},
kTeXDist2SetUpExpectedProgramsKey:@[@"tex",@"etex",@"pdftex",@"pdfetex",@"xetex",@"luatex"],
@"READMEs":@[
@"For intel machines, universal identifiers must come before intel and ppc",
@"For ppc machines, ppc comes before universal",
@"For intel machines, rosetta is 32 bits only"
]
};
}
+ (NSArray *)expectedPrograms;
{
return self.defaults[kTeXDist2SetUpExpectedProgramsKey];
}
#import <mach/machine.h>
+ (cpu_type_t) CPUType;
{
cpu_type_t cpu_type;
size_t length = sizeof(cpu_type);
int value = 0;
sysctlbyname("hw.cputype", &cpu_type, &length, NULL, 0);
length = sizeof(value);
switch ((unsigned long)cpu_type&~CPU_ARCH_MASK) {
case CPU_TYPE_X86:
if (!sysctlbyname("hw.cpu64bit_capable", &value, &length, NULL, 0)) {
cpu_type |= CPU_ARCH_ABI64;
}
break;
case CPU_TYPE_POWERPC:
if (NOT sysctlbyname("hw.optional.64bitops", &value, &length, NULL, 0)) {
cpu_type |= CPU_ARCH_ABI64;
}
break;
default:
[Logger logWithLevel:kTeXDist2MsgLevelERR
format:@"TeXDist2 ERROR: unknown CPU type %lul",(unsigned long)cpu_type];
break;
}
return cpu_type;
}
+ (NSString *)archForCPU:(cpu_type_t)CPUType;
{
switch (CPUType) {
#define SWITCH_CASE(ID) case CPU_TYPE_##ID:return [[NSString stringWithFormat:@"%s",#ID] lowercaseString]
#if 0
SWITCH_CASE(VAX);
SWITCH_CASE(MC680x0);
SWITCH_CASE(MC98000);
SWITCH_CASE(HPPA);
SWITCH_CASE(MC88000);
SWITCH_CASE(SPARC);
SWITCH_CASE(I860);
#endif
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
SWITCH_CASE(X86_64);
case CPU_TYPE_POWERPC64:return @POWERPC64_STRING;
#endif
SWITCH_CASE(I386);
case CPU_TYPE_POWERPC:return @POWERPC_STRING;
default: return @"default";
}
}
+ (NSString *)arch;
{
return [self archForCPU:self.CPUType];
}
+ (cpu_type_t) CPUForArch:(NSString *)arch;
{
if ([arch isEqual:@POWERPC_STRING]) {
return CPU_TYPE_POWERPC;
}
if ([arch isEqual:@POWERPC64_STRING]) {
return CPU_TYPE_POWERPC64;
}
if ([arch isEqual:@I386_STRING] OR [arch isEqual:@"x86"]) {
return CPU_TYPE_X86;
}
if ([arch isEqual:@X86_64_STRING]) {
return CPU_TYPE_X86_64;
}
return CPU_TYPE_ANY;
}
+ (NSArray *)architectureNamesOfCPU:(cpu_type_t)CPUType;
{
return self.defaults[kTeXDist2SetUpArchitectureNamesPriorityByArchKey][[self archForCPU:CPUType]]?:@[];
}
+ (NSArray *)architectureNames;
{
static NSArray * Ns = nil;
static dispatch_once_t once;
dispatch_once(&once,^{
NSMutableArray * MRA = [NSMutableArray array];
for (NSString * arch in [self architectureNamesOfCPU:self.CPUType]) {
SystemVersion * minSV = self.minimumSystemVersionsByArch[arch]?:[SystemVersion firstSystemVersion];
SystemVersion * maxSV = self.maximumSystemVersionsByArch[arch]?:[SystemVersion lastSystemVersion];
if ( (NSOrderedAscending != [[SystemVersion runningSystemVersion] compare:minSV])
AND (NSOrderedAscending == [[SystemVersion runningSystemVersion] compare:maxSV]) ) {
[MRA addObject:arch];
}
}
Ns = MRA.copy;
assert(Ns.count>0);
});
return Ns;
}
+ (NSArray *)archNames;
{
static NSArray * Ns = nil;
static dispatch_once_t once;
dispatch_once(&once,^{
NSMutableArray * MRA = [NSMutableArray array];
for (NSString * arch in [self architectureNamesOfCPU:self.CPUType]) {
SystemVersion * minSV = self.minimumSystemVersionsByArch[arch]?:[SystemVersion firstSystemVersion];
SystemVersion * maxSV = self.maximumSystemVersionsByArch[arch]?:[SystemVersion lastSystemVersion];
if ( (NSOrderedAscending != [[SystemVersion runningSystemVersion] compare:minSV])
AND (NSOrderedAscending == [[SystemVersion runningSystemVersion] compare:maxSV]) ) {
[MRA addObject:arch];
}
}
Ns = MRA.copy;
assert(Ns.count>0);
});
return Ns;
}
+ (SystemVersion *)minimumSystemVersion;
{
return self.defaults[kLSMinimumSystemVersionByArchitecture][self.arch]?:[SystemVersion firstSystemVersion];
}
+ (SystemVersion *)maximumSystemVersion;
{
return self.defaults[kLSMaximumSystemVersionByArchitecture][self.arch]?:[SystemVersion lastSystemVersion];
}
+ (NSDictionary *)minimumSystemVersionsByArch;
{
return self.defaults[kLSMinimumSystemVersionByArchitecture];
}
+ (NSDictionary *)maximumSystemVersionsByArch;
{
return self.defaults[kLSMaximumSystemVersionByArchitecture];
}
+ (NSDictionary *)architecturePriorityByArch;
{
static NSDictionary * D = nil;
static dispatch_once_t once;
dispatch_once(&once,^{
NSMutableArray * excludedNames = [NSMutableArray array];
[self.defaults[kTeXDist2SetUpArchitecturePrioritiesByArchKey]
enumerateKeysAndObjectsUsingBlock:^(NSString * arch, NSArray * priorities, BOOL *stop) {
#pragma unused(stop)
if (NOT[self.archNames containsObject:arch]) {
[priorities enumerateObjectsUsingBlock:^(NSString * name, NSUInteger idx, BOOL *soustop){
#pragma unused(idx)
#pragma unused(soustop)
if (NOT [name rangeOfString:@"universal" options:NSCaseInsensitiveSearch].length) {
[excludedNames addObject:name];
};
}];
}
}
];
NSMutableDictionary * MD = [NSMutableDictionary dictionary];
[self.defaults[kTeXDist2SetUpArchitecturePrioritiesByArchKey]
enumerateKeysAndObjectsUsingBlock:^(NSString * arch, NSArray * priorities, BOOL *stop) {
#pragma unused(stop)
if ([self.archNames containsObject:arch]) {
NSMutableArray * mra = priorities.mutableCopy;
[mra removeObjectsInArray:excludedNames];
MD[arch]=mra.copy;
}
}
];
D = MD.copy;
});
return D;
}
+ (NSArray *)architecturePriority;
{
return self.defaults[kTeXDist2SetUpArchitecturePrioritiesByArchKey][self.arch]?:@[];
}
#ifndef TeXDist2_HELPER
+ (BOOL)isAlternateKeyDown;
{
return 0 != (kCGEventFlagMaskAlternate & CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState));
}
#endif
+ (BOOL) is_texdistmgr_OKForName:(NSString *)name;
{
NSFileManager * DFM = [NSFileManager defaultManager];
NSString * expected = [self texdistmgr_DestinationForName:name];
if ([DFM isExecutableFileAtPath:expected]) {
NSString * texdistmgr = [self LTD_texdistmgr_Path];
NSString * destination = [DFM destinationOfSymbolicLinkAtPath:texdistmgr error:NULL];
return [destination isEqual:expected];
}
return NO;
}
+ (BOOL) is_user_texdistmgr_OKForName:(NSString *)name;
{
NSFileManager * DFM = [NSFileManager defaultManager];
NSString * expected = [self texdistmgr_DestinationForName:name];
expected = [NSHomeDirectory() stringByAppendingPathComponent:expected];
if ([DFM isExecutableFileAtPath:expected]) {
NSString * texdistmgr = [self LTD_texdistmgr_Path];
texdistmgr = [NSHomeDirectory() stringByAppendingPathComponent:texdistmgr];
NSString * destination = [DFM destinationOfSymbolicLinkAtPath:texdistmgr error:NULL];
return [destination isEqual:expected];
}
return NO;
}
+ (BOOL) fix_texdistmgr_ForName:(NSString *)name error:(NSError **)RORef;
{
NSString * expected = [self texdistmgr_DestinationForName:name];
NSFileManager * DFM = [NSFileManager defaultManager];
BOOL result = [DFM createSymbolicLinkAtPath:[self LTD_texdistmgr_Path] withDestinationPath:expected error4TeXDist2:RORef];
return [DFM createSymbolicLinkAtPath:[self ULB_texdistmgr_Path] withDestinationPath:expected error4TeXDist2:RORef] AND result;
}
+ (BOOL) fix_user_texdistmgr_ForName:(NSString *)name error:(NSError **)RORef;
{
NSString * texdistmgr = [self LTD_texdistmgr_Path];
texdistmgr = [NSHomeDirectory() stringByAppendingPathComponent:texdistmgr];
NSString * expected = [self texdistmgr_DestinationForName:name];
expected = [NSHomeDirectory() stringByAppendingPathComponent:expected];
NSFileManager * DFM = [NSFileManager defaultManager];
return [DFM createSymbolicLinkAtPath:texdistmgr withDestinationPath:expected error4TeXDist2:RORef];
}
+ (BOOL) isResetOK;
{
NSFileManager * DFM = [NSFileManager defaultManager];
NSString * P = [Tools LTD_DefaultTeX_Path];
BOOL existsP = [DFM fileExistsAtPath:P followSymLinks4TeXDist2:NO];
NSString * Q = [Tools usr_texbin_Path];
BOOL existsQ = [DFM fileExistsAtPath:Q followSymLinks4TeXDist2:NO];
NSString * R = [Tools Programs_texbin_Path];
BOOL existsR = [DFM fileExistsAtPath:R followSymLinks4TeXDist2:NO];
return NOT existsP AND NOT existsQ AND NOT existsR;
}
+ (BOOL) resetOrReturnError:(NSError **)RORef;
{
NSFileManager * DFM = [NSFileManager defaultManager];
NSString * P = [Tools LTD_DefaultTeX_Path];
BOOL existsP = [DFM fileExistsAtPath:P followSymLinks4TeXDist2:NO];
NSString * Q = [Tools usr_texbin_Path];
BOOL existsQ = [DFM fileExistsAtPath:Q followSymLinks4TeXDist2:NO];
NSString * R = [Tools Programs_texbin_Path];
BOOL existsR = [DFM fileExistsAtPath:R followSymLinks4TeXDist2:NO];
NSError * ROR = nil;
if ((NOT existsP OR [DFM removeItemAtPath:P error:&ROR])
AND (NOT existsQ OR [DFM removeItemAtPath:Q error:&ROR])
AND (NOT existsR OR [DFM removeItemAtPath:R error:&ROR])) {
return YES;
}
TeXDist2ForwardLocalError(ROR,RORef,errTeXDist2ResetFailed);
return NO;
}
@end
#define kTeXDist2_isExecutableFileAtPath(THE_PATH)\
(([[[NSFileManager defaultManager] attributesOfItemAtPath:[THE_PATH stringByResolvingSymlinksInPath] error:NULL] filePosixPermissions]&(S_IXUSR|S_IXGRP|S_IXOTH))\
==(S_IXUSR|S_IXGRP|S_IXOTH))
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.