~emersion/mrsh

mrsh/builtin/command.c -rw-r--r-- 2.8 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
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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "builtin.h"
#include "mrsh_getopt.h"
#include "parser.h"
#include "shell/job.h"
#include "shell/path.h"
#include "shell/process.h"
#include "shell/shell.h"

static const char command_usage[] = "usage: command [-v|-V|-p] "
	"command_name [argument...]\n";

static int verify_command(struct mrsh_state *state, const char *command_name,
		bool default_path) {
	struct mrsh_state_priv *priv = state_get_priv(state);

	size_t len_command_name = strlen(command_name);

	const char *look_alias =
		mrsh_hashtable_get(&priv->aliases, command_name);
	if (look_alias != NULL) {
		printf("alias %s='%s'\n", command_name, look_alias);
		return 0;
	}

	const char *look_fn =
		mrsh_hashtable_get(&priv->functions, command_name);
	if (look_fn != NULL) {
		printf("%s\n", command_name);
		return 0;
	}

	if (mrsh_has_builtin(command_name)) {
		printf("%s\n", command_name);
		return 0;
	}

	for (size_t i = 0; i < keywords_len; ++i) {
		if (strlen(keywords[i]) == len_command_name &&
				strcmp(command_name, keywords[i]) == 0) {
			printf("%s\n", command_name);
			return 0;
		}
	}

	const char *expanded = expand_path(state, command_name, true, default_path);
	if (expanded != NULL) {
		printf("%s\n", expanded);
		return 0;
	}

	return 1;
}

static int run_command(struct mrsh_state *state, int argc, char *argv[],
		bool default_path) {
	if (mrsh_has_builtin(argv[0])) {
		return mrsh_run_builtin(state, argc - _mrsh_optind, &argv[_mrsh_optind]);
	}

	const char *path = expand_path(state, argv[0], true, default_path);
	if (!path) {
		fprintf(stderr, "%s: not found\n", argv[0]);
		return 127;
	}

	// TODO: job control support
	pid_t pid = fork();
	if (pid < 0) {
		perror("fork");
		return 126;
	} else if (pid == 0) {
		execv(path, argv);

		// Something went wrong
		perror(argv[0]);
		exit(126);
	}

	struct mrsh_process *proc = process_create(state, pid);
	return job_wait_process(proc);
}

int builtin_command(struct mrsh_state *state, int argc, char *argv[]) {
	_mrsh_optind = 0;
	int opt;

	bool verify = false, default_path = false;
	while ((opt = _mrsh_getopt(argc, argv, ":vVp")) != -1) {
		switch (opt) {
		case 'v':
			verify = true;
			break;
		case 'V':
			fprintf(stderr, "command: `-V` has an unspecified output format, "
				"use `-v` instead\n");
			return 0;
		case 'p':
			default_path = true;
			break;
		default:
			fprintf(stderr, "command: unknown option -- %c\n", _mrsh_optopt);
			fprintf(stderr, command_usage);
			return 1;
		}
	}

	if (_mrsh_optind >= argc) {
		fprintf(stderr, command_usage);
		return 1;
	}

	if (verify) {
		if (_mrsh_optind != argc - 1) {
			fprintf(stderr, command_usage);
			return 1;
		}
		return verify_command(state, argv[_mrsh_optind], default_path);
	}

	return run_command(state, argc - _mrsh_optind, &argv[_mrsh_optind],
		default_path);
}