~emersion/mrsh

ref: bda92abbcf206e6374593ee7e1962ba6341be98e mrsh/builtin/unalias.c -rw-r--r-- 1.1 KiB View raw
bda92abbemersion Fix error handling in basic frontend 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#define _POSIX_C_SOURCE 200809L
#include <getopt.h>
#include <mrsh/builtin.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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[]) {
	bool all = false;

	optind = 1;
	int opt;
	while ((opt = getopt(argc, argv, ":a")) != -1) {
		switch (opt) {
		case 'a':
			all = true;
			break;
		default:
			fprintf(stderr, "unalias: unknown option -- %c\n", optopt);
			fprintf(stderr, unalias_usage);
			return EXIT_FAILURE;
		}
	}

	if (all) {
		if (optind < argc) {
			fprintf(stderr, unalias_usage);
			return EXIT_FAILURE;
		}
		mrsh_hashtable_for_each(&state->aliases, delete_alias_iterator, &state->aliases);
		return EXIT_SUCCESS;
	}

	if (optind == argc) {
		fprintf(stderr, unalias_usage);
		return EXIT_FAILURE;
	}

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