forked from MacPaw/XADMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLineCommon.m
415 lines (347 loc) · 11.7 KB
/
CommandLineCommon.m
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
/*
* CommandLineCommon.m
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#import "CommandLineCommon.h"
#import "XADArchiveParser.h"
#import "XADArchiveParserDescriptions.h"
#import "XADString.h"
#import "NSStringPrinting.h"
#include <time.h>
#ifndef __MINGW32__
#import <unistd.h>
#endif
BOOL IsListRequest(NSString *encoding)
{
if(!encoding) return NO;
if([encoding caseInsensitiveCompare:@"list"]==NSOrderedSame) return YES;
if([encoding caseInsensitiveCompare:@"help"]==NSOrderedSame) return YES;
return NO;
}
void PrintEncodingList()
{
NSEnumerator *enumerator=[[XADString availableEncodingNames] objectEnumerator];
NSArray *encodingarray;
while((encodingarray=[enumerator nextObject]))
{
NSString *description=[encodingarray objectAtIndex:0];
if((id)description==[NSNull null]||[description length]==0) description=nil;
NSString *encoding=[encodingarray objectAtIndex:1];
NSString *aliases=nil;
if([encodingarray count]>2) aliases=[[encodingarray subarrayWithRange:
NSMakeRange(2,[encodingarray count]-2)] componentsJoinedByString:@", "];
[@" * " print];
[encoding print];
if(aliases)
{
[@" (" print];
[aliases print];
[@")" print];
}
if(description)
{
[@": " print];
[description print];
}
[@"\n" print];
}
}
NSString *ShortInfoLineForEntryWithDictionary(NSDictionary *dict)
{
NSNumber *dirnum=[dict objectForKey:XADIsDirectoryKey];
BOOL isdir=dirnum && [dirnum boolValue];
NSString *name=[[dict objectForKey:XADFileNameKey] string];
name=[name stringByEscapingControlCharacters];
if(isdir) return [name stringByAppendingString:@"/"]; // TODO: What about Windows?
else return name;
}
NSString *MediumInfoLineForEntryWithDictionary(NSDictionary *dict)
{
NSNumber *dirnum=[dict objectForKey:XADIsDirectoryKey];
NSNumber *linknum=[dict objectForKey:XADIsLinkKey];
NSNumber *resnum=[dict objectForKey:XADIsResourceForkKey];
NSNumber *corruptednum=[dict objectForKey:XADIsCorruptedKey];
NSNumber *sizenum=[dict objectForKey:XADFileSizeKey];
// NSNumber *compsize=[dict objectForKey:XADCompressedSizeKey];
BOOL isdir=dirnum && [dirnum boolValue];
BOOL islink=linknum && [linknum boolValue];
BOOL isres=resnum && [resnum boolValue];
BOOL iscorrupted=corruptednum && [corruptednum boolValue];
BOOL hassize=(sizenum!=nil);
NSString *name=[[dict objectForKey:XADFileNameKey] string];
name=[name stringByEscapingControlCharacters];
NSMutableString *string=[NSMutableString stringWithString:name];
if(isdir) [string appendString:@"/"]; // TODO: What about Windows?
NSMutableArray *tags=[NSMutableArray array];
if(isdir) [tags addObject:@"dir"];
else if(islink) [tags addObject:@"link"];
else if(hassize) [tags addObject:[NSString stringWithFormat:@"%lld B",[sizenum longLongValue]]];
if(isres) [tags addObject:@"rsrc"];
if(iscorrupted) [tags addObject:@"corrupted"];
if([tags count])
{
[string appendString:@" ("];
[string appendString:[tags componentsJoinedByString:@", "]];
[string appendString:@")"];
}
return string;
}
static NSString *CodeForCompressionName(NSString *compname);
NSString *LongInfoLineForEntryWithDictionary(NSDictionary *dict,XADArchiveParser *parser)
{
NSNumber *indexnum=[dict objectForKey:XADIndexKey];
NSNumber *dirnum=[dict objectForKey:XADIsDirectoryKey];
NSNumber *linknum=[dict objectForKey:XADIsLinkKey];
NSNumber *resnum=[dict objectForKey:XADIsResourceForkKey];
NSNumber *encryptednum=[dict objectForKey:XADIsEncryptedKey];
//NSNumber *corruptednum=[dict objectForKey:XADIsCorruptedKey];
BOOL isdir=dirnum && [dirnum boolValue];
BOOL islink=linknum && [linknum boolValue];
BOOL isres=resnum && [resnum boolValue];
BOOL isencrypted=encryptednum && [encryptednum boolValue];
//BOOL iscorrupted=corruptednum && [corruptednum boolValue];
NSObject *extattrs=[dict objectForKey:XADExtendedAttributesKey];
// TODO: check for non-empty finder info &c
BOOL hasextattrs=extattrs?YES:NO;
NSNumber *sizenum=[dict objectForKey:XADFileSizeKey];
NSNumber *compsizenum=[dict objectForKey:XADCompressedSizeKey];
off_t size=sizenum?[sizenum longLongValue]:0;
off_t compsize=compsizenum?[compsizenum longLongValue]:0;
NSString *sizestr;
if(sizenum)
{
sizestr=[NSString stringWithFormat:@"%11lld",[sizenum longLongValue]];
}
else
{
sizestr=@" ----------";
}
NSString *compstr;
if(size&&compsize)
{
double compression=100*(1-(double)compsize/(double)size);
if(compression<=-100) compstr=[NSString stringWithFormat:@"%5.0f%%",compression];
else compstr=[NSString stringWithFormat:@"%5.1f%%",compression];
}
else
{
compstr=@" -----";
}
XADString *compname=[dict objectForKey:XADCompressionNameKey];
NSString *compcode;
if(compname)
{
compcode=CodeForCompressionName([compname string]);
if([compcode length]<4) compcode=[NSString stringWithFormat:@"%@%s",compcode,&" "[[compcode length]]];
}
else
{
compcode=@"----";
}
NSDate *date=[dict objectForKey:XADLastModificationDateKey];
if(!date) date=[dict objectForKey:XADCreationDateKey];
if(!date) date=[dict objectForKey:XADLastAccessDateKey];
NSString *datestr;
if(date)
{
#ifndef __COCOTRON__
NSDateFormatter *formatter=[[NSDateFormatter new] autorelease];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
datestr=[formatter stringFromDate:date];
#else
NSDateFormatter *formatter=[[NSDateFormatter new] autorelease];
[formatter setDateFormat:@"%Y-%m-%d %H:%M"];
datestr=[formatter stringFromDate:date];
#endif
}
else
{
datestr=@"----------------";
}
NSString *name=[[dict objectForKey:XADFileNameKey] string];
name=[name stringByEscapingControlCharacters];
NSString *linkstr=@"";
if(islink)
{
XADError error;
XADString *link=[parser linkDestinationForDictionary:dict error:&error];
if(link) linkstr=[NSString stringWithFormat:@" -> %@",link];
}
return [NSString stringWithFormat:
@"%3d. %c%c%c%c%c %@ %@ %@ %@ %@%s%@",
[indexnum intValue],
isdir?'D':'-',
isres?'R':'-',
islink?'L':'-',
isencrypted?'E':'-',
hasextattrs?'@':'-',
sizestr,
compstr,
compcode,
datestr,
name,
isdir?"/":"",
linkstr];
}
static NSMutableDictionary *codeforname=nil;
static NSMutableSet *usedcodes;
static NSDictionary *abbreviations=nil;
static NSString *CodeForCompressionName(NSString *compname)
{
if(!codeforname) codeforname=[NSMutableDictionary new];
NSString *cachedcode=[codeforname objectForKey:compname];
if(cachedcode) return cachedcode;
if(!usedcodes) usedcodes=[NSMutableSet new];
if(!abbreviations) abbreviations=[[NSDictionary alloc] initWithObjectsAndKeys:
@"Df64",@"Deflate64",
@"LZBC",@"LZMA+BCJ",
@"LZB2",@"LZMA+BCJ2",
@"PPBC",@"PPMd+BCJ",
@"PPB2",@"PPMd+BCJ2",
@"Ftst",@"Fastest",
@"MSZP",@"MSZIP",
@"Qua0",@"Quantum:0",@"Qua1",@"Quantum:1",@"Qua2",@"Quantum:2",@"Qua3",@"Quantum:3",
@"Qua4",@"Quantum:4",@"Qua5",@"Quantum:5",@"Qua6",@"Quantum:6",@"Qua7",@"Quantum:7",
@"Qua8",@"Quantum:8",@"Qua9",@"Quantum:9",@"Qu10",@"Quantum:10",@"Qu11",@"Quantum:11",
@"Qu12",@"Quantum:12",@"Qu13",@"Quantum:13",@"Qu14",@"Quantum:14",@"Qu15",@"Quantum:15",
@"Qu16",@"Quantum:16",@"Qu17",@"Quantum:17",@"Qu18",@"Quantum:18",@"Qu19",@"Quantum:19",
@"Qu20",@"Quantum:20",@"Qu21",@"Quantum:21",@"Qu22",@"Quantum:22",@"Qu23",@"Quantum:23",
@"Qu24",@"Quantum:24",@"Qu25",@"Quantum:25",@"Qu26",@"Quantum:26",@"Qu27",@"Quantum:27",
@"Qu28",@"Quantum:28",@"Qu29",@"Quantum:29",@"Qu30",@"Quantum:30",@"Qu31",@"Quantum:31",
@"LZX0",@"LZX:0",@"LZX1",@"LZX:1",@"LZX2",@"LZX:2",@"LZX3",@"LZX:3",
@"LZX4",@"LZX:4",@"LZX5",@"LZX:5",@"LZX6",@"LZX:6",@"LZX7",@"LZX:7",
@"LZX8",@"LZX:8",@"LZX9",@"LZX:9",@"LZ10",@"LZX:10",@"LZ11",@"LZX:11",
@"LZ12",@"LZX:12",@"LZ13",@"LZX:13",@"LZ14",@"LZX:14",@"LZ15",@"LZX:15",
@"LZ16",@"LZX:16",@"LZ17",@"LZX:17",@"LZ18",@"LZX:18",@"LZ19",@"LZX:19",
@"LZ20",@"LZX:20",@"LZ21",@"LZX:21",@"LZ22",@"LZX:22",@"LZ23",@"LZX:23",
@"LZ24",@"LZX:24",@"LZ25",@"LZX:25",@"LZ26",@"LZX:26",@"LZ27",@"LZX:27",
@"LZ28",@"LZX:28",@"LZ29",@"LZX:29",@"LZ30",@"LZX:30",@"LZ31",@"LZX:31",
@"Mth1",@"Method 1",@"Mth2",@"Method 2",@"Mth3",@"Method 3",@"Mth4",@"Method 4",
@"Mth5",@"Method 5",@"Mth6",@"Method 6",@"Mth7",@"Method 7",@"Mth8",@"Method 8",
@"Mth9",@"Method 9",@"Mt10",@"Method 10",@"Mt11",@"Method 11",@"Mt12",@"Method 12",
@"Mt13",@"Method 13",@"Mt14",@"Method 14",@"Mt15",@"Method 15",@"Mt16",@"Method 16",
@"AD2",@"ADS/AD2",
@"CPT",@"Compact Pro",
@"AD1",@"AD/AD1",
@"None",@"-lh0-",
@"lh1",@"-lh1-",
@"lh2",@"-lh2-",
@"lh3",@"-lh3-",
@"lh4",@"-lh4-",
@"lh5",@"-lh5-",
@"lh6",@"-lh6-",
@"lh7",@"-lh7-",
@"lzs",@"-lzs-",
@"lz4",@"-lz4-",
@"lz5",@"-lz5-",
@"None",@"-pm0-",
@"pm2",@"-pm2-",
@"PPck",@"PowerPacker",
@"Ft15",@"Fastest v1.5",@"Ft20",@"Fastest v2.0",@"Ft29",@"Fastest v2.9",
@"Fs15",@"Fast v1.5",@"Fs20",@"Fast v2.0",@"Fs29",@"Fast v2.9",
@"Nr15",@"Normal v1.5",@"Nr20",@"Normal v2.0",@"Nr29",@"Normal v2.9",
@"Gd15",@"Good v1.5",@"Gd20",@"Good v2.0",@"Gd29",@"Good v2.9",
@"Bs15",@"Best v1.5",@"Bs20",@"Best v2.0",@"Bs29",@"Best v2.9",
nil];
NSString *code=[abbreviations objectForKey:compname];
if(!code)
{
if([compname length]<=4) code=compname;
else code=[compname substringWithRange:NSMakeRange(0,4)];
}
if([usedcodes containsObject:code])
{
int i=2;
do
{
unichar c[4];
c[3]=i%10;
if(i>=10) c[2]=(i/10)%10;
else c[2]=[code characterAtIndex:2];
if(i>=100) c[1]=(i/100)%10;
else c[1]=[code characterAtIndex:1];
if(i>=1000) c[0]=(i/1000)%10;
else c[0]=[code characterAtIndex:0];
if(i>=10000) return @"@@@@";
i++;
code=[NSString stringWithCharacters:c length:4];
}
while([usedcodes containsObject:code]);
}
[usedcodes addObject:code];
[codeforname setObject:code forKey:compname];
return code;
}
NSString *CompressionNameExplanationForLongInfo()
{
NSMutableString *res=nil;
NSEnumerator *enumerator=[[[codeforname allKeys] sortedArrayUsingSelector:@selector(compare:)] objectEnumerator];
NSString *compname;
while((compname=[enumerator nextObject]))
{
NSString *code=[codeforname objectForKey:compname];
if([code isEqualToString:compname]) continue;
if(res) [res appendFormat:@", %@=%@",code,compname];
else res=[NSMutableString stringWithFormat:@"%@=%@",code,compname];
}
return res;
}
BOOL IsInteractive()
{
// #ifdef __MINGW32__
// return isatty(fileno(stdin))&&isatty(fileno(stdout));
// #else
return isatty(fileno(stdin))&&isatty(fileno(stdout));
// #endif
}
int GetPromptCharacter()
{
#ifdef __APPLE__
fpurge(stdin);
int c=getc(stdin);
fpurge(stdin);
#else
// TODO: Handle purging.
char c;
if(scanf("%c%*c",&c)<1) return -1;
#endif
return c;
}
NSString *AskForPassword(NSString *prompt)
{
[prompt print];
fflush(stdout); // getpass() doesn't print its prompt to stdout.
#ifdef __MINGW32__
[@"Password (will be shown): " print];
fflush(stdout);
char pass[1024];
fgets(pass,sizeof(pass),stdin);
int length=strlen(pass);
if(pass[length-1]=='\n')
{
pass[length-1]=0;
if(pass[length-2]=='\r') pass[length-2]=0;
}
#else
char *pass=getpass("Password (will not be shown): ");
if(!pass) return nil;
#endif
return [NSString stringWithUTF8String:pass];
}