devcons.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include "compat.h"
  12. #include "kbd.h"
  13. #include "error.h"
  14. typedef struct Queue Queue;
  15. struct Queue
  16. {
  17. QLock qwait;
  18. Rendez rwait;
  19. Lock lock;
  20. int notempty;
  21. char buf[1024];
  22. char *w;
  23. char *r;
  24. char *e;
  25. };
  26. Queue* kbdq; /* unprocessed console input */
  27. Queue* lineq; /* processed console input */
  28. Snarf snarf = {
  29. .vers = 1
  30. };
  31. static struct
  32. {
  33. QLock qlock;
  34. int raw; /* true if we shouldn't process input */
  35. int ctl; /* number of opens to the control file */
  36. int x; /* index into line */
  37. char line[1024]; /* current input line */
  38. } kbd;
  39. /*
  40. * cheapo fixed-length queues
  41. */
  42. static void
  43. qwrite(Queue *q, void *v, int n)
  44. {
  45. char *buf, *next;
  46. int i;
  47. buf = v;
  48. lock(&q->lock);
  49. for(i = 0; i < n; i++){
  50. next = q->w+1;
  51. if(next >= q->e)
  52. next = q->buf;
  53. if(next == q->r)
  54. break;
  55. *q->w = buf[i];
  56. q->w = next;
  57. }
  58. q->notempty = 1;
  59. unlock(&q->lock);
  60. rendwakeup(&q->rwait);
  61. }
  62. static int
  63. qcanread(void *vq)
  64. {
  65. Queue *q;
  66. int ne;
  67. q = vq;
  68. lock(&q->lock);
  69. ne = q->notempty;
  70. unlock(&q->lock);
  71. return ne;
  72. }
  73. static int
  74. qread(Queue *q, void *v, int n)
  75. {
  76. char *a;
  77. int nn, notempty;
  78. if(n == 0)
  79. return 0;
  80. a = v;
  81. nn = 0;
  82. for(;;){
  83. lock(&q->lock);
  84. while(nn < n && q->r != q->w){
  85. a[nn++] = *q->r++;
  86. if(q->r >= q->e)
  87. q->r = q->buf;
  88. }
  89. notempty = q->notempty;
  90. q->notempty = q->r != q->w;
  91. unlock(&q->lock);
  92. if(notempty)
  93. break;
  94. /*
  95. * wait for something to show up in the kbd buffer.
  96. */
  97. qlock(&q->qwait);
  98. if(waserror()){
  99. qunlock(&q->qwait);
  100. nexterror();
  101. }
  102. rendsleep(&q->rwait, qcanread, q);
  103. qunlock(&q->qwait);
  104. poperror();
  105. }
  106. return nn;
  107. }
  108. static Queue *
  109. mkqueue(void)
  110. {
  111. Queue *q;
  112. q = smalloc(sizeof(Queue));
  113. q->r = q->buf;
  114. q->w = q->r;
  115. q->e = &q->buf[sizeof q->buf];
  116. q->notempty = 0;
  117. return q;
  118. }
  119. static void
  120. echoscreen(char *buf, int n)
  121. {
  122. char *e, *p;
  123. char ebuf[128];
  124. int x;
  125. p = ebuf;
  126. e = ebuf + sizeof(ebuf) - 4;
  127. while(n-- > 0){
  128. if(p >= e){
  129. screenputs(ebuf, p - ebuf);
  130. p = ebuf;
  131. }
  132. x = *buf++;
  133. if(x == 0x15){
  134. *p++ = '^';
  135. *p++ = 'U';
  136. *p++ = '\n';
  137. } else
  138. *p++ = x;
  139. }
  140. if(p != ebuf)
  141. screenputs(ebuf, p - ebuf);
  142. }
  143. /*
  144. * Put character, possibly a rune, into read queue at interrupt time.
  145. * Called at interrupt time to process a character.
  146. */
  147. void
  148. kbdputc(int ch)
  149. {
  150. int n;
  151. char buf[UTFmax];
  152. Rune r;
  153. r = ch;
  154. n = runetochar(buf, &r);
  155. qwrite(kbdq, buf, n);
  156. if(!kbd.raw)
  157. echoscreen(buf, n);
  158. }
  159. static void
  160. kbdputcinit(void)
  161. {
  162. kbdq = mkqueue();
  163. lineq = mkqueue();
  164. kbd.raw = 0;
  165. kbd.ctl = 0;
  166. kbd.x = 0;
  167. }
  168. enum{
  169. Qdir,
  170. Qcons,
  171. Qconsctl,
  172. Qsnarf,
  173. Qwinname,
  174. };
  175. static Dirtab consdir[]={
  176. {".", {Qdir, 0, QTDIR}, 0, DMDIR|0555},
  177. {"cons", {Qcons}, 0, 0660},
  178. {"consctl", {Qconsctl}, 0, 0220},
  179. {"snarf", {Qsnarf}, 0, 0600},
  180. {"winname", {Qwinname}, 0, 0000},
  181. };
  182. static void
  183. consinit(void)
  184. {
  185. kbdputcinit();
  186. }
  187. static Chan*
  188. consattach(char *spec)
  189. {
  190. return devattach('c', spec);
  191. }
  192. static Walkqid*
  193. conswalk(Chan *c, Chan *nc, char **name, int nname)
  194. {
  195. return devwalk(c, nc, name,nname, consdir, nelem(consdir), devgen);
  196. }
  197. static int
  198. consstat(Chan *c, uint8_t *dp, int n)
  199. {
  200. return devstat(c, dp, n, consdir, nelem(consdir), devgen);
  201. }
  202. static Chan*
  203. consopen(Chan *c, int omode)
  204. {
  205. c->aux = nil;
  206. c = devopen(c, omode, consdir, nelem(consdir), devgen);
  207. switch((uint32_t)c->qid.path){
  208. case Qconsctl:
  209. qlock(&kbd.qlock);
  210. kbd.ctl++;
  211. qunlock(&kbd.qlock);
  212. break;
  213. case Qsnarf:
  214. if((c->mode&3) == OWRITE || (c->mode&3) == ORDWR)
  215. c->aux = smalloc(sizeof(Snarf));
  216. break;
  217. }
  218. return c;
  219. }
  220. void
  221. setsnarf(char *buf, int n, int *vers)
  222. {
  223. int i;
  224. qlock(&snarf.qlock);
  225. snarf.vers++;
  226. if(vers)
  227. *vers = snarf.vers;
  228. for(i = 0; i < nelem(consdir); i++){
  229. if(consdir[i].qid.type == Qsnarf){
  230. consdir[i].qid.vers = snarf.vers;
  231. break;
  232. }
  233. }
  234. free(snarf.buf);
  235. snarf.n = n;
  236. snarf.buf = buf;
  237. qunlock(&snarf.qlock);
  238. }
  239. static void
  240. consclose(Chan *c)
  241. {
  242. Snarf *t;
  243. switch((uint32_t)c->qid.path){
  244. /* last close of control file turns off raw */
  245. case Qconsctl:
  246. if(c->flag&COPEN){
  247. qlock(&kbd.qlock);
  248. if(--kbd.ctl == 0)
  249. kbd.raw = 0;
  250. qunlock(&kbd.qlock);
  251. }
  252. break;
  253. /* odd behavior but really ok: replace snarf buffer when /dev/snarf is closed */
  254. case Qsnarf:
  255. t = c->aux;
  256. if(t == nil)
  257. break;
  258. setsnarf(t->buf, t->n, 0);
  259. t->buf = nil; /* setsnarf took it */
  260. free(t);
  261. c->aux = nil;
  262. break;
  263. }
  264. }
  265. static int32_t
  266. consread(Chan *c, void *buf, int32_t n, int64_t off)
  267. {
  268. char ch;
  269. int send;
  270. if(n <= 0)
  271. return n;
  272. switch((uint32_t)c->qid.path){
  273. case Qsnarf:
  274. qlock(&snarf.qlock);
  275. if(off < snarf.n){
  276. if(off + n > snarf.n)
  277. n = snarf.n - off;
  278. memmove(buf, snarf.buf+off, n);
  279. }else
  280. n = 0;
  281. qunlock(&snarf.qlock);
  282. return n;
  283. case Qdir:
  284. return devdirread(c, buf, n, consdir, nelem(consdir), devgen);
  285. case Qcons:
  286. qlock(&kbd.qlock);
  287. if(waserror()){
  288. qunlock(&kbd.qlock);
  289. nexterror();
  290. }
  291. while(!qcanread(lineq)){
  292. qread(kbdq, &ch, 1);
  293. send = 0;
  294. if(ch == 0){
  295. /* flush output on rawoff -> rawon */
  296. if(kbd.x > 0)
  297. send = !qcanread(kbdq);
  298. }else if(kbd.raw){
  299. kbd.line[kbd.x++] = ch;
  300. send = !qcanread(kbdq);
  301. }else{
  302. switch(ch){
  303. case '\b':
  304. if(kbd.x > 0)
  305. kbd.x--;
  306. break;
  307. case 0x15: /* ^U */
  308. kbd.x = 0;
  309. break;
  310. case '\n':
  311. case 0x04: /* ^D */
  312. send = 1;
  313. default:
  314. if(ch != 0x04)
  315. kbd.line[kbd.x++] = ch;
  316. break;
  317. }
  318. }
  319. if(send || kbd.x == sizeof kbd.line){
  320. qwrite(lineq, kbd.line, kbd.x);
  321. kbd.x = 0;
  322. }
  323. }
  324. n = qread(lineq, buf, n);
  325. qunlock(&kbd.qlock);
  326. poperror();
  327. return n;
  328. default:
  329. print("consread 0x%llux\n", c->qid.path);
  330. error(Egreg);
  331. }
  332. return -1; /* never reached */
  333. }
  334. static int32_t
  335. conswrite(Chan *c, void *va, int32_t n, int64_t u)
  336. {
  337. Snarf *t;
  338. char buf[256], *a;
  339. char ch;
  340. switch((uint32_t)c->qid.path){
  341. case Qcons:
  342. screenputs(va, n);
  343. break;
  344. case Qconsctl:
  345. if(n >= sizeof(buf))
  346. n = sizeof(buf)-1;
  347. strncpy(buf, va, n);
  348. buf[n] = 0;
  349. for(a = buf; a;){
  350. if(strncmp(a, "rawon", 5) == 0){
  351. kbd.raw = 1;
  352. /* clumsy hack - wake up reader */
  353. ch = 0;
  354. qwrite(kbdq, &ch, 1);
  355. } else if(strncmp(a, "rawoff", 6) == 0){
  356. kbd.raw = 0;
  357. }
  358. if((a = strchr(a, ' ')))
  359. a++;
  360. }
  361. break;
  362. case Qsnarf:
  363. t = c->aux;
  364. /* always append only */
  365. if(t->n > MAXSNARF) /* avoid thrashing when people cut huge text */
  366. error("snarf buffer too big");
  367. a = realloc(t->buf, t->n + n + 1);
  368. if(a == nil)
  369. error("snarf buffer too big");
  370. t->buf = a;
  371. memmove(t->buf+t->n, va, n);
  372. t->n += n;
  373. t->buf[t->n] = '\0';
  374. break;
  375. default:
  376. print("conswrite: 0x%llux\n", c->qid.path);
  377. error(Egreg);
  378. }
  379. return n;
  380. }
  381. Dev consdevtab = {
  382. 'c',
  383. "cons",
  384. devreset,
  385. consinit,
  386. consattach,
  387. conswalk,
  388. consstat,
  389. consopen,
  390. devcreate,
  391. consclose,
  392. consread,
  393. devbread,
  394. conswrite,
  395. devbwrite,
  396. devremove,
  397. devwstat,
  398. };