~emersion/mrsh-dev

Add command -v builtin. v3 PROPOSED

dragnel: 1
 Add command -v builtin.

 6 files changed, 101 insertions(+), 0 deletions(-)
What is this style, gnu ? Maybe I can add an option in vim to force the style. It will be easier to me because this style is completly unnatural to me. I didn't see any guidelines either. Do you plan to add one ?



          
          
          
          
Next
I still have issue with the test, I removed the -e but I think there is a thing with stdout and stderr.
$ ninja test -C build
...
--- stderr ---
Running with mrsh
Running with reference shell
/home/xant0me/dev/mrsh/test/./command.sh: mismatch

mrsh: 0
exists if 0
exits cd 0
exists ls 0
exists ll 0
exists lla 0
ok
if
cd
/bin/ls
alias ll='ls -l'
lla

ref: 0
if
exists if 0
cd
exits cd 0
/bin/ls
exists ls 0
alias ll='ls -l'
exists ll 0
lla
exists lla 0
ok

-------

Full log written to ...
FAILED: meson-test
/usr/local/bin/meson test --no-rebuild --print-errorlogs
ninja: build stopped: subcommand failed.
$ build/mrsh test/command.sh
if
exists if 0
cd
exits cd 0
/bin/ls
exists ls 0
alias ll='ls -l'
exists ll 0
lla
exists lla 0
ok
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/4267/mbox | git am -3
Learn more about email & git

[PATCH v3] Add command -v builtin. Export this patch

---
Hi, thx for review.
I have a little problem through, test is passing only if I remove
the echos in test/command.sh

 builtin/builtin.c |  1 +
 builtin/command.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++
 include/builtin.h |  1 +
 meson.build       |  1 +
 test/command.sh   | 20 ++++++++++++
 test/meson.build  |  1 +
 6 files changed, 101 insertions(+)
 create mode 100644 builtin/command.c
 create mode 100644 test/command.sh

diff --git a/builtin/builtin.c b/builtin/builtin.c
index 0f0c563..20ed598 100644
--- a/builtin/builtin.c
@@ -18,6 +18,7 @@ static const struct builtin builtins[] = {
	{ ":", builtin_colon, true },
	{ "alias", builtin_alias, false },
	{ "cd", builtin_cd, false },
	{ "command", builtin_command, false },
	{ "eval", builtin_eval, true },
	{ "exit", builtin_exit, true },
	{ "export", builtin_export, true },
diff --git a/builtin/command.c b/builtin/command.c
new file mode 100644
index 0000000..1817c4f
--- /dev/null
@@ -0,0 +1,77 @@
#define _POSIX_C_SOURCE 200809L
#include "parser.h"
#include "mrsh/getopt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "shell/path.h"
#include "mrsh/builtin.h"

static const char command_usage[] = "usage: command [-v|-V|-p] "
	"command_name [argument...]\n";

static int command_v(struct mrsh_state *state, const char *command_name) {
	size_t len_command_name = strlen(command_name);

	const char *look_alias = mrsh_hashtable_get(&state->aliases,command_name);
	if (look_alias != NULL) {
		printf("alias %s='%s'\n",command_name,look_alias);
		return 0;
	}

	const char *look_fn = mrsh_hashtable_get(&state->functions,command_name);
	if (look_fn != NULL) {
		printf("%s\n",command_name);
		return 0;
	}

	if (mrsh_has_builtin(command_name)) {
		printf("%s\n",command_name);
		return 0;
	}

	for (size_t i = 0; i < keywords_len; ++i) {
		if (strlen(keywords[i]) == len_command_name &&
			strcmp(command_name, keywords[i]) == 0) {
			printf("%s\n",command_name);
			return 0;
		}
	}

	const char *expanded = expand_path(state,command_name,1);

	if (expanded != NULL) {
		printf("%s\n", expanded);
		return 0;
	}

	return 127;
}

int builtin_command(struct mrsh_state *state, int argc, char *argv[]) {
	mrsh_optind = 0;
	int opt;

	while ((opt = mrsh_getopt(argc, argv, ":vVp")) != -1) {
		switch (opt) {
		case 'v':
			if (argc != 3)
				return 1;
			return command_v(state,argv[mrsh_optind]);
		case 'V':
		case 'p':
			fprintf(stderr, "command: `-V` and `-p` and no arg not "
					"yet implemented\n");
			return 1;
		default:
			fprintf(stderr, "command: unknown option -- %c\n",
					mrsh_optopt);
			fprintf(stderr, command_usage);
			return 1;
		}
	}

	return 0;
}

diff --git a/include/builtin.h b/include/builtin.h
index 8a848c8..491a739 100644
--- a/include/builtin.h
+++ b/include/builtin.h
@@ -10,6 +10,7 @@ void print_escaped(const char *value);

int builtin_alias(struct mrsh_state *state, int argc, char *argv[]);
int builtin_cd(struct mrsh_state *state, int argc, char *argv[]);
int builtin_command(struct mrsh_state *state, int argc, char *argv[]);
int builtin_colon(struct mrsh_state *state, int argc, char *argv[]);
int builtin_dot(struct mrsh_state *state, int argc, char *argv[]);
int builtin_eval(struct mrsh_state *state, int argc, char *argv[]);
diff --git a/meson.build b/meson.build
index 41494c4..ef531da 100644
--- a/meson.build
+++ b/meson.build
@@ -76,6 +76,7 @@ lib_mrsh = library(
		'builtin/builtin.c',
		'builtin/cd.c',
		'builtin/colon.c',
		'builtin/command.c',
		'builtin/dot.c',
		'builtin/eval.c',
		'builtin/exit.c',
diff --git a/test/command.sh b/test/command.sh
new file mode 100644
index 0000000..edd0983
--- /dev/null
+++ b/test/command.sh
@@ -0,0 +1,20 @@
#!/bin/sh -e

lla () {
	ls -la
}

alias ll="ls -l"

command -v if
echo "exists if $?"
command -v cd
echo "exists cd $?"
command -v ls
echo "exists ls $?"
command -v ll
echo "exists ll $?"
command -v lla
echo "exists lla $?"
command -v idontexists
exit $?
diff --git a/test/meson.build b/test/meson.build
index 4f74e3d..513b3f3 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -11,6 +11,7 @@ test_files = [
	'subshell.sh',
	'ulimit.sh',
	'word.sh',
	'command.sh',
]

foreach test_file : test_files
-- 
2.20.1
Hi,

Thanks for your updated patch! A few more comments.

On Tuesday, April 16, 2019 4:24 PM, dragnel <dragnel@protonmail.com> wrote:
View this thread in the archives