~emersion/mrsh

3c59ae46c791518743e389c15cecd5c091ba27ee — delthas 3 years ago 199fc07
builtin/type: implement type builtin
4 files changed, 45 insertions(+), 0 deletions(-)

M builtin/builtin.c
A builtin/type.c
M include/builtin.h
M meson.build
M builtin/builtin.c => builtin/builtin.c +1 -0
@@ 30,6 30,7 @@ static const struct builtin builtins[] = {
	{ "shift", builtin_shift, true },
	{ "times", builtin_times, true },
	{ "true", builtin_true, false },
	{ "type", builtin_type, false },
	{ "unalias", builtin_unalias, false },
	{ "unset", builtin_unset, true },
};

A builtin/type.c => builtin/type.c +42 -0
@@ 0,0 1,42 @@
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <mrsh/builtin.h>
#include <mrsh/shell.h>
#include <shell/path.h>
#include "builtin.h"

static const char type_usage[] = "usage: type name...\n";

int builtin_type(struct mrsh_state *state, int argc, char *argv[]) {
	if (argc == 1) {
		fprintf(stderr, type_usage);
		return EXIT_FAILURE;
	}

	bool error = false;
	for (int i = 1; i < argc; ++i) {
		char *name = argv[i];
		
		char *alias = mrsh_hashtable_get(&state->aliases, name);
		if (alias != NULL) {
			fprintf(stdout, "%s is an alias for %s\n", name, alias);
			continue;
		}
		
		if (mrsh_has_builtin(name)) {
			fprintf(stdout, "%s is a shell builtin\n", name);
			continue;
		}
		
		const char *path = expand_path(state, name, true);
		if (path) {
			fprintf(stdout, "%s is %s\n", name, path);
			continue;
		}
		
		fprintf(stdout, "%s: not found\n", name);
		error = true;
	}
	
	return error ? EXIT_FAILURE : EXIT_SUCCESS;
}

M include/builtin.h => include/builtin.h +1 -0
@@ 23,6 23,7 @@ int builtin_source(struct mrsh_state *state, int argc, char *argv[]);
int builtin_times(struct mrsh_state *state, int argc, char *argv[]);
int builtin_set(struct mrsh_state *state, int argc, char *argv[]);
int builtin_true(struct mrsh_state *state, int argc, char *argv[]);
int builtin_type(struct mrsh_state *state, int argc, char *argv[]);
int builtin_unalias(struct mrsh_state *state, int argc, char *argv[]);
int builtin_unset(struct mrsh_state *state, int argc, char *argv[]);


M meson.build => meson.build +1 -0
@@ 48,6 48,7 @@ lib_mrsh = library(
		'builtin/source.c',
		'builtin/times.c',
		'builtin/true.c',
		'builtin/type.c',
		'builtin/unalias.c',
		'builtin/unset.c',
		'hashtable.c',