void free_command(command_t to_be_freed) {
    
    enum command_type cmd_type = to_be_freed->type;
    
    free(to_be_freed->input);
    free(to_be_freed->output);
    
    if (cmd_type == AND_COMMAND || cmd_type == OR_COMMAND || cmd_type == PIPE_COMMAND || cmd_type == SEQUENCE_COMMAND) {
        
        free_command(to_be_freed->u.command[0]);
        free_command(to_be_freed->u.command[1]);
        
        //we're outta there, now free yourself
        free(to_be_freed->u.command[0]);
        free(to_be_freed->u.command[1]);
        
    }
    
    else if (cmd_type == SIMPLE_COMMAND) {
        
        char **w = to_be_freed->u.word;
        while ( *++w) {
            free(*w);
        }
        //free(w);
    }
    
    else //(cmd_type == SUBSHELL_COMMAND)
    {
        
        free_command(to_be_freed->u.subshell_command);
        free(to_be_freed->u.subshell_command);
        
    }
}
int main(int argc, char **argv) {

	GList *o;
	void *plugin_data = NULL;
	OSyncError *error = NULL;

	if (!g_thread_supported())
		g_thread_init(NULL);

	parse_args(argc, argv);
	/* Set defaults if not set on the command line */
	if (!syncgroup) 
		syncgroup = strdup("osyncplugin");
	
	if (pluginlist) {
		if (!plugin_list(&error))
			goto error;
			
		goto success;
	}

	if (!init(&error))
		goto error;

	for (o=cmdlist; o; o = o->next)
		if (!run_command((Command *) o->data, &plugin_data, &error))
			goto error_disconnect_and_finalize;


 success:
	if (plugin_env)
		osync_plugin_env_unref(plugin_env);

	for (o=cmdlist; o; o = o->next) {
		Command *cmd = o->data;
		free_command(&cmd);
	}


	return EXIT_SUCCESS;

 error_disconnect_and_finalize:
	if (plugin_data)
		disconnect(NULL, NULL);
	//error_finalize:
	finalize_plugin(&plugin_data);
	//error_free_plugin_env:
	if (plugin_env)
		osync_plugin_env_unref(plugin_env);

	for (o=cmdlist; o; o = o->next) {
		Command *cmd = o->data;
		free_command(&cmd);
	}

 error:	
	fprintf(stderr, "Error: %s\n", osync_error_print(&error));
	osync_error_unref(&error);
	return EXIT_FAILURE;
}
Exemple #3
0
void free_command(command_t c)
{
  switch(c->type)
  {
    case SIMPLE_COMMAND:
      {
        free(c);
        break;
      }
    case SUBSHELL_COMMAND:
      {
        free_command(c->u.subshell_command);
        break;
      }
    case AND_COMMAND:
    case OR_COMMAND:
    case PIPE_COMMAND:
    case SEQUENCE_COMMAND:
      {
        free_command(c->u.command[0]);
        free_command(c->u.command[1]);
        break;
      }
  }
  return;
}
/*
 * Function that returns a malloc'd string containing
 * the env value.
 * CHECKED: functions, length, width, brackets TODO comments...
 */
char *get_env(char *check, char **env)
{
	int i = 0;
	char **env_var;
	char *ret;
	char *ptr;
	while (env[i] != NULL) {
		env_var = string_split(env[i], '=');
		if (strings_compare(env_var[0], check) == 0) {
			/* found variable */
			ptr = env[i];
			while (*ptr != '=') {
			ptr++;
		}
		ptr++;
		ret = malloc(sizeof(char) * (str_len(ptr)+1));
		string_copy(ret, ptr);
		free_command(env_var);
		return ret;
	}
	free_command(env_var);
	i++;
	}
	return 0;
}
Exemple #5
0
//Frees a list of commands
void free_command_list(Command** cl)
{
	int indx = 0;
	while(cl[indx] != NULL)
	{
		free_command(cl[indx]);
		indx++;
	}
	free_command(cl[indx]);
	free(cl);
}
void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data) {
    apr_status_t rv;
    struct redis_command *command = NULL;

    info_print("enter: consumer thread\n");

    while (1) {
        rv = apr_queue_pop(queue, (void **)&command);

        if (rv == APR_EINTR)
            continue;

        if (rv == APR_EOF) {
    	    info_print("queue has terminated, consumer thread exit\n");
            break;
        }

        if (rv != APR_SUCCESS  ) {
            apr_sleep(1000* 1000); //sleep 1 second.
            continue;
        }

        if(command) {
            int res = _do_redis_command((const char **)command->argv,(const size_t *)command->argvlen, command->arg_count);
            free_command(command);
            command = NULL;
        }
    }

    info_print("exit:consumer thread\n");
    return NULL;
}
static int
process_commands_stream(struct cfg *cf, struct rtpp_cmd_connection *rcc,
  double dtime, struct rtpp_command_stats *csp, struct rtpp_stats_obj *rsc)
{
    int rval;
    struct rtpp_command *cmd;

    rval = rtpp_command_stream_doio(cf, rcc);
    if (rval <= 0) {
        return (-1);
    }
    do {
        cmd = rtpp_command_stream_get(cf, rcc, &rval, dtime, csp);
        if (cmd == NULL) {
            if (rval != 0) {
                break;
            }
            continue;
        }
        cmd->laddr = sstosa(&rcc->csock->bindaddr);
        if (cmd->cca.op == GET_STATS) {
            flush_cstats(rsc, csp);
        }
        if (cmd->no_glock == 0) {
            pthread_mutex_lock(&cf->glock);
        }
        rval = handle_command(cf, cmd);
        if (cmd->no_glock == 0) {
            pthread_mutex_unlock(&cf->glock);
        }
        free_command(cmd);
    } while (rval == 0);
    return (rval);
}
Exemple #8
0
/**
* Executes a command, setting the right environment and eventually forking.
*/
unsigned execute(Command* command)
{
    int toRet = 1;

	//Setup the execution, updating stdout/stdin based on command
    if(command->fun)
    {
        assert(command->fun);
        toRet = command->fun(command->argv);
    }
    else
    {
        logm("Command: %s, args: %s\n", command->name, command->argv[1]);
        //Try search for an external command.
        external_command_pid = fork();
        switch(external_command_pid){
            case -1:
                perror("Fork:"); 
                exit(EXIT_FAILURE);
                break;
            case 0: //child
                execvp(command->name, command->argv);
                printf("%s: command not found\n", command->name);
                exit(127);
                break;
            default: //father
                waitpid(external_command_pid, &toRet, 0);
                toRet = WEXITSTATUS(toRet);
        }
        external_command_pid = NULL; //TODO: Check this lol
    }
    free_command(command);
    addEnvInt("?", toRet);
	return toRet > 0 ? toRet : -toRet;
}
Exemple #9
0
/**
 * Process the buffer
 * Return TRUE to exit program
 */
int process_buf(char **bufargs) {
    struct Command *cmd;
    int argc = 0;

    /*  cd command*/
    if (!strcmp(bufargs[0], "cd") && bufargs[0][2] == '\0') {
        set_cwd(bufargs[1]);
        return FALSE;
    }

    /* exit command */
    if (!strcmp(bufargs[0], "exit") && bufargs[0][4] == '\0') {
        return TRUE;
    }

    /* Get argc */
    while (bufargs[argc++] != ARR_END);

    /* Set up command and first process */
    cmd = init_command();
    cmd->procHead = init_process(argc * sizeof(char *));
    cmd->runningProcs++;

    /* Allocate arguments to their processes within the command */
    if (allocate_args(argc, bufargs, cmd) == TRUE) {
        /* Invalid command */
        free_command(NULL, cmd);
        return FALSE;
    }

    /* Add the command to the global commands */
    add_global(cmd);

    /* For each process in the command, fork it and set it up as running */
    fork_command(cmd);

    /* If foreground command, then wait on processes */
    if (cmd->type == FG) {
        wait_running(cmd);

        /* set the foreground command to null and clean it */
        globalCmd[FG] = NULL;
        free_command(NULL, cmd);
    }

    return FALSE;
}
Exemple #10
0
/*** function run_heap ***
Run the memory allocation as outlined in lab 3:
https://eee.uci.edu/16s/36680/labs/lab3_malloc.pdf
*/
void heap_alloc() {
  char *heap = malloc (400);
  struct Command input; // Input read in from the command line.
  // Set all values in the heap to zero
  memset(heap, '\0', 400);
  *(header_t*)heap = 400;
  // Run the heap_alloc's loop.
  while(1) {
    // Read in input from the console and run the command.
    if(read_command(&input) > 0) {
      // Quit condition.
      if(strcmp(input.program, "quit") == 0) {
        // End Program
        free_command(&input);
        break;
      }
      else if (strcmp(input.program, "allocate") == 0 && input.len == 2){
		    //Allocate Heap Space
        allocate_block(heap, input.array);
  	  }
  	  else if (strcmp(input.program, "free") == 0 && input.len == 2){
  		  //Free Heap Space
        free_block(heap, input.array);
  	  }
  	  else if (strcmp(input.program, "blocklist") == 0 && input.len == 1){
  		  //Prints out Block Info
        print_blocklist(heap, input.array);
  	  }
  	  else if (strcmp(input.program, "writeheap") == 0 && input.len == 4){
  		  //Writes X chars to heap block
        write_block(heap, input.array);
  	  }
  	  else if (strcmp(input.program, "printheap") == 0 && input.len == 3){
  		  //Prints out heap w/out header
        print_heap(heap, input.array);
  	  }
  	  else{
  		  fprintf(stderr, "Error: Invalid command or invalid arguments.\n");
  	  }
  	} else {
        fprintf(stderr, "Error: Unable to read in input.\n");
  	}
    free_command(&input);
  }
  // Deallocate the heap.
  free(heap);
}
int child(char **command, char **env, char *line)
{
  int status;
  pid_t pid;
  pid = fork();

  if ( pid == -1)
  {
    perror("fork");
    return (1);
  }
  else if (pid == 0)
  {
 execvp(command[0], command);

    if ( str_cmp(command[0],"env") == 0)
    {
      call_env(command,env);
      free(line);
      free_command(command);
      exit(0);
    }
    else if( str_cmp (command[0], "$?") == 0)
    {
      printf("%d\n",status);
      exit(0);
    }
    else
    {
      printf("%s: command not found\n",command[0]);
      free(line);
      free_command(command);
      exit(0);
    }
    return (1);
    free(line);
    free_command(command);
  }
  else
  {
    wait(&status);
  }
  return status;
}
Exemple #12
0
void 		clean_mem(t_mysh *mysh)
{
  int 		ret;

  ret = mysh->ret_val;
  free_list(&mysh->tab_sel_ctrl);
  free_command(mysh);
  free_struct(mysh);
  exit(ret);
}
void
free_command (command_t c)
{
	if(c->type == SIMPLE_COMMAND)
	{
		/*free(c->input);
		free(c->output);
		free(c->c.word);*/
	}
	else if(c->type == SUBSHELL_COMMAND)
	{
		free_command(c->u.subshell_command);
	}
	else
	{
		free_command(c->u.command[0]);
		free_command(c->u.command[1]);
	}
	free(c);
}
Exemple #14
0
void		do_subshell(t_shell *shell, char *str, int b_exit)
{
	t_command	*command;

	shell->env = update_shlvl(shell->env);
	command = cut_semicolon(shell, str);
	execute(shell, command, NEXT_DEFAULT);
	free_command(command);
	if (b_exit)
		exit(shell->last_status);
}
Exemple #15
0
void			stop_cmd(t_list *pipeline)
{
	t_list	*tmp;

	while (pipeline)
	{
		tmp = pipeline->next;
		free_command(pipeline->content);
		free(pipeline);
		pipeline = tmp;
	}
}
Exemple #16
0
// Execute a command without piping.
exec_error execute_unpiped(Command *cmd) {
    int childPid, status;
    exec_error error = NONE;
    
    childPid = fork();

    if (childPid == 0) {
        // Set IO redirections before executing
        if (cmd->inputfd > -1) {
            dup2(cmd->inputfd, STDIN_FILENO);
            close(cmd->inputfd);
        }
        if (cmd->outputfd > -1) {
            dup2(cmd->outputfd, STDOUT_FILENO);
            close(cmd->outputfd);
        }
        if (execvp(cmd->argv[0], cmd->argv) != 0) {
            if (errno == ENOENT) {
                error = COMMAND_NOT_FOUND;
            } else {
                error = OTHER;
            }
        }
        // Exit with status corresponding to the exec_error value.
        exit((int) error);
    } else {
        // Wait as parent. Close any file descriptors not needed.
        if (cmd->inputfd > -1) {
            close(cmd->inputfd);
        }
        if (cmd->outputfd > -1) {
            close(cmd->outputfd);
        }
        if (cmd->background) {
            // Command needs to be backgrounded, do not block waiting.
            //waitpid(childPid, &status, WNOHANG);
            printf("%d\n", childPid);
        } else {
            // Wait blocking until foreground command completes.
            waitpid(childPid, &status, 0);

            // Check if child exited with a command not found error.
            if (WIFEXITED(status) && ((exec_error) WEXITSTATUS(status)) 
                    == COMMAND_NOT_FOUND) {
                fprintf(stderr, "No such command.\n");
            }
        }
    }

    free_command(cmd);
    return error;
}
Exemple #17
0
void 		my_exit(t_mysh *mysh)
{
  int 		exite;

  exite = (mysh->tab_com[1]) ? check_str(mysh->tab_com[1]) : 0;
  if (comp_str(mysh->tab_com[0], "exit") == 1)
    {
      if (aff_prompt() == 1)
	my_putstr("exit\n");
      free_command(mysh);
      free_struct(mysh);
      exit(exite);
    }
}
void
clear_job (job *ptr)
{
    if (ptr != NULL)
    {
        /*
        xdebug ("[%d] %d (%s)",
                ptr->content->job,
                ptr->content->pid,
                ptr->content->cmd);
        */
        free_command (ptr->content);
        xfree (ptr);
    }
}
static int
process_commands(struct rtpp_ctrl_sock *csock, struct cfg *cf, int controlfd, double dtime,
  struct rtpp_command_stats *csp, struct rtpp_stats_obj *rsc,
  struct rtpp_cmd_rcache_obj *rcp)
{
    int i, rval;
    struct rtpp_command *cmd;
    int umode;

    umode = RTPP_CTRL_ISDG(csock);
    i = 0;
    do {
        cmd = get_command(cf, controlfd, &rval, dtime, csp, umode, rcp);
        if (cmd == NULL && rval == 0) {
            /*
             * get_command() failed with error other than I/O error
             * or something, there might be some good commands in
             * the queue.
             */
            continue;
        }
        if (cmd != NULL) {
            cmd->laddr = sstosa(&csock->bindaddr);
            if (cmd->cca.op == GET_STATS) {
                flush_cstats(rsc, csp);
            }
            if (cmd->no_glock == 0) {
                pthread_mutex_lock(&cf->glock);
            }
            i = handle_command(cf, cmd);
            if (cmd->no_glock == 0) {
                pthread_mutex_unlock(&cf->glock);
            }
            free_command(cmd);
        } else {
            i = -1;
        }
    } while (i == 0 && umode != 0);
    return (i);
}
Exemple #20
0
void
execute_command (command_t c, bool time_travel)
{
  /* FIXME: Replace this with your implementation.  You may need to
     add auxiliary functions and otherwise modify the source code.
     You can also use external functions defined in the GNU C Library.  */
  if (time_travel)
  {
    // Implement Me
    error (1, 0, "time travel execution not yet implemented");
    return;
  }
  else
  {
    break_tree(c, array);
    form_tree(c,array);
    recurse_command(c);
    free_command(c);
  }


  return;
}
Exemple #21
0
int main(void)
{
	entry* entry_head = calloc(sizeof(entry), 1);
	snapshot* snapshot_head = calloc(sizeof(snapshot), 1);

	int latest_snapshotID = 1;

	char buffer[MAX_LINE_LENGTH];

	printf("> ");

	while(fgets(buffer, sizeof(buffer), stdin))
	{
		struct command_struct *command = get_command_struct(buffer);
		if(!command) continue;


		if(strcmp(command->args_malloc_ptr[0], "bye") == 0)
		{
			bye_command(snapshot_head, entry_head);
			free_command(command);
			printf("bye");
			return 0;
		}
		else if(strcmp(command->args_malloc_ptr[0], "help") == 0)
		{
			print_help_string();
		}
		else if(strcmp(command->args_malloc_ptr[0], "list") == 0)
		{
			list_command(command, entry_head, snapshot_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "get") == 0)
		{
			get_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "del") == 0)
		{
			del_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "purge") == 0)
		{
			purge_command(command, entry_head, snapshot_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "set") == 0)
		{
			set_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "push") == 0)
		{
			push_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "append") == 0)
		{
			append_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "pick") == 0)
		{
			pick_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "pluck") == 0)
		{
			pluck_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "pop") == 0)
		{
			pop_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "drop") == 0)
		{
			drop_command(command, snapshot_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "rollback") == 0)
		{
			rollback_command(command, snapshot_head, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "checkout") == 0)
		{
			checkout_command(command, snapshot_head, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "snapshot") == 0)
		{
			snapshot_command(snapshot_head, entry_head, &latest_snapshotID);
		}
		else if(strcmp(command->args_malloc_ptr[0], "min") == 0)
		{
			min_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "max") == 0)
		{
			max_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "sum") == 0)
		{
			sum_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "len") == 0)
		{
			len_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "rev") == 0)
		{
			rev_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "uniq") == 0)
		{
			uniq_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "sort") == 0)
		{
			sort_command(command, entry_head);
		}
		printf("\n> ");
		free_command(command);
	}
		bye_command(snapshot_head, entry_head);
	return 0;
}
Exemple #22
0
void fread_command( FILE * fp )
{
   char buf[MAX_STRING_LENGTH];
   const char *word;
   bool fMatch;
   CMDTYPE *command;

   CREATE( command, CMDTYPE, 1 );

   for( ;; )
   {
      word = feof( fp ) ? "End" : fread_word( fp );
      fMatch = FALSE;

      switch ( UPPER( word[0] ) )
      {
         case '*':
            fMatch = TRUE;
            fread_to_eol( fp );
            break;

	case 'C':
	    KEY( "Code",	command->fun_name, str_dup( fread_word( fp ) ) );
	    break;

	case 'E':
	    if ( !str_cmp( word, "End" ) )
	    {
		if( !command->name )
		{
		   bug( "%s", "Fread_command: Name not found" );
		   free_command( command );
		   return;
		}
		if( !command->fun_name )
		{
		   bug( "fread_command: No function name supplied for %s", command->name );
		   free_command( command );
		   return;
		}
		/*
	 	 * Mods by Trax
		 * Fread in code into char* and try linkage here then
		 * deal in the "usual" way I suppose..
		 */
	      command->do_fun = skill_function( command->fun_name );
		if( command->do_fun == skill_notfound )
		{
		   bug( "Fread_command: Function %s not found for %s", command->fun_name, command->name );
		   free_command( command );
		   return;
		}
		add_command( command );
		return;
	    }
	    break;

         case 'L':
            KEY( "Level", command->level, fread_number( fp ) );
            KEY( "Log", command->log, fread_number( fp ) );
            break;

         case 'N':
            KEY( "Name", command->name, fread_string_nohash( fp ) );
            break;

         case 'P':
            KEY( "Position", command->position, fread_number( fp ) );
            break;
      }

      if( !fMatch )
      {
         sprintf( buf, "Fread_command: no match: %s", word );
         bug( buf, 0 );
      }
   }
}
// Frees the command arrays commands and file tree
void free_command_array_dependents (command_array cmd_arr)
{
	free_command (cmd_arr.command_tree);
    free_file_tree (cmd_arr.files);
}
Exemple #24
0
int main(int argc, char *argv[]) {

    /* The shell process itself ignores SIGINT. */
    struct sigaction action;
    action.sa_handler = SIG_IGN;
    action.sa_flags = 0;
    sigemptyset(&action.sa_mask);
    if (sigaction(SIGINT, &action, NULL) == -1) {
        perror("sigaction");
        return EXIT_FAILURE;
    }

    while (1) {
        // Read the next command.
        command_t *command = read_command();

        // Check for finished background processes.
        int status;
        pid_t pid;
        while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
            printf("Background process %d finished\n", pid);
        }

        if (!command)
            continue; // Ignore empty commands.

        if (match(command->argv[0], "exit")) {
            // Handle built-in exit command.
            if (handle_exit(command)) {
                free_command(command);
                break;
            }
        } else if (match(command->argv[0], "cd")) {
            // Handle built-in cd command.
            handle_cd(command);
        } else {
            // Fork a child process.
            pid = fork();
            if (pid == -1) {
                perror("fork");
            } else if (pid == 0) {
                // Execute command.
                execvp(command->argv[0], command->argv);
                perror(command->argv[0]);
                exit(EXIT_FAILURE);
            } else {
                if (command->type == FOREGROUND) {
                    printf("Spawned foreground process pid: %d\n", pid);

                    struct timeval t0, t1;

                    // Wait for foreground process.
                    gettimeofday(&t0, NULL);
                    waitpid(pid, &status, 0);
                    gettimeofday(&t1, NULL);

                    printf("Foreground process %d terminated\n", pid);
                    printf("wallclock time: %.3f msec\n", elapsed_ms(&t0, &t1));
                } else {
                    printf("Spawned background process pid: %d\n", pid);
                }
            }
        }

        free_command(command);
    }

    clear_history(); // Clear GNU readline history.

    return EXIT_SUCCESS;
}