~sircmpwn/mrsh

mrsh/shell/entry.c -rw-r--r-- 3.5 KiB View raw
6770e0deDrew DeVault Add configure script and Makefile 8 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
#define _XOPEN_SOURCE 700
#include <mrsh/builtin.h>
#include <mrsh/entry.h>
#include <mrsh/shell.h>
#include <mrsh/parser.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "builtin.h"

static char *expand_parameter(struct mrsh_state *state, const char *src) {
	struct mrsh_parser *parser = mrsh_parser_with_data(src, strlen(src));
	if (parser == NULL) {
		return NULL;
	}
	struct mrsh_word *word = mrsh_parse_word(parser);
	mrsh_parser_destroy(parser);
	if (word == NULL) {
		return NULL;
	}
	mrsh_run_word(state, &word);
	char *str = mrsh_word_str(word);
	mrsh_word_destroy(word);
	return str;
}

char *mrsh_get_ps1(struct mrsh_state *state, int next_history_id) {
	// TODO: Replace ! with next history ID
	const char *ps1 = mrsh_env_get(state, "PS1", NULL);
	if (ps1 != NULL) {
		return expand_parameter(state, ps1);
	}
	char *p = malloc(3);
	sprintf(p, "%s", getuid() ? "$ " : "# ");
	return p;
}

char *mrsh_get_ps2(struct mrsh_state *state) {
	// TODO: Replace ! with next history ID
	const char *ps2 = mrsh_env_get(state, "PS2", NULL);
	if (ps2 != NULL) {
		return expand_parameter(state, ps2);
	}
	return strdup("> ");
}

char *mrsh_get_ps4(struct mrsh_state *state) {
	const char *ps4 = mrsh_env_get(state, "PS4", NULL);
	if (ps4 != NULL) {
		return expand_parameter(state, ps4);
	}
	return strdup("+ ");
}

bool mrsh_populate_env(struct mrsh_state *state, char **environ) {
	for (size_t i = 0; environ[i] != NULL; ++i) {
		char *eql = strchr(environ[i], '=');
		size_t klen = eql - environ[i];
		char *key = strndup(environ[i], klen);
		char *val = &eql[1];
		mrsh_env_set(state, key, val, MRSH_VAR_ATTRIB_EXPORT);
		free(key);
	}

	mrsh_env_set(state, "IFS", " \t\n", MRSH_VAR_ATTRIB_NONE);

	pid_t ppid = getppid();
	char ppid_str[24];
	snprintf(ppid_str, sizeof(ppid_str), "%d", ppid);
	mrsh_env_set(state, "PPID", ppid_str, MRSH_VAR_ATTRIB_NONE);

	// TODO check if path is well-formed, has . or .., and handle symbolic links
	const char *pwd = mrsh_env_get(state, "PWD", NULL);
	if (pwd == NULL || strlen(pwd) >= PATH_MAX) {
		char cwd[PATH_MAX];
		if (getcwd(cwd, PATH_MAX) == NULL) {
			fprintf(stderr, "getcwd failed: %s\n", strerror(errno));
			return false;
		}
		mrsh_env_set(state, "PWD", cwd,
				MRSH_VAR_ATTRIB_EXPORT | MRSH_VAR_ATTRIB_READONLY);
	} else {
		mrsh_env_set(state, "PWD", pwd,
				MRSH_VAR_ATTRIB_EXPORT | MRSH_VAR_ATTRIB_READONLY);
	}

	mrsh_env_set(state, "OPTIND", "1", MRSH_VAR_ATTRIB_NONE);
	return true;
}

static void source_file(struct mrsh_state *state, char *path) {
	if (access(path, F_OK) == -1) {
		return;
	}
	char *env_argv[] = { ".", path };
	mrsh_run_builtin(state, sizeof(env_argv) / sizeof(env_argv[0]), env_argv);
}

void mrsh_source_profile(struct mrsh_state *state) {
	source_file(state, "/etc/profile");

	char path[PATH_MAX];
	int n = snprintf(path, sizeof(path), "%s/.profile", getenv("HOME"));
	if (n == sizeof(path)) {
		fprintf(stderr, "Warning: $HOME/.profile is longer than PATH_MAX\n");
		return;
	}
	source_file(state, path);
}

void mrsh_source_env(struct mrsh_state *state) {
	char *path = getenv("ENV");
	if (path == NULL) {
		return;
	}
	if (getuid() != geteuid() || getgid() != getegid()) {
		return;
	}
	path = expand_parameter(state, path);
	char *real = realpath(path, NULL);
	if (strcmp(path, real) != 0) {
		fprintf(stderr, "Error: $ENV is not an absolute path; "
				"this is undefined behavior.\n");
		fprintf(stderr, "Continuing without sourcing it.\n");
		free(path);
		free(real);
		return;
	}
	source_file(state, real);
	free(path);
	free(real);
}