Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <glib.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include "command.h"
- #include "strextra.h"
- #define CHR_SPACE " "
- #define CHR_REDIR_IN "<"
- #define CHR_REDIR_OUT ">"
- #define CHR_PIPE "|"
- #define CHR_BG "&"
- /* https://docs.gtk.org/glib/struct.SList.html */
- struct scommand_s {
- GSList *args; /* simply linked list */
- char *redir_in;
- char *redir_out;
- };
- scommand
- scommand_new(void)
- {
- scommand result = NULL;
- result = calloc(1, sizeof(*result));
- assert(result!=NULL);
- result->args = NULL;
- result->redir_in = NULL;
- result->redir_out = NULL;
- assert(scommand_is_empty(result));
- assert(scommand_get_redir_in(result)==NULL);
- assert(scommand_get_redir_out(result)==NULL);
- return(result);
- }
- scommand
- scommand_destroy(scommand killme)
- {
- assert(killme!=NULL);
- /* free the arguments */
- while(killme->args!=NULL) /* while there are arguments */
- {
- scommand_pop_front(killme);
- }
- /* free the redirections */
- if(killme->redir_in)
- {
- free(killme->redir_in);
- killme->redir_in=NULL;
- }
- if(killme->redir_out)
- {
- free(killme->redir_out);
- killme->redir_out=NULL;
- }
- free(killme);
- killme=NULL;
- return(killme);
- }
- void
- scommand_push_back(scommand self, char *argument)
- {
- assert(self!=NULL);
- assert(argument!=NULL);
- self->args = g_slist_append(self->args, (gpointer)argument);
- assert(self!=NULL);
- assert(!scommand_is_empty(self));
- }
- void
- scommand_pop_front(scommand self)
- {
- assert(self!=NULL);
- assert(!scommand_is_empty(self));
- free((char*) g_slist_nth_data(self->args, 0u));
- self->args = g_slist_delete_link(self->args, self->args);
- }
- void
- scommand_set_redir_in(scommand self, char * filename)
- {
- assert(self!=NULL);
- if(self->redir_in!=NULL)
- {
- free(self->redir_in);
- self->redir_in=NULL;
- }
- self->redir_in = filename;
- }
- void
- scommand_set_redir_out(scommand self, char * filename)
- {
- assert(self!=NULL);
- if(self->redir_out!=NULL)
- {
- free(self->redir_out);
- self->redir_out=NULL;
- }
- self->redir_out = filename;
- }
- bool
- scommand_is_empty(const scommand self)
- {
- assert(self!=NULL);
- return(g_slist_length(self->args) == 0u);
- }
- unsigned int
- scommand_length(const scommand self)
- {
- assert(self!=NULL);
- unsigned int result = 0u;
- result = g_slist_length(self->args);
- assert((result==0u) == scommand_is_empty(self));
- return(result);
- }
- char *
- scommand_front(const scommand self)
- {
- assert(self!=NULL);
- assert(!scommand_is_empty(self));
- char *result = NULL;
- result = (char*) g_slist_nth_data(self->args, 0u);
- assert(result!=NULL);
- return(result);
- }
- char *
- scommand_get_redir_in(const scommand self)
- {
- assert(self!=NULL);
- return(self->redir_in);
- }
- char *
- scommand_get_redir_out(const scommand self)
- {
- assert(self!=NULL);
- return(self->redir_out);
- }
- /*
- static char *
- strmerge_f(char *s1, char *s2)
- {
- char *aux = NULL;
- char *result = strmrege(s1, s2);
- free(aux);
- return(result);
- }
- */
- char *
- scommand_to_string(const scommand self)
- {
- assert(self!=NULL);
- char *result = strdup("");
- GSList *curr_arg = NULL;
- curr_arg = self->args;
- while(curr_arg!=NULL)
- {
- char *arg;
- arg = (char*) curr_arg->data;
- result = strmerge(result, arg);
- curr_arg = g_slist_next(curr_arg);
- if(curr_arg!=NULL)
- {
- result = strmerge(result, CHR_SPACE);
- }
- }
- assert(scommand_is_empty(self) || strlen(result)>0u);
- if(scommand_get_redir_in(self)!=NULL)
- {
- result = strmerge(result, CHR_SPACE);
- result = strmerge(result, CHR_REDIR_IN);
- result = strmerge(result, CHR_SPACE);
- result = strmerge(result, scommand_get_redir_in(self));
- }
- assert(scommand_get_redir_in(self)==NULL || strlen(result)>0u);
- if(scommand_get_redir_out(self)!=NULL)
- {
- result = strmerge(result, CHR_SPACE);
- result = strmerge(result, CHR_REDIR_OUT);
- result = strmerge(result, CHR_SPACE);
- result = strmerge(result, scommand_get_redir_out(self));
- }
- assert(scommand_get_redir_out(self)==NULL || strlen(result)>0u);
- return(result);
- }
- struct pipeline_s {
- GSList *scmd;
- bool wait;
- };
- pipeline
- pipeline_new(void)
- {
- pipeline result = NULL;
- result = calloc(1, sizeof(*result));
- assert(result!=NULL);
- result->scmd = NULL;
- result->wait = true;
- assert(result!=NULL);
- assert(pipeline_is_empty(result));
- assert(pipeline_get_wait(result) == true);
- return(result);
- }
- pipeline
- pipeline_destroy(pipeline killme)
- {
- assert(killme!=NULL);
- while(killme->scmd!=NULL)
- {
- pipeline_pop_front(killme);
- }
- free(killme);
- killme = NULL;
- return(killme);
- }
- void
- pipeline_push_back(pipeline self, scommand sc)
- {
- assert(self!=NULL);
- assert(sc!=NULL);
- self->scmd = g_slist_append(self->scmd, (gpointer)sc);
- assert(self!=NULL);
- assert(!pipeline_is_empty(self));
- }
- void
- pipeline_pop_front(pipeline self)
- {
- assert(self!=NULL);
- assert(!pipeline_is_empty(self));
- scommand_destroy(pipeline_front(self));
- self->scmd = g_slist_delete_link(self->scmd, self->scmd);
- }
- void
- pipeline_set_wait(pipeline self, const bool wait)
- {
- assert(self!=NULL);
- self->wait = wait;
- }
- bool
- pipeline_is_empty(const pipeline self)
- {
- assert(self!=NULL);
- bool result = false;
- result = (g_slist_length(self->scmd) == 0u);
- return(result);
- }
- unsigned int
- pipeline_length(const pipeline self)
- {
- assert(self!=NULL);
- unsigned int result = 0u;
- result = g_slist_length(self->scmd);
- assert((result==0u) == pipeline_is_empty(self));
- return(result);
- }
- scommand
- pipeline_front(const pipeline self)
- {
- assert(self!=NULL);
- assert(!pipeline_is_empty(self));
- scommand result = NULL;
- result = (scommand)g_slist_nth_data(self->scmd, 0u);
- assert(result!=NULL);
- return(result);
- }
- bool
- pipeline_get_wait(const pipeline self)
- {
- assert(self!=NULL);
- return(self->wait);
- }
- char *
- pipeline_to_string(const pipeline self)
- {
- assert(self != NULL);
- char* result = strdup("");
- char* str = NULL;
- GSList* curr_scmd = NULL;
- curr_scmd = self->scmd;
- while(curr_scmd != NULL)
- {
- result = strmerge(result, str=scommand_to_string(curr_scmd->data));
- free(str);
- curr_scmd = g_slist_next(curr_scmd);
- if(curr_scmd != NULL)
- {
- result = strmerge(result, CHR_SPACE);
- result = strmerge(result, CHR_PIPE);
- result = strmerge(result, CHR_SPACE);
- }
- }
- assert(pipeline_is_empty(self) || strlen(result)>0u);
- if(!pipeline_get_wait(self))
- {
- result = strmerge(result, CHR_SPACE);
- result = strmerge(result, CHR_BG);
- }
- assert(pipeline_get_wait(self) || strlen(result)>0u);
- return(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement