Advertisement
diofanto33

ADT .scommand .pipeline

Aug 14th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.22 KB | Software | 0 0
  1. #include <assert.h>
  2. #include <glib.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. #include "command.h"
  8. #include "strextra.h"
  9.  
  10. #define CHR_SPACE " "
  11. #define CHR_REDIR_IN "<"
  12. #define CHR_REDIR_OUT ">"
  13. #define CHR_PIPE "|"
  14. #define CHR_BG "&"
  15.  
  16. /* https://docs.gtk.org/glib/struct.SList.html */
  17.  
  18. struct scommand_s {
  19.     GSList *args;   /* simply linked list */
  20.     char *redir_in;
  21.     char *redir_out;
  22. };
  23.  
  24. scommand
  25. scommand_new(void)
  26. {
  27.     scommand result = NULL;
  28.     result = calloc(1, sizeof(*result));
  29.     assert(result!=NULL);
  30.  
  31.     result->args = NULL;
  32.     result->redir_in = NULL;
  33.     result->redir_out = NULL;
  34.  
  35.     assert(scommand_is_empty(result));
  36.     assert(scommand_get_redir_in(result)==NULL);
  37.     assert(scommand_get_redir_out(result)==NULL);
  38.  
  39.     return(result);
  40. }
  41.  
  42. scommand
  43. scommand_destroy(scommand killme)
  44. {
  45.     assert(killme!=NULL);
  46.     /* free the arguments */
  47.     while(killme->args!=NULL) /* while there are arguments */
  48.     {
  49.         scommand_pop_front(killme);
  50.     }
  51.     /* free the redirections */
  52.     if(killme->redir_in)
  53.     {
  54.         free(killme->redir_in);
  55.         killme->redir_in=NULL;
  56.     }
  57.     if(killme->redir_out)
  58.     {
  59.         free(killme->redir_out);
  60.         killme->redir_out=NULL;
  61.     }
  62.     free(killme);
  63.     killme=NULL;
  64.  
  65.     return(killme);
  66. }
  67.  
  68. void
  69. scommand_push_back(scommand self, char *argument)
  70. {
  71.     assert(self!=NULL);
  72.     assert(argument!=NULL);
  73.     self->args = g_slist_append(self->args, (gpointer)argument);
  74.     assert(self!=NULL);
  75.     assert(!scommand_is_empty(self));
  76. }
  77.  
  78. void
  79. scommand_pop_front(scommand self)
  80. {
  81.     assert(self!=NULL);
  82.     assert(!scommand_is_empty(self));
  83.     free((char*) g_slist_nth_data(self->args, 0u));
  84.     self->args = g_slist_delete_link(self->args, self->args);
  85. }
  86.  
  87. void
  88. scommand_set_redir_in(scommand self, char * filename)
  89. {
  90.     assert(self!=NULL);
  91.     if(self->redir_in!=NULL)
  92.     {
  93.         free(self->redir_in);
  94.         self->redir_in=NULL;
  95.     }
  96.     self->redir_in = filename;
  97. }
  98.  
  99. void
  100. scommand_set_redir_out(scommand self, char * filename)
  101. {
  102.     assert(self!=NULL);
  103.     if(self->redir_out!=NULL)
  104.     {
  105.         free(self->redir_out);
  106.         self->redir_out=NULL;
  107.     }
  108.     self->redir_out = filename;
  109. }
  110.  
  111. bool
  112. scommand_is_empty(const scommand self)
  113. {
  114.     assert(self!=NULL);
  115.    
  116.     return(g_slist_length(self->args) == 0u);
  117. }
  118.  
  119. unsigned int
  120. scommand_length(const scommand self)
  121. {
  122.     assert(self!=NULL);
  123.     unsigned int result = 0u;
  124.     result = g_slist_length(self->args);
  125.     assert((result==0u) == scommand_is_empty(self));
  126.  
  127.     return(result);
  128. }
  129.  
  130. char *
  131. scommand_front(const scommand self)
  132. {
  133.     assert(self!=NULL);
  134.     assert(!scommand_is_empty(self));
  135.     char *result = NULL;
  136.     result = (char*) g_slist_nth_data(self->args, 0u);
  137.     assert(result!=NULL);
  138.  
  139.     return(result);
  140. }
  141.  
  142. char *
  143. scommand_get_redir_in(const scommand self)
  144. {
  145.     assert(self!=NULL);
  146.    
  147.     return(self->redir_in);
  148. }
  149.  
  150. char *
  151. scommand_get_redir_out(const scommand self)
  152. {
  153.     assert(self!=NULL);
  154.    
  155.     return(self->redir_out);
  156. }
  157.  
  158. /*
  159. static char *
  160. strmerge_f(char *s1, char *s2)
  161. {
  162.     char *aux = NULL;
  163.     char *result = strmrege(s1, s2);
  164.     free(aux);
  165.     return(result);
  166. }
  167. */
  168.  
  169. char *
  170. scommand_to_string(const scommand self)
  171. {
  172.     assert(self!=NULL);
  173.  
  174.     char *result = strdup("");
  175.     GSList *curr_arg = NULL;
  176.  
  177.     curr_arg = self->args;
  178.     while(curr_arg!=NULL)
  179.     {
  180.         char *arg;
  181.         arg = (char*) curr_arg->data;
  182.         result = strmerge(result, arg);
  183.         curr_arg = g_slist_next(curr_arg);
  184.         if(curr_arg!=NULL)
  185.         {
  186.             result = strmerge(result, CHR_SPACE);
  187.         }
  188.     }
  189.     assert(scommand_is_empty(self) || strlen(result)>0u);
  190.     if(scommand_get_redir_in(self)!=NULL)
  191.     {
  192.         result = strmerge(result, CHR_SPACE);
  193.         result = strmerge(result, CHR_REDIR_IN);
  194.         result = strmerge(result, CHR_SPACE);
  195.         result = strmerge(result, scommand_get_redir_in(self));
  196.     }
  197.     assert(scommand_get_redir_in(self)==NULL || strlen(result)>0u);
  198.     if(scommand_get_redir_out(self)!=NULL)
  199.     {
  200.         result = strmerge(result, CHR_SPACE);
  201.         result = strmerge(result, CHR_REDIR_OUT);
  202.         result = strmerge(result, CHR_SPACE);
  203.         result = strmerge(result, scommand_get_redir_out(self));
  204.     }
  205.     assert(scommand_get_redir_out(self)==NULL || strlen(result)>0u);
  206.  
  207.     return(result);
  208. }
  209.  
  210. struct pipeline_s {
  211.     GSList *scmd;
  212.     bool wait;
  213. };
  214.  
  215. pipeline
  216. pipeline_new(void)
  217. {
  218.     pipeline result = NULL;
  219.     result = calloc(1, sizeof(*result));
  220.     assert(result!=NULL);
  221.  
  222.     result->scmd = NULL;
  223.     result->wait = true;
  224.  
  225.     assert(result!=NULL);
  226.     assert(pipeline_is_empty(result));
  227.     assert(pipeline_get_wait(result) == true);
  228.  
  229.     return(result);
  230. }
  231.  
  232. pipeline
  233. pipeline_destroy(pipeline killme)
  234. {
  235.     assert(killme!=NULL);
  236.     while(killme->scmd!=NULL)
  237.     {
  238.         pipeline_pop_front(killme);
  239.     }
  240.     free(killme);
  241.     killme = NULL;
  242.  
  243.     return(killme);
  244. }
  245.  
  246. void
  247. pipeline_push_back(pipeline self, scommand sc)
  248. {
  249.     assert(self!=NULL);
  250.     assert(sc!=NULL);
  251.     self->scmd = g_slist_append(self->scmd, (gpointer)sc);
  252.     assert(self!=NULL);
  253.     assert(!pipeline_is_empty(self));
  254. }
  255.  
  256. void
  257. pipeline_pop_front(pipeline self)
  258. {
  259.     assert(self!=NULL);
  260.     assert(!pipeline_is_empty(self));
  261.     scommand_destroy(pipeline_front(self));
  262.     self->scmd = g_slist_delete_link(self->scmd, self->scmd);
  263. }
  264.  
  265. void
  266. pipeline_set_wait(pipeline self, const bool wait)
  267. {
  268.     assert(self!=NULL);
  269.     self->wait = wait;
  270. }
  271.  
  272. bool
  273. pipeline_is_empty(const pipeline self)
  274. {
  275.     assert(self!=NULL);
  276.     bool result = false;
  277.     result = (g_slist_length(self->scmd) == 0u);
  278.  
  279.     return(result);
  280. }
  281.  
  282. unsigned int
  283. pipeline_length(const pipeline self)
  284. {
  285.     assert(self!=NULL);
  286.     unsigned int result = 0u;
  287.     result = g_slist_length(self->scmd);
  288.     assert((result==0u) == pipeline_is_empty(self));
  289.  
  290.     return(result);
  291. }
  292.  
  293. scommand
  294. pipeline_front(const pipeline self)
  295. {
  296.     assert(self!=NULL);
  297.     assert(!pipeline_is_empty(self));
  298.     scommand result = NULL;
  299.     result = (scommand)g_slist_nth_data(self->scmd, 0u);
  300.     assert(result!=NULL);
  301.  
  302.     return(result);
  303. }
  304.  
  305. bool
  306. pipeline_get_wait(const pipeline self)
  307. {
  308.     assert(self!=NULL);
  309.  
  310.     return(self->wait);
  311. }
  312.  
  313. char *
  314. pipeline_to_string(const pipeline self)
  315. {
  316.     assert(self != NULL);
  317.     char* result = strdup("");
  318.     char* str = NULL;
  319.     GSList* curr_scmd = NULL;
  320.  
  321.     curr_scmd = self->scmd;
  322.     while(curr_scmd != NULL)
  323.     {
  324.         result = strmerge(result, str=scommand_to_string(curr_scmd->data));
  325.         free(str);
  326.         curr_scmd = g_slist_next(curr_scmd);
  327.         if(curr_scmd != NULL)
  328.         {
  329.             result = strmerge(result, CHR_SPACE);
  330.             result = strmerge(result, CHR_PIPE);
  331.             result = strmerge(result, CHR_SPACE);
  332.         }
  333.     }
  334.     assert(pipeline_is_empty(self) || strlen(result)>0u);
  335.     if(!pipeline_get_wait(self))
  336.     {
  337.         result = strmerge(result, CHR_SPACE);
  338.         result = strmerge(result, CHR_BG);
  339.     }
  340.     assert(pipeline_get_wait(self) || strlen(result)>0u);
  341.  
  342.     return(result);
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement