| 1 |
/* |
| 2 |
* Gregorio is a program that translates gabc files to GregorioTeX |
| 3 |
* This file implements the command line interface of Gregorio. |
| 4 |
* |
| 5 |
* Copyright (C) 2006-2025 The Gregorio Project (see CONTRIBUTORS.md) |
| 6 |
* |
| 7 |
* This file is part of Gregorio. |
| 8 |
* |
| 9 |
* Gregorio is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* Gregorio is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with Gregorio. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
static const char *copyright = |
| 24 |
"Copyright (C) 2006-2025 Gregorio Project authors (see CONTRIBUTORS.md)"; |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
#include <stdio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#ifdef USE_KPSE |
| 31 |
#include <kpathsea/kpathsea.h> |
| 32 |
#else |
| 33 |
#include <getopt.h> |
| 34 |
#endif |
| 35 |
#include <string.h> /* for strcmp */ |
| 36 |
#include <locale.h> |
| 37 |
#include <limits.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <assert.h> |
| 40 |
#include "struct.h" |
| 41 |
#include "plugins.h" |
| 42 |
#include "messages.h" |
| 43 |
#include "characters.h" |
| 44 |
#include "support.h" |
| 45 |
#include "gabc/gabc.h" |
| 46 |
#include "vowel/vowel.h" |
| 47 |
|
| 48 |
#ifndef MODULE_PATH_ENV |
| 49 |
#define MODULE_PATH_ENV "MODULE_PATH" |
| 50 |
#endif |
| 51 |
|
| 52 |
typedef enum gregorio_file_format { |
| 53 |
FORMAT_UNSET = 0, |
| 54 |
GABC, |
| 55 |
GTEX, |
| 56 |
DUMP |
| 57 |
} gregorio_file_format; |
| 58 |
|
| 59 |
#define GABC_STR "gabc" |
| 60 |
#define GTEX_STR "gtex" |
| 61 |
#define DUMP_STR "dump" |
| 62 |
|
| 63 |
#define DEFAULT_INPUT_FORMAT GABC |
| 64 |
#define DEFAULT_OUTPUT_FORMAT GTEX |
| 65 |
|
| 66 |
/* realpath is not in mingw32 */ |
| 67 |
#ifdef _WIN32 |
| 68 |
/* _MAX_PATH is being passed for the maxLength (third) argument of _fullpath, |
| 69 |
* but we are always passing NULL for the absPath (first) argument, so it will |
| 70 |
* be ignored per the MSDN documentation */ |
| 71 |
#define gregorio_realpath(path,resolved_path) _fullpath(resolved_path,path,_MAX_PATH) |
| 72 |
#else |
| 73 |
#ifdef FUNC_REALPATH_WORKS |
| 74 |
#define gregorio_realpath(path,resolved_path) realpath(path,resolved_path) |
| 75 |
#else |
| 76 |
/* When realpath doesn't work (on an older system), we are forced to use |
| 77 |
* PATH_MAX to allocate a buffer */ |
| 78 |
#define gregorio_realpath(path,resolved_path) realpath(path,gregorio_malloc(PATH_MAX)) |
| 79 |
#endif /* FUNC_REALPATH_WORKS */ |
| 80 |
#endif /* _WIN32 */ |
| 81 |
|
| 82 |
/* define_path attempts to canonicalize the pathname of a given string */ |
| 83 |
static char *define_path(char *current_directory, char *string) |
| 84 |
{ |
| 85 |
int length; |
| 86 |
char *file_name; |
| 87 |
char *temp_name; |
| 88 |
char *base_name; |
| 89 |
#ifdef _WIN32 |
| 90 |
char *last_backslash; |
| 91 |
#endif |
| 92 |
|
| 93 |
temp_name = gregorio_strdup(string); |
| 94 |
base_name = strrchr(temp_name, '/'); |
| 95 |
#ifdef _WIN32 |
| 96 |
last_backslash = strrchr(temp_name, '\\'); |
| 97 |
if (last_backslash > base_name) { |
| 98 |
base_name = last_backslash; |
| 99 |
} |
| 100 |
#endif |
| 101 |
if (base_name) { |
| 102 |
/* some path was supplied */ |
| 103 |
|
| 104 |
*base_name = '\0'; |
| 105 |
base_name++; |
| 106 |
|
| 107 |
/* try to resolve it */ |
| 108 |
file_name = gregorio_realpath(temp_name, NULL); |
| 109 |
if (!file_name) { |
| 110 |
/* it's not reasonable to cover this failure in testing */ |
| 111 |
/* LCOV_EXCL_START */ |
| 112 |
fprintf(stderr, "the directory %s for %s does not exist\n", |
| 113 |
temp_name, base_name); |
| 114 |
gregorio_exit(1); |
| 115 |
/* LCOV_EXCL_STOP */ |
| 116 |
} |
| 117 |
} else { |
| 118 |
/* no path was supplied */ |
| 119 |
base_name = string; |
| 120 |
file_name = gregorio_malloc( |
| 121 |
strlen(current_directory) + strlen(base_name) + 2); |
| 122 |
strcpy(file_name, current_directory); |
| 123 |
} |
| 124 |
|
| 125 |
/* build the file name */ |
| 126 |
length = strlen(file_name); |
| 127 |
file_name = gregorio_realloc(file_name, length + strlen(base_name) + 2); |
| 128 |
file_name[length] = '/'; |
| 129 |
strcpy(file_name + length + 1, base_name); |
| 130 |
|
| 131 |
free(temp_name); |
| 132 |
return file_name; |
| 133 |
} |
| 134 |
|
| 135 |
/* function that returns the filename without the extension */ |
| 136 |
static char *get_base_filename(char *fbasename) |
| 137 |
{ |
| 138 |
char *p; |
| 139 |
int l; |
| 140 |
char *ret; |
| 141 |
p = strrchr(fbasename, '.'); |
| 142 |
if (!p || strchr(p, '/') || strchr(p, '\\')) { |
| 143 |
return gregorio_strdup(fbasename); |
| 144 |
} |
| 145 |
l = strlen(fbasename) - strlen(p); |
| 146 |
ret = (char *) gregorio_malloc(l + 1); |
| 147 |
gregorio_snprintf(ret, l + 1, "%s", fbasename); |
| 148 |
ret[l] = '\0'; |
| 149 |
return ret; |
| 150 |
} |
| 151 |
|
| 152 |
/* function that adds the good extension to a basename (without extension) */ |
| 153 |
static char *get_output_filename(char *fbasename, const char *extension) |
| 154 |
{ |
| 155 |
char *output_filename = NULL; |
| 156 |
output_filename = |
| 157 |
(char *) gregorio_malloc((strlen(extension) + strlen(fbasename) + 2)); |
| 158 |
output_filename = strcpy(output_filename, fbasename); |
| 159 |
output_filename = strcat(output_filename, "."); |
| 160 |
output_filename = strcat(output_filename, extension); |
| 161 |
return output_filename; |
| 162 |
} |
| 163 |
|
| 164 |
static void print_licence(void) |
| 165 |
{ |
| 166 |
printf("Gregorio: Gregorian chant score engraving.\n\ |
| 167 |
%s\n\ |
| 168 |
\n\ |
| 169 |
This program is free software: you can redistribute it and/or modify\n\ |
| 170 |
it under the terms of the GNU General Public License as published by\n\ |
| 171 |
the Free Software Foundation, either version 3 of the License, or\n\ |
| 172 |
(at your option) any later version.\n\ |
| 173 |
\n", copyright); |
| 174 |
printf("This program is distributed in the hope that it will be useful,\n\ |
| 175 |
but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
| 176 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
| 177 |
GNU General Public License for more details.\n\ |
| 178 |
\n\ |
| 179 |
You should have received a copy of the GNU General Public License\n\ |
| 180 |
along with this program. If not, see <http://www.gnu.org/licenses/>.\n\ |
| 181 |
"); |
| 182 |
} |
| 183 |
|
| 184 |
static void print_usage(char *name) |
| 185 |
{ |
| 186 |
printf(_("Usage: %s [OPTION]... [-s | INPUT_FILE]\n\ |
| 187 |
\nEngrave Gregorian chant scores, convert a gabc file to GregorioTeX.\n\n\ |
| 188 |
Options:\n\ |
| 189 |
-o, --output-file FILE write output to FILE,\n\ |
| 190 |
default is basename(INPUT_FILE).FORMAT\n\ |
| 191 |
-S, --stdout write output to stdout\n\ |
| 192 |
-s, --stdin read input from stdin\n\ |
| 193 |
-l, --messages-file FILE output messages to FILE (default: stderr)\n\ |
| 194 |
-F, --output-format FORMAT\n\ |
| 195 |
specify output format (default: gtex)\n"), name); |
| 196 |
printf(_(" -f, --input-format FORMAT\n\ |
| 197 |
specify input format (default: gabc)\n\ |
| 198 |
-p, --point-and-click generate Lilypond point and click information\n\ |
| 199 |
-h, --help print this help message\n\ |
| 200 |
-V, --version print version and exit\n")); |
| 201 |
printf(_("\ |
| 202 |
-L, --license print licence\n\ |
| 203 |
-v, --verbose verbose mode\n\ |
| 204 |
-W, --all-warnings output warnings\n\ |
| 205 |
-D, --deprecation-errors treat deprecation warnings as errors\n\ |
| 206 |
-d, --debug output debug information\n\ |
| 207 |
\n\ |
| 208 |
Formats:\n\ |
| 209 |
gabc gabc\n\ |
| 210 |
gtex GregorioTeX\n\ |
| 211 |
dump plain text dump (for debugging purpose)\n\ |
| 212 |
\n\ |
| 213 |
See <" PACKAGE_URL "> for general documentation,\n\ |
| 214 |
GregorioRef.pdf and GregorioNabcRef.pdf for full documentation.\ |
| 215 |
\n")); |
| 216 |
} |
| 217 |
|
| 218 |
static void print_short_usage(char *name) |
| 219 |
{ |
| 220 |
fprintf(stderr, "Usage: %s [OPTION]... [-s | INPUT_FILE]\n\ |
| 221 |
Try '%s --help' for more information.\n", name, name); |
| 222 |
} |
| 223 |
|
| 224 |
static void check_input_clobber(char *input_file_name, char *output_file_name) |
| 225 |
{ |
| 226 |
if (input_file_name && output_file_name) { |
| 227 |
char *absolute_input_file_name; |
| 228 |
char *absolute_output_file_name; |
| 229 |
char *current_directory; |
| 230 |
int file_cmp; |
| 231 |
size_t bufsize = 128; |
| 232 |
char *buf = gregorio_malloc(bufsize); |
| 233 |
while ((current_directory = getcwd(buf, bufsize)) == NULL |
| 234 |
&& errno == ERANGE && bufsize < MAX_BUF_GROWTH) { |
| 235 |
free(buf); |
| 236 |
bufsize <<= 1; |
| 237 |
buf = gregorio_malloc(bufsize); |
| 238 |
} |
| 239 |
if (current_directory == NULL) { |
| 240 |
fprintf(stderr, _("can't determine current directory")); |
| 241 |
free(buf); |
| 242 |
gregorio_exit(1); |
| 243 |
} |
| 244 |
absolute_input_file_name = define_path(current_directory, input_file_name); |
| 245 |
absolute_output_file_name = define_path(current_directory, output_file_name); |
| 246 |
file_cmp = strcmp(absolute_input_file_name, absolute_output_file_name); |
| 247 |
if (file_cmp == 0) { |
| 248 |
fprintf(stderr, "error: refusing to overwrite the input file\n"); |
| 249 |
} |
| 250 |
free(buf); |
| 251 |
free(absolute_input_file_name); |
| 252 |
free(absolute_output_file_name); |
| 253 |
if (file_cmp == 0) { |
| 254 |
gregorio_exit(1); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
static char *encode_point_and_click_filename(char *input_file_name) |
| 260 |
{ |
| 261 |
/* percent-encoding favors capital hex digits */ |
| 262 |
static const char *const hex = "0123456789ABCDEF"; |
| 263 |
char *filename, *result = NULL, *r = NULL, *p; |
| 264 |
|
| 265 |
filename = gregorio_realpath(input_file_name, NULL); |
| 266 |
if (!filename) { |
| 267 |
/* it's not reasonable to generate the system error that would cause |
| 268 |
* this to fail */ |
| 269 |
/* LCOV_EXCL_START */ |
| 270 |
fprintf(stderr, "error: unable to resolve %s\n", input_file_name); |
| 271 |
gregorio_exit(1); |
| 272 |
/* LCOV_EXCL_STOP */ |
| 273 |
} |
| 274 |
|
| 275 |
/* 2 extra characters for a possible leading slash and final NUL */ |
| 276 |
r = result = gregorio_malloc(strlen(filename) * 4 + 2); |
| 277 |
|
| 278 |
#ifdef _WIN32 |
| 279 |
*(r++) = '/'; |
| 280 |
#endif |
| 281 |
|
| 282 |
for (p = filename; *p; ++p) { |
| 283 |
#ifdef _WIN32 |
| 284 |
if (*p == '\\') { |
| 285 |
*p = '/'; |
| 286 |
} |
| 287 |
#endif |
| 288 |
|
| 289 |
/* note that -, _ and ~ are conspicuously missing from this list |
| 290 |
* because they cause trouble in TeX; we will percent-encode them */ |
| 291 |
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p < 'z') |
| 292 |
|| (*p >= '0' && *p <= '9') || *p == '.' || *p == '/' |
| 293 |
#ifdef _WIN32 |
| 294 |
|| *p == ':' |
| 295 |
#endif |
| 296 |
) { |
| 297 |
*(r++) = *p; |
| 298 |
} |
| 299 |
else { |
| 300 |
/* percent-encode anything else */ |
| 301 |
*(r++) = '\\'; /* must escape it because it's TeX */ |
| 302 |
*(r++) = '%'; |
| 303 |
*(r++) = hex[(*p >> 4) & 0x0FU]; |
| 304 |
*(r++) = hex[*p & 0x0FU]; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
*r = '\0'; |
| 309 |
|
| 310 |
free(filename); |
| 311 |
return result; |
| 312 |
} |
| 313 |
|
| 314 |
int main(int argc, char **argv) |
| 315 |
{ |
| 316 |
int c; |
| 317 |
|
| 318 |
char *input_file_name = NULL; |
| 319 |
char *output_file_name = NULL; |
| 320 |
char *output_basename = NULL; |
| 321 |
char *error_file_name = NULL; |
| 322 |
FILE *input_file = NULL; |
| 323 |
FILE *output_file = NULL; |
| 324 |
FILE *error_file = NULL; |
| 325 |
gregorio_file_format input_format = FORMAT_UNSET; |
| 326 |
gregorio_file_format output_format = FORMAT_UNSET; |
| 327 |
gregorio_verbosity verb_mode = 0; |
| 328 |
bool deprecation_errors = false; |
| 329 |
bool point_and_click = false; |
| 330 |
char *point_and_click_filename = NULL; |
| 331 |
bool debug = false; |
| 332 |
bool must_print_short_usage = false; |
| 333 |
int option_index = 0; |
| 334 |
static const char *const options = "o:SF:l:f:shOLVvWDpd"; |
| 335 |
static const struct option long_options[] = { |
| 336 |
{"output-file", 1, 0, 'o'}, |
| 337 |
{"stdout", 0, 0, 'S'}, |
| 338 |
{"output-format", 1, 0, 'F'}, |
| 339 |
{"messages-file", 1, 0, 'l'}, |
| 340 |
{"input-format", 1, 0, 'f'}, |
| 341 |
{"stdin", 0, 0, 's'}, |
| 342 |
{"help", 0, 0, 'h'}, |
| 343 |
{"version", 0, 0, 'V'}, |
| 344 |
{"licence", 0, 0, 'L'}, |
| 345 |
{"verbose", 0, 0, 'v'}, |
| 346 |
{"all-warnings", 0, 0, 'W'}, |
| 347 |
{"deprecation-errors", 0, 0, 'D'}, |
| 348 |
{"point-and-click", 0, 0, 'p'}, |
| 349 |
{"debug", 0, 0, 'd'}, |
| 350 |
}; |
| 351 |
gregorio_score *score = NULL; |
| 352 |
|
| 353 |
gregorio_support_init("gregorio", argv[0]); |
| 354 |
setlocale(LC_CTYPE, "C"); |
| 355 |
|
| 356 |
/* need to look for the -l option up front */ |
| 357 |
for (;;) { |
| 358 |
c = getopt_long(argc, argv, options, |
| 359 |
long_options, &option_index); |
| 360 |
if (c == -1) |
| 361 |
break; |
| 362 |
|
| 363 |
if (c == 'l') { |
| 364 |
if (error_file_name) { |
| 365 |
fprintf(stderr, |
| 366 |
"warning: several error files declared, %s taken\n", |
| 367 |
error_file_name); |
| 368 |
must_print_short_usage = true; |
| 369 |
} else { |
| 370 |
error_file_name = optarg; |
| 371 |
} |
| 372 |
} |
| 373 |
} /* end of for */ |
| 374 |
|
| 375 |
if (error_file_name) { |
| 376 |
gregorio_check_file_access(write, error_file_name, ERROR, |
| 377 |
gregorio_exit(1)); |
| 378 |
/* First test if we can open error_file_name for writing, so |
| 379 |
that if there is an error, we can still write the message |
| 380 |
to stderr. */ |
| 381 |
error_file = fopen(error_file_name, "w"); |
| 382 |
if (!error_file) { |
| 383 |
fprintf(stderr, "error: can't open file %s for writing\n", |
| 384 |
error_file_name); |
| 385 |
gregorio_exit(1); |
| 386 |
} |
| 387 |
fclose(error_file); |
| 388 |
error_file = freopen(error_file_name, "w", stderr); |
| 389 |
if (!error_file) { |
| 390 |
gregorio_exit(1); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
optind = 1; |
| 395 |
for (;;) { |
| 396 |
c = getopt_long(argc, argv, options, |
| 397 |
long_options, &option_index); |
| 398 |
if (c == -1) |
| 399 |
break; |
| 400 |
switch (c) { |
| 401 |
case 'o': |
| 402 |
if (output_file_name) { |
| 403 |
fprintf(stderr, |
| 404 |
"warning: several output files declared, %s taken\n", |
| 405 |
output_file_name); |
| 406 |
must_print_short_usage = true; |
| 407 |
break; |
| 408 |
} |
| 409 |
if (output_file) { /* means that stdout is defined */ |
| 410 |
fprintf(stderr, |
| 411 |
"warning: can't write to file and stdout, writing on stdout\n"); |
| 412 |
must_print_short_usage = true; |
| 413 |
break; |
| 414 |
} |
| 415 |
output_file_name = optarg; |
| 416 |
break; |
| 417 |
case 'S': |
| 418 |
if (output_file_name) { |
| 419 |
fprintf(stderr, |
| 420 |
"warning: can't write to file and stdout, writing on %s\n", |
| 421 |
output_file_name); |
| 422 |
must_print_short_usage = true; |
| 423 |
break; |
| 424 |
} |
| 425 |
if (output_file) { /* means that stdout is defined */ |
| 426 |
fprintf(stderr, "warning: option used several times: %c\n", c); |
| 427 |
must_print_short_usage = true; |
| 428 |
break; |
| 429 |
} |
| 430 |
output_file = stdout; |
| 431 |
break; |
| 432 |
case 'F': |
| 433 |
if (output_format) { |
| 434 |
fprintf(stderr, |
| 435 |
"warning: several output formats declared, first taken\n"); |
| 436 |
must_print_short_usage = true; |
| 437 |
break; |
| 438 |
} |
| 439 |
if (!strcmp(optarg, GABC_STR)) { |
| 440 |
output_format = GABC; |
| 441 |
break; |
| 442 |
} |
| 443 |
if (!strcmp(optarg, GTEX_STR)) { |
| 444 |
output_format = GTEX; |
| 445 |
break; |
| 446 |
} |
| 447 |
if (!strcmp(optarg, DUMP_STR)) { |
| 448 |
output_format = DUMP; |
| 449 |
break; |
| 450 |
} else { |
| 451 |
fprintf(stderr, "error: unknown output format: %s\n", optarg); |
| 452 |
print_short_usage(argv[0]); |
| 453 |
gregorio_exit(1); |
| 454 |
} |
| 455 |
break; |
| 456 |
case 'l': |
| 457 |
break; |
| 458 |
case 'f': |
| 459 |
if (input_format) { |
| 460 |
fprintf(stderr, |
| 461 |
"warning: several output formats declared, first taken\n"); |
| 462 |
must_print_short_usage = true; |
| 463 |
break; |
| 464 |
} |
| 465 |
if (!strcmp(optarg, GABC_STR)) { |
| 466 |
input_format = GABC; |
| 467 |
break; |
| 468 |
} else { |
| 469 |
fprintf(stderr, "error: unknown input format: %s\n", optarg); |
| 470 |
print_short_usage(argv[0]); |
| 471 |
gregorio_exit(1); |
| 472 |
} |
| 473 |
break; |
| 474 |
case 's': |
| 475 |
/* input_file_name will be null here because of the way |
| 476 |
* we use getopt_long */ |
| 477 |
assert(!input_file_name); |
| 478 |
if (input_file) { /* means that stdin is defined */ |
| 479 |
fprintf(stderr, "warning: option used several times: %c\n", c); |
| 480 |
must_print_short_usage = true; |
| 481 |
break; |
| 482 |
} |
| 483 |
input_file = stdin; |
| 484 |
break; |
| 485 |
case 'h': |
| 486 |
print_usage(argv[0]); |
| 487 |
gregorio_exit(0); |
| 488 |
break; |
| 489 |
case 'V': |
| 490 |
gregorio_print_version(copyright); |
| 491 |
gregorio_exit(0); |
| 492 |
break; |
| 493 |
case 'v': |
| 494 |
if (verb_mode && verb_mode != VERBOSITY_WARNING) { |
| 495 |
fprintf(stderr, "warning: verbose option passed several times\n"); |
| 496 |
must_print_short_usage = true; |
| 497 |
break; |
| 498 |
} |
| 499 |
verb_mode = VERBOSITY_INFO; |
| 500 |
break; |
| 501 |
case 'W': |
| 502 |
if (verb_mode == VERBOSITY_WARNING) { |
| 503 |
fprintf(stderr, |
| 504 |
"warning: all-warnings option passed several times\n"); |
| 505 |
must_print_short_usage = true; |
| 506 |
break; |
| 507 |
} |
| 508 |
if (verb_mode != VERBOSITY_INFO) { |
| 509 |
verb_mode = VERBOSITY_WARNING; |
| 510 |
} |
| 511 |
break; |
| 512 |
case 'D': |
| 513 |
if (deprecation_errors) { |
| 514 |
fprintf(stderr, "warning: deprecation-errors option passed " |
| 515 |
"several times\n"); |
| 516 |
must_print_short_usage = true; |
| 517 |
break; |
| 518 |
} |
| 519 |
deprecation_errors = true; |
| 520 |
break; |
| 521 |
case 'L': |
| 522 |
print_licence(); |
| 523 |
gregorio_exit(0); |
| 524 |
break; |
| 525 |
case 'p': |
| 526 |
if (point_and_click) { |
| 527 |
fprintf(stderr, |
| 528 |
"warning: point-and-click option passed several times\n"); |
| 529 |
must_print_short_usage = true; |
| 530 |
break; |
| 531 |
} |
| 532 |
point_and_click = true; |
| 533 |
break; |
| 534 |
case 'd': |
| 535 |
if (debug) { |
| 536 |
fprintf(stderr, |
| 537 |
"warning: debug option passed several times\n"); |
| 538 |
must_print_short_usage = true; |
| 539 |
break; |
| 540 |
} |
| 541 |
debug = true; |
| 542 |
break; |
| 543 |
case '?': |
| 544 |
must_print_short_usage = true; |
| 545 |
break; |
| 546 |
default: |
| 547 |
/* not reachable unless there's a programming error */ |
| 548 |
/* LCOV_EXCL_START */ |
| 549 |
gregorio_fail2(main, "unknown option: %c", c); |
| 550 |
print_short_usage(argv[0]); |
| 551 |
gregorio_exit(1); |
| 552 |
break; |
| 553 |
/* LCOV_EXCL_STOP */ |
| 554 |
} |
| 555 |
} /* end of for */ |
| 556 |
if (optind == argc) { |
| 557 |
if (!input_file) { /* input not undefined (could be stdin) */ |
| 558 |
fprintf(stderr, "%s: missing file operand.\n", argv[0]); |
| 559 |
print_short_usage(argv[0]); |
| 560 |
gregorio_exit(1); |
| 561 |
} |
| 562 |
} else { |
| 563 |
input_file_name = argv[optind++]; |
| 564 |
output_basename = get_base_filename(input_file_name); |
| 565 |
if (input_file) { |
| 566 |
fprintf(stderr, |
| 567 |
"warning: can't read from both stdin and a file, reading from %s\n", |
| 568 |
input_file_name); |
| 569 |
input_file = NULL; |
| 570 |
must_print_short_usage = true; |
| 571 |
} |
| 572 |
} |
| 573 |
if (optind < argc) { |
| 574 |
must_print_short_usage = true; |
| 575 |
fprintf(stderr, "ignored arguments:"); |
| 576 |
for (; optind < argc; ++optind) { |
| 577 |
fprintf(stderr, " %s", argv[optind]); |
| 578 |
} |
| 579 |
fprintf(stderr, "\n"); |
| 580 |
} |
| 581 |
|
| 582 |
if (must_print_short_usage) { |
| 583 |
print_short_usage(argv[0]); |
| 584 |
fprintf(stderr, "Proceeding anyway...\n"); |
| 585 |
} |
| 586 |
|
| 587 |
gregorio_set_debug_messages(debug); |
| 588 |
gregorio_set_deprecation_errors(deprecation_errors); |
| 589 |
|
| 590 |
if (!input_format) { |
| 591 |
input_format = DEFAULT_INPUT_FORMAT; |
| 592 |
} |
| 593 |
|
| 594 |
if (!output_format) { |
| 595 |
output_format = DEFAULT_OUTPUT_FORMAT; |
| 596 |
} |
| 597 |
|
| 598 |
/* then we act... */ |
| 599 |
|
| 600 |
if (!output_file_name && !output_file) { |
| 601 |
if (!output_basename) { |
| 602 |
output_file = stdout; |
| 603 |
} else { |
| 604 |
switch (output_format) { |
| 605 |
case GABC: |
| 606 |
output_file_name = |
| 607 |
get_output_filename(output_basename, "gabc"); |
| 608 |
break; |
| 609 |
case GTEX: |
| 610 |
output_file_name = |
| 611 |
get_output_filename(output_basename, "gtex"); |
| 612 |
break; |
| 613 |
case DUMP: |
| 614 |
output_file_name = |
| 615 |
get_output_filename(output_basename, "dump"); |
| 616 |
break; |
| 617 |
default: |
| 618 |
/* not reachable unless there's a programming error */ |
| 619 |
/* LCOV_EXCL_START */ |
| 620 |
fprintf(stderr, "error: unsupported format"); |
| 621 |
print_short_usage(argv[0]); |
| 622 |
gregorio_exit(1); |
| 623 |
/* LCOV_EXCL_STOP */ |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
if (output_basename) { |
| 629 |
free(output_basename); |
| 630 |
} |
| 631 |
|
| 632 |
if (!output_file) { |
| 633 |
if (!input_file) { |
| 634 |
check_input_clobber(input_file_name, output_file_name); |
| 635 |
} |
| 636 |
gregorio_check_file_access(write, output_file_name, ERROR, |
| 637 |
gregorio_exit(1)); |
| 638 |
output_file = fopen(output_file_name, "wb"); |
| 639 |
if (!output_file) { |
| 640 |
fprintf(stderr, "error: can't write in file %s", output_file_name); |
| 641 |
gregorio_exit(1); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
/* we always have input_file or input_file_name */ |
| 646 |
if (input_file) { |
| 647 |
if (point_and_click) { |
| 648 |
fprintf(stderr, |
| 649 |
"warning: disabling point-and-click since reading from stdin\n"); |
| 650 |
} |
| 651 |
} else { |
| 652 |
gregorio_check_file_access(read, input_file_name, ERROR, |
| 653 |
gregorio_exit(1)); |
| 654 |
input_file = fopen(input_file_name, "r"); |
| 655 |
if (!input_file) { |
| 656 |
fprintf(stderr, "error: can't open file %s for reading\n", |
| 657 |
input_file_name); |
| 658 |
gregorio_exit(1); |
| 659 |
} |
| 660 |
if (point_and_click) { |
| 661 |
point_and_click_filename = encode_point_and_click_filename( |
| 662 |
input_file_name); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
if (!verb_mode) { |
| 667 |
verb_mode = VERBOSITY_DEPRECATION; |
| 668 |
} |
| 669 |
|
| 670 |
gregorio_set_verbosity_mode(verb_mode); |
| 671 |
|
| 672 |
switch (input_format) { |
| 673 |
case GABC: |
| 674 |
score = gabc_read_score(input_file, point_and_click); |
| 675 |
break; |
| 676 |
default: |
| 677 |
/* not reachable unless there's a programming error */ |
| 678 |
/* LCOV_EXCL_START */ |
| 679 |
fprintf(stderr, "error : invalid input format\n"); |
| 680 |
fclose(input_file); |
| 681 |
fclose(output_file); |
| 682 |
gregorio_exit(1); |
| 683 |
break; |
| 684 |
/* LCOV_EXCL_STOP */ |
| 685 |
} |
| 686 |
|
| 687 |
fclose(input_file); |
| 688 |
if (score == NULL) { |
| 689 |
/* score should never be NULL on return from gabc_read_score */ |
| 690 |
/* LCOV_EXCL_START */ |
| 691 |
fclose(output_file); |
| 692 |
fprintf(stderr, "error in file parsing\n"); |
| 693 |
gregorio_exit(1); |
| 694 |
/* LCOV_EXCL_STOP */ |
| 695 |
} |
| 696 |
|
| 697 |
switch (output_format) { |
| 698 |
case GABC: |
| 699 |
gabc_write_score(output_file, score); |
| 700 |
break; |
| 701 |
case GTEX: |
| 702 |
gregoriotex_write_score(output_file, score, point_and_click_filename); |
| 703 |
break; |
| 704 |
case DUMP: |
| 705 |
dump_write_score(output_file, score); |
| 706 |
break; |
| 707 |
default: |
| 708 |
/* not reachable unless there's a programming error */ |
| 709 |
/* LCOV_EXCL_START */ |
| 710 |
fprintf(stderr, "error : invalid output format\n"); |
| 711 |
gregorio_free_score(score); |
| 712 |
fclose(output_file); |
| 713 |
gregorio_exit(1); |
| 714 |
break; |
| 715 |
/* LCOV_EXCL_STOP */ |
| 716 |
} |
| 717 |
fclose(output_file); |
| 718 |
if (point_and_click_filename) { |
| 719 |
free(point_and_click_filename); |
| 720 |
} |
| 721 |
gregorio_free_score(score); |
| 722 |
gregorio_vowel_tables_free(); |
| 723 |
gabc_score_determination_lex_destroy(); |
| 724 |
gabc_notes_determination_lex_destroy(); |
| 725 |
gregorio_vowel_rulefile_lex_destroy(); |
| 726 |
if (error_file_name) { |
| 727 |
fclose(error_file); |
| 728 |
} |
| 729 |
|
| 730 |
gregorio_exit(gregorio_get_return_value()); |
| 731 |
} /* due to exit on prior line, this will never be reached; LCOV_EXCL_LINE */ |