Ejemplo n.º 1
0
void execute_and_or_command(command_t command)
{


        if(command->type == AND_COMMAND){
                execute_wrapper(command->u.command[0]);
                command->status = command->u.command[0]->status;
                if(command_status(command->u.command[0]) == 0){
                        execute_wrapper(command->u.command[1]);
                        command->status = command->u.command[1]->status;
                }
                else
                		command->status = 1;

        }

        if(command->type == OR_COMMAND){
                execute_wrapper(command->u.command[0]);
                command->status = command->u.command[0]->status;
                //printf("%d\n",command->status);
                if(command_status(command->u.command[0]) > 0){
                        execute_wrapper(command->u.command[1]);
                        command->status = command->u.command[1]->status;
                }
                else
                	command->status = 0;
        }        
        
}        
Ejemplo n.º 2
0
static void hci_link_control(uint16_t ocf, int plen, uint8_t *data)
{
	uint8_t status;

	const uint16_t ogf = OGF_LINK_CTL;

	switch (ocf) {
	case OCF_CREATE_CONN:
		command_status(ogf, ocf, 0x00);
		create_connection(data);
		break;

	case OCF_ACCEPT_CONN_REQ:
		command_status(ogf, ocf, 0x00);
		accept_connection(data);
		break;

	case OCF_DISCONNECT:
		command_status(ogf, ocf, 0x00);
		disconnect(data);
		break;

	default:
		status = 0x01;
		command_complete(ogf, ocf, 1, &status);
		break;
	}
}
Ejemplo n.º 3
0
void executeANDCommand(command_t c)
{
	callCommand(c->u.command[0]);
	int cStatus1 = command_status(c->u.command[0]);
	if (cStatus1) 	// if true (fails, returns -1), whole AND command fails, you lose, get a life
		c->status = cStatus1;
	else {		// false, first command succeeded, run second command and save status in AND command
		callCommand(c->u.command[1]);
		c->status = command_status(c->u.command[1]);
	}
}
Ejemplo n.º 4
0
void executingOr(command_t c)
{
    execute_switch(c->u.command[0]);
    if (command_status(c->u.command[0]) == 0)
    {
	c->status = command_status(c->u.command[0]);
    } else {
	execute_switch(c->u.command[1]);
	c->status = command_status(c->u.command[1]);
    }
}
Ejemplo n.º 5
0
void executeORCommand(command_t c)
{
	callCommand(c->u.command[0]);
	int cStatus1 = command_status(c->u.command[0]);
	if (cStatus1) 	// if true (fails, returns -1), try the other command
	{
		callCommand(c->u.command[1]);
		c->status = command_status(c->u.command[1]);
	}	
	else {		// false, first command succeeded, can forget about the second command
		c->status = cStatus1;
	}
}
Ejemplo n.º 6
0
bool xRedisClient::lset(const RedisDBIdx& dbi, const string& key, int index, const string& value) {
	if (0 == key.length()) {
		return false;
	}
	SETDEFAULTIOTYPE(MASTER);
	return command_status(dbi, "LSET %s %d %s", key.c_str(), index, value.c_str());
}
Ejemplo n.º 7
0
Archivo: hciemu.c Proyecto: intgr/bluez
static void hci_status_param(uint16_t ocf, int plen, uint8_t *data)
{
	read_local_amp_info_rp ai;

	const uint16_t ogf = OGF_STATUS_PARAM;

	switch (ocf) {
	case OCF_READ_LOCAL_AMP_INFO:
		memset(&ai, 0, sizeof(ai));

		/* BT only */
		ai.amp_status = 0x01;
		ai.max_pdu_size = htobl(L2CAP_DEFAULT_MTU);
		ai.controller_type = HCI_AMP;
		ai.max_amp_assoc_length = htobl(HCI_MAX_ACL_SIZE);
		/* No flushing at all */
		ai.max_flush_timeout = 0xFFFFFFFF;
		ai.best_effort_flush_timeout = 0xFFFFFFFF;

		command_complete(ogf, ocf, sizeof(ai), &ai);
		break;

	default:
		command_status(ogf, ocf, 0x01);
		break;
	}
}
Ejemplo n.º 8
0
bool xRedisClient::ltrim(const RedisDBIdx& dbi, const string& key, int start, int end) {
	if (0 == key.length()) {
		return false;
	}
	SETDEFAULTIOTYPE(MASTER);
	return command_status(dbi, "LTRIM %s %d %d", key.c_str(), start, end);
}
Ejemplo n.º 9
0
bool CRedisClient::ltrim(const CString &key, int start, int stop)
{
	CString sStart, sStop;
	sStart.Format("%d", start);
	sStop.Format("%d", stop);
	return command_status("LTRIM %s %s %s", (LPCSTR)key, (LPCSTR)sStart, (LPCSTR)sStop);
}
Ejemplo n.º 10
0
bool CRedisClient::zremrangebyrank(const CString &key, int start, int stop)
{
	CString sStart, sStop;
	sStart.Format("%d", start);
	sStop.Format("%d", stop);
	return command_status("ZREMRANGEBYRANK %s %s %s", (LPCSTR)key, (LPCSTR)sStart, (LPCSTR)sStop);
}
Ejemplo n.º 11
0
void 
execute_and_command(command_t c)
{
	command_t left = c->u.command[0];
	command_t right = c->u.command[1];
	execute_command(left, 0);//recursively call the execute command
	if(command_status(left) == 0)
	{
		execute_command(right,0);
		c->status = command_status(right);
	}
	else//the left command is not runned successfully
	{
		//there is no need to execute the right command
		c->status = command_status(left);
	}
}
Ejemplo n.º 12
0
int
main (int argc, char **argv)
{
  int opt;
  int command_number = 1;
  int print_tree = 0;
  int time_travel = 0;
  program_name = argv[0];

  for (;;)
    switch (getopt (argc, argv, "pt"))
      {
      case 'p': print_tree = 1; break;
      case 't': time_travel = 1; break;
      default: usage (); break;
      case -1: goto options_exhausted;
      }
 options_exhausted:;

  // There must be exactly one file argument.
  if (optind != argc - 1)
    usage ();

  script_name = argv[optind];
  FILE *script_stream = fopen (script_name, "r");
  if (! script_stream)
  {
  return 0;
  //error (1, errno, "%s: cannot open", script_name);
  }
  command_stream_t command_stream =
    make_command_stream (get_next_byte, script_stream);

  command_t last_command = NULL;
  command_t command;
void execute_timeTravel(command_stream_t c);

	if(time_travel ==1)
	{ //execute in time travel mode 
	execute_timeTravel(command_stream);
	}
else{ //this means we are not in timetravel mode
  while ((command = read_command_stream (&command_stream)))
    {
      if (print_tree)
	{
	  printf ("# %d\n", command_number++);
	  print_command (command);
	}
      else
	{
	  last_command = command;
	  execute_command (command);
	}
    }
} //close the else statement
  return print_tree || !last_command ? 0 : command_status (last_command);
}
Ejemplo n.º 13
0
int
main (int argc, char **argv)
{
  int opt;
  int command_number = 1;
  int print_tree = 0;
  int time_travel = 0;
  program_name = argv[0];

  for (;;)
    switch (getopt (argc, argv, "pt"))
      {
      case 'p': print_tree = 1; break;
      case 't': time_travel = 1; break;
      default: usage (); break;
      case -1: goto options_exhausted;
      }
 options_exhausted:;

  // There must be exactly one file argument.
  if (optind != argc - 1)
    usage ();

  script_name = argv[optind];
  FILE *script_stream = fopen (script_name, "r");
  if (! script_stream)
    error (1, errno, "%s: cannot open", script_name);
  command_stream_t command_stream =
    make_command_stream (get_next_byte, script_stream);





  command_t last_command = NULL;
  command_t command;
  if (!time_travel){
    while ((command = read_command_stream (command_stream)))
      {
        if (print_tree)
	  {
	    printf ("# %d\n", command_number++);
	    print_command (command);
	  }
        else
	  {
	    last_command = command;
	    execute_command (command, time_travel);
	  }
      }
  }
  else{
    parallel_execute (command_stream);
  }

  return print_tree || !last_command ? 0 : command_status (last_command);
}
Ejemplo n.º 14
0
Archivo: hciemu.c Proyecto: intgr/bluez
static void hci_link_policy(uint16_t ocf, int plen, uint8_t *data)
{
	const uint16_t ogf = OGF_INFO_PARAM;

	switch (ocf) {
	default:
		command_status(ogf, ocf, 0x01);
		break;
	}
}
Ejemplo n.º 15
0
int
main (int argc, char **argv)
{
    int command_number = 1;
    bool print_tree = false;
    char const *profile_name = 0;
    program_name = argv[0];
    
    for (;;)
    switch (getopt (argc, argv, "p:t"))
    {
        case 'p': profile_name = optarg; break;
        case 't': print_tree = true; break;
        default: usage (); break;
        case -1: goto options_exhausted;
    }
    options_exhausted:;
    
    // There must be exactly one file argument.
    if (optind != argc - 1)
    usage ();
    
    script_name = argv[optind];
    FILE *script_stream = fopen (script_name, "r");
    if (! script_stream)
    error (1, errno, "%s: cannot open", script_name);
    command_stream_t command_stream =
    make_command_stream (get_next_byte, script_stream);
    int profiling = -1;
    if (profile_name)
    {
        profiling = prepare_profiling (profile_name);
        if (profiling < 0)
        error (1, errno, "%s: cannot open", profile_name);
    }
    
    command_t last_command = NULL;
    command_t command;
    while ((command = read_command_stream (command_stream)))
    {
        if (print_tree)
        {
            printf ("# %dn", command_number++);
            print_command (command);
        }
        else
        {
            last_command = command;
            execute_command (command, profiling);
        }
    }
    
    return print_tree || !last_command ? 0 : command_status (last_command);
}
Ejemplo n.º 16
0
bool xRedisClient::mset(const DBIArray& vdbi, const VDATA& vData) {
    DBIArray::const_iterator iter_dbi = vdbi.begin();
    VDATA::const_iterator iter_data = vData.begin();
    for (; iter_data != vData.end(); iter_dbi++) {
        const string &key = (*iter_data++);
        const string &value = (*iter_data++);
        const RedisDBIdx& dbi = *iter_dbi;
        SETDEFAULTIOTYPE(SLAVE);
        command_status(dbi, "SET %s %s", key.c_str(), value.c_str());
    }
    return true;
}
void
execute_graph(int** graph, command_stream_t stream, int N)
{
  int i,j,n = 0, m, np = N;
  while (graph[n]) n++;

  command_t* command_stream = malloc(sizeof(command_t)*n);
  int count = 0, status;
  while(count < n)
      command_stream[count++] = read_command_stream(stream);

  int *executed = malloc(sizeof(int)*n);

  for (i = 0; i<n; i++)
    executed[i] = 0;
  m = n;
  while (m > 0)
    {
      for (i = 0; i < n; i++)
	if (!executed[i])
	  {
	    int dependency = 0;
	    for (j = 0; j < n; j++)
	      if (graph[i][j]) dependency = 1;
	    if (!dependency)
	      {
		if (np == 0) // if the limit of subprocesses is met 
		  {
		    waitpid(WAIT_ANY, &status, WUNTRACED); // wait for any child to return
		    if (errno == EINTR || errno == EINVAL) perror("waitpid");
		    np = 1;
		  } // the check is put right before forking, so that the efficiency is maximized.
		m--; np--;
		executed[i] = 1;
		pid_t pid = fork();
		if (!pid)
		  {/* child process execute the command */
		    execute_command(command_stream[i], 0);
		    for (j = 0; j < n; j++)
		      graph[j][i] = 0;
		    exit(command_status(command_stream[i]));
		  }
	      }
	  }
    }
 
  while (waitpid(WAIT_ANY, &status, WUNTRACED))
     { 
       if (errno == ECHILD) break; 
       if (errno == EINTR || errno == EINVAL) perror("waitpid");
     }
}
Ejemplo n.º 18
0
void
execute_or_command (command_t c) {
  execute_command(c->u.command[0], 0);
  //puts("TESTOR");

  if (command_status(c->u.command[0]) == 0) {
      c->status = c->u.command[0]->status;
  }
  else {
      execute_command(c->u.command[1], 0);
      c->status = c->u.command[1]->status;
      //printf("%d\n", c->type);
  }
}
Ejemplo n.º 19
0
void
execute_and_command (command_t c)
{
  execute_command(c->u.command[0], 0);

  if (command_status(c->u.command[0]) == 0) {
      // run the second command
      execute_command(c->u.command[1], 0);
      // set the status of the AND command
      c->status = c->u.command[1]->status;
  }
  else {
      // do not run c2
      // set the status of the AND command
      c->status = c->u.command[0]->status;
  }
}
Ejemplo n.º 20
0
bool CRedisClient::hmset(const CString &key,ATF_MAP map)
{
	bool bRet = true;
	//iRetVal = 0;
	ATF_MAP::iterator it=map.begin();
	for( ;it!=map.end();it++)
	{
		CString strField = it->first;
		CString strValue = it->second;
		if(strField.GetLength() > 0 && strValue.GetLength() > 0)
		{
			bRet = bRet && command_status("HSET %s %s %s", (LPCSTR)key, (LPCSTR)strField, (LPCSTR)strValue);
			//iRetVal++;
		}
	}
	return bRet;
}
Ejemplo n.º 21
0
Archivo: hciemu.c Proyecto: intgr/bluez
static void hci_command(uint8_t *data)
{
	hci_command_hdr *ch;
	uint8_t *ptr = data;
	uint16_t ogf, ocf;

	ch = (hci_command_hdr *) ptr;
	ptr += HCI_COMMAND_HDR_SIZE;

	ch->opcode = btohs(ch->opcode);
	ogf = cmd_opcode_ogf(ch->opcode);
	ocf = cmd_opcode_ocf(ch->opcode);

	switch (ogf) {
	case OGF_LINK_CTL:
		hci_link_control(ocf, ch->plen, ptr);
		break;

	case OGF_LINK_POLICY:
		hci_link_policy(ocf, ch->plen, ptr);
		break;

	case OGF_HOST_CTL:
		hci_host_control(ocf, ch->plen, ptr);
		break;

	case OGF_INFO_PARAM:
		hci_info_param(ocf, ch->plen, ptr);
		break;

	case OGF_STATUS_PARAM:
		hci_status_param(ocf, ch->plen, ptr);
		break;

	case OGF_LE_CTL:
		hci_le_control(ocf, ch->plen, ptr);
		break;

	default:
		command_status(ogf, ocf, 0x01);
		break;
	}
}
Ejemplo n.º 22
0
Archivo: hciemu.c Proyecto: intgr/bluez
static void hci_le_control(uint16_t ocf, int plen, uint8_t *data)
{
	le_read_buffer_size_rp bs;

	const uint16_t ogf = OGF_LE_CTL;

	switch (ocf) {
	case OCF_LE_READ_BUFFER_SIZE:
		bs.status = 0;
		bs.pkt_len = htobs(VHCI_ACL_MTU);
		bs.max_pkt = htobs(VHCI_ACL_MAX_PKT);
		command_complete(ogf, ocf, sizeof(bs), &bs);
		break;

	default:
		command_status(ogf, ocf, 0x01);
		break;
	}
}
Ejemplo n.º 23
0
void executingSubshell(command_t c)
{
	int eStatus;
        pid_t pid = fork();
        if (pid < 0)
        {
               error(1, 0, "fork was unsuccessful\n");
        }
        else if (pid == 0) // child process
        {
                setupInOut(c); // redirects inputs and outputs
                execute_switch(c->u.subshell_command);
        	_exit(command_status(c->u.subshell_command));
	}
        else //parent process
        {
                waitpid(pid, &eStatus, 0);
                c->status = WEXITSTATUS(eStatus);
        }
}
Ejemplo n.º 24
0
int execute_command_type(command_t c)
{
    switch(c->type){
            
        case SIMPLE_COMMAND:
            execute(c);
            break;
            
        case PIPE_COMMAND:
            execute_pipe(c);
            break;
            
        case AND_COMMAND:
            // the first command already fails
            if(execute_command_type(c->u.command[0]) != 0){
                c->status = command_status(c->u.command[0]);
            }else{
                c->status = execute_command_type(c->u.command[1]);
            }
            break;
            
        case OR_COMMAND:
            if(execute_command_type(c->u.command[0]) == 0){
                c->status = 0;
            }else{
                // try the second command if the first fails
                c->status = execute_command_type(c->u.command[1]);
            }
            break;
            
        case SUBSHELL_COMMAND:
            c->status = execute_command_type(c->u.subshell_command);
            break;
            
        case SEQUENCE_COMMAND: // return the status of the last command
            execute_sequence(c);
            break;
    }
    return c->status;
}
Ejemplo n.º 25
0
int
excute_parallel (command_array *cmd_arr, size_t arr_size)
{
  int status;
  size_t i;
  node_t n;
  node_t pid_list;
  
  // List of pids to wait for
  pid_list = initialize_llist ();

  i = 0;
  // Go through all the processes
  while (i < arr_size)
    {
      int pid;
      size_t current = cmd_arr[i].ranking;
      while (i < arr_size && cmd_arr[i].ranking == current)
        {
          pid = fork();
          // Spawn the child process to execute the command
          if (pid == 0) // child process
            {
              execute_command (cmd_arr[i].command_tree);
              exit (command_status (cmd_arr[i].command_tree));
            }
          insert_node (pid_list, pid);
          i++;
        }
      // Wait for all the processes of this ranking to finish
      for (n = pid_list->next; n != pid_list; n = n->next)
        waitpid(n->val, &status, 0);
      // Processes finished waiting for
      while (pid_list != pid_list->next)
        remove_last_element (pid_list);
    }
  // Return the status of the last process waited on
  return status;
}
Ejemplo n.º 26
0
/**
 * The callback function for handling the !history command.
 */
static int command_history(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd)
{
	struct cbuffer* buf;
	struct chat_history_data* data = (struct chat_history_data*) plugin->ptr;
	struct plugin_command_arg_data* arg = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer);
	int maxlines;

	if (!list_size(data->chat_history))
		return command_status(plugin, user, cmd, cbuf_create_const("No messages."));

	if (arg)
		maxlines = arg->data.integer;
	else
		maxlines = data->history_default;
	
	buf = cbuf_create(MAX_HISTORY_SIZE);
	cbuf_append_format(buf, "*** %s: Chat History:\n", cmd->prefix);
	get_messages(data, maxlines, buf);

	plugin->hub.send_message(plugin, user, cbuf_get(buf));
	cbuf_destroy(buf);
	return 0;
}
Ejemplo n.º 27
0
int
main (int argc, char **argv)
{
  int command_number = 1;
  bool print_tree = false;
  char const *profile_name = 0;
  program_name = argv[0];

  for (;;)
    switch (getopt (argc, argv, "p:t"))
      {
      case 'p': profile_name = optarg; break;
      case 't': print_tree = true; break;
      default: usage (); break;
      case -1: goto options_exhausted;
      }
 options_exhausted:;

  // There must be exactly one file argument.
  if (optind != argc - 1)
    usage ();

  script_name = argv[optind];
  FILE *script_stream = fopen (script_name, "r");
  if (! script_stream)
    error (1, errno, "%s: cannot open", script_name);
  command_stream_t command_stream =
    make_command_stream (get_next_byte, script_stream);
  int profiling = -1;
  if (profile_name)
    {
      profiling = prepare_profiling (profile_name);
      if (profiling < 0)
	error (1, errno, "%s: cannot open", profile_name);
    }

  command_t last_command = NULL;
  command_t command;

    //timer starts
    struct timespec absolute;
    double start_time, prev_user_CPU, prev_sys_CPU;
    get_timer(&start_time, &prev_user_CPU, &prev_sys_CPU);
  while ((command = read_command_stream (command_stream)))
    {
      if (print_tree)
	{
	  printf ("# %d\n", command_number++);
	  print_command (command);
	}
      else
	{
	  last_command = command;
	  execute_command (command, profiling);
	}
    }
    //timer stops
    clock_gettime(CLOCK_REALTIME, &absolute);
    double absolute_time = absolute.tv_sec + (double)absolute.tv_nsec/1000000000.0;
    double end_time, current_user_CPU, current_sys_CPU;
    get_timer(&end_time, &current_user_CPU, &current_sys_CPU);
    double real_time = end_time - start_time;
    double userCPU = current_user_CPU-prev_user_CPU;
    double sysCPU = current_sys_CPU-prev_sys_CPU;
    
    pid_t pid = getpid();
    char buffer[1024];
    sprintf(buffer, "%f %f %.3f %.3f %d\n", absolute_time, real_time, userCPU, sysCPU, (int)pid);
    write(profiling, buffer, strlen(buffer));
    

  return print_tree || !last_command ? 0 : command_status (last_command);
}
Ejemplo n.º 28
0
int
main (int argc, char **argv)
{
  int command_number = 1;
  bool print_tree = false;
  bool time_travel = false;

  bool output_to_file=false;
  bool error_to_file=false;
  bool all_to_file=false;
  
  program_name = argv[0];

  for (;;)
    switch (getopt (argc, argv, "ptvxoehas"))
      {
      case 'p': print_tree = true; break;
      case 't': time_travel = true; break;
      case 'x': x_option=true;break;
      case 'v': v_option=true;break;
      case 'o': output_to_file=true;break;
      case 'e': error_to_file=true;break;
      case 'a': all_to_file=true;break;
      case 's': s_option=true;break;
      case 'h':
	fprintf(stderr,"option p to print command trees without execution.\noption t to exploit parallelism between command trees.\n");
	fprintf(stderr,"option x to print simple commands and their arguments before execution.\noption v to print shell input before execution.\n");
	fprintf(stderr,"option o to save output to output.txt.\noption e to save error to error.txt.\n");
	fprintf(stderr,"option a to save output and error to  output_and_error.txt.\n");
	fprintf(stderr,"option s to slowly go through script, one command tree at a time\n");
	fprintf(stderr,"option x and v not available in combination with option t.\n");
	fprintf(stderr,"options x or v in combination with option s to tell what the next command is before continuing.\n");
	
	return 0;
      default: usage (); break;
      case -1: goto options_exhausted;
      }
 options_exhausted:;

  // There must be exactly one file argument.
  if (optind != argc - 1)
    usage ();

  if(time_travel)
    {
      if(x_option || v_option || s_option)
	{
	  no_debug_time_travel();
	}
      }
  if(print_tree && s_option)
    {
      error (1, 0, "usage: %s [-pxvtoehas] SCRIPT-FILE, cannot use step_mode and print tree", program_name);
    }

  script_name = argv[optind];


  if(s_option)
    {
      fprintf(stderr,"Step mode enabled. Press d to disable step mode, press a to abort further execution, or press enter to move onto the next command.\n");
    }


  if (all_to_file==false)
    {  
      if(output_to_file==true)
	{
	  
	  int fd=open("output.txt",O_CREAT|O_TRUNC|O_WRONLY,0644);
	  if(fd<0)
	    {
	      fprintf(stderr,"No available file descriptors");
	      return(1);
	    }
	  dup2(fd,1);
	  close(fd);

	}
      if(error_to_file==true)
	{
	  
	  int fd=open("error.txt",O_CREAT|O_TRUNC|O_WRONLY,0644);
	  if(fd<0)
	    {
	      fprintf(stderr,"No available file descriptors");
	      return(1);
	    }
	  dup2(fd,2);
	  close(fd);
	}
    }
  else//all to file is true
    {
      int fd=open("output_and_error.txt",O_CREAT|O_TRUNC|O_WRONLY,0644);
      if(fd<0)
	{
	  fprintf(stderr,"No available file descriptors");
	  return(1);
	}
      dup2(fd,2);
      dup2(fd,1);
      close(fd);


    }

  FILE *script_stream = fopen (script_name, "r");
  if (! script_stream)
    error (1, errno, "%s: cannot open", script_name);
  command_stream_t command_stream =
    make_command_stream (get_next_byte, script_stream);

  command_t last_command = NULL;
  command_t command;
  if(time_travel)
    {
      struct dependency_graph* graph=create_graph(command_stream);
      int final_status=0;
      final_status=execute_graph(graph);
    }
 
  if(print_tree==true || time_travel==false)
    {
      while ((command = read_command_stream (command_stream)))
	{
	  if (print_tree)
	    {
	      printf ("# %d\n", command_number++);
	      print_command (command);
	    }
	  else
	    {
	      last_command = command;
	      
	      execute_command (command, time_travel);
	    }
	}
    }
  return print_tree || !last_command ? 0 : command_status (last_command);
}
Ejemplo n.º 29
0
int
main (int argc, char **argv)
{
  int command_number = 1;
  bool print_tree = false;
  bool time_travel = false;
	bool time_travel_limit = false;
	int num_of_processes =0;
	program_name = argv[0];
	char c = ' ';

  for (;;)
    switch (getopt (argc, argv, "ptj"))
      {
      case 'p': print_tree = true;c = 'p'; break;
      case 't': time_travel = true; c = 't';break;
			case 'j': time_travel_limit = true;
								num_of_processes = atoi(argv[2]);
								c = 'j';
								break;
      default: usage (); break;
      case -1: goto options_exhausted;
      }
 options_exhausted:;

  // There must be exactly one file argument.
  if (optind != argc - 1)
	{
		if (argc > 1 && c == 'j' && optind == argc-2)
		{}
		else
		{
    usage ();
		}
	}	
	

  script_name = argv[argc - 1];
  FILE *script_stream = fopen (script_name, "r");
  if (! script_stream)
    error (1, errno, "%s: cannot open", script_name);
  command_stream_t command_stream =
    make_command_stream (get_next_byte, script_stream);

  command_t last_command = NULL;
  command_t command;
  while ((command = read_command_stream (command_stream)))
    {
      if (print_tree)
			{
				printf ("# %d\n", command_number++);
				print_command (command);
			}
      else
			{
				if (time_travel == false && time_travel_limit == false)
				{
				last_command = command;
				execute_command (command, time_travel);
				}
			}
    }
		if (time_travel == true)
		{execute(command_stream);}
		if (time_travel_limit == true)
		{execute_limit(command_stream, num_of_processes);}

  return print_tree || !last_command ? 0 : command_status (last_command);
}
Ejemplo n.º 30
0
int
main(int argc, char *argv[])
{
	char buf[1024];
	int i;
#ifdef TIOCGWINSZ
	struct winsize ws;

	if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0)
		TERMWIDTH = ws.ws_col;
#endif

	/* Start handling the arguments.
	 * monetdb [monetdb_options] command [options] [database [...]]
	 * this means we first scout for monetdb_options which stops as soon
	 * as we find a non-option argument, which then must be command */
	
	/* first handle the simple no argument case */
	if (argc <= 1) {
		command_help(0, NULL);
		return(1);
	}
	
	/* handle monetdb_options */
	for (i = 1; argc > i && argv[i][0] == '-'; i++) {
		switch (argv[i][1]) {
			case 'v':
				command_version();
			return(0);
			case 'q':
				monetdb_quiet = 1;
			break;
			case 'h':
				if (strlen(&argv[i][2]) > 0) {
					mero_host = &argv[i][2];
				} else {
					if (i + 1 < argc) {
						mero_host = argv[++i];
					} else {
						fprintf(stderr, "monetdb: -h needs an argument\n");
						return(1);
					}
				}
			break;
			case 'p':
				if (strlen(&argv[i][2]) > 0) {
					mero_port = atoi(&argv[i][2]);
				} else {
					if (i + 1 < argc) {
						mero_port = atoi(argv[++i]);
					} else {
						fprintf(stderr, "monetdb: -p needs an argument\n");
						return(1);
					}
				}
			break;
			case 'P':
				/* take care we remove the password from argv so it
				 * doesn't show up in e.g. ps -ef output */
				if (strlen(&argv[i][2]) > 0) {
					mero_pass = strdup(&argv[i][2]);
					memset(&argv[i][2], 0, strlen(mero_pass));
				} else {
					if (i + 1 < argc) {
						mero_pass = strdup(argv[++i]);
						memset(argv[i], 0, strlen(mero_pass));
					} else {
						fprintf(stderr, "monetdb: -P needs an argument\n");
						return(1);
					}
				}
			break;
			case '-':
				/* skip -- */
				if (argv[i][2] == '\0')
					break;
				if (strcmp(&argv[i][2], "version") == 0) {
					command_version();
					return(0);
				} else if (strcmp(&argv[i][2], "help") == 0) {
					command_help(0, NULL);
					return(0);
				}
			default:
				fprintf(stderr, "monetdb: unknown option: %s\n", argv[i]);
				command_help(0, NULL);
				return(1);
			break;
		}
	}

	/* check consistency of -h -p and -P args */
	if (mero_pass != NULL && (mero_host == NULL || *mero_host == '/')) {
		fprintf(stderr, "monetdb: -P requires -h to be used with a TCP hostname\n");
		exit(1);
	} else if (mero_host != NULL && *mero_host != '/' && mero_pass == NULL) {
		fprintf(stderr, "monetdb: -h requires -P to be used\n");
		exit(1);
	}

	/* see if we still have arguments at this stage */
	if (i >= argc) {
		command_help(0, NULL);
		return(1);
	}
	
	/* commands that do not need merovingian to be running */
	if (strcmp(argv[i], "help") == 0) {
		command_help(argc - i, &argv[i]);
		return(0);
	} else if (strcmp(argv[i], "version") == 0) {
		command_version();
		return(0);
	}

	/* use UNIX socket if no hostname given */
	if (mero_host == NULL || *mero_host == '/') {
		/* a socket looks like /tmp/.s.merovingian.<tcpport>, try
		 * finding such port.  If mero_host is set, it is the location
		 * where we should search, which defaults to '/tmp' */
		if (mero_host == NULL)
			mero_host = "/tmp";
		/* first try the port given (or else its default) */
		snprintf(buf, sizeof(buf), "%s/.s.merovingian.%d",
			 mero_host, mero_port == -1 ? 50000 : mero_port);
		if (control_ping(buf, -1, NULL) == 0) {
			mero_host = buf;
		} else {
			/* if port wasn't given, we can try and search
			 * for available sockets */
			if (mero_port == -1) {
				DIR *d;
				struct dirent *e;
				struct stat s;

				d = opendir(mero_host);
				if (d == NULL) {
					fprintf(stderr, "monetdb: cannot find a control socket, use -h and/or -p\n");
					exit(1);
				}
				while ((e = readdir(d)) != NULL) {
					if (strncmp(e->d_name, ".s.merovingian.", 15) != 0)
						continue;
					snprintf(buf, sizeof(buf), "%s/%s", mero_host, e->d_name);
					if (stat(buf, &s) == -1)
						continue;
					if (S_ISSOCK(s.st_mode)) {
						if (control_ping(buf, -1, NULL) == 0) {
							mero_host = buf;
							break;
						}
					}
				}
				closedir(d);
			}
		}

		if (mero_host != buf) {
			fprintf(stderr, "monetdb: cannot find a control socket, use -h and/or -p\n");
			exit(1);
		}
		/* don't confuse control_send lateron */
		mero_port = -1;
	}
	/* for TCP connections */
	if (mero_host != NULL && *mero_host != '/' && mero_port == -1)
		mero_port = 50000;

	/* handle regular commands */
	if (strcmp(argv[i], "create") == 0) {
		command_create(argc - i, &argv[i]);
	} else if (strcmp(argv[i], "destroy") == 0) {
		command_destroy(argc - i, &argv[i]);
	} else if (strcmp(argv[i], "lock") == 0) {
		command_lock(argc - i, &argv[i]);
	} else if (strcmp(argv[i], "release") == 0) {
		command_release(argc - i, &argv[i]);
	} else if (strcmp(argv[i], "status") == 0) {
		command_status(argc - i, &argv[i]);
	} else if (strcmp(argv[i], "start") == 0) {
		command_startstop(argc - i, &argv[i], START);
	} else if (strcmp(argv[i], "stop") == 0) {
		command_startstop(argc - i, &argv[i], STOP);
	} else if (strcmp(argv[i], "kill") == 0) {
		command_startstop(argc - i, &argv[i], KILL);
	} else if (strcmp(argv[i], "set") == 0) {
		command_set(argc - i, &argv[i], SET);
	} else if (strcmp(argv[i], "get") == 0) {
		command_get(argc - i, &argv[i]);
	} else if (strcmp(argv[i], "inherit") == 0) {
		command_set(argc - i, &argv[i], INHERIT);
	} else if (strcmp(argv[i], "discover") == 0) {
		command_discover(argc - i, &argv[i]);
	} else {
		fprintf(stderr, "monetdb: unknown command: %s\n", argv[i]);
		command_help(0, NULL);
	}

	if (mero_pass != NULL)
		free(mero_pass);

	return(0);
}