commit 377313c0f8f0b901fe2f34c9f9d5891c2b98cd2a
parent 8a367ec398a999499a34d773f67cc6a6834b8a56
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Fri, 22 Nov 2019 17:00:53 +0100
code-style and change EXIT_* back to the standard code
Diffstat:
M | sob.c | | | 209 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 104 insertions(+), 105 deletions(-)
diff --git a/sob.c b/sob.c
@@ -110,11 +110,11 @@ colw(const char *s, size_t max)
wchar_t w;
int r;
- for(i = 0; *s && i < max; s++, i++) {
- if(ISUTF8(*s)) {
- if((r = mbtowc(&w, s, i + 4 > max ? max - i : 4)) == -1)
+ for (i = 0; *s && i < max; s++, i++) {
+ if (ISUTF8(*s)) {
+ if ((r = mbtowc(&w, s, i + 4 > max ? max - i : 4)) == -1)
break;
- if((r = wcwidth(w)) == -1)
+ if ((r = wcwidth(w)) == -1)
r = 1;
len += r;
}
@@ -127,8 +127,8 @@ utf8len(const char *s)
{
size_t i;
- for(i = 0; *s; s++) {
- if(ISUTF8(*s))
+ for (i = 0; *s; s++) {
+ if (ISUTF8(*s))
i++;
}
return i;
@@ -141,8 +141,8 @@ utfprevn(const char *s, size_t p, size_t n)
{
size_t i;
- for(i = 1; p > 0; p--, i++) {
- if(ISUTF8(s[p - 1]) && !--n)
+ for (i = 1; p > 0; p--, i++) {
+ if (ISUTF8(s[p - 1]) && !--n)
return i;
}
return 0;
@@ -155,8 +155,8 @@ utfnextn(const char *s, size_t p, size_t n)
{
size_t i;
- for(i = 1; s[p]; p++, i++) {
- if(ISUTF8(s[p + 1]) && !--n)
+ for (i = 1; s[p]; p++, i++) {
+ if (ISUTF8(s[p + 1]) && !--n)
return i;
}
return 0;
@@ -169,21 +169,21 @@ utfuntilchar(size_t *b, size_t *u, int (*f)(int), int dir)
{
size_t n;
- if(dir > 0) {
- while(*u < line.utflen && *b < line.bytesiz) {
- if(f(line.line[*b]))
+ if (dir > 0) {
+ while (*u < line.utflen && *b < line.bytesiz) {
+ if (f(line.line[*b]))
break;
- if((n = utfnextn(line.line, *b, 1)) == 0)
+ if ((n = utfnextn(line.line, *b, 1)) == 0)
break;
*b += n;
(*u)++;
}
} else {
- while(*u > 0 && *b > 0) {
- if(f(line.line[*b - 1]))
+ while (*u > 0 && *b > 0) {
+ if (f(line.line[*b - 1]))
break;
- if((n = utfprevn(line.line, *b, 1)) == 0)
+ if ((n = utfprevn(line.line, *b, 1)) == 0)
break;
*b -= n;
(*u)--;
@@ -197,12 +197,12 @@ line_inserttext(const char *s)
size_t siz, ulen, clen;
siz = strlen(s);
- if(line.bytepos + siz + 1 > sizeof(line.line))
+ if (line.bytepos + siz + 1 > sizeof(line.line))
return;
clen = colw(s, siz);
ulen = utf8len(s);
/* append */
- if(line.bytepos == line.bytesiz) {
+ if (line.bytepos == line.bytesiz) {
memmove(&line.line[line.bytepos], s, siz);
} else {
/* insert */
@@ -226,10 +226,10 @@ line_promptlen(void)
size_t i;
int t = 0, n = 0;
- for(i = 0; prompt[i]; i++) {
- if(prompt[i] == 1)
+ for (i = 0; prompt[i]; i++) {
+ if (prompt[i] == 1)
t = !t;
- else if(!t && ISUTF8(prompt[i]))
+ else if (!t && ISUTF8(prompt[i]))
n++;
}
return n;
@@ -240,8 +240,8 @@ line_prompt(void)
{
size_t i;
- for(i = 0; prompt[i]; i++) {
- if(prompt[i] != 1)
+ for (i = 0; prompt[i]; i++) {
+ if (prompt[i] != 1)
fputc(prompt[i], outfp);
}
}
@@ -266,8 +266,8 @@ line_draw(void)
fwrite(line.line, 1, line.bytesiz, outfp);
/* replace dirty chars with ' ' */
- if(dirtylen > line.collen) {
- for(i = line.collen; i < dirtylen; i++)
+ if (dirtylen > line.collen) {
+ for (i = line.collen; i < dirtylen; i++)
fputc(' ', outfp);
}
line_cursor_move(line.colpos);
@@ -290,7 +290,7 @@ line_cursor_move(size_t newpos)
x = newpos + line_promptlen();
/* linewrap */
- if(cols > 0 && x > (size_t)cols - 1) {
+ if (cols > 0 && x > (size_t)cols - 1) {
y = (x - (x % cols)) / cols;
x %= cols;
}
@@ -330,9 +330,9 @@ line_cursor_prev(void)
{
size_t n;
- if(line.utfpos <= 0)
+ if (line.utfpos <= 0)
return;
- if((n = utfprevn(line.line, line.bytepos, 1)) == 0)
+ if ((n = utfprevn(line.line, line.bytepos, 1)) == 0)
return;
line.bytepos -= n;
@@ -346,10 +346,10 @@ line_cursor_next(void)
{
size_t n;
- if(line.utfpos >= line.utflen)
+ if (line.utfpos >= line.utflen)
return;
- if((n = utfnextn(line.line, line.bytepos, 1)) == 0)
+ if ((n = utfnextn(line.line, line.bytepos, 1)) == 0)
return;
line.colpos += colw(&line.line[line.bytepos], n);
line.bytepos += n;
@@ -378,10 +378,10 @@ line_delcharnext(void)
{
size_t siz;
- if(line.utfpos == line.utflen || line.utflen <= 0)
+ if (line.utfpos == line.utflen || line.utflen <= 0)
return;
- if((siz = utfnextn(line.line, line.bytepos, 1)) == 0)
+ if ((siz = utfnextn(line.line, line.bytepos, 1)) == 0)
return;
line.collen -= colw(&line.line[line.bytepos], siz);
@@ -400,9 +400,9 @@ line_delcharprev(void)
{
size_t siz, col;
- if(line.utfpos <= 0 || line.utflen <= 0)
+ if (line.utfpos <= 0 || line.utflen <= 0)
return;
- if((siz = utfprevn(line.line, line.bytepos, 1)) == 0)
+ if ((siz = utfprevn(line.line, line.bytepos, 1)) == 0)
return;
col = colw(&line.line[line.bytepos - siz], siz);
@@ -459,7 +459,7 @@ line_delwordprev(void)
{
size_t bs, us, siz, len;
- if(line.utfpos <= 0 || line.utflen <= 0)
+ if (line.utfpos <= 0 || line.utflen <= 0)
return;
line_getwordposprev(line.bytepos, line.utfpos, &bs, &us);
@@ -503,16 +503,16 @@ line_getwordpos(size_t b, size_t u, size_t *bs, size_t *be,
size_t tb = b, tu = u;
utfuntilchar(&b, &u, isspace, -1);
- if(bs)
+ if (bs)
*bs = b;
- if(us)
+ if (us)
*us = u;
/* seek from original specified position */
utfuntilchar(&tb, &tu, isspace, +1);
- if(be)
+ if (be)
*be = tb;
- if(ue)
+ if (ue)
*ue = tu;
}
@@ -521,9 +521,9 @@ line_getwordposprev(size_t sb, size_t su, size_t *b, size_t *u)
{
utfuntilchar(&sb, &su, nonspace, -1);
utfuntilchar(&sb, &su, isspace, -1);
- if(b)
+ if (b)
*b = sb;
- if(u)
+ if (u)
*u = su;
}
@@ -532,9 +532,9 @@ line_getwordposnext(size_t sb, size_t su, size_t *b, size_t *u)
{
utfuntilchar(&sb, &su, nonspace, +1);
utfuntilchar(&sb, &su, isspace, +1);
- if(b)
+ if (b)
*b = sb;
- if(u)
+ if (u)
*u = su;
}
@@ -547,7 +547,7 @@ line_copywordcursor(char *buf, size_t bufsiz)
len = be - bs;
/* truncate */
- if(len + 1 > bufsiz)
+ if (len + 1 > bufsiz)
len = bufsiz - 1;
memcpy(buf, &line.line[bs], len);
buf[len] = '\0';
@@ -568,14 +568,14 @@ pipe_rw(int fd_in, int fd_out, char *writestr,
ssize_t r;
int maxfd, status = -1, haswritten = 0;
- if(fd_out == -1 || writestr == NULL)
+ if (fd_out == -1 || writestr == NULL)
haswritten = 1;
- while(isrunning) {
+ while (isrunning) {
FD_ZERO(&fdr);
FD_ZERO(&fdw);
- if(haswritten) {
- if(!f || fd_in == -1)
+ if (haswritten) {
+ if (!f || fd_in == -1)
break;
FD_SET(fd_in, &fdr);
maxfd = fd_in;
@@ -586,49 +586,49 @@ pipe_rw(int fd_in, int fd_out, char *writestr,
memset(&tv, 0, sizeof(tv));
tv.tv_usec = 50000; /* 50 ms */
- if((r = select(maxfd + 1, haswritten ? &fdr : NULL,
+ if ((r = select(maxfd + 1, haswritten ? &fdr : NULL,
haswritten ? NULL : &fdw, NULL, &tv)) == -1) {
- if(errno != EINTR)
+ if (errno != EINTR)
goto fini;
- } else if(!r) { /* timeout */
+ } else if (!r) { /* timeout */
continue;
}
- if(fd_out != -1 && FD_ISSET(fd_out, &fdw)) {
- if(write(fd_out, writestr, strlen(writestr)) == -1) {
- if(errno == EWOULDBLOCK || errno == EAGAIN ||
+ if (fd_out != -1 && FD_ISSET(fd_out, &fdw)) {
+ if (write(fd_out, writestr, strlen(writestr)) == -1) {
+ if (errno == EWOULDBLOCK || errno == EAGAIN ||
errno == EINTR)
continue;
- else if(errno == EPIPE)
+ else if (errno == EPIPE)
goto fini;
goto fini;
}
- if(fd_out != STDOUT_FILENO)
+ if (fd_out > 2)
close(fd_out); /* sends EOF */
fd_out = -1;
haswritten = 1;
}
- if(haswritten && fd_in != -1 && FD_ISSET(fd_in, &fdr)) {
+ if (haswritten && fd_in != -1 && FD_ISSET(fd_in, &fdr)) {
r = read(fd_in, buf, sizeof(buf));
- if(r == -1) {
- if(errno == EWOULDBLOCK || errno == EAGAIN)
+ if (r == -1) {
+ if (errno == EWOULDBLOCK || errno == EAGAIN)
continue;
goto fini;
}
- if(r > 0) {
+ if (r > 0) {
buf[r] = '\0';
total += (size_t)r;
- if(f)
+ if (f)
f(buf, r, total);
- } else if(!r) {
+ } else if (!r) {
status = 0;
goto fini;
}
}
}
fini:
- if(fd_in != -1 && fd_in != STDIN_FILENO)
+ if (fd_in != -1 && fd_in > 2)
close(fd_in);
- if(fd_out != -1 && fd_out != STDOUT_FILENO)
+ if (fd_out != -1 && fd_out > 2)
close(fd_out);
return status;
}
@@ -655,17 +655,16 @@ pipe_cmd(char *cmd[], char *writestr, void (*f)(const char *, size_t, size_t))
close(cp[0]);
close(pc[1]);
- if (dup2(pc[0], STDIN_FILENO) == -1 ||
- dup2(cp[1], STDOUT_FILENO) == -1) {
+ if (dup2(pc[0], 0) == -1 || dup2(cp[1], 1) == -1) {
perror("dup2");
return -1;
}
- if(execv(cmd[0], (char**)cmd) == -1) {
+ if (execv(cmd[0], (char**)cmd) == -1) {
perror("execv");
- _exit(EXIT_FAILURE); /* NOTE: must be _exit */
+ _exit(1); /* NOTE: must be _exit */
}
- _exit(EXIT_SUCCESS);
+ _exit(0);
} else {
/* parent */
close(pc[0]);
@@ -677,7 +676,7 @@ pipe_cmd(char *cmd[], char *writestr, void (*f)(const char *, size_t, size_t))
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
- if(pipe_rw(cp[0], pc[1], writestr, f) == -1)
+ if (pipe_rw(cp[0], pc[1], writestr, f) == -1)
return -1;
}
return 0;
@@ -686,7 +685,7 @@ pipe_cmd(char *cmd[], char *writestr, void (*f)(const char *, size_t, size_t))
static void
cb_handleinput(const char *buf, size_t len, size_t total)
{
- if(!len || !total)
+ if (!len || !total)
return;
handleinput((unsigned char *)buf, len);
}
@@ -694,7 +693,7 @@ cb_handleinput(const char *buf, size_t len, size_t total)
static void
cb_pipe_insert(const char *buf, size_t len, size_t total)
{
- if(!len || !total)
+ if (!len || !total)
return;
memset(&line, 0, sizeof(line));
handleinput((unsigned char *)buf, len);
@@ -703,10 +702,10 @@ cb_pipe_insert(const char *buf, size_t len, size_t total)
static void
cb_pipe_replaceword(const char *buf, size_t len, size_t total)
{
- if(!len)
+ if (!len)
return;
/* first read: delete word under cursor. */
- if(len == total)
+ if (len == total)
line_delwordcursor();
handleinput((unsigned char *)buf, len);
}
@@ -751,7 +750,7 @@ gettermsize(void)
{
struct winsize w;
- if(ioctl(STDIN_FILENO, TIOCGWINSZ, &w) == -1)
+ if (ioctl(0, TIOCGWINSZ, &w) == -1)
return;
cols = w.ws_col;
rows = w.ws_row;
@@ -771,12 +770,12 @@ handleinput(const unsigned char *input, size_t len)
char buf[BUFSIZ];
dirtylen = line.collen;
- while(p < len && input[p] != '\0') {
- if(input[p] == 0x1b || iscntrl(input[p])) {
+ while (p < len && input[p] != '\0') {
+ if (input[p] == 0x1b || iscntrl(input[p])) {
ismatch = 0;
- for(i = 0; i < LEN(keybinds); i++) {
+ for (i = 0; i < LEN(keybinds); i++) {
keylen = strlen((char*)keybinds[i].key);
- if(len - p >= keylen &&
+ if (len - p >= keylen &&
memcmp(&input[p], keybinds[i].key, keylen) == 0) {
keybinds[i].func();
p += keylen;
@@ -784,15 +783,15 @@ handleinput(const unsigned char *input, size_t len)
break;
}
}
- if(!ismatch) {
- if(input[p] == 0x1b)
+ if (!ismatch) {
+ if (input[p] == 0x1b)
return;
p++;
}
} else {
- for(i = p; input[i] && input[i] > 0x1b; i++)
+ for (i = p; input[i] && input[i] > 0x1b; i++)
;
- if(i - p < sizeof(buf)) {
+ if (i - p < sizeof(buf)) {
memcpy(buf, &input[p], i - p);
buf[i - p] = '\0';
p = i;
@@ -828,27 +827,27 @@ setup(void)
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
- if(tcgetattr(STDIN_FILENO, &ttystate) == 0) {
+ if (tcgetattr(0, &ttystate) == 0) {
termattrset = 1;
ttysave = ttystate;
/* turn off canonical mode and echo */
ttystate.c_lflag &= ~(ICANON | ECHO);
ttystate.c_cc[VMIN] = 1;
/* set the terminal attributes */
- tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
+ tcsetattr(0, TCSANOW, &ttystate);
} else {
/* not a tty */
initialinput();
/* setup tty again because we (re)open "/dev/tty" */
termattrset = 0;
- if(tcgetattr(STDIN_FILENO, &ttystate) == 0) {
+ if (tcgetattr(0, &ttystate) == 0) {
termattrset = 1;
ttysave = ttystate;
/* turn off canonical mode and echo */
ttystate.c_lflag &= ~(ICANON | ECHO);
ttystate.c_cc[VMIN] = 1;
/* set the terminal attributes */
- tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
+ tcsetattr(0, TCSANOW, &ttystate);
}
}
/* get terminal window size */
@@ -858,21 +857,21 @@ setup(void)
static void
run(void)
{
- if(!isrunning)
+ if (!isrunning)
return;
/* clear screen */
clear();
- fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
+ fcntl(0, F_SETFL, O_NONBLOCK);
line_draw();
- pipe_rw(STDIN_FILENO, -1, NULL, cb_handleinput);
+ pipe_rw(0, -1, NULL, cb_handleinput);
/* cleanup: restore terminal attributes */
- if(termattrset) {
+ if (termattrset) {
ttystate.c_lflag = ttysave.c_lflag;
- tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
+ tcsetattr(0, TCSANOW, &ttystate);
}
}
@@ -882,24 +881,24 @@ initialinput(void)
int fd;
/* read initial input from stdin */
- fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
- pipe_rw(STDIN_FILENO, -1, NULL, cb_handleinput);
+ fcntl(0, F_SETFL, O_NONBLOCK);
+ pipe_rw(0, -1, NULL, cb_handleinput);
/* restore terminal attributes */
- if(termattrset) {
+ if (termattrset) {
ttystate.c_lflag = ttysave.c_lflag;
- tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
+ tcsetattr(0, TCSANOW, &ttystate);
}
- if(!isrunning)
+ if (!isrunning)
return;
/* close and re-attach to stdin */
- close(STDIN_FILENO);
- if((fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) == -1) {
+ close(0);
+ if ((fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) == -1) {
fprintf(stderr, "open: /dev/tty: %s\n", strerror(errno));
- exit(EXIT_FAILURE);
+ exit(1);
}
- if(dup2(fd, STDIN_FILENO) == -1) {
+ if (dup2(fd, 0) == -1) {
fprintf(stderr, "dup2: /dev/tty: %s\n", strerror(errno));
- exit(EXIT_FAILURE);
+ exit(1);
}
}
@@ -907,7 +906,7 @@ static void
usage(void)
{
fprintf(stderr, "usage: %s [-p prompt]\n", argv0);
- exit(EXIT_FAILURE);
+ exit(1);
}
int
@@ -924,5 +923,5 @@ main(int argc, char **argv)
setup();
run();
- return EXIT_SUCCESS;
+ return 0;
}