Skip to content
Newer
Older
100644 131 lines (126 sloc) 2.75 KB
ea19ab8 @ec429 Add copyright messages to source files that lacked them
authored
1 /*
2 quIRC - simple terminal-based IRC client
3 Copyright (C) 2010-13 Edward Cree
4
5 See quirc.c for license information
6 genkeymap: generate keymapping code from data
7 */
8
62617ff Configurable keymapping
ec429 authored
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
ebc74a9 Refactored string buffers to new strbuf.h, shared with the gen* progs.
ec429 authored
13 #include "strbuf.h"
62617ff Configurable keymapping
ec429 authored
14
15 typedef struct
16 {
17 char *name;
18 char *mod;
19 }
20 keymod;
21
22 int main(int argc, char **argv)
23 {
24 if(argc!=2)
25 {
26 fprintf(stderr, "Usage: genkeymap {c|h}\n");
27 return(EXIT_FAILURE);
28 }
29 int mode=0;
30 if(strcmp(argv[1], "c")==0)
31 mode=1;
32 else if(strcmp(argv[1], "h")==0)
33 mode=2;
34 if(!mode)
35 {
36 fprintf(stderr, "Usage: genkeymap {c|h}\n");
37 return(EXIT_FAILURE);
38 }
39 int nkeys=0;
40 keymod *keys=NULL;
41 while(!feof(stdin))
42 {
43 char *line=fgetl(stdin);
44 if(line)
45 {
46 if((*line)&&(*line!='#'))
47 {
48 keymod new;
49 new.name=strdup(strtok(line, " \t"));
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
50 char *mod=strtok(NULL, "\t");
51 if(*mod==':')
62617ff Configurable keymapping
ec429 authored
52 {
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
53 new.mod=strdup(mod);
62617ff Configurable keymapping
ec429 authored
54 }
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
55 else
62617ff Configurable keymapping
ec429 authored
56 {
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
57 off_t o=strlen(mod);
58 if(o&1)
62617ff Configurable keymapping
ec429 authored
59 {
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
60 fprintf(stderr, "genkeymap: bad line (o&1) %s\t%s\n", new.name, mod);
62617ff Configurable keymapping
ec429 authored
61 return(EXIT_FAILURE);
62 }
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
63 new.mod=malloc((o>>1)+1);
64 for(int i=0;i<o;i+=2)
62617ff Configurable keymapping
ec429 authored
65 {
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
66 if(!(isxdigit(mod[i])&&isxdigit(mod[i+1])))
67 {
68 fprintf(stderr, "genkeymap: bad line (not hex) %s\t%s\n", new.name, mod);
69 return(EXIT_FAILURE);
70 }
71 char buf[3];buf[0]=mod[i];buf[1]=mod[i+1];buf[2]=0;
72 unsigned int c;
73 if(sscanf(buf, "%x", &c)!=1)
74 {
75 fprintf(stderr, "genkeymap: bad line (sscanf) %s\t%s\n", new.name, mod);
76 return(EXIT_FAILURE);
77 }
78 new.mod[i>>1]=c;
62617ff Configurable keymapping
ec429 authored
79 }
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
80 new.mod[o>>1]=0;
62617ff Configurable keymapping
ec429 authored
81 }
82 int n=nkeys++;
83 (keys=realloc(keys, nkeys*sizeof(keymod)))[n]=new;
84 }
85 free(line);
86 }
87 else
88 break;
89 }
90 switch(mode)
91 {
92 case 1:
93 printf("#include <stdbool.h>\n\n");
94 printf("int initkeys(void)\n{\n");
95 printf("\tnkeys=%d;\n", nkeys);
96 printf("\tkmap=malloc(nkeys*sizeof(keymod));\n");
97 printf("\tif(!kmap) return(1);\n");
98 for(int i=0;i<nkeys;i++)
99 {
100 printf("\tkmap[%d].name=\"%s\";\n", i, keys[i].name);
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
101 if(keys[i].mod[0]==':')
b492f6b @ec429 genkeymap: fix allocation: strdup the "" when termcap missing
authored
102 printf("\tkmap[%d].mod=strdup(%s?%s:\"\");\n", i, keys[i].mod+1, keys[i].mod+1);
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
103 else
62617ff Configurable keymapping
ec429 authored
104 {
53d0fcf Fix keymapping to use terminfo values where available
ec429 authored
105 printf("\tkmap[%d].mod=malloc(%zu);\n", i, strlen(keys[i].mod)+1);
106 for(unsigned int j=0;j<=strlen(keys[i].mod);j++)
107 {
108 printf("\tkmap[%d].mod[%d]=%hhd;\n", i, j, keys[i].mod[j]);
109 }
62617ff Configurable keymapping
ec429 authored
110 }
111 }
112 printf("\treturn(0);\n");
113 printf("}\n");
114 break;
115 case 2:
116 printf("#pragma once\n\ntypedef struct\n{\n\tconst char *name;\n\tchar *mod;\n}\nkeymod;\n\n");
117 for(int i=0;i<nkeys;i++)
118 {
119 printf("#define KEY_%s\t%d\n", keys[i].name, i);
120 }
121 printf("#define KEY_F(n)\t((int[12]){");
0c2b006 Fixed bugs with F-key tabbing
ec429 authored
122 for(int i=1;i<=12;i++)
62617ff Configurable keymapping
ec429 authored
123 {
124 printf("KEY_F%d,", i);
125 }
0c2b006 Fixed bugs with F-key tabbing
ec429 authored
126 printf("}[n-1])\n");
62617ff Configurable keymapping
ec429 authored
127 break;
128 }
129 return(0);
130 }
Something went wrong with that request. Please try again.