diff --git a/source/dialogs/script.c b/source/dialogs/script.c index df006ba4..a4daf289 100644 --- a/source/dialogs/script.c +++ b/source/dialogs/script.c @@ -39,104 +39,28 @@ #include "rofi.h" #include "dialogs/script.h" #include "helper.h" -#include "widgets/textbox.h" + #include "mode-private.h" - -#define START_OF_HEADING '\x01' -#define START_OF_TEXT '\x02' -#define UNIT_SEPARATOR '\x1F' - -typedef enum { - PARSING_ENTRIES = 0, - PARSING_HEADER = 1 - -} ParserMode; - -typedef struct +static char **get_script_output ( const char *command, unsigned int *length ) { - unsigned int id; - - ParserMode pmode; - char **cmd_list; - unsigned int cmd_list_length; - /** Display msg */ - char *message; - char *prompt; - char *format; - gboolean markup_rows; -} ScriptModePrivateData; - -static void parse_header ( Mode *sw, ScriptModePrivateData *pd, char *buffer, const ssize_t buffer_length ) -{ - if ( *buffer == START_OF_TEXT ) - { - pd->pmode = PARSING_ENTRIES; - return; - } - char *unit_sep = strchr ( buffer, UNIT_SEPARATOR); - if ( unit_sep ) - { - const char *command = buffer; - const char *value = unit_sep+1; - *unit_sep = '\0'; - if ( strcmp ( command, "message" ) == 0 ) { - if ( pd->message ) g_free ( pd->message ); - pd->message = g_strdup(value); - } - else if ( strcmp ( command, "prompt" ) == 0 ) { - if ( pd->prompt ) g_free ( pd->prompt ); - pd->prompt = g_strdup(value); - sw->display_name = pd->prompt; - } - else if ( strcmp ( command, "markup_rows" ) == 0 ){ - pd->markup_rows = strcasecmp( value, "true") == 0; - } - else if ( strcmp ( command, "format" ) == 0 ) { - if ( pd->format ) g_free ( pd->format ); - pd->format = g_strdup(value); - } - } -} - -static char **get_script_output ( Mode *sw, ScriptModePrivateData *pd, const char *command, unsigned int *length ) -{ - // Default to parsing entries. - pd->pmode = PARSING_ENTRIES; - char **retv = NULL; *length = 0; - unsigned int actual_length = 0; int fd = execute_generator ( command ); if ( fd >= 0 ) { FILE *inp = fdopen ( fd, "r" ); if ( inp ) { char *buffer = NULL; size_t buffer_length = 0; - ssize_t read_length = 0; - while ( (read_length = getline ( &buffer, &buffer_length, inp )) > 0 ) { - if ( buffer[read_length-1] == '\n' ){ - buffer[read_length-1] = '\0'; - read_length--; - } - if ( buffer[0] == START_OF_HEADING ) { - pd->pmode = PARSING_HEADER; - parse_header( sw, pd, buffer+1, read_length); - continue; - } - if ( pd->pmode == PARSING_HEADER ){ - parse_header(sw, pd, buffer, read_length); - continue; - } - - if ( actual_length < ((*length)+2)) { - actual_length += 128; - retv = g_realloc ( retv, ( actual_length ) * sizeof ( char* ) ); - } - + while ( getline ( &buffer, &buffer_length, inp ) > 0 ) { + 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 )++; } @@ -154,10 +78,9 @@ static char **get_script_output ( Mode *sw, ScriptModePrivateData *pd, const cha static char **execute_executor ( Mode *sw, const char *result, unsigned int *length ) { - ScriptModePrivateData *pd = (ScriptModePrivateData *) sw->private_data; char *arg = g_shell_quote ( result ); char *command = g_strdup_printf ( "%s %s", (const char *) sw->ed, arg ); - char **retv = get_script_output ( sw, pd, command, length ); + char **retv = get_script_output ( command, length ); g_free ( command ); g_free ( arg ); return retv; @@ -173,12 +96,19 @@ static void script_switcher_free ( Mode *sw ) g_free ( sw ); } +typedef struct +{ + unsigned int id; + char **cmd_list; + unsigned int cmd_list_length; +} ScriptModePrivateData; + static int script_mode_init ( Mode *sw ) { if ( sw->private_data == NULL ) { ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) ); sw->private_data = (void *) pd; - pd->cmd_list = get_script_output ( sw,pd, (const char *) sw->ed, &( pd->cmd_list_length ) ); + pd->cmd_list = get_script_output ( (const char *) sw->ed, &( pd->cmd_list_length ) ); } return TRUE; } @@ -227,8 +157,6 @@ static void script_mode_destroy ( Mode *sw ) ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data; if ( rmpd != NULL ) { g_strfreev ( rmpd->cmd_list ); - g_free ( rmpd->message ); - g_free ( rmpd->prompt ); g_free ( rmpd ); sw->private_data = NULL; } @@ -236,9 +164,6 @@ static void script_mode_destroy ( Mode *sw ) 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 ) { ScriptModePrivateData *rmpd = sw->private_data; - if ( rmpd->markup_rows ) { - *state |= MARKUP; - } return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL; } @@ -247,14 +172,6 @@ static int script_token_match ( const Mode *sw, GRegex **tokens, unsigned int in ScriptModePrivateData *rmpd = sw->private_data; return helper_token_match ( tokens, rmpd->cmd_list[index] ); } -static char *script_get_message ( const Mode *sw ) -{ - ScriptModePrivateData *pd = (ScriptModePrivateData *) mode_get_private_data ( sw ); - if ( pd->message ) { - return g_strdup ( pd->message ); - } - return NULL; -} #include "mode-private.h" Mode *script_switcher_parse_setup ( const char *str ) @@ -281,10 +198,9 @@ Mode *script_switcher_parse_setup ( const char *str ) sw->_result = script_mode_result; sw->_destroy = script_mode_destroy; sw->_token_match = script_token_match; - sw->_get_completion = NULL; - sw->_preprocess_input = NULL; + sw->_get_completion = NULL, + sw->_preprocess_input = NULL, sw->_get_display_value = _get_display_value; - sw->_get_message = script_get_message; return sw; }