commit eea428c2c0770069dc104d4ea7fcee8c70f7a3d4
parent 1e03dbee8b6bdea6018d26c78f3920874ff9e2ac
Author: nibo <nibo@relim.de>
Date: Sat, 7 Dec 2024 17:51:33 +0100
Handle case where there is no chordpro filepath
If you pipe the chordpro file contents into lorid
we don't know where the chordpro file is coming from.
If that chordpro file includes an 'image' directive
with a relative filepath that filepath will be added
to the current working directory.
Diffstat:
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/out_pdf.c b/out_pdf.c
@@ -1,9 +1,11 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
+#include <unistd.h>
#include <pdfio.h>
#include <pdfio-content.h>
#include <sys/stat.h>
+#include <errno.h>
#include "chordpro.h"
#include "config.h"
#include "out_pdf.h"
@@ -731,6 +733,7 @@ image_name(struct ChoImage *image)
}
if (stat(image_path, &s)) {
LOG_DEBUG("stat failed.");
+ util_log(LOG_ERR, "%s: %s", image_path, strerror(errno));
return NULL;
}
memset(tmp, 0, PATH_MAX);
@@ -762,6 +765,10 @@ out_pdf_load_images(struct Obj ***images, pdfio_file_t *file, struct ChoSong **s
strcat((char *)&filepath, "/");
strcat((char *)&filepath, (*items)->u.image->src);
name = image_name((*items)->u.image);
+ if (!name) {
+ LOG_DEBUG("image_name failed.");
+ return false;
+ }
if (!objs_has(*images, name)) {
*images = erealloc(*images, (i+2) * sizeof(struct Obj *));
(*images)[i] = obj_new();
@@ -895,6 +902,10 @@ line_width_until_text_above(
free(name);
} else {
name = image_name(items[i]->u.image);
+ if (!name) {
+ LOG_DEBUG("image_name failed.");
+ return ERROR;
+ }
obj = objs_get_obj(img_objs, name);
if (!obj) {
LOG_DEBUG("objs_get_obj failed.");
@@ -1168,6 +1179,10 @@ images_find_biggest_height(struct ChoLineItem **left_items, int line_item_index,
while (*items && i <= end) {
if (!(*items)->is_text) {
name = image_name((*items)->u.image);
+ if (!name) {
+ LOG_DEBUG("image_name failed.");
+ return ERROR;
+ }
obj = objs_get_obj(img_objs, name);
if (!obj) {
LOG_DEBUG("objs_get_obj failed.");
@@ -1681,6 +1696,10 @@ pdf_content_create(
*imgs = erealloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *));
(*imgs)[ctx.image] = pdf_image_new();
(*imgs)[ctx.image]->name = image_name((*left_items)->u.image);
+ if (!(*imgs)[ctx.image]->name) {
+ LOG_DEBUG("image_name failed.");
+ return false;
+ }
(*imgs)[ctx.image]->obj = objs_get_obj(img_objs, (*imgs)[ctx.image]->name);
if (!(*imgs)[ctx.image]->obj) {
LOG_DEBUG("objs_get_obj failed.");
@@ -1711,6 +1730,10 @@ pdf_content_create(
*imgs = erealloc(*imgs, (ctx.image+1) * sizeof(struct PDFImage *));
(*imgs)[ctx.image] = pdf_image_new();
(*imgs)[ctx.image]->name = image_name((*left_items)->u.image);
+ if (!(*imgs)[ctx.image]->name) {
+ LOG_DEBUG("image_name failed.");
+ return false;
+ }
(*imgs)[ctx.image]->obj = objs_get_obj(img_objs, (*imgs)[ctx.image]->name);
if (!(*imgs)[ctx.image]->obj) {
LOG_DEBUG("objs_get_obj failed.");
@@ -1887,7 +1910,11 @@ out_pdf_create(
memset(&g_current_font_name, 0, sizeof(g_current_font_name));
memset(&g_cho_dirpath, 0, PATH_MAX);
- dirpath = filepath_dirname(cho_filepath);
+ if (cho_filepath) {
+ dirpath = filepath_dirname(cho_filepath);
+ } else {
+ dirpath = getcwd(NULL, 0);
+ }
strcpy((char *)&g_cho_dirpath, dirpath);
free(dirpath);
pdf_filename = out_pdf_filename_create(songs, cho_filepath, output_folder_or_file);