File: tzxlist.c

package info (click to toggle)
fuse-emulator-utils 1.3.1-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,480 kB
  • ctags: 1,058
  • sloc: sh: 11,498; ansic: 10,101; cpp: 1,116; makefile: 158
file content (720 lines) | stat: -rw-r--r-- 21,840 bytes parent folder | download
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
/* tzxlist.c: Produce a listing of the blocks in a .tzx file
   Copyright (c) 2001-2016 Philip Kendall, Darren Salt, Fredrick Meunier
   Copyright (c) 2015 Sergio Baldovi
   Copyright (c) 2016 Gergely Szasz

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#ifdef HAVE_ICONV
#include <iconv.h>
#endif /* #ifdef HAVE_ICONV */
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif /* #ifdef HAVE_LOCALE_H */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include <libspectrum.h>

#include "utils.h"

#define PROGRAM_NAME "tzxlist"
#define DESCRIPTION_LENGTH 80

const char *progname;

static int dump_block = -9999;
static int block_num = 0;

#ifdef HAVE_ICONV
static iconv_t conversion_descriptor;
#endif /* #ifdef HAVE_ICONV */

static void
dump_symbol_table( libspectrum_tape_generalised_data_symbol_table *table )
{
  size_t i, j;

  libspectrum_byte pulses = 
    libspectrum_tape_generalised_data_symbol_table_max_pulses( table );
  libspectrum_word symbols =
    libspectrum_tape_generalised_data_symbol_table_symbols_in_table( table );

  printf("    Symbols in block: %ld\n", (unsigned long)libspectrum_tape_generalised_data_symbol_table_symbols_in_block( table ) );
  printf("    Max pulses: %d\n", pulses );
  printf("    Symbols in table: %d\n", symbols );

  for( i = 0; i < symbols; i++ ) {

    libspectrum_tape_generalised_data_symbol *symbol =
      libspectrum_tape_generalised_data_symbol_table_symbol( table, i );

    printf("      Symbol %lu: type %d; pulse lengths: ", (unsigned long)i, libspectrum_tape_generalised_data_symbol_type( symbol ) );

    for( j = 0; j < pulses; j++ )
      printf( "%d ", libspectrum_tape_generalised_data_symbol_pulse( symbol, j ) );

    printf( "\n" );

  }
    
}

static void
dump_pilot_repeats( libspectrum_tape_block *block )
{
  size_t i;

  libspectrum_tape_generalised_data_symbol_table *pilot =
    libspectrum_tape_block_pilot_table( block );

  for( i = 0; i < libspectrum_tape_generalised_data_symbol_table_symbols_in_block( pilot ); i++ ) {
    printf( "    Repeat %2lu: symbol %d repeated %d times\n", (unsigned long)i,
	    libspectrum_tape_block_pilot_symbols( block, i ),
	    libspectrum_tape_block_pilot_repeats( block, i ) );
  }
}

static const char*
hardware_desc( int type, int id )
{
  switch( type ) {
  case 0:
    switch( id ) {
    case 0: return "16K Spectrum";
    case 1: return "48K Spectrum/Spectrum +";
    case 2: return "48K Spectrum (Issue 1)";
    case 3: return "128K Spectrum";
    case 4: return "Spectrum +2";
    case 5: return "Spectrum +2A/+3";
    default: return "Unknown machine";
    }
  case 3:
    switch( id ) {
    case 0: return "AY-3-8192";
    default: return "Unknown sound device";
    }
  case 4:
    switch( id ) {
    case 0: return "Kempston joystick";
    case 1: return "Cursor/Protek/AGF joystick";
    case 2: return "Sinclair joystick (Left)";
    case 3: return "Sinclair joystick (Right)";
    case 4: return "Fuller joystick";
    default: return "Unknown joystick";
    }
  default: return "Unknown type";
  }
}

/* A simple routine to convert the input text to another character encoding.
   We really are only thinking about simple conversions of accented characters,
   the pound and Euro signs so just reserve 3x the input buffer size to give
   "plenty" of space for the most common encondings and let any failure in
   conversion fall back to just returning the source string unmodified */
static char *
convert_charset( char *input )
{
#ifdef HAVE_ICONV
  const int expansion_factor = 3;
  static char *output = NULL;
  static size_t output_size = 0;
  char* output_ptr = output;
  size_t output_left = output_size;
  ICONV_CONST char* input_ptr = input;
  size_t input_left = strlen( input ) + 1;
  size_t min_output_size = input_left * expansion_factor;
 
  if( conversion_descriptor == (iconv_t)(-1) ) return input;

  if( output_size == 0 || output_size < min_output_size ) {
    output = realloc( output, min_output_size );
    if( output == NULL ) {
      output_size = 0;
      return input;
    }
    output_ptr = output;
    output_size = input_left * expansion_factor;
    output_left = output_size;
    *output = '\0';
  } 

  if( (size_t)(-1) == iconv( conversion_descriptor, &input_ptr, &input_left,
                             &output_ptr, &output_left ) ) {
    return input;
  }

  /* Write the byte sequence to get into the initial state if required. */
  if( (size_t)(-1) == iconv( conversion_descriptor, NULL, NULL, &output_ptr,
                             &output_left ) ) {
    return input;
  }

  return output;
#else  /* #ifdef HAVE_ICONV */
  return input;
#endif /* #ifdef HAVE_ICONV */
}

static void
check_checksum(unsigned long length, libspectrum_byte * data)
{
  int retval = 0;
  libspectrum_byte checksum = 0;
  if( length ) {
    size_t i;
    for( i = 0; i < length-1; i++ ) {
      checksum ^= data[i];
    }
    retval = checksum == data[length-1];
  }

  printf("  Checksum: %s\n", (retval ? "pass" : "fail"));
}

static void
fprint_block_name( FILE *f, libspectrum_byte *data )
{
  int i;
  for( i = 2; i < 12; i++) {
    char c;
    if( data[i] >= 32 && data[i] < 127 ) {
      c = data[i];
    } else {
      c = '?';
    }
    fprintf( f, "%c", c );
  }
}

static void
print_block_name( libspectrum_byte * data )
{
  fprint_block_name( stdout, data );
}

/* decode header and save data */
static void
decode_header( libspectrum_tape_block *block )
{
  unsigned long length = libspectrum_tape_block_data_length( block );
  libspectrum_byte * data = libspectrum_tape_block_data( block );
  int is_header = length == 19 && data[0] == 0x00;
  char filename[17]; /* %8d.dsc; %8d.hdr %8d.dat */
  int save = 0;
  static int save_data = -9999;
  FILE *f = NULL;
  if(is_header) {
    save = ( dump_block == -1 || dump_block == block_num ) ? 1 : 0;
    if( save ) {
      snprintf( filename, 16, "%08d.dsc", block_num );
      f = fopen( filename, "wb" );
      if( !f ) {
        fprintf( stderr, "%s: couldn't open '%s': %s\n", progname, filename,
          strerror( errno ) );
      }
    }
    switch( data[1] ) {
    case 0:
      printf("  Program: ");
      print_block_name( data );
      if( f ) {
        fprintf( f, "Program: " );
        fprint_block_name( f, data );
      }
      break;
    case 1:
      printf("  Number Array: ");
      print_block_name( data );
      if( f ) {
        fprintf( f, "Number Array: " );
        fprint_block_name( f, data );
      }
      break;
    case 2:
      printf("  Character Array: ");
      print_block_name( data );
      if( f ) {
        fprintf( f, "Character Array: " );
        fprint_block_name( f, data );
      }
      break;
    case 3:
      printf("  CODE: ");
      print_block_name( data );
      printf("\n  Start: %d\n", data[14] + data[15] * 0x100 );
      printf("  Length: %d", data[12] + data[13] * 0x100 );
      if( f ) {
        fprintf( f, "CODE: " );
        fprint_block_name( f, data );
        fprintf( f, "\nStart: %d\n", data[14] + data[15] * 0x100 );
        fprintf( f, "Length: %d", data[12] + data[13] * 0x100 );
      }
      break;
    default:
      printf("  Unknown");
      if( f ) {
        fprintf( f, "Unknown" );
      }
      break;
    }
    printf("\n");
    if( f ) {
      fprintf( f, "\n" );
      fclose( f );
    }
  }
  check_checksum(length, data);

  /* save data block */
  if( save || save_data == block_num - 1 || dump_block == block_num ) {

    snprintf( filename, 16, "%08d.%s",
              save || dump_block == block_num ? block_num : save_data,
              save ? "hdr" : "dat" );
    f = fopen( filename, "wb" );
    if( !f ) {
      fprintf( stderr, "%s: couldn't open '%s': %s\n", progname, filename,
               strerror( errno ) );
    } else if( fwrite( data + 1, 1, length - 2, f ) != length - 2 ) {
      fprintf( stderr, "%s: error writing to '%s'\n", progname, filename );
      fclose( f );
    } else {
      fclose( f );
      printf( "* Tape block dumped\n" );
    }
  }
  save_data = save ? block_num : -9999;
}

static int
process_tape( char *filename )
{
  int error;

  unsigned char *buffer; size_t length;
  libspectrum_tape *tape;
  libspectrum_tape_iterator iterator;
  libspectrum_tape_block *block;
  libspectrum_qword total_length = 0;
  libspectrum_id_t type;

  size_t i;

  error = read_file( filename, &buffer, &length ); if( error ) return error;

  error = libspectrum_identify_file( &type, filename, buffer, length );
  if( error != LIBSPECTRUM_ERROR_NONE ) {
    free( buffer );
    return error;
  }

#ifdef HAVE_ICONV
  /* Looks like GNU libc iconv needs to have the character type locale set
     to do the current locale target thing (but oddly GNU libiconv doesn't) */
#ifdef HAVE_SETLOCALE
  setlocale(LC_CTYPE, "");
#endif /* #ifdef HAVE_SETLOCALE */
  switch( type ) {
  case LIBSPECTRUM_ID_TAPE_TZX:
    /* Convert from Windows code page 1252 to our current locale.
       CP1252 is a valid name of the Windows-1252 charset used by WoS, Tapir and
       ZX-Blockeditor on at least Linux, MacOS X and Solaris 9. If we need to
       use another string on another OS we will need to do a little more work */
    conversion_descriptor = iconv_open("", "CP1252");
    break;
  case LIBSPECTRUM_ID_TAPE_PZX:
    conversion_descriptor = iconv_open("", "UTF-8");
    break;
  default:
    conversion_descriptor = (iconv_t)(-1);
    break;
  }
#endif /* #ifdef HAVE_ICONV */

  tape = libspectrum_tape_alloc();

  error = libspectrum_tape_read( tape, buffer, length, LIBSPECTRUM_ID_UNKNOWN,
                                 filename );
  if( error != LIBSPECTRUM_ERROR_NONE ) {
    free( buffer );
    return error;
  }

  free( buffer );

  printf("Listing of `%s':\n\n", filename );

  block = libspectrum_tape_iterator_init( &iterator, tape );

  while( block ) {
    char description[ DESCRIPTION_LENGTH ];

    error =
      libspectrum_tape_block_description( description, DESCRIPTION_LENGTH,
					  block );
    if( error ) return 1;
    printf( "--= Block #%d =--\n", block_num );
    printf( "Block type 0x%02x (%s)\n", libspectrum_tape_block_type( block ),
	    description );
    printf("  Block duration: %.2f sec\n",
           libspectrum_tape_block_length( block )/3500000.0 );
    total_length += libspectrum_tape_block_length( block );

    switch( libspectrum_tape_block_type( block ) ) {

    case LIBSPECTRUM_TAPE_BLOCK_ROM:
      {
        printf("  Data length: %ld bytes\n",
               (unsigned long)libspectrum_tape_block_data_length( block ) );
        decode_header( block );
        printf("  Pause length: %d ms\n",
               libspectrum_tape_block_pause( block ) );
      }
      break;

    case LIBSPECTRUM_TAPE_BLOCK_TURBO:
      printf("  %ld pilot pulses of %d tstates\n",
	     (unsigned long)libspectrum_tape_block_pilot_pulses( block ),
	     libspectrum_tape_block_pilot_length( block ) );
      printf("  Sync pulses of %d and %d tstates\n",
	     libspectrum_tape_block_sync1_length( block ),
	     libspectrum_tape_block_sync2_length( block ) );
      /* Fall through */

    case LIBSPECTRUM_TAPE_BLOCK_PURE_DATA:
      printf("  Data bits are %d (reset) and %d (set) tstates\n",
	     libspectrum_tape_block_bit0_length( block ),
	     libspectrum_tape_block_bit1_length( block ) );
      printf("  Data length: %ld bytes (%ld bits in last byte used)\n",
	     (unsigned long)libspectrum_tape_block_data_length( block ),
	     libspectrum_tape_block_bits_in_last_byte(block) ? 
               (unsigned long)libspectrum_tape_block_bits_in_last_byte(block) :
               8 );
      printf("  Pause length: %d ms\n",
	     libspectrum_tape_block_pause( block ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_PURE_TONE:
      printf("  %ld pulses of %ld tstates\n",
	     (unsigned long)libspectrum_tape_block_count( block ),
	     (unsigned long)libspectrum_tape_block_pulse_length( block ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_PULSES:
      for( i=0; i < libspectrum_tape_block_count( block ); i++ )
	printf("  Pulse %3ld: length %d tstates\n",
	       (unsigned long)i,
	       libspectrum_tape_block_pulse_lengths( block, i ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_RAW_DATA:
      printf("  Length: %ld bytes\n", (unsigned long)
	     libspectrum_tape_block_data_length( block ) );
      printf("  Bits in last byte: %ld\n",
	     (unsigned long)libspectrum_tape_block_bits_in_last_byte(block) );
      printf("  Each bit is %d tstates\n",
	     libspectrum_tape_block_bit_length( block ) );
      printf("  Pause length: %d ms\n",
	     libspectrum_tape_block_pause( block ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_GENERALISED_DATA:
      printf("  Pause length: %d ms\n", libspectrum_tape_block_pause( block ) );
      printf("  Pilot table:\n");
      dump_symbol_table( libspectrum_tape_block_pilot_table( block ) );
      dump_pilot_repeats( block );
      printf("  Data table:\n");
      dump_symbol_table( libspectrum_tape_block_data_table( block ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_PAUSE:
      printf( "  Initial polarity: " );
      if( libspectrum_tape_block_level( block ) < 0 ) {
        printf( "none specified\n" );
      } else {
        printf( "%s\n",
                libspectrum_tape_block_level( block ) ? "high" : "low" );
      }
      printf("  Length: %d ms\n", libspectrum_tape_block_pause( block ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_GROUP_START:
      printf("  Name: %s\n", 
                    convert_charset( libspectrum_tape_block_text( block ) ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_GROUP_END:
    case LIBSPECTRUM_TAPE_BLOCK_LOOP_END:
    case LIBSPECTRUM_TAPE_BLOCK_STOP48:
      /* Do nothing */
      break;

    case LIBSPECTRUM_TAPE_BLOCK_SET_SIGNAL_LEVEL:
      printf( "  Polarity: %s\n",
                libspectrum_tape_block_level( block ) ? "high" : "low" );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_JUMP:
      printf("  Offset: %d\n", libspectrum_tape_block_offset( block ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_LOOP_START:
      printf("  Count: %lu\n",
	     (unsigned long)libspectrum_tape_block_count( block ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_SELECT:
      for( i = 0; i < libspectrum_tape_block_count( block ); i++ ) {
	printf("  Choice %2ld: Offset %d: %s\n", (unsigned long)i,
	       libspectrum_tape_block_offsets( block, i ),
               convert_charset( libspectrum_tape_block_texts( block, i ) ) );
      }
      break;


    case LIBSPECTRUM_TAPE_BLOCK_MESSAGE:
      printf("  Display for %d seconds\n",
	     libspectrum_tape_block_pause( block ) );
      /* Fall through */

    case LIBSPECTRUM_TAPE_BLOCK_COMMENT:
      printf("  Comment: %s\n", 
               convert_charset( libspectrum_tape_block_text( block ) ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_ARCHIVE_INFO:
      for( i = 0; i < libspectrum_tape_block_count( block ); i++ ) {
	printf("  ");
	switch( libspectrum_tape_block_ids( block, i ) ) {
	case   0: printf("Full Title:"); break;
	case   1: printf(" Publisher:"); break;
	case   2: printf("    Author:"); break;
	case   3: printf("      Year:"); break;
	case   4: printf("  Language:"); break;
	case   5: printf("  Category:"); break;
	case   6: printf("     Price:"); break;
	case   7: printf("    Loader:"); break;
	case   8: printf("    Origin:"); break;
	case 255: printf("   Comment:"); break;
        default: printf("(Unknown string): "); break;
	}
	printf(" %s\n", 
               convert_charset( libspectrum_tape_block_texts( block, i ) ));
      }
      break;

    case LIBSPECTRUM_TAPE_BLOCK_HARDWARE:
      for( i = 0; i < libspectrum_tape_block_count( block ); i++ ) {
	printf( "  %s: ",
	        hardware_desc( libspectrum_tape_block_types( block, i ),
			       libspectrum_tape_block_ids( block, i )
			     )
	      );
	switch( libspectrum_tape_block_values( block, i ) ) {
	case 0: printf("runs"); break;
	case 1: printf("runs, using hardware"); break;
	case 2: printf("runs, does not use hardware"); break;
	case 3: printf("does not run"); break;
	}
	printf("\n");
      }
      break;

    case LIBSPECTRUM_TAPE_BLOCK_CUSTOM:
      printf( "  Description: %s\n", 
                    convert_charset( libspectrum_tape_block_text( block ) ) );
      printf( "       Length: %ld bytes\n",
	      (unsigned long)libspectrum_tape_block_data_length( block ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_PULSE_SEQUENCE:
      printf( "  Initial polarity: low\n" );
      for( i=0; i < libspectrum_tape_block_count( block ); i++ )
        printf("  Pulse %3ld: length %4d tstates, repeated %4ld times\n",
             (unsigned long)i,
             libspectrum_tape_block_pulse_lengths( block, i ),
             (unsigned long)libspectrum_tape_block_pulse_repeats( block, i ) );
      break;

    case LIBSPECTRUM_TAPE_BLOCK_DATA_BLOCK:
      printf( "  Initial polarity: " );
      if( libspectrum_tape_block_level( block ) < 0 ) {
        printf( "none specified\n" );
      } else {
        printf( "%s\n",
                libspectrum_tape_block_level( block ) ? "high" : "low" );
      }
      printf("  Reset data bits are\n");
      for( i=0; i < libspectrum_tape_block_bit0_pulse_count( block ); i++ )
	printf("    Pulse %3ld: length %4d tstates\n",
             (unsigned long)i,
             libspectrum_tape_block_bit0_pulses( block, i ) );
      printf("  Set data bits are\n");
      for( i=0; i < libspectrum_tape_block_bit1_pulse_count( block ); i++ )
	printf("    Pulse %3ld: length %4d tstates\n",
             (unsigned long)i,
             libspectrum_tape_block_bit1_pulses( block, i ) );
      printf("  Data length: %ld bytes (%ld bits in last byte used)\n",
	     (unsigned long)libspectrum_tape_block_data_length( block ),
	     libspectrum_tape_block_bits_in_last_byte(block) ? 
               (unsigned long)libspectrum_tape_block_bits_in_last_byte(block) :
               8 );
      printf("  Tail length: %d tstates\n",
	     libspectrum_tape_block_tail_length( block ) );
      decode_header( block );
      break;

    default:
      printf("  (Sorry -- %s can't handle that kind of block. Skipping it)\n",
	     progname );
      break;
    }

    block = libspectrum_tape_iterator_next( &iterator );
    printf( "\n" );
    block_num++;

  }

  printf( "Total tape duration: %.2f sec\n", total_length/3500000.0 );

  error = libspectrum_tape_free( tape );
  if( error != LIBSPECTRUM_ERROR_NONE ) return error;

#ifdef HAVE_ICONV
  if( conversion_descriptor != (iconv_t)(-1) ) {
    error = iconv_close( conversion_descriptor ); if( error ) return error;
  }
#endif /* #ifdef HAVE_ICONV */

  return 0;
}

static void
show_version( void )
{
  printf(
    PROGRAM_NAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
    "Copyright (c) 2001-2011 Philip Kendall, Darren Salt, Fredrick Meunier\n"
    "License GPLv2+: GNU GPL version 2 or later "
    "<http://gnu.org/licenses/gpl.html>\n"
    "This is free software: you are free to change and redistribute it.\n"
    "There is NO WARRANTY, to the extent permitted by law.\n" );
}

static void
show_help( void )
{
  printf(
    "Usage: %s [OPTION] <tapefile>...\n"
    "Outputs a description of the contents of TZX, TAP, PZX and Warajevo tape files.\n"
    "\n"
    "Options:\n"
    "  -d <num>       Dump standard block (header and data) number <num>.\n"
    "                   Use -1 to dump all blocks.\n"
    "  -h, --help     Display this help and exit.\n"
    "  -V, --version  Output version information and exit.\n"
    "\n"
    "Report %s bugs to <%s>\n"
    "%s home page: <%s>\n"
    "For complete documentation, see the manual page of %s.\n",
    progname,
    PROGRAM_NAME, PACKAGE_BUGREPORT, PACKAGE_NAME, PACKAGE_URL, PROGRAM_NAME
  );
}

int
main( int argc, char **argv )
{
  int ret = 0;
  int i;
  int c, error = 0;

  struct option long_options[] = {
    { "help", 0, NULL, 'h' },
    { "version", 0, NULL, 'V' },
    { 0, 0, 0, 0 }
  };

  progname = argv[0];

  while( ( c = getopt_long( argc, argv, "d:hV", long_options, NULL ) ) != -1 ) {

    switch( c ) {

    case 'd':
      dump_block = atoi( optarg );
      break;

    case 'h':
      show_help();
      return 0;

    case 'V':
      show_version();
      return 0;

    case '?':
      /* getopt prints an error message to stderr */
      error = 1;
      break;

    default:
      error = 1;
      fprintf( stderr, "%s: unknown option `%c'\n", progname, (char) c );
      break;

    }
  }

  argc -= optind;
  argv += optind;

  if( error ) {
    fprintf( stderr, "Try `%s --help' for more information.\n", progname );
    return error;
  }

  if( argc < 1 ) {
    fprintf( stderr, "%s: usage: %s [OPTION] <tape files>...\n", progname,
             progname );
    fprintf( stderr, "Try `%s --help' for more information.\n", progname );
    return 1;
  }

  error = init_libspectrum(); if( error ) return error;


  for( i = 0; i < argc; i++ ) {
    ret |= process_tape( argv[i] );
  }

  return ret;
}