tycommon: add ty_write() to handle EINTR/EAGAIN on write()

This commit is contained in:
Boris Faure 2019-12-05 19:58:25 +01:00
parent a782ce61e2
commit 0eb8948d67
Signed by: borisfaure
GPG Key ID: EAA9CD729F522998
9 changed files with 51 additions and 16 deletions

View File

@ -45,7 +45,8 @@ main(int argc, char **argv)
snprintf(tbuf, sizeof(tbuf), "%c}ap%s", 0x1b, argv[i]);
else
snprintf(tbuf, sizeof(tbuf), "%c}at%s", 0x1b, argv[i]);
if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write");
if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
perror("write");
}
return 0;
}

View File

@ -29,7 +29,8 @@ main(int argc, char **argv)
{
char tbuf[32];
snprintf(tbuf, sizeof(tbuf), "%c}bt", 0x1b);
if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write");
if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
perror("write");
return 0;
}
for (i = 1; i < argc; i++)
@ -48,7 +49,8 @@ main(int argc, char **argv)
snprintf(tbuf, sizeof(tbuf), "%c}bp%s", 0x1b, path);
else
snprintf(tbuf, sizeof(tbuf), "%c}bt%s", 0x1b, path);
if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write");
if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
perror("write");
}
return 0;
}

View File

@ -96,7 +96,8 @@ prnt(const char *path, int w, int h, int mode)
snprintf(buf, sizeof(buf), "%c}if#%i;%i;%s", 0x1b, w, h, path);
else
snprintf(buf, sizeof(buf), "%c}is#%i;%i;%s", 0x1b, w, h, path);
if (write(1, buf, strlen(buf) + 1) < 0) perror("write");
if (ty_write(1, buf, strlen(buf) + 1) < 0)
perror("write");
i = 0;
line[i++] = 0x1b;
line[i++] = '}';
@ -112,7 +113,8 @@ prnt(const char *path, int w, int h, int mode)
line[i++] = '\n';
for (y = 0; y < h; y++)
{
if (write(1, line, i) < 0) perror("write");
if (ty_write(1, line, i) < 0)
perror("write");
}
free(line);
}
@ -329,11 +331,13 @@ main(int argc, char **argv)
emotion_init();
ee = ecore_evas_buffer_new(1, 1);
if (!ee) goto shutdown;
if (!ee)
goto shutdown;
evas = ecore_evas_get(ee);
echo_off();
snprintf(buf, sizeof(buf), "%c}qs", 0x1b);
if (write(1, buf, strlen(buf) + 1) < 0) perror("write");
if (ty_write(1, buf, strlen(buf) + 1) < 0)
perror("write");
if (scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4 ||
((tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1)))
{

View File

@ -1,5 +1,7 @@
#include "private.h"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "tycommon.h"
@ -14,3 +16,25 @@ is_running_in_terminology(void)
return 1;
}
ssize_t
ty_write(int fd, const void *buf, size_t count)
{
const char *data = buf;
ssize_t len = count;
while (len > 0)
{
ssize_t res = write(fd, data, len);
if (res < 0)
{
if (errno == EINTR || errno == EAGAIN)
continue;
return res;
}
data += res;
len -= res;
}
return len;
}

View File

@ -2,6 +2,7 @@
#define _TY_COMMON_H__ 1
int is_running_in_terminology(void);
ssize_t ty_write(int fd, const void *buf, size_t count);
#define ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1() \
do \

View File

@ -763,7 +763,8 @@ main(int argc, char **argv)
echo_off();
snprintf(buf, sizeof(buf), "%c}qs", 0x1b);
len = strlen(buf);
if (write(1, buf, len + 1) < (signed)len + 1) perror("write");
if (ty_write(1, buf, len + 1) < (signed)len + 1)
perror("write");
if ((scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4)
|| (tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1))
{

View File

@ -39,7 +39,8 @@ main(int argc, char **argv)
if (realpath(path, buf)) path = buf;
snprintf(tbuf, sizeof(tbuf), "%c}p%c%s", 0x1b,
(i == 1) ? 'n': 'q', path);
if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write");
if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
perror("write");
}
return 0;
}

View File

@ -38,7 +38,8 @@ main(int argc, char **argv)
path = argv[i];
if (realpath(path, buf)) path = buf;
snprintf(tbuf, sizeof(tbuf), "%c}pq%s", 0x1b, path);
if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write");
if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
perror("write");
}
return 0;
}

View File

@ -69,7 +69,7 @@ main(int argc, char **argv)
path = argv[i];
snprintf(tbuf, sizeof(tbuf), "%c}fr%s", 0x1b, path);
if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
goto err;
file_fd = open(path, O_RDONLY);
if (file_fd >= 0)
@ -79,7 +79,7 @@ main(int argc, char **argv)
off = lseek(file_fd, 0, SEEK_END);
lseek(file_fd, 0, SEEK_SET);
snprintf(tbuf, sizeof(tbuf), "%c}fs%llu", 0x1b, (unsigned long long)off);
if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
goto err;
for (;;)
{
@ -104,9 +104,9 @@ main(int argc, char **argv)
pksum += rawbuf2[bin];
}
snprintf(tbuf, sizeof(tbuf), "%c}fd%i ", 0x1b, pksum);
if (write(1, tbuf, strlen(tbuf)) != (signed)(strlen(tbuf)))
if (ty_write(1, tbuf, strlen(tbuf)) != (signed)(strlen(tbuf)))
goto err;
if (write(1, rawbuf2, bout + 1) != bout + 1)
if (ty_write(1, rawbuf2, bout + 1) != bout + 1)
goto err;
}
else break;
@ -123,10 +123,10 @@ main(int argc, char **argv)
close(file_fd);
}
snprintf(tbuf, sizeof(tbuf), "%c}fx", 0x1b);
if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
goto err;
tbuf[0] = 0;
if (write(1, tbuf, 1) != 1)
if (ty_write(1, tbuf, 1) != 1)
goto err;
}
echo_on();