~emersion/mrsh-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH] history: check for HISTFILE env variable

Details
Message ID
<20210117082556.10779-1-merlin@nixnetmail.com>
DKIM signature
missing
Download raw message
Patch: +19 -3
if exists and not empty, use its as history file's path.
---
 frontend/readline.c | 19 +++++++++++++++++--
 main.c              |  3 ++-
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/frontend/readline.c b/frontend/readline.c
index 19880fd..6cb53ec 100644
--- a/frontend/readline.c
+++ b/frontend/readline.c
@@ -38,8 +38,23 @@ static void sigint_handler(int n) {

static char *get_history_path(void) {
	const char *home = getenv("HOME");
	int len = snprintf(NULL, 0, "%s/.mrsh_history", home);
	char *path = malloc(len + 1);
	extern struct mrsh_state *state;
	const char *histfile = mrsh_env_get(state, "HISTFILE", NULL);
	int len;
	char *path;

	if (histfile != NULL && strcmp(histfile, "") != 0) {
		len = strlen(histfile);
		path = malloc(len + 1);
		if (path == NULL) {
			return NULL;
		}
		snprintf(path, len + 1, "%s", histfile);
		return path;
	}

	len = snprintf(NULL, 0, "%s/.mrsh_history", home);
	path = malloc(len + 1);
	if (path == NULL) {
		return NULL;
	}
diff --git a/main.c b/main.c
index 0f433d2..25e278e 100644
--- a/main.c
+++ b/main.c
@@ -15,9 +15,10 @@
#include "frontend.h"

extern char **environ;
struct mrsh_state *state;

int main(int argc, char *argv[]) {
	struct mrsh_state *state = mrsh_state_create();
	state = mrsh_state_create();

	struct mrsh_init_args init_args = {0};
	if (mrsh_process_args(state, &init_args, argc, argv) != 0) {
-- 
2.30.0
Details
Message ID
<hXJU8FEHgo4ilmmE5xdewUGtyCR5aZgB7Dyc7aAoC5Mh1xmBqfB2QZn3M2rvZ0MIH3pKUtOux3W6XDAmV8g2dGhquy62GqGPRA46ZWVeZPM=@emersion.fr>
In-Reply-To
<20210117082556.10779-1-merlin@nixnetmail.com> (view parent)
DKIM signature
missing
Download raw message
On Sunday, January 17th, 2021 at 9:25 AM, Merlin <merlin@nixnetmail.com> wrote:

> if exists and not empty, use its as history file's path.

Sorry for the delay.

The idea looks good to me. Here are two comments.

> ---
>  frontend/readline.c | 19 +++++++++++++++++--
>  main.c              |  3 ++-
>  2 files changed, 19 insertions(+), 3 deletions(-)
> 

> diff --git a/frontend/readline.c b/frontend/readline.c
> index 19880fd..6cb53ec 100644
> --- a/frontend/readline.c
> +++ b/frontend/readline.c
> @@ -38,8 +38,23 @@ static void sigint_handler(int n) {
> 

>  static char *get_history_path(void) {
>  	const char *home = getenv("HOME");
> -	int len = snprintf(NULL, 0, "%s/.mrsh_history", home);
> -	char *path = malloc(len + 1);
> +	extern struct mrsh_state *state;
> +	const char *histfile = mrsh_env_get(state, "HISTFILE", NULL);
> +	int len;
> +	char *path;
> +
> +	if (histfile != NULL && strcmp(histfile, "") != 0) {
> +		len = strlen(histfile);
> +		path = malloc(len + 1);
> +		if (path == NULL) {
> +			return NULL;
> +		}
> +		snprintf(path, len + 1, "%s", histfile);
> +		return path;

We can use strdup here.

> +	}
> +
> +	len = snprintf(NULL, 0, "%s/.mrsh_history", home);
> +	path = malloc(len + 1);
>  	if (path == NULL) {
>  		return NULL;
>  	}
> diff --git a/main.c b/main.c
> index 0f433d2..25e278e 100644
> --- a/main.c
> +++ b/main.c
> @@ -15,9 +15,10 @@
>  #include "frontend.h"
> 

>  extern char **environ;
> +struct mrsh_state *state;

Hm, I'm not a fan of having this global variable here. Can't we pass it to
get_history_path without this trick?
Details
Message ID
<0b756451-6521-4a2a-afe8-a506b6dc1d0b@nixnetmail.com>
In-Reply-To
<hXJU8FEHgo4ilmmE5xdewUGtyCR5aZgB7Dyc7aAoC5Mh1xmBqfB2QZn3M2rvZ0MIH3pKUtOux3W6XDAmV8g2dGhquy62GqGPRA46ZWVeZPM=@emersion.fr> (view parent)
DKIM signature
missing
Download raw message
- I didn't know about strdup

- I was so blind that I didn't even look for which function calls 
get_history_path, which is defined next to get_history_path and it takes 
state as a parameter
Reply to thread Export thread (mbox)