R: get rid of direct "write" system call, when sending messages to the server

This commit is contained in:
2026-03-23 14:13:07 +02:00
parent 0661b0721a
commit f0dfe89483
11 changed files with 59 additions and 70 deletions
+8 -10
View File
@@ -1,6 +1,9 @@
/* SPDX-License-Identifier: MIT */ /* SPDX-License-Identifier: MIT */
/* Copyright (c) 2026 Oleksandr Zhylin */ /* Copyright (c) 2026 Oleksandr Zhylin */
#include "alert.h"
#include "dialogue.h"
#include "server.h"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <fcntl.h> #include <fcntl.h>
#include <netinet/in.h> #include <netinet/in.h>
@@ -68,8 +71,6 @@ int32_t ui_file_select(file_args_t *f_args, query_args_t *q_args, int32_t idx) {
fl_item_t *l_selected; /* from the list */ fl_item_t *l_selected; /* from the list */
fl_item_t *f_selected = fl_item_t *f_selected =
&(f_args->f_selected); /* new copy of file struct (list be cleared) */ &(f_args->f_selected); /* new copy of file struct (list be cleared) */
uint32_t qlen;
char send_buf[256];
struct stat st = {0}; struct stat st = {0};
l_selected = fl_select(f_args->l_start, idx); l_selected = fl_select(f_args->l_start, idx);
@@ -91,15 +92,13 @@ int32_t ui_file_select(file_args_t *f_args, query_args_t *q_args, int32_t idx) {
fl_clear(&f_args->l_start, &f_args->l_current); fl_clear(&f_args->l_start, &f_args->l_current);
f_args->file_d = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, 0666); f_args->file_d = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (f_args->file_d == -1) { if (f_args->file_d == -1) {
qlen = sprintf(send_buf, "error: %s\n", f_selected->name); server_send_string(q_args, "error: %s\n", f_selected->name);
write(q_args->sd, send_buf, qlen);
perror(f_selected->name); perror(f_selected->name);
q_args->state = WAIT_CLIENT; q_args->state = WAIT_CLIENT;
free(file_path); free(file_path);
return -2; return -2;
} }
qlen = sprintf(send_buf, "file download [%s]\n", f_selected->name); server_send_string(q_args, "file download [%s]\n", f_selected->name);
write(q_args->sd, send_buf, qlen);
q_args->state = S_FILE_DOWNLOAD; q_args->state = S_FILE_DOWNLOAD;
free(file_path); free(file_path);
return OK; return OK;
@@ -150,9 +149,8 @@ void file_download(file_args_t *f_args, query_args_t *q_args) {
q_args->notification = malloc(strlen(answer) + 1); q_args->notification = malloc(strlen(answer) + 1);
strcpy(q_args->notification, answer); strcpy(q_args->notification, answer);
free(f_selected->name); free(f_selected->name);
sprintf(answer, "file list %u %u\n%n", fui->max_lines, fui->current_page, server_send_string(q_args, "file list %u %u\n%n", fui->max_lines,
&a_len); fui->current_page, &a_len);
write(q_args->sd, answer, a_len);
q_args->state = S_FILE_LIST; q_args->state = S_FILE_LIST;
} }
} }
@@ -183,7 +181,7 @@ int32_t file_upload_start(query_args_t *q_args) {
query_extract_from_buf(q_args->buf, &(q_args->buf_used), &query); query_extract_from_buf(q_args->buf, &(q_args->buf_used), &query);
if (strcmp(query, "accept")) { if (strcmp(query, "accept")) {
clear_file_in_query(q_args); clear_file_in_query(q_args);
write(STDERR_FILENO, query, strlen(query)); notification("Server response", dc_alert, query);
return -1; return -1;
} }
return 0; return 0;
+1 -1
View File
@@ -17,7 +17,7 @@ void init_alert(app_t *_app) { app = _app; }
void alert(const char *message) { notification("Alert", dc_alert, message); } void alert(const char *message) { notification("Alert", dc_alert, message); }
void notification(const char *title, enum d_color_scheme color, void notification(const char *title, enum dc_color_scheme color,
const char *f_message, ...) { const char *f_message, ...) {
if (app == NULL) if (app == NULL)
return; return;
+1 -1
View File
@@ -4,6 +4,6 @@
#include <stdarg.h> #include <stdarg.h>
void init_alert(app_t *app); void init_alert(app_t *app);
void alert(const char *message); void alert(const char *message);
void notification(const char *title, enum d_color_scheme color, void notification(const char *title, enum dc_color_scheme color,
const char *f_message, ...); const char *f_message, ...);
#endif #endif
+2 -1
View File
@@ -6,6 +6,7 @@
#include <unistd.h> #include <unistd.h>
#include <widget.h> #include <widget.h>
#include "../server.h"
typedef struct { typedef struct {
button_t *element; button_t *element;
@@ -31,7 +32,7 @@ void init_login_credentials_modal_cb(callback_args_t *args) {
app->params->uname[in_name->value_len] = 0; app->params->uname[in_name->value_len] = 0;
strncpy(app->params->pass, in_pass->value, in_pass->value_len); strncpy(app->params->pass, in_pass->value, in_pass->value_len);
app->params->pass[in_pass->value_len] = 0; app->params->pass[in_pass->value_len] = 0;
write(app->params->sd, app->params->uname, in_name->value_len); server_send_string(app->query_args, app->params->uname);
app->query_args->state = S_WAIT_SERVER; app->query_args->state = S_WAIT_SERVER;
d->needs_destroy = true; d->needs_destroy = true;
print_bars(app); print_bars(app);
+2 -1
View File
@@ -6,6 +6,7 @@
#include <unistd.h> #include <unistd.h>
#include <widget.h> #include <widget.h>
#include "../server.h"
typedef struct { typedef struct {
button_t *element; button_t *element;
@@ -26,7 +27,7 @@ void login_modal_option_cb(callback_args_t *args) {
d->needs_destroy = true; d->needs_destroy = true;
app->params->uname = malloc(sizeof "anonymous"); app->params->uname = malloc(sizeof "anonymous");
strcpy(app->params->uname, "anonymous"); strcpy(app->params->uname, "anonymous");
write(app->params->sd, app->params->uname, sizeof "anonymous" - 1); server_send_string(app->query_args, app->params->uname);
app->query_args->state = S_WAIT_SERVER; app->query_args->state = S_WAIT_SERVER;
print_bars(app); print_bars(app);
break; break;
+4 -7
View File
@@ -1,6 +1,5 @@
#include <ncursesw/ncurses.h> #include <ncursesw/ncurses.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@@ -9,6 +8,7 @@
#include "alert.h" #include "alert.h"
#include "group.h" #include "group.h"
#include "../server.h"
typedef struct { typedef struct {
button_t *element; button_t *element;
@@ -16,8 +16,6 @@ typedef struct {
} group_bt_t; } group_bt_t;
void init_register_modal_cb(callback_args_t *args) { void init_register_modal_cb(callback_args_t *args) {
char query[256];
int32_t qlen = 0;
callback_args_t d_args; callback_args_t d_args;
app_t *app = args->app; app_t *app = args->app;
dialogue_t *d = (dialogue_t *)app->active_widget; dialogue_t *d = (dialogue_t *)app->active_widget;
@@ -65,10 +63,9 @@ void init_register_modal_cb(callback_args_t *args) {
app->params->uname[in_name->value_len] = 0; app->params->uname[in_name->value_len] = 0;
strncpy(app->params->pass, in_pass->value, in_pass->value_len); strncpy(app->params->pass, in_pass->value, in_pass->value_len);
app->params->pass[in_pass->value_len] = 0; app->params->pass[in_pass->value_len] = 0;
sprintf(query, "register %s %s %*s\n%n", app->params->uname, server_send_string(app->query_args, "register %s %s %*s\n", app->params->uname,
app->params->pass, (int)in_email->value_len, in_email->value, app->params->pass, (int)in_email->value_len, in_email->value
&qlen); );
write(app->params->sd, query, qlen);
app->query_args->state = S_WAIT_REGISTER_CONFIRMATION; app->query_args->state = S_WAIT_REGISTER_CONFIRMATION;
} }
} }
+17 -13
View File
@@ -3,12 +3,13 @@
#include "widget_core.h" #include "widget_core.h"
#include <ncursesw/ncurses.h> #include <ncursesw/ncurses.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <widget.h>
#include <utils.h> #include <utils.h>
#include <widget.h>
#include "../server.h"
void upload_props_dialogue_modal_cb(callback_args_t *args) { void upload_props_dialogue_modal_cb(callback_args_t *args) {
callback_args_t d_args; callback_args_t d_args;
@@ -18,30 +19,33 @@ void upload_props_dialogue_modal_cb(callback_args_t *args) {
d_args.app = NULL; d_args.app = NULL;
d_args.element = app->active_widget; d_args.element = app->active_widget;
dialogue_default_callback(&d_args); dialogue_default_callback(&d_args);
char query[256];
int32_t query_len = 0; int32_t query_len = 0;
if (d_args.resp_data.code == cbrp_val) { if (d_args.resp_data.code == cbrp_val) {
checkbox_t *reg_cbx = (checkbox_t *)d->g_content->elements[0].element; checkbox_t *reg_cbx = (checkbox_t *)d->g_content->elements[0].element;
checkbox_t *anon_cbx = (checkbox_t *)d->g_content->elements[1].element; checkbox_t *anon_cbx = (checkbox_t *)d->g_content->elements[1].element;
checkbox_t *comp_cbx = (checkbox_t *)d->g_content->elements[2].element; checkbox_t *comp_cbx = (checkbox_t *)d->g_content->elements[2].element;
int32_t privileges = 0; int32_t privileges = 0;
if (anon_cbx->value == true) privileges |= 1; if (anon_cbx->value == true)
if (reg_cbx->value == true) privileges |= 2; privileges |= 1;
if (comp_cbx->value == true) privileges |= 4; if (reg_cbx->value == true)
privileges |= 2;
if (comp_cbx->value == true)
privileges |= 4;
switch (d_args.resp_data.val.val.num) { switch (d_args.resp_data.val.val.num) {
case 1: case 1:
query_len = server_send_string(app->query_args, "file upload \"%s\" %zu %d\n",
sprintf(query, "file upload \"%s\" %zu %d\n", app->query_args->file->name,
app->query_args->file->name, app->query_args->file->size, privileges); app->query_args->file->size, privileges);
/* app->query_args->file->description = /* app->query_args->file->description =
malloc(desc_input->value_len + 9); reserve space for \n:END:\n */ malloc(desc_input->value_len + 9); reserve space for \n:END:\n */
app->query_args->file->description = malloc(sizeof ("[Empty description]\n:END:\n")); app->query_args->file->description =
strcpy(app->query_args->file->description, "[Empty description]\n:END:\n"); malloc(sizeof("[Empty description]\n:END:\n"));
strcpy(app->query_args->file->description,
"[Empty description]\n:END:\n");
d->needs_destroy = true; d->needs_destroy = true;
app->query_args->state = S_UPLOAD_REQUESTED; app->query_args->state = S_UPLOAD_REQUESTED;
write(app->query_args->sd, query, query_len);
break; break;
case 2: case 2:
FREE_MLC(app->query_args->file->name); FREE_MLC(app->query_args->file->name);
+3 -6
View File
@@ -126,12 +126,11 @@ void query_loop(app_t *app) {
void wait_register(query_args_t *q_args) { void wait_register(query_args_t *q_args) {
params_t *params = q_args->params; params_t *params = q_args->params;
char r_buf[INBUFSIZE];
q_args->state = WAIT_REGISTER_CONFIRMATION; q_args->state = WAIT_REGISTER_CONFIRMATION;
char email[EMAIL_LEN]; char email[EMAIL_LEN];
ask_register(q_args->params, email); ask_register(q_args->params, email);
sprintf(r_buf, "register %s %s %s\n", params->uname, params->pass, email); server_send_string(q_args, "register %s %s %s\n", params->uname, params->pass,
write(q_args->sd, r_buf, strlen(r_buf) - 1); email);
} }
int upload_confirm_cb(app_t *app, char *query) { int upload_confirm_cb(app_t *app, char *query) {
@@ -278,7 +277,5 @@ void init_query_args(query_args_t *q_args, params_t *params) {
} }
void user_request_description(query_args_t *q_args) { void user_request_description(query_args_t *q_args) {
strcat(q_args->file->description, "\n\n:END:\n"); server_send_string(q_args, "%s\n\n:END:\n", q_args->file->description);
write(q_args->sd, q_args->file->description,
strlen(q_args->file->description) - 1);
} }
+6 -9
View File
@@ -48,7 +48,6 @@ int process_server_command(char *line, int l_len, app_t *app) {
uint64_t new_capacity; uint64_t new_capacity;
ui_file_list_t *fui = (ui_file_list_t *)q_args->file_list_ui; ui_file_list_t *fui = (ui_file_list_t *)q_args->file_list_ui;
char query[INBUFSIZE]; char query[INBUFSIZE];
int32_t q_len = 0;
char *cptr = strchr(line, ' '); char *cptr = strchr(line, ' ');
if (cptr != NULL && cptr > line) if (cptr != NULL && cptr > line)
@@ -60,7 +59,7 @@ int process_server_command(char *line, int l_len, app_t *app) {
PRINT_SRV_MESSAGE(q_args, l_len, line); PRINT_SRV_MESSAGE(q_args, l_len, line);
} }
if (q_args->params->uname != NULL && q_args->params->pass != NULL) { if (q_args->params->uname != NULL && q_args->params->pass != NULL) {
write(params->sd, q_args->params->uname, strlen(q_args->params->uname)); server_send_string(q_args, q_args->params->uname);
} else { } else {
q_args->state = S_ASK_LOGIN_TYPE; q_args->state = S_ASK_LOGIN_TYPE;
} }
@@ -72,7 +71,7 @@ int process_server_command(char *line, int l_len, app_t *app) {
if (q_args->params->pass == NULL) { if (q_args->params->pass == NULL) {
return 1; return 1;
} }
write(q_args->sd, params->pass, strlen(params->pass)); server_send_string(q_args, params->pass);
return 0; return 0;
} }
@@ -90,9 +89,8 @@ int process_server_command(char *line, int l_len, app_t *app) {
/* REGISTER CONFIRMATION */ /* REGISTER CONFIRMATION */
if (q_args->state == S_WAIT_REGISTER_CONFIRMATION) { if (q_args->state == S_WAIT_REGISTER_CONFIRMATION) {
if (!strcmp(line, "ok\n")) { if (!strcmp(line, "ok\n")) {
sprintf(query, "file list %u %u\n%n", fui->max_lines, fui->current_page, server_send_string(q_args, "file list %u %u\n", fui->max_lines,
&q_len); fui->current_page);
write(q_args->sd, query, q_len);
q_args->state = S_FILE_LIST; q_args->state = S_FILE_LIST;
destroy_dialogue(&(app->modal), app); destroy_dialogue(&(app->modal), app);
sprintf(query, sprintf(query,
@@ -116,9 +114,8 @@ int process_server_command(char *line, int l_len, app_t *app) {
/* WELCOME MES */ /* WELCOME MES */
if (!strncmp(line, "Welcome, ", ws_pos)) { if (!strncmp(line, "Welcome, ", ws_pos)) {
sprintf(query, "file list %u %u\n%n", fui->max_lines, fui->current_page, server_send_string(q_args, "file list %u %u\n", fui->max_lines,
&q_len); fui->current_page);
write(q_args->sd, query, q_len);
q_args->state = S_FILE_LIST; q_args->state = S_FILE_LIST;
return 0; return 0;
} }
+2 -2
View File
@@ -12,7 +12,7 @@
#include <widget_core.h> #include <widget_core.h>
#define MAX_IDS 64 #define MAX_IDS 64
enum d_color_scheme { dc_normal, dc_alert }; enum dc_color_scheme { dc_normal, dc_alert };
typedef struct { typedef struct {
widget_t w; widget_t w;
@@ -25,7 +25,7 @@ typedef struct {
bool needs_update; bool needs_update;
bool needs_destroy; bool needs_destroy;
coordinates_t *p_coordinates; coordinates_t *p_coordinates;
enum d_color_scheme color_scheme; enum dc_color_scheme color_scheme;
d_array_ptr_t id_map; d_array_ptr_t id_map;
} dialogue_t; } dialogue_t;
+12 -18
View File
@@ -4,6 +4,7 @@
#include "file_list.h" #include "file_list.h"
#include "../file_processor.h" #include "../file_processor.h"
#include "../fs.h" #include "../fs.h"
#include "../server.h"
#include "app.h" #include "app.h"
#include <bstrlib.h> #include <bstrlib.h>
#include <ncursesw/ncurses.h> #include <ncursesw/ncurses.h>
@@ -21,9 +22,7 @@ void file_list_cb(callback_args_t *args) {
app_t *app = args->app; app_t *app = args->app;
ui_file_list_t *fui = app->query_args->file_list_ui; ui_file_list_t *fui = app->query_args->file_list_ui;
int32_t key = *((int32_t *)args->data); int32_t key = *((int32_t *)args->data);
char query[256];
char q_prefix[128]; char q_prefix[128];
int32_t q_len = 0;
if (fui->search_key->slen > 0) { if (fui->search_key->slen > 0) {
sprintf(q_prefix, "file search name %s", fui->search_key->data); sprintf(q_prefix, "file search name %s", fui->search_key->data);
} else { } else {
@@ -44,12 +43,8 @@ void file_list_cb(callback_args_t *args) {
fui->search_key->slen = 0; fui->search_key->slen = 0;
sprintf(q_prefix, "file list"); sprintf(q_prefix, "file list");
} }
if (fui->search_key->slen > 0) { server_send_string(app->query_args, "%s %u %u\n", q_prefix,
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len); fui->max_lines, 1);
} else {
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len);
}
write(app->query_args->sd, query, q_len);
reset_file_list(fui); reset_file_list(fui);
} else { } else {
bconchar(fui->search_key, key); bconchar(fui->search_key, key);
@@ -65,9 +60,8 @@ void file_list_cb(callback_args_t *args) {
draw_file_list(fui); draw_file_list(fui);
} else if (fui->current_page < fui->pages) { } else if (fui->current_page < fui->pages) {
app->query_args->state = S_FILE_LIST; app->query_args->state = S_FILE_LIST;
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, server_send_string(app->query_args, "%s %u %u\n", q_prefix,
fui->current_page + 1, &q_len); fui->max_lines, fui->current_page + 1);
write(app->query_args->sd, query, q_len);
reset_file_list(fui); reset_file_list(fui);
} }
break; break;
@@ -77,9 +71,8 @@ void file_list_cb(callback_args_t *args) {
draw_file_list(fui); draw_file_list(fui);
} else if (fui->current_page > 1) { } else if (fui->current_page > 1) {
app->query_args->state = S_FILE_LIST; app->query_args->state = S_FILE_LIST;
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, server_send_string(app->query_args, "%s %u %u\n", q_prefix,
fui->current_page - 1, &q_len); fui->max_lines, fui->current_page - 1);
write(app->query_args->sd, query, q_len);
reset_file_list(fui); reset_file_list(fui);
fui->activate_last = true; fui->activate_last = true;
} }
@@ -90,8 +83,7 @@ void file_list_cb(callback_args_t *args) {
fui->search_key->slen = 0; fui->search_key->slen = 0;
sprintf(q_prefix, "file list"); sprintf(q_prefix, "file list");
app->query_args->state = S_FILE_LIST; app->query_args->state = S_FILE_LIST;
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len); server_send_string(app->query_args, "%s %u %u\n", q_prefix, fui->max_lines, 1);
write(app->query_args->sd, query, q_len);
reset_file_list(fui); reset_file_list(fui);
case '\n': case '\n':
ui_file_select(app->file_args, app->query_args, fui->current_idx + 1); ui_file_select(app->file_args, app->query_args, fui->current_idx + 1);
@@ -181,13 +173,15 @@ void draw_file_list(ui_file_list_t *fui) {
p_x = 1; p_x = 1;
int32_t p_len; int32_t p_len;
if (!fui->active_search && fui->search_key->slen > 0 && *(fui->start) == NULL) { if (!fui->active_search && fui->search_key->slen > 0 &&
*(fui->start) == NULL) {
bstring text = bfromStatic("[No data to show]"); bstring text = bfromStatic("[No data to show]");
for (; p_y < (sz_y_f / 2); p_y++) { for (; p_y < (sz_y_f / 2); p_y++) {
mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, ""); mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, "");
} }
p_len = (sz_x - text->slen) / 2; p_len = (sz_x - text->slen) / 2;
mvwprintw(parent_win, p_y++, p_x, "%*s%s%*s", p_len, "", text->data, p_len, ""); mvwprintw(parent_win, p_y++, p_x, "%*s%s%*s", p_len, "", text->data, p_len,
"");
for (; p_y < sz_y_f; p_y++) { for (; p_y < sz_y_f; p_y++) {
mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, ""); mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, "");
} }