Ejemplo n.º 1
0
Archivo: main.c Proyecto: Nakrez/zOS
static void execute(char *buf)
{
    struct command *cmd = command_new();
    char *arg;
    char *rest;

    if (!cmd)
    {
        fprintf(stderr, "Memory exhausted\n");

        return;
    }

    while (*buf && *buf == ' ')
        buf++;

    if (!*buf)
    {
        command_free(cmd);

        return;
    }

    arg = strtok_r(buf, " ", &rest);

    while (arg)
    {
        if (command_append(cmd, arg) < 0)
        {
            command_free(cmd);

            fprintf(stderr, "Memory exhausted\n");

            return;
        }

        while (*rest && *rest == ' ')
            rest++;

        arg = strtok_r(NULL, " ", &rest);
    }

    command_append(cmd, NULL);

    command_execute(cmd);

    command_free(cmd);
}
Ejemplo n.º 2
0
static gboolean handle_stdin(GIOChannel* io, GIOCondition cond, gpointer data) {
    struct command_ctx* ctx = (struct command_ctx*)data;

    gchar in;
    GError *error = NULL;

    switch (g_io_channel_read_chars(io, &in, 1, NULL, &error)) {
        case G_IO_STATUS_NORMAL:
            if(in == '\n') {
                command_consume(ctx);
                return TRUE;
            }

            command_append(ctx, in);
            return TRUE;

        case G_IO_STATUS_ERROR:
            g_printerr("IO error: %s\n", error->message);
            g_error_free(error);
            return FALSE;

        case G_IO_STATUS_EOF:
            g_warning("No input data available");
            return TRUE;

        case G_IO_STATUS_AGAIN:
            return TRUE;

        default:
            g_return_val_if_reached(FALSE);
            break;
      }

    return FALSE;
}