Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include <plumb.h>
11 #include "dat.h"
12 #include "fns.h"
14 int winid;
16 void
17 wininit(Window *w, Window *clone, Rectangle r)
18 {
19 Rectangle r1, br;
20 File *f;
21 Reffont *rf;
22 Rune *rp;
23 int nc;
25 w->tag.w = w;
26 w->taglines = 1;
27 w->tagexpand = TRUE;
28 w->body.w = w;
29 w->id = ++winid;
30 incref(&w->ref);
31 if(globalincref)
32 incref(&w->ref);
33 w->ctlfid = ~0;
34 w->utflastqid = -1;
35 r1 = r;
37 w->tagtop = r;
38 w->tagtop.max.y = r.min.y + font->height;
40 r1.max.y = r1.min.y + w->taglines*font->height;
41 incref(&reffont.ref);
42 f = fileaddtext(nil, &w->tag);
43 textinit(&w->tag, f, r1, &reffont, tagcols);
44 w->tag.what = Tag;
45 /* tag is a copy of the contents, not a tracked image */
46 if(clone){
47 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
48 nc = clone->tag.file->b.nc;
49 rp = runemalloc(nc);
50 bufread(&clone->tag.file->b, 0, rp, nc);
51 textinsert(&w->tag, 0, rp, nc, TRUE);
52 free(rp);
53 filereset(w->tag.file);
54 textsetselect(&w->tag, nc, nc);
55 }
56 /*assert(w->body.w == w); */
57 r1 = r;
58 r1.min.y += w->taglines*font->height + 1;
59 if(r1.max.y < r1.min.y)
60 r1.max.y = r1.min.y;
61 f = nil;
62 if(clone){
63 f = clone->body.file;
64 w->body.org = clone->body.org;
65 w->isscratch = clone->isscratch;
66 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
67 }else
68 rf = rfget(FALSE, FALSE, FALSE, nil);
69 /*assert(w->body.w == w); */
70 f = fileaddtext(f, &w->body);
71 w->body.what = Body;
72 textinit(&w->body, f, r1, rf, textcols);
73 r1.min.y -= 1;
74 r1.max.y = r1.min.y+1;
75 draw(screen, r1, tagcols[BORD], nil, ZP);
76 textscrdraw(&w->body);
77 w->r = r;
78 br.min = w->tag.scrollr.min;
79 br.max.x = br.min.x + Dx(button->r);
80 br.max.y = br.min.y + Dy(button->r);
81 draw(screen, br, button, nil, button->r.min);
82 w->filemenu = TRUE;
83 w->maxlines = w->body.fr.maxlines;
84 w->autoindent = globalautoindent;
85 /*assert(w->body.w == w); */
86 if(clone){
87 w->dirty = clone->dirty;
88 w->autoindent = clone->autoindent;
89 textsetselect(&w->body, clone->body.q0, clone->body.q1);
90 winsettag(w);
91 }
92 }
94 /*
95 * Compute number of tag lines required
96 * to display entire tag text.
97 */
98 int
99 wintaglines(Window *w, Rectangle r)
101 int n;
102 Rune rune;
104 /* TAG policy here */
106 if(!w->tagexpand)
107 return 1;
108 w->tag.fr.noredraw = 1;
109 textresize(&w->tag, r, TRUE);
110 w->tag.fr.noredraw = 0;
112 /* can't use more than we have */
113 if(w->tag.fr.nlines >= w->tag.fr.maxlines)
114 return w->tag.fr.maxlines;
116 /* if tag ends with \n, include empty line at end for typing */
117 n = w->tag.fr.nlines;
118 if(w->tag.file->b.nc > 0)
119 bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
120 if(rune == '\n')
121 n++;
122 if(n == 0)
123 n = 1;
124 return n;
127 int
128 winresize(Window *w, Rectangle r, int safe, int keepextra)
130 int oy, y, mouseintag, tagresized;
131 Image *b;
132 Point p;
133 Rectangle br, r1;
135 if(0) fprint(2, "winresize %d %R safe=%d keep=%d h=%d\n", w->id, r, safe, keepextra, font->height);
136 w->tagtop = r;
137 w->tagtop.max.y = r.min.y+font->height;
139 /*
140 * TAG If necessary, recompute the number of lines that should
141 * be in the tag.
142 */
143 r1 = r;
144 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
145 y = r1.max.y;
146 mouseintag = ptinrect(mouse->xy, w->tag.all);
147 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
149 w->taglines = wintaglines(w, r);
150 w->tagsafe = TRUE;
152 /* END TAG */
154 r1 = r;
155 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
156 y = r1.max.y;
157 tagresized = 0;
158 if(1 || !safe || !eqrect(w->tag.all, r1)){
159 tagresized = 1;
160 if(0) fprint(2, "resize tag %R => %R\n", w->tag.all, r1);
161 textresize(&w->tag, r1, TRUE);
162 if(0) fprint(2, "=> %R (%R)\n", w->tag.all, w->tag.fr.r);
163 y = w->tag.fr.r.max.y;
164 b = button;
165 if(w->body.file->mod && !w->isdir && !w->isscratch)
166 b = modbutton;
167 br.min = w->tag.scrollr.min;
168 br.max.x = br.min.x + Dx(b->r);
169 br.max.y = br.min.y + Dy(b->r);
170 draw(screen, br, b, nil, b->r.min);
171 /* TAG */
172 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
173 p = mouse->xy;
174 p.y = w->tag.all.max.y-3;
175 moveto(mousectl, p);
177 /* END TAG */
181 r1 = r;
182 r1.min.y = y;
183 if(tagresized || !safe || !eqrect(w->body.all, r1)){
184 oy = y;
185 if(0) fprint(2, "resizing body; safe=%d all=%R r1=%R\n", safe, w->body.all, r1);
186 if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
187 r1.min.y = y;
188 r1.max.y = y+1;
189 draw(screen, r1, tagcols[BORD], nil, ZP);
190 y++;
191 r1.min.y = min(y, r.max.y);
192 r1.max.y = r.max.y;
193 }else{
194 r1.min.y = y;
195 r1.max.y = y;
197 if(0) fprint(2, "resizing body; new r=%R; r1=%R\n", r, r1);
198 w->r = r;
199 w->r.max.y = textresize(&w->body, r1, keepextra);
200 if(0) fprint(2, "after textresize: body.all=%R\n", w->body.all);
201 textscrdraw(&w->body);
202 w->body.all.min.y = oy;
204 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
205 return w->r.max.y;
208 void
209 winlock1(Window *w, int owner)
211 incref(&w->ref);
212 qlock(&w->lk);
213 w->owner = owner;
216 void
217 winlock(Window *w, int owner)
219 int i;
220 File *f;
222 f = w->body.file;
223 for(i=0; i<f->ntext; i++)
224 winlock1(f->text[i]->w, owner);
227 void
228 winunlock(Window *w)
230 int i;
231 File *f;
233 /*
234 * subtle: loop runs backwards to avoid tripping over
235 * winclose indirectly editing f->text and freeing f
236 * on the last iteration of the loop.
237 */
238 f = w->body.file;
239 for(i=f->ntext-1; i>=0; i--){
240 w = f->text[i]->w;
241 w->owner = 0;
242 qunlock(&w->lk);
243 winclose(w);
247 void
248 winmousebut(Window *w)
250 moveto(mousectl, addpt(w->tag.scrollr.min,
251 divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
254 void
255 windirfree(Window *w)
257 int i;
258 Dirlist *dl;
260 if(w->isdir){
261 for(i=0; i<w->ndl; i++){
262 dl = w->dlp[i];
263 free(dl->r);
264 free(dl);
266 free(w->dlp);
268 w->dlp = nil;
269 w->ndl = 0;
272 void
273 winclose(Window *w)
275 int i;
277 if(decref(&w->ref) == 0){
278 windirfree(w);
279 textclose(&w->tag);
280 textclose(&w->body);
281 if(activewin == w)
282 activewin = nil;
283 for(i=0; i<w->nincl; i++)
284 free(w->incl[i]);
285 free(w->incl);
286 free(w->events);
287 free(w);
291 void
292 windelete(Window *w)
294 Xfid *x;
296 x = w->eventx;
297 if(x){
298 w->nevents = 0;
299 free(w->events);
300 w->events = nil;
301 w->eventx = nil;
302 sendp(x->c, nil); /* wake him up */
306 void
307 winundo(Window *w, int isundo)
309 Text *body;
310 int i;
311 File *f;
312 Window *v;
314 w->utflastqid = -1;
315 body = &w->body;
316 fileundo(body->file, isundo, &body->q0, &body->q1);
317 textshow(body, body->q0, body->q1, 1);
318 f = body->file;
319 for(i=0; i<f->ntext; i++){
320 v = f->text[i]->w;
321 v->dirty = (f->seq != v->putseq);
322 if(v != w){
323 v->body.q0 = v->body.fr.p0+v->body.org;
324 v->body.q1 = v->body.fr.p1+v->body.org;
327 winsettag(w);
330 void
331 winsetname(Window *w, Rune *name, int n)
333 Text *t;
334 Window *v;
335 int i;
336 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
337 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
338 t = &w->body;
339 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
340 return;
341 w->isscratch = FALSE;
342 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
343 w->isscratch = TRUE;
344 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
345 w->isscratch = TRUE;
346 filesetname(t->file, name, n);
347 for(i=0; i<t->file->ntext; i++){
348 v = t->file->text[i]->w;
349 winsettag(v);
350 v->isscratch = w->isscratch;
354 void
355 wintype(Window *w, Text *t, Rune r)
357 int i;
359 texttype(t, r);
360 if(t->what == Body)
361 for(i=0; i<t->file->ntext; i++)
362 textscrdraw(t->file->text[i]);
363 winsettag(w);
366 void
367 wincleartag(Window *w)
369 int i, n;
370 Rune *r;
372 /* w must be committed */
373 n = w->tag.file->b.nc;
374 r = runemalloc(n);
375 bufread(&w->tag.file->b, 0, r, n);
376 for(i=0; i<n; i++)
377 if(r[i]==' ' || r[i]=='\t')
378 break;
379 for(; i<n; i++)
380 if(r[i] == '|')
381 break;
382 if(i == n)
383 return;
384 i++;
385 textdelete(&w->tag, i, n, TRUE);
386 free(r);
387 w->tag.file->mod = FALSE;
388 if(w->tag.q0 > i)
389 w->tag.q0 = i;
390 if(w->tag.q1 > i)
391 w->tag.q1 = i;
392 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
395 void
396 winsettag1(Window *w)
398 int bar, dirty, i, j, k, n, ntagname, resize;
399 Rune *new, *old, *r, *tagname;
400 Image *b;
401 uint q0, q1;
402 Rectangle br;
403 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
404 'S', 'n', 'a', 'r', 'f', 0 };
405 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
406 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
407 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
408 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
409 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
410 static Rune Lpipe[] = { ' ', '|', 0 };
411 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
412 if(w->tag.ncache!=0 || w->tag.file->mod)
413 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
414 old = runemalloc(w->tag.file->b.nc+1);
415 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
416 old[w->tag.file->b.nc] = '\0';
417 for(i=0; i<w->tag.file->b.nc; i++)
418 if(old[i]==' ' || old[i]=='\t')
419 break;
421 /* make sure the file name is set correctly in the tag */
422 ntagname = w->body.file->nname;
423 tagname = runemalloc(ntagname);
424 runemove(tagname, w->body.file->name, ntagname);
425 abbrevenv(&tagname, (uint*)&ntagname);
427 /*
428 * XXX Why is this here instead of letting the code
429 * down below do the work?
430 */
431 if(runeeq(old, i, tagname, ntagname) == FALSE){
432 q0 = w->tag.q0;
433 q1 = w->tag.q1;
434 textdelete(&w->tag, 0, i, TRUE);
435 textinsert(&w->tag, 0, tagname, ntagname, TRUE);
436 free(old);
437 old = runemalloc(w->tag.file->b.nc+1);
438 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
439 old[w->tag.file->b.nc] = '\0';
440 if(q0 >= i){
441 /*
442 * XXX common case - typing at end of name
443 */
444 w->tag.q0 = q0+ntagname-i;
445 w->tag.q1 = q1+ntagname-i;
449 /* compute the text for the whole tag, replacing current only if it differs */
450 new = runemalloc(ntagname+100);
451 i = 0;
452 runemove(new+i, tagname, ntagname);
453 i += ntagname;
454 runemove(new+i, Ldelsnarf, 10);
455 i += 10;
456 if(w->filemenu){
457 if(w->body.needundo || w->body.file->delta.nc>0 || w->body.ncache){
458 runemove(new+i, Lundo, 5);
459 i += 5;
461 if(w->body.file->epsilon.nc > 0){
462 runemove(new+i, Lredo, 5);
463 i += 5;
465 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
466 if(!w->isdir && dirty){
467 runemove(new+i, Lput, 4);
468 i += 4;
471 if(w->isdir){
472 runemove(new+i, Lget, 4);
473 i += 4;
475 runemove(new+i, Lpipe, 2);
476 i += 2;
477 r = runestrchr(old, '|');
478 if(r)
479 k = r-old+1;
480 else{
481 k = w->tag.file->b.nc;
482 if(w->body.file->seq == 0){
483 runemove(new+i, Llook, 6);
484 i += 6;
487 new[i] = 0;
488 if(runestrlen(new) != i)
489 fprint(2, "s '%S' len not %d\n", new, i);
490 assert(i==runestrlen(new));
492 /* replace tag if the new one is different */
493 resize = 0;
494 if(runeeq(new, i, old, k) == FALSE){
495 resize = 1;
496 n = k;
497 if(n > i)
498 n = i;
499 for(j=0; j<n; j++)
500 if(old[j] != new[j])
501 break;
502 q0 = w->tag.q0;
503 q1 = w->tag.q1;
504 textdelete(&w->tag, j, k, TRUE);
505 textinsert(&w->tag, j, new+j, i-j, TRUE);
506 /* try to preserve user selection */
507 r = runestrchr(old, '|');
508 if(r){
509 bar = r-old;
510 if(q0 > bar){
511 bar = (runestrchr(new, '|')-new)-bar;
512 w->tag.q0 = q0+bar;
513 w->tag.q1 = q1+bar;
517 free(tagname);
518 free(old);
519 free(new);
520 w->tag.file->mod = FALSE;
521 n = w->tag.file->b.nc+w->tag.ncache;
522 if(w->tag.q0 > n)
523 w->tag.q0 = n;
524 if(w->tag.q1 > n)
525 w->tag.q1 = n;
526 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
527 b = button;
528 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
529 b = modbutton;
530 br.min = w->tag.scrollr.min;
531 br.max.x = br.min.x + Dx(b->r);
532 br.max.y = br.min.y + Dy(b->r);
533 draw(screen, br, b, nil, b->r.min);
534 if(resize){
535 w->tagsafe = 0;
536 winresize(w, w->r, TRUE, TRUE);
540 void
541 winsettag(Window *w)
543 int i;
544 File *f;
545 Window *v;
547 f = w->body.file;
548 for(i=0; i<f->ntext; i++){
549 v = f->text[i]->w;
550 if(v->col->safe || v->body.fr.maxlines>0)
551 winsettag1(v);
555 void
556 wincommit(Window *w, Text *t)
558 Rune *r;
559 int i;
560 File *f;
562 textcommit(t, TRUE);
563 f = t->file;
564 if(f->ntext > 1)
565 for(i=0; i<f->ntext; i++)
566 textcommit(f->text[i], FALSE); /* no-op for t */
567 if(t->what == Body)
568 return;
569 r = runemalloc(w->tag.file->b.nc);
570 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
571 for(i=0; i<w->tag.file->b.nc; i++)
572 if(r[i]==' ' || r[i]=='\t')
573 break;
574 expandenv(&r, (uint*)&i);
575 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
576 seq++;
577 filemark(w->body.file);
578 w->body.file->mod = TRUE;
579 w->dirty = TRUE;
580 winsetname(w, r, i);
581 winsettag(w);
583 free(r);
586 void
587 winaddincl(Window *w, Rune *r, int n)
589 char *a;
590 Dir *d;
591 Runestr rs;
593 a = runetobyte(r, n);
594 d = dirstat(a);
595 if(d == nil){
596 if(a[0] == '/')
597 goto Rescue;
598 rs = dirname(&w->body, r, n);
599 r = rs.r;
600 n = rs.nr;
601 free(a);
602 a = runetobyte(r, n);
603 d = dirstat(a);
604 if(d == nil)
605 goto Rescue;
606 r = runerealloc(r, n+1);
607 r[n] = 0;
609 free(a);
610 if((d->qid.type&QTDIR) == 0){
611 free(d);
612 warning(nil, "%s: not a directory\n", a);
613 free(r);
614 return;
616 free(d);
617 w->nincl++;
618 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
619 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
620 w->incl[0] = runemalloc(n+1);
621 runemove(w->incl[0], r, n);
622 free(r);
623 return;
625 Rescue:
626 warning(nil, "%s: %r\n", a);
627 free(r);
628 free(a);
629 return;
632 int
633 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
635 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
636 return TRUE;
637 if(!conservative && w->nopen[QWevent]>0)
638 return TRUE;
639 if(w->dirty){
640 if(w->body.file->nname)
641 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
642 else{
643 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
644 return TRUE;
645 warning(nil, "unnamed file modified\n");
647 w->dirty = FALSE;
648 return FALSE;
650 return TRUE;
653 char*
654 winctlprint(Window *w, char *buf, int fonts)
656 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
657 w->body.file->b.nc, w->isdir, w->dirty);
658 if(fonts)
659 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
660 w->body.reffont->f->name, w->body.fr.maxtab);
661 return buf;
664 void
665 winevent(Window *w, char *fmt, ...)
667 int n;
668 char *b;
669 Xfid *x;
670 va_list arg;
672 if(w->nopen[QWevent] == 0)
673 return;
674 if(w->owner == 0)
675 error("no window owner");
676 va_start(arg, fmt);
677 b = vsmprint(fmt, arg);
678 va_end(arg);
679 if(b == nil)
680 error("vsmprint failed");
681 n = strlen(b);
682 w->events = erealloc(w->events, w->nevents+1+n);
683 w->events[w->nevents++] = w->owner;
684 memmove(w->events+w->nevents, b, n);
685 free(b);
686 w->nevents += n;
687 x = w->eventx;
688 if(x){
689 w->eventx = nil;
690 sendp(x->c, nil);
694 /*
695 * This is here as a first stab at something.
696 * Run acme with the -'$' flag to enable it.
698 * This code isn't quite right, in that it doesn't play well with
699 * the plumber and with other applications. For example:
701 * If the window tag is $home/bin and you execute script, then acme runs
702 * script in $home/bin, via the shell, so everything is fine. If you do
703 * execute "echo $home", it too goes to the shell so you see the value
704 * of $home. And if you right-click on script, then acme plumbs "script"
705 * in the directory "/home/you/bin", so that works, but if you right-click
706 * on "$home/bin/script", then what? It's not correct to expand in acme
707 * since what you're plumbing might be a price tag for all we know. So the
708 * plumber has to expand it, but in order to do that the plumber should
709 * probably publish (and allow users to change) the set of variables it is
710 * using in expansions.
712 * Rob has suggested that a better solution is to make tag lines expand
713 * automatically to fit the necessary number of lines.
715 * The best solution, of course, is to use nice short path names, but this
716 * is not always possible.
717 */
719 int
720 expandenv(Rune **rp, uint *np)
722 char *s, *t;
723 Rune *p, *r, *q;
724 uint n, pref;
725 int nb, nr, slash;
726 Runestr rs;
728 if(!dodollarsigns)
729 return FALSE;
731 r = *rp;
732 n = *np;
733 if(n == 0 || r[0] != '$')
734 return FALSE;
735 for(p=r+1; *p && *p != '/'; p++)
737 pref = p-r;
738 s = runetobyte(r+1, pref-1);
739 if((t = getenv(s)) == nil){
740 free(s);
741 return FALSE;
744 q = runemalloc(utflen(t)+(n-pref));
745 cvttorunes(t, strlen(t), q, &nb, &nr, nil);
746 assert(nr==utflen(t));
747 runemove(q+nr, p, n-pref);
748 free(r);
749 rs = runestr(q, nr+(n-pref));
750 slash = rs.nr>0 && q[rs.nr-1] == '/';
751 rs = cleanrname(rs);
752 if(slash){
753 rs.r = runerealloc(rs.r, rs.nr+1);
754 rs.r[rs.nr++] = '/';
756 *rp = rs.r;
757 *np = rs.nr;
758 free(t);
759 return TRUE;
762 extern char **environ;
763 Rune **runeenv;
765 /*
766 * Weird sort order -- shorter names first,
767 * prefer lowercase over uppercase ($home over $HOME),
768 * then do normal string comparison.
769 */
770 int
771 runeenvcmp(const void *va, const void *vb)
773 Rune *a, *b;
774 int na, nb;
776 a = *(Rune**)va;
777 b = *(Rune**)vb;
778 na = runestrchr(a, '=') - a;
779 nb = runestrchr(b, '=') - b;
780 if(na < nb)
781 return -1;
782 if(nb > na)
783 return 1;
784 if(na == 0)
785 return 0;
786 if(islowerrune(a[0]) && !islowerrune(b[0]))
787 return -1;
788 if(islowerrune(b[0]) && !islowerrune(a[0]))
789 return 1;
790 return runestrncmp(a, b, na);
793 void
794 mkruneenv(void)
796 int i, bad, n, nr;
797 char *p;
798 Rune *r, *q;
800 n = 0;
801 for(i=0; environ[i]; i++){
802 /*
803 * Ignore some pollution.
804 */
805 if(environ[i][0] == '_')
806 continue;
807 if(strncmp(environ[i], "PWD=", 4) == 0)
808 continue;
809 if(strncmp(environ[i], "OLDPWD=", 7) == 0)
810 continue;
812 /*
813 * Must be a rooted path.
814 */
815 if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
816 continue;
818 /*
819 * Only use the ones that we accept in look - all isfilec
820 */
821 bad = 0;
822 r = bytetorune(environ[i], &nr);
823 for(q=r; *q != '='; q++)
824 if(!isfilec(*q)){
825 free(r);
826 bad = 1;
827 break;
829 if(!bad){
830 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
831 runeenv[n++] = r;
834 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
835 runeenv[n] = nil;
836 qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
839 int
840 abbrevenv(Rune **rp, uint *np)
842 int i, len, alen;
843 Rune *r, *p, *q;
844 uint n;
846 if(!dodollarsigns)
847 return FALSE;
849 r = *rp;
850 n = *np;
851 if(n == 0 || r[0] != '/')
852 return FALSE;
854 if(runeenv == nil)
855 mkruneenv();
857 for(i=0; runeenv[i]; i++){
858 p = runestrchr(runeenv[i], '=')+1;
859 len = runestrlen(p);
860 if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
861 alen = (p-1) - runeenv[i];
862 q = runemalloc(1+alen+n-len);
863 q[0] = '$';
864 runemove(q+1, runeenv[i], alen);
865 runemove(q+1+alen, r+len, n-len);
866 free(r);
867 *rp = q;
868 *np = 1+alen+n-len;
869 return TRUE;
872 return FALSE;