summaryrefslogtreecommitdiff
path: root/src/cmd/jsh/jsh.c
blob: 8e07d8c78fc7e0a8346c7db6e936bb1f59c2b1ab (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include "builtin.h"

size_t getargs(const size_t n, char *args[n], FILE * const stream);
int shellexec(size_t n, char * const args[n]);
/* int shellredirect(size_t n, char * const args[n], const char * const file, const int mode); */
/* int shelltofile(size_t n, char * const args[n], const int fd); */

static bool run = true;
static const char * const delim = "\t\n &|><";

int main(int argc, char *argv[]) {
	char *words[BUFSIZ] = {NULL};
	FILE *fp = stdin;
	size_t numberofwords = 0;
	if (argc == 2) {
		if ((fp = fopen(argv[1], "r")) == NULL) {
			perror("jsh");
			return EXIT_FAILURE;
		}
	}
	while (run) {
		printf("$ ");
		numberofwords = getargs(BUFSIZ, words, fp);
		shellexec(numberofwords, words);
		for (int i = 0; i < numberofwords; i++) {
			free(words[i]);
			words[i] = NULL;
		}
	}
	return 0;
}

size_t getargs(const size_t n, char *args[n], FILE * const stream) {
	char *line = (char *)malloc(BUFSIZ), *tofree, *token;
	size_t linecap = 0, num = 0;
	getline(&line, &linecap, stream);
	tofree = line;
	while ((token = strsep(&line, delim)) != NULL) {
		if (strcmp(token, "") != 0) {
			args[num++] = strdup(token);
		}
	}
	free((void *)tofree);
	return num;
}

int shellexec(const size_t n, char * const args[n]) {
	int index;
	if (strcmp(args[0], "exit") == 0) {
		run = false;
		return EXIT_SUCCESS;
	} else if ((index = checkbuiltin(args[0])) != -1) {
		return execbuiltin(index, n, args);
	}
	pid_t pid = fork();
	int status;
	if (pid == 0) {
		if (execvp(args[0], args) == -1) {
			perror("jsh");
		}
		exit(EXIT_FAILURE);
	} else if (pid < 0) {
		perror("jsh");
		exit(EXIT_FAILURE);
	} else {
		do {
		  waitpid(pid, &status, WUNTRACED);
		} while (!WIFEXITED(status) && !WIFSIGNALED(status));
	}
	return EXIT_SUCCESS;
}


/* int shellredirect(size_t n, char * const args[n], const char * const file, const int mode) { */
/* 	int fd = open(file, mode); */
/* 	int status; */
/* 	if (mode & O_WRONLY || mode & O_APPEND) { */
/* 		status = shelltofile(n, args, fd); */
/* 	} else if (mode & O_RDONLY) { */
/* 		status = EXIT_FAILURE; */
/* 	} else { */
/* 		status = EXIT_FAILURE; */
/* 	} */
/* 	close(fd); */
/* 	return status; */
/* } */

/* int shelltofile(size_t n, char * const args[n], const int fd) { */
/* 	pid_t pid = fork(); */
/* 	int status; */
/* 	if (pid == 0) { */
/* 		close(STDOUT_FILENO); */
/* 		dup2(fd, STDOUT_FILENO); */
/* 		if (execvp(args[0], args) == -1) { */
/* 			perror("jsh"); */
/* 			exit(EXIT_FAILURE); */
/* 		} */
/* 		exit(EXIT_SUCCESS); */
/* 	} else if (pid < 0) { */
/* 		perror("jsh"); */
/* 		exit(EXIT_FAILURE); */
/* 	} else { */
/* 		do { */
/* 		  waitpid(pid, &status, WUNTRACED); */
/* 		} while (!WIFEXITED(status) && !WIFSIGNALED(status)); */
/* 	} */
/* 	return EXIT_SUCCESS; */
/* } */