~emersion/mrsh

9c120394d5aa72249519dcc6807034adf7296ecb — Drew DeVault 3 years ago 6296951
Implement readonly builtin
4 files changed, 34 insertions(+), 5 deletions(-)

M builtin/builtin.c
M builtin/export.c
M shell/task/assignment.c
M shell/task/command.c
M builtin/builtin.c => builtin/builtin.c +1 -0
@@ 22,6 22,7 @@ static const struct builtin builtins[] = {
	{ "exit", builtin_exit, true },
	{ "export", builtin_export, true },
	{ "false", builtin_false, false },
	{ "readonly", builtin_export, true },
	{ "set", builtin_set, true },
	{ "shift", builtin_shift, true },
	{ "times", builtin_times, true },

M builtin/export.c => builtin/export.c +18 -5
@@ 5,18 5,23 @@
#include <string.h>
#include "builtin.h"

static const char export_usage[] = "usage: export (-p|name[=word]...)\n";
static const char export_usage[] = "usage: %s (-p|name[=word]...)\n";

int builtin_export(struct mrsh_state *state, int argc, char *argv[]) {
	uint32_t attrib = MRSH_VAR_ATTRIB_EXPORT;
	if (strcmp(argv[0], "readonly") == 0) {
		attrib = MRSH_VAR_ATTRIB_READONLY;
	}

	if (argc < 2) {
		fprintf(stderr, export_usage);
		fprintf(stderr, export_usage, argv[0]);
		return EXIT_FAILURE;
	} else if (argc == 2 && strcmp(argv[1], "-p") == 0) {
		size_t count;
		struct mrsh_collect_var *vars = mrsh_collect_vars(
				state, MRSH_VAR_ATTRIB_EXPORT, &count);
		for (size_t i = 0; i < count; ++i) {
			printf("export %s=", vars[i].key);
			printf("%s %s=", argv[0], vars[i].key);
			print_escaped(vars[i].value);
			printf("\n");
		}


@@ 27,19 32,27 @@ int builtin_export(struct mrsh_state *state, int argc, char *argv[]) {
	for (int i = 1; i < argc; ++i) {
		char *eql, *key;
		const char *val;
		uint32_t prev_attribs = 0;
		eql = strchr(argv[i], '=');
		if (eql) {
			size_t klen = eql - argv[i];
			key = strndup(argv[i], klen);
			val = &eql[1];
			mrsh_env_get(state, key, &prev_attribs);
		} else {
			key = strdup(argv[i]);
			val = mrsh_env_get(state, key, NULL);
			val = mrsh_env_get(state, key, &prev_attribs);
			if (!val) {
				val = "";
			}
		}
		mrsh_env_set(state, key, val, MRSH_VAR_ATTRIB_EXPORT);
		if ((prev_attribs & MRSH_VAR_ATTRIB_READONLY)) {
			fprintf(stderr, "%s: cannot modify readonly variable %s\n",
					argv[0], key);
			free(key);
			return EXIT_FAILURE;
		}
		mrsh_env_set(state, key, val, attrib | prev_attribs);
		free(key);
	}


M shell/task/assignment.c => shell/task/assignment.c +8 -0
@@ 17,6 17,14 @@ static int task_assignment_poll(struct task *task, struct context *ctx) {
		if ((ctx->state->options & MRSH_OPT_ALLEXPORT)) {
			attribs = MRSH_VAR_ATTRIB_EXPORT;
		}
		uint32_t prev_attribs = 0;
		if (mrsh_env_get(ctx->state, assign->name, &prev_attribs) != NULL
				&& (prev_attribs & MRSH_VAR_ATTRIB_READONLY)) {
			fprintf(stderr, "cannot modify readonly variable %s\n",
					assign->name);
			task->status = EXIT_FAILURE;
			return TASK_STATUS_ERROR;
		}
		mrsh_env_set(ctx->state, assign->name, new_value, attribs);
	}


M shell/task/command.c => shell/task/command.c +7 -0
@@ 145,6 145,13 @@ static bool task_process_start(struct task_command *tc, struct context *ctx) {
	} else if (pid == 0) {
		for (size_t i = 0; i < sc->assignments.len; ++i) {
			struct mrsh_assignment *assign = sc->assignments.data[i];
			uint32_t prev_attribs;
			if (mrsh_env_get(ctx->state, assign->name, &prev_attribs)
					&& (prev_attribs & MRSH_VAR_ATTRIB_READONLY)) {
				fprintf(stderr, "cannot modify readonly variable %s\n",
						assign->name);
				return false;
			}
			char *value = mrsh_word_str(assign->value);
			setenv(assign->name, value, true);
			free(value);