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 * Draw the appropriate button.
96 */
97 void
98 windrawbutton(Window *w)
99 {
100 Image *b;
101 Rectangle br;
103 b = button;
104 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
105 b = modbutton;
106 br.min = w->tag.scrollr.min;
107 br.max.x = br.min.x + Dx(b->r);
108 br.max.y = br.min.y + Dy(b->r);
109 draw(screen, br, b, nil, b->r.min);
112 /*
113 * Compute number of tag lines required
114 * to display entire tag text.
115 */
116 int
117 wintaglines(Window *w, Rectangle r)
119 int n;
120 Rune rune;
122 /* TAG policy here */
124 if(!w->tagexpand)
125 return 1;
126 w->tag.fr.noredraw = 1;
127 textresize(&w->tag, r, TRUE);
128 w->tag.fr.noredraw = 0;
129 w->tagsafe = FALSE;
131 /* can't use more than we have */
132 if(w->tag.fr.nlines >= w->tag.fr.maxlines)
133 return w->tag.fr.maxlines;
135 /* if tag ends with \n, include empty line at end for typing */
136 n = w->tag.fr.nlines;
137 if(w->tag.file->b.nc > 0)
138 bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
139 if(rune == '\n')
140 n++;
141 if(n == 0)
142 n = 1;
143 return n;
146 int
147 winresize(Window *w, Rectangle r, int safe, int keepextra)
149 int oy, y, mouseintag, tagresized;
150 Point p;
151 Rectangle r1;
153 if(0) fprint(2, "winresize %d %R safe=%d keep=%d h=%d\n", w->id, r, safe, keepextra, font->height);
154 w->tagtop = r;
155 w->tagtop.max.y = r.min.y+font->height;
157 /*
158 * TAG If necessary, recompute the number of lines that should
159 * be in the tag.
160 */
161 r1 = r;
162 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
163 y = r1.max.y;
164 mouseintag = ptinrect(mouse->xy, w->tag.all);
165 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
167 w->taglines = wintaglines(w, r);
169 /* END TAG */
171 r1 = r;
172 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
173 y = r1.max.y;
174 tagresized = 0;
175 if(0) fprint(2, "winresize tag %R %R\n", w->tag.all, r1);
176 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
177 tagresized = 1;
178 if(0) fprint(2, "resize tag %R => %R\n", w->tag.all, r1);
179 textresize(&w->tag, r1, TRUE);
180 if(0) fprint(2, "=> %R (%R)\n", w->tag.all, w->tag.fr.r);
181 y = w->tag.fr.r.max.y;
182 windrawbutton(w);
183 w->tagsafe = TRUE;
184 /* TAG */
185 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
186 p = mouse->xy;
187 p.y = w->tag.all.max.y-3;
188 moveto(mousectl, p);
190 /* END TAG */
194 r1 = r;
195 r1.min.y = y;
196 if(tagresized || !safe || !eqrect(w->body.all, r1)){
197 oy = y;
198 if(0) fprint(2, "resizing body; safe=%d all=%R r1=%R\n", safe, w->body.all, r1);
199 if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
200 r1.min.y = y;
201 r1.max.y = y+1;
202 draw(screen, r1, tagcols[BORD], nil, ZP);
203 y++;
204 r1.min.y = min(y, r.max.y);
205 r1.max.y = r.max.y;
206 }else{
207 r1.min.y = y;
208 r1.max.y = y;
210 if(0) fprint(2, "resizing body; new r=%R; r1=%R\n", r, r1);
211 w->r = r;
212 w->r.max.y = textresize(&w->body, r1, keepextra);
213 if(0) fprint(2, "after textresize: body.all=%R\n", w->body.all);
214 textscrdraw(&w->body);
215 w->body.all.min.y = oy;
217 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
218 return w->r.max.y;
221 void
222 winlock1(Window *w, int owner)
224 incref(&w->ref);
225 qlock(&w->lk);
226 w->owner = owner;
229 void
230 winlock(Window *w, int owner)
232 int i;
233 File *f;
235 f = w->body.file;
236 for(i=0; i<f->ntext; i++)
237 winlock1(f->text[i]->w, owner);
240 void
241 winunlock(Window *w)
243 int i;
244 File *f;
246 /*
247 * subtle: loop runs backwards to avoid tripping over
248 * winclose indirectly editing f->text and freeing f
249 * on the last iteration of the loop.
250 */
251 f = w->body.file;
252 for(i=f->ntext-1; i>=0; i--){
253 w = f->text[i]->w;
254 w->owner = 0;
255 qunlock(&w->lk);
256 winclose(w);
260 void
261 winmousebut(Window *w)
263 moveto(mousectl, addpt(w->tag.scrollr.min,
264 divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
267 void
268 windirfree(Window *w)
270 int i;
271 Dirlist *dl;
273 if(w->isdir){
274 for(i=0; i<w->ndl; i++){
275 dl = w->dlp[i];
276 free(dl->r);
277 free(dl);
279 free(w->dlp);
281 w->dlp = nil;
282 w->ndl = 0;
285 void
286 winclose(Window *w)
288 int i;
290 if(decref(&w->ref) == 0){
291 windirfree(w);
292 textclose(&w->tag);
293 textclose(&w->body);
294 if(activewin == w)
295 activewin = nil;
296 for(i=0; i<w->nincl; i++)
297 free(w->incl[i]);
298 free(w->incl);
299 free(w->events);
300 free(w);
304 void
305 windelete(Window *w)
307 Xfid *x;
309 x = w->eventx;
310 if(x){
311 w->nevents = 0;
312 free(w->events);
313 w->events = nil;
314 w->eventx = nil;
315 sendp(x->c, nil); /* wake him up */
319 void
320 winundo(Window *w, int isundo)
322 Text *body;
323 int i;
324 File *f;
325 Window *v;
327 w->utflastqid = -1;
328 body = &w->body;
329 fileundo(body->file, isundo, &body->q0, &body->q1);
330 textshow(body, body->q0, body->q1, 1);
331 f = body->file;
332 for(i=0; i<f->ntext; i++){
333 v = f->text[i]->w;
334 v->dirty = (f->seq != v->putseq);
335 if(v != w){
336 v->body.q0 = v->body.fr.p0+v->body.org;
337 v->body.q1 = v->body.fr.p1+v->body.org;
340 winsettag(w);
343 void
344 winsetname(Window *w, Rune *name, int n)
346 Text *t;
347 Window *v;
348 int i;
349 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
350 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
351 t = &w->body;
352 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
353 return;
354 w->isscratch = FALSE;
355 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
356 w->isscratch = TRUE;
357 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
358 w->isscratch = TRUE;
359 filesetname(t->file, name, n);
360 for(i=0; i<t->file->ntext; i++){
361 v = t->file->text[i]->w;
362 winsettag(v);
363 v->isscratch = w->isscratch;
367 void
368 wintype(Window *w, Text *t, Rune r)
370 int i;
372 texttype(t, r);
373 if(t->what == Body)
374 for(i=0; i<t->file->ntext; i++)
375 textscrdraw(t->file->text[i]);
376 winsettag(w);
379 void
380 wincleartag(Window *w)
382 int i, n;
383 Rune *r;
385 /* w must be committed */
386 n = w->tag.file->b.nc;
387 r = runemalloc(n);
388 bufread(&w->tag.file->b, 0, r, n);
389 for(i=0; i<n; i++)
390 if(r[i]==' ' || r[i]=='\t')
391 break;
392 for(; i<n; i++)
393 if(r[i] == '|')
394 break;
395 if(i == n)
396 return;
397 i++;
398 textdelete(&w->tag, i, n, TRUE);
399 free(r);
400 w->tag.file->mod = FALSE;
401 if(w->tag.q0 > i)
402 w->tag.q0 = i;
403 if(w->tag.q1 > i)
404 w->tag.q1 = i;
405 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
408 void
409 winsettag1(Window *w)
411 int bar, dirty, i, j, k, n, ntagname, resize;
412 Rune *new, *old, *r, *tagname;
413 uint q0, q1;
414 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
415 'S', 'n', 'a', 'r', 'f', 0 };
416 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
417 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
418 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
419 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
420 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
421 static Rune Lpipe[] = { ' ', '|', 0 };
422 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
423 if(w->tag.ncache!=0 || w->tag.file->mod)
424 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
425 old = runemalloc(w->tag.file->b.nc+1);
426 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
427 old[w->tag.file->b.nc] = '\0';
428 for(i=0; i<w->tag.file->b.nc; i++)
429 if(old[i]==' ' || old[i]=='\t')
430 break;
432 /* make sure the file name is set correctly in the tag */
433 ntagname = w->body.file->nname;
434 tagname = runemalloc(ntagname);
435 runemove(tagname, w->body.file->name, ntagname);
436 abbrevenv(&tagname, (uint*)&ntagname);
438 /*
439 * XXX Why is this here instead of letting the code
440 * down below do the work?
441 */
442 if(runeeq(old, i, tagname, ntagname) == FALSE){
443 q0 = w->tag.q0;
444 q1 = w->tag.q1;
445 textdelete(&w->tag, 0, i, TRUE);
446 textinsert(&w->tag, 0, tagname, ntagname, TRUE);
447 free(old);
448 old = runemalloc(w->tag.file->b.nc+1);
449 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
450 old[w->tag.file->b.nc] = '\0';
451 if(q0 >= i){
452 /*
453 * XXX common case - typing at end of name
454 */
455 w->tag.q0 = q0+ntagname-i;
456 w->tag.q1 = q1+ntagname-i;
460 /* compute the text for the whole tag, replacing current only if it differs */
461 new = runemalloc(ntagname+100);
462 i = 0;
463 runemove(new+i, tagname, ntagname);
464 i += ntagname;
465 runemove(new+i, Ldelsnarf, 10);
466 i += 10;
467 if(w->filemenu){
468 if(w->body.needundo || w->body.file->delta.nc>0 || w->body.ncache){
469 runemove(new+i, Lundo, 5);
470 i += 5;
472 if(w->body.file->epsilon.nc > 0){
473 runemove(new+i, Lredo, 5);
474 i += 5;
476 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
477 if(!w->isdir && dirty){
478 runemove(new+i, Lput, 4);
479 i += 4;
482 if(w->isdir){
483 runemove(new+i, Lget, 4);
484 i += 4;
486 runemove(new+i, Lpipe, 2);
487 i += 2;
488 r = runestrchr(old, '|');
489 if(r)
490 k = r-old+1;
491 else{
492 k = w->tag.file->b.nc;
493 if(w->body.file->seq == 0){
494 runemove(new+i, Llook, 6);
495 i += 6;
498 new[i] = 0;
499 if(runestrlen(new) != i)
500 fprint(2, "s '%S' len not %d\n", new, i);
501 assert(i==runestrlen(new));
503 /* replace tag if the new one is different */
504 resize = 0;
505 if(runeeq(new, i, old, k) == FALSE){
506 resize = 1;
507 n = k;
508 if(n > i)
509 n = i;
510 for(j=0; j<n; j++)
511 if(old[j] != new[j])
512 break;
513 q0 = w->tag.q0;
514 q1 = w->tag.q1;
515 textdelete(&w->tag, j, k, TRUE);
516 textinsert(&w->tag, j, new+j, i-j, TRUE);
517 /* try to preserve user selection */
518 r = runestrchr(old, '|');
519 if(r){
520 bar = r-old;
521 if(q0 > bar){
522 bar = (runestrchr(new, '|')-new)-bar;
523 w->tag.q0 = q0+bar;
524 w->tag.q1 = q1+bar;
528 free(tagname);
529 free(old);
530 free(new);
531 w->tag.file->mod = FALSE;
532 n = w->tag.file->b.nc+w->tag.ncache;
533 if(w->tag.q0 > n)
534 w->tag.q0 = n;
535 if(w->tag.q1 > n)
536 w->tag.q1 = n;
537 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
538 windrawbutton(w);
539 if(resize){
540 w->tagsafe = 0;
541 winresize(w, w->r, TRUE, TRUE);
545 void
546 winsettag(Window *w)
548 int i;
549 File *f;
550 Window *v;
552 f = w->body.file;
553 for(i=0; i<f->ntext; i++){
554 v = f->text[i]->w;
555 if(v->col->safe || v->body.fr.maxlines>0)
556 winsettag1(v);
560 void
561 wincommit(Window *w, Text *t)
563 Rune *r;
564 int i;
565 File *f;
567 textcommit(t, TRUE);
568 f = t->file;
569 if(f->ntext > 1)
570 for(i=0; i<f->ntext; i++)
571 textcommit(f->text[i], FALSE); /* no-op for t */
572 if(t->what == Body)
573 return;
574 r = runemalloc(w->tag.file->b.nc);
575 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
576 for(i=0; i<w->tag.file->b.nc; i++)
577 if(r[i]==' ' || r[i]=='\t')
578 break;
579 expandenv(&r, (uint*)&i);
580 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
581 seq++;
582 filemark(w->body.file);
583 w->body.file->mod = TRUE;
584 w->dirty = TRUE;
585 winsetname(w, r, i);
586 winsettag(w);
588 free(r);
591 void
592 winaddincl(Window *w, Rune *r, int n)
594 char *a;
595 Dir *d;
596 Runestr rs;
598 a = runetobyte(r, n);
599 d = dirstat(a);
600 if(d == nil){
601 if(a[0] == '/')
602 goto Rescue;
603 rs = dirname(&w->body, r, n);
604 r = rs.r;
605 n = rs.nr;
606 free(a);
607 a = runetobyte(r, n);
608 d = dirstat(a);
609 if(d == nil)
610 goto Rescue;
611 r = runerealloc(r, n+1);
612 r[n] = 0;
614 free(a);
615 if((d->qid.type&QTDIR) == 0){
616 free(d);
617 warning(nil, "%s: not a directory\n", a);
618 free(r);
619 return;
621 free(d);
622 w->nincl++;
623 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
624 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
625 w->incl[0] = runemalloc(n+1);
626 runemove(w->incl[0], r, n);
627 free(r);
628 return;
630 Rescue:
631 warning(nil, "%s: %r\n", a);
632 free(r);
633 free(a);
634 return;
637 int
638 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
640 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
641 return TRUE;
642 if(!conservative && w->nopen[QWevent]>0)
643 return TRUE;
644 if(w->dirty){
645 if(w->body.file->nname)
646 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
647 else{
648 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
649 return TRUE;
650 warning(nil, "unnamed file modified\n");
652 w->dirty = FALSE;
653 return FALSE;
655 return TRUE;
658 char*
659 winctlprint(Window *w, char *buf, int fonts)
661 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
662 w->body.file->b.nc, w->isdir, w->dirty);
663 if(fonts)
664 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
665 w->body.reffont->f->name, w->body.fr.maxtab);
666 return buf;
669 void
670 winevent(Window *w, char *fmt, ...)
672 int n;
673 char *b;
674 Xfid *x;
675 va_list arg;
677 if(w->nopen[QWevent] == 0)
678 return;
679 if(w->owner == 0)
680 error("no window owner");
681 va_start(arg, fmt);
682 b = vsmprint(fmt, arg);
683 va_end(arg);
684 if(b == nil)
685 error("vsmprint failed");
686 n = strlen(b);
687 w->events = erealloc(w->events, w->nevents+1+n);
688 w->events[w->nevents++] = w->owner;
689 memmove(w->events+w->nevents, b, n);
690 free(b);
691 w->nevents += n;
692 x = w->eventx;
693 if(x){
694 w->eventx = nil;
695 sendp(x->c, nil);
699 /*
700 * This is here as a first stab at something.
701 * Run acme with the -'$' flag to enable it.
703 * This code isn't quite right, in that it doesn't play well with
704 * the plumber and with other applications. For example:
706 * If the window tag is $home/bin and you execute script, then acme runs
707 * script in $home/bin, via the shell, so everything is fine. If you do
708 * execute "echo $home", it too goes to the shell so you see the value
709 * of $home. And if you right-click on script, then acme plumbs "script"
710 * in the directory "/home/you/bin", so that works, but if you right-click
711 * on "$home/bin/script", then what? It's not correct to expand in acme
712 * since what you're plumbing might be a price tag for all we know. So the
713 * plumber has to expand it, but in order to do that the plumber should
714 * probably publish (and allow users to change) the set of variables it is
715 * using in expansions.
717 * Rob has suggested that a better solution is to make tag lines expand
718 * automatically to fit the necessary number of lines.
720 * The best solution, of course, is to use nice short path names, but this
721 * is not always possible.
722 */
724 int
725 expandenv(Rune **rp, uint *np)
727 char *s, *t;
728 Rune *p, *r, *q;
729 uint n, pref;
730 int nb, nr, slash;
731 Runestr rs;
733 if(!dodollarsigns)
734 return FALSE;
736 r = *rp;
737 n = *np;
738 if(n == 0 || r[0] != '$')
739 return FALSE;
740 for(p=r+1; *p && *p != '/'; p++)
742 pref = p-r;
743 s = runetobyte(r+1, pref-1);
744 if((t = getenv(s)) == nil){
745 free(s);
746 return FALSE;
749 q = runemalloc(utflen(t)+(n-pref));
750 cvttorunes(t, strlen(t), q, &nb, &nr, nil);
751 assert(nr==utflen(t));
752 runemove(q+nr, p, n-pref);
753 free(r);
754 rs = runestr(q, nr+(n-pref));
755 slash = rs.nr>0 && q[rs.nr-1] == '/';
756 rs = cleanrname(rs);
757 if(slash){
758 rs.r = runerealloc(rs.r, rs.nr+1);
759 rs.r[rs.nr++] = '/';
761 *rp = rs.r;
762 *np = rs.nr;
763 free(t);
764 return TRUE;
767 extern char **environ;
768 Rune **runeenv;
770 /*
771 * Weird sort order -- shorter names first,
772 * prefer lowercase over uppercase ($home over $HOME),
773 * then do normal string comparison.
774 */
775 int
776 runeenvcmp(const void *va, const void *vb)
778 Rune *a, *b;
779 int na, nb;
781 a = *(Rune**)va;
782 b = *(Rune**)vb;
783 na = runestrchr(a, '=') - a;
784 nb = runestrchr(b, '=') - b;
785 if(na < nb)
786 return -1;
787 if(nb > na)
788 return 1;
789 if(na == 0)
790 return 0;
791 if(islowerrune(a[0]) && !islowerrune(b[0]))
792 return -1;
793 if(islowerrune(b[0]) && !islowerrune(a[0]))
794 return 1;
795 return runestrncmp(a, b, na);
798 void
799 mkruneenv(void)
801 int i, bad, n, nr;
802 char *p;
803 Rune *r, *q;
805 n = 0;
806 for(i=0; environ[i]; i++){
807 /*
808 * Ignore some pollution.
809 */
810 if(environ[i][0] == '_')
811 continue;
812 if(strncmp(environ[i], "PWD=", 4) == 0)
813 continue;
814 if(strncmp(environ[i], "OLDPWD=", 7) == 0)
815 continue;
817 /*
818 * Must be a rooted path.
819 */
820 if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
821 continue;
823 /*
824 * Only use the ones that we accept in look - all isfilec
825 */
826 bad = 0;
827 r = bytetorune(environ[i], &nr);
828 for(q=r; *q != '='; q++)
829 if(!isfilec(*q)){
830 free(r);
831 bad = 1;
832 break;
834 if(!bad){
835 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
836 runeenv[n++] = r;
839 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
840 runeenv[n] = nil;
841 qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
844 int
845 abbrevenv(Rune **rp, uint *np)
847 int i, len, alen;
848 Rune *r, *p, *q;
849 uint n;
851 if(!dodollarsigns)
852 return FALSE;
854 r = *rp;
855 n = *np;
856 if(n == 0 || r[0] != '/')
857 return FALSE;
859 if(runeenv == nil)
860 mkruneenv();
862 for(i=0; runeenv[i]; i++){
863 p = runestrchr(runeenv[i], '=')+1;
864 len = runestrlen(p);
865 if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
866 alen = (p-1) - runeenv[i];
867 q = runemalloc(1+alen+n-len);
868 q[0] = '$';
869 runemove(q+1, runeenv[i], alen);
870 runemove(q+1+alen, r+len, n-len);
871 free(r);
872 *rp = q;
873 *np = 1+alen+n-len;
874 return TRUE;
877 return FALSE;