1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/**
* 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.
*
*/
#include <config.h>
#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"
#include "helper.h"
pid_t execute_generator ( char * cmd )
{
char **args = NULL;
int argv = 0;
helper_parse_setup ( config.run_command, &args, &argv, "{cmd}", cmd, NULL );
int fd = -1;
GError *error = NULL;
g_spawn_async_with_pipes ( NULL,
args,
NULL,
G_SPAWN_SEARCH_PATH,
NULL,
NULL,
NULL,
NULL, &fd, NULL,
&error );
if ( error != NULL ) {
char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", cmd,
error->message );
error_dialog ( msg );
g_free ( msg );
// print error.
g_error_free ( error );
}
g_strfreev ( args );
return fd;
}
static char **get_script_output ( char *command, unsigned int *length )
{
char **retv = NULL;
*length = 0;
int fd = execute_generator ( command );
if ( fd >= 0 ) {
FILE *inp = fdopen ( fd, "r" );
if ( inp ) {
char buffer[1024];
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 );
}
}
return retv;
}
char **execute_executor ( ScriptOptions *options, const char *result, unsigned int *length )
{
char **retv = NULL;
char *command = g_strdup_printf ( "%s '%s'", options->script_path, result );
retv = get_script_output ( command, length );
g_free ( command );
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 );
char *prompt = g_strdup_printf ( "%s:", options->name );
do {
unsigned int new_length = 0;
char **new_list = NULL;
int mretv = menu ( list, length, input, prompt, NULL, NULL,
token_match, NULL, &selected_line, FALSE );
if ( mretv == MENU_NEXT ) {
retv = NEXT_DIALOG;
}
else if ( mretv == MENU_QUICK_SWITCH ) {
retv = selected_line;
}
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.
g_strfreev ( list );
list = NULL;
// If a new list was generated, use that an loop around.
if ( new_list != NULL ) {
list = new_list;
length = new_length;
g_free ( *input );
*input = NULL;
}
} while ( list != NULL );
g_free ( prompt );
return retv;
}
void script_switcher_free_options ( ScriptOptions *sw )
{
if ( sw == NULL ) {
return;
}
g_free ( sw->name );
g_free ( sw->script_path );
g_free ( sw );
}
ScriptOptions *script_switcher_parse_setup ( const char *str )
{
ScriptOptions *sw = g_malloc0 ( sizeof ( *sw ) );
char *endp = NULL;
char *parse = g_strdup ( str );
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 ) {
sw->name = g_strdup ( token );
}
else if ( index == 1 ) {
sw->script_path = g_strdup ( token );
}
index++;
}
g_free ( parse );
if ( index == 2 ) {
return sw;
}
fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.\n",
str, index );
script_switcher_free_options ( sw );
return NULL;
}
|