rofi  1.6.1
script.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2020 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 
29 #define G_LOG_DOMAIN "Dialogs.Script"
30 
31 #include <config.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <strings.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <assert.h>
39 #include <errno.h>
40 #include "rofi.h"
41 #include "dialogs/script.h"
42 #include "helper.h"
43 
44 #include "widgets/textbox.h"
45 
46 #include "mode-private.h"
47 
48 #include "rofi-icon-fetcher.h"
49 
51 
52 typedef struct
53 {
55  unsigned int id;
59  unsigned int cmd_list_length;
60 
63  unsigned int num_urgent_list;
66  unsigned int num_active_list;
68  char *message;
69  char *prompt;
70  gboolean do_markup;
71  char delim;
73  gboolean no_custom;
75 
79 void dmenuscript_parse_entry_extras ( G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, size_t length )
80 {
81  gchar **extras = g_strsplit ( buffer, "\x1f", -1 );
82  gchar **extra;
83  for ( extra = extras; *extra != NULL && *( extra + 1 ) != NULL; extra += 2 ) {
84  gchar *key = *extra;
85  gchar *value = *( extra + 1 );
86  if ( strcasecmp ( key, "icon" ) == 0 ) {
87  entry->icon_name = value;
88  }
89  else if ( strcasecmp ( key, "meta" ) == 0 ) {
90  entry->meta = value;
91  }
92  else if ( strcasecmp ( key, "info" ) == 0 ) {
93  entry->info = value;
94  }
95  else if ( strcasecmp ( key, "nonselectable" ) == 0 ) {
96  entry->nonselectable = strcasecmp ( value, "true" ) == 0;
97  g_free ( value );
98  }
99  else {
100  g_free ( value );
101  }
102  g_free ( key );
103  }
104  g_free ( extras );
105 }
106 
111 static void parse_header_entry ( Mode *sw, char *line, ssize_t length )
112 {
114  ssize_t length_key = 0;//strlen ( line );
115  while ( length_key < length && line[length_key] != '\x1f' ) {
116  length_key++;
117  }
118 
119  if ( ( length_key + 1 ) < length ) {
120  line[length_key] = '\0';
121  char *value = line + length_key + 1;
122  if ( strcasecmp ( line, "message" ) == 0 ) {
123  g_free ( pd->message );
124  pd->message = strlen ( value ) ? g_strdup ( value ) : NULL;
125  }
126  else if ( strcasecmp ( line, "prompt" ) == 0 ) {
127  g_free ( pd->prompt );
128  pd->prompt = g_strdup ( value );
129  sw->display_name = pd->prompt;
130  }
131  else if ( strcasecmp ( line, "markup-rows" ) == 0 ) {
132  pd->do_markup = ( strcasecmp ( value, "true" ) == 0 );
133  }
134  else if ( strcasecmp ( line, "urgent" ) == 0 ) {
135  parse_ranges ( value, &( pd->urgent_list ), &( pd->num_urgent_list ) );
136  }
137  else if ( strcasecmp ( line, "active" ) == 0 ) {
138  parse_ranges ( value, &( pd->active_list ), &( pd->num_active_list ) );
139  }
140  else if ( strcasecmp ( line, "delim" ) == 0 ) {
141  pd->delim = helper_parse_char ( value );
142  }
143  else if ( strcasecmp ( line, "no-custom" ) == 0 ) {
144  pd->no_custom = ( strcasecmp ( value, "true" ) == 0 );
145  }
146  }
147 }
148 
149 static DmenuScriptEntry *execute_executor ( Mode *sw, char *arg, unsigned int *length, int value, DmenuScriptEntry *entry )
150 {
152  int fd = -1;
153  GError *error = NULL;
154  DmenuScriptEntry *retv = NULL;
155  char **argv = NULL;
156  int argc = 0;
157  *length = 0;
158 
159  // Environment
160  char ** env = g_get_environ ();
161 
162  char *str_value = g_strdup_printf ( "%d", value );
163  env = g_environ_setenv ( env, "ROFI_RETV", str_value, TRUE );
164  g_free ( str_value );
165 
166  str_value = g_strdup_printf ( "%d", (int) getpid () );
167  env = g_environ_setenv ( env, "ROFI_OUTSIDE", str_value, TRUE );
168  g_free ( str_value );
169 
170  if ( entry && entry->info ) {
171  env = g_environ_setenv ( env, "ROFI_INFO", entry->info, TRUE );
172  }
173 
174  if ( g_shell_parse_argv ( sw->ed, &argc, &argv, &error ) ) {
175  argv = g_realloc ( argv, ( argc + 2 ) * sizeof ( char* ) );
176  argv[argc] = g_strdup ( arg );
177  argv[argc + 1] = NULL;
178  g_spawn_async_with_pipes ( NULL, argv, env, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
179  }
180  g_strfreev ( env );
181  if ( error != NULL ) {
182  char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", (char *) sw->ed, error->message );
183  rofi_view_error_dialog ( msg, FALSE );
184  g_free ( msg );
185  // print error.
186  g_error_free ( error );
187  fd = -1;
188  }
189  if ( fd >= 0 ) {
190  FILE *inp = fdopen ( fd, "r" );
191  if ( inp ) {
192  char *buffer = NULL;
193  size_t buffer_length = 0;
194  ssize_t read_length = 0;
195  size_t actual_size = 0;
196  while ( ( read_length = getdelim ( &buffer, &buffer_length, pd->delim, inp ) ) > 0 ) {
197  // Filter out line-end.
198  if ( buffer[read_length - 1] == pd->delim ) {
199  buffer[read_length - 1] = '\0';
200  }
201  if ( buffer[0] == '\0' ) {
202  parse_header_entry ( sw, &buffer[1], read_length - 1 );
203  }
204  else {
205  if ( actual_size < ( ( *length ) + 2 ) ) {
206  actual_size += 256;
207  retv = g_realloc ( retv, ( actual_size ) * sizeof ( DmenuScriptEntry ) );
208  }
209  size_t buf_length = strlen ( buffer ) + 1;
210  retv[( *length )].entry = g_memdup ( buffer, buf_length );
211  retv[( *length )].icon_name = NULL;
212  retv[( *length )].meta = NULL;
213  retv[( *length )].info = NULL;
214  retv[( *length )].icon_fetch_uid = 0;
215  retv[( *length )].nonselectable = FALSE;
216  if ( buf_length > 0 && ( read_length > (ssize_t) buf_length ) ) {
217  dmenuscript_parse_entry_extras ( sw, &( retv[( *length )] ), buffer + buf_length, read_length - buf_length );
218  }
219  retv[( *length ) + 1].entry = NULL;
220  ( *length )++;
221  }
222  }
223  if ( buffer ) {
224  free ( buffer );
225  }
226  if ( fclose ( inp ) != 0 ) {
227  g_warning ( "Failed to close stdout off executor script: '%s'",
228  g_strerror ( errno ) );
229  }
230  }
231  }
232  g_strfreev ( argv );
233  return retv;
234 }
235 
236 static void script_switcher_free ( Mode *sw )
237 {
238  if ( sw == NULL ) {
239  return;
240  }
241  g_free ( sw->name );
242  g_free ( sw->ed );
243  g_free ( sw );
244 }
245 
246 static int script_mode_init ( Mode *sw )
247 {
248  if ( sw->private_data == NULL ) {
249  ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
250  pd->delim = '\n';
251  sw->private_data = (void *) pd;
252  pd->cmd_list = execute_executor ( sw, NULL, &( pd->cmd_list_length ), 0, NULL );
253  }
254  return TRUE;
255 }
256 static unsigned int script_mode_get_num_entries ( const Mode *sw )
257 {
258  const ScriptModePrivateData *rmpd = (const ScriptModePrivateData *) sw->private_data;
259  return rmpd->cmd_list_length;
260 }
261 
263 {
265 
266  rmpd->num_urgent_list = 0;
267  g_free ( rmpd->urgent_list );
268  rmpd->urgent_list = NULL;
269  rmpd->num_active_list = 0;
270  g_free ( rmpd->active_list );
271  rmpd->active_list = NULL;
272 }
273 
274 static ModeMode script_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
275 {
277  ModeMode retv = MODE_EXIT;
278  DmenuScriptEntry *new_list = NULL;
279  unsigned int new_length = 0;
280 
281  if ( ( mretv & MENU_CUSTOM_COMMAND ) ) {
282  //retv = 1+( mretv & MENU_LOWER_MASK );
284  if ( selected_line != UINT32_MAX ) {
285  new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length, 10 + ( mretv & MENU_LOWER_MASK ), &( rmpd->cmd_list[selected_line] ) );
286  }
287  else {
288  if ( rmpd->no_custom == FALSE ) {
289  new_list = execute_executor ( sw, *input, &new_length, 10 + ( mretv & MENU_LOWER_MASK ), NULL );
290  }
291  else {
292  return RELOAD_DIALOG;
293  }
294  }
295  }
296  else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) {
297  if ( rmpd->cmd_list[selected_line].nonselectable ) {
298  return RELOAD_DIALOG;
299  }
301  new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length, 1, &( rmpd->cmd_list[selected_line] ) );
302  }
303  else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
304  if ( rmpd->no_custom == FALSE ) {
306  new_list = execute_executor ( sw, *input, &new_length, 2, NULL );
307  }
308  else {
309  return RELOAD_DIALOG;
310  }
311  }
312 
313  // If a new list was generated, use that an loop around.
314  if ( new_list != NULL ) {
315  for ( unsigned int i = 0; i < rmpd->cmd_list_length; i++ ) {
316  g_free ( rmpd->cmd_list[i].entry );
317  g_free ( rmpd->cmd_list[i].icon_name );
318  g_free ( rmpd->cmd_list[i].meta );
319  }
320  g_free ( rmpd->cmd_list );
321 
322  rmpd->cmd_list = new_list;
323  rmpd->cmd_list_length = new_length;
324  retv = RESET_DIALOG;
325  }
326  return retv;
327 }
328 
329 static void script_mode_destroy ( Mode *sw )
330 {
332  if ( rmpd != NULL ) {
333  for ( unsigned int i = 0; i < rmpd->cmd_list_length; i++ ) {
334  g_free ( rmpd->cmd_list[i].entry );
335  g_free ( rmpd->cmd_list[i].icon_name );
336  g_free ( rmpd->cmd_list[i].meta );
337  }
338  g_free ( rmpd->cmd_list );
339  g_free ( rmpd->message );
340  g_free ( rmpd->prompt );
341  g_free ( rmpd->urgent_list );
342  g_free ( rmpd->active_list );
343  g_free ( rmpd );
344  sw->private_data = NULL;
345  }
346 }
347 static inline unsigned int get_index ( unsigned int length, int index )
348 {
349  if ( index >= 0 ) {
350  return index;
351  }
352  if ( ( (unsigned int) -index ) <= length ) {
353  return length + index;
354  }
355  // Out of range.
356  return UINT_MAX;
357 }
358 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 )
359 {
361  for ( unsigned int i = 0; i < pd->num_active_list; i++ ) {
362  unsigned int start = get_index ( pd->cmd_list_length, pd->active_list[i].start );
363  unsigned int stop = get_index ( pd->cmd_list_length, pd->active_list[i].stop );
364  if ( selected_line >= start && selected_line <= stop ) {
365  *state |= ACTIVE;
366  }
367  }
368  for ( unsigned int i = 0; i < pd->num_urgent_list; i++ ) {
369  unsigned int start = get_index ( pd->cmd_list_length, pd->urgent_list[i].start );
370  unsigned int stop = get_index ( pd->cmd_list_length, pd->urgent_list[i].stop );
371  if ( selected_line >= start && selected_line <= stop ) {
372  *state |= URGENT;
373  }
374  }
375  if ( pd->do_markup ) {
376  *state |= MARKUP;
377  }
378  return get_entry ? g_strdup ( pd->cmd_list[selected_line].entry ) : NULL;
379 }
380 
381 static int script_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
382 {
384  int match = 1;
385  if ( tokens ) {
386  for ( int j = 0; match && tokens != NULL && tokens[j] != NULL; j++ ) {
387  rofi_int_matcher *ftokens[2] = { tokens[j], NULL };
388  int test = 0;
389  test = helper_token_match ( ftokens, rmpd->cmd_list[index].entry );
390  if ( test == tokens[j]->invert && rmpd->cmd_list[index].meta ) {
391  test = helper_token_match ( ftokens, rmpd->cmd_list[index].meta );
392  }
393 
394  if ( test == 0 ) {
395  match = 0;
396  }
397  }
398  }
399  return match;
400 }
401 static char *script_get_message ( const Mode *sw )
402 {
404  return g_strdup ( pd->message );
405 }
406 static cairo_surface_t *script_get_icon ( const Mode *sw, unsigned int selected_line, int height )
407 {
409  g_return_val_if_fail ( pd->cmd_list != NULL, NULL );
410  DmenuScriptEntry *dr = &( pd->cmd_list[selected_line] );
411  if ( dr->icon_name == NULL ) {
412  return NULL;
413  }
414  if ( dr->icon_fetch_uid > 0 ) {
415  return rofi_icon_fetcher_get ( dr->icon_fetch_uid );
416  }
417  dr->icon_fetch_uid = rofi_icon_fetcher_query ( dr->icon_name, height );
418  return rofi_icon_fetcher_get ( dr->icon_fetch_uid );
419 }
420 
421 #include "mode-private.h"
422 Mode *script_switcher_parse_setup ( const char *str )
423 {
424  Mode *sw = g_malloc0 ( sizeof ( *sw ) );
425  char *endp = NULL;
426  char *parse = g_strdup ( str );
427  unsigned int index = 0;
428  const char *const sep = ":";
429  for ( char *token = strtok_r ( parse, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
430  if ( index == 0 ) {
431  sw->name = g_strdup ( token );
432  }
433  else if ( index == 1 ) {
434  sw->ed = (void *) rofi_expand_path ( token );
435  }
436  index++;
437  }
438  g_free ( parse );
439  if ( index == 2 ) {
441  sw->_init = script_mode_init;
448  sw->_get_completion = NULL,
449  sw->_preprocess_input = NULL,
451 
452  return sw;
453  }
454  fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.", str, index );
455  script_switcher_free ( sw );
456  return NULL;
457 }
458 
459 gboolean script_switcher_is_valid ( const char *token )
460 {
461  return strchr ( token, ':' ) != NULL;
462 }
RESET_DIALOG
@ RESET_DIALOG
Definition: mode.h:60
rofi_range_pair
Definition: rofi-types.h:260
DmenuScriptEntry::nonselectable
gboolean nonselectable
Definition: dmenuscriptshared.h:19
ACTIVE
@ ACTIVE
Definition: textbox.h:110
script_mode_result
static ModeMode script_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
Definition: script.c:274
rofi_mode::name
char * name
Definition: mode-private.h:156
ScriptModePrivateData::num_active_list
unsigned int num_active_list
Definition: script.c:66
DmenuScriptEntry::info
char * info
Definition: dmenuscriptshared.h:16
MARKUP
@ MARKUP
Definition: textbox.h:114
helper_parse_char
char helper_parse_char(const char *arg)
Definition: helper.c:362
rofi_mode::_init
__mode_init _init
Definition: mode-private.h:164
MENU_OK
@ MENU_OK
Definition: mode.h:69
rofi_int_matcher_t
Definition: rofi-types.h:269
DmenuScriptEntry::icon_fetch_uid
uint32_t icon_fetch_uid
Definition: dmenuscriptshared.h:11
rofi_mode::ed
void * ed
Definition: mode-private.h:194
_get_display_value
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:358
rofi_view_error_dialog
int rofi_view_error_dialog(const char *msg, int markup)
Definition: view.c:1898
ScriptModePrivateData::do_markup
gboolean do_markup
Definition: script.c:70
rofi_mode::_token_match
_mode_token_match _token_match
Definition: mode-private.h:172
rofi_range_pair::start
int start
Definition: rofi-types.h:261
script_get_icon
static cairo_surface_t * script_get_icon(const Mode *sw, unsigned int selected_line, int height)
Definition: script.c:406
ScriptModePrivateData::id
unsigned int id
Definition: script.c:55
rofi_mode::_get_icon
_mode_get_icon _get_icon
Definition: mode-private.h:176
rofi_mode::_preprocess_input
_mode_preprocess_input _preprocess_input
Definition: mode-private.h:180
script_switcher_free
static void script_switcher_free(Mode *sw)
Definition: script.c:236
ScriptModePrivateData::message
char * message
Definition: script.c:68
script_mode_get_num_entries
static unsigned int script_mode_get_num_entries(const Mode *sw)
Definition: script.c:256
mode-private.h
DmenuScriptEntry::meta
char * meta
Definition: dmenuscriptshared.h:13
ScriptModePrivateData::active_list
struct rofi_range_pair * active_list
Definition: script.c:65
script_switcher_parse_setup
Mode * script_switcher_parse_setup(const char *str)
Definition: script.c:422
DmenuScriptEntry::icon_name
char * icon_name
Definition: dmenuscriptshared.h:9
MODE_EXIT
@ MODE_EXIT
Definition: mode.h:52
ScriptModePrivateData
Definition: script.c:53
ScriptModePrivateData::prompt
char * prompt
Definition: script.c:69
dmenuscriptshared.h
execute_executor
static DmenuScriptEntry * execute_executor(Mode *sw, char *arg, unsigned int *length, int value, DmenuScriptEntry *entry)
Definition: script.c:149
rofi_icon_fetcher_get
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
Definition: rofi-icon-fetcher.c:360
rofi_mode::free
_mode_free free
Definition: mode-private.h:192
rofi_range_pair::stop
int stop
Definition: rofi-types.h:262
rofi_expand_path
char * rofi_expand_path(const char *input)
Definition: helper.c:711
URGENT
@ URGENT
Definition: textbox.h:108
rofi_mode
Definition: mode-private.h:152
DmenuScriptEntry::entry
char * entry
Definition: dmenuscriptshared.h:7
rofi.h
rofi_mode::private_data
void * private_data
Definition: mode-private.h:185
ScriptModePrivateData::urgent_list
struct rofi_range_pair * urgent_list
Definition: script.c:62
ScriptModePrivateData::delim
char delim
Definition: script.c:71
rofi_mode::_destroy
__mode_destroy _destroy
Definition: mode-private.h:166
MENU_CUSTOM_INPUT
@ MENU_CUSTOM_INPUT
Definition: mode.h:75
rofi_mode::display_name
char * display_name
Definition: mode-private.h:158
mode_get_private_data
void * mode_get_private_data(const Mode *mode)
Definition: mode.c:139
dmenuscript_parse_entry_extras
void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, size_t length)
Definition: script.c:79
parse_ranges
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
Definition: helper.c:1170
script_mode_destroy
static void script_mode_destroy(Mode *sw)
Definition: script.c:329
rofi_mode::_get_display_value
_mode_get_display_value _get_display_value
Definition: mode-private.h:174
script_switcher_is_valid
gboolean script_switcher_is_valid(const char *token)
Definition: script.c:459
rofi_icon_fetcher_query
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
Definition: rofi-icon-fetcher.c:326
script_mode_init
static int script_mode_init(Mode *sw)
Definition: script.c:246
helper_token_match
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition: helper.c:488
get_index
static unsigned int get_index(unsigned int length, int index)
Definition: script.c:347
textbox.h
rofi-icon-fetcher.h
script.h
script_token_match
static int script_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
Definition: script.c:381
ModeMode
ModeMode
Definition: mode.h:50
DmenuScriptEntry
Definition: dmenuscriptshared.h:5
MENU_CUSTOM_COMMAND
@ MENU_CUSTOM_COMMAND
Definition: mode.h:81
RELOAD_DIALOG
@ RELOAD_DIALOG
Definition: mode.h:56
parse_header_entry
static void parse_header_entry(Mode *sw, char *line, ssize_t length)
Definition: script.c:111
ScriptModePrivateData::no_custom
gboolean no_custom
Definition: script.c:73
script_mode_reset_highlight
static void script_mode_reset_highlight(Mode *sw)
Definition: script.c:262
MENU_LOWER_MASK
@ MENU_LOWER_MASK
Definition: mode.h:87
helper.h
ScriptModePrivateData::cmd_list_length
unsigned int cmd_list_length
Definition: script.c:59
ScriptModePrivateData::num_urgent_list
unsigned int num_urgent_list
Definition: script.c:63
script_get_message
static char * script_get_message(const Mode *sw)
Definition: script.c:401
rofi_mode::_result
_mode_result _result
Definition: mode-private.h:170
rofi_mode::_get_completion
_mode_get_completion _get_completion
Definition: mode-private.h:178
rofi_mode::_get_message
_mode_get_message _get_message
Definition: mode-private.h:182
ScriptModePrivateData::cmd_list
DmenuScriptEntry * cmd_list
Definition: script.c:57
rofi_mode::_get_num_entries
__mode_get_num_entries _get_num_entries
Definition: mode-private.h:168