2014-07-21 15:39:24 -04:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
|
|
|
* Copyright 2013-2014 Qball Cow <qball@gmpclient.org>
|
|
|
|
*
|
|
|
|
* 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 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>
|
|
|
|
#include "rofi.h"
|
|
|
|
#include "script-dialog.h"
|
2014-09-03 07:07:26 -04:00
|
|
|
#include "helper.h"
|
2014-07-21 15:39:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char **get_script_output ( char *command, unsigned int *length )
|
|
|
|
{
|
|
|
|
char **retv = NULL;
|
|
|
|
|
|
|
|
*length = 0;
|
2014-08-25 14:02:48 -04:00
|
|
|
int fd = execute_generator ( command );
|
|
|
|
if ( fd >= 0 ) {
|
|
|
|
FILE *inp = fdopen ( fd, "r" );
|
|
|
|
if ( inp ) {
|
2014-09-06 08:57:30 -04:00
|
|
|
char buffer[1024];
|
2014-08-25 14:02:48 -04: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 )++;
|
|
|
|
}
|
|
|
|
fclose ( inp );
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char **execute_executor ( ScriptOptions *options, const char *result, unsigned int *length )
|
|
|
|
{
|
2014-11-22 15:39:34 -05:00
|
|
|
char **retv = NULL;
|
|
|
|
|
2014-11-22 16:40:53 -05:00
|
|
|
char *arg = g_shell_quote ( result );
|
2014-11-22 15:39:34 -05:00
|
|
|
char *command = g_strdup_printf ( "%s %s", options->script_path, arg );
|
2014-08-09 05:40:42 -04:00
|
|
|
retv = get_script_output ( command, length );
|
|
|
|
g_free ( command );
|
2014-11-22 15:39:34 -05:00
|
|
|
g_free ( arg );
|
2014-07-21 15:39:24 -04:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SwitcherMode script_switcher_dialog ( char **input, void *data )
|
|
|
|
{
|
|
|
|
ScriptOptions *options = (ScriptOptions *) data;
|
|
|
|
assert ( options != NULL );
|
|
|
|
int selected_line = 0;
|
|
|
|
SwitcherMode retv = MODE_EXIT;
|
|
|
|
unsigned int length = 0;
|
|
|
|
char **list = get_script_output ( options->script_path, &length );
|
2014-08-09 05:40:42 -04:00
|
|
|
char *prompt = g_strdup_printf ( "%s:", options->name );
|
2014-07-21 15:39:24 -04:00
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned int new_length = 0;
|
|
|
|
char **new_list = NULL;
|
2014-07-22 12:19:00 -04:00
|
|
|
int mretv = menu ( list, length, input, prompt, NULL, NULL,
|
2014-08-03 15:09:20 -04:00
|
|
|
token_match, NULL, &selected_line, FALSE );
|
2014-07-21 15:39:24 -04:00
|
|
|
|
|
|
|
if ( mretv == MENU_NEXT ) {
|
|
|
|
retv = NEXT_DIALOG;
|
|
|
|
}
|
2014-11-11 15:50:16 -05:00
|
|
|
else if ( mretv == MENU_PREVIOUS ) {
|
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
2014-07-21 17:19:45 -04:00
|
|
|
else if ( mretv == MENU_QUICK_SWITCH ) {
|
|
|
|
retv = selected_line;
|
|
|
|
}
|
2014-07-21 15:39:24 -04:00
|
|
|
else if ( mretv == MENU_OK && list[selected_line] != NULL ) {
|
|
|
|
new_list = execute_executor ( options, list[selected_line], &new_length );
|
|
|
|
}
|
|
|
|
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
|
|
|
|
new_list = execute_executor ( options, *input, &new_length );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free old list.
|
2014-08-09 05:40:42 -04:00
|
|
|
g_strfreev ( list );
|
|
|
|
list = NULL;
|
2014-07-21 15:39:24 -04:00
|
|
|
|
|
|
|
// If a new list was generated, use that an loop around.
|
|
|
|
if ( new_list != NULL ) {
|
|
|
|
list = new_list;
|
|
|
|
length = new_length;
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( *input );
|
2014-07-21 15:39:24 -04:00
|
|
|
*input = NULL;
|
|
|
|
}
|
|
|
|
} while ( list != NULL );
|
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( prompt );
|
2014-07-21 15:39:24 -04:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void script_switcher_free_options ( ScriptOptions *sw )
|
|
|
|
{
|
|
|
|
if ( sw == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( sw->name );
|
|
|
|
g_free ( sw->script_path );
|
|
|
|
g_free ( sw );
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ScriptOptions *script_switcher_parse_setup ( const char *str )
|
|
|
|
{
|
2014-08-09 05:40:42 -04:00
|
|
|
ScriptOptions *sw = g_malloc0 ( sizeof ( *sw ) );
|
2014-07-21 15:39:24 -04:00
|
|
|
char *endp = NULL;
|
2014-08-09 05:40:42 -04:00
|
|
|
char *parse = g_strdup ( str );
|
2014-07-21 15:39:24 -04:00
|
|
|
unsigned int index = 0;
|
|
|
|
// TODO: This is naive and can be improved.
|
|
|
|
for ( char *token = strtok_r ( parse, ":", &endp ); token != NULL; token = strtok_r ( NULL, ":", &endp ) ) {
|
|
|
|
if ( index == 0 ) {
|
2014-08-09 05:40:42 -04:00
|
|
|
sw->name = g_strdup ( token );
|
2014-07-21 15:39:24 -04:00
|
|
|
}
|
|
|
|
else if ( index == 1 ) {
|
2014-08-09 05:40:42 -04:00
|
|
|
sw->script_path = g_strdup ( 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 ) {
|
|
|
|
return sw;
|
|
|
|
}
|
2014-08-24 09:21:35 -04:00
|
|
|
fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.\n",
|
2014-07-21 15:39:24 -04:00
|
|
|
str, index );
|
|
|
|
script_switcher_free_options ( sw );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|