summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2023-09-10 13:47:21 -0400
committerpommicket <pommicket@gmail.com>2023-09-10 13:47:21 -0400
commitfeee83bf24564212832fd113e594a17498c87c21 (patch)
tree91fb15b718f9716b7b35dd871d687fb45ac0dad0
parent6586f9e66d87017984f57b3b4579c78897ba66f2 (diff)
swap return 0 and -1 for process_read/socket_read
for consistency with process_write/socket_write
-rw-r--r--build.c4
-rw-r--r--lsp.c18
-rw-r--r--main.c2
-rw-r--r--os-posix.c4
-rw-r--r--os-win.c6
-rw-r--r--os.h8
6 files changed, 14 insertions, 28 deletions
diff --git a/build.c b/build.c
index f45b025..df9a140 100644
--- a/build.c
+++ b/build.c
@@ -357,10 +357,10 @@ void build_frame(Ted *ted, float x1, float y1, float x2, float y2) {
ted_error(ted, "Error reading command output: %s.", process_geterr(ted->build_process));
build_stop(ted);
break;
- } else if (bytes_read == -1) {
+ } else if (bytes_read == 0) {
// no data right now.
break;
- } else if (bytes_read == 0) {
+ } else if (bytes_read == -1) {
// end of file
break;
} else {
diff --git a/lsp.c b/lsp.c
index b4b65bb..274176c 100644
--- a/lsp.c
+++ b/lsp.c
@@ -424,11 +424,11 @@ static bool lsp_receive(LSP *lsp, size_t max_size) {
? socket_read(lsp->socket, lsp->received_data + received_so_far, max_size)
: process_read(lsp->process, lsp->received_data + received_so_far, max_size);
- if (bytes_read == -1) {
+ if (bytes_read == 0) {
// no data
return true;
}
- if (bytes_read == 0) {
+ if (bytes_read == -1) {
lsp_set_error(lsp, "LSP server closed connection unexpectedly.");
return false;
}
@@ -613,20 +613,6 @@ static int lsp_communication_thread(void *data) {
// response, but who gives a fuck
write_request(lsp, &shutdown);
write_request(lsp, &exit);
-
- #if 0
- char buf[1024]={0};
- long long n = process_read(&lsp->process, buf, sizeof buf);
- if (n>0) {
- buf[n]=0;
- printf("%s\n",buf);
- }
- n = process_read_stderr(&lsp->process, buf, sizeof buf);
- if (n>0) {
- buf[n]=0;
- printf("\x1b[1m%s\x1b[0m\n",buf);
- }
- #endif
}
return 0;
}
diff --git a/main.c b/main.c
index 930d6da..b1f7e08 100644
--- a/main.c
+++ b/main.c
@@ -1,4 +1,6 @@
/*
+TODO:
+- put exact match first in selector
FUTURE FEATURES:
- autodetect indentation (tabs vs spaces)
- custom file/build command associations
diff --git a/os-posix.c b/os-posix.c
index 32c1853..6b274fb 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -285,10 +285,8 @@ static long long read_fd(int fd, char *error, size_t error_size, char *data, siz
ssize_t bytes_read = read(fd, data + so_far, size - so_far);
if (bytes_read > 0) {
so_far += (size_t)bytes_read;
- } else if (bytes_read == 0) {
+ } else if (bytes_read == 0 || errno == EAGAIN || errno == EWOULDBLOCK) {
return (long long)so_far;
- } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
- return so_far == 0 ? -1 : (long long)so_far;
} else if (errno == EPIPE) {
return -1;
} else {
diff --git a/os-win.c b/os-win.c
index fe08be3..6a50122 100644
--- a/os-win.c
+++ b/os-win.c
@@ -302,7 +302,7 @@ static long long process_read_handle(Process *process, HANDLE pipe, char *data,
DWORD bytes_read = 0, bytes_avail = 0, bytes_left = 0;
if (PeekNamedPipe(pipe, data, (DWORD)size, &bytes_read, &bytes_avail, &bytes_left)) {
if (bytes_read == 0) {
- return -1;
+ return 0;
} else {
ReadFile(pipe, data, (DWORD)size, &bytes_read, NULL); // make sure data is removed from pipe
return bytes_read;
@@ -490,9 +490,9 @@ long long socket_read(Socket *s, char *data, size_t size) {
} else {
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK) {
- return so_far == 0 ? -1 : (long long)so_far;
+ return (long long)so_far;
} else if (err == WSAECONNRESET) {
- return 0;
+ return -1;
} else {
strbuf_printf(s->error, "recv failed (error code %d)", err);
return -2;
diff --git a/os.h b/os.h
index bb52d63..4769063 100644
--- a/os.h
+++ b/os.h
@@ -136,8 +136,8 @@ long long process_write(Process *process, const char *data, size_t size);
///
/// \returns
/// -2 on error\n
-/// -1 if no data is available right now\n
-/// 0 on end of file\n
+/// -1 on end of file\n
+/// 0 if no data is available right now\n
/// or a positive number indicating the number of bytes read to `data` (at most `size`)\n
/// This does a nonblocking read.
long long process_read(Process *process, char *data, size_t size);
@@ -177,8 +177,8 @@ const char *socket_get_error(Socket *socket);
///
/// \returns
/// -2 on error\n
-/// -1 if no data is available right now\n
-/// 0 on end of file\n
+/// -1 on end of file\n
+/// 0 if no data is available right now\n
/// or a positive number indicating the number of bytes read to `data` (at most `size`)\n
/// This does a nonblocking read.
long long socket_read(Socket *socket, char *data, size_t size);