Example #1
0
mc_pipe_t *
mc_popen (const char *command, GError ** error)
{
    mc_pipe_t *p;
    char **argv;

    p = g_try_new (mc_pipe_t, 1);
    if (p == NULL)
    {
        mc_replace_error (error, MC_PIPE_ERROR_CREATE_PIPE, "%s",
                          _("Cannot create pipe descriptor"));
        goto ret_err;
    }

    if (!g_shell_parse_argv (command, NULL, &argv, error))
    {
        mc_replace_error (error, MC_PIPE_ERROR_PARSE_COMMAND, "%s",
                          _("Cannot parse command for pipe"));
        goto ret_err;
    }

    if (!g_spawn_async_with_pipes
        (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL,
         &p->child_pid, NULL, &p->out.fd, &p->err.fd, error))
    {
        mc_replace_error (error, MC_PIPE_ERROR_CREATE_PIPE_STREAM, "%s",
                          _("Cannot create pipe streams"));
        goto ret_err;
    }

    g_strfreev (argv);

    p->out.buf[0] = '\0';
    p->out.len = MC_PIPE_BUFSIZE;
    p->out.null_term = FALSE;

    p->err.buf[0] = '\0';
    p->err.len = MC_PIPE_BUFSIZE;
    p->err.null_term = FALSE;

    return p;

  ret_err:
    g_free (p);
    return NULL;
}
Example #2
0
mc_pipe_t *
mc_popen (const char *command, GError ** error)
{
    mc_pipe_t *p;
    const char *const argv[] = { "/bin/sh", "sh", "-c", command, NULL };

    p = g_try_new (mc_pipe_t, 1);
    if (p == NULL)
    {
        mc_replace_error (error, MC_PIPE_ERROR_CREATE_PIPE, "%s",
                          _("Cannot create pipe descriptor"));
        goto ret_err;
    }

    if (!g_spawn_async_with_pipes
        (NULL, (gchar **) argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_FILE_AND_ARGV_ZERO,
         NULL, NULL, &p->child_pid, NULL, &p->out.fd, &p->err.fd, error))
    {
        mc_replace_error (error, MC_PIPE_ERROR_CREATE_PIPE_STREAM, "%s",
                          _("Cannot create pipe streams"));
        goto ret_err;
    }

    p->out.buf[0] = '\0';
    p->out.len = MC_PIPE_BUFSIZE;
    p->out.null_term = FALSE;

    p->err.buf[0] = '\0';
    p->err.len = MC_PIPE_BUFSIZE;
    p->err.null_term = FALSE;

    return p;

  ret_err:
    g_free (p);
    return NULL;
}
Example #3
0
void
mc_pclose (mc_pipe_t * p, GError ** error)
{
    int res;

    if (p->out.fd >= 0)
        res = close (p->out.fd);
    if (p->err.fd >= 0)
        res = close (p->err.fd);

    do
    {
        int status;

        res = waitpid (p->child_pid, &status, 0);
    }
    while (res < 0 && errno == EINTR);

    if (res < 0)
        mc_replace_error (error, MC_PIPE_ERROR_READ, _("Unexpected error in waitpid():\n%s"),
                          unix_error_string (errno));

    g_free (p);
}
Example #4
0
File: args.c Project: idispatch/mc
gboolean
mc_args_parse (int *argc, char ***argv, const char *translation_domain, GError ** mcerror)
{
    const gchar *_system_codepage;
    gboolean ok = TRUE;

    mc_return_val_if_error (mcerror, FALSE);

    _system_codepage = str_detect_termencoding ();

#ifdef ENABLE_NLS
    if (!str_isutf8 (_system_codepage))
        bind_textdomain_codeset ("mc", "UTF-8");
#endif

    context = g_option_context_new (mc_args_add_usage_info ());

    g_option_context_set_ignore_unknown_options (context, FALSE);

    mc_args_add_extended_info_to_help ();

    main_group = g_option_group_new ("main", _("Main options"), _("Main options"), NULL, NULL);

    g_option_group_add_entries (main_group, argument_main_table);
    g_option_context_set_main_group (context, main_group);
    g_option_group_set_translation_domain (main_group, translation_domain);

    terminal_group = g_option_group_new ("terminal", _("Terminal options"),
                                         _("Terminal options"), NULL, NULL);

    g_option_group_add_entries (terminal_group, argument_terminal_table);
    g_option_context_add_group (context, terminal_group);
    g_option_group_set_translation_domain (terminal_group, translation_domain);

    color_group = mc_args_new_color_group ();

    g_option_group_add_entries (color_group, argument_color_table);
    g_option_context_add_group (context, color_group);
    g_option_group_set_translation_domain (color_group, translation_domain);

    if (!g_option_context_parse (context, argc, argv, mcerror))
    {
        if (*mcerror == NULL)
            mc_propagate_error (mcerror, 0, "%s\n", _("Arguments parse error!"));
        else
        {
            gchar *help_str;

            help_str = g_option_context_get_help (context, TRUE, NULL);

            if (str_isutf8 (_system_codepage))
                mc_replace_error (mcerror, (*mcerror)->code, "%s\n\n%s\n", (*mcerror)->message,
                                  help_str);
            else
            {
                gchar *full_help_str;

                full_help_str =
                    mc_args__convert_help_to_syscharset (_system_codepage, (*mcerror)->message,
                                                         help_str);
                mc_replace_error (mcerror, (*mcerror)->code, "%s", full_help_str);
                g_free (full_help_str);
            }
            g_free (help_str);
        }

        ok = FALSE;
    }

    g_option_context_free (context);
    mc_args_clean_temp_help_strings ();

#ifdef ENABLE_NLS
    if (!str_isutf8 (_system_codepage))
        bind_textdomain_codeset ("mc", _system_codepage);
#endif

    return ok;
}