main.c 12 KB

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