0f38b6f159dbcfd69b259ce57751d257a4689186
[platform/upstream/flac.git] / src / metaflac / options.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002,2003,2004,2005  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #include "options.h"
20 #include "usage.h"
21 #include "utils.h"
22 #include "FLAC/assert.h"
23 #include "share/grabbag/replaygain.h"
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /*
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.
32 */
33 struct share__option long_options_[] = {
34         /* global 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 */
83         { "help", 0, 0, 0 },
84         { "version", 0, 0, 0 },
85         { "list", 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 },
98         {0, 0, 0, 0}
99 };
100
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);
120
121
122 void init_options(CommandLineOptions *options)
123 {
124         options->preserve_modtime = false;
125
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;
129
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;
136
137         options->ops.operations = 0;
138         options->ops.num_operations = 0;
139         options->ops.capacity = 0;
140
141         options->args.arguments = 0;
142         options->args.num_arguments = 0;
143         options->args.capacity = 0;
144
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;
149
150         options->num_files = 0;
151         options->filenames = 0;
152 }
153
154 FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
155 {
156         int ret;
157         int option_index = 1;
158         FLAC__bool had_error = false;
159
160         while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) {
161                 switch (ret) {
162                         case 0:
163                                 had_error |= !parse_option(option_index, share__optarg, options);
164                                 break;
165                         case '?':
166                         case ':':
167                                 had_error = true;
168                                 break;
169                         default:
170                                 FLAC__ASSERT(0);
171                                 break;
172                 }
173         }
174
175         if(options->prefix_with_filename == 2)
176                 options->prefix_with_filename = (argc - share__optind > 1);
177
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");
181                 had_error = true;
182         }
183
184         options->num_files = argc - share__optind;
185
186         if(options->num_files > 0) {
187                 unsigned i = 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++]);
192         }
193
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");
197                         had_error = true;
198                 }
199                 else if(options->args.checks.num_shorthand_ops > 0) {
200                         fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
201                         had_error = true;
202                 }
203         }
204
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");
208                 had_error = true;
209         }
210
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");
213                 had_error = true;
214         }
215
216         if(had_error)
217                 short_usage(0);
218
219         /*
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:
223          */
224         if(options->cued_seekpoints) {
225                 Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
226                 if(0 != op) {
227                         Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
228                         if(0 == op2)
229                                 op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
230                         op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint);
231                 }
232         }
233
234         return !had_error;
235 }
236
237 void free_options(CommandLineOptions *options)
238 {
239         unsigned i;
240         Operation *op;
241         Argument *arg;
242
243         FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0);
244         FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0);
245
246         for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) {
247                 switch(op->type) {
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);
253                                 break;
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);
261                                 break;
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);
267                                 break;
268                         case OP__IMPORT_CUESHEET_FROM:
269                                 if(0 != op->argument.import_cuesheet_from.filename)
270                                         free(op->argument.import_cuesheet_from.filename);
271                                 break;
272                         case OP__ADD_SEEKPOINT:
273                                 if(0 != op->argument.add_seekpoint.specification)
274                                         free(op->argument.add_seekpoint.specification);
275                                 break;
276                         default:
277                                 break;
278                 }
279         }
280
281         for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) {
282                 switch(arg->type) {
283                         case ARG__BLOCK_NUMBER:
284                                 if(0 != arg->value.block_number.entries)
285                                         free(arg->value.block_number.entries);
286                                 break;
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);
291                                 break;
292                         case ARG__FROM_FILE:
293                                 if(0 != arg->value.from_file.file_name)
294                                         free(arg->value.from_file.file_name);
295                                 break;
296                         default:
297                                 break;
298                 }
299         }
300
301         if(0 != options->ops.operations)
302                 free(options->ops.operations);
303
304         if(0 != options->args.arguments)
305                 free(options->args.arguments);
306
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]);
311                 }
312                 free(options->filenames);
313         }
314 }
315
316 /*
317  * local routines
318  */
319
320 FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options)
321 {
322         const char *opt = long_options_[option_index].name;
323         Operation *op;
324         Argument *arg;
325         FLAC__bool ok = true;
326
327         if(0 == strcmp(opt, "preserve-modtime")) {
328                 options->preserve_modtime = true;
329         }
330         else if(0 == strcmp(opt, "with-filename")) {
331                 options->prefix_with_filename = true;
332         }
333         else if(0 == strcmp(opt, "no-filename")) {
334                 options->prefix_with_filename = false;
335         }
336         else if(0 == strcmp(opt, "no-utf8-convert")) {
337                 options->utf8_convert = false;
338         }
339         else if(0 == strcmp(opt, "dont-use-padding")) {
340                 options->use_padding = false;
341         }
342         else if(0 == strcmp(opt, "no-cued-seekpoints")) {
343                 options->cued_seekpoints = false;
344         }
345         else if(0 == strcmp(opt, "show-md5sum")) {
346                 (void) append_shorthand_operation(options, OP__SHOW_MD5SUM);
347         }
348         else if(0 == strcmp(opt, "show-min-blocksize")) {
349                 (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE);
350         }
351         else if(0 == strcmp(opt, "show-max-blocksize")) {
352                 (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE);
353         }
354         else if(0 == strcmp(opt, "show-min-framesize")) {
355                 (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE);
356         }
357         else if(0 == strcmp(opt, "show-max-framesize")) {
358                 (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE);
359         }
360         else if(0 == strcmp(opt, "show-sample-rate")) {
361                 (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE);
362         }
363         else if(0 == strcmp(opt, "show-channels")) {
364                 (void) append_shorthand_operation(options, OP__SHOW_CHANNELS);
365         }
366         else if(0 == strcmp(opt, "show-bps")) {
367                 (void) append_shorthand_operation(options, OP__SHOW_BPS);
368         }
369         else if(0 == strcmp(opt, "show-total-samples")) {
370                 (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES);
371         }
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);
377                         ok = false;
378                 }
379                 else
380                         undocumented_warning(opt);
381         }
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);
386                         ok = false;
387                 }
388                 else
389                         undocumented_warning(opt);
390         }
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);
395                         ok = false;
396                 }
397                 else
398                         undocumented_warning(opt);
399         }
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);
404                         ok = false;
405                 }
406                 else
407                         undocumented_warning(opt);
408         }
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);
413                         ok = false;
414                 }
415                 else
416                         undocumented_warning(opt);
417         }
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);
422                         ok = false;
423                 }
424                 else
425                         undocumented_warning(opt);
426         }
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);
431                         ok = false;
432                 }
433                 else
434                         undocumented_warning(opt);
435         }
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);
440                         ok = false;
441                 }
442                 else
443                         undocumented_warning(opt);
444         }
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);
449                         ok = false;
450                 }
451                 else
452                         undocumented_warning(opt);
453         }
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);
458         }
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);
468                         ok = false;
469                 }
470         }
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);
475         }
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);
485                         ok = false;
486                 }
487         }
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);
497                         ok = false;
498                 }
499         }
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);
509                         ok = false;
510                 }
511         }
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);
519                         ok = false;
520                 }
521         }
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);
529                         ok = false;
530                 }
531         }
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);
535                         ok = false;
536                 }
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);
541                         ok = false;
542                 }
543         }
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);
549                         ok = false;
550                 }
551         }
552         else if(0 == strcmp(opt, "add-seekpoint")) {
553                 const char *violation;
554                 char *spec;
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);
559                         ok = false;
560                 }
561                 else {
562                         op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
563                         if(0 == op)
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), ";");
567                         free(spec);
568                 }
569         }
570         else if(0 == strcmp(opt, "add-replay-gain")) {
571                 (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
572         }
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
579                 };
580                 size_t i;
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]);
584                 }
585         }
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);
591                         ok = false;
592                 }
593         }
594         else if(0 == strcmp(opt, "help")) {
595                 options->show_long_help = true;
596         }
597         else if(0 == strcmp(opt, "version")) {
598                 options->show_version = true;
599         }
600         else if(0 == strcmp(opt, "list")) {
601                 (void) append_major_operation(options, OP__LIST);
602         }
603         else if(0 == strcmp(opt, "append")) {
604                 (void) append_major_operation(options, OP__APPEND);
605         }
606         else if(0 == strcmp(opt, "remove")) {
607                 (void) append_major_operation(options, OP__REMOVE);
608         }
609         else if(0 == strcmp(opt, "remove-all")) {
610                 (void) append_major_operation(options, OP__REMOVE_ALL);
611         }
612         else if(0 == strcmp(opt, "merge-padding")) {
613                 (void) append_major_operation(options, OP__MERGE_PADDING);
614         }
615         else if(0 == strcmp(opt, "sort-padding")) {
616                 (void) append_major_operation(options, OP__SORT_PADDING);
617         }
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);
623                         ok = false;
624                 }
625         }
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);
631                         ok = false;
632                 }
633                 options->args.checks.has_block_type = true;
634         }
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);
640                         ok = false;
641                 }
642                 options->args.checks.has_except_block_type = true;
643         }
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);
649                         ok = false;
650                 }
651         }
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);
656                         ok = false;
657                 }
658         }
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);
663         }
664         else {
665                 FLAC__ASSERT(0);
666         }
667
668         return ok;
669 }
670
671 void append_new_operation(CommandLineOptions *options, Operation operation)
672 {
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);
678         }
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));
685         }
686
687         options->ops.operations[options->ops.num_operations++] = operation;
688 }
689
690 void append_new_argument(CommandLineOptions *options, Argument argument)
691 {
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);
697         }
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));
704         }
705
706         options->args.arguments[options->args.num_arguments++] = argument;
707 }
708
709 Operation *append_major_operation(CommandLineOptions *options, OperationType type)
710 {
711         Operation op;
712         memset(&op, 0, sizeof(op));
713         op.type = type;
714         append_new_operation(options, op);
715         options->args.checks.num_major_ops++;
716         return options->ops.operations + (options->ops.num_operations - 1);
717 }
718
719 Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type)
720 {
721         Operation op;
722         memset(&op, 0, sizeof(op));
723         op.type = type;
724         append_new_operation(options, op);
725         options->args.checks.num_shorthand_ops++;
726         return options->ops.operations + (options->ops.num_operations - 1);
727 }
728
729 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
730 {
731         unsigned i;
732         for(i = 0; i < options->ops.num_operations; i++)
733                 if(options->ops.operations[i].type == type)
734                         return &options->ops.operations[i];
735         return 0;
736 }
737
738 Argument *append_argument(CommandLineOptions *options, ArgumentType type)
739 {
740         Argument arg;
741         memset(&arg, 0, sizeof(arg));
742         arg.type = type;
743         append_new_argument(options, arg);
744         return options->args.arguments + (options->args.num_arguments - 1);
745 }
746
747 FLAC__bool parse_md5(const char *src, FLAC__byte dest[16])
748 {
749         unsigned i, d;
750         int c;
751         FLAC__ASSERT(0 != src);
752         if(strlen(src) != 32)
753                 return false;
754         /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */
755         for(i = 0; i < 16; i++) {
756                 c = (int)(*src++);
757                 if(isdigit(c))
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;
763                 else
764                         return false;
765                 d <<= 4;
766                 c = (int)(*src++);
767                 if(isdigit(c))
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;
773                 else
774                         return false;
775                 dest[i] = (FLAC__byte)d;
776         }
777         return true;
778 }
779
780 FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
781 {
782         FLAC__ASSERT(0 != src);
783         if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
784                 return false;
785         *dest = strtoul(src, 0, 10);
786         return true;
787 }
788
789 /* There's no stroull() in MSVC6 so we just write a specialized one */
790 static FLAC__uint64 local__strtoull(const char *src)
791 {
792         FLAC__uint64 ret = 0;
793         int c;
794         FLAC__ASSERT(0 != src);
795         while(0 != (c = *src++)) {
796                 c -= '0';
797                 if(c >= 0 && c <= 9)
798                         ret = (ret * 10) + c;
799                 else
800                         break;
801         }
802         return ret;
803 }
804
805 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
806 {
807         FLAC__ASSERT(0 != src);
808         if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
809                 return false;
810         *dest = local__strtoull(src);
811         return true;
812 }
813
814 FLAC__bool parse_filename(const char *src, char **dest)
815 {
816         if(0 == src || strlen(src) == 0)
817                 return false;
818         *dest = strdup(src);
819         return true;
820 }
821
822 FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation)
823 {
824         static const char * const violations[] = {
825                 "field name contains invalid character"
826         };
827
828         char *q, *s;
829
830         s = local_strdup(field_ref);
831
832         for(q = s; *q; q++) {
833                 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
834                         free(s);
835                         *violation = violations[0];
836                         return false;
837                 }
838         }
839
840         *name = s;
841
842         return true;
843 }
844
845 FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation)
846 {
847         static const char *garbled_ = "garbled specification";
848         const unsigned n = strlen(in);
849
850         FLAC__ASSERT(0 != in);
851         FLAC__ASSERT(0 != out);
852
853         if(n == 0) {
854                 *violation = "specification is empty";
855                 return false;
856         }
857
858         if(n > strspn(in, "0123456789.Xsx")) {
859                 *violation = "specification contains invalid character";
860                 return false;
861         }
862
863         if(in[n-1] == 'X') {
864                 if(n > 1) {
865                         *violation = garbled_;
866                         return false;
867                 }
868         }
869         else if(in[n-1] == 's') {
870                 if(n-1 > strspn(in, "0123456789.")) {
871                         *violation = garbled_;
872                         return false;
873                 }
874         }
875         else if(in[n-1] == 'x') {
876                 if(n-1 > strspn(in, "0123456789")) {
877                         *violation = garbled_;
878                         return false;
879                 }
880         }
881         else {
882                 if(n > strspn(in, "0123456789")) {
883                         *violation = garbled_;
884                         return false;
885                 }
886         }
887
888         *out = local_strdup(in);
889         return true;
890 }
891
892 FLAC__bool parse_add_padding(const char *in, unsigned *out)
893 {
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);
898 }
899
900 FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
901 {
902         char *p, *q, *s, *end;
903         long i;
904         unsigned entry;
905
906         if(*in == '\0')
907                 return false;
908
909         s = local_strdup(in);
910
911         /* first count the entries */
912         for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
913                 ;
914
915         /* make space */
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");
919
920         /* load 'em up */
921         entry = 0;
922         q = s;
923         while(q) {
924                 FLAC__ASSERT(entry < out->num_entries);
925                 if(0 != (p = strchr(q, ',')))
926                         *p++ = '\0';
927                 if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) {
928                         free(s);
929                         return false;
930                 }
931                 out->entries[entry++] = (unsigned)i;
932                 q = p;
933         }
934         FLAC__ASSERT(entry == out->num_entries);
935
936         free(s);
937         return true;
938 }
939
940 FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
941 {
942         char *p, *q, *r, *s;
943         unsigned entry;
944
945         if(*in == '\0')
946                 return false;
947
948         s = local_strdup(in);
949
950         /* first count the entries */
951         for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ','))
952                 ;
953
954         /* make space */
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");
958
959         /* load 'em up */
960         entry = 0;
961         q = s;
962         while(q) {
963                 FLAC__ASSERT(entry < out->num_entries);
964                 if(0 != (p = strchr(q, ',')))
965                         *p++ = 0;
966                 r = strchr(q, ':');
967                 if(r)
968                         *r++ = '\0';
969                 if(0 != r && 0 != strcmp(q, "APPLICATION")) {
970                         free(s);
971                         return false;
972                 }
973                 if(0 == strcmp(q, "STREAMINFO")) {
974                         out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO;
975                 }
976                 else if(0 == strcmp(q, "PADDING")) {
977                         out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING;
978                 }
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);
982                         if(0 != r) {
983                                 if(strlen(r) == 4) {
984                                         strcpy(out->entries[entry].application_id, r);
985                                 }
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);
992                                 }
993                                 else {
994                                         free(s);
995                                         return false;
996                                 }
997                         }
998                         entry++;
999                 }
1000                 else if(0 == strcmp(q, "SEEKTABLE")) {
1001                         out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE;
1002                 }
1003                 else if(0 == strcmp(q, "VORBIS_COMMENT")) {
1004                         out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1005                 }
1006                 else if(0 == strcmp(q, "CUESHEET")) {
1007                         out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
1008                 }
1009                 else {
1010                         free(s);
1011                         return false;
1012                 }
1013                 q = p;
1014         }
1015         FLAC__ASSERT(entry == out->num_entries);
1016
1017         free(s);
1018         return true;
1019 }
1020
1021 FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out)
1022 {
1023         if(0 == strcmp(in, "binary"))
1024                 out->is_binary = true;
1025         else if(0 == strcmp(in, "text"))
1026                 out->is_binary = false;
1027         else
1028                 return false;
1029         return true;
1030 }
1031
1032 FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
1033 {
1034         if(0 == strcmp(in, "hexdump"))
1035                 *out = true;
1036         else if(0 == strcmp(in, "text"))
1037                 *out = false;
1038         else
1039                 return false;
1040         return true;
1041 }
1042
1043 void undocumented_warning(const char *opt)
1044 {
1045         fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n         only for repairing a damaged STREAMINFO block\n", opt);
1046 }