contrib: Add utility to remove all kernel keymaps
[platform/upstream/kbd.git] / contrib / splitfont.c
1 /* splitfont: extract characters from font */
2 /* this is for iso fonts, no psf header, just 256 characters */
3
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8
9 void
10 dosplit (int from, int to, char *fontbuf, int size, char *fontfile) {
11         int itemsize = size/256;
12         int i, fd;
13         char *p, *q, s;
14         char filename[4096];
15
16         if (from < 0 || from > 255 || to < 0 || to > 255) {
17                 fprintf(stderr, "splitfont: bad argument %s,%s\n",
18                         from, to);
19                 exit(1);
20         }
21         if(strlen(fontfile) >= sizeof(filename)-4) {
22                 fprintf(stderr, "splitfont: ridiculously long name\n");
23                 exit(1);
24         }
25         while (from <= to) {
26                 sprintf(filename, "%s.%02x", fontfile, from);
27                 if ((fd = open(filename, O_WRONLY|O_CREAT, 0666)) < 0) {
28                         perror("splitfont");
29                         fprintf(stderr, "cannot open %s for writing\n", filename);
30                 }
31                 p = &fontbuf[from*itemsize];
32                 if (write(fd, p, itemsize) != itemsize) {
33                         perror("splitfont");
34                         fprintf(stderr, "error writing %s\n", filename);
35                 }
36                 close(fd);
37                 from++;
38         }
39 }
40
41 int main(int argc, char **argv) {
42         struct stat statbuf;
43         char fontbuf[4096];
44         int fd;
45         char *p, *q;
46         int from, to;
47
48         if (argc != 3) {
49                 fprintf(stderr, "call: splitfont fontfile 17,23-30,...\n");
50                 exit(1);
51         }
52         if (stat(argv[1], &statbuf)) {
53                 perror("splitfont");
54                 fprintf(stderr, "cannot stat fontfile %s", argv[1]);
55                 exit(1);
56         }
57         if (statbuf.st_size > 4096) {
58                 fprintf(stderr, "splitfont: file unexpectedly large\n");
59                 exit(1);
60         }
61         if (statbuf.st_size % 256) {
62                 fprintf(stderr, "splitfont: file size not a multiple of 256\n");
63                 exit(1);
64         }
65         if ((fd = open(argv[1], O_RDONLY)) < 0) {
66                 perror("splitfont");
67                 fprintf(stderr, "cannot open fontfile %s", argv[1]);
68                 exit(1);
69         }
70         if (read(fd, fontbuf, statbuf.st_size) != statbuf.st_size) {
71                 perror("splitfont");
72                 fprintf(stderr, "error reading fontfile %s", argv[1]);
73                 exit(1);
74         }
75
76         p = argv[2];
77         while(1) {
78                 to = from = strtoul(p,&q,0);
79                 if(*q == '-') {
80                         p = q+1;
81                         to = strtoul(p,&q,0);
82                 }
83                 if(*q && *q != ',') {
84                         fprintf(stderr, "splitfont: garbage in %s\n", p);
85                         exit(1);
86                 }
87                 dosplit(from, to, fontbuf, statbuf.st_size, argv[1]);
88                 if (!*q)
89                         break;
90                 p = q+1;
91         }
92         return 0;
93 }