ocarina/ocarina/pipe.cpp

48 lines
949 B
C++

// Copyright (c) Bryan Schumaker 2012.
#include <libsaria.h>
#include <print.h>
#include <fs.h>
#include <glib.h>
#include <string>
using namespace std;
static GIOChannel *pipe_in;
static void pipe_read_text(GIOChannel *source, string &text)
{
gchar *str = NULL;
GError *error = NULL;
gsize size, end;
g_io_channel_read_line(source, &str, &size, &end, &error);
text = str;
/* Strip newline character */
text = text.substr(0, text.size() - 1);
/* g_io_channel_read_line allocates memory */
g_free(str);
}
static gboolean pipe_read(GIOChannel *source, GIOCondition condition, gpointer data)
{
string text;
pipe_read_text(source, text);
libsaria::run_cmd(text);
return TRUE;
}
void init_pipe()
{
GError *error = NULL;
string pipe = libsaria::app::pipe_file();
if (pipe == "")
return;
pipe_in = g_io_channel_new_file(pipe.c_str(), "r+", &error);
if (!pipe_in)
return;
g_io_add_watch(pipe_in, G_IO_IN, pipe_read, NULL);
}