Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/console-tools/loadfont.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/console-tools/loadfont.c
r821 r1770 8 8 * (Adapted for busybox by Matej Vela.) 9 9 */ 10 #include <stdio.h> 11 #include <string.h> 12 #include <fcntl.h> 13 #include <stdlib.h> 14 #include <unistd.h> 15 #include <sys/types.h> 16 #include <dirent.h> 17 #include <errno.h> 18 #include <sys/ioctl.h> 10 #include "libbb.h" 19 11 #include <sys/kd.h> 20 #include <endian.h>21 #include "busybox.h"22 12 23 enum {13 enum { 24 14 PSF_MAGIC1 = 0x36, 25 15 PSF_MAGIC2 = 0x04, … … 32 22 33 23 struct psf_header { 34 unsigned char magic1, magic2; 35 unsigned char mode; 36 unsigned char charsize; 24 unsigned char magic1, magic2; /* Magic number */ 25 unsigned char mode; /* PSF font mode */ 26 unsigned char charsize; /* Character size */ 37 27 }; 38 28 39 29 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2) 40 30 41 static void loadnewfont(int fd);42 43 int loadfont_main(int argc, char **argv)44 {45 int fd;46 47 if (argc != 1)48 bb_show_usage();49 50 fd = bb_xopen(CURRENT_VC, O_RDWR);51 loadnewfont(fd);52 53 return EXIT_SUCCESS;54 }55 56 31 static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize) 57 32 { 58 char buf[16384];33 char *buf; 59 34 int i; 60 35 61 memset(buf, 0, sizeof(buf)); 36 if (unit < 1 || unit > 32) 37 bb_error_msg_and_die("bad character size %d", unit); 62 38 63 if (unit < 1 || unit > 32) 64 bb_error_msg_and_die("Bad character size %d", unit); 65 39 buf = xzalloc(16 * 1024); 40 /*memset(buf, 0, 16 * 1024);*/ 66 41 for (i = 0; i < fontsize; i++) 67 42 memcpy(buf + (32 * i), inbuf + (unit * i), unit); 68 43 69 #if defined( PIO_FONTX ) && !defined( __sparc__)44 #if defined(PIO_FONTX) && !defined(__sparc__) 70 45 { 71 46 struct consolefontdesc cfd; … … 75 50 cfd.chardata = buf; 76 51 77 if (ioctl(fd, PIO_FONTX, &cfd) == 0) 78 return; /* success */ 79 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)"); 52 if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)")) 53 goto ret; /* success */ 80 54 } 81 55 #endif 82 if (ioctl(fd, PIO_FONT, buf)) 83 bb_perror_msg_and_die("PIO_FONT ioctl error"); 56 xioctl(fd, PIO_FONT, buf); 57 ret: 58 free(buf); 84 59 } 85 60 … … 92 67 int ct = 0, maxct; 93 68 int glyph; 94 u _short unicode;69 uint16_t unicode; 95 70 96 71 maxct = tailsz; /* more than enough */ 97 up = (struct unipair *)xmalloc(maxct * sizeof(struct unipair));72 up = xmalloc(maxct * sizeof(struct unipair)); 98 73 99 74 for (glyph = 0; glyph < fontsize; glyph++) { 100 75 while (tailsz >= 2) { 101 unicode = (((u _short) inbuf[1]) << 8) + inbuf[0];76 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0]; 102 77 tailsz -= 2; 103 78 inbuf += 2; … … 116 91 advice.advised_hashstep = 0; 117 92 advice.advised_hashlevel = 0; 118 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) { 119 #ifdef ENOIOCTLCMD 120 if (errno == ENOIOCTLCMD) { 121 bb_error_msg("It seems this kernel is older than 1.1.92"); 122 bb_error_msg_and_die("No Unicode mapping table loaded."); 123 } else 124 #endif 125 bb_perror_msg_and_die("PIO_UNIMAPCLR"); 126 } 93 xioctl(fd, PIO_UNIMAPCLR, &advice); 127 94 ud.entry_ct = ct; 128 95 ud.entries = up; 129 if (ioctl(fd, PIO_UNIMAP, &ud)) { 130 #if 0 131 if (errno == ENOMEM) { 132 /* change advice parameters */ 133 } 134 #endif 135 bb_perror_msg_and_die("PIO_UNIMAP"); 136 } 96 xioctl(fd, PIO_UNIMAP, &ud); 137 97 } 138 98 139 99 static void loadnewfont(int fd) 140 100 { 101 enum { INBUF_SIZE = 32*1024 + 1 }; 102 141 103 int unit; 142 unsigned char inbuf[32768]; /* primitive */ 143 unsigned int inputlth, offset; 104 unsigned inputlth, offset; 105 /* Was on stack, but 32k is a bit too much: */ 106 unsigned char *inbuf = xmalloc(INBUF_SIZE); 144 107 145 108 /* … … 148 111 * just read the entire file. 149 112 */ 150 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin); 151 if (ferror(stdin)) 152 bb_perror_msg_and_die("Error reading input font"); 153 /* use malloc/realloc in case of giant files; 154 maybe these do not occur: 16kB for the font, 155 and 16kB for the map leaves 32 unicode values 156 for each font position */ 157 if (!feof(stdin)) 158 bb_perror_msg_and_die("Font too large"); 113 inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE); 114 if (inputlth < 0) 115 bb_perror_msg_and_die("error reading input font"); 116 if (inputlth >= INBUF_SIZE) 117 bb_error_msg_and_die("font too large"); 159 118 160 119 /* test for psf first */ … … 163 122 int fontsize; 164 123 int hastable; 165 unsigned inthead0, head;124 unsigned head0, head; 166 125 167 126 if (inputlth < sizeof(struct psf_header)) … … 174 133 175 134 if (psfhdr.mode > PSF_MAXMODE) 176 bb_error_msg_and_die(" Unsupported psf file mode");135 bb_error_msg_and_die("unsupported psf file mode"); 177 136 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256); 178 #if !defined( PIO_FONTX ) || defined( __sparc__)137 #if !defined(PIO_FONTX) || defined(__sparc__) 179 138 if (fontsize != 256) 180 bb_error_msg_and_die(" Only fontsize 256 supported");139 bb_error_msg_and_die("only fontsize 256 supported"); 181 140 #endif 182 141 hastable = (psfhdr.mode & PSF_MODEHASTAB); … … 186 145 head = head0 + fontsize * unit; 187 146 if (head > inputlth || (!hastable && head != inputlth)) 188 bb_error_msg_and_die(" Input file: bad length");147 bb_error_msg_and_die("input file: bad length"); 189 148 do_loadfont(fd, inbuf + head0, unit, fontsize); 190 149 if (hastable) … … 192 151 return; 193 152 } 194 no_psf:195 153 154 no_psf: 196 155 /* file with three code pages? */ 197 156 if (inputlth == 9780) { … … 201 160 /* bare font */ 202 161 if (inputlth & 0377) 203 bb_error_msg_and_die(" Bad input file size");162 bb_error_msg_and_die("bad input file size"); 204 163 offset = 0; 205 164 unit = inputlth / 256; … … 207 166 do_loadfont(fd, inbuf + offset, unit, 256); 208 167 } 168 169 int loadfont_main(int argc, char **argv); 170 int loadfont_main(int argc, char **argv) 171 { 172 int fd; 173 174 if (argc != 1) 175 bb_show_usage(); 176 177 fd = xopen(CURRENT_VC, O_RDWR); 178 loadnewfont(fd); 179 180 return EXIT_SUCCESS; 181 }
Note:
See TracChangeset
for help on using the changeset viewer.