Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <errno.h>
- #include <ctype.h>
- #include "util.h"
- #include <stdlib.h>
- #define max_words 30
- typedef struct arg_list{
- struct arg_list *next;
- char* arg;
- }arg_list;
- typedef struct pr_list{
- pid_t pid;
- char *pr;
- arg_list *args;
- struct pr_list *next;
- }pr_list;
- typedef struct pipe_list{
- char *line;
- struct pipe_list *next;
- pr_list *prs;
- }pipe_list;
- void printf_args(arg_list **curr){
- arg_list **pointer;
- pointer = curr;
- if (*curr != NULL){
- if ((*pointer)->arg != NULL){
- printf("%s\n", (*pointer)->arg);
- while ((*pointer)->next){
- pointer = (&(*pointer)->next);
- if ((*pointer)->arg != NULL)
- printf("%s\n", (*pointer)->arg);
- }
- }
- }
- }
- void printf_prs(pr_list **curr){
- pr_list **pointer;
- pointer = curr;
- if (*curr != NULL){
- if ((*pointer)->pr != NULL){
- printf("%s\n", (*pointer)->pr);
- printf_args(&(*pointer)->args);
- while ((*pointer)->next != NULL){
- pointer = (&(*pointer)->next);
- if ((*pointer)->pr != NULL)
- printf("%s\n", (*pointer)->pr);
- printf_args(&(*pointer)->args);
- }
- }
- }
- }
- void free_args(arg_list **curr){
- arg_list *pointer;
- pointer = *curr;
- if (*curr != NULL){
- while ((*curr)->next){
- pointer = (*curr)->next;
- if ((*curr)->arg)
- free((*curr)->arg);
- free(*curr);
- (*curr) = pointer;
- }
- }
- }
- void free_prs(pr_list **curr){
- pr_list *pointer;
- pointer = *curr;
- if (*curr != NULL){
- if ((*curr)->args != NULL)
- free_args(&((*curr)->args));
- while ((*curr)->next != NULL){
- pointer = (*curr)->next;
- if ((*curr)->pr)
- free((*curr)->pr);
- if ((*curr)->args != NULL)
- free_args(&(pointer->args));
- free(*curr);
- (*curr) = pointer;
- }
- }
- }
- void free_pipes(pipe_list **curr){
- pipe_list *pointer;
- pointer = *curr;
- if (*curr != NULL){
- if ((*curr)->line != NULL){
- free((*curr)->line);
- if ((*curr)->prs != NULL)
- free_prs(&((*curr)->prs));
- while ((*curr)->next != NULL){
- pointer = (*curr)->next;
- if ((*curr)->prs != NULL)
- free_prs(&(pointer->prs));
- free(*curr);
- (*curr) = pointer;
- }
- }
- }
- }
- int word_len(char* word){
- int i=0;
- if (strcmp(word,"")){
- for (; word[i] != '\0'; i++);
- }
- return i;
- }
- int word_counter(arg_list **curr){
- int i;
- arg_list *pointer;
- pointer = *curr;
- if (*curr != NULL){
- if ((*curr)->arg != NULL){
- while ((*curr)->next != NULL){
- pointer = (*curr)->next;
- i++;
- (*curr) = pointer;
- }
- }
- }
- return i;
- }
- void make_arr(pr_list **curr, char *exec_arg[]){
- //arg_list *c_arg;
- //c_arg = ((*curr)->args);
- //int i, j, word_num, flag = 1;
- /*if ((*curr)->args){
- word_num = word_counter((&(*curr)->args));
- for (i = 0; i <= word_num; i++){
- exec_arg[i] = calloc(word_len, sizeof(char));
- }
- for (j = 0; j < word_len && (*curr)->pr[j] != '\0'; j++){
- exec_arg[0][j] = (*curr)->pr[j];
- }
- for (; j < word_len; j++){
- exec_arg[0][j] = '\0';
- }
- for (i = 1; i <= word_num && flag; i++){
- for (j = 0; j < word_len && c_arg->arg[j] != '\0' ; j++){
- exec_arg[i][j] = c_arg->arg[j];
- }
- for (; j < word_len; j++){
- exec_arg[i][j] = '\0';
- }
- if (c_arg->next != NULL){
- c_arg = c_arg->next;
- }else{
- flag = 0;
- }
- }
- }*/
- }
- void put_pr(pr_list **curr, char *new_pr){
- int i, j;
- j = word_len(new_pr);
- (*curr)->pr = calloc(word_len(new_pr), sizeof(char));
- for (i = 0; i < j; i++){
- (*curr)->pr[i] = new_pr[i];
- }
- }
- void put_arg(arg_list **curr, char *new_arg){
- int i, j;
- j = word_len(new_arg);
- (*curr)->arg = calloc(j, sizeof(char));
- for (i = 0; i < j; i++){
- (*curr)->arg[i] = new_arg[i];
- }
- }
- /* MAIN */
- int main(int argc, char *argv[]){
- int i, len, flag, pr_flag, not_empty, back_mode;
- char *new_line, *word_beg;
- pipe_list *executing_pipes, *c_pipe;
- pr_list *c_pr;
- arg_list *c_arg;
- c_pipe = malloc(sizeof(pipe_list));
- c_pipe->prs = malloc(sizeof(pr_list));
- c_pipe->prs->args = malloc(sizeof(arg_list));
- c_pipe->line = NULL;
- c_pipe->next = NULL;
- c_pr = c_pipe->prs;
- c_pr->pr = NULL;
- c_pr->next = NULL;
- c_arg = c_pr->args;
- c_arg->next = NULL;
- c_arg->arg = NULL;
- executing_pipes = c_pipe;
- while ((new_line = getline_unlim()) != NULL){
- len = strlen(new_line);
- pr_flag = 1;
- not_empty = 0;
- back_mode = 0;
- /* processing line, creating pipe */
- c_pipe->line = calloc(len + 1, sizeof(char));
- c_pipe->line = new_line;
- for (i = 0, word_beg = new_line; i < len; i++){
- flag = 0; back_mode = 0;
- if (!isalnum(new_line[i]) || new_line[i] == '\0'){
- if (new_line[i] == '|'){
- flag = 1;
- }
- if (new_line[i] == '&'){
- back_mode = 1;
- }
- new_line[i] = '\0';
- if (flag && (!not_empty)){
- printf("pipe was written incorrectly\n");
- if (c_pipe->prs != NULL){
- //printf_prs(&(c_pipe->prs));
- free_prs(&(c_pipe->prs));
- }
- not_empty = 1;
- break;
- }
- if ( strcmp(word_beg,"") && strcmp(word_beg, " ") && !back_mode ){
- if (pr_flag == 1){
- put_pr(&c_pr, word_beg);
- //c_pr->next = malloc(sizeof(arg_list));
- not_empty = 1;
- pr_flag = 0;
- }else{
- put_arg(&c_arg, word_beg);
- c_arg->next = malloc(sizeof(arg_list));
- c_arg = c_arg->next;
- c_arg->arg = NULL;
- c_arg->next = NULL;
- }
- }
- word_beg = new_line + i + 1;
- if (flag == 1){
- c_pr->next = malloc(sizeof(pr_list));
- c_pr->next->args = malloc(sizeof(arg_list));
- c_pr = c_pr->next;
- c_pr->next = NULL;
- c_pr->pr = NULL;
- c_arg = c_pr->args;
- c_arg->next = NULL;
- c_arg->arg = NULL;
- pr_flag = 1;
- not_empty = 0;
- }
- }
- }
- if (strcmp(word_beg,"")){
- if (pr_flag){
- put_pr(&c_pr, word_beg);
- }else{
- put_arg(&c_arg, word_beg);
- }
- }else{
- if ((flag || pr_flag) && (!not_empty)){
- printf("pipe was written incorrectly\n");
- if (c_pipe->prs != NULL){
- //printf_prs(&(c_pipe->prs));
- free_prs(&(c_pipe->prs));
- }
- }
- }
- /* pipe was created */
- /* executing pipe */
- /* */
- printf_prs(&(c_pipe->prs));
- if (!back_mode){
- if (c_pipe && c_pipe->prs){
- c_pr = c_pipe->prs;
- int j, i = 0;
- j = 1 + word_counter(&(c_pr->args));
- printf("%d\n", j);
- // char* exec_arg[j];
- // for (i = 0; i < j; i++){
- // exec_arg[i] = calloc(2, sizeof(char*));
- // }
- // c_arg = (c_pr->args);
- // int k, flag = 1;
- /*if (c_pr){
- for (i = 0; i < 2; i++){
- for (k = 0; k < word_len && c_pr->pr[k] != '\0'; j++){
- exec_arg[i][k] = c_pr->pr[k];
- exec_arg[i] = realloc(exec_arg[i], k + 1);
- }
- for (; k < word_len; j++){
- exec_arg[i][k] = '\0';
- }
- }*/
- /*for (; i <= j && flag; i++){
- for (j = 0; j < word_len && c_arg->arg[j] != '\0' ; j++){
- exec_arg[i][j] = c_arg->arg[j];
- }
- for (; j < word_len; j++){
- exec_arg[i][j] = '\0';
- }
- if (c_arg->next != NULL){
- c_arg = c_arg->next;
- }else{
- flag = 0;
- }
- }*/
- // }
- /*for (i; i < j; i++)
- printf("%s ", mass[i]);
- printf("\n");*/
- }
- }else{
- printf("background mode\n");
- }
- c_pipe->next = malloc(sizeof(pipe_list));
- c_pipe->next->prs = malloc(sizeof(pr_list));
- c_pipe->next->prs->args = malloc(sizeof(arg_list));
- c_pipe->next->line = NULL;
- c_pipe->next->next = NULL;
- c_pr = c_pipe->next->prs;
- c_pr->pr = NULL;
- c_pr->next = NULL;
- c_arg = c_pr->args;
- c_arg->next = NULL;
- c_arg->arg = NULL;
- c_pipe = c_pipe->next;
- }
- free_pipes(&executing_pipes);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement