Exemplo n.º 1
0
std::string
word_wrap(
  const std::string &	s,
  unsigned int		line_length,
  const std::string &	prefix,
  const std::string &	prefix_first_line)
{
  std::string t;
  const std::string *u = &prefix_first_line;

  std::string::const_iterator p0, p1, p2, p3;
  p0 = p1 = p2 = s.begin();

  while (p2 != s.end() ) {

    // skip preceeding whitespace
    p1 = find_next_nonspace(p0, s.end());
    p3 = find_next_endl(p0, s.end());
    p2 = p1 = find_next_space(p1, s.end());
    do {
      p1 = find_next_nonspace(p1, s.end());
      p1 = find_next_space(p1, s.end());
      if (p3 < p1) {
	p2 = p3;
	break;
      }
      unsigned int diff = p1 - p0;
      if (diff > (line_length - u->size()))
	break;
      p2 = p1;
    } while (p2 != s.end());

    t.append(*u).append(p0, p2).append("\n");

    if (p2 == p3)
      u = &prefix_first_line;
    else
      u = &prefix;

    p0 = p2 + 1;
  }

  return t;
}
Exemplo n.º 2
0
void parse_cmd(char *cmd) {
	cmd = remove_leading_whitespaces(cmd);
	char *tok = find_next_space(cmd); // Find Command
	int status;
	debug_msg(cmd);
	#if USEBUILTINCLEAR
	if (strcmp(cmd, "clear") == 0) {
			clear();
	} else
	#endif
	if (strcmp(cmd, "env") == 0) {
		env();
	} else if (strcmp(cmd, "quit") == 0) {
		exit(EXIT_SUCCESS);
	} else if (strcmp(cmd, "version") == 0) {
		version();
	} else if (strcmp(cmd, "pwd") == 0) {
		pwd();
	}
	#if USEBUILTINLS 
	else if (strcmp(cmd, "ls") == 0) {
		ls();
	} 
	#endif
	else if (strcmp(cmd, "echo") == 0) {
		find_next_space(tok);
		remove_character(tok,'\\');
		echo(tok);
	} else if (strcmp(cmd, "cd") == 0) {
		find_next_space(tok); // Get Path
		remove_character(tok,'\\'); // Remove all '\' in target Path.
		debug_msg(tok);
		cd(tok);
	} else if (strcmp(cmd, "set") == 0) {
		// Get Name of Variable
		char *buffer = strtok(tok,"=");
		// Get new Value of Variable and set.
		set(buffer, strtok(NULL,"="));
	} else if (strcmp(cmd, "\0") == 0) {
		// Do nothing.
	} else {
		// Fork application here
		pid_t pID = fork();
		
		if (pID < 0) {
			// pID == -1: There was an error! Quit shawn!!
			errno_msg("Could not fork.",0);
		} else if (pID > 0) {
			// Parent Process, pID is PID of child.
			
			pID = wait(&status); // Wait for childs completion.
			if (WIFEXITED(status)) { // Has Child ended?
				last_exit_code = WEXITSTATUS(status); // Set our internal $?
				#if DEBUG
					printf("[DEBUG] Child exited with %i\n",last_exit_code);
				#endif
			}
		} else { // PID == 0
			// Child Process.
			
			// We need to fork here.
			unsigned int i = 2; // Prepare a counter
			char *parameters[count_paramters(tok)]; // Create a list for parameters
			char *buffer = find_next_space(tok); // Select first parameter.
			remove_character(tok,'\\');
			parameters[0] = cmd; // Set name of call. See execvp() Documentation
			parameters[1] = tok; // Set first parameter.
		
			// Save every Parameter now for our Call
			while (buffer != NULL) {
				// We need a second pointer here.
				char *placeholder = find_next_space(buffer);
				// remove all \ characters
				while(remove_next_special_character(buffer,'\\') == 1); 
				// Set the Parameter to our newly delimited string
				parameters[i] = buffer;
				// Replace buffer with our placeholder
				buffer = placeholder;
				i++; // Increment i, so we set the next parameter
			}
			parameters[i-1] = NULL; // Remove an empty Parameter. Else This would be a whitespace
			
			// Run Application now.
			int result = execvp(cmd,parameters);
			if (result != 0) {
				errno_msg("Executing Application",1);
			}
			exit(result);
		} 
	} // End of fork() Stuff.
}