kmap.c (6742B)
1 /* see LICENSE file for copyright and license details */ 2 #include <dirent.h> 3 #include <limits.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <unistd.h> 8 9 #include "arg.h" 10 #include "kmap.h" 11 #include "paths.h" 12 #include "util.h" 13 14 static void 15 usage(void) 16 { 17 eprintf( 18 "usage: %s [-d dir] [-l [layout]] [-n] [-x [-o options]]\n" 19 " [layout[,layout] [variant[,variant]]]\n", 20 argv0 21 ); 22 } 23 24 static int 25 by_name(const void *a, const void *b) 26 { 27 return strcmp(*(char *const *)a, *(char *const *)b); 28 } 29 30 // the layouts under dir, or one layout's variants, a line each with its name 31 static int 32 list(const char *dir, const char *layout) 33 { 34 char path[PATH_MAX], **names = NULL; 35 size_t n = 0, i; 36 DIR *d; 37 struct dirent *e; 38 39 snprintf(path, sizeof(path), "%s%s%s", dir, layout ? "/" : "", layout ? layout : ""); 40 if (!(d = opendir(path))) 41 eprintf("open %s:", path); 42 while ((e = readdir(d))) { 43 size_t l = strlen(e->d_name); 44 45 if (e->d_name[0] == '.' || !strcmp(e->d_name, "compose.kmap")) 46 continue; 47 if (layout && (l < 6 || strcmp(e->d_name + l - 5, ".kmap"))) 48 continue; 49 names = ereallocarray(names, n + 1, sizeof(*names)); 50 names[n++] = layout ? estrndup(e->d_name, l - 5) : estrdup(e->d_name); 51 } 52 closedir(d); 53 qsort(names, n, sizeof(*names), by_name); 54 for (i = 0; i < n; i++) { 55 struct kmap k; 56 57 memset(&k, 0, sizeof(k)); 58 kmap_path(path, sizeof(path), dir, layout ? layout : names[i], layout ? names[i] : NULL); 59 if (kmap_read(path, &k) == 0) 60 printf("%s\t%s\n", names[i], k.name); 61 kmap_free(&k); 62 free(names[i]); 63 } 64 free(names); 65 return 0; 66 } 67 68 // "layout [variant]" from the file the system keeps its keyboard in 69 static int 70 configured(char *layout, size_t lsz, char *variant, size_t vsz) 71 { 72 char line[256], *w[3]; 73 FILE *fp = fopen(ARUU_PATH_ETC "/kmap", "r"); 74 int n = 0; 75 76 if (!fp) 77 return -1; 78 while (fgets(line, sizeof(line), fp) && !(n = kmap_words(line, w, 3))) 79 ; 80 fclose(fp); 81 if (!n) 82 return -1; 83 snprintf(layout, lsz, "%s", w[0]); 84 snprintf(variant, vsz, "%s", n > 1 ? w[1] : ""); 85 return 0; 86 } 87 88 // the n-th comma separated word of list into buf, "" past its end 89 static void 90 nth(char *buf, size_t sz, const char *list, int n) 91 { 92 const char *e; 93 94 while (n-- > 0 && list && (list = strchr(list, ','))) 95 list++; 96 if (!list) { 97 buf[0] = '\0'; 98 return; 99 } 100 e = strchr(list, ','); 101 snprintf(buf, sz, "%.*s", e ? (int)(e - list) : (int)strlen(list), list); 102 } 103 104 // ?man kmap: set the keyboard layout of the console, or give it to xkb 105 // ?man arguments: [-d dir] [-l [layout]] [-n] [-x [-o options]] [layout[,layout] [variant[,variant]]] 106 // ?man kmap loads a kmap layout (see kmap.h: portable text, every layout 107 // ?man alpine ships) into the console's keyboard: every console takes it, 108 // ?man through the system's own interface (linux's console, wscons, syscons 109 // ?man and vt). 110 // ?man with no layout it loads what /etc/kmap names ("layout variant"). 111 // ?man with -x it writes the layouts as one xkb keymap instead, a group 112 // ?man each, for wayland compositors and x: what wlde's keyboard is made from 113 int 114 main(int argc, char *argv[]) 115 { 116 const char *dir = KMAP_DIR, *options = NULL; 117 char path[PATH_MAX], layouts[256], variants[256]; 118 struct kmap pc, opts[8]; 119 int nopts = 0; 120 int lflag = 0, xflag = 0, nflag = 0, rc = 0, n, i; 121 struct kmap maps[4]; 122 123 ARGBEGIN 124 { 125 // ?man -d:str: -d dir: where the layouts are, /share/kmap when not given 126 case 'd': 127 dir = EARGF(usage()); 128 break; 129 // ?man -l: list the layouts, or with a layout its variants 130 case 'l': 131 lflag = 1; 132 break; 133 // ?man -n: read the layout and say whether it is good, set nothing 134 case 'n': 135 nflag = 1; 136 break; 137 // ?man -o:str: -o options: xkb options for -x, comma separated 138 case 'o': 139 options = EARGF(usage()); 140 break; 141 // ?man -x: write the layouts as an xkb keymap 142 case 'x': 143 xflag = 1; 144 break; 145 default: 146 usage(); 147 } 148 ARGEND 149 150 if (lflag) 151 return argc > 1 ? (usage(), 1) : list(dir, argc ? argv[0] : NULL); 152 if (argc > 2) 153 usage(); 154 if (argc) { 155 snprintf(layouts, sizeof(layouts), "%s", argv[0]); 156 snprintf(variants, sizeof(variants), "%s", argc > 1 ? argv[1] : ""); 157 } else if (configured(layouts, sizeof(layouts), variants, sizeof(variants)) != 0) { 158 // no layout chosen: the console keeps its own, and xkb is given us 159 if (!xflag) 160 return 0; 161 snprintf(layouts, sizeof(layouts), "us"); 162 variants[0] = '\0'; 163 } 164 165 // the console has one layout: the first of them 166 memset(maps, 0, sizeof(maps)); 167 memset(&pc, 0, sizeof(pc)); 168 memset(opts, 0, sizeof(opts)); 169 for (n = 0; n < (xflag ? (int)LEN(maps) : 1); n++) { 170 char layout[64], variant[128]; 171 172 nth(layout, sizeof(layout), layouts, n); 173 if (!layout[0]) 174 break; 175 nth(variant, sizeof(variant), variants, n); 176 kmap_path(path, sizeof(path), dir, layout, variant); 177 if (kmap_read(path, &maps[n]) != 0) { 178 rc = 1; 179 goto done; 180 } 181 } 182 if (!xflag) { 183 snprintf(path, sizeof(path), "%s/compose.kmap", dir); 184 if (access(path, F_OK) == 0 && kmap_read(path, &maps[0]) != 0) { 185 rc = 1; 186 goto done; 187 } 188 } 189 if (xflag) { 190 // the pc keyboard under the layouts, and each option, xkb's name for it 191 // (caps:escape) the file options/caps/escape.kmap 192 snprintf(path, sizeof(path), "%s/pc.kmap", dir); 193 if (kmap_read(path, &pc) != 0) { 194 rc = 1; 195 goto done; 196 } 197 for (const char *o = options; o && *o && nopts < (int)LEN(opts);) { 198 const char *end = strchr(o, ','), *colon; 199 size_t len = end ? (size_t)(end - o) : strlen(o); 200 char one[128]; 201 202 snprintf(one, sizeof(one), "%.*s", (int)(len < sizeof(one) ? len : sizeof(one) - 1), o); 203 o = end ? end + 1 : o + len; 204 if (!one[0]) 205 continue; 206 if (!(colon = strchr(one, ':'))) { 207 weprintf("%s: an option is group:name\n", one); 208 rc = 1; 209 goto done; 210 } 211 snprintf( 212 path, sizeof(path), "%s/options/%.*s/%s.kmap", dir, (int)(colon - one), one, colon + 1 213 ); 214 if (access(path, F_OK) != 0) { 215 weprintf("%s: kmap has no such option\n", one); 216 rc = 1; 217 goto done; 218 } 219 if (kmap_read(path, &opts[nopts++]) != 0) { 220 rc = 1; 221 goto done; 222 } 223 } 224 rc = kmap_write_xkb(stdout, dir, &pc, maps, n, opts, nopts); 225 } else if (!nflag) 226 rc = kmap_load_console(&maps[0]); 227 done: 228 for (i = 0; i < (int)LEN(maps); i++) 229 kmap_free(&maps[i]); 230 for (i = 0; i < nopts; i++) 231 kmap_free(&opts[i]); 232 kmap_free(&pc); 233 return rc != 0; 234 }