| 1 |
/* |
| 2 |
* Gregorio is a program that translates gabc files to GregorioTeX |
| 3 |
* This file contains miscellaneous support functions. |
| 4 |
* |
| 5 |
* Copyright (C) 2015-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 |
#include "config.h" |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdarg.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <ctype.h> |
| 29 |
#include <errno.h> |
| 30 |
#include "support.h" |
| 31 |
#include "struct.h" |
| 32 |
#include "messages.h" |
| 33 |
|
| 34 |
/* Our version of snprintf; this is NOT semantically the same as C99's |
| 35 |
* snprintf; rather, it's a "lowest common denominator" implementation |
| 36 |
* blending C99 and MS-C */ |
| 37 |
void gregorio_snprintf(char *s, size_t size, const char *format, ...) |
| 38 |
{ |
| 39 |
va_list args; |
| 40 |
|
| 41 |
#ifdef _MSC_VER |
| 42 |
memset(s, 0, size); |
| 43 |
#endif |
| 44 |
|
| 45 |
va_start(args, format); |
| 46 |
#ifdef _MSC_VER |
| 47 |
_vsnprintf_s(s, size, _TRUNCATE, format, args); |
| 48 |
#else |
| 49 |
vsnprintf(s, size, format, args); |
| 50 |
#endif |
| 51 |
va_end(args); |
| 52 |
} |
| 53 |
|
| 54 |
static __inline void *assert_successful_allocation(void *ptr, char *funcname) { |
| 55 |
if (!ptr) { |
| 56 |
/* it's not realistic to test this for coverage */ |
| 57 |
/* LCOV_EXCL_START */ |
| 58 |
gregorio_message(_("error in memory allocation"), |
| 59 |
funcname, VERBOSITY_FATAL, 0); |
| 60 |
gregorio_exit(1); |
| 61 |
} |
| 62 |
/* LCOV_EXCL_STOP */ |
| 63 |
return ptr; |
| 64 |
} |
| 65 |
|
| 66 |
void *gregorio_malloc(size_t size) |
| 67 |
{ |
| 68 |
return assert_successful_allocation(malloc(size), "gregorio_malloc"); |
| 69 |
} |
| 70 |
|
| 71 |
void *gregorio_calloc(size_t nmemb, size_t size) |
| 72 |
{ |
| 73 |
return assert_successful_allocation(calloc(nmemb, size), "gregorio_calloc"); |
| 74 |
} |
| 75 |
|
| 76 |
void *gregorio_realloc(void *ptr, size_t size) |
| 77 |
{ |
| 78 |
return assert_successful_allocation(realloc(ptr, size), "gregorio_realloc"); |
| 79 |
} |
| 80 |
|
| 81 |
char *gregorio_strdup(const char *s) |
| 82 |
{ |
| 83 |
return (char *)assert_successful_allocation(strdup(s), "gregorio_strdup"); |
| 84 |
} |
| 85 |
|
| 86 |
void *_gregorio_grow_buffer(void *buffer, size_t *nmemb, size_t size) |
| 87 |
{ |
| 88 |
if (buffer == NULL) { |
| 89 |
return gregorio_malloc(*nmemb * size); |
| 90 |
} |
| 91 |
if (*nmemb >= MAX_BUF_GROWTH) { |
| 92 |
/* it's not realistic to test this case */ |
| 93 |
/* LCOV_EXCL_START */ |
| 94 |
gregorio_message(_("buffer too large"), "gregorio_grow_buffer", |
| 95 |
VERBOSITY_FATAL, 0); |
| 96 |
gregorio_exit(1); |
| 97 |
/* LCOV_EXCL_STOP */ |
| 98 |
} |
| 99 |
*nmemb <<= 1; |
| 100 |
return gregorio_realloc(buffer, *nmemb * size); |
| 101 |
} |
| 102 |
|
| 103 |
#ifdef USE_KPSE |
| 104 |
static kpathsea kpse = NULL; |
| 105 |
#define USED_FOR_KPSE |
| 106 |
#else |
| 107 |
#define USED_FOR_KPSE __attribute__((unused)) |
| 108 |
#endif |
| 109 |
|
| 110 |
void gregorio_support_init(const char *const program USED_FOR_KPSE, |
| 111 |
const char *const argv0 USED_FOR_KPSE) |
| 112 |
{ |
| 113 |
gregorio_set_verbosity_mode(VERBOSITY_ERROR); |
| 114 |
gregorio_struct_init(); |
| 115 |
#ifdef USE_KPSE |
| 116 |
kpse = kpathsea_new(); |
| 117 |
kpathsea_set_program_name(kpse, argv0, program); |
| 118 |
#endif |
| 119 |
} |
| 120 |
|
| 121 |
void gregorio_print_version(const char *copyright) |
| 122 |
{ |
| 123 |
#ifdef USE_KPSE |
| 124 |
printf("Gregorio %s (%s).\n%s\n", GREGORIO_VERSION, |
| 125 |
kpathsea_version_string, copyright); |
| 126 |
#else |
| 127 |
printf("Gregorio %s.\n%s\n", GREGORIO_VERSION, |
| 128 |
copyright); |
| 129 |
#endif |
| 130 |
printf("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\ |
| 131 |
This is free software: you are free to change and redistribute it.\n\ |
| 132 |
There is NO WARRANTY, to the extent permitted by law.\n"); |
| 133 |
} |
| 134 |
|
| 135 |
#ifdef USE_KPSE |
| 136 |
bool gregorio_read_ok(const char *const filename, |
| 137 |
const gregorio_verbosity verbosity) |
| 138 |
{ |
| 139 |
if (kpathsea_in_name_ok_silent(kpse, filename)) { |
| 140 |
return true; |
| 141 |
} |
| 142 |
gregorio_messagef("gregorio_in_name_ok", verbosity, __LINE__, |
| 143 |
_("kpse prohibits read from file %s"), filename); |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
bool gregorio_write_ok(const char *const filename, |
| 148 |
const gregorio_verbosity verbosity) |
| 149 |
{ |
| 150 |
if (kpathsea_out_name_ok_silent(kpse, filename)) { |
| 151 |
return true; |
| 152 |
} |
| 153 |
gregorio_messagef("gregorio_out_name_ok", verbosity, __LINE__, |
| 154 |
_("kpse prohibits write to file %s"), filename); |
| 155 |
return false; |
| 156 |
} |
| 157 |
#endif |
| 158 |
|
| 159 |
#ifndef USE_KPSE |
| 160 |
static __inline void rtrim(char *buf) |
| 161 |
{ |
| 162 |
char *p; |
| 163 |
for (p = buf + strlen(buf) - 1; p >= buf && isspace(*p); --p) { |
| 164 |
*p = '\0'; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
static bool gregorio_readline(char **buf, size_t *bufsize, FILE *file) |
| 169 |
{ |
| 170 |
size_t oldsize; |
| 171 |
if (*buf == NULL) { |
| 172 |
*bufsize = 128; |
| 173 |
*buf = gregorio_grow_buffer(NULL, bufsize, char); |
| 174 |
} else { |
| 175 |
if (*bufsize < 128) { |
| 176 |
/* not reachable unless there's a programming error */ |
| 177 |
/* LCOV_EXCL_START */ |
| 178 |
gregorio_message(_("invalid buffer size"), "gregorio_getline", |
| 179 |
VERBOSITY_FATAL, 0); |
| 180 |
gregorio_exit(1); |
| 181 |
/* LCOV_EXCL_STOP */ |
| 182 |
} |
| 183 |
} |
| 184 |
(*buf)[0] = '\0'; |
| 185 |
oldsize = 1; |
| 186 |
for (;;) { |
| 187 |
(*buf)[*bufsize - 2] = '\0'; |
| 188 |
|
| 189 |
if (feof(file) || ferror(file) |
| 190 |
|| !fgets((*buf) + oldsize - 1, (int)(*bufsize - oldsize + 1), |
| 191 |
file) |
| 192 |
|| (*buf)[*bufsize - 2] == '\0') { |
| 193 |
if (ferror(file)) { |
| 194 |
/* it's not realistic to simulate the system error required to |
| 195 |
* cover this case */ |
| 196 |
/* LCOV_EXCL_START */ |
| 197 |
gregorio_message(_("Error reading from the file"), |
| 198 |
"gregorio_getline", VERBOSITY_FATAL, 0); |
| 199 |
gregorio_exit(1); |
| 200 |
/* LCOV_EXCL_STOP */ |
| 201 |
} |
| 202 |
return (*buf)[0] != '\0'; |
| 203 |
} |
| 204 |
|
| 205 |
oldsize = *bufsize; |
| 206 |
*buf = gregorio_grow_buffer(*buf, bufsize, char); |
| 207 |
} |
| 208 |
} |
| 209 |
#endif |
| 210 |
|
| 211 |
|
| 212 |
char **gregorio_kpse_find(const char *filename) |
| 213 |
{ |
| 214 |
char **filenames; |
| 215 |
#ifdef USE_KPSE |
| 216 |
filenames = kpathsea_find_file_generic(kpse, filename, kpse_tex_format, |
| 217 |
true, true); |
| 218 |
if (!filenames) { |
| 219 |
/* It's not reasonable to break kpse in such a way that this would |
| 220 |
* fail. */ |
| 221 |
/* LCOV_EXCL_START */ |
| 222 |
gregorio_messagef("gregorio_kpse_find", VERBOSITY_WARNING, 0, |
| 223 |
_("kpathsea_find_file_generic returned NULL: %s"), filename); |
| 224 |
return NULL; |
| 225 |
/* LCOV_EXCL_STOP */ |
| 226 |
} |
| 227 |
#else |
| 228 |
FILE *file; |
| 229 |
size_t bufsize = 0; |
| 230 |
char *buf = NULL; |
| 231 |
size_t capacity = 16, size = 0; |
| 232 |
#define KPSE_COMMAND "kpsewhich -must-exist -all " |
| 233 |
size_t command_size = sizeof(KPSE_COMMAND) + strlen(filename); |
| 234 |
char *command = gregorio_malloc(command_size); |
| 235 |
|
| 236 |
strcpy(command, KPSE_COMMAND); |
| 237 |
strcpy(command + sizeof(KPSE_COMMAND) - 1, filename); |
| 238 |
file = popen(command, "r"); |
| 239 |
free(command); |
| 240 |
command = NULL; /* just to be sure */ |
| 241 |
if (!file) { |
| 242 |
/* it's not reasonable to cause popen to fail */ |
| 243 |
/* LCOV_EXCL_START */ |
| 244 |
gregorio_messagef("gregorio_kpse_find", VERBOSITY_WARNING, 0, |
| 245 |
_("unable to run kpsewhich %s: %s"), filename, |
| 246 |
strerror(errno)); |
| 247 |
return NULL; |
| 248 |
/* LCOV_EXCL_STOP */ |
| 249 |
} |
| 250 |
filenames = gregorio_malloc(capacity * sizeof(char *)); |
| 251 |
while (gregorio_readline(&buf, &bufsize, file)) { |
| 252 |
rtrim(buf); |
| 253 |
if (strlen(buf) > 0) { |
| 254 |
filenames[size++] = gregorio_strdup(buf); |
| 255 |
if (size >= capacity) { |
| 256 |
capacity <<= 1; |
| 257 |
filenames = gregorio_realloc(filenames, |
| 258 |
capacity * sizeof(char *)); |
| 259 |
} |
| 260 |
} else { |
| 261 |
gregorio_messagef("read_patterns", VERBOSITY_WARNING, 0, |
| 262 |
_("kpsewhich returned bad value for %s"), filename); |
| 263 |
} |
| 264 |
} |
| 265 |
free(buf); |
| 266 |
filenames[size] = NULL; |
| 267 |
pclose(file); |
| 268 |
#endif |
| 269 |
return filenames; |
| 270 |
} |
| 271 |
|
| 272 |
void gregorio_exit(int status) |
| 273 |
{ |
| 274 |
#ifdef USE_KPSE |
| 275 |
if (kpse) { |
| 276 |
kpathsea_finish(kpse); |
| 277 |
} |
| 278 |
#endif |
| 279 |
gregorio_struct_destroy(); |
| 280 |
exit(status); |
| 281 |
} /* the prior line exits; LCOV_EXCL_LINE */ |