R: get rid of direct "write" system call, when sending messages to the server
This commit is contained in:
+8
-10
@@ -1,6 +1,9 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
/* Copyright (c) 2026 Oleksandr Zhylin */
|
||||
|
||||
#include "alert.h"
|
||||
#include "dialogue.h"
|
||||
#include "server.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.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 *f_selected =
|
||||
&(f_args->f_selected); /* new copy of file struct (list be cleared) */
|
||||
uint32_t qlen;
|
||||
char send_buf[256];
|
||||
struct stat st = {0};
|
||||
|
||||
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);
|
||||
f_args->file_d = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||
if (f_args->file_d == -1) {
|
||||
qlen = sprintf(send_buf, "error: %s\n", f_selected->name);
|
||||
write(q_args->sd, send_buf, qlen);
|
||||
server_send_string(q_args, "error: %s\n", f_selected->name);
|
||||
perror(f_selected->name);
|
||||
q_args->state = WAIT_CLIENT;
|
||||
free(file_path);
|
||||
return -2;
|
||||
}
|
||||
qlen = sprintf(send_buf, "file download [%s]\n", f_selected->name);
|
||||
write(q_args->sd, send_buf, qlen);
|
||||
server_send_string(q_args, "file download [%s]\n", f_selected->name);
|
||||
q_args->state = S_FILE_DOWNLOAD;
|
||||
free(file_path);
|
||||
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);
|
||||
strcpy(q_args->notification, answer);
|
||||
free(f_selected->name);
|
||||
sprintf(answer, "file list %u %u\n%n", fui->max_lines, fui->current_page,
|
||||
&a_len);
|
||||
write(q_args->sd, answer, a_len);
|
||||
server_send_string(q_args, "file list %u %u\n%n", fui->max_lines,
|
||||
fui->current_page, &a_len);
|
||||
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);
|
||||
if (strcmp(query, "accept")) {
|
||||
clear_file_in_query(q_args);
|
||||
write(STDERR_FILENO, query, strlen(query));
|
||||
notification("Server response", dc_alert, query);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ void init_alert(app_t *_app) { app = _app; }
|
||||
|
||||
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, ...) {
|
||||
if (app == NULL)
|
||||
return;
|
||||
|
||||
+1
-1
@@ -4,6 +4,6 @@
|
||||
#include <stdarg.h>
|
||||
void init_alert(app_t *app);
|
||||
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, ...);
|
||||
#endif
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <widget.h>
|
||||
#include "../server.h"
|
||||
|
||||
typedef struct {
|
||||
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;
|
||||
strncpy(app->params->pass, in_pass->value, in_pass->value_len);
|
||||
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;
|
||||
d->needs_destroy = true;
|
||||
print_bars(app);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <widget.h>
|
||||
#include "../server.h"
|
||||
|
||||
typedef struct {
|
||||
button_t *element;
|
||||
@@ -26,7 +27,7 @@ void login_modal_option_cb(callback_args_t *args) {
|
||||
d->needs_destroy = true;
|
||||
app->params->uname = malloc(sizeof "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;
|
||||
print_bars(app);
|
||||
break;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
@@ -9,6 +8,7 @@
|
||||
|
||||
#include "alert.h"
|
||||
#include "group.h"
|
||||
#include "../server.h"
|
||||
|
||||
typedef struct {
|
||||
button_t *element;
|
||||
@@ -16,8 +16,6 @@ typedef struct {
|
||||
} group_bt_t;
|
||||
|
||||
void init_register_modal_cb(callback_args_t *args) {
|
||||
char query[256];
|
||||
int32_t qlen = 0;
|
||||
callback_args_t d_args;
|
||||
app_t *app = args->app;
|
||||
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;
|
||||
strncpy(app->params->pass, in_pass->value, in_pass->value_len);
|
||||
app->params->pass[in_pass->value_len] = 0;
|
||||
sprintf(query, "register %s %s %*s\n%n", app->params->uname,
|
||||
app->params->pass, (int)in_email->value_len, in_email->value,
|
||||
&qlen);
|
||||
write(app->params->sd, query, qlen);
|
||||
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->query_args->state = S_WAIT_REGISTER_CONFIRMATION;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@
|
||||
#include "widget_core.h"
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <widget.h>
|
||||
#include <utils.h>
|
||||
#include <widget.h>
|
||||
|
||||
#include "../server.h"
|
||||
|
||||
void upload_props_dialogue_modal_cb(callback_args_t *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.element = app->active_widget;
|
||||
dialogue_default_callback(&d_args);
|
||||
char query[256];
|
||||
int32_t query_len = 0;
|
||||
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 *comp_cbx = (checkbox_t *)d->g_content->elements[2].element;
|
||||
|
||||
int32_t privileges = 0;
|
||||
if (anon_cbx->value == true) privileges |= 1;
|
||||
if (reg_cbx->value == true) privileges |= 2;
|
||||
if (comp_cbx->value == true) privileges |= 4;
|
||||
if (anon_cbx->value == true)
|
||||
privileges |= 1;
|
||||
if (reg_cbx->value == true)
|
||||
privileges |= 2;
|
||||
if (comp_cbx->value == true)
|
||||
privileges |= 4;
|
||||
|
||||
switch (d_args.resp_data.val.val.num) {
|
||||
case 1:
|
||||
query_len =
|
||||
sprintf(query, "file upload \"%s\" %zu %d\n",
|
||||
app->query_args->file->name, app->query_args->file->size, privileges);
|
||||
server_send_string(app->query_args, "file upload \"%s\" %zu %d\n",
|
||||
app->query_args->file->name,
|
||||
app->query_args->file->size, privileges);
|
||||
/* app->query_args->file->description =
|
||||
malloc(desc_input->value_len + 9); reserve space for \n:END:\n */
|
||||
app->query_args->file->description = malloc(sizeof ("[Empty description]\n:END:\n"));
|
||||
strcpy(app->query_args->file->description, "[Empty description]\n:END:\n");
|
||||
app->query_args->file->description =
|
||||
malloc(sizeof("[Empty description]\n:END:\n"));
|
||||
strcpy(app->query_args->file->description,
|
||||
"[Empty description]\n:END:\n");
|
||||
d->needs_destroy = true;
|
||||
app->query_args->state = S_UPLOAD_REQUESTED;
|
||||
write(app->query_args->sd, query, query_len);
|
||||
break;
|
||||
case 2:
|
||||
FREE_MLC(app->query_args->file->name);
|
||||
|
||||
+3
-6
@@ -126,12 +126,11 @@ void query_loop(app_t *app) {
|
||||
|
||||
void wait_register(query_args_t *q_args) {
|
||||
params_t *params = q_args->params;
|
||||
char r_buf[INBUFSIZE];
|
||||
q_args->state = WAIT_REGISTER_CONFIRMATION;
|
||||
char email[EMAIL_LEN];
|
||||
ask_register(q_args->params, email);
|
||||
sprintf(r_buf, "register %s %s %s\n", params->uname, params->pass, email);
|
||||
write(q_args->sd, r_buf, strlen(r_buf) - 1);
|
||||
server_send_string(q_args, "register %s %s %s\n", params->uname, params->pass,
|
||||
email);
|
||||
}
|
||||
|
||||
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) {
|
||||
strcat(q_args->file->description, "\n\n:END:\n");
|
||||
write(q_args->sd, q_args->file->description,
|
||||
strlen(q_args->file->description) - 1);
|
||||
server_send_string(q_args, "%s\n\n:END:\n", q_args->file->description);
|
||||
}
|
||||
+6
-9
@@ -48,7 +48,6 @@ int process_server_command(char *line, int l_len, app_t *app) {
|
||||
uint64_t new_capacity;
|
||||
ui_file_list_t *fui = (ui_file_list_t *)q_args->file_list_ui;
|
||||
char query[INBUFSIZE];
|
||||
int32_t q_len = 0;
|
||||
|
||||
char *cptr = strchr(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);
|
||||
}
|
||||
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 {
|
||||
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) {
|
||||
return 1;
|
||||
}
|
||||
write(q_args->sd, params->pass, strlen(params->pass));
|
||||
server_send_string(q_args, params->pass);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -90,9 +89,8 @@ int process_server_command(char *line, int l_len, app_t *app) {
|
||||
/* REGISTER CONFIRMATION */
|
||||
if (q_args->state == S_WAIT_REGISTER_CONFIRMATION) {
|
||||
if (!strcmp(line, "ok\n")) {
|
||||
sprintf(query, "file list %u %u\n%n", fui->max_lines, fui->current_page,
|
||||
&q_len);
|
||||
write(q_args->sd, query, q_len);
|
||||
server_send_string(q_args, "file list %u %u\n", fui->max_lines,
|
||||
fui->current_page);
|
||||
q_args->state = S_FILE_LIST;
|
||||
destroy_dialogue(&(app->modal), app);
|
||||
sprintf(query,
|
||||
@@ -116,9 +114,8 @@ int process_server_command(char *line, int l_len, app_t *app) {
|
||||
|
||||
/* WELCOME MES */
|
||||
if (!strncmp(line, "Welcome, ", ws_pos)) {
|
||||
sprintf(query, "file list %u %u\n%n", fui->max_lines, fui->current_page,
|
||||
&q_len);
|
||||
write(q_args->sd, query, q_len);
|
||||
server_send_string(q_args, "file list %u %u\n", fui->max_lines,
|
||||
fui->current_page);
|
||||
q_args->state = S_FILE_LIST;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include <widget_core.h>
|
||||
#define MAX_IDS 64
|
||||
|
||||
enum d_color_scheme { dc_normal, dc_alert };
|
||||
enum dc_color_scheme { dc_normal, dc_alert };
|
||||
|
||||
typedef struct {
|
||||
widget_t w;
|
||||
@@ -25,7 +25,7 @@ typedef struct {
|
||||
bool needs_update;
|
||||
bool needs_destroy;
|
||||
coordinates_t *p_coordinates;
|
||||
enum d_color_scheme color_scheme;
|
||||
enum dc_color_scheme color_scheme;
|
||||
d_array_ptr_t id_map;
|
||||
} dialogue_t;
|
||||
|
||||
|
||||
+12
-18
@@ -4,6 +4,7 @@
|
||||
#include "file_list.h"
|
||||
#include "../file_processor.h"
|
||||
#include "../fs.h"
|
||||
#include "../server.h"
|
||||
#include "app.h"
|
||||
#include <bstrlib.h>
|
||||
#include <ncursesw/ncurses.h>
|
||||
@@ -21,9 +22,7 @@ void file_list_cb(callback_args_t *args) {
|
||||
app_t *app = args->app;
|
||||
ui_file_list_t *fui = app->query_args->file_list_ui;
|
||||
int32_t key = *((int32_t *)args->data);
|
||||
char query[256];
|
||||
char q_prefix[128];
|
||||
int32_t q_len = 0;
|
||||
if (fui->search_key->slen > 0) {
|
||||
sprintf(q_prefix, "file search name %s", fui->search_key->data);
|
||||
} else {
|
||||
@@ -44,12 +43,8 @@ void file_list_cb(callback_args_t *args) {
|
||||
fui->search_key->slen = 0;
|
||||
sprintf(q_prefix, "file list");
|
||||
}
|
||||
if (fui->search_key->slen > 0) {
|
||||
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len);
|
||||
} else {
|
||||
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len);
|
||||
}
|
||||
write(app->query_args->sd, query, q_len);
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix,
|
||||
fui->max_lines, 1);
|
||||
reset_file_list(fui);
|
||||
} else {
|
||||
bconchar(fui->search_key, key);
|
||||
@@ -65,9 +60,8 @@ void file_list_cb(callback_args_t *args) {
|
||||
draw_file_list(fui);
|
||||
} else if (fui->current_page < fui->pages) {
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines,
|
||||
fui->current_page + 1, &q_len);
|
||||
write(app->query_args->sd, query, q_len);
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix,
|
||||
fui->max_lines, fui->current_page + 1);
|
||||
reset_file_list(fui);
|
||||
}
|
||||
break;
|
||||
@@ -77,9 +71,8 @@ void file_list_cb(callback_args_t *args) {
|
||||
draw_file_list(fui);
|
||||
} else if (fui->current_page > 1) {
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines,
|
||||
fui->current_page - 1, &q_len);
|
||||
write(app->query_args->sd, query, q_len);
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix,
|
||||
fui->max_lines, fui->current_page - 1);
|
||||
reset_file_list(fui);
|
||||
fui->activate_last = true;
|
||||
}
|
||||
@@ -90,8 +83,7 @@ void file_list_cb(callback_args_t *args) {
|
||||
fui->search_key->slen = 0;
|
||||
sprintf(q_prefix, "file list");
|
||||
app->query_args->state = S_FILE_LIST;
|
||||
sprintf(query, "%s %u %u\n%n", q_prefix, fui->max_lines, 1, &q_len);
|
||||
write(app->query_args->sd, query, q_len);
|
||||
server_send_string(app->query_args, "%s %u %u\n", q_prefix, fui->max_lines, 1);
|
||||
reset_file_list(fui);
|
||||
case '\n':
|
||||
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;
|
||||
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]");
|
||||
for (; p_y < (sz_y_f / 2); p_y++) {
|
||||
mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, "");
|
||||
}
|
||||
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++) {
|
||||
mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - 1, "");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user