Example #1
0
File: 2.c Project: tonylijo/shell
void read_cmd()
{
	char cmdline[100];
	printf(">>");
	fflush(stdout);
	fgets(cmdline,100,stdin);	
	fill_argv(cmdline);
}
Example #2
0
void read_input(int mode, char *batch_path) {

	char c;
	int current_length = (BUFFER_SIZE + 1);
	line = (char *) malloc(sizeof(char) * (BUFFER_SIZE + 1));
	char *path = (char *) malloc(sizeof(char) * (BUFFER_SIZE + 1));
	fflush(stderr);
	printf("\n[SHELL ] ");
	fflush(stdout);
	int run = 1, len = 0;
	if (mode == BATCH) {
		if (open(batch_path, O_RDONLY) > 0) {
			batch_file = fopen(batch_path, "r");
		} else {
			fprintf(stderr, "cann't open or read the file\n");
			return;
		}
	}
	while (c != EOF && run) {

		if (mode == INTERACTIVE)
			c = getchar();
		else {
			c = fgetc(batch_file);
			if (feof(stdin)) {
				run = 0;
				break;
			}
		}
		switch (c) {
		case '\n':
			if (line[0] == '\0') {
				len = 0;
				free_args();
				bzero(line, current_length);
				fflush(stderr);
				printf("\n[SHELL ] ");
				fflush(stdout);
			} else {

				if (mode == BATCH) {
					printf("%s\n", line);
				}
				if (len >= BUFFER_SIZE) {
					fprintf(stderr, "buffer limit excited\n");
					bzero(line, current_length);
					len = 0;
					continue;
				}
				add_to_history(line);
				free_args();
				fill_argv(line);
				if (is_comment(line) == 0) {
					if (strchr(line, '=') != NULL
							&& strcmp(args[0], "echo") != 0) {
						assign_var(line);
					} else {
//						free_args();
//						fill_argv(line);
						if (strcmp(args[0], "echo") != 0) {
							int i;
							for (i = 0; args[i] != NULL; i++) {
								append_home_dir(args[i]);
								fix_echo_line(args[i]);
							}
						} else {
							int i;
							for (i = 1; args[i] != NULL; i++) {
								fix_echo_line(args[i]);
							}
						}

						if (strcmp("exit", line) == 0) {
							run = 0;
							break;
						} else if (strcmp("history", args[0]) == 0) {
							execute_history();
						} else if (strcmp("cd", args[0]) == 0)
							cd();
						else {
							strcpy(path, args[0]);
							if (strchr(path, '/') != NULL) {
								int fd;
								if ((fd = open(path, O_RDONLY)) > 0) {
									close(fd);
									call_execve(path);
								} else {
									fprintf(stderr,
											"%s: No such file or directory\n",
											args[0]);
								}
							} else {
								if (!is_command(path)) {
									fprintf(stderr, "%s: command not found\n",
											args[0]);
								} else {
									call_execve(path);
								}
							}

						}
					}
				}
				free_args();
				fflush(stderr);
				printf("\n[SHELL ] ");
				fflush(stdout);
				bzero(line, BUFFER_SIZE);
				len = 0;
			}
			bzero(line, BUFFER_SIZE);
			break;
		default:
			len++;
			if (len < BUFFER_SIZE) {
				strncat(line, &c, 1);
			}
			break;
		}
	}
	if (mode == BATCH)
		fclose(batch_file);
}
Example #3
0
int main(int argc, char *argv[], char *envp[])
{
	int write_bytes;
	char exec_file_name[STRING_MAX];
	char path_str[STRING_MAX];
	int flag_exit;
	int exec_status;
	int index;
	int fd;
	pid_t pid;
	signal(SIGINT, SIG_IGN);
	signal(SIGINT, handle_signal);
	read_bytes = STRING_MAX;
	fflush(stdout);
	get_path_string(envp, path_str);
	insert_path_str_to_search(path_str);
	do {
		memset(exec_file_name, 0, read_bytes);
		write_bytes = write(1, MESS_BASH_WAIT_COMMAND,
				sizeof(MESS_BASH_WAIT_COMMAND));
		read_bytes = 1;
		read_bytes = read(0, exec_file_name, STRING_MAX);
		if (read_bytes != 1) {
			exec_file_name[read_bytes-1] = 0;
			flag_exit = strcmp(exec_file_name, COMMAND_BASH_CLOSE);
			if (flag_exit)
				switch (pid = fork()) {
				case -1:
					perror("fork");
					exit(1);
				case 0:
					fill_argv(exec_file_name);
					if (argv_child[0][0] != '/') {
						fd = open(argv_child[0], O_RDONLY);
						if (fd > 0) {
							close(fd);
						} else if (attach_path() != 0) {
							printf("0 %s: %s\n",
								argv_child[0],
								MESS_BASH_COMMAND_MISS);
							exit(1);
						}
					} else {
						fd = open(argv_child[0], O_RDONLY);
						if (fd > 0) {
							close(fd);
						} else {
							printf("1 %s: %s\n",
								argv_child[0],
								MESS_BASH_COMMAND_MISS);
							exit(1);
						}
					}
					exec_status = execve(argv_child[0],
						argv_child, envp);
					if (exec_status < 0) {
						printf("2 %s: %s\n",
							argv_child[0],
							MESS_BASH_COMMAND_MISS);
						exit(1);
					}
					exit(0);
				default:
					wait(NULL);
				}
		}
	} while (flag_exit);
	for (index = 0; argv_child[index] != NULL; index++)
		free(argv_child[index]);
	for (index = 0; search_path[index] != NULL; index++)
		free(search_path[index]);
	write_bytes = write(1, MESS_BASH_NL, sizeof(MESS_BASH_NL));
	return 0;
}