acme: sync against Plan 9, remove -$ option

This commit is contained in:
Russ Cox 2008-03-07 10:45:59 -05:00
parent 7927fe8f9e
commit 734a96bd16
7 changed files with 38 additions and 281 deletions

View file

@ -1322,7 +1322,7 @@ cmdname(File *f, String *str, int set)
}else{ }else{
newname = dirname(f->curtext, runestrdup(s), n); newname = dirname(f->curtext, runestrdup(s), n);
n = newname.nr; n = newname.nr;
r = runemalloc(n+1); r = runemalloc(n+1); /* NUL terminate */
runemove(r, newname.r, n); runemove(r, newname.r, n);
free(newname.r); free(newname.r);
} }

View file

@ -82,8 +82,6 @@ editthread(void *v)
USED(v); USED(v);
threadsetname("editthread"); threadsetname("editthread");
while((cmdp=parsecmd(0)) != 0){ while((cmdp=parsecmd(0)) != 0){
/* ocurfile = curfile; */
/* loaded = curfile && !curfile->unread; */
if(cmdexec(curtext, cmdp) == 0) if(cmdexec(curtext, cmdp) == 0)
break; break;
freecmd(); freecmd();
@ -257,11 +255,11 @@ growlist(List *l)
{ {
if(l->u.listptr==0 || l->nalloc==0){ if(l->u.listptr==0 || l->nalloc==0){
l->nalloc = INCR; l->nalloc = INCR;
l->u.listptr = emalloc(INCR*sizeof(long)); l->u.listptr = emalloc(INCR*sizeof(void*));
l->nused = 0; l->nused = 0;
}else if(l->nused == l->nalloc){ }else if(l->nused == l->nalloc){
l->u.listptr = erealloc(l->u.listptr, (l->nalloc+INCR)*sizeof(long)); l->u.listptr = erealloc(l->u.listptr, (l->nalloc+INCR)*sizeof(void*));
memset((void*)(l->u.longptr+l->nalloc), 0, INCR*sizeof(long)); memset(l->u.ptr+l->nalloc, 0, INCR*sizeof(void*));
l->nalloc += INCR; l->nalloc += INCR;
} }
} }
@ -272,7 +270,7 @@ growlist(List *l)
void void
dellist(List *l, int i) dellist(List *l, int i)
{ {
memmove(&l->u.longptr[i], &l->u.longptr[i+1], (l->nused-(i+1))*sizeof(long)); memmove(&l->u.ptr[i], &l->u.ptr[i+1], (l->nused-(i+1))*sizeof(void*));
l->nused--; l->nused--;
} }
@ -280,11 +278,11 @@ dellist(List *l, int i)
* Add a new element, whose position is i, to the list * Add a new element, whose position is i, to the list
*/ */
void void
inslist(List *l, int i, long val) inslist(List *l, int i, void *v)
{ {
growlist(l); growlist(l);
memmove(&l->u.longptr[i+1], &l->u.longptr[i], (l->nused-i)*sizeof(long)); memmove(&l->u.ptr[i+1], &l->u.ptr[i], (l->nused-i)*sizeof(void*));
l->u.longptr[i] = val; l->u.ptr[i] = v;
l->nused++; l->nused++;
} }
@ -320,7 +318,7 @@ newcmd(void){
Cmd *p; Cmd *p;
p = emalloc(sizeof(Cmd)); p = emalloc(sizeof(Cmd));
inslist(&cmdlist, cmdlist.nused, (long)p); inslist(&cmdlist, cmdlist.nused, p);
return p; return p;
} }
@ -330,7 +328,7 @@ newstring(int n)
String *p; String *p;
p = allocstring(n); p = allocstring(n);
inslist(&stringlist, stringlist.nused, (long)p); inslist(&stringlist, stringlist.nused, p);
return p; return p;
} }
@ -340,7 +338,7 @@ newaddr(void)
Addr *p; Addr *p;
p = emalloc(sizeof(Addr)); p = emalloc(sizeof(Addr));
inslist(&addrlist, addrlist.nused, (long)p); inslist(&addrlist, addrlist.nused, p);
return p; return p;
} }

View file

@ -65,11 +65,9 @@ struct List /* code depends on a long being able to hold a pointer */
int nused; int nused;
union{ union{
void *listptr; void *listptr;
Block *blkptr; void* *ptr;
long *longptr;
uchar* *ucharptr; uchar* *ucharptr;
String* *stringptr; String* *stringptr;
File* *fileptr;
} u; } u;
}; };

View file

@ -289,11 +289,7 @@ respond(Xfid *x, Fcall *t, char *err)
x->buf = emalloc(messagesize); x->buf = emalloc(messagesize);
n = convS2M(t, x->buf, messagesize); n = convS2M(t, x->buf, messagesize);
if(n <= 0) if(n <= 0)
{
fprint(2, "convert error (n=%d, msgsize %d): have %F\n", n, messagesize, &x->fcall);
fprint(2, "\tresponse: %F\n", t);
error("convert error in convS2M"); error("convert error in convS2M");
}
if(write(sfd, x->buf, n) != n) if(write(sfd, x->buf, n) != n)
error("write error in respond"); error("write error in respond");
free(x->buf); free(x->buf);
@ -467,10 +463,6 @@ fsyswalk(Xfid *x, Fid *f)
goto Accept; goto Accept;
Regular: Regular:
/*
if(FILE(f->qid) == Qacme) // empty directory
break;
*/
if(strcmp(x->fcall.wname[i], "new") == 0){ if(strcmp(x->fcall.wname[i], "new") == 0){
if(w) if(w)
error("w set in walk to new"); error("w set in walk to new");
@ -732,13 +724,6 @@ newfid(int fid)
uint uint
getclock(void) getclock(void)
{ {
/*
char buf[32];
buf[0] = '\0';
pread(clockfd, buf, sizeof buf, 0);
return atoi(buf);
*/
return time(0); return time(0);
} }

View file

@ -16,7 +16,7 @@
CFid *plumbsendfid; CFid *plumbsendfid;
CFid *plumbeditfid; CFid *plumbeditfid;
Window* openfile(Text*, Expand*, int); Window* openfile(Text*, Expand*);
int nuntitled; int nuntitled;
@ -195,7 +195,7 @@ look3(Text *t, uint q0, uint q1, int external)
if(expanded == FALSE) if(expanded == FALSE)
return; return;
if(e.name || e.u.at) if(e.name || e.u.at)
openfile(t, &e, FALSE); openfile(t, &e);
else{ else{
if(t->w == nil) if(t->w == nil)
return; return;
@ -225,7 +225,7 @@ plumbgetc(void *a, uint n)
Rune *r; Rune *r;
r = a; r = a;
if(n<0 || n>runestrlen(r)) if(n>runestrlen(r))
return 0; return 0;
return r[n]; return r[n];
} }
@ -235,7 +235,6 @@ plumblook(Plumbmsg *m)
{ {
Expand e; Expand e;
char *addr; char *addr;
int newwindow;
if(m->ndata >= BUFSIZE){ if(m->ndata >= BUFSIZE){
warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data); warning(nil, "insanely long file name (%d bytes) in plumb message (%.32s...)\n", m->ndata, m->data);
@ -256,8 +255,7 @@ plumblook(Plumbmsg *m)
e.u.ar = bytetorune(addr, &e.a1); e.u.ar = bytetorune(addr, &e.a1);
e.agetc = plumbgetc; e.agetc = plumbgetc;
} }
newwindow = plumblookup(m->attr, "newwindow") != nil; openfile(nil, &e);
openfile(nil, &e, newwindow);
free(e.name); free(e.name);
free(e.u.at); free(e.u.at);
drawtopwindow(); drawtopwindow();
@ -485,8 +483,6 @@ dirname(Text *t, Rune *r, int n)
b = nil; b = nil;
if(t==nil || t->w==nil) if(t==nil || t->w==nil)
goto Rescue; goto Rescue;
if(dodollarsigns && n>=1 && r[0]=='$')
expandenv(&r, (uint*)&n);
nt = t->w->tag.file->b.nc; nt = t->w->tag.file->b.nc;
if(nt == 0) if(nt == 0)
goto Rescue; goto Rescue;
@ -504,12 +500,9 @@ dirname(Text *t, Rune *r, int n)
} }
if(slash < 0) if(slash < 0)
goto Rescue; goto Rescue;
slash++; runemove(b+slash+1, r, n);
if(dodollarsigns && expandenv(&b, (uint*)&slash))
b = runerealloc(b, slash+n);
runemove(b+slash, r, n);
free(r); free(r);
return cleanrname(runestr(b, slash+n)); return cleanrname(runestr(b, slash+1+n));
Rescue: Rescue:
free(b); free(b);
@ -570,7 +563,7 @@ expandfile(Text *t, uint q0, uint q1, Expand *e)
if(n == 0) if(n == 0)
return FALSE; return FALSE;
/* see if it's a file name */ /* see if it's a file name */
r = runemalloc(n+1); /* +1 for possible $ below */ r = runemalloc(n);
bufread(&t->file->b, q0, r, n); bufread(&t->file->b, q0, r, n);
/* first, does it have bad chars? */ /* first, does it have bad chars? */
nname = -1; nname = -1;
@ -586,12 +579,9 @@ expandfile(Text *t, uint q0, uint q1, Expand *e)
} }
if(nname == -1) if(nname == -1)
nname = n; nname = n;
for(i=0; i<nname; i++){ for(i=0; i<nname; i++)
if(dodollarsigns && i==0 && r[0]=='$')
continue;
if(!isfilec(r[i])) if(!isfilec(r[i]))
goto Isntfile; goto Isntfile;
}
/* /*
* See if it's a file name in <>, and turn that into an include * See if it's a file name in <>, and turn that into an include
* file name if so. Should probably do it for "" too, but that's not * file name if so. Should probably do it for "" too, but that's not
@ -605,15 +595,11 @@ expandfile(Text *t, uint q0, uint q1, Expand *e)
} }
else if(amin == q0) else if(amin == q0)
goto Isfile; goto Isfile;
else{
if(dodollarsigns && r[0] == '$')
expandenv(&r, (uint*)&nname);
else{ else{
rs = dirname(t, r, nname); rs = dirname(t, r, nname);
r = rs.r; r = rs.r;
nname = rs.nr; nname = rs.nr;
} }
}
e->bname = runetobyte(r, nname); e->bname = runetobyte(r, nname);
/* if it's already a window name, it's a file */ /* if it's already a window name, it's a file */
w = lookfile(r, nname); w = lookfile(r, nname);
@ -656,6 +642,7 @@ expand(Text *t, uint q0, uint q1, Expand *e)
if(expandfile(t, q0, q1, e)) if(expandfile(t, q0, q1, e))
return TRUE; return TRUE;
if(q0 == q1){ if(q0 == q1){
while(q1<t->file->b.nc && isalnum(textreadc(t, q1))) while(q1<t->file->b.nc && isalnum(textreadc(t, q1)))
q1++; q1++;
@ -718,7 +705,7 @@ lookid(int id, int dump)
Window* Window*
openfile(Text *t, Expand *e, int newwindow) openfile(Text *t, Expand *e)
{ {
Range r; Range r;
Window *w, *ow; Window *w, *ow;
@ -757,8 +744,6 @@ openfile(Text *t, Expand *e, int newwindow)
} }
} }
if(w){ if(w){
if(newwindow==TRUE && !w->isdir)
w = coladd(w->col, nil, w, -1);
t = &w->body; t = &w->body;
if(!t->col->safe && t->fr.maxlines==0) /* window is obscured by full-column window */ if(!t->col->safe && t->fr.maxlines==0) /* window is obscured by full-column window */
colgrow(t->col, t->col->w[0], 1); colgrow(t->col, t->col->w[0], 1);
@ -837,7 +822,7 @@ new(Text *et, Text *t, Text *argt, int flag1, int flag2, Rune *arg, int narg)
e.nname = rs.nr; e.nname = rs.nr;
e.bname = runetobyte(rs.r, rs.nr); e.bname = runetobyte(rs.r, rs.nr);
e.jump = TRUE; e.jump = TRUE;
openfile(et, &e, FALSE); openfile(et, &e);
free(e.name); free(e.name);
free(e.bname); free(e.bname);
arg = skipbl(a, na, &narg); arg = skipbl(a, na, &narg);

View file

@ -415,11 +415,8 @@ emalloc(uint n)
void *p; void *p;
p = malloc(n); p = malloc(n);
if(p == nil){ if(p == nil)
fprint(2, "allocating %d from %lux: %r\n", n, getcallerpc(&n));
*(int*)0=0;
error("malloc failed"); error("malloc failed");
}
setmalloctag(p, getcallerpc(&n)); setmalloctag(p, getcallerpc(&n));
memset(p, 0, n); memset(p, 0, n);
return p; return p;
@ -429,10 +426,8 @@ void*
erealloc(void *p, uint n) erealloc(void *p, uint n)
{ {
p = realloc(p, n); p = realloc(p, n);
if(p == nil){ if(p == nil)
fprint(2, "reallocating %d: %r\n", n);
error("realloc failed"); error("realloc failed");
}
setmalloctag(p, getcallerpc(&n)); setmalloctag(p, getcallerpc(&n));
return p; return p;
} }

View file

@ -414,8 +414,8 @@ wincleartag(Window *w)
void void
winsettag1(Window *w) winsettag1(Window *w)
{ {
int bar, dirty, i, j, k, n, ntagname, resize; int i, j, k, n, bar, dirty, resize;
Rune *new, *old, *r, *tagname; Rune *new, *old, *r;
uint q0, q1; uint q0, q1;
static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ', static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
'S', 'n', 'a', 'r', 'f', 0 }; 'S', 'n', 'a', 'r', 'f', 0 };
@ -425,6 +425,7 @@ winsettag1(Window *w)
static Rune Lput[] = { ' ', 'P', 'u', 't', 0 }; static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 }; static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
static Rune Lpipe[] = { ' ', '|', 0 }; static Rune Lpipe[] = { ' ', '|', 0 };
/* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */ /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
if(w->tag.ncache!=0 || w->tag.file->mod) if(w->tag.ncache!=0 || w->tag.file->mod)
wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */ wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
@ -434,40 +435,20 @@ winsettag1(Window *w)
for(i=0; i<w->tag.file->b.nc; i++) for(i=0; i<w->tag.file->b.nc; i++)
if(old[i]==' ' || old[i]=='\t') if(old[i]==' ' || old[i]=='\t')
break; break;
if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
/* make sure the file name is set correctly in the tag */
ntagname = w->body.file->nname;
tagname = runemalloc(ntagname);
runemove(tagname, w->body.file->name, ntagname);
abbrevenv(&tagname, (uint*)&ntagname);
/*
* XXX Why is this here instead of letting the code
* down below do the work?
*/
if(runeeq(old, i, tagname, ntagname) == FALSE){
q0 = w->tag.q0;
q1 = w->tag.q1;
textdelete(&w->tag, 0, i, TRUE); textdelete(&w->tag, 0, i, TRUE);
textinsert(&w->tag, 0, tagname, ntagname, TRUE); textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
free(old); free(old);
old = runemalloc(w->tag.file->b.nc+1); old = runemalloc(w->tag.file->b.nc+1);
bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc); bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
old[w->tag.file->b.nc] = '\0'; old[w->tag.file->b.nc] = '\0';
if(q0 >= i){
/*
* XXX common case - typing at end of name
*/
w->tag.q0 = q0+ntagname-i;
w->tag.q1 = q1+ntagname-i;
}
} }
/* compute the text for the whole tag, replacing current only if it differs */ /* compute the text for the whole tag, replacing current only if it differs */
new = runemalloc(ntagname+100); new = runemalloc(w->body.file->nname+100);
i = 0; i = 0;
runemove(new+i, tagname, ntagname); runemove(new+i, w->body.file->name, w->body.file->nname);
i += ntagname; i += w->body.file->nname;
runemove(new+i, Ldelsnarf, 10); runemove(new+i, Ldelsnarf, 10);
i += 10; i += 10;
if(w->filemenu){ if(w->filemenu){
@ -502,9 +483,6 @@ winsettag1(Window *w)
} }
} }
new[i] = 0; new[i] = 0;
if(runestrlen(new) != i)
fprint(2, "s '%S' len not %d\n", new, i);
assert(i==runestrlen(new));
/* replace tag if the new one is different */ /* replace tag if the new one is different */
resize = 0; resize = 0;
@ -531,7 +509,6 @@ winsettag1(Window *w)
} }
} }
} }
free(tagname);
free(old); free(old);
free(new); free(new);
w->tag.file->mod = FALSE; w->tag.file->mod = FALSE;
@ -702,184 +679,3 @@ winevent(Window *w, char *fmt, ...)
} }
} }
/*
* This is here as a first stab at something.
* Run acme with the -'$' flag to enable it.
*
* This code isn't quite right, in that it doesn't play well with
* the plumber and with other applications. For example:
*
* If the window tag is $home/bin and you execute script, then acme runs
* script in $home/bin, via the shell, so everything is fine. If you do
* execute "echo $home", it too goes to the shell so you see the value
* of $home. And if you right-click on script, then acme plumbs "script"
* in the directory "/home/you/bin", so that works, but if you right-click
* on "$home/bin/script", then what? It's not correct to expand in acme
* since what you're plumbing might be a price tag for all we know. So the
* plumber has to expand it, but in order to do that the plumber should
* probably publish (and allow users to change) the set of variables it is
* using in expansions.
*
* Rob has suggested that a better solution is to make tag lines expand
* automatically to fit the necessary number of lines.
*
* The best solution, of course, is to use nice short path names, but this
* is not always possible.
*/
int
expandenv(Rune **rp, uint *np)
{
char *s, *t;
Rune *p, *r, *q;
uint n, pref;
int nb, nr, slash;
Runestr rs;
if(!dodollarsigns)
return FALSE;
r = *rp;
n = *np;
if(n == 0 || r[0] != '$')
return FALSE;
for(p=r+1; *p && *p != '/'; p++)
;
pref = p-r;
s = runetobyte(r+1, pref-1);
if((t = getenv(s)) == nil){
free(s);
return FALSE;
}
q = runemalloc(utflen(t)+(n-pref));
cvttorunes(t, strlen(t), q, &nb, &nr, nil);
assert(nr==utflen(t));
runemove(q+nr, p, n-pref);
free(r);
rs = runestr(q, nr+(n-pref));
slash = rs.nr>0 && q[rs.nr-1] == '/';
rs = cleanrname(rs);
if(slash){
rs.r = runerealloc(rs.r, rs.nr+1);
rs.r[rs.nr++] = '/';
}
*rp = rs.r;
*np = rs.nr;
free(t);
return TRUE;
}
extern char **environ;
Rune **runeenv;
/*
* Weird sort order -- shorter names first,
* prefer lowercase over uppercase ($home over $HOME),
* then do normal string comparison.
*/
int
runeenvcmp(const void *va, const void *vb)
{
Rune *a, *b;
int na, nb;
a = *(Rune**)va;
b = *(Rune**)vb;
na = runestrchr(a, '=') - a;
nb = runestrchr(b, '=') - b;
if(na < nb)
return -1;
if(nb > na)
return 1;
if(na == 0)
return 0;
if(islowerrune(a[0]) && !islowerrune(b[0]))
return -1;
if(islowerrune(b[0]) && !islowerrune(a[0]))
return 1;
return runestrncmp(a, b, na);
}
void
mkruneenv(void)
{
int i, bad, n, nr;
char *p;
Rune *r, *q;
n = 0;
for(i=0; environ[i]; i++){
/*
* Ignore some pollution.
*/
if(environ[i][0] == '_')
continue;
if(strncmp(environ[i], "PWD=", 4) == 0)
continue;
if(strncmp(environ[i], "OLDPWD=", 7) == 0)
continue;
/*
* Must be a rooted path.
*/
if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
continue;
/*
* Only use the ones that we accept in look - all isfilec
*/
bad = 0;
r = bytetorune(environ[i], &nr);
for(q=r; *q != '='; q++)
if(!isfilec(*q)){
free(r);
bad = 1;
break;
}
if(!bad){
runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
runeenv[n++] = r;
}
}
runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
runeenv[n] = nil;
qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
}
int
abbrevenv(Rune **rp, uint *np)
{
int i, len, alen;
Rune *r, *p, *q;
uint n;
if(!dodollarsigns)
return FALSE;
r = *rp;
n = *np;
if(n == 0 || r[0] != '/')
return FALSE;
if(runeenv == nil)
mkruneenv();
for(i=0; runeenv[i]; i++){
p = runestrchr(runeenv[i], '=')+1;
len = runestrlen(p);
if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
alen = (p-1) - runeenv[i];
q = runemalloc(1+alen+n-len);
q[0] = '$';
runemove(q+1, runeenv[i], alen);
runemove(q+1+alen, r+len, n-len);
free(r);
*rp = q;
*np = 1+alen+n-len;
return TRUE;
}
}
return FALSE;
}