1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004,2005 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.
22 #include "FLAC/assert.h"
23 #include "share/grabbag/replaygain.h"
30 share__getopt format struct; note we don't use short options so we just
31 set the 'val' field to 0 everywhere to indicate a valid option.
33 struct share__option long_options_[] = {
35 { "preserve-modtime", 0, 0, 0 },
36 { "with-filename", 0, 0, 0 },
37 { "no-filename", 0, 0, 0 },
38 { "no-utf8-convert", 0, 0, 0 },
39 { "dont-use-padding", 0, 0, 0 },
40 { "no-cued-seekpoints", 0, 0, 0 },
41 /* shorthand operations */
42 { "show-md5sum", 0, 0, 0 },
43 { "show-min-blocksize", 0, 0, 0 },
44 { "show-max-blocksize", 0, 0, 0 },
45 { "show-min-framesize", 0, 0, 0 },
46 { "show-max-framesize", 0, 0, 0 },
47 { "show-sample-rate", 0, 0, 0 },
48 { "show-channels", 0, 0, 0 },
49 { "show-bps", 0, 0, 0 },
50 { "show-total-samples", 0, 0, 0 },
51 { "set-md5sum", 1, 0, 0 }, /* undocumented */
52 { "set-min-blocksize", 1, 0, 0 }, /* undocumented */
53 { "set-max-blocksize", 1, 0, 0 }, /* undocumented */
54 { "set-min-framesize", 1, 0, 0 }, /* undocumented */
55 { "set-max-framesize", 1, 0, 0 }, /* undocumented */
56 { "set-sample-rate", 1, 0, 0 }, /* undocumented */
57 { "set-channels", 1, 0, 0 }, /* undocumented */
58 { "set-bps", 1, 0, 0 }, /* undocumented */
59 { "set-total-samples", 1, 0, 0 }, /* undocumented */ /* WATCHOUT: used by test/test_flac.sh on windows */
60 { "show-vendor-tag", 0, 0, 0 },
61 { "show-tag", 1, 0, 0 },
62 { "remove-all-tags", 0, 0, 0 },
63 { "remove-tag", 1, 0, 0 },
64 { "remove-first-tag", 1, 0, 0 },
65 { "set-tag", 1, 0, 0 },
66 { "import-tags-from", 1, 0, 0 },
67 { "export-tags-to", 1, 0, 0 },
68 { "show-vc-vendor", 0, 0, 0 }, /* deprecated */
69 { "show-vc-field", 1, 0, 0 }, /* deprecated */
70 { "remove-vc-all", 0, 0, 0 }, /* deprecated */
71 { "remove-vc-field", 1, 0, 0 }, /* deprecated */
72 { "remove-vc-firstfield", 1, 0, 0 }, /* deprecated */
73 { "set-vc-field", 1, 0, 0 }, /* deprecated */
74 { "import-vc-from", 1, 0, 0 }, /* deprecated */
75 { "export-vc-to", 1, 0, 0 }, /* deprecated */
76 { "import-cuesheet-from", 1, 0, 0 },
77 { "export-cuesheet-to", 1, 0, 0 },
78 { "add-seekpoint", 1, 0, 0 },
79 { "add-replay-gain", 0, 0, 0 },
80 { "remove-replay-gain", 0, 0, 0 },
81 { "add-padding", 1, 0, 0 },
82 /* major operations */
84 { "version", 0, 0, 0 },
86 { "append", 0, 0, 0 },
87 { "remove", 0, 0, 0 },
88 { "remove-all", 0, 0, 0 },
89 { "merge-padding", 0, 0, 0 },
90 { "sort-padding", 0, 0, 0 },
91 /* major operation arguments */
92 { "block-number", 1, 0, 0 },
93 { "block-type", 1, 0, 0 },
94 { "except-block-type", 1, 0, 0 },
95 { "data-format", 1, 0, 0 },
96 { "application-data-format", 1, 0, 0 },
97 { "from-file", 1, 0, 0 },
101 static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options);
102 static void append_new_operation(CommandLineOptions *options, Operation operation);
103 static void append_new_argument(CommandLineOptions *options, Argument argument);
104 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
105 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
106 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
107 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
108 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
109 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
110 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
111 static FLAC__bool parse_filename(const char *src, char **dest);
112 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
113 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
114 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
115 static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out);
116 static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out);
117 static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out);
118 static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out);
119 static void undocumented_warning(const char *opt);
122 void init_options(CommandLineOptions *options)
124 options->preserve_modtime = false;
126 /* '2' is a hack to mean "use default if not forced on command line" */
127 FLAC__ASSERT(true != 2);
128 options->prefix_with_filename = 2;
130 options->utf8_convert = true;
131 options->use_padding = true;
132 options->cued_seekpoints = true;
133 options->show_long_help = false;
134 options->show_version = false;
135 options->application_data_format_is_hexdump = false;
137 options->ops.operations = 0;
138 options->ops.num_operations = 0;
139 options->ops.capacity = 0;
141 options->args.arguments = 0;
142 options->args.num_arguments = 0;
143 options->args.capacity = 0;
145 options->args.checks.num_shorthand_ops = 0;
146 options->args.checks.num_major_ops = 0;
147 options->args.checks.has_block_type = false;
148 options->args.checks.has_except_block_type = false;
150 options->num_files = 0;
151 options->filenames = 0;
154 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
157 int option_index = 1;
158 FLAC__bool had_error = false;
160 while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
163 had_error |= !parse_option(option_index, share__optarg, options);
175 if(options->prefix_with_filename == 2)
176 options->prefix_with_filename = (argc - share__optind > 1);
178 if(share__optind >= argc && !options->show_long_help && !options->show_version) {
179 fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
180 fprintf(stderr," metaflac cannot be used as a pipe\n");
184 options->num_files = argc - share__optind;
186 if(options->num_files > 0) {
188 if(0 == (options->filenames = (char**)malloc(sizeof(char*) * options->num_files)))
189 die("out of memory allocating space for file names list");
190 while(share__optind < argc)
191 options->filenames[i++] = local_strdup(argv[share__optind++]);
194 if(options->args.checks.num_major_ops > 0) {
195 if(options->args.checks.num_major_ops > 1) {
196 fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
199 else if(options->args.checks.num_shorthand_ops > 0) {
200 fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
205 /* check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to */
206 if((0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) || 0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) && options->num_files > 1) {
207 fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from' or '--export-cuesheet-to'\n");
211 if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
212 fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
220 * We need to create an OP__ADD_SEEKPOINT operation if there is
221 * not one already, and --import-cuesheet-from was specified but
222 * --no-cued-seekpoints was not:
224 if(options->cued_seekpoints) {
225 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
227 Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
229 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
230 op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
237 void free_options(CommandLineOptions *options)
243 FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
244 FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
246 for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
248 case OP__SHOW_VC_FIELD:
249 case OP__REMOVE_VC_FIELD:
250 case OP__REMOVE_VC_FIRSTFIELD:
251 if(0 != op->argument.vc_field_name.value)
252 free(op->argument.vc_field_name.value);
254 case OP__SET_VC_FIELD:
255 if(0 != op->argument.vc_field.field)
256 free(op->argument.vc_field.field);
257 if(0 != op->argument.vc_field.field_name)
258 free(op->argument.vc_field.field_name);
259 if(0 != op->argument.vc_field.field_value)
260 free(op->argument.vc_field.field_value);
262 case OP__IMPORT_VC_FROM:
263 case OP__EXPORT_VC_TO:
264 case OP__EXPORT_CUESHEET_TO:
265 if(0 != op->argument.filename.value)
266 free(op->argument.filename.value);
268 case OP__IMPORT_CUESHEET_FROM:
269 if(0 != op->argument.import_cuesheet_from.filename)
270 free(op->argument.import_cuesheet_from.filename);
272 case OP__ADD_SEEKPOINT:
273 if(0 != op->argument.add_seekpoint.specification)
274 free(op->argument.add_seekpoint.specification);
281 for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
283 case ARG__BLOCK_NUMBER:
284 if(0 != arg->value.block_number.entries)
285 free(arg->value.block_number.entries);
287 case ARG__BLOCK_TYPE:
288 case ARG__EXCEPT_BLOCK_TYPE:
289 if(0 != arg->value.block_type.entries)
290 free(arg->value.block_type.entries);
293 if(0 != arg->value.from_file.file_name)
294 free(arg->value.from_file.file_name);
301 if(0 != options->ops.operations)
302 free(options->ops.operations);
304 if(0 != options->args.arguments)
305 free(options->args.arguments);
307 if(0 != options->filenames) {
308 for(i = 0; i < options->num_files; i++) {
309 if(0 != options->filenames[i])
310 free(options->filenames[i]);
312 free(options->filenames);
320 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
322 const char *opt = long_options_[option_index].name;
325 FLAC__bool ok = true;
327 if(0 == strcmp(opt, "preserve-modtime")) {
328 options->preserve_modtime = true;
330 else if(0 == strcmp(opt, "with-filename")) {
331 options->prefix_with_filename = true;
333 else if(0 == strcmp(opt, "no-filename")) {
334 options->prefix_with_filename = false;
336 else if(0 == strcmp(opt, "no-utf8-convert")) {
337 options->utf8_convert = false;
339 else if(0 == strcmp(opt, "dont-use-padding")) {
340 options->use_padding = false;
342 else if(0 == strcmp(opt, "no-cued-seekpoints")) {
343 options->cued_seekpoints = false;
345 else if(0 == strcmp(opt, "show-md5sum")) {
346 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
348 else if(0 == strcmp(opt, "show-min-blocksize")) {
349 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
351 else if(0 == strcmp(opt, "show-max-blocksize")) {
352 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
354 else if(0 == strcmp(opt, "show-min-framesize")) {
355 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
357 else if(0 == strcmp(opt, "show-max-framesize")) {
358 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
360 else if(0 == strcmp(opt, "show-sample-rate")) {
361 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
363 else if(0 == strcmp(opt, "show-channels")) {
364 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
366 else if(0 == strcmp(opt, "show-bps")) {
367 (void) append_shorthand_operation(options, OP__SHOW_BPS);
369 else if(0 == strcmp(opt, "show-total-samples")) {
370 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
372 else if(0 == strcmp(opt, "set-md5sum")) {
373 op = append_shorthand_operation(options, OP__SET_MD5SUM);
374 FLAC__ASSERT(0 != option_argument);
375 if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
376 fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
380 undocumented_warning(opt);
382 else if(0 == strcmp(opt, "set-min-blocksize")) {
383 op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
384 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) {
385 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
389 undocumented_warning(opt);
391 else if(0 == strcmp(opt, "set-max-blocksize")) {
392 op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
393 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) {
394 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
398 undocumented_warning(opt);
400 else if(0 == strcmp(opt, "set-min-framesize")) {
401 op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
402 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
403 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
407 undocumented_warning(opt);
409 else if(0 == strcmp(opt, "set-max-framesize")) {
410 op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
411 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
412 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
416 undocumented_warning(opt);
418 else if(0 == strcmp(opt, "set-sample-rate")) {
419 op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
420 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
421 fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
425 undocumented_warning(opt);
427 else if(0 == strcmp(opt, "set-channels")) {
428 op = append_shorthand_operation(options, OP__SET_CHANNELS);
429 if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
430 fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
434 undocumented_warning(opt);
436 else if(0 == strcmp(opt, "set-bps")) {
437 op = append_shorthand_operation(options, OP__SET_BPS);
438 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) {
439 fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
443 undocumented_warning(opt);
445 else if(0 == strcmp(opt, "set-total-samples")) {
446 op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
447 if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
448 fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
452 undocumented_warning(opt);
454 else if(0 == strcmp(opt, "show-vendor-tag") || 0 == strcmp(opt, "show-vc-vendor")) {
455 if(0 == strcmp(opt, "show-vc-vendor"))
456 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --show-vendor-tag\n", opt);
457 (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
459 else if(0 == strcmp(opt, "show-tag") || 0 == strcmp(opt, "show-vc-field")) {
460 const char *violation;
461 if(0 == strcmp(opt, "show-vc-field"))
462 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --show-tag\n", opt);
463 op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
464 FLAC__ASSERT(0 != option_argument);
465 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
466 FLAC__ASSERT(0 != violation);
467 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
471 else if(0 == strcmp(opt, "remove-all-tags") || 0 == strcmp(opt, "remove-vc-all")) {
472 if(0 == strcmp(opt, "remove-vc-all"))
473 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-all-tags\n", opt);
474 (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
476 else if(0 == strcmp(opt, "remove-tag") || 0 == strcmp(opt, "remove-vc-field")) {
477 const char *violation;
478 if(0 == strcmp(opt, "remove-vc-field"))
479 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-tag\n", opt);
480 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
481 FLAC__ASSERT(0 != option_argument);
482 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
483 FLAC__ASSERT(0 != violation);
484 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
488 else if(0 == strcmp(opt, "remove-first-tag") || 0 == strcmp(opt, "remove-vc-firstfield")) {
489 const char *violation;
490 if(0 == strcmp(opt, "remove-vc-firstfield"))
491 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-first-tag\n", opt);
492 op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
493 FLAC__ASSERT(0 != option_argument);
494 if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
495 FLAC__ASSERT(0 != violation);
496 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
500 else if(0 == strcmp(opt, "set-tag") || 0 == strcmp(opt, "set-vc-field")) {
501 const char *violation;
502 if(0 == strcmp(opt, "set-vc-field"))
503 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --set-tag\n", opt);
504 op = append_shorthand_operation(options, OP__SET_VC_FIELD);
505 FLAC__ASSERT(0 != option_argument);
506 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)) {
507 FLAC__ASSERT(0 != violation);
508 fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
512 else if(0 == strcmp(opt, "import-tags-from") || 0 == strcmp(opt, "import-vc-from")) {
513 if(0 == strcmp(opt, "import-vc-from"))
514 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --import-tags-from\n", opt);
515 op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
516 FLAC__ASSERT(0 != option_argument);
517 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
518 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
522 else if(0 == strcmp(opt, "export-tags-to") || 0 == strcmp(opt, "export-vc-to")) {
523 if(0 == strcmp(opt, "export-vc-to"))
524 fprintf(stderr, "WARNING: --%s is deprecated, the new name is --export-tags-to\n", opt);
525 op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
526 FLAC__ASSERT(0 != option_argument);
527 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
528 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
532 else if(0 == strcmp(opt, "import-cuesheet-from")) {
533 if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
534 fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
537 op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
538 FLAC__ASSERT(0 != option_argument);
539 if(!parse_filename(option_argument, &(op->argument.import_cuesheet_from.filename))) {
540 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
544 else if(0 == strcmp(opt, "export-cuesheet-to")) {
545 op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
546 FLAC__ASSERT(0 != option_argument);
547 if(!parse_filename(option_argument, &(op->argument.filename.value))) {
548 fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
552 else if(0 == strcmp(opt, "add-seekpoint")) {
553 const char *violation;
555 FLAC__ASSERT(0 != option_argument);
556 if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
557 FLAC__ASSERT(0 != violation);
558 fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation);
562 op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
564 op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
565 local_strcat(&(op->argument.add_seekpoint.specification), spec);
566 local_strcat(&(op->argument.add_seekpoint.specification), ";");
570 else if(0 == strcmp(opt, "add-replay-gain")) {
571 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
573 else if(0 == strcmp(opt, "remove-replay-gain")) {
574 const FLAC__byte * const tags[4] = {
575 GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN,
576 GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK,
577 GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN,
578 GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK
581 for(i = 0; i < sizeof(tags)/sizeof(tags[0]); i++) {
582 op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
583 op->argument.vc_field_name.value = local_strdup((const char *)tags[i]);
586 else if(0 == strcmp(opt, "add-padding")) {
587 op = append_shorthand_operation(options, OP__ADD_PADDING);
588 FLAC__ASSERT(0 != option_argument);
589 if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
590 fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
594 else if(0 == strcmp(opt, "help")) {
595 options->show_long_help = true;
597 else if(0 == strcmp(opt, "version")) {
598 options->show_version = true;
600 else if(0 == strcmp(opt, "list")) {
601 (void) append_major_operation(options, OP__LIST);
603 else if(0 == strcmp(opt, "append")) {
604 (void) append_major_operation(options, OP__APPEND);
606 else if(0 == strcmp(opt, "remove")) {
607 (void) append_major_operation(options, OP__REMOVE);
609 else if(0 == strcmp(opt, "remove-all")) {
610 (void) append_major_operation(options, OP__REMOVE_ALL);
612 else if(0 == strcmp(opt, "merge-padding")) {
613 (void) append_major_operation(options, OP__MERGE_PADDING);
615 else if(0 == strcmp(opt, "sort-padding")) {
616 (void) append_major_operation(options, OP__SORT_PADDING);
618 else if(0 == strcmp(opt, "block-number")) {
619 arg = append_argument(options, ARG__BLOCK_NUMBER);
620 FLAC__ASSERT(0 != option_argument);
621 if(!parse_block_number(option_argument, &(arg->value.block_number))) {
622 fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
626 else if(0 == strcmp(opt, "block-type")) {
627 arg = append_argument(options, ARG__BLOCK_TYPE);
628 FLAC__ASSERT(0 != option_argument);
629 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
630 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
633 options->args.checks.has_block_type = true;
635 else if(0 == strcmp(opt, "except-block-type")) {
636 arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
637 FLAC__ASSERT(0 != option_argument);
638 if(!parse_block_type(option_argument, &(arg->value.block_type))) {
639 fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
642 options->args.checks.has_except_block_type = true;
644 else if(0 == strcmp(opt, "data-format")) {
645 arg = append_argument(options, ARG__DATA_FORMAT);
646 FLAC__ASSERT(0 != option_argument);
647 if(!parse_data_format(option_argument, &(arg->value.data_format))) {
648 fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
652 else if(0 == strcmp(opt, "application-data-format")) {
653 FLAC__ASSERT(0 != option_argument);
654 if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
655 fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
659 else if(0 == strcmp(opt, "from-file")) {
660 arg = append_argument(options, ARG__FROM_FILE);
661 FLAC__ASSERT(0 != option_argument);
662 arg->value.from_file.file_name = local_strdup(option_argument);
671 void append_new_operation(CommandLineOptions *options, Operation operation)
673 if(options->ops.capacity == 0) {
674 options->ops.capacity = 50;
675 if(0 == (options->ops.operations = (Operation*)malloc(sizeof(Operation) * options->ops.capacity)))
676 die("out of memory allocating space for option list");
677 memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
679 if(options->ops.capacity <= options->ops.num_operations) {
680 unsigned original_capacity = options->ops.capacity;
681 options->ops.capacity *= 4;
682 if(0 == (options->ops.operations = (Operation*)realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
683 die("out of memory allocating space for option list");
684 memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
687 options->ops.operations[options->ops.num_operations++] = operation;
690 void append_new_argument(CommandLineOptions *options, Argument argument)
692 if(options->args.capacity == 0) {
693 options->args.capacity = 50;
694 if(0 == (options->args.arguments = (Argument*)malloc(sizeof(Argument) * options->args.capacity)))
695 die("out of memory allocating space for option list");
696 memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
698 if(options->args.capacity <= options->args.num_arguments) {
699 unsigned original_capacity = options->args.capacity;
700 options->args.capacity *= 4;
701 if(0 == (options->args.arguments = (Argument*)realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
702 die("out of memory allocating space for option list");
703 memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
706 options->args.arguments[options->args.num_arguments++] = argument;
709 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
712 memset(&op, 0, sizeof(op));
714 append_new_operation(options, op);
715 options->args.checks.num_major_ops++;
716 return options->ops.operations + (options->ops.num_operations - 1);
719 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
722 memset(&op, 0, sizeof(op));
724 append_new_operation(options, op);
725 options->args.checks.num_shorthand_ops++;
726 return options->ops.operations + (options->ops.num_operations - 1);
729 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
732 for(i = 0; i < options->ops.num_operations; i++)
733 if(options->ops.operations[i].type == type)
734 return &options->ops.operations[i];
738 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
741 memset(&arg, 0, sizeof(arg));
743 append_new_argument(options, arg);
744 return options->args.arguments + (options->args.num_arguments - 1);
747 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
751 FLAC__ASSERT(0 != src);
752 if(strlen(src) != 32)
754 /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
755 for(i = 0; i < 16; i++) {
758 d = (unsigned)(c - '0');
759 else if(c >= 'a' && c <= 'f')
760 d = (unsigned)(c - 'a') + 10u;
761 else if(c >= 'A' && c <= 'F')
762 d = (unsigned)(c - 'A') + 10u;
768 d |= (unsigned)(c - '0');
769 else if(c >= 'a' && c <= 'f')
770 d |= (unsigned)(c - 'a') + 10u;
771 else if(c >= 'A' && c <= 'F')
772 d |= (unsigned)(c - 'A') + 10u;
775 dest[i] = (FLAC__byte)d;
780 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
782 FLAC__ASSERT(0 != src);
783 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
785 *dest = strtoul(src, 0, 10);
789 /* There's no stroull() in MSVC6 so we just write a specialized one */
790 static FLAC__uint64 local__strtoull(const char *src)
792 FLAC__uint64 ret = 0;
794 FLAC__ASSERT(0 != src);
795 while(0 != (c = *src++)) {
798 ret = (ret * 10) + c;
805 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
807 FLAC__ASSERT(0 != src);
808 if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
810 *dest = local__strtoull(src);
814 FLAC__bool parse_filename(const char *src, char **dest)
816 if(0 == src || strlen(src) == 0)
822 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
824 static const char * const violations[] = {
825 "field name contains invalid character"
830 s = local_strdup(field_ref);
832 for(q = s; *q; q++) {
833 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
835 *violation = violations[0];
845 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
847 static const char *garbled_ = "garbled specification";
848 const unsigned n = strlen(in);
850 FLAC__ASSERT(0 != in);
851 FLAC__ASSERT(0 != out);
854 *violation = "specification is empty";
858 if(n > strspn(in, "0123456789.Xsx")) {
859 *violation = "specification contains invalid character";
865 *violation = garbled_;
869 else if(in[n-1] == 's') {
870 if(n-1 > strspn(in, "0123456789.")) {
871 *violation = garbled_;
875 else if(in[n-1] == 'x') {
876 if(n-1 > strspn(in, "0123456789")) {
877 *violation = garbled_;
882 if(n > strspn(in, "0123456789")) {
883 *violation = garbled_;
888 *out = local_strdup(in);
892 FLAC__bool parse_add_padding(const char *in, unsigned *out)
894 FLAC__ASSERT(0 != in);
895 FLAC__ASSERT(0 != out);
896 *out = (unsigned)strtoul(in, 0, 10);
897 return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
900 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
902 char *p, *q, *s, *end;
909 s = local_strdup(in);
911 /* first count the entries */
912 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
916 FLAC__ASSERT(out->num_entries > 0);
917 if(0 == (out->entries = (unsigned*)malloc(sizeof(unsigned) * out->num_entries)))
918 die("out of memory allocating space for option list");
924 FLAC__ASSERT(entry < out->num_entries);
925 if(0 != (p = strchr(q, ',')))
927 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
931 out->entries[entry++] = (unsigned)i;
934 FLAC__ASSERT(entry == out->num_entries);
940 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
948 s = local_strdup(in);
950 /* first count the entries */
951 for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
955 FLAC__ASSERT(out->num_entries > 0);
956 if(0 == (out->entries = (Argument_BlockTypeEntry*)malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
957 die("out of memory allocating space for option list");
963 FLAC__ASSERT(entry < out->num_entries);
964 if(0 != (p = strchr(q, ',')))
969 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
973 if(0 == strcmp(q, "STREAMINFO")) {
974 out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
976 else if(0 == strcmp(q, "PADDING")) {
977 out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
979 else if(0 == strcmp(q, "APPLICATION")) {
980 out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
981 out->entries[entry].filter_application_by_id = (0 != r);
984 strcpy(out->entries[entry].application_id, r);
986 else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
987 FLAC__uint32 x = strtoul(r+2, 0, 16);
988 out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
989 out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
990 out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
991 out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
1000 else if(0 == strcmp(q, "SEEKTABLE")) {
1001 out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
1003 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
1004 out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1006 else if(0 == strcmp(q, "CUESHEET")) {
1007 out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
1015 FLAC__ASSERT(entry == out->num_entries);
1021 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1023 if(0 == strcmp(in, "binary"))
1024 out->is_binary = true;
1025 else if(0 == strcmp(in, "text"))
1026 out->is_binary = false;
1032 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1034 if(0 == strcmp(in, "hexdump"))
1036 else if(0 == strcmp(in, "text"))
1043 void undocumented_warning(const char *opt)
1045 fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt);