~emersion/mrsh

ref: 287f4e235bfd2a89eeece06c4fa2aeffac30772b mrsh/shell/shell.c -rw-r--r-- 4.5 KiB
287f4e23Simon Ser Fix saved_sigactions memory leak 9 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <errno.h>
#include <mrsh/hashtable.h>
#include <mrsh/parser.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shell/job.h"
#include "shell/shell.h"
#include "shell/process.h"

void function_destroy(struct mrsh_function *fn) {
	if (!fn) {
		return;
	}
	mrsh_command_destroy(fn->body);
	free(fn);
}

struct mrsh_state *mrsh_state_create(void) {
	struct mrsh_state_priv *priv = calloc(1, sizeof(*priv));
	if (priv == NULL) {
		return NULL;
	}

	priv->term_fd = STDIN_FILENO;

	struct mrsh_state *state = &priv->pub;
	state->exit = -1;
	state->interactive = isatty(priv->term_fd);
	state->options = state->interactive ? MRSH_OPT_INTERACTIVE : 0;

	struct mrsh_call_frame_priv *frame_priv =
		calloc(1, sizeof(struct mrsh_call_frame_priv));
	if (frame_priv == NULL) {
		free(priv);
		return NULL;
	}
	state->frame = &frame_priv->pub;

	return state;
}

static const char *get_alias_func(const char *name, void *data) {
	struct mrsh_state *state = data;
	struct mrsh_state_priv *priv = state_get_priv(state);
	return mrsh_hashtable_get(&priv->aliases, name);
}

void mrsh_state_set_parser_alias_func(
		struct mrsh_state *state, struct mrsh_parser *parser) {
	mrsh_parser_set_alias_func(parser, get_alias_func, state);
}

static void state_string_finish_iterator(const char *key, void *value,
		void *user_data) {
	free(value);
}

static void variable_destroy(struct mrsh_variable *var) {
	if (!var) {
		return;
	}
	free(var->value);
	free(var);
}

static void state_var_finish_iterator(const char *key, void *value,
		void *user_data) {
	variable_destroy((struct mrsh_variable *)value);
}

static void state_fn_finish_iterator(const char *key, void *value, void *_) {
	function_destroy((struct mrsh_function *)value);
}

static void call_frame_destroy(struct mrsh_call_frame *frame) {
	for (int i = 0; i < frame->argc; ++i) {
		free(frame->argv[i]);
	}
	free(frame->argv);
	free(frame);
}

void mrsh_state_destroy(struct mrsh_state *state) {
	struct mrsh_state_priv *priv = state_get_priv(state);
	broadcast_sighup(state);
	mrsh_hashtable_for_each(&priv->variables, state_var_finish_iterator, NULL);
	mrsh_hashtable_finish(&priv->variables);
	mrsh_hashtable_for_each(&priv->functions, state_fn_finish_iterator, NULL);
	mrsh_hashtable_finish(&priv->functions);
	mrsh_hashtable_for_each(&priv->aliases,
		state_string_finish_iterator, NULL);
	mrsh_hashtable_finish(&priv->aliases);
	while (priv->jobs.len > 0) {
		job_destroy(priv->jobs.data[priv->jobs.len - 1]);
	}
	mrsh_array_finish(&priv->jobs);
	while (priv->processes.len > 0) {
		process_destroy(priv->processes.data[priv->processes.len - 1]);
	}
	mrsh_array_finish(&priv->processes);
	free(priv->saved_sigactions);
	struct mrsh_call_frame *frame = state->frame;
	while (frame) {
		struct mrsh_call_frame *prev = frame->prev;
		call_frame_destroy(frame);
		frame = prev;
	}
	free(state);
}

struct mrsh_state_priv *state_get_priv(struct mrsh_state *state) {
	return (struct mrsh_state_priv *)state;
}

void mrsh_env_set(struct mrsh_state *state,
		const char *key, const char *value, uint32_t attribs) {
	struct mrsh_state_priv *priv = state_get_priv(state);

	struct mrsh_variable *var = calloc(1, sizeof(struct mrsh_variable));
	if (!var) {
		return;
	}
	var->value = strdup(value);
	var->attribs = attribs;
	struct mrsh_variable *old = mrsh_hashtable_set(&priv->variables, key, var);
	variable_destroy(old);
}

void mrsh_env_unset(struct mrsh_state *state, const char *key) {
	struct mrsh_state_priv *priv = state_get_priv(state);

	variable_destroy(mrsh_hashtable_del(&priv->variables, key));
}

const char *mrsh_env_get(struct mrsh_state *state,
		const char *key, uint32_t *attribs) {
	struct mrsh_state_priv *priv = state_get_priv(state);

	struct mrsh_variable *var = mrsh_hashtable_get(&priv->variables, key);
	if (var && attribs) {
		*attribs = var->attribs;
	}
	return var ? var->value : NULL;
}

struct mrsh_call_frame_priv *call_frame_get_priv(struct mrsh_call_frame *frame) {
	return (struct mrsh_call_frame_priv *)frame;
}

void push_frame(struct mrsh_state *state, int argc, const char *argv[]) {
	struct mrsh_call_frame_priv *next = calloc(1, sizeof(*next));
	next->pub.argc = argc;
	next->pub.argv = malloc(sizeof(char *) * argc);
	for (int i = 0; i < argc; ++i) {
		next->pub.argv[i] = strdup(argv[i]);
	}
	next->pub.prev = state->frame;
	state->frame = &next->pub;
}

void pop_frame(struct mrsh_state *state) {
	struct mrsh_call_frame *frame = state->frame;
	assert(frame->prev != NULL);
	state->frame = frame->prev;
	call_frame_destroy(frame);
}