-
- free_options(&options);
-
- return ret;
-}
-
-void die(const char *message)
-{
- FLAC__ASSERT(0 != message);
- fprintf(stderr, "ERROR: %s\n", message);
- exit(1);
-}
-
-void init_options(CommandLineOptions *options)
-{
- options->preserve_modtime = false;
-
- /* '2' is a hack to mean "use default if not forced on command line" */
- FLAC__ASSERT(true != 2);
- options->prefix_with_filename = 2;
-
- options->utf8_convert = true;
- options->use_padding = true;
- options->show_long_help = false;
- options->show_version = false;
- options->application_data_format_is_hexdump = false;
-
- options->ops.operations = 0;
- options->ops.num_operations = 0;
- options->ops.capacity = 0;
-
- options->args.arguments = 0;
- options->args.num_arguments = 0;
- options->args.capacity = 0;
-
- options->args.checks.num_shorthand_ops = 0;
- options->args.checks.num_major_ops = 0;
- options->args.checks.has_block_type = false;
- options->args.checks.has_except_block_type = false;
-
- options->num_files = 0;
- options->filenames = 0;
-}
-
-FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
-{
- int ret;
- int option_index = 1;
- FLAC__bool had_error = false;
-
- while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
- switch (ret) {
- case 0:
- had_error |= !parse_option(option_index, share__optarg, options);
- break;
- case '?':
- case ':':
- had_error = true;
- break;
- default:
- FLAC__ASSERT(0);
- break;
- }
- }
-
- if(options->prefix_with_filename == 2)
- options->prefix_with_filename = (argc - share__optind > 1);
-
- if(share__optind >= argc && !options->show_long_help && !options->show_version) {
- fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
- fprintf(stderr," metaflac cannot be used as a pipe\n");
- had_error = true;
- }
-
- options->num_files = argc - share__optind;
-
- if(options->num_files > 0) {
- unsigned i = 0;
- if(0 == (options->filenames = malloc(sizeof(char *) * options->num_files)))
- die("out of memory allocating space for file names list");
- while(share__optind < argc)
- options->filenames[i++] = local_strdup(argv[share__optind++]);
- }
-
- if(options->args.checks.num_major_ops > 0) {
- if(options->args.checks.num_major_ops > 1) {
- fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
- had_error = true;
- }
- else if(options->args.checks.num_shorthand_ops > 0) {
- fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
- had_error = true;
- }
- }
-
- if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
- fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
- had_error = true;
- }
-
- if(had_error)
- short_usage(0);
-
- return !had_error;
-}
-
-FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
-{
- const char *opt = long_options_[option_index].name;
- Operation *op;
- Argument *arg;
- FLAC__bool ok = true;
-
- if(0 == strcmp(opt, "preserve-modtime")) {
- options->preserve_modtime = true;
- }
- else if(0 == strcmp(opt, "with-filename")) {
- options->prefix_with_filename = true;
- }
- else if(0 == strcmp(opt, "no-filename")) {
- options->prefix_with_filename = false;
- }
- else if(0 == strcmp(opt, "no-utf8-convert")) {
- options->utf8_convert = false;
- }
- else if(0 == strcmp(opt, "dont-use-padding")) {
- options->use_padding = false;
- }
- else if(0 == strcmp(opt, "show-md5sum")) {
- (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
- }
- else if(0 == strcmp(opt, "show-min-blocksize")) {
- (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
- }
- else if(0 == strcmp(opt, "show-max-blocksize")) {
- (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
- }
- else if(0 == strcmp(opt, "show-min-framesize")) {
- (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
- }
- else if(0 == strcmp(opt, "show-max-framesize")) {
- (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
- }
- else if(0 == strcmp(opt, "show-sample-rate")) {
- (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
- }
- else if(0 == strcmp(opt, "show-channels")) {
- (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
- }
- else if(0 == strcmp(opt, "show-bps")) {
- (void) append_shorthand_operation(options, OP__SHOW_BPS);
- }
- else if(0 == strcmp(opt, "show-total-samples")) {
- (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
- }
- else if(0 == strcmp(opt, "set-md5sum")) {
- op = append_shorthand_operation(options, OP__SET_MD5SUM);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
- fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "set-min-blocksize")) {
- op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
- 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) {
- fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "set-max-blocksize")) {
- op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
- 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) {
- fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "set-min-framesize")) {
- op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
- if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
- fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "set-max-framesize")) {
- op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
- if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
- fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "set-sample-rate")) {
- op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
- if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
- fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "set-channels")) {
- op = append_shorthand_operation(options, OP__SET_CHANNELS);
- if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
- fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "set-bps")) {
- op = append_shorthand_operation(options, OP__SET_BPS);
- 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) {
- fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "set-total-samples")) {
- op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
- if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
- fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
- ok = false;
- }
- else
- undocumented_warning(opt);
- }
- else if(0 == strcmp(opt, "show-vc-vendor")) {
- (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
- }
- else if(0 == strcmp(opt, "show-vc-field")) {
- const char *violation;
- op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
- FLAC__ASSERT(0 != violation);
- fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "remove-vc-all")) {
- (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
- }
- else if(0 == strcmp(opt, "remove-vc-field")) {
- const char *violation;
- op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
- FLAC__ASSERT(0 != violation);
- fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "remove-vc-firstfield")) {
- const char *violation;
- op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
- FLAC__ASSERT(0 != violation);
- fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n %s\n", opt, option_argument, violation);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "set-vc-field")) {
- const char *violation;
- op = append_shorthand_operation(options, OP__SET_VC_FIELD);
- FLAC__ASSERT(0 != option_argument);
- 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)) {
- FLAC__ASSERT(0 != violation);
- fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n %s\n", opt, option_argument, violation);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "import-vc-from")) {
- op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_filename(option_argument, &(op->argument.vc_filename.value))) {
- fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "export-vc-to")) {
- op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_filename(option_argument, &(op->argument.vc_filename.value))) {
- fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "add-replay-gain")) {
- (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
- }
- else if(0 == strcmp(opt, "add-padding")) {
- op = append_shorthand_operation(options, OP__ADD_PADDING);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
- fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "help")) {
- options->show_long_help = true;
- }
- else if(0 == strcmp(opt, "version")) {
- options->show_version = true;
- }
- else if(0 == strcmp(opt, "list")) {
- (void) append_major_operation(options, OP__LIST);
- }
- else if(0 == strcmp(opt, "append")) {
- (void) append_major_operation(options, OP__APPEND);
- }
- else if(0 == strcmp(opt, "remove")) {
- (void) append_major_operation(options, OP__REMOVE);
- }
- else if(0 == strcmp(opt, "remove-all")) {
- (void) append_major_operation(options, OP__REMOVE_ALL);
- }
- else if(0 == strcmp(opt, "merge-padding")) {
- (void) append_major_operation(options, OP__MERGE_PADDING);
- }
- else if(0 == strcmp(opt, "sort-padding")) {
- (void) append_major_operation(options, OP__SORT_PADDING);
- }
- else if(0 == strcmp(opt, "block-number")) {
- arg = append_argument(options, ARG__BLOCK_NUMBER);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_block_number(option_argument, &(arg->value.block_number))) {
- fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "block-type")) {
- arg = append_argument(options, ARG__BLOCK_TYPE);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_block_type(option_argument, &(arg->value.block_type))) {
- fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
- ok = false;
- }
- options->args.checks.has_block_type = true;
- }
- else if(0 == strcmp(opt, "except-block-type")) {
- arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_block_type(option_argument, &(arg->value.block_type))) {
- fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
- ok = false;
- }
- options->args.checks.has_except_block_type = true;
- }
- else if(0 == strcmp(opt, "data-format")) {
- arg = append_argument(options, ARG__DATA_FORMAT);
- FLAC__ASSERT(0 != option_argument);
- if(!parse_data_format(option_argument, &(arg->value.data_format))) {
- fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "application-data-format")) {
- FLAC__ASSERT(0 != option_argument);
- if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
- fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
- ok = false;
- }
- }
- else if(0 == strcmp(opt, "from-file")) {
- arg = append_argument(options, ARG__FROM_FILE);
- FLAC__ASSERT(0 != option_argument);
- arg->value.from_file.file_name = local_strdup(option_argument);
- }
- else {
- FLAC__ASSERT(0);
- }
-
- return ok;
-}
-
-void free_options(CommandLineOptions *options)
-{
- unsigned i;
- Operation *op;
- Argument *arg;
-
- FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
- FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
-
- for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
- switch(op->type) {
- case OP__SHOW_VC_FIELD:
- case OP__REMOVE_VC_FIELD:
- case OP__REMOVE_VC_FIRSTFIELD:
- if(0 != op->argument.vc_field_name.value)
- free(op->argument.vc_field_name.value);
- break;
- case OP__SET_VC_FIELD:
- if(0 != op->argument.vc_field.field)
- free(op->argument.vc_field.field);
- if(0 != op->argument.vc_field.field_name)
- free(op->argument.vc_field.field_name);
- if(0 != op->argument.vc_field.field_value)
- free(op->argument.vc_field.field_value);
- break;
- case OP__IMPORT_VC_FROM:
- case OP__EXPORT_VC_TO:
- if(0 != op->argument.vc_filename.value)
- free(op->argument.vc_filename.value);
- break;
- default:
- break;
- }
- }
-
- for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
- switch(arg->type) {
- case ARG__BLOCK_NUMBER:
- if(0 != arg->value.block_number.entries)
- free(arg->value.block_number.entries);
- break;
- case ARG__BLOCK_TYPE:
- case ARG__EXCEPT_BLOCK_TYPE:
- if(0 != arg->value.block_type.entries)
- free(arg->value.block_type.entries);
- break;
- case ARG__FROM_FILE:
- if(0 != arg->value.from_file.file_name)
- free(arg->value.from_file.file_name);
- break;
- default:
- break;
- }
- }
-
- if(0 != options->ops.operations)
- free(options->ops.operations);
-
- if(0 != options->args.arguments)
- free(options->args.arguments);
-
- if(0 != options->filenames)
- free(options->filenames);
-}
-
-void append_new_operation(CommandLineOptions *options, Operation operation)
-{
- if(options->ops.capacity == 0) {
- options->ops.capacity = 50;
- if(0 == (options->ops.operations = malloc(sizeof(Operation) * options->ops.capacity)))
- die("out of memory allocating space for option list");
- memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
- }
- if(options->ops.capacity <= options->ops.num_operations) {
- unsigned original_capacity = options->ops.capacity;
- options->ops.capacity *= 4;
- if(0 == (options->ops.operations = realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
- die("out of memory allocating space for option list");
- memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
- }
-
- options->ops.operations[options->ops.num_operations++] = operation;
-}
-
-void append_new_argument(CommandLineOptions *options, Argument argument)
-{
- if(options->args.capacity == 0) {
- options->args.capacity = 50;
- if(0 == (options->args.arguments = malloc(sizeof(Argument) * options->args.capacity)))
- die("out of memory allocating space for option list");
- memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
- }
- if(options->args.capacity <= options->args.num_arguments) {
- unsigned original_capacity = options->args.capacity;
- options->args.capacity *= 4;
- if(0 == (options->args.arguments = realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
- die("out of memory allocating space for option list");
- memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
- }
-
- options->args.arguments[options->args.num_arguments++] = argument;
-}
-
-Operation *append_major_operation(CommandLineOptions *options, OperationType type)
-{
- Operation op;
- memset(&op, 0, sizeof(op));
- op.type = type;
- append_new_operation(options, op);
- options->args.checks.num_major_ops++;
- return options->ops.operations + (options->ops.num_operations - 1);
-}
-
-Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
-{
- Operation op;
- memset(&op, 0, sizeof(op));
- op.type = type;
- append_new_operation(options, op);
- options->args.checks.num_shorthand_ops++;
- return options->ops.operations + (options->ops.num_operations - 1);
-}
-
-Argument *append_argument(CommandLineOptions *options, ArgumentType type)
-{
- Argument arg;
- memset(&arg, 0, sizeof(arg));
- arg.type = type;
- append_new_argument(options, arg);
- return options->args.arguments + (options->args.num_arguments - 1);
-}
-
-void show_version()
-{
- printf("metaflac %s\n", FLAC__VERSION_STRING);
-}
-
-static void usage_header(FILE *out)
-{
- fprintf(out, "==============================================================================\n");
- fprintf(out, "metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING);
- fprintf(out, "Copyright (C) 2001,2002 Josh Coalson\n");
- fprintf(out, "\n");
- fprintf(out, "This program is free software; you can redistribute it and/or\n");
- fprintf(out, "modify it under the terms of the GNU General Public License\n");
- fprintf(out, "as published by the Free Software Foundation; either version 2\n");
- fprintf(out, "of the License, or (at your option) any later version.\n");
- fprintf(out, "\n");
- fprintf(out, "This program is distributed in the hope that it will be useful,\n");
- fprintf(out, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
- fprintf(out, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
- fprintf(out, "GNU General Public License for more details.\n");
- fprintf(out, "\n");
- fprintf(out, "You should have received a copy of the GNU General Public License\n");
- fprintf(out, "along with this program; if not, write to the Free Software\n");
- fprintf(out, "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
- fprintf(out, "==============================================================================\n");
-}
-
-static void usage_summary(FILE *out)
-{
- fprintf(out, "Usage:\n");
- fprintf(out, " metaflac [options] [operations] FLACfile [FLACfile ...]\n");
- fprintf(out, "\n");
- fprintf(out, "Use metaflac to list, add, remove, or edit metadata in one or more FLAC files.\n");
- fprintf(out, "You may perform one major operation, or many shorthand operations at a time.\n");
- fprintf(out, "\n");
- fprintf(out, "Options:\n");
- fprintf(out, "--preserve-modtime Preserve the original modification time in spite of edits\n");
- fprintf(out, "--with-filename Prefix each output line with the FLAC file name\n");
- fprintf(out, " (the default if more than one FLAC file is specified)\n");
- fprintf(out, "--no-filename Do not prefix each output line with the FLAC file name\n");
- fprintf(out, " (the default if only one FLAC file is specified)\n");
- fprintf(out, "--no-utf8-convert Do not convert Vorbis comments from UTF-8 to local charset,\n");
- fprintf(out, " or vice versa. This is useful for scripts.\n");
- fprintf(out, "--dont-use-padding By default metaflac tries to use padding where possible\n");
- fprintf(out, " to avoid rewriting the entire file if the metadata size\n");
- fprintf(out, " changes. Use this option to tell metaflac to not take\n");
- fprintf(out, " advantage of padding this way.\n");
-}
-
-int short_usage(const char *message, ...)
-{
- va_list args;
-
- if(message) {
- va_start(args, message);
-
- (void) vfprintf(stderr, message, args);
-
- va_end(args);
-
- }
- usage_header(stderr);
- fprintf(stderr, "\n");
- fprintf(stderr, "This is the short help; for full help use 'metaflac --help'\n");
- fprintf(stderr, "\n");
- usage_summary(stderr);
-
- return message? 1 : 0;
-}
-
-int long_usage(const char *message, ...)
-{
- FILE *out = (message? stderr : stdout);
- va_list args;
-
- if(message) {
- va_start(args, message);
-
- (void) vfprintf(stderr, message, args);
-
- va_end(args);
-
- }
- usage_header(out);
- fprintf(out, "\n");
- usage_summary(out);
- fprintf(out, "\n");
- fprintf(out, "Shorthand operations:\n");
- fprintf(out, "--show-md5sum Show the MD5 signature from the STREAMINFO block.\n");
- fprintf(out, "--show-min-blocksize Show the minimum block size from the STREAMINFO block.\n");
- fprintf(out, "--show-max-blocksize Show the maximum block size from the STREAMINFO block.\n");
- fprintf(out, "--show-min-framesize Show the minimum frame size from the STREAMINFO block.\n");
- fprintf(out, "--show-max-framesize Show the maximum frame size from the STREAMINFO block.\n");
- fprintf(out, "--show-sample-rate Show the sample rate from the STREAMINFO block.\n");
- fprintf(out, "--show-channels Show the number of channels from the STREAMINFO block.\n");
- fprintf(out, "--show-bps Show the # of bits per sample from the STREAMINFO block.\n");
- fprintf(out, "--show-total-samples Show the total # of samples from the STREAMINFO block.\n");
- fprintf(out, "\n");
- fprintf(out, "--show-vc-vendor Show the vendor string from the VORBIS_COMMENT block.\n");
- fprintf(out, "--show-vc-field=name Show all Vorbis comment fields where the the field name\n");
- fprintf(out, " matches 'name'.\n");
- fprintf(out, "--remove-vc-field=name\n");
- fprintf(out, " Remove all Vorbis comment fields whose field name is\n");
- fprintf(out, " 'name'.\n");
- fprintf(out, "--remove-vc-firstfield=name\n");
- fprintf(out, " Remove first Vorbis comment field whose field name is\n");
- fprintf(out, " 'name'.\n");
- fprintf(out, "--remove-vc-all Remove all Vorbis comment fields, leaving only the\n");
- fprintf(out, " vendor string in the VORBIS_COMMENT block.\n");
- fprintf(out, "--set-vc-field=field Add a Vorbis comment field. The field must comply with\n");
- fprintf(out, " the Vorbis comment spec, of the form \"NAME=VALUE\". If\n");
- fprintf(out, " there is currently no VORBIS_COMMENT block, one will be\n");
- fprintf(out, " created.\n");
- fprintf(out, "--import-vc-from=file Import Vorbis comments from a file. Use '-' for stdin.\n");
- fprintf(out, " Each line should be of the form NAME=VALUE. Multi-\n");
- fprintf(out, " line comments are currently not supported. Specify\n");
- fprintf(out, " --remove-vc-all and/or --no-utf8-convert before\n");
- fprintf(out, " --import-vc-from if necessary.\n");
- fprintf(out, "--export-vc-to=file Export Vorbis comments to a file. Use '-' for stdin.\n");
- fprintf(out, " Each line will be of the form NAME=VALUE. Specify\n");
- fprintf(out, " --no-utf8-convert if necessary.\n");
- fprintf(out, "--add-replay-gain Calculates the title and album gains/peaks of the given\n");
- fprintf(out, " FLAC files as if all the files were part of one album,\n");
- fprintf(out, " then stores them in the VORBIS_COMMENT block. The tags\n");
- fprintf(out, " are the same as those used by vorbisgain. Existing\n");
- fprintf(out, " ReplayGain tags will be replaced. If only one FLAC file\n");
- fprintf(out, " is given, the album and title gains will be the same.\n");
- fprintf(out, " Since this operation requires two passes, it is always\n");
- fprintf(out, " executed last, after all other operations have been\n");
- fprintf(out, " completed and written to disk. All FLAC files specified\n");
- fprintf(out, " must have the same resolution, sample rate, and number\n");
- fprintf(out, " of channels. The sample rate must be one of 8, 11.025,\n");
- fprintf(out, " 12, 16, 22.05, 24, 32, 44.1, or 48 kHz.\n");
- fprintf(out, "--add-padding=length Add a padding block of the given length (in bytes).\n");
- fprintf(out, " The overall length of the new block will be 4 + length;\n");
- fprintf(out, " the extra 4 bytes is for the metadata block header.\n");
- fprintf(out, "\n");
- fprintf(out, "Major operations:\n");
- fprintf(out, "--version\n");
- fprintf(out, " Show the metaflac version number.\n");
- fprintf(out, "--list\n");
- fprintf(out, " List the contents of one or more metadata blocks to stdout. By default,\n");
- fprintf(out, " all metadata blocks are listed in text format. Use the following options\n");
- fprintf(out, " to change this behavior:\n");
- fprintf(out, "\n");
- fprintf(out, " --block-number=#[,#[...]]\n");
- fprintf(out, " An optional comma-separated list of block numbers to display. The first\n");
- fprintf(out, " block, the STREAMINFO block, is block 0.\n");
- fprintf(out, "\n");
- fprintf(out, " --block-type=type[,type[...]]\n");
- fprintf(out, " --except-block-type=type[,type[...]]\n");
- fprintf(out, " An optional comma-separated list of block types to included or ignored\n");
- fprintf(out, " with this option. Use only one of --block-type or --except-block-type.\n");
- fprintf(out, " The valid block types are: STREAMINFO, PADDING, APPLICATION, SEEKTABLE,\n");
- fprintf(out, " VORBIS_COMMENT. You may narrow down the types of APPLICATION blocks\n");
- fprintf(out, " displayed as follows:\n");
- fprintf(out, " APPLICATION:abcd The APPLICATION block(s) whose textual repre-\n");
- fprintf(out, " sentation of the 4-byte ID is \"abcd\"\n");
- fprintf(out, " APPLICATION:0xXXXXXXXX The APPLICATION block(s) whose hexadecimal big-\n");
- fprintf(out, " endian representation of the 4-byte ID is\n");
- fprintf(out, " \"0xXXXXXXXX\". For the example \"abcd\" above the\n");
- fprintf(out, " hexadecimal equivalalent is 0x61626364\n");
- fprintf(out, "\n");
- fprintf(out, " NOTE: if both --block-number and --[except-]block-type are specified,\n");
- fprintf(out, " the result is the logical AND of both arguments.\n");
- fprintf(out, "\n");
-#if 0
- /*@@@ not implemented yet */
- fprintf(out, " --data-format=binary|text\n");
- fprintf(out, " By default a human-readable text representation of the data is displayed.\n");
- fprintf(out, " You may specify --data-format=binary to dump the raw binary form of each\n");
- fprintf(out, " metadata block. The output can be read in using a subsequent call to\n");
- fprintf(out, " "metaflac --append --from-file=..."\n");
- fprintf(out, "\n");
-#endif
- fprintf(out, " --application-data-format=hexdump|text\n");
- fprintf(out, " If the application block you are displaying contains binary data but your\n");
- fprintf(out, " --data-format=text, you can display a hex dump of the application data\n");
- fprintf(out, " contents instead using --application-data-format=hexdump\n");
- fprintf(out, "\n");
-#if 0
- /*@@@ not implemented yet */
- fprintf(out, "--append\n");
- fprintf(out, " Insert a metadata block from a file. The input file must be in the same\n");
- fprintf(out, " format as generated with --list.\n");
- fprintf(out, "\n");
- fprintf(out, " --block-number=#\n");
- fprintf(out, " Specify the insertion point (defaults to last block). The new block will\n");
- fprintf(out, " be added after the given block number. This prevents the illegal insertion\n");
- fprintf(out, " of a block before the first STREAMINFO block. You may not --append another\n");
- fprintf(out, " STREAMINFO block.\n");
- fprintf(out, "\n");
- fprintf(out, " --from-file=filename\n");
- fprintf(out, " Mandatory 'option' to specify the input file containing the block contents.\n");
- fprintf(out, "\n");
- fprintf(out, " --data-format=binary|text\n");
- fprintf(out, " By default the block contents are assumed to be in binary format. You can\n");
- fprintf(out, " override this by specifying --data-format=text\n");
- fprintf(out, "\n");
-#endif
- fprintf(out, "--remove\n");
- fprintf(out, " Remove one or more metadata blocks from the metadata. Unless\n");
- fprintf(out, " --dont-use-padding is specified, the blocks will be replaced with padding.\n");
- fprintf(out, " You may not remove the STREAMINFO block.\n");
- fprintf(out, "\n");
- fprintf(out, " --block-number=#[,#[...]]\n");
- fprintf(out, " --block-type=type[,type[...]]\n");
- fprintf(out, " --except-block-type=type[,type[...]]\n");
- fprintf(out, " See --list above for usage.\n");
- fprintf(out, "\n");
- fprintf(out, " NOTE: if both --block-number and --[except-]block-type are specified,\n");
- fprintf(out, " the result is the logical AND of both arguments.\n");
- fprintf(out, "\n");
- fprintf(out, "--remove-all\n");
- fprintf(out, " Remove all metadata blocks (except the STREAMINFO block) from the\n");
- fprintf(out, " metadata. Unless --dont-use-padding is specified, the blocks will be\n");
- fprintf(out, " replaced with padding.\n");
- fprintf(out, "\n");
- fprintf(out, "--merge-padding\n");
- fprintf(out, " Merge adjacent PADDING blocks into single blocks.\n");
- fprintf(out, "\n");
- fprintf(out, "--sort-padding\n");
- fprintf(out, " Move all PADDING blocks to the end of the metadata and merge them into a\n");
- fprintf(out, " single block.\n");
-
- return message? 1 : 0;
-}
-
-char *local_strdup(const char *source)
-{
- char *ret;
- FLAC__ASSERT(0 != source);
- if(0 == (ret = strdup(source)))
- die("out of memory during strdup()");
- return ret;
-}
-
-FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
-{
- unsigned i, d;
- int c;
- FLAC__ASSERT(0 != src);
- if(strlen(src) != 32)
- return false;
- /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
- for(i = 0; i < 16; i++) {
- c = (int)(*src++);
- if(isdigit(c))
- d = (unsigned)(c - '0');
- else if(c >= 'a' && c <= 'f')
- d = (unsigned)(c - 'a') + 10u;
- else if(c >= 'A' && c <= 'F')
- d = (unsigned)(c - 'A') + 10u;
- else
- return false;
- d <<= 4;
- c = (int)(*src++);
- if(isdigit(c))
- d |= (unsigned)(c - '0');
- else if(c >= 'a' && c <= 'f')
- d |= (unsigned)(c - 'a') + 10u;
- else if(c >= 'A' && c <= 'F')
- d |= (unsigned)(c - 'A') + 10u;
- else
- return false;
- dest[i] = (FLAC__byte)d;
- }
- return true;
-}
-
-FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
-{
- FLAC__ASSERT(0 != src);
- if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
- return false;
- *dest = strtoul(src, 0, 10);
- return true;
-}
-
-/* There's no stroull() in MSVC6 so we just write a specialized one */
-static FLAC__uint64 local__strtoull(const char *src)
-{
- FLAC__uint64 ret = 0;
- int c;
- FLAC__ASSERT(0 != src);
- while(0 != (c = *src++)) {
- c -= '0';
- if(c >= 0 && c <= 9)
- ret = (ret * 10) + c;
- else
- break;
- }
- return ret;
-}
-
-FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
-{
- FLAC__ASSERT(0 != src);
- if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
- return false;
- *dest = local__strtoull(src);
- return true;
-}
-
-FLAC__bool parse_filename(const char *src, char **dest)
-{
- if(0 == src || strlen(src) == 0)
- return false;
- *dest = strdup(src);
- return true;
-}
-
-FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
-{
- static const char * const violations[] = {
- "field name contains invalid character",
- "field contains no '=' character"
- };
-
- char *p, *q, *s;
-
- if(0 != field)
- *field = local_strdup(field_ref);
-
- s = local_strdup(field_ref);
-
- if(0 == (p = strchr(s, '='))) {
- free(s);
- *violation = violations[1];
- return false;
- }
- *p++ = '\0';
-
- for(q = s; *q; q++) {
- if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
- free(s);
- *violation = violations[0];
- return false;
- }
- }
-
- *name = local_strdup(s);
- *value = local_strdup(p);
- *length = strlen(p);
-
- free(s);
- return true;
-}
-
-static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
-{
- static const char * const violations[] = {
- "field name contains invalid character"
- };
-
- char *q, *s;
-
- s = local_strdup(field_ref);
-
- for(q = s; *q; q++) {
- if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
- free(s);
- *violation = violations[0];
- return false;
- }
- }
-
- *name = s;
-
- return true;
-}
-
-FLAC__bool parse_add_padding(const char *in, unsigned *out)
-{
- *out = (unsigned)strtoul(in, 0, 10);
- return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN);
-}
-
-FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
-{
- char *p, *q, *s, *end;
- long i;
- unsigned entry;
-
- if(*in == '\0')
- return false;
-
- s = local_strdup(in);
-
- /* first count the entries */
- for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
- ;
-
- /* make space */
- FLAC__ASSERT(out->num_entries > 0);
- if(0 == (out->entries = malloc(sizeof(unsigned) * out->num_entries)))
- die("out of memory allocating space for option list");
-
- /* load 'em up */
- entry = 0;
- q = s;
- while(q) {
- FLAC__ASSERT(entry < out->num_entries);
- if(0 != (p = strchr(q, ',')))
- *p++ = '\0';
- if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
- free(s);
- return false;
- }
- out->entries[entry++] = (unsigned)i;
- q = p;
- }
- FLAC__ASSERT(entry == out->num_entries);
-
- free(s);
- return true;
-}
-
-FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
-{
- char *p, *q, *r, *s;
- unsigned entry;
-
- if(*in == '\0')
- return false;
-
- s = local_strdup(in);
-
- /* first count the entries */
- for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
- ;
-
- /* make space */
- FLAC__ASSERT(out->num_entries > 0);
- if(0 == (out->entries = malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
- die("out of memory allocating space for option list");
-
- /* load 'em up */
- entry = 0;
- q = s;
- while(q) {
- FLAC__ASSERT(entry < out->num_entries);
- if(0 != (p = strchr(q, ',')))
- *p++ = 0;
- r = strchr(q, ':');
- if(r)
- *r++ = '\0';
- if(0 != r && 0 != strcmp(q, "APPLICATION")) {
- free(s);
- return false;
- }
- if(0 == strcmp(q, "STREAMINFO")) {
- out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
- }
- else if(0 == strcmp(q, "PADDING")) {
- out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
- }
- else if(0 == strcmp(q, "APPLICATION")) {
- out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
- out->entries[entry].filter_application_by_id = (0 != r);
- if(0 != r) {
- if(strlen(r) == 4) {
- strcpy(out->entries[entry].application_id, r);
- }
- else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
- FLAC__uint32 x = strtoul(r+2, 0, 16);
- out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff);
- out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff);
- out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff);
- out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff);
- }
- else {
- free(s);
- return false;
- }
- }
- entry++;
- }
- else if(0 == strcmp(q, "SEEKTABLE")) {
- out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
- }
- else if(0 == strcmp(q, "VORBIS_COMMENT")) {
- out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
- }
- else {
- free(s);
- return false;
- }
- q = p;
- }
- FLAC__ASSERT(entry == out->num_entries);
-
- free(s);
- return true;
-}
-
-FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
-{
- if(0 == strcmp(in, "binary"))
- out->is_binary = true;
- else if(0 == strcmp(in, "text"))
- out->is_binary = false;