devcons.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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&3) == OWRITE || (c->mode&3) == 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, ch;
  261. int i, eol;
  262. int send;
  263. if(n <= 0)
  264. return n;
  265. switch((ulong)c->qid.path){
  266. case Qsnarf:
  267. qlock(&snarf);
  268. if(off < snarf.n){
  269. if(off + n > snarf.n)
  270. n = snarf.n - off;
  271. memmove(buf, snarf.buf+off, n);
  272. }else
  273. n = 0;
  274. qunlock(&snarf);
  275. return n;
  276. case Qdir:
  277. return devdirread(c, buf, n, consdir, nelem(consdir), devgen);
  278. case Qcons:
  279. qlock(&kbd);
  280. if(waserror()){
  281. qunlock(&kbd);
  282. nexterror();
  283. }
  284. while(!qcanread(lineq)){
  285. qread(kbdq, &ch, 1);
  286. send = 0;
  287. if(ch == 0){
  288. /* flush output on rawoff -> rawon */
  289. if(kbd.x > 0)
  290. send = !qcanread(kbdq);
  291. }else if(kbd.raw){
  292. kbd.line[kbd.x++] = ch;
  293. send = !qcanread(kbdq);
  294. }else{
  295. switch(ch){
  296. case '\b':
  297. if(kbd.x > 0)
  298. kbd.x--;
  299. break;
  300. case 0x15: /* ^U */
  301. kbd.x = 0;
  302. break;
  303. case '\n':
  304. case 0x04: /* ^D */
  305. send = 1;
  306. default:
  307. if(ch != 0x04)
  308. kbd.line[kbd.x++] = ch;
  309. break;
  310. }
  311. }
  312. if(send || kbd.x == sizeof kbd.line){
  313. qwrite(lineq, kbd.line, kbd.x);
  314. kbd.x = 0;
  315. }
  316. }
  317. n = qread(lineq, buf, n);
  318. qunlock(&kbd);
  319. poperror();
  320. return n;
  321. default:
  322. print("consread 0x%llux\n", c->qid.path);
  323. error(Egreg);
  324. }
  325. return -1; /* never reached */
  326. }
  327. static long
  328. conswrite(Chan *c, void *va, long n, vlong)
  329. {
  330. Snarf *t;
  331. char buf[256], *a;
  332. char ch;
  333. switch((ulong)c->qid.path){
  334. case Qcons:
  335. screenputs(va, n);
  336. break;
  337. case Qconsctl:
  338. if(n >= sizeof(buf))
  339. n = sizeof(buf)-1;
  340. strncpy(buf, va, n);
  341. buf[n] = 0;
  342. for(a = buf; a;){
  343. if(strncmp(a, "rawon", 5) == 0){
  344. kbd.raw = 1;
  345. /* clumsy hack - wake up reader */
  346. ch = 0;
  347. qwrite(kbdq, &ch, 1);
  348. } else if(strncmp(a, "rawoff", 6) == 0){
  349. kbd.raw = 0;
  350. }
  351. if(a = strchr(a, ' '))
  352. a++;
  353. }
  354. break;
  355. case Qsnarf:
  356. t = c->aux;
  357. /* always append only */
  358. if(t->n > MAXSNARF) /* avoid thrashing when people cut huge text */
  359. error("snarf buffer too big");
  360. a = realloc(t->buf, t->n + n + 1);
  361. if(a == nil)
  362. error("snarf buffer too big");
  363. t->buf = a;
  364. memmove(t->buf+t->n, va, n);
  365. t->n += n;
  366. t->buf[t->n] = '\0';
  367. break;
  368. default:
  369. print("conswrite: 0x%llux\n", c->qid.path);
  370. error(Egreg);
  371. }
  372. return n;
  373. }
  374. Dev consdevtab = {
  375. 'c',
  376. "cons",
  377. devreset,
  378. consinit,
  379. consattach,
  380. conswalk,
  381. consstat,
  382. consopen,
  383. devcreate,
  384. consclose,
  385. consread,
  386. devbread,
  387. conswrite,
  388. devbwrite,
  389. devremove,
  390. devwstat,
  391. };