devcons.c 6.8 KB

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