summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorQuentin Glidic <sardemff7+git@sardemff7.net>2016-03-11 10:43:32 +0100
committerQuentin Glidic <sardemff7+git@sardemff7.net>2017-06-02 12:50:32 +0200
commit0daab1844efe172318e68909520ec8b97a3acff9 (patch)
tree98e0dc45d842e696bef846199111e9cfa6fc6d76
parentdbac6fba5c6158e0bd3819db71940ec4ebddfbae (diff)
helper: Add execute helper
Signed-off-by: Quentin Glidic <sardemff7+git@sardemff7.net>
-rw-r--r--include/helper.h14
-rw-r--r--source/dialogs/ssh.c19
-rw-r--r--source/helper.c37
3 files changed, 39 insertions, 31 deletions
diff --git a/include/helper.h b/include/helper.h
index ab7fa1c2..de9419dd 100644
--- a/include/helper.h
+++ b/include/helper.h
@@ -39,6 +39,7 @@
*
* @{
*/
+
/**
* @param string The input string.
* @param output Pointer to 2 dimensional array with parsed string.
@@ -259,6 +260,17 @@ int rofi_scorer_fuzzy_evaluate ( const char *pattern, glong plen, const char *st
int utf8_strncmp ( const char *a, const char* b, size_t n ) __attribute__( ( nonnull ( 1, 2 ) ) );
/**
+ * @param args The arguments of the command to exec.
+ * @param error_precmd Prefix to error message command.
+ * @param error_cmd Error message command
+ *
+ * Executes the command
+ *
+ * @returns TRUE when successful, FALSE when failed.
+ */
+gboolean helper_execute ( const char *wd, char **args, const char *error_precmd, const char *error_cmd );
+
+/**
* @param wd The work directory (optional)
* @param cmd The cmd to execute
* @param run_in_term Indicate if command should be run in a terminal
@@ -267,7 +279,7 @@ int utf8_strncmp ( const char *a, const char* b, size_t n ) __attribute__( ( non
*
* @returns FALSE On failure, TRUE on success
*/
-int helper_execute_command ( const char *wd, const char *cmd, int run_in_term );
+gboolean helper_execute_command ( const char *wd, const char *cmd, int run_in_term );
/**
* @param file The file path
diff --git a/source/dialogs/ssh.c b/source/dialogs/ssh.c
index 96230bf8..66fdc74a 100644
--- a/source/dialogs/ssh.c
+++ b/source/dialogs/ssh.c
@@ -78,25 +78,10 @@ static inline int execshssh ( const char *host )
{
char **args = NULL;
int argsv = 0;
- helper_parse_setup ( config.ssh_command, &args, &argsv, "{host}", host, NULL );
-
- GError *error = NULL;
- g_spawn_async ( NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error );
-
- if ( error != NULL ) {
- char *msg = g_strdup_printf ( "Failed to execute: 'ssh %s'\nError: '%s'", host, error->message );
- rofi_view_error_dialog ( msg, FALSE );
- g_free ( msg );
- // print error.
- g_error_free ( error );
- g_strfreev ( args );
- return FALSE;
- }
- // Free the args list.
- g_strfreev ( args );
+ helper_parse_setup ( config.ssh_command, &args, &argsv, "{host}", host, NULL );
- return TRUE;
+ return helper_execute ( NULL, args, "ssh ", host );
}
/**
diff --git a/source/helper.c b/source/helper.c
index e0a0563e..ebcf783d 100644
--- a/source/helper.c
+++ b/source/helper.c
@@ -962,21 +962,17 @@ int utf8_strncmp ( const char* a, const char* b, size_t n )
return r;
}
-int helper_execute_command ( const char *wd, const char *cmd, int run_in_term )
+gboolean helper_execute ( const char *wd, char **args, const char *error_precmd, const char *error_cmd )
{
- int retv = TRUE;
- char **args = NULL;
- int argc = 0;
- if ( run_in_term ) {
- helper_parse_setup ( config.run_shell_command, &args, &argc, "{cmd}", cmd, NULL );
- }
- else {
- helper_parse_setup ( config.run_command, &args, &argc, "{cmd}", cmd, NULL );
- }
- GError *error = NULL;
- g_spawn_async ( wd, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error );
+ gboolean retv = TRUE;
+ GError *error = NULL;
+
+ GSpawnChildSetupFunc child_setup = NULL;
+ gpointer user_data = NULL;
+
+ g_spawn_async ( wd, args, NULL, G_SPAWN_SEARCH_PATH, child_setup, user_data, NULL, &error );
if ( error != NULL ) {
- char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", cmd, error->message );
+ char *msg = g_strdup_printf ( "Failed to execute: '%s%s'\nError: '%s'", error_precmd, error_cmd, error->message );
rofi_view_error_dialog ( msg, FALSE );
g_free ( msg );
// print error.
@@ -989,6 +985,21 @@ int helper_execute_command ( const char *wd, const char *cmd, int run_in_term )
return retv;
}
+gboolean helper_execute_command ( const char *wd, const char *cmd, int run_in_term )
+{
+ char **args = NULL;
+ int argc = 0;
+
+ if ( run_in_term ) {
+ helper_parse_setup ( config.run_shell_command, &args, &argc, "{cmd}", cmd, NULL );
+ }
+ else {
+ helper_parse_setup ( config.run_command, &args, &argc, "{cmd}", cmd, NULL );
+ }
+
+ return helper_execute ( wd, args, "", cmd );
+}
+
char *helper_get_theme_path ( const char *file )
{
char *filename = rofi_expand_path ( file );