~emersion/mrsh

9d5ca300f2478b80a1ef04210ad41abb7bcc5402 — delthas 3 years ago 2ba02ed
builtin/unalias: implement unalias builtin
4 files changed, 38 insertions(+), 2 deletions(-)

M builtin/builtin.c
A builtin/unalias.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 },
	{ "unalias", builtin_unalias, false },
	{ "unset", builtin_unset, true },
};


A builtin/unalias.c => builtin/unalias.c +33 -0
@@ 0,0 1,33 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mrsh/builtin.h>
#include "builtin.h"

static const char unalias_usage[] = "usage: unalias <-a|alias-name...>\n";

static void delete_alias_iterator(const char *key, void *_value, void *user_data) {
	free(mrsh_hashtable_del((struct mrsh_hashtable*)user_data, key));
}

int builtin_unalias(struct mrsh_state *state, int argc, char *argv[]) {
	if (argc == 1) {
		fprintf(stderr, unalias_usage);
		return EXIT_FAILURE;
	}
	
	if (strcmp(argv[1], "-a") == 0) {
		if (argc > 2) {
			fprintf(stderr, unalias_usage);
			return EXIT_FAILURE;
		}
		mrsh_hashtable_for_each(&state->aliases, delete_alias_iterator, &state->aliases);
		return EXIT_SUCCESS;
	}

	for (int i = 1; i < argc; ++i) {
		free(mrsh_hashtable_del(&state->aliases, argv[i]));
	}
	return EXIT_SUCCESS;
}

M include/builtin.h => include/builtin.h +2 -1
@@ 14,6 14,7 @@ int builtin_colon(struct mrsh_state *state, int argc, char *argv[]);
int builtin_exit(struct mrsh_state *state, int argc, char *argv[]);
int builtin_export(struct mrsh_state *state, int argc, char *argv[]);
int builtin_eval(struct mrsh_state *state, int argc, char *argv[]);
int builtin_false(struct mrsh_state *state, int argc, char *argv[]);
int builtin_getopts(struct mrsh_state *state, int argc, char *argv[]);
int builtin_pwd(struct mrsh_state *state, int argc, char *argv[]);
int builtin_read(struct mrsh_state *state, int argc, char *argv[]);


@@ 22,7 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_false(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[]);

const char *print_options(struct mrsh_state *state);

M meson.build => meson.build +2 -1
@@ 39,6 39,7 @@ lib_mrsh = library(
		'builtin/eval.c',
		'builtin/exit.c',
		'builtin/export.c',
		'builtin/false.c',
		'builtin/getopts.c',
		'builtin/pwd.c',
		'builtin/read.c',


@@ 47,7 48,7 @@ lib_mrsh = library(
		'builtin/source.c',
		'builtin/times.c',
		'builtin/true.c',
		'builtin/false.c',
		'builtin/unalias.c',
		'builtin/unset.c',
		'hashtable.c',
		'parser/parser.c',