2014-07-21 19:39:24 +00:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2015-04-14 20:12:21 +00:00
|
|
|
* Copyright 2013-2015 Qball Cow <qball@gmpclient.org>
|
2014-07-21 19:39:24 +00: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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-22 07:26:46 +00:00
|
|
|
#include <config.h>
|
2014-07-21 19:39:24 +00: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 08:21:32 +00:00
|
|
|
#include <errno.h>
|
2014-07-21 19:39:24 +00:00
|
|
|
#include "rofi.h"
|
2015-03-25 07:36:19 +00:00
|
|
|
#include "dialogs/script.h"
|
2014-09-03 11:07:26 +00:00
|
|
|
#include "helper.h"
|
2014-07-21 19:39:24 +00:00
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
static char **get_script_output ( const char *command, unsigned int *length )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
|
|
|
char **retv = NULL;
|
|
|
|
|
|
|
|
*length = 0;
|
2014-08-25 18:02:48 +00:00
|
|
|
int fd = execute_generator ( command );
|
|
|
|
if ( fd >= 0 ) {
|
|
|
|
FILE *inp = fdopen ( fd, "r" );
|
|
|
|
if ( inp ) {
|
2014-09-06 12:57:30 +00:00
|
|
|
char buffer[1024];
|
2014-08-25 18:02:48 +00:00
|
|
|
while ( fgets ( buffer, 1024, inp ) != NULL ) {
|
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
|
|
|
retv[( *length )] = g_strdup ( buffer );
|
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
|
|
|
|
// Filter out line-end.
|
|
|
|
if ( retv[( *length )][strlen ( buffer ) - 1] == '\n' ) {
|
|
|
|
retv[( *length )][strlen ( buffer ) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
( *length )++;
|
|
|
|
}
|
2015-07-31 08:21:32 +00:00
|
|
|
if ( fclose ( inp ) != 0 ) {
|
2015-09-19 10:21:30 +00:00
|
|
|
fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n",
|
|
|
|
strerror ( errno ) );
|
2015-07-31 08:21:32 +00:00
|
|
|
}
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
static char **execute_executor ( Switcher *sw, const char *result, unsigned int *length )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
2014-11-22 20:39:34 +00:00
|
|
|
char **retv = NULL;
|
|
|
|
|
2014-11-22 21:40:53 +00:00
|
|
|
char *arg = g_shell_quote ( result );
|
2015-03-27 19:28:53 +00:00
|
|
|
char *command = g_strdup_printf ( "%s %s", (const char *) sw->ed, arg );
|
2014-08-09 09:40:42 +00:00
|
|
|
retv = get_script_output ( command, length );
|
|
|
|
g_free ( command );
|
2014-11-22 20:39:34 +00:00
|
|
|
g_free ( arg );
|
2014-07-21 19:39:24 +00:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
static void script_switcher_free ( Switcher *sw )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
if ( sw == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_free ( sw->ed );
|
|
|
|
g_free ( sw );
|
|
|
|
}
|
2014-07-21 19:39:24 +00:00
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
typedef struct _ScriptModePrivateData
|
|
|
|
{
|
|
|
|
unsigned int id;
|
|
|
|
char **cmd_list;
|
|
|
|
unsigned int cmd_list_length;
|
|
|
|
} ScriptModePrivateData;
|
2014-07-21 19:39:24 +00:00
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
static void script_mode_init ( Switcher *sw )
|
|
|
|
{
|
|
|
|
if ( sw->private_data == NULL ) {
|
|
|
|
ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
|
|
|
sw->private_data = (void *) pd;
|
2015-11-21 22:59:59 +00:00
|
|
|
pd->cmd_list = get_script_output ( (const char *) sw->ed, &( pd->cmd_list_length ) );
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-23 20:52:55 +00:00
|
|
|
static unsigned int script_mode_get_num_entries ( const Switcher *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2015-11-23 20:52:55 +00:00
|
|
|
const ScriptModePrivateData *rmpd = (const ScriptModePrivateData *) sw->private_data;
|
2015-11-21 22:59:59 +00:00
|
|
|
return rmpd->cmd_list_length;
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 10:21:30 +00:00
|
|
|
static SwitcherMode script_mode_result ( int mretv, char **input, unsigned int selected_line,
|
|
|
|
Switcher *sw )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data;
|
|
|
|
SwitcherMode retv = MODE_EXIT;
|
|
|
|
char **new_list = NULL;
|
|
|
|
unsigned int new_length = 0;
|
|
|
|
|
|
|
|
if ( ( mretv & MENU_NEXT ) ) {
|
|
|
|
retv = NEXT_DIALOG;
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
else if ( ( mretv & MENU_PREVIOUS ) ) {
|
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
|
|
|
else if ( ( mretv & MENU_QUICK_SWITCH ) ) {
|
2015-05-03 11:04:03 +00:00
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2015-03-27 19:28:53 +00: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;
|
|
|
|
g_free ( *input );
|
|
|
|
*input = NULL;
|
|
|
|
retv = RELOAD_DIALOG;
|
|
|
|
}
|
|
|
|
return retv;
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
static void script_mode_destroy ( Switcher *sw )
|
|
|
|
{
|
|
|
|
ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data;
|
|
|
|
if ( rmpd != NULL ) {
|
|
|
|
g_strfreev ( rmpd->cmd_list );
|
|
|
|
g_free ( rmpd );
|
|
|
|
sw->private_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 23:33:26 +00:00
|
|
|
static char *mgrv ( unsigned int selected_line, const Switcher *sw, G_GNUC_UNUSED int *state, int get_entry )
|
2015-11-21 22:59:59 +00:00
|
|
|
{
|
|
|
|
ScriptModePrivateData *rmpd = sw->private_data;
|
|
|
|
return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-21 23:33:26 +00:00
|
|
|
static int script_token_match ( char **tokens, int not_ascii, int case_sensitive, unsigned int index, const Switcher *sw )
|
2015-11-21 22:59:59 +00:00
|
|
|
{
|
|
|
|
ScriptModePrivateData *rmpd = sw->private_data;
|
|
|
|
return token_match ( tokens, rmpd->cmd_list[index], not_ascii, case_sensitive );
|
|
|
|
}
|
|
|
|
|
2015-11-21 23:33:26 +00:00
|
|
|
static int script_is_not_ascii ( const Switcher *sw, unsigned int index )
|
2015-03-31 20:45:02 +00:00
|
|
|
{
|
2015-11-21 22:59:59 +00:00
|
|
|
ScriptModePrivateData *rmpd = sw->private_data;
|
|
|
|
return is_not_ascii ( rmpd->cmd_list[index] );
|
2015-03-31 20:45:02 +00:00
|
|
|
}
|
2014-07-21 19:39:24 +00:00
|
|
|
|
2015-03-27 19:28:53 +00:00
|
|
|
Switcher *script_switcher_parse_setup ( const char *str )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
Switcher *sw = g_malloc0 ( sizeof ( *sw ) );
|
|
|
|
char *endp = NULL;
|
|
|
|
char *parse = g_strdup ( str );
|
|
|
|
unsigned int index = 0;
|
2015-09-19 18:59:50 +00:00
|
|
|
for ( char *token = strtok_r ( parse, ":", &endp ); token != NULL; token = strtok_r ( NULL, ":", &endp ) ) {
|
2014-07-21 19:39:24 +00:00
|
|
|
if ( index == 0 ) {
|
2015-03-27 19:28:53 +00:00
|
|
|
g_strlcpy ( sw->name, token, 32 );
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
else if ( index == 1 ) {
|
2015-03-27 19:28:53 +00:00
|
|
|
sw->ed = (void *) g_strdup ( token );
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( parse );
|
2014-07-21 19:39:24 +00:00
|
|
|
if ( index == 2 ) {
|
2015-11-21 22:59:59 +00:00
|
|
|
sw->free = script_switcher_free;
|
|
|
|
sw->keysym = None;
|
|
|
|
sw->modmask = AnyModifier;
|
|
|
|
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;
|
|
|
|
sw->mgrv = mgrv;
|
|
|
|
sw->is_not_ascii = script_is_not_ascii;
|
2015-03-27 19:28:53 +00:00
|
|
|
|
2014-07-21 19:39:24 +00:00
|
|
|
return sw;
|
|
|
|
}
|
2015-09-19 18:59:50 +00:00
|
|
|
fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.\n", str, index );
|
2015-03-27 19:28:53 +00:00
|
|
|
script_switcher_free ( sw );
|
2014-07-21 19:39:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|