~emersion/mrsh-dev

imrsh: Properly handle errors v1 PROPOSED

Sebastian: 1
 Properly handle errors

 4 files changed, 46 insertions(+), 8 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/mrsh-dev/patches/28353/mbox | git am -3
Learn more about email & git

[PATCH imrsh] Properly handle errors Export this patch

1. `assert` is no longer used to check that a function succeeded. A new
   function, `err`, is used instead to exit when a fatal error is
   encountered.
2. Calls to `calloc` are replaced by calls to `xcalloc`, which checks
   that the allocation succeeded.

Signed-off-by: Sebastian <sebastian@sebsite.pw>
---
 include/imrsh.h   |  8 ++++++++
 src/history.c     |  5 +++--
 src/interactive.c | 17 +++++++++++++----
 src/main.c        | 24 ++++++++++++++++++++++--
 4 files changed, 46 insertions(+), 8 deletions(-)
 create mode 100644 include/imrsh.h

diff --git a/include/imrsh.h b/include/imrsh.h
new file mode 100644
index 0000000..1e9d4a4
--- /dev/null
+++ b/include/imrsh.h
@@ -0,0 +1,8 @@
#ifndef _IMRSH_H
#define _IMRSH_H
#include <stddef.h>

void err(const char *s);
void *xcalloc(size_t n, size_t s);

#endif
diff --git a/src/history.c b/src/history.c
index 16c024e..cb151da 100644
--- a/src/history.c
+++ b/src/history.c
@@ -8,6 +8,7 @@
#include <sys/stat.h>
#include <wordexp.h>
#include "history.h"
#include "imrsh.h"

static void
posix_dirname(char *path, char *dname)
@@ -87,7 +88,7 @@ imrsh_history_load(struct imrsh_history *history)
			}
		}
		size_t l = strlen(line);
		tail = calloc(1, sizeof(struct imrsh_history_entry));
		tail = xcalloc(1, sizeof(struct imrsh_history_entry));
		tail->cmd = strdup(line);
		if (tail->cmd[l - 1] == '\n') {
			tail->cmd[l - 1] = '\0';
@@ -123,7 +124,7 @@ imrsh_history_append(struct imrsh_history *history, const char *cmd)
		return;
	}
	struct imrsh_history_entry *next =
		calloc(1, sizeof(struct imrsh_history_entry));
		xcalloc(1, sizeof(struct imrsh_history_entry));
	next->cmd = strdup(cmd);
	if (history->tail) {
		history->tail->next = next;
diff --git a/src/interactive.c b/src/interactive.c
index 0643f29..d615825 100644
--- a/src/interactive.c
+++ b/src/interactive.c
@@ -5,6 +5,7 @@
#include <string.h>
#include <tickit.h>
#include <unistd.h>
#include "imrsh.h"
#include "interactive.h"
#include "history.h"

@@ -12,7 +13,9 @@ static char *
buffer_insert(struct mrsh_buffer *buf, int at, size_t n)
{
	char *b = mrsh_buffer_add(buf, n);
	assert(b);
	if (!b) {
		err("mrsh_buffer_add");
	}
	memmove(&buf->data[at + n], &buf->data[at], buf->len - at + n);
	return &buf->data[at];
}
@@ -221,7 +224,9 @@ imrsh_set_cmd(struct imrsh_interactive *imrsh, const char *cmd)
	if (imrsh->read_buffer->len < l + 1) {
		char *_ = mrsh_buffer_add(imrsh->read_buffer,
				l + 1 - imrsh->read_buffer->len);
		assert(_);
		if (!_) {
			err("mrsh_buffer_add");
		}
	}
	int diff = (int)_l - (int)l;
	strcpy(imrsh->read_buffer->data, cmd);
@@ -350,11 +355,15 @@ void
interactive_init(struct imrsh_interactive *imrsh)
{
	imrsh->tt = tickit_term_new();
	assert(imrsh->tt);
	if (!imrsh->tt) {
		err("tickit_term_new");
	}
	imrsh->pens._default = tickit_pen_new_attrs(
			TICKIT_PEN_FG, -1, TICKIT_PEN_BG, -1, 0);
	if (!imrsh->pens._default) {
		err("tickit_pen_new_attrs");
	}
	imrsh->pens.typeahead = tickit_pen_new_attrs(TICKIT_PEN_FG, 240, 0);
	assert(imrsh->pens._default);

	tickit_term_set_input_fd(imrsh->tt, STDIN_FILENO);
	tickit_term_set_output_fd(imrsh->tt, STDOUT_FILENO);
diff --git a/src/main.c b/src/main.c
index f1d627a..fcfce66 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,3 @@
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <mrsh/ast.h>
@@ -7,14 +6,33 @@
#include <mrsh/entry.h>
#include <mrsh/parser.h>
#include <mrsh/shell.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "imrsh.h"
#include "history.h"
#include "interactive.h"

extern char **environ;

void
err(const char *s)
{
	perror(s);
	exit(EXIT_FAILURE);
}

void *
xcalloc(size_t n, size_t s)
{
	void *p = calloc(n, s);
	if (!p) {
		err("calloc");
	}
	return p;
}

static int
noninteractive_init(struct mrsh_init_args *init_args,
		struct mrsh_parser **parser)
@@ -44,7 +62,9 @@ int
main(int argc, char *argv[])
{
	struct mrsh_state *state = mrsh_state_create();
	assert(state);
	if (!state) {
		err("mrsh_state_create");
	}
	struct imrsh_history history = { 0 };
	struct mrsh_buffer read_buffer = { 0 };
	struct imrsh_interactive istate = {
-- 
2.34.1