2017-04-15 06:32:05 -04:00
|
|
|
/*
|
2014-07-21 15:39:24 -04:00
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2017-04-15 06:32:05 -04:00
|
|
|
* Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
|
2014-07-21 15:39:24 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-27 16:59:14 -04:00
|
|
|
#define G_LOG_DOMAIN "Dialogs.Script"
|
2017-04-15 05:58:49 -04:00
|
|
|
|
2014-08-22 03:26:46 -04:00
|
|
|
#include <config.h>
|
2014-07-21 15:39:24 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
2015-07-31 04:21:32 -04:00
|
|
|
#include <errno.h>
|
2014-07-21 15:39:24 -04:00
|
|
|
#include "rofi.h"
|
2015-03-25 03:36:19 -04:00
|
|
|
#include "dialogs/script.h"
|
2014-09-03 07:07:26 -04:00
|
|
|
#include "helper.h"
|
2017-05-22 03:24:05 -04:00
|
|
|
|
2017-05-23 11:19:04 -04:00
|
|
|
#include "mode-private.h"
|
2017-08-14 11:28:25 -04:00
|
|
|
static char **get_script_output ( char *command, char *arg, unsigned int *length )
|
2014-07-21 15:39:24 -04:00
|
|
|
{
|
2017-08-14 11:28:25 -04:00
|
|
|
int fd = -1;
|
|
|
|
GError *error = NULL;
|
2014-07-21 15:39:24 -04:00
|
|
|
char **retv = NULL;
|
2017-08-14 15:33:12 -04:00
|
|
|
char **argv = NULL;
|
|
|
|
int argc = 0;
|
2014-07-21 15:39:24 -04:00
|
|
|
*length = 0;
|
2017-08-14 15:33:12 -04:00
|
|
|
if ( g_shell_parse_argv ( command, &argc, &argv, &error ) )
|
|
|
|
{
|
|
|
|
argv = g_realloc ( argv, (argc+2)*sizeof(char*) );
|
|
|
|
argv[argc] = g_strdup(arg);
|
|
|
|
argv[argc+1] = NULL;
|
|
|
|
g_spawn_async_with_pipes ( NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
|
|
|
|
}
|
2017-08-14 11:28:25 -04:00
|
|
|
if ( error != NULL ) {
|
|
|
|
char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", command, error->message );
|
|
|
|
rofi_view_error_dialog ( msg, FALSE );
|
|
|
|
g_free ( msg );
|
|
|
|
// print error.
|
|
|
|
g_error_free ( error );
|
|
|
|
fd = -1;
|
|
|
|
}
|
2014-08-25 14:02:48 -04:00
|
|
|
if ( fd >= 0 ) {
|
|
|
|
FILE *inp = fdopen ( fd, "r" );
|
|
|
|
if ( inp ) {
|
2016-04-10 08:30:13 -04:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
2017-05-23 11:19:04 -04:00
|
|
|
while ( getline ( &buffer, &buffer_length, inp ) > 0 ) {
|
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
2014-08-25 14:02:48 -04:00
|
|
|
retv[( *length )] = g_strdup ( buffer );
|
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
|
2017-05-23 11:19:04 -04:00
|
|
|
// Filter out line-end.
|
|
|
|
if ( retv[( *length )][strlen ( buffer ) - 1] == '\n' ) {
|
|
|
|
retv[( *length )][strlen ( buffer ) - 1] = '\0';
|
|
|
|
}
|
2014-08-25 14:02:48 -04:00
|
|
|
|
|
|
|
( *length )++;
|
|
|
|
}
|
2016-04-10 08:30:13 -04:00
|
|
|
if ( buffer ) {
|
|
|
|
free ( buffer );
|
|
|
|
}
|
2015-07-31 04:21:32 -04:00
|
|
|
if ( fclose ( inp ) != 0 ) {
|
2017-04-15 05:58:49 -04:00
|
|
|
g_warning ( "Failed to close stdout off executor script: '%s'",
|
2017-04-27 16:59:14 -04:00
|
|
|
g_strerror ( errno ) );
|
2015-07-31 04:21:32 -04:00
|
|
|
}
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2017-08-14 11:28:25 -04:00
|
|
|
static char **execute_executor ( Mode *sw, char *result, unsigned int *length )
|
2014-07-21 15:39:24 -04:00
|
|
|
{
|
2017-08-14 11:28:25 -04:00
|
|
|
char **retv = get_script_output ( sw->ed, result, length );
|
2014-07-21 15:39:24 -04:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static void script_switcher_free ( Mode *sw )
|
2014-07-21 15:39:24 -04:00
|
|
|
{
|
2015-03-27 15:28:53 -04:00
|
|
|
if ( sw == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 03:37:03 -04:00
|
|
|
g_free ( sw->name );
|
2015-03-27 15:28:53 -04:00
|
|
|
g_free ( sw->ed );
|
|
|
|
g_free ( sw );
|
|
|
|
}
|
2014-07-21 15:39:24 -04:00
|
|
|
|
2017-05-23 11:19:04 -04:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned int id;
|
|
|
|
char **cmd_list;
|
|
|
|
unsigned int cmd_list_length;
|
|
|
|
} ScriptModePrivateData;
|
|
|
|
|
2016-01-08 03:16:59 -05:00
|
|
|
static int script_mode_init ( Mode *sw )
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
|
|
|
if ( sw->private_data == NULL ) {
|
|
|
|
ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
|
|
|
sw->private_data = (void *) pd;
|
2017-08-14 11:28:25 -04:00
|
|
|
pd->cmd_list = get_script_output ( (char *) sw->ed, NULL, &( pd->cmd_list_length ) );
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
2016-01-08 03:16:59 -05:00
|
|
|
return TRUE;
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
2015-11-25 03:26:38 -05:00
|
|
|
static unsigned int script_mode_get_num_entries ( const Mode *sw )
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
2015-11-23 15:52:55 -05:00
|
|
|
const ScriptModePrivateData *rmpd = (const ScriptModePrivateData *) sw->private_data;
|
2015-11-21 17:59:59 -05:00
|
|
|
return rmpd->cmd_list_length;
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static ModeMode script_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
|
2014-07-21 15:39:24 -04:00
|
|
|
{
|
2015-03-27 15:28:53 -04:00
|
|
|
ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data;
|
2015-11-25 03:26:38 -05:00
|
|
|
ModeMode retv = MODE_EXIT;
|
2015-03-27 15:28:53 -04:00
|
|
|
char **new_list = NULL;
|
|
|
|
unsigned int new_length = 0;
|
|
|
|
|
|
|
|
if ( ( mretv & MENU_NEXT ) ) {
|
|
|
|
retv = NEXT_DIALOG;
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
else if ( ( mretv & MENU_PREVIOUS ) ) {
|
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
|
|
|
else if ( ( mretv & MENU_QUICK_SWITCH ) ) {
|
2015-05-03 07:04:03 -04:00
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
|
|
|
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
|
|
|
|
new_list = execute_executor ( sw, rmpd->cmd_list[selected_line], &new_length );
|
|
|
|
}
|
|
|
|
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
|
|
|
|
new_list = execute_executor ( sw, *input, &new_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a new list was generated, use that an loop around.
|
|
|
|
if ( new_list != NULL ) {
|
|
|
|
g_strfreev ( rmpd->cmd_list );
|
|
|
|
|
|
|
|
rmpd->cmd_list = new_list;
|
|
|
|
rmpd->cmd_list_length = new_length;
|
2017-01-08 15:36:06 -05:00
|
|
|
retv = RESET_DIALOG;
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
|
|
|
return retv;
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static void script_mode_destroy ( Mode *sw )
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
|
|
|
ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data;
|
|
|
|
if ( rmpd != NULL ) {
|
|
|
|
g_strfreev ( rmpd->cmd_list );
|
|
|
|
g_free ( rmpd );
|
|
|
|
sw->private_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 17:39:29 -05:00
|
|
|
static char *_get_display_value ( const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **list, int get_entry )
|
2015-11-21 17:59:59 -05:00
|
|
|
{
|
|
|
|
ScriptModePrivateData *rmpd = sw->private_data;
|
|
|
|
return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-22 11:47:34 -04:00
|
|
|
static int script_token_match ( const Mode *sw, GRegex **tokens, unsigned int index )
|
2015-11-21 17:59:59 -05:00
|
|
|
{
|
|
|
|
ScriptModePrivateData *rmpd = sw->private_data;
|
2017-02-17 03:58:47 -05:00
|
|
|
return helper_token_match ( tokens, rmpd->cmd_list[index] );
|
2015-03-31 16:45:02 -04:00
|
|
|
}
|
2014-07-21 15:39:24 -04:00
|
|
|
|
2016-01-07 15:27:20 -05:00
|
|
|
#include "mode-private.h"
|
2015-11-25 03:26:38 -05:00
|
|
|
Mode *script_switcher_parse_setup ( const char *str )
|
2014-07-21 15:39:24 -04:00
|
|
|
{
|
2016-03-20 05:16:55 -04:00
|
|
|
Mode *sw = g_malloc0 ( sizeof ( *sw ) );
|
|
|
|
char *endp = NULL;
|
|
|
|
char *parse = g_strdup ( str );
|
|
|
|
unsigned int index = 0;
|
|
|
|
const char *const sep = ":";
|
2016-03-19 08:29:04 -04:00
|
|
|
for ( char *token = strtok_r ( parse, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
|
2014-07-21 15:39:24 -04:00
|
|
|
if ( index == 0 ) {
|
2017-04-21 03:37:03 -04:00
|
|
|
sw->name = g_strdup ( token );
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
|
|
|
else if ( index == 1 ) {
|
2015-11-27 07:01:25 -05:00
|
|
|
sw->ed = (void *) rofi_expand_path ( token );
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( parse );
|
2014-07-21 15:39:24 -04:00
|
|
|
if ( index == 2 ) {
|
2016-01-07 15:27:20 -05:00
|
|
|
sw->free = script_switcher_free;
|
|
|
|
sw->_init = script_mode_init;
|
|
|
|
sw->_get_num_entries = script_mode_get_num_entries;
|
|
|
|
sw->_result = script_mode_result;
|
|
|
|
sw->_destroy = script_mode_destroy;
|
|
|
|
sw->_token_match = script_token_match;
|
2017-05-23 11:19:04 -04:00
|
|
|
sw->_get_completion = NULL,
|
|
|
|
sw->_preprocess_input = NULL,
|
2016-01-07 15:27:20 -05:00
|
|
|
sw->_get_display_value = _get_display_value;
|
2015-03-27 15:28:53 -04:00
|
|
|
|
2014-07-21 15:39:24 -04:00
|
|
|
return sw;
|
|
|
|
}
|
2017-04-21 03:37:03 -04:00
|
|
|
fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.", str, index );
|
2015-03-27 15:28:53 -04:00
|
|
|
script_switcher_free ( sw );
|
2014-07-21 15:39:24 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-20 03:24:09 -04:00
|
|
|
|
|
|
|
gboolean script_switcher_is_valid ( const char *token )
|
|
|
|
{
|
2017-04-27 16:59:14 -04:00
|
|
|
return strchr ( token, ':' ) != NULL;
|
2017-04-20 03:24:09 -04:00
|
|
|
}
|