This source file includes following definitions.
- bprintf
- bpathf
- bwritef
- breadfrom
- xgetenv
- run
- runv
- bgrunv
- genrun
- bgwait1
- bgwait
- xgetwd
- xrealwd
- isdir
- isfile
- mtime
- isabs
- readfile
- writefile
- xmkdir
- xmkdirall
- xremove
- xremoveall
- xreaddir
- xworkdir
- fatal
- xmalloc
- xstrdup
- xrealloc
- xfree
- hassuffix
- hasprefix
- contains
- streq
- lastelem
- xmemmove
- xmemcmp
- xstrlen
- xexit
- xatexit
- xprintf
- errprintf
- xsetenv
- main
- xqsort
- xstrcmp
- xstrstr
- xstrrchr
- xsamefile
- xtryexecfunc
- cansse2
#ifdef PLAN9
#include <u.h>
#include <libc.h>
#include <stdio.h>
#undef nil
#undef nelem
#include "a.h"
char*
bprintf(Buf *b, char *fmt, ...)
{
va_list arg;
char buf[4096];
breset(b);
va_start(arg, fmt);
vsnprintf(buf, sizeof buf, fmt, arg);
va_end(arg);
bwritestr(b, buf);
return bstr(b);
}
char*
bpathf(Buf *b, char *fmt, ...)
{
va_list arg;
char buf[4096];
breset(b);
va_start(arg, fmt);
vsnprintf(buf, sizeof buf, fmt, arg);
va_end(arg);
bwritestr(b, buf);
return bstr(b);
}
void
bwritef(Buf *b, char *fmt, ...)
{
va_list arg;
char buf[4096];
va_start(arg, fmt);
vsnprintf(buf, sizeof buf, fmt, arg);
va_end(arg);
bwritestr(b, buf);
}
static void
breadfrom(Buf *b, int fd)
{
int n;
for(;;) {
bgrow(b, 4096);
n = read(fd, b->p+b->len, 4096);
if(n < 0)
fatal("read");
if(n == 0)
break;
b->len += n;
}
}
void
xgetenv(Buf *b, char *name)
{
char *p;
breset(b);
p = getenv(name);
if(p != nil)
bwritestr(b, p);
}
static void genrun(Buf *b, char *dir, int mode, Vec *argv, int bg);
void
run(Buf *b, char *dir, int mode, char *cmd, ...)
{
va_list arg;
Vec argv;
char *p;
vinit(&argv);
vadd(&argv, cmd);
va_start(arg, cmd);
while((p = va_arg(arg, char*)) != nil)
vadd(&argv, p);
va_end(arg);
runv(b, dir, mode, &argv);
vfree(&argv);
}
void
runv(Buf *b, char *dir, int mode, Vec *argv)
{
genrun(b, dir, mode, argv, 1);
}
void
bgrunv(char *dir, int mode, Vec *argv)
{
genrun(nil, dir, mode, argv, 0);
}
#define MAXBG 4
static struct {
int pid;
int mode;
char *cmd;
Buf *b;
} bg[MAXBG];
static int nbg;
static int maxnbg = nelem(bg);
static void bgwait1(void);
static void
genrun(Buf *b, char *dir, int mode, Vec *argv, int wait)
{
int i, p[2], pid;
Buf b1, cmd;
char *q;
while(nbg >= maxnbg)
bgwait1();
binit(&b1);
binit(&cmd);
if(!isabs(argv->p[0])) {
bpathf(&b1, "/bin/%s", argv->p[0]);
free(argv->p[0]);
argv->p[0] = xstrdup(bstr(&b1));
}
for(i=0; i<argv->len; i++) {
if(i > 0)
bwritestr(&cmd, " ");
q = argv->p[i];
if(workdir != nil && hasprefix(q, workdir)) {
bwritestr(&cmd, "$WORK");
q += strlen(workdir);
}
bwritestr(&cmd, q);
}
if(vflag > 1)
errprintf("%s\n", bstr(&cmd));
if(b != nil) {
breset(b);
if(pipe(p) < 0)
fatal("pipe");
}
switch(pid = fork()) {
case -1:
fatal("fork");
case 0:
if(b != nil) {
close(0);
close(p[0]);
dup(p[1], 1);
dup(p[1], 2);
if(p[1] > 2)
close(p[1]);
}
if(dir != nil) {
if(chdir(dir) < 0) {
fprint(2, "chdir: %r\n");
_exits("chdir");
}
}
vadd(argv, nil);
exec(argv->p[0], argv->p);
fprint(2, "%s\n", bstr(&cmd));
fprint(2, "exec: %r\n");
_exits("exec");
}
if(b != nil) {
close(p[1]);
breadfrom(b, p[0]);
close(p[0]);
}
if(nbg < 0)
fatal("bad bookkeeping");
bg[nbg].pid = pid;
bg[nbg].mode = mode;
bg[nbg].cmd = btake(&cmd);
bg[nbg].b = b;
nbg++;
if(wait)
bgwait();
bfree(&cmd);
bfree(&b1);
}
static void
bgwait1(void)
{
Waitmsg *w;
int i, mode;
char *cmd;
Buf *b;
w = wait();
if(w == nil)
fatal("wait");
for(i=0; i<nbg; i++)
if(bg[i].pid == w->pid)
goto ok;
fatal("wait: unexpected pid");
ok:
cmd = bg[i].cmd;
mode = bg[i].mode;
bg[i].pid = 0;
b = bg[i].b;
bg[i].b = nil;
bg[i] = bg[--nbg];
if(mode == CheckExit && w->msg[0]) {
if(b != nil)
xprintf("%s\n", bstr(b));
fatal("FAILED: %s", cmd);
}
xfree(cmd);
}
void
bgwait(void)
{
while(nbg > 0)
bgwait1();
}
void
xgetwd(Buf *b)
{
char buf[4096];
breset(b);
if(getwd(buf, sizeof buf) == nil)
fatal("getwd");
bwritestr(b, buf);
}
void
xrealwd(Buf *b, char *path)
{
char buf[4096];
int fd;
fd = open(path, OREAD);
if(fd2path(fd, buf, sizeof buf) < 0)
fatal("fd2path");
close(fd);
breset(b);
bwritestr(b, buf);
}
bool
isdir(char *p)
{
Dir *d;
ulong mode;
d = dirstat(p);
if(d == nil)
return 0;
mode = d->mode;
free(d);
return (mode & DMDIR) == DMDIR;
}
bool
isfile(char *p)
{
Dir *d;
ulong mode;
d = dirstat(p);
if(d == nil)
return 0;
mode = d->mode;
free(d);
return (mode & DMDIR) == 0;
}
Time
mtime(char *p)
{
Dir *d;
ulong t;
d = dirstat(p);
if(d == nil)
return 0;
t = d->mtime;
free(d);
return (Time)t;
}
bool
isabs(char *p)
{
return hasprefix(p, "/");
}
void
readfile(Buf *b, char *file)
{
int fd;
breset(b);
fd = open(file, OREAD);
if(fd < 0)
fatal("open %s", file);
breadfrom(b, fd);
close(fd);
}
void
writefile(Buf *b, char *file, int exec)
{
int fd;
Dir d;
fd = create(file, ORDWR, 0666);
if(fd < 0)
fatal("create %s", file);
if(write(fd, b->p, b->len) != b->len)
fatal("short write");
if(exec) {
nulldir(&d);
d.mode = 0755;
dirfwstat(fd, &d);
}
close(fd);
}
void
xmkdir(char *p)
{
int fd;
if(isdir(p))
return;
fd = create(p, OREAD, 0777|DMDIR);
close(fd);
if(fd < 0)
fatal("mkdir %s", p);
}
void
xmkdirall(char *p)
{
char *q;
if(isdir(p))
return;
q = strrchr(p, '/');
if(q != nil) {
*q = '\0';
xmkdirall(p);
*q = '/';
}
xmkdir(p);
}
void
xremove(char *p)
{
if(vflag > 2)
errprintf("rm %s\n", p);
remove(p);
}
void
xremoveall(char *p)
{
int i;
Buf b;
Vec dir;
binit(&b);
vinit(&dir);
if(isdir(p)) {
xreaddir(&dir, p);
for(i=0; i<dir.len; i++) {
bprintf(&b, "%s/%s", p, dir.p[i]);
xremoveall(bstr(&b));
}
}
if(vflag > 2)
errprintf("rm %s\n", p);
remove(p);
bfree(&b);
vfree(&dir);
}
void
xreaddir(Vec *dst, char *dir)
{
Dir *d;
int fd, i, n;
vreset(dst);
fd = open(dir, OREAD);
if(fd < 0)
fatal("open %s", dir);
n = dirreadall(fd, &d);
for(i=0; i<n; i++)
vadd(dst, d[i].name);
free(d);
close(fd);
}
char*
xworkdir(void)
{
Buf b;
char *p;
int fd, tries;
binit(&b);
fd = 0;
for(tries=0; tries<1000; tries++) {
bprintf(&b, "/tmp/go-cbuild-%06x", nrand((1<<24)-1));
fd = create(bstr(&b), OREAD|OEXCL, 0700|DMDIR);
if(fd >= 0)
goto done;
}
fatal("xworkdir create");
done:
close(fd);
p = btake(&b);
bfree(&b);
return p;
}
void
fatal(char *msg, ...)
{
char buf[ERRMAX];
va_list arg;
rerrstr(buf, sizeof buf);
fflush(stdout);
fprintf(stderr, "go tool dist: ");
va_start(arg, msg);
vfprintf(stderr, msg, arg);
va_end(arg);
if(buf[0])
fprintf(stderr, ": %s", buf);
fprintf(stderr, "\n");
bgwait();
exits(msg);
}
void*
xmalloc(int n)
{
void *p;
p = malloc(n);
if(p == nil)
fatal("out of memory");
memset(p, 0, n);
return p;
}
char*
xstrdup(char *p)
{
p = strdup(p);
if(p == nil)
fatal("out of memory");
return p;
}
void*
xrealloc(void *p, int n)
{
p = realloc(p, n);
if(p == nil)
fatal("out of memory");
return p;
}
void
xfree(void *p)
{
free(p);
}
bool
hassuffix(char *p, char *suffix)
{
int np, ns;
np = strlen(p);
ns = strlen(suffix);
return np >= ns && strcmp(p+np-ns, suffix) == 0;
}
bool
hasprefix(char *p, char *prefix)
{
return strncmp(p, prefix, strlen(prefix)) == 0;
}
bool
contains(char *p, char *sep)
{
return strstr(p, sep) != nil;
}
bool
streq(char *p, char *q)
{
return strcmp(p, q) == 0;
}
char*
lastelem(char *p)
{
char *out;
out = p;
for(; *p; p++)
if(*p == '/')
out = p+1;
return out;
}
void
xmemmove(void *dst, void *src, int n)
{
memmove(dst, src, n);
}
int
xmemcmp(void *a, void *b, int n)
{
return memcmp(a, b, n);
}
int
xstrlen(char *p)
{
return strlen(p);
}
void
xexit(int n)
{
char buf[32];
snprintf(buf, sizeof buf, "%d", n);
exits(buf);
}
void
xatexit(void (*f)(void))
{
atexit(f);
}
void
xprintf(char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
vprintf(fmt, arg);
va_end(arg);
}
void
errprintf(char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
vfprintf(stderr, fmt, arg);
va_end(arg);
}
void
xsetenv(char *name, char *value)
{
putenv(name, value);
}
void
main(int argc, char **argv)
{
Buf b;
setvbuf(stdout, nil, _IOLBF, BUFSIZ);
setvbuf(stderr, nil, _IOLBF, BUFSIZ);
binit(&b);
rfork(RFENVG);
slash = "/";
gohostos = "plan9";
xgetenv(&b, "objtype");
if(b.len == 0)
fatal("$objtype is unset");
gohostarch = btake(&b);
srand(time(0)+getpid());
init();
xmain(argc, argv);
bfree(&b);
exits(nil);
}
void
xqsort(void *data, int n, int elemsize, int (*cmp)(const void*, const void*))
{
qsort(data, n, elemsize, cmp);
}
int
xstrcmp(char *a, char *b)
{
return strcmp(a, b);
}
char*
xstrstr(char *a, char *b)
{
return strstr(a, b);
}
char*
xstrrchr(char *p, int c)
{
return strrchr(p, c);
}
int
xsamefile(char *f1, char *f2)
{
return streq(f1, f2);
}
int
xtryexecfunc(void (*f)(void))
{
USED(f);
return 0;
}
bool
cansse2(void)
{
return 0;
}
#endif