diff options
author | Alexey Gladkov <gladkov.alexey@gmail.com> | 2020-04-10 18:44:48 +0200 |
---|---|---|
committer | Alexey Gladkov <gladkov.alexey@gmail.com> | 2020-04-11 18:57:45 +0200 |
commit | f4bf7700e146c8a1f12700a0efb85a62efe723e5 (patch) | |
tree | 4b6e0fd2bb33de3aacfac5aaec07d86a92f6acb7 | |
parent | bdc74f89c19c258d77c3e19eacf503d6277d3056 (diff) | |
download | kbd-f4bf7700e146c8a1f12700a0efb85a62efe723e5.tar.gz |
Add kfont_context
For now this is a placeholder of logging function for all kfont related
functions.
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
-rw-r--r-- | src/Makefile.am | 7 | ||||
-rw-r--r-- | src/clrunimap.c | 9 | ||||
-rw-r--r-- | src/context.c | 45 | ||||
-rw-r--r-- | src/context.h | 39 | ||||
-rw-r--r-- | src/getunimap.c | 9 | ||||
-rw-r--r-- | src/kdfontop.c | 95 | ||||
-rw-r--r-- | src/kdfontop.h | 33 | ||||
-rw-r--r-- | src/kdmapop.c | 48 | ||||
-rw-r--r-- | src/kdmapop.h | 11 | ||||
-rw-r--r-- | src/kfont.h | 63 | ||||
-rw-r--r-- | src/loadunimap.c | 96 | ||||
-rw-r--r-- | src/loadunimap.h | 10 | ||||
-rw-r--r-- | src/mapscrn.c | 27 | ||||
-rw-r--r-- | src/psffontop.c | 127 | ||||
-rw-r--r-- | src/psffontop.h | 27 | ||||
-rw-r--r-- | src/psfxtable.c | 95 | ||||
-rw-r--r-- | src/readpsfheader.c | 11 | ||||
-rw-r--r-- | src/setfont.c | 267 | ||||
-rw-r--r-- | src/showconsolefont.c | 62 |
19 files changed, 641 insertions, 440 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index f0409df2..ed508313 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -42,11 +42,12 @@ EXTRA_DIST = \ libfont_a_SOURCES = \ psf.h psffontop.c psffontop.h \ + context.c context.h \ unicode.c unicode.h \ utf8.c utf8.h \ - kdmapop.c kdmapop.h \ - loadunimap.c loadunimap.h \ - kdfontop.c kdfontop.h + kdmapop.c \ + loadunimap.c \ + kdfontop.c ALL_S = paths.h diff --git a/src/clrunimap.c b/src/clrunimap.c index feddc4e2..f02d8ef1 100644 --- a/src/clrunimap.c +++ b/src/clrunimap.c @@ -10,9 +10,9 @@ #include <fcntl.h> #include <string.h> #include <linux/kd.h> -#include "kdmapop.h" #include "libcommon.h" +#include "kfont.h" int main(int argc, char *argv[]) { @@ -28,5 +28,10 @@ int main(int argc, char *argv[]) if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); - return loadunimap(fd, NULL, NULL); + struct kfont_context ctx = { + .progname = get_progname(), + .log_fn = log_stderr, + }; + + return loadunimap(&ctx, fd, NULL, NULL); } diff --git a/src/context.c b/src/context.c new file mode 100644 index 00000000..2f48207c --- /dev/null +++ b/src/context.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <syslog.h> + +#include "context.h" + +void +logger(struct kfont_context *ctx, int priority, const char *file, int line, const char *fn, + const char *fmt, ...) +{ + va_list args; + if (ctx->log_fn == NULL) + return; + va_start(args, fmt); + ctx->log_fn(ctx, priority, file, line, fn, fmt, args); + va_end(args); +} + + +void +log_stderr(struct kfont_context *ctx, int priority, const char *file, const int line, + const char *fn, const char *format, va_list args) +{ + char buf[16]; + const char *priname; + + switch (priority) { + case LOG_EMERG: priname = "EMERGENCY"; break; + case LOG_ALERT: priname = "ALERT"; break; + case LOG_CRIT: priname = "CRITICAL"; break; + case LOG_ERR: priname = "ERROR"; break; + case LOG_WARNING: priname = "WARNING"; break; + case LOG_NOTICE: priname = "NOTICE"; break; + case LOG_INFO: priname = "INFO"; break; + case LOG_DEBUG: priname = "DEBUG"; break; + default: + snprintf(buf, sizeof(buf), "L:%d", priority); + priname = buf; + } + + fprintf(stderr, "%s: %s %s:%d %s: ", ctx->progname, priname, file, line, fn); + vfprintf(stderr, format, args); + fprintf(stderr, "\n"); + + fflush(stderr); +} diff --git a/src/context.h b/src/context.h new file mode 100644 index 00000000..3bfb8131 --- /dev/null +++ b/src/context.h @@ -0,0 +1,39 @@ +#ifndef _CONTEXT_H_ +#define _CONTEXT_H_ + +#ifndef __GNUC__ +#undef __attribute__ +#define __attribute__(x) /*NOTHING*/ +#endif + +struct kfont_context; + +typedef void (*logger_t)(struct kfont_context *, int, const char *, int, + const char *, const char *, va_list) + __attribute__((nonnull(1))) + __attribute__((format(printf, 6, 0))); + +struct kfont_context { + const char *progname; + logger_t log_fn; +}; + +void logger(struct kfont_context *ctx, int priority, const char *file, int line, + const char *fn, const char *fmt, ...) + __attribute__((format(printf, 6, 7))) + __attribute__((nonnull(1))); + +#include <syslog.h> + +#define DBG(ctx, arg...) logger(ctx, LOG_DEBUG, __FILE__, __LINE__, __func__, ##arg) +#define INFO(ctx, arg...) logger(ctx, LOG_INFO, __FILE__, __LINE__, __func__, ##arg) +#define WARN(ctx, arg...) logger(ctx, LOG_WARNING, __FILE__, __LINE__, __func__, ##arg) +#define ERR(ctx, arg...) logger(ctx, LOG_ERR, __FILE__, __LINE__, __func__, ##arg) + +#include <stdarg.h> + +void log_stderr(struct kfont_context *ctx, int priority, const char *file, + const int line, const char *fn, const char *format, va_list args) + __attribute__((format(printf, 6, 0))); + +#endif /* _CONTEXT_H_ */ diff --git a/src/getunimap.c b/src/getunimap.c index bcfbdeb6..56530974 100644 --- a/src/getunimap.c +++ b/src/getunimap.c @@ -9,9 +9,9 @@ #include <sysexits.h> #include <sys/ioctl.h> #include <linux/kd.h> -#include "kdmapop.h" #include "libcommon.h" +#include "kfont.h" #ifndef USE_LIBC /* There is such function in libc5 but it doesn't work for me [libc 5.4.13] */ @@ -69,7 +69,12 @@ int main(int argc, char **argv) if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); - if (getunimap(fd, &ud)) + struct kfont_context ctx = { + .progname = get_progname(), + .log_fn = log_stderr, + }; + + if (getunimap(&ctx, fd, &ud)) return EXIT_FAILURE; if (sortflag) { diff --git a/src/kdfontop.c b/src/kdfontop.c index 8cf93916..d09bfebe 100644 --- a/src/kdfontop.c +++ b/src/kdfontop.c @@ -12,24 +12,26 @@ #include <string.h> #include <sys/ioctl.h> #include <linux/kd.h> -#include "kdfontop.h" #include "libcommon.h" +#include "kfont.h" #ifdef COMPAT_HEADERS #include "compat/linux-kd.h" #endif -int restorefont(int fd) +int +restorefont(struct kfont_context *ctx, int fd) { if (ioctl(fd, PIO_FONTRESET, 0)) { - perror("PIO_FONTRESET"); + ERR(ctx, "ioctl(PIO_FONTRESET): %m"); return -1; } return 0; } -unsigned int font_charheight(unsigned char *buf, unsigned int count, unsigned int width) +unsigned int +font_charheight(unsigned char *buf, unsigned int count, unsigned int width) { unsigned int h, i, x; unsigned int bytewidth = (width + 7) / 8; @@ -48,20 +50,23 @@ nonzero: * May be called with width==NULL and height==NULL. * Must not exit - we may have cleanup to do. */ -int getfont(int fd, unsigned char *buf, unsigned int *count, unsigned int *width, unsigned int *height) +int +getfont(struct kfont_context *ctx, int fd, unsigned char *buf, unsigned int *count, + unsigned int *width, unsigned int *height) { struct consolefontdesc cfd; struct console_font_op cfo; int i; /* First attempt: KDFONTOP */ - cfo.op = KD_FONT_OP_GET; + cfo.op = KD_FONT_OP_GET; cfo.flags = 0; cfo.width = cfo.height = 32; - cfo.charcount = *count; - cfo.data = buf; - i = ioctl(fd, KDFONTOP, &cfo); - if (i == 0) { + cfo.charcount = *count; + cfo.data = buf; + + i = ioctl(fd, KDFONTOP, &cfo); + if (!i) { *count = cfo.charcount; if (height) *height = cfo.height; @@ -69,8 +74,9 @@ int getfont(int fd, unsigned char *buf, unsigned int *count, unsigned int *width *width = cfo.width; return 0; } + if (errno != ENOSYS && errno != EINVAL) { - perror("getfont: KDFONTOP"); + ERR(ctx, "ioctl(KDFONTOP): %m"); return -1; } @@ -81,49 +87,56 @@ int getfont(int fd, unsigned char *buf, unsigned int *count, unsigned int *width cfd.charcount = *count; cfd.charheight = 0; cfd.chardata = (char *)buf; - i = ioctl(fd, GIO_FONTX, &cfd); - if (i == 0) { + + i = ioctl(fd, GIO_FONTX, &cfd); + if (!i) { *count = cfd.charcount; if (height) *height = cfd.charheight; return 0; } + if (errno != ENOSYS && errno != EINVAL) { - perror("getfont: GIO_FONTX"); + ERR(ctx, "ioctl(GIO_FONTX): %m"); return -1; } /* Third attempt: GIO_FONT */ if (*count < 256) { - fprintf(stderr, _("bug: getfont called with count<256\n")); + ERR(ctx, _("bug: getfont called with count<256")); return -1; } + if (!buf) { - fprintf(stderr, _("bug: getfont using GIO_FONT needs buf.\n")); + ERR(ctx, _("bug: getfont using GIO_FONT needs buf.")); return -1; } + i = ioctl(fd, GIO_FONT, buf); if (i) { - perror("getfont: GIO_FONT"); + ERR(ctx, "ioctl(GIO_FONT): %m"); return -1; } + *count = 256; if (height) *height = 0; /* undefined, at most 32 */ + return 0; } -int unsigned getfontsize(int fd) +int unsigned +getfontsize(struct kfont_context *ctx, int fd) { - unsigned int count; - int i; - - count = 0; - i = getfont(fd, NULL, &count, NULL, NULL); - return (i == 0) ? count : 256; + unsigned int count = 0; + if (!getfont(ctx, fd, NULL, &count, NULL, NULL)) + return count; + return 256; } -int putfont(int fd, unsigned char *buf, unsigned int count, unsigned int width, unsigned int height) +int +putfont(struct kfont_context *ctx, int fd, unsigned char *buf, unsigned int count, + unsigned int width, unsigned int height) { struct consolefontdesc cfd; struct console_font_op cfo; @@ -131,6 +144,7 @@ int putfont(int fd, unsigned char *buf, unsigned int count, unsigned int width, if (!width) width = 8; + if (!height) height = font_charheight(buf, count, width); @@ -141,30 +155,36 @@ int putfont(int fd, unsigned char *buf, unsigned int count, unsigned int width, cfo.height = height; cfo.charcount = count; cfo.data = buf; - i = ioctl(fd, KDFONTOP, &cfo); - if (i == 0) + + i = ioctl(fd, KDFONTOP, &cfo); + if (!i) return 0; + if (width != 8 || (errno != ENOSYS && errno != EINVAL)) { - perror("putfont: KDFONTOP"); + ERR(ctx, "ioctl(KDFONTOP): %m"); return -1; } /* Variation on first attempt: in case count is not 256 or 512 round up and try again. */ if (errno == EINVAL && width == 8 && count != 256 && count < 512) { - unsigned int ct = ((count > 256) ? 512 : 256); + unsigned int ct = ((count > 256) ? 512 : 256); unsigned char *mybuf = malloc(32U * ct); if (!mybuf) { - fprintf(stderr, _("%s: out of memory\n"), get_progname()); + ERR(ctx, "malloc: %m"); return -1; } + memset(mybuf, 0, 32U * ct); memcpy(mybuf, buf, 32U * count); + cfo.data = mybuf; cfo.charcount = ct; - i = ioctl(fd, KDFONTOP, &cfo); + + i = ioctl(fd, KDFONTOP, &cfo); free(mybuf); + if (i == 0) return 0; } @@ -173,12 +193,13 @@ int putfont(int fd, unsigned char *buf, unsigned int count, unsigned int width, cfd.charcount = count; cfd.charheight = height; cfd.chardata = (char *)buf; - i = ioctl(fd, PIO_FONTX, &cfd); - if (i == 0) + + i = ioctl(fd, PIO_FONTX, &cfd); + if (!i) return 0; + if (errno != ENOSYS && errno != EINVAL) { - fprintf(stderr, "%s: putfont: %d,%dx%d:failed: %d\n", get_progname(), count, width, height, i); - perror("putfont: PIO_FONTX"); + ERR(ctx, "ioctl(PIO_FONTX): %d,%dx%d: failed: %m", count, width, height); return -1; } @@ -186,9 +207,9 @@ int putfont(int fd, unsigned char *buf, unsigned int count, unsigned int width, /* This will load precisely 256 chars, independent of count */ i = ioctl(fd, PIO_FONT, buf); if (i) { - fprintf(stderr, "%s: putfont: %d,%dx%d: failed: %d\n", get_progname(), count, width, height, i); - perror("putfont: PIO_FONT"); + ERR(ctx, "ioctl(PIO_FONT): %d,%dx%d: failed: %m", count, width, height); return -1; } + return 0; } diff --git a/src/kdfontop.h b/src/kdfontop.h deleted file mode 100644 index 77cbaf69..00000000 --- a/src/kdfontop.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef _KDFONTOP_H -#define _KDFONTOP_H -/* - * Read kernel font into BUF with room for COUNT 32x32 glyphs. - * Return 0 on success -1 on failure. - * Sets number of glyphs in COUNT, glyph size in WIDTH and HEIGHT. - */ -extern int getfont(int fd, unsigned char *buf, unsigned int *count, unsigned int *width, unsigned int *height); - -/* - * Load kernel font of width WIDTH and pointsize HEIGHT from BUF - * with length COUNT. - * Return 0 on success, -1 on failure. - */ -extern int putfont(int fd, unsigned char *buf, unsigned int count, unsigned int width, unsigned int height); - -/* - * Find the maximum height of nonblank pixels - * (in the ((WIDTH+7)/8)*32*COUNT bytes of BUF). - */ -extern unsigned int font_charheight(unsigned char *buf, unsigned int count, unsigned int width); - -/* - * Find the size of the kernel font. - */ -extern unsigned int getfontsize(int fd); - -/* - * Restore font (doesn't work). - */ -extern int restorefont(int fd); - -#endif /* _KDFONTOP_H */ diff --git a/src/kdmapop.c b/src/kdmapop.c index 8448053c..1e68ab79 100644 --- a/src/kdmapop.c +++ b/src/kdmapop.c @@ -14,8 +14,7 @@ #include <linux/kd.h> #include "libcommon.h" - -#include "kdmapop.h" +#include "kfont.h" /* * Linux pre-0.96 defined GIO_SCRNMAP, PIO_SCRNMAP: @@ -38,19 +37,19 @@ * translation tables, and this ioctl would get/set the fourth * table, while the other three tables are built-in and constant.) */ -int getscrnmap(int fd, unsigned char *map) +int getscrnmap(struct kfont_context *ctx, int fd, unsigned char *map) { if (ioctl(fd, GIO_SCRNMAP, map)) { - perror("GIO_SCRNMAP"); + ERR(ctx, "ioctl(GIO_SCRNMAP): %m"); return -1; } return 0; } -int loadscrnmap(int fd, unsigned char *map) +int loadscrnmap(struct kfont_context *ctx, int fd, unsigned char *map) { if (ioctl(fd, PIO_SCRNMAP, map)) { - perror("PIO_SCRNMAP"); + ERR(ctx, "ioctl(PIO_SCRNMAP): %m"); return -1; } return 0; @@ -80,17 +79,17 @@ int loadscrnmap(int fd, unsigned char *map) * table with such direct-to-font values. */ -int getuniscrnmap(int fd, unsigned short *map) +int getuniscrnmap(struct kfont_context *ctx, int fd, unsigned short *map) { if (ioctl(fd, GIO_UNISCRNMAP, map)) { - perror("GIO_UNISCRNMAP"); + ERR(ctx, "ioctl(GIO_UNISCRNMAP): %m"); return -1; } return 0; } int -loaduniscrnmap(int fd, unsigned short *map) +loaduniscrnmap(struct kfont_context *ctx, int fd, unsigned short *map) { unsigned short inbuf[E_TABSZ]; @@ -100,7 +99,7 @@ loaduniscrnmap(int fd, unsigned short *map) memcpy(inbuf, map, sizeof(inbuf)); if (ioctl(fd, PIO_UNISCRNMAP, inbuf)) { - perror("PIO_UNISCRNMAP"); + ERR(ctx, "ioctl(PIO_UNISCRNMAP): %m"); return -1; } return 0; @@ -143,7 +142,7 @@ loaduniscrnmap(int fd, unsigned short *map) * Linux 2.6.1 makes GIO_UNIMAP, PIO_UNIMAP, PIO_UNIMAPCLR per-vt * so that fd no longer is random. */ -int getunimap(int fd, struct unimapdesc *ud0) +int getunimap(struct kfont_context *ctx, int fd, struct unimapdesc *ud0) { struct unimapdesc ud; int ct; @@ -152,24 +151,23 @@ int getunimap(int fd, struct unimapdesc *ud0) ud.entries = 0; if (ioctl(fd, GIO_UNIMAP, &ud)) { if (errno != ENOMEM || ud.entry_ct == 0) { - perror("GIO_UNIMAP(0)"); + ERR(ctx, "ioctl(GIO_UNIMAP): %m"); return -1; } - ct = ud.entry_ct; - ud.entries = (struct unipair *) - malloc(ct * sizeof(struct unipair)); + ct = ud.entry_ct; + + ud.entries = malloc(ct * sizeof(struct unipair)); if (ud.entries == NULL) { - fprintf(stderr, _("%s: out of memory\n"), get_progname()); + ERR(ctx, "malloc: %m"); return -1; } + if (ioctl(fd, GIO_UNIMAP, &ud)) { - perror("GIO_UNIMAP"); + ERR(ctx, "ioctl(GIO_UNIMAP): %m"); return -1; } if (ct != ud.entry_ct) - fprintf(stderr, - _("strange... ct changed from %d to %d\n"), - ct, ud.entry_ct); + ERR(ctx, _("strange... ct changed from %d to %d"), ct, ud.entry_ct); /* someone could change the unimap between our first and second ioctl, so the above errors are not impossible */ @@ -178,7 +176,7 @@ int getunimap(int fd, struct unimapdesc *ud0) return 0; } -int loadunimap(int fd, struct unimapinit *ui, struct unimapdesc *ud) +int loadunimap(struct kfont_context *ctx, int fd, struct unimapinit *ui, struct unimapdesc *ud) { struct unimapinit advice; @@ -193,12 +191,12 @@ again: if (ioctl(fd, PIO_UNIMAPCLR, &advice)) { #ifdef ENOIOCTLCMD if (errno == ENOIOCTLCMD) { - fprintf(stderr, + ERR(ctx, _("It seems this kernel is older than 1.1.92\n" - "No Unicode mapping table loaded.\n")); + "No Unicode mapping table loaded.")); } else #endif - perror("PIO_UNIMAPCLR"); + ERR(ctx, "ioctl(PIO_UNIMAPCLR): %m"); return -1; } if (ud == NULL) @@ -209,7 +207,7 @@ again: advice.advised_hashlevel++; goto again; } - perror("PIO_UNIMAP"); + ERR(ctx, "ioctl(PIO_UNIMAP): %m"); return -1; } diff --git a/src/kdmapop.h b/src/kdmapop.h deleted file mode 100644 index 8efef2b8..00000000 --- a/src/kdmapop.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _KDMAPOP_H -#define _KDMAPOP_H - -extern int getscrnmap(int fd, unsigned char *map); -extern int loadscrnmap(int fd, unsigned char *map); -extern int getuniscrnmap(int fd, unsigned short *map); -extern int loaduniscrnmap(int fd, unsigned short *map); -extern int getunimap(int fd, struct unimapdesc *ud); -extern int loadunimap(int fd, struct unimapinit *ui, struct unimapdesc *ud); - -#endif /* _KDMAPOP_H */ diff --git a/src/kfont.h b/src/kfont.h new file mode 100644 index 00000000..c8db32cd --- /dev/null +++ b/src/kfont.h @@ -0,0 +1,63 @@ +#ifndef _FONT_H_ +#define _FONT_H_ + +#include "context.h" + +/* mapscrn.c */ + +void loadnewmap(struct kfont_context *ctx, int fd, const char *mfil); +void saveoldmap(struct kfont_context *ctx, int fd, const char *omfil); + +/* loadunimap.c */ + +void saveunicodemap(struct kfont_context *ctx, int fd, char *oufil); /* save humanly readable */ +void loadunicodemap(struct kfont_context *ctx, int fd, const char *ufil); +void appendunicodemap(struct kfont_context *ctx, int fd, FILE *fp, + unsigned int ct, int utf8); + +/* kdfontop.c */ + +/* + * Read kernel font into BUF with room for COUNT 32x32 glyphs. + * Return 0 on success -1 on failure. + * Sets number of glyphs in COUNT, glyph size in WIDTH and HEIGHT. + */ +int getfont(struct kfont_context *ctx, int fd, unsigned char *buf, + unsigned int *count, unsigned int *width, unsigned int *height); + +/* + * Load kernel font of width WIDTH and pointsize HEIGHT from BUF + * with length COUNT. + * Return 0 on success, -1 on failure. + */ +int putfont(struct kfont_context *ctx, int fd, unsigned char *buf, + unsigned int count, unsigned int width, unsigned int height); + +/* + * Find the maximum height of nonblank pixels + * (in the ((WIDTH+7)/8)*32*COUNT bytes of BUF). + */ +unsigned int font_charheight(unsigned char *buf, unsigned int count, + unsigned int width); + +/* + * Find the size of the kernel font. + */ +unsigned int getfontsize(struct kfont_context *ctx, int fd); + +/* + * Restore font (doesn't work). + */ +int restorefont(struct kfont_context *ctx, int fd); + +/* kdmapop.c */ + +int getscrnmap(struct kfont_context *ctx, int fd, unsigned char *map); +int loadscrnmap(struct kfont_context *ctx, int fd, unsigned char *map); +int getuniscrnmap(struct kfont_context *ctx, int fd, unsigned short *map); +int loaduniscrnmap(struct kfont_context *ctx, int fd, unsigned short *map); +int getunimap(struct kfont_context *ctx, int fd, struct unimapdesc *ud); +int loadunimap(struct kfont_context *ctx, int fd, struct unimapinit *ui, + struct unimapdesc *ud); + +#endif /* _FONT_H_ */ diff --git a/src/loadunimap.c b/src/loadunimap.c index c4eb2aeb..ea478a2e 100644 --- a/src/loadunimap.c +++ b/src/loadunimap.c @@ -19,13 +19,12 @@ #include <kbdfile.h> #include "paths.h" -#include "kdmapop.h" #include "psffontop.h" -#include "loadunimap.h" #include "utf8.h" #include "psf.h" #include "libcommon.h" +#include "kfont.h" extern char *progname; extern int force; @@ -88,15 +87,20 @@ int main(int argc, char *argv[]) if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); + struct kfont_context ctx = { + .progname = get_progname(), + .log_fn = log_stderr, + }; + if (outfnam) { - saveunicodemap(fd, outfnam); + saveunicodemap(&ctx, fd, outfnam); if (argc == optind) exit(0); } if (argc == optind + 1) infnam = argv[optind]; - loadunicodemap(fd, infnam); + loadunicodemap(&ctx, fd, infnam); exit(0); } #endif @@ -144,11 +148,15 @@ static unsigned int listsz = 0; static unsigned int listct = 0; static void -add_unipair(int fp, int un) +add_unipair(struct kfont_context *ctx, int fp, int un) { if (listct == listsz) { listsz += 4096; - list = xrealloc((char *)list, listsz); + list = realloc(list, listsz); + if (!list) { + ERR(ctx, "realloc: %m"); + exit(EX_OSERR); + } } list[listct].fontpos = fp; list[listct].unicode = un; @@ -168,7 +176,7 @@ add_unipair(int fp, int un) */ static void -parseline(char *buffer, const char *tblname) +parseline(struct kfont_context *ctx, char *buffer, const char *tblname) { int fontlen = 512; int i; @@ -184,7 +192,7 @@ parseline(char *buffer, const char *tblname) fp0 = strtol(p, &p1, 0); if (p1 == p) { - fprintf(stderr, _("Bad input line: %s\n"), buffer); + ERR(ctx, _("Bad input line: %s"), buffer); exit(EX_DATAERR); } p = p1; @@ -195,7 +203,7 @@ parseline(char *buffer, const char *tblname) p++; fp1 = strtol(p, &p1, 0); if (p1 == p) { - fprintf(stderr, _("Bad input line: %s\n"), buffer); + ERR(ctx, _("Bad input line: %s"), buffer); exit(EX_DATAERR); } p = p1; @@ -203,15 +211,13 @@ parseline(char *buffer, const char *tblname) fp1 = 0; if (fp0 < 0 || fp0 >= fontlen) { - fprintf(stderr, - _("%s: Glyph number (0x%x) larger than font length\n"), - tblname, fp0); + ERR(ctx, _("%s: Glyph number (0x%x) larger than font length"), + tblname, fp0); exit(EX_DATAERR); } if (fp1 && (fp1 < fp0 || fp1 >= fontlen)) { - fprintf(stderr, - _("%s: Bad end of range (0x%x)\n"), - tblname, fp1); + ERR(ctx, _("%s: Bad end of range (0x%x)\n"), + tblname, fp1); exit(EX_DATAERR); } @@ -223,7 +229,7 @@ parseline(char *buffer, const char *tblname) if (!strncmp(p, "idem", 4)) { p += 4; for (i = fp0; i <= fp1; i++) - add_unipair(i, i); + add_unipair(ctx, i, i); goto lookattail; } @@ -232,45 +238,44 @@ parseline(char *buffer, const char *tblname) p++; if (*p != '-') { for (i = fp0; i <= fp1; i++) - add_unipair(i, un0); + add_unipair(ctx, i, un0); goto lookattail; } p++; un1 = getunicode(&p); if (un0 < 0 || un1 < 0) { - fprintf(stderr, + ERR(ctx, _("%s: Bad Unicode range corresponding to " - "font position range 0x%x-0x%x\n"), + "font position range 0x%x-0x%x"), tblname, fp0, fp1); exit(EX_DATAERR); } if (un1 - un0 != fp1 - fp0) { - fprintf(stderr, + ERR(ctx, _("%s: Unicode range U+%x-U+%x not of the same" - " length as font position range 0x%x-0x%x\n"), + " length as font position range 0x%x-0x%x"), tblname, un0, un1, fp0, fp1); exit(EX_DATAERR); } for (i = fp0; i <= fp1; i++) - add_unipair(i, un0 - fp0 + i); + add_unipair(ctx, i, un0 - fp0 + i); } else { /* no range; expect a list of unicode values for a single font position */ while ((un0 = getunicode(&p)) >= 0) - add_unipair(fp0, un0); + add_unipair(ctx, fp0, un0); } lookattail: while (*p == ' ' || *p == '\t') p++; if (*p && *p != '#') - fprintf(stderr, _("%s: trailing junk (%s) ignored\n"), - tblname, p); + ERR(ctx, _("%s: trailing junk (%s) ignored"), tblname, p); } -void loadunicodemap(int fd, const char *tblname) +void loadunicodemap(struct kfont_context *ctx, int fd, const char *tblname) { char buffer[65536]; char *p; @@ -280,43 +285,41 @@ void loadunicodemap(int fd, const char *tblname) nomem(); if (kbdfile_find(tblname, unidirpath, unisuffixes, fp)) { - perror(tblname); + ERR(ctx, "unable to find unimap: %s: %m", tblname); exit(EX_NOINPUT); } if (verbose) - printf(_("Loading unicode map from file %s\n"), kbdfile_get_pathname(fp)); + INFO(ctx, _("Loading unicode map from file %s"), kbdfile_get_pathname(fp)); while (fgets(buffer, sizeof(buffer), kbdfile_get_file(fp)) != NULL) { if ((p = strchr(buffer, '\n')) != NULL) *p = '\0'; else - fprintf(stderr, _("%s: %s: Warning: line too long\n"), - get_progname(), tblname); + WARN(ctx, _("%s: Warning: line too long"), tblname); - parseline(buffer, tblname); + parseline(ctx, buffer, tblname); } kbdfile_free(fp); if (listct == 0 && !force) { - fprintf(stderr, - _("%s: not loading empty unimap\n" - "(if you insist: use option -f to override)\n"), - get_progname()); + ERR(ctx, + _("not loading empty unimap\n" + "(if you insist: use option -f to override)")); } else { descr.entry_ct = listct; descr.entries = list; - if (loadunimap(fd, NULL, &descr)) + if (loadunimap(ctx, fd, NULL, &descr)) exit(1); listct = 0; } } static int -getunicodemap(int fd, struct unimapdesc *unimap_descr) +getunicodemap(struct kfont_context *ctx, int fd, struct unimapdesc *unimap_descr) { - if (getunimap(fd, unimap_descr)) + if (getunimap(ctx, fd, unimap_descr)) return -1; #ifdef MAIN @@ -326,7 +329,7 @@ getunicodemap(int fd, struct unimapdesc *unimap_descr) return 0; } -void saveunicodemap(int fd, char *oufil) +void saveunicodemap(struct kfont_context *ctx, int fd, char *oufil) { FILE *fpo; struct unimapdesc unimap_descr = { 0 }; @@ -334,11 +337,11 @@ void saveunicodemap(int fd, char *oufil) int i; if ((fpo = fopen(oufil, "w")) == NULL) { - perror(oufil); + ERR(ctx, "unable to open file: %s", oufil); exit(1); } - if (getunicodemap(fd, &unimap_descr) < 0) + if (getunicodemap(ctx, fd, &unimap_descr) < 0) exit(1); unilist = unimap_descr.entries; @@ -351,14 +354,15 @@ void saveunicodemap(int fd, char *oufil) printf(_("Saved unicode map on `%s'\n"), oufil); } -void appendunicodemap(int fd, FILE *fp, unsigned int fontsize, int utf8) +void appendunicodemap(struct kfont_context *ctx, int fd, FILE *fp, + unsigned int fontsize, int utf8) { struct unimapdesc unimap_descr = { 0 }; struct unipair *unilist; unsigned int i; int j; - if (getunicodemap(fd, &unimap_descr) < 0) + if (getunicodemap(ctx, fd, &unimap_descr) < 0) exit(1); unilist = unimap_descr.entries; @@ -367,7 +371,7 @@ void appendunicodemap(int fd, FILE *fp, unsigned int fontsize, int utf8) #if 0 /* More than one mapping is not a sequence! */ int no = 0; - for(j=0; j<unimap_descr.entry_ct; j++) + for(j=0; j<unimap_descr.entry_ct; j++) if (unilist[j].fontpos == i) no++; if (no > 1) @@ -379,9 +383,9 @@ void appendunicodemap(int fd, FILE *fp, unsigned int fontsize, int utf8) if (unilist[j].fontpos == i) { if (debug) printf("%04x ", unilist[j].unicode); - appendunicode(fp, unilist[j].unicode, utf8); + appendunicode(ctx,fp, unilist[j].unicode, utf8); } - appendseparator(fp, 0, utf8); + appendseparator(ctx, fp, 0, utf8); } if (debug) diff --git a/src/loadunimap.h b/src/loadunimap.h deleted file mode 100644 index 5f5091fc..00000000 --- a/src/loadunimap.h +++ /dev/null @@ -1,10 +0,0 @@ -/* loadunimap.h */ - -#ifndef _LOADUNIMAP_H -#define _LOADUNIMAP_H - -void saveunicodemap(int fd, char *oufil); /* save humanly readable */ -void loadunicodemap(int fd, const char *ufil); -void appendunicodemap(int fd, FILE *fp, unsigned int ct, int utf8); - -#endif /* _LOADUNIMAP_H */ diff --git a/src/mapscrn.c b/src/mapscrn.c index 0611e2d3..f3d3aaf2 100644 --- a/src/mapscrn.c +++ b/src/mapscrn.c @@ -18,13 +18,9 @@ #include "libcommon.h" #include "paths.h" -#include "kdmapop.h" +#include "kfont.h" #include "utf8.h" -/* the two exported functions */ -void saveoldmap(int fd, const char *omfil); -void loadnewmap(int fd, const char *mfil); - static int ctoi(const char *); /* search for the map file in these directories (with trailing /) */ @@ -64,8 +60,13 @@ int main(int argc, char *argv[]) if ((fd = getfd(NULL)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); + struct kfont_context ctx = { + .progname = get_progname(), + .log_fn = log_stderr, + }; + if (argc >= 3 && !strcmp(argv[1], "-o")) { - saveoldmap(fd, argv[2]); + saveoldmap(&ctx, fd, argv[2]); argc -= 2; argv += 2; if (argc == 1) @@ -77,7 +78,7 @@ int main(int argc, char *argv[]) get_progname()); exit(EXIT_FAILURE); } - loadnewmap(fd, argv[1]); + loadnewmap(&ctx, fd, argv[1]); exit(EXIT_SUCCESS); } #endif @@ -192,7 +193,7 @@ readnewmapfromfile(const char *mfil, unsigned char *buf, unsigned short *ubuf) return u; } -void loadnewmap(int fd, const char *mfil) +void loadnewmap(struct kfont_context *ctx, int fd, const char *mfil) { unsigned short ubuf[E_TABSZ]; unsigned char buf[E_TABSZ]; @@ -212,11 +213,11 @@ void loadnewmap(int fd, const char *mfil) /* do we need to use loaduniscrnmap() ? */ if (u) { /* yes */ - if (loaduniscrnmap(fd, ubuf)) + if (loaduniscrnmap(ctx, fd, ubuf)) exit(1); } else { /* no */ - if (loadscrnmap(fd, buf)) + if (loadscrnmap(ctx, fd, buf)) exit(1); } } @@ -262,7 +263,7 @@ int ctoi(const char *s) return i; } -void saveoldmap(int fd, const char *omfil) +void saveoldmap(struct kfont_context *ctx, int fd, const char *omfil) { FILE *fp; unsigned char buf[E_TABSZ]; @@ -274,9 +275,9 @@ void saveoldmap(int fd, const char *omfil) exit(1); } havemap = haveumap = 1; - if (getscrnmap(fd, buf)) + if (getscrnmap(ctx, fd, buf)) havemap = 0; - if (getuniscrnmap(fd, ubuf)) + if (getuniscrnmap(ctx, fd, ubuf)) haveumap = 0; if (havemap && haveumap) { for (i = 0; i < E_TABSZ; i++) { diff --git a/src/psffontop.c b/src/psffontop.c index dfd6bc7a..60eb1f68 100644 --- a/src/psffontop.c +++ b/src/psffontop.c @@ -12,6 +12,7 @@ #include "libcommon.h" #include "psf.h" +#include "context.h" #include "unicode.h" #include "psffontop.h" #include "utf8.h" @@ -37,43 +38,41 @@ store_int_le(unsigned char *ip, unsigned int num) ip[3] = (unsigned char)((num >> 24) & 0xff); } -static int32_t -assemble_ucs2(const unsigned char **inptr, int cnt) +static unicode +assemble_ucs2(struct kfont_context *ctx, const unsigned char **inptr, int cnt) { int u1, u2; if (cnt < 2) { - const char *u = _("%s: short ucs2 unicode table\n"); - fprintf(stderr, u, get_progname()); + ERR(ctx, _("short ucs2 unicode table")); exit(EX_DATAERR); } u1 = *(*inptr)++; u2 = *(*inptr)++; + return (u1 | (u2 << 8)); } /* called with cnt > 0 and **inptr not 0xff or 0xfe */ -static int -assemble_utf8(const unsigned char **inptr, int cnt) +static unicode +assemble_utf8(struct kfont_context *ctx, const unsigned char **inptr, int cnt) { int err; int32_t uc; - const char *u; uc = from_utf8(inptr, cnt, &err); if (err) { switch (err) { case UTF8_SHORT: - u = _("%s: short utf8 unicode table\n"); + ERR(ctx, _("short utf8 unicode table")); break; case UTF8_BAD: - u = _("%s: bad utf8\n"); + ERR(ctx, _("bad utf8")); break; default: - u = _("%s: unknown utf8 error\n"); + ERR(ctx, _("unknown utf8 error")); } - fprintf(stderr, u, get_progname()); exit(EX_DATAERR); } return uc; @@ -83,7 +82,9 @@ assemble_utf8(const unsigned char **inptr, int cnt) * Read description of a single font position. */ static void -get_uni_entry(const unsigned char **inptr, const unsigned char **endptr, struct unicode_list *up, int utf8) +get_uni_entry(struct kfont_context *ctx, + const unsigned char **inptr, const unsigned char **endptr, + struct unicode_list *up, int utf8) { unsigned char uc; unicode unichar; @@ -95,8 +96,7 @@ get_uni_entry(const unsigned char **inptr, const unsigned char **endptr, struct while (1) { if (*endptr == *inptr) { - const char *u = _("%s: short unicode table\n"); - fprintf(stderr, u, get_progname()); + ERR(ctx, _("short unicode table")); exit(EX_DATAERR); } if (utf8) { @@ -108,9 +108,9 @@ get_uni_entry(const unsigned char **inptr, const unsigned char **endptr, struct continue; } --(*inptr); - unichar = assemble_utf8(inptr, *endptr - *inptr); + unichar = assemble_utf8(ctx, inptr, *endptr - *inptr); } else { - unichar = assemble_ucs2(inptr, *endptr - *inptr); + unichar = assemble_ucs2(ctx, inptr, *endptr - *inptr); if (unichar == PSF1_SEPARATOR) break; if (unichar == PSF1_STARTSEQ) { @@ -127,7 +127,7 @@ get_uni_entry(const unsigned char **inptr, const unsigned char **endptr, struct ret = addseq(up, unichar); if (ret < 0) { - fprintf(stderr, "%s\n", strerror(-ret)); + ERR(ctx, "unable to unichar: %s", strerror(-ret)); exit(EX_OSERR); } @@ -138,7 +138,8 @@ get_uni_entry(const unsigned char **inptr, const unsigned char **endptr, struct extern char *progname; -int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, +int readpsffont(struct kfont_context *ctx, + FILE *fontf, unsigned char **allbufp, unsigned int *allszp, unsigned char **fontbufp, unsigned int *fontszp, unsigned int *fontwidthp, unsigned int *fontlenp, unsigned int fontpos0, struct unicode_list **uclistheadsp) @@ -156,18 +157,28 @@ int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, */ if (fontf) { inputbuflth = MAXFONTSIZE / 4; /* random */ - inputbuf = xmalloc(inputbuflth); - n = 0; + + inputbuf = malloc(inputbuflth); + if (!inputbuf) { + ERR(ctx, "malloc: %m"); + exit(EX_OSERR); + } + + n = 0; while (1) { if (n >= inputbuflth) { inputbuflth *= 2; - inputbuf = xrealloc(inputbuf, inputbuflth); + + inputbuf = realloc(inputbuf, inputbuflth); + if (!inputbuf) { + ERR(ctx, "realloc: %m"); + exit(EX_OSERR); + } } n += fread(inputbuf + n, 1, inputbuflth - n, fontf); if (ferror(fontf)) { - const char *u = _("%s: Error reading input font"); - fprintf(stderr, u, progname); + ERR(ctx, _("Error reading input font")); exit(EX_DATAERR); } if (feof(fontf)) @@ -180,8 +191,7 @@ int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, inputlth = n; } else { if (!allbufp || !allszp) { - const char *u = _("%s: Bad call of readpsffont\n"); - fprintf(stderr, u, progname); + ERR(ctx, _("Bad call of readpsffont")); exit(EX_SOFTWARE); } inputbuf = *allbufp; @@ -195,8 +205,7 @@ int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, psfhdr = (struct psf1_header *)&inputbuf[0]; if (psfhdr->mode > PSF1_MAXMODE) { - const char *u = _("%s: Unsupported psf file mode (%d)\n"); - fprintf(stderr, u, progname, psfhdr->mode); + ERR(ctx, _("Unsupported psf file mode (%d)"), psfhdr->mode); exit(EX_DATAERR); } fontlen = ((psfhdr->mode & PSF1_MODE512) ? 512 : 256); @@ -213,8 +222,7 @@ int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, memcpy(&psfhdr, inputbuf, sizeof(struct psf2_header)); if (psfhdr.version > PSF2_MAXVERSION) { - const char *u = _("%s: Unsupported psf version (%d)\n"); - fprintf(stderr, u, progname, psfhdr.version); + ERR(ctx, _("Unsupported psf version (%d)"), psfhdr.version); exit(EX_DATAERR); } fontlen = assemble_uint32((unsigned char *)&psfhdr.length); @@ -229,19 +237,16 @@ int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, /* tests required - we divide by these */ if (fontlen == 0) { - const char *u = _("%s: zero input font length?\n"); - fprintf(stderr, u, progname); + ERR(ctx, _("zero input font length?")); exit(EX_DATAERR); } if (charsize == 0) { - const char *u = _("%s: zero input character size?\n"); - fprintf(stderr, u, progname); + ERR(ctx, _("zero input character size?")); exit(EX_DATAERR); } i = ftoffset + fontlen * charsize; if (i > inputlth || (!hastable && i != inputlth)) { - const char *u = _("%s: Input file: bad input length (%d)\n"); - fprintf(stderr, u, progname, inputlth); + ERR(ctx, _("Input file: bad input length (%d)"), inputlth); exit(EX_DATAERR); } @@ -257,8 +262,15 @@ int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, if (!uclistheadsp) return 0; /* got font, don't need unicode_list */ - *uclistheadsp = xrealloc(*uclistheadsp, - (fontpos0 + fontlen) * sizeof(struct unicode_list)); + void *ptr; + + ptr = realloc(*uclistheadsp, + (fontpos0 + fontlen) * sizeof(struct unicode_list)); + if (!ptr) { + ERR(ctx, "realloc: %m"); + exit(EX_OSERR); + } + *uclistheadsp = ptr; if (hastable) { const unsigned char *inptr, *endptr; @@ -268,12 +280,11 @@ int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, for (i = 0; i < fontlen; i++) { k = fontpos0 + i; - get_uni_entry(&inptr, &endptr, + get_uni_entry(ctx, &inptr, &endptr, &(*uclistheadsp)[k], utf8); } if (inptr != endptr) { - const char *u = _("%s: Input file: trailing garbage\n"); - fprintf(stderr, u, progname); + ERR(ctx, _("Input file: trailing garbage")); exit(EX_DATAERR); } } else { @@ -305,13 +316,13 @@ has_sequences(struct unicode_list *uclistheads, unsigned int fontlen) return 0; } -void appendunicode(FILE *fp, int u, int utf8) +void appendunicode(struct kfont_context *ctx, FILE *fp, int u, int utf8) { unsigned int n = 6; unsigned char out[6]; if (u < 0) { - fprintf(stderr, _("appendunicode: illegal unicode %d\n"), u); + ERR(ctx, _("appendunicode: illegal unicode %d"), u); exit(1); } @@ -332,7 +343,7 @@ void appendunicode(FILE *fp, int u, int utf8) out[--n] = ((uc + ~mask + ~mask) & 0xff); } if (fwrite(out + n, 6 - n, 1, fp) != 1) { - perror("appendunimap"); + ERR(ctx, "appendunimap: %m"); exit(1); } if (debug) { @@ -345,25 +356,26 @@ void appendunicode(FILE *fp, int u, int utf8) } } -void appendseparator(FILE *fp, int seq, int utf8) +void appendseparator(struct kfont_context *ctx, FILE *fp, int seq, int utf8) { size_t n; if (utf8) { unsigned char u = (seq ? PSF2_STARTSEQ : PSF2_SEPARATOR); - n = fwrite(&u, sizeof(u), 1, fp); + n = fwrite(&u, sizeof(u), 1, fp); } else { unsigned short u = (seq ? PSF1_STARTSEQ : PSF1_SEPARATOR); - n = fwrite(&u, sizeof(u), 1, fp); + n = fwrite(&u, sizeof(u), 1, fp); } if (n != 1) { - perror("appendseparator"); + ERR(ctx, "fwrite: %m"); exit(1); } } -void writepsffontheader(FILE *ofil, unsigned int width, unsigned int height, unsigned int fontlen, - int *psftype, int flags) +void writepsffontheader(struct kfont_context *ctx, + FILE *ofil, unsigned int width, unsigned int height, + unsigned int fontlen, int *psftype, int flags) { unsigned int bytewidth, charsize; size_t ret; @@ -405,16 +417,17 @@ void writepsffontheader(FILE *ofil, unsigned int width, unsigned int height, uns else if (flags & WPSFH_HASTAB) h.mode |= PSF1_MODEHASTAB; h.charsize = charsize; - ret = fwrite(&h, sizeof(h), 1, ofil); + ret = fwrite(&h, sizeof(h), 1, ofil); } if (ret != 1) { - fprintf(stderr, _("Cannot write font file header")); + ERR(ctx, _("Cannot write font file header")); exit(EX_IOERR); } } -int writepsffont(FILE *ofil, unsigned char *fontbuf, unsigned int width, unsigned int height, unsigned int fontlen, +int writepsffont(struct kfont_context *ctx, + FILE *ofil, unsigned char *fontbuf, unsigned int width, unsigned int height, unsigned int fontlen, int psftype, struct unicode_list *uclistheads) { unsigned int bytewidth, charsize, i; @@ -430,11 +443,11 @@ int writepsffont(FILE *ofil, unsigned char *fontbuf, unsigned int width, unsigne flags |= WPSFH_HASSEQ; } - writepsffontheader(ofil, width, height, fontlen, &psftype, flags); + writepsffontheader(ctx, ofil, width, height, fontlen, &psftype, flags); utf8 = (psftype == 2); if ((fwrite(fontbuf, charsize, fontlen, ofil)) != fontlen) { - fprintf(stderr, _("Cannot write font file")); + ERR(ctx, _("Cannot write font file")); exit(EX_IOERR); } @@ -448,14 +461,14 @@ int writepsffont(FILE *ofil, unsigned char *fontbuf, unsigned int width, unsigne while (ul) { us = ul->seq; if (us && us->next) - appendseparator(ofil, 1, utf8); + appendseparator(ctx, ofil, 1, utf8); while (us) { - appendunicode(ofil, us->uc, utf8); + appendunicode(ctx, ofil, us->uc, utf8); us = us->next; } ul = ul->next; } - appendseparator(ofil, 0, utf8); + appendseparator(ctx, ofil, 0, utf8); } } return utf8; diff --git a/src/psffontop.h b/src/psffontop.h index b36fc5d5..e9d74549 100644 --- a/src/psffontop.h +++ b/src/psffontop.h @@ -4,6 +4,7 @@ #define _PSFFONTOP_H #include <stdint.h> +#include "context.h" #include "unicode.h" /* Maximum font size that we try to handle */ @@ -33,22 +34,24 @@ * not psf (but has been read). > 0 means that the Unicode table contains * sequences. */ -extern int readpsffont(FILE *fontf, unsigned char **allbufp, unsigned int *allszp, - unsigned char **fontbufp, unsigned int *fontszp, - unsigned int *fontwidthp, unsigned int *fontlenp, unsigned int fontpos0, - struct unicode_list **uclistheadsp); +int readpsffont(struct kfont_context *ctx, + FILE *fontf, unsigned char **allbufp, unsigned int *allszp, + unsigned char **fontbufp, unsigned int *fontszp, + unsigned int *fontwidthp, unsigned int *fontlenp, unsigned int fontpos0, + struct unicode_list **uclistheadsp); -extern int writepsffont(FILE *ofil, unsigned char *fontbuf, - unsigned int width, unsigned int height, unsigned int fontlen, int psftype, - struct unicode_list *uclistheads); +int writepsffont(struct kfont_context *ctx, + FILE *ofil, unsigned char *fontbuf, + unsigned int width, unsigned int height, unsigned int fontlen, int psftype, + struct unicode_list *uclistheads); #define WPSFH_HASTAB 1 #define WPSFH_HASSEQ 2 -extern void writepsffontheader(FILE *ofil, - unsigned int width, unsigned int height, unsigned int fontlen, - int *psftype, int flags); +void writepsffontheader(struct kfont_context *ctx, FILE *ofil, + unsigned int width, unsigned int height, unsigned int fontlen, + int *psftype, int flags); -extern void appendunicode(FILE *fp, unicode uc, int utf8); -extern void appendseparator(FILE *fp, int seq, int utf8); +void appendunicode(struct kfont_context *ctx, FILE *fp, unicode uc, int utf8); +void appendseparator(struct kfont_context *ctx, FILE *fp, int seq, int utf8); #endif /* _PSFFONTOP_H */ diff --git a/src/psfxtable.c b/src/psfxtable.c index 2c5a00c5..89ca3a92 100644 --- a/src/psfxtable.c +++ b/src/psfxtable.c @@ -23,6 +23,7 @@ #include "psf.h" #include "psffontop.h" +#include "context.h" /* * call: psfxtable -i infont -o outfont -it intable -ot outtable @@ -69,7 +70,7 @@ getunicode(char **p0) } static void -parse_itab_line(char *buf, unsigned int fontlen) +parse_itab_line(struct kfont_context *ctx, char *buf, unsigned int fontlen) { char *p, *p1; long i; @@ -78,8 +79,7 @@ parse_itab_line(char *buf, unsigned int fontlen) if ((p = strchr(buf, '\n')) != NULL) *p = 0; else { - const char *u = _("%s: Warning: line too long\n"); - fprintf(stderr, u, get_progname()); + ERR(ctx, _("Warning: line too long")); exit(EX_DATAERR); } @@ -92,8 +92,7 @@ parse_itab_line(char *buf, unsigned int fontlen) fp0 = strtol(p, &p1, 0); if (p1 == p) { - const char *u = _("%s: Bad input line: %s\n"); - fprintf(stderr, u, get_progname(), buf); + ERR(ctx, _("Bad input line: %s"), buf); exit(EX_DATAERR); } p = p1; @@ -102,8 +101,7 @@ parse_itab_line(char *buf, unsigned int fontlen) p++; fp1 = strtol(p, &p1, 0); if (p1 == p) { - const char *u = _("%s: Bad input line: %s\n"); - fprintf(stderr, u, get_progname(), buf); + ERR(ctx, _("Bad input line: %s"), buf); exit(EX_DATAERR); } p = p1; @@ -111,13 +109,11 @@ parse_itab_line(char *buf, unsigned int fontlen) fp1 = 0; if (fp0 < 0 || fp0 >= fontlen) { - const char *u = _("%s: Glyph number (0x%lx) past end of font\n"); - fprintf(stderr, u, get_progname(), fp0); + ERR(ctx, _("Glyph number (0x%lx) past end of font"), fp0); exit(EX_DATAERR); } if (fp1 && (fp1 < fp0 || fp1 >= fontlen)) { - const char *u = _("%s: Bad end of range (0x%lx)\n"); - fprintf(stderr, u, get_progname(), fp1); + ERR(ctx, _("Bad end of range (0x%lx)"), fp1); exit(EX_DATAERR); } @@ -132,7 +128,7 @@ parse_itab_line(char *buf, unsigned int fontlen) for (i = fp0; i <= fp1; i++) { ret = addpair(uclistheads + i, i); if (ret < 0) { - fprintf(stderr, "%s\n", strerror(-ret)); + ERR(ctx, "unable to add pair: %s", strerror(-ret)); exit(EX_OSERR); } } @@ -142,33 +138,34 @@ parse_itab_line(char *buf, unsigned int fontlen) while (*p == ' ' || *p == '\t') p++; if (*p != '-') { - const char *u = _("%s: Corresponding to a range of " - "font positions, there should be " - "a Unicode range\n"); - fprintf(stderr, u, get_progname()); + ERR(ctx, + _("Corresponding to a range of " + "font positions, there should be " + "a Unicode range")); exit(EX_DATAERR); } p++; un1 = getunicode(&p); if (un0 < 0 || un1 < 0) { - const char *u = _("%s: Bad Unicode range " - "corresponding to font position " - "range 0x%x-0x%x\n"); - fprintf(stderr, u, get_progname(), fp0, fp1); + ERR(ctx, + _("Bad Unicode range " + "corresponding to font position " + "range 0x%lx-0x%lx"), + fp0, fp1); exit(EX_DATAERR); } if (un1 - un0 != fp1 - fp0) { - const char *u = _("%s: Unicode range U+%x-U+%x not " - "of the same length as font " - "position range 0x%x-0x%x\n"); - fprintf(stderr, u, get_progname(), + ERR(ctx, + _("Unicode range U+%lx-U+%lx not " + "of the same length as font " + "position range 0x%lx-0x%lx"), un0, un1, fp0, fp1); exit(EX_DATAERR); } for (i = fp0; i <= fp1; i++) { ret = addpair(uclistheads + i, un0 - fp0 + i); if (ret < 0) { - fprintf(stderr, "%s\n", strerror(-ret)); + ERR(ctx, "unable to add pair: %s", strerror(-ret)); exit(EX_OSERR); } } @@ -177,13 +174,13 @@ parse_itab_line(char *buf, unsigned int fontlen) while ((un0 = getunicode(&p)) >= 0) { ret = addpair(uclistheads + fp0, un0); if (ret < 0) { - fprintf(stderr, "%s\n", strerror(-ret)); + ERR(ctx, "unable to add pair: %s", strerror(-ret)); exit(EX_OSERR); } while (*p++ == ',' && (un1 = getunicode(&p)) >= 0) { ret = addseq(uclistheads + fp0, un1); if (ret < 0) { - fprintf(stderr, "%s\n", strerror(-ret)); + ERR(ctx, "unable to add sequence: %s", strerror(-ret)); exit(EX_OSERR); } } @@ -192,29 +189,35 @@ parse_itab_line(char *buf, unsigned int fontlen) while (*p == ' ' || *p == '\t') p++; if (*p && *p != '#') { - const char *u = _("%s: trailing junk (%s) ignored\n"); - fprintf(stderr, u, get_progname(), p); + ERR(ctx, _("trailing junk (%s) ignored"), p); } } } static void -read_itable(FILE *itab, unsigned int fontlen, struct unicode_list **uclistheadsp) +read_itable(struct kfont_context *ctx, FILE *itab, unsigned int fontlen, + struct unicode_list **uclistheadsp) { char buf[65536]; unsigned int i; if (uclistheadsp) { - *uclistheadsp = xrealloc(*uclistheadsp, + *uclistheadsp = realloc(*uclistheadsp, fontlen * sizeof(struct unicode_list)); + if (!*uclistheadsp) { + ERR(ctx, "realloc: %m"); + exit(EX_OSERR); + } + for (i = 0; i < fontlen; i++) { struct unicode_list *up = &((*uclistheadsp)[i]); up->next = NULL; up->seq = NULL; up->prev = up; } + while (fgets(buf, sizeof(buf), itab) != NULL) - parse_itab_line(buf, fontlen); + parse_itab_line(ctx, buf, fontlen); } } @@ -241,6 +244,11 @@ int main(int argc, char **argv) fontbuf = NULL; notable = 0; + struct kfont_context ctx = { + .progname = get_progname(), + .log_fn = log_stderr, + }; + if (!strcmp(get_progname(), "psfaddtable")) { /* Do not send binary data to stdout without explicit "-" */ if (argc != 4) { @@ -299,7 +307,7 @@ int main(int argc, char **argv) else { ifil = fopen(ifname, "r"); if (!ifil) { - perror(ifname); + ERR(&ctx, "Unable to open: %s: %m", ifname); exit(EX_NOINPUT); } } @@ -311,7 +319,7 @@ int main(int argc, char **argv) else { itab = fopen(itname, "r"); if (!itab) { - perror(itname); + ERR(&ctx, "Unable to open: %s: %m", itname); exit(EX_NOINPUT); } } @@ -325,7 +333,7 @@ int main(int argc, char **argv) else { ofil = fopen(ofname, "w"); if (!ofil) { - perror(ofname); + ERR(&ctx, "Unable to open: %s: %m", ofname); exit(EX_CANTCREAT); } } @@ -337,16 +345,15 @@ int main(int argc, char **argv) else { otab = fopen(otname, "w"); if (!otab) { - perror(otname); + ERR(&ctx, "Unable to open: %s: %m", otname); exit(EX_CANTCREAT); } } - if (readpsffont(ifil, &inbuf, &inbuflth, &fontbuf, &fontbuflth, + if (readpsffont(&ctx, ifil, &inbuf, &inbuflth, &fontbuf, &fontbuflth, &width, &fontlen, 0, itab ? NULL : &uclistheads) == -1) { - const char *u = _("%s: Bad magic number on %s\n"); - fprintf(stderr, u, get_progname(), ifname); + ERR(&ctx, _("Bad magic number on %s"), ifname); exit(EX_DATAERR); } fclose(ifil); @@ -364,13 +371,12 @@ int main(int argc, char **argv) } else if (PSF2_MAGIC_OK((unsigned char *)inbuf)) { psftype = 2; } else { - const char *u = _("%s: psf file with unknown magic\n"); - fprintf(stderr, u, get_progname()); + ERR(&ctx, _("psf file with unknown magic")); exit(EX_DATAERR); } if (itab) { - read_itable(itab, fontlen, &uclistheads); + read_itable(&ctx, itab, fontlen, &uclistheads); fclose(itab); } @@ -380,8 +386,7 @@ int main(int argc, char **argv) const char *sep; if (!hastable) { - const char *u = _("%s: input font does not have an index\n"); - fprintf(stderr, u, get_progname()); + ERR(&ctx, _("input font does not have an index")); exit(EX_DATAERR); } fprintf(otab, @@ -407,7 +412,7 @@ int main(int argc, char **argv) } if (ofil) { - writepsffont(ofil, fontbuf, width, height, fontlen, psftype, + writepsffont(&ctx, ofil, fontbuf, width, height, fontlen, psftype, notable ? NULL : uclistheads); fclose(ofil); } diff --git a/src/readpsfheader.c b/src/readpsfheader.c index 2c608750..82d09632 100644 --- a/src/readpsfheader.c +++ b/src/readpsfheader.c @@ -3,10 +3,8 @@ #include <stdio.h> #include <stdlib.h> /* exit */ -#include "kdmapop.h" -#include "kdfontop.h" - #include "libcommon.h" +#include "kfont.h" int verbose = 0; int debug = 0; @@ -38,7 +36,12 @@ int main(int argc, char **argv) exit(1); } - if (readpsffont(f, &inbuf, &inbuflth, &fontbuf, &fontbuflth, &width, &fontlen, 0, &uclistheads) == -1) { + struct kfont_context ctx = { + .progname = get_progname(), + .log_fn = log_stderr, + }; + + if (readpsffont(&ctx, f, &inbuf, &inbuflth, &fontbuf, &fontbuflth, &width, &fontlen, 0, &uclistheads) == -1) { fprintf(stderr, "%s: Bad magic number\n", argv[0]); return 1; } diff --git a/src/setfont.c b/src/setfont.c index 5d06b4f6..18e2218a 100644 --- a/src/setfont.c +++ b/src/setfont.c @@ -29,23 +29,21 @@ #include "libcommon.h" #include "paths.h" -#include "loadunimap.h" #include "psf.h" #include "psffontop.h" -#include "kdfontop.h" -#include "kdmapop.h" +#include "kfont.h" static unsigned int position_codepage(unsigned int iunit); -static void saveoldfont(int fd, const char *ofil); -static void saveoldfontplusunicodemap(int fd, const char *Ofil); -static void loadnewfont(int fd, const char *ifil, +static void saveoldfont(struct kfont_context *ctx, int fd, const char *ofil); +static void saveoldfontplusunicodemap(struct kfont_context *ctx, int fd, const char *Ofil); +static void loadnewfont(struct kfont_context *ctx, + int fd, const char *ifil, unsigned int iunit, unsigned int hwunit, int no_m, int no_u); -static void loadnewfonts(int fd, const char *const *ifiles, int ifilct, +static void loadnewfonts(struct kfont_context *ctx, + int fd, const char *const *ifiles, int ifilct, unsigned int iunit, unsigned int hwunit, int no_m, int no_u); -extern void saveoldmap(int fd, char *omfil); -extern void loadnewmap(int fd, char *mfil); -extern void activatemap(int fd); -extern void disactivatemap(int fd); +static void activatemap(int fd); +static void disactivatemap(int fd); int verbose = 0; int force = 0; @@ -227,25 +225,30 @@ int main(int argc, char *argv[]) return 0; } + struct kfont_context ctx = { + .progname = get_progname(), + .log_fn = log_stderr, + }; + if (!ifilct && !mfil && !ufil && !Ofil && !ofil && !omfil && !oufil && !restore) /* reset to some default */ ifiles[ifilct++] = ""; if (Ofil) - saveoldfontplusunicodemap(fd, Ofil); + saveoldfontplusunicodemap(&ctx, fd, Ofil); if (ofil) - saveoldfont(fd, ofil); + saveoldfont(&ctx, fd, ofil); if (omfil) - saveoldmap(fd, omfil); + saveoldmap(&ctx, fd, omfil); if (oufil) - saveunicodemap(fd, oufil); + saveunicodemap(&ctx, fd, oufil); if (mfil) { - loadnewmap(fd, mfil); + loadnewmap(&ctx, fd, mfil); activatemap(fd); no_m = 1; } @@ -254,13 +257,13 @@ int main(int argc, char *argv[]) no_u = 1; if (restore) - restorefont(fd); + restorefont(&ctx, fd); if (ifilct) - loadnewfonts(fd, ifiles, ifilct, iunit, hwunit, no_m, no_u); + loadnewfonts(&ctx, fd, ifiles, ifilct, iunit, hwunit, no_m, no_u); if (ufil) - loadunicodemap(fd, ufil); + loadunicodemap(&ctx, fd, ufil); return 0; } @@ -271,7 +274,8 @@ int main(int argc, char *argv[]) static int erase_mode = 1; static void -do_loadfont(int fd, const unsigned char *inbuf, unsigned int width, unsigned int height, unsigned int hwunit, +do_loadfont(struct kfont_context *ctx, int fd, const unsigned char *inbuf, + unsigned int width, unsigned int height, unsigned int hwunit, unsigned int fontsize, const char *filename) { unsigned char *buf; @@ -279,11 +283,11 @@ do_loadfont(int fd, const unsigned char *inbuf, unsigned int width, unsigned int int bad_video_erase_char = 0; if (height < 1 || height > 32) { - fprintf(stderr, _("Bad character height %d\n"), height); + ERR(ctx, _("Bad character height %d"), height); exit(EX_DATAERR); } if (width < 1 || width > 32) { - fprintf(stderr, _("Bad character width %d\n"), width); + ERR(ctx, _("Bad character width %d"), width); exit(EX_DATAERR); } @@ -291,8 +295,7 @@ do_loadfont(int fd, const unsigned char *inbuf, unsigned int width, unsigned int hwunit = height; if (double_size && (height > 16 || width > 16)) { - fprintf(stderr, _("Cannot double %dx%d font (limit is 16x16)"), - width, height); + ERR(ctx, _("Cannot double %dx%d font (limit is 16x16)"), width, height); double_size = 0; } @@ -300,9 +303,16 @@ do_loadfont(int fd, const unsigned char *inbuf, unsigned int width, unsigned int unsigned int bytewidth = (width + 7) / 8; unsigned int kbytewidth = (2 * width + 7) / 8; unsigned int charsize = height * bytewidth; - kcharsize = 32 * kbytewidth; - buflen = kcharsize * ((fontsize < 128) ? 128 : fontsize); - buf = xmalloc(buflen); + + kcharsize = 32 * kbytewidth; + buflen = kcharsize * ((fontsize < 128) ? 128 : fontsize); + + buf = malloc(buflen); + if (!buf) { + ERR(ctx, "malloc: %m"); + exit(EX_OSERR); + } + memset(buf, 0, buflen); const unsigned char *src = inbuf; @@ -329,9 +339,16 @@ do_loadfont(int fd, const unsigned char *inbuf, unsigned int width, unsigned int } else { unsigned int bytewidth = (width + 7) / 8; unsigned int charsize = height * bytewidth; - kcharsize = 32 * bytewidth; - buflen = kcharsize * ((fontsize < 128) ? 128 : fontsize); - buf = xmalloc(buflen); + + kcharsize = 32 * bytewidth; + buflen = kcharsize * ((fontsize < 128) ? 128 : fontsize); + + buf = malloc(buflen); + if (!buf) { + ERR(ctx, "malloc: %m"); + exit(EX_OSERR); + } + memset(buf, 0, buflen); for (i = 0; i < fontsize; i++) @@ -344,52 +361,52 @@ do_loadfont(int fd, const unsigned char *inbuf, unsigned int width, unsigned int * So, usually this font position should be blank. */ if (erase_mode) { - for (i = 0; i < kcharsize; i++) + for (i = 0; i < kcharsize; i++) { if (buf[32 * kcharsize + i]) bad_video_erase_char = 1; + } + if (bad_video_erase_char) { - fprintf(stderr, - _("%s: font position 32 is nonblank\n"), - get_progname()); + ERR(ctx, _("font position 32 is nonblank")); + switch (erase_mode) { case 3: exit(EX_DATAERR); case 2: - for (i = 0; i < kcharsize; i++) + for (i = 0; i < kcharsize; i++) buf[32 * kcharsize + i] = 0; - fprintf(stderr, _("%s: wiped it\n"), get_progname()); + ERR(ctx, _("wiped it")); break; case 1: - fprintf(stderr, - _("%s: background will look funny\n"), - get_progname()); + ERR(ctx, _("background will look funny")); + break; } - fflush(stderr); sleep(2); } } if (verbose) { if (height == hwunit && filename) - printf(_("Loading %d-char %dx%d font from file %s\n"), + INFO(ctx, _("Loading %d-char %dx%d font from file %s"), fontsize, width, height, filename); else if (height == hwunit) - printf(_("Loading %d-char %dx%d font\n"), + INFO(ctx, _("Loading %d-char %dx%d font"), fontsize, width, height); else if (filename) - printf(_("Loading %d-char %dx%d (%d) font from file %s\n"), + INFO(ctx, _("Loading %d-char %dx%d (%d) font from file %s"), fontsize, width, height, hwunit, filename); else - printf(_("Loading %d-char %dx%d (%d) font\n"), + INFO(ctx, _("Loading %d-char %dx%d (%d) font"), fontsize, width, height, hwunit); } - if (putfont(fd, buf, fontsize, width, hwunit)) + if (putfont(ctx, fd, buf, fontsize, width, hwunit)) exit(EX_OSERR); } static void -do_loadtable(int fd, struct unicode_list *uclistheads, unsigned int fontsize) +do_loadtable(struct kfont_context *ctx, int fd, struct unicode_list *uclistheads, + unsigned int fontsize) { struct unimapdesc ud; struct unipair *up; @@ -407,7 +424,13 @@ do_loadtable(int fd, struct unicode_list *uclistheads, unsigned int fontsize) ul = ul->next; } } - up = xmalloc(maxct * sizeof(struct unipair)); + + up = malloc(maxct * sizeof(*up)); + if (!up) { + ERR(ctx, "malloc: %m"); + exit(EX_OSERR); + } + for (i = 0; i < fontsize; i++) { ul = uclistheads[i].next; if (debug) @@ -436,22 +459,22 @@ do_loadtable(int fd, struct unicode_list *uclistheads, unsigned int fontsize) printf("\n"); } if (ct != maxct) { - const char *u = _("%s: bug in do_loadtable\n"); - fprintf(stderr, u, get_progname()); + ERR(ctx, _("bug in do_loadtable")); exit(EX_SOFTWARE); } if (verbose) - printf(_("Loading Unicode mapping table...\n")); + INFO(ctx, _("Loading Unicode mapping table...")); ud.entry_ct = ct; ud.entries = up; - if (loadunimap(fd, NULL, &ud)) + if (loadunimap(ctx, fd, NULL, &ud)) exit(EX_OSERR); } static void -loadnewfonts(int fd, const char *const *ifiles, int ifilct, +loadnewfonts(struct kfont_context *ctx, + int fd, const char *const *ifiles, int ifilct, unsigned int iunit, unsigned int hwunit, int no_m, int no_u) { const char *ifil; @@ -462,7 +485,7 @@ loadnewfonts(int fd, const char *const *ifiles, int ifilct, int i; if (ifilct == 1) { - loadnewfont(fd, ifiles[0], iunit, hwunit, no_m, no_u); + loadnewfont(ctx, fd, ifiles[0], iunit, hwunit, no_m, no_u); return; } @@ -483,7 +506,7 @@ loadnewfonts(int fd, const char *const *ifiles, int ifilct, ifil = ifiles[i]; if (findfont(ifil, fp) && findpartialfont(ifil, fp)) { - fprintf(stderr, _("Cannot open font file %s\n"), ifil); + ERR(ctx, _("Cannot open font file %s"), ifil); exit(EX_NOINPUT); } @@ -491,12 +514,11 @@ loadnewfonts(int fd, const char *const *ifiles, int ifilct, inputlth = fontbuflth = 0; fontsize = 0; - if (readpsffont(kbdfile_get_file(fp), &inbuf, &inputlth, &fontbuf, &fontbuflth, - &width, &fontsize, bigfontsize, + if (readpsffont(ctx, kbdfile_get_file(fp), &inbuf, &inputlth, + &fontbuf, &fontbuflth, &width, &fontsize, bigfontsize, no_u ? NULL : &uclistheads)) { - fprintf(stderr, _("When loading several fonts, all " - "must be psf fonts - %s isn't\n"), - kbdfile_get_pathname(fp)); + ERR(ctx, _("When loading several fonts, all must be psf fonts - %s isn't"), + kbdfile_get_pathname(fp)); kbdfile_free(fp); exit(EX_DATAERR); } @@ -505,42 +527,47 @@ loadnewfonts(int fd, const char *const *ifiles, int ifilct, height = fontbuflth / (bytewidth * fontsize); if (verbose) - printf(_("Read %d-char %dx%d font from file %s\n"), - fontsize, width, height, kbdfile_get_pathname(fp)); + INFO(ctx, _("Read %d-char %dx%d font from file %s"), + fontsize, width, height, kbdfile_get_pathname(fp)); kbdfile_free(fp); // avoid zombies, jw@suse.de (#88501) if (bigheight == 0) bigheight = height; else if (bigheight != height) { - fprintf(stderr, _("When loading several fonts, all " - "must have the same height\n")); + ERR(ctx, _("When loading several fonts, all must have the same height")); exit(EX_DATAERR); } if (bigwidth == 0) bigwidth = width; else if (bigwidth != width) { - fprintf(stderr, _("When loading several fonts, all " - "must have the same width\n")); + ERR(ctx, _("When loading several fonts, all must have the same width")); exit(EX_DATAERR); } bigfontsize += fontsize; bigfontbuflth += fontbuflth; - bigfontbuf = xrealloc(bigfontbuf, bigfontbuflth); + + bigfontbuf = realloc(bigfontbuf, bigfontbuflth); + if (!bigfontbuf) { + ERR(ctx, "realloc: %m"); + exit(EX_OSERR); + } + memcpy(bigfontbuf + bigfontbuflth - fontbuflth, fontbuf, fontbuflth); } - do_loadfont(fd, bigfontbuf, bigwidth, bigheight, hwunit, + do_loadfont(ctx, fd, bigfontbuf, bigwidth, bigheight, hwunit, bigfontsize, NULL); free(bigfontbuf); if (uclistheads && !no_u) - do_loadtable(fd, uclistheads, bigfontsize); + do_loadtable(ctx, fd, uclistheads, bigfontsize); } static void -loadnewfont(int fd, const char *ifil, unsigned int iunit, unsigned int hwunit, int no_m, int no_u) +loadnewfont(struct kfont_context *ctx, int fd, const char *ifil, + unsigned int iunit, unsigned int hwunit, int no_m, int no_u) { struct kbdfile *fp; @@ -551,8 +578,10 @@ loadnewfont(int fd, const char *ifil, unsigned int iunit, unsigned int hwunit, i unsigned int inputlth, fontbuflth, fontsize, offset; struct unicode_list *uclistheads; - if ((fp = kbdfile_new(NULL)) == NULL) - nomem(); + if (!(fp = kbdfile_new(NULL))) { + ERR(ctx, "Unable to create kbdfile instance: %m"); + exit(EX_OSERR); + } if (!*ifil) { /* try to find some default file */ @@ -566,47 +595,47 @@ loadnewfont(int fd, const char *ifil, unsigned int iunit, unsigned int hwunit, i findfont(ifil = "default8x16", fp) && findfont(ifil = "default8x14", fp) && findfont(ifil = "default8x8", fp)) { - fprintf(stderr, _("Cannot find default font\n")); + ERR(ctx, _("Cannot find default font")); exit(EX_NOINPUT); } } else { sprintf(defname, "default8x%u", iunit); if (findfont(ifil = defname, fp) && findfont(ifil = "default", fp)) { - fprintf(stderr, _("Cannot find %s font\n"), ifil); + ERR(ctx, _("Cannot find %s font"), ifil); exit(EX_NOINPUT); } } } else { if (findfont(ifil, fp)) { - fprintf(stderr, _("Cannot open font file %s\n"), ifil); + ERR(ctx, _("Cannot open font file %s"), ifil); exit(EX_NOINPUT); } } if (verbose > 1) - printf(_("Reading font file %s\n"), ifil); + INFO(ctx, _("Reading font file %s"), ifil); inbuf = fontbuf = NULL; inputlth = fontbuflth = fontsize = 0; - width = 8; - uclistheads = NULL; - if (readpsffont(kbdfile_get_file(fp), &inbuf, &inputlth, &fontbuf, &fontbuflth, - &width, &fontsize, 0, - no_u ? NULL : &uclistheads) == 0) { + width = 8; + uclistheads = NULL; + if (!readpsffont(ctx, kbdfile_get_file(fp), &inbuf, &inputlth, &fontbuf, + &fontbuflth, &width, &fontsize, 0, + no_u ? NULL : &uclistheads)) { /* we've got a psf font */ bytewidth = (width + 7) / 8; height = fontbuflth / (bytewidth * fontsize); - do_loadfont(fd, fontbuf, width, height, hwunit, + do_loadfont(ctx, fd, fontbuf, width, height, hwunit, fontsize, kbdfile_get_pathname(fp)); if (uclistheads && !no_u) - do_loadtable(fd, uclistheads, fontsize); + do_loadtable(ctx, fd, uclistheads, fontsize); #if 1 if (!uclistheads && !no_u && def) - loadunicodemap(fd, "def.uni"); + loadunicodemap(ctx, fd, "def.uni"); #endif goto exit; } @@ -618,26 +647,28 @@ loadnewfont(int fd, const char *ifil, unsigned int iunit, unsigned int hwunit, i if (inputlth >= chlth && !memcmp(inbuf, combineheader, chlth)) { const char *ifiles[MAXIFILES]; int ifilct = 0; - char *p, *q = (char *)inbuf + chlth, *end = (char *)inbuf + inputlth; + char *p; + char *q = (char *)inbuf + chlth; + char *end = (char *)inbuf + inputlth; + while (q < end) { p = q; while (q < end && *q != '\n') q++; if (q == end) { - fprintf(stderr, - _("No final newline in combine file\n")); + ERR(ctx, _("No final newline in combine file")); exit(EX_DATAERR); } *q++ = 0; if (ifilct == MAXIFILES) { - fprintf(stderr, - _("Too many files to combine\n")); + ERR(ctx, _("Too many files to combine")); exit(EX_DATAERR); } ifiles[ifilct++] = p; } + /* recursive call */ - loadnewfonts(fd, ifiles, ifilct, iunit, hwunit, no_m, no_u); + loadnewfonts(ctx, fd, ifiles, ifilct, iunit, hwunit, no_m, no_u); goto exit; } } @@ -655,8 +686,8 @@ loadnewfont(int fd, const char *ifil, unsigned int iunit, unsigned int hwunit, i In fact, when BROKEN_GRAPHICS_PROGRAMS is defined, and it always is, there is no default font that is saved, so probably the second half is always garbage. */ - fprintf(stderr, _("Hmm - a font from restorefont? " - "Using the first half.\n")); + INFO(ctx, _("Hmm - a font from restorefont? " + "Using the first half.")); inputlth = 16384; /* ignore rest */ fontsize = 512; offset = 0; @@ -672,14 +703,14 @@ loadnewfont(int fd, const char *ifil, unsigned int iunit, unsigned int hwunit, i /* we might check some header details */ offset = rem; } else { - fprintf(stderr, _("Bad input file size\n")); + ERR(ctx, _("Bad input file size")); exit(EX_DATAERR); } fontsize = 256; width = 8; height = inputlth / 256; } - do_loadfont(fd, inbuf + offset, width, height, hwunit, fontsize, + do_loadfont(ctx, fd, inbuf + offset, width, height, hwunit, fontsize, kbdfile_get_pathname(fp)); exit: kbdfile_free(fp); @@ -723,7 +754,8 @@ position_codepage(unsigned int iunit) } static void -do_saveoldfont(int fd, const char *ofil, FILE *fpo, int unimap_follows, +do_saveoldfont(struct kfont_context *ctx, + int fd, const char *ofil, FILE *fpo, int unimap_follows, unsigned int *count, int *utf8) { @@ -734,7 +766,7 @@ do_saveoldfont(int fd, const char *ofil, FILE *fpo, int unimap_follows, unsigned int i, ct, width, height, bytewidth, charsize, kcharsize; ct = sizeof(buf) / (32 * 32 / 8); /* max size 32x32, 8 bits/byte */ - if (getfont(fd, buf, &ct, &width, &height)) + if (getfont(ctx, fd, buf, &ct, &width, &height)) exit(EX_OSERR); /* save as efficiently as possible */ @@ -754,23 +786,26 @@ do_saveoldfont(int fd, const char *ofil, FILE *fpo, int unimap_follows, if (unimap_follows) flags |= WPSFH_HASTAB; - writepsffontheader(fpo, width, height, ct, &psftype, flags); + + writepsffontheader(ctx, fpo, width, height, ct, &psftype, flags); + if (utf8) *utf8 = (psftype == 2); } if (height == 0) { - fprintf(stderr, _("Found nothing to save\n")); + INFO(ctx, _("Found nothing to save")); } else { for (i = 0; i < ct; i++) { if (fwrite(buf + (i * kcharsize), charsize, 1, fpo) != 1) { - fprintf(stderr, _("Cannot write font file")); + ERR(ctx, _("Cannot write font file: %m")); exit(EX_IOERR); } } if (verbose) { - printf(_("Saved %d-char %dx%d font file on %s\n"), - ct, width, height, ofil); + INFO(ctx, + _("Saved %d-char %dx%d font file on %s"), + ct, width, height, ofil); } } @@ -779,32 +814,36 @@ do_saveoldfont(int fd, const char *ofil, FILE *fpo, int unimap_follows, } static void -saveoldfont(int fd, const char *ofil) +saveoldfont(struct kfont_context *ctx, int fd, const char *ofil) { - FILE *fpo; + FILE *fpo = fopen(ofil, "w"); - if ((fpo = fopen(ofil, "w")) == NULL) { - perror(ofil); + if (!fpo) { + ERR(ctx, "Unable to open: %s: %m", ofil); exit(EX_CANTCREAT); } - do_saveoldfont(fd, ofil, fpo, 0, NULL, NULL); + + do_saveoldfont(ctx, fd, ofil, fpo, 0, NULL, NULL); + fclose(fpo); } static void -saveoldfontplusunicodemap(int fd, const char *Ofil) +saveoldfontplusunicodemap(struct kfont_context *ctx, int fd, const char *Ofil) { - FILE *fpo; - unsigned int ct; - int utf8 = 0; + FILE *fpo = fopen(Ofil, "w"); - if ((fpo = fopen(Ofil, "w")) == NULL) { - perror(Ofil); + if (!fpo) { + ERR(ctx, "unable to open: %s: %m", Ofil); exit(EX_CANTCREAT); } - ct = 0; - do_saveoldfont(fd, Ofil, fpo, 1, &ct, &utf8); - appendunicodemap(fd, fpo, ct, utf8); + + int utf8 = 0; + unsigned int ct = 0; + + do_saveoldfont(ctx, fd, Ofil, fpo, 1, &ct, &utf8); + appendunicodemap(ctx, fd, fpo, ct, utf8); + fclose(fpo); } diff --git a/src/showconsolefont.c b/src/showconsolefont.c index 78bfd10b..d3b9d754 100644 --- a/src/showconsolefont.c +++ b/src/showconsolefont.c @@ -8,14 +8,13 @@ #include <string.h> #include <errno.h> #include <unistd.h> +#include <sysexits.h> #include <sys/types.h> #include <sys/ioctl.h> #include <linux/kd.h> #include "libcommon.h" - -#include "kdmapop.h" -#include "kdfontop.h" +#include "kfont.h" /* * Showing the font is nontrivial mostly because testing whether @@ -31,13 +30,13 @@ int have_obuf = 0; int have_ounimap = 0; static void __attribute__((noreturn)) -leave(int n) +leave(struct kfont_context *ctx, int n) { - if (have_obuf && loaduniscrnmap(fd, obuf)) { + if (have_obuf && loaduniscrnmap(ctx, fd, obuf)) { kbd_warning(0, _("failed to restore original translation table\n")); n = EXIT_FAILURE; } - if (have_ounimap && loadunimap(fd, NULL, &ounimap)) { + if (have_ounimap && loadunimap(ctx, fd, NULL, &ounimap)) { kbd_warning(0, _("failed to restore original unimap\n")); n = EXIT_FAILURE; } @@ -45,43 +44,48 @@ leave(int n) } static void -settrivialscreenmap(void) +settrivialscreenmap(struct kfont_context *ctx) { unsigned short i; - if (getuniscrnmap(fd, obuf)) + if (getuniscrnmap(ctx, fd, obuf)) exit(1); have_obuf = 1; for (i = 0; i < E_TABSZ; i++) nbuf[i] = i; - if (loaduniscrnmap(fd, nbuf)) { + if (loaduniscrnmap(ctx, fd, nbuf)) { kbd_error(EXIT_FAILURE, 0, _("cannot change translation table\n")); } } static void -getoldunicodemap(void) +getoldunicodemap(struct kfont_context *ctx) { struct unimapdesc descr; - if (getunimap(fd, &descr)) - leave(EXIT_FAILURE); - ounimap = descr; + if (getunimap(ctx, fd, &descr)) + leave(ctx, EXIT_FAILURE); + ounimap = descr; have_ounimap = 1; } #define BASE 041 /* ' '+1 */ static void -setnewunicodemap(int *list, int cnt) +setnewunicodemap(struct kfont_context *ctx, int *list, int cnt) { unsigned short i; if (!nunimap.entry_ct) { nunimap.entry_ct = 512; - nunimap.entries = (struct unipair *)xmalloc(nunimap.entry_ct * sizeof(struct unipair)); + + nunimap.entries = malloc(nunimap.entry_ct * sizeof(struct unipair)); + if (!nunimap.entries) { + ERR(ctx, "malloc: %m"); + exit(EX_OSERR); + } } for (i = 0; i < 512; i++) { nunimap.entries[i].fontpos = i; @@ -90,8 +94,8 @@ setnewunicodemap(int *list, int cnt) for (i = 0; i < cnt; i++) nunimap.entries[list[i]].unicode = (unsigned short) (BASE + i); - if (loadunimap(fd, NULL, &nunimap)) - leave(EXIT_FAILURE); + if (loadunimap(ctx, fd, NULL, &nunimap)) + leave(ctx, EXIT_FAILURE); } static void __attribute__((noreturn)) @@ -147,12 +151,17 @@ int main(int argc, char **argv) if (optind != argc) usage(); + struct kfont_context ctx = { + .progname = get_progname(), + .log_fn = log_stderr, + }; + if ((fd = getfd(console)) < 0) kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console")); if (ioctl(fd, KDGKBMODE, &mode)) { kbd_warning(errno, "ioctl KDGKBMODE"); - leave(EXIT_FAILURE); + leave(&ctx, EXIT_FAILURE); } if (mode == K_UNICODE) space = "\xef\x80\xa0"; /* U+F020 (direct-to-font space) */ @@ -161,9 +170,10 @@ int main(int argc, char **argv) if (info) { nr = rows = cols = 0; - n = getfont(fd, NULL, &nr, &rows, &cols); + + n = getfont(&ctx, fd, NULL, &nr, &rows, &cols); if (n != 0) - leave(EXIT_FAILURE); + leave(&ctx, EXIT_FAILURE); if (verbose) { printf(_("Character count: %u\n"), nr); @@ -171,13 +181,13 @@ int main(int argc, char **argv) printf(_("Font height : %u\n"), cols); } else printf("%dx%dx%d\n", rows, cols, nr); - leave(EXIT_SUCCESS); + leave(&ctx, EXIT_SUCCESS); } - settrivialscreenmap(); - getoldunicodemap(); + settrivialscreenmap(&ctx); + getoldunicodemap(&ctx); - n = getfontsize(fd); + n = getfontsize(&ctx, fd); if (verbose) printf(_("Showing %d-char font\n\n"), n); cols = ((n > 256) ? 32 : 16); @@ -191,7 +201,7 @@ int main(int argc, char **argv) for (k = i; k < i + nr; k++) for (j = 0; j < cols; j++) list[lth++] = k + j * rows; - setnewunicodemap(list, lth); + setnewunicodemap(&ctx, list, lth); } printf("%1$s%1$s%1$s%1$s", space); for (j = 0; j < cols && i + j * rows < n; j++) { @@ -206,6 +216,6 @@ int main(int argc, char **argv) fflush(stdout); } - leave(EXIT_SUCCESS); + leave(&ctx, EXIT_SUCCESS); return EXIT_SUCCESS; } |