Skip to content

Commit b1d335b

Browse files
committed
[thdat] Detect for -l
1 parent 4454197 commit b1d335b

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

thdat/thdat.1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
.Sh SYNOPSIS
3434
.Nm
3535
.Op Fl V
36-
.Op Oo Fl c | l | x Oc Ar version
36+
.Op Oo Fl c | l | x Oc Oo Li d | Ar version Oc
3737
.Op Ar archive Op Ar
3838
.Sh DESCRIPTION
3939
The
@@ -43,20 +43,21 @@ The following commands are available:
4343
.Bl -tag -width Ds
4444
.It Nm Fl c Ar version Ar archive Ar file Op Ar
4545
Archives the specified files.
46-
.It Nm Fl l Ar version Ar archive
46+
.It Nm Fl l Oo Li d | Ar version Oc Ar archive
4747
Lists the contents of the archive.
4848
.It Nm Fl x Oo Li d | Ar version Oc Ar archive Op Ar
4949
Extracts files.
5050
If no files are specified, all files are extracted.
51+
.El
52+
.Pp
53+
The version specifies which archive format to use.
54+
Running the program without a command will list the supported formats.
5155
.No If Li d is specified instead of Ar version ,
5256
.Nm
5357
automatically detects the file format.
5458
.It Nm Fl V
5559
Displays the program version.
56-
.El
57-
.Pp
58-
The version specifies which archive format to use.
59-
Running the program without a command will list the supported formats.
60+
6061
.Sh ENVIRONMENT
6162
.Bl -tag -width OMP_NUM_THREADS
6263
.It Ev OMP_NUM_THREADS

thdat/thdat.c

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ print_usage(
4747
" -V display version information and exit\n"
4848
"VERSION can be:\n"
4949
" 1, 2, 3, 4, 5, 6, 7, 8, 9, 95, 10, 103 (for Uwabami Breakers), 105, 11, 12, 123, 125, 128, 13, 14, 143, 15, or 16\n"
50-
"For extraction mode, VERSION can also be set to 'd' to enable automatic file detection\n\n"
50+
"Specify 'd' as VERSION to automatically detect archive format. (-l and -x only)\n\n"
5151
"Report bugs to <" PACKAGE_BUGREPORT ">.\n", argv0);
5252
}
5353

@@ -345,7 +345,7 @@ main(
345345
exit(1);
346346
}
347347
mode = opt;
348-
if(opt == 'x' && *util_optarg == 'd') {
348+
if((opt == 'x' || mode == 'l') && *util_optarg == 'd') {
349349
version = ~0;
350350
}
351351
else if(opt != 'd') version = parse_version(util_optarg);
@@ -357,6 +357,40 @@ main(
357357
argc = ind;
358358
argv[argc] = NULL;
359359

360+
/* detect version */
361+
if(argc && (mode == 'x' || mode == 'l') && version == ~0) {
362+
thtk_io_t* file;
363+
if(!(file = thtk_io_open_file(argv[0], "rb", &error))) {
364+
print_error(error);
365+
thtk_error_free(&error);
366+
exit(1);
367+
}
368+
uint32_t out[4];
369+
unsigned int heur;
370+
printf("Detecting '%s'...\n",argv[0]);
371+
if(-1 == thdat_detect(argv[0], file, out, &heur, &error)) {
372+
thtk_io_close(file);
373+
print_error(error);
374+
thtk_error_free(&error);
375+
exit(1);
376+
}
377+
if(heur == -1) {
378+
const thdat_detect_entry_t* ent;
379+
printf("Couldn't detect version!\nPossible versions: ");
380+
while((ent = thdat_detect_iter(out))) {
381+
printf("%d,",ent->alias);
382+
}
383+
printf("\n");
384+
thtk_io_close(file);
385+
exit(1);
386+
}
387+
else {
388+
printf("Detected version %d\n",heur);
389+
version = heur;
390+
}
391+
thtk_io_close(file);
392+
}
393+
360394
switch (mode) {
361395
case 'd': {
362396
if (argc < 1) {
@@ -425,39 +459,6 @@ main(
425459
exit(1);
426460
}
427461

428-
if(version == ~0) {
429-
thtk_io_t* file;
430-
if(!(file = thtk_io_open_file(argv[0], "rb", &error))) {
431-
print_error(error);
432-
thtk_error_free(&error);
433-
exit(1);
434-
}
435-
uint32_t out[4];
436-
unsigned int heur;
437-
printf("Detecting '%s'...\n",argv[0]);
438-
if(-1 == thdat_detect(argv[0], file, out, &heur, &error)) {
439-
thtk_io_close(file);
440-
print_error(error);
441-
thtk_error_free(&error);
442-
exit(1);
443-
}
444-
if(heur == -1) {
445-
const thdat_detect_entry_t* ent;
446-
printf("Couldn't detect version!\nPossible versions: ");
447-
while((ent = thdat_detect_iter(out))) {
448-
printf("%d,",ent->alias);
449-
}
450-
printf("\n");
451-
thtk_io_close(file);
452-
exit(1);
453-
}
454-
else {
455-
printf("Detected version %d\n",heur);
456-
version = heur;
457-
}
458-
thtk_io_close(file);
459-
}
460-
461462
thdat_state_t* state = thdat_open_file(version, argv[0], &error);
462463
if (!state) {
463464
print_error(error);

0 commit comments

Comments
 (0)