~emersion/mrsh-dev

Add command -v builtin. v4 PROPOSED

dragnel: 1
 Add command -v builtin.

 6 files changed, 107 insertions(+), 0 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/5481/mbox | git am -3
Learn more about email & git

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

---

I've already rebased with git pull; git rebase master mybranch.
The tests are passing, and history shows
56a63e1 (HEAD -> features) Add command -v builtin.
57e0022 (origin/master, origin/HEAD, master) Fix builtins I/O redirections
what is wrong ?
 builtin/builtin.c |  1 +
 builtin/command.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++
 include/builtin.h |  1 +
 meson.build       |  1 +
 test/command.sh   | 25 +++++++++++++++
 test/meson.build  |  1 +
 6 files changed, 107 insertions(+)
 create mode 100644 builtin/command.c
 create mode 100644 test/command.sh

diff --git a/builtin/builtin.c b/builtin/builtin.c
index 48b4f0e..aaeac2b 100644
--- a/builtin/builtin.c
@@ -19,6 +19,7 @@ static const struct builtin builtins[] = {
	{ "alias", builtin_alias, false },
	{ "bg", builtin_bg, 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..2a09af4
--- /dev/null
@@ -0,0 +1,78 @@
#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 82095a8..fb1933a 100644
--- a/include/builtin.h
+++ b/include/builtin.h
@@ -11,6 +11,7 @@ void print_escaped(const char *value);
int builtin_alias(struct mrsh_state *state, int argc, char *argv[]);
int builtin_bg(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 ef36b18..e9b43e8 100644
--- a/meson.build
+++ b/meson.build
@@ -77,6 +77,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..58635dd
--- /dev/null
+++ b/test/command.sh
@@ -0,0 +1,25 @@
#!/bin/sh

lla () {
	ls -la
}

alias ll="ls -l"

command -v if
echo "exists if $?"
command -v cd
echo "exits cd $?"
command -v ls
echo "exists ls $?"
command -v ll
echo "exists ll $?"
command -v lla
echo "exists lla $?"
command -v idontexists
if [ $? -ne 0 ]
then
	echo "ok"
else
	echo "ko"
fi
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.17.1
View this thread in the archives