From: Grant Erickson <gerickson@nuovations.com>
To: ell@lists.linux.dev
Cc: Marcel Holtmann <marcel@holtmann.org>
Subject: [RFC PATCH v5] edit: change return signatures from 'bool' to 'int'
Date: Thu, 4 Apr 2024 17:20:09 -0700 [thread overview]
Message-ID: <20240405002011.3480886-7-gerickson@nuovations.com> (raw)
In-Reply-To: <20240405002011.3480886-1-gerickson@nuovations.com>
Using an 'int' signature with 0 for success and < 0 for failure with
failure enumerations sources from the negated POSIX errors from errno.h
eases system integration by precluding system integrators from having
to synthesize their own errors on the prior 'false' returns.
---
ell/edit.c | 136 ++++++++++++++++++++++++++---------------------------
ell/edit.h | 40 ++++++++--------
2 files changed, 88 insertions(+), 88 deletions(-)
diff --git a/ell/edit.c b/ell/edit.c
index 54cfc566c4ad..d6329d063c78 100644
--- a/ell/edit.c
+++ b/ell/edit.c
@@ -234,18 +234,18 @@ static void update_debug(struct l_edit *edit)
l_free(tmp);
}
-LIB_EXPORT bool l_edit_set_debug_handler(struct l_edit *edit,
+LIB_EXPORT int l_edit_set_debug_handler(struct l_edit *edit,
l_edit_debug_func_t handler, void *user_data)
{
if (!edit)
- return false;
+ return -EINVAL;
edit->debug_handler = handler;
edit->debug_data = user_data;
update_debug(edit);
- return true;
+ return 0;
}
static void update_display(struct l_edit *edit)
@@ -275,36 +275,36 @@ static void update_display(struct l_edit *edit)
update_debug(edit);
}
-LIB_EXPORT bool l_edit_set_display_handler(struct l_edit *edit,
+LIB_EXPORT int l_edit_set_display_handler(struct l_edit *edit,
l_edit_display_func_t handler, void *user_data)
{
if (!edit)
- return false;
+ return -EINVAL;
edit->display_handler = handler;
edit->display_data = user_data;
update_display(edit);
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_set_max_display_length(struct l_edit *edit, size_t len)
+LIB_EXPORT int l_edit_set_max_display_length(struct l_edit *edit, size_t len)
{
if (!edit)
- return false;
+ return -EINVAL;
edit->max_display_len= len;
update_display(edit);
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_set_max_input_length(struct l_edit *edit, size_t len)
+LIB_EXPORT int l_edit_set_max_input_length(struct l_edit *edit, size_t len)
{
if (!edit)
- return false;
+ return -EINVAL;
/* When switching to unlimited input length, then nothing is there
* do to, except storing the value. Refreshing the display is not
@@ -313,7 +313,7 @@ LIB_EXPORT bool l_edit_set_max_input_length(struct l_edit *edit, size_t len)
if (len == 0) {
edit->max_input_len = 0;
update_debug(edit);
- return true;
+ return 0;
}
edit->max_input_len = len;
@@ -334,13 +334,13 @@ LIB_EXPORT bool l_edit_set_max_input_length(struct l_edit *edit, size_t len)
update_debug(edit);
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_set_history_size(struct l_edit *edit, unsigned int size)
+LIB_EXPORT int l_edit_set_history_size(struct l_edit *edit, unsigned int size)
{
if (!edit)
- return false;
+ return -EINVAL;
edit->max_list_size = size;
@@ -382,17 +382,17 @@ LIB_EXPORT bool l_edit_set_history_size(struct l_edit *edit, unsigned int size)
update_display(edit);
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_refresh(struct l_edit *edit)
+LIB_EXPORT int l_edit_refresh(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
update_display(edit);
- return true;
+ return 0;
}
LIB_EXPORT bool l_edit_is_empty(struct l_edit *edit)
@@ -463,10 +463,10 @@ LIB_EXPORT char *l_edit_enter(struct l_edit *edit)
return str;
}
-LIB_EXPORT bool l_edit_reset(struct l_edit *edit, const char *input)
+LIB_EXPORT int l_edit_reset(struct l_edit *edit, const char *input)
{
if (!edit)
- return false;
+ return -EINVAL;
/* Reset the main item back to the head of the history before
* resetting it or overwriting it with the provided input.
@@ -476,17 +476,17 @@ LIB_EXPORT bool l_edit_reset(struct l_edit *edit, const char *input)
reset_input_buf(edit->main, input);
update_display(edit);
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_insert(struct l_edit *edit, wint_t ch)
+LIB_EXPORT int l_edit_insert(struct l_edit *edit, wint_t ch)
{
if (!edit)
- return false;
+ return -EINVAL;
/* Check if the max input length has already been reached */
if (edit->max_input_len && edit->main->len >= edit->max_input_len)
- return false;
+ return -EALREADY;
/* This will magically grow the buffer to make room for at least
* one wide character.
@@ -497,7 +497,7 @@ LIB_EXPORT bool l_edit_insert(struct l_edit *edit, wint_t ch)
* string, there is nothing more to add.
*/
if (edit->main->len == SIZE_MAX)
- return false;
+ return -ENOBUFS;
/* If the cursor is not at the end, the new character has to be
* inserted and for thus the tail portion needs to move one
@@ -513,13 +513,13 @@ LIB_EXPORT bool l_edit_insert(struct l_edit *edit, wint_t ch)
edit->main->buf[edit->main->len] = L'\0';
update_display(edit);
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_delete(struct l_edit *edit)
+LIB_EXPORT int l_edit_delete(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* If the cursor is not at the end, deletion of a character means
* that the tail moves one character forward.
@@ -533,13 +533,13 @@ LIB_EXPORT bool l_edit_delete(struct l_edit *edit)
update_display(edit);
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_delete_all(struct l_edit *edit)
+LIB_EXPORT int l_edit_delete_all(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* Keep the buffer allocated, but reset it to an empty string */
edit->main->buf[0] = L'\0';
@@ -547,26 +547,26 @@ LIB_EXPORT bool l_edit_delete_all(struct l_edit *edit)
edit->main->len = 0;
update_display(edit);
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_truncate(struct l_edit *edit)
+LIB_EXPORT int l_edit_truncate(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* Keep the buffer allocated, but truncate after the cursor */
edit->main->buf[edit->main->pos] = L'\0';
edit->main->len = edit->main->pos;
update_display(edit);
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_backspace(struct l_edit *edit)
+LIB_EXPORT int l_edit_backspace(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* If the cursor is not at the beginning, the backspace operation
* means that tail has to move one character forward.
@@ -581,13 +581,13 @@ LIB_EXPORT bool l_edit_backspace(struct l_edit *edit)
update_display(edit);
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_move_left(struct l_edit *edit)
+LIB_EXPORT int l_edit_move_left(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* If the cursor is not at the beginning, then move it one back */
if (edit->main->pos > 0) {
@@ -595,13 +595,13 @@ LIB_EXPORT bool l_edit_move_left(struct l_edit *edit)
update_display(edit);
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_move_right(struct l_edit *edit)
+LIB_EXPORT int l_edit_move_right(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* If the cursor is not at the end, then move it one forward */
if (edit->main->pos != edit->main->len) {
@@ -609,13 +609,13 @@ LIB_EXPORT bool l_edit_move_right(struct l_edit *edit)
update_display(edit);
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_move_home(struct l_edit *edit)
+LIB_EXPORT int l_edit_move_home(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* If the cursor is not at the beginning, move it there */
if (edit->main->pos != 0) {
@@ -623,13 +623,13 @@ LIB_EXPORT bool l_edit_move_home(struct l_edit *edit)
update_display(edit);
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_move_end(struct l_edit *edit)
+LIB_EXPORT int l_edit_move_end(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* If the cursor is not at the end, move it there */
if (edit->main->pos != edit->main->len) {
@@ -637,13 +637,13 @@ LIB_EXPORT bool l_edit_move_end(struct l_edit *edit)
update_display(edit);
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_history_backward(struct l_edit *edit)
+LIB_EXPORT int l_edit_history_backward(struct l_edit *edit)
{
if (!edit)
- return false;
+ return -EINVAL;
/* If there is another item in the history list, move the main
* item to that and enforce the max input length on the new item.
@@ -654,15 +654,15 @@ LIB_EXPORT bool l_edit_history_backward(struct l_edit *edit)
update_display(edit);
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_history_forward(struct l_edit *edit)
+LIB_EXPORT int l_edit_history_forward(struct l_edit *edit)
{
struct input_buf *buf;
if (!edit)
- return false;
+ return -EINVAL;
/* Walk the list of history items until the current main item
* matches the next item, then move the main item to current
@@ -677,10 +677,10 @@ LIB_EXPORT bool l_edit_history_forward(struct l_edit *edit)
}
}
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_history_load(struct l_edit *edit, const char *pathname)
+LIB_EXPORT int l_edit_history_load(struct l_edit *edit, const char *pathname)
{
static size_t initial_line_size = 16;
struct input_buf *buf;
@@ -689,17 +689,17 @@ LIB_EXPORT bool l_edit_history_load(struct l_edit *edit, const char *pathname)
int fd;
if (!edit)
- return false;
+ return -EINVAL;
if (!pathname)
- return false;
+ return -EINVAL;
if (!edit->max_list_size)
- return true;
+ return 0;
fd = open(pathname, O_RDONLY);
if (fd < 0)
- return false;
+ return -errno;
str = l_string_new(initial_line_size);
@@ -746,23 +746,23 @@ LIB_EXPORT bool l_edit_history_load(struct l_edit *edit, const char *pathname)
edit->list_count = count;
update_display(edit);
- return true;
+ return 0;
}
-LIB_EXPORT bool l_edit_history_save(struct l_edit *edit, const char *pathname)
+LIB_EXPORT int l_edit_history_save(struct l_edit *edit, const char *pathname)
{
struct input_buf *buf;
int fd;
if (!edit)
- return false;
+ return -EINVAL;
if (!pathname)
- return false;
+ return -EINVAL;
fd = open(pathname, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd < 0)
- return false;
+ return -errno;
buf = edit->head->next;
@@ -781,5 +781,5 @@ LIB_EXPORT bool l_edit_history_save(struct l_edit *edit, const char *pathname)
close(fd);
- return true;
+ return 0;
}
diff --git a/ell/edit.h b/ell/edit.h
index decf261c7b9e..811246578894 100644
--- a/ell/edit.h
+++ b/ell/edit.h
@@ -22,35 +22,35 @@ void l_edit_free(struct l_edit *edit);
typedef void (*l_edit_debug_func_t) (struct l_edit *edit, const char *str, void *user_data);
-bool l_edit_set_debug_handler(struct l_edit *edit,
+int l_edit_set_debug_handler(struct l_edit *edit,
l_edit_debug_func_t handler, void *user_data);
typedef void (*l_edit_display_func_t) (struct l_edit *edit, const wchar_t *wstr, size_t wlen,
size_t pos, void *user_data);
-bool l_edit_set_display_handler(struct l_edit *edit,
+int l_edit_set_display_handler(struct l_edit *edit,
l_edit_display_func_t handler, void *user_data);
-bool l_edit_set_max_display_length(struct l_edit *edit, size_t len);
-bool l_edit_set_max_input_length(struct l_edit *edit, size_t len);
-bool l_edit_set_history_size(struct l_edit *edit, unsigned int size);
-bool l_edit_refresh(struct l_edit *edit);
+int l_edit_set_max_display_length(struct l_edit *edit, size_t len);
+int l_edit_set_max_input_length(struct l_edit *edit, size_t len);
+int l_edit_set_history_size(struct l_edit *edit, unsigned int size);
+int l_edit_refresh(struct l_edit *edit);
bool l_edit_is_empty(struct l_edit *edit);
char *l_edit_enter(struct l_edit *edit);
-bool l_edit_reset(struct l_edit *edit, const char *input);
-bool l_edit_insert(struct l_edit *edit, wint_t ch);
-bool l_edit_delete(struct l_edit *edit);
-bool l_edit_delete_all(struct l_edit *edit);
-bool l_edit_truncate(struct l_edit *edit);
-bool l_edit_backspace(struct l_edit *edit);
-bool l_edit_move_left(struct l_edit *edit);
-bool l_edit_move_right(struct l_edit *edit);
-bool l_edit_move_home(struct l_edit *edit);
-bool l_edit_move_end(struct l_edit *edit);
-bool l_edit_history_backward(struct l_edit *edit);
-bool l_edit_history_forward(struct l_edit *edit);
-bool l_edit_history_load(struct l_edit *edit, const char *pathname);
-bool l_edit_history_save(struct l_edit *edit, const char *pathname);
+int l_edit_reset(struct l_edit *edit, const char *input);
+int l_edit_insert(struct l_edit *edit, wint_t ch);
+int l_edit_delete(struct l_edit *edit);
+int l_edit_delete_all(struct l_edit *edit);
+int l_edit_truncate(struct l_edit *edit);
+int l_edit_backspace(struct l_edit *edit);
+int l_edit_move_left(struct l_edit *edit);
+int l_edit_move_right(struct l_edit *edit);
+int l_edit_move_home(struct l_edit *edit);
+int l_edit_move_end(struct l_edit *edit);
+int l_edit_history_backward(struct l_edit *edit);
+int l_edit_history_forward(struct l_edit *edit);
+int l_edit_history_load(struct l_edit *edit, const char *pathname);
+int l_edit_history_save(struct l_edit *edit, const char *pathname);
#ifdef __cplusplus
}
--
2.42.0
next prev parent reply other threads:[~2024-04-05 0:20 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-05 0:20 [RFC PATCH v5] Line Editor Grant Erickson
2024-04-05 0:20 ` [RFC PATCH v5] edit: Initial revision Grant Erickson
2024-04-05 0:20 ` [RFC PATCH v5] ell: Add include directive for 'ell/edit.h' Grant Erickson
2024-04-05 0:20 ` [RFC PATCH v5] ell/Makefile: Added 'edit.[ch]' to HEADERS and SOURCES Grant Erickson
2024-04-05 0:20 ` [RFC PATCH v5] edit: Added 'l_edit_*' symbols Grant Erickson
2024-04-05 0:20 ` [RFC PATCH v5] edit: pass the l_edit instance to the display and debug handlers Grant Erickson
2024-04-05 0:20 ` Grant Erickson [this message]
2024-04-05 0:20 ` [RFC PATCH v5] edit: change the 'l_edit_enter' return signature from 'char *' to 'int' Grant Erickson
2024-04-05 0:20 ` [RFC PATCH v5] edit: improve error handling of 'mbstowcs' and 'wcstombs' functions Grant Erickson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240405002011.3480886-7-gerickson@nuovations.com \
--to=gerickson@nuovations.com \
--cc=ell@lists.linux.dev \
--cc=marcel@holtmann.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).