summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Faure <billiob@gmail.com>2019-12-05 19:58:25 +0100
committerBoris Faure <billiob@gmail.com>2019-12-05 19:58:25 +0100
commit0eb8948d676bcef01aeb17081b9c11c95f912a8f (patch)
tree79893086e9abf73f418865e2dfde821aa792e37b
parenta782ce61e2260431d4ecd878a3b8f48c9b609c75 (diff)
tycommon: add ty_write() to handle EINTR/EAGAIN on write()terminology-1.6
-rw-r--r--src/bin/tyalpha.c3
-rw-r--r--src/bin/tybg.c6
-rw-r--r--src/bin/tycat.c12
-rw-r--r--src/bin/tycommon.c24
-rw-r--r--src/bin/tycommon.h1
-rw-r--r--src/bin/tyls.c3
-rw-r--r--src/bin/typop.c3
-rw-r--r--src/bin/tyq.c3
-rw-r--r--src/bin/tysend.c12
9 files changed, 51 insertions, 16 deletions
diff --git a/src/bin/tyalpha.c b/src/bin/tyalpha.c
index 63cdfaa..ce22792 100644
--- a/src/bin/tyalpha.c
+++ b/src/bin/tyalpha.c
@@ -45,7 +45,8 @@ main(int argc, char **argv)
45 snprintf(tbuf, sizeof(tbuf), "%c}ap%s", 0x1b, argv[i]); 45 snprintf(tbuf, sizeof(tbuf), "%c}ap%s", 0x1b, argv[i]);
46 else 46 else
47 snprintf(tbuf, sizeof(tbuf), "%c}at%s", 0x1b, argv[i]); 47 snprintf(tbuf, sizeof(tbuf), "%c}at%s", 0x1b, argv[i]);
48 if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write"); 48 if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
49 perror("write");
49 } 50 }
50 return 0; 51 return 0;
51} 52}
diff --git a/src/bin/tybg.c b/src/bin/tybg.c
index 53c9b02..e797466 100644
--- a/src/bin/tybg.c
+++ b/src/bin/tybg.c
@@ -29,7 +29,8 @@ main(int argc, char **argv)
29 { 29 {
30 char tbuf[32]; 30 char tbuf[32];
31 snprintf(tbuf, sizeof(tbuf), "%c}bt", 0x1b); 31 snprintf(tbuf, sizeof(tbuf), "%c}bt", 0x1b);
32 if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write"); 32 if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
33 perror("write");
33 return 0; 34 return 0;
34 } 35 }
35 for (i = 1; i < argc; i++) 36 for (i = 1; i < argc; i++)
@@ -48,7 +49,8 @@ main(int argc, char **argv)
48 snprintf(tbuf, sizeof(tbuf), "%c}bp%s", 0x1b, path); 49 snprintf(tbuf, sizeof(tbuf), "%c}bp%s", 0x1b, path);
49 else 50 else
50 snprintf(tbuf, sizeof(tbuf), "%c}bt%s", 0x1b, path); 51 snprintf(tbuf, sizeof(tbuf), "%c}bt%s", 0x1b, path);
51 if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write"); 52 if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
53 perror("write");
52 } 54 }
53 return 0; 55 return 0;
54} 56}
diff --git a/src/bin/tycat.c b/src/bin/tycat.c
index 4344427..1a618c0 100644
--- a/src/bin/tycat.c
+++ b/src/bin/tycat.c
@@ -96,7 +96,8 @@ prnt(const char *path, int w, int h, int mode)
96 snprintf(buf, sizeof(buf), "%c}if#%i;%i;%s", 0x1b, w, h, path); 96 snprintf(buf, sizeof(buf), "%c}if#%i;%i;%s", 0x1b, w, h, path);
97 else 97 else
98 snprintf(buf, sizeof(buf), "%c}is#%i;%i;%s", 0x1b, w, h, path); 98 snprintf(buf, sizeof(buf), "%c}is#%i;%i;%s", 0x1b, w, h, path);
99 if (write(1, buf, strlen(buf) + 1) < 0) perror("write"); 99 if (ty_write(1, buf, strlen(buf) + 1) < 0)
100 perror("write");
100 i = 0; 101 i = 0;
101 line[i++] = 0x1b; 102 line[i++] = 0x1b;
102 line[i++] = '}'; 103 line[i++] = '}';
@@ -112,7 +113,8 @@ prnt(const char *path, int w, int h, int mode)
112 line[i++] = '\n'; 113 line[i++] = '\n';
113 for (y = 0; y < h; y++) 114 for (y = 0; y < h; y++)
114 { 115 {
115 if (write(1, line, i) < 0) perror("write"); 116 if (ty_write(1, line, i) < 0)
117 perror("write");
116 } 118 }
117 free(line); 119 free(line);
118} 120}
@@ -329,11 +331,13 @@ main(int argc, char **argv)
329 emotion_init(); 331 emotion_init();
330 332
331 ee = ecore_evas_buffer_new(1, 1); 333 ee = ecore_evas_buffer_new(1, 1);
332 if (!ee) goto shutdown; 334 if (!ee)
335 goto shutdown;
333 evas = ecore_evas_get(ee); 336 evas = ecore_evas_get(ee);
334 echo_off(); 337 echo_off();
335 snprintf(buf, sizeof(buf), "%c}qs", 0x1b); 338 snprintf(buf, sizeof(buf), "%c}qs", 0x1b);
336 if (write(1, buf, strlen(buf) + 1) < 0) perror("write"); 339 if (ty_write(1, buf, strlen(buf) + 1) < 0)
340 perror("write");
337 if (scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4 || 341 if (scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4 ||
338 ((tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1))) 342 ((tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1)))
339 { 343 {
diff --git a/src/bin/tycommon.c b/src/bin/tycommon.c
index aef8a65..0c34503 100644
--- a/src/bin/tycommon.c
+++ b/src/bin/tycommon.c
@@ -1,5 +1,7 @@
1#include "private.h" 1#include "private.h"
2#include <errno.h>
2#include <stdlib.h> 3#include <stdlib.h>
4#include <unistd.h>
3#include "tycommon.h" 5#include "tycommon.h"
4 6
5 7
@@ -14,3 +16,25 @@ is_running_in_terminology(void)
14 16
15 return 1; 17 return 1;
16} 18}
19
20ssize_t
21ty_write(int fd, const void *buf, size_t count)
22{
23 const char *data = buf;
24 ssize_t len = count;
25
26 while (len > 0)
27 {
28 ssize_t res = write(fd, data, len);
29
30 if (res < 0)
31 {
32 if (errno == EINTR || errno == EAGAIN)
33 continue;
34 return res;
35 }
36 data += res;
37 len -= res;
38 }
39 return len;
40}
diff --git a/src/bin/tycommon.h b/src/bin/tycommon.h
index 2d7cabd..d5bf90e 100644
--- a/src/bin/tycommon.h
+++ b/src/bin/tycommon.h
@@ -2,6 +2,7 @@
2#define _TY_COMMON_H__ 1 2#define _TY_COMMON_H__ 1
3 3
4int is_running_in_terminology(void); 4int is_running_in_terminology(void);
5ssize_t ty_write(int fd, const void *buf, size_t count);
5 6
6#define ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1() \ 7#define ON_NOT_RUNNING_IN_TERMINOLOGY_EXIT_1() \
7 do \ 8 do \
diff --git a/src/bin/tyls.c b/src/bin/tyls.c
index 86331c3..a9267e3 100644
--- a/src/bin/tyls.c
+++ b/src/bin/tyls.c
@@ -763,7 +763,8 @@ main(int argc, char **argv)
763 echo_off(); 763 echo_off();
764 snprintf(buf, sizeof(buf), "%c}qs", 0x1b); 764 snprintf(buf, sizeof(buf), "%c}qs", 0x1b);
765 len = strlen(buf); 765 len = strlen(buf);
766 if (write(1, buf, len + 1) < (signed)len + 1) perror("write"); 766 if (ty_write(1, buf, len + 1) < (signed)len + 1)
767 perror("write");
767 if ((scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4) 768 if ((scanf("%i;%i;%i;%i", &tw, &th, &cw, &ch) != 4)
768 || (tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1)) 769 || (tw <= 0) || (th <= 0) || (cw <= 1) || (ch <= 1))
769 { 770 {
diff --git a/src/bin/typop.c b/src/bin/typop.c
index 8a9e159..6485a69 100644
--- a/src/bin/typop.c
+++ b/src/bin/typop.c
@@ -39,7 +39,8 @@ main(int argc, char **argv)
39 if (realpath(path, buf)) path = buf; 39 if (realpath(path, buf)) path = buf;
40 snprintf(tbuf, sizeof(tbuf), "%c}p%c%s", 0x1b, 40 snprintf(tbuf, sizeof(tbuf), "%c}p%c%s", 0x1b,
41 (i == 1) ? 'n': 'q', path); 41 (i == 1) ? 'n': 'q', path);
42 if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write"); 42 if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
43 perror("write");
43 } 44 }
44 return 0; 45 return 0;
45} 46}
diff --git a/src/bin/tyq.c b/src/bin/tyq.c
index 1d6167f..2bcac24 100644
--- a/src/bin/tyq.c
+++ b/src/bin/tyq.c
@@ -38,7 +38,8 @@ main(int argc, char **argv)
38 path = argv[i]; 38 path = argv[i];
39 if (realpath(path, buf)) path = buf; 39 if (realpath(path, buf)) path = buf;
40 snprintf(tbuf, sizeof(tbuf), "%c}pq%s", 0x1b, path); 40 snprintf(tbuf, sizeof(tbuf), "%c}pq%s", 0x1b, path);
41 if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) perror("write"); 41 if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
42 perror("write");
42 } 43 }
43 return 0; 44 return 0;
44} 45}
diff --git a/src/bin/tysend.c b/src/bin/tysend.c
index 33ff5f0..ea8b291 100644
--- a/src/bin/tysend.c
+++ b/src/bin/tysend.c
@@ -69,7 +69,7 @@ main(int argc, char **argv)
69 69
70 path = argv[i]; 70 path = argv[i];
71 snprintf(tbuf, sizeof(tbuf), "%c}fr%s", 0x1b, path); 71 snprintf(tbuf, sizeof(tbuf), "%c}fr%s", 0x1b, path);
72 if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) 72 if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
73 goto err; 73 goto err;
74 file_fd = open(path, O_RDONLY); 74 file_fd = open(path, O_RDONLY);
75 if (file_fd >= 0) 75 if (file_fd >= 0)
@@ -79,7 +79,7 @@ main(int argc, char **argv)
79 off = lseek(file_fd, 0, SEEK_END); 79 off = lseek(file_fd, 0, SEEK_END);
80 lseek(file_fd, 0, SEEK_SET); 80 lseek(file_fd, 0, SEEK_SET);
81 snprintf(tbuf, sizeof(tbuf), "%c}fs%llu", 0x1b, (unsigned long long)off); 81 snprintf(tbuf, sizeof(tbuf), "%c}fs%llu", 0x1b, (unsigned long long)off);
82 if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) 82 if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
83 goto err; 83 goto err;
84 for (;;) 84 for (;;)
85 { 85 {
@@ -104,9 +104,9 @@ main(int argc, char **argv)
104 pksum += rawbuf2[bin]; 104 pksum += rawbuf2[bin];
105 } 105 }
106 snprintf(tbuf, sizeof(tbuf), "%c}fd%i ", 0x1b, pksum); 106 snprintf(tbuf, sizeof(tbuf), "%c}fd%i ", 0x1b, pksum);
107 if (write(1, tbuf, strlen(tbuf)) != (signed)(strlen(tbuf))) 107 if (ty_write(1, tbuf, strlen(tbuf)) != (signed)(strlen(tbuf)))
108 goto err; 108 goto err;
109 if (write(1, rawbuf2, bout + 1) != bout + 1) 109 if (ty_write(1, rawbuf2, bout + 1) != bout + 1)
110 goto err; 110 goto err;
111 } 111 }
112 else break; 112 else break;
@@ -123,10 +123,10 @@ main(int argc, char **argv)
123 close(file_fd); 123 close(file_fd);
124 } 124 }
125 snprintf(tbuf, sizeof(tbuf), "%c}fx", 0x1b); 125 snprintf(tbuf, sizeof(tbuf), "%c}fx", 0x1b);
126 if (write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1)) 126 if (ty_write(1, tbuf, strlen(tbuf) + 1) != (signed)(strlen(tbuf) + 1))
127 goto err; 127 goto err;
128 tbuf[0] = 0; 128 tbuf[0] = 0;
129 if (write(1, tbuf, 1) != 1) 129 if (ty_write(1, tbuf, 1) != 1)
130 goto err; 130 goto err;
131 } 131 }
132 echo_on(); 132 echo_on();