Example #1
0
void scrollback_load(session *sess)
{
	int fh;
	char buf[512 * 4];
	char *text;
	time_t stamp;
	int lines;

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if (scrollback_get_filename(sess, buf, sizeof(buf)) == nullptr)
		return;

	fh = open(buf, O_RDONLY | OFLAGS);
	if (fh == -1)
		return;

	
	lines = 0;
	while (waitline(fh, buf, sizeof buf, FALSE) != -1)
	{
		if (buf[0] == 'T')
		{
			if (sizeof(time_t) == 4)
				stamp = strtoul(buf + 2, nullptr, 10);
			else
				stamp = strtoull(buf + 2, nullptr, 10); // just incase time_t is 64 bits
			text = strchr(buf + 3, ' ');
			if (text)
			{
				text = strip_color(text + 1, -1, STRIP_COLOR);
				fe_print_text(sess, text, stamp);
				g_free(text);
			}
			lines++;
		}
	}

	sess->scrollwritten = lines;

	if (lines)
	{
		text = ctime(&stamp);
		text[24] = 0;	// get rid of the \n
		snprintf(buf, sizeof(buf), "\n*\t%s %s\n\n", _("Loaded log from"), text);
		fe_print_text(sess, buf, 0);
		//EMIT_SIGNAL(XP_TE_GENMSG, sess, "*", buf, nullptr, nullptr, nullptr, 0);
	}

	close(fh);
}
Example #2
0
File: text.c Project: sb057/hexchat
static void
scrollback_shrink (session *sess)
{
	char file[1024];
	char *buf;
	int fh;
	int lines;
	int line;
	int len;
	char *p;

	scrollback_close (sess);
	sess->scrollwritten = 0;
	lines = 0;

	if (scrollback_get_filename (sess, file, sizeof (file)) == NULL)
		return;

	buf = file_to_buffer (file, &len);
	if (!buf)
		return;

	/* count all lines */
	p = buf;
	while (p != buf + len)
	{
		if (*p == '\n')
			lines++;
		p++;
	}

	fh = open (file, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644);
	if (fh == -1)
	{
		free (buf);
		return;
	}

	line = 0;
	p = buf;
	while (p != buf + len)
	{
		if (*p == '\n')
		{
			line++;
			if (line >= lines - prefs.max_lines &&
				 p + 1 != buf + len)
			{
				p++;
				write (fh, p, len - (p - buf));
				break;
			}
		}
		p++;
	}

	close (fh);
	free (buf);
}
Example #3
0
static void scrollback_unlock(session *sess)
{
	char buf[1024];

	if (scrollback_get_filename(sess, buf, sizeof(buf) - 6) == nullptr)
		return;

	strcat(buf, ".lock");
	unlink(buf);
}
Example #4
0
static void
scrollback_save (session *sess, char *text)
{
	char *buf;
	time_t stamp;
	int len;

	if (sess->type == SESS_SERVER && prefs.hex_gui_tab_server == 1)
		return;

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.hex_text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if (sess->scrollfd == -1)
	{
		if ((buf = scrollback_get_filename (sess)) == NULL)
			return;

		sess->scrollfd = g_open (buf, O_CREAT | O_APPEND | O_WRONLY, 0644);
		g_free (buf);
		if (sess->scrollfd == -1)
			return;
	}

	stamp = time (0);
	if (sizeof (stamp) == 4)	/* gcc will optimize one of these out */
		buf = g_strdup_printf ("T %d ", (int) stamp);
	else
		buf = g_strdup_printf ("T %" G_GINT64_FORMAT " ", (gint64)stamp);
	write (sess->scrollfd, buf, strlen (buf));
	g_free (buf);

	len = strlen (text);
	write (sess->scrollfd, text, len);
	if (len && text[len - 1] != '\n')
		write (sess->scrollfd, "\n", 1);

	sess->scrollwritten++;

	if ((sess->scrollwritten * 2 > prefs.hex_text_max_lines && prefs.hex_text_max_lines > 0) ||
       sess->scrollwritten > 32000)
		scrollback_shrink (sess);
}
Example #5
0
static void scrollback_save(session *sess, char *text)
{
	char buf[512 * 4];
	time_t stamp;
	int len;

	if (sess->type == SESS_SERVER)
		return;

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if (sess->scrollfd == -1)
	{
		if (scrollback_get_filename(sess, buf, sizeof(buf)) == nullptr)
			return;

		sess->scrollfd = open(buf, O_CREAT | O_APPEND | O_WRONLY, 0644);
		if (sess->scrollfd == -1)
			return;
	}

	stamp = time(0);
	if (sizeof(stamp) == 4)	// gcc will optimize one of these out
		write(sess->scrollfd, buf, snprintf(buf, sizeof(buf), "T %d ", (int)stamp));
	else
		write(sess->scrollfd, buf, snprintf(buf, sizeof(buf), "T %""I64i"/*G_GINT64_FORMAT*/" ", (int)stamp));

	len = strlen(text);
	write(sess->scrollfd, text, len);
	if (len && text[len - 1] != '\n')
		write(sess->scrollfd, "\n", 1);

	sess->scrollwritten++;

	if ((sess->scrollwritten * 2 > prefs.max_lines && prefs.max_lines > 0) ||
		sess->scrollwritten > 32000)
		scrollback_shrink(sess);
}
Example #6
0
static bool scrollback_lock(session *sess)
{
	char buf[1024];
	int fh;

	if (scrollback_get_filename(sess, buf, sizeof(buf) - 6) == nullptr)
		return FALSE;

	strcat(buf, ".lock");

	if (access(buf, F_OK) == 0)
		return FALSE;	// can't get lock

	fh = open(buf, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644);
	if (fh == -1)
		return FALSE;

	return TRUE;
}
Example #7
0
void
scrollback_load (session *sess)
{
	char *buf;
	char *text;
	time_t stamp;
	int lines;
	GIOChannel *io;
	GError *file_error = NULL;
	GError *io_err = NULL;

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.hex_text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if ((buf = scrollback_get_filename (sess)) == NULL)
		return;

	io = g_io_channel_new_file (buf, "r", &file_error);
	g_free (buf);
	if (!io)
		return;

	lines = 0;

	while (1)
	{
		gsize n_bytes;
		GIOStatus io_status;

		io_status = g_io_channel_read_line (io, &buf, &n_bytes, NULL, &io_err);
		
		if (io_status == G_IO_STATUS_NORMAL)
		{
			char *buf_tmp;

			/* If nothing but funny trailing matter e.g. 0x0d or 0x0d0a, toss it */
			if (n_bytes >= 1 && buf[0] == 0x0d)
			{
				g_free (buf);
				continue;
			}

			n_bytes--;
			buf_tmp = buf;
			buf = g_strndup (buf_tmp, n_bytes);
			g_free (buf_tmp);

			/*
			 * Some scrollback lines have three blanks after the timestamp and a newline
			 * Some have only one blank and a newline
			 * Some don't even have a timestamp
			 * Some don't have any text at all
			 */
			if (buf[0] == 'T')
			{
				if (sizeof (time_t) == 4)
					stamp = strtoul (buf + 2, NULL, 10);
				else
					stamp = strtoull (buf + 2, NULL, 10); /* in case time_t is 64 bits */
				text = strchr (buf + 3, ' ');
				if (text && text[1])
				{
					if (prefs.hex_text_stripcolor_replay)
					{
						text = strip_color (text + 1, -1, STRIP_COLOR);
					}

					fe_print_text (sess, text, stamp, TRUE);

					if (prefs.hex_text_stripcolor_replay)
					{
						g_free (text);
					}
				}
				else
				{
					fe_print_text (sess, "  ", stamp, TRUE);
				}
			}
			else
			{
				if (strlen (buf))
					fe_print_text (sess, buf, 0, TRUE);
				else
					fe_print_text (sess, "  ", 0, TRUE);
			}
			lines++;

			g_free (buf);
		}

		else
			break;
	}

	g_io_channel_unref (io);

	sess->scrollwritten = lines;

	if (lines)
	{
		text = ctime (&stamp);
		text[24] = 0;	/* get rid of the \n */
		buf = g_strdup_printf ("\n*\t%s %s\n\n", _("Loaded log from"), text);
		fe_print_text (sess, buf, 0, TRUE);
		g_free (buf);
		/*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf);*/
	}
}
Example #8
0
static void
scrollback_shrink (session *sess)
{
	char *file;
	char *buf;
	int fh;
	int lines;
	int line;
	gsize len;
	char *p;

	scrollback_close (sess);
	sess->scrollwritten = 0;
	lines = 0;

	if ((file = scrollback_get_filename (sess)) == NULL)
	{
		g_free (file);
		return;
	}

	if (!g_file_get_contents (file, &buf, &len, NULL))
	{
		g_free (file);
		return;
	}

	/* count all lines */
	p = buf;
	while (p != buf + len)
	{
		if (*p == '\n')
			lines++;
		p++;
	}

	fh = g_open (file, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644);
	g_free (file);
	if (fh == -1)
	{
		free (buf);
		return;
	}

	line = 0;
	p = buf;
	while (p != buf + len)
	{
		if (*p == '\n')
		{
			line++;
			if (line >= lines - prefs.hex_text_max_lines &&
				 p + 1 != buf + len)
			{
				p++;
				write (fh, p, len - (p - buf));
				break;
			}
		}
		p++;
	}

	close (fh);
	free (buf);
}
Example #9
0
void
scrollback_load (session *sess)
{
	char *buf;
	char *text;
	time_t stamp;
	int lines;
	GIOChannel *io;
	GError *file_error = NULL;
	GError *io_err = NULL;

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.pchat_text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if ((buf = scrollback_get_filename (sess)) == NULL)
		return;

	io = g_io_channel_new_file (buf, "r", &file_error);
	g_free (buf);
	if (!io)
		return;

	lines = 0;

	while (1)
	{
		gsize n_bytes;
		GIOStatus io_status;

		io_status = g_io_channel_read_line (io, &buf, &n_bytes, NULL, &io_err);

		if (io_status == G_IO_STATUS_NORMAL)
		{
			char *buf_tmp;

			n_bytes--;
			buf_tmp = buf;
			buf = g_strndup (buf_tmp, n_bytes);
			g_free (buf_tmp);

			if (buf[0] == 'T')
			{
				if (sizeof (time_t) == 4)
					stamp = g_ascii_strtoul (buf + 2, NULL, 10);
				else
					stamp = g_ascii_strtoull (buf + 2, NULL, 10); /* in case time_t is 64 bits */
				text = strchr (buf + 3, ' ');
				if (text)
				{
					if (prefs.pchat_text_stripcolor_replay)
					{
						text = strip_color (text + 1, -1, STRIP_COLOR);
					}

					fe_print_text (sess, text, stamp, TRUE);

					if (prefs.pchat_text_stripcolor_replay)
					{
						g_free (text);
					}
				}
				lines++;
			}

			g_free (buf);
		}

		else
			break;
	}

	g_io_channel_unref (io);

	sess->scrollwritten = lines;

	if (lines)
	{
		text = ctime (&stamp);
		text[24] = 0;	/* get rid of the \n */
		buf = g_strdup_printf ("\n*\t%s %s\n\n", _("Loaded log from"), text);
		fe_print_text (sess, buf, 0, TRUE);
		g_free (buf);
		/*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf, NULL, NULL, NULL, 0);*/
	}
}
Example #10
0
void
scrollback_load (session *sess)
{
	GInputStream *stream;
	GDataInputStream *istream;
	gchar *buf, *text;
	gint lines = 0;
	time_t stamp = 0;

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.hex_text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if (!sess->scrollfile)
	{
		if ((buf = scrollback_get_filename (sess)) == NULL)
			return;

		sess->scrollfile = g_file_new_for_path (buf);
		g_free (buf);
	}

	stream = G_INPUT_STREAM(g_file_read (sess->scrollfile, NULL, NULL));
	if (!stream)
		return;

	istream = g_data_input_stream_new (stream);
	/*
	 * This is to avoid any issues moving between windows/unix
	 * but the docs mention an invalid \r without a following \n
	 * can lock up the program... (Our write() always adds \n)
	 */
	g_data_input_stream_set_newline_type (istream, G_DATA_STREAM_NEWLINE_TYPE_ANY);
	g_object_unref (stream);

	while (1)
	{
		GError *err = NULL;
		gsize n_bytes;

		buf = g_data_input_stream_read_line_utf8 (istream, &n_bytes, NULL, &err);

		if (!err && buf)
		{
			/*
			 * Some scrollback lines have three blanks after the timestamp and a newline
			 * Some have only one blank and a newline
			 * Some don't even have a timestamp
			 * Some don't have any text at all
			 */
			if (buf[0] == 'T' && buf[1] == ' ')
			{
				if (sizeof (time_t) == 4)
					stamp = strtoul (buf + 2, NULL, 10);
				else
					stamp = g_ascii_strtoull (buf + 2, NULL, 10); /* in case time_t is 64 bits */

				if (G_UNLIKELY(stamp == 0))
				{
					g_warning ("Invalid timestamp in scrollback file");
					continue;
				}

				text = strchr (buf + 3, ' ');
				if (text && text[1])
				{
					if (prefs.hex_text_stripcolor_replay)
					{
						text = strip_color (text + 1, -1, STRIP_COLOR);
					}

					fe_print_text (sess, text, stamp, TRUE);

					if (prefs.hex_text_stripcolor_replay)
					{
						g_free (text);
					}
				}
				else
				{
					fe_print_text (sess, "  ", stamp, TRUE);
				}
			}
			else
			{
				if (strlen (buf))
					fe_print_text (sess, buf, 0, TRUE);
				else
					fe_print_text (sess, "  ", 0, TRUE);
			}
			lines++;

			g_free (buf);
		}
		else if (err)
		{
			/* If its only an encoding error it may be specific to the line */
			if (g_error_matches (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
			{
				g_warning ("Invalid utf8 in scrollback file");
				g_clear_error (&err);
				continue;
			}

			/* For general errors just give up */
			g_clear_error (&err);
			break;
		}
		else /* No new line */
		{
			break;
		}
	}

	g_object_unref (istream);

	sess->scrollwritten = lines;

	if (lines)
	{
		text = ctime (&stamp);
		buf = g_strdup_printf ("\n*\t%s %s\n", _("Loaded log from"), text);
		fe_print_text (sess, buf, 0, TRUE);
		g_free (buf);
		/*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf, NULL, NULL, NULL, 0);*/
	}
}
Example #11
0
static void
scrollback_save (session *sess, char *text, time_t stamp)
{
	GOutputStream *ostream;
	char *buf;

	if (sess->type == SESS_SERVER && prefs.hex_gui_tab_server == 1)
		return;

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.hex_text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if (!sess->scrollfile)
	{
		if ((buf = scrollback_get_filename (sess)) == NULL)
			return;

		sess->scrollfile = g_file_new_for_path (buf);
		g_free (buf);
	}
	else
	{
		/* Users can delete the folder after it's created... */
		GFile *parent = g_file_get_parent (sess->scrollfile);
		g_file_make_directory_with_parents (parent, NULL, NULL);
		g_object_unref (parent);
	}

	ostream = G_OUTPUT_STREAM(g_file_append_to (sess->scrollfile, G_FILE_CREATE_PRIVATE, NULL, NULL));
	if (!ostream)
		return;

	if (!stamp)
		stamp = time(0);
	if (sizeof (stamp) == 4)	/* gcc will optimize one of these out */
		buf = g_strdup_printf ("T %d ", (int) stamp);
	else
		buf = g_strdup_printf ("T %" G_GINT64_FORMAT " ", (gint64)stamp);

	g_output_stream_write (ostream, buf, strlen (buf), NULL, NULL);
	g_output_stream_write (ostream, text, strlen (text), NULL, NULL);
	if (!g_str_has_suffix (text, "\n"))
		g_output_stream_write (ostream, "\n", 1, NULL, NULL);

	g_free (buf);
	g_object_unref (ostream);

	sess->scrollwritten++;

	if ((sess->scrollwritten > prefs.hex_text_max_lines && prefs.hex_text_max_lines > 0) ||
       sess->scrollwritten > SCROLLBACK_MAX)
		scrollback_shrink (sess);
}
Example #12
0
File: text.c Project: sb057/hexchat
void
scrollback_load (session *sess)
{
	int fh;
	char buf[512 * 4];
	char *text;
	time_t stamp;
	int lines;

#ifdef WIN32
#if 0
	char *cleaned_text;
	int cleaned_len;
#endif
#else
	char *map, *end_map;
	struct stat statbuf;
	const char *begin, *eol;
#endif

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if (scrollback_get_filename (sess, buf, sizeof (buf)) == NULL)
		return;

	fh = open (buf, O_RDONLY | OFLAGS);
	if (fh == -1)
		return;

#ifndef WIN32
	if (fstat (fh, &statbuf) < 0)
		return;

	map = mmap (NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fh, 0);
	if (map == MAP_FAILED)
		return;

	end_map = map + statbuf.st_size;

	lines = 0;
	begin = map;
	while (begin < end_map)
	{
		int n_bytes;

		eol = memchr (begin, '\n', end_map - begin);

		if (!eol)
			eol = end_map;

		n_bytes = MIN (eol - begin, sizeof (buf) - 1);

		strncpy (buf, begin, n_bytes);

		buf[n_bytes] = 0;

		if (buf[0] == 'T')
		{
			if (sizeof (time_t) == 4)
				stamp = strtoul (buf + 2, NULL, 10);
			else
				stamp = strtoull (buf + 2, NULL, 10); /* just incase time_t is 64 bits */
			text = strchr (buf + 3, ' ');
			if (text)
			{
				if (prefs.text_stripcolor_replay)
				{
					text = strip_color (text + 1, -1, STRIP_COLOR);
				}
				fe_print_text (sess, text, stamp);
				if (prefs.text_stripcolor_replay)
				{
					g_free (text);
				}
			}
			lines++;
		}

		begin = eol + 1;
	}

	sess->scrollwritten = lines;

	if (lines)
	{
		text = ctime (&stamp);
		text[24] = 0;	/* get rid of the \n */
		snprintf (buf, sizeof (buf), "\n*\t%s %s\n\n", _("Loaded log from"), text);
		fe_print_text (sess, buf, 0);
		/*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf, NULL, NULL, NULL, 0);*/
	}

	munmap (map, statbuf.st_size);
#else
	lines = 0;
	while (waitline (fh, buf, sizeof buf, FALSE) != -1)
	{
		if (buf[0] == 'T')
		{
			if (sizeof (time_t) == 4)
				stamp = strtoul (buf + 2, NULL, 10);
			else
				stamp = strtoull (buf + 2, NULL, 10); /* just incase time_t is 64 bits */
			text = strchr (buf + 3, ' ');
			if (text)
			{
				if (prefs.text_stripcolor_replay)
				{
					text = strip_color (text + 1, -1, STRIP_COLOR);
				}
#if 0
				cleaned_text = text_replace_non_bmp (text, -1, &cleaned_len);
				if (cleaned_text != NULL)
				{
					if (prefs.text_stripcolor_replay)
					{
						g_free (text);
					}
					text = cleaned_text;
				}
#endif
				text_replace_non_bmp2 (text);
				fe_print_text (sess, text, stamp);
				if (prefs.text_stripcolor_replay)
				{
					g_free (text);
				}
			}
			lines++;
		}
	}

	sess->scrollwritten = lines;

	if (lines)
	{
		text = ctime (&stamp);
		text[24] = 0;	/* get rid of the \n */
		snprintf (buf, sizeof (buf), "\n*\t%s %s\n\n", _("Loaded log from"), text);
		fe_print_text (sess, buf, 0);
		/*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf, NULL, NULL, NULL, 0);*/
	}
#endif

	close (fh);
}