devkbmap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * keyboard map
  3. */
  4. #include "u.h"
  5. #include "../port/lib.h"
  6. #include "mem.h"
  7. #include "dat.h"
  8. #include "fns.h"
  9. #include "../port/error.h"
  10. enum{
  11. Qdir,
  12. Qdata,
  13. };
  14. Dirtab kbmaptab[]={
  15. ".", {Qdir, 0, QTDIR}, 0, 0555,
  16. "kbmap", {Qdata, 0}, 0, 0600,
  17. };
  18. #define NKBFILE sizeof(kbmaptab)/sizeof(kbmaptab[0])
  19. #define KBLINELEN (3*NUMSIZE+1) /* t code val\n */
  20. static Chan *
  21. kbmapattach(char *spec)
  22. {
  23. return devattach(L'κ', spec);
  24. }
  25. static Walkqid*
  26. kbmapwalk(Chan *c, Chan *nc, char **name, int nname)
  27. {
  28. return devwalk(c, nc, name, nname, kbmaptab, NKBFILE, devgen);
  29. }
  30. static int
  31. kbmapstat(Chan *c, uchar *dp, int n)
  32. {
  33. return devstat(c, dp, n, kbmaptab, NKBFILE, devgen);
  34. }
  35. static Chan*
  36. kbmapopen(Chan *c, int omode)
  37. {
  38. if(!iseve())
  39. error(Eperm);
  40. return devopen(c, omode, kbmaptab, NKBFILE, devgen);
  41. }
  42. static void
  43. kbmapclose(Chan *c)
  44. {
  45. if(c->aux){
  46. free(c->aux);
  47. c->aux = nil;
  48. }
  49. }
  50. static long
  51. kbmapread(Chan *c, void *a, long n, vlong offset)
  52. {
  53. char *bp;
  54. char tmp[KBLINELEN+1];
  55. int t, sc;
  56. Rune r;
  57. if(c->qid.type == QTDIR)
  58. return devdirread(c, a, n, kbmaptab, NKBFILE, devgen);
  59. switch((int)(c->qid.path)){
  60. case Qdata:
  61. if(kbdgetmap(offset/KBLINELEN, &t, &sc, &r)) {
  62. bp = tmp;
  63. bp += readnum(0, bp, NUMSIZE, t, NUMSIZE);
  64. bp += readnum(0, bp, NUMSIZE, sc, NUMSIZE);
  65. bp += readnum(0, bp, NUMSIZE, r, NUMSIZE);
  66. *bp++ = '\n';
  67. *bp = 0;
  68. n = readstr(offset%KBLINELEN, a, n, tmp);
  69. } else
  70. n = 0;
  71. break;
  72. default:
  73. n=0;
  74. break;
  75. }
  76. return n;
  77. }
  78. static long
  79. kbmapwrite(Chan *c, void *a, long n, vlong)
  80. {
  81. char line[100], *lp, *b;
  82. int key, m, l;
  83. Rune r;
  84. if(c->qid.type == QTDIR)
  85. error(Eperm);
  86. switch((int)(c->qid.path)){
  87. case Qdata:
  88. b = a;
  89. l = n;
  90. lp = line;
  91. if(c->aux){
  92. strcpy(line, c->aux);
  93. lp = line+strlen(line);
  94. free(c->aux);
  95. c->aux = nil;
  96. }
  97. while(--l >= 0) {
  98. *lp++ = *b++;
  99. if(lp[-1] == '\n' || lp == &line[sizeof(line)-1]) {
  100. *lp = 0;
  101. if(*line == 0)
  102. error(Ebadarg);
  103. if(*line == '\n' || *line == '#'){
  104. lp = line;
  105. continue;
  106. }
  107. lp = line;
  108. while(*lp == ' ' || *lp == '\t')
  109. lp++;
  110. m = strtoul(line, &lp, 0);
  111. key = strtoul(lp, &lp, 0);
  112. while(*lp == ' ' || *lp == '\t')
  113. lp++;
  114. r = 0;
  115. if(*lp == '\'' && lp[1])
  116. chartorune(&r, lp+1);
  117. else if(*lp == '^' && lp[1]){
  118. chartorune(&r, lp+1);
  119. if(0x40 <= r && r < 0x60)
  120. r -= 0x40;
  121. else
  122. error(Ebadarg);
  123. }else if(*lp == 'M' && ('1' <= lp[1] && lp[1] <= '5'))
  124. r = 0xF900+lp[1]-'0';
  125. else if(*lp>='0' && *lp<='9') /* includes 0x... */
  126. r = strtoul(lp, &lp, 0);
  127. else
  128. error(Ebadarg);
  129. kbdputmap(m, key, r);
  130. lp = line;
  131. }
  132. }
  133. if(lp != line){
  134. l = lp-line;
  135. c->aux = lp = smalloc(l+1);
  136. memmove(lp, line, l);
  137. lp[l] = 0;
  138. }
  139. break;
  140. default:
  141. error(Ebadusefd);
  142. }
  143. return n;
  144. }
  145. Dev kbmapdevtab = {
  146. L'κ',
  147. "kbmap",
  148. devreset,
  149. devinit,
  150. devshutdown,
  151. kbmapattach,
  152. kbmapwalk,
  153. kbmapstat,
  154. kbmapopen,
  155. devcreate,
  156. kbmapclose,
  157. kbmapread,
  158. devbread,
  159. kbmapwrite,
  160. devbwrite,
  161. devremove,
  162. devwstat,
  163. };