main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <thread.h>
  5. #include <fcall.h>
  6. #include <9p.h>
  7. #include <ctype.h>
  8. #include "dat.h"
  9. void mainctl(void*);
  10. void startcmd(char *[], int*);
  11. void stdout2body(void*);
  12. int debug;
  13. int notepg;
  14. int eraseinput;
  15. int dirty = 0;
  16. Window *win; /* the main window */
  17. void
  18. usage(void)
  19. {
  20. fprint(2, "usage: win [command]\n");
  21. threadexitsall("usage");
  22. }
  23. void
  24. threadmain(int argc, char *argv[])
  25. {
  26. int i, j;
  27. char *dir, *tag, *name;
  28. char buf[1024], **av;
  29. quotefmtinstall();
  30. rfork(RFNAMEG);
  31. ARGBEGIN{
  32. case 'd':
  33. debug = 1;
  34. chatty9p++;
  35. break;
  36. case 'e':
  37. eraseinput = 1;
  38. break;
  39. case 'D':
  40. {extern int _threaddebuglevel;
  41. _threaddebuglevel = 1<<20;
  42. }
  43. }ARGEND
  44. if(argc == 0){
  45. av = emalloc(3*sizeof(char*));
  46. av[0] = "rc";
  47. av[1] = "-i";
  48. name = getenv("sysname");
  49. }else{
  50. av = argv;
  51. name = utfrrune(av[0], '/');
  52. if(name)
  53. name++;
  54. else
  55. name = av[0];
  56. }
  57. if(getwd(buf, sizeof buf) == 0)
  58. dir = "/";
  59. else
  60. dir = buf;
  61. dir = estrdup(dir);
  62. tag = estrdup(dir);
  63. tag = eappend(estrdup(tag), "/-", name);
  64. win = newwindow();
  65. snprint(buf, sizeof buf, "%d", win->id);
  66. putenv("winid", buf);
  67. winname(win, tag);
  68. wintagwrite(win, "Send Noscroll", 5+8);
  69. threadcreate(mainctl, win, STACK);
  70. mountcons();
  71. threadcreate(fsloop, nil, STACK);
  72. startpipe();
  73. startcmd(av, &notepg);
  74. strcpy(buf, "win");
  75. j = 3;
  76. for(i=0; i<argc && j+1+strlen(argv[i])+1<sizeof buf; i++){
  77. strcpy(buf+j, " ");
  78. strcpy(buf+j+1, argv[i]);
  79. j += 1+strlen(argv[i]);
  80. }
  81. ctlprint(win->ctl, "scroll");
  82. winsetdump(win, dir, buf);
  83. }
  84. int
  85. EQUAL(char *s, char *t)
  86. {
  87. while(tolower(*s) == tolower(*t++))
  88. if(*s++ == '\0')
  89. return 1;
  90. return 0;
  91. }
  92. int
  93. command(Window *w, char *s)
  94. {
  95. while(*s==' ' || *s=='\t' || *s=='\n')
  96. s++;
  97. if(strcmp(s, "Delete")==0){
  98. windel(w, 1);
  99. threadexitsall(nil);
  100. return 1;
  101. }
  102. if(strcmp(s, "Del")==0){
  103. if(windel(w, 0))
  104. threadexitsall(nil);
  105. return 1;
  106. }
  107. if(EQUAL(s, "scroll")){
  108. ctlprint(w->ctl, "scroll\nshow");
  109. return 1;
  110. }
  111. if(EQUAL(s, "noscroll")){
  112. ctlprint(w->ctl, "noscroll");
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. static long
  118. utfncpy(char *to, char *from, int n)
  119. {
  120. char *end, *e;
  121. e = to+n;
  122. if(to >= e)
  123. return 0;
  124. end = memccpy(to, from, '\0', e - to);
  125. if(end == nil){
  126. end = e;
  127. if(end[-1]&0x80){
  128. if(end-2>=to && (end[-2]&0xE0)==0xC0)
  129. return end-to;
  130. if(end-3>=to && (end[-3]&0xF0)==0xE0)
  131. return end-to;
  132. while(end>to && (*--end&0xC0)==0x80)
  133. ;
  134. }
  135. }else
  136. end--;
  137. return end - to;
  138. }
  139. /* sendinput and fsloop run in the same proc (can't interrupt each other). */
  140. static Req *q;
  141. static Req **eq;
  142. static int
  143. __sendinput(Window *w, ulong q0, ulong q1)
  144. {
  145. char *s, *t;
  146. int n, nb, eofchar;
  147. static int partial;
  148. static char tmp[UTFmax];
  149. Req *r;
  150. Rune rune;
  151. if(!q)
  152. return 0;
  153. r = q;
  154. n = 0;
  155. if(partial){
  156. Partial:
  157. nb = partial;
  158. if(nb > r->ifcall.count)
  159. nb = r->ifcall.count;
  160. memmove(r->ofcall.data, tmp, nb);
  161. if(nb!=partial)
  162. memmove(tmp, tmp+nb, partial-nb);
  163. partial -= nb;
  164. q = r->aux;
  165. if(q == nil)
  166. eq = &q;
  167. r->aux = nil;
  168. r->ofcall.count = nb;
  169. if(debug)
  170. fprint(2, "satisfy read with partial\n");
  171. respond(r, nil);
  172. return n;
  173. }
  174. if(q0==q1)
  175. return 0;
  176. s = emalloc((q1-q0)*UTFmax+1);
  177. n = winread(w, q0, q1, s);
  178. s[n] = '\0';
  179. t = strpbrk(s, "\n\004");
  180. if(t == nil){
  181. free(s);
  182. return 0;
  183. }
  184. r = q;
  185. eofchar = 0;
  186. if(*t == '\004'){
  187. eofchar = 1;
  188. *t = '\0';
  189. }else
  190. *++t = '\0';
  191. nb = utfncpy((char*)r->ofcall.data, s, r->ifcall.count);
  192. if(nb==0 && s<t && r->ifcall.count > 0){
  193. partial = utfncpy(tmp, s, UTFmax);
  194. assert(partial > 0);
  195. chartorune(&rune, tmp);
  196. partial = runelen(rune);
  197. free(s);
  198. n = 1;
  199. goto Partial;
  200. }
  201. n = utfnlen(r->ofcall.data, nb);
  202. if(nb==strlen(s) && eofchar)
  203. n++;
  204. r->ofcall.count = nb;
  205. q = r->aux;
  206. if(q == nil)
  207. eq = &q;
  208. r->aux = nil;
  209. if(debug)
  210. fprint(2, "read returns %lud-%lud: %.*q\n", q0, q0+n, n, r->ofcall.data);
  211. respond(r, nil);
  212. return n;
  213. }
  214. static int
  215. _sendinput(Window *w, ulong q0, ulong *q1)
  216. {
  217. char buf[32];
  218. int n;
  219. n = __sendinput(w, q0, *q1);
  220. if(!n || !eraseinput)
  221. return n;
  222. /* erase q0 to q0+n */
  223. sprint(buf, "#%lud,#%lud", q0, q0+n);
  224. winsetaddr(w, buf, 0);
  225. write(w->data, buf, 0);
  226. *q1 -= n;
  227. return 0;
  228. }
  229. int
  230. sendinput(Window *w, ulong q0, ulong *q1)
  231. {
  232. ulong n;
  233. Req *oq;
  234. n = 0;
  235. do {
  236. oq = q;
  237. n += _sendinput(w, q0+n, q1);
  238. } while(q != oq);
  239. return n;
  240. }
  241. Event esendinput;
  242. void
  243. fsloop(void*)
  244. {
  245. Fsevent e;
  246. Req **l, *r;
  247. eq = &q;
  248. memset(&esendinput, 0, sizeof esendinput);
  249. esendinput.c1 = 'C';
  250. for(;;){
  251. while(recv(fschan, &e) == -1)
  252. ;
  253. r = e.r;
  254. switch(e.type){
  255. case 'r':
  256. *eq = r;
  257. r->aux = nil;
  258. eq = &r->aux;
  259. /* call sendinput with hostpt and endpt */
  260. sendp(win->cevent, &esendinput);
  261. break;
  262. case 'f':
  263. for(l=&q; *l; l=&(*l)->aux){
  264. if(*l == r->oldreq){
  265. *l = (*l)->aux;
  266. if(*l == nil)
  267. eq = l;
  268. respond(r->oldreq, "interrupted");
  269. break;
  270. }
  271. }
  272. respond(r, nil);
  273. break;
  274. }
  275. }
  276. }
  277. void
  278. sendit(char *s)
  279. {
  280. // char tmp[32];
  281. write(win->body, s, strlen(s));
  282. /*
  283. * RSC: The problem here is that other procs can call sendit,
  284. * so we lose our single-threadedness if we call sendinput.
  285. * In fact, we don't even have the right queue memory,
  286. * I think that we'll get a write event from the body write above,
  287. * and we can do the sendinput then, from our single thread.
  288. *
  289. * I still need to figure out how to test this assertion for
  290. * programs that use /srv/win*
  291. *
  292. winselect(win, "$", 0);
  293. seek(win->addr, 0UL, 0);
  294. if(read(win->addr, tmp, 2*12) == 2*12)
  295. hostpt += sendinput(win, hostpt, atol(tmp), );
  296. */
  297. }
  298. void
  299. execevent(Window *w, Event *e, int (*command)(Window*, char*))
  300. {
  301. Event *ea, *e2;
  302. int n, na, len, needfree;
  303. char *s, *t;
  304. ea = nil;
  305. e2 = nil;
  306. if(e->flag & 2)
  307. e2 = recvp(w->cevent);
  308. if(e->flag & 8){
  309. ea = recvp(w->cevent);
  310. na = ea->nb;
  311. recvp(w->cevent);
  312. }else
  313. na = 0;
  314. needfree = 0;
  315. s = e->b;
  316. if(e->nb==0 && (e->flag&2)){
  317. s = e2->b;
  318. e->q0 = e2->q0;
  319. e->q1 = e2->q1;
  320. e->nb = e2->nb;
  321. }
  322. if(e->nb==0 && e->q0<e->q1){
  323. /* fetch data from window */
  324. s = emalloc((e->q1-e->q0)*UTFmax+2);
  325. n = winread(w, e->q0, e->q1, s);
  326. s[n] = '\0';
  327. needfree = 1;
  328. }else
  329. if(na){
  330. t = emalloc(strlen(s)+1+na+2);
  331. sprint(t, "%s %s", s, ea->b);
  332. if(needfree)
  333. free(s);
  334. s = t;
  335. needfree = 1;
  336. }
  337. /* if it's a known command, do it */
  338. /* if it's a long message, it can't be for us anyway */
  339. if(!command(w, s) && s[0]!='\0'){ /* send it as typed text */
  340. /* if it's a built-in from the tag, send it back */
  341. if(e->flag & 1)
  342. fprint(w->event, "%c%c%d %d\n", e->c1, e->c2, e->q0, e->q1);
  343. else{ /* send text to main window */
  344. len = strlen(s);
  345. if(len>0 && s[len-1]!='\n' && s[len-1]!='\004'){
  346. if(!needfree){
  347. /* if(needfree), we left room for a newline before */
  348. t = emalloc(len+2);
  349. strcpy(t, s);
  350. s = t;
  351. needfree = 1;
  352. }
  353. s[len++] = '\n';
  354. s[len] = '\0';
  355. }
  356. sendit(s);
  357. }
  358. }
  359. if(needfree)
  360. free(s);
  361. }
  362. int
  363. hasboundary(Rune *r, int nr)
  364. {
  365. int i;
  366. for(i=0; i<nr; i++)
  367. if(r[i]=='\n' || r[i]=='\004')
  368. return 1;
  369. return 0;
  370. }
  371. void
  372. mainctl(void *v)
  373. {
  374. Window *w;
  375. Event *e;
  376. int delta, pendingS, pendingK;
  377. ulong hostpt, endpt;
  378. char tmp[32];
  379. w = v;
  380. proccreate(wineventproc, w, STACK);
  381. hostpt = 0;
  382. endpt = 0;
  383. winsetaddr(w, "0", 0);
  384. pendingS = 0;
  385. pendingK = 0;
  386. for(;;){
  387. if(debug)
  388. fprint(2, "input range %lud-%lud\n", hostpt, endpt);
  389. e = recvp(w->cevent);
  390. if(debug)
  391. fprint(2, "msg: %C %C %d %d %d %d %q\n",
  392. e->c1 ? e->c1 : ' ', e->c2 ? e->c2 : ' ', e->q0, e->q1, e->flag, e->nb, e->b);
  393. switch(e->c1){
  394. default:
  395. Unknown:
  396. fprint(2, "unknown message %c%c\n", e->c1, e->c2);
  397. break;
  398. case 'C': /* input needed for /dev/cons */
  399. if(pendingS)
  400. pendingK = 1;
  401. else
  402. hostpt += sendinput(w, hostpt, &endpt);
  403. break;
  404. case 'S': /* output to stdout */
  405. sprint(tmp, "#%lud", hostpt);
  406. winsetaddr(w, tmp, 0);
  407. write(w->data, e->b, e->nb);
  408. pendingS += utfnlen(e->b, e->nb);
  409. break;
  410. case 'E': /* write to tag or body; body happens due to sendit */
  411. delta = e->q1-e->q0;
  412. if(e->c2=='I'){
  413. endpt += delta;
  414. if(e->q0 < hostpt)
  415. hostpt += delta;
  416. else
  417. hostpt += sendinput(w, hostpt, &endpt);
  418. break;
  419. }
  420. if(!islower(e->c2))
  421. fprint(2, "win msg: %C %C %d %d %d %d %q\n",
  422. e->c1, e->c2, e->q0, e->q1, e->flag, e->nb, e->b);
  423. break;
  424. case 'F': /* generated by our actions (specifically case 'S' above) */
  425. delta = e->q1-e->q0;
  426. if(e->c2=='D'){
  427. /* we know about the delete by _sendinput */
  428. break;
  429. }
  430. if(e->c2=='I'){
  431. pendingS -= e->q1 - e->q0;
  432. if(pendingS < 0)
  433. fprint(2, "win: pendingS = %d\n", pendingS);
  434. if(e->q0 != hostpt)
  435. fprint(2, "win: insert at %d expected %lud\n", e->q0, hostpt);
  436. endpt += delta;
  437. hostpt += delta;
  438. sendp(writechan, nil);
  439. if(pendingS == 0 && pendingK){
  440. pendingK = 0;
  441. hostpt += sendinput(w, hostpt, &endpt);
  442. }
  443. break;
  444. }
  445. if(!islower(e->c2))
  446. fprint(2, "win msg: %C %C %d %d %d %d %q\n",
  447. e->c1, e->c2, e->q0, e->q1, e->flag, e->nb, e->b);
  448. break;
  449. case 'K':
  450. delta = e->q1-e->q0;
  451. switch(e->c2){
  452. case 'D':
  453. endpt -= delta;
  454. if(e->q1 < hostpt)
  455. hostpt -= delta;
  456. else if(e->q0 < hostpt)
  457. hostpt = e->q0;
  458. break;
  459. case 'I':
  460. delta = e->q1 - e->q0;
  461. endpt += delta;
  462. if(endpt < e->q1) /* just in case */
  463. endpt = e->q1;
  464. if(e->q0 < hostpt)
  465. hostpt += delta;
  466. if(e->nr>0 && e->r[e->nr-1]==0x7F){
  467. write(notepg, "interrupt", 9);
  468. hostpt = endpt;
  469. break;
  470. }
  471. if(e->q0 >= hostpt
  472. && hasboundary(e->r, e->nr)){
  473. /*
  474. * If we are between the S message (which
  475. * we processed by inserting text in the
  476. * window) and the F message notifying us
  477. * that the text has been inserted, then our
  478. * impression of the hostpt and acme's
  479. * may be different. This could be seen if you
  480. * hit enter a bunch of times in a con
  481. * session. To work around the unreliability,
  482. * only send input if we don't have an S pending.
  483. * The same race occurs between when a character
  484. * is typed and when we get notice of it, but
  485. * since characters tend to be typed at the end
  486. * of the buffer, we don't run into it. There's
  487. * no workaround possible for this typing race,
  488. * since we can't tell when the user has typed
  489. * something but we just haven't been notified.
  490. */
  491. if(pendingS)
  492. pendingK = 1;
  493. else
  494. hostpt += sendinput(w, hostpt, &endpt);
  495. }
  496. break;
  497. }
  498. break;
  499. case 'M': /* mouse */
  500. delta = e->q1-e->q0;
  501. switch(e->c2){
  502. case 'x':
  503. case 'X':
  504. execevent(w, e, command);
  505. break;
  506. case 'l': /* reflect all searches back to acme */
  507. case 'L':
  508. if(e->flag & 2)
  509. recvp(w->cevent);
  510. winwriteevent(w, e);
  511. break;
  512. case 'I':
  513. endpt += delta;
  514. if(e->q0 < hostpt)
  515. hostpt += delta;
  516. else
  517. hostpt += sendinput(w, hostpt, &endpt);
  518. break;
  519. case 'D':
  520. endpt -= delta;
  521. if(e->q1 < hostpt)
  522. hostpt -= delta;
  523. else if(e->q0 < hostpt)
  524. hostpt = e->q0;
  525. break;
  526. case 'd': /* modify away; we don't care */
  527. case 'i':
  528. break;
  529. default:
  530. goto Unknown;
  531. }
  532. }
  533. }
  534. }
  535. enum
  536. {
  537. NARGS = 100,
  538. NARGCHAR = 8*1024,
  539. EXECSTACK = STACK+(NARGS+1)*sizeof(char*)+NARGCHAR
  540. };
  541. struct Exec
  542. {
  543. char **argv;
  544. Channel *cpid;
  545. };
  546. int
  547. lookinbin(char *s)
  548. {
  549. if(s[0] == '/')
  550. return 0;
  551. if(s[0]=='.' && s[1]=='/')
  552. return 0;
  553. if(s[0]=='.' && s[1]=='.' && s[2]=='/')
  554. return 0;
  555. return 1;
  556. }
  557. /* adapted from mail. not entirely free of details from that environment */
  558. void
  559. execproc(void *v)
  560. {
  561. struct Exec *e;
  562. char *cmd, **av;
  563. Channel *cpid;
  564. e = v;
  565. rfork(RFCFDG|RFNOTEG);
  566. av = e->argv;
  567. close(0);
  568. open("/dev/cons", OREAD);
  569. close(1);
  570. open("/dev/cons", OWRITE);
  571. dup(1, 2);
  572. cpid = e->cpid;
  573. free(e);
  574. procexec(cpid, av[0], av);
  575. if(lookinbin(av[0])){
  576. cmd = estrstrdup("/bin/", av[0]);
  577. procexec(cpid, cmd, av);
  578. }
  579. error("can't exec %s: %r", av[0]);
  580. }
  581. void
  582. startcmd(char *argv[], int *notepg)
  583. {
  584. struct Exec *e;
  585. Channel *cpid;
  586. char buf[64];
  587. int pid;
  588. e = emalloc(sizeof(struct Exec));
  589. e->argv = argv;
  590. cpid = chancreate(sizeof(ulong), 0);
  591. e->cpid = cpid;
  592. sprint(buf, "/mnt/wsys/%d", win->id);
  593. bind(buf, "/dev/acme", MREPL);
  594. proccreate(execproc, e, EXECSTACK);
  595. do
  596. pid = recvul(cpid);
  597. while(pid == -1);
  598. sprint(buf, "/proc/%d/notepg", pid);
  599. *notepg = open(buf, OWRITE);
  600. }