exec.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <thread.h>
  13. #include <cursor.h>
  14. #include <mouse.h>
  15. #include <keyboard.h>
  16. #include <frame.h>
  17. #include <fcall.h>
  18. #include <plumb.h>
  19. #include "dat.h"
  20. #include "fns.h"
  21. Buffer snarfbuf;
  22. /*
  23. * These functions get called as:
  24. *
  25. * fn(et, t, argt, flag1, flag1, flag2, s, n);
  26. *
  27. * Where the arguments are:
  28. *
  29. * et: the Text* in which the executing event (click) occurred
  30. * t: the Text* containing the current selection (Edit, Cut, Snarf, Paste)
  31. * argt: the Text* containing the argument for a 2-1 click.
  32. * e->flag1: from Exectab entry
  33. * e->flag2: from Exectab entry
  34. * s: the command line remainder (e.g., "x" if executing "Dump x")
  35. * n: length of s (s is *not* NUL-terminated)
  36. */
  37. void del(Text*, Text*, Text*, int, int, Rune*, int);
  38. void delcol(Text*, Text*, Text*, int, int, Rune*, int);
  39. void dump(Text*, Text*, Text*, int, int, Rune*, int);
  40. void edit(Text*, Text*, Text*, int, int, Rune*, int);
  41. void exit(Text*, Text*, Text*, int, int, Rune*, int);
  42. void fontx(Text*, Text*, Text*, int, int, Rune*, int);
  43. void get(Text*, Text*, Text*, int, int, Rune*, int);
  44. void id(Text*, Text*, Text*, int, int, Rune*, int);
  45. void incl(Text*, Text*, Text*, int, int, Rune*, int);
  46. void indent(Text*, Text*, Text*, int, int, Rune*, int);
  47. void kill(Text*, Text*, Text*, int, int, Rune*, int);
  48. void local(Text*, Text*, Text*, int, int, Rune*, int);
  49. void look(Text*, Text*, Text*, int, int, Rune*, int);
  50. void newcol(Text*, Text*, Text*, int, int, Rune*, int);
  51. void paste(Text*, Text*, Text*, int, int, Rune*, int);
  52. void put(Text*, Text*, Text*, int, int, Rune*, int);
  53. void putall(Text*, Text*, Text*, int, int, Rune*, int);
  54. void sendx(Text*, Text*, Text*, int, int, Rune*, int);
  55. void sort(Text*, Text*, Text*, int, int, Rune*, int);
  56. void tab(Text*, Text*, Text*, int, int, Rune*, int);
  57. void zeroxx(Text*, Text*, Text*, int, int, Rune*, int);
  58. typedef struct Exectab Exectab;
  59. struct Exectab
  60. {
  61. Rune *name;
  62. void (*fn)(Text*, Text*, Text*, int, int, Rune*, int);
  63. int mark;
  64. int flag1;
  65. int flag2;
  66. };
  67. Exectab exectab[] = {
  68. { (Rune*)L"Cut", cut, TRUE, TRUE, TRUE },
  69. { (Rune*)L"Del", del, FALSE, FALSE, XXX },
  70. { (Rune*)L"Delcol", delcol, FALSE, XXX, XXX },
  71. { (Rune*)L"Delete", del, FALSE, TRUE, XXX },
  72. { (Rune*)L"Dump", dump, FALSE, TRUE, XXX },
  73. { (Rune*)L"Edit", edit, FALSE, XXX, XXX },
  74. { (Rune*)L"Exit", exit, FALSE, XXX, XXX },
  75. { (Rune*)L"Font", fontx, FALSE, XXX, XXX },
  76. { (Rune*)L"Get", get, FALSE, TRUE, XXX },
  77. { (Rune*)L"ID", id, FALSE, XXX, XXX },
  78. { (Rune*)L"Incl", incl, FALSE, XXX, XXX },
  79. { (Rune*)L"Indent", indent, FALSE, XXX, XXX },
  80. { (Rune*)L"Kill", kill, FALSE, XXX, XXX },
  81. { (Rune*)L"Load", dump, FALSE, FALSE, XXX },
  82. { (Rune*)L"Local", local, FALSE, XXX, XXX },
  83. { (Rune*)L"Look", look, FALSE, XXX, XXX },
  84. { (Rune*)L"New", new, FALSE, XXX, XXX },
  85. { (Rune*)L"Newcol", newcol, FALSE, XXX, XXX },
  86. { (Rune*)L"Paste", paste, TRUE, TRUE, XXX },
  87. { (Rune*)L"Put", put, FALSE, XXX, XXX },
  88. { (Rune*)L"Putall", putall, FALSE, XXX, XXX },
  89. { (Rune*)L"Redo", undo, FALSE, FALSE, XXX },
  90. { (Rune*)L"Send", sendx, TRUE, XXX, XXX },
  91. { (Rune*)L"Snarf", cut, FALSE, TRUE, FALSE },
  92. { (Rune*)L"Sort", sort, FALSE, XXX, XXX },
  93. { (Rune*)L"Tab", tab, FALSE, XXX, XXX },
  94. { (Rune*)L"Undo", undo, FALSE, TRUE, XXX },
  95. { (Rune*)L"Zerox", zeroxx, FALSE, XXX, XXX },
  96. { nil, nil, 0, 0, 0 },
  97. };
  98. Exectab*
  99. lookup(Rune *r, int n)
  100. {
  101. Exectab *e;
  102. int nr;
  103. r = skipbl(r, n, &n);
  104. if(n == 0)
  105. return nil;
  106. findbl(r, n, &nr);
  107. nr = n-nr;
  108. for(e=exectab; e->name; e++)
  109. if(runeeq(r, nr, e->name, runestrlen(e->name)) == TRUE)
  110. return e;
  111. return nil;
  112. }
  113. int
  114. isexecc(int c)
  115. {
  116. if(isfilec(c))
  117. return 1;
  118. return c=='<' || c=='|' || c=='>';
  119. }
  120. void
  121. execute(Text *t, uint aq0, uint aq1, int external, Text *argt)
  122. {
  123. uint q0, q1;
  124. Rune *r, *s;
  125. char *b, *a, *aa;
  126. Exectab *e;
  127. int c, n, f;
  128. Runestr dir;
  129. q0 = aq0;
  130. q1 = aq1;
  131. if(q1 == q0){ /* expand to find word (actually file name) */
  132. /* if in selection, choose selection */
  133. if(t->q1>t->q0 && t->q0<=q0 && q0<=t->q1){
  134. q0 = t->q0;
  135. q1 = t->q1;
  136. }else{
  137. while(q1<t->file->Buffer.nc && isexecc(c=textreadc(t, q1)) && c!=':')
  138. q1++;
  139. while(q0>0 && isexecc(c=textreadc(t, q0-1)) && c!=':')
  140. q0--;
  141. if(q1 == q0)
  142. return;
  143. }
  144. }
  145. r = runemalloc(q1-q0);
  146. bufread(&t->file->Buffer, q0, r, q1-q0);
  147. e = lookup(r, q1-q0);
  148. if(!external && t->w!=nil && t->w->nopen[QWevent]>0){
  149. f = 0;
  150. if(e)
  151. f |= 1;
  152. if(q0!=aq0 || q1!=aq1){
  153. bufread(&t->file->Buffer, aq0, r, aq1-aq0);
  154. f |= 2;
  155. }
  156. aa = getbytearg(argt, TRUE, TRUE, &a);
  157. if(a){
  158. if(strlen(a) > EVENTSIZE){ /* too big; too bad */
  159. free(aa);
  160. free(a);
  161. warning(nil, "`argument string too long\n");
  162. return;
  163. }
  164. f |= 8;
  165. }
  166. c = 'x';
  167. if(t->what == Body)
  168. c = 'X';
  169. n = aq1-aq0;
  170. if(n <= EVENTSIZE)
  171. winevent(t->w, "%c%d %d %d %d %.*S\n", c, aq0, aq1, f, n, n, r);
  172. else
  173. winevent(t->w, "%c%d %d %d 0 \n", c, aq0, aq1, f, n);
  174. if(q0!=aq0 || q1!=aq1){
  175. n = q1-q0;
  176. bufread(&t->file->Buffer, q0, r, n);
  177. if(n <= EVENTSIZE)
  178. winevent(t->w, "%c%d %d 0 %d %.*S\n", c, q0, q1, n, n, r);
  179. else
  180. winevent(t->w, "%c%d %d 0 0 \n", c, q0, q1, n);
  181. }
  182. if(a){
  183. winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(a), a);
  184. if(aa)
  185. winevent(t->w, "%c0 0 0 %d %s\n", c, utflen(aa), aa);
  186. else
  187. winevent(t->w, "%c0 0 0 0 \n", c);
  188. }
  189. free(r);
  190. free(aa);
  191. free(a);
  192. return;
  193. }
  194. if(e){
  195. if(e->mark && seltext!=nil)
  196. if(seltext->what == Body){
  197. seq++;
  198. filemark(seltext->w->body.file);
  199. }
  200. s = skipbl(r, q1-q0, &n);
  201. s = findbl(s, n, &n);
  202. s = skipbl(s, n, &n);
  203. (*e->fn)(t, seltext, argt, e->flag1, e->flag2, s, n);
  204. free(r);
  205. return;
  206. }
  207. b = runetobyte(r, q1-q0);
  208. free(r);
  209. dir = dirname(t, nil, 0);
  210. if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
  211. free(dir.r);
  212. dir.r = nil;
  213. dir.nr = 0;
  214. }
  215. aa = getbytearg(argt, TRUE, TRUE, &a);
  216. if(t->w)
  217. incref(&t->w->Ref);
  218. run(t->w, b, dir.r, dir.nr, TRUE, aa, a, FALSE);
  219. }
  220. char*
  221. printarg(Text *argt, uint q0, uint q1)
  222. {
  223. char *buf;
  224. if(argt->what!=Body || argt->file->name==nil)
  225. return nil;
  226. buf = emalloc(argt->file->nname+32);
  227. if(q0 == q1)
  228. sprint(buf, "%.*S:#%d", argt->file->nname, argt->file->name, q0);
  229. else
  230. sprint(buf, "%.*S:#%d,#%d", argt->file->nname, argt->file->name, q0, q1);
  231. return buf;
  232. }
  233. char*
  234. getarg(Text *argt, int doaddr, int dofile, Rune **rp, int *nrp)
  235. {
  236. int n;
  237. Expand e;
  238. char *a;
  239. *rp = nil;
  240. *nrp = 0;
  241. if(argt == nil)
  242. return nil;
  243. a = nil;
  244. textcommit(argt, TRUE);
  245. if(expand(argt, argt->q0, argt->q1, &e)){
  246. free(e.bname);
  247. if(e.nname && dofile){
  248. e.name = runerealloc(e.name, e.nname+1);
  249. if(doaddr)
  250. a = printarg(argt, e.q0, e.q1);
  251. *rp = e.name;
  252. *nrp = e.nname;
  253. return a;
  254. }
  255. free(e.name);
  256. }else{
  257. e.q0 = argt->q0;
  258. e.q1 = argt->q1;
  259. }
  260. n = e.q1 - e.q0;
  261. *rp = runemalloc(n+1);
  262. bufread(&argt->file->Buffer, e.q0, *rp, n);
  263. if(doaddr)
  264. a = printarg(argt, e.q0, e.q1);
  265. *nrp = n;
  266. return a;
  267. }
  268. char*
  269. getbytearg(Text *argt, int doaddr, int dofile, char **bp)
  270. {
  271. Rune *r;
  272. int n;
  273. char *aa;
  274. *bp = nil;
  275. aa = getarg(argt, doaddr, dofile, &r, &n);
  276. if(r == nil)
  277. return nil;
  278. *bp = runetobyte(r, n);
  279. free(r);
  280. return aa;
  281. }
  282. void
  283. newcol(Text *et, Text*a, Text*b, int ic, int d, Rune*e, int f)
  284. {
  285. Column *c;
  286. c = rowadd(et->row, nil, -1);
  287. if(c)
  288. winsettag(coladd(c, nil, nil, -1));
  289. }
  290. void
  291. delcol(Text *et, Text*a, Text*b, int ic, int d, Rune*e, int f)
  292. {
  293. int i;
  294. Column *c;
  295. Window *w;
  296. c = et->col;
  297. if(c==nil || colclean(c)==0)
  298. return;
  299. for(i=0; i<c->nw; i++){
  300. w = c->w[i];
  301. if(w->nopen[QWevent]+w->nopen[QWaddr]+w->nopen[QWdata]+w->nopen[QWxdata] > 0){
  302. warning(nil, "can't delete column; %.*S is running an external command\n", w->body.file->nname, w->body.file->name);
  303. return;
  304. }
  305. }
  306. rowclose(et->col->row, et->col, TRUE);
  307. }
  308. void
  309. del(Text *et, Text*a, Text *argt, int flag1, int b, Rune *arg, int narg)
  310. {
  311. Window *w;
  312. char *name, *p;
  313. Plumbmsg *pm;
  314. if(et->col==nil || et->w == nil)
  315. return;
  316. if(flag1 || et->w->body.file->ntext>1 || winclean(et->w, FALSE)){
  317. w = et->w;
  318. name = getname(&w->body, argt, arg, narg, TRUE);
  319. if(name && plumbsendfd >= 0){
  320. pm = emalloc(sizeof(Plumbmsg));
  321. pm->src = estrdup("acme");
  322. pm->dst = estrdup("close");
  323. pm->wdir = estrdup(name);
  324. if((p = strrchr(pm->wdir, '/')) != nil)
  325. *p = '\0';
  326. pm->type = estrdup("text");
  327. pm->attr = nil;
  328. pm->data = estrdup(name);
  329. pm->ndata = strlen(pm->data);
  330. if(pm->ndata < messagesize-1024)
  331. plumbsend(plumbsendfd, pm);
  332. else
  333. plumbfree(pm);
  334. }
  335. colclose(et->col, et->w, TRUE);
  336. }
  337. }
  338. void
  339. sort(Text *et, Text*a, Text*b, int c, int d, Rune*e, int f)
  340. {
  341. if(et->col)
  342. colsort(et->col);
  343. }
  344. uint
  345. seqof(Window *w, int isundo)
  346. {
  347. /* if it's undo, see who changed with us */
  348. if(isundo)
  349. return w->body.file->seq;
  350. /* if it's redo, see who we'll be sync'ed up with */
  351. return fileredoseq(w->body.file);
  352. }
  353. void
  354. undo(Text *et, Text*a, Text*b, int flag1, int ic, Rune*d, int e)
  355. {
  356. int i, j;
  357. Column *c;
  358. Window *w;
  359. uint seq;
  360. if(et==nil || et->w== nil)
  361. return;
  362. seq = seqof(et->w, flag1);
  363. if(seq == 0){
  364. /* nothing to undo */
  365. return;
  366. }
  367. /*
  368. * Undo the executing window first. Its display will update. other windows
  369. * in the same file will not call show() and jump to a different location in the file.
  370. * Simultaneous changes to other files will be chaotic, however.
  371. */
  372. winundo(et->w, flag1);
  373. for(i=0; i<row.ncol; i++){
  374. c = row.col[i];
  375. for(j=0; j<c->nw; j++){
  376. w = c->w[j];
  377. if(w == et->w)
  378. continue;
  379. if(seqof(w, flag1) == seq)
  380. winundo(w, flag1);
  381. }
  382. }
  383. }
  384. char*
  385. getname(Text *t, Text *argt, Rune *arg, int narg, int isput)
  386. {
  387. char *s;
  388. Rune *r;
  389. int i, n, promote;
  390. Runestr dir;
  391. getarg(argt, FALSE, TRUE, &r, &n);
  392. promote = FALSE;
  393. if(r == nil)
  394. promote = TRUE;
  395. else if(isput){
  396. /* if are doing a Put, want to synthesize name even for non-existent file */
  397. /* best guess is that file name doesn't contain a slash */
  398. promote = TRUE;
  399. for(i=0; i<n; i++)
  400. if(r[i] == '/'){
  401. promote = FALSE;
  402. break;
  403. }
  404. if(promote){
  405. t = argt;
  406. arg = r;
  407. narg = n;
  408. }
  409. }
  410. if(promote){
  411. n = narg;
  412. if(n <= 0){
  413. s = runetobyte(t->file->name, t->file->nname);
  414. return s;
  415. }
  416. /* prefix with directory name if necessary */
  417. dir.r = nil;
  418. dir.nr = 0;
  419. if(n>0 && arg[0]!='/'){
  420. dir = dirname(t, nil, 0);
  421. if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
  422. free(dir.r);
  423. dir.r = nil;
  424. dir.nr = 0;
  425. }
  426. }
  427. if(dir.r){
  428. r = runemalloc(dir.nr+n+1);
  429. runemove(r, dir.r, dir.nr);
  430. free(dir.r);
  431. if(dir.nr>0 && r[dir.nr]!='/' && n>0 && arg[0]!='/')
  432. r[dir.nr++] = '/';
  433. runemove(r+dir.nr, arg, n);
  434. n += dir.nr;
  435. }else{
  436. r = runemalloc(n+1);
  437. runemove(r, arg, n);
  438. }
  439. }
  440. s = runetobyte(r, n);
  441. free(r);
  442. if(strlen(s) == 0){
  443. free(s);
  444. s = nil;
  445. }
  446. return s;
  447. }
  448. void
  449. zeroxx(Text *et, Text *t, Text*a, int b, int ic, Rune*d, int e)
  450. {
  451. Window *nw;
  452. int c, locked;
  453. locked = FALSE;
  454. if(t!=nil && t->w!=nil && t->w!=et->w){
  455. locked = TRUE;
  456. c = 'M';
  457. if(et->w)
  458. c = et->w->owner;
  459. winlock(t->w, c);
  460. }
  461. if(t == nil)
  462. t = et;
  463. if(t==nil || t->w==nil)
  464. return;
  465. t = &t->w->body;
  466. if(t->w->isdir)
  467. warning(nil, "%.*S is a directory; Zerox illegal\n", t->file->nname, t->file->name);
  468. else{
  469. nw = coladd(t->w->col, nil, t->w, -1);
  470. /* ugly: fix locks so w->unlock works */
  471. winlock1(nw, t->w->owner);
  472. }
  473. if(locked)
  474. winunlock(t->w);
  475. }
  476. void
  477. get(Text *et, Text *t, Text *argt, int flag1, int a, Rune *arg, int narg)
  478. {
  479. char *name;
  480. Rune *r;
  481. int i, n, dirty, samename, isdir;
  482. Window *w;
  483. Text *u;
  484. Dir *d;
  485. if(flag1)
  486. if(et==nil || et->w==nil)
  487. return;
  488. if(!et->w->isdir && (et->w->body.file->Buffer.nc>0 && !winclean(et->w, TRUE)))
  489. return;
  490. w = et->w;
  491. t = &w->body;
  492. name = getname(t, argt, arg, narg, FALSE);
  493. if(name == nil){
  494. warning(nil, "no file name\n");
  495. return;
  496. }
  497. if(t->file->ntext>1){
  498. d = dirstat(name);
  499. isdir = (d!=nil && (d->qid.type & QTDIR));
  500. free(d);
  501. if(isdir){
  502. warning(nil, "%s is a directory; can't read with multiple windows on it\n", name);
  503. return;
  504. }
  505. }
  506. r = bytetorune(name, &n);
  507. for(i=0; i<t->file->ntext; i++){
  508. u = t->file->text[i];
  509. /* second and subsequent calls with zero an already empty buffer, but OK */
  510. textreset(u);
  511. windirfree(u->w);
  512. }
  513. samename = runeeq(r, n, t->file->name, t->file->nname);
  514. textload(t, 0, name, samename);
  515. if(samename){
  516. t->file->mod = FALSE;
  517. dirty = FALSE;
  518. }else{
  519. t->file->mod = TRUE;
  520. dirty = TRUE;
  521. }
  522. for(i=0; i<t->file->ntext; i++)
  523. t->file->text[i]->w->dirty = dirty;
  524. free(name);
  525. free(r);
  526. winsettag(w);
  527. t->file->unread = FALSE;
  528. for(i=0; i<t->file->ntext; i++){
  529. u = t->file->text[i];
  530. textsetselect(&u->w->tag, u->w->tag.file->Buffer.nc, u->w->tag.file->Buffer.nc);
  531. textscrdraw(u);
  532. }
  533. }
  534. void
  535. putfile(File *f, int q0, int q1, Rune *namer, int nname)
  536. {
  537. uint n, m;
  538. Rune *r;
  539. char *s, *name, *p;
  540. int i, fd, q;
  541. Dir *d, *d1;
  542. Window *w;
  543. Plumbmsg *pm;
  544. int isapp;
  545. w = f->curtext->w;
  546. name = runetobyte(namer, nname);
  547. d = dirstat(name);
  548. if(d!=nil && runeeq(namer, nname, f->name, f->nname)){
  549. /* f->mtime+1 because when talking over NFS it's often off by a second */
  550. if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime+1<d->mtime){
  551. f->dev = d->dev;
  552. f->qidpath = d->qid.path;
  553. f->mtime = d->mtime;
  554. if(f->unread)
  555. warning(nil, "%s not written; file already exists\n", name);
  556. else
  557. warning(nil, "%s modified%s%s since last read\n", name, d->muid[0]?" by ":"", d->muid);
  558. goto Rescue1;
  559. }
  560. }
  561. fd = create(name, OWRITE, 0666);
  562. if(fd < 0){
  563. warning(nil, "can't create file %s: %r\n", name);
  564. goto Rescue1;
  565. }
  566. r = fbufalloc();
  567. s = fbufalloc();
  568. free(d);
  569. d = dirfstat(fd);
  570. isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND));
  571. if(isapp){
  572. warning(nil, "%s not written; file is append only\n", name);
  573. goto Rescue2;
  574. }
  575. for(q=q0; q<q1; q+=n){
  576. n = q1 - q;
  577. if(n > BUFSIZE/UTFmax)
  578. n = BUFSIZE/UTFmax;
  579. bufread(&f->Buffer, q, r, n);
  580. m = snprint(s, BUFSIZE+1, "%.*S", n, r);
  581. if(write(fd, s, m) != m){
  582. warning(nil, "can't write file %s: %r\n", name);
  583. goto Rescue2;
  584. }
  585. }
  586. if(runeeq(namer, nname, f->name, f->nname)){
  587. if(q0!=0 || q1!=f->Buffer.nc){
  588. f->mod = TRUE;
  589. w->dirty = TRUE;
  590. f->unread = TRUE;
  591. }else{
  592. d1 = dirfstat(fd);
  593. if(d1 != nil){
  594. free(d);
  595. d = d1;
  596. }
  597. f->qidpath = d->qid.path;
  598. f->dev = d->dev;
  599. f->mtime = d->mtime;
  600. f->mod = FALSE;
  601. w->dirty = FALSE;
  602. f->unread = FALSE;
  603. }
  604. for(i=0; i<f->ntext; i++){
  605. f->text[i]->w->putseq = f->seq;
  606. f->text[i]->w->dirty = w->dirty;
  607. }
  608. }
  609. if(plumbsendfd >= 0){
  610. pm = emalloc(sizeof(Plumbmsg));
  611. pm->src = estrdup("acme");
  612. pm->dst = estrdup("put");
  613. pm->wdir = estrdup(name);
  614. if((p = strrchr(pm->wdir, '/')) != nil)
  615. *p = '\0';
  616. pm->type = estrdup("text");
  617. pm->attr = nil;
  618. pm->data = estrdup(name);
  619. pm->ndata = strlen(pm->data);
  620. if(pm->ndata < messagesize-1024)
  621. plumbsend(plumbsendfd, pm);
  622. else
  623. plumbfree(pm);
  624. }
  625. fbuffree(s);
  626. fbuffree(r);
  627. free(d);
  628. free(namer);
  629. free(name);
  630. close(fd);
  631. winsettag(w);
  632. return;
  633. Rescue2:
  634. fbuffree(s);
  635. fbuffree(r);
  636. close(fd);
  637. /* fall through */
  638. Rescue1:
  639. free(d);
  640. free(namer);
  641. free(name);
  642. }
  643. void
  644. put(Text *et, Text*a, Text *argt, int b, int c, Rune *arg, int narg)
  645. {
  646. int nname;
  647. Rune *namer;
  648. Window *w;
  649. File *f;
  650. char *name;
  651. if(et==nil || et->w==nil || et->w->isdir)
  652. return;
  653. w = et->w;
  654. f = w->body.file;
  655. name = getname(&w->body, argt, arg, narg, TRUE);
  656. if(name == nil){
  657. warning(nil, "no file name\n");
  658. return;
  659. }
  660. namer = bytetorune(name, &nname);
  661. putfile(f, 0, f->Buffer.nc, namer, nname);
  662. free(name);
  663. }
  664. void
  665. dump(Text *a, Text *b, Text *argt, int isdump, int c, Rune *arg, int narg)
  666. {
  667. char *name;
  668. if(narg)
  669. name = runetobyte(arg, narg);
  670. else
  671. getbytearg(argt, FALSE, TRUE, &name);
  672. if(isdump)
  673. rowdump(&row, name);
  674. else
  675. rowload(&row, name, FALSE);
  676. free(name);
  677. }
  678. void
  679. cut(Text *et, Text *t, Text*a, int dosnarf, int docut, Rune*b, int ic)
  680. {
  681. uint q0, q1, n, locked, c;
  682. Rune *r;
  683. /*
  684. * if not executing a mouse chord (et != t) and snarfing (dosnarf)
  685. * and executed Cut or Snarf in window tag (et->w != nil),
  686. * then use the window body selection or the tag selection
  687. * or do nothing at all.
  688. */
  689. if(et!=t && dosnarf && et->w!=nil){
  690. if(et->w->body.q1>et->w->body.q0){
  691. t = &et->w->body;
  692. if(docut)
  693. filemark(t->file); /* seq has been incremented by execute */
  694. }else if(et->w->tag.q1>et->w->tag.q0)
  695. t = &et->w->tag;
  696. else
  697. t = nil;
  698. }
  699. if(t == nil) /* no selection */
  700. return;
  701. locked = FALSE;
  702. if(t->w!=nil && et->w!=t->w){
  703. locked = TRUE;
  704. c = 'M';
  705. if(et->w)
  706. c = et->w->owner;
  707. winlock(t->w, c);
  708. }
  709. if(t->q0 == t->q1){
  710. if(locked)
  711. winunlock(t->w);
  712. return;
  713. }
  714. if(dosnarf){
  715. q0 = t->q0;
  716. q1 = t->q1;
  717. bufdelete(&snarfbuf, 0, snarfbuf.nc);
  718. r = fbufalloc();
  719. while(q0 < q1){
  720. n = q1 - q0;
  721. if(n > RBUFSIZE)
  722. n = RBUFSIZE;
  723. bufread(&t->file->Buffer, q0, r, n);
  724. bufinsert(&snarfbuf, snarfbuf.nc, r, n);
  725. q0 += n;
  726. }
  727. fbuffree(r);
  728. putsnarf();
  729. }
  730. if(docut){
  731. textdelete(t, t->q0, t->q1, TRUE);
  732. textsetselect(t, t->q0, t->q0);
  733. if(t->w){
  734. textscrdraw(t);
  735. winsettag(t->w);
  736. }
  737. }else if(dosnarf) /* Snarf command */
  738. argtext = t;
  739. if(locked)
  740. winunlock(t->w);
  741. }
  742. void
  743. paste(Text *et, Text *t, Text*a, int selectall, int tobody, Rune*b, int ic)
  744. {
  745. int c;
  746. uint q, q0, q1, n;
  747. Rune *r;
  748. /* if(tobody), use body of executing window (Paste or Send command) */
  749. if(tobody && et!=nil && et->w!=nil){
  750. t = &et->w->body;
  751. filemark(t->file); /* seq has been incremented by execute */
  752. }
  753. if(t == nil)
  754. return;
  755. getsnarf();
  756. if(t==nil || snarfbuf.nc==0)
  757. return;
  758. if(t->w!=nil && et->w!=t->w){
  759. c = 'M';
  760. if(et->w)
  761. c = et->w->owner;
  762. winlock(t->w, c);
  763. }
  764. cut(t, t, nil, FALSE, TRUE, nil, 0);
  765. q = 0;
  766. q0 = t->q0;
  767. q1 = t->q0+snarfbuf.nc;
  768. r = fbufalloc();
  769. while(q0 < q1){
  770. n = q1 - q0;
  771. if(n > RBUFSIZE)
  772. n = RBUFSIZE;
  773. if(r == nil)
  774. r = runemalloc(n);
  775. bufread(&snarfbuf, q, r, n);
  776. textinsert(t, q0, r, n, TRUE);
  777. q += n;
  778. q0 += n;
  779. }
  780. fbuffree(r);
  781. if(selectall)
  782. textsetselect(t, t->q0, q1);
  783. else
  784. textsetselect(t, q1, q1);
  785. if(t->w){
  786. textscrdraw(t);
  787. winsettag(t->w);
  788. }
  789. if(t->w!=nil && et->w!=t->w)
  790. winunlock(t->w);
  791. }
  792. void
  793. look(Text *et, Text *t, Text *argt, int a, int b, Rune *arg, int narg)
  794. {
  795. Rune *r;
  796. int n;
  797. if(et && et->w){
  798. t = &et->w->body;
  799. if(narg > 0){
  800. search(t, arg, narg);
  801. return;
  802. }
  803. getarg(argt, FALSE, FALSE, &r, &n);
  804. if(r == nil){
  805. n = t->q1-t->q0;
  806. r = runemalloc(n);
  807. bufread(&t->file->Buffer, t->q0, r, n);
  808. }
  809. search(t, r, n);
  810. free(r);
  811. }
  812. }
  813. void
  814. sendx(Text *et, Text *t, Text*a, int b, int c, Rune*d, int f)
  815. {
  816. if(et->w==nil)
  817. return;
  818. t = &et->w->body;
  819. if(t->q0 != t->q1)
  820. cut(t, t, nil, TRUE, FALSE, nil, 0);
  821. textsetselect(t, t->file->Buffer.nc, t->file->Buffer.nc);
  822. paste(t, t, nil, TRUE, TRUE, nil, 0);
  823. if(textreadc(t, t->file->Buffer.nc-1) != '\n'){
  824. textinsert(t, t->file->Buffer.nc, (Rune*)L"\n", 1, TRUE);
  825. textsetselect(t, t->file->Buffer.nc, t->file->Buffer.nc);
  826. }
  827. }
  828. void
  829. edit(Text *et, Text*a, Text *argt, int b, int c, Rune *arg, int narg)
  830. {
  831. Rune *r;
  832. int len;
  833. if(et == nil)
  834. return;
  835. getarg(argt, FALSE, TRUE, &r, &len);
  836. seq++;
  837. if(r != nil){
  838. editcmd(et, r, len);
  839. free(r);
  840. }else
  841. editcmd(et, arg, narg);
  842. }
  843. void
  844. exit(Text*a, Text*b, Text*c, int d, int e, Rune*f, int g)
  845. {
  846. if(rowclean(&row)){
  847. sendul(cexit, 0);
  848. threadexits(nil);
  849. }
  850. }
  851. void
  852. putall(Text*ta, Text*b, Text*tc, int d, int ie, Rune*f, int g)
  853. {
  854. int i, j, e;
  855. Window *w;
  856. Column *c;
  857. char *a;
  858. for(i=0; i<row.ncol; i++){
  859. c = row.col[i];
  860. for(j=0; j<c->nw; j++){
  861. w = c->w[j];
  862. if(w->isscratch || w->isdir || w->body.file->nname==0)
  863. continue;
  864. if(w->nopen[QWevent] > 0)
  865. continue;
  866. a = runetobyte(w->body.file->name, w->body.file->nname);
  867. e = access(a, 0);
  868. if(w->body.file->mod || w->body.ncache) {
  869. if(e < 0)
  870. warning(nil, "no auto-Put of %s: %r\n", a);
  871. else{
  872. wincommit(w, &w->body);
  873. put(&w->body, nil, nil, XXX, XXX, nil, 0);
  874. }
  875. }
  876. free(a);
  877. }
  878. }
  879. }
  880. void
  881. id(Text*et, Text*a, Text*b, int c, int d, Rune*e, int f)
  882. {
  883. if(et && et->w)
  884. warning(nil, "/mnt/acme/%d/\n", et->w->id);
  885. }
  886. void
  887. local(Text *et, Text*ta, Text*argt, int b, int c, Rune*arg, int narg)
  888. {
  889. char *a, *aa;
  890. Runestr dir;
  891. aa = getbytearg(argt, TRUE, TRUE, &a);
  892. dir = dirname(et, nil, 0);
  893. if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
  894. free(dir.r);
  895. dir.r = nil;
  896. dir.nr = 0;
  897. }
  898. run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
  899. }
  900. void
  901. kill(Text *ta, Text*b, Text *argt, int c, int d, Rune *arg, int narg)
  902. {
  903. Rune *a, *cmd, *r;
  904. int na;
  905. getarg(argt, FALSE, FALSE, &r, &na);
  906. if(r)
  907. kill(nil, nil, nil, 0, 0, r, na);
  908. /* loop condition: *arg is not a blank */
  909. for(;;){
  910. a = findbl(arg, narg, &na);
  911. if(a == arg)
  912. break;
  913. cmd = runemalloc(narg-na+1);
  914. runemove(cmd, arg, narg-na);
  915. sendp(ckill, cmd);
  916. arg = skipbl(a, na, &narg);
  917. }
  918. }
  919. void
  920. fontx(Text *et, Text *t, Text *argt, int ia, int b, Rune *arg, int narg)
  921. {
  922. Rune *a, *r, *flag, *file;
  923. int na, nf;
  924. char *aa;
  925. Reffont *newfont;
  926. Dirlist *dp;
  927. int i, fix;
  928. if(et==nil || et->w==nil)
  929. return;
  930. t = &et->w->body;
  931. flag = nil;
  932. file = nil;
  933. /* loop condition: *arg is not a blank */
  934. nf = 0;
  935. for(;;){
  936. a = findbl(arg, narg, &na);
  937. if(a == arg)
  938. break;
  939. r = runemalloc(narg-na+1);
  940. runemove(r, arg, narg-na);
  941. if(runeeq(r, narg-na, (Rune*)L"fix", 3) || runeeq(r, narg-na, (Rune*)L"var", 3)){
  942. free(flag);
  943. flag = r;
  944. }else{
  945. free(file);
  946. file = r;
  947. nf = narg-na;
  948. }
  949. arg = skipbl(a, na, &narg);
  950. }
  951. getarg(argt, FALSE, TRUE, &r, &na);
  952. if(r) {
  953. if(runeeq(r, na, (Rune*)L"fix", 3) || runeeq(r, na, (Rune*)L"var", 3)){
  954. free(flag);
  955. flag = r;
  956. }else{
  957. free(file);
  958. file = r;
  959. nf = na;
  960. }
  961. }
  962. fix = 1;
  963. if(flag)
  964. fix = runeeq(flag, runestrlen(flag), (Rune*)L"fix", 3);
  965. else if(file == nil){
  966. newfont = rfget(FALSE, FALSE, FALSE, nil);
  967. if(newfont)
  968. fix = strcmp(newfont->f->name, t->Frame.font->name)==0;
  969. }
  970. if(file){
  971. aa = runetobyte(file, nf);
  972. newfont = rfget(fix, flag!=nil, FALSE, aa);
  973. free(aa);
  974. }else
  975. newfont = rfget(fix, FALSE, FALSE, nil);
  976. if(newfont){
  977. draw(screen, t->w->r, textcols[BACK], nil, ZP);
  978. rfclose(t->reffont);
  979. t->reffont = newfont;
  980. t->Frame.font = newfont->f;
  981. frinittick(&t->Frame);
  982. if(t->w->isdir){
  983. t->all.min.x++; /* force recolumnation; disgusting! */
  984. for(i=0; i<t->w->ndl; i++){
  985. dp = t->w->dlp[i];
  986. aa = runetobyte(dp->r, dp->nr);
  987. dp->wid = stringwidth(newfont->f, aa);
  988. free(aa);
  989. }
  990. }
  991. /* avoid shrinking of window due to quantization */
  992. colgrow(t->w->col, t->w, -1);
  993. }
  994. free(file);
  995. free(flag);
  996. }
  997. void
  998. incl(Text *et, Text*ta, Text *argt, int b, int c, Rune *arg, int narg)
  999. {
  1000. Rune *a, *r;
  1001. Window *w;
  1002. int na, n, len;
  1003. if(et==nil || et->w==nil)
  1004. return;
  1005. w = et->w;
  1006. n = 0;
  1007. getarg(argt, FALSE, TRUE, &r, &len);
  1008. if(r){
  1009. n++;
  1010. winaddincl(w, r, len);
  1011. }
  1012. /* loop condition: *arg is not a blank */
  1013. for(;;){
  1014. a = findbl(arg, narg, &na);
  1015. if(a == arg)
  1016. break;
  1017. r = runemalloc(narg-na+1);
  1018. runemove(r, arg, narg-na);
  1019. n++;
  1020. winaddincl(w, r, narg-na);
  1021. arg = skipbl(a, na, &narg);
  1022. }
  1023. if(n==0 && w->nincl){
  1024. for(n=w->nincl; --n>=0; )
  1025. warning(nil, "%S ", w->incl[n]);
  1026. warning(nil, "\n");
  1027. }
  1028. }
  1029. enum {
  1030. IGlobal = -2,
  1031. IError = -1,
  1032. Ion = 0,
  1033. Ioff = 1,
  1034. };
  1035. static int
  1036. indentval(Rune *s, int n)
  1037. {
  1038. if(n < 2)
  1039. return IError;
  1040. if(runestrncmp(s, (Rune*)L"ON", n) == 0){
  1041. globalautoindent = TRUE;
  1042. warning(nil, "Indent ON\n");
  1043. return IGlobal;
  1044. }
  1045. if(runestrncmp(s, (Rune*)L"OFF", n) == 0){
  1046. globalautoindent = FALSE;
  1047. warning(nil, "Indent OFF\n");
  1048. return IGlobal;
  1049. }
  1050. return runestrncmp(s, (Rune*)L"on", n) == 0;
  1051. }
  1052. static void
  1053. fixindent(Window *w, void*v)
  1054. {
  1055. w->autoindent = globalautoindent;
  1056. }
  1057. void
  1058. indent(Text *et, Text*ta, Text *argt, int b, int c, Rune *arg, int narg)
  1059. {
  1060. Rune *a, *r;
  1061. Window *w;
  1062. int na, len, autoindent;
  1063. w = nil;
  1064. if(et!=nil && et->w!=nil)
  1065. w = et->w;
  1066. autoindent = IError;
  1067. getarg(argt, FALSE, TRUE, &r, &len);
  1068. if(r!=nil && len>0)
  1069. autoindent = indentval(r, len);
  1070. else{
  1071. a = findbl(arg, narg, &na);
  1072. if(a != arg)
  1073. autoindent = indentval(arg, narg-na);
  1074. }
  1075. if(autoindent == IGlobal)
  1076. allwindows(fixindent, nil);
  1077. else if(w != nil && autoindent >= 0)
  1078. w->autoindent = autoindent;
  1079. }
  1080. void
  1081. tab(Text *et, Text*ta, Text *argt, int b, int c, Rune *arg, int narg)
  1082. {
  1083. Rune *a, *r;
  1084. Window *w;
  1085. int na, len, tab;
  1086. char *p;
  1087. if(et==nil || et->w==nil)
  1088. return;
  1089. w = et->w;
  1090. getarg(argt, FALSE, TRUE, &r, &len);
  1091. tab = 0;
  1092. if(r!=nil && len>0){
  1093. p = runetobyte(r, len);
  1094. if('0'<=p[0] && p[0]<='9')
  1095. tab = atoi(p);
  1096. free(p);
  1097. }else{
  1098. a = findbl(arg, narg, &na);
  1099. if(a != arg){
  1100. p = runetobyte(arg, narg-na);
  1101. if('0'<=p[0] && p[0]<='9')
  1102. tab = atoi(p);
  1103. free(p);
  1104. }
  1105. }
  1106. if(tab > 0){
  1107. if(w->body.tabstop != tab){
  1108. w->body.tabstop = tab;
  1109. winresize(w, w->r, 1);
  1110. }
  1111. }else
  1112. warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
  1113. }
  1114. void
  1115. runproc(void *argvp)
  1116. {
  1117. /* args: */
  1118. Window *win;
  1119. char *s;
  1120. Rune *rdir;
  1121. int ndir;
  1122. int newns;
  1123. char *argaddr;
  1124. char *arg;
  1125. Command *c;
  1126. Channel *cpid;
  1127. int iseditcmd;
  1128. /* end of args */
  1129. char *e, *t, *name, *filename, *dir, **av, *news;
  1130. Rune r, **incl;
  1131. int ac, w, inarg, i, n, fd, nincl, winid;
  1132. int pipechar;
  1133. char buf[512];
  1134. // static void *parg[2];
  1135. void **argv;
  1136. argv = argvp;
  1137. win = argv[0];
  1138. s = argv[1];
  1139. rdir = argv[2];
  1140. ndir = (uintptr)argv[3];
  1141. newns = (uintptr)argv[4];
  1142. argaddr = argv[5];
  1143. arg = argv[6];
  1144. c = argv[7];
  1145. cpid = argv[8];
  1146. iseditcmd = (uintptr)argv[9];
  1147. free(argv);
  1148. t = s;
  1149. while(*t==' ' || *t=='\n' || *t=='\t')
  1150. t++;
  1151. for(e=t; *e; e++)
  1152. if(*e==' ' || *e=='\n' || *e=='\t' )
  1153. break;
  1154. name = emalloc((e-t)+2);
  1155. memmove(name, t, e-t);
  1156. name[e-t] = 0;
  1157. e = utfrrune(name, '/');
  1158. if(e)
  1159. memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
  1160. strcat(name, " "); /* add blank here for ease in waittask */
  1161. c->name = bytetorune(name, &c->nname);
  1162. free(name);
  1163. pipechar = 0;
  1164. if(*t=='<' || *t=='|' || *t=='>')
  1165. pipechar = *t++;
  1166. c->iseditcmd = iseditcmd;
  1167. c->text = s;
  1168. if(rdir != nil){
  1169. dir = runetobyte(rdir, ndir);
  1170. chdir(dir); /* ignore error: probably app. window */
  1171. free(dir);
  1172. }
  1173. if(newns){
  1174. nincl = 0;
  1175. incl = nil;
  1176. if(win){
  1177. filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
  1178. nincl = win->nincl;
  1179. if(nincl > 0){
  1180. incl = emalloc(nincl*sizeof(Rune*));
  1181. for(i=0; i<nincl; i++){
  1182. n = runestrlen(win->incl[i]);
  1183. incl[i] = runemalloc(n+1);
  1184. runemove(incl[i], win->incl[i], n);
  1185. }
  1186. }
  1187. winid = win->id;
  1188. }else{
  1189. filename = nil;
  1190. winid = 0;
  1191. if(activewin)
  1192. winid = activewin->id;
  1193. }
  1194. rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
  1195. sprint(buf, "%d", winid);
  1196. putenv("winid", buf);
  1197. if(filename){
  1198. putenv("%", filename);
  1199. free(filename);
  1200. }
  1201. c->md = fsysmount(rdir, ndir, incl, nincl);
  1202. if(c->md == nil){
  1203. fprint(2, "child: can't mount /dev/cons: %r\n");
  1204. threadexits("mount");
  1205. }
  1206. close(0);
  1207. if(winid>0 && (pipechar=='|' || pipechar=='>')){
  1208. sprint(buf, "/mnt/acme/%d/rdsel", winid);
  1209. open(buf, OREAD);
  1210. }else
  1211. open("/dev/null", OREAD);
  1212. close(1);
  1213. if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
  1214. if(iseditcmd){
  1215. if(winid > 0)
  1216. sprint(buf, "/mnt/acme/%d/editout", winid);
  1217. else
  1218. sprint(buf, "/mnt/acme/editout");
  1219. }else
  1220. sprint(buf, "/mnt/acme/%d/wrsel", winid);
  1221. open(buf, OWRITE);
  1222. close(2);
  1223. open("/dev/cons", OWRITE);
  1224. }else{
  1225. open("/dev/cons", OWRITE);
  1226. dup(1, 2);
  1227. }
  1228. }else{
  1229. rfork(RFFDG|RFNOTEG);
  1230. fsysclose();
  1231. close(0);
  1232. open("/dev/null", OREAD);
  1233. close(1);
  1234. open(acmeerrorfile, OWRITE);
  1235. dup(1, 2);
  1236. }
  1237. if(win)
  1238. winclose(win);
  1239. if(argaddr)
  1240. putenv("acmeaddr", argaddr);
  1241. if(strlen(t) > sizeof buf-10) /* may need to print into stack */
  1242. goto Hard;
  1243. inarg = FALSE;
  1244. for(e=t; *e; e+=w){
  1245. w = chartorune(&r, e);
  1246. if(r==' ' || r=='\t')
  1247. continue;
  1248. if(r < ' ')
  1249. goto Hard;
  1250. if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
  1251. goto Hard;
  1252. inarg = TRUE;
  1253. }
  1254. if(!inarg)
  1255. goto Fail;
  1256. ac = 0;
  1257. av = nil;
  1258. inarg = FALSE;
  1259. for(e=t; *e; e+=w){
  1260. w = chartorune(&r, e);
  1261. if(r==' ' || r=='\t'){
  1262. inarg = FALSE;
  1263. *e = 0;
  1264. continue;
  1265. }
  1266. if(!inarg){
  1267. inarg = TRUE;
  1268. av = realloc(av, (ac+1)*sizeof(char**));
  1269. av[ac++] = e;
  1270. }
  1271. }
  1272. av = realloc(av, (ac+2)*sizeof(char**));
  1273. av[ac++] = arg;
  1274. av[ac] = nil;
  1275. c->av = av;
  1276. procexec(cpid, av[0], av);
  1277. e = av[0];
  1278. if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
  1279. goto Fail;
  1280. if(cputype){
  1281. sprint(buf, "%s/%s", cputype, av[0]);
  1282. procexec(cpid, buf, av);
  1283. }
  1284. sprint(buf, "/bin/%s", av[0]);
  1285. procexec(cpid, buf, av);
  1286. goto Fail;
  1287. Hard:
  1288. /*
  1289. * ugly: set path = (. $cputype /bin)
  1290. * should honor $path if unusual.
  1291. */
  1292. if(cputype){
  1293. n = 0;
  1294. memmove(buf+n, ".", 2);
  1295. n += 2;
  1296. i = strlen(cputype)+1;
  1297. memmove(buf+n, cputype, i);
  1298. n += i;
  1299. memmove(buf+n, "/bin", 5);
  1300. n += 5;
  1301. fd = create("/env/path", OWRITE, 0666);
  1302. write(fd, buf, n);
  1303. close(fd);
  1304. }
  1305. if(arg){
  1306. news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
  1307. if(news){
  1308. sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
  1309. free(s);
  1310. t = news;
  1311. c->text = news;
  1312. }
  1313. }
  1314. procexecl(cpid, "/bin/rc", "rc", "-c", t, nil);
  1315. Fail:
  1316. /* procexec hasn't happened, so send a zero */
  1317. sendul(cpid, 0);
  1318. threadexits(nil);
  1319. }
  1320. void
  1321. runwaittask(void *v)
  1322. {
  1323. Command *c;
  1324. Channel *cpid;
  1325. void **a;
  1326. threadsetname("runwaittask");
  1327. a = v;
  1328. c = a[0];
  1329. cpid = a[1];
  1330. free(a);
  1331. do
  1332. c->pid = recvul(cpid);
  1333. while(c->pid == ~0);
  1334. free(c->av);
  1335. if(c->pid != 0) /* successful exec */
  1336. sendp(ccommand, c);
  1337. else{
  1338. if(c->iseditcmd)
  1339. sendul(cedit, 0);
  1340. free(c->name);
  1341. free(c->text);
  1342. free(c);
  1343. }
  1344. chanfree(cpid);
  1345. }
  1346. void
  1347. run(Window *win, char *s, Rune *rdir, int ndir, int newns,
  1348. char *argaddr, char *xarg, int iseditcmd)
  1349. {
  1350. void **arg;
  1351. Command *c;
  1352. Channel *cpid;
  1353. if(s == nil)
  1354. return;
  1355. arg = emalloc(10*sizeof(void*));
  1356. c = emalloc(sizeof *c);
  1357. cpid = chancreate(sizeof(uint32_t), 0);
  1358. arg[0] = win;
  1359. arg[1] = s;
  1360. arg[2] = rdir;
  1361. arg[3] = (void*)(uintptr_t)ndir;
  1362. arg[4] = (void*)(uintptr_t)newns;
  1363. arg[5] = argaddr;
  1364. arg[6] = xarg;
  1365. arg[7] = c;
  1366. arg[8] = cpid;
  1367. arg[9] = (void*)(uintptr_t)iseditcmd;
  1368. proccreate(runproc, arg, STACK);
  1369. /* mustn't block here because must be ready to answer mount() call in run() */
  1370. arg = emalloc(2*sizeof(void*));
  1371. arg[0] = c;
  1372. arg[1] = cpid;
  1373. threadcreate(runwaittask, arg, STACK);
  1374. }