mirror of
https://github.com/irssi/irssi.git
synced 2024-09-01 04:14:16 -04:00
Use GIOChannel API to read a file linewise.
git-svn-id: http://svn.irssi.org/repos/irssi/trunk@4736 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
parent
2b0ebef911
commit
a3021ccf5f
@ -20,37 +20,36 @@
|
|||||||
|
|
||||||
#include "module.h"
|
#include "module.h"
|
||||||
#include "signals.h"
|
#include "signals.h"
|
||||||
#include "line-split.h"
|
|
||||||
#include "special-vars.h"
|
#include "special-vars.h"
|
||||||
|
|
||||||
#include "fe-windows.h"
|
#include "fe-windows.h"
|
||||||
|
|
||||||
void autorun_startup(void)
|
void autorun_startup(void)
|
||||||
{
|
{
|
||||||
char tmpbuf[1024], *str, *path;
|
char *path;
|
||||||
LINEBUF_REC *buffer = NULL;
|
GIOChannel *handle;
|
||||||
int f, ret, recvlen;
|
GString *buf;
|
||||||
|
gsize tpos;
|
||||||
|
|
||||||
/* open ~/.irssi/startup and run all commands in it */
|
/* open ~/.irssi/startup and run all commands in it */
|
||||||
path = g_strdup_printf("%s/startup", get_irssi_dir());
|
path = g_strdup_printf("%s/startup", get_irssi_dir());
|
||||||
f = open(path, O_RDONLY);
|
handle = g_io_channel_new_file(path, "r", NULL);
|
||||||
g_free(path);
|
g_free(path);
|
||||||
if (f == -1) {
|
if (handle == NULL) {
|
||||||
/* file not found */
|
/* file not found */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
buf = g_string_sized_new(512);
|
||||||
recvlen = read(f, tmpbuf, sizeof(tmpbuf));
|
while (g_io_channel_read_line_string(handle, buf, &tpos, NULL) == G_IO_STATUS_NORMAL) {
|
||||||
|
buf->str[tpos] = '\0';
|
||||||
ret = line_split(tmpbuf, recvlen, &str, &buffer);
|
if (buf->str[0] != '#') {
|
||||||
if (ret > 0 && *str != '#') {
|
eval_special_string(buf->str, "",
|
||||||
eval_special_string(str, "",
|
|
||||||
active_win->active_server,
|
active_win->active_server,
|
||||||
active_win->active);
|
active_win->active);
|
||||||
}
|
}
|
||||||
} while (ret > 0);
|
}
|
||||||
line_split_free(buffer);
|
g_string_free(buf, TRUE);
|
||||||
|
|
||||||
close(f);
|
g_io_channel_close(handle);
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
#include "levels.h"
|
#include "levels.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "line-split.h"
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "irssi-version.h"
|
#include "irssi-version.h"
|
||||||
#include "servers.h"
|
#include "servers.h"
|
||||||
@ -114,11 +113,12 @@ static void cmd_version(char *data)
|
|||||||
/* SYNTAX: CAT <file> */
|
/* SYNTAX: CAT <file> */
|
||||||
static void cmd_cat(const char *data)
|
static void cmd_cat(const char *data)
|
||||||
{
|
{
|
||||||
LINEBUF_REC *buffer = NULL;
|
|
||||||
char *fname, *fposstr;
|
char *fname, *fposstr;
|
||||||
char tmpbuf[1024], *str;
|
|
||||||
void *free_arg;
|
void *free_arg;
|
||||||
int f, ret, recvlen, fpos;
|
int fpos;
|
||||||
|
GIOChannel *handle;
|
||||||
|
GString *buf;
|
||||||
|
gsize tpos;
|
||||||
|
|
||||||
if (!cmd_get_params(data, &free_arg, 2, &fname, &fposstr))
|
if (!cmd_get_params(data, &free_arg, 2, &fname, &fposstr))
|
||||||
return;
|
return;
|
||||||
@ -127,29 +127,26 @@ static void cmd_cat(const char *data)
|
|||||||
fpos = atoi(fposstr);
|
fpos = atoi(fposstr);
|
||||||
cmd_params_free(free_arg);
|
cmd_params_free(free_arg);
|
||||||
|
|
||||||
f = open(fname, O_RDONLY);
|
handle = g_io_channel_new_file(fname, "r", NULL);
|
||||||
g_free(fname);
|
g_free(fname);
|
||||||
|
|
||||||
if (f == -1) {
|
if (handle == NULL) {
|
||||||
/* file not found */
|
/* file not found */
|
||||||
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
|
printtext(NULL, NULL, MSGLEVEL_CLIENTERROR,
|
||||||
"%s", g_strerror(errno));
|
"%s", g_strerror(errno));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
lseek(f, fpos, SEEK_SET);
|
g_io_channel_seek_position(handle, fpos, G_SEEK_SET, NULL);
|
||||||
do {
|
buf = g_string_sized_new(512);
|
||||||
recvlen = read(f, tmpbuf, sizeof(tmpbuf));
|
while (g_io_channel_read_line_string(handle, buf, &tpos, NULL) == G_IO_STATUS_NORMAL) {
|
||||||
|
buf->str[tpos] = '\0';
|
||||||
ret = line_split(tmpbuf, recvlen, &str, &buffer);
|
|
||||||
if (ret > 0) {
|
|
||||||
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP |
|
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP |
|
||||||
MSGLEVEL_NEVER, "%s", str);
|
MSGLEVEL_NEVER, "%s", buf->str);
|
||||||
}
|
}
|
||||||
} while (ret > 0);
|
g_string_free(buf, TRUE);
|
||||||
line_split_free(buffer);
|
|
||||||
|
|
||||||
close(f);
|
g_io_channel_close(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SYNTAX: BEEP */
|
/* SYNTAX: BEEP */
|
||||||
|
@ -23,7 +23,6 @@
|
|||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
#include "levels.h"
|
#include "levels.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "line-split.h"
|
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
#include "printtext.h"
|
#include "printtext.h"
|
||||||
@ -117,46 +116,42 @@ static void help_category(GSList *cmdlist, int items)
|
|||||||
static int show_help_file(const char *file)
|
static int show_help_file(const char *file)
|
||||||
{
|
{
|
||||||
const char *helppath;
|
const char *helppath;
|
||||||
char tmpbuf[1024], *str, *path, **paths, **tmp;
|
char *path, **paths, **tmp;
|
||||||
LINEBUF_REC *buffer = NULL;
|
GIOChannel *handle;
|
||||||
int f, ret, recvlen;
|
GString *buf;
|
||||||
|
gsize tpos;
|
||||||
|
|
||||||
helppath = settings_get_str("help_path");
|
helppath = settings_get_str("help_path");
|
||||||
|
|
||||||
paths = g_strsplit(helppath, ":", -1);
|
paths = g_strsplit(helppath, ":", -1);
|
||||||
|
|
||||||
f = -1;
|
handle = NULL;
|
||||||
for (tmp = paths; *tmp != NULL; tmp++) {
|
for (tmp = paths; *tmp != NULL; tmp++) {
|
||||||
/* helpdir/command or helpdir/category/command */
|
/* helpdir/command or helpdir/category/command */
|
||||||
path = g_strdup_printf("%s/%s", *tmp, file);
|
path = g_strdup_printf("%s/%s", *tmp, file);
|
||||||
f = open(path, O_RDONLY);
|
handle = g_io_channel_new_file(path, "r", NULL);
|
||||||
g_free(path);
|
g_free(path);
|
||||||
|
|
||||||
if (f != -1)
|
if (handle != NULL)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_strfreev(paths);
|
g_strfreev(paths);
|
||||||
|
|
||||||
if (f == -1)
|
if (handle == NULL)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
|
buf = g_string_sized_new(512);
|
||||||
/* just print to screen whatever is in the file */
|
/* just print to screen whatever is in the file */
|
||||||
do {
|
while (g_io_channel_read_line_string(handle, buf, &tpos, NULL) == G_IO_STATUS_NORMAL) {
|
||||||
recvlen = read(f, tmpbuf, sizeof(tmpbuf));
|
buf->str[tpos] = '\0';
|
||||||
|
g_string_prepend(buf, "%|");
|
||||||
ret = line_split(tmpbuf, recvlen, &str, &buffer);
|
printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, buf->str);
|
||||||
if (ret > 0) {
|
|
||||||
str = g_strconcat("%|", str, NULL);
|
|
||||||
printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, str);
|
|
||||||
g_free(str);
|
|
||||||
}
|
}
|
||||||
}
|
g_string_free(buf, TRUE);
|
||||||
while (ret > 0);
|
|
||||||
line_split_free(buffer);
|
|
||||||
|
|
||||||
close(f);
|
g_io_channel_close(handle);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user