exec.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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, '/'))
  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, '/'))
  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. free(a);
  876. }
  877. }
  878. }
  879. void
  880. id(Text*et, Text*a, Text*b, int c, int d, Rune*e, int f)
  881. {
  882. if(et && et->w)
  883. warning(nil, "/mnt/acme/%d/\n", et->w->id);
  884. }
  885. void
  886. local(Text *et, Text*ta, Text*argt, int b, int c, Rune*arg, int narg)
  887. {
  888. char *a, *aa;
  889. Runestr dir;
  890. aa = getbytearg(argt, TRUE, TRUE, &a);
  891. dir = dirname(et, nil, 0);
  892. if(dir.nr==1 && dir.r[0]=='.'){ /* sigh */
  893. free(dir.r);
  894. dir.r = nil;
  895. dir.nr = 0;
  896. }
  897. run(nil, runetobyte(arg, narg), dir.r, dir.nr, FALSE, aa, a, FALSE);
  898. }
  899. void
  900. kill(Text *ta, Text*b, Text *argt, int c, int d, Rune *arg, int narg)
  901. {
  902. Rune *a, *cmd, *r;
  903. int na;
  904. getarg(argt, FALSE, FALSE, &r, &na);
  905. if(r)
  906. kill(nil, nil, nil, 0, 0, r, na);
  907. /* loop condition: *arg is not a blank */
  908. for(;;){
  909. a = findbl(arg, narg, &na);
  910. if(a == arg)
  911. break;
  912. cmd = runemalloc(narg-na+1);
  913. runemove(cmd, arg, narg-na);
  914. sendp(ckill, cmd);
  915. arg = skipbl(a, na, &narg);
  916. }
  917. }
  918. void
  919. fontx(Text *et, Text *t, Text *argt, int ia, int b, Rune *arg, int narg)
  920. {
  921. Rune *a, *r, *flag, *file;
  922. int na, nf;
  923. char *aa;
  924. Reffont *newfont;
  925. Dirlist *dp;
  926. int i, fix;
  927. if(et==nil || et->w==nil)
  928. return;
  929. t = &et->w->body;
  930. flag = nil;
  931. file = nil;
  932. /* loop condition: *arg is not a blank */
  933. nf = 0;
  934. for(;;){
  935. a = findbl(arg, narg, &na);
  936. if(a == arg)
  937. break;
  938. r = runemalloc(narg-na+1);
  939. runemove(r, arg, narg-na);
  940. if(runeeq(r, narg-na, (Rune *)L"fix", 3) || runeeq(r, narg-na, (Rune *)L"var", 3)){
  941. free(flag);
  942. flag = r;
  943. }else{
  944. free(file);
  945. file = r;
  946. nf = narg-na;
  947. }
  948. arg = skipbl(a, na, &narg);
  949. }
  950. getarg(argt, FALSE, TRUE, &r, &na);
  951. if(r)
  952. if(runeeq(r, na, (Rune *)L"fix", 3) || runeeq(r, na, (Rune *)L"var", 3)){
  953. free(flag);
  954. flag = r;
  955. }else{
  956. free(file);
  957. file = r;
  958. nf = na;
  959. }
  960. fix = 1;
  961. if(flag)
  962. fix = runeeq(flag, runestrlen(flag), (Rune *)L"fix", 3);
  963. else if(file == nil){
  964. newfont = rfget(FALSE, FALSE, FALSE, nil);
  965. if(newfont)
  966. fix = strcmp(newfont->f->name, t->Frame.font->name)==0;
  967. }
  968. if(file){
  969. aa = runetobyte(file, nf);
  970. newfont = rfget(fix, flag!=nil, FALSE, aa);
  971. free(aa);
  972. }else
  973. newfont = rfget(fix, FALSE, FALSE, nil);
  974. if(newfont){
  975. draw(screen, t->w->r, textcols[BACK], nil, ZP);
  976. rfclose(t->reffont);
  977. t->reffont = newfont;
  978. t->Frame.font = newfont->f;
  979. frinittick(&t->Frame);
  980. if(t->w->isdir){
  981. t->all.min.x++; /* force recolumnation; disgusting! */
  982. for(i=0; i<t->w->ndl; i++){
  983. dp = t->w->dlp[i];
  984. aa = runetobyte(dp->r, dp->nr);
  985. dp->wid = stringwidth(newfont->f, aa);
  986. free(aa);
  987. }
  988. }
  989. /* avoid shrinking of window due to quantization */
  990. colgrow(t->w->col, t->w, -1);
  991. }
  992. free(file);
  993. free(flag);
  994. }
  995. void
  996. incl(Text *et, Text*ta, Text *argt, int b, int c, Rune *arg, int narg)
  997. {
  998. Rune *a, *r;
  999. Window *w;
  1000. int na, n, len;
  1001. if(et==nil || et->w==nil)
  1002. return;
  1003. w = et->w;
  1004. n = 0;
  1005. getarg(argt, FALSE, TRUE, &r, &len);
  1006. if(r){
  1007. n++;
  1008. winaddincl(w, r, len);
  1009. }
  1010. /* loop condition: *arg is not a blank */
  1011. for(;;){
  1012. a = findbl(arg, narg, &na);
  1013. if(a == arg)
  1014. break;
  1015. r = runemalloc(narg-na+1);
  1016. runemove(r, arg, narg-na);
  1017. n++;
  1018. winaddincl(w, r, narg-na);
  1019. arg = skipbl(a, na, &narg);
  1020. }
  1021. if(n==0 && w->nincl){
  1022. for(n=w->nincl; --n>=0; )
  1023. warning(nil, "%S ", w->incl[n]);
  1024. warning(nil, "\n");
  1025. }
  1026. }
  1027. enum {
  1028. IGlobal = -2,
  1029. IError = -1,
  1030. Ion = 0,
  1031. Ioff = 1,
  1032. };
  1033. static int
  1034. indentval(Rune *s, int n)
  1035. {
  1036. if(n < 2)
  1037. return IError;
  1038. if(runestrncmp(s, (Rune *)L"ON", n) == 0){
  1039. globalautoindent = TRUE;
  1040. warning(nil, "Indent ON\n");
  1041. return IGlobal;
  1042. }
  1043. if(runestrncmp(s, (Rune *)L"OFF", n) == 0){
  1044. globalautoindent = FALSE;
  1045. warning(nil, "Indent OFF\n");
  1046. return IGlobal;
  1047. }
  1048. return runestrncmp(s, (Rune *)L"on", n) == 0;
  1049. }
  1050. static void
  1051. fixindent(Window *w, void*v)
  1052. {
  1053. w->autoindent = globalautoindent;
  1054. }
  1055. void
  1056. indent(Text *et, Text*ta, Text *argt, int b, int c, Rune *arg, int narg)
  1057. {
  1058. Rune *a, *r;
  1059. Window *w;
  1060. int na, len, autoindent;
  1061. w = nil;
  1062. if(et!=nil && et->w!=nil)
  1063. w = et->w;
  1064. autoindent = IError;
  1065. getarg(argt, FALSE, TRUE, &r, &len);
  1066. if(r!=nil && len>0)
  1067. autoindent = indentval(r, len);
  1068. else{
  1069. a = findbl(arg, narg, &na);
  1070. if(a != arg)
  1071. autoindent = indentval(arg, narg-na);
  1072. }
  1073. if(autoindent == IGlobal)
  1074. allwindows(fixindent, nil);
  1075. else if(w != nil && autoindent >= 0)
  1076. w->autoindent = autoindent;
  1077. }
  1078. void
  1079. tab(Text *et, Text*ta, Text *argt, int b, int c, Rune *arg, int narg)
  1080. {
  1081. Rune *a, *r;
  1082. Window *w;
  1083. int na, len, tab;
  1084. char *p;
  1085. if(et==nil || et->w==nil)
  1086. return;
  1087. w = et->w;
  1088. getarg(argt, FALSE, TRUE, &r, &len);
  1089. tab = 0;
  1090. if(r!=nil && len>0){
  1091. p = runetobyte(r, len);
  1092. if('0'<=p[0] && p[0]<='9')
  1093. tab = atoi(p);
  1094. free(p);
  1095. }else{
  1096. a = findbl(arg, narg, &na);
  1097. if(a != arg){
  1098. p = runetobyte(arg, narg-na);
  1099. if('0'<=p[0] && p[0]<='9')
  1100. tab = atoi(p);
  1101. free(p);
  1102. }
  1103. }
  1104. if(tab > 0){
  1105. if(w->body.tabstop != tab){
  1106. w->body.tabstop = tab;
  1107. winresize(w, w->r, 1);
  1108. }
  1109. }else
  1110. warning(nil, "%.*S: Tab %d\n", w->body.file->nname, w->body.file->name, w->body.tabstop);
  1111. }
  1112. void
  1113. runproc(void *argvp)
  1114. {
  1115. /* args: */
  1116. Window *win;
  1117. char *s;
  1118. Rune *rdir;
  1119. int ndir;
  1120. int newns;
  1121. char *argaddr;
  1122. char *arg;
  1123. Command *c;
  1124. Channel *cpid;
  1125. int iseditcmd;
  1126. /* end of args */
  1127. char *e, *t, *name, *filename, *dir, **av, *news;
  1128. Rune r, **incl;
  1129. int ac, w, inarg, i, n, fd, nincl, winid;
  1130. int pipechar;
  1131. char buf[512];
  1132. // static void *parg[2];
  1133. void **argv;
  1134. argv = argvp;
  1135. win = argv[0];
  1136. s = argv[1];
  1137. rdir = argv[2];
  1138. ndir = (uintptr)argv[3];
  1139. newns = (uintptr)argv[4];
  1140. argaddr = argv[5];
  1141. arg = argv[6];
  1142. c = argv[7];
  1143. cpid = argv[8];
  1144. iseditcmd = (uintptr)argv[9];
  1145. free(argv);
  1146. t = s;
  1147. while(*t==' ' || *t=='\n' || *t=='\t')
  1148. t++;
  1149. for(e=t; *e; e++)
  1150. if(*e==' ' || *e=='\n' || *e=='\t' )
  1151. break;
  1152. name = emalloc((e-t)+2);
  1153. memmove(name, t, e-t);
  1154. name[e-t] = 0;
  1155. e = utfrrune(name, '/');
  1156. if(e)
  1157. memmove(name, e+1, strlen(e+1)+1); /* strcpy but overlaps */
  1158. strcat(name, " "); /* add blank here for ease in waittask */
  1159. c->name = bytetorune(name, &c->nname);
  1160. free(name);
  1161. pipechar = 0;
  1162. if(*t=='<' || *t=='|' || *t=='>')
  1163. pipechar = *t++;
  1164. c->iseditcmd = iseditcmd;
  1165. c->text = s;
  1166. if(rdir != nil){
  1167. dir = runetobyte(rdir, ndir);
  1168. chdir(dir); /* ignore error: probably app. window */
  1169. free(dir);
  1170. }
  1171. if(newns){
  1172. nincl = 0;
  1173. incl = nil;
  1174. if(win){
  1175. filename = smprint("%.*S", win->body.file->nname, win->body.file->name);
  1176. nincl = win->nincl;
  1177. if(nincl > 0){
  1178. incl = emalloc(nincl*sizeof(Rune*));
  1179. for(i=0; i<nincl; i++){
  1180. n = runestrlen(win->incl[i]);
  1181. incl[i] = runemalloc(n+1);
  1182. runemove(incl[i], win->incl[i], n);
  1183. }
  1184. }
  1185. winid = win->id;
  1186. }else{
  1187. filename = nil;
  1188. winid = 0;
  1189. if(activewin)
  1190. winid = activewin->id;
  1191. }
  1192. rfork(RFNAMEG|RFENVG|RFFDG|RFNOTEG);
  1193. sprint(buf, "%d", winid);
  1194. putenv("winid", buf);
  1195. if(filename){
  1196. putenv("%", filename);
  1197. free(filename);
  1198. }
  1199. c->md = fsysmount(rdir, ndir, incl, nincl);
  1200. if(c->md == nil){
  1201. fprint(2, "child: can't mount /dev/cons: %r\n");
  1202. threadexits("mount");
  1203. }
  1204. close(0);
  1205. if(winid>0 && (pipechar=='|' || pipechar=='>')){
  1206. sprint(buf, "/mnt/acme/%d/rdsel", winid);
  1207. open(buf, OREAD);
  1208. }else
  1209. open("/dev/null", OREAD);
  1210. close(1);
  1211. if((winid>0 || iseditcmd) && (pipechar=='|' || pipechar=='<')){
  1212. if(iseditcmd){
  1213. if(winid > 0)
  1214. sprint(buf, "/mnt/acme/%d/editout", winid);
  1215. else
  1216. sprint(buf, "/mnt/acme/editout");
  1217. }else
  1218. sprint(buf, "/mnt/acme/%d/wrsel", winid);
  1219. open(buf, OWRITE);
  1220. close(2);
  1221. open("/dev/cons", OWRITE);
  1222. }else{
  1223. open("/dev/cons", OWRITE);
  1224. dup(1, 2);
  1225. }
  1226. }else{
  1227. rfork(RFFDG|RFNOTEG);
  1228. fsysclose();
  1229. close(0);
  1230. open("/dev/null", OREAD);
  1231. close(1);
  1232. open(acmeerrorfile, OWRITE);
  1233. dup(1, 2);
  1234. }
  1235. if(win)
  1236. winclose(win);
  1237. if(argaddr)
  1238. putenv("acmeaddr", argaddr);
  1239. if(strlen(t) > sizeof buf-10) /* may need to print into stack */
  1240. goto Hard;
  1241. inarg = FALSE;
  1242. for(e=t; *e; e+=w){
  1243. w = chartorune(&r, e);
  1244. if(r==' ' || r=='\t')
  1245. continue;
  1246. if(r < ' ')
  1247. goto Hard;
  1248. if(utfrune("#;&|^$=`'{}()<>[]*?^~`", r))
  1249. goto Hard;
  1250. inarg = TRUE;
  1251. }
  1252. if(!inarg)
  1253. goto Fail;
  1254. ac = 0;
  1255. av = nil;
  1256. inarg = FALSE;
  1257. for(e=t; *e; e+=w){
  1258. w = chartorune(&r, e);
  1259. if(r==' ' || r=='\t'){
  1260. inarg = FALSE;
  1261. *e = 0;
  1262. continue;
  1263. }
  1264. if(!inarg){
  1265. inarg = TRUE;
  1266. av = realloc(av, (ac+1)*sizeof(char**));
  1267. av[ac++] = e;
  1268. }
  1269. }
  1270. av = realloc(av, (ac+2)*sizeof(char**));
  1271. av[ac++] = arg;
  1272. av[ac] = nil;
  1273. c->av = av;
  1274. procexec(cpid, av[0], av);
  1275. e = av[0];
  1276. if(e[0]=='/' || (e[0]=='.' && e[1]=='/'))
  1277. goto Fail;
  1278. if(cputype){
  1279. sprint(buf, "%s/%s", cputype, av[0]);
  1280. procexec(cpid, buf, av);
  1281. }
  1282. sprint(buf, "/bin/%s", av[0]);
  1283. procexec(cpid, buf, av);
  1284. goto Fail;
  1285. Hard:
  1286. /*
  1287. * ugly: set path = (. $cputype /bin)
  1288. * should honor $path if unusual.
  1289. */
  1290. if(cputype){
  1291. n = 0;
  1292. memmove(buf+n, ".", 2);
  1293. n += 2;
  1294. i = strlen(cputype)+1;
  1295. memmove(buf+n, cputype, i);
  1296. n += i;
  1297. memmove(buf+n, "/bin", 5);
  1298. n += 5;
  1299. fd = create("/env/path", OWRITE, 0666);
  1300. write(fd, buf, n);
  1301. close(fd);
  1302. }
  1303. if(arg){
  1304. news = emalloc(strlen(t) + 1 + 1 + strlen(arg) + 1 + 1);
  1305. if(news){
  1306. sprint(news, "%s '%s'", t, arg); /* BUG: what if quote in arg? */
  1307. free(s);
  1308. t = news;
  1309. c->text = news;
  1310. }
  1311. }
  1312. procexecl(cpid, "/bin/rc", "rc", "-c", t, nil);
  1313. Fail:
  1314. /* procexec hasn't happened, so send a zero */
  1315. sendul(cpid, 0);
  1316. threadexits(nil);
  1317. }
  1318. void
  1319. runwaittask(void *v)
  1320. {
  1321. Command *c;
  1322. Channel *cpid;
  1323. void **a;
  1324. threadsetname("runwaittask");
  1325. a = v;
  1326. c = a[0];
  1327. cpid = a[1];
  1328. free(a);
  1329. do
  1330. c->pid = recvul(cpid);
  1331. while(c->pid == ~0);
  1332. free(c->av);
  1333. if(c->pid != 0) /* successful exec */
  1334. sendp(ccommand, c);
  1335. else{
  1336. if(c->iseditcmd)
  1337. sendul(cedit, 0);
  1338. free(c->name);
  1339. free(c->text);
  1340. free(c);
  1341. }
  1342. chanfree(cpid);
  1343. }
  1344. void
  1345. run(Window *win, char *s, Rune *rdir, int ndir, int newns,
  1346. char *argaddr, char *xarg, int iseditcmd)
  1347. {
  1348. void **arg;
  1349. Command *c;
  1350. Channel *cpid;
  1351. if(s == nil)
  1352. return;
  1353. arg = emalloc(10*sizeof(void*));
  1354. c = emalloc(sizeof *c);
  1355. cpid = chancreate(sizeof(uint32_t), 0);
  1356. arg[0] = win;
  1357. arg[1] = s;
  1358. arg[2] = rdir;
  1359. arg[3] = (void*)(uintptr_t)ndir;
  1360. arg[4] = (void*)(uintptr_t)newns;
  1361. arg[5] = argaddr;
  1362. arg[6] = xarg;
  1363. arg[7] = c;
  1364. arg[8] = cpid;
  1365. arg[9] = (void*)(uintptr_t)iseditcmd;
  1366. proccreate(runproc, arg, STACK);
  1367. /* mustn't block here because must be ready to answer mount() call in run() */
  1368. arg = emalloc(2*sizeof(void*));
  1369. arg[0] = c;
  1370. arg[1] = cpid;
  1371. threadcreate(runwaittask, arg, STACK);
  1372. }