~emersion/mrsh

mrsh/builtin/export.c -rw-r--r-- 1.7 KiB View raw
7105405aBenjamin Lowry Makefile: remove getopt.h from public_includes a day 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#define _POSIX_C_SOURCE 200809L
#include <mrsh/builtin.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "builtin.h"
#include "shell/word.h"

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, argv[0]);
		return 1;
	} else if (argc == 2 && strcmp(argv[1], "-p") == 0) {
		size_t count;
		struct mrsh_collect_var *vars = collect_vars(
			state, attrib, &count);
		for (size_t i = 0; i < count; ++i) {
			printf("%s %s=", argv[0], vars[i].key);
			print_escaped(vars[i].value);
			printf("\n");
		}
		free(vars);
		return 0;
	}

	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, &prev_attribs);
			if (!val) {
				val = "";
			}
		}
		if ((prev_attribs & MRSH_VAR_ATTRIB_READONLY)) {
			fprintf(stderr, "%s: cannot modify readonly variable %s\n",
					argv[0], key);
			free(key);
			return 1;
		}
		struct mrsh_word_string *ws =
			mrsh_word_string_create(strdup(val), false);
		struct mrsh_word *word = &ws->word;
		expand_tilde(state, &word, true);
		char *new_val = mrsh_word_str(word);
		mrsh_word_destroy(word);
		mrsh_env_set(state, key, new_val, attrib | prev_attribs);
		free(key);
		free(new_val);
	}

	return 0;
}