Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
  char *cmdline, **arglist;
  char *prompt;
  char **single_cmds;
  int i, cmd_number;
  int rv;

  prompt = DEL_PROMPT;
  setup();

  exec_cmds = malloc(sizeof(char*) * MAXEXECCMDS);

  while((cmdline = next_command(prompt, stdin)) != NULL)
  {
    single_cmds = split_command(cmdline);

    for (i = 0; single_cmds[i] != '\0'; ++i)
    {
      if ((arglist = splitline(single_cmds[i])) != NULL)
      {
        // for those arglist executed later, we don't free it.
        // we free later arglists in do_control_command's fi.
        if ((rv = process(arglist)) != -1)
          freelist(arglist);
      }
    }

    freelist(single_cmds);
    free(cmdline);
  }

  free(exec_cmds);
  return 0;
}
Ejemplo n.º 2
0
Archivo: shell.c Proyecto: lingmar/os16
int execute_command_line(char *line, char *argv[]) {

    int  num_of_commands = 0;
    enum cmd_pos pos     = unknown;
    int  left_pipe[2]    = {-1, -1};
    int  right_pipe[2]   = {-1, -1};

    do {

        // Repeat this loop for each of the commands separated by '|' in
        // the command pipeline.

        // Use the next_command() parser to get the position and argument
        // vector of the next command in the command pipeline.
        pos = next_command(line, argv);

        create_pipe(pos, right_pipe);


        fork_child(pos, left_pipe, right_pipe, argv);

        // Only the parent will return here!!!

        num_of_commands++;

        // The previous rigth pipe now becomes the current left pipe.
        shift_pipes(left_pipe, right_pipe);


    } while (pos != single && pos != last);

    return num_of_commands;
}
Ejemplo n.º 3
0
void control_up(int state)
{
	if(r_DrawConsole)
		next_command(state);
	else
		server_up(state);
}
Ejemplo n.º 4
0
int main(void)
{
	enum cmd_pos pos;
	char* argv[MAX_ARGV_SIZE];
	char str[MAX_CMD_SIZE] = "cat parse.c | grep argv | wc -l";
	do{
		int i;
		pos = next_command(str, argv);
		for(i = 0; argv[i] != NULL; i++)
		{
			printf("argv[%d]: %s\n", i, argv[i]);
		}
		print_position(pos);
	}while(pos != single && pos != last);
	return 0;
}
Ejemplo n.º 5
0
/* Main loop of a slave thread.
 * Send the current command to the slave machine and wait for a reply.
 * Resend command history if the slave machine is out of sync.
 * Returns when the connection with the slave machine is cut.
 * slave_lock is held on both entry and exit of this function. */
static void
slave_loop(FILE *f, char *reply_buf, struct slave_state *sstate, bool resend)
{
	char *to_send;
	int last_cmd_count = 0;
	int last_reply_id = -1;
	int reply_slot = -1;
	for (;;) {
		if (resend) {
			/* Resend complete or partial history */
			to_send = next_command(last_reply_id);
		} else {
			/* Wait for a new command. */
			while (last_cmd_count == cmd_count)
				pthread_cond_wait(&cmd_cond, &slave_lock);
			to_send = gtp_cmd;
		}

		/* Command available, send it to slave machine.
		 * If slave was out of sync, send the history.
		 * But first get binary arguments if necessary. */
		int bin_size = 0;
		void *bin_buf = get_binary_arg(sstate, gtp_cmd,
					       gtp_cmds + CMDS_SIZE - gtp_cmd,
					       &bin_size);
		/* Check that the command is still valid. */
		resend = true;
		if (!bin_buf) continue;

		/* Send the command and get the reply, which always ends with \n\n
		 * The slave machine sends "=id reply" or "?id reply"
		 * with id == cmd_id if it is in sync. */
		last_cmd_count = cmd_count;
		char buf[CMDS_SIZE];
		int reply_id = send_command(to_send, bin_buf, &bin_size, f,
					    sstate, buf);
		if (reply_id == -1) return;

		resend = process_reply(reply_id, buf, reply_buf, bin_buf, bin_size,
				       &last_reply_id, &reply_slot, sstate);
	}
}
Ejemplo n.º 6
0
t_exec	*next_exec(t_exec *it, t_exec *list, t_env *ev)
{
  int	num;

  if (ev->val_exit == 1)
    return (next_command(list, it->prev));
  if (it == list)
    return (it);
  num = elem_redirection(it->tab[0], "<<,>>,||,&&,<,>,|,&,;");
  if (num == 7 || num == 8)
    return (it->next);
  else if (num == 1 || num == 2 || num == 5 || num == 6)
    {
      if (num == 1)
	remove(".my_teemo");
      if (it->next != list)
	return (it->next->next);
      else
	return (it->next);
    }
  if (default_io(ev->stdin, ev->stdout, 3) == -1)
    return (NULL);
  return (it);
}
Ejemplo n.º 7
0
int main(int argc, char *argv[])
{
	static char		*cmdstr = NULL;
	struct cmd_set 		**cmd_set;
	static struct vfs_state vfs;
	int i;
	static char		*filename = NULL;

	/* make sure the vars that get altered (4th field) are in
	   a fixed location or certain compilers complain */
	poptContext pc;
	struct poptOption long_options[] = {
		POPT_AUTOHELP
		{"file",	'f', POPT_ARG_STRING,	&filename, 0, },
		{"command",	'c', POPT_ARG_STRING,	&cmdstr, 0, "Execute specified list of commands" },
		POPT_COMMON_SAMBA
		POPT_TABLEEND
	};


	setlinebuf(stdout);

	pc = poptGetContext("vfstest", argc, (const char **) argv,
			    long_options, 0);
	
	while(poptGetNextOpt(pc) != -1);


	poptFreeContext(pc);

	/* TODO: check output */
	reload_services(False);

	/* the following functions are part of the Samba debugging
	   facilities.  See lib/debug.c */
	setup_logging("vfstest", True);
	
	/* Load command lists */

	cmd_set = vfstest_command_list;

	while(*cmd_set) {
		add_command_set(*cmd_set);
		add_command_set(separator_command);
		cmd_set++;
	}

	/* some basic initialization stuff */
	sec_init();
	conn_init();
	vfs.conn = conn_new();
	string_set(&vfs.conn->user,"vfstest");
	for (i=0; i < 1024; i++)
		vfs.files[i] = NULL;

	/* some advanced initiliazation stuff */
	smbd_vfs_init(vfs.conn);

	/* Do we have a file input? */
	if (filename && filename[0]) {
		process_file(&vfs, filename);
		return 0;
	}

	/* Do anything specified with -c */
	if (cmdstr && cmdstr[0]) {
		char    *cmd;
		char    *p = cmdstr;
 
		while((cmd=next_command(&p)) != NULL) {
			process_cmd(&vfs, cmd);
		}
		
		return 0;
	}

	/* Loop around accepting commands */

	while(1) {
		pstring prompt;
		char *line;

		slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");

		line = smb_readline(prompt, NULL, completion_fn);

		if (line == NULL)
			break;

		if (line[0] != '\n')
			process_cmd(&vfs, line);
	}
	
	free(vfs.conn);
	return 0;
}
Ejemplo n.º 8
0
static int
execute_update_commands(WIMStruct *wim,
			const struct wimlib_update_command *cmds,
			size_t num_cmds,
			int update_flags)
{
	struct wim_inode_table *inode_table;
	struct wim_sd_set *sd_set;
	struct list_head unhashed_streams;
	struct update_command_journal *j;
	union wimlib_progress_info info;
	int ret;

	if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
		/* If we have at least one "add" command, create the inode and
		 * security descriptor tables to index new inodes and new
		 * security descriptors, respectively.  */
		inode_table = alloca(sizeof(struct wim_inode_table));
		sd_set = alloca(sizeof(struct wim_sd_set));

		ret = init_inode_table(inode_table, 9001);
		if (ret)
			goto out;

		ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
		if (ret)
			goto out_destroy_inode_table;

		INIT_LIST_HEAD(&unhashed_streams);
	} else {
		inode_table = NULL;
		sd_set = NULL;
	}

	/* Start an in-memory journal to allow rollback if something goes wrong
	 */
	j = new_update_command_journal(num_cmds,
				       &wim_get_current_image_metadata(wim)->root_dentry,
				       wim->lookup_table);
	if (!j) {
		ret = WIMLIB_ERR_NOMEM;
		goto out_destroy_sd_set;
	}

	info.update.completed_commands = 0;
	info.update.total_commands = num_cmds;
	ret = 0;
	for (size_t i = 0; i < num_cmds; i++) {
		DEBUG("Executing update command %zu of %zu (op=%"TS")",
		      i + 1, num_cmds, update_op_to_str(cmds[i].op));
		info.update.command = &cmds[i];
		if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
			ret = call_progress(wim->progfunc,
					    WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
					    &info, wim->progctx);
			if (ret)
				goto rollback;
		}

		switch (cmds[i].op) {
		case WIMLIB_UPDATE_OP_ADD:
			ret = execute_add_command(j, wim, &cmds[i], inode_table,
						  sd_set, &unhashed_streams);
			break;
		case WIMLIB_UPDATE_OP_DELETE:
			ret = execute_delete_command(j, wim, &cmds[i]);
			break;
		case WIMLIB_UPDATE_OP_RENAME:
			ret = execute_rename_command(j, wim, &cmds[i]);
			break;
		}
		if (unlikely(ret))
			goto rollback;
		info.update.completed_commands++;
		if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
			ret = call_progress(wim->progfunc,
					    WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
					    &info, wim->progctx);
			if (ret)
				goto rollback;
		}
		next_command(j);
	}

	commit_update(j);
	if (inode_table) {
		struct wim_image_metadata *imd;

		imd = wim_get_current_image_metadata(wim);

		list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
		inode_table_prepare_inode_list(inode_table, &imd->inode_list);
	}
	goto out_destroy_sd_set;

rollback:
	if (sd_set)
		rollback_new_security_descriptors(sd_set);
	rollback_update(j);
out_destroy_sd_set:
	if (sd_set)
		destroy_sd_set(sd_set);
out_destroy_inode_table:
	if (inode_table)
		destroy_inode_table(inode_table);
out:
	return ret;
}
Ejemplo n.º 9
0
/**
 * DESCRIPTION: 
 *
 * Prints a promt to the screen. Reads a command line of the following format from the  the user:
 * 
 *	cmd0 -a00 -b01 ... -z0n | cmd1 | cmd2 | ... | cmdN -an0 ... -znn
 *
 * For each command, the fork_and_pipe() function is called with the
 * appropiate argv and relative position of the command.
 * 
 * NOTE: 
 * 
 * You only have to add code at the end of main, see TODO comment. 
 */
int main() {
  
  // Allocate a buffer for the command line read from the user. 

  char line_buffer[COMMAND_LINE_BUFFER_SIZE];
  
  
  // We parse the command line using the next_command() parser. The
  // parser will populate the following array with the result of each call
  // to the parse.
  
  char* argv[MAX_ARGV_SIZE];  
    
  // Count the number of non empty command lines.
  
  int line_nr = 0;
 
  
  // Position of each command in a command line.
  
  enum cmd_pos pos;  // {single, first, middle, last}
  
  
  // Pipe read descriptor to the pipe to the "left".
  
  int left_pipe_read_fd = -1;

  // Count the number of children forked for each command line. 

  int children = 0; 
  

  while(1) {

    do {
      
      // Print the command prompt including the command line counter. 
      printf(" %d> ", line_nr);
      fflush(NULL);
      
      
      // Read input from the user. 
      if ( fgets(line_buffer, COMMAND_LINE_BUFFER_SIZE, stdin) == NULL) {
	perror("====== ERROR ====> READING COMMAND LINE FAILED :(");
	exit(EXIT_FAILURE);
      }

      // Exit if the user types "exit" and presses enter. 
      if (strcmp(line_buffer, "exit\n") == 0) {
	printf("     Goodbye!\n");
	exit(EXIT_SUCCESS);
      }
      
      // If the user presses enter without typing anything else, don't
      // update the command line counter, just start over again.
      
    } while (empty_line(line_buffer));
    
    // We got some input from the user.
    line_nr++;
    
    // Parse the command line
    do {
      pos = next_command(line_buffer, argv);
      
      // After the call to the next_command() parser function, the
      // command possition within the command line is now available in the pos
      // varialble.
      
      DBG("\n%6s command %s\n", pos2str(pos), argv[0]);
      
      // Loop through the argv and print the command data. 
          
      int i = 0;
      
      
      while (argv[i] != NULL) {
	DBG("         argv[%d] @ [0x%x] = \"%s\"\n", i, (unsigned int) argv[i], argv[i]);
	i++;
      }
      
      
      // Create a pipe fork a new process for the command. We also
      // must remember the read descriptor to the new pipe. This
      // descriptor will become the read descriptor to the pipe to the
      // "left" for the next command (if any).
      
      left_pipe_read_fd = pipe_and_fork(pos, argv, left_pipe_read_fd);
      
      children++;

      // When are we done?
    } while (pos != single && pos != last);
    
    
    DBG("\nAfter calling next_command(), line [0x%X]  ==>%s<==\n",  
	(unsigned int) &line_buffer, line_buffer);
    
    
    // The parent goes here after after all children have been 
    // created for the command. 
    
    // TODO: Make sure shell doesn't print a new prompt until 
    // all the command processes (children) have terminated.
    while(children>0){
      wait(NULL);   
      children--;
    }
    
  } // end while(1)
} // end of main()
Ejemplo n.º 10
0
int start_rendering()
{
	static int done = 0;
	static void * network_thread_data[2] = { NULL, NULL };
	static Uint32 last_frame_and_command_update = 0;

	SDL_Thread *network_thread;
	queue_t *message_queue;

#ifndef WINDOWS
	SDL_EventState(SDL_SYSWMEVENT,SDL_ENABLE);
#endif
	queue_initialise(&message_queue);
	network_thread_data[0] = message_queue;
	network_thread_data[1] = &done;
	network_thread = SDL_CreateThread(get_message_from_server, network_thread_data);

	/* Loop until done. */
	while( !done )
		{
			SDL_Event event;

			// handle SDL events
			in_main_event_loop = 1;
			while( SDL_PollEvent( &event ) )
				{
					done = HandleEvent(&event);
				}
			in_main_event_loop = 0;

			//advance the clock
			cur_time = SDL_GetTicks();

			//check for network data
			if(!queue_isempty(message_queue)) {
				message_t *message;

				while((message = queue_pop(message_queue)) != NULL)
				{
					process_message_from_server(message->data, message->length);
					free(message->data);
					free(message);
				}
			}
#ifdef	OLC
			olc_process();
#endif	//OLC
			my_tcp_flush(my_socket);    // make sure the tcp output buffer is set
			
			if (have_a_map && cur_time > last_frame_and_command_update + 60) {
				LOCK_ACTORS_LISTS();
				next_command();
				UNLOCK_ACTORS_LISTS();
				move_to_next_frame();
				last_frame_and_command_update = cur_time;
			}

			while (cur_time > next_second_time && real_game_second < 59)
			{
				real_game_second += 1;
				new_second();
				next_second_time += 1000;
			}

#ifdef NEW_SOUND
			weather_sound_control();
#endif	//NEW_SOUND

			if(!limit_fps || (cur_time-last_time && 1000/(cur_time-last_time) <= limit_fps))
			{
				weather_update();

                animate_actors();
				//draw everything
				draw_scene();
				last_time=cur_time;
			}
			else {
				SDL_Delay(1);//give up timeslice for anyone else
			}

#ifdef TIMER_CHECK
			//Check the timers to make sure that they are all still alive...
			check_timers();
#endif

			//cache handling
			if(cache_system)cache_system_maint();
			//see if we need to exit
			if(exit_now) {
				done = 1;
				break;
			}
		}
	if(!done) {
		done = 1;
	}
	LOG_INFO("Client closed");
	SDL_WaitThread(network_thread,&done);
	queue_destroy(message_queue);
	if(pm_log.ppl)free_pm_log();

	//save all local data
	save_local_data(NULL, 0);

#ifdef PAWN
	cleanup_pawn ();
#endif

#ifdef NEW_SOUND
	destroy_sound();		// Cleans up physical elements of the sound system and the streams thread
	clear_sound_data();		// Cleans up the config data
#endif // NEW_SOUND
	ec_destroy_all_effects();
	if (have_a_map)
	{
		destroy_map();
		free_buffers();
	}
	unload_questlog();
	save_item_lists();
	free_emotes();
	free_actor_defs();
	free_books();
	free_vars();
	cleanup_rules();
	save_exploration_map();
	cleanup_counters();
	cleanup_chan_names();
	cleanup_hud();
	SDL_RemoveTimer(draw_scene_timer);
	SDL_RemoveTimer(misc_timer);
	end_particles ();
	free_bbox_tree(main_bbox_tree);
	main_bbox_tree = NULL;
	free_astro_buffer();
	free_translations();
	free_skybox();
	/* Destroy our GL context, etc. */
	SDL_QuitSubSystem(SDL_INIT_AUDIO);
	SDL_QuitSubSystem(SDL_INIT_TIMER);
/*#ifdef WINDOWS
	// attempt to restart if requested
	if(restart_required > 0){
		LOG_INFO("Restarting %s", win_command_line);
		SDL_CreateThread(system, win_command_line);
	}
#endif  //WINDOWS
*/
#ifdef NEW_SOUND
	final_sound_exit();
#endif
#ifdef	CUSTOM_UPDATE
	stopp_custom_update();
#endif	/* CUSTOM_UPDATE */
	clear_zip_archives();

	destroy_tcp_out_mutex();

	if (use_frame_buffer) free_reflection_framebuffer();

	printf("doing SDL_Quit\n");
	fflush(stderr);
	SDL_Quit( );
	printf("done SDL_Quit\n");
	fflush(stderr);
	cleanup_mem();
	xmlCleanupParser();
	FreeXML();

	exit_logging();

	return(0);
}
Ejemplo n.º 11
0
int main(int argc, char **argv) {
  if (argc == 1) { 
    fprintf(stderr, "You need to supply a binary as an argument e.g ./zdb <bin>");
    exit(-1);
  }

  const char *path = argv[1];  
  const char *name = strrchr(path, '/');

  if (name) {
    name += 1;
  } else {
    name = path;
  }

  pid_t pid;

  switch(pid = fork())
  {
    // error
    case -1: 
      {
        perror("fork()");
        exit(-1);
      }   
      // this is our child
    case 0: 
      {
        ptrace(PTRACE_TRACEME, 1337, NULL);     
        execl(path, name, NULL);                
        perror("execl()");
        exit(-1);
      }
  }

  int status;
  long opcode[4];
  long previous_ip;

  wait(&status);

  while(1)
  {       
    if(WIFEXITED(status) || (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL))
    {
      printf("terminated");
      exit(0);
    }

    if(WIFSTOPPED(status))
    {           
      long rip =  ptrace(PTRACE_PEEKUSER, pid, 8 * RIP, NULL)-1;
      printf("Instruction stopped at: 0x%lx\n", rip);
    }

    /* read our user's command in */
    zdb_command command = next_command();
    switch(command.instruction) {
      case NEXT:
        {
          long ip =  ptrace(PTRACE_PEEKUSER, pid, 8 * RIP, NULL);

          if(previous_ip)
          {
            long opcode_len = ip - previous_ip;
            if(opcode_len > 0 && opcode_len < 16) 
              print_instruction(&opcode, opcode_len);
          }

          opcode[0] = ptrace(PTRACE_PEEKDATA, pid, ip, NULL);
          opcode[1] = ptrace(PTRACE_PEEKDATA, pid, ip + sizeof(long), NULL);
          previous_ip = ip;

          ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL);
          wait(&status);
          break;
        }
      case DUMP:
        dump_registers(pid);
        break;
      case HEXDUMP:
        {
          long addr;
          unsigned int size;
          sscanf(command.args, "%lx %u", &addr, &size);
          hexdump_region(addr, size, pid, true);
          break;
        }
      case STACK_TRACE:
        dump_stack_trace(pid);
        break;
      case QUIT:
        {
          printf("quitting zdb");
          exit(0);
        }
      default:
        fprintf(stderr, "invalid or unknown command\n");
    }

  }
}
Ejemplo n.º 12
0
int test(betree<uint64_t, std::string> &b,
	 uint64_t nops,
	 uint64_t number_of_distinct_keys,
	 FILE *script_input,
	 FILE *script_output)
{
  std::map<uint64_t, std::string> reference;

  for (unsigned int i = 0; i < nops; i++) {
    int op;
    uint64_t t;
    if (script_input) {
      int r = next_command(script_input, &op, &t);
      if (r == EOF)
	exit(0);
      else if (r < 0)
	exit(4);
    } else {
      op = rand() % 7;
      t = rand() % number_of_distinct_keys;
    }
    
    switch (op) {
    case 0: // insert
      if (script_output)
	fprintf(script_output, "Inserting %lu\n", t);
      b.insert(t, std::to_string(t) + ":");
      reference[t] = std::to_string(t) + ":";
      break;
    case 1: // update
      if (script_output)
	fprintf(script_output, "Updating %lu\n", t);
      b.update(t, std::to_string(t) + ":");
      if (reference.count(t) > 0)
      	reference[t] += std::to_string(t) + ":";
      else
      	reference[t] = std::to_string(t) + ":";
      break;
    case 2: // delete
      if (script_output)
	fprintf(script_output, "Deleting %lu\n", t);
      b.erase(t);
      reference.erase(t);
      break;
    case 3: // query
      try {
	std::string bval = b.query(t);
	assert(reference.count(t) > 0);
	std::string rval = reference[t];
	assert(bval == rval);
	if (script_output)
	  fprintf(script_output, "Query %lu -> %s\n", t, bval.c_str());
      } catch (std::out_of_range e) {
	if (script_output)
	  fprintf(script_output, "Query %lu -> DNE\n", t);
	assert(reference.count(t) == 0);
      }
      break;
    case 4: // full scan
      {
	if (script_output)
	  fprintf(script_output, "Full_scan 0\n");
	auto betit = b.begin();
	auto refit = reference.begin();
	do_scan(betit, refit, b, reference);
      }
      break;
    case 5: // lower-bound scan
      {
	if (script_output)
	  fprintf(script_output, "Lower_bound_scan %lu\n", t);
	auto betit = b.lower_bound(t);
	auto refit = reference.lower_bound(t);
	do_scan(betit, refit, b, reference);
      }
      break;
    case 6: // scan
      {
	if (script_output)
	  fprintf(script_output, "Upper_bound_scan %lu\n", t);
	auto betit = b.upper_bound(t);
	auto refit = reference.upper_bound(t);
	do_scan(betit, refit, b, reference);
      }
      break;
    default:
      abort();
    }
  }

  std::cout << "Test PASSED" << std::endl;
  
  return 0;
}