diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/image.c | 3 | ||||
-rw-r--r-- | src/image.h | 1 | ||||
-rw-r--r-- | src/local.mk | 1 | ||||
-rw-r--r-- | src/main.c | 24 | ||||
-rw-r--r-- | src/output.c | 47 | ||||
-rw-r--r-- | src/output.h | 31 |
6 files changed, 93 insertions, 14 deletions
diff --git a/src/image.c b/src/image.c index e55f955..59716da 100644 --- a/src/image.c +++ b/src/image.c @@ -49,8 +49,7 @@ image_new(const char *file_name) if (strcmp(file_name, "-") != 0) { image->fp = fopen(file_name, "wb"); if (!image->fp) { - printf(_("Error: Cannot open \"%s\" for writing\n"), - file_name); + error(_("Cannot open \"%s\" for writing"), file_name); return image_destroy(&image); } } diff --git a/src/image.h b/src/image.h index ece53f7..be3b66d 100644 --- a/src/image.h +++ b/src/image.h @@ -27,6 +27,7 @@ #include <string.h> #include "font.h" +#include "output.h" #define IMAGE_BIT_DEPTH 8 #define IMAGE_CHANNELS 3 diff --git a/src/local.mk b/src/local.mk index 24d8af2..27ceddb 100644 --- a/src/local.mk +++ b/src/local.mk @@ -2,5 +2,6 @@ fbcon2png_SOURCES += \ %reldir%/font.c \ %reldir%/image.c \ %reldir%/main.c \ + %reldir%/output.c \ %reldir%/sgr.c \ %reldir%/text.c @@ -26,6 +26,7 @@ #include "font.h" #include "i18n.h" #include "image.h" +#include "output.h" #include "text.h" static void @@ -54,6 +55,8 @@ main(int argc, char **argv) struct text *text; struct image *image; + set_program_name(argv[0]); + font = NULL; w = 0; h = 0; @@ -66,8 +69,7 @@ main(int argc, char **argv) } font = font_find(optarg); if (!font) { - fprintf(stderr, _("Font \"%s\" not " - "found\n"), + error(_("Font \"%s\" not found"), optarg); usage(stderr, argv[0]); return 1; @@ -76,10 +78,9 @@ main(int argc, char **argv) case 'w': w = strtol(optarg, &end, 10); if (*optarg == '\0' || *end != '\0' || w <= 0) { - fprintf(stderr, _("Width must be an " - "integer " - "greater than " - "0\n")); + error(_("Width must be an integer " + "greater than 0" + )); usage(stderr, argv[0]); return 1; } @@ -87,10 +88,9 @@ main(int argc, char **argv) case 'h': h = strtol(optarg, &end, 10); if (*optarg == '\0' || *end != '\0' || h <= 0) { - fprintf(stderr, _("Height must be an " - "integer " - "greater than " - "0\n")); + error(_("Height must be an integer " + "greater than 0" + )); usage(stderr, argv[0]); return 1; } @@ -113,8 +113,8 @@ main(int argc, char **argv) if (strcmp(input, "-") != 0) { fp = fopen(input, "r"); if (!fp) { - fprintf(stderr, _("Error: Cannot open \"%s\" for " - "reading\n"), input); + error(_("Error: Cannot open \"%s\" for reading"), + input); return 1; } } else { diff --git a/src/output.c b/src/output.c new file mode 100644 index 0000000..0d8341c --- /dev/null +++ b/src/output.c @@ -0,0 +1,47 @@ +/* + * Program output functions + * + * Copyright (C) 2019 Patrick McDermott + * + * This file is part of fbcon2png. + * + * fbcon2png is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * fbcon2png is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with fbcon2png. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "output.h" + +#include <stdarg.h> +#include <stdio.h> + +#include "i18n.h" + +static const char *_program_name; + +void +set_program_name(const char *name) +{ + _program_name = name; +} + +void +error(const char *format, ...) +{ + va_list ap; + + fprintf(stderr, _("%s: Error: "), _program_name); + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + fputs("\n", stderr); +} diff --git a/src/output.h b/src/output.h new file mode 100644 index 0000000..a2c83b5 --- /dev/null +++ b/src/output.h @@ -0,0 +1,31 @@ +/* + * Program output functions + * + * Copyright (C) 2019 Patrick McDermott + * + * This file is part of fbcon2png. + * + * fbcon2png is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * fbcon2png is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with fbcon2png. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef OUTPUT_H_ +#define OUTPUT_H_ + +void +set_program_name(const char *name); + +void +error(const char *format, ...) __attribute__((format(gnu_printf, 1, 2))); + +#endif /* OUTPUT_H_ */ |