~emersion/mrsh

e94d66a6ab3f891af4ffcf017c3009351500a758 — Drew DeVault 3 years ago 9c12039
Partially implement unset builtin

It's missing the ability to unset functions
4 files changed, 51 insertions(+), 0 deletions(-)

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

static int builtin_compare(const void *_a, const void *_b) {

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

static const char unset_usage[] = "unset: unset [-fv] name...\n";

int builtin_unset(struct mrsh_state *state, int argc, char *argv[]) {
	int funcs = 0;

	int opt;
	while ((opt = getopt(argc, argv, "fv")) != -1) {
		switch (opt) {
		case 'f':
			funcs = 1;
			break;
		case 'v':
			funcs = 0;
			break;
		default:
			fprintf(stderr, unset_usage);
			return EXIT_FAILURE;
		}
	}
	if (optind >= argc) {
		fprintf(stderr, unset_usage);
		return EXIT_FAILURE;
	}
	for (int i = optind; i < argc; ++i) {
		if (!funcs) {
			uint32_t prev_attribs = 0;
			if (mrsh_env_get(state, argv[i], &prev_attribs)) {
				if ((prev_attribs & MRSH_VAR_ATTRIB_READONLY)) {
					fprintf(stderr,
						"unset: cannot modify readonly variable %s\n", argv[i]);
					return EXIT_FAILURE;
				}
				mrsh_env_unset(state, argv[i]);
			}
		} else {
			// TODO: functions
		}
	}
	return EXIT_SUCCESS;
}

M include/builtin.h => include/builtin.h +1 -0
@@ 20,6 20,7 @@ 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_unset(struct mrsh_state *state, int argc, char *argv[]);

const char *print_options(struct mrsh_state *state);


M meson.build => meson.build +1 -0
@@ 45,6 45,7 @@ lib_mrsh = library(
		'builtin/times.c',
		'builtin/true.c',
		'builtin/false.c',
		'builtin/unset.c',
		'hashtable.c',
		'parser/parser.c',
		'parser/program.c',