Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Ilchuk committed May 14, 2020
1 parent 1faec22 commit 8b58ad7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions inc/ush.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#include <sys/stat.h>
#include <dirent.h>
#include <spawn.h>
#include <pwd.h>

#define MX_BUFSIZE 1024
#define MX_DELIMITERS "\t\r\n\a "
#define MX_LOOP_BREAK 1
#define MX_RETURN_EMPTY 2
#define MX_LOOP_CONTINUE 3
#define MX_DEFAULT_PATH "/bin:/usr/bin:/usr/local/bin:/usr/sbin:/sbin"

typedef struct s_dirs {
char *pwd;
Expand Down
24 changes: 22 additions & 2 deletions src/mx_ush_loop.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
#include "ush.h"

static bool semicol_error(char *str) {
bool flag = true;

if (mx_strlen(str) > 0) {
for (int i = 0; i < mx_strlen(str); i++) {
if (str[i] == ';' && flag) {
mx_printerr("ush: syntax error near unexpected token `;\'\n");
break;
}
if (isalpha(str[i])) {
flag = false;
break;
}
}
return flag;
}
return flag;
}

void mx_ush_loop(t_global *hd) {
t_lst *root;
int status = 1;

while (status) {
if (!mx_string_has_chars(hd->input) || semicol_error(hd->input))
break;
hd->new = mx_ush_read_line(hd->input); //зчитуємо строку
if (hd->new == NULL)
continue;
root = hd->new;
signal(SIGINT, mx_handler);
for ( ; hd->new; hd->new = hd->new->next) {
for ( ; hd->new; hd->new = hd->new->next)
status = mx_ush_execute(hd, hd->new); //виконуємо команди
}
mx_delete_list(root);
if (mx_strcmp("emptyinput", hd->input) != 0)
break;
Expand Down
20 changes: 16 additions & 4 deletions src/test_main.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
#include "ush.h"

static void init_val() {
char cur_path[PATH_MAX];

getwd(cur_path);
setenv("PWD", cur_path, 1);
mx_nosig();
if (getenv("HOME") == NULL) {
struct passwd *pw = getpwuid(getuid());

setenv("HOME", pw->pw_dir, 1);
}
}

int main() {
t_global *head = malloc(sizeof(t_global));
char *line;
size_t buf = 0;

head->input = NULL;
head->last_exit_status = 0;
mx_nosig();

init_val();
if(isatty(0) == 0) {
if(getline(&line, &buf, stdin) < 0) {
if(getline(&line, &buf, stdin) < 0)
exit(1);
}
head->input = mx_strnew(1024);
mx_strcpy(head->input, line);
}
Expand Down
Binary file added ush
Binary file not shown.

0 comments on commit 8b58ad7

Please sign in to comment.