devkbmap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. m = strtoul(line, &lp, 0);
  108. key = strtoul(lp, &lp, 0);
  109. while(*lp == ' ' || *lp == '\t')
  110. lp++;
  111. r = 0;
  112. if(*lp == '\'' && lp[1])
  113. chartorune(&r, lp+1);
  114. else if(*lp>='0' && *lp<='9') /* includes 0x... */
  115. r = strtoul(lp, &lp, 0);
  116. else
  117. error(Ebadarg);
  118. kbdputmap(m, key, r);
  119. lp = line;
  120. }
  121. }
  122. if(lp != line){
  123. l = lp-line;
  124. c->aux = lp = smalloc(l+1);
  125. memmove(lp, line, l);
  126. lp[l] = 0;
  127. }
  128. break;
  129. default:
  130. error(Ebadusefd);
  131. }
  132. return n;
  133. }
  134. Dev kbmapdevtab = {
  135. L'κ',
  136. "kbmap",
  137. devreset,
  138. devinit,
  139. devshutdown,
  140. kbmapattach,
  141. kbmapwalk,
  142. kbmapstat,
  143. kbmapopen,
  144. devcreate,
  145. kbmapclose,
  146. kbmapread,
  147. devbread,
  148. kbmapwrite,
  149. devbwrite,
  150. devremove,
  151. devwstat,
  152. };