1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004,2005,2006 Josh Coalson
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.
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.
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.
26 #include "FLAC/assert.h"
27 #include "share/grabbag/replaygain.h"
34 share__getopt format struct; note we don't use short options so we just
35 set the 'val' field to 0 everywhere to indicate a valid option.
37 struct share__option long_options_[] = {
39 { "preserve-modtime", 0, 0, 0 },
40 { "with-filename", 0, 0, 0 },
41 { "no-filename", 0, 0, 0 },
42 { "no-utf8-convert", 0, 0, 0 },
43 { "dont-use-padding", 0, 0, 0 },
44 { "no-cued-seekpoints", 0, 0, 0 },
45 /* shorthand operations */
46 { "show-md5sum", 0, 0, 0 },
47 { "show-min-blocksize", 0, 0, 0 },
48 { "show-max-blocksize", 0, 0, 0 },
49 { "show-min-framesize", 0, 0, 0 },
50 { "show-max-framesize", 0, 0, 0 },
51 { "show-sample-rate", 0, 0, 0 },
52 { "show-channels", 0, 0, 0 },
53 { "show-bps", 0, 0, 0 },
54 { "show-total-samples", 0, 0, 0 },
55 { "set-md5sum", 1, 0, 0 }, /* undocumented */
56 { "set-min-blocksize", 1, 0, 0 }, /* undocumented */
57 { "set-max-blocksize", 1, 0, 0 }, /* undocumented */
58 { "set-min-framesize", 1, 0, 0 }, /* undocumented */
59 { "set-max-framesize", 1, 0, 0 }, /* undocumented */
60 { "set-sample-rate", 1, 0, 0 }, /* undocumented */
61 { "set-channels", 1, 0, 0 }, /* undocumented */
62 { "set-bps", 1, 0, 0 }, /* undocumented */
63 { "set-total-samples", 1, 0, 0 }, /* undocumented */ /* WATCHOUT: used by test/test_flac.sh on windows */
64 { "show-vendor-tag", 0, 0, 0 },
65 { "show-tag", 1, 0, 0 },
66 { "remove-all-tags", 0, 0, 0 },
67 { "remove-tag", 1, 0, 0 },
68 { "remove-first-tag", 1, 0, 0 },
69 { "set-tag", 1, 0, 0 },
70 { "set-tag-from-file", 1, 0, 0 },
71 { "import-tags-from", 1, 0, 0 },
72 { "export-tags-to", 1, 0, 0 },
73 { "show-vc-vendor", 0, 0, 0 }, /* deprecated */
74 { "show-vc-field", 1, 0, 0 }, /* deprecated */
75 { "remove-vc-all", 0, 0, 0 }, /* deprecated */
76 { "remove-vc-field", 1, 0, 0 }, /* deprecated */
77 { "remove-vc-firstfield", 1, 0, 0 }, /* deprecated */
78 { "set-vc-field", 1, 0, 0 }, /* deprecated */
79 { "import-vc-from", 1, 0, 0 }, /* deprecated */
80 { "export-vc-to", 1, 0, 0 }, /* deprecated */
81 { "import-cuesheet-from", 1, 0, 0 },
82 { "export-cuesheet-to", 1, 0, 0 },
83 { "add-seekpoint", 1, 0, 0 },
84 { "add-replay-gain", 0, 0, 0 },
85 { "remove-replay-gain", 0, 0, 0 },
86 { "add-padding", 1, 0, 0 },
87 /* major operations */
89 { "version", 0, 0, 0 },
91 { "append", 0, 0, 0 },
92 { "remove", 0, 0, 0 },
93 { "remove-all", 0, 0, 0 },
94 { "merge-padding", 0, 0, 0 },
95 { "sort-padding", 0, 0, 0 },
96 /* major operation arguments */
97 { "block-number", 1, 0, 0 },
98 { "block-type", 1, 0, 0 },
99 { "except-block-type", 1, 0, 0 },
100 { "data-format", 1, 0, 0 },
101 { "application-data-format", 1, 0, 0 },
102 { "from-file", 1, 0, 0 },
106 static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options);
107 static void append_new_operation(CommandLineOptions *options, Operation operation);
108 static void append_new_argument(CommandLineOptions *options, Argument argument);
109 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
110 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
111 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
112 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
113 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
114 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
115 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
116 static FLAC__bool parse_filename(const char *src, char **dest);
117 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
118 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
119 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
120 static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out);
121 static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out);
122 static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out);
123 static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out);
124 static void undocumented_warning(const char *opt);
127 void init_options(CommandLineOptions *options)
129 options->preserve_modtime = false;
131 /* '2' is a hack to mean "use default if not forced on command line" */
132 FLAC__ASSERT(true != 2);
133 options->prefix_with_filename = 2;
135 options->utf8_convert = true;
136 options->use_padding = true;
137 options->cued_seekpoints = true;
138 options->show_long_help = false;
139 options->show_version = false;
140 options->application_data_format_is_hexdump = false;
142 options->ops.operations = 0;
143 options->ops.num_operations = 0;
144 options->ops.capacity = 0;
146 options->args.arguments = 0;
147 options->args.num_arguments = 0;
148 options->args.capacity = 0;
150 options->args.checks.num_shorthand_ops = 0;
151 options->args.checks.num_major_ops = 0;
152 options->args.checks.has_block_type = false;
153 options->args.checks.has_except_block_type = false;
155 options->num_files = 0;
156 options->filenames = 0;
159 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
162 int option_index = 1;
163 FLAC__bool had_error = false;
165 while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
168 had_error |= !parse_option(option_index, share__optarg, options);
180 if(options->prefix_with_filename == 2)
181 options->prefix_with_filename = (argc - share__optind > 1);
183 if(share__optind >= argc && !options->show_long_help && !options->show_version) {
184 fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
185 fprintf(stderr," metaflac cannot be used as a pipe\n");
189 options->num_files = argc - share__optind;
191 if(options->num_files > 0) {
193 if(0 == (options->filenames = (char**)malloc(sizeof(char*) * options->num_files)))
194 die("out of memory allocating space for file names list");
195 while(share__optind < argc)
196 options->filenames[i++] = local_strdup(argv[share__optind++]);
199 if(options->args.checks.num_major_ops > 0) {
200 if(options->args.checks.num_major_ops > 1) {
201 fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
204 else if(options->args.checks.num_shorthand_ops > 0) {
205 fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
210 /* check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to */
211 if((0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) || 0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) && options->num_files > 1) {
212 fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from' or '--export-cuesheet-to'\n");
216 if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
217 fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
225 * We need to create an OP__ADD_SEEKPOINT operation if there is
226 * not one already, and --import-cuesheet-from was specified but
227 * --no-cued-seekpoints was not:
229 if(options->cued_seekpoints) {
230 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
232 Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
234 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
235 op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
242 void free_options(CommandLineOptions *options)
248 FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
249 FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
251 for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
253 case OP__SHOW_VC_FIELD:
254 case OP__REMOVE_VC_FIELD:
255 case OP__REMOVE_VC_FIRSTFIELD:
256 if(0 != op->argument.vc_field_name.value)
257 free(op->argument.vc_field_name.value);
259 case OP__SET_VC_FIELD:
260 if(0 != op->argument.vc_field.field)
261 free(op->argument.vc_field.field);
262 if(0 != op->argument.vc_field.field_name)
263 free(op->argument.vc_field.field_name);
264 if(0 != op->argument.vc_field.field_value)
265 free(op->argument.vc_field.field_value);
267 case OP__IMPORT_VC_FROM:
268 case OP__EXPORT_VC_TO:
269 case OP__EXPORT_CUESHEET_TO:
270 if(0 != op->argument.filename.value)
271 free(op->argument.filename.value);
273 case OP__IMPORT_CUESHEET_FROM:
274 if(0 != op->argument.import_cuesheet_from.filename)
275 free(op->argument.import_cuesheet_from.filename);
277 case OP__ADD_SEEKPOINT:
278 if(0 != op->argument.add_seekpoint.specification)
279 free(op->argument.add_seekpoint.specification);
286 for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
288 case ARG__BLOCK_NUMBER:
289 if(0 != arg->value.block_number.entries)
290 free(arg->value.block_number.entries);
292 case ARG__BLOCK_TYPE:
293 case ARG__EXCEPT_BLOCK_TYPE:
294 if(0 != arg->value.block_type.entries)
295 free(arg->value.block_type.entries);
298 if(0 != arg->value.from_file.file_name)
299 free(arg->value.from_file.file_name);
306 if(0 != options->ops.operations)
307 free(options->ops.operations);
309 if(0 != options->args.arguments)
310 free(options->args.arguments);
312 if(0 != options->filenames) {
313 for(i = 0; i < options->num_files; i++) {
314 if(0 != options->filenames[i])
315 free(options->filenames[i]);
317 free(options->filenames);
325 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
327 const char *opt = long_options_[option_index].name;
330 FLAC__bool ok = true;
332 if(0 == strcmp(opt, "preserve-modtime")) {
333 options->preserve_modtime = true;
335 else if(0 == strcmp(opt, "with-filename")) {
336 options->prefix_with_filename = true;
338 else if(0 == strcmp(opt, "no-filename")) {
339 options->prefix_with_filename = false;
341 else if(0 == strcmp(opt, "no-utf8-convert")) {
342 options->utf8_convert = false;
344 else if(0 == strcmp(opt, "dont-use-padding")) {
345 options->use_padding = false;
347 else if(0 == strcmp(opt, "no-cued-seekpoints")) {
348 options->cued_seekpoints = false;
350 else if(0 == strcmp(opt, "show-md5sum")) {
351 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
353 else if(0 == strcmp(opt, "show-min-blocksize")) {
354 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
356 else if(0 == strcmp(opt, "show-max-blocksize")) {
357 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
359 else if(0 == strcmp(opt, "show-min-framesize")) {
360 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
362 else if(0 == strcmp(opt, "show-max-framesize")) {
363 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
365 else if(0 == strcmp(opt, "show-sample-rate")) {
366 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
368 else if(0 == strcmp(opt, "show-channels")) {
369 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
371 else if(0 == strcmp(opt, "show-bps")) {
372 (void) append_shorthand_operation(options, OP__SHOW_BPS);
374 else if(0 == strcmp(opt, "show-total-samples")) {
375 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
377 else if(0 == strcmp(opt, "set-md5sum")) {
378 op = append_shorthand_operation(options, OP__SET_MD5SUM);
379 FLAC__ASSERT(0 != option_argument);
380 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
381 fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
385 undocumented_warning(opt);
387 else if(0 == strcmp(opt, "set-min-blocksize")) {
388 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
389 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
390 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
394 undocumented_warning(opt);
396 else if(0 == strcmp(opt, "set-max-blocksize")) {
397 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
398 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
399 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
403 undocumented_warning(opt);
405 else if(0 == strcmp(opt, "set-min-framesize")) {
406 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
407 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
408 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
412 undocumented_warning(opt);
414 else if(0 == strcmp(opt, "set-max-framesize")) {
415 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
416 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
417 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
421 undocumented_warning(opt);
423 else if(0 == strcmp(opt, "set-sample-rate")) {
424 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
425 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
426 fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
430 undocumented_warning(opt);
432 else if(0 == strcmp(opt, "set-channels")) {
433 op = append_shorthand_operation(options, OP__SET_CHANNELS);
434 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
435 fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
439 undocumented_warning(opt);
441 else if(0 == strcmp(opt, "set-bps")) {
442 op = append_shorthand_operation(options, OP__SET_BPS);
443 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BITS_PER_SAMPLE || op->argument.streaminfo_uint32.value > FLAC__MAX_BITS_PER_SAMPLE) {
444 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
448 undocumented_warning(opt);
450 else if(0 == strcmp(opt, "set-total-samples")) {
451 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
452 if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
453 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
457 undocumented_warning(opt);
459 else if(0 == strcmp(opt, "show-vendor-tag") || 0 == strcmp(opt, "show-vc-vendor")) {
460 if(0 == strcmp(opt, "show-vc-vendor"))
461 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --show-vendor-tag\n", opt);
462 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
464 else if(0 == strcmp(opt, "show-tag") || 0 == strcmp(opt, "show-vc-field")) {
465 const char *violation;
466 if(0 == strcmp(opt, "show-vc-field"))
467 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --show-tag\n", opt);
468 op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
469 FLAC__ASSERT(0 != option_argument);
470 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
471 FLAC__ASSERT(0 != violation);
472 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
476 else if(0 == strcmp(opt, "remove-all-tags") || 0 == strcmp(opt, "remove-vc-all")) {
477 if(0 == strcmp(opt, "remove-vc-all"))
478 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-all-tags\n", opt);
479 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
481 else if(0 == strcmp(opt, "remove-tag") || 0 == strcmp(opt, "remove-vc-field")) {
482 const char *violation;
483 if(0 == strcmp(opt, "remove-vc-field"))
484 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-tag\n", opt);
485 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
486 FLAC__ASSERT(0 != option_argument);
487 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
488 FLAC__ASSERT(0 != violation);
489 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
493 else if(0 == strcmp(opt, "remove-first-tag") || 0 == strcmp(opt, "remove-vc-firstfield")) {
494 const char *violation;
495 if(0 == strcmp(opt, "remove-vc-firstfield"))
496 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-first-tag\n", opt);
497 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
498 FLAC__ASSERT(0 != option_argument);
499 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
500 FLAC__ASSERT(0 != violation);
501 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
505 else if(0 == strcmp(opt, "set-tag") || 0 == strcmp(opt, "set-vc-field")) {
506 const char *violation;
507 if(0 == strcmp(opt, "set-vc-field"))
508 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --set-tag\n", opt);
509 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
510 FLAC__ASSERT(0 != option_argument);
511 op->argument.vc_field.field_value_from_file = false;
512 if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
513 FLAC__ASSERT(0 != violation);
514 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
518 else if(0 == strcmp(opt, "set-tag-from-file")) {
519 const char *violation;
520 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
521 FLAC__ASSERT(0 != option_argument);
522 op->argument.vc_field.field_value_from_file = true;
523 if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
524 FLAC__ASSERT(0 != violation);
525 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
529 else if(0 == strcmp(opt, "import-tags-from") || 0 == strcmp(opt, "import-vc-from")) {
530 if(0 == strcmp(opt, "import-vc-from"))
531 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --import-tags-from\n", opt);
532 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
533 FLAC__ASSERT(0 != option_argument);
534 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
535 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
539 else if(0 == strcmp(opt, "export-tags-to") || 0 == strcmp(opt, "export-vc-to")) {
540 if(0 == strcmp(opt, "export-vc-to"))
541 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --export-tags-to\n", opt);
542 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
543 FLAC__ASSERT(0 != option_argument);
544 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
545 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
549 else if(0 == strcmp(opt, "import-cuesheet-from")) {
550 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
551 fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
554 op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
555 FLAC__ASSERT(0 != option_argument);
556 if(!parse_filename(option_argument, &(op->argument.import_cuesheet_from.filename))) {
557 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
561 else if(0 == strcmp(opt, "export-cuesheet-to")) {
562 op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
563 FLAC__ASSERT(0 != option_argument);
564 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
565 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
569 else if(0 == strcmp(opt, "add-seekpoint")) {
570 const char *violation;
572 FLAC__ASSERT(0 != option_argument);
573 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
574 FLAC__ASSERT(0 != violation);
575 fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation);
579 op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
581 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
582 local_strcat(&(op->argument.add_seekpoint.specification), spec);
583 local_strcat(&(op->argument.add_seekpoint.specification), ";");
587 else if(0 == strcmp(opt, "add-replay-gain")) {
588 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
590 else if(0 == strcmp(opt, "remove-replay-gain")) {
591 const FLAC__byte * const tags[4] = {
592 GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN,
593 GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK,
594 GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN,
595 GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK
598 for(i = 0; i < sizeof(tags)/sizeof(tags[0]); i++) {
599 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
600 op->argument.vc_field_name.value = local_strdup((const char *)tags[i]);
603 else if(0 == strcmp(opt, "add-padding")) {
604 op = append_shorthand_operation(options, OP__ADD_PADDING);
605 FLAC__ASSERT(0 != option_argument);
606 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
607 fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
611 else if(0 == strcmp(opt, "help")) {
612 options->show_long_help = true;
614 else if(0 == strcmp(opt, "version")) {
615 options->show_version = true;
617 else if(0 == strcmp(opt, "list")) {
618 (void) append_major_operation(options, OP__LIST);
620 else if(0 == strcmp(opt, "append")) {
621 (void) append_major_operation(options, OP__APPEND);
623 else if(0 == strcmp(opt, "remove")) {
624 (void) append_major_operation(options, OP__REMOVE);
626 else if(0 == strcmp(opt, "remove-all")) {
627 (void) append_major_operation(options, OP__REMOVE_ALL);
629 else if(0 == strcmp(opt, "merge-padding")) {
630 (void) append_major_operation(options, OP__MERGE_PADDING);
632 else if(0 == strcmp(opt, "sort-padding")) {
633 (void) append_major_operation(options, OP__SORT_PADDING);
635 else if(0 == strcmp(opt, "block-number")) {
636 arg = append_argument(options, ARG__BLOCK_NUMBER);
637 FLAC__ASSERT(0 != option_argument);
638 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
639 fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
643 else if(0 == strcmp(opt, "block-type")) {
644 arg = append_argument(options, ARG__BLOCK_TYPE);
645 FLAC__ASSERT(0 != option_argument);
646 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
647 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
650 options->args.checks.has_block_type = true;
652 else if(0 == strcmp(opt, "except-block-type")) {
653 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
654 FLAC__ASSERT(0 != option_argument);
655 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
656 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
659 options->args.checks.has_except_block_type = true;
661 else if(0 == strcmp(opt, "data-format")) {
662 arg = append_argument(options, ARG__DATA_FORMAT);
663 FLAC__ASSERT(0 != option_argument);
664 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
665 fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
669 else if(0 == strcmp(opt, "application-data-format")) {
670 FLAC__ASSERT(0 != option_argument);
671 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
672 fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
676 else if(0 == strcmp(opt, "from-file")) {
677 arg = append_argument(options, ARG__FROM_FILE);
678 FLAC__ASSERT(0 != option_argument);
679 arg->value.from_file.file_name = local_strdup(option_argument);
688 void append_new_operation(CommandLineOptions *options, Operation operation)
690 if(options->ops.capacity == 0) {
691 options->ops.capacity = 50;
692 if(0 == (options->ops.operations = (Operation*)malloc(sizeof(Operation) * options->ops.capacity)))
693 die("out of memory allocating space for option list");
694 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
696 if(options->ops.capacity <= options->ops.num_operations) {
697 unsigned original_capacity = options->ops.capacity;
698 options->ops.capacity *= 4;
699 if(0 == (options->ops.operations = (Operation*)realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
700 die("out of memory allocating space for option list");
701 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
704 options->ops.operations[options->ops.num_operations++] = operation;
707 void append_new_argument(CommandLineOptions *options, Argument argument)
709 if(options->args.capacity == 0) {
710 options->args.capacity = 50;
711 if(0 == (options->args.arguments = (Argument*)malloc(sizeof(Argument) * options->args.capacity)))
712 die("out of memory allocating space for option list");
713 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
715 if(options->args.capacity <= options->args.num_arguments) {
716 unsigned original_capacity = options->args.capacity;
717 options->args.capacity *= 4;
718 if(0 == (options->args.arguments = (Argument*)realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
719 die("out of memory allocating space for option list");
720 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
723 options->args.arguments[options->args.num_arguments++] = argument;
726 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
729 memset(&op, 0, sizeof(op));
731 append_new_operation(options, op);
732 options->args.checks.num_major_ops++;
733 return options->ops.operations + (options->ops.num_operations - 1);
736 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
739 memset(&op, 0, sizeof(op));
741 append_new_operation(options, op);
742 options->args.checks.num_shorthand_ops++;
743 return options->ops.operations + (options->ops.num_operations - 1);
746 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
749 for(i = 0; i < options->ops.num_operations; i++)
750 if(options->ops.operations[i].type == type)
751 return &options->ops.operations[i];
755 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
758 memset(&arg, 0, sizeof(arg));
760 append_new_argument(options, arg);
761 return options->args.arguments + (options->args.num_arguments - 1);
764 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
768 FLAC__ASSERT(0 != src);
769 if(strlen(src) != 32)
771 /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
772 for(i = 0; i < 16; i++) {
775 d = (unsigned)(c - '0');
776 else if(c >= 'a' && c <= 'f')
777 d = (unsigned)(c - 'a') + 10u;
778 else if(c >= 'A' && c <= 'F')
779 d = (unsigned)(c - 'A') + 10u;
785 d |= (unsigned)(c - '0');
786 else if(c >= 'a' && c <= 'f')
787 d |= (unsigned)(c - 'a') + 10u;
788 else if(c >= 'A' && c <= 'F')
789 d |= (unsigned)(c - 'A') + 10u;
792 dest[i] = (FLAC__byte)d;
797 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
799 FLAC__ASSERT(0 != src);
800 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
802 *dest = strtoul(src, 0, 10);
806 /* There's no stroull() in MSVC6 so we just write a specialized one */
807 static FLAC__uint64 local__strtoull(const char *src)
809 FLAC__uint64 ret = 0;
811 FLAC__ASSERT(0 != src);
812 while(0 != (c = *src++)) {
815 ret = (ret * 10) + c;
822 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
824 FLAC__ASSERT(0 != src);
825 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
827 *dest = local__strtoull(src);
831 FLAC__bool parse_filename(const char *src, char **dest)
833 if(0 == src || strlen(src) == 0)
839 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
841 static const char * const violations[] = {
842 "field name contains invalid character"
847 s = local_strdup(field_ref);
849 for(q = s; *q; q++) {
850 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
852 *violation = violations[0];
862 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
864 static const char *garbled_ = "garbled specification";
865 const unsigned n = strlen(in);
867 FLAC__ASSERT(0 != in);
868 FLAC__ASSERT(0 != out);
871 *violation = "specification is empty";
875 if(n > strspn(in, "0123456789.Xsx")) {
876 *violation = "specification contains invalid character";
882 *violation = garbled_;
886 else if(in[n-1] == 's') {
887 if(n-1 > strspn(in, "0123456789.")) {
888 *violation = garbled_;
892 else if(in[n-1] == 'x') {
893 if(n-1 > strspn(in, "0123456789")) {
894 *violation = garbled_;
899 if(n > strspn(in, "0123456789")) {
900 *violation = garbled_;
905 *out = local_strdup(in);
909 FLAC__bool parse_add_padding(const char *in, unsigned *out)
911 FLAC__ASSERT(0 != in);
912 FLAC__ASSERT(0 != out);
913 *out = (unsigned)strtoul(in, 0, 10);
914 return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
917 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
919 char *p, *q, *s, *end;
926 s = local_strdup(in);
928 /* first count the entries */
929 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
933 FLAC__ASSERT(out->num_entries > 0);
934 if(0 == (out->entries = (unsigned*)malloc(sizeof(unsigned) * out->num_entries)))
935 die("out of memory allocating space for option list");
941 FLAC__ASSERT(entry < out->num_entries);
942 if(0 != (p = strchr(q, ',')))
944 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
948 out->entries[entry++] = (unsigned)i;
951 FLAC__ASSERT(entry == out->num_entries);
957 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
965 s = local_strdup(in);
967 /* first count the entries */
968 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
972 FLAC__ASSERT(out->num_entries > 0);
973 if(0 == (out->entries = (Argument_BlockTypeEntry*)malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
974 die("out of memory allocating space for option list");
980 FLAC__ASSERT(entry < out->num_entries);
981 if(0 != (p = strchr(q, ',')))
986 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
990 if(0 == strcmp(q, "STREAMINFO")) {
991 out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
993 else if(0 == strcmp(q, "PADDING")) {
994 out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
996 else if(0 == strcmp(q, "APPLICATION")) {
997 out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
998 out->entries[entry].filter_application_by_id = (0 != r);
1000 if(strlen(r) == 4) {
1001 strcpy(out->entries[entry].application_id, r);
1003 else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
1004 FLAC__uint32 x = strtoul(r+2, 0, 16);
1005 out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
1006 out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
1007 out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
1008 out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
1017 else if(0 == strcmp(q, "SEEKTABLE")) {
1018 out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
1020 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
1021 out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1023 else if(0 == strcmp(q, "CUESHEET")) {
1024 out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
1032 FLAC__ASSERT(entry == out->num_entries);
1038 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1040 if(0 == strcmp(in, "binary"))
1041 out->is_binary = true;
1042 else if(0 == strcmp(in, "text"))
1043 out->is_binary = false;
1049 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1051 if(0 == strcmp(in, "hexdump"))
1053 else if(0 == strcmp(in, "text"))
1060 void undocumented_warning(const char *opt)
1062 fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt);