summaryrefslogtreecommitdiff
path: root/builtins.c
blob: b98d16e38f427d3f04b7fc1016dd7e591f1cc57a (plain)
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
170
171
172
/*
 * UNG's Not GNU
 *
 * Copyright (c) 2011-2017, Jakob Kaivo <jkk@ung.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "sh.h"
#include <stddef.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>

struct builtin {
        const char *name;
        int (*main)(int argc, char *argv[]);
};

/* Special built-ins */
/* break */
/* : */
/* continue */
int dot_main(int argc, char *argv[]);
int eval_main(int argc, char *argv[]);
int exec_main(int argc, char *argv[]);
int exit_main(int argc, char *argv[]);
int export_main(int argc, char *argv[]);
int readonly_main(int argc, char *argv[]);
/* return */
int set_main(int argc, char *argv[]);
int shift_main(int argc, char *argv[]);
int times_main(int argc, char *argv[]);
int trap_main(int argc, char *argv[]);
int unset_main(int argc, char *argv[]);

static struct builtin special_builtins[] = {
	{ "break", NULL },
	{ ":", NULL },
	{ "continue", NULL },
	{ ".", dot_main },
	{ "eval", eval_main },
	{ "exec", exec_main },
	{ "exit", exit_main },
	{ "export", export_main },
	{ "readonly", readonly_main },
	{ "return", NULL },
	{ "set", set_main },
	{ "shift", shift_main },
	{ "times", times_main },
	{ "trap", trap_main },
	{ "unset", unset_main },
	{ 0, 0 },
};

/* Regular built-ins */
int alias_main(int argc, char *argv[]);
int bg_main(int argc, char *argv[]);
int cd_main(int argc, char *argv[]);
int command_main(int argc, char *argv[]);
int false_main(int argc, char *argv[]);
int fc_main(int argc, char *argv[]);
int fg_main(int argc, char *argv[]);
int getopts_main(int argc, char *argv[]);
int jobs_main(int argc, char *argv[]);
int kill_main(int argc, char *argv[]);
int newgrp_main(int argc, char *argv[]);
int pwd_main(int argc, char *argv[]);
int read_main(int argc, char *argv[]);
int true_main(int argc, char *argv[]);
int ulimit_main(int argc, char *argv[]);
int umask_main(int argc, char *argv[]);
int unalias_main(int argc, char *argv[]);
int wait_main(int argc, char *argv[]);

#ifndef EXTRA_BUILTINS
#define EXTRA_BUILTINS /* empty */
#endif

static struct builtin regular_builtins[] = {
	{ "alias", alias_main },
	{ "bg", bg_main },
	{ "cd", cd_main },
	{ "command", command_main },
	{ "false", false_main },
	{ "fc", fc_main },
	{ "fg", fg_main },
	{ "getopts", getopts_main },
	{ "jobs", jobs_main },
	{ "kill", kill_main },
	{ "newgrp", newgrp_main },
	{ "pwd", pwd_main },
	{ "read", read_main },
	{ "true", true_main },
	{ "ulimit", ulimit_main },
	{ "umask", umask_main },
	{ "unalias", unalias_main },
	{ "wait", wait_main },
	EXTRA_BUILTINS
	{ 0, 0 },
};

static int (*sh_getbuiltin(const char *util, struct builtin *bi))(int argc, char *argv[])
{
	for (int i = 0; bi[i].name; i++) {
		if (!strcmp(bi[i].name, util)) {
			return bi[i].main;
		}
	}

	return NULL;
}

int sh_builtin(int argc, char *argv[])
{
	char *util = basename(argv[0]);
	if (!strcmp(util, "break")) {
		return BREAK;
	} else if (!strcmp(util, ":")) {
		return 0;
	} else if (!strcmp(util, "continue")) {
		return CONTINUE;
	} else if (!strcmp(util, "return")) {
		return RETURN;
	}

	int (*m)(int, char *[]) = sh_getbuiltin(util, special_builtins);
	if (m == NULL) {
		m = sh_getbuiltin(util, regular_builtins);
		if (m == NULL) {
			return 1;
		}
	}

	optind = 1;
	opterr = 1;
	optarg = NULL;

	return m(argc, argv);
}

static int is_builtin(const char *util, struct builtin *bi)
{
	for (int i = 0; bi[i].name; i++) {
		if (!strcmp(bi[i].name, util)) {
			return 1;
		}
	}

	return 0;
}

int sh_is_special_builtin(const char *util)
{
	return is_builtin(util, special_builtins);
}

int sh_is_regular_builtin(const char *util)
{
	return is_builtin(util, regular_builtins);
}