beginning of rewrite of metaflac to the new metadata interface
[platform/upstream/flac.git] / src / metaflac / main.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 /*@@@
20 more powerful operations yet to add:
21         add a seektable, using same args as flac
22 */
23
24 #if HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include "FLAC/metadata.h"
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #if HAVE_GETOPT_LONG
35 #  include <getopt.h>
36 #else
37 #  include "share/getopt.h"
38 #endif
39
40 /* getopt format struct; note we don't use short options so we just
41    set the 'val' field to 1 everywhere to indicate a valid option. */
42 static struct option long_options_[] = {
43         /* global options */
44     { "preserve-modtime", 0, 0, 1 },
45     { "with-filename", 0, 0, 1 },
46     { "no-filename", 0, 0, 1 },
47     { "dont-use-padding", 0, 0, 1 },
48         /* shorthand operations */
49     { "show-md5sum", 0, 0, 1 },
50     { "show-min-blocksize", 0, 0, 1 },
51     { "show-max-blocksize", 0, 0, 1 },
52     { "show-min-framesize", 0, 0, 1 },
53     { "show-max-framesize", 0, 0, 1 },
54     { "show-sample-rate", 0, 0, 1 },
55     { "show-channels", 0, 0, 1 },
56     { "show-bps", 0, 0, 1 },
57     { "show-total-samples", 0, 0, 1 },
58     { "show-vc-vendor", 0, 0, 1 },
59     { "show-vc-field", 1, 0, 1 },
60     { "remove-vc-all", 0, 0, 1 },
61     { "remove-vc-field", 1, 0, 1 },
62     { "remove-vc-firstfield", 1, 0, 1 },
63     { "set-vc-field", 1, 0, 1 },
64         /* major operations */
65     { "list", 0, 0, 1 },
66     { "append", 0, 0, 1 },
67     { "remove", 0, 0, 1 },
68     { "remove-all", 0, 0, 1 },
69     { "merge-padding", 0, 0, 1 },
70     { "sort-padding", 0, 0, 1 },
71     { "block-number", 1, 0, 1 },
72     { "block-type", 1, 0, 1 },
73     { "except-block-type", 1, 0, 1 },
74     { "data-format", 1, 0, 1 },
75     { "application-data-format", 1, 0, 1 },
76     { "from-file", 1, 0, 1 },
77     {0, 0, 0, 0}
78 };
79
80 typedef enum {
81         OP__SHOW_MD5SUM,
82         OP__SHOW_MIN_BLOCKSIZE,
83         OP__SHOW_MAX_BLOCKSIZE,
84         OP__SHOW_MIN_FRAMESIZE,
85         OP__SHOW_MAX_FRAMESIZE,
86         OP__SHOW_SAMPLE_RATE,
87         OP__SHOW_CHANNELS,
88         OP__SHOW_BPS,
89         OP__SHOW_TOTAL_SAMPLES,
90         OP__SHOW_VC_VENDOR,
91         OP__SHOW_VC_FIELD,
92         OP__REMOVE_VC_FIELD,
93         OP__REMOVE_VC_FIRSTFIELD,
94         OP__SET_VC_FIELD,
95         OP__LIST,
96         OP__APPEND,
97         OP__REMOVE,
98         OP__REMOVE_ALL,
99         OP__MERGE_PADDING,
100         OP__SORT_PADDING,
101         OP__BLOCK_NUMBER,
102         OP__BLOCK_TYPE,
103         OP__EXCEPT_BLOCK_TYPE,
104         OP__DATA_FORMAT,
105         OP__APPLICATION_DATA_FORMAT,
106         OP__FROM_FILE
107 } OperationType;
108
109 typedef struct {
110         int dummy;
111 } Argument_VcFieldName;
112
113 typedef struct {
114         int dummy;
115 } Argument_VcField;
116
117 typedef struct {
118         int dummy;
119 } Argument_BlockNumber;
120
121 typedef struct {
122         int dummy;
123 } Argument_BlockType;
124
125 typedef struct {
126         int dummy;
127 } Argument_DataFormat;
128
129 typedef struct {
130         int dummy;
131 } Argument_ApplicationDataFormat;
132
133 typedef struct {
134         int dummy;
135 } Argument_FromFile;
136
137 typedef struct {
138         OperationType operation;
139         union {
140                 Argument_VcFieldName show_vc_field;
141                 Argument_VcFieldName remove_vc_field;
142                 Argument_VcFieldName remove_vc_firstfield;
143                 Argument_VcField set_vc_field;
144                 Argument_BlockNumber block_number;
145                 Argument_BlockType block_type;
146                 Argument_BlockType except_block_type;
147                 Argument_DataFormat data_format;
148                 Argument_ApplicationDataFormat application_data_format;
149                 Argument_FromFile from_file;
150         } argument;
151 } Operation;
152
153 typedef struct {
154         FLAC__bool preserve_modtime;
155         FLAC__bool prefix_with_filename;
156         FLAC__bool use_padding;
157         Operation *operations;
158         struct {
159                 FLAC__bool has_shorthand_op;
160                 FLAC__bool has_major_op;
161                 FLAC__bool has_block_type;
162                 FLAC__bool has_except_block_type;
163         } checks;
164 } CommandLineOptions;
165
166 static void parse_options(int argc, char *argv[], CommandLineOptions *options);
167 static int short_usage(const char *message, ...);
168 static int long_usage(const char *message, ...);
169 static void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent);
170
171 int main(int argc, char *argv[])
172 {
173         CommandLineOptions o;
174         parse_options(argc, argv, &o);
175         return 3;
176 }
177
178 void parse_options(int argc, char *argv[], CommandLineOptions *options)
179 {
180     int ret;
181     int option_index = 1;
182
183     while ((ret = getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
184         switch (ret) {
185             case 0:
186                 fprintf(stderr, "Internal error parsing command options\n");
187                 exit(1);
188                 break;
189             case 1:
190                 break;
191                         case '?':
192                 break;
193                         case ':':
194                 break;
195             default:
196                 /*@@@usage();*/
197                 exit(1);
198         }
199     }
200
201 #if 0
202     /* remaining bits must be the filenames */
203     if((param->mode == MODE_LIST && (argc-optind) != 1) ||
204        ((param->mode == MODE_WRITE || param->mode == MODE_APPEND) &&
205        ((argc-optind) < 1 || (argc-optind) > 2))) {
206             usage();
207             exit(1);
208     }
209
210     param->infilename = strdup(argv[optind]);
211     if (param->mode == MODE_WRITE || param->mode == MODE_APPEND)
212     {
213         if(argc-optind == 1)
214         {
215             param->tempoutfile = 1;
216             param->outfilename = malloc(strlen(param->infilename)+8);
217             strcpy(param->outfilename, param->infilename);
218             strcat(param->outfilename, ".vctemp");
219         }
220         else
221             param->outfilename = strdup(argv[optind+1]);
222     }
223 #endif
224 }
225
226 static void usage_header(FILE *out)
227 {
228         fprintf(out, "==============================================================================\n");
229         fprintf(out, "metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
230         fprintf(out, "Copyright (C) 2001,2002  Josh Coalson\n");
231         fprintf(out, "\n");
232         fprintf(out, "This program is free software; you can redistribute it and/or\n");
233         fprintf(out, "modify it under the terms of the GNU General Public License\n");
234         fprintf(out, "as published by the Free Software Foundation; either version 2\n");
235         fprintf(out, "of the License, or (at your option) any later version.\n");
236         fprintf(out, "\n");
237         fprintf(out, "This program is distributed in the hope that it will be useful,\n");
238         fprintf(out, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
239         fprintf(out, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
240         fprintf(out, "GNU General Public License for more details.\n");
241         fprintf(out, "\n");
242         fprintf(out, "You should have received a copy of the GNU General Public License\n");
243         fprintf(out, "along with this program; if not, write to the Free Software\n");
244         fprintf(out, "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n");
245         fprintf(out, "==============================================================================\n");
246 }
247
248 static void usage_summary(FILE *out)
249 {
250     fprintf(out, "Usage:\n");
251     fprintf(out, "  metaflac [options] [operations] FLACfile [FLACfile ...]\n");
252     fprintf(out, "\n");
253     fprintf(out, "Use metaflac to list, add, remove, or edit metadata in one or more FLAC files.\n");
254     fprintf(out, "You may perform one major operation, or many shorthand operations at a time.\n");
255     fprintf(out, "\n");
256     fprintf(out, "Options:\n");
257     fprintf(out, "--preserve-modtime    Preserve the original modification time in spite of edits\n");
258     fprintf(out, "--with-filename       Prefix each output line with the FLAC file name\n");
259     fprintf(out, "                      (the default if more than one FLAC file is specified)\n");
260     fprintf(out, "--no-filename         Do not prefix each output line with the FLAC file name\n");
261     fprintf(out, "                      (the default if only one FLAC file is specified)\n");
262     fprintf(out, "--dont-use-padding    By default metaflac tries to use padding where possible\n");
263     fprintf(out, "                      to avoid rewriting the entire file if the metadata size\n");
264     fprintf(out, "                      changes.  Use this option to tell metaflac to not use\n");
265     fprintf(out, "                      padding at all.\n");
266 }
267
268 int short_usage(const char *message, ...)
269 {
270         va_list args;
271
272         if(message) {
273                 va_start(args, message);
274
275                 (void) vfprintf(stderr, message, args);
276
277                 va_end(args);
278
279         }
280         usage_header(stderr);
281         fprintf(stderr, "\n");
282         fprintf(stderr, "This is the short help; for full help use 'metaflac --help'\n");
283         fprintf(stderr, "\n");
284         usage_summary(stderr);
285
286         return message? 1 : 0;
287 }
288
289 int long_usage(const char *message, ...)
290 {
291         FILE *out = (message? stderr : stdout);
292         va_list args;
293
294         if(message) {
295                 va_start(args, message);
296
297                 (void) vfprintf(stderr, message, args);
298
299                 va_end(args);
300
301         }
302         usage_header(out);
303     fprintf(out, "\n");
304         usage_summary(out);
305     fprintf(out, "\n");
306     fprintf(out, "Shorthand operations:\n");
307     fprintf(out, "--show-md5sum         Show the MD5 signature from the STREAMINFO block.\n");
308     fprintf(out, "--show-min-blocksize  Show the minimum block size from the STREAMINFO block.\n");
309     fprintf(out, "--show-max-blocksize  Show the maximum block size from the STREAMINFO block.\n");
310     fprintf(out, "--show-min-framesize  Show the minimum frame size from the STREAMINFO block.\n");
311     fprintf(out, "--show-max-framesize  Show the maximum frame size from the STREAMINFO block.\n");
312     fprintf(out, "--show-sample-rate    Show the sample rate from the STREAMINFO block.\n");
313     fprintf(out, "--show-channels       Show the number of channels from the STREAMINFO block.\n");
314     fprintf(out, "--show-bps            Show the # of bits per sample from the STREAMINFO block.\n");
315     fprintf(out, "--show-total-samples  Show the total # of samples from the STREAMINFO block.\n");
316     fprintf(out, "\n");
317     fprintf(out, "--show-vc-vendor      Show the vendor string from the VORBIS_COMMENT block.\n");
318     fprintf(out, "--show-vc-field=name  Show all Vorbis comment fields where the the field name\n");
319     fprintf(out, "                      matches 'name'.\n");
320     fprintf(out, "--remove-vc-field=name\n");
321     fprintf(out, "                      Remove all Vorbis comment fields whose field name is\n");
322     fprintf(out, "                      'name'.\n");
323     fprintf(out, "--remove-vc-firstfield=name\n");
324     fprintf(out, "                      Remove first Vorbis comment field whose field name is\n");
325     fprintf(out, "                      'name'.\n");
326     fprintf(out, "--remove-vc-all       Remove all Vorbis comment fields, leaving only the\n");
327     fprintf(out, "                      vendor string in the VORBIS_COMMENT block.\n");
328     fprintf(out, "--set-vc-field=field  Add a Vorbis comment field.  The field must comply with\n");
329     fprintf(out, "                      the Vorbis comment spec, of the form \"NAME=VALUE\".  If\n");
330     fprintf(out, "                      there is currently no VORBIS_COMMENT block, one will be\n");
331     fprintf(out, "                      created.\n");
332     fprintf(out, "\n");
333     fprintf(out, "Major operations:\n");
334     fprintf(out, "--list : List the contents of one or more metadata blocks to stdout.  By\n");
335     fprintf(out, "         default, all metadata blocks are listed in text format.  Use the\n");
336     fprintf(out, "         following options to change this behavior:\n");
337     fprintf(out, "\n");
338     fprintf(out, "    --block-number=#[,#[...]]\n");
339     fprintf(out, "    An optional comma-separated list of block numbers to display.  The first\n");
340     fprintf(out, "    block, the STREAMINFO block, is block 0.\n");
341     fprintf(out, "\n");
342     fprintf(out, "    --block-type=type[,type[...]]\n");
343     fprintf(out, "    --except-block-type=type[,type[...]]\n");
344     fprintf(out, "    An optional comma-separated list of block types to included or ignored\n");
345     fprintf(out, "    with this option.  Use only one of --block-type or --except-block-type.\n");
346     fprintf(out, "    The valid block types are: STREAMINFO, PADDING, APPLICATION, SEEKTABLE,\n");
347     fprintf(out, "    VORBIS_COMMENT.  You may narrow down the types of APPLICATION blocks\n");
348     fprintf(out, "    displayed as follows:\n");
349     fprintf(out, "        APPLICATION:abcd        The APPLICATION block(s) whose textual repre-\n");
350     fprintf(out, "                                sentation of the 4-byte ID is \"abcd\"\n");
351     fprintf(out, "        APPLICATION:0xXXXXXXXX  The APPLICATION block(s) whose hexadecimal big-\n");
352     fprintf(out, "                                endian representation of the 4-byte ID is\n");
353     fprintf(out, "                                \"0xXXXXXXXX\".  For the example \"abcd\" above the\n");
354     fprintf(out, "                                hexadecimal equivalalent is 0x61626364\n");
355     fprintf(out, "\n");
356     fprintf(out, "    NOTE: if both --block-number and --[except-]block-type are specified,\n");
357     fprintf(out, "          the result is the logical AND of both arguments.\n");
358     fprintf(out, "\n");
359 #if 0
360         /*@@@ not implemented yet */
361     fprintf(out, "    --data-format=binary|text\n");
362     fprintf(out, "    By default a human-readable text representation of the data is displayed.\n");
363     fprintf(out, "    You may specify --data-format=binary to dump the raw binary form of each\n");
364     fprintf(out, "    metadata block.  The output can be read in using a subsequent call to\n");
365     fprintf(out, "    "metaflac --append --from-file=..."\n");
366 #endif
367     fprintf(out, "\n");
368     fprintf(out, "    --application-data-format=hexdump|text\n");
369     fprintf(out, "    If the application block you are displaying contains binary data but your\n");
370     fprintf(out, "    --data-format=text, you can display a hex dump of the application data\n");
371     fprintf(out, "    contents instead using --application-data-format=hexdump\n");
372     fprintf(out, "\n");
373 #if 0
374         /*@@@ not implemented yet */
375     fprintf(out, "--append : Insert a metadata block from a file.  The input file must be in the\n");
376     fprintf(out, "           same format as generated with --list.\n");
377     fprintf(out, "\n");
378     fprintf(out, "    --block-number=#\n");
379     fprintf(out, "    Specify the insertion point (defaults to last block).  The new block will\n");
380     fprintf(out, "    be added after the given block number.  This prevents the illegal insertion\n");
381     fprintf(out, "    of a block before the first STREAMINFO block.  You may not --append another\n");
382     fprintf(out, "    STREAMINFO block.\n");
383     fprintf(out, "\n");
384     fprintf(out, "    --from-file=filename\n");
385     fprintf(out, "    Mandatory 'option' to specify the input file containing the block contents.\n");
386     fprintf(out, "\n");
387     fprintf(out, "    --data-format=binary|text\n");
388     fprintf(out, "    By default the block contents are assumed to be in binary format.  You can\n");
389     fprintf(out, "    override this by specifying --data-format=text\n");
390     fprintf(out, "\n");
391 #endif
392     fprintf(out, "--remove : Remove one or more metadata blocks from the metadata.  Unless\n");
393     fprintf(out, "           --dont-use-padding is specified, the blocks will be replaced with\n");
394     fprintf(out, "           padding.  You may not remove the STREAMINFO block.\n");
395     fprintf(out, "\n");
396     fprintf(out, "    --block-number=#[,#[...]]\n");
397     fprintf(out, "    --block-type=type[,type[...]]\n");
398     fprintf(out, "    --except-block-type=type[,type[...]]\n");
399     fprintf(out, "    See --list above for usage.\n");
400     fprintf(out, "\n");
401     fprintf(out, "    NOTE: if both --block-number and --[except-]block-type are specified,\n");
402     fprintf(out, "          the result is the logical AND of both arguments.\n");
403     fprintf(out, "\n");
404     fprintf(out, "--remove-all : Remove all metadata blocks (except the STREAMINFO block) from\n");
405     fprintf(out, "               the metadata.  Unless --dont-use-padding is specified, the\n");
406     fprintf(out, "               blocks will be replaced with padding.\n");
407     fprintf(out, "\n");
408     fprintf(out, "--merge-padding : Merge adjacent PADDING blocks into single blocks.\n");
409     fprintf(out, "\n");
410     fprintf(out, "--sort-padding : Move all PADDING blocks to the end of the metadata and merge\n");
411     fprintf(out, "                 them into a single block.\n");
412
413         return message? 1 : 0;
414 }
415
416 void hexdump(const FLAC__byte *buf, unsigned bytes, const char *indent)
417 {
418         unsigned i, left = bytes;
419         const FLAC__byte *b = buf;
420
421         for(i = 0; i < bytes; i += 16) {
422                 printf("%s%08X: "
423                         "%02X %02X %02X %02X %02X %02X %02X %02X "
424                         "%02X %02X %02X %02X %02X %02X %02X %02X "
425                         "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
426                         indent, i,
427                         left >  0? (unsigned char)b[ 0] : 0,
428                         left >  1? (unsigned char)b[ 1] : 0,
429                         left >  2? (unsigned char)b[ 2] : 0,
430                         left >  3? (unsigned char)b[ 3] : 0,
431                         left >  4? (unsigned char)b[ 4] : 0,
432                         left >  5? (unsigned char)b[ 5] : 0,
433                         left >  6? (unsigned char)b[ 6] : 0,
434                         left >  7? (unsigned char)b[ 7] : 0,
435                         left >  8? (unsigned char)b[ 8] : 0,
436                         left >  9? (unsigned char)b[ 9] : 0,
437                         left > 10? (unsigned char)b[10] : 0,
438                         left > 11? (unsigned char)b[11] : 0,
439                         left > 12? (unsigned char)b[12] : 0,
440                         left > 13? (unsigned char)b[13] : 0,
441                         left > 14? (unsigned char)b[14] : 0,
442                         left > 15? (unsigned char)b[15] : 0,
443                         (left >  0) ? (isprint(b[ 0]) ? b[ 0] : '.') : ' ',
444                         (left >  1) ? (isprint(b[ 1]) ? b[ 1] : '.') : ' ',
445                         (left >  2) ? (isprint(b[ 2]) ? b[ 2] : '.') : ' ',
446                         (left >  3) ? (isprint(b[ 3]) ? b[ 3] : '.') : ' ',
447                         (left >  4) ? (isprint(b[ 4]) ? b[ 4] : '.') : ' ',
448                         (left >  5) ? (isprint(b[ 5]) ? b[ 5] : '.') : ' ',
449                         (left >  6) ? (isprint(b[ 6]) ? b[ 6] : '.') : ' ',
450                         (left >  7) ? (isprint(b[ 7]) ? b[ 7] : '.') : ' ',
451                         (left >  8) ? (isprint(b[ 8]) ? b[ 8] : '.') : ' ',
452                         (left >  9) ? (isprint(b[ 9]) ? b[ 9] : '.') : ' ',
453                         (left > 10) ? (isprint(b[10]) ? b[10] : '.') : ' ',
454                         (left > 11) ? (isprint(b[11]) ? b[11] : '.') : ' ',
455                         (left > 12) ? (isprint(b[12]) ? b[12] : '.') : ' ',
456                         (left > 13) ? (isprint(b[13]) ? b[13] : '.') : ' ',
457                         (left > 14) ? (isprint(b[14]) ? b[14] : '.') : ' ',
458                         (left > 15) ? (isprint(b[15]) ? b[15] : '.') : ' '
459                 );
460                 left -= 16;
461                 b += 16;
462    }
463 }