aux/statusbar, aux/statusmsg: dont fork in background for window, cleanup

This commit is contained in:
cinap_lenrek 2013-09-16 02:10:18 +02:00
parent c24dd620f9
commit eee51d7720
2 changed files with 19 additions and 250 deletions

View file

@ -4,11 +4,7 @@
#include <bio.h>
#include <event.h>
enum {PNCTL=3};
static char* rdenv(char*);
int newwin(char*);
Rectangle screenrect(void);
int nokill;
int textmode;
@ -156,7 +152,7 @@ bar(Biobuf *b)
d = strtoll(f[1], 0, 0);
drawbar();
}
postnote(PNCTL, child, "kill");
postnote(PNPROC, child, "kill");
}
@ -215,143 +211,31 @@ main(int argc, char **argv)
exits(0);
}
/* all code below this line should be in the library, but is stolen from colors instead */
static char*
rdenv(char *name)
{
char *v;
int fd, size;
fd = open(name, OREAD);
if(fd < 0)
return 0;
size = seek(fd, 0, 2);
v = malloc(size+1);
if(v == 0){
fprint(2, "%s: can't malloc: %r\n", argv0);
exits("no mem");
}
seek(fd, 0, 0);
read(fd, v, size);
v[size] = 0;
close(fd);
return v;
}
int
newwin(char *win)
{
char *srv, *mntsrv;
char spec[100];
int srvfd, cons, pid;
int cons;
switch(rfork(RFFDG|RFPROC|RFNAMEG|RFENVG|RFNOTEG|RFNOWAIT)){
case -1:
fprint(2, "statusbar: can't fork: %r\n");
return -1;
case 0:
break;
default:
exits(0);
if(win != nil){
snprint(spec, sizeof(spec), "-r %s", win);
win = spec;
}
srv = rdenv("/env/wsys");
if(srv == 0){
mntsrv = rdenv("/mnt/term/env/wsys");
if(mntsrv == 0){
fprint(2, "statusbar: can't find $wsys\n");
return -1;
}
srv = malloc(strlen(mntsrv)+10);
sprint(srv, "/mnt/term%s", mntsrv);
free(mntsrv);
pid = 0; /* can't send notes to remote processes! */
}else
pid = getpid();
USED(pid);
srvfd = open(srv, ORDWR);
free(srv);
if(srvfd == -1){
fprint(2, "statusbar: can't open %s: %r\n", srv);
if(newwindow(win) < 0){
fprint(2, "%s: newwindow: %r", argv0);
return -1;
}
sprint(spec, "new -r %s", win);
if(mount(srvfd, -1, "/mnt/wsys", 0, spec) == -1){
fprint(2, "statusbar: can't mount /mnt/wsys: %r (spec=%s)\n", spec);
return -1;
}
close(srvfd);
unmount("/mnt/acme", "/dev");
bind("/mnt/wsys", "/dev", MBEFORE);
cons = open("/dev/cons", OREAD);
if(cons==-1){
if((cons = open("/dev/cons", OREAD)) < 0){
NoCons:
fprint(2, "statusbar: can't open /dev/cons: %r");
fprint(2, "%s: can't open /dev/cons: %r", argv0);
return -1;
}
dup(cons, 0);
close(cons);
cons = open("/dev/cons", OWRITE);
if(cons==-1)
if((cons = open("/dev/cons", OWRITE)) < 0)
goto NoCons;
dup(cons, 1);
dup(cons, 2);
close(cons);
// wctlfd = open("/dev/wctl", OWRITE);
return 0;
}
Rectangle
screenrect(void)
{
int fd;
char buf[12*5];
fd = open("/dev/screen", OREAD);
if(fd == -1)
fd=open("/mnt/term/dev/screen", OREAD);
if(fd == -1){
fprint(2, "%s: can't open /dev/screen: %r\n", argv0);
exits("window read");
}
if(read(fd, buf, sizeof buf) != sizeof buf){
fprint(2, "%s: can't read /dev/screen: %r\n", argv0);
exits("screen read");
}
close(fd);
return Rect(atoi(buf+12), atoi(buf+24), atoi(buf+36), atoi(buf+48));
}
int
postnote(int group, int pid, char *note)
{
char file[128];
int f, r;
switch(group) {
case PNPROC:
sprint(file, "/proc/%d/note", pid);
break;
case PNGROUP:
sprint(file, "/proc/%d/notepg", pid);
break;
case PNCTL:
sprint(file, "/proc/%d/ctl", pid);
break;
default:
return -1;
}
f = open(file, OWRITE);
if(f < 0)
return -1;
r = strlen(note);
if(write(f, note, r) != r) {
close(f);
return -1;
}
close(f);
return 0;
}

View file

@ -4,11 +4,7 @@
#include <bio.h>
#include <event.h>
enum {PNCTL=3};
static char* rdenv(char*);
int newwin(char*);
Rectangle screenrect(void);
int nokill;
int textmode;
@ -95,7 +91,7 @@ msg(Biobuf *b)
}
if(textmode)
write(1, "\n", 1);
postnote(PNCTL, child, "kill");
postnote(PNPROC, child, "kill");
}
@ -157,142 +153,31 @@ main(int argc, char **argv)
exits(0);
}
/* all code below this line should be in the library, but is stolen from colors instead */
static char*
rdenv(char *name)
{
char *v;
int fd, size;
fd = open(name, OREAD);
if(fd < 0)
return 0;
size = seek(fd, 0, 2);
v = malloc(size+1);
if(v == 0){
fprint(2, "%s: can't malloc: %r\n", argv0);
exits("no mem");
}
seek(fd, 0, 0);
read(fd, v, size);
v[size] = 0;
close(fd);
return v;
}
int
newwin(char *win)
{
char *srv, *mntsrv;
char spec[100];
int srvfd, cons, pid;
int cons;
switch(rfork(RFFDG|RFPROC|RFNAMEG|RFENVG|RFNOTEG|RFNOWAIT)){
case -1:
fprint(2, "%s: can't fork: %r\n", argv0);
return -1;
case 0:
break;
default:
exits(0);
if(win != nil){
snprint(spec, sizeof(spec), "-r %s", win);
win = spec;
}
srv = rdenv("/env/wsys");
if(srv == 0){
mntsrv = rdenv("/mnt/term/env/wsys");
if(mntsrv == 0){
fprint(2, "%s: can't find $wsys\n", argv0);
return -1;
}
srv = malloc(strlen(mntsrv)+10);
sprint(srv, "/mnt/term%s", mntsrv);
free(mntsrv);
pid = 0; /* can't send notes to remote processes! */
}else
pid = getpid();
USED(pid);
srvfd = open(srv, ORDWR);
free(srv);
if(srvfd == -1){
fprint(2, "%s: can't open %s: %r\n", argv0, srv);
if(newwindow(win) < 0){
fprint(2, "%s: newwindow: %r", argv0);
return -1;
}
sprint(spec, "new -r %s", win);
if(mount(srvfd, -1, "/mnt/wsys", 0, spec) == -1){
fprint(2, "%s: can't mount /mnt/wsys: %r (spec=%s)\n", argv0, spec);
return -1;
}
close(srvfd);
unmount("/mnt/acme", "/dev");
bind("/mnt/wsys", "/dev", MBEFORE);
cons = open("/dev/cons", OREAD);
if(cons==-1){
if((cons = open("/dev/cons", OREAD)) < 0){
NoCons:
fprint(2, "%s: can't open /dev/cons: %r", argv0);
return -1;
}
dup(cons, 0);
close(cons);
cons = open("/dev/cons", OWRITE);
if(cons==-1)
if((cons = open("/dev/cons", OWRITE)) < 0)
goto NoCons;
dup(cons, 1);
dup(cons, 2);
close(cons);
return 0;
}
Rectangle
screenrect(void)
{
int fd;
char buf[12*5];
fd = open("/dev/screen", OREAD);
if(fd == -1)
fd=open("/mnt/term/dev/screen", OREAD);
if(fd == -1){
fprint(2, "%s: can't open /dev/screen: %r\n", argv0);
exits("window read");
}
if(read(fd, buf, sizeof buf) != sizeof buf){
fprint(2, "%s: can't read /dev/screen: %r\n", argv0);
exits("screen read");
}
close(fd);
return Rect(atoi(buf+12), atoi(buf+24), atoi(buf+36), atoi(buf+48));
}
int
postnote(int group, int pid, char *note)
{
char file[128];
int f, r;
switch(group) {
case PNPROC:
sprint(file, "/proc/%d/note", pid);
break;
case PNGROUP:
sprint(file, "/proc/%d/notepg", pid);
break;
case PNCTL:
sprint(file, "/proc/%d/ctl", pid);
break;
default:
return -1;
}
f = open(file, OWRITE);
if(f < 0)
return -1;
r = strlen(note);
if(write(f, note, r) != r) {
close(f);
return -1;
}
close(f);
return 0;
}