rofi  1.5.1
script.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27 
28 #define G_LOG_DOMAIN "Dialogs.Script"
29 
30 #include <config.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <strings.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <assert.h>
38 #include <errno.h>
39 #include "rofi.h"
40 #include "dialogs/script.h"
41 #include "helper.h"
42 
43 #include "widgets/textbox.h"
44 
45 #include "mode-private.h"
46 
47 typedef struct
48 {
50  unsigned int id;
52  char **cmd_list;
54  unsigned int cmd_list_length;
55 
58  unsigned int num_urgent_list;
61  unsigned int num_active_list;
63  char *message;
64  char *prompt;
65  gboolean do_markup;
67 
68 static void parse_header_entry ( Mode *sw, char *line, ssize_t length )
69 {
71  ssize_t length_key = 0;//strlen ( line );
72  while ( length_key <= length && line[length_key] != '\x1f' ) {
73  length_key++;
74  }
75 
76  if ( length_key < length ) {
77  line[length_key] = '\0';
78  char *value = line + length_key + 1;
79  if ( strcasecmp ( line, "message" ) == 0 ) {
80  g_free ( pd->message );
81  pd->message = strlen(value)? g_strdup ( value ):NULL;
82  }
83  else if ( strcasecmp ( line, "prompt" ) == 0 ) {
84  g_free ( pd->prompt );
85  pd->prompt = g_strdup ( value );
86  sw->display_name = pd->prompt;
87  }
88  else if ( strcasecmp ( line, "markup-rows" ) == 0 ) {
89  pd->do_markup = ( strcasecmp ( value, "true" ) == 0 );
90  }
91  else if ( strcasecmp ( line, "urgent" ) == 0 ) {
92  parse_ranges ( value, &( pd->urgent_list ), &( pd->num_urgent_list ) );
93  }
94  else if ( strcasecmp ( line, "active" ) == 0 ) {
95  parse_ranges ( value, &( pd->active_list ), &( pd->num_active_list ) );
96  }
97  }
98 }
99 
100 static char **get_script_output ( Mode *sw, char *command, char *arg, unsigned int *length )
101 {
102  int fd = -1;
103  GError *error = NULL;
104  char **retv = NULL;
105  char **argv = NULL;
106  int argc = 0;
107  *length = 0;
108  if ( g_shell_parse_argv ( command, &argc, &argv, &error ) ) {
109  argv = g_realloc ( argv, ( argc + 2 ) * sizeof ( char* ) );
110  argv[argc] = g_strdup ( arg );
111  argv[argc + 1] = NULL;
112  g_spawn_async_with_pipes ( NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
113  }
114  if ( error != NULL ) {
115  char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", command, error->message );
116  rofi_view_error_dialog ( msg, FALSE );
117  g_free ( msg );
118  // print error.
119  g_error_free ( error );
120  fd = -1;
121  }
122  if ( fd >= 0 ) {
123  FILE *inp = fdopen ( fd, "r" );
124  if ( inp ) {
125  char *buffer = NULL;
126  size_t buffer_length = 0;
127  ssize_t read_length = 0;
128  size_t actual_size = 0;
129  while ( ( read_length = getline ( &buffer, &buffer_length, inp ) ) > 0 ) {
130  // Filter out line-end.
131  if ( buffer[read_length - 1] == '\n' ) {
132  buffer[read_length - 1] = '\0';
133  }
134  if ( buffer[0] == '\0' ) {
135  parse_header_entry ( sw, &buffer[1], read_length - 1 );
136  }
137  else {
138  if ( actual_size < ( ( *length ) + 2 ) ) {
139  actual_size += 256;
140  retv = g_realloc ( retv, ( actual_size ) * sizeof ( char* ) );
141  }
142  retv[( *length )] = g_strdup ( buffer );
143  retv[( *length ) + 1] = NULL;
144  ( *length )++;
145  }
146  }
147  if ( buffer ) {
148  free ( buffer );
149  }
150  if ( fclose ( inp ) != 0 ) {
151  g_warning ( "Failed to close stdout off executor script: '%s'",
152  g_strerror ( errno ) );
153  }
154  }
155  }
156  return retv;
157 }
158 
159 static char **execute_executor ( Mode *sw, char *result, unsigned int *length )
160 {
161  char **retv = get_script_output ( sw, sw->ed, result, length );
162  return retv;
163 }
164 
165 static void script_switcher_free ( Mode *sw )
166 {
167  if ( sw == NULL ) {
168  return;
169  }
170  g_free ( sw->name );
171  g_free ( sw->ed );
172  g_free ( sw );
173 }
174 
175 static int script_mode_init ( Mode *sw )
176 {
177  if ( sw->private_data == NULL ) {
178  ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
179  sw->private_data = (void *) pd;
180  pd->cmd_list = get_script_output ( sw, (char *) sw->ed, NULL, &( pd->cmd_list_length ) );
181  }
182  return TRUE;
183 }
184 static unsigned int script_mode_get_num_entries ( const Mode *sw )
185 {
186  const ScriptModePrivateData *rmpd = (const ScriptModePrivateData *) sw->private_data;
187  return rmpd->cmd_list_length;
188 }
189 
191 {
193 
194  rmpd->num_urgent_list = 0;
195  g_free ( rmpd->urgent_list );
196  rmpd->urgent_list = NULL;
197  rmpd->num_active_list = 0;
198  g_free ( rmpd->active_list );
199  rmpd->active_list = NULL;
200 
201 }
202 
203 static ModeMode script_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
204 {
206  ModeMode retv = MODE_EXIT;
207  char **new_list = NULL;
208  unsigned int new_length = 0;
209 
210  if ( ( mretv & MENU_NEXT ) ) {
211  retv = NEXT_DIALOG;
212  }
213  else if ( ( mretv & MENU_PREVIOUS ) ) {
214  retv = PREVIOUS_DIALOG;
215  }
216  else if ( ( mretv & MENU_QUICK_SWITCH ) ) {
217  retv = ( mretv & MENU_LOWER_MASK );
218  }
219  else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
221  new_list = execute_executor ( sw, rmpd->cmd_list[selected_line], &new_length );
222  }
223  else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
225  new_list = execute_executor ( sw, *input, &new_length );
226  }
227 
228  // If a new list was generated, use that an loop around.
229  if ( new_list != NULL ) {
230  g_strfreev ( rmpd->cmd_list );
231 
232  rmpd->cmd_list = new_list;
233  rmpd->cmd_list_length = new_length;
234  retv = RESET_DIALOG;
235  }
236  return retv;
237 }
238 
239 static void script_mode_destroy ( Mode *sw )
240 {
242  if ( rmpd != NULL ) {
243  g_strfreev ( rmpd->cmd_list );
244  g_free ( rmpd->message );
245  g_free ( rmpd->prompt );
246  g_free ( rmpd->urgent_list );
247  g_free ( rmpd->active_list );
248  g_free ( rmpd );
249  sw->private_data = NULL;
250  }
251 }
252 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 )
253 {
255  for ( unsigned int i = 0; i < pd->num_active_list; i++ ) {
256  if ( selected_line >= pd->active_list[i].start && selected_line <= pd->active_list[i].stop ) {
257  *state |= ACTIVE;
258  }
259  }
260  for ( unsigned int i = 0; i < pd->num_urgent_list; i++ ) {
261  if ( selected_line >= pd->urgent_list[i].start && selected_line <= pd->urgent_list[i].stop ) {
262  *state |= URGENT;
263  }
264  }
265  if ( pd->do_markup ) {
266  *state |= MARKUP;
267  }
268  return get_entry ? g_strdup ( pd->cmd_list[selected_line] ) : NULL;
269 }
270 
271 static int script_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
272 {
274  return helper_token_match ( tokens, rmpd->cmd_list[index] );
275 }
276 static char *script_get_message ( const Mode *sw )
277 {
279  return g_strdup ( pd->message );
280 }
281 
282 #include "mode-private.h"
283 Mode *script_switcher_parse_setup ( const char *str )
284 {
285  Mode *sw = g_malloc0 ( sizeof ( *sw ) );
286  char *endp = NULL;
287  char *parse = g_strdup ( str );
288  unsigned int index = 0;
289  const char *const sep = ":";
290  for ( char *token = strtok_r ( parse, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
291  if ( index == 0 ) {
292  sw->name = g_strdup ( token );
293  }
294  else if ( index == 1 ) {
295  sw->ed = (void *) rofi_expand_path ( token );
296  }
297  index++;
298  }
299  g_free ( parse );
300  if ( index == 2 ) {
302  sw->_init = script_mode_init;
308  sw->_get_completion = NULL,
309  sw->_preprocess_input = NULL,
311 
312  return sw;
313  }
314  fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.", str, index );
315  script_switcher_free ( sw );
316  return NULL;
317 }
318 
319 gboolean script_switcher_is_valid ( const char *token )
320 {
321  return strchr ( token, ':' ) != NULL;
322 }
_mode_get_message _get_message
Definition: mode-private.h:182
unsigned int id
Definition: script.c:50
static int script_mode_init(Mode *sw)
Definition: script.c:175
_mode_token_match _token_match
Definition: mode-private.h:172
Definition: mode.h:69
__mode_init _init
Definition: mode-private.h:164
unsigned int num_urgent_list
Definition: script.c:58
char ** cmd_list
Definition: script.c:52
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)
Definition: script.c:252
static void script_switcher_free(Mode *sw)
Definition: script.c:165
ModeMode
Definition: mode.h:49
void * ed
Definition: mode-private.h:194
Definition: mode.h:52
static unsigned int script_mode_get_num_entries(const Mode *sw)
Definition: script.c:184
unsigned int num_active_list
Definition: script.c:61
_mode_free free
Definition: mode-private.h:192
_mode_preprocess_input _preprocess_input
Definition: mode-private.h:180
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition: helper.c:473
unsigned int cmd_list_length
Definition: script.c:54
static char ** get_script_output(Mode *sw, char *command, char *arg, unsigned int *length)
Definition: script.c:100
void * private_data
Definition: mode-private.h:185
int rofi_view_error_dialog(const char *msg, int markup)
Definition: view.c:1726
gboolean do_markup
Definition: script.c:65
static void script_mode_destroy(Mode *sw)
Definition: script.c:239
Definition: textbox.h:98
gboolean script_switcher_is_valid(const char *token)
Definition: script.c:319
char * rofi_expand_path(const char *input)
Definition: helper.c:669
static void parse_header_entry(Mode *sw, char *line, ssize_t length)
Definition: script.c:68
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
Definition: helper.c:1148
Mode * script_switcher_parse_setup(const char *str)
Definition: script.c:283
char * display_name
Definition: mode-private.h:158
struct rofi_range_pair * active_list
Definition: script.c:60
unsigned int start
Definition: rofi-types.h:225
static int script_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
Definition: script.c:271
Definition: mode.h:73
static ModeMode script_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
Definition: script.c:203
_mode_result _result
Definition: mode-private.h:170
__mode_destroy _destroy
Definition: mode-private.h:166
_mode_get_display_value _get_display_value
Definition: mode-private.h:174
static char * script_get_message(const Mode *sw)
Definition: script.c:276
static char ** execute_executor(Mode *sw, char *result, unsigned int *length)
Definition: script.c:159
__mode_get_num_entries _get_num_entries
Definition: mode-private.h:168
struct rofi_range_pair * urgent_list
Definition: script.c:57
char * name
Definition: mode-private.h:156
static void script_mode_reset_highlight(Mode *sw)
Definition: script.c:190
_mode_get_completion _get_completion
Definition: mode-private.h:178