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 ", pointer->arg);
- while (pointer->next){
- pointer = (pointer->next);
- if (pointer->arg != NULL){
- if (!strcmp(pointer->arg, "")){
- printf("ПУСТАЯ СТРОКА\n");
- }else printf("%s ", pointer->arg);
- }
- }
- }
- }
- }
- void printf_prs(pr_list *curr){
- pr_list *pointer;
- pointer = curr;
- if (curr != NULL){
- if (pointer->pr != NULL){
- printf("%s ", pointer->pr);
- printf_args(pointer->args);
- while (pointer->next != NULL){
- pointer = (pointer->next);
- printf("| ");
- if (pointer->pr != NULL){
- if (!strcmp(pointer->pr, "")){
- printf("ПУСТАЯ СТРОКА\n");
- }else printf("%s ", pointer->pr);
- printf_args(pointer->args);
- }
- }
- }
- }
- printf("\n");
- }
- int word_counter(arg_list *curr){
- int i = 0;
- arg_list *pointer;
- pointer = curr;
- if (curr != NULL){
- if (curr->arg != NULL){
- i++;
- while (curr->next != NULL){
- pointer = curr->next;
- if (curr->arg)
- i++;
- curr = pointer;
- }
- }
- }
- return i;
- }
- /* MAIN */
- int main(int argc, char *argv[]){
- int i, j, k, len, pipe_flag, pr_flag, arg_flag, not_empty, back_mode, word_begin, word_end;
- char *new_line, *cur_word;
- pipe_list *c_pipe;
- pr_list *c_pr;
- arg_list *c_arg;
- c_pipe = calloc(1, sizeof(pipe_list));
- c_pipe->next = calloc(1, sizeof(pipe_list));
- c_pipe->prs = calloc(1, sizeof(pr_list));
- c_pipe->prs->args = calloc(1, 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;
- pipe_list *executing_pipes = calloc(1, sizeof(pipe_list));
- executing_pipes = c_pipe;
- pipe_flag = 1;
- while ((new_line = getline_unlim()) != NULL){
- if (pipe_flag){
- c_pipe->next = malloc(sizeof(pipe_list));
- c_pipe = c_pipe->next;
- 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;
- pipe_flag = 0;
- }
- len = strlen(new_line);
- /* processing line, creating pipe */
- c_pipe->line = calloc(len + 1, sizeof(char));
- for (i = 0; i < len + 1; i++){
- c_pipe->line[i] = new_line[i];
- }
- pr_flag = 0; arg_flag = 0;
- word_begin = 0; word_end = 0;
- not_empty = 0; back_mode = 0;
- for (i = 0; i < len; i++){
- if (pr_flag){;
- 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 = 0;
- }else if (arg_flag){
- c_arg->next = malloc(sizeof(arg_list));
- c_arg = c_arg->next;
- c_arg->arg = NULL;
- c_arg->next = NULL;
- arg_flag = 0;
- }
- if ((!isalnum(new_line[i]) && new_line[i] != '-')){
- if (new_line[i] == '|'){
- pr_flag = 1;
- arg_flag = 0;
- not_empty = 0;
- }
- if (new_line[i] == '&'){
- back_mode = 1;
- }
- new_line[i] = '\0';
- word_end = i;//
- k = word_end - word_begin;
- if (k != 0){
- cur_word = malloc(sizeof(char));
- cur_word = realloc(cur_word, k);
- for (j = 0; j < k; j++){
- cur_word[j] = new_line[word_begin + j];
- }
- word_begin = i + 1;
- }
- if (word_begin != word_end){
- if (!not_empty){
- c_pr->pr = malloc(sizeof(char));
- c_pr->pr = realloc(c_pr->pr, strlen(cur_word) + 1);
- if (strcmp(cur_word,"")){
- k = strlen(cur_word);
- for (j = 0; j < k; j++){
- c_pr->pr[j] = cur_word[j];
- }
- c_pr->pr[j] = '\0';
- free(cur_word);
- not_empty = 1;
- continue;
- }
- }else{
- c_arg->arg = malloc(sizeof(char));
- c_arg->arg = realloc(c_arg->arg, strlen(cur_word));
- if (strcmp(cur_word,"")){
- k = strlen(cur_word);
- for (j = 0; j < k; j++){
- c_arg->arg[j] = cur_word[j];
- }
- free(cur_word);
- arg_flag = 1;
- continue;
- }
- }
- }
- }
- }
- k = i - word_begin;
- if (k != 0){
- cur_word = malloc(sizeof(char));
- cur_word = realloc(cur_word, k);
- for (j = 0; j < k; j++){
- cur_word[j] = new_line[word_begin + j];
- }
- }
- if (word_begin != i){
- if (!not_empty){
- c_pr->pr = malloc(sizeof(char));
- c_pr->pr = realloc(c_pr->pr, strlen(cur_word) + 1);
- if (strcmp(cur_word,"")){
- k = strlen(cur_word);
- for (j = 0; j < k; j++){
- c_pr->pr[j] = cur_word[j];
- }
- c_pr->pr[j] = '\0';
- free(cur_word);
- not_empty = 1;
- }
- }else{
- c_arg->arg = malloc(sizeof(char));
- c_arg->arg = realloc(c_arg->arg, strlen(cur_word));
- if (strcmp(cur_word,"")){
- k = strlen(cur_word);
- for (j = 0; j < k; j++){
- c_arg->arg[j] = cur_word[j];
- }
- free(cur_word);
- arg_flag = 1;
- }
- }
- }
- /* pipe was created */
- /* executing pipe */
- /* */
- printf_prs(c_pipe->prs);
- if (!back_mode){
- if (c_pipe && c_pipe->prs){
- c_pr = c_pipe->prs;
- if (c_pr->args != NULL){
- j = 2 + word_counter(c_pr->args);
- }
- else j = 2;
- 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, word_len;
- if (c_pr){
- word_len = strlen(c_pr->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");
- }
- pipe_flag = 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement