main.c 12 KB

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