devcons.c 6.6 KB

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