Exemple #1
0
void register_info_chars(char *name, char *value)
{
	if (value == NULL)
	{
		append_to_string(&log_function_line, " %s=(null)", name);
	}
	else
	{
		append_to_string(&log_function_line, " %s=%s", name, value);
	}
}
Exemple #2
0
int
capture_output_to_menu(FileView *view, const char cmd[], menu_info *m)
{
	FILE *file, *err;
	char *line = NULL;
	int x;
	pid_t pid;

	LOG_INFO_MSG("Capturing output of the command to a menu: %s", cmd);

	pid = background_and_capture((char *)cmd, &file, &err);
	if(pid == (pid_t)-1)
	{
		show_error_msgf("Trouble running command", "Unable to run: %s", cmd);
		return 0;
	}

	show_progress("", 0);

	ui_cancellation_reset();
	ui_cancellation_enable();

	wait_for_data_from(pid, file, 0);

	x = 0;
	while((line = read_line(file, line)) != NULL)
	{
		char *expanded_line;
		show_progress("Loading menu", 1000);
		m->items = realloc(m->items, sizeof(char *)*(x + 1));
		expanded_line = expand_tabulation_a(line, cfg.tab_stop);
		if(expanded_line != NULL)
		{
			m->items[x++] = expanded_line;
		}

		wait_for_data_from(pid, file, 0);
	}
	m->len = x;

	ui_cancellation_disable();

	fclose(file);
	print_errors(err);

	if(ui_cancellation_requested())
	{
		append_to_string(&m->title, "(cancelled) ");
		append_to_string(&m->empty_msg, " (cancelled)");
	}

	return display_menu(m, view);
}
Exemple #3
0
static gchar *
help_xml_doc_to_string_command (xmlNodePtr node, gint width, gboolean color_term)
{
	if (!strcmp ((gchar*) node->name, "command"))
		return help_xml_doc_to_string_single_command (node, width, color_term);
	else {
		GString *string;
		string = g_string_new ("");
		for (node = node->children; node; node = node->next) {
			if (!strcmp ((gchar*) node->name, "command")) {
				gchar *tmp;
				tmp = help_xml_doc_to_string_single_command (node, width, color_term);
				g_string_append (string, tmp);
				g_free (tmp);
			}
			else if (!strcmp ((gchar*) node->name, "section")) {
				xmlChar *section_name;
				section_name = xmlGetProp (node, BAD_CAST "name");
				if (width > 0) {
					gint i, nb, remain;
					nb = (width - g_utf8_strlen ((gchar*) section_name, -1) - 2) / 2;
					remain = width - (2 * nb + 2 + g_utf8_strlen ((gchar*) section_name, -1));
					for (i = 0; i < nb; i++)
						g_string_append_c (string, '=');
					g_string_append_c (string, ' ');
					append_to_string (string, (gchar*) section_name, width, 0);
					g_string_append_c (string, ' ');
					for (i = 0; i < nb + remain; i++)
						g_string_append_c (string, '=');
					g_string_append_c (string, '\n');
				}
				else {
					g_string_append (string, "=== ");
					append_to_string (string, (gchar*) section_name, width, 0);
					g_string_append (string, " ===\n");
				}

				xmlNodePtr snode;
				for (snode = node->children; snode; snode = snode->next) {
					if (!strcmp ((gchar*) snode->name, "command")) {
						gchar *tmp;
						tmp = help_xml_doc_to_string_single_command (snode, width, color_term);
						g_string_append (string, tmp);
						g_free (tmp);
					}
					if (snode->next)
						g_string_append_c (string, '\n');
				}
			}
		}
		return g_string_free (string, FALSE);
	}
}
Exemple #4
0
void register_info_end()
{
	int current_line_size;

	current_line_size = strlen(log_function_line);	
	if (log_function_line[current_line_size-1] == '[') 
	{	// no arguments, remove the " with ["
		log_function_line[current_line_size-7] = '\0';
	}
	else
	{
		append_to_string(&log_function_line, " ]");
	}

	current_line_size = strlen(log_function_line);	

	// if verbose_level greater than 0 the function calls are not only
	// registered in the log file but also displayed 
	// in the standard output of the program
	if (verbose_level > 0)
	{
		colored_print_if_tty(GREEN, "%s", ONLINE_LOG_PREFIX);
		colored_print_if_tty(BLUE, " %s", log_function_line);
		PRINTF("\n"); 
		fflush(tty_fd);
	}

	if ((index_last_line != -1) && (memcmp(log_file_content + index_last_line, log_function_line, current_line_size) == 0))
	{	
		// The current line is the same as the previous one: we remove it and append "repeated several times" instead
		log_file_content[index_last_line + current_line_size] = '\0';
		append_to_string(&log_file_content, " repeated several times\n");
	}
	else
	{	// Otherwise we pass to the next one
		index_last_line = 0;
		if (log_file_content != NULL)
		{
			index_last_line = strlen(log_file_content);
		}
		append_to_string(&log_file_content, "%s\n", log_function_line);
	}

	log_if_needed();

	free(log_function_line);
	log_function_line = NULL;
}
Exemple #5
0
	std::string Decimal::to_string(int p) const {
		std::string ret;

		if (is_neg && !this->is_zero()) {
			ret = "-";
		}

		append_to_string(ret, this->integer);

		ret.append(1, '.');

		for (int i = 0; i < len; i++) {
			// append data[i] as "%09d"
			int x = mo / 10;
			int tmp = data[i];
			while (x) {
				ret.append(1, 48 + tmp / x);
				tmp %= x;
				x /= 10;
				if (--p == 0) {
					break;
				}
			}
			if (p == 0) break;
		}

		if (p > 0) {
			ret.append(p, '0');
		}

		return ret;
	}
Exemple #6
0
        static void append_to_container(T_ const& val, Attribute& attr, mpl::false_)
        {
            typedef typename char_type_of<T_>::type char_type;

            append_to_string(attr, traits::get_begin<char_type>(val)
              , traits::get_end<char_type>(val));
        }
char *rson_serialize_list(rson_list *list) {
	int i;
	size_t serial_list_len, serial_part_len;
	char *serial_list, *serial_part, *list_size;

	list_size = int_to_string(list->size);

	// rson list begins with: "L<number of list items>:"
	serial_list = allocate_string(strlen(list_size) + 2);
	sprintf(serial_list, "L%s:", list_size);

	for (i = 0; i < list->size; i++) {
		serial_part = rson_serialize_type(list->objects[i]);

		serial_part_len = strlen(serial_part);
		serial_list_len = strlen(serial_list);
		if (serial_part_len >= SIZE_MAX - serial_list_len) {
			print_error("Unsigned integer overflow");
		}
		serial_list = append_to_string(serial_list, serial_part);	
	}

	free(list_size);
	return serial_list;
}
Exemple #8
0
        static type call(Attribute const& attr, Context&, mpl::false_, mpl::true_)
        {
            typedef typename char_type_of<Attribute>::type char_type;

            Exposed result;
            append_to_string(result, traits::get_begin<char_type>(attr)
              , traits::get_end<char_type>(attr));
            return result;
        }
Exemple #9
0
int
capture_output_to_menu(FileView *view, const char cmd[], int user_sh,
		menu_state_t *m)
{
	if(process_cmd_output("Loading menu", cmd, user_sh, 0, &output_handler,
				m->d) != 0)
	{
		show_error_msgf("Trouble running command", "Unable to run: %s", cmd);
		return 0;
	}

	if(ui_cancellation_requested())
	{
		append_to_string(&m->d->title, "(cancelled)");
		append_to_string(&m->d->empty_msg, " (cancelled)");
	}

	return display_menu(m, view);
}
Exemple #10
0
int register_info_start(char *function_name)
{
	int depth, result;
	result = -1;

	if (one_time_library_init() == 0)
	{
		if (function_depth <= max_function_depth_reported)
		{
			for (depth=0; depth<function_depth; depth++)
			{
				append_to_string(&log_function_line, "  ");
			}

			append_to_string(&log_function_line, function_name);
			append_to_string(&log_function_line, "() with [");

			result = 0;
		} 
	} 

	return result;
}
int get_result_of_command(char **storage_string, char *command, char *description)
{
	FILE *fp;
	char c[2];
	int result;

	// we temporarily unset LD_PRELOAD (we don't want the 
	// subprocess created by popen to be monitored by ipv6-care)
	unset_ld_preload();

	c[1] = '\0';

	// let's run the command
	fp = popen(command, "r");
	if (fp == NULL)
	{
		// provide diagnosis in case of problems
		write_failure_diagnosis(description, "running the popen() function failed");
		result = -1;
	}
	else
	{
		// let's read its output
		while (!feof(fp))
		{
			fread(c, 1, 1, fp);
			if ((c[0] != '\n') && (c[0] != '\r'))
			{
				append_to_string(storage_string, c);
			}
		}
		result = 0;
	}
	pclose(fp);

	// we restore LD_PRELOAD
	set_ld_preload();

	return result;
}
Exemple #12
0
void register_info_int(char *name, int value)
{
	append_to_string(&log_function_line, " %s=%d", name, value);
}
Exemple #13
0
void init_lib()
{
	FILE *fp;
	char c[2];
	char *path_of_program_alloc = NULL;
	char *substring, *char_pointer, *underscore_var;

	save_tty_fd();

	program_command_line = NULL;

	// let's get the full command line of the program for future reference
	c[1] = '\0';
	fp = fopen("/proc/self/cmdline", "r");
	if (fp != NULL)
	{	// could read /proc/self/cmdline successfully
		while (!feof(fp))
		{
			fread(c, 1, 1, fp);
			if (c[0] == '\0')
			{
				c[0] = ' ';
			}
			append_to_string(&program_command_line, c);
		}
		fclose(fp);
		program_command_line[strlen(program_command_line)-2] = '\0'; // it ends with two '\0' converted to spaces

	}
	else
	{	// could not read /proc/self/cmdline (some systems have no /proc interface)
		// let's try to get the full command line by other means
		if (get_result_of_command(&program_command_line, "ps -p `ps -p $$ -o ppid=` -o args=", 
					"retrieve the full command line of this program") != 0)
		{	// could not get the full command line
			program_command_line = NULL;
		}
	}

	underscore_var = getenv("_");
	if (program_command_line != NULL)
	{
		// and the program basename
		// Normally it is the first word of the process command line
		// but in the case of a script, the command line would be for example
		// /usr/bin/python my_script.py
		// so in this case we try to get 'my_script.py' instead of 'python' in order to be more
		// explicit about the program being run.
		substring = NULL;
		if (underscore_var != NULL)
		{
			substring = strstr(program_command_line, underscore_var);
		}
		if (substring == NULL)
		{	// we point to the first word
			asprintf(&path_of_program_alloc, "%s", program_command_line);
			
			// we seperate this first word by putting a '\0' after it
			char_pointer = path_of_program_alloc;
			while(*char_pointer != '\0')
			{
				if (isspace(*char_pointer))
				{
					*char_pointer = '\0';
					break;
				}
				char_pointer++;
			}
		}
		else
		{
			asprintf(&path_of_program_alloc, "%s", underscore_var);
		}

		asprintf(&program_basename, "%s", basename(path_of_program_alloc));
		free(path_of_program_alloc);
	}
	else
	{	// we could not get the full command line
		// we will just retrieve the name of the program
		// and also copy it as the full command line  
		if (underscore_var != NULL)
		{
			asprintf(&path_of_program_alloc, "%s", underscore_var);
		}
		else
		{
			asprintf(&path_of_program_alloc, "%s", UNKNOWN_PROGRAM);
		}
		asprintf(&program_basename, "%s", basename(path_of_program_alloc));
		free(path_of_program_alloc);
		asprintf(&program_command_line, "%s", program_basename);
	}

	// let's save the interpreter name into the variable interpreter_name
	save_interpreter_name();
	
	// let's check if this program is written in an interpreted language
	// (= if the interpreter is known)
	if (	(strcmp(interpreter_name, "python") == 0) ||
		(strcmp(interpreter_name, "perl") == 0) ||
		(strcmp(interpreter_name, "java") == 0))
	{
		interpreted_language = 1;
	}
	else
	{
		interpreted_language = 0;
	}
	
	// Environment variables
	if (getenv("IPV6_MAX_FUNCTION_DEPTH") != NULL)
	{
		sscanf(getenv("IPV6_MAX_FUNCTION_DEPTH"), "%d", &max_function_depth_reported);
	}

	if (getenv("IPV6_VERBOSE_LEVEL") != NULL)
	{
		sscanf(getenv("IPV6_VERBOSE_LEVEL"), "%d", &verbose_level);
	}
}
Exemple #14
0
/*
 * @node must be a <command> tag
 */
static gchar *
help_xml_doc_to_string_single_command (xmlNodePtr node, gint width, gboolean color_term)
{
	g_assert (!strcmp ((gchar*) node->name, "command"));

	/* create output string */
	GString *string;
	string = g_string_new ("");
	for (node = node->children; node; node = node->next) {
		xmlChar *data = NULL;
		if (!strcmp ((gchar*) node->name, "shortdescription")) {
			data = xmlNodeGetContent (node);
			if (data) {
				append_to_string (string, (gchar*) data, width, 0);
				g_string_append (string, "\n");
			}
		}
		else if (!strcmp ((gchar*) node->name, "usage") || !strcmp ((gchar*) node->name, "example")) {
			if (!strcmp ((gchar*) node->name, "usage"))
				append_to_string (string, _("Usage"), width, 0);
			else
				append_to_string (string, _("Example"), width, 0);
			g_string_append (string, ":\n");
			xmlNodePtr snode;
			for (snode = node->children; snode; snode = snode->next) {
				if (!strcmp ((gchar*) snode->name, "synopsis")) {
					data = xmlNodeGetContent (snode);
					if (data) {
						append_to_string (string, "> ", width, 3);
						gchar *tmp;
						tmp = g_strdup_printf ("%s%s%s",
								       base_tool_output_color_s (BASE_TOOL_COLOR_BOLD, color_term),
								       data,
								       base_tool_output_color_s (BASE_TOOL_COLOR_RESET, color_term));
						append_to_string (string, tmp, width, 3);
						g_free (tmp);
						g_string_append_c (string, '\n');
					}
				}
				else if (!strcmp ((gchar*) snode->name, "comment")) {
					data = xmlNodeGetContent (snode);
					if (data) {
						append_to_string (string, (gchar*) data, width, 6);
						g_string_append_c (string, '\n');
					}
				}
				else if (!strcmp ((gchar*) snode->name, "raw")) {
					data = xmlNodeGetContent (snode);
					if (data) {
						append_raw_to_string (string, (gchar*) data, width, 6);
						g_string_append (string, "\n\n");
					}
				}
				if (data)
					xmlFree (data);
				data = NULL;
			}
		}
		if (data)
			xmlFree (data);
	}

	return g_string_free (string, FALSE);
}
Exemple #15
0
 void append_to_string(std::string& s, const char prefix, const char infix, const char suffix, int precision) const {
     s += prefix;
     append_to_string(s, infix, precision);
     s += suffix;
 }