mirror of
https://github.com/LukeSmithxyz/st.git
synced 2024-11-22 07:15:59 +01:00
added sigchld handler, cleaned error checking.
This commit is contained in:
parent
2f35cef54b
commit
ad39f000be
2 changed files with 41 additions and 26 deletions
54
st.c
54
st.c
|
@ -7,6 +7,7 @@ XWindow xw;
|
||||||
Term term;
|
Term term;
|
||||||
Escseq escseq;
|
Escseq escseq;
|
||||||
int cmdfd;
|
int cmdfd;
|
||||||
|
pid_t pid;
|
||||||
int running;
|
int running;
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -31,30 +32,41 @@ xbell(void) { /* visual bell */
|
||||||
XRectangle r = { 0, 0, xw.w, xw.h };
|
XRectangle r = { 0, 0, xw.w, xw.h };
|
||||||
XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
|
XSetForeground(xw.dis, dc.gc, dc.col[BellCol]);
|
||||||
XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
|
XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1);
|
||||||
usleep(30000);
|
/* usleep(30000); */
|
||||||
draw(SCredraw);
|
draw(SCredraw);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
sigchld(int a) {
|
||||||
|
int stat = 0;
|
||||||
|
if(waitpid(pid, &stat, 0) < 0)
|
||||||
|
die("Waiting for pid %hd failed: %s\n", pid, SERRNO);
|
||||||
|
if(WIFEXITED(stat))
|
||||||
|
exit(WEXITSTATUS(stat));
|
||||||
|
else
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
ttynew(void) {
|
ttynew(void) {
|
||||||
int m, s;
|
int m, s;
|
||||||
pid_t pid;
|
|
||||||
char *pts;
|
char *pts;
|
||||||
|
|
||||||
if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
|
if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0)
|
||||||
die("openpt");
|
die("openpt failed: %s\n", SERRNO);
|
||||||
if(grantpt(m) == -1)
|
if(grantpt(m) < 0)
|
||||||
die("grandpt");
|
die("grandpt failed: %s\n", SERRNO);
|
||||||
if(unlockpt(m) == -1)
|
if(unlockpt(m) < 0)
|
||||||
die("unlockpt");
|
die("unlockpt failed: %s\n", SERRNO);
|
||||||
if((pts = ptsname(m)) == NULL)
|
if(!(pts = ptsname(m)))
|
||||||
die("ptsname");
|
die("ptsname failed: %s\n", SERRNO);
|
||||||
if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
|
if((s = open(pts, O_RDWR | O_NOCTTY)) < 0)
|
||||||
die("slave open");
|
die("Couldn't open slave: %s\n", SERRNO);
|
||||||
fcntl(s, F_SETFL, O_NDELAY);
|
fcntl(s, F_SETFL, O_NDELAY);
|
||||||
switch(pid = fork()) {
|
switch(pid = fork()) {
|
||||||
case -1:
|
case -1:
|
||||||
die("fork");
|
die("fork failed\n");
|
||||||
break;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
setsid(); /* create a new process group */
|
setsid(); /* create a new process group */
|
||||||
|
@ -62,12 +74,13 @@ ttynew(void) {
|
||||||
dup2(s, STDOUT_FILENO);
|
dup2(s, STDOUT_FILENO);
|
||||||
dup2(s, STDERR_FILENO);
|
dup2(s, STDERR_FILENO);
|
||||||
if(ioctl(s, TIOCSCTTY, NULL) < 0)
|
if(ioctl(s, TIOCSCTTY, NULL) < 0)
|
||||||
die("slave TTIOCSTTY");
|
die("ioctl TTIOCSTTY failed: %s\n", SERRNO);
|
||||||
execsh();
|
execsh();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
close(s);
|
close(s);
|
||||||
cmdfd = m;
|
cmdfd = m;
|
||||||
|
signal(SIGCHLD, sigchld);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,9 +98,8 @@ ttyread(void) {
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
switch(ret = read(cmdfd, buf, BUFSIZ)) {
|
switch(ret = read(cmdfd, buf, BUFSIZ)) {
|
||||||
case -1: /* error or exit */
|
case -1:
|
||||||
/* XXX: be more precise */
|
die("Couldn't read from shell: %s\n", SERRNO);
|
||||||
running = 0;
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
tputs(buf, ret);
|
tputs(buf, ret);
|
||||||
|
@ -97,7 +109,7 @@ ttyread(void) {
|
||||||
void
|
void
|
||||||
ttywrite(char *s, size_t n) {
|
ttywrite(char *s, size_t n) {
|
||||||
if(write(cmdfd, s, n) == -1)
|
if(write(cmdfd, s, n) == -1)
|
||||||
die("write error on tty.");
|
die("write error on tty: %s\n", SERRNO);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -108,7 +120,7 @@ ttyresize(int x, int y) {
|
||||||
w.ws_col = term.col;
|
w.ws_col = term.col;
|
||||||
w.ws_xpixel = w.ws_ypixel = 0;
|
w.ws_xpixel = w.ws_ypixel = 0;
|
||||||
if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
|
if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
|
||||||
fprintf(stderr, "Couldn't set window size: %m\n");
|
fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -889,7 +901,7 @@ run(void) {
|
||||||
|
|
||||||
running = 1;
|
running = 1;
|
||||||
XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
|
XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask);
|
||||||
XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* seems to fix the resize bug in wmii */
|
XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* fix resize bug in wmii (?) */
|
||||||
|
|
||||||
while(running) {
|
while(running) {
|
||||||
FD_ZERO(&rfd);
|
FD_ZERO(&rfd);
|
||||||
|
@ -898,10 +910,8 @@ run(void) {
|
||||||
XFlush(xw.dis);
|
XFlush(xw.dis);
|
||||||
ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
|
ret = select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, NULL);
|
||||||
|
|
||||||
if(ret < 0) {
|
if(ret < 0)
|
||||||
fprintf(stderr, "select: %m\n");
|
die("select failed: %s\n", SERRNO);
|
||||||
running = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(FD_ISSET(xfd, &rfd)) {
|
if(FD_ISSET(xfd, &rfd)) {
|
||||||
while(XPending(xw.dis)) {
|
while(XPending(xw.dis)) {
|
||||||
|
|
7
st.h
7
st.h
|
@ -1,16 +1,19 @@
|
||||||
/* See LICENSE for licence details. */
|
/* See LICENSE for licence details. */
|
||||||
#define _XOPEN_SOURCE
|
#define _XOPEN_SOURCE
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
#include <X11/keysym.h>
|
#include <X11/keysym.h>
|
||||||
|
@ -52,6 +55,7 @@ static char* colorname[] = {
|
||||||
#define ESCSIZ 256
|
#define ESCSIZ 256
|
||||||
#define ESCARG 16
|
#define ESCARG 16
|
||||||
|
|
||||||
|
#define SERRNO strerror(errno)
|
||||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
#define MAX(a, b) ((a) < (b) ? (b) : (a))
|
#define MAX(a, b) ((a) < (b) ? (b) : (a))
|
||||||
#define LEN(a) (sizeof(a) / sizeof(a[0]))
|
#define LEN(a) (sizeof(a) / sizeof(a[0]))
|
||||||
|
@ -63,7 +67,7 @@ static char* colorname[] = {
|
||||||
enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 }; /* Attribute */
|
enum { ATnone=0 , ATreverse=1 , ATunderline=2, ATbold=4 }; /* Attribute */
|
||||||
enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload }; /* Cursor */
|
enum { CSup, CSdown, CSright, CSleft, CShide, CSdraw, CSwrap, CSsave, CSload }; /* Cursor */
|
||||||
enum { CRset=1 , CRupdate=2 }; /* Character state */
|
enum { CRset=1 , CRupdate=2 }; /* Character state */
|
||||||
enum { TMwrap=1 , TMinsert=2 }; /* Terminal mode */
|
enum { TMwrap=1 , TMinsert=2, TMaltcharset }; /* Terminal mode */
|
||||||
enum { SCupdate, SCredraw }; /* screen draw mode */
|
enum { SCupdate, SCredraw }; /* screen draw mode */
|
||||||
|
|
||||||
typedef int Color;
|
typedef int Color;
|
||||||
|
@ -130,6 +134,7 @@ typedef struct {
|
||||||
void die(const char *errstr, ...);
|
void die(const char *errstr, ...);
|
||||||
void draw(int);
|
void draw(int);
|
||||||
void execsh(void);
|
void execsh(void);
|
||||||
|
void sigchld(int);
|
||||||
void kpress(XKeyEvent *);
|
void kpress(XKeyEvent *);
|
||||||
void resize(XEvent *);
|
void resize(XEvent *);
|
||||||
void run(void);
|
void run(void);
|
||||||
|
|
Loading…
Reference in a new issue